Coverage Report

Created: 2025-07-23 07:04

/src/samba/libcli/security/access_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   Unix SMB/CIFS implementation.
3
4
   Copyright (C) Andrew Tridgell 2004
5
   Copyright (C) Gerald Carter 2005
6
   Copyright (C) Volker Lendecke 2007
7
   Copyright (C) Jeremy Allison 2008
8
   Copyright (C) Andrew Bartlett 2010
9
10
   This program is free software; you can redistribute it and/or modify
11
   it under the terms of the GNU General Public License as published by
12
   the Free Software Foundation; either version 3 of the License, or
13
   (at your option) any later version.
14
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
20
   You should have received a copy of the GNU General Public License
21
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
*/
23
24
#include "replace.h"
25
#include "lib/util/debug.h"
26
#include "libcli/security/security.h"
27
#include "librpc/gen_ndr/conditional_ace.h"
28
#include "libcli/security/conditional_ace.h"
29
30
/* Map generic access rights to object specific rights.  This technique is
31
   used to give meaning to assigning read, write, execute and all access to
32
   objects.  Each type of object has its own mapping of generic to object
33
   specific access rights. */
34
35
void se_map_generic(uint32_t *access_mask, const struct generic_mapping *mapping)
36
0
{
37
0
  uint32_t old_mask = *access_mask;
38
39
0
  if (*access_mask & GENERIC_READ_ACCESS) {
40
0
    *access_mask &= ~GENERIC_READ_ACCESS;
41
0
    *access_mask |= mapping->generic_read;
42
0
  }
43
44
0
  if (*access_mask & GENERIC_WRITE_ACCESS) {
45
0
    *access_mask &= ~GENERIC_WRITE_ACCESS;
46
0
    *access_mask |= mapping->generic_write;
47
0
  }
48
49
0
  if (*access_mask & GENERIC_EXECUTE_ACCESS) {
50
0
    *access_mask &= ~GENERIC_EXECUTE_ACCESS;
51
0
    *access_mask |= mapping->generic_execute;
52
0
  }
53
54
0
  if (*access_mask & GENERIC_ALL_ACCESS) {
55
0
    *access_mask &= ~GENERIC_ALL_ACCESS;
56
0
    *access_mask |= mapping->generic_all;
57
0
  }
58
59
0
  if (old_mask != *access_mask) {
60
0
    DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
61
0
         old_mask, *access_mask));
62
0
  }
63
0
}
64
65
/* Map generic access rights to object specific rights for all the ACE's
66
 * in a security_acl.
67
 */
68
69
void security_acl_map_generic(struct security_acl *sa,
70
        const struct generic_mapping *mapping)
71
0
{
72
0
  unsigned int i;
73
74
0
  if (!sa) {
75
0
    return;
76
0
  }
77
78
0
  for (i = 0; i < sa->num_aces; i++) {
79
0
    se_map_generic(&sa->aces[i].access_mask, mapping);
80
0
  }
81
0
}
82
83
/* Map standard access rights to object specific rights.  This technique is
84
   used to give meaning to assigning read, write, execute and all access to
85
   objects.  Each type of object has its own mapping of standard to object
86
   specific access rights. */
87
88
void se_map_standard(uint32_t *access_mask, const struct standard_mapping *mapping)
89
0
{
90
0
  uint32_t old_mask = *access_mask;
91
92
0
  if (*access_mask & SEC_STD_READ_CONTROL) {
93
0
    *access_mask &= ~SEC_STD_READ_CONTROL;
94
0
    *access_mask |= mapping->std_read;
95
0
  }
96
97
0
  if (*access_mask & (SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE)) {
98
0
    *access_mask &= ~(SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE);
99
0
    *access_mask |= mapping->std_all;
100
0
  }
101
102
0
  if (old_mask != *access_mask) {
103
0
    DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
104
0
         old_mask, *access_mask));
105
0
  }
106
0
}
107
108
enum ace_callback_result {
109
  ACE_CALLBACK_DENY,
110
  ACE_CALLBACK_ALLOW,
111
  ACE_CALLBACK_SKIP,      /* do not apply this ACE */
112
  ACE_CALLBACK_INVALID    /* we don't want to process the conditional ACE */
113
};
114
115
116
static enum ace_callback_result check_callback_ace_allow(
117
  const struct security_ace *ace,
118
  const struct security_token *token,
119
  const struct security_descriptor *sd)
