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 <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <unistd.h>
35
36 #include <sepol/policydb/conditional.h>
37 #include <sepol/errcodes.h>
38
39 #include "cil_internal.h"
40 #include "cil_flavor.h"
41 #include "cil_log.h"
42 #include "cil_mem.h"
43 #include "cil_tree.h"
44 #include "cil_list.h"
45 #include "cil_post.h"
46 #include "cil_policy.h"
47 #include "cil_verify.h"
48 #include "cil_symtab.h"
49
50 #define GEN_REQUIRE_ATTR "cil_gen_require" /* Also in libsepol/src/module_to_cil.c */
51 #define TYPEATTR_INFIX "_typeattr_" /* Also in libsepol/src/module_to_cil.c */
52
53 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db);
54 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db);
55
cil_verify_is_list(struct cil_list * list,enum cil_flavor flavor)56 static int cil_verify_is_list(struct cil_list *list, enum cil_flavor flavor)
57 {
58 struct cil_list_item *curr;
59
60 cil_list_for_each(curr, list) {
61 switch (curr->flavor) {
62 case CIL_LIST:
63 return CIL_FALSE;
64 break;
65 case CIL_OP:
66 return CIL_FALSE;
67 break;
68 default:
69 if (flavor == CIL_CAT) {
70 struct cil_symtab_datum *d = curr->data;
71 struct cil_tree_node *n = d->nodes->head->data;
72 if (n->flavor == CIL_CATSET) {
73 return CIL_FALSE;
74 }
75 }
76 break;
77 }
78 }
79 return CIL_TRUE;
80 }
81
cil_post_fc_fill_data(struct fc_data * fc,char * path)82 void cil_post_fc_fill_data(struct fc_data *fc, char *path)
83 {
84 int c = 0;
85 fc->meta = 0;
86 fc->stem_len = 0;
87 fc->str_len = 0;
88
89 while (path[c] != '\0') {
90 switch (path[c]) {
91 case '.':
92 case '^':
93 case '$':
94 case '?':
95 case '*':
96 case '+':
97 case '|':
98 case '[':
99 case '(':
100 case '{':
101 fc->meta = 1;
102 break;
103 case '\\':
104 c++;
105 default:
106 if (!fc->meta) {
107 fc->stem_len++;
108 }
109 break;
110 }
111 fc->str_len++;
112 c++;
113 }
114 }
115
cil_post_filecon_compare(const void * a,const void * b)116 int cil_post_filecon_compare(const void *a, const void *b)
117 {
118 int rc = 0;
119 struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
120 struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
121 struct fc_data *a_data = cil_malloc(sizeof(*a_data));
122 struct fc_data *b_data = cil_malloc(sizeof(*b_data));
123 char *a_path = cil_malloc(strlen(a_filecon->path_str) + 1);
124 a_path[0] = '\0';
125 char *b_path = cil_malloc(strlen(b_filecon->path_str) + 1);
126 b_path[0] = '\0';
127 strcat(a_path, a_filecon->path_str);
128 strcat(b_path, b_filecon->path_str);
129 cil_post_fc_fill_data(a_data, a_path);
130 cil_post_fc_fill_data(b_data, b_path);
131 if (a_data->meta && !b_data->meta) {
132 rc = -1;
133 } else if (b_data->meta && !a_data->meta) {
134 rc = 1;
135 } else if (a_data->stem_len < b_data->stem_len) {
136 rc = -1;
137 } else if (b_data->stem_len < a_data->stem_len) {
138 rc = 1;
139 } else if (a_data->str_len < b_data->str_len) {
140 rc = -1;
141 } else if (b_data->str_len < a_data->str_len) {
142 rc = 1;
143 } else if (a_filecon->type < b_filecon->type) {
144 rc = -1;
145 } else if (b_filecon->type < a_filecon->type) {
146 rc = 1;
147 }
148
149 free(a_path);
150 free(b_path);
151 free(a_data);
152 free(b_data);
153
154 return rc;
155 }
156
cil_post_portcon_compare(const void * a,const void * b)157 int cil_post_portcon_compare(const void *a, const void *b)
158 {
159 int rc = SEPOL_ERR;
160 struct cil_portcon *aportcon = *(struct cil_portcon**)a;
161 struct cil_portcon *bportcon = *(struct cil_portcon**)b;
162
163 rc = (aportcon->port_high - aportcon->port_low)
164 - (bportcon->port_high - bportcon->port_low);
165 if (rc == 0) {
166 if (aportcon->port_low < bportcon->port_low) {
167 rc = -1;
168 } else if (bportcon->port_low < aportcon->port_low) {
169 rc = 1;
170 }
171 }
172
173 return rc;
174 }
175
cil_post_genfscon_compare(const void * a,const void * b)176 int cil_post_genfscon_compare(const void *a, const void *b)
177 {
178 int rc = SEPOL_ERR;
179 struct cil_genfscon *agenfscon = *(struct cil_genfscon**)a;
180 struct cil_genfscon *bgenfscon = *(struct cil_genfscon**)b;
181
182 rc = strcmp(agenfscon->fs_str, bgenfscon->fs_str);
183 if (rc == 0) {
184 rc = strcmp(agenfscon->path_str, bgenfscon->path_str);
185 }
186
187 return rc;
188 }
189
cil_post_netifcon_compare(const void * a,const void * b)190 int cil_post_netifcon_compare(const void *a, const void *b)
191 {
192 struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
193 struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
194
195 return strcmp(anetifcon->interface_str, bnetifcon->interface_str);
196 }
197
cil_post_nodecon_compare(const void * a,const void * b)198 int cil_post_nodecon_compare(const void *a, const void *b)
199 {
200 struct cil_nodecon *anodecon;
201 struct cil_nodecon *bnodecon;
202 anodecon = *(struct cil_nodecon**)a;
203 bnodecon = *(struct cil_nodecon**)b;
204
205 /* sort ipv4 before ipv6 */
206 if (anodecon->addr->family != bnodecon->addr->family) {
207 if (anodecon->addr->family == AF_INET) {
208 return -1;
209 } else {
210 return 1;
211 }
212 }
213
214 /* most specific netmask goes first, then order by ip addr */
215 if (anodecon->addr->family == AF_INET) {
216 int rc = memcmp(&anodecon->mask->ip.v4, &bnodecon->mask->ip.v4, sizeof(anodecon->mask->ip.v4));
217 if (rc != 0) {
218 return -1 * rc;
219 }
220 return memcmp(&anodecon->addr->ip.v4, &bnodecon->addr->ip.v4, sizeof(anodecon->addr->ip.v4));
221 } else {
222 int rc = memcmp(&anodecon->mask->ip.v6, &bnodecon->mask->ip.v6, sizeof(anodecon->mask->ip.v6));
223 if (rc != 0) {
224 return -1 * rc;
225 }
226 return memcmp(&anodecon->addr->ip.v6, &bnodecon->addr->ip.v6, sizeof(anodecon->addr->ip.v6));
227 }
228 }
229
cil_post_pirqcon_compare(const void * a,const void * b)230 int cil_post_pirqcon_compare(const void *a, const void *b)
231 {
232 int rc = SEPOL_ERR;
233 struct cil_pirqcon *apirqcon = *(struct cil_pirqcon**)a;
234 struct cil_pirqcon *bpirqcon = *(struct cil_pirqcon**)b;
235
236 if (apirqcon->pirq < bpirqcon->pirq) {
237 rc = -1;
238 } else if (bpirqcon->pirq < apirqcon->pirq) {
239 rc = 1;
240 } else {
241 rc = 0;
242 }
243
244 return rc;
245 }
246
cil_post_iomemcon_compare(const void * a,const void * b)247 int cil_post_iomemcon_compare(const void *a, const void *b)
248 {
249 int rc = SEPOL_ERR;
250 struct cil_iomemcon *aiomemcon = *(struct cil_iomemcon**)a;
251 struct cil_iomemcon *biomemcon = *(struct cil_iomemcon**)b;
252
253 rc = (aiomemcon->iomem_high - aiomemcon->iomem_low)
254 - (biomemcon->iomem_high - biomemcon->iomem_low);
255 if (rc == 0) {
256 if (aiomemcon->iomem_low < biomemcon->iomem_low) {
257 rc = -1;
258 } else if (biomemcon->iomem_low < aiomemcon->iomem_low) {
259 rc = 1;
260 }
261 }
262
263 return rc;
264 }
265
cil_post_ioportcon_compare(const void * a,const void * b)266 int cil_post_ioportcon_compare(const void *a, const void *b)
267 {
268 int rc = SEPOL_ERR;
269 struct cil_ioportcon *aioportcon = *(struct cil_ioportcon**)a;
270 struct cil_ioportcon *bioportcon = *(struct cil_ioportcon**)b;
271
272 rc = (aioportcon->ioport_high - aioportcon->ioport_low)
273 - (bioportcon->ioport_high - bioportcon->ioport_low);
274 if (rc == 0) {
275 if (aioportcon->ioport_low < bioportcon->ioport_low) {
276 rc = -1;
277 } else if (bioportcon->ioport_low < aioportcon->ioport_low) {
278 rc = 1;
279 }
280 }
281
282 return rc;
283 }
284
cil_post_pcidevicecon_compare(const void * a,const void * b)285 int cil_post_pcidevicecon_compare(const void *a, const void *b)
286 {
287 int rc = SEPOL_ERR;
288 struct cil_pcidevicecon *apcidevicecon = *(struct cil_pcidevicecon**)a;
289 struct cil_pcidevicecon *bpcidevicecon = *(struct cil_pcidevicecon**)b;
290
291 if (apcidevicecon->dev < bpcidevicecon->dev) {
292 rc = -1;
293 } else if (bpcidevicecon->dev < apcidevicecon->dev) {
294 rc = 1;
295 } else {
296 rc = 0;
297 }
298
299 return rc;
300 }
301
cil_post_devicetreecon_compare(const void * a,const void * b)302 int cil_post_devicetreecon_compare(const void *a, const void *b)
303 {
304 int rc = SEPOL_ERR;
305 struct cil_devicetreecon *adevicetreecon = *(struct cil_devicetreecon**)a;
306 struct cil_devicetreecon *bdevicetreecon = *(struct cil_devicetreecon**)b;
307
308 rc = strcmp(adevicetreecon->path, bdevicetreecon->path);
309
310 return rc;
311 }
312
cil_post_fsuse_compare(const void * a,const void * b)313 int cil_post_fsuse_compare(const void *a, const void *b)
314 {
315 int rc;
316 struct cil_fsuse *afsuse;
317 struct cil_fsuse *bfsuse;
318 afsuse = *(struct cil_fsuse**)a;
319 bfsuse = *(struct cil_fsuse**)b;
320 if (afsuse->type < bfsuse->type) {
321 rc = -1;
322 } else if (bfsuse->type < afsuse->type) {
323 rc = 1;
324 } else {
325 rc = strcmp(afsuse->fs_str, bfsuse->fs_str);
326 }
327 return rc;
328 }
329
__cil_post_db_count_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)330 static int __cil_post_db_count_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
331 {
332 struct cil_db *db = extra_args;
333
334 switch(node->flavor) {
335 case CIL_BLOCK: {
336 struct cil_block *blk = node->data;
337 if (blk->is_abstract == CIL_TRUE) {
338 *finished = CIL_TREE_SKIP_HEAD;
339 }
340 break;
341 }
342 case CIL_MACRO:
343 *finished = CIL_TREE_SKIP_HEAD;
344 break;
345 case CIL_CLASS: {
346 struct cil_class *class = node->data;
347 if (class->datum.nodes->head->data == node) {
348 // Multiple nodes can point to the same datum. Only count once.
349 db->num_classes++;
350 }
351 break;
352 }
353 case CIL_TYPE: {
354 struct cil_type *type = node->data;
355 if (type->datum.nodes->head->data == node) {
356 // Multiple nodes can point to the same datum. Only count once.
357 type->value = db->num_types;
358 db->num_types++;
359 db->num_types_and_attrs++;
360 }
361 break;
362 }
363 case CIL_TYPEATTRIBUTE: {
364 struct cil_typeattribute *attr = node->data;
365 if (attr->datum.nodes->head->data == node) {
366 // Multiple nodes can point to the same datum. Only count once.
367 db->num_types_and_attrs++;
368 }
369 break;
370 }
371
372 case CIL_ROLE: {
373 struct cil_role *role = node->data;
374 if (role->datum.nodes->head->data == node) {
375 // Multiple nodes can point to the same datum. Only count once.
376 role->value = db->num_roles;
377 db->num_roles++;
378 }
379 break;
380 }
381 case CIL_USER: {
382 struct cil_user *user = node->data;
383 if (user->datum.nodes->head->data == node) {
384 // multiple AST nodes can point to the same cil_user data (like if
385 // copied from a macro). This check ensures we only count the
386 // duplicates once
387 user->value = db->num_users;
388 db->num_users++;
389 }
390 break;
391 }
392 case CIL_NETIFCON:
393 db->netifcon->count++;
394 break;
395 case CIL_GENFSCON:
396 db->genfscon->count++;
397 break;
398 case CIL_FILECON:
399 db->filecon->count++;
400 break;
401 case CIL_NODECON:
402 db->nodecon->count++;
403 break;
404 case CIL_PORTCON:
405 db->portcon->count++;
406 break;
407 case CIL_PIRQCON:
408 db->pirqcon->count++;
409 break;
410 case CIL_IOMEMCON:
411 db->iomemcon->count++;
412 break;
413 case CIL_IOPORTCON:
414 db->ioportcon->count++;
415 break;
416 case CIL_PCIDEVICECON:
417 db->pcidevicecon->count++;
418 break;
419 case CIL_DEVICETREECON:
420 db->devicetreecon->count++;
421 break;
422 case CIL_FSUSE:
423 db->fsuse->count++;
424 break;
425 default:
426 break;
427 }
428
429 return SEPOL_OK;
430 }
431
__cil_post_db_array_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)432 static int __cil_post_db_array_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
433 {
434 struct cil_db *db = extra_args;
435
436 switch(node->flavor) {
437 case CIL_BLOCK: {
438 struct cil_block *blk = node->data;
439 if (blk->is_abstract == CIL_TRUE) {
440 *finished = CIL_TREE_SKIP_HEAD;
441 }
442 break;
443 }
444 case CIL_MACRO:
445 *finished = CIL_TREE_SKIP_HEAD;
446 break;
447 case CIL_TYPE: {
448 struct cil_type *type = node->data;
449 if (db->val_to_type == NULL) {
450 db->val_to_type = cil_malloc(sizeof(*db->val_to_type) * db->num_types);
451 }
452 db->val_to_type[type->value] = type;
453 break;
454 }
455 case CIL_ROLE: {
456 struct cil_role *role = node->data;
457 if (db->val_to_role == NULL) {
458 db->val_to_role = cil_malloc(sizeof(*db->val_to_role) * db->num_roles);
459 }
460 db->val_to_role[role->value] = role;
461 break;
462 }
463 case CIL_USER: {
464 struct cil_user *user= node->data;
465 if (db->val_to_user == NULL) {
466 db->val_to_user = cil_malloc(sizeof(*db->val_to_user) * db->num_users);
467 }
468 db->val_to_user[user->value] = user;
469 break;
470 }
471 case CIL_USERPREFIX: {
472 cil_list_append(db->userprefixes, CIL_USERPREFIX, node->data);
473 break;
474 }
475 case CIL_SELINUXUSER: {
476 cil_list_prepend(db->selinuxusers, CIL_SELINUXUSER, node->data);
477 break;
478 }
479 case CIL_SELINUXUSERDEFAULT: {
480 cil_list_append(db->selinuxusers, CIL_SELINUXUSERDEFAULT, node->data);
481 break;
482 }
483 case CIL_NETIFCON: {
484 struct cil_sort *sort = db->netifcon;
485 uint32_t count = sort->count;
486 uint32_t i = sort->index;
487 if (sort->array == NULL) {
488 sort->array = cil_malloc(sizeof(*sort->array)*count);
489 }
490 sort->array[i] = node->data;
491 sort->index++;
492 break;
493 }
494 case CIL_FSUSE: {
495 struct cil_sort *sort = db->fsuse;
496 uint32_t count = sort->count;
497 uint32_t i = sort->index;
498 if (sort->array == NULL) {
499 sort->array = cil_malloc(sizeof(*sort->array)*count);
500 }
501 sort->array[i] = node->data;
502 sort->index++;
503 break;
504 }
505 case CIL_GENFSCON: {
506 struct cil_sort *sort = db->genfscon;
507 uint32_t count = sort->count;
508 uint32_t i = sort->index;
509 if (sort->array == NULL) {
510 sort->array = cil_malloc(sizeof(*sort->array)*count);
511 }
512 sort->array[i] = node->data;
513 sort->index++;
514 break;
515 }
516 case CIL_FILECON: {
517 struct cil_sort *sort = db->filecon;
518 uint32_t count = sort->count;
519 uint32_t i = sort->index;
520 if (sort->array == NULL) {
521 sort->array = cil_malloc(sizeof(*sort->array)*count);
522 }
523 sort->array[i] = node->data;
524 sort->index++;
525 break;
526 }
527 case CIL_NODECON: {
528 struct cil_sort *sort = db->nodecon;
529 uint32_t count = sort->count;
530 uint32_t i = sort->index;
531 if (sort->array == NULL) {
532 sort->array = cil_malloc(sizeof(*sort->array)*count);
533 }
534 sort->array[i] = node->data;
535 sort->index++;
536 break;
537 }
538 case CIL_PORTCON: {
539 struct cil_sort *sort = db->portcon;
540 uint32_t count = sort->count;
541 uint32_t i = sort->index;
542 if (sort->array == NULL) {
543 sort->array = cil_malloc(sizeof(*sort->array)*count);
544 }
545 sort->array[i] = node->data;
546 sort->index++;
547 break;
548 }
549 case CIL_PIRQCON: {
550 struct cil_sort *sort = db->pirqcon;
551 uint32_t count = sort->count;
552 uint32_t i = sort->index;
553 if (sort->array == NULL) {
554 sort->array = cil_malloc(sizeof(*sort->array)*count);
555 }
556 sort->array[i] = node->data;
557 sort->index++;
558 break;
559 }
560 case CIL_IOMEMCON: {
561 struct cil_sort *sort = db->iomemcon;
562 uint32_t count = sort->count;
563 uint32_t i = sort->index;
564 if (sort->array == NULL) {
565 sort->array = cil_malloc(sizeof(*sort->array)*count);
566 }
567 sort->array[i] = node->data;
568 sort->index++;
569 break;
570 }
571 case CIL_IOPORTCON: {
572 struct cil_sort *sort = db->ioportcon;
573 uint32_t count = sort->count;
574 uint32_t i = sort->index;
575 if (sort->array == NULL) {
576 sort->array = cil_malloc(sizeof(*sort->array)*count);
577 }
578 sort->array[i] = node->data;
579 sort->index++;
580 break;
581 }
582 case CIL_PCIDEVICECON: {
583 struct cil_sort *sort = db->pcidevicecon;
584 uint32_t count = sort->count;
585 uint32_t i = sort->index;
586 if (sort->array == NULL) {
587 sort->array = cil_malloc(sizeof(*sort->array)*count);
588 }
589 sort->array[i] = node->data;
590 sort->index++;
591 break;
592 }
593 case CIL_DEVICETREECON: {
594 struct cil_sort *sort = db->devicetreecon;
595 uint32_t count = sort->count;
596 uint32_t i = sort->index;
597 if (sort->array == NULL) {
598 sort->array = cil_malloc(sizeof(*sort->array)*count);
599 }
600 sort->array[i] = node->data;
601 sort->index++;
602 break;
603 }
604 default:
605 break;
606 }
607
608 return SEPOL_OK;
609 }
610
__evaluate_type_expression(struct cil_typeattribute * attr,struct cil_db * db)611 static int __evaluate_type_expression(struct cil_typeattribute *attr, struct cil_db *db)
612 {
613 int rc;
614
615 attr->types = cil_malloc(sizeof(*attr->types));
616 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->types, db->num_types, db);
617 if (rc != SEPOL_OK) {
618 cil_log(CIL_ERR, "Failed to expand type attribute to bitmap\n");
619 ebitmap_destroy(attr->types);
620 free(attr->types);
621 attr->types = NULL;
622 }
623 return rc;
624 }
625
__cil_type_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)626 static int __cil_type_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
627 {
628 int rc = SEPOL_ERR;
629 struct cil_tree_node *node = datum->nodes->head->data;
630
631 ebitmap_init(bitmap);
632
633 if (node->flavor == CIL_TYPEATTRIBUTE) {
634 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
635 if (attr->types == NULL) {
636 rc = __evaluate_type_expression(attr, db);
637 if (rc != SEPOL_OK) goto exit;
638 }
639 ebitmap_union(bitmap, attr->types);
640 } else if (node->flavor == CIL_TYPEALIAS) {
641 struct cil_alias *alias = (struct cil_alias *)datum;
642 struct cil_type *type = alias->actual;
643 if (ebitmap_set_bit(bitmap, type->value, 1)) {
644 cil_log(CIL_ERR, "Failed to set type bit\n");
645 ebitmap_destroy(bitmap);
646 goto exit;
647 }
648 } else {
649 struct cil_type *type = (struct cil_type *)datum;
650 if (ebitmap_set_bit(bitmap, type->value, 1)) {
651 cil_log(CIL_ERR, "Failed to set type bit\n");
652 ebitmap_destroy(bitmap);
653 goto exit;
654 }
655 }
656
657 return SEPOL_OK;
658
659 exit:
660 return rc;
661 }
662
__evaluate_user_expression(struct cil_userattribute * attr,struct cil_db * db)663 static int __evaluate_user_expression(struct cil_userattribute *attr, struct cil_db *db)
664 {
665 int rc;
666
667 attr->users = cil_malloc(sizeof(*attr->users));
668 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->users, db->num_users, db);
669 if (rc != SEPOL_OK) {
670 cil_log(CIL_ERR, "Failed to expand user attribute to bitmap\n");
671 ebitmap_destroy(attr->users);
672 free(attr->users);
673 attr->users = NULL;
674 }
675 return rc;
676 }
677
__cil_user_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)678 static int __cil_user_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
679 {
680 int rc = SEPOL_ERR;
681 struct cil_tree_node *node = datum->nodes->head->data;
682 struct cil_userattribute *attr = NULL;
683 struct cil_user *user = NULL;
684
685 ebitmap_init(bitmap);
686
687 if (node->flavor == CIL_USERATTRIBUTE) {
688 attr = (struct cil_userattribute *)datum;
689 if (attr->users == NULL) {
690 rc = __evaluate_user_expression(attr, db);
691 if (rc != SEPOL_OK) {
692 goto exit;
693 }
694 }
695 ebitmap_union(bitmap, attr->users);
696 } else {
697 user = (struct cil_user *)datum;
698 if (ebitmap_set_bit(bitmap, user->value, 1)) {
699 cil_log(CIL_ERR, "Failed to set user bit\n");
700 ebitmap_destroy(bitmap);
701 goto exit;
702 }
703 }
704
705 return SEPOL_OK;
706
707 exit:
708 return rc;
709 }
710
__evaluate_role_expression(struct cil_roleattribute * attr,struct cil_db * db)711 static int __evaluate_role_expression(struct cil_roleattribute *attr, struct cil_db *db)
712 {
713 int rc;
714
715 attr->roles = cil_malloc(sizeof(*attr->roles));
716 rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->roles, db->num_roles, db);
717 if (rc != SEPOL_OK) {
718 cil_log(CIL_ERR, "Failed to expand role attribute to bitmap\n");
719 ebitmap_destroy(attr->roles);
720 free(attr->roles);
721 attr->roles = NULL;
722 }
723 return rc;
724 }
725
__cil_role_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)726 static int __cil_role_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
727 {
728 int rc = SEPOL_ERR;
729 struct cil_tree_node *node = datum->nodes->head->data;
730
731 ebitmap_init(bitmap);
732
733 if (node->flavor == CIL_ROLEATTRIBUTE) {
734 struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
735 if (attr->roles == NULL) {
736 rc = __evaluate_role_expression(attr, db);
737 if (rc != SEPOL_OK) goto exit;
738 }
739 ebitmap_union(bitmap, attr->roles);
740 } else {
741 struct cil_role *role = (struct cil_role *)datum;
742 if (ebitmap_set_bit(bitmap, role->value, 1)) {
743 cil_log(CIL_ERR, "Failed to set role bit\n");
744 ebitmap_destroy(bitmap);
745 goto exit;
746 }
747 }
748
749 return SEPOL_OK;
750
751 exit:
752 return rc;
753 }
754
__evaluate_permissionx_expression(struct cil_permissionx * permx,struct cil_db * db)755 static int __evaluate_permissionx_expression(struct cil_permissionx *permx, struct cil_db *db)
756 {
757 int rc;
758
759 permx->perms = cil_malloc(sizeof(*permx->perms));
760 ebitmap_init(permx->perms);
761
762 rc = __cil_expr_to_bitmap(permx->expr_str, permx->perms, 0x10000, db); // max is one more than 0xFFFF
763 if (rc != SEPOL_OK) {
764 cil_log(CIL_ERR, "Failed to expand permissionx expression\n");
765 ebitmap_destroy(permx->perms);
766 free(permx->perms);
767 permx->perms = NULL;
768 }
769
770 return rc;
771 }
772
__cil_permx_str_to_int(char * permx_str,uint16_t * val)773 static int __cil_permx_str_to_int(char *permx_str, uint16_t *val)
774 {
775 char *endptr = NULL;
776 long lval = strtol(permx_str, &endptr, 0);
777
778 if (*endptr != '\0') {
779 cil_log(CIL_ERR, "permissionx value %s not valid number\n", permx_str);
780 goto exit;
781 }
782 if (lval < 0x0000 || lval > 0xFFFF) {
783 cil_log(CIL_ERR, "permissionx value %s must be between 0x0000 and 0xFFFF\n", permx_str);
784 goto exit;
785 }
786
787 *val = (uint16_t)lval;
788
789 return SEPOL_OK;
790
791 exit:
792 return SEPOL_ERR;
793 }
794
__cil_permx_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)795 static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
796 {
797 int rc = SEPOL_ERR;
798 uint16_t val;
799
800 ebitmap_init(bitmap);
801
802 rc = __cil_permx_str_to_int((char*)datum, &val);
803 if (rc != SEPOL_OK) {
804 goto exit;
805 }
806
807 if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
808 cil_log(CIL_ERR, "Failed to set permissionx bit\n");
809 ebitmap_destroy(bitmap);
810 goto exit;
811 }
812
813 return SEPOL_OK;
814
815 exit:
816 return rc;
817 }
818
__cil_perm_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)819 static int __cil_perm_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
820 {
821 struct cil_perm *perm = (struct cil_perm *)datum;
822 unsigned int value = perm->value;
823
824 ebitmap_init(bitmap);
825 if (ebitmap_set_bit(bitmap, value, 1)) {
826 cil_log(CIL_INFO, "Failed to set perm bit\n");
827 ebitmap_destroy(bitmap);
828 return SEPOL_ERR;
829 }
830
831 return SEPOL_OK;
832 }
833
__evaluate_cat_expression(struct cil_cats * cats,struct cil_db * db)834 static int __evaluate_cat_expression(struct cil_cats *cats, struct cil_db *db)
835 {
836 int rc = SEPOL_ERR;
837 ebitmap_t bitmap;
838 struct cil_list *new;
839 struct cil_list_item *curr;
840
841 if (cats->evaluated == CIL_TRUE) {
842 return SEPOL_OK;
843 }
844
845 if (cil_verify_is_list(cats->datum_expr, CIL_CAT)) {
846 return SEPOL_OK;
847 }
848
849 ebitmap_init(&bitmap);
850 rc = __cil_expr_to_bitmap(cats->datum_expr, &bitmap, db->num_cats, db);
851 if (rc != SEPOL_OK) {
852 cil_log(CIL_ERR, "Failed to expand category expression to bitmap\n");
853 ebitmap_destroy(&bitmap);
854 goto exit;
855 }
856
857 cil_list_init(&new, CIL_CAT);
858
859 cil_list_for_each(curr, db->catorder) {
860 struct cil_cat *cat = curr->data;
861 if (ebitmap_get_bit(&bitmap, cat->value)) {
862 cil_list_append(new, CIL_DATUM, cat);
863 }
864 }
865
866 ebitmap_destroy(&bitmap);
867 cil_list_destroy(&cats->datum_expr, CIL_FALSE);
868 cats->datum_expr = new;
869
870 cats->evaluated = CIL_TRUE;
871
872 return SEPOL_OK;
873
874 exit:
875 return rc;
876 }
877
__cil_cat_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)878 static int __cil_cat_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
879 {
880 int rc = SEPOL_ERR;
881 struct cil_tree_node *node = datum->nodes->head->data;
882
883 ebitmap_init(bitmap);
884
885 if (node->flavor == CIL_CATSET) {
886 struct cil_catset *catset = (struct cil_catset *)datum;
887 struct cil_list_item *curr;
888 if (catset->cats->evaluated == CIL_FALSE) {
889 rc = __evaluate_cat_expression(catset->cats, db);
890 if (rc != SEPOL_OK) goto exit;
891 }
892 for (curr = catset->cats->datum_expr->head; curr; curr = curr->next) {
893 struct cil_cat *cat = (struct cil_cat *)curr->data;
894 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
895 cil_log(CIL_ERR, "Failed to set cat bit\n");
896 ebitmap_destroy(bitmap);
897 goto exit;
898 }
899 }
900 } else if (node->flavor == CIL_CATALIAS) {
901 struct cil_alias *alias = (struct cil_alias *)datum;
902 struct cil_cat *cat = alias->actual;
903 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
904 cil_log(CIL_ERR, "Failed to set cat bit\n");
905 ebitmap_destroy(bitmap);
906 goto exit;
907 }
908 } else {
909 struct cil_cat *cat = (struct cil_cat *)datum;
910 if (ebitmap_set_bit(bitmap, cat->value, 1)) {
911 cil_log(CIL_ERR, "Failed to set cat bit\n");
912 ebitmap_destroy(bitmap);
913 goto exit;
914 }
915 }
916
917 return SEPOL_OK;
918
919 exit:
920 return rc;
921 }
922
__cil_cat_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)923 static int __cil_cat_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
924 {
925 int rc = SEPOL_ERR;
926 struct cil_symtab_datum *d1 = i1->data;
927 struct cil_symtab_datum *d2 = i2->data;
928 struct cil_tree_node *n1 = d1->nodes->head->data;
929 struct cil_tree_node *n2 = d2->nodes->head->data;
930 struct cil_cat *c1 = (struct cil_cat *)d1;
931 struct cil_cat *c2 = (struct cil_cat *)d2;
932 int i;
933
934 if (n1->flavor == CIL_CATSET || n2->flavor == CIL_CATSET) {
935 cil_log(CIL_ERR, "Category sets cannont be used in a category range\n");
936 goto exit;
937 }
938
939 if (n1->flavor == CIL_CATALIAS) {
940 struct cil_alias *alias = (struct cil_alias *)d1;
941 c1 = alias->actual;
942 }
943
944 if (n2->flavor == CIL_CATALIAS) {
945 struct cil_alias *alias = (struct cil_alias *)d2;
946 c2 = alias->actual;
947 }
948
949 if (c1->value > c2->value) {
950 cil_log(CIL_ERR, "Invalid category range\n");
951 goto exit;
952 }
953
954 for (i = c1->value; i <= c2->value; i++) {
955 if (ebitmap_set_bit(bitmap, i, 1)) {
956 cil_log(CIL_ERR, "Failed to set cat bit\n");
957 ebitmap_destroy(bitmap);
958 goto exit;
959 }
960 }
961
962 return SEPOL_OK;
963
964 exit:
965 return rc;
966 }
967
__cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)968 static int __cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
969 {
970 int rc = SEPOL_ERR;
971 char *p1 = i1->data;
972 char *p2 = i2->data;
973 uint16_t v1;
974 uint16_t v2;
975 uint32_t i;
976
977 rc = __cil_permx_str_to_int(p1, &v1);
978 if (rc != SEPOL_OK) {
979 goto exit;
980 }
981
982 rc = __cil_permx_str_to_int(p2, &v2);
983 if (rc != SEPOL_OK) {
984 goto exit;
985 }
986
987 for (i = v1; i <= v2; i++) {
988 if (ebitmap_set_bit(bitmap, i, 1)) {
989 cil_log(CIL_ERR, "Failed to set permissionx bit\n");
990 ebitmap_destroy(bitmap);
991 goto exit;
992 }
993 }
994
995 return SEPOL_OK;
996
997 exit:
998 return rc;
999 }
1000
__cil_expr_to_bitmap_helper(struct cil_list_item * curr,enum cil_flavor flavor,ebitmap_t * bitmap,int max,struct cil_db * db)1001 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)
1002 {
1003 int rc = SEPOL_ERR;
1004
1005 if (curr->flavor == CIL_DATUM) {
1006 switch (flavor) {
1007 case CIL_TYPE:
1008 rc = __cil_type_to_bitmap(curr->data, bitmap, db);
1009 break;
1010 case CIL_ROLE:
1011 rc = __cil_role_to_bitmap(curr->data, bitmap, db);
1012 break;
1013 case CIL_USER:
1014 rc = __cil_user_to_bitmap(curr->data, bitmap, db);
1015 break;
1016 case CIL_PERM:
1017 rc = __cil_perm_to_bitmap(curr->data, bitmap, db);
1018 break;
1019 case CIL_CAT:
1020 rc = __cil_cat_to_bitmap(curr->data, bitmap, db);
1021 break;
1022 default:
1023 rc = SEPOL_ERR;
1024 }
1025 } else if (curr->flavor == CIL_LIST) {
1026 struct cil_list *l = curr->data;
1027 ebitmap_init(bitmap);
1028 rc = __cil_expr_to_bitmap(l, bitmap, max, db);
1029 if (rc != SEPOL_OK) {
1030 ebitmap_destroy(bitmap);
1031 }
1032 } else if (flavor == CIL_PERMISSIONX) {
1033 // permissionx expressions aren't resolved into anything, so curr->flavor
1034 // is just a CIL_STRING, not a CIL_DATUM, so just check on flavor for those
1035 rc = __cil_permx_to_bitmap(curr->data, bitmap, db);
1036 }
1037
1038 return rc;
1039 }
1040
__cil_expr_to_bitmap(struct cil_list * expr,ebitmap_t * out,int max,struct cil_db * db)1041 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db)
1042 {
1043 int rc = SEPOL_ERR;
1044 struct cil_list_item *curr;
1045 enum cil_flavor flavor;
1046 ebitmap_t tmp, b1, b2;
1047
1048 if (expr == NULL || expr->head == NULL) {
1049 return SEPOL_OK;
1050 }
1051
1052 curr = expr->head;
1053 flavor = expr->flavor;
1054
1055 if (curr->flavor == CIL_OP) {
1056 enum cil_flavor op = (enum cil_flavor)curr->data;
1057
1058 if (op == CIL_ALL) {
1059 ebitmap_init(&b1); /* all zeros */
1060 rc = ebitmap_not(&tmp, &b1, max);
1061 ebitmap_destroy(&b1);
1062 if (rc != SEPOL_OK) {
1063 cil_log(CIL_INFO, "Failed to expand 'all' operator\n");
1064 ebitmap_destroy(&tmp);
1065 goto exit;
1066 }
1067 } else if (op == CIL_RANGE) {
1068 if (flavor == CIL_CAT) {
1069 ebitmap_init(&tmp);
1070 rc = __cil_cat_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1071 if (rc != SEPOL_OK) {
1072 cil_log(CIL_INFO, "Failed to expand category range\n");
1073 ebitmap_destroy(&tmp);
1074 goto exit;
1075 }
1076 } else if (flavor == CIL_PERMISSIONX) {
1077 ebitmap_init(&tmp);
1078 rc = __cil_permissionx_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1079 if (rc != SEPOL_OK) {
1080 cil_log(CIL_INFO, "Failed to expand category range\n");
1081 ebitmap_destroy(&tmp);
1082 goto exit;
1083 }
1084 } else {
1085 cil_log(CIL_INFO, "Range operation only supported for categories permissionx\n");
1086 rc = SEPOL_ERR;
1087 goto exit;
1088 }
1089 } else {
1090 rc = __cil_expr_to_bitmap_helper(curr->next, flavor, &b1, max, db);
1091 if (rc != SEPOL_OK) {
1092 cil_log(CIL_INFO, "Failed to get first operand bitmap\n");
1093 goto exit;
1094 }
1095
1096 if (op == CIL_NOT) {
1097 rc = ebitmap_not(&tmp, &b1, max);
1098 ebitmap_destroy(&b1);
1099 if (rc != SEPOL_OK) {
1100 cil_log(CIL_INFO, "Failed to NOT bitmap\n");
1101 ebitmap_destroy(&tmp);
1102 goto exit;
1103 }
1104 } else {
1105 rc = __cil_expr_to_bitmap_helper(curr->next->next, flavor, &b2, max, db);
1106 if (rc != SEPOL_OK) {
1107 cil_log(CIL_INFO, "Failed to get second operand bitmap\n");
1108 goto exit;
1109 }
1110
1111 if (op == CIL_OR) {
1112 rc = ebitmap_or(&tmp, &b1, &b2);
1113 } else if (op == CIL_AND) {
1114 rc = ebitmap_and(&tmp, &b1, &b2);
1115 } else if (op == CIL_XOR) {
1116 rc = ebitmap_xor(&tmp, &b1, &b2);
1117 } else {
1118 rc = SEPOL_ERR;
1119 }
1120 ebitmap_destroy(&b1);
1121 ebitmap_destroy(&b2);
1122 if (rc != SEPOL_OK) {
1123 cil_log(CIL_INFO, "Failed to apply operator to bitmaps\n");
1124 ebitmap_destroy(&tmp);
1125 goto exit;
1126 }
1127 }
1128 }
1129 } else {
1130 ebitmap_init(&tmp);
1131 for (;curr; curr = curr->next) {
1132 rc = __cil_expr_to_bitmap_helper(curr, flavor, &b2, max, db);
1133 if (rc != SEPOL_OK) {
1134 cil_log(CIL_INFO, "Failed to get operand in list\n");
1135 ebitmap_destroy(&tmp);
1136 goto exit;
1137 }
1138 b1 = tmp;
1139 rc = ebitmap_or(&tmp, &b1, &b2);
1140 ebitmap_destroy(&b1);
1141 ebitmap_destroy(&b2);
1142 if (rc != SEPOL_OK) {
1143 cil_log(CIL_INFO, "Failed to OR operands in list\n");
1144 ebitmap_destroy(&tmp);
1145 goto exit;
1146 }
1147
1148 }
1149 }
1150
1151 ebitmap_union(out, &tmp);
1152 ebitmap_destroy(&tmp);
1153
1154 return SEPOL_OK;
1155
1156 exit:
1157 return rc;
1158 }
1159
__cil_expr_list_to_bitmap(struct cil_list * expr_list,ebitmap_t * out,int max,struct cil_db * db)1160 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db)
1161 {
1162 int rc = SEPOL_ERR;
1163 struct cil_list_item *expr;
1164
1165 ebitmap_init(out);
1166
1167 if (expr_list == NULL) {
1168 return SEPOL_OK;
1169 }
1170
1171 cil_list_for_each(expr, expr_list) {
1172 ebitmap_t bitmap;
1173 struct cil_list *l = (struct cil_list *)expr->data;
1174 ebitmap_init(&bitmap);
1175 rc = __cil_expr_to_bitmap(l, &bitmap, max, db);
1176 if (rc != SEPOL_OK) {
1177 cil_log(CIL_INFO, "Failed to expand expression list to bitmap\n");
1178 ebitmap_destroy(&bitmap);
1179 goto exit;
1180 }
1181 ebitmap_union(out, &bitmap);
1182 ebitmap_destroy(&bitmap);
1183 }
1184
1185 return SEPOL_OK;
1186
1187 exit:
1188 return SEPOL_ERR;
1189 }
1190
cil_typeattribute_used(struct cil_typeattribute * attr,struct cil_db * db)1191 static int cil_typeattribute_used(struct cil_typeattribute *attr, struct cil_db *db)
1192 {
1193 if (!attr->used) {
1194 return CIL_FALSE;
1195 }
1196
1197 if (attr->used & CIL_ATTR_CONSTRAINT) {
1198 return CIL_TRUE;
1199 }
1200
1201 if (db->attrs_expand_generated || attr->used == CIL_ATTR_NEVERALLOW) {
1202 if (strcmp(DATUM(attr)->name, GEN_REQUIRE_ATTR) == 0) {
1203 return CIL_FALSE;
1204 } else if (strstr(DATUM(attr)->name, TYPEATTR_INFIX) != NULL) {
1205 return CIL_FALSE;
1206 }
1207
1208 if (attr->used == CIL_ATTR_NEVERALLOW) {
1209 return CIL_TRUE;
1210 }
1211 }
1212
1213 if (attr->used == CIL_ATTR_AVRULE) {
1214 if (ebitmap_cardinality(attr->types) < db->attrs_expand_size) {
1215 return CIL_FALSE;
1216 }
1217 }
1218
1219 return CIL_TRUE;
1220 }
1221
__cil_post_db_attr_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1222 static int __cil_post_db_attr_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1223 {
1224 int rc = SEPOL_ERR;
1225 struct cil_db *db = extra_args;
1226
1227 switch (node->flavor) {
1228 case CIL_BLOCK: {
1229 struct cil_block *blk = node->data;
1230 if (blk->is_abstract == CIL_TRUE) {
1231 *finished = CIL_TREE_SKIP_HEAD;
1232 }
1233 break;
1234 }
1235 case CIL_MACRO: {
1236 *finished = CIL_TREE_SKIP_HEAD;
1237 break;
1238 }
1239 case CIL_TYPEATTRIBUTE: {
1240 struct cil_typeattribute *attr = node->data;
1241 if (attr->types == NULL) {
1242 rc = __evaluate_type_expression(attr, db);
1243 if (rc != SEPOL_OK) goto exit;
1244 }
1245 attr->used = cil_typeattribute_used(attr, db);
1246 break;
1247 }
1248 case CIL_ROLEATTRIBUTE: {
1249 struct cil_roleattribute *attr = node->data;
1250 if (attr->roles == NULL) {
1251 rc = __evaluate_role_expression(attr, db);
1252 if (rc != SEPOL_OK) goto exit;
1253 }
1254 break;
1255 }
1256 case CIL_AVRULEX: {
1257 struct cil_avrule *rule = node->data;
1258 if (rule->perms.x.permx_str == NULL) {
1259 rc = __evaluate_permissionx_expression(rule->perms.x.permx, db);
1260 if (rc != SEPOL_OK) goto exit;
1261 }
1262 break;
1263 }
1264 case CIL_PERMISSIONX: {
1265 struct cil_permissionx *permx = node->data;
1266 rc = __evaluate_permissionx_expression(permx, db);
1267 if (rc != SEPOL_OK) goto exit;
1268 break;
1269 }
1270 case CIL_USERATTRIBUTE: {
1271 struct cil_userattribute *attr = node->data;
1272 if (attr->users == NULL) {
1273 rc = __evaluate_user_expression(attr, db);
1274 if (rc != SEPOL_OK) {
1275 goto exit;
1276 }
1277 }
1278 break;
1279 }
1280 default:
1281 break;
1282 }
1283
1284 return SEPOL_OK;
1285
1286 exit:
1287 return rc;
1288 }
1289
__cil_role_assign_types(struct cil_role * role,struct cil_symtab_datum * datum)1290 static int __cil_role_assign_types(struct cil_role *role, struct cil_symtab_datum *datum)
1291 {
1292 struct cil_tree_node *node = datum->nodes->head->data;
1293
1294 if (role->types == NULL) {
1295 role->types = cil_malloc(sizeof(*role->types));
1296 ebitmap_init(role->types);
1297 }
1298
1299 if (node->flavor == CIL_TYPE) {
1300 struct cil_type *type = (struct cil_type *)datum;
1301 if (ebitmap_set_bit(role->types, type->value, 1)) {
1302 cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1303 goto exit;
1304 }
1305 } else if (node->flavor == CIL_TYPEALIAS) {
1306 struct cil_alias *alias = (struct cil_alias *)datum;
1307 struct cil_type *type = alias->actual;
1308 if (ebitmap_set_bit(role->types, type->value, 1)) {
1309 cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1310 goto exit;
1311 }
1312 } else if (node->flavor == CIL_TYPEATTRIBUTE) {
1313 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
1314 ebitmap_union(role->types, attr->types);
1315 }
1316
1317 return SEPOL_OK;
1318
1319 exit:
1320 return SEPOL_ERR;
1321 }
1322
__cil_post_db_roletype_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1323 static int __cil_post_db_roletype_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1324 {
1325 int rc = SEPOL_ERR;
1326 struct cil_db *db = extra_args;
1327
1328 switch (node->flavor) {
1329 case CIL_BLOCK: {
1330 struct cil_block *blk = node->data;
1331 if (blk->is_abstract == CIL_TRUE) {
1332 *finished = CIL_TREE_SKIP_HEAD;
1333 }
1334 break;
1335 }
1336 case CIL_MACRO: {
1337 *finished = CIL_TREE_SKIP_HEAD;
1338 break;
1339 }
1340 case CIL_ROLETYPE: {
1341 struct cil_roletype *roletype = node->data;
1342 struct cil_symtab_datum *role_datum = roletype->role;
1343 struct cil_symtab_datum *type_datum = roletype->type;
1344 struct cil_tree_node *role_node = role_datum->nodes->head->data;
1345
1346 if (role_node->flavor == CIL_ROLEATTRIBUTE) {
1347 struct cil_roleattribute *attr = roletype->role;
1348 ebitmap_node_t *rnode;
1349 unsigned int i;
1350
1351 ebitmap_for_each_bit(attr->roles, rnode, i) {
1352 struct cil_role *role = NULL;
1353
1354 if (!ebitmap_get_bit(attr->roles, i)) {
1355 continue;
1356 }
1357
1358 role = db->val_to_role[i];
1359
1360 rc = __cil_role_assign_types(role, type_datum);
1361 if (rc != SEPOL_OK) {
1362 goto exit;
1363 }
1364 }
1365 } else {
1366 struct cil_role *role = roletype->role;
1367
1368 rc = __cil_role_assign_types(role, type_datum);
1369 if (rc != SEPOL_OK) {
1370 goto exit;
1371 }
1372 }
1373 break;
1374 }
1375 default:
1376 break;
1377 }
1378
1379 return SEPOL_OK;
1380 exit:
1381 cil_log(CIL_INFO, "cil_post_db_roletype_helper failed\n");
1382 return rc;
1383 }
1384
__cil_user_assign_roles(struct cil_user * user,struct cil_symtab_datum * datum)1385 static int __cil_user_assign_roles(struct cil_user *user, struct cil_symtab_datum *datum)
1386 {
1387 struct cil_tree_node *node = datum->nodes->head->data;
1388 struct cil_role *role = NULL;
1389 struct cil_roleattribute *attr = NULL;
1390
1391 if (user->roles == NULL) {
1392 user->roles = cil_malloc(sizeof(*user->roles));
1393 ebitmap_init(user->roles);
1394 }
1395
1396 if (node->flavor == CIL_ROLE) {
1397 role = (struct cil_role *)datum;
1398 if (ebitmap_set_bit(user->roles, role->value, 1)) {
1399 cil_log(CIL_INFO, "Failed to set bit in user roles bitmap\n");
1400 goto exit;
1401 }
1402 } else if (node->flavor == CIL_ROLEATTRIBUTE) {
1403 attr = (struct cil_roleattribute *)datum;
1404 ebitmap_union(user->roles, attr->roles);
1405 }
1406
1407 return SEPOL_OK;
1408
1409 exit:
1410 return SEPOL_ERR;
1411 }
1412
__cil_post_db_userrole_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1413 static int __cil_post_db_userrole_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1414 {
1415 int rc = SEPOL_ERR;
1416 struct cil_db *db = extra_args;
1417 struct cil_block *blk = NULL;
1418 struct cil_userrole *userrole = NULL;
1419 struct cil_symtab_datum *user_datum = NULL;
1420 struct cil_symtab_datum *role_datum = NULL;
1421 struct cil_tree_node *user_node = NULL;
1422 struct cil_userattribute *u_attr = NULL;
1423 unsigned int i;
1424 struct cil_user *user = NULL;
1425 ebitmap_node_t *unode = NULL;
1426
1427 switch (node->flavor) {
1428 case CIL_BLOCK: {
1429 blk = node->data;
1430 if (blk->is_abstract == CIL_TRUE) {
1431 *finished = CIL_TREE_SKIP_HEAD;
1432 }
1433 break;
1434 }
1435 case CIL_MACRO: {
1436 *finished = CIL_TREE_SKIP_HEAD;
1437 break;
1438 }
1439 case CIL_USERROLE: {
1440 userrole = node->data;
1441 user_datum = userrole->user;
1442 role_datum = userrole->role;
1443 user_node = user_datum->nodes->head->data;
1444
1445 if (user_node->flavor == CIL_USERATTRIBUTE) {
1446 u_attr = userrole->user;
1447
1448 ebitmap_for_each_bit(u_attr->users, unode, i) {
1449 if (!ebitmap_get_bit(u_attr->users, i)) {
1450 continue;
1451 }
1452
1453 user = db->val_to_user[i];
1454
1455 rc = __cil_user_assign_roles(user, role_datum);
1456 if (rc != SEPOL_OK) {
1457 goto exit;
1458 }
1459 }
1460 } else {
1461 user = userrole->user;
1462
1463 rc = __cil_user_assign_roles(user, role_datum);
1464 if (rc != SEPOL_OK) {
1465 goto exit;
1466 }
1467 }
1468
1469 break;
1470 }
1471 default:
1472 break;
1473 }
1474
1475 return SEPOL_OK;
1476 exit:
1477 cil_log(CIL_INFO, "cil_post_db_userrole_helper failed\n");
1478 return rc;
1479 }
1480
__evaluate_level_expression(struct cil_level * level,struct cil_db * db)1481 static int __evaluate_level_expression(struct cil_level *level, struct cil_db *db)
1482 {
1483 if (level->cats != NULL) {
1484 return __evaluate_cat_expression(level->cats, db);
1485 }
1486
1487 return SEPOL_OK;
1488 }
1489
__evaluate_levelrange_expression(struct cil_levelrange * levelrange,struct cil_db * db)1490 static int __evaluate_levelrange_expression(struct cil_levelrange *levelrange, struct cil_db *db)
1491 {
1492 int rc = SEPOL_OK;
1493
1494 if (levelrange->low != NULL && levelrange->low->cats != NULL) {
1495 rc = __evaluate_cat_expression(levelrange->low->cats, db);
1496 if (rc != SEPOL_OK) {
1497 goto exit;
1498 }
1499 }
1500 if (levelrange->high != NULL && levelrange->high->cats != NULL) {
1501 rc = __evaluate_cat_expression(levelrange->high->cats, db);
1502 if (rc != SEPOL_OK) {
1503 goto exit;
1504 }
1505 }
1506
1507 exit:
1508 return rc;
1509 }
1510
__cil_post_db_cat_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1511 static int __cil_post_db_cat_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1512 {
1513 int rc = SEPOL_ERR;
1514 struct cil_db *db = extra_args;
1515
1516 switch (node->flavor) {
1517 case CIL_BLOCK: {
1518 struct cil_block *blk = node->data;
1519 if (blk->is_abstract == CIL_TRUE) {
1520 *finished = CIL_TREE_SKIP_HEAD;
1521 }
1522 break;
1523 }
1524 case CIL_MACRO: {
1525 *finished = CIL_TREE_SKIP_HEAD;
1526 break;
1527 }
1528 case CIL_CATSET: {
1529 struct cil_catset *catset = node->data;
1530 rc = __evaluate_cat_expression(catset->cats, db);
1531 if (rc != SEPOL_OK) {
1532 goto exit;
1533 }
1534 break;
1535 }
1536 case CIL_SENSCAT: {
1537 struct cil_senscat *senscat = node->data;
1538 rc = __evaluate_cat_expression(senscat->cats, db);
1539 if (rc != SEPOL_OK) {
1540 goto exit;
1541 }
1542 break;
1543 }
1544 case CIL_LEVEL: {
1545 rc = __evaluate_level_expression(node->data, db);
1546 if (rc != SEPOL_OK) {
1547 goto exit;
1548 }
1549 break;
1550 }
1551 case CIL_LEVELRANGE: {
1552 rc = __evaluate_levelrange_expression(node->data, db);
1553 if (rc != SEPOL_OK) {
1554 goto exit;
1555 }
1556 break;
1557 }
1558 case CIL_USER: {
1559 struct cil_user *user = node->data;
1560 rc = __evaluate_level_expression(user->dftlevel, db);
1561 if (rc != SEPOL_OK) {
1562 goto exit;
1563 }
1564 rc = __evaluate_levelrange_expression(user->range, db);
1565 if (rc != SEPOL_OK) {
1566 goto exit;
1567 }
1568 break;
1569 }
1570 case CIL_SELINUXUSERDEFAULT:
1571 case CIL_SELINUXUSER: {
1572 struct cil_selinuxuser *selinuxuser = node->data;
1573 rc = __evaluate_levelrange_expression(selinuxuser->range, db);
1574 if (rc != SEPOL_OK) {
1575 goto exit;
1576 }
1577 break;
1578 }
1579 case CIL_RANGETRANSITION: {
1580 struct cil_rangetransition *rangetrans = node->data;
1581 rc = __evaluate_levelrange_expression(rangetrans->range, db);
1582 if (rc != SEPOL_OK) {
1583 goto exit;
1584 }
1585 break;
1586 }
1587 case CIL_CONTEXT: {
1588 struct cil_context *context = node->data;
1589 rc = __evaluate_levelrange_expression(context->range, db);
1590 if (rc != SEPOL_OK) {
1591 goto exit;
1592 }
1593 break;
1594 }
1595 case CIL_SIDCONTEXT: {
1596 struct cil_sidcontext *sidcontext = node->data;
1597 rc = __evaluate_levelrange_expression(sidcontext->context->range, db);
1598 if (rc != SEPOL_OK) {
1599 goto exit;
1600 }
1601 break;
1602 }
1603 case CIL_FILECON: {
1604 struct cil_filecon *filecon = node->data;
1605 if (filecon->context) {
1606 rc = __evaluate_levelrange_expression(filecon->context->range, db);
1607 if (rc != SEPOL_OK) {
1608 goto exit;
1609 }
1610 }
1611 break;
1612 }
1613 case CIL_PORTCON: {
1614 struct cil_portcon *portcon = node->data;
1615 rc = __evaluate_levelrange_expression(portcon->context->range, db);
1616 if (rc != SEPOL_OK) {
1617 goto exit;
1618 }
1619 break;
1620 }
1621 case CIL_NODECON: {
1622 struct cil_nodecon *nodecon = node->data;
1623 rc = __evaluate_levelrange_expression(nodecon->context->range, db);
1624 if (rc != SEPOL_OK) {
1625 goto exit;
1626 }
1627 break;
1628 }
1629 case CIL_GENFSCON: {
1630 struct cil_genfscon *genfscon = node->data;
1631 rc = __evaluate_levelrange_expression(genfscon->context->range, db);
1632 if (rc != SEPOL_OK) {
1633 goto exit;
1634 }
1635 break;
1636 }
1637 case CIL_NETIFCON: {
1638 struct cil_netifcon *netifcon = node->data;
1639 rc = __evaluate_levelrange_expression(netifcon->if_context->range, db);
1640 if (rc != SEPOL_OK) {
1641 goto exit;
1642 }
1643 rc = __evaluate_levelrange_expression(netifcon->packet_context->range, db);
1644 if (rc != SEPOL_OK) {
1645 goto exit;
1646 }
1647 break;
1648 }
1649 case CIL_PIRQCON: {
1650 struct cil_pirqcon *pirqcon = node->data;
1651 rc = __evaluate_levelrange_expression(pirqcon->context->range, db);
1652 if (rc != SEPOL_OK) {
1653 goto exit;
1654 }
1655 break;
1656 }
1657 case CIL_IOMEMCON: {
1658 struct cil_iomemcon *iomemcon = node->data;
1659 rc = __evaluate_levelrange_expression(iomemcon->context->range, db);
1660 if (rc != SEPOL_OK) {
1661 goto exit;
1662 }
1663 break;
1664 }
1665 case CIL_IOPORTCON: {
1666 struct cil_ioportcon *ioportcon = node->data;
1667 rc = __evaluate_levelrange_expression(ioportcon->context->range, db);
1668 if (rc != SEPOL_OK) {
1669 goto exit;
1670 }
1671 break;
1672 }
1673 case CIL_PCIDEVICECON: {
1674 struct cil_pcidevicecon *pcidevicecon = node->data;
1675 rc = __evaluate_levelrange_expression(pcidevicecon->context->range, db);
1676 if (rc != SEPOL_OK) {
1677 goto exit;
1678 }
1679 break;
1680 }
1681 case CIL_DEVICETREECON: {
1682 struct cil_devicetreecon *devicetreecon = node->data;
1683 rc = __evaluate_levelrange_expression(devicetreecon->context->range, db);
1684 if (rc != SEPOL_OK) {
1685 goto exit;
1686 }
1687 break;
1688 }
1689 case CIL_FSUSE: {
1690 struct cil_fsuse *fsuse = node->data;
1691 rc = __evaluate_levelrange_expression(fsuse->context->range, db);
1692 if (rc != SEPOL_OK) {
1693 goto exit;
1694 }
1695 break;
1696 }
1697 default:
1698 break;
1699 }
1700
1701 return SEPOL_OK;
1702
1703 exit:
1704 return rc;
1705 }
1706
1707 struct perm_to_list {
1708 enum cil_flavor flavor;
1709 ebitmap_t *perms;
1710 struct cil_list *new_list;
1711 };
1712
__perm_bits_to_list(hashtab_key_t k,hashtab_datum_t d,void * args)1713 static int __perm_bits_to_list(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
1714 {
1715 struct perm_to_list *perm_args = (struct perm_to_list *)args;
1716 ebitmap_t *perms = perm_args->perms;
1717 struct cil_list *new_list = perm_args->new_list;
1718 struct cil_perm *perm = (struct cil_perm *)d;
1719 unsigned int value = perm->value;
1720
1721 if (!ebitmap_get_bit(perms, value)) {
1722 return SEPOL_OK;
1723 }
1724
1725 cil_list_append(new_list, CIL_DATUM, d);
1726
1727 return SEPOL_OK;
1728 }
1729
__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)1730 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)
1731 {
1732 int rc = SEPOL_ERR;
1733 struct perm_to_list args;
1734 ebitmap_t bitmap;
1735
1736 if (cil_verify_is_list(perms, CIL_PERM)) {
1737 return SEPOL_OK;
1738 }
1739
1740 ebitmap_init(&bitmap);
1741 rc = __cil_expr_to_bitmap(perms, &bitmap, num_perms, db);
1742 if (rc != SEPOL_OK) {
1743 ebitmap_destroy(&bitmap);
1744 goto exit;
1745 }
1746
1747 cil_list_init(new_list, flavor);
1748
1749 args.flavor = flavor;
1750 args.perms = &bitmap;
1751 args.new_list = *new_list;
1752
1753 cil_symtab_map(class_symtab, __perm_bits_to_list, &args);
1754
1755 if (common_symtab != NULL) {
1756 cil_symtab_map(common_symtab, __perm_bits_to_list, &args);
1757 }
1758
1759 ebitmap_destroy(&bitmap);
1760 return SEPOL_OK;
1761
1762 exit:
1763 return rc;
1764 }
1765
__evaluate_classperms(struct cil_classperms * cp,struct cil_db * db)1766 static int __evaluate_classperms(struct cil_classperms *cp, struct cil_db *db)
1767 {
1768 int rc = SEPOL_ERR;
1769 struct cil_class *class = cp->class;
1770 struct cil_class *common = class->common;
1771 symtab_t *common_symtab = NULL;
1772 struct cil_list *new_list = NULL;
1773
1774 if (common) {
1775 common_symtab = &common->perms;
1776 }
1777
1778 rc = __evaluate_perm_expression(cp->perms, CIL_PERM, &class->perms, common_symtab, class->num_perms, &new_list, db);
1779 if (rc != SEPOL_OK) {
1780 goto exit;
1781 }
1782
1783 if (new_list == NULL) {
1784 return SEPOL_OK;
1785 }
1786
1787 cil_list_destroy(&cp->perms, CIL_FALSE);
1788
1789 cp->perms = new_list;
1790
1791 return SEPOL_OK;
1792
1793 exit:
1794 return rc;
1795 }
1796
__evaluate_classperms_list(struct cil_list * classperms,struct cil_db * db)1797 static int __evaluate_classperms_list(struct cil_list *classperms, struct cil_db *db)
1798 {
1799 int rc = SEPOL_ERR;
1800 struct cil_list_item *curr;
1801
1802 cil_list_for_each(curr, classperms) {
1803 if (curr->flavor == CIL_CLASSPERMS) {
1804 struct cil_classperms *cp = curr->data;
1805 if (FLAVOR(cp->class) == CIL_CLASS) {
1806 rc = __evaluate_classperms(cp, db);
1807 if (rc != SEPOL_OK) {
1808 goto exit;
1809 }
1810 } else { /* MAP */
1811 struct cil_list_item *i = NULL;
1812 cil_list_for_each(i, cp->perms) {
1813 struct cil_perm *cmp = i->data;
1814 rc = __evaluate_classperms_list(cmp->classperms, db);
1815 if (rc != SEPOL_OK) {
1816 goto exit;
1817 }
1818 }
1819 }
1820 } else { /* SET */
1821 struct cil_classperms_set *cp_set = curr->data;
1822 struct cil_classpermission *cp = cp_set->set;
1823 rc = __evaluate_classperms_list(cp->classperms, db);
1824 if (rc != SEPOL_OK) {
1825 goto exit;
1826 }
1827 }
1828 }
1829
1830 return SEPOL_OK;
1831
1832 exit:
1833 return rc;
1834 }
1835
1836 struct class_map_args {
1837 struct cil_db *db;
1838 int rc;
1839 };
1840
__evaluate_map_perm_classperms(hashtab_key_t k,hashtab_datum_t d,void * args)1841 static int __evaluate_map_perm_classperms(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
1842 {
1843 struct class_map_args *map_args = args;
1844 struct cil_perm *cmp = (struct cil_perm *)d;
1845
1846 int rc = __evaluate_classperms_list(cmp->classperms, map_args->db);
1847
1848 if (rc != SEPOL_OK) {
1849 map_args->rc = rc;
1850 }
1851
1852 return SEPOL_OK;
1853 }
1854
__evaluate_map_class(struct cil_class * mc,struct cil_db * db)1855 static int __evaluate_map_class(struct cil_class *mc, struct cil_db *db)
1856 {
1857 struct class_map_args map_args;
1858
1859 map_args.db = db;
1860 map_args.rc = SEPOL_OK;
1861 cil_symtab_map(&mc->perms, __evaluate_map_perm_classperms, &map_args);
1862
1863 return map_args.rc;
1864 }
1865
__cil_post_db_classperms_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1866 static int __cil_post_db_classperms_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1867 {
1868 int rc = SEPOL_ERR;
1869 struct cil_db *db = extra_args;
1870
1871 switch (node->flavor) {
1872 case CIL_BLOCK: {
1873 struct cil_block *blk = node->data;
1874 if (blk->is_abstract == CIL_TRUE) {
1875 *finished = CIL_TREE_SKIP_HEAD;
1876 }
1877 break;
1878 }
1879 case CIL_MACRO:
1880 *finished = CIL_TREE_SKIP_HEAD;
1881 break;
1882 case CIL_MAP_CLASS: {
1883 rc = __evaluate_map_class(node->data, db);
1884 if (rc != SEPOL_OK) {
1885 goto exit;
1886 }
1887 break;
1888 }
1889 case CIL_CLASSPERMISSION: {
1890 struct cil_classpermission *cp = node->data;
1891 rc = __evaluate_classperms_list(cp->classperms, db);
1892 if (rc != SEPOL_OK) {
1893 goto exit;
1894 }
1895 break;
1896 }
1897 case CIL_AVRULE: {
1898 struct cil_avrule *avrule = node->data;
1899 rc = __evaluate_classperms_list(avrule->perms.classperms, db);
1900 if (rc != SEPOL_OK) {
1901 goto exit;
1902 }
1903 break;
1904 }
1905 case CIL_CONSTRAIN:
1906 case CIL_MLSCONSTRAIN: {
1907 struct cil_constrain *constrain = node->data;
1908 rc = __evaluate_classperms_list(constrain->classperms, db);
1909 if (rc != SEPOL_OK) {
1910 goto exit;
1911 }
1912 break;
1913 }
1914 default:
1915 break;
1916 }
1917
1918 return SEPOL_OK;
1919
1920 exit:
1921 return rc;
1922 }
1923
cil_post_db(struct cil_db * db)1924 static int cil_post_db(struct cil_db *db)
1925 {
1926 int rc = SEPOL_ERR;
1927
1928 rc = cil_tree_walk(db->ast->root, __cil_post_db_count_helper, NULL, NULL, db);
1929 if (rc != SEPOL_OK) {
1930 cil_log(CIL_INFO, "Failure during cil databse count helper\n");
1931 goto exit;
1932 }
1933
1934 rc = cil_tree_walk(db->ast->root, __cil_post_db_array_helper, NULL, NULL, db);
1935 if (rc != SEPOL_OK) {
1936 cil_log(CIL_INFO, "Failure during cil database array helper\n");
1937 goto exit;
1938 }
1939
1940 rc = cil_tree_walk(db->ast->root, __cil_post_db_attr_helper, NULL, NULL, db);
1941 if (rc != SEPOL_OK) {
1942 cil_log(CIL_INFO, "Failed to create attribute bitmaps\n");
1943 goto exit;
1944 }
1945
1946 rc = cil_tree_walk(db->ast->root, __cil_post_db_roletype_helper, NULL, NULL, db);
1947 if (rc != SEPOL_OK) {
1948 cil_log(CIL_INFO, "Failed during roletype association\n");
1949 goto exit;
1950 }
1951
1952 rc = cil_tree_walk(db->ast->root, __cil_post_db_userrole_helper, NULL, NULL, db);
1953 if (rc != SEPOL_OK) {
1954 cil_log(CIL_INFO, "Failed during userrole association\n");
1955 goto exit;
1956 }
1957
1958 rc = cil_tree_walk(db->ast->root, __cil_post_db_classperms_helper, NULL, NULL, db);
1959 if (rc != SEPOL_OK) {
1960 cil_log(CIL_INFO, "Failed to evaluate class mapping permissions expressions\n");
1961 goto exit;
1962 }
1963
1964 rc = cil_tree_walk(db->ast->root, __cil_post_db_cat_helper, NULL, NULL, db);
1965 if (rc != SEPOL_OK) {
1966 cil_log(CIL_INFO, "Failed to evaluate category expressions\n");
1967 goto exit;
1968 }
1969
1970 qsort(db->netifcon->array, db->netifcon->count, sizeof(db->netifcon->array), cil_post_netifcon_compare);
1971 qsort(db->genfscon->array, db->genfscon->count, sizeof(db->genfscon->array), cil_post_genfscon_compare);
1972 qsort(db->portcon->array, db->portcon->count, sizeof(db->portcon->array), cil_post_portcon_compare);
1973 qsort(db->nodecon->array, db->nodecon->count, sizeof(db->nodecon->array), cil_post_nodecon_compare);
1974 qsort(db->fsuse->array, db->fsuse->count, sizeof(db->fsuse->array), cil_post_fsuse_compare);
1975 qsort(db->filecon->array, db->filecon->count, sizeof(db->filecon->array), cil_post_filecon_compare);
1976 qsort(db->pirqcon->array, db->pirqcon->count, sizeof(db->pirqcon->array), cil_post_pirqcon_compare);
1977 qsort(db->iomemcon->array, db->iomemcon->count, sizeof(db->iomemcon->array), cil_post_iomemcon_compare);
1978 qsort(db->ioportcon->array, db->ioportcon->count, sizeof(db->ioportcon->array), cil_post_ioportcon_compare);
1979 qsort(db->pcidevicecon->array, db->pcidevicecon->count, sizeof(db->pcidevicecon->array), cil_post_pcidevicecon_compare);
1980 qsort(db->devicetreecon->array, db->devicetreecon->count, sizeof(db->devicetreecon->array), cil_post_devicetreecon_compare);
1981
1982 exit:
1983 return rc;
1984 }
1985
cil_post_verify(struct cil_db * db)1986 static int cil_post_verify(struct cil_db *db)
1987 {
1988 int rc = SEPOL_ERR;
1989 int avrule_cnt = 0;
1990 int handleunknown = -1;
1991 int mls = -1;
1992 int nseuserdflt = 0;
1993 int pass = 0;
1994 struct cil_args_verify extra_args;
1995 struct cil_complex_symtab csymtab;
1996
1997 cil_complex_symtab_init(&csymtab, CIL_CLASS_SYM_SIZE);
1998
1999 extra_args.db = db;
2000 extra_args.csymtab = &csymtab;
2001 extra_args.avrule_cnt = &avrule_cnt;
2002 extra_args.handleunknown = &handleunknown;
2003 extra_args.mls = &mls;
2004 extra_args.nseuserdflt = &nseuserdflt;
2005 extra_args.pass = &pass;
2006
2007 for (pass = 0; pass < 2; pass++) {
2008 rc = cil_tree_walk(db->ast->root, __cil_verify_helper, NULL, NULL, &extra_args);
2009 if (rc != SEPOL_OK) {
2010 cil_log(CIL_ERR, "Failed to verify cil database\n");
2011 goto exit;
2012 }
2013 }
2014
2015 if (db->handle_unknown == -1) {
2016 if (handleunknown == -1) {
2017 db->handle_unknown = SEPOL_DENY_UNKNOWN;
2018 } else {
2019 db->handle_unknown = handleunknown;
2020 }
2021 }
2022
2023 if (db->mls == -1) {
2024 if (mls == -1) {
2025 db->mls = CIL_FALSE;
2026 } else {
2027 db->mls = mls;
2028 }
2029 }
2030
2031 if (avrule_cnt == 0) {
2032 cil_log(CIL_ERR, "Policy must include at least one avrule\n");
2033 rc = SEPOL_ERR;
2034 goto exit;
2035 }
2036
2037 if (nseuserdflt > 1) {
2038 cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
2039 rc = SEPOL_ERR;
2040 goto exit;
2041 }
2042
2043 exit:
2044 cil_complex_symtab_destroy(&csymtab);
2045 return rc;
2046 }
2047
cil_pre_verify(struct cil_db * db)2048 static int cil_pre_verify(struct cil_db *db)
2049 {
2050 int rc = SEPOL_ERR;
2051 struct cil_args_verify extra_args;
2052
2053 extra_args.db = db;
2054
2055 rc = cil_tree_walk(db->ast->root, __cil_pre_verify_helper, NULL, NULL, &extra_args);
2056 if (rc != SEPOL_OK) {
2057 cil_log(CIL_ERR, "Failed to verify cil database\n");
2058 goto exit;
2059 }
2060
2061 exit:
2062 return rc;
2063 }
2064
cil_post_process(struct cil_db * db)2065 int cil_post_process(struct cil_db *db)
2066 {
2067 int rc = SEPOL_ERR;
2068
2069 rc = cil_pre_verify(db);
2070 if (rc != SEPOL_OK) {
2071 cil_log(CIL_ERR, "Failed to verify cil database\n");
2072 goto exit;
2073 }
2074
2075 rc = cil_post_db(db);
2076 if (rc != SEPOL_OK) {
2077 cil_log(CIL_ERR, "Failed post db handling\n");
2078 goto exit;
2079 }
2080
2081 rc = cil_post_verify(db);
2082 if (rc != SEPOL_OK) {
2083 cil_log(CIL_ERR, "Failed to verify cil database\n");
2084 goto exit;
2085 }
2086
2087 exit:
2088 return rc;
2089
2090 }
2091