Coverage Report

Created: 2026-12-01 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/selinux/libsepol/src/avtab.c
Line
Count
Source
1
2
/* Author : Stephen Smalley, <stephen.smalley.work@gmail.com> */
3
4
/*
5
 * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
6
 *  Tuned number of hash slots for avtab to reduce memory usage
7
 */
8
9
/* Updated: Frank Mayer <mayerf@tresys.com>
10
 *          and Karl MacMillan <kmacmillan@mentalrootkit.com>
11
 *
12
 *  Added conditional policy language extensions
13
 *
14
 * Updated: Red Hat, Inc.  James Morris <jmorris@redhat.com>
15
 *
16
 *      Code cleanup
17
 *
18
 * Updated: Karl MacMillan <kmacmillan@mentalrootkit.com>
19
 *
20
 * Copyright (C) 2003 Tresys Technology, LLC
21
 * Copyright (C) 2003,2007 Red Hat, Inc.
22
 *
23
 *  This library is free software; you can redistribute it and/or
24
 *  modify it under the terms of the GNU Lesser General Public
25
 *  License as published by the Free Software Foundation; either
26
 *  version 2.1 of the License, or (at your option) any later version.
27
 *
28
 *  This library is distributed in the hope that it will be useful,
29
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31
 *  Lesser General Public License for more details.
32
 *
33
 *  You should have received a copy of the GNU Lesser General Public
34
 *  License along with this library; if not, write to the Free Software
35
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
36
 */
37
38
/* FLASK */
39
40
/* 
41
 * Implementation of the access vector table type.
42
 */
43
44
#include <stdlib.h>
45
#include <sepol/policydb/avtab.h>
46
#include <sepol/policydb/policydb.h>
47
#include <sepol/errcodes.h>
48
49
#include "debug.h"
50
#include "private.h"
51
52
/* Based on MurmurHash3, written by Austin Appleby and placed in the
53
 * public domain.
54
 */
55
ignore_unsigned_overflow_ static inline int avtab_hash(struct avtab_key *keyp,
56
                   uint32_t mask)
57
491M
{
58
491M
  static const uint32_t c1 = 0xcc9e2d51;
59
491M
  static const uint32_t c2 = 0x1b873593;
60
491M
  static const uint32_t r1 = 15;
61
491M
  static const uint32_t r2 = 13;
62
491M
  static const uint32_t m = 5;
63
491M
  static const uint32_t n = 0xe6546b64;
64
65
491M
  uint32_t hash = 0;
66
67
491M
#define mix(input)                                         \
68
1.47G
  do {                                               \
69
1.47G
    uint32_t v = input;                        \
70
1.47G
    v *= c1;                                   \
71
1.47G
    v = (v << r1) | (v >> (32 - r1));          \
72
1.47G
    v *= c2;                                   \
73
1.47G
    hash ^= v;                                 \
74
1.47G
    hash = (hash << r2) | (hash >> (32 - r2)); \
75
1.47G
    hash = hash * m + n;                       \
76
1.47G
  } while (0)
77
78
491M
  mix(keyp->target_class);
79
491M
  mix(keyp->target_type);
80
491M
  mix(keyp->source_type);
81
82
491M
#undef mix
83
84
491M
  hash ^= hash >> 16;
85
491M
  hash *= 0x85ebca6b;
86
491M
  hash ^= hash >> 13;
87
491M
  hash *= 0xc2b2ae35;
88
491M
  hash ^= hash >> 16;
89
90
491M
  return hash & mask;
91
491M
}
92
93
static avtab_ptr_t avtab_insert_node(avtab_t *h, int hvalue, avtab_ptr_t prev,
94
             avtab_key_t *key, avtab_datum_t *datum)