120
9.47k
{
121
9.47k
  bool ok;
122
9.47k
  int result;
123
124
9.47k
  switch (token->evaluate_claims) {
125
9.01k
  case CLAIMS_EVALUATION_ALWAYS:
126
9.01k
    break;
127
128
13
  case CLAIMS_EVALUATION_INVALID_STATE:
129
13
    DBG_WARNING("Refusing to evaluate ACL with "
130
13
          "conditional ACE against security "
131
13
          "token with CLAIMS_EVALUATION_INVALID_STATE\n");
132
13
    return ACE_CALLBACK_INVALID;
133
4
  case CLAIMS_EVALUATION_NEVER:
134
441
  default:
135
    /*
136
     * We are asked to pretend we never understood this
137
     * ACE type.
138
     *
139
     * By returning SKIP, this ACE will not adjust any
140
     * permission bits making it an effective no-op, which
141
     * was the default behaviour up to Samba 4.19.
142
     */
143
441
    return ACE_CALLBACK_SKIP;
144
9.47k
  }
145
146
9.01k
  if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK &&
147
9.01k
      ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT) {
148
    /* This indicates a programming error */
149
0
    DBG_ERR("bad conditional allow ACE type: %u\n", ace->type);
150
0
    return ACE_CALLBACK_INVALID;
151
0
  }
152
153
  /*
154
   * Until we discover otherwise, we assume all callback ACEs
155
   * are conditional ACEs.
156
   */
157
9.01k
  ok = access_check_conditional_ace(ace, token, sd, &result);
158
9.01k
  if (!ok) {
159
    /*
160
     * An error in processing the conditional ACE is
161
     * treated as UNKNOWN, which amounts to a DENY/SKIP
162
     * result.
163
     *
164
     * This is different from the INVALID result which
165
     * means we should not be thinking about conditional
166
     * ACES at all, and will abort the whole access check.
167
     */
168
4.41k
    DBG_WARNING("callback ACE was not a valid conditional ACE\n");
169
4.41k
    return ACE_CALLBACK_SKIP;
170
4.41k
  }
171
4.60k
  if (result == ACE_CONDITION_TRUE) {
172
456
    return ACE_CALLBACK_ALLOW;
173
456
  }
174
  /* UNKNOWN means do not allow */
175
4.14k
  return ACE_CALLBACK_SKIP;
176
4.60k
}
177
178
179
static enum ace_callback_result check_callback_ace_deny(
180
  const struct security_ace *ace,
181
  const struct security_token *token,
182
  const struct security_descriptor *sd)
183
32.8k
{
184
32.8k
  bool ok;
185
32.8k
  int result;
186
187
32.8k
  switch (token->evaluate_claims) {
188
31.1k
  case CLAIMS_EVALUATION_ALWAYS:
189
31.1k
    break;
190
191
13
  case CLAIMS_EVALUATION_INVALID_STATE:
192
13
    DBG_WARNING("Refusing to evaluate ACL with "
193
13
          "conditional ACE against security "
194
13
          "token with CLAIMS_EVALUATION_INVALID_STATE\n");
195
13
    return ACE_CALLBACK_INVALID;
196
44
  case CLAIMS_EVALUATION_NEVER:
197
1.66k
  default:
198
    /*
199
     * We are asked to pretend we never understood this
200
     * ACE type.
201
     */
202
1.66k
    return ACE_CALLBACK_SKIP;
203
32.8k
  }
204
205
31.1k
  if (ace->type != SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK &&
206
31.1k
      ace->type != SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT) {
207
0
    DBG_ERR("bad conditional deny ACE type: %u\n", ace->type);
208
0
    return ACE_CALLBACK_INVALID;
209
0
  }
210
211
  /*
212
   * Until we discover otherwise, we assume all callback ACEs
213
   * are conditional ACEs.
214
   */
215
31.1k
  ok = access_check_conditional_ace(ace, token, sd, &result);
216
31.1k
  if (!ok) {
217
    /*
218
     * An error in processing the conditional ACE is
219
     * treated as UNKNOWN, which means DENY.
220
     */
221
14.4k
    DBG_WARNING("callback ACE was not a valid conditional ACE\n");
222
14.4k
    return ACE_CALLBACK_DENY;
223
14.4k
  }
224
16.7k
  if (result != ACE_CONDITION_FALSE) {
225
    /* UNKNOWN means deny */
226
15.1k
    return ACE_CALLBACK_DENY;
227
15.1k
  }
228
1.64k
  return ACE_CALLBACK_SKIP;
229
16.7k
}
230
231
232
/*
233
  perform a SEC_FLAG_MAXIMUM_ALLOWED access check
234
*/
235
static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
236
           const struct security_token *token,
237
           enum implicit_owner_rights implicit_owner_rights)
