Coverage Report

Created: 2026-12-01 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/selinux/libsepol/src/hierarchy.c
Line
Count
Source
1
/* Authors: Joshua Brindle <jbrindle@tresys.com>
2
 *      Jason Tang <jtang@tresys.com>
3
 *
4
 * Updates: KaiGai Kohei <kaigai@ak.jp.nec.com>
5
 *          adds checks based on newer boundary facility.
6
 *
7
 * A set of utility functions that aid policy decision when dealing
8
 * with hierarchal namespaces.
9
 *
10
 * Copyright (C) 2005 Tresys Technology, LLC
11
 *
12
 * Copyright (c) 2008 NEC Corporation
13
 *
14
 *  This library is free software; you can redistribute it and/or
15
 *  modify it under the terms of the GNU Lesser General Public
16
 *  License as published by the Free Software Foundation; either
17
 *  version 2.1 of the License, or (at your option) any later version.
18
 *
19
 *  This library is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
 *  Lesser General Public License for more details.
23
 *
24
 *  You should have received a copy of the GNU Lesser General Public
25
 *  License along with this library; if not, write to the Free Software
26
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27
 */
28
29
#include <string.h>
30
#include <stdlib.h>
31
#include <assert.h>
32
#include <sepol/policydb/policydb.h>
33
#include <sepol/policydb/conditional.h>
34
#include <sepol/policydb/hierarchy.h>
35
#include <sepol/policydb/expand.h>
36
#include <sepol/policydb/util.h>
37
38
#include "debug.h"
39
40
139k
#define BOUNDS_AVTAB_SIZE 1024
41
42
static int bounds_insert_helper(sepol_handle_t *handle, avtab_t *avtab,
43
        avtab_key_t *avtab_key, avtab_datum_t *datum)
44
182k
{
45
182k
  int rc = avtab_insert(avtab, avtab_key, datum);
46
182k
  if (rc) {
47
0
    if (rc == SEPOL_ENOMEM)
48
0
      ERR(handle, "Insufficient memory");
49
0
    else
50
0
      ERR(handle, "Unexpected error (%d)", rc);
51
0
  }
52
182k
  return rc;
53
182k
}
54
55
static int bounds_insert_rule(sepol_handle_t *handle, avtab_t *avtab,
56
            avtab_t *global, avtab_t *other,
57
            avtab_key_t *avtab_key, avtab_datum_t *datum)
58
1.28M
{
59
1.28M
  int rc = 0;
60
1.28M
  avtab_datum_t *dup = avtab_search(avtab, avtab_key);
61
62
1.28M
  if (!dup) {
63
182k
    rc = bounds_insert_helper(handle, avtab, avtab_key, datum);
64
182k
    if (rc)
65
0
      goto exit;
66
1.10M
  } else {
67
1.10M
    dup->data |= datum->data;
68
1.10M
  }
69
70
1.28M
  if (other) {
71
    /* Search the other conditional avtab for the key and
72
     * add any common permissions to the global avtab
73
     */
74
1.20M
    uint32_t data = 0;
75
1.20M
    dup = avtab_search(other, avtab_key);
76
1.20M
    if (dup) {
77
234k
      data = dup->data & datum->data;
78
234k
      if (data) {
79
234k
        dup = avtab_search(global, avtab_key);
80
234k
        if (!dup) {
81
296
          avtab_datum_t d;
82
296
          d.data = data;
83
296
          rc = bounds_insert_helper(
84
296
            handle, global, avtab_key, &d);
85
296
          if (rc)
86
0
            goto exit;
87
233k
        } else {
88
233k
          dup->data |= data;
89
233k
        }
90
234k
      }
91
234k
    }
92
1.20M
  }
93
94
1.28M
exit:
95
1.28M
  return rc;
96
1.28M
}
97
98
static int bounds_expand_rule(sepol_handle_t *handle, policydb_t *p,
99
            avtab_t *avtab, avtab_t *global, avtab_t *other,
100
            uint32_t parent, uint32_t src, uint32_t tgt,
101
            uint32_t class, uint32_t data)