95
3.40M
{
96
3.40M
  avtab_ptr_t newnode;
97
3.40M
  avtab_extended_perms_t *xperms;
98
99
3.40M
  newnode = (avtab_ptr_t)malloc(sizeof(struct avtab_node));
100
3.40M
  if (newnode == NULL)
101
0
    return NULL;
102
3.40M
  memset(newnode, 0, sizeof(struct avtab_node));
103
3.40M
  newnode->key = *key;
104
105
3.40M
  if (key->specified & AVTAB_XPERMS) {
106
12.3k
    xperms = calloc(1, sizeof(avtab_extended_perms_t));
107
12.3k
    if (xperms == NULL) {
108
0
      free(newnode);
109
0
      return NULL;
110
0
    }
111
12.3k
    if (datum->xperms) /* else caller populates xperms */
112
12.3k
      *xperms = *(datum->xperms);
113
114
12.3k
    newnode->datum.xperms = xperms;
115
    /* data is usually ignored with xperms, except in the case of
116
     * neverallow checking, which requires permission bits to be set.
117
     * So copy data so it is set in the avtab
118
     */
119
12.3k
    newnode->datum.data = datum->data;
120
3.39M
  } else {
121
3.39M
    newnode->datum = *datum;
122
3.39M
  }
123
124
3.40M
  if (prev) {
125
97.6k
    newnode->next = prev->next;
126
97.6k
    prev->next = newnode;
127
3.30M
  } else {
128
3.30M
    newnode->next = h->htable[hvalue];
129
3.30M
    h->htable[hvalue] = newnode;
130
3.30M
  }
131
132
3.40M
  h->nel++;
133
3.40M
  return newnode;
134
3.40M
}
135
136
int avtab_insert(avtab_t *h, avtab_key_t *key, avtab_datum_t *datum)
137
2.79M
{
138
2.79M
  int hvalue;
139
2.79M
  avtab_ptr_t prev, cur, newnode;
140
2.79M
  uint16_t specified = key->specified &
141
2.79M
           ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
142
143
2.79M
  if (!h || !h->htable)
144
0
    return SEPOL_ENOMEM;
145
146
2.79M
  hvalue = avtab_hash(key, h->mask);
147
2.89M
  for (prev = NULL, cur = h->htable[hvalue]; cur;
148
2.79M
       prev = cur, cur = cur->next) {
149
207k
    if (key->source_type == cur->key.source_type &&
150
58.8k
        key->target_type == cur->key.target_type &&
151
9.73k
        key->target_class == cur->key.target_class &&
152
8.56k
        (specified & cur->key.specified)) {
153
      /* Extended permissions are not necessarily unique */
154
3.72k
      if (specified & AVTAB_XPERMS)
155
3.72k
        break;
156
6
      return SEPOL_EEXIST;
157
3.72k
    }
158
204k
    if (key->source_type < cur->key.source_type)
159
66.6k
      break;
160
137k
    if (key->source_type == cur->key.source_type &&
161
55.1k
        key->target_type < cur->key.target_type)
162
36.8k
      break;
163
100k
    if (key->source_type == cur->key.source_type &&
164
18.2k
        key->target_type == cur->key.target_type &&
165
6.01k
        key->target_class < cur->key.target_class)
166
660
      break;
167
100k
  }
168
169
2.79M
  newnode = avtab_insert_node(h, hvalue, prev, key, datum);
170
2.79M
  if (!newnode)
171
0
    return SEPOL_ENOMEM;
172
173
2.79M
  return 0;
174
2.79M
}
175
176
/* Unlike avtab_insert(), this function allow multiple insertions of the same 
177
 * key/specified mask into the table, as needed by the conditional avtab.  
178
 * It also returns a pointer to the node inserted.
179
 */
180
avtab_ptr_t avtab_insert_nonunique(avtab_t *h, avtab_key_t *key,
181
           avtab_datum_t *datum)
182
606k
{
183
606k
  int hvalue;
184
606k
  avtab_ptr_t prev, cur, newnode;
185
606k
  uint16_t specified = key->specified &
186
606k
           ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
187
188
606k
  if (!h || !h->htable)
189
0
    return NULL;
190
606k
  hvalue = avtab_hash(key, h->mask);
191
681k
  for (prev = NULL, cur = h->htable[hvalue]; cur;
192
606k
       prev = cur, cur = cur->next) {
193
545k
    if (key->source_type == cur->key.source_type &&
194
526k
        key->target_type == cur->key.target_type &&
195
518k
        key->target_class == cur->key.target_class &&
196
509k
        (specified & cur->key.specified))
197
469k
      break;
198
76.5k
    if (key->source_type < cur->key.source_type)
199
671
      break;
200
75.8k
    if (key->source_type == cur->key.source_type &&
201
57.3k
        key->target_type < cur->key.target_type)
202
510
      break;
203
75.3k
    if (key->source_type == cur->key.source_type &&
204
56.8k
        key->target_type == cur->key.target_type &&
205
48.9k
        key->target_class < cur->key.target_class)
206
282
      break;
207
75.3k
  }
208
606k
  newnode = avtab_insert_node(h, hvalue, prev, key, datum);
209
210
606k
  return newnode;
211
606k
}
212
213
avtab_datum_t *avtab_search(avtab_t *h, avtab_key_t *key)
214
486M
{
215
486M
  int hvalue;
216
486M
  avtab_ptr_t cur;
217
486M
  uint16_t specified = key->specified &
218
486M
           ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
219
220
486M
  if (!h || !h->htable)
221
974k
    return NULL;
222
223
485M
  hvalue = avtab_hash(key, h->mask);
224
489M
  for (cur = h->htable[hvalue]; cur; cur = cur->next) {
225
32.6M
    if (key->source_type == cur->key.source_type &&
226
20.5M
        key->target_type == cur->key.target_type &&
227
19.4M
        key->target_class == cur->key.target_class &&
228
19.4M
        (specified & cur->key.specified))
229
19.4M
      return &cur->datum;
230
231
13.2M
    if (key->source_type < cur->key.source_type)
232
8.47M
      break;
233
4.79M
    if (key->source_type == cur->key.source_type &&
234
1.14M
        key->target_type < cur->key.target_type)
235
41.6k
      break;
236
4.75M
    if (key->source_type == cur->key.source_type &&
237
1.09M
        key->target_type == cur->key.target_type &&
238
6.95k
        key->target_class < cur->key.target_class)
239
653
      break;
240
4.75M
  }
241
242
465M
  return NULL;
243
485M
}
244
245
/* This search function returns a node pointer, and can be used in
246
 * conjunction with avtab_search_next_node()
247
 */