238
5.60k
{
239
5.60k
  uint32_t denied = 0, granted = 0;
240
5.60k
  bool am_owner = false;
241
5.60k
  bool have_owner_rights_ace = false;
242
5.60k
  unsigned i;
243
244
5.60k
  if (sd->dacl == NULL) {
245
130
    if (security_token_has_sid(token, sd->owner_sid)) {
246
9
      switch (implicit_owner_rights) {
247
2
      case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
248
2
        granted |= (SEC_STD_READ_CONTROL |
249
2
              SEC_STD_WRITE_DAC);
250
2
        break;
251
7
      case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
252
7
        granted |= SEC_STD_READ_CONTROL;
253
7
        break;
254
9
      }
255
9
    }
256
130
    return granted;
257
130
  }
258
259
5.47k
  if (security_token_has_sid(token, sd->owner_sid)) {
260
    /*
261
     * Check for explicit owner rights: if there are none, we remove
262
     * the default owner right SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL
263
     * from remaining_access. Otherwise we just process the
264
     * explicitly granted rights when processing the ACEs.
265
     */
266
2.16k
    am_owner = true;
267
268
83.3k
    for (i=0; i < sd->dacl->num_aces; i++) {
269
81.1k
      struct security_ace *ace = &sd->dacl->aces[i];
270
271
81.1k
      if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
272
4.31k
        continue;
273
4.31k
      }
274
275
76.8k
      have_owner_rights_ace = dom_sid_equal(
276
76.8k
        &ace->trustee, &global_sid_Owner_Rights);
277
76.8k
      if (have_owner_rights_ace) {
278
13
        break;
279
13
      }
280
76.8k
    }
281
2.16k
  }
282
283
5.47k
  if (am_owner && !have_owner_rights_ace) {
284
2.15k
    switch (implicit_owner_rights) {
285
1.81k
    case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
286
1.81k
      granted |= (SEC_STD_READ_CONTROL | SEC_STD_WRITE_DAC);
287
1.81k
      break;
288
340
    case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
289
340
      granted |= SEC_STD_READ_CONTROL;
290
340
      break;
291
2.15k
    }
292
2.15k
  }
293
294
176k
  for (i = 0;i<sd->dacl->num_aces; i++) {
295
170k
    struct security_ace *ace = &sd->dacl->aces[i];
296
170k
    bool is_owner_rights_ace = false;
297
298
170k
    if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
299
6.75k
      continue;
300
6.75k
    }
301
302
163k
    if (am_owner) {
303
76.8k
      is_owner_rights_ace = dom_sid_equal(
304
76.8k
        &ace->trustee, &global_sid_Owner_Rights);
305
76.8k
    }
306
307
163k
    if (!is_owner_rights_ace &&
308
163k
        !security_token_has_sid(token, &ace->trustee))
309
95.3k
    {
310
95.3k
      continue;
311
95.3k
    }
312
313
68.4k
    switch (ace->type) {
314
33.4k
    case SEC_ACE_TYPE_ACCESS_ALLOWED:
315
33.4k
      granted |= ace->access_mask;
316
33.4k
      break;
317
4.85k
    case SEC_ACE_TYPE_ACCESS_DENIED:
318
5.60k
    case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
319
5.60k
      denied |= ~granted & ace->access_mask;
320
5.60k
      break;
321
322
2.01k
    case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
323
2.01k
    {
324
2.01k
      enum ace_callback_result allow =
325
2.01k
        check_callback_ace_allow(ace, token, sd);
326
2.01k
      if (allow == ACE_CALLBACK_INVALID) {
327
3
        return 0;
328
3
      }
329
2.01k
      if (allow == ACE_CALLBACK_ALLOW) {
330
180
        granted |= ace->access_mask;
331
180
      }
332
2.01k
      break;
333
2.01k
    }
334
335
14.8k
    case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
336
14.8k
    {
337
14.8k
      enum ace_callback_result deny =
338
14.8k
        check_callback_ace_deny(ace, token, sd);
339
14.8k
      if (deny == ACE_CALLBACK_INVALID) {
340
6
        return 0;
341
6
      }
342
14.8k
      if (deny == ACE_CALLBACK_DENY) {
343
13.3k
        denied |= ~granted & ace->access_mask;
344
13.3k
      }
345
14.8k
      break;
346
14.8k
    }
347
348
12.4k
    default:  /* Other ACE types not handled/supported */
349
12.4k
      break;
350
68.4k
    }
351
68.4k
  }
352
353
5.46k
  return granted & ~denied;
354
5.47k
}
355
356
357
358
static NTSTATUS se_access_check_implicit_owner(const struct security_descriptor *sd,
359
                 const struct security_token *token,
360
                 uint32_t access_desired,
361
                 uint32_t *access_granted,
362
                 enum implicit_owner_rights implicit_owner_rights)