102
3.34M
{
103
3.34M
  int rc = 0;
104
3.34M
  avtab_key_t avtab_key;
105
3.34M
  avtab_datum_t datum;
106
3.34M
  ebitmap_node_t *tnode;
107
3.34M
  unsigned int i;
108
109
3.34M
  avtab_key.specified = AVTAB_ALLOWED;
110
3.34M
  avtab_key.target_class = class;
111
3.34M
  datum.data = data;
112
113
3.34M
  if (ebitmap_get_bit(&p->attr_type_map[src - 1], parent - 1)) {
114
1.22M
    avtab_key.source_type = parent;
115
78.4M
    ebitmap_for_each_positive_bit(&p->attr_type_map[tgt - 1], tnode,
116
78.4M
                i) {
117
1.28M
      avtab_key.target_type = i + 1;
118
1.28M
      rc = bounds_insert_rule(handle, avtab, global, other,
119
1.28M
            &avtab_key, &datum);
120
1.28M
      if (rc)
121
0
        goto exit;
122
1.28M
    }
123
1.22M
  }
124
125
3.34M
exit:
126
3.34M
  return rc;
127
3.34M
}
128
129
static int bounds_expand_cond_rules(sepol_handle_t *handle, policydb_t *p,
130
            cond_av_list_t *cur, avtab_t *avtab,
131
            avtab_t *global, avtab_t *other,
132
            uint32_t parent)
133
134k
{
134
134k
  int rc = 0;
135
136
2.73M
  for (; cur; cur = cur->next) {
137
2.60M
    avtab_ptr_t n = cur->node;
138
2.60M
    rc = bounds_expand_rule(handle, p, avtab, global, other, parent,
139
2.60M
          n->key.source_type, n->key.target_type,
140
2.60M
          n->key.target_class, n->datum.data);
141
2.60M
    if (rc)
142
0
      goto exit;
143
2.60M
  }
144
145
134k
exit:
146
134k
  return rc;
147
134k
}
148
149
struct bounds_expand_args {
150
  sepol_handle_t *handle;
151
  policydb_t *p;
152
  avtab_t *avtab;
153
  uint32_t parent;
154
};
155
156
static int bounds_expand_rule_callback(avtab_key_t *k, avtab_datum_t *d,
157
               void *args)
158
756k
{
159
756k
  struct bounds_expand_args *a = (struct bounds_expand_args *)args;
160
161
756k
  if (!(k->specified & AVTAB_ALLOWED))
162
17.9k
    return 0;
163
164
738k
  return bounds_expand_rule(a->handle, a->p, a->avtab, NULL, NULL,
165
738k
          a->parent, k->source_type, k->target_type,
166
738k
          k->target_class, d->data);
167
756k
}
168
169
struct bounds_cond_info {
170
  avtab_t true_avtab;
171
  avtab_t false_avtab;
172
  cond_list_t *cond_list;
173
  struct bounds_cond_info *next;
174
};
175
176
static void bounds_destroy_cond_info(struct bounds_cond_info *cur)
177
4.89k
{
178
4.89k
  struct bounds_cond_info *next;
179
180
142k
  for (; cur; cur = next) {
181
137k
    next = cur->next;
182
137k
    avtab_destroy(&cur->true_avtab);
183
137k
    avtab_destroy(&cur->false_avtab);
184
137k
    cur->next = NULL;
185
137k
    free(cur);
186
137k
  }
187
4.89k
}
188
189
static int bounds_expand_parent_rules(sepol_handle_t *handle, policydb_t *p,
190
              avtab_t *global_avtab,
191
              struct bounds_cond_info **cond_info,
192
              uint32_t parent)
