Debian Codegraph @

cil/src/cil_post.c

raw
1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <unistd.h>
36
37 #include <sepol/policydb/conditional.h>
38 #include <sepol/errcodes.h>
39
40 #include "cil_internal.h"
41 #include "cil_flavor.h"
42 #include "cil_log.h"
43 #include "cil_mem.h"
44 #include "cil_tree.h"
45 #include "cil_list.h"
46 #include "cil_post.h"
47 #include "cil_policy.h"
48 #include "cil_verify.h"
49 #include "cil_symtab.h"
50 #include "cil_deny.h"
51
52 #define GEN_REQUIRE_ATTR "cil_gen_require" /* Also in libsepol/src/module_to_cil.c */
53 #define TYPEATTR_INFIX "_typeattr_" /* Also in libsepol/src/module_to_cil.c */
54
55 #define spaceship_cmp(a, b) (((a) > (b)) - ((a) < (b)))
56
57 struct fc_data {
58 unsigned int meta;
59 size_t stem_len;
60 size_t str_len;
61 };
62
63 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db);
64 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db);
65
66 static int cats_compare(struct cil_cats *a, struct cil_cats *b)
67 {
68 struct cil_list_item *i, *j;
69 int rc;
70
71 if (a == b) return 0;
72 if (!a) return -1;
73 if (!b) return 1;
74
75 /* Expects cat expression to have been evaluated */
76 cil_list_for_each(i, a->datum_expr) {
77 cil_list_for_each(j, b->datum_expr) {
78 rc = strcmp(DATUM(i->data)->fqn, DATUM(j->data)->fqn);
79 if (!rc) return rc;
80 }
81 }
82 return 0;
83 }
84
85 static int level_compare(struct cil_level *a, struct cil_level *b)
86 {
87 int rc;
88
89 if (a == b) return 0;
90 if (!a) return -1;
91 if (!b) return 1;
92
93 if (a->sens != b->sens) {
94 rc = strcmp(DATUM(a->sens)->fqn, DATUM(b->sens)->fqn);
95 if (rc != 0) return rc;
96 }
97 if (a->cats != b->cats) {
98 return cats_compare(a->cats, b->cats);
99 }
100 return 0;
101 }
102
103 static int range_compare(struct cil_levelrange *a, struct cil_levelrange *b)
104 {
105 int rc;
106
107 if (a == b) return 0;
108 if (!a) return -1;
109 if (!b) return 1;
110
111 if (a->low != b->low) {
112 rc = level_compare(a->low, b->low);
113 if (rc != 0) return rc;
114 }
115 if (a->high != b->high) {
116 return level_compare(a->high, b->high);
117 }
118 return 0;
119 }
120
121 static int context_compare(struct cil_context *a, struct cil_context *b)
122 {
123 int rc;
124
125 if (a->user != b->user) {
126 rc = strcmp(DATUM(a->user)->fqn, DATUM(b->user)->fqn);
127 if (rc != 0) return rc;
128 }
129 if (a->role != b->role) {
130 rc = strcmp(DATUM(a->role)->fqn, DATUM(b->role)->fqn);
131 if (rc != 0) return rc;
132 }
133 if (a->type != b->type) {
134 rc = strcmp(DATUM(a->type)->fqn, DATUM(b->type)->fqn);
135 if (rc != 0) return rc;
136 }
137 if (a->range != b->range) {
138 return range_compare(a->range, b->range);
139 }
140 return 0;
141 }
142
143 static int cil_verify_is_list(struct cil_list *list, enum cil_flavor flavor)
144 {
145 struct cil_list_item *curr;
146
147 cil_list_for_each(curr, list) {
148 switch (curr->flavor) {
149 case CIL_LIST:
150 return CIL_FALSE;
151 break;
152 case CIL_OP:
153 return CIL_FALSE;
154 break;
155 default:
156 if (flavor == CIL_CAT) {
157 struct cil_symtab_datum *d = curr->data;
158 struct cil_tree_node *n = d->nodes->head->data;
159 if (n->flavor == CIL_CATSET) {
160 return CIL_FALSE;
161 }
162 }
163 break;
164 }
165 }
166 return CIL_TRUE;
167 }
168
169 static void cil_post_fc_fill_data(struct fc_data *fc, const char *path)
170 {
171 size_t c = 0;
172 fc->meta = 0;
173 fc->stem_len = 0;
174 fc->str_len = 0;
175
176 while (path[c] != '\0') {
177 switch (path[c]) {
178 case '.':
179 case '^':
180 case '$':
181 case '?':
182 case '*':
183 case '+':
184 case '|':
185 case '[':
186 case '(':
187 case '{':
188 fc->meta = 1;
189 break;
190 case '\\':
191 c++;
192 if (path[c] == '\0') {
193 if (!fc->meta) {
194 fc->stem_len++;
195 }
196 fc->str_len++;
197 return;
198 }
199 /* FALLTHRU */
200 default:
201 if (!fc->meta) {
202 fc->stem_len++;
203 }
204 break;
205 }
206 fc->str_len++;
207 c++;
208 }
209 }
210
211 int cil_post_filecon_compare(const void *a, const void *b)
212 {
213 int rc = 0;
214 struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
215 struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
216 struct fc_data *a_data = cil_malloc(sizeof(*a_data));
217 struct fc_data *b_data = cil_malloc(sizeof(*b_data));
218 char *a_path_str, *a_path, *b_path_str, *b_path;
219
220 a_path_str = a_filecon->path ? DATUM(a_filecon->path)->fqn : a_filecon->path_str;
221 b_path_str = b_filecon->path ? DATUM(b_filecon->path)->fqn : b_filecon->path_str;
222 a_path = cil_malloc(strlen(a_path_str) + 1);
223 b_path = cil_malloc(strlen(b_path_str) + 1);
224 a_path[0] = '\0';
225 b_path[0] = '\0';
226 strcat(a_path, a_path_str);
227 strcat(b_path, b_path_str);
228 cil_post_fc_fill_data(a_data, a_path);
229 cil_post_fc_fill_data(b_data, b_path);
230 if (a_data->meta && !b_data->meta) {
231 rc = -1;
232 } else if (b_data->meta && !a_data->meta) {
233 rc = 1;
234 } else if (a_data->stem_len < b_data->stem_len) {
235 rc = -1;
236 } else if (b_data->stem_len < a_data->stem_len) {
237 rc = 1;
238 } else if (a_data->str_len < b_data->str_len) {
239 rc = -1;
240 } else if (b_data->str_len < a_data->str_len) {
241 rc = 1;
242 } else if (a_filecon->type < b_filecon->type) {
243 rc = -1;
244 } else if (b_filecon->type < a_filecon->type) {
245 rc = 1;
246 } else {
247 rc = strcmp(a_path_str, b_path_str);
248 }
249
250 free(a_path);
251 free(b_path);
252 free(a_data);
253 free(b_data);
254
255 return rc;
256 }
257
258 int cil_post_ibpkeycon_compare(const void *a, const void *b)
259 {
260 int rc = SEPOL_ERR;
261 struct cil_ibpkeycon *aibpkeycon = *(struct cil_ibpkeycon **)a;
262 struct cil_ibpkeycon *bibpkeycon = *(struct cil_ibpkeycon **)b;
263
264 rc = strcmp(aibpkeycon->subnet_prefix_str, bibpkeycon->subnet_prefix_str);
265 if (rc)
266 return rc;
267
268 rc = spaceship_cmp(aibpkeycon->pkey_high - aibpkeycon->pkey_low,
269 bibpkeycon->pkey_high - bibpkeycon->pkey_low);
270 if (rc == 0) {
271 if (aibpkeycon->pkey_low < bibpkeycon->pkey_low)
272 rc = -1;
273 else if (bibpkeycon->pkey_low < aibpkeycon->pkey_low)
274 rc = 1;
275 }
276
277 return rc;
278 }
279
280 int cil_post_portcon_compare(const void *a, const void *b)
281 {
282 int rc = SEPOL_ERR;
283 struct cil_portcon *aportcon = *(struct cil_portcon**)a;
284 struct cil_portcon *bportcon = *(struct cil_portcon**)b;
285
286 rc = spaceship_cmp(aportcon->port_high - aportcon->port_low,
287 bportcon->port_high - bportcon->port_low);
288 if (rc == 0) {
289 if (aportcon->port_low < bportcon->port_low) {
290 rc = -1;
291 } else if (bportcon->port_low < aportcon->port_low) {
292 rc = 1;
293 } else if (aportcon->proto < bportcon->proto) {
294 rc = -1;
295 } else if (aportcon->proto > bportcon->proto) {
296 rc = 1;
297 }
298 }
299
300 return rc;
301 }
302
303 int cil_post_genfscon_compare(const void *a, const void *b)
304 {
305 int rc = SEPOL_ERR;
306 struct cil_genfscon *agenfscon = *(struct cil_genfscon**)a;
307 struct cil_genfscon *bgenfscon = *(struct cil_genfscon**)b;
308
309 rc = strcmp(agenfscon->fs_str, bgenfscon->fs_str);
310 if (rc == 0) {
311 rc = strcmp(agenfscon->path_str, bgenfscon->path_str);
312 }
313
314 return rc;
315 }
316
317 int cil_post_netifcon_compare(const void *a, const void *b)
318 {
319 /* keep in sync with kernel_to_common.c:netif_data_cmp() */
320 const struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
321 const struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
322 const char *a_name = anetifcon->interface_str;
323 const char *b_name = bnetifcon->interface_str;
324 size_t a_stem = strcspn(a_name, "*?");
325 size_t b_stem = strcspn(b_name, "*?");
326 size_t a_len = strlen(a_name);
327 size_t b_len = strlen(b_name);
328 int a_iswildcard = a_stem != a_len;
329 int b_iswildcard = b_stem != b_len;
330 int rc;
331
332 /* order non-wildcards first */
333 rc = spaceship_cmp(a_iswildcard, b_iswildcard);
334 if (rc)
335 return rc;
336
337 /* order non-wildcards alphabetically */
338 if (!a_iswildcard)
339 return strcmp(a_name, b_name);
340
341 /* order by decreasing stem length */
342 rc = spaceship_cmp(a_stem, b_stem);
343 if (rc)
344 return -rc;
345
346 /* order '?' (0x3f) before '*' (0x2A) */
347 rc = spaceship_cmp(a_name[a_stem], b_name[b_stem]);
348 if (rc)
349 return -rc;
350
351 /* order alphabetically */
352 return strcmp(a_name, b_name);
353 }
354
355 int cil_post_ibendportcon_compare(const void *a, const void *b)
356 {
357 int rc = SEPOL_ERR;
358
359 struct cil_ibendportcon *aibendportcon = *(struct cil_ibendportcon **)a;
360 struct cil_ibendportcon *bibendportcon = *(struct cil_ibendportcon **)b;
361
362 rc = strcmp(aibendportcon->dev_name_str, bibendportcon->dev_name_str);
363 if (rc)
364 return rc;
365
366 if (aibendportcon->port < bibendportcon->port)
367 return -1;
368 else if (bibendportcon->port < aibendportcon->port)
369 return 1;
370
371 return rc;
372 }
373
374 int cil_post_nodecon_compare(const void *a, const void *b)
375 {
376 struct cil_nodecon *anodecon;
377 struct cil_nodecon *bnodecon;
378 anodecon = *(struct cil_nodecon**)a;
379 bnodecon = *(struct cil_nodecon**)b;
380
381 /* sort ipv4 before ipv6 */
382 if (anodecon->addr->family != bnodecon->addr->family) {
383 if (anodecon->addr->family == AF_INET) {
384 return -1;
385 } else {
386 return 1;
387 }
388 }
389
390 /* most specific netmask goes first, then order by ip addr */
391 if (anodecon->addr->family == AF_INET) {
392 int rc = memcmp(&anodecon->mask->ip.v4, &bnodecon->mask->ip.v4, sizeof(anodecon->mask->ip.v4));
393 if (rc != 0) {
394 return -1 * rc;
395 }
396 return memcmp(&anodecon->addr->ip.v4, &bnodecon->addr->ip.v4, sizeof(anodecon->addr->ip.v4));
397 } else {
398 int rc = memcmp(&anodecon->mask->ip.v6, &bnodecon->mask->ip.v6, sizeof(anodecon->mask->ip.v6));
399 if (rc != 0) {
400 return -1 * rc;
401 }
402 return memcmp(&anodecon->addr->ip.v6, &bnodecon->addr->ip.v6, sizeof(anodecon->addr->ip.v6));
403 }
404 }
405
406 static int cil_post_pirqcon_compare(const void *a, const void *b)
407 {
408 int rc = SEPOL_ERR;
409 struct cil_pirqcon *apirqcon = *(struct cil_pirqcon**)a;
410 struct cil_pirqcon *bpirqcon = *(struct cil_pirqcon**)b;
411
412 if (apirqcon->pirq < bpirqcon->pirq) {
413 rc = -1;
414 } else if (bpirqcon->pirq < apirqcon->pirq) {
415 rc = 1;
416 } else {
417 rc = 0;
418 }
419
420 return rc;
421 }
422
423 static int cil_post_iomemcon_compare(const void *a, const void *b)
424 {
425 int rc = SEPOL_ERR;
426 struct cil_iomemcon *aiomemcon = *(struct cil_iomemcon**)a;
427 struct cil_iomemcon *biomemcon = *(struct cil_iomemcon**)b;
428
429 rc = spaceship_cmp(aiomemcon->iomem_high - aiomemcon->iomem_low,
430 biomemcon->iomem_high - biomemcon->iomem_low);
431 if (rc == 0) {
432 if (aiomemcon->iomem_low < biomemcon->iomem_low) {
433 rc = -1;
434 } else if (biomemcon->iomem_low < aiomemcon->iomem_low) {
435 rc = 1;
436 }
437 }
438
439 return rc;
440 }
441
442 static int cil_post_ioportcon_compare(const void *a, const void *b)
443 {
444 int rc = SEPOL_ERR;
445 struct cil_ioportcon *aioportcon = *(struct cil_ioportcon**)a;
446 struct cil_ioportcon *bioportcon = *(struct cil_ioportcon**)b;
447
448 rc = spaceship_cmp(aioportcon->ioport_high - aioportcon->ioport_low,
449 bioportcon->ioport_high - bioportcon->ioport_low);
450 if (rc == 0) {
451 if (aioportcon->ioport_low < bioportcon->ioport_low) {
452 rc = -1;
453 } else if (bioportcon->ioport_low < aioportcon->ioport_low) {
454 rc = 1;
455 }
456 }
457
458 return rc;
459 }
460
461 static int cil_post_pcidevicecon_compare(const void *a, const void *b)
462 {
463 int rc = SEPOL_ERR;
464 struct cil_pcidevicecon *apcidevicecon = *(struct cil_pcidevicecon**)a;
465 struct cil_pcidevicecon *bpcidevicecon = *(struct cil_pcidevicecon**)b;
466
467 if (apcidevicecon->dev < bpcidevicecon->dev) {
468 rc = -1;
469 } else if (bpcidevicecon->dev < apcidevicecon->dev) {
470 rc = 1;
471 } else {
472 rc = 0;
473 }
474
475 return rc;
476 }
477
478 static int cil_post_devicetreecon_compare(const void *a, const void *b)
479 {
480 int rc = SEPOL_ERR;
481 struct cil_devicetreecon *adevicetreecon = *(struct cil_devicetreecon**)a;
482 struct cil_devicetreecon *bdevicetreecon = *(struct cil_devicetreecon**)b;
483
484 rc = strcmp(adevicetreecon->path, bdevicetreecon->path);
485
486 return rc;
487 }
488
489 int cil_post_fsuse_compare(const void *a, const void *b)
490 {
491 int rc;
492 struct cil_fsuse *afsuse;
493 struct cil_fsuse *bfsuse;
494 afsuse = *(struct cil_fsuse**)a;
495 bfsuse = *(struct cil_fsuse**)b;
496 if (afsuse->type < bfsuse->type) {
497 rc = -1;
498 } else if (bfsuse->type < afsuse->type) {
499 rc = 1;
500 } else {
501 rc = strcmp(afsuse->fs_str, bfsuse->fs_str);
502 }
503 return rc;
504 }
505
506 static int cil_post_filecon_context_compare(const void *a, const void *b)
507 {
508 struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
509 struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
510 return context_compare(a_filecon->context, b_filecon->context);
511 }
512
513 static int cil_post_ibpkeycon_context_compare(const void *a, const void *b)
514 {
515 struct cil_ibpkeycon *a_ibpkeycon = *(struct cil_ibpkeycon **)a;
516 struct cil_ibpkeycon *b_ibpkeycon = *(struct cil_ibpkeycon **)b;
517 return context_compare(a_ibpkeycon->context, b_ibpkeycon->context);
518 }
519
520 static int cil_post_portcon_context_compare(const void *a, const void *b)
521 {
522 struct cil_portcon *a_portcon = *(struct cil_portcon**)a;
523 struct cil_portcon *b_portcon = *(struct cil_portcon**)b;
524 return context_compare(a_portcon->context, b_portcon->context);
525 }
526
527 static int cil_post_genfscon_context_compare(const void *a, const void *b)
528 {
529 struct cil_genfscon *a_genfscon = *(struct cil_genfscon**)a;
530 struct cil_genfscon *b_genfscon = *(struct cil_genfscon**)b;
531 return context_compare(a_genfscon->context, b_genfscon->context);
532 }
533
534 static int cil_post_netifcon_context_compare(const void *a, const void *b)
535 {
536 int rc;
537 struct cil_netifcon *a_netifcon = *(struct cil_netifcon**)a;
538 struct cil_netifcon *b_netifcon = *(struct cil_netifcon**)b;
539 rc = context_compare(a_netifcon->if_context, b_netifcon->if_context);
540 if (rc != 0) {
541 return rc;
542 }
543 return context_compare(a_netifcon->packet_context, b_netifcon->packet_context);
544 }
545
546 static int cil_post_ibendportcon_context_compare(const void *a, const void *b)
547 {
548 struct cil_ibendportcon *a_ibendportcon = *(struct cil_ibendportcon **)a;
549 struct cil_ibendportcon *b_ibendportcon = *(struct cil_ibendportcon **)b;
550 return context_compare(a_ibendportcon->context, b_ibendportcon->context);
551 }
552
553 static int cil_post_nodecon_context_compare(const void *a, const void *b)
554 {
555 struct cil_nodecon *a_nodecon = *(struct cil_nodecon **)a;
556 struct cil_nodecon *b_nodecon = *(struct cil_nodecon **)b;
557 return context_compare(a_nodecon->context, b_nodecon->context);
558 }
559
560 static int cil_post_pirqcon_context_compare(const void *a, const void *b)
561 {
562 struct cil_pirqcon *a_pirqcon = *(struct cil_pirqcon**)a;
563 struct cil_pirqcon *b_pirqcon = *(struct cil_pirqcon**)b;
564 return context_compare(a_pirqcon->context, b_pirqcon->context);
565 }
566
567 static int cil_post_iomemcon_context_compare(const void *a, const void *b)
568 {
569 struct cil_iomemcon *a_iomemcon = *(struct cil_iomemcon**)a;
570 struct cil_iomemcon *b_iomemcon = *(struct cil_iomemcon**)b;
571 return context_compare(a_iomemcon->context, b_iomemcon->context);
572 }
573
574 static int cil_post_ioportcon_context_compare(const void *a, const void *b)
575 {
576 struct cil_ioportcon *a_ioportcon = *(struct cil_ioportcon**)a;
577 struct cil_ioportcon *b_ioportcon = *(struct cil_ioportcon**)b;
578 return context_compare(a_ioportcon->context, b_ioportcon->context);
579 }
580
581 static int cil_post_pcidevicecon_context_compare(const void *a, const void *b)
582 {
583 struct cil_pcidevicecon *a_pcidevicecon = *(struct cil_pcidevicecon**)a;
584 struct cil_pcidevicecon *b_pcidevicecon = *(struct cil_pcidevicecon**)b;
585 return context_compare(a_pcidevicecon->context, b_pcidevicecon->context);
586 }
587
588 static int cil_post_devicetreecon_context_compare(const void *a, const void *b)
589 {
590 struct cil_devicetreecon *a_devicetreecon = *(struct cil_devicetreecon**)a;
591 struct cil_devicetreecon *b_devicetreecon = *(struct cil_devicetreecon**)b;
592 return context_compare(a_devicetreecon->context, b_devicetreecon->context);
593 }
594
595 static int cil_post_fsuse_context_compare(const void *a, const void *b)
596 {
597 struct cil_fsuse *a_fsuse = *(struct cil_fsuse**)a;
598 struct cil_fsuse *b_fsuse = *(struct cil_fsuse**)b;
599 return context_compare(a_fsuse->context, b_fsuse->context);
600 }
601
602 static int __cil_post_db_count_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
603 {
604 struct cil_db *db = extra_args;
605
606 switch(node->flavor) {
607 case CIL_BLOCK: {
608 struct cil_block *blk = node->data;
609 if (blk->is_abstract == CIL_TRUE) {
610 *finished = CIL_TREE_SKIP_HEAD;
611 }
612 break;
613 }
614 case CIL_MACRO:
615 *finished = CIL_TREE_SKIP_HEAD;
616 break;
617 case CIL_CLASS: {
618 struct cil_class *class = node->data;
619 if (class->datum.nodes->head->data == node) {
620 // Multiple nodes can point to the same datum. Only count once.
621 db->num_classes++;
622 }
623 break;
624 }
625 case CIL_TYPE: {
626 struct cil_type *type = node->data;
627 if (type->datum.nodes->head->data == node) {
628 // Multiple nodes can point to the same datum. Only count once.
629 type->value = db->num_types;
630 db->num_types++;
631 db->num_types_and_attrs++;
632 }
633 break;
634 }
635 case CIL_TYPEATTRIBUTE: {
636 struct cil_typeattribute *attr = node->data;
637 if (attr->datum.nodes->head->data == node) {
638 // Multiple nodes can point to the same datum. Only count once.
639 db->num_types_and_attrs++;
640 }
641 break;
642 }
643
644 case CIL_ROLE: {
645 struct cil_role *role = node->data;
646 if (role->datum.nodes->head->data == node) {
647 // Multiple nodes can point to the same datum. Only count once.
648 role->value = db->num_roles;
649 db->num_roles++;
650 }
651 break;
652 }
653 case CIL_USER: {
654 struct cil_user *user = node->data;
655 if (user->datum.nodes->head->data == node) {
656 // multiple AST nodes can point to the same cil_user data (like if
657 // copied from a macro). This check ensures we only count the
658 // duplicates once
659 user->value = db->num_users;
660 db->num_users++;
661 }
662 break;
663 }
664 case CIL_NETIFCON:
665 db->netifcon->count++;
666 break;
667 case CIL_GENFSCON:
668 db->genfscon->count++;
669 break;
670 case CIL_FILECON:
671 db->filecon->count++;
672 break;
673 case CIL_NODECON:
674 db->nodecon->count++;
675 break;
676 case CIL_IBPKEYCON:
677 db->ibpkeycon->count++;
678 break;
679 case CIL_IBENDPORTCON:
680 db->ibendportcon->count++;
681 break;
682 case CIL_PORTCON:
683 db->portcon->count++;
684 break;
685 case CIL_PIRQCON:
686 db->pirqcon->count++;
687 break;
688 case CIL_IOMEMCON:
689 db->iomemcon->count++;
690 break;
691 case CIL_IOPORTCON:
692 db->ioportcon->count++;
693 break;
694 case CIL_PCIDEVICECON:
695 db->pcidevicecon->count++;
696 break;
697 case CIL_DEVICETREECON:
698 db->devicetreecon->count++;
699 break;
700 case CIL_FSUSE:
701 db->fsuse->count++;
702 break;
703 default:
704 break;
705 }
706
707 return SEPOL_OK;
708 }
709
710 static int __cil_post_db_array_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
711 {
712 struct cil_db *db = extra_args;
713
714 switch(node->flavor) {
715 case CIL_BLOCK: {
716 struct cil_block *blk = node->data;
717 if (blk->is_abstract == CIL_TRUE) {
718 *finished = CIL_TREE_SKIP_HEAD;
719 }
720 break;
721 }
722 case CIL_MACRO:
723 *finished = CIL_TREE_SKIP_HEAD;
724 break;
725 case CIL_TYPE: {
726 struct cil_type *type = node->data;
727 if (db->val_to_type == NULL) {
728 db->val_to_type = cil_malloc(sizeof(*db->val_to_type) * db->num_types);
729 }
730 db->val_to_type[type->value] = type;
731 break;
732 }
733 case CIL_ROLE: {
734 struct cil_role *role = node->data;
735 if (db->val_to_role == NULL) {
736 db->val_to_role = cil_malloc(sizeof(*db->val_to_role) * db->num_roles);
737 }
738 db->val_to_role[role->value] = role;
739 break;
740 }
741 case CIL_USER: {
742 struct cil_user *user= node->data;
743 if (db->val_to_user == NULL) {
744 db->val_to_user = cil_malloc(sizeof(*db->val_to_user) * db->num_users);
745 }
746 db->val_to_user[user->value] = user;
747 break;
748 }
749 case CIL_USERPREFIX: {
750 cil_list_append(db->userprefixes, CIL_USERPREFIX, node->data);
751 break;
752 }
753 case CIL_SELINUXUSER: {
754 cil_list_prepend(db->selinuxusers, CIL_SELINUXUSER, node->data);
755 break;
756 }
757 case CIL_SELINUXUSERDEFAULT: {
758 cil_list_append(db->selinuxusers, CIL_SELINUXUSERDEFAULT, node->data);
759 break;
760 }
761 case CIL_NETIFCON: {
762 struct cil_sort *sort = db->netifcon;
763 uint32_t count = sort->count;
764 uint32_t i = sort->index;
765 if (sort->array == NULL) {
766 sort->array = cil_malloc(sizeof(*sort->array)*count);
767 }
768 sort->array[i] = node->data;
769 sort->index++;
770 break;
771 }
772 case CIL_IBENDPORTCON: {
773 struct cil_sort *sort = db->ibendportcon;
774 uint32_t count = sort->count;
775 uint32_t i = sort->index;
776
777 if (!sort->array)
778 sort->array = cil_malloc(sizeof(*sort->array) * count);
779 sort->array[i] = node->data;
780 sort->index++;
781 break;
782 }
783 case CIL_FSUSE: {
784 struct cil_sort *sort = db->fsuse;
785 uint32_t count = sort->count;
786 uint32_t i = sort->index;
787 if (sort->array == NULL) {
788 sort->array = cil_malloc(sizeof(*sort->array)*count);
789 }
790 sort->array[i] = node->data;
791 sort->index++;
792 break;
793 }
794 case CIL_GENFSCON: {
795 struct cil_sort *sort = db->genfscon;
796 uint32_t count = sort->count;
797 uint32_t i = sort->index;
798 if (sort->array == NULL) {
799 sort->array = cil_malloc(sizeof(*sort->array)*count);
800 }
801 sort->array[i] = node->data;
802 sort->index++;
803 break;
804 }
805 case CIL_FILECON: {
806 struct cil_sort *sort = db->filecon;
807 uint32_t count = sort->count;
808 uint32_t i = sort->index;
809 if (sort->array == NULL) {
810 sort->array = cil_malloc(sizeof(*sort->array)*count);
811 }
812 sort->array[i] = node->data;
813 sort->index++;
814 break;
815 }
816 case CIL_NODECON: {
817 struct cil_sort *sort = db->nodecon;
818 uint32_t count = sort->count;
819 uint32_t i = sort->index;
820 if (sort->array == NULL) {
821 sort->array = cil_malloc(sizeof(*sort->array)*count);
822 }
823 sort->array[i] = node->data;
824 sort->index++;
825 break;
826 }
827 case CIL_IBPKEYCON: {
828 struct cil_sort *sort = db->ibpkeycon;
829 uint32_t count = sort->count;
830 uint32_t i = sort->index;
831
832 if (!sort->array)
833 sort->array = cil_malloc(sizeof(*sort->array) * count);
834 sort->array[i] = node->data;
835 sort->index++;
836 break;
837 }
838 case CIL_PORTCON: {
839 struct cil_sort *sort = db->portcon;
840 uint32_t count = sort->count;
841 uint32_t i = sort->index;
842 if (sort->array == NULL) {
843 sort->array = cil_malloc(sizeof(*sort->array)*count);
844 }
845 sort->array[i] = node->data;
846 sort->index++;
847 break;
848 }
849 case CIL_PIRQCON: {
850 struct cil_sort *sort = db->pirqcon;
851 uint32_t count = sort->count;
852 uint32_t i = sort->index;
853 if (sort->array == NULL) {
854 sort->array = cil_malloc(sizeof(*sort->array)*count);
855 }
856 sort->array[i] = node->data;
857 sort->index++;
858 break;
859 }
860 case CIL_IOMEMCON: {
861 struct cil_sort *sort = db->iomemcon;
862 uint32_t count = sort->count;
863 uint32_t i = sort->index;
864 if (sort->array == NULL) {
865 sort->array = cil_malloc(sizeof(*sort->array)*count);
866 }
867 sort->array[i] = node->data;
868 sort->index++;
869 break;
870 }
871 case CIL_IOPORTCON: {
872 struct cil_sort *sort = db->ioportcon;
873 uint32_t count = sort->count;
874 uint32_t i = sort->index;
875 if (sort->array == NULL) {
876 sort->array = cil_malloc(sizeof(*sort->array)*count);
877 }
878 sort->array[i] = node->data;
879 sort->index++;
880 break;
881 }
882 case CIL_PCIDEVICECON: {
883 struct cil_sort *sort = db->pcidevicecon;
884 uint32_t count = sort->count;
885 uint32_t i = sort->index;
886 if (sort->array == NULL) {
887 sort->array = cil_malloc(sizeof(*sort->array)*count);
888 }
889 sort->array[i] = node->data;
890 sort->index++;
891 break;
892 }
893 case CIL_DEVICETREECON: {
894 struct cil_sort *sort = db->devicetreecon;
895 uint32_t count = sort->count;
896 uint32_t i = sort->index;
897 if (sort->array == NULL) {
898 sort->array = cil_malloc(sizeof(*sort->array)*count);
899 }
900 sort->array[i] = node->data;
901 sort->index++;
902 break;
903 }
904 default:
905 break;
906 }
907
908 return SEPOL_OK;
909 }
910
911 static int __evaluate_type_expression(struct cil_typeattribute *attr, struct cil_db *db)
912 {
913 int rc;
914
915 attr->types = cil_malloc(sizeof(*attr->types));
916 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->types, db->num_types, db);
917 if (rc != SEPOL_OK) {
918 cil_log(CIL_ERR, "Failed to expand type attribute to bitmap\n");
919 ebitmap_destroy(attr->types);
920 free(attr->types);
921 attr->types = NULL;
922 }
923 return rc;
924 }
925
926 static int __cil_type_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
927 {
928 int rc = SEPOL_ERR;
929 struct cil_tree_node *node = datum->nodes->head->data;
930
931 ebitmap_init(bitmap);
932
933 if (node->flavor == CIL_TYPEATTRIBUTE) {
934 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
935 if (attr->types == NULL) {
936 rc = __evaluate_type_expression(attr, db);
937 if (rc != SEPOL_OK) goto exit;
938 }
939 ebitmap_union(bitmap, attr->types);
940 } else if (node->flavor == CIL_TYPEALIAS) {
941 struct cil_alias *alias = (struct cil_alias *)datum;
942 struct cil_type *type = alias->actual;
943 if (ebitmap_set_bit(bitmap, type->value, 1)) {
944 cil_log(CIL_ERR, "Failed to set type bit\n");
945 ebitmap_destroy(bitmap);
946 goto exit;
947 }
948 } else {
949 struct cil_type *type = (struct cil_type *)datum;
950 if (ebitmap_set_bit(bitmap, type->value, 1)) {
951 cil_log(CIL_ERR, "Failed to set type bit\n");
952 ebitmap_destroy(bitmap);
953 goto exit;
954 }
955 }
956
957 return SEPOL_OK;
958
959 exit:
960 return rc;
961 }
962
963 static int __evaluate_user_expression(struct cil_userattribute *attr, struct cil_db *db)
964 {
965 int rc;
966
967 attr->users = cil_malloc(sizeof(*attr->users));
968 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->users, db->num_users, db);
969 if (rc != SEPOL_OK) {
970 cil_log(CIL_ERR, "Failed to expand user attribute to bitmap\n");
971 ebitmap_destroy(attr->users);
972 free(attr->users);
973 attr->users = NULL;
974 }
975 return rc;
976 }
977
978 static int __cil_user_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
979 {
980 int rc = SEPOL_ERR;
981 struct cil_tree_node *node = datum->nodes->head->data;
982 struct cil_userattribute *attr = NULL;
983 struct cil_user *user = NULL;
984
985 ebitmap_init(bitmap);
986
987 if (node->flavor == CIL_USERATTRIBUTE) {
988 attr = (struct cil_userattribute *)datum;
989 if (attr->users == NULL) {
990 rc = __evaluate_user_expression(attr, db);
991 if (rc != SEPOL_OK) {
992 goto exit;
993 }
994 }
995 ebitmap_union(bitmap, attr->users);
996 } else {
997 user = (struct cil_user *)datum;
998 if (ebitmap_set_bit(bitmap, user->value, 1)) {
999 cil_log(CIL_ERR, "Failed to set user bit\n");
1000 ebitmap_destroy(bitmap);
1001 goto exit;
1002 }
1003 }
1004
1005 return SEPOL_OK;
1006
1007 exit:
1008 return rc;
1009 }
1010
1011 static int __evaluate_role_expression(struct cil_roleattribute *attr, struct cil_db *db)
1012 {
1013 int rc;
1014
1015 attr->roles = cil_malloc(sizeof(*attr->roles));
1016 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->roles, db->num_roles, db);
1017 if (rc != SEPOL_OK) {
1018 cil_log(CIL_ERR, "Failed to expand role attribute to bitmap\n");
1019 ebitmap_destroy(attr->roles);
1020 free(attr->roles);
1021 attr->roles = NULL;
1022 }
1023 return rc;
1024 }
1025
1026 static int __cil_role_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
1027 {
1028 int rc = SEPOL_ERR;
1029 struct cil_tree_node *node = datum->nodes->head->data;
1030
1031 ebitmap_init(bitmap);
1032
1033 if (node->flavor == CIL_ROLEATTRIBUTE) {
1034 struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
1035 if (attr->roles == NULL) {
1036 rc = __evaluate_role_expression(attr, db);
1037 if (rc != SEPOL_OK) goto exit;
1038 }
1039 ebitmap_union(bitmap, attr->roles);
1040 } else {
1041 struct cil_role *role = (struct cil_role *)datum;
1042 if (ebitmap_set_bit(bitmap, role->value, 1)) {
1043 cil_log(CIL_ERR, "Failed to set role bit\n");
1044 ebitmap_destroy(bitmap);
1045 goto exit;
1046 }
1047 }
1048
1049 return SEPOL_OK;
1050
1051 exit:
1052 return rc;
1053 }
1054
1055 static int __evaluate_permissionx_expression(struct cil_permissionx *permx, struct cil_db *db)
1056 {
1057 int rc;
1058
1059 permx->perms = cil_malloc(sizeof(*permx->perms));
1060 ebitmap_init(permx->perms);
1061
1062 rc = __cil_expr_to_bitmap(permx->expr_str, permx->perms, 0x10000, db); // max is one more than 0xFFFF
1063 if (rc != SEPOL_OK) {
1064 cil_log(CIL_ERR, "Failed to expand permissionx expression\n");
1065 ebitmap_destroy(permx->perms);
1066 free(permx->perms);
1067 permx->perms = NULL;
1068 }
1069
1070 return rc;
1071 }
1072
1073 static int __cil_permx_str_to_int(char *permx_str, uint16_t *val)
1074 {
1075 char *endptr = NULL;
1076 long lval = strtol(permx_str, &endptr, 0);
1077
1078 if (*endptr != '\0') {
1079 cil_log(CIL_ERR, "permissionx value %s not valid number\n", permx_str);
1080 goto exit;
1081 }
1082 if (lval < 0x0000 || lval > 0xFFFF) {
1083 cil_log(CIL_ERR, "permissionx value %s must be between 0x0000 and 0xFFFF\n", permx_str);
1084 goto exit;
1085 }
1086
1087 *val = (uint16_t)lval;
1088
1089 return SEPOL_OK;
1090
1091 exit:
1092 return SEPOL_ERR;
1093 }
1094
1095 static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1096 {
1097 int rc = SEPOL_ERR;
1098 uint16_t val;
1099
1100 rc = __cil_permx_str_to_int((char*)datum, &val);
1101 if (rc != SEPOL_OK) {
1102 goto exit;
1103 }
1104
1105 ebitmap_init(bitmap);
1106 if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
1107 cil_log(CIL_ERR, "Failed to set permissionx bit\n");
1108 ebitmap_destroy(bitmap);
1109 goto exit;
1110 }
1111
1112 return SEPOL_OK;
1113
1114 exit:
1115 return rc;
1116 }
1117
1118 static int __cil_perm_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1119 {
1120 struct cil_perm *perm = (struct cil_perm *)datum;
1121 unsigned int value = perm->value;
1122
1123 ebitmap_init(bitmap);
1124 if (ebitmap_set_bit(bitmap, value, 1)) {
1125 cil_log(CIL_INFO, "Failed to set perm bit\n");
1126 ebitmap_destroy(bitmap);
1127 return SEPOL_ERR;
1128 }
1129
1130 return SEPOL_OK;
1131 }
1132
1133 static int __evaluate_cat_expression(struct cil_cats *cats, struct cil_db *db)
1134 {
1135 int rc = SEPOL_ERR;
1136 ebitmap_t bitmap;
1137 struct cil_list *new;
1138 struct cil_list_item *curr;
1139
1140 if (cats->evaluated == CIL_TRUE) {
1141 return SEPOL_OK;
1142 }
1143
1144 if (cil_verify_is_list(cats->datum_expr, CIL_CAT)) {
1145 return SEPOL_OK;
1146 }
1147
1148 ebitmap_init(&bitmap);
1149 rc = __cil_expr_to_bitmap(cats->datum_expr, &bitmap, db->num_cats, db);
1150 if (rc != SEPOL_OK) {
1151 cil_log(CIL_ERR, "Failed to expand category expression to bitmap\n");
1152 ebitmap_destroy(&bitmap);
1153 goto exit;
1154 }
1155
1156 cil_list_init(&new, CIL_CAT);
1157
1158 cil_list_for_each(curr, db->catorder) {
1159 struct cil_cat *cat = curr->data;
1160 if (ebitmap_get_bit(&bitmap, cat->value)) {
1161 cil_list_append(new, CIL_DATUM, cat);
1162 }
1163 }
1164
1165 ebitmap_destroy(&bitmap);
1166 cil_list_destroy(&cats->datum_expr, CIL_FALSE);
1167 cats->datum_expr = new;
1168
1169 cats->evaluated = CIL_TRUE;
1170
1171 return SEPOL_OK;
1172
1173 exit:
1174 return rc;
1175 }
1176
1177 static int __cil_cat_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
1178 {
1179 int rc = SEPOL_ERR;
1180 struct cil_tree_node *node = datum->nodes->head->data;
1181
1182 ebitmap_init(bitmap);
1183
1184 if (node->flavor == CIL_CATSET) {
1185 struct cil_catset *catset = (struct cil_catset *)datum;
1186 struct cil_list_item *curr;
1187 if (catset->cats->evaluated == CIL_FALSE) {
1188 rc = __evaluate_cat_expression(catset->cats, db);
1189 if (rc != SEPOL_OK) goto exit;
1190 }
1191 for (curr = catset->cats->datum_expr->head; curr; curr = curr->next) {
1192 struct cil_cat *cat = (struct cil_cat *)curr->data;
1193 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1194 cil_log(CIL_ERR, "Failed to set cat bit\n");
1195 ebitmap_destroy(bitmap);
1196 goto exit;
1197 }
1198 }
1199 } else if (node->flavor == CIL_CATALIAS) {
1200 struct cil_alias *alias = (struct cil_alias *)datum;
1201 struct cil_cat *cat = alias->actual;
1202 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1203 cil_log(CIL_ERR, "Failed to set cat bit\n");
1204 ebitmap_destroy(bitmap);
1205 goto exit;
1206 }
1207 } else {
1208 struct cil_cat *cat = (struct cil_cat *)datum;
1209 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1210 cil_log(CIL_ERR, "Failed to set cat bit\n");
1211 ebitmap_destroy(bitmap);
1212 goto exit;
1213 }
1214 }
1215
1216 return SEPOL_OK;
1217
1218 exit:
1219 return rc;
1220 }
1221
1222 static int __cil_cat_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1223 {
1224 int rc = SEPOL_ERR;
1225 struct cil_symtab_datum *d1 = i1->data;
1226 struct cil_symtab_datum *d2 = i2->data;
1227 struct cil_tree_node *n1 = d1->nodes->head->data;
1228 struct cil_tree_node *n2 = d2->nodes->head->data;
1229 struct cil_cat *c1 = (struct cil_cat *)d1;
1230 struct cil_cat *c2 = (struct cil_cat *)d2;
1231
1232 if (n1->flavor == CIL_CATSET || n2->flavor == CIL_CATSET) {
1233 cil_log(CIL_ERR, "Category sets cannot be used in a category range\n");
1234 goto exit;
1235 }
1236
1237 if (n1->flavor == CIL_CATALIAS) {
1238 struct cil_alias *alias = (struct cil_alias *)d1;
1239 c1 = alias->actual;
1240 }
1241
1242 if (n2->flavor == CIL_CATALIAS) {
1243 struct cil_alias *alias = (struct cil_alias *)d2;
1244 c2 = alias->actual;
1245 }
1246
1247 if (c1->value > c2->value) {
1248 cil_log(CIL_ERR, "Invalid category range\n");
1249 goto exit;
1250 }
1251
1252 if (ebitmap_init_range(bitmap, c1->value, c2->value)) {
1253 cil_log(CIL_ERR, "Failed to set cat bit\n");
1254 ebitmap_destroy(bitmap);
1255 goto exit;
1256 }
1257
1258 return SEPOL_OK;
1259
1260 exit:
1261 return rc;
1262 }
1263
1264 static int __cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1265 {
1266 int rc = SEPOL_ERR;
1267 char *p1 = i1->data;
1268 char *p2 = i2->data;
1269 uint16_t v1;
1270 uint16_t v2;
1271
1272 rc = __cil_permx_str_to_int(p1, &v1);
1273 if (rc != SEPOL_OK) {
1274 goto exit;
1275 }
1276
1277 rc = __cil_permx_str_to_int(p2, &v2);
1278 if (rc != SEPOL_OK) {
1279 goto exit;
1280 }
1281
1282 if (ebitmap_init_range(bitmap, v1, v2)) {
1283 cil_log(CIL_ERR, "Failed to set permissionx bits\n");
1284 ebitmap_destroy(bitmap);
1285 goto exit;
1286 }
1287
1288 return SEPOL_OK;
1289
1290 exit:
1291 return rc;
1292 }
1293
1294 static int __cil_expr_to_bitmap_helper(struct cil_list_item *curr, enum cil_flavor flavor, ebitmap_t *bitmap, int max, struct cil_db *db)
1295 {
1296 int rc = SEPOL_ERR;
1297
1298 if (curr->flavor == CIL_DATUM) {
1299 switch (flavor) {
1300 case CIL_TYPE:
1301 rc = __cil_type_to_bitmap(curr->data, bitmap, db);
1302 break;
1303 case CIL_ROLE:
1304 rc = __cil_role_to_bitmap(curr->data, bitmap, db);
1305 break;
1306 case CIL_USER:
1307 rc = __cil_user_to_bitmap(curr->data, bitmap, db);
1308 break;
1309 case CIL_PERM:
1310 rc = __cil_perm_to_bitmap(curr->data, bitmap, db);
1311 break;
1312 case CIL_CAT:
1313 rc = __cil_cat_to_bitmap(curr->data, bitmap, db);
1314 break;
1315 default:
1316 rc = SEPOL_ERR;
1317 }
1318 } else if (curr->flavor == CIL_LIST) {
1319 struct cil_list *l = curr->data;
1320 ebitmap_init(bitmap);
1321 rc = __cil_expr_to_bitmap(l, bitmap, max, db);
1322 if (rc != SEPOL_OK) {
1323 ebitmap_destroy(bitmap);
1324 }
1325 } else if (flavor == CIL_PERMISSIONX) {
1326 // permissionx expressions aren't resolved into anything, so curr->flavor
1327 // is just a CIL_STRING, not a CIL_DATUM, so just check on flavor for those
1328 rc = __cil_permx_to_bitmap(curr->data, bitmap, db);
1329 }
1330
1331 return rc;
1332 }
1333
1334 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db)
1335 {
1336 int rc = SEPOL_ERR;
1337 struct cil_list_item *curr;
1338 enum cil_flavor flavor;
1339 ebitmap_t tmp, b1, b2;
1340
1341 if (expr == NULL || expr->head == NULL) {
1342 return SEPOL_OK;
1343 }
1344
1345 curr = expr->head;
1346 flavor = expr->flavor;
1347
1348 ebitmap_init(&tmp);
1349
1350 if (curr->flavor == CIL_OP) {
1351 enum cil_flavor op = (enum cil_flavor)(uintptr_t)curr->data;
1352
1353 if (op == CIL_ALL) {
1354 rc = ebitmap_init_range(&tmp, 0, max - 1);
1355 if (rc != SEPOL_OK) {
1356 cil_log(CIL_INFO, "Failed to expand 'all' operator\n");
1357 ebitmap_destroy(&tmp);
1358 goto exit;
1359 }
1360 } else if (op == CIL_RANGE) {
1361 if (flavor == CIL_CAT) {
1362 rc = __cil_cat_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1363 if (rc != SEPOL_OK) {
1364 cil_log(CIL_INFO, "Failed to expand category range\n");
1365 goto exit;
1366 }
1367 } else if (flavor == CIL_PERMISSIONX) {
1368 rc = __cil_permissionx_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1369 if (rc != SEPOL_OK) {
1370 cil_log(CIL_INFO, "Failed to expand category range\n");
1371 goto exit;
1372 }
1373 } else {
1374 cil_log(CIL_INFO, "Range operation only supported for categories permissionx\n");
1375 rc = SEPOL_ERR;
1376 goto exit;
1377 }
1378 } else {
1379 rc = __cil_expr_to_bitmap_helper(curr->next, flavor, &b1, max, db);
1380 if (rc != SEPOL_OK) {
1381 cil_log(CIL_INFO, "Failed to get first operand bitmap\n");
1382 goto exit;
1383 }
1384
1385 if (op == CIL_NOT) {
1386 rc = ebitmap_not(&tmp, &b1, max);
1387 ebitmap_destroy(&b1);
1388 if (rc != SEPOL_OK) {
1389 cil_log(CIL_INFO, "Failed to NOT bitmap\n");
1390 ebitmap_destroy(&tmp);
1391 goto exit;
1392 }
1393 } else {
1394 rc = __cil_expr_to_bitmap_helper(curr->next->next, flavor, &b2, max, db);
1395 if (rc != SEPOL_OK) {
1396 cil_log(CIL_INFO, "Failed to get second operand bitmap\n");
1397 ebitmap_destroy(&b1);
1398 goto exit;
1399 }
1400
1401 if (op == CIL_OR) {
1402 rc = ebitmap_or(&tmp, &b1, &b2);
1403 } else if (op == CIL_AND) {
1404 rc = ebitmap_and(&tmp, &b1, &b2);
1405 } else if (op == CIL_XOR) {
1406 rc = ebitmap_xor(&tmp, &b1, &b2);
1407 } else {
1408 rc = SEPOL_ERR;
1409 }
1410 ebitmap_destroy(&b1);
1411 ebitmap_destroy(&b2);
1412 if (rc != SEPOL_OK) {
1413 cil_log(CIL_INFO, "Failed to apply operator to bitmaps\n");
1414 ebitmap_destroy(&tmp);
1415 goto exit;
1416 }
1417 }
1418 }
1419 } else {
1420 ebitmap_init(&tmp);
1421 for (;curr; curr = curr->next) {
1422 rc = __cil_expr_to_bitmap_helper(curr, flavor, &b2, max, db);
1423 if (rc != SEPOL_OK) {
1424 cil_log(CIL_INFO, "Failed to get operand in list\n");
1425 ebitmap_destroy(&tmp);
1426 goto exit;
1427 }
1428 b1 = tmp;
1429 rc = ebitmap_or(&tmp, &b1, &b2);
1430 ebitmap_destroy(&b1);
1431 ebitmap_destroy(&b2);
1432 if (rc != SEPOL_OK) {
1433 cil_log(CIL_INFO, "Failed to OR operands in list\n");
1434 ebitmap_destroy(&tmp);
1435 goto exit;
1436 }
1437
1438 }
1439 }
1440
1441 ebitmap_union(out, &tmp);
1442 ebitmap_destroy(&tmp);
1443
1444 return SEPOL_OK;
1445
1446 exit:
1447 return rc;
1448 }
1449
1450 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db)
1451 {
1452 int rc = SEPOL_ERR;
1453 struct cil_list_item *expr;
1454
1455 ebitmap_init(out);
1456
1457 if (expr_list == NULL) {
1458 return SEPOL_OK;
1459 }
1460
1461 cil_list_for_each(expr, expr_list) {
1462 ebitmap_t bitmap;
1463 struct cil_list *l = (struct cil_list *)expr->data;
1464 ebitmap_init(&bitmap);
1465 rc = __cil_expr_to_bitmap(l, &bitmap, max, db);
1466 if (rc != SEPOL_OK) {
1467 cil_log(CIL_INFO, "Failed to expand expression list to bitmap\n");
1468 ebitmap_destroy(&bitmap);
1469 goto exit;
1470 }
1471 ebitmap_union(out, &bitmap);
1472 ebitmap_destroy(&bitmap);
1473 }
1474
1475 return SEPOL_OK;
1476
1477 exit:
1478 return SEPOL_ERR;
1479 }
1480
1481 static int cil_typeattribute_used(struct cil_typeattribute *attr, struct cil_db *db)
1482 {
1483 if (!attr->used) {
1484 return CIL_FALSE;
1485 }
1486
1487 if (attr->used & CIL_ATTR_EXPAND_FALSE) {
1488 return CIL_TRUE;
1489 }
1490
1491 if (attr->used & CIL_ATTR_EXPAND_TRUE) {
1492 return CIL_FALSE;
1493 }
1494
1495 if (attr->used & CIL_ATTR_CONSTRAINT) {
1496 return CIL_TRUE;
1497 }
1498
1499 if (db->attrs_expand_generated || attr->used == CIL_ATTR_NEVERALLOW) {
1500 if (strcmp(DATUM(attr)->name, GEN_REQUIRE_ATTR) == 0) {
1501 return CIL_FALSE;
1502 } else if (strstr(DATUM(attr)->name, TYPEATTR_INFIX) != NULL) {
1503 return CIL_FALSE;
1504 }
1505
1506 if (attr->used == CIL_ATTR_NEVERALLOW) {
1507 return CIL_TRUE;
1508 }
1509 }
1510
1511 if (attr->used == CIL_ATTR_AVRULE) {
1512 if (ebitmap_cardinality(attr->types) < db->attrs_expand_size) {
1513 return CIL_FALSE;
1514 }
1515 }
1516
1517 return CIL_TRUE;
1518 }
1519
1520 static void __mark_neverallow_attrs(struct cil_list *expr_list)
1521 {
1522 struct cil_list_item *curr;
1523
1524 if (!expr_list) {
1525 return;
1526 }
1527
1528 cil_list_for_each(curr, expr_list) {
1529 if (curr->flavor == CIL_DATUM) {
1530 if (FLAVOR(curr->data) == CIL_TYPEATTRIBUTE) {
1531 struct cil_typeattribute *attr = curr->data;
1532 if (strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1533 __mark_neverallow_attrs(attr->expr_list);
1534 } else {
1535 attr->used |= CIL_ATTR_NEVERALLOW;
1536 }
1537 }
1538 } else if (curr->flavor == CIL_LIST) {
1539 __mark_neverallow_attrs(curr->data);
1540 }
1541 }
1542 }
1543
1544 static int __cil_post_db_neverallow_attr_helper(struct cil_tree_node *node, uint32_t *finished, __attribute__((unused)) void *extra_args)
1545 {
1546 switch (node->flavor) {
1547 case CIL_BLOCK: {
1548 struct cil_block *blk = node->data;
1549 if (blk->is_abstract == CIL_TRUE) {
1550 *finished = CIL_TREE_SKIP_HEAD;
1551 }
1552 break;
1553 }
1554 case CIL_MACRO: {
1555 *finished = CIL_TREE_SKIP_HEAD;
1556 break;
1557 }
1558 case CIL_TYPEATTRIBUTE: {
1559 struct cil_typeattribute *attr = node->data;
1560 if ((attr->used & CIL_ATTR_NEVERALLOW) &&
1561 strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1562 __mark_neverallow_attrs(attr->expr_list);
1563 }
1564 break;
1565 }
1566 default:
1567 break;
1568 }
1569
1570 return SEPOL_OK;
1571 }
1572
1573 static int __cil_post_db_attr_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1574 {
1575 int rc = SEPOL_ERR;
1576 struct cil_db *db = extra_args;
1577
1578 switch (node->flavor) {
1579 case CIL_BLOCK: {
1580 struct cil_block *blk = node->data;
1581 if (blk->is_abstract == CIL_TRUE) {
1582 *finished = CIL_TREE_SKIP_HEAD;
1583 }
1584 break;
1585 }
1586 case CIL_MACRO: {
1587 *finished = CIL_TREE_SKIP_HEAD;
1588 break;
1589 }
1590 case CIL_TYPEATTRIBUTE: {
1591 struct cil_typeattribute *attr = node->data;
1592 if (attr->types == NULL) {
1593 rc = __evaluate_type_expression(attr, db);
1594 if (rc != SEPOL_OK) goto exit;
1595 }
1596 attr->keep = cil_typeattribute_used(attr, db);
1597 break;
1598 }
1599 case CIL_ROLEATTRIBUTE: {
1600 struct cil_roleattribute *attr = node->data;
1601 if (attr->roles == NULL) {
1602 rc = __evaluate_role_expression(attr, db);
1603 if (rc != SEPOL_OK) goto exit;
1604 }
1605 break;
1606 }
1607 case CIL_AVRULEX: {
1608 struct cil_avrule *rule = node->data;
1609 if (rule->perms.x.permx_str == NULL) {
1610 rc = __evaluate_permissionx_expression(rule->perms.x.permx, db);
1611 if (rc != SEPOL_OK) goto exit;
1612 }
1613 break;
1614 }
1615 case CIL_PERMISSIONX: {
1616 struct cil_permissionx *permx = node->data;
1617 rc = __evaluate_permissionx_expression(permx, db);
1618 if (rc != SEPOL_OK) goto exit;
1619 break;
1620 }
1621 case CIL_USERATTRIBUTE: {
1622 struct cil_userattribute *attr = node->data;
1623 if (attr->users == NULL) {
1624 rc = __evaluate_user_expression(attr, db);
1625 if (rc != SEPOL_OK) {
1626 goto exit;
1627 }
1628 }
1629 break;
1630 }
1631 default:
1632 break;
1633 }
1634
1635 return SEPOL_OK;
1636
1637 exit:
1638 return rc;
1639 }
1640
1641 static int __cil_role_assign_types(struct cil_role *role, struct cil_symtab_datum *datum)
1642 {
1643 struct cil_tree_node *node = datum->nodes->head->data;
1644
1645 if (role->types == NULL) {
1646 role->types = cil_malloc(sizeof(*role->types));
1647 ebitmap_init(role->types);
1648 }
1649
1650 if (node->flavor == CIL_TYPE) {
1651 struct cil_type *type = (struct cil_type *)datum;
1652 if (ebitmap_set_bit(role->types, type->value, 1)) {
1653 cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1654 goto exit;
1655 }
1656 } else if (node->flavor == CIL_TYPEALIAS) {
1657 struct cil_alias *alias = (struct cil_alias *)datum;
1658 struct cil_type *type = alias->actual;
1659 if (ebitmap_set_bit(role->types, type->value, 1)) {
1660 cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1661 goto exit;
1662 }
1663 } else if (node->flavor == CIL_TYPEATTRIBUTE) {
1664 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
1665 ebitmap_union(role->types, attr->types);
1666 }
1667
1668 return SEPOL_OK;
1669
1670 exit:
1671 return SEPOL_ERR;
1672 }
1673
1674 static int __cil_post_db_roletype_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1675 {
1676 int rc = SEPOL_ERR;
1677 struct cil_db *db = extra_args;
1678
1679 switch (node->flavor) {
1680 case CIL_BLOCK: {
1681 struct cil_block *blk = node->data;
1682 if (blk->is_abstract == CIL_TRUE) {
1683 *finished = CIL_TREE_SKIP_HEAD;
1684 }
1685 break;
1686 }
1687 case CIL_MACRO: {
1688 *finished = CIL_TREE_SKIP_HEAD;
1689 break;
1690 }
1691 case CIL_ROLETYPE: {
1692 struct cil_roletype *roletype = node->data;
1693 struct cil_symtab_datum *role_datum = roletype->role;
1694 struct cil_symtab_datum *type_datum = roletype->type;
1695 struct cil_tree_node *role_node = role_datum->nodes->head->data;
1696
1697 if (role_node->flavor == CIL_ROLEATTRIBUTE) {
1698 struct cil_roleattribute *attr = roletype->role;
1699 ebitmap_node_t *rnode;
1700 unsigned int i;
1701
1702 ebitmap_for_each_positive_bit(attr->roles, rnode, i) {
1703 struct cil_role *role = NULL;
1704
1705 role = db->val_to_role[i];
1706
1707 rc = __cil_role_assign_types(role, type_datum);
1708 if (rc != SEPOL_OK) {
1709 goto exit;
1710 }
1711 }
1712 } else {
1713 struct cil_role *role = roletype->role;
1714
1715 rc = __cil_role_assign_types(role, type_datum);
1716 if (rc != SEPOL_OK) {
1717 goto exit;
1718 }
1719 }
1720 break;
1721 }
1722 default:
1723 break;
1724 }
1725
1726 return SEPOL_OK;
1727 exit:
1728 cil_log(CIL_INFO, "cil_post_db_roletype_helper failed\n");
1729 return rc;
1730 }
1731
1732 static int __cil_user_assign_roles(struct cil_user *user, struct cil_symtab_datum *datum)
1733 {
1734 struct cil_tree_node *node = datum->nodes->head->data;
1735 struct cil_role *role = NULL;
1736 struct cil_roleattribute *attr = NULL;
1737
1738 if (user->roles == NULL) {
1739 user->roles = cil_malloc(sizeof(*user->roles));
1740 ebitmap_init(user->roles);
1741 }
1742
1743 if (node->flavor == CIL_ROLE) {
1744 role = (struct cil_role *)datum;
1745 if (ebitmap_set_bit(user->roles, role->value, 1)) {
1746 cil_log(CIL_INFO, "Failed to set bit in user roles bitmap\n");
1747 goto exit;
1748 }
1749 } else if (node->flavor == CIL_ROLEATTRIBUTE) {
1750 attr = (struct cil_roleattribute *)datum;
1751 ebitmap_union(user->roles, attr->roles);
1752 }
1753
1754 return SEPOL_OK;
1755
1756 exit:
1757 return SEPOL_ERR;
1758 }
1759
1760 static int __cil_post_db_userrole_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1761 {
1762 int rc = SEPOL_ERR;
1763 struct cil_db *db = extra_args;
1764 struct cil_block *blk = NULL;
1765 struct cil_userrole *userrole = NULL;
1766 struct cil_symtab_datum *user_datum = NULL;
1767 struct cil_symtab_datum *role_datum = NULL;
1768 struct cil_tree_node *user_node = NULL;
1769 struct cil_userattribute *u_attr = NULL;
1770 unsigned int i;
1771 struct cil_user *user = NULL;
1772 ebitmap_node_t *unode = NULL;
1773
1774 switch (node->flavor) {
1775 case CIL_BLOCK: {
1776 blk = node->data;
1777 if (blk->is_abstract == CIL_TRUE) {
1778 *finished = CIL_TREE_SKIP_HEAD;
1779 }
1780 break;
1781 }
1782 case CIL_MACRO: {
1783 *finished = CIL_TREE_SKIP_HEAD;
1784 break;
1785 }
1786 case CIL_USERROLE: {
1787 userrole = node->data;
1788 user_datum = userrole->user;
1789 role_datum = userrole->role;
1790 user_node = user_datum->nodes->head->data;
1791
1792 if (user_node->flavor == CIL_USERATTRIBUTE) {
1793 u_attr = userrole->user;
1794
1795 ebitmap_for_each_positive_bit(u_attr->users, unode, i) {
1796 user = db->val_to_user[i];
1797
1798 rc = __cil_user_assign_roles(user, role_datum);
1799 if (rc != SEPOL_OK) {
1800 goto exit;
1801 }
1802 }
1803 } else {
1804 user = userrole->user;
1805
1806 rc = __cil_user_assign_roles(user, role_datum);
1807 if (rc != SEPOL_OK) {
1808 goto exit;
1809 }
1810 }
1811
1812 break;
1813 }
1814 default:
1815 break;
1816 }
1817
1818 return SEPOL_OK;
1819 exit:
1820 cil_log(CIL_INFO, "cil_post_db_userrole_helper failed\n");
1821 return rc;
1822 }
1823
1824 static int __evaluate_level_expression(struct cil_level *level, struct cil_db *db)
1825 {
1826 if (level->cats != NULL) {
1827 return __evaluate_cat_expression(level->cats, db);
1828 }
1829
1830 return SEPOL_OK;
1831 }
1832
1833 static int __evaluate_levelrange_expression(struct cil_levelrange *levelrange, struct cil_db *db)
1834 {
1835 int rc = SEPOL_OK;
1836
1837 if (levelrange->low != NULL && levelrange->low->cats != NULL) {
1838 rc = __evaluate_cat_expression(levelrange->low->cats, db);
1839 if (rc != SEPOL_OK) {
1840 goto exit;
1841 }
1842 }
1843 if (levelrange->high != NULL && levelrange->high->cats != NULL) {
1844 rc = __evaluate_cat_expression(levelrange->high->cats, db);
1845 if (rc != SEPOL_OK) {
1846 goto exit;
1847 }
1848 }
1849
1850 exit:
1851 return rc;
1852 }
1853
1854 static int __cil_post_db_cat_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1855 {
1856 int rc = SEPOL_ERR;
1857 struct cil_db *db = extra_args;
1858
1859 switch (node->flavor) {
1860 case CIL_BLOCK: {
1861 struct cil_block *blk = node->data;
1862 if (blk->is_abstract == CIL_TRUE) {
1863 *finished = CIL_TREE_SKIP_HEAD;
1864 }
1865 break;
1866 }
1867 case CIL_MACRO: {
1868 *finished = CIL_TREE_SKIP_HEAD;
1869 break;
1870 }
1871 case CIL_CATSET: {
1872 struct cil_catset *catset = node->data;
1873 rc = __evaluate_cat_expression(catset->cats, db);
1874 if (rc != SEPOL_OK) {
1875 goto exit;
1876 }
1877 break;
1878 }
1879 case CIL_SENSCAT: {
1880 struct cil_senscat *senscat = node->data;
1881 rc = __evaluate_cat_expression(senscat->cats, db);
1882 if (rc != SEPOL_OK) {
1883 goto exit;
1884 }
1885 break;
1886 }
1887 case CIL_LEVEL: {
1888 rc = __evaluate_level_expression(node->data, db);
1889 if (rc != SEPOL_OK) {
1890 goto exit;
1891 }
1892 break;
1893 }
1894 case CIL_LEVELRANGE: {
1895 rc = __evaluate_levelrange_expression(node->data, db);
1896 if (rc != SEPOL_OK) {
1897 goto exit;
1898 }
1899 break;
1900 }
1901 case CIL_USER: {
1902 struct cil_user *user = node->data;
1903 rc = __evaluate_level_expression(user->dftlevel, db);
1904 if (rc != SEPOL_OK) {
1905 goto exit;
1906 }
1907 rc = __evaluate_levelrange_expression(user->range, db);
1908 if (rc != SEPOL_OK) {
1909 goto exit;
1910 }
1911 break;
1912 }
1913 case CIL_SELINUXUSERDEFAULT:
1914 case CIL_SELINUXUSER: {
1915 struct cil_selinuxuser *selinuxuser = node->data;
1916 rc = __evaluate_levelrange_expression(selinuxuser->range, db);
1917 if (rc != SEPOL_OK) {
1918 goto exit;
1919 }
1920 break;
1921 }
1922 case CIL_RANGETRANSITION: {
1923 struct cil_rangetransition *rangetrans = node->data;
1924 rc = __evaluate_levelrange_expression(rangetrans->range, db);
1925 if (rc != SEPOL_OK) {
1926 goto exit;
1927 }
1928 break;
1929 }
1930 case CIL_CONTEXT: {
1931 struct cil_context *context = node->data;
1932 rc = __evaluate_levelrange_expression(context->range, db);
1933 if (rc != SEPOL_OK) {
1934 goto exit;
1935 }
1936 break;
1937 }
1938 case CIL_SIDCONTEXT: {
1939 struct cil_sidcontext *sidcontext = node->data;
1940 rc = __evaluate_levelrange_expression(sidcontext->context->range, db);
1941 if (rc != SEPOL_OK) {
1942 goto exit;
1943 }
1944 break;
1945 }
1946 case CIL_FILECON: {
1947 struct cil_filecon *filecon = node->data;
1948 if (filecon->context) {
1949 rc = __evaluate_levelrange_expression(filecon->context->range, db);
1950 if (rc != SEPOL_OK) {
1951 goto exit;
1952 }
1953 }
1954 break;
1955 }
1956 case CIL_IBPKEYCON: {
1957 struct cil_ibpkeycon *ibpkeycon = node->data;
1958
1959 rc = __evaluate_levelrange_expression(ibpkeycon->context->range, db);
1960 if (rc != SEPOL_OK)
1961 goto exit;
1962 break;
1963 }
1964 case CIL_IBENDPORTCON: {
1965 struct cil_ibendportcon *ibendportcon = node->data;
1966
1967 rc = __evaluate_levelrange_expression(ibendportcon->context->range, db);
1968 if (rc != SEPOL_OK)
1969 goto exit;
1970 break;
1971 }
1972 case CIL_PORTCON: {
1973 struct cil_portcon *portcon = node->data;
1974 rc = __evaluate_levelrange_expression(portcon->context->range, db);
1975 if (rc != SEPOL_OK) {
1976 goto exit;
1977 }
1978 break;
1979 }
1980 case CIL_NODECON: {
1981 struct cil_nodecon *nodecon = node->data;
1982 rc = __evaluate_levelrange_expression(nodecon->context->range, db);
1983 if (rc != SEPOL_OK) {
1984 goto exit;
1985 }
1986 break;
1987 }
1988 case CIL_GENFSCON: {
1989 struct cil_genfscon *genfscon = node->data;
1990 rc = __evaluate_levelrange_expression(genfscon->context->range, db);
1991 if (rc != SEPOL_OK) {
1992 goto exit;
1993 }
1994 break;
1995 }
1996 case CIL_NETIFCON: {
1997 struct cil_netifcon *netifcon = node->data;
1998 rc = __evaluate_levelrange_expression(netifcon->if_context->range, db);
1999 if (rc != SEPOL_OK) {
2000 goto exit;
2001 }
2002 rc = __evaluate_levelrange_expression(netifcon->packet_context->range, db);
2003 if (rc != SEPOL_OK) {
2004 goto exit;
2005 }
2006 break;
2007 }
2008 case CIL_PIRQCON: {
2009 struct cil_pirqcon *pirqcon = node->data;
2010 rc = __evaluate_levelrange_expression(pirqcon->context->range, db);
2011 if (rc != SEPOL_OK) {
2012 goto exit;
2013 }
2014 break;
2015 }
2016 case CIL_IOMEMCON: {
2017 struct cil_iomemcon *iomemcon = node->data;
2018 rc = __evaluate_levelrange_expression(iomemcon->context->range, db);
2019 if (rc != SEPOL_OK) {
2020 goto exit;
2021 }
2022 break;
2023 }
2024 case CIL_IOPORTCON: {
2025 struct cil_ioportcon *ioportcon = node->data;
2026 rc = __evaluate_levelrange_expression(ioportcon->context->range, db);
2027 if (rc != SEPOL_OK) {
2028 goto exit;
2029 }
2030 break;
2031 }
2032 case CIL_PCIDEVICECON: {
2033 struct cil_pcidevicecon *pcidevicecon = node->data;
2034 rc = __evaluate_levelrange_expression(pcidevicecon->context->range, db);
2035 if (rc != SEPOL_OK) {
2036 goto exit;
2037 }
2038 break;
2039 }
2040 case CIL_DEVICETREECON: {
2041 struct cil_devicetreecon *devicetreecon = node->data;
2042 rc = __evaluate_levelrange_expression(devicetreecon->context->range, db);
2043 if (rc != SEPOL_OK) {
2044 goto exit;
2045 }
2046 break;
2047 }
2048 case CIL_FSUSE: {
2049 struct cil_fsuse *fsuse = node->data;
2050 rc = __evaluate_levelrange_expression(fsuse->context->range, db);
2051 if (rc != SEPOL_OK) {
2052 goto exit;
2053 }
2054 break;
2055 }
2056 default:
2057 break;
2058 }
2059
2060 return SEPOL_OK;
2061
2062 exit:
2063 return rc;
2064 }
2065
2066 struct perm_to_list {
2067 enum cil_flavor flavor;
2068 ebitmap_t *perms;
2069 struct cil_list *new_list;
2070 };
2071
2072 static int __perm_bits_to_list(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2073 {
2074 struct perm_to_list *perm_args = (struct perm_to_list *)args;
2075 ebitmap_t *perms = perm_args->perms;
2076 struct cil_list *new_list = perm_args->new_list;
2077 struct cil_perm *perm = (struct cil_perm *)d;
2078 unsigned int value = perm->value;
2079
2080 if (!ebitmap_get_bit(perms, value)) {
2081 return SEPOL_OK;
2082 }
2083
2084 cil_list_append(new_list, CIL_DATUM, d);
2085
2086 return SEPOL_OK;
2087 }
2088
2089 static int __evaluate_perm_expression(struct cil_list *perms, enum cil_flavor flavor, symtab_t *class_symtab, symtab_t *common_symtab, unsigned int num_perms, struct cil_list **new_list, struct cil_db *db)
2090 {
2091 int rc = SEPOL_ERR;
2092 struct perm_to_list args;
2093 ebitmap_t bitmap;
2094
2095 if (cil_verify_is_list(perms, CIL_PERM)) {
2096 return SEPOL_OK;
2097 }
2098
2099 ebitmap_init(&bitmap);
2100 rc = __cil_expr_to_bitmap(perms, &bitmap, num_perms, db);
2101 if (rc != SEPOL_OK) {
2102 ebitmap_destroy(&bitmap);
2103 goto exit;
2104 }
2105
2106 cil_list_init(new_list, flavor);
2107
2108 args.flavor = flavor;
2109 args.perms = &bitmap;
2110 args.new_list = *new_list;
2111
2112 cil_symtab_map(class_symtab, __perm_bits_to_list, &args);
2113
2114 if (common_symtab != NULL) {
2115 cil_symtab_map(common_symtab, __perm_bits_to_list, &args);
2116 }
2117
2118 ebitmap_destroy(&bitmap);
2119 return SEPOL_OK;
2120
2121 exit:
2122 return rc;
2123 }
2124
2125 static int __evaluate_classperms(struct cil_classperms *cp, struct cil_db *db)
2126 {
2127 int rc = SEPOL_ERR;
2128 struct cil_class *class = cp->class;
2129 struct cil_class *common = class->common;
2130 symtab_t *common_symtab = NULL;
2131 struct cil_list *new_list = NULL;
2132
2133 if (common) {
2134 common_symtab = &common->perms;
2135 }
2136
2137 rc = __evaluate_perm_expression(cp->perms, CIL_PERM, &class->perms, common_symtab, class->num_perms, &new_list, db);
2138 if (rc != SEPOL_OK) {
2139 goto exit;
2140 }
2141
2142 if (new_list == NULL) {
2143 return SEPOL_OK;
2144 }
2145
2146 cil_list_destroy(&cp->perms, CIL_FALSE);
2147
2148 cp->perms = new_list;
2149
2150 return SEPOL_OK;
2151
2152 exit:
2153 return rc;
2154 }
2155
2156 static int __evaluate_classperms_list(struct cil_list *classperms, struct cil_db *db)
2157 {
2158 int rc = SEPOL_ERR;
2159 struct cil_list_item *curr;
2160
2161 cil_list_for_each(curr, classperms) {
2162 if (curr->flavor == CIL_CLASSPERMS) {
2163 struct cil_classperms *cp = curr->data;
2164 if (FLAVOR(cp->class) == CIL_CLASS) {
2165 rc = __evaluate_classperms(cp, db);
2166 if (rc != SEPOL_OK) {
2167 goto exit;
2168 }
2169 } else { /* MAP */
2170 struct cil_list_item *i = NULL;
2171 rc = __evaluate_classperms(cp, db);
2172 if (rc != SEPOL_OK) {
2173 goto exit;
2174 }
2175 cil_list_for_each(i, cp->perms) {
2176 struct cil_perm *cmp = i->data;
2177 rc = __evaluate_classperms_list(cmp->classperms, db);
2178 if (rc != SEPOL_OK) {
2179 goto exit;
2180 }
2181 }
2182 }
2183 } else { /* SET */
2184 struct cil_classperms_set *cp_set = curr->data;
2185 struct cil_classpermission *cp = cp_set->set;
2186 rc = __evaluate_classperms_list(cp->classperms, db);
2187 if (rc != SEPOL_OK) {
2188 goto exit;
2189 }
2190 }
2191 }
2192
2193 return SEPOL_OK;
2194
2195 exit:
2196 return rc;
2197 }
2198
2199 struct class_map_args {
2200 struct cil_db *db;
2201 int rc;
2202 };
2203
2204 static int __evaluate_map_perm_classperms(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2205 {
2206 struct class_map_args *map_args = args;
2207 struct cil_perm *cmp = (struct cil_perm *)d;
2208
2209 int rc = __evaluate_classperms_list(cmp->classperms, map_args->db);
2210
2211 if (rc != SEPOL_OK) {
2212 map_args->rc = rc;
2213 }
2214
2215 return SEPOL_OK;
2216 }
2217
2218 static int __evaluate_map_class(struct cil_class *mc, struct cil_db *db)
2219 {
2220 struct class_map_args map_args;
2221
2222 map_args.db = db;
2223 map_args.rc = SEPOL_OK;
2224 cil_symtab_map(&mc->perms, __evaluate_map_perm_classperms, &map_args);
2225
2226 return map_args.rc;
2227 }
2228
2229 static int __cil_post_db_classperms_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2230 {
2231 int rc = SEPOL_ERR;
2232 struct cil_db *db = extra_args;
2233
2234 switch (node->flavor) {
2235 case CIL_BLOCK: {
2236 struct cil_block *blk = node->data;
2237 if (blk->is_abstract == CIL_TRUE) {
2238 *finished = CIL_TREE_SKIP_HEAD;
2239 }
2240 break;
2241 }
2242 case CIL_MACRO:
2243 *finished = CIL_TREE_SKIP_HEAD;
2244 break;
2245 case CIL_MAP_CLASS: {
2246 rc = __evaluate_map_class(node->data, db);
2247 if (rc != SEPOL_OK) {
2248 goto exit;
2249 }
2250 break;
2251 }
2252 case CIL_CLASSPERMISSION: {
2253 struct cil_classpermission *cp = node->data;
2254 rc = __evaluate_classperms_list(cp->classperms, db);
2255 if (rc != SEPOL_OK) {
2256 goto exit;
2257 }
2258 break;
2259 }
2260 case CIL_AVRULE: {
2261 struct cil_avrule *avrule = node->data;
2262 rc = __evaluate_classperms_list(avrule->perms.classperms, db);
2263 if (rc != SEPOL_OK) {
2264 goto exit;
2265 }
2266 break;
2267 }
2268 case CIL_DENY_RULE: {
2269 struct cil_deny_rule *deny = node->data;
2270 rc = __evaluate_classperms_list(deny->classperms, db);
2271 if (rc != SEPOL_OK) {
2272 goto exit;
2273 }
2274 break;
2275 }
2276 case CIL_CONSTRAIN:
2277 case CIL_MLSCONSTRAIN: {
2278 struct cil_constrain *constrain = node->data;
2279 rc = __evaluate_classperms_list(constrain->classperms, db);
2280 if (rc != SEPOL_OK) {
2281 goto exit;
2282 }
2283 break;
2284 }
2285 default:
2286 break;
2287 }
2288
2289 return SEPOL_OK;
2290
2291 exit:
2292 return rc;
2293 }
2294
2295 static int __cil_post_report_conflict(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2296 {
2297 struct cil_list_item *li = extra_args;
2298
2299 if (node->flavor == CIL_BLOCK) {
2300 struct cil_block *blk = node->data;
2301 if (blk->is_abstract == CIL_TRUE) {
2302 *finished = CIL_TREE_SKIP_HEAD;
2303 }
2304 } else if (node->flavor == CIL_MACRO) {
2305 *finished = CIL_TREE_SKIP_HEAD;
2306 } else if (node->flavor == li->flavor) {
2307 if (node->data == li->data) {
2308 char *path = cil_tree_get_cil_path(node);
2309 cil_log(CIL_WARN, " at %s:%d\n", path, node->line);
2310 }
2311 }
2312 return SEPOL_OK;
2313 }
2314
2315 static int __cil_post_process_context_rules(struct cil_sort *sort, int (*compar)(const void *, const void *), int (*concompar)(const void *, const void *), struct cil_db *db, enum cil_flavor flavor, const char *flavor_str)
2316 {
2317 uint32_t count = sort->count;
2318 uint32_t i = 0, j, removed = 0;
2319 int conflicting = 0;
2320 int rc = SEPOL_OK;
2321 enum cil_log_level log_level = cil_get_log_level();
2322
2323 if (count < 2) {
2324 return SEPOL_OK;
2325 }
2326
2327 qsort(sort->array, sort->count, sizeof(sort->array), compar);
2328
2329 for (j=1; j<count; j++) {
2330 if (compar(&sort->array[i], &sort->array[j]) != 0) {
2331 i++;
2332 if (conflicting >= 4) {
2333 /* 2 rules were written when conflicting == 1 */
2334 cil_log(CIL_WARN, " Only first 4 of %d conflicting rules shown\n", conflicting);
2335 }
2336 conflicting = 0;
2337 } else {
2338 removed++;
2339 if (!db->multiple_decls || concompar(&sort->array[i], &sort->array[j]) != 0) {
2340 rc = SEPOL_ERR;
2341 conflicting++;
2342 if (log_level >= CIL_WARN) {
2343 struct cil_list_item li;
2344 int rc2;
2345 li.flavor = flavor;
2346 if (conflicting == 1) {
2347 cil_log(CIL_WARN, "Found conflicting %s rules\n", flavor_str);
2348 li.data = sort->array[i];
2349 rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2350 NULL, NULL, &li);
2351 if (rc2 != SEPOL_OK) goto exit;
2352 }
2353 if (conflicting < 4 || log_level > CIL_WARN) {
2354 li.data = sort->array[j];
2355 rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2356 NULL, NULL, &li);
2357 if (rc2 != SEPOL_OK) goto exit;
2358 }
2359 }
2360 }
2361 }
2362 if (i != j && !conflicting) {
2363 sort->array[i] = sort->array[j];
2364 }
2365 }
2366 sort->count = count - removed;
2367
2368 exit:
2369 return rc;
2370 }
2371
2372 static int cil_post_db(struct cil_db *db)
2373 {
2374 int rc = SEPOL_ERR;
2375
2376 rc = cil_tree_walk(db->ast->root, __cil_post_db_count_helper, NULL, NULL, db);
2377 if (rc != SEPOL_OK) {
2378 cil_log(CIL_INFO, "Failure during cil database count helper\n");
2379 goto exit;
2380 }
2381
2382 rc = cil_tree_walk(db->ast->root, __cil_post_db_array_helper, NULL, NULL, db);
2383 if (rc != SEPOL_OK) {
2384 cil_log(CIL_INFO, "Failure during cil database array helper\n");
2385 goto exit;
2386 }
2387
2388 rc = cil_tree_walk(db->ast->root, __cil_post_db_neverallow_attr_helper, NULL, NULL, db);
2389 if (rc != SEPOL_OK) {
2390 cil_log(CIL_INFO, "Failed to mark attributes used by generated attributes used in neverallow rules\n");
2391 goto exit;
2392 }
2393
2394 rc = cil_tree_walk(db->ast->root, __cil_post_db_attr_helper, NULL, NULL, db);
2395 if (rc != SEPOL_OK) {
2396 cil_log(CIL_INFO, "Failed to create attribute bitmaps\n");
2397 goto exit;
2398 }
2399
2400 rc = cil_tree_walk(db->ast->root, __cil_post_db_roletype_helper, NULL, NULL, db);
2401 if (rc != SEPOL_OK) {
2402 cil_log(CIL_INFO, "Failed during roletype association\n");
2403 goto exit;
2404 }
2405
2406 rc = cil_tree_walk(db->ast->root, __cil_post_db_userrole_helper, NULL, NULL, db);
2407 if (rc != SEPOL_OK) {
2408 cil_log(CIL_INFO, "Failed during userrole association\n");
2409 goto exit;
2410 }
2411
2412 rc = cil_tree_walk(db->ast->root, __cil_post_db_classperms_helper, NULL, NULL, db);
2413 if (rc != SEPOL_OK) {
2414 cil_log(CIL_INFO, "Failed to evaluate class mapping permissions expressions\n");
2415 goto exit;
2416 }
2417
2418 rc = cil_tree_walk(db->ast->root, __cil_post_db_cat_helper, NULL, NULL, db);
2419 if (rc != SEPOL_OK) {
2420 cil_log(CIL_INFO, "Failed to evaluate category expressions\n");
2421 goto exit;
2422 }
2423
2424 rc = __cil_post_process_context_rules(db->netifcon, cil_post_netifcon_compare, cil_post_netifcon_context_compare, db, CIL_NETIFCON, CIL_KEY_NETIFCON);
2425 if (rc != SEPOL_OK) {
2426 cil_log(CIL_ERR, "Problems processing netifcon rules\n");
2427 goto exit;
2428 }
2429
2430 rc = __cil_post_process_context_rules(db->genfscon, cil_post_genfscon_compare, cil_post_genfscon_context_compare, db, CIL_GENFSCON, CIL_KEY_GENFSCON);
2431 if (rc != SEPOL_OK) {
2432 cil_log(CIL_ERR, "Problems processing genfscon rules\n");
2433 goto exit;
2434 }
2435
2436 rc = __cil_post_process_context_rules(db->ibpkeycon, cil_post_ibpkeycon_compare, cil_post_ibpkeycon_context_compare, db, CIL_IBPKEYCON, CIL_KEY_IBPKEYCON);
2437 if (rc != SEPOL_OK) {
2438 cil_log(CIL_ERR, "Problems processing ibpkeycon rules\n");
2439 goto exit;
2440 }
2441
2442 rc = __cil_post_process_context_rules(db->ibendportcon, cil_post_ibendportcon_compare, cil_post_ibendportcon_context_compare, db, CIL_IBENDPORTCON, CIL_KEY_IBENDPORTCON);
2443 if (rc != SEPOL_OK) {
2444 cil_log(CIL_ERR, "Problems processing ibendportcon rules\n");
2445 goto exit;
2446 }
2447
2448 rc = __cil_post_process_context_rules(db->portcon, cil_post_portcon_compare, cil_post_portcon_context_compare, db, CIL_PORTCON, CIL_KEY_PORTCON);
2449 if (rc != SEPOL_OK) {
2450 cil_log(CIL_ERR, "Problems processing portcon rules\n");
2451 goto exit;
2452 }
2453
2454 rc = __cil_post_process_context_rules(db->nodecon, cil_post_nodecon_compare, cil_post_nodecon_context_compare, db, CIL_NODECON, CIL_KEY_NODECON);
2455 if (rc != SEPOL_OK) {
2456 cil_log(CIL_ERR, "Problems processing nodecon rules\n");
2457 goto exit;
2458 }
2459
2460 rc = __cil_post_process_context_rules(db->fsuse, cil_post_fsuse_compare, cil_post_fsuse_context_compare, db, CIL_FSUSE, CIL_KEY_FSUSE);
2461 if (rc != SEPOL_OK) {
2462 cil_log(CIL_ERR, "Problems processing fsuse rules\n");
2463 goto exit;
2464 }
2465
2466 rc = __cil_post_process_context_rules(db->filecon, cil_post_filecon_compare, cil_post_filecon_context_compare, db, CIL_FILECON, CIL_KEY_FILECON);
2467 if (rc != SEPOL_OK) {
2468 cil_log(CIL_ERR, "Problems processing filecon rules\n");
2469 goto exit;
2470 }
2471
2472 rc = __cil_post_process_context_rules(db->pirqcon, cil_post_pirqcon_compare, cil_post_pirqcon_context_compare, db, CIL_PIRQCON, CIL_KEY_IOMEMCON);
2473 if (rc != SEPOL_OK) {
2474 cil_log(CIL_ERR, "Problems processing pirqcon rules\n");
2475 goto exit;
2476 }
2477
2478 rc = __cil_post_process_context_rules(db->iomemcon, cil_post_iomemcon_compare, cil_post_iomemcon_context_compare, db, CIL_IOMEMCON, CIL_KEY_IOMEMCON);
2479 if (rc != SEPOL_OK) {
2480 cil_log(CIL_ERR, "Problems processing iomemcon rules\n");
2481 goto exit;
2482 }
2483
2484 rc = __cil_post_process_context_rules(db->ioportcon, cil_post_ioportcon_compare, cil_post_ioportcon_context_compare, db, CIL_IOPORTCON, CIL_KEY_IOPORTCON);
2485 if (rc != SEPOL_OK) {
2486 cil_log(CIL_ERR, "Problems processing ioportcon rules\n");
2487 goto exit;
2488 }
2489
2490 rc = __cil_post_process_context_rules(db->pcidevicecon, cil_post_pcidevicecon_compare, cil_post_pcidevicecon_context_compare, db, CIL_PCIDEVICECON, CIL_KEY_PCIDEVICECON);
2491 if (rc != SEPOL_OK) {
2492 cil_log(CIL_ERR, "Problems processing pcidevicecon rules\n");
2493 goto exit;
2494 }
2495
2496 rc = __cil_post_process_context_rules(db->devicetreecon, cil_post_devicetreecon_compare, cil_post_devicetreecon_context_compare, db, CIL_DEVICETREECON, CIL_KEY_DEVICETREECON);
2497 if (rc != SEPOL_OK) {
2498 cil_log(CIL_ERR, "Problems processing devicetreecon rules\n");
2499 goto exit;
2500 }
2501
2502 exit:
2503 return rc;
2504 }
2505
2506 static int cil_post_verify(struct cil_db *db)
2507 {
2508 int rc = SEPOL_ERR;
2509 int avrule_cnt = 0;
2510 int handleunknown = -1;
2511 int mls = -1;
2512 int nseuserdflt = 0;
2513 int pass = 0;
2514 struct cil_args_verify extra_args;
2515 struct cil_complex_symtab csymtab;
2516
2517 cil_complex_symtab_init(&csymtab, CIL_CLASS_SYM_SIZE);
2518
2519 extra_args.db = db;
2520 extra_args.csymtab = &csymtab;
2521 extra_args.avrule_cnt = &avrule_cnt;
2522 extra_args.handleunknown = &handleunknown;
2523 extra_args.mls = &mls;
2524 extra_args.nseuserdflt = &nseuserdflt;
2525 extra_args.pass = &pass;
2526
2527 for (pass = 0; pass < 2; pass++) {
2528 rc = cil_tree_walk(db->ast->root, __cil_verify_helper, NULL, NULL, &extra_args);
2529 if (rc != SEPOL_OK) {
2530 cil_log(CIL_ERR, "Failed to verify cil database\n");
2531 goto exit;
2532 }
2533 }
2534
2535 if (db->handle_unknown == -1) {
2536 if (handleunknown == -1) {
2537 db->handle_unknown = SEPOL_DENY_UNKNOWN;
2538 } else {
2539 db->handle_unknown = handleunknown;
2540 }
2541 }
2542
2543 if (db->mls == -1) {
2544 if (mls == -1) {
2545 db->mls = CIL_FALSE;
2546 } else {
2547 db->mls = mls;
2548 }
2549 }
2550
2551 if (avrule_cnt == 0) {
2552 cil_log(CIL_ERR, "Policy must include at least one avrule\n");
2553 rc = SEPOL_ERR;
2554 goto exit;
2555 }
2556
2557 if (nseuserdflt > 1) {
2558 cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
2559 rc = SEPOL_ERR;
2560 goto exit;
2561 }
2562
2563 exit:
2564 cil_complex_symtab_destroy(&csymtab);
2565 return rc;
2566 }
2567
2568 static int cil_pre_verify(struct cil_db *db)
2569 {
2570 int rc = SEPOL_ERR;
2571 struct cil_args_verify extra_args;
2572
2573 extra_args.db = db;
2574
2575 rc = cil_tree_walk(db->ast->root, __cil_pre_verify_helper, NULL, NULL, &extra_args);
2576 if (rc != SEPOL_OK) {
2577 cil_log(CIL_ERR, "Failed to verify cil database\n");
2578 goto exit;
2579 }
2580
2581 exit:
2582 return rc;
2583 }
2584
2585 int cil_post_process(struct cil_db *db)
2586 {
2587 int rc = SEPOL_ERR;
2588
2589 rc = cil_pre_verify(db);
2590 if (rc != SEPOL_OK) {
2591 cil_log(CIL_ERR, "Failed to verify cil database\n");
2592 goto exit;
2593 }
2594
2595 rc = cil_post_db(db);
2596 if (rc != SEPOL_OK) {
2597 cil_log(CIL_ERR, "Failed post db handling\n");
2598 goto exit;
2599 }
2600
2601 rc = cil_process_deny_rules_in_ast(db);
2602 if (rc != SEPOL_OK) {
2603 cil_log(CIL_ERR, "Failed to process deny rules\n");
2604 goto exit;
2605 }
2606
2607 rc = cil_post_verify(db);
2608 if (rc != SEPOL_OK) {
2609 cil_log(CIL_ERR, "Failed to verify cil database\n");
2610 goto exit;
2611 }
2612
2613 exit:
2614 return rc;
2615
2616 }