363
5.55k
{
364
5.55k
  uint32_t i;
365
5.55k
  uint32_t bits_remaining;
366
5.55k
  uint32_t explicitly_denied_bits = 0;
367
5.55k
  bool am_owner = false;
368
5.55k
  bool have_owner_rights_ace = false;
369
370
5.55k
  switch (token->evaluate_claims) {
371
714
  case CLAIMS_EVALUATION_INVALID_STATE:
372
714
    if (token->num_local_claims > 0 ||
373
714
        token->num_user_claims > 0 ||
374
714
        token->num_device_claims > 0 ||
375
714
        token->num_device_sids > 0) {
376
589
      DBG_WARNING("Refusing to evaluate token with claims or device SIDs but also "
377
589
            "with CLAIMS_EVALUATION_INVALID_STATE\n");
378
589
      return NT_STATUS_INVALID_TOKEN;
379
589
    }
380
125
    break;
381
4.53k
  case CLAIMS_EVALUATION_ALWAYS:
382
4.54k
  case CLAIMS_EVALUATION_NEVER:
383
4.54k
    break;
384
5.55k
  }
385
386
4.96k
  *access_granted = access_desired;
387
4.96k
  bits_remaining = access_desired;
388
389
  /* handle the maximum allowed flag */
390
4.96k
  if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
391
3.28k
    uint32_t orig_access_desired = access_desired;
392
393
3.28k
    access_desired |= access_check_max_allowed(sd, token, implicit_owner_rights);
394
3.28k
    access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
395
3.28k
    *access_granted = access_desired;
396
3.28k
    bits_remaining = access_desired;
397
398
3.28k
    DEBUG(10,("se_access_check: MAX desired = 0x%x, granted = 0x%x, remaining = 0x%x\n",
399
3.28k
      orig_access_desired,
400
3.28k
      *access_granted,
401
3.28k
      bits_remaining));
402
3.28k
  }
403
404
  /* a NULL dacl allows access */
405
4.96k
  if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
406
5
    *access_granted = access_desired;
407
5
    return NT_STATUS_OK;
408
5
  }
409
410
4.96k
  if (sd->dacl == NULL) {
411
114
    goto done;
412
114
  }
413
414
4.84k
  if (security_token_has_sid(token, sd->owner_sid)) {
415
    /*
416
     * Check for explicit owner rights: if there are none, we remove
417
     * the default owner right SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL
418
     * from remaining_access. Otherwise we just process the
419
     * explicitly granted rights when processing the ACEs.
420
     */
421
2.60k
    am_owner = true;
422
423
61.7k
    for (i=0; i < sd->dacl->num_aces; i++) {
424
59.1k
      struct security_ace *ace = &sd->dacl->aces[i];
425
426
59.1k
      if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
427
3.41k
        continue;
428
3.41k
      }
429
430
55.7k
      have_owner_rights_ace = dom_sid_equal(
431
55.7k
        &ace->trustee, &global_sid_Owner_Rights);
432
55.7k
      if (have_owner_rights_ace) {
433
4
        break;
434
4
      }
435
55.7k
    }
436
2.60k
  }
437
4.84k
  if (am_owner && !have_owner_rights_ace) {
438
2.59k
    switch (implicit_owner_rights) {
439
2.59k
    case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
440
2.59k
      bits_remaining &= ~(SEC_STD_WRITE_DAC |
441
2.59k
              SEC_STD_READ_CONTROL);
442
2.59k
      break;
443
0
    case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
444
0
      bits_remaining &= ~SEC_STD_READ_CONTROL;
445
0
      break;
446
2.59k
    }
447
2.59k
  }
448
449
  /* check each ace in turn. */
450
90.0k
  for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
451
85.1k
    struct security_ace *ace = &sd->dacl->aces[i];
452
85.1k
    bool is_owner_rights_ace = false;
453
454
85.1k
    if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
455
3.93k
      continue;
456
3.93k
    }
457
458
81.2k
    if (am_owner) {
459
51.8k
      is_owner_rights_ace = dom_sid_equal(
460
51.8k
        &ace->trustee, &global_sid_Owner_Rights);
461
51.8k
    }
462
463
81.2k
    if (!is_owner_rights_ace &&
464
81.2k
        !security_token_has_sid(token, &ace->trustee))
465
46.9k
    {
466
46.9k
      continue;
467
46.9k
    }
468
469
34.3k
    switch (ace->type) {
470
15.3k
    case SEC_ACE_TYPE_ACCESS_ALLOWED:
471
15.3k
      bits_remaining &= ~ace->access_mask;
472
15.3k
      break;
473
808
    case SEC_ACE_TYPE_ACCESS_DENIED:
474
990
    case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
475
990
      explicitly_denied_bits |= (bits_remaining & ace->access_mask);
476
990
      break;
477
478
877
    case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
479
877
    {
480
877
      enum ace_callback_result allow =
481
877
        check_callback_ace_allow(ace, token, sd);
482
877
      if (allow == ACE_CALLBACK_INVALID) {
483
2
        return NT_STATUS_INVALID_ACE_CONDITION;
484
2
      }
485
875
      if (allow == ACE_CALLBACK_ALLOW) {
486
91
        bits_remaining &= ~ace->access_mask;
487
91
      }
488
875
      break;
489
877
    }
490
491
8.88k
    case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
492
9.15k
    case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT:
493
9.15k
    {
494
9.15k
      enum ace_callback_result deny =
495
9.15k
        check_callback_ace_deny(ace, token, sd);
496
9.15k
      if (deny == ACE_CALLBACK_INVALID) {
497
3
        return NT_STATUS_INVALID_ACE_CONDITION;
498
3
      }
499
9.15k
      if (deny == ACE_CALLBACK_DENY) {
500
8.22k
        explicitly_denied_bits |= (bits_remaining & ace->access_mask);
501
8.22k
      }
502
9.15k
      break;
503
9.15k
    }
504
505
7.97k
    default:  /* Other ACE types not handled/supported */
506
7.97k
      break;
507
34.3k
    }
508
34.3k
  }
509
510
  /* Explicitly denied bits always override */
511
4.84k
  bits_remaining |= explicitly_denied_bits;
512
513
  /*
514
   * We check privileges here because they override even DENY entries.
515
   */
516
517
  /* Does the user have the privilege to gain SEC_PRIV_SECURITY? */
518
4.84k
  if (bits_remaining & SEC_FLAG_SYSTEM_SECURITY) {
519
1.19k
    if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
520
230
      bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
521
963
    } else {
522
963
      return NT_STATUS_PRIVILEGE_NOT_HELD;
523
963
    }
524
1.19k
  }
525
526
3.87k
  if ((bits_remaining & SEC_STD_WRITE_OWNER) &&
527
3.87k
       security_token_has_privilege(token, SEC_PRIV_TAKE_OWNERSHIP)) {
528
191
    bits_remaining &= ~(SEC_STD_WRITE_OWNER);
529
191
  }
530
531
3.99k
done:
532
3.99k
  if (bits_remaining != 0) {
533
3.35k
    *access_granted = bits_remaining;
534
3.35k
    return NT_STATUS_ACCESS_DENIED;
535
3.35k
  }
536
537
637
  return NT_STATUS_OK;
538
3.99k
}
539
540
/*
541
  The main entry point for access checking. If returning ACCESS_DENIED
542
  this function returns the denied bits in the uint32_t pointed
543
  to by the access_granted pointer.
544
*/
545
NTSTATUS se_access_check(const struct security_descriptor *sd,
546
       const struct security_token *token,
547
       uint32_t access_desired,
548
       uint32_t *access_granted)
549
5.55k
{
550
5.55k
  return se_access_check_implicit_owner(sd,
551
5.55k
                token,
552
5.55k
                access_desired,
553
5.55k
                access_granted,
554
5.55k
                IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
555
5.55k
}
556
557
/*
558
  The main entry point for access checking FOR THE FILE SERVER ONLY !
559
  If returning ACCESS_DENIED this function returns the denied bits in
560
  the uint32_t pointed to by the access_granted pointer.
561
*/
562
NTSTATUS se_file_access_check(const struct security_descriptor *sd,
563
        const struct security_token *token,
564
        bool priv_open_requested,
565
        uint32_t access_desired,
566
        uint32_t *access_granted)
567
0
{
568
0
  uint32_t bits_remaining;
569
0
  NTSTATUS status;
570
571
0
  if (!priv_open_requested) {
572
    /* Fall back to generic se_access_check(). */
573
0
    return se_access_check_implicit_owner(sd,
574
0
                  token,
575
0
                  access_desired,
576
0
                  access_granted,
577
0
                  IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
578
0
  }
579
580
  /*
581
   * We need to handle the maximum allowed flag
582
   * outside of se_access_check(), as we need to
583
   * add in the access allowed by the privileges
584
   * as well.
585
   */
586
587
0
  if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
588
0
    uint32_t orig_access_desired = access_desired;
589
590
0
    access_desired |= access_check_max_allowed(sd, token, true);
591
0
    access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
592
593
0
    if (security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
594
0
      access_desired |= SEC_RIGHTS_PRIV_BACKUP;
595
0
    }
596
597
0
    if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
598
0
      access_desired |= SEC_RIGHTS_PRIV_RESTORE;
599
0
    }
600
601
0
    DBG_DEBUG("MAX desired = 0x%0" PRIx32 " mapped to 0x%" PRIx32
602
0
        "\n ",
603
0
        orig_access_desired,
604
0
        access_desired);
605
0
  }
606
607
0
  status = se_access_check_implicit_owner(sd,
608
0
            token,
609
0
            access_desired,
610
0
            access_granted,
611
0
            IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
612
613
0
  if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
614
0
    return status;
615
0
  }
616
617
0
  bits_remaining = *access_granted;
618
619
  /* Check if we should override with privileges. */
620
0
  if ((bits_remaining & SEC_RIGHTS_PRIV_BACKUP) &&
621
0
      security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
622
0
    bits_remaining &= ~(SEC_RIGHTS_PRIV_BACKUP);
623
0
  }
624
0
  if ((bits_remaining & SEC_RIGHTS_PRIV_RESTORE) &&
625
0
      security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
626
0
    bits_remaining &= ~(SEC_RIGHTS_PRIV_RESTORE);
627
0
  }
628
0
  if (bits_remaining != 0) {
629
0
    *access_granted = bits_remaining;
630
0
    return NT_STATUS_ACCESS_DENIED;
631
0
  }
632
633
0
  return NT_STATUS_OK;
634
0
}
635
636
static const struct GUID *get_ace_object_type(const struct security_ace *ace)
637
0
{
638
0
  if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
639
0
    return &ace->object.object.type.type;
640
0
  }