193
4.89k
{
194
4.89k
  int rc = 0;
195
4.89k
  struct bounds_expand_args args;
196
4.89k
  cond_list_t *cur;
197
198
4.89k
  avtab_init(global_avtab);
199
4.89k
  rc = avtab_alloc(global_avtab, BOUNDS_AVTAB_SIZE);
200
4.89k
  if (rc)
201
0
    goto oom;
202
203
4.89k
  args.handle = handle;
204
4.89k
  args.p = p;
205
4.89k
  args.avtab = global_avtab;
206
4.89k
  args.parent = parent;
207
4.89k
  rc = avtab_map(&p->te_avtab, bounds_expand_rule_callback, &args);
208
4.89k
  if (rc)
209
0
    goto exit;
210
211
4.89k
  *cond_info = NULL;
212
142k
  for (cur = p->cond_list; cur; cur = cur->next) {
213
137k
    struct bounds_cond_info *ci;
214
137k
    ci = malloc(sizeof(struct bounds_cond_info));
215
137k
    if (!ci)
216
0
      goto oom;
217
137k
    avtab_init(&ci->true_avtab);
218
137k
    avtab_init(&ci->false_avtab);
219
137k
    ci->cond_list = cur;
220
137k
    ci->next = *cond_info;
221
137k
    *cond_info = ci;
222
137k
    if (cur->true_list) {
223
52.6k
      rc = avtab_alloc(&ci->true_avtab, BOUNDS_AVTAB_SIZE);
224
52.6k
      if (rc)
225
0
        goto oom;
226
52.6k
      rc = bounds_expand_cond_rules(handle, p, cur->true_list,
227
52.6k
                  &ci->true_avtab, NULL,
228
52.6k
                  NULL, parent);
229
52.6k
      if (rc)
230
0
        goto exit;
231
52.6k
    }
232
137k
    if (cur->false_list) {
233
81.8k
      rc = avtab_alloc(&ci->false_avtab, BOUNDS_AVTAB_SIZE);
234
81.8k
      if (rc)
235
0
        goto oom;
236
81.8k
      rc = bounds_expand_cond_rules(
237
81.8k
        handle, p, cur->false_list, &ci->false_avtab,
238
81.8k
        global_avtab, &ci->true_avtab, parent);
239
81.8k
      if (rc)
240
0
        goto exit;
241
81.8k
    }
242
137k
  }
243
244
4.89k
  return 0;
245
246
0
oom:
247
0
  ERR(handle, "Insufficient memory");
248
249
0
exit:
250
0
  ERR(handle, "Failed to expand parent rules");
251
0
  avtab_destroy(global_avtab);
252
0
  bounds_destroy_cond_info(*cond_info);
253
0
  *cond_info = NULL;
254
0
  return rc;
255
0
}
256
257
static int bounds_not_covered(avtab_t *global_avtab, avtab_t *cur_avtab,
258
            avtab_key_t *avtab_key, uint32_t data)
259
4.72M
{
260
4.72M
  avtab_datum_t *datum = avtab_search(cur_avtab, avtab_key);
261
4.72M
  if (datum)
262
224k
    data &= ~datum->data;
263
4.72M
  if (global_avtab && data) {
264
4.49M
    datum = avtab_search(global_avtab, avtab_key);
265
4.49M
    if (datum)
266
4.30M
      data &= ~datum->data;
267
4.49M
  }
268
269
4.72M
  return data;
270
4.72M
}
271
272
static int bounds_add_bad(sepol_handle_t *handle, uint32_t src, uint32_t tgt,
273
        uint32_t class, uint32_t data, avtab_ptr_t *bad)
274
195k
{
275
195k
  struct avtab_node *new = malloc(sizeof(struct avtab_node));
276
195k
  if (new == NULL) {
277
0
    ERR(handle, "Insufficient memory");
278
0
    return SEPOL_ENOMEM;
279
0
  }
280
195k
  memset(new, 0, sizeof(struct avtab_node));
281
195k
  new->key.source_type = src;
282
195k
  new->key.target_type = tgt;
283
195k
  new->key.target_class = class;
284
195k
  new->datum.data = data;
285
195k
  new->next = *bad;
286
195k
  *bad = new;
287
288
195k
  return 0;
289
195k
}
290
291
static int bounds_check_rule(sepol_handle_t *handle, policydb_t *p,
292
           avtab_t *global_avtab, avtab_t *cur_avtab,
293
           uint32_t child, uint32_t parent, uint32_t src,
294
           uint32_t tgt, uint32_t class, uint32_t data,
295
           avtab_ptr_t *bad, int *numbad)