248
avtab_ptr_t avtab_search_node(avtab_t *h, avtab_key_t *key)
249
2.91M
{
250
2.91M
  int hvalue;
251
2.91M
  avtab_ptr_t cur;
252
2.91M
  uint16_t specified = key->specified &
253
2.91M
           ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
254
255
2.91M
  if (!h || !h->htable)
256
0
    return NULL;
257
258
2.91M
  hvalue = avtab_hash(key, h->mask);
259
2.99M
  for (cur = h->htable[hvalue]; cur; cur = cur->next) {
260
1.52M
    if (key->source_type == cur->key.source_type &&
261
1.38M
        key->target_type == cur->key.target_type &&
262
1.37M
        key->target_class == cur->key.target_class &&
263
1.37M
        (specified & cur->key.specified))
264
1.37M
      return cur;
265
266
148k
    if (key->source_type < cur->key.source_type)
267
65.4k
      break;
268
83.4k
    if (key->source_type == cur->key.source_type &&
269
4.30k
        key->target_type < cur->key.target_type)
270
126
      break;
271
83.3k
    if (key->source_type == cur->key.source_type &&
272
4.17k
        key->target_type == cur->key.target_type &&
273
2.68k
        key->target_class < cur->key.target_class)
274
81
      break;
275
83.3k
  }
276
1.53M
  return NULL;
277
2.91M
}
278
279
avtab_ptr_t avtab_search_node_next(avtab_ptr_t node, int specified)
280
251
{
281
251
  avtab_ptr_t cur;
282
283
251
  if (!node)
284
0
    return NULL;
285
286
251
  specified &= ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
287
513
  for (cur = node->next; cur; cur = cur->next) {
288
466
    if (node->key.source_type == cur->key.source_type &&
289
377
        node->key.target_type == cur->key.target_type &&
290
316
        node->key.target_class == cur->key.target_class &&
291
265
        (specified & cur->key.specified))
292
3
      return cur;
293
294
463
    if (node->key.source_type < cur->key.source_type)
295
89
      break;
296
374
    if (node->key.source_type == cur->key.source_type &&
297
374
        node->key.target_type < cur->key.target_type)
298
61
      break;
299
313
    if (node->key.source_type == cur->key.source_type &&
300
313
        node->key.target_type == cur->key.target_type &&
301
313
        node->key.target_class < cur->key.target_class)
302
51
      break;
303
313
  }
304
248
  return NULL;
305
251
}
306
307
void avtab_destroy(avtab_t *h)
308
451k
{
309
451k
  unsigned int i;
310
451k
  avtab_ptr_t cur, temp;
311
312
451k
  if (!h || !h->htable)
313
275k
    return;
314
315
14.8G
  for (i = 0; i < h->nslot; i++) {
316
14.8G
    cur = h->htable[i];
317
14.8G
    while (cur != NULL) {
318
3.28M
      if (cur->key.specified & AVTAB_XPERMS) {
319
11.9k
        free(cur->datum.xperms);
320
11.9k
      }
321
3.28M
      temp = cur;
322
3.28M
      cur = cur->next;
323
3.28M
      free(temp);
324
3.28M
    }
325
14.8G
    h->htable[i] = NULL;
326
14.8G
  }
327
175k
  free(h->htable);
328
175k
  h->htable = NULL;
329
175k
  h->nslot = 0;
330
175k
  h->mask = 0;
331
175k
}
332
333
int avtab_map(const avtab_t *h,
334
        int (*apply)(avtab_key_t *k, avtab_datum_t *d, void *args),
335
        void *args)
