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