296
3.22M
{
297
3.22M
  int rc = 0;
298
3.22M
  avtab_key_t avtab_key;
299
3.22M
  type_datum_t *td;
300
3.22M
  ebitmap_node_t *tnode;
301
3.22M
  unsigned int i;
302
3.22M
  uint32_t d;
303
304
3.22M
  avtab_key.specified = AVTAB_ALLOWED;
305
3.22M
  avtab_key.target_class = class;
306
307
3.22M
  if (ebitmap_get_bit(&p->attr_type_map[src - 1], child - 1)) {
308
309k
    avtab_key.source_type = parent;
309
24.5M
    ebitmap_for_each_positive_bit(&p->attr_type_map[tgt - 1], tnode,
310
24.5M
                i) {
311
4.72M
      td = p->type_val_to_struct[i];
312
4.72M
      if (td && td->bounds) {
313
2.43M
        avtab_key.target_type = td->bounds;
314
2.43M
        d = bounds_not_covered(global_avtab, cur_avtab,
315
2.43M
                   &avtab_key, data);
316
2.43M
      } else {
317
2.29M
        avtab_key.target_type = i + 1;
318
2.29M
        d = bounds_not_covered(global_avtab, cur_avtab,
319
2.29M
                   &avtab_key, data);
320
2.29M
      }
321
4.72M
      if (d) {
322
195k
        (*numbad)++;
323
195k
        rc = bounds_add_bad(handle, child, i + 1, class,
324
195k
                d, bad);
325
195k
        if (rc)
326
0
          goto exit;
327
195k
      }
328
4.72M
    }
329
309k
  }
330
331
3.22M
exit:
332
3.22M
  return rc;
333
3.22M
}
334
335
static int bounds_check_cond_rules(sepol_handle_t *handle, policydb_t *p,
336
           avtab_t *global_avtab, avtab_t *cond_avtab,
337
           cond_av_list_t *rules, uint32_t child,
338
           uint32_t parent, avtab_ptr_t *bad,
339
           int *numbad)
340
275k
{
341
275k
  int rc = 0;
342
275k
  cond_av_list_t *cur;
343
344
2.87M
  for (cur = rules; cur; cur = cur->next) {
345
2.60M
    avtab_ptr_t ap = cur->node;
346
2.60M
    avtab_key_t *key = &ap->key;
347
2.60M
    avtab_datum_t *datum = &ap->datum;
348
2.60M
    if (!(key->specified & AVTAB_ALLOWED))
349
111k
      continue;
350
2.49M
    rc = bounds_check_rule(handle, p, global_avtab, cond_avtab,
351
2.49M
               child, parent, key->source_type,
352
2.49M
               key->target_type, key->target_class,
353
2.49M
               datum->data, bad, numbad);
354
2.49M
    if (rc)
355
0
      goto exit;
356
2.49M
  }
357
358
275k
exit:
359
275k
  return rc;
360
275k
}
361
362
struct bounds_check_args {
363
  sepol_handle_t *handle;
364
  policydb_t *p;
365
  avtab_t *cur_avtab;
366
  uint32_t child;
367
  uint32_t parent;
368
  avtab_ptr_t bad;
369
  int numbad;
370
};
371
372
static int bounds_check_rule_callback(avtab_key_t *k, avtab_datum_t *d,
373
              void *args)
374
756k
{
375
756k
  struct bounds_check_args *a = (struct bounds_check_args *)args;
376
377
756k
  if (!(k->specified & AVTAB_ALLOWED))
378
17.9k
    return 0;
379
380
738k
  return bounds_check_rule(a->handle, a->p, NULL, a->cur_avtab, a->child,
381
738k
         a->parent, k->source_type, k->target_type,
382
738k
         k->target_class, d->data, &a->bad, &a->numbad);
383
756k
}
384
385
static int bounds_check_child_rules(sepol_handle_t *handle, policydb_t *p,
386
            avtab_t *global_avtab,
387
            struct bounds_cond_info *cond_info,
388
            uint32_t child, uint32_t parent,
389
            avtab_ptr_t *bad, int *numbad)
390
4.89k
{
391
4.89k
  int rc;
392
4.89k
  struct bounds_check_args args;
393
4.89k
  struct bounds_cond_info *cur;
394
395
4.89k
  args.handle = handle;
396
4.89k
  args.p = p;
397
4.89k
  args.cur_avtab = global_avtab;
398
4.89k
  args.child = child;
399
4.89k
  args.parent = parent;
400
4.89k
  args.bad = NULL;
401
4.89k
  args.numbad = 0;
402
4.89k
  rc = avtab_map(&p->te_avtab, bounds_check_rule_callback, &args);
403
4.89k
  if (rc)
404
0
    goto exit;
405
406
142k
  for (cur = cond_info; cur; cur = cur->next) {
407
137k
    cond_list_t *node = cur->cond_list;
408
137k
    rc = bounds_check_cond_rules(handle, p, global_avtab,
409
137k
               &cur->true_avtab, node->true_list,
410
137k
               child, parent, &args.bad,
411
137k
               &args.numbad);
412
137k
    if (rc)
413
0
      goto exit;
414
415
137k
    rc = bounds_check_cond_rules(handle, p, global_avtab,
416
137k
               &cur->false_avtab,
417
137k
               node->false_list, child, parent,
418
137k
               &args.bad, &args.numbad);
419
137k
    if (rc)
420
0
      goto exit;
421
137k
  }
422
423
4.89k
  *numbad += args.numbad;
424
4.89k
  *bad = args.bad;
425
426
4.89k
exit:
427
4.89k
  return rc;
428
4.89k
}
429
430
int bounds_check_type(sepol_handle_t *handle, policydb_t *p, uint32_t child,
431
          uint32_t parent, avtab_ptr_t *bad, int *numbad)