336
166k
{
337
166k
  unsigned int i;
338
166k
  int ret;
339
166k
  avtab_ptr_t cur;
340
341
166k
  if (!h)
342
0
    return 0;
343
344
114G
  for (i = 0; i < h->nslot; i++) {
345
114G
    cur = h->htable[i];
346
114G
    while (cur != NULL) {
347
32.4M
      ret = apply(&cur->key, &cur->datum, args);
348
32.4M
      if (ret)
349
21.4k
        return ret;
350
32.3M
      cur = cur->next;
351
32.3M
    }
352
114G
  }
353
145k
  return 0;
354
166k
}
355
356
int avtab_init(avtab_t *h)
357
379k
{
358
379k
  h->htable = NULL;
359
379k
  h->nel = 0;
360
379k
  return 0;
361
379k
}
362
363
int avtab_alloc(avtab_t *h, uint32_t nrules)
364
196k
{
365
196k
  uint32_t mask = 0;
366
196k
  uint32_t shift = 0;
367
196k
  uint32_t work = nrules;
368
196k
  uint32_t nslot = 0;
369
370
196k
  if (nrules == 0)
371
20.9k
    goto out;
372
373
2.13M
  while (work) {
374
1.95M
    work = work >> 1;
375
1.95M
    shift++;
376
1.95M
  }
377
175k
  if (shift > 2)
378
163k
    shift = shift - 2;
379
175k
  nslot = UINT32_C(1) << shift;
380
175k
  if (nslot > MAX_AVTAB_HASH_BUCKETS)
381
0
    nslot = MAX_AVTAB_HASH_BUCKETS;
382
175k
  mask = nslot - 1;
383
384
175k
  h->htable = calloc(nslot, sizeof(avtab_ptr_t));
385
175k
  if (!h->htable)
386
0
    return -1;
387
196k
out:
388
196k
  h->nel = 0;
389
196k
  h->nslot = nslot;
390
196k
  h->mask = mask;
391
196k
  return 0;
392
175k
}
393
394
void avtab_hash_eval(avtab_t *h, char *tag)
395
0
{
396
0
  unsigned int i, chain_len, slots_used, max_chain_len;
397
0
  avtab_ptr_t cur;
398
399
0
  slots_used = 0;
400
0
  max_chain_len = 0;
401
0
  for (i = 0; i < h->nslot; i++) {
402
0
    cur = h->htable[i];
403
0
    if (cur) {
404
0
      slots_used++;
405
0
      chain_len = 0;
406
0
      while (cur) {
407
0
        chain_len++;
408
0
        cur = cur->next;
409
0
      }
410
411
0
      if (chain_len > max_chain_len)
412
0
        max_chain_len = chain_len;
413
0
    }
414
0
  }
415
416
0
  printf("%s:  %d entries and %d/%d buckets used, longest chain length %d\n",
417
0
         tag, h->nel, slots_used, h->nslot, max_chain_len);
418
0
}
419
420
int avtab_read_item(struct policy_file *fp, avtab_t *a,
421
        int (*insertf)(avtab_t *a, avtab_key_t *k, avtab_datum_t *d,
422
           void *p),
423
        void *p)
424
19.4k
{
425
19.4k
  uint8_t buf8;
426
19.4k
  uint16_t buf16[4];
427
19.4k
  uint32_t buf32[8], items;
428
19.4k
  avtab_key_t key;
429
19.4k
  avtab_datum_t datum;
430
19.4k
  avtab_extended_perms_t xperms;
431
19.4k
  unsigned int i;
432
19.4k
  int rc;
433
434
19.4k
  memset(&key, 0, sizeof(avtab_key_t));
435
19.4k
  memset(&datum, 0, sizeof(avtab_datum_t));
436
19.4k
  memset(&xperms, 0, sizeof(avtab_extended_perms_t));
437
438
19.4k
  rc = next_entry(buf16, fp, sizeof(uint16_t) * 4);
439
19.4k
  if (rc < 0) {
440
509
    ERR(fp->handle, "truncated entry");
441
509
    return -1;
442
509
  }
443
18.8k
  items = 0;
444
18.8k
  key.source_type = le16_to_cpu(buf16[items++]);
445
18.8k
  key.target_type = le16_to_cpu(buf16[items++]);
446
18.8k
  key.target_class = le16_to_cpu(buf16[items++]);
447
18.8k
  key.specified = le16_to_cpu(buf16[items++]);
448
449
18.8k
  if (key.specified &
450
18.8k
      ~(AVTAB_AV | AVTAB_TYPE | AVTAB_XPERMS | AVTAB_ENABLED)) {
451
22
    ERR(fp->handle, "invalid specifier");
452
22
    return -1;
453
22
  }
454
455
18.8k
  if (__builtin_popcount(key.specified & ~AVTAB_ENABLED) != 1) {
456
14
    ERR(fp->handle, "not exactly one specifier");
457
14
    return -1;
458
14
  }
459
460
18.8k
  if (key.specified & AVTAB_XPERMS) {
461
5.02k
    rc = next_entry(&buf8, fp, sizeof(uint8_t));
462
5.02k
    if (rc < 0) {
463
1
      ERR(fp->handle, "truncated entry");
464
1
      return -1;
465
1
    }
466
5.01k
    xperms.specified = buf8;
467
5.01k
    rc = next_entry(&buf8, fp, sizeof(uint8_t));
468
5.01k
    if (rc < 0) {
469
1
      ERR(fp->handle, "truncated entry");
470
1
      return -1;
471
1
    }
472
5.01k
    xperms.driver = buf8;
473
5.01k
    rc = next_entry(buf32, fp, sizeof(uint32_t) * 8);
474
5.01k
    if (rc < 0) {
475
2
      ERR(fp->handle, "truncated entry");
476
2
      return -1;
477
2
    }
478
45.1k
    for (i = 0; i < ARRAY_SIZE(xperms.perms); i++)
479
40.1k
      xperms.perms[i] = le32_to_cpu(buf32[i]);
480
5.01k
    datum.xperms = &xperms;
481
13.8k
  } else {
482
13.8k
    rc = next_entry(buf32, fp, sizeof(uint32_t));
483
13.8k
    if (rc < 0) {
484
2
      ERR(fp->handle, "truncated entry");
485
2
      return -1;
486
2
    }
487
13.8k
    datum.data = le32_to_cpu(*buf32);
488
13.8k
  }
489
18.8k
  return insertf(a, &key, &datum, p);
490
18.8k
}
491
492
static int avtab_insertf(avtab_t *a, avtab_key_t *k, avtab_datum_t *d,
493
       void *p __attribute__((unused)))
494
10.2k
{
495
10.2k
  return avtab_insert(a, k, d);
496
10.2k
}
497
498
int avtab_read(avtab_t *a, struct policy_file *fp)
499
6.73k
{
500
6.73k
  unsigned int i;
501
6.73k
  int rc;
502
6.73k
  uint32_t buf[1];
503
6.73k
  uint32_t nel;
504
505
6.73k
  rc = next_entry(buf, fp, sizeof(uint32_t));
506
6.73k
  if (rc < 0) {
507
9
    ERR(fp->handle, "truncated table");
508
9
    goto bad;
509
9
  }
510
6.72k
  nel = le32_to_cpu(buf[0]);
511
6.72k
  if (zero_or_saturated(nel) ||
512
6.72k
      exceeds_available_bytes(fp, nel, sizeof(uint32_t) * 3)) {
513
79
    ERR(fp->handle, "table is empty");
514
79
    goto bad;
515
79
  }
516
517
6.64k
  rc = avtab_alloc(a, nel);
518
6.64k
  if (rc) {
519
0
    ERR(fp->handle, "out of memory");
520
0
    goto bad;
521
0
  }
522
523
16.8k
  for (i = 0; i < nel; i++) {
524
10.3k
    rc = avtab_read_item(fp, a, avtab_insertf, NULL);
525
10.3k
    if (rc) {
526
65
      if (rc == SEPOL_ENOMEM)
527
65
        ERR(fp->handle, "out of memory");
528
65
      if (rc == SEPOL_EEXIST)
529
65
        ERR(fp->handle, "duplicate entry");
530
65
      ERR(fp->handle, "failed on entry %d of %u", i, nel);
531
65
      goto bad;
532
65
    }
533
10.3k
  }
534
535
6.58k
  return 0;
536
537
153
bad:
538
153
  avtab_destroy(a);
539
153
  return -1;
540
6.64k
}