641
642
0
  return NULL;
643
0
}
644
645
/**
646
 * Evaluates access rights specified in a object-specific ACE for an AD object.
647
 * This logic corresponds to MS-ADTS 5.1.3.3.3 Checking Object-Specific Access.
648
 * @param[in] ace - the ACE being processed
649
 * @param[in/out] tree - remaining_access gets updated for the tree
650
 * @param[out] grant_access - set to true if the ACE grants sufficient access
651
 *                            rights to the object/attribute
652
 * @returns NT_STATUS_OK, unless access was denied
653
 */
654
static NTSTATUS check_object_specific_access(const struct security_ace *ace,
655
               struct object_tree *tree,
656
               bool *grant_access)
657
1.70k
{
658
1.70k
  struct object_tree *node = NULL;
659
1.70k
  const struct GUID *type = NULL;
660
661
1.70k
  *grant_access = false;
662
663
  /* if no tree was supplied, we can't do object-specific access checks */
664
1.70k
  if (!tree) {
665
1.70k
    return NT_STATUS_OK;
666
1.70k
  }
667
668
  /* Get the ObjectType GUID this ACE applies to */
669
0
  type = get_ace_object_type(ace);
670
671
  /*
672
   * If the ACE doesn't have a type, then apply it to the whole tree, i.e.
673
   * treat 'OA' ACEs as 'A' and 'OD' as 'D'
674
   */
675
0
  if (!type) {
676
0
    node = tree;
677
0
  } else {
678
679
    /* skip it if the ACE's ObjectType GUID is not in the tree */
680
0
    node = get_object_tree_by_GUID(tree, type);
681
0
    if (!node) {
682
0
      return NT_STATUS_OK;
683
0
    }
684
0
  }
685
686
0
  if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
687
0
      ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT) {
688
    /* apply the access rights to this node, and any children */
689
0
    object_tree_modify_access(node, ace->access_mask);
690
691
    /*
692
     * Currently all nodes in the tree request the same access mask,
693
     * so we can use any node to check if processing this ACE now
694
     * means the requested access has been granted
695
     */
696
0
    if (node->remaining_access == 0) {
697
0
      *grant_access = true;
698
0
      return NT_STATUS_OK;
699
0
    }
700
701
    /*
702
     * As per 5.1.3.3.4 Checking Control Access Right-Based Access,
703
     * if the CONTROL_ACCESS right is present, then we can grant
704
     * access and stop any further access checks
705
     */
706
0
    if (ace->access_mask & SEC_ADS_CONTROL_ACCESS) {
707
0
      *grant_access = true;
708
0
      return NT_STATUS_OK;
709
0
    }
710
0
  } else {
711
712
    /* this ACE denies access to the requested object/attribute */
713
0
    if (node->remaining_access & ace->access_mask){
714
0
      return NT_STATUS_ACCESS_DENIED;
715
0
    }
716
0
  }
717
0
  return NT_STATUS_OK;