432
4.89k
{
433
4.89k
  int rc = 0;
434
4.89k
  avtab_t global_avtab;
435
4.89k
  struct bounds_cond_info *cond_info = NULL;
436
437
4.89k
  rc = bounds_expand_parent_rules(handle, p, &global_avtab, &cond_info,
438
4.89k
          parent);
439
4.89k
  if (rc)
440
0
    goto exit;
441
442
4.89k
  rc = bounds_check_child_rules(handle, p, &global_avtab, cond_info,
443
4.89k
              child, parent, bad, numbad);
444
445
4.89k
  bounds_destroy_cond_info(cond_info);
446
4.89k
  avtab_destroy(&global_avtab);
447
448
4.89k
exit:
449
4.89k
  return rc;
450
4.89k
}
451
452
struct bounds_args {
453
  sepol_handle_t *handle;
454
  policydb_t *p;
455
  int numbad;
456
};
457
458
static void bounds_report(sepol_handle_t *handle, policydb_t *p, uint32_t child,
459
        uint32_t parent, avtab_ptr_t cur)
460
0
{
461
0
  ERR(handle,
462
0
      "Child type %s exceeds bounds of parent %s in the following rules:",
463
0
      p->p_type_val_to_name[child - 1],
464
0
      p->p_type_val_to_name[parent - 1]);
465
0
  for (; cur; cur = cur->next) {
466
0
    char *permstr = sepol_av_to_string(p, cur->key.target_class,
467
0
               cur->datum.data);
468
469
0
    ERR(handle, "    %s %s : %s { %s }",
470
0
        p->p_type_val_to_name[cur->key.source_type - 1],
471
0
        p->p_type_val_to_name[cur->key.target_type - 1],
472
0
        p->p_class_val_to_name[cur->key.target_class - 1],
473
0
        permstr ?: "<format-failure>");
474
475
0
    free(permstr);
476
0
  }
477
0
}
478
479
void bounds_destroy_bad(avtab_ptr_t cur)
480
1.92k
{
481
1.92k
  avtab_ptr_t next;
482
483
197k
  for (; cur; cur = next) {
484
195k
    next = cur->next;
485
195k
    cur->next = NULL;
486
195k
    free(cur);
487
195k
  }
488
1.92k
}
489
490
static int bounds_check_type_callback(hashtab_key_t k __attribute__((unused)),
491
              hashtab_datum_t d, void *args)
492
0
{
493
0
  int rc = 0;
494
0
  struct bounds_args *a = (struct bounds_args *)args;
495
0
  type_datum_t *t = (type_datum_t *)d;
496
0
  avtab_ptr_t bad = NULL;
497
498
0
  if (t->bounds) {
499
0
    rc = bounds_check_type(a->handle, a->p, t->s.value, t->bounds,
500
0
               &bad, &a->numbad);
501
0
    if (bad) {
502
0
      bounds_report(a->handle, a->p, t->s.value, t->bounds,
503
0
              bad);
504
0
      bounds_destroy_bad(bad);
505
0
    }
506
0
  }
507
508
0
  return rc;
509
0
}
510
511
int bounds_check_types(sepol_handle_t *handle, policydb_t *p)
512
0
{
513
0
  int rc;
514
0
  struct bounds_args args;
515
516
0
  args.handle = handle;
517
0
  args.p = p;
518
0
  args.numbad = 0;
519
520
0
  rc = hashtab_map(p->p_types.table, bounds_check_type_callback, &args);
521
0
  if (rc)
522
0
    goto exit;
523
524
0
  if (args.numbad > 0) {
525
0
    ERR(handle, "%d errors found during type bounds check",
526
0
        args.numbad);
527
0
    rc = SEPOL_ERR;
528
0
  }
529
530
0
exit:
531
0
  return rc;
532
0
}
533
534
/* The role bounds is defined as: a child role cannot have a type that
535
 * its parent doesn't have.
536
 */
537
static int bounds_check_role_callback(hashtab_key_t k, hashtab_datum_t d,
538
              void *args)
539
24.8k
{
540
24.8k
  struct bounds_args *a = (struct bounds_args *)args;
541
24.8k
  role_datum_t *r = (role_datum_t *)d;
542
24.8k
  role_datum_t *rp = NULL;
543
544
24.8k
  if (!r->bounds)
545
24.8k
    return 0;
546
547
7
  rp = a->p->role_val_to_struct[r->bounds - 1];
548
549
7
  if (rp && !ebitmap_contains(&rp->types.types, &r->types.types)) {
550
0
    ERR(a->handle, "Role bounds violation, %s exceeds %s",
551
0
        (char *)k, a->p->p_role_val_to_name[rp->s.value - 1]);
552
0
    a->numbad++;
553
0
  }
554
555
7
  return 0;
556
24.8k
}
557
558
int bounds_check_roles(sepol_handle_t *handle, policydb_t *p)
559
3.50k
{
560
3.50k
  struct bounds_args args;
561
562
3.50k
  args.handle = handle;
563
3.50k
  args.p = p;
564
3.50k
  args.numbad = 0;
565
566
3.50k
  hashtab_map(p->p_roles.table, bounds_check_role_callback, &args);
567
568
3.50k
  if (args.numbad > 0) {
569
0
    ERR(handle, "%d errors found during role bounds check",
570
0
        args.numbad);
571
0
    return SEPOL_ERR;
572
0
  }
573
574
3.50k
  return 0;
575
3.50k
}
576
577
/* The user bounds is defined as: a child user cannot have a role that
578
 * its parent doesn't have.
579
 */
580
static int bounds_check_user_callback(hashtab_key_t k, hashtab_datum_t d,
581
              void *args)
582
4.58k
{
583
4.58k
  struct bounds_args *a = (struct bounds_args *)args;
584
4.58k
  user_datum_t *u = (user_datum_t *)d;
585
4.58k
  user_datum_t *up = NULL;
586
587
4.58k
  if (!u->bounds)
588
4.57k
    return 0;
589
590
8
  up = a->p->user_val_to_struct[u->bounds - 1];
591
592
8
  if (up && !ebitmap_contains(&up->roles.roles, &u->roles.roles)) {
593
1
    ERR(a->handle, "User bounds violation, %s exceeds %s",
594
1
        (char *)k, a->p->p_user_val_to_name[up->s.value - 1]);
595
1
    a->numbad++;
596
1
  }
597
598
8
  return 0;
599
4.58k
}
600
601
int bounds_check_users(sepol_handle_t *handle, policydb_t *p)
602
3.50k
{
603
3.50k
  struct bounds_args args;
604
605
3.50k
  args.handle = handle;
606
3.50k
  args.p = p;
607
3.50k
  args.numbad = 0;
608
609
3.50k
  hashtab_map(p->p_users.table, bounds_check_user_callback, &args);
610
611
3.50k
  if (args.numbad > 0) {
612
1
    ERR(handle, "%d errors found during user bounds check",
613
1
        args.numbad);
614
1
    return SEPOL_ERR;
615
1
  }
616
617
3.50k
  return 0;
618
3.50k
}
619
620
0
#define type_datum_is_attr(d) ((d)->flavor == TYPE_ATTRIB)
621
0
#define role_datum_is_attr(d) ((d)->flavor == ROLE_ATTRIB)
622
0
#define user_datum_is_attr(d) (0)
623
624
#define add_hierarchy_callback_template(prefix)                                  \
625
  int hierarchy_add_##prefix##_callback(hashtab_key_t k                    \
626
                __attribute__((unused)),           \
627
                hashtab_datum_t d, void *args)     \
628
0
  {                                                                        \
629
0
    struct bounds_args *a = (struct bounds_args *)args;              \
630
0
    sepol_handle_t *handle = a->handle;                              \
631
0
    policydb_t *p = a->p;                                            \
632
0
    prefix##_datum_t *datum = (prefix##_datum_t *)d;                 \
633
0
    prefix##_datum_t *parent;                                        \
634
0
    char *parent_name, *datum_name, *tmp;                            \
635
0
                                                                                 \
636
0
    if (prefix##_datum_is_attr(datum))                               \
637
0
      return 0;                                                \
638
0
                                                                                 \
639
0
    if (!datum->bounds) {                                            \
640
0
      datum_name =                                             \
641
0
        p->p_##prefix##_val_to_name[datum->s.value -     \
642
0
                  1];                  \
643
0
                                                                                 \
644
0
      if (!datum_name)                                         \
645
0
        return 0;                                        \
646
0
      tmp = strrchr(datum_name, '.');                          \
647
0
      /* no '.' means it has no parent */                      \
648
0
      if (!tmp)                                                \
649
0
        return 0;                                        \
650
0
                                                                                 \
651
0
      parent_name = strdup(datum_name);                        \
652
0
      if (!parent_name) {                                      \
653
0
        ERR(handle, "Insufficient memory");              \
654
0
        return SEPOL_ENOMEM;                             \
655
0
      }                                                        \
656
0
      parent_name[tmp - datum_name] = '\0';                    \
657
0
                                                                                 \
658
0
      parent = hashtab_search(p->p_##prefix##s.table,          \
659
0
            parent_name);                    \
660
0
      if (!parent) {                                           \
661
0
        /* Orphan type/role/user */                      \
662
0
        ERR(handle,                                      \
663
0
            "%s doesn't exist, %s is an orphan",         \
664
0
            parent_name,                                 \
665
0
            p->p_##prefix##_val_to_name[datum->s.value - \
666
0
                1]);             \
667
0
        free(parent_name);                               \
668
0
        a->numbad++;                                     \
669
0
        return 0;                                        \
670
0
      }                                                        \
671
0
      if (prefix##_datum_is_attr(parent)) {                    \
672
0
        ERR(handle,                                      \
673
0
            "%s is an attribute, %s is an orphan",       \
674
0
            parent_name,                                 \
675
0
            p->p_##prefix##_val_to_name[datum->s.value - \
676
0
                1]);             \
677
0
        free(parent_name);                               \
678
0
        a->numbad++;                                     \
679
0
        return 0;                                        \
680
0
      }                                                        \
681
0
      datum->bounds = parent->s.value;                         \
682
0
      free(parent_name);                                       \
683
0
    }                                                                \
684
0
                                                                                 \
685
0
    return 0;                                                        \
686
0
  }
687
688
0
static add_hierarchy_callback_template(type) static add_hierarchy_callback_template(
689
0
  role) static add_hierarchy_callback_template(user)
690
691
  int hierarchy_add_bounds(sepol_handle_t *handle, policydb_t *p)
692
0
{
693
0
  int rc = 0;
694
0
  struct bounds_args args;
695
696
0
  args.handle = handle;
697
0
  args.p = p;
698
0
  args.numbad = 0;
699
700
0
  rc = hashtab_map(p->p_users.table, hierarchy_add_user_callback, &args);
701
0
  if (rc)
702
0
    goto exit;
703
704
0
  rc = hashtab_map(p->p_roles.table, hierarchy_add_role_callback, &args);
705
0
  if (rc)
706
0
    goto exit;
707
708
0
  rc = hashtab_map(p->p_types.table, hierarchy_add_type_callback, &args);
709
0
  if (rc)
710
0
    goto exit;
711
712
0
  if (args.numbad > 0) {
713
0
    ERR(handle, "%d errors found while adding hierarchies",
714
0
        args.numbad);
715
0
    rc = SEPOL_ERR;
716
0
  }
717
718
0
exit:
719
0
  return rc;
720
0
}
721
722
int hierarchy_check_constraints(sepol_handle_t *handle, policydb_t *p)
723
0
{
724
0
  int rc = 0;
725
0
  int violation = 0;
726
727
0
  rc = hierarchy_add_bounds(handle, p);
728
0
  if (rc)
729
0
    goto exit;
730
731
0
  rc = bounds_check_users(handle, p);
732
0
  if (rc)
733
0
    violation = 1;
734
735
0
  rc = bounds_check_roles(handle, p);
736
0
  if (rc)
737
0
    violation = 1;
738
739
0
  rc = bounds_check_types(handle, p);
740
0
  if (rc) {
741
0
    if (rc == SEPOL_ERR)
742
0
      violation = 1;
743
0
    else
744
0
      goto exit;
745
0
  }
746
747
0
  if (violation)
748
0
    rc = SEPOL_ERR;
749
750
0
exit:
751
0
  return rc;
752
0
}