718
0
}
719
720
721
NTSTATUS sec_access_check_ds_implicit_owner(const struct security_descriptor *sd,
722
              const struct security_token *token,
723
              uint32_t access_desired,
724
              uint32_t *access_granted,
725
              struct object_tree *tree,
726
              const struct dom_sid *replace_sid,
727
              enum implicit_owner_rights implicit_owner_rights)
728
6.27k
{
729
6.27k
  uint32_t i;
730
6.27k
  uint32_t bits_remaining;
731
732
6.27k
  *access_granted = access_desired;
733
6.27k
  bits_remaining = access_desired;
734
735
  /* handle the maximum allowed flag */
736
6.27k
  if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
737
2.31k
    access_desired |= access_check_max_allowed(sd, token, implicit_owner_rights);
738
2.31k
    access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
739
2.31k
    *access_granted = access_desired;
740
2.31k
    bits_remaining = access_desired;
741
2.31k
  }
742
743
6.27k
  if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
744
710
    if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
745
393
      bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
746
393
    } else {
747
317
      return NT_STATUS_PRIVILEGE_NOT_HELD;
748
317
    }
749
710
  }
750
751
  /* the owner always gets SEC_STD_WRITE_DAC and SEC_STD_READ_CONTROL */
752
5.95k
  if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL)) &&
753
5.95k
      security_token_has_sid(token, sd->owner_sid)) {
754
330
    switch (implicit_owner_rights) {
755
0
    case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
756
0
      bits_remaining &= ~(SEC_STD_WRITE_DAC |
757
0
              SEC_STD_READ_CONTROL);
758
0
      break;
759
330
    case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
760
330
      bits_remaining &= ~SEC_STD_READ_CONTROL;
761
330
      break;
762
330
    }
763
330
  }
764
765
  /* SEC_PRIV_TAKE_OWNERSHIP grants SEC_STD_WRITE_OWNER */
766
5.95k
  if ((bits_remaining & (SEC_STD_WRITE_OWNER)) &&
767
5.95k
      security_token_has_privilege(token, SEC_PRIV_TAKE_OWNERSHIP)) {
768
128
    bits_remaining &= ~(SEC_STD_WRITE_OWNER);
769
128
  }
770
771
  /* a NULL dacl allows access */
772
5.95k
  if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
773
22
    *access_granted = access_desired;
774
22
    return NT_STATUS_OK;
775
22
  }
776
777
5.93k
  if (sd->dacl == NULL) {
778
532
    goto done;
779
532
  }
780
781
  /* check each ace in turn. */
782
133k
  for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
783
129k
    const struct dom_sid *trustee;
784
129k
    const struct security_ace *ace = &sd->dacl->aces[i];
785
129k
    NTSTATUS status;
786
129k
    bool grant_access = false;
787
788
129k
    if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
789
4.68k
      continue;
790
4.68k
    }
791
792
124k
    if (dom_sid_equal(&ace->trustee, &global_sid_Self) && replace_sid) {
793
0
      trustee = replace_sid;
794
124k
    } else {
795
124k
      trustee = &ace->trustee;
796
124k
    }
797
798
124k
    if (!security_token_has_sid(token, trustee)) {
799
70.4k
      continue;
800
70.4k
    }
801
802
54.2k
    switch (ace->type) {
803
27.1k
    case SEC_ACE_TYPE_ACCESS_ALLOWED:
804
27.1k
      if (tree) {
805
0
        object_tree_modify_access(tree, ace->access_mask);
806
0
      }
807
808
27.1k
      bits_remaining &= ~ace->access_mask;
809
27.1k
      break;
810
4.26k
    case SEC_ACE_TYPE_ACCESS_DENIED:
811
4.26k
      if (bits_remaining & ace->access_mask) {
812
74
        return NT_STATUS_ACCESS_DENIED;
813
74
      }
814
4.19k
      break;
815
4.19k
    case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
816
2.13k
    {
817
2.13k
      enum ace_callback_result allow =
818
2.13k
        check_callback_ace_allow(ace, token, sd);
819
2.13k
      if (allow == ACE_CALLBACK_INVALID) {
820
5
        return NT_STATUS_INVALID_ACE_CONDITION;
821
5
      }
822
2.13k
      if (allow == ACE_CALLBACK_ALLOW) {
823
115
        bits_remaining &= ~ace->access_mask;
824
115
      }
825
2.13k
      break;
826
2.13k
    }
827
828
8.63k
    case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
829
8.63k
    {
830
8.63k
      enum ace_callback_result deny =
831
8.63k
        check_callback_ace_deny(ace, token, sd);
832
8.63k
      if (deny == ACE_CALLBACK_INVALID) {
833
3
        return NT_STATUS_INVALID_ACE_CONDITION;
834
3
      }
835
8.62k
      if (deny == ACE_CALLBACK_DENY) {
836
7.83k
        if (bits_remaining & ace->access_mask) {
837
1.68k
          return NT_STATUS_ACCESS_DENIED;
838
1.68k
        }
839
7.83k
      }
840
6.94k
      break;
841
8.62k
    }
842
843
6.94k
    case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
844
1.53k
    case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
845
1.53k
      status = check_object_specific_access(ace, tree,
846
1.53k
                    &grant_access);
847
848
1.53k
      if (!NT_STATUS_IS_OK(status)) {
849
0
        return status;
850
0
      }
851
852
1.53k
      if (grant_access) {
853
0
        return NT_STATUS_OK;
854
0
      }
855
1.53k
      break;
856
4.44k
    case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT:
857
4.44k
    {
858
      /*
859
       * if the callback says ALLOW, we treat this as a
860
       * SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT.
861
       *
862
       * Otherwise we act as if this ACE does not exist.
863
       */
864
4.44k
      enum ace_callback_result allow =
865
4.44k
        check_callback_ace_allow(ace, token, sd);
866
4.44k
      if (allow == ACE_CALLBACK_INVALID) {
867
3
        return NT_STATUS_INVALID_ACE_CONDITION;
868
3
      }
869
4.44k
      if (allow != ACE_CALLBACK_ALLOW) {
870
4.37k
        break;
871
4.37k
      }
872
873
70
      status = check_object_specific_access(ace, tree,
874
70
                    &grant_access);
875
876
70
      if (!NT_STATUS_IS_OK(status)) {
877
0
        return status;
878
0
      }
879
880
70
      if (grant_access) {
881
0
        return NT_STATUS_OK;
882
0
      }
883
70
      break;
884
70
    }
885
185
    case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT:
886
185
    {
887
      /*
888
       * ACCESS_DENIED_OBJECT ACEs can't grant access --
889
       * they either don't match the object and slide
890
       * harmlessly past or they return
891
       * NT_STATUS_ACCESS_DENIED.
892
       *
893
       * ACCESS_DENIED_CALLBACK_OBJECT ACEs add another way
894
       * of not applying, and another way of failing.
895
       */
896
185
      enum ace_callback_result deny =
897
185
        check_callback_ace_deny(ace, token, sd);
898
185
      if (deny == ACE_CALLBACK_INVALID) {
899
1
        return NT_STATUS_INVALID_ACE_CONDITION;
900
1
      }
901
184
      if (deny != ACE_CALLBACK_DENY) {
902
80
        break;
903
80
      }
904
104
      status = check_object_specific_access(ace, tree,
905
104
                    &grant_access);
906
907
104
      if (!NT_STATUS_IS_OK(status)) {
908
0
        return status;
909
0
      }
910
104
      break;
911
104
    }
912
5.90k
    default:  /* Other ACE types not handled/supported */
913
5.90k
      break;
914
54.2k
    }
915
54.2k
  }
916
917
4.16k
done:
918
4.16k
  if (bits_remaining != 0) {
919
3.98k
    return NT_STATUS_ACCESS_DENIED;
920
3.98k
  }
921
922
182
  return NT_STATUS_OK;
923
4.16k
}
924
925
/**
926
 * @brief Perform directoryservice (DS) related access checks for a given user
927
 *
928
 * Perform DS access checks for the user represented by its security_token, on
929
 * the provided security descriptor. If an tree associating GUID and access
930
 * required is provided then object access (OA) are checked as well. *
931
 * @param[in]   sd             The security descriptor against which the required
932
 *                             access are requested
933
 *
934
 * @param[in]   token          The security_token associated with the user to
935
 *                             test
936
 *
937
 * @param[in]   access_desired A bitfield of rights that must be granted for the
938
 *                             given user in the specified SD.
939
 *
940
 * If one
941
 * of the entry in the tree grants all the requested rights for the given GUID
942
 * FIXME
943
 * tree can be null if not null it's the
944
 * Lots of code duplication, it will be united in just one
945
 * function eventually */
946
947
NTSTATUS sec_access_check_ds(const struct security_descriptor *sd,
948
           const struct security_token *token,
949
           uint32_t access_desired,
950
           uint32_t *access_granted,
951
           struct object_tree *tree,
952
           const struct dom_sid *replace_sid)
953
6.27k
{
954
6.27k
  return sec_access_check_ds_implicit_owner(sd,
955
6.27k
              token,
956
6.27k
              access_desired,
957
6.27k
              access_granted,
958
6.27k
              tree,
959
6.27k
              replace_sid,
960
6.27k
              IMPLICIT_OWNER_READ_CONTROL_RIGHTS);
961
6.27k
}