Debian Codegraph @

cil/src/cil_resolve_ast.c

raw
1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <sepol/policydb/conditional.h>
35
36 #include "cil_internal.h"
37 #include "cil_flavor.h"
38 #include "cil_log.h"
39 #include "cil_mem.h"
40 #include "cil_tree.h"
41 #include "cil_list.h"
42 #include "cil_build_ast.h"
43 #include "cil_resolve_ast.h"
44 #include "cil_reset_ast.h"
45 #include "cil_copy_ast.h"
46 #include "cil_verify.h"
47 #include "cil_strpool.h"
48 #include "cil_symtab.h"
49 #include "cil_stack.h"
50
51 struct cil_args_resolve {
52 struct cil_db *db;
53 enum cil_pass pass;
54 uint32_t *changed;
55 struct cil_list *to_destroy;
56 struct cil_tree_node *block;
57 struct cil_tree_node *macro;
58 struct cil_tree_node *optional;
59 struct cil_tree_node *disabled_optional;
60 struct cil_tree_node *boolif;
61 struct cil_list *sidorder_lists;
62 struct cil_list *classorder_lists;
63 struct cil_list *unordered_classorder_lists;
64 struct cil_list *catorder_lists;
65 struct cil_list *sensitivityorder_lists;
66 struct cil_list *in_list_before;
67 struct cil_list *in_list_after;
68 struct cil_list *abstract_blocks;
69 };
70
71 static int __cil_resolve_perms(symtab_t *class_symtab, symtab_t *common_symtab, struct cil_list *perm_strs, struct cil_list **perm_datums, enum cil_flavor class_flavor)
72 {
73 int rc = SEPOL_ERR;
74 struct cil_list_item *curr;
75
76 cil_list_init(perm_datums, perm_strs->flavor);
77
78 cil_list_for_each(curr, perm_strs) {
79 if (curr->flavor == CIL_LIST) {
80 struct cil_list *sub_list;
81 rc = __cil_resolve_perms(class_symtab, common_symtab, curr->data, &sub_list, class_flavor);
82 if (rc != SEPOL_OK) {
83 cil_log(CIL_ERR, "Failed to resolve permission list\n");
84 goto exit;
85 }
86 cil_list_append(*perm_datums, CIL_LIST, sub_list);
87 } else if (curr->flavor == CIL_STRING) {
88 struct cil_symtab_datum *perm_datum = NULL;
89 rc = cil_symtab_get_datum(class_symtab, curr->data, &perm_datum);
90 if (rc == SEPOL_ENOENT) {
91 if (common_symtab) {
92 rc = cil_symtab_get_datum(common_symtab, curr->data, &perm_datum);
93 }
94 }
95 if (rc != SEPOL_OK) {
96 if (class_flavor == CIL_MAP_CLASS) {
97 cil_log(CIL_ERR, "Failed to resolve permission %s for map class\n", (char*)curr->data);
98 } else {
99 cil_log(CIL_ERR, "Failed to resolve permission %s\n", (char*)curr->data);
100 }
101 goto exit;
102 }
103 cil_list_append(*perm_datums, CIL_DATUM, perm_datum);
104 } else {
105 cil_list_append(*perm_datums, curr->flavor, curr->data);
106 }
107 }
108
109 return SEPOL_OK;
110
111 exit:
112 cil_list_destroy(perm_datums, CIL_FALSE);
113 return rc;
114 }
115
116 int cil_resolve_classperms(struct cil_tree_node *current, struct cil_classperms *cp, struct cil_db *db)
117 {
118 int rc = SEPOL_ERR;
119 struct cil_symtab_datum *datum = NULL;
120 symtab_t *common_symtab = NULL;
121 struct cil_class *class;
122
123 if (cp->class) {
124 return SEPOL_OK;
125 }
126
127 rc = cil_resolve_name(current, cp->class_str, CIL_SYM_CLASSES, db, &datum);
128 if (rc != SEPOL_OK) {
129 goto exit;
130 }
131
132 class = (struct cil_class *)datum;
133
134 if (class->common != NULL) {
135 common_symtab = &class->common->perms;
136 }
137
138 cp->class = class;
139
140 rc = __cil_resolve_perms(&class->perms, common_symtab, cp->perm_strs, &cp->perms, FLAVOR(datum));
141 if (rc != SEPOL_OK) {
142 goto exit;
143 }
144
145 return SEPOL_OK;
146
147 exit:
148 return rc;
149 }
150
151 static int cil_resolve_classperms_set(struct cil_tree_node *current, struct cil_classperms_set *cp_set, struct cil_db *db)
152 {
153 int rc = SEPOL_ERR;
154 struct cil_symtab_datum *datum = NULL;
155
156 rc = cil_resolve_name(current, cp_set->set_str, CIL_SYM_CLASSPERMSETS, db, &datum);
157 if (rc != SEPOL_OK) {
158 goto exit;
159 }
160 cp_set->set = (struct cil_classpermission*)datum;
161
162 /* This could be an anonymous classpermission */
163 if (datum->name == NULL) {
164 rc = cil_resolve_classperms_list(current, cp_set->set->classperms, db);
165 if (rc != SEPOL_OK) {
166 goto exit;
167 }
168 }
169
170 return SEPOL_OK;
171
172 exit:
173 return rc;
174 }
175
176 int cil_resolve_classperms_list(struct cil_tree_node *current, struct cil_list *cp_list, struct cil_db *db)
177 {
178 int rc = SEPOL_ERR;
179 struct cil_list_item *curr;
180
181 cil_list_for_each(curr, cp_list) {
182 if (curr->flavor == CIL_CLASSPERMS) {
183 rc = cil_resolve_classperms(current, curr->data, db);
184 if (rc != SEPOL_OK) {
185 goto exit;
186 }
187 } else {
188 rc = cil_resolve_classperms_set(current, curr->data, db);
189 if (rc != SEPOL_OK) {
190 goto exit;
191 }
192 }
193 }
194
195 return SEPOL_OK;
196
197 exit:
198 return rc;
199 }
200
201 int cil_resolve_classpermissionset(struct cil_tree_node *current, struct cil_classpermissionset *cps, struct cil_db *db)
202 {
203 int rc = SEPOL_ERR;
204 struct cil_list_item *curr;
205 struct cil_symtab_datum *datum;
206 struct cil_classpermission *cp;
207
208 rc = cil_resolve_name(current, cps->set_str, CIL_SYM_CLASSPERMSETS, db, &datum);
209 if (rc != SEPOL_OK) {
210 goto exit;
211 }
212
213 if (!datum->fqn) {
214 cil_tree_log(current, CIL_ERR, "Anonymous classpermission used in a classpermissionset");
215 rc = SEPOL_ERR;
216 goto exit;
217 }
218
219 rc = cil_resolve_classperms_list(current, cps->classperms, db);
220 if (rc != SEPOL_OK) {
221 goto exit;
222 }
223
224 cp = (struct cil_classpermission *)datum;
225 cps->set = cp;
226
227 if (cp->classperms == NULL) {
228 cil_list_init(&cp->classperms, CIL_CLASSPERMS);
229 }
230
231 cil_list_for_each(curr, cps->classperms) {
232 cil_list_append(cp->classperms, curr->flavor, curr->data);
233 }
234
235 return SEPOL_OK;
236
237 exit:
238 return rc;
239 }
240
241 static void cil_type_used(struct cil_symtab_datum *datum, int used)
242 {
243 struct cil_typeattribute *attr = NULL;
244
245 if (FLAVOR(datum) == CIL_TYPEATTRIBUTE) {
246 attr = (struct cil_typeattribute*)datum;
247 attr->used |= used;
248 if ((attr->used & CIL_ATTR_EXPAND_TRUE) &&
249 (attr->used & CIL_ATTR_EXPAND_FALSE)) {
250 cil_log(CIL_WARN, "Conflicting use of expandtypeattribute. "
251 "Expandtypeattribute was set to both true or false for %s. "
252 "Resolving to false. \n", attr->datum.name);
253 attr->used &= ~CIL_ATTR_EXPAND_TRUE;
254 }
255 }
256 }
257
258 static int cil_resolve_permissionx(struct cil_tree_node *current, struct cil_permissionx *permx, struct cil_db *db)
259 {
260 struct cil_symtab_datum *obj_datum = NULL;
261 int rc = SEPOL_ERR;
262
263 rc = cil_resolve_name(current, permx->obj_str, CIL_SYM_CLASSES, db, &obj_datum);
264 if (rc != SEPOL_OK) {
265 goto exit;
266 }
267 permx->obj = (struct cil_class*)obj_datum;
268
269 return SEPOL_OK;
270
271 exit:
272 return rc;
273 }
274
275 int cil_resolve_avrule(struct cil_tree_node *current, struct cil_db *db)
276 {
277 struct cil_avrule *rule = current->data;
278 struct cil_symtab_datum *src_datum = NULL;
279 struct cil_symtab_datum *tgt_datum = NULL;
280 struct cil_symtab_datum *permx_datum = NULL;
281 int used;
282 int rc = SEPOL_ERR;
283
284 rc = cil_resolve_name(current, rule->src_str, CIL_SYM_TYPES, db, &src_datum);
285 if (rc != SEPOL_OK) {
286 goto exit;
287 }
288 rule->src = src_datum;
289
290 if (rule->tgt_str == CIL_KEY_SELF) {
291 rule->tgt = db->selftype;
292 } else if (rule->tgt_str == CIL_KEY_NOTSELF) {
293 rule->tgt = db->notselftype;
294 } else if (rule->tgt_str == CIL_KEY_OTHER) {
295 rule->tgt = db->othertype;
296 } else {
297 rc = cil_resolve_name(current, rule->tgt_str, CIL_SYM_TYPES, db, &tgt_datum);
298 if (rc != SEPOL_OK) {
299 goto exit;
300 }
301 rule->tgt = tgt_datum;
302 used = (rule->rule_kind == CIL_AVRULE_NEVERALLOW) ?
303 CIL_ATTR_NEVERALLOW : CIL_ATTR_AVRULE;
304 cil_type_used(src_datum, used); /* src not used if tgt is self */
305 cil_type_used(tgt_datum, used);
306 }
307
308 if (!rule->is_extended) {
309 rc = cil_resolve_classperms_list(current, rule->perms.classperms, db);
310 if (rc != SEPOL_OK) {
311 goto exit;
312 }
313 } else {
314 if (rule->perms.x.permx_str != NULL) {
315 rc = cil_resolve_name(current, rule->perms.x.permx_str, CIL_SYM_PERMX, db, &permx_datum);
316 if (rc != SEPOL_OK) {
317 goto exit;
318 }
319 rule->perms.x.permx = (struct cil_permissionx*)permx_datum;
320 } else {
321 rc = cil_resolve_permissionx(current, rule->perms.x.permx, db);
322 if (rc != SEPOL_OK) {
323 goto exit;
324 }
325 }
326 }
327
328 return SEPOL_OK;
329
330 exit:
331 return rc;
332 }
333
334 int cil_resolve_deny_rule(struct cil_tree_node *current, struct cil_db *db)
335 {
336 struct cil_deny_rule *rule = current->data;
337 struct cil_symtab_datum *src_datum = NULL;
338 struct cil_symtab_datum *tgt_datum = NULL;
339 int rc = SEPOL_ERR;
340
341 rc = cil_resolve_name(current, rule->src_str, CIL_SYM_TYPES, db, &src_datum);
342 if (rc != SEPOL_OK) {
343 goto exit;
344 }
345 rule->src = src_datum;
346
347 if (rule->tgt_str == CIL_KEY_SELF) {
348 rule->tgt = db->selftype;
349 } else if (rule->tgt_str == CIL_KEY_NOTSELF) {
350 rule->tgt = db->notselftype;
351 } else if (rule->tgt_str == CIL_KEY_OTHER) {
352 rule->tgt = db->othertype;
353 } else {
354 rc = cil_resolve_name(current, rule->tgt_str, CIL_SYM_TYPES, db, &tgt_datum);
355 if (rc != SEPOL_OK) {
356 goto exit;
357 }
358 rule->tgt = tgt_datum;
359 }
360
361 rc = cil_resolve_classperms_list(current, rule->classperms, db);
362 if (rc != SEPOL_OK) {
363 goto exit;
364 }
365
366 return SEPOL_OK;
367
368 exit:
369 return rc;
370 }
371
372 int cil_resolve_type_rule(struct cil_tree_node *current, struct cil_db *db)
373 {
374 struct cil_type_rule *rule = current->data;
375 struct cil_symtab_datum *src_datum = NULL;
376 struct cil_symtab_datum *tgt_datum = NULL;
377 struct cil_symtab_datum *obj_datum = NULL;
378 struct cil_symtab_datum *result_datum = NULL;
379 struct cil_tree_node *result_node = NULL;
380 int rc = SEPOL_ERR;
381
382 rc = cil_resolve_name(current, rule->src_str, CIL_SYM_TYPES, db, &src_datum);
383 if (rc != SEPOL_OK) {
384 goto exit;
385 }
386 rule->src = src_datum;
387
388 if (rule->tgt_str == CIL_KEY_SELF) {
389 rule->tgt = db->selftype;
390 } else {
391 rc = cil_resolve_name(current, rule->tgt_str, CIL_SYM_TYPES, db, &tgt_datum);
392 if (rc != SEPOL_OK) {
393 goto exit;
394 }
395 rule->tgt = tgt_datum;
396 }
397
398 rc = cil_resolve_name(current, rule->obj_str, CIL_SYM_CLASSES, db, &obj_datum);
399 if (rc != SEPOL_OK) {
400 goto exit;
401 }
402 rule->obj = (struct cil_class*)obj_datum;
403
404 rc = cil_resolve_name(current, rule->result_str, CIL_SYM_TYPES, db, &result_datum);
405 if (rc != SEPOL_OK) {
406 goto exit;
407 }
408
409 result_node = NODE(result_datum);
410
411 if (result_node->flavor != CIL_TYPE) {
412 cil_log(CIL_ERR, "Type rule result must be a type [%d]\n",result_node->flavor);
413 rc = SEPOL_ERR;
414 goto exit;
415 }
416 rule->result = result_datum;
417
418 return SEPOL_OK;
419
420 exit:
421 return rc;
422 }
423
424 int cil_resolve_typeattributeset(struct cil_tree_node *current, struct cil_db *db)
425 {
426 struct cil_typeattributeset *attrtypes = current->data;
427 struct cil_symtab_datum *attr_datum = NULL;
428 struct cil_tree_node *attr_node = NULL;
429 struct cil_typeattribute *attr = NULL;
430 int rc = SEPOL_ERR;
431
432 rc = cil_resolve_name(current, attrtypes->attr_str, CIL_SYM_TYPES, db, &attr_datum);
433 if (rc != SEPOL_OK) {
434 goto exit;
435 }
436
437 attr_node = NODE(attr_datum);
438
439 if (attr_node->flavor != CIL_TYPEATTRIBUTE) {
440 rc = SEPOL_ERR;
441 cil_log(CIL_ERR, "Attribute type not an attribute\n");
442 goto exit;
443 }
444
445 attr = (struct cil_typeattribute*)attr_datum;
446
447 rc = cil_resolve_expr(CIL_TYPEATTRIBUTESET, attrtypes->str_expr, &attrtypes->datum_expr, current, db);
448 if (rc != SEPOL_OK) {
449 goto exit;
450 }
451
452 if (attr->expr_list == NULL) {
453 cil_list_init(&attr->expr_list, CIL_TYPEATTRIBUTE);
454 }
455
456 cil_list_append(attr->expr_list, CIL_LIST, attrtypes->datum_expr);
457
458 return SEPOL_OK;
459
460 exit:
461 return rc;
462 }
463
464 static int cil_resolve_expandtypeattribute(struct cil_tree_node *current, struct cil_db *db)
465 {
466 struct cil_expandtypeattribute *expandattr = current->data;
467 struct cil_symtab_datum *attr_datum = NULL;
468 struct cil_tree_node *attr_node = NULL;
469 struct cil_list_item *curr;
470 int used;
471 int rc = SEPOL_ERR;
472
473 cil_list_init(&expandattr->attr_datums, CIL_TYPE);
474
475 cil_list_for_each(curr, expandattr->attr_strs) {
476 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_TYPES, db, &attr_datum);
477 if (rc != SEPOL_OK) {
478 goto exit;
479 }
480
481 attr_node = NODE(attr_datum);
482
483 if (attr_node->flavor != CIL_TYPEATTRIBUTE) {
484 rc = SEPOL_ERR;
485 cil_log(CIL_ERR, "Attribute type not an attribute\n");
486 goto exit;
487 }
488 used = expandattr->expand ? CIL_ATTR_EXPAND_TRUE : CIL_ATTR_EXPAND_FALSE;
489 cil_type_used(attr_datum, used);
490 cil_list_append(expandattr->attr_datums, CIL_TYPE, attr_datum);
491 }
492
493 return SEPOL_OK;
494 exit:
495 return rc;
496 }
497
498 static int cil_resolve_aliasactual(struct cil_tree_node *current, struct cil_db *db, enum cil_flavor flavor, enum cil_flavor alias_flavor)
499 {
500 int rc = SEPOL_ERR;
501 enum cil_sym_index sym_index;
502 struct cil_aliasactual *aliasactual = current->data;
503 struct cil_symtab_datum *alias_datum = NULL;
504 struct cil_symtab_datum *actual_datum = NULL;
505 struct cil_alias *alias;
506
507 rc = cil_flavor_to_symtab_index(flavor, &sym_index);
508 if (rc != SEPOL_OK) {
509 goto exit;
510 }
511
512 rc = cil_resolve_name_keep_aliases(current, aliasactual->alias_str, sym_index, db, &alias_datum);
513 if (rc != SEPOL_OK) {
514 goto exit;
515 }
516 if (FLAVOR(alias_datum) != alias_flavor) {
517 cil_log(CIL_ERR, "%s is not an alias\n",alias_datum->name);
518 rc = SEPOL_ERR;
519 goto exit;
520 }
521
522 rc = cil_resolve_name(current, aliasactual->actual_str, sym_index, db, &actual_datum);
523 if (rc != SEPOL_OK) {
524 goto exit;
525 }
526
527 if (FLAVOR(actual_datum) != flavor && FLAVOR(actual_datum) != alias_flavor) {
528 cil_log(CIL_ERR, "%s is a %s, but aliases a %s\n", alias_datum->name, cil_node_to_string(NODE(alias_datum)), cil_node_to_string(NODE(actual_datum)));
529 rc = SEPOL_ERR;
530 goto exit;
531 }
532
533 alias = (struct cil_alias *)alias_datum;
534 aliasactual->alias = alias_datum;
535
536 if (alias->actual != NULL) {
537 cil_log(CIL_ERR, "%s %s cannot bind more than one value\n", cil_node_to_string(NODE(alias_datum)), alias_datum->name);
538 rc = SEPOL_ERR;
539 goto exit;
540 }
541
542 alias->actual = actual_datum;
543 aliasactual->actual = actual_datum;
544
545 return SEPOL_OK;
546
547 exit:
548 return rc;
549 }
550
551 static int cil_resolve_alias_to_actual(struct cil_tree_node *current, enum cil_flavor flavor)
552 {
553 struct cil_alias *alias = current->data;
554 struct cil_alias *a1 = current->data;
555 struct cil_alias *a2 = current->data;
556 struct cil_tree_node *a1_node = NULL;
557 int steps = 0;
558 int limit = 2;
559
560 if (alias->actual == NULL) {
561 cil_tree_log(current, CIL_ERR, "Alias declared but not used");
562 return SEPOL_ERR;
563 }
564
565 a1_node = a1->datum.nodes->head->data;
566
567 while (flavor != a1_node->flavor) {
568 if (a1->actual == NULL) {
569 cil_tree_log(current, CIL_ERR, "Alias %s references an unused alias %s", alias->datum.name, a1->datum.name);
570 return SEPOL_ERR;
571 }
572 a1 = a1->actual;
573 a1_node = a1->datum.nodes->head->data;
574 steps += 1;
575
576 if (a1 == a2) {
577 cil_log(CIL_ERR, "Circular alias found: %s ", a1->datum.name);
578 a1 = a1->actual;
579 while (a1 != a2) {
580 cil_log(CIL_ERR, "%s ", a1->datum.name);
581 a1 = a1->actual;
582 }
583 cil_log(CIL_ERR,"\n");
584 return SEPOL_ERR;
585 }
586
587 if (steps == limit) {
588 steps = 0;
589 limit *= 2;
590 a2 = a1;
591 }
592 }
593
594 alias->actual = a1;
595
596 return SEPOL_OK;
597 }
598
599 int cil_resolve_typepermissive(struct cil_tree_node *current, struct cil_db *db)
600 {
601 struct cil_typepermissive *typeperm = current->data;
602 struct cil_symtab_datum *type_datum = NULL;
603 struct cil_tree_node *type_node = NULL;
604 int rc = SEPOL_ERR;
605
606 rc = cil_resolve_name(current, typeperm->type_str, CIL_SYM_TYPES, db, &type_datum);
607 if (rc != SEPOL_OK) {
608 goto exit;
609 }
610
611 type_node = NODE(type_datum);
612
613 if (type_node->flavor != CIL_TYPE && type_node->flavor != CIL_TYPEALIAS) {
614 cil_log(CIL_ERR, "Typepermissive must be a type or type alias\n");
615 rc = SEPOL_ERR;
616 goto exit;
617 }
618
619 typeperm->type = type_datum;
620
621 return SEPOL_OK;
622
623 exit:
624 return rc;
625 }
626
627 int cil_resolve_typeneveraudit(struct cil_tree_node *current, struct cil_db *db)
628 {
629 struct cil_typeneveraudit *typeperm = current->data;
630 struct cil_symtab_datum *type_datum = NULL;
631 struct cil_tree_node *type_node = NULL;
632 int rc = SEPOL_ERR;
633
634 rc = cil_resolve_name(current, typeperm->type_str, CIL_SYM_TYPES, db, &type_datum);
635 if (rc != SEPOL_OK) {
636 goto exit;
637 }
638
639 type_node = NODE(type_datum);
640
641 if (type_node->flavor != CIL_TYPE && type_node->flavor != CIL_TYPEALIAS) {
642 cil_log(CIL_ERR, "Typeneveraudit must be a type or type alias\n");
643 rc = SEPOL_ERR;
644 goto exit;
645 }
646
647 typeperm->type = type_datum;
648
649 return SEPOL_OK;
650
651 exit:
652 return rc;
653 }
654
655 int cil_resolve_nametypetransition(struct cil_tree_node *current, struct cil_db *db)
656 {
657 struct cil_nametypetransition *nametypetrans = current->data;
658 struct cil_symtab_datum *src_datum = NULL;
659 struct cil_symtab_datum *tgt_datum = NULL;
660 struct cil_symtab_datum *obj_datum = NULL;
661 struct cil_symtab_datum *name_datum = NULL;
662 struct cil_symtab_datum *result_datum = NULL;
663 struct cil_tree_node *result_node = NULL;
664 int rc = SEPOL_ERR;
665
666 rc = cil_resolve_name(current, nametypetrans->src_str, CIL_SYM_TYPES, db, &src_datum);
667 if (rc != SEPOL_OK) {
668 goto exit;
669 }
670 nametypetrans->src = src_datum;
671
672 if (nametypetrans->tgt_str == CIL_KEY_SELF) {
673 nametypetrans->tgt = db->selftype;
674 } else {
675 rc = cil_resolve_name(current, nametypetrans->tgt_str, CIL_SYM_TYPES, db, &tgt_datum);
676 if (rc != SEPOL_OK) {
677 goto exit;
678 }
679 nametypetrans->tgt = tgt_datum;
680 }
681
682 rc = cil_resolve_name(current, nametypetrans->obj_str, CIL_SYM_CLASSES, db, &obj_datum);
683 if (rc != SEPOL_OK) {
684 goto exit;
685 }
686 nametypetrans->obj = (struct cil_class*)obj_datum;
687
688 if (!nametypetrans->name) {
689 rc = cil_resolve_name(current, nametypetrans->name_str, CIL_SYM_STRINGS, db, &name_datum);
690 if (rc != SEPOL_OK) {
691 goto exit;
692 }
693 nametypetrans->name = name_datum;
694 }
695
696 rc = cil_resolve_name(current, nametypetrans->result_str, CIL_SYM_TYPES, db, &result_datum);
697 if (rc != SEPOL_OK) {
698 goto exit;
699 }
700
701 result_node = NODE(result_datum);
702
703 if (result_node->flavor != CIL_TYPE && result_node->flavor != CIL_TYPEALIAS) {
704 cil_log(CIL_ERR, "typetransition result is not a type or type alias\n");
705 rc = SEPOL_ERR;
706 goto exit;
707 }
708 nametypetrans->result = result_datum;
709
710 return SEPOL_OK;
711
712 exit:
713 return rc;
714 }
715
716 int cil_resolve_rangetransition(struct cil_tree_node *current, struct cil_db *db)
717 {
718 struct cil_rangetransition *rangetrans = current->data;
719 struct cil_symtab_datum *src_datum = NULL;
720 struct cil_symtab_datum *exec_datum = NULL;
721 struct cil_symtab_datum *obj_datum = NULL;
722 struct cil_symtab_datum *range_datum = NULL;
723 int rc = SEPOL_ERR;
724
725 rc = cil_resolve_name(current, rangetrans->src_str, CIL_SYM_TYPES, db, &src_datum);
726 if (rc != SEPOL_OK) {
727 goto exit;
728 }
729 rangetrans->src = src_datum;
730
731 rc = cil_resolve_name(current, rangetrans->exec_str, CIL_SYM_TYPES, db, &exec_datum);
732 if (rc != SEPOL_OK) {
733 goto exit;
734 }
735 rangetrans->exec = exec_datum;
736
737 rc = cil_resolve_name(current, rangetrans->obj_str, CIL_SYM_CLASSES, db, &obj_datum);
738 if (rc != SEPOL_OK) {
739 goto exit;
740 }
741 rangetrans->obj = (struct cil_class*)obj_datum;
742
743 if (rangetrans->range_str != NULL) {
744 rc = cil_resolve_name(current, rangetrans->range_str, CIL_SYM_LEVELRANGES, db, &range_datum);
745 if (rc != SEPOL_OK) {
746 goto exit;
747 }
748 rangetrans->range = (struct cil_levelrange*)range_datum;
749
750 /* This could still be an anonymous levelrange even if range_str is set, if range_str is a param_str*/
751 if (rangetrans->range->datum.name == NULL) {
752 rc = cil_resolve_levelrange(current, rangetrans->range, db);
753 if (rc != SEPOL_OK) {
754 goto exit;
755 }
756 }
757 } else {
758 rc = cil_resolve_levelrange(current, rangetrans->range, db);
759 if (rc != SEPOL_OK) {
760 goto exit;
761 }
762 }
763
764 return SEPOL_OK;
765
766 exit:
767 return rc;
768 }
769
770 static int __class_update_perm_values(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
771 {
772 struct cil_perm *perm = (struct cil_perm *)d;
773
774 perm->value += *((int *)args);
775
776 return SEPOL_OK;
777 }
778
779 int cil_resolve_classcommon(struct cil_tree_node *current, struct cil_db *db)
780 {
781 struct cil_class *class = NULL;
782 struct cil_class *common = NULL;
783 struct cil_classcommon *clscom = current->data;
784 struct cil_symtab_datum *class_datum = NULL;
785 struct cil_symtab_datum *common_datum = NULL;
786 int rc = SEPOL_ERR;
787
788 rc = cil_resolve_name(current, clscom->class_str, CIL_SYM_CLASSES, db, &class_datum);
789 if (rc != SEPOL_OK) {
790 goto exit;
791 }
792 if (NODE(class_datum)->flavor != CIL_CLASS) {
793 cil_log(CIL_ERR, "Class %s is not a kernel class and cannot be associated with common %s\n", clscom->class_str, clscom->common_str);
794 rc = SEPOL_ERR;
795 goto exit;
796 }
797
798 rc = cil_resolve_name(current, clscom->common_str, CIL_SYM_COMMONS, db, &common_datum);
799 if (rc != SEPOL_OK) {
800 goto exit;
801 }
802
803 class = (struct cil_class *)class_datum;
804 common = (struct cil_class *)common_datum;
805 if (class->common != NULL) {
806 cil_log(CIL_ERR, "class cannot be associated with more than one common\n");
807 rc = SEPOL_ERR;
808 goto exit;
809 }
810
811 class->common = common;
812
813 clscom->class = class;
814 clscom->common = common;
815
816 cil_symtab_map(&class->perms, __class_update_perm_values, &common->num_perms);
817
818 class->num_perms += common->num_perms;
819 if (class->num_perms > CIL_PERMS_PER_CLASS) {
820 cil_tree_log(current, CIL_ERR, "Too many permissions in class '%s' when including common permissions", class->datum.name);
821 rc = SEPOL_ERR;
822 goto exit;
823 }
824
825 return SEPOL_OK;
826
827 exit:
828 return rc;
829 }
830
831 int cil_resolve_classmapping(struct cil_tree_node *current, struct cil_db *db)
832 {
833 int rc = SEPOL_ERR;
834 struct cil_classmapping *mapping = current->data;
835 struct cil_class *map = NULL;
836 struct cil_perm *mp = NULL;
837 struct cil_symtab_datum *datum = NULL;
838 struct cil_list_item *curr;
839
840 rc = cil_resolve_name(current, mapping->map_class_str, CIL_SYM_CLASSES, db, &datum);
841 if (rc != SEPOL_OK) {
842 goto exit;
843 }
844 map = (struct cil_class*)datum;
845 mapping->map_class = map;
846
847 rc = cil_symtab_get_datum(&map->perms, mapping->map_perm_str, &datum);
848 if (rc != SEPOL_OK) {
849 goto exit;
850 }
851
852 mp = (struct cil_perm*)datum;
853 mapping->map_perm = mp;
854
855 rc = cil_resolve_classperms_list(current, mapping->classperms, db);
856 if (rc != SEPOL_OK) {
857 goto exit;
858 }
859
860 if (mp->classperms == NULL) {
861 cil_list_init(&mp->classperms, CIL_CLASSPERMS);
862 }
863
864 cil_list_for_each(curr, mapping->classperms) {
865 cil_list_append(mp->classperms, curr->flavor, curr->data);
866 }
867
868 return SEPOL_OK;
869
870 exit:
871 return rc;
872 }
873
874 int cil_resolve_userrole(struct cil_tree_node *current, struct cil_db *db)
875 {
876 struct cil_userrole *userrole = current->data;
877 struct cil_symtab_datum *user_datum = NULL;
878 struct cil_symtab_datum *role_datum = NULL;
879 int rc = SEPOL_ERR;
880
881 rc = cil_resolve_name(current, userrole->user_str, CIL_SYM_USERS, db, &user_datum);
882 if (rc != SEPOL_OK) {
883 goto exit;
884 }
885 userrole->user = (struct cil_user*)user_datum;
886
887 rc = cil_resolve_name(current, userrole->role_str, CIL_SYM_ROLES, db, &role_datum);
888 if (rc != SEPOL_OK) {
889 goto exit;
890 }
891 userrole->role = role_datum;
892
893 return SEPOL_OK;
894
895 exit:
896 return rc;
897 }
898
899 int cil_resolve_userlevel(struct cil_tree_node *current, struct cil_db *db)
900 {
901 struct cil_userlevel *usrlvl = current->data;
902 struct cil_symtab_datum *user_datum = NULL;
903 struct cil_symtab_datum *lvl_datum = NULL;
904 struct cil_user *user = NULL;
905 struct cil_tree_node *user_node = NULL;
906 int rc = SEPOL_ERR;
907
908 rc = cil_resolve_name(current, usrlvl->user_str, CIL_SYM_USERS, db, &user_datum);
909 if (rc != SEPOL_OK) {
910 goto exit;
911 }
912
913 user_node = NODE(user_datum);
914
915 if (user_node->flavor != CIL_USER) {
916 cil_log(CIL_ERR, "Userlevel must be a user\n");
917 rc = SEPOL_ERR;
918 goto exit;
919 }
920
921 user = (struct cil_user*)user_datum;
922 usrlvl->user = user;
923
924 if (usrlvl->level_str != NULL) {
925 rc = cil_resolve_name(current, usrlvl->level_str, CIL_SYM_LEVELS, db, &lvl_datum);
926 if (rc != SEPOL_OK) {
927 goto exit;
928 }
929 usrlvl->level = (struct cil_level*)lvl_datum;
930 user->dftlevel = usrlvl->level;
931
932 /* This could still be an anonymous level even if level_str is set, if level_str is a param_str*/
933 if (user->dftlevel->datum.name == NULL) {
934 rc = cil_resolve_level(current, user->dftlevel, db);
935 if (rc != SEPOL_OK) {
936 goto exit;
937 }
938 }
939 } else if (usrlvl->level != NULL) {
940 rc = cil_resolve_level(current, usrlvl->level, db);
941 if (rc != SEPOL_OK) {
942 goto exit;
943 }
944 user->dftlevel = usrlvl->level;
945 }
946
947 return SEPOL_OK;
948
949 exit:
950 return rc;
951 }
952
953 int cil_resolve_userrange(struct cil_tree_node *current, struct cil_db *db)
954 {
955 struct cil_userrange *userrange = current->data;
956 struct cil_symtab_datum *user_datum = NULL;
957 struct cil_symtab_datum *range_datum = NULL;
958 struct cil_user *user = NULL;
959 struct cil_tree_node *user_node = NULL;
960 int rc = SEPOL_ERR;
961
962 rc = cil_resolve_name(current, userrange->user_str, CIL_SYM_USERS, db, &user_datum);
963 if (rc != SEPOL_OK) {
964 goto exit;
965 }
966
967 user_node = NODE(user_datum);
968
969 if (user_node->flavor != CIL_USER) {
970 cil_log(CIL_ERR, "Userrange must be a user: %s\n", user_datum->fqn);
971 rc = SEPOL_ERR;
972 goto exit;
973 }
974
975 user = (struct cil_user*)user_datum;
976 userrange->user = user;
977
978 if (userrange->range_str != NULL) {
979 rc = cil_resolve_name(current, userrange->range_str, CIL_SYM_LEVELRANGES, db, &range_datum);
980 if (rc != SEPOL_OK) {
981 goto exit;
982 }
983 userrange->range = (struct cil_levelrange*)range_datum;
984 user->range = userrange->range;
985
986 /* This could still be an anonymous levelrange even if levelrange_str is set, if levelrange_str is a param_str*/
987 if (user->range->datum.name == NULL) {
988 rc = cil_resolve_levelrange(current, user->range, db);
989 if (rc != SEPOL_OK) {
990 goto exit;
991 }
992 }
993 } else if (userrange->range != NULL) {
994 rc = cil_resolve_levelrange(current, userrange->range, db);
995 if (rc != SEPOL_OK) {
996 goto exit;
997 }
998 user->range = userrange->range;
999 }
1000
1001 return SEPOL_OK;
1002
1003 exit:
1004 return rc;
1005 }
1006
1007 int cil_resolve_userprefix(struct cil_tree_node *current, struct cil_db *db)
1008 {
1009 struct cil_userprefix *userprefix = current->data;
1010 struct cil_symtab_datum *user_datum = NULL;
1011 struct cil_tree_node *user_node = NULL;
1012 int rc = SEPOL_ERR;
1013
1014 rc = cil_resolve_name(current, userprefix->user_str, CIL_SYM_USERS, db, &user_datum);
1015 if (rc != SEPOL_OK) {
1016 goto exit;
1017 }
1018
1019 user_node = NODE(user_datum);
1020
1021 if (user_node->flavor != CIL_USER) {
1022 cil_log(CIL_ERR, "Userprefix must be a user: %s\n", user_datum->fqn);
1023 rc = SEPOL_ERR;
1024 goto exit;
1025 }
1026
1027 userprefix->user = (struct cil_user*)user_datum;
1028
1029 exit:
1030 return rc;
1031 }
1032
1033 int cil_resolve_selinuxuser(struct cil_tree_node *current, struct cil_db *db)
1034 {
1035 struct cil_selinuxuser *selinuxuser = current->data;
1036 struct cil_symtab_datum *user_datum = NULL;
1037 struct cil_symtab_datum *lvlrange_datum = NULL;
1038 struct cil_tree_node *user_node = NULL;
1039 int rc = SEPOL_ERR;
1040
1041 rc = cil_resolve_name(current, selinuxuser->user_str, CIL_SYM_USERS, db, &user_datum);
1042 if (rc != SEPOL_OK) {
1043 goto exit;
1044 }
1045
1046 user_node = NODE(user_datum);
1047
1048 if (user_node->flavor != CIL_USER) {
1049 cil_log(CIL_ERR, "Selinuxuser must be a user: %s\n", user_datum->fqn);
1050 rc = SEPOL_ERR;
1051 goto exit;
1052 }
1053
1054 selinuxuser->user = (struct cil_user*)user_datum;
1055
1056 if (selinuxuser->range_str != NULL) {
1057 rc = cil_resolve_name(current, selinuxuser->range_str, CIL_SYM_LEVELRANGES, db, &lvlrange_datum);
1058 if (rc != SEPOL_OK) {
1059 goto exit;
1060 }
1061 selinuxuser->range = (struct cil_levelrange*)lvlrange_datum;
1062
1063 /* This could still be an anonymous levelrange even if range_str is set, if range_str is a param_str*/
1064 if (selinuxuser->range->datum.name == NULL) {
1065 rc = cil_resolve_levelrange(current, selinuxuser->range, db);
1066 if (rc != SEPOL_OK) {
1067 goto exit;
1068 }
1069 }
1070 } else if (selinuxuser->range != NULL) {
1071 rc = cil_resolve_levelrange(current, selinuxuser->range, db);
1072 if (rc != SEPOL_OK) {
1073 goto exit;
1074 }
1075 }
1076
1077 rc = SEPOL_OK;
1078 exit:
1079 return rc;
1080 }
1081
1082 int cil_resolve_roletype(struct cil_tree_node *current, struct cil_db *db)
1083 {
1084 struct cil_roletype *roletype = current->data;
1085 struct cil_symtab_datum *role_datum = NULL;
1086 struct cil_symtab_datum *type_datum = NULL;
1087 int rc = SEPOL_ERR;
1088
1089 rc = cil_resolve_name(current, roletype->role_str, CIL_SYM_ROLES, db, &role_datum);
1090 if (rc != SEPOL_OK) {
1091 goto exit;
1092 }
1093 roletype->role = (struct cil_role*)role_datum;
1094
1095 rc = cil_resolve_name(current, roletype->type_str, CIL_SYM_TYPES, db, &type_datum);
1096 if (rc != SEPOL_OK) {
1097 goto exit;
1098 }
1099 roletype->type = (struct cil_type*)type_datum;
1100
1101 return SEPOL_OK;
1102
1103 exit:
1104 return rc;
1105 }
1106
1107 int cil_resolve_roletransition(struct cil_tree_node *current, struct cil_db *db)
1108 {
1109 struct cil_roletransition *roletrans = current->data;
1110 struct cil_symtab_datum *src_datum = NULL;
1111 struct cil_symtab_datum *tgt_datum = NULL;
1112 struct cil_symtab_datum *obj_datum = NULL;
1113 struct cil_symtab_datum *result_datum = NULL;
1114 struct cil_tree_node *node = NULL;
1115 int rc = SEPOL_ERR;
1116
1117 rc = cil_resolve_name(current, roletrans->src_str, CIL_SYM_ROLES, db, &src_datum);
1118 if (rc != SEPOL_OK) {
1119 goto exit;
1120 }
1121 roletrans->src = (struct cil_role*)src_datum;
1122
1123 rc = cil_resolve_name(current, roletrans->tgt_str, CIL_SYM_TYPES, db, &tgt_datum);
1124 if (rc != SEPOL_OK) {
1125 goto exit;
1126 }
1127 roletrans->tgt = tgt_datum;
1128
1129 rc = cil_resolve_name(current, roletrans->obj_str, CIL_SYM_CLASSES, db, &obj_datum);
1130 if (rc != SEPOL_OK) {
1131 goto exit;
1132 }
1133 roletrans->obj = (struct cil_class*)obj_datum;
1134
1135 rc = cil_resolve_name(current, roletrans->result_str, CIL_SYM_ROLES, db, &result_datum);
1136 if (rc != SEPOL_OK) {
1137 goto exit;
1138 }
1139 node = NODE(result_datum);
1140 if (node->flavor != CIL_ROLE) {
1141 rc = SEPOL_ERR;
1142 cil_log(CIL_ERR, "roletransition must result in a role, but %s is a %s\n", roletrans->result_str, cil_node_to_string(node));
1143 goto exit;
1144 }
1145 roletrans->result = (struct cil_role*)result_datum;
1146
1147 return SEPOL_OK;
1148
1149 exit:
1150 return rc;
1151 }
1152
1153 int cil_resolve_roleallow(struct cil_tree_node *current, struct cil_db *db)
1154 {
1155 struct cil_roleallow *roleallow = current->data;
1156 struct cil_symtab_datum *src_datum = NULL;
1157 struct cil_symtab_datum *tgt_datum = NULL;
1158 int rc = SEPOL_ERR;
1159
1160 rc = cil_resolve_name(current, roleallow->src_str, CIL_SYM_ROLES, db, &src_datum);
1161 if (rc != SEPOL_OK) {
1162 goto exit;
1163 }
1164 roleallow->src = (struct cil_role*)src_datum;
1165
1166 rc = cil_resolve_name(current, roleallow->tgt_str, CIL_SYM_ROLES, db, &tgt_datum);
1167 if (rc != SEPOL_OK) {
1168 goto exit;
1169 }
1170 roleallow->tgt = (struct cil_role*)tgt_datum;
1171
1172 return SEPOL_OK;
1173
1174 exit:
1175 return rc;
1176 }
1177
1178 int cil_resolve_roleattributeset(struct cil_tree_node *current, struct cil_db *db)
1179 {
1180 int rc = SEPOL_ERR;
1181 struct cil_roleattributeset *attrroles = current->data;
1182 struct cil_symtab_datum *attr_datum = NULL;
1183 struct cil_tree_node *attr_node = NULL;
1184 struct cil_roleattribute *attr = NULL;
1185
1186 rc = cil_resolve_name(current, attrroles->attr_str, CIL_SYM_ROLES, db, &attr_datum);
1187 if (rc != SEPOL_OK) {
1188 goto exit;
1189 }
1190 attr_node = NODE(attr_datum);
1191
1192 if (attr_node->flavor != CIL_ROLEATTRIBUTE) {
1193 rc = SEPOL_ERR;
1194 cil_log(CIL_ERR, "Attribute role not an attribute\n");
1195 goto exit;
1196 }
1197 attr = (struct cil_roleattribute*)attr_datum;
1198
1199 rc = cil_resolve_expr(CIL_ROLEATTRIBUTESET, attrroles->str_expr, &attrroles->datum_expr, current, db);
1200 if (rc != SEPOL_OK) {
1201 goto exit;
1202 }
1203
1204 if (attr->expr_list == NULL) {
1205 cil_list_init(&attr->expr_list, CIL_ROLEATTRIBUTE);
1206 }
1207
1208 cil_list_append(attr->expr_list, CIL_LIST, attrroles->datum_expr);
1209
1210 return SEPOL_OK;
1211
1212 exit:
1213 return rc;
1214 }
1215
1216 static struct cil_list_item *__cil_ordered_item_insert(struct cil_list *old, struct cil_list_item *curr, struct cil_list_item *item)
1217 {
1218 if (item->flavor == CIL_SID) {
1219 struct cil_sid *sid = item->data;
1220 if (sid->ordered == CIL_TRUE) {
1221 cil_log(CIL_ERR, "SID %s has already been merged into the ordered list\n", sid->datum.name);
1222 return NULL;
1223 }
1224 sid->ordered = CIL_TRUE;
1225 } else if (item->flavor == CIL_CLASS) {
1226 struct cil_class *class = item->data;
1227 if (class->ordered == CIL_TRUE) {
1228 cil_log(CIL_ERR, "Class %s has already been merged into the ordered list\n", class->datum.name);
1229 return NULL;
1230 }
1231 class->ordered = CIL_TRUE;
1232 } else if (item->flavor == CIL_CAT) {
1233 struct cil_cat *cat = item->data;
1234 if (cat->ordered == CIL_TRUE) {
1235 cil_log(CIL_ERR, "Category %s has already been merged into the ordered list\n", cat->datum.name);
1236 return NULL;
1237 }
1238 cat->ordered = CIL_TRUE;
1239 } else if (item->flavor == CIL_SENS) {
1240 struct cil_sens *sens = item->data;
1241 if (sens->ordered == CIL_TRUE) {
1242 cil_log(CIL_ERR, "Sensitivity %s has already been merged into the ordered list\n", sens->datum.name);
1243 return NULL;
1244 }
1245 sens->ordered = CIL_TRUE;
1246 }
1247
1248 return cil_list_insert(old, curr, item->flavor, item->data);
1249 }
1250
1251 static int __cil_ordered_list_insert(struct cil_list *old, struct cil_list_item *ocurr, struct cil_list_item *nstart, struct cil_list_item *nstop)
1252 {
1253 struct cil_list_item *ncurr = NULL;
1254
1255 for (ncurr = nstart; ncurr != nstop; ncurr = ncurr->next) {
1256 ocurr = __cil_ordered_item_insert(old, ocurr, ncurr);
1257 if (ocurr == NULL) {
1258 return SEPOL_ERR;
1259 }
1260 }
1261 return SEPOL_OK;
1262 }
1263
1264 static void __cil_ordered_find_next_match(struct cil_list_item **i, struct cil_list_item **j, struct cil_list_item **p)
1265 {
1266 struct cil_list_item *pstart = *p;
1267 struct cil_list_item *jstart = *j;
1268
1269 while (*i) {
1270 *p = pstart;
1271 *j = jstart;
1272 while (*j) {
1273 if ((*i)->data == (*j)->data) {
1274 return;
1275 }
1276 *p = *j;
1277 *j = (*j)->next;
1278 }
1279 *i = (*i)->next;
1280 }
1281 }
1282
1283 static int __cil_ordered_lists_merge(struct cil_list *old, struct cil_list *new)
1284 {
1285 struct cil_list_item *ofirst = old->head;
1286 struct cil_list_item *ocurr = NULL;
1287 struct cil_list_item *oprev = NULL;
1288 struct cil_list_item *nfirst = new->head;
1289 struct cil_list_item *ncurr = NULL;
1290 int rc = SEPOL_ERR;
1291
1292 if (nfirst == NULL) {
1293 return SEPOL_OK;
1294 }
1295
1296 if (ofirst == NULL) {
1297 /* First list added */
1298 return __cil_ordered_list_insert(old, NULL, nfirst, NULL);
1299 }
1300
1301 ncurr = nfirst;
1302 ocurr = ofirst;
1303 oprev = NULL;
1304 while (ncurr && ocurr) {
1305 __cil_ordered_find_next_match(&ncurr, &ocurr, &oprev);
1306 if (!ncurr || !ocurr) {
1307 break;
1308 }
1309 if (ncurr != nfirst) {
1310 rc = __cil_ordered_list_insert(old, oprev, nfirst, ncurr);
1311 if (rc != SEPOL_OK) {
1312 return rc;
1313 }
1314 }
1315 ncurr = ncurr->next;
1316 nfirst = ncurr;
1317 oprev = ocurr;
1318 ocurr = ocurr->next;
1319 }
1320
1321 if (!ncurr) {
1322 if (!nfirst) {
1323 /* Done */
1324 return SEPOL_OK;
1325 } else {
1326 /* Can't merge yet */
1327 return SEPOL_ERR;
1328 }
1329 }
1330
1331 if (ncurr && !ocurr) { /* some remaining */
1332 rc = __cil_ordered_list_insert(old, oprev, ncurr, NULL);
1333 if (rc != SEPOL_OK) {
1334 return rc;
1335 }
1336 }
1337
1338 return SEPOL_OK;
1339 }
1340
1341 static int insert_unordered(struct cil_list *merged, struct cil_list *unordered_list)
1342 {
1343 struct cil_tree_node *node;
1344 struct cil_ordered *unordered;
1345 struct cil_list_item *curr = NULL;
1346 struct cil_list_item *item = NULL;
1347 struct cil_list_item *ret = NULL;
1348 int rc = SEPOL_ERR;
1349
1350 cil_list_for_each(curr, unordered_list) {
1351 node = curr->data;
1352 unordered = node->data;
1353 cil_list_for_each(item, unordered->datums) {
1354 if (cil_list_contains(merged, item->data)) {
1355 /* item was declared in an ordered statement, which supersedes
1356 * all unordered statements */
1357 if (item->flavor == CIL_CLASS) {
1358 cil_log(CIL_WARN, "Ignoring '%s' as it has already been declared in classorder.\n", ((struct cil_class*)(item->data))->datum.name);
1359 }
1360 continue;
1361 }
1362
1363 ret = __cil_ordered_item_insert(merged, merged->tail, item);
1364 if (ret == NULL) {
1365 rc = SEPOL_ERR;
1366 goto exit;
1367 }
1368 }
1369 }
1370
1371 rc = SEPOL_OK;
1372
1373 exit:
1374 return rc;
1375 }
1376
1377 static struct cil_list *__cil_ordered_lists_merge_all(struct cil_list **ordered_lists, struct cil_list **unordered_lists)
1378 {
1379 struct cil_list *composite = NULL;
1380 struct cil_tree_node *node;
1381 struct cil_ordered *ordered;
1382 struct cil_list_item *curr = NULL;
1383 int changed = CIL_TRUE;
1384 int waiting = 1;
1385 int rc = SEPOL_ERR;
1386
1387 cil_list_init(&composite, (*ordered_lists)->flavor);
1388
1389 while (waiting && changed == CIL_TRUE) {
1390 changed = CIL_FALSE;
1391 waiting = 0;
1392 cil_list_for_each(curr, *ordered_lists) {
1393 node = curr->data;
1394 ordered = node->data;
1395 if (ordered->merged == CIL_FALSE) {
1396 rc = __cil_ordered_lists_merge(composite, ordered->datums);
1397 if (rc != SEPOL_OK) {
1398 /* Can't merge yet */
1399 waiting++;
1400 } else {
1401 ordered->merged = CIL_TRUE;
1402 changed = CIL_TRUE;
1403 }
1404 }
1405 }
1406 if (waiting > 0 && changed == CIL_FALSE) {
1407 cil_list_for_each(curr, *ordered_lists) {
1408 node = curr->data;
1409 ordered = node->data;
1410 if (ordered->merged == CIL_FALSE) {
1411 cil_tree_log(node, CIL_ERR, "Unable to merge ordered list");
1412 }
1413 }
1414 goto exit;
1415 }
1416 }
1417
1418 rc = cil_verify_completed_ordered_list(composite, *ordered_lists);
1419 if (rc != SEPOL_OK) {
1420 cil_log(CIL_ERR, "Unable to validate ordering\n");
1421 goto exit;
1422 }
1423
1424 if (unordered_lists != NULL) {
1425 rc = insert_unordered(composite, *unordered_lists);
1426 if (rc != SEPOL_OK) {
1427 goto exit;
1428 }
1429 }
1430
1431 return composite;
1432
1433 exit:
1434 cil_list_destroy(&composite, CIL_FALSE);
1435 return NULL;
1436 }
1437
1438 int cil_resolve_classorder(struct cil_tree_node *current, struct cil_db *db, struct cil_list *classorder_list, struct cil_list *unordered_classorder_list)
1439 {
1440 struct cil_ordered *ordered = current->data;
1441 struct cil_list_item *curr = NULL;
1442 struct cil_symtab_datum *datum = NULL;
1443 int rc = SEPOL_ERR;
1444 int unordered = CIL_FALSE;
1445
1446 cil_list_init(&ordered->datums, CIL_DATUM);
1447
1448 cil_list_for_each(curr, ordered->strs) {
1449 if (curr->data == CIL_KEY_UNORDERED) {
1450 unordered = CIL_TRUE;
1451 continue;
1452 }
1453
1454 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_CLASSES, db, &datum);
1455 if (rc != SEPOL_OK) {
1456 cil_log(CIL_ERR, "Failed to resolve class %s in classorder\n", (char *)curr->data);
1457 rc = SEPOL_ERR;
1458 goto exit;
1459 }
1460 if (FLAVOR(datum) != CIL_CLASS) {
1461 cil_log(CIL_ERR, "%s is not a class. Only classes are allowed in classorder statements\n", datum->name);
1462 rc = SEPOL_ERR;
1463 goto exit;
1464 }
1465 cil_list_append(ordered->datums, CIL_CLASS, datum);
1466 }
1467
1468 if (unordered) {
1469 cil_list_append(unordered_classorder_list, CIL_CLASSORDER, current);
1470 } else {
1471 cil_list_append(classorder_list, CIL_CLASSORDER, current);
1472 }
1473
1474 return SEPOL_OK;
1475
1476 exit:
1477 cil_list_destroy(&ordered->datums, CIL_FALSE);
1478 return rc;
1479 }
1480
1481 int cil_resolve_sidorder(struct cil_tree_node *current, struct cil_db *db, struct cil_list *sidorder_list)
1482 {
1483 struct cil_ordered *ordered = current->data;
1484 struct cil_list_item *curr = NULL;
1485 struct cil_symtab_datum *datum = NULL;
1486 int rc = SEPOL_ERR;
1487
1488 cil_list_init(&ordered->datums, CIL_DATUM);
1489
1490 cil_list_for_each(curr, ordered->strs) {
1491 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_SIDS, db, &datum);
1492 if (rc != SEPOL_OK) {
1493 cil_log(CIL_ERR, "Failed to resolve sid %s in sidorder\n", (char *)curr->data);
1494 goto exit;
1495 }
1496 if (FLAVOR(datum) != CIL_SID) {
1497 cil_log(CIL_ERR, "%s is not a sid. Only sids are allowed in sidorder statements\n", datum->name);
1498 rc = SEPOL_ERR;
1499 goto exit;
1500 }
1501
1502 cil_list_append(ordered->datums, CIL_SID, datum);
1503 }
1504
1505 cil_list_append(sidorder_list, CIL_SIDORDER, current);
1506
1507 return SEPOL_OK;
1508
1509 exit:
1510 cil_list_destroy(&ordered->datums, CIL_FALSE);
1511 return rc;
1512 }
1513
1514 static void cil_set_cat_values(struct cil_list *ordered_cats, struct cil_db *db)
1515 {
1516 struct cil_list_item *curr;
1517 int v = 0;
1518
1519 cil_list_for_each(curr, ordered_cats) {
1520 struct cil_cat *cat = curr->data;
1521 cat->value = v;
1522 v++;
1523 }
1524
1525 db->num_cats = v;
1526 }
1527
1528 int cil_resolve_catorder(struct cil_tree_node *current, struct cil_db *db, struct cil_list *catorder_list)
1529 {
1530 struct cil_ordered *ordered = current->data;
1531 struct cil_list_item *curr = NULL;
1532 struct cil_symtab_datum *datum;
1533 int rc = SEPOL_ERR;
1534
1535 cil_list_init(&ordered->datums, CIL_DATUM);
1536
1537 cil_list_for_each(curr, ordered->strs) {
1538 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_CATS, db, &datum);
1539 if (rc != SEPOL_OK) {
1540 cil_log(CIL_ERR, "Failed to resolve category %s in categoryorder\n", (char *)curr->data);
1541 goto exit;
1542 }
1543 if (FLAVOR(datum) != CIL_CAT) {
1544 cil_log(CIL_ERR, "%s is not a category. Only categories are allowed in categoryorder statements\n", datum->name);
1545 rc = SEPOL_ERR;
1546 goto exit;
1547 }
1548
1549 cil_list_append(ordered->datums, CIL_CAT, datum);
1550 }
1551
1552 cil_list_append(catorder_list, CIL_CATORDER, current);
1553
1554 return SEPOL_OK;
1555
1556 exit:
1557 cil_list_destroy(&ordered->datums, CIL_FALSE);
1558 return rc;
1559 }
1560
1561 int cil_resolve_sensitivityorder(struct cil_tree_node *current, struct cil_db *db, struct cil_list *sensitivityorder_list)
1562 {
1563 struct cil_ordered *ordered = current->data;
1564 struct cil_list_item *curr = NULL;
1565 struct cil_symtab_datum *datum = NULL;
1566 int rc = SEPOL_ERR;
1567
1568 cil_list_init(&ordered->datums, CIL_DATUM);
1569
1570 cil_list_for_each(curr, ordered->strs) {
1571 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_SENS, db, &datum);
1572 if (rc != SEPOL_OK) {
1573 cil_log(CIL_ERR, "Failed to resolve sensitivity %s in sensitivityorder\n", (char *)curr->data);
1574 goto exit;
1575 }
1576 if (FLAVOR(datum) != CIL_SENS) {
1577 cil_log(CIL_ERR, "%s is not a sensitivity. Only sensitivities are allowed in sensitivityorder statements\n", datum->name);
1578 rc = SEPOL_ERR;
1579 goto exit;
1580 }
1581 cil_list_append(ordered->datums, CIL_SENS, datum);
1582 }
1583
1584 cil_list_append(sensitivityorder_list, CIL_SENSITIVITYORDER, current);
1585
1586 return SEPOL_OK;
1587
1588 exit:
1589 cil_list_destroy(&ordered->datums, CIL_FALSE);
1590 return rc;
1591 }
1592
1593 static int cil_resolve_cats(struct cil_tree_node *current, struct cil_cats *cats, struct cil_db *db)
1594 {
1595 int rc = SEPOL_ERR;
1596
1597 rc = cil_resolve_expr(CIL_CATSET, cats->str_expr, &cats->datum_expr, current, db);
1598 if (rc != SEPOL_OK) {
1599 goto exit;
1600 }
1601
1602 return SEPOL_OK;
1603
1604 exit:
1605 return rc;
1606 }
1607
1608
1609 int cil_resolve_catset(struct cil_tree_node *current, struct cil_catset *catset, struct cil_db *db)
1610 {
1611 return cil_resolve_cats(current, catset->cats, db);
1612 }
1613
1614 int cil_resolve_senscat(struct cil_tree_node *current, struct cil_db *db)
1615 {
1616 int rc = SEPOL_ERR;
1617 struct cil_senscat *senscat = current->data;
1618 struct cil_symtab_datum *sens_datum;
1619 struct cil_sens *sens = NULL;
1620
1621 rc = cil_resolve_name(current, (char*)senscat->sens_str, CIL_SYM_SENS, db, &sens_datum);
1622 if (rc != SEPOL_OK) {
1623 cil_log(CIL_ERR, "Failed to find sensitivity\n");
1624 goto exit;
1625 }
1626
1627 rc = cil_resolve_cats(current, senscat->cats, db);
1628 if (rc != SEPOL_OK) {
1629 goto exit;
1630 }
1631
1632 sens = (struct cil_sens *)sens_datum;
1633
1634 if (sens->cats_list == NULL ) {
1635 cil_list_init(&sens->cats_list, CIL_CAT);
1636 }
1637
1638 cil_list_append(sens->cats_list, CIL_CAT, senscat->cats);
1639
1640 return SEPOL_OK;
1641
1642 exit:
1643 return rc;
1644 }
1645
1646 int cil_resolve_level(struct cil_tree_node *current, struct cil_level *level, struct cil_db *db)
1647 {
1648 struct cil_symtab_datum *sens_datum = NULL;
1649 int rc = SEPOL_ERR;
1650
1651 if (level->sens) {
1652 return SEPOL_OK;
1653 }
1654
1655 rc = cil_resolve_name(current, (char*)level->sens_str, CIL_SYM_SENS, db, &sens_datum);
1656 if (rc != SEPOL_OK) {
1657 cil_log(CIL_ERR, "Failed to find sensitivity\n");
1658 goto exit;
1659 }
1660
1661 level->sens = (struct cil_sens *)sens_datum;
1662
1663 if (level->cats != NULL) {
1664 rc = cil_resolve_cats(current, level->cats, db);
1665 if (rc != SEPOL_OK) {
1666 goto exit;
1667 }
1668 }
1669
1670 return SEPOL_OK;
1671
1672 exit:
1673 return rc;
1674 }
1675
1676 int cil_resolve_levelrange(struct cil_tree_node *current, struct cil_levelrange *lvlrange, struct cil_db *db)
1677 {
1678 struct cil_symtab_datum *low_datum = NULL;
1679 struct cil_symtab_datum *high_datum = NULL;
1680 int rc = SEPOL_ERR;
1681
1682 if (lvlrange->low_str != NULL) {
1683 rc = cil_resolve_name(current, lvlrange->low_str, CIL_SYM_LEVELS, db, &low_datum);
1684 if (rc != SEPOL_OK) {
1685 goto exit;
1686 }
1687 lvlrange->low = (struct cil_level*)low_datum;
1688
1689 /* This could still be an anonymous level even if low_str is set, if low_str is a param_str */
1690 if (lvlrange->low->datum.name == NULL) {
1691 rc = cil_resolve_level(current, lvlrange->low, db);
1692 if (rc != SEPOL_OK) {
1693 goto exit;
1694 }
1695 }
1696 } else if (lvlrange->low != NULL) {
1697 rc = cil_resolve_level(current, lvlrange->low, db);
1698 if (rc != SEPOL_OK) {
1699 goto exit;
1700 }
1701 }
1702
1703 if (lvlrange->high_str != NULL) {
1704 rc = cil_resolve_name(current, lvlrange->high_str, CIL_SYM_LEVELS, db, &high_datum);
1705 if (rc != SEPOL_OK) {
1706 goto exit;
1707 }
1708 lvlrange->high = (struct cil_level*)high_datum;
1709
1710 /* This could still be an anonymous level even if high_str is set, if high_str is a param_str */
1711 if (lvlrange->high->datum.name == NULL) {
1712 rc = cil_resolve_level(current, lvlrange->high, db);
1713 if (rc != SEPOL_OK) {
1714 goto exit;
1715 }
1716 }
1717 } else if (lvlrange->high != NULL) {
1718 rc = cil_resolve_level(current, lvlrange->high, db);
1719 if (rc != SEPOL_OK) {
1720 goto exit;
1721 }
1722 }
1723
1724 return SEPOL_OK;
1725
1726 exit:
1727 return rc;
1728 }
1729
1730 int cil_resolve_constrain(struct cil_tree_node *current, struct cil_db *db)
1731 {
1732 struct cil_constrain *cons = current->data;
1733 int rc = SEPOL_ERR;
1734
1735 rc = cil_resolve_classperms_list(current, cons->classperms, db);
1736 if (rc != SEPOL_OK) {
1737 goto exit;
1738 }
1739
1740 rc = cil_resolve_expr(CIL_CONSTRAIN, cons->str_expr, &cons->datum_expr, current, db);
1741 if (rc != SEPOL_OK) {
1742 goto exit;
1743 }
1744
1745 return SEPOL_OK;
1746
1747 exit:
1748 return rc;
1749 }
1750
1751 int cil_resolve_validatetrans(struct cil_tree_node *current, struct cil_db *db)
1752 {
1753 struct cil_validatetrans *validtrans = current->data;
1754 struct cil_symtab_datum *class_datum = NULL;
1755 int rc = SEPOL_ERR;
1756
1757 rc = cil_resolve_name(current, validtrans->class_str, CIL_SYM_CLASSES, db, &class_datum);
1758 if (rc != SEPOL_OK) {
1759 goto exit;
1760 }
1761 validtrans->class = (struct cil_class*)class_datum;
1762
1763 rc = cil_resolve_expr(CIL_VALIDATETRANS, validtrans->str_expr, &validtrans->datum_expr, current, db);
1764 if (rc != SEPOL_OK) {
1765 goto exit;
1766 }
1767
1768 return SEPOL_OK;
1769
1770 exit:
1771 return rc;
1772 }
1773
1774 int cil_resolve_context(struct cil_tree_node *current, struct cil_context *context, struct cil_db *db)
1775 {
1776 struct cil_symtab_datum *user_datum = NULL;
1777 struct cil_symtab_datum *role_datum = NULL;
1778 struct cil_symtab_datum *type_datum = NULL;
1779 struct cil_tree_node *node = NULL;
1780 struct cil_symtab_datum *lvlrange_datum = NULL;
1781
1782 int rc = SEPOL_ERR;
1783
1784 rc = cil_resolve_name(current, context->user_str, CIL_SYM_USERS, db, &user_datum);
1785 if (rc != SEPOL_OK) {
1786 goto exit;
1787 }
1788
1789 node = NODE(user_datum);
1790
1791 if (node->flavor != CIL_USER) {
1792 cil_log(CIL_ERR, "Context user must be a user: %s\n", user_datum->fqn);
1793 rc = SEPOL_ERR;
1794 goto exit;
1795 }
1796
1797 context->user = (struct cil_user*)user_datum;
1798
1799 rc = cil_resolve_name(current, context->role_str, CIL_SYM_ROLES, db, &role_datum);
1800 if (rc != SEPOL_OK) {
1801 goto exit;
1802 }
1803
1804 node = NODE(role_datum);
1805 if (node->flavor != CIL_ROLE) {
1806 rc = SEPOL_ERR;
1807 cil_log(CIL_ERR, "Context role not a role: %s\n", role_datum->fqn);
1808 goto exit;
1809 }
1810
1811 context->role = (struct cil_role*)role_datum;
1812
1813 rc = cil_resolve_name(current, context->type_str, CIL_SYM_TYPES, db, &type_datum);
1814 if (rc != SEPOL_OK) {
1815 goto exit;
1816 }
1817
1818 node = NODE(type_datum);
1819
1820 if (node->flavor != CIL_TYPE && node->flavor != CIL_TYPEALIAS) {
1821 rc = SEPOL_ERR;
1822 cil_log(CIL_ERR, "Type not a type or type alias\n");
1823 goto exit;
1824 }
1825 context->type = type_datum;
1826
1827 if (context->range_str != NULL) {
1828 rc = cil_resolve_name(current, context->range_str, CIL_SYM_LEVELRANGES, db, &lvlrange_datum);
1829 if (rc != SEPOL_OK) {
1830 goto exit;
1831 }
1832 context->range = (struct cil_levelrange*)lvlrange_datum;
1833
1834 /* This could still be an anonymous levelrange even if levelrange_str is set, if levelrange_str is a param_str*/
1835 if (context->range->datum.name == NULL) {
1836 rc = cil_resolve_levelrange(current, context->range, db);
1837 if (rc != SEPOL_OK) {
1838 goto exit;
1839 }
1840 }
1841 } else if (context->range != NULL) {
1842 rc = cil_resolve_levelrange(current, context->range, db);
1843 if (rc != SEPOL_OK) {
1844 goto exit;
1845 }
1846 }
1847
1848 return SEPOL_OK;
1849
1850 exit:
1851 return rc;
1852 }
1853
1854 int cil_resolve_filecon(struct cil_tree_node *current, struct cil_db *db)
1855 {
1856 struct cil_filecon *filecon = current->data;
1857 struct cil_symtab_datum *context_datum = NULL;
1858 struct cil_symtab_datum *path_datum = NULL;
1859 int rc = SEPOL_ERR;
1860
1861 if (!filecon->path) {
1862 rc = cil_resolve_name(current, filecon->path_str, CIL_SYM_STRINGS, db, &path_datum);
1863 if (rc != SEPOL_OK) {
1864 return rc;
1865 }
1866 filecon->path = path_datum;
1867 }
1868
1869 if (filecon->context_str != NULL) {
1870 rc = cil_resolve_name(current, filecon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
1871 if (rc != SEPOL_OK) {
1872 return rc;
1873 }
1874 filecon->context = (struct cil_context*)context_datum;
1875 } else if (filecon->context != NULL) {
1876 rc = cil_resolve_context(current, filecon->context, db);
1877 if (rc != SEPOL_OK) {
1878 return rc;
1879 }
1880 }
1881
1882 return SEPOL_OK;
1883 }
1884
1885 int cil_resolve_ibpkeycon(struct cil_tree_node *current, struct cil_db *db)
1886 {
1887 struct cil_ibpkeycon *ibpkeycon = current->data;
1888 struct cil_symtab_datum *context_datum = NULL;
1889 int rc = SEPOL_ERR;
1890
1891 if (ibpkeycon->context_str) {
1892 rc = cil_resolve_name(current, ibpkeycon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
1893 if (rc != SEPOL_OK)
1894 goto exit;
1895
1896 ibpkeycon->context = (struct cil_context *)context_datum;
1897 } else {
1898 rc = cil_resolve_context(current, ibpkeycon->context, db);
1899 if (rc != SEPOL_OK)
1900 goto exit;
1901 }
1902
1903 return SEPOL_OK;
1904
1905 exit:
1906 return rc;
1907 }
1908
1909 int cil_resolve_portcon(struct cil_tree_node *current, struct cil_db *db)
1910 {
1911 struct cil_portcon *portcon = current->data;
1912 struct cil_symtab_datum *context_datum = NULL;
1913 int rc = SEPOL_ERR;
1914
1915 if (portcon->context_str != NULL) {
1916 rc = cil_resolve_name(current, portcon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
1917 if (rc != SEPOL_OK) {
1918 goto exit;
1919 }
1920 portcon->context = (struct cil_context*)context_datum;
1921 } else {
1922 rc = cil_resolve_context(current, portcon->context, db);
1923 if (rc != SEPOL_OK) {
1924 goto exit;
1925 }
1926 }
1927
1928 return SEPOL_OK;
1929
1930 exit:
1931 return rc;
1932 }
1933
1934 int cil_resolve_genfscon(struct cil_tree_node *current, struct cil_db *db)
1935 {
1936 struct cil_genfscon *genfscon = current->data;
1937 struct cil_symtab_datum *context_datum = NULL;
1938 int rc = SEPOL_ERR;
1939
1940 if (genfscon->context_str != NULL) {
1941 rc = cil_resolve_name(current, genfscon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
1942 if (rc != SEPOL_OK) {
1943 goto exit;
1944 }
1945 genfscon->context = (struct cil_context*)context_datum;
1946 } else {
1947 rc = cil_resolve_context(current, genfscon->context, db);
1948 if (rc != SEPOL_OK) {
1949 goto exit;
1950 }
1951 }
1952
1953 return SEPOL_OK;
1954
1955 exit:
1956 return rc;
1957 }
1958
1959 int cil_resolve_nodecon(struct cil_tree_node *current, struct cil_db *db)
1960 {
1961 struct cil_nodecon *nodecon = current->data;
1962 struct cil_symtab_datum *addr_datum = NULL;
1963 struct cil_symtab_datum *mask_datum = NULL;
1964 struct cil_symtab_datum *context_datum = NULL;
1965 int rc = SEPOL_ERR;
1966
1967 if (nodecon->addr_str != NULL) {
1968 rc = cil_resolve_name(current, nodecon->addr_str, CIL_SYM_IPADDRS, db, &addr_datum);
1969 if (rc != SEPOL_OK) {
1970 goto exit;
1971 }
1972 nodecon->addr = (struct cil_ipaddr*)addr_datum;
1973 }
1974
1975 if (nodecon->mask_str != NULL) {
1976 rc = cil_resolve_name(current, nodecon->mask_str, CIL_SYM_IPADDRS, db, &mask_datum);
1977 if (rc != SEPOL_OK) {
1978 goto exit;
1979 }
1980 nodecon->mask = (struct cil_ipaddr*)mask_datum;
1981 }
1982
1983 if (nodecon->context_str != NULL) {
1984 rc = cil_resolve_name(current, nodecon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
1985 if (rc != SEPOL_OK) {
1986 goto exit;
1987 }
1988 nodecon->context = (struct cil_context*)context_datum;
1989 } else {
1990 rc = cil_resolve_context(current, nodecon->context, db);
1991 if (rc != SEPOL_OK) {
1992 goto exit;
1993 }
1994 }
1995
1996 if (nodecon->addr->family != nodecon->mask->family) {
1997 cil_log(CIL_ERR, "Nodecon ip address not in the same family\n");
1998 rc = SEPOL_ERR;
1999 goto exit;
2000 }
2001
2002
2003 return SEPOL_OK;
2004
2005 exit:
2006 return rc;
2007 }
2008
2009 int cil_resolve_netifcon(struct cil_tree_node *current, struct cil_db *db)
2010 {
2011 struct cil_netifcon *netifcon = current->data;
2012 struct cil_symtab_datum *ifcon_datum = NULL;
2013 struct cil_symtab_datum *packcon_datum = NULL;
2014
2015 int rc = SEPOL_ERR;
2016
2017 if (netifcon->if_context_str != NULL) {
2018 rc = cil_resolve_name(current, netifcon->if_context_str, CIL_SYM_CONTEXTS, db, &ifcon_datum);
2019 if (rc != SEPOL_OK) {
2020 goto exit;
2021 }
2022 netifcon->if_context = (struct cil_context*)ifcon_datum;
2023 } else {
2024 rc = cil_resolve_context(current, netifcon->if_context, db);
2025 if (rc != SEPOL_OK) {
2026 goto exit;
2027 }
2028 }
2029
2030 if (netifcon->packet_context_str != NULL) {
2031 rc = cil_resolve_name(current, netifcon->packet_context_str, CIL_SYM_CONTEXTS, db, &packcon_datum);
2032 if (rc != SEPOL_OK) {
2033 goto exit;
2034 }
2035 netifcon->packet_context = (struct cil_context*)packcon_datum;
2036 } else {
2037 rc = cil_resolve_context(current, netifcon->packet_context, db);
2038 if (rc != SEPOL_OK) {
2039 goto exit;
2040 }
2041 }
2042 return SEPOL_OK;
2043
2044 exit:
2045 return rc;
2046 }
2047
2048 int cil_resolve_ibendportcon(struct cil_tree_node *current, struct cil_db *db)
2049 {
2050 struct cil_ibendportcon *ibendportcon = current->data;
2051 struct cil_symtab_datum *con_datum = NULL;
2052
2053 int rc = SEPOL_ERR;
2054
2055 if (ibendportcon->context_str) {
2056 rc = cil_resolve_name(current, ibendportcon->context_str, CIL_SYM_CONTEXTS, db, &con_datum);
2057 if (rc != SEPOL_OK)
2058 goto exit;
2059
2060 ibendportcon->context = (struct cil_context *)con_datum;
2061 } else {
2062 rc = cil_resolve_context(current, ibendportcon->context, db);
2063 if (rc != SEPOL_OK)
2064 goto exit;
2065 }
2066
2067 return SEPOL_OK;
2068
2069 exit:
2070 return rc;
2071 }
2072
2073 int cil_resolve_pirqcon(struct cil_tree_node *current, struct cil_db *db)
2074 {
2075 struct cil_pirqcon *pirqcon = current->data;
2076 struct cil_symtab_datum *context_datum = NULL;
2077 int rc = SEPOL_ERR;
2078
2079 if (pirqcon->context_str != NULL) {
2080 rc = cil_resolve_name(current, pirqcon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2081 if (rc != SEPOL_OK) {
2082 goto exit;
2083 }
2084 pirqcon->context = (struct cil_context*)context_datum;
2085 } else {
2086 rc = cil_resolve_context(current, pirqcon->context, db);
2087 if (rc != SEPOL_OK) {
2088 goto exit;
2089 }
2090 }
2091
2092 return SEPOL_OK;
2093
2094 exit:
2095 return rc;
2096 }
2097
2098 int cil_resolve_iomemcon(struct cil_tree_node *current, struct cil_db *db)
2099 {
2100 struct cil_iomemcon *iomemcon = current->data;
2101 struct cil_symtab_datum *context_datum = NULL;
2102 int rc = SEPOL_ERR;
2103
2104 if (iomemcon->context_str != NULL) {
2105 rc = cil_resolve_name(current, iomemcon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2106 if (rc != SEPOL_OK) {
2107 goto exit;
2108 }
2109 iomemcon->context = (struct cil_context*)context_datum;
2110 } else {
2111 rc = cil_resolve_context(current, iomemcon->context, db);
2112 if (rc != SEPOL_OK) {
2113 goto exit;
2114 }
2115 }
2116
2117 return SEPOL_OK;
2118
2119 exit:
2120 return rc;
2121 }
2122
2123 int cil_resolve_ioportcon(struct cil_tree_node *current, struct cil_db *db)
2124 {
2125 struct cil_ioportcon *ioportcon = current->data;
2126 struct cil_symtab_datum *context_datum = NULL;
2127 int rc = SEPOL_ERR;
2128
2129 if (ioportcon->context_str != NULL) {
2130 rc = cil_resolve_name(current, ioportcon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2131 if (rc != SEPOL_OK) {
2132 goto exit;
2133 }
2134 ioportcon->context = (struct cil_context*)context_datum;
2135 } else {
2136 rc = cil_resolve_context(current, ioportcon->context, db);
2137 if (rc != SEPOL_OK) {
2138 goto exit;
2139 }
2140 }
2141
2142 return SEPOL_OK;
2143
2144 exit:
2145 return rc;
2146 }
2147
2148 int cil_resolve_pcidevicecon(struct cil_tree_node *current, struct cil_db *db)
2149 {
2150 struct cil_pcidevicecon *pcidevicecon = current->data;
2151 struct cil_symtab_datum *context_datum = NULL;
2152 int rc = SEPOL_ERR;
2153
2154 if (pcidevicecon->context_str != NULL) {
2155 rc = cil_resolve_name(current, pcidevicecon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2156 if (rc != SEPOL_OK) {
2157 goto exit;
2158 }
2159 pcidevicecon->context = (struct cil_context*)context_datum;
2160 } else {
2161 rc = cil_resolve_context(current, pcidevicecon->context, db);
2162 if (rc != SEPOL_OK) {
2163 goto exit;
2164 }
2165 }
2166
2167 return SEPOL_OK;
2168
2169 exit:
2170 return rc;
2171 }
2172
2173 static int cil_resolve_devicetreecon(struct cil_tree_node *current, struct cil_db *db)
2174 {
2175 struct cil_devicetreecon *devicetreecon = current->data;
2176 struct cil_symtab_datum *context_datum = NULL;
2177 int rc = SEPOL_ERR;
2178
2179 if (devicetreecon->context_str != NULL) {
2180 rc = cil_resolve_name(current, devicetreecon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2181 if (rc != SEPOL_OK) {
2182 goto exit;
2183 }
2184 devicetreecon->context = (struct cil_context*)context_datum;
2185 } else {
2186 rc = cil_resolve_context(current, devicetreecon->context, db);
2187 if (rc != SEPOL_OK) {
2188 goto exit;
2189 }
2190 }
2191
2192 return SEPOL_OK;
2193
2194 exit:
2195 return rc;
2196 }
2197
2198 int cil_resolve_fsuse(struct cil_tree_node *current, struct cil_db *db)
2199 {
2200 struct cil_fsuse *fsuse = current->data;
2201 struct cil_symtab_datum *context_datum = NULL;
2202 int rc = SEPOL_ERR;
2203
2204 if (fsuse->context_str != NULL) {
2205 rc = cil_resolve_name(current, fsuse->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2206 if (rc != SEPOL_OK) {
2207 goto exit;
2208 }
2209 fsuse->context = (struct cil_context*)context_datum;
2210 } else {
2211 rc = cil_resolve_context(current, fsuse->context, db);
2212 if (rc != SEPOL_OK) {
2213 goto exit;
2214 }
2215 }
2216
2217 return SEPOL_OK;
2218
2219 exit:
2220 return rc;
2221 }
2222
2223 int cil_resolve_sidcontext(struct cil_tree_node *current, struct cil_db *db)
2224 {
2225 struct cil_sidcontext *sidcon = current->data;
2226 struct cil_symtab_datum *sid_datum = NULL;
2227 struct cil_symtab_datum *context_datum = NULL;
2228 struct cil_sid *sid = NULL;
2229
2230 int rc = SEPOL_ERR;
2231
2232 rc = cil_resolve_name(current, sidcon->sid_str, CIL_SYM_SIDS, db, &sid_datum);
2233 if (rc != SEPOL_OK) {
2234 goto exit;
2235 }
2236 sid = (struct cil_sid*)sid_datum;
2237 sidcon->sid = sid;
2238
2239 if (sidcon->context_str != NULL) {
2240 rc = cil_resolve_name(current, sidcon->context_str, CIL_SYM_CONTEXTS, db, &context_datum);
2241 if (rc != SEPOL_OK) {
2242 goto exit;
2243 }
2244 sidcon->context = (struct cil_context*)context_datum;
2245 } else if (sidcon->context != NULL) {
2246 rc = cil_resolve_context(current, sidcon->context, db);
2247 if (rc != SEPOL_OK) {
2248 goto exit;
2249 }
2250 }
2251
2252 if (sid->context != NULL) {
2253 cil_log(CIL_ERR, "sid's cannot be associated with more than one context\n");
2254 rc = SEPOL_ERR;
2255 goto exit;
2256 }
2257
2258 sid->context = sidcon->context;
2259
2260 return SEPOL_OK;
2261
2262 exit:
2263 return rc;
2264 }
2265
2266 static int cil_resolve_blockinherit_link(struct cil_tree_node *current, struct cil_db *db)
2267 {
2268 struct cil_blockinherit *inherit = current->data;
2269 struct cil_symtab_datum *block_datum = NULL;
2270 struct cil_tree_node *node = NULL;
2271 int rc = SEPOL_ERR;
2272
2273 rc = cil_resolve_name(current, inherit->block_str, CIL_SYM_BLOCKS, db, &block_datum);
2274 if (rc != SEPOL_OK) {
2275 goto exit;
2276 }
2277
2278 node = NODE(block_datum);
2279
2280 if (node->flavor != CIL_BLOCK) {
2281 cil_log(CIL_ERR, "%s is not a block\n", cil_node_to_string(node));
2282 rc = SEPOL_ERR;
2283 goto exit;
2284 }
2285
2286 inherit->block = (struct cil_block *)block_datum;
2287
2288 if (inherit->block->bi_nodes == NULL) {
2289 cil_list_init(&inherit->block->bi_nodes, CIL_NODE);
2290 }
2291 cil_list_append(inherit->block->bi_nodes, CIL_NODE, current);
2292
2293 return SEPOL_OK;
2294
2295 exit:
2296 return rc;
2297 }
2298
2299 static int cil_resolve_blockinherit_copy(struct cil_tree_node *current, struct cil_db *db)
2300 {
2301 struct cil_block *block = current->data;
2302 struct cil_list_item *item = NULL;
2303 int rc = SEPOL_ERR;
2304
2305 // This block is not inherited
2306 if (block->bi_nodes == NULL) {
2307 rc = SEPOL_OK;
2308 goto exit;
2309 }
2310
2311 // Make sure this is the original block and not a merged block from a blockinherit
2312 if (current != block->datum.nodes->head->data) {
2313 rc = SEPOL_OK;
2314 goto exit;
2315 }
2316
2317 cil_list_for_each(item, block->bi_nodes) {
2318 rc = cil_copy_ast(db, current, item->data);
2319 if (rc != SEPOL_OK) {
2320 cil_log(CIL_ERR, "Failed to copy block contents into blockinherit\n");
2321 goto exit;
2322 }
2323 }
2324
2325 return SEPOL_OK;
2326
2327 exit:
2328 return rc;
2329 }
2330
2331 static void cil_mark_subtree_abstract(struct cil_tree_node *node)
2332 {
2333 struct cil_block *block = node->data;
2334
2335 block->is_abstract = CIL_TRUE;
2336
2337 for (node = node->cl_head; node; node = node->next) {
2338 if (node->flavor == CIL_BLOCK) {
2339 cil_mark_subtree_abstract(node);
2340 }
2341 }
2342 }
2343
2344 static int cil_resolve_blockabstract(struct cil_tree_node *current, struct cil_db *db, struct cil_list *abstract_blocks)
2345 {
2346 struct cil_blockabstract *abstract = current->data;
2347 struct cil_symtab_datum *block_datum = NULL;
2348 struct cil_tree_node *block_node = NULL;
2349 int rc = SEPOL_ERR;
2350
2351 rc = cil_resolve_name(current, abstract->block_str, CIL_SYM_BLOCKS, db, &block_datum);
2352 if (rc != SEPOL_OK) {
2353 goto exit;
2354 }
2355
2356 block_node = NODE(block_datum);
2357 if (block_node->flavor != CIL_BLOCK) {
2358 cil_log(CIL_ERR, "Failed to resolve blockabstract to a block, rc: %d\n", rc);
2359 rc = SEPOL_ERR;
2360 goto exit;
2361 }
2362
2363 abstract->block = (struct cil_block *)block_datum;
2364
2365 cil_list_append(abstract_blocks, CIL_NODE, block_node);
2366
2367 return SEPOL_OK;
2368
2369 exit:
2370 return rc;
2371 }
2372
2373 int cil_resolve_in(struct cil_tree_node *current, struct cil_db *db)
2374 {
2375 struct cil_in *in = current->data;
2376 struct cil_symtab_datum *block_datum = NULL;
2377 struct cil_tree_node *block_node = NULL;
2378 int rc = SEPOL_ERR;
2379
2380 rc = cil_resolve_name(current, in->block_str, CIL_SYM_BLOCKS, db, &block_datum);
2381 if (rc != SEPOL_OK) {
2382 goto exit;
2383 }
2384
2385 in->block = (struct cil_block *)block_datum;
2386
2387 block_node = NODE(block_datum);
2388
2389 if (block_node->flavor == CIL_OPTIONAL) {
2390 if (block_datum->nodes && block_datum->nodes->head != block_datum->nodes->tail) {
2391 cil_tree_log(current, CIL_ERR, "Multiple optional blocks referred to by in-statement");
2392 cil_tree_log(block_node, CIL_ERR, "First optional block");
2393 rc = SEPOL_ERR;
2394 goto exit;
2395 }
2396 }
2397
2398 rc = cil_copy_ast(db, current, block_node);
2399 if (rc != SEPOL_OK) {
2400 cil_tree_log(current, CIL_ERR, "Failed to copy in-statement");
2401 goto exit;
2402 }
2403
2404 cil_tree_children_destroy(current);
2405
2406 return SEPOL_OK;
2407
2408 exit:
2409 return rc;
2410 }
2411
2412 static int cil_resolve_in_list(struct cil_list *in_list, struct cil_db *db)
2413 {
2414 struct cil_list_item *curr = NULL;
2415 struct cil_tree_node *node = NULL;
2416 struct cil_tree_node *last_failed_node = NULL;
2417 struct cil_in *in = NULL;
2418 struct cil_symtab_datum *block_datum = NULL;
2419 int resolved = 0;
2420 int unresolved = 0;
2421 int rc = SEPOL_ERR;
2422
2423 do {
2424 resolved = 0;
2425 unresolved = 0;
2426
2427 cil_list_for_each(curr, in_list) {
2428 if (curr->flavor != CIL_NODE) {
2429 continue;
2430 }
2431
2432 node = curr->data;
2433 in = node->data;
2434
2435 rc = cil_resolve_name(node, in->block_str, CIL_SYM_BLOCKS, db, &block_datum);
2436 if (rc != SEPOL_OK) {
2437 unresolved++;
2438 last_failed_node = node;
2439 } else {
2440 rc = cil_resolve_in(node, db);
2441 if (rc != SEPOL_OK) {
2442 goto exit;
2443 }
2444
2445 resolved++;
2446 curr->data = NULL;
2447 curr->flavor = CIL_NONE;
2448 }
2449 }
2450
2451 if (unresolved > 0 && resolved == 0) {
2452 cil_tree_log(last_failed_node, CIL_ERR, "Failed to resolve in-statement");
2453 rc = SEPOL_ERR;
2454 goto exit;
2455 }
2456
2457 } while (unresolved > 0);
2458
2459 rc = SEPOL_OK;
2460
2461 exit:
2462 return rc;
2463 }
2464
2465
2466 static int cil_resolve_bounds(struct cil_tree_node *current, struct cil_db *db, enum cil_flavor flavor, enum cil_flavor attr_flavor)
2467 {
2468 int rc = SEPOL_ERR;
2469 struct cil_bounds *bounds = current->data;
2470 enum cil_sym_index index;
2471 struct cil_symtab_datum *parent_datum = NULL;
2472 struct cil_symtab_datum *child_datum = NULL;
2473
2474 rc = cil_flavor_to_symtab_index(flavor, &index);
2475 if (rc != SEPOL_OK) {
2476 goto exit;
2477 }
2478
2479 rc = cil_resolve_name(current, bounds->parent_str, index, db, &parent_datum);
2480 if (rc != SEPOL_OK) {
2481 goto exit;
2482 }
2483 if (FLAVOR(parent_datum) == attr_flavor) {
2484 cil_log(CIL_ERR, "Bounds parent %s is an attribute\n", bounds->parent_str);
2485 rc = SEPOL_ERR;
2486 goto exit;
2487 }
2488
2489
2490 rc = cil_resolve_name(current, bounds->child_str, index, db, &child_datum);
2491 if (rc != SEPOL_OK) {
2492 goto exit;
2493 }
2494 if (FLAVOR(child_datum) == attr_flavor) {
2495 cil_log(CIL_ERR, "Bounds child %s is an attribute\n", bounds->child_str);
2496 rc = SEPOL_ERR;
2497 goto exit;
2498 }
2499
2500 switch (flavor) {
2501 case CIL_USER: {
2502 struct cil_user *user = (struct cil_user *)child_datum;
2503
2504 if (user->bounds != NULL) {
2505 cil_tree_log(NODE(user->bounds), CIL_ERR, "User %s already bound by parent", bounds->child_str);
2506 rc = SEPOL_ERR;
2507 goto exit;
2508 }
2509
2510 user->bounds = (struct cil_user *)parent_datum;
2511 break;
2512 }
2513 case CIL_ROLE: {
2514 struct cil_role *role = (struct cil_role *)child_datum;
2515
2516 if (role->bounds != NULL) {
2517 cil_tree_log(NODE(role->bounds), CIL_ERR, "Role %s already bound by parent", bounds->child_str);
2518 rc = SEPOL_ERR;
2519 goto exit;
2520 }
2521
2522 role->bounds = (struct cil_role *)parent_datum;
2523 break;
2524 }
2525 case CIL_TYPE: {
2526 struct cil_type *type = (struct cil_type *)child_datum;
2527
2528 if (type->bounds != NULL) {
2529 cil_tree_log(NODE(type->bounds), CIL_ERR, "Type %s already bound by parent", bounds->child_str);
2530 rc = SEPOL_ERR;
2531 goto exit;
2532 }
2533
2534 type->bounds = (struct cil_type *)parent_datum;
2535 break;
2536 }
2537 default:
2538 break;
2539 }
2540
2541 return SEPOL_OK;
2542
2543 exit:
2544 cil_tree_log(current, CIL_ERR, "Bad bounds statement");
2545 return rc;
2546 }
2547
2548 static int cil_resolve_default(struct cil_tree_node *current, struct cil_db *db)
2549 {
2550 int rc = SEPOL_ERR;
2551 struct cil_default *def = current->data;
2552 struct cil_list_item *curr;
2553 struct cil_symtab_datum *datum;
2554
2555 cil_list_init(&def->class_datums, def->flavor);
2556
2557 cil_list_for_each(curr, def->class_strs) {
2558 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_CLASSES, db, &datum);
2559 if (rc != SEPOL_OK) {
2560 goto exit;
2561 }
2562 cil_list_append(def->class_datums, CIL_CLASS, datum);
2563 }
2564
2565 return SEPOL_OK;
2566
2567 exit:
2568 return rc;
2569 }
2570
2571 static int cil_resolve_defaultrange(struct cil_tree_node *current, struct cil_db *db)
2572 {
2573 int rc = SEPOL_ERR;
2574 struct cil_defaultrange *def = current->data;
2575 struct cil_list_item *curr;
2576 struct cil_symtab_datum *datum;
2577
2578 cil_list_init(&def->class_datums, CIL_DEFAULTRANGE);
2579
2580 cil_list_for_each(curr, def->class_strs) {
2581 rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_CLASSES, db, &datum);
2582 if (rc != SEPOL_OK) {
2583 goto exit;
2584 }
2585 cil_list_append(def->class_datums, CIL_CLASS, datum);
2586 }
2587
2588 return SEPOL_OK;
2589
2590 exit:
2591 return rc;
2592 }
2593
2594 static void cil_print_recursive_call(struct cil_tree_node *call_node, struct cil_tree_node *terminating_node)
2595 {
2596 struct cil_list *trace = NULL;
2597 struct cil_list_item * item = NULL;
2598 struct cil_tree_node *curr = NULL;
2599
2600 cil_list_init(&trace, CIL_NODE);
2601
2602 for (curr = call_node; curr != terminating_node; curr = curr->parent) {
2603 if (curr->flavor == CIL_CALL) {
2604 if (curr != call_node) {
2605 cil_list_prepend(trace, CIL_NODE, NODE(((struct cil_call *)curr->data)->macro));
2606 }
2607 cil_list_prepend(trace, CIL_NODE, curr);
2608 }
2609 }
2610
2611 if (terminating_node->flavor == CIL_MACRO) {
2612 cil_list_prepend(trace, CIL_NODE, terminating_node);
2613 } else {
2614 cil_list_prepend(trace, CIL_NODE, NODE(((struct cil_call *)terminating_node->data)->macro));
2615 }
2616
2617 cil_list_for_each(item, trace) {
2618 curr = item->data;
2619 if (curr->flavor == CIL_MACRO) {
2620 cil_tree_log(curr, CIL_ERR, "macro %s", DATUM(curr->data)->name);
2621 } else {
2622 cil_tree_log(curr, CIL_ERR, "call %s", ((struct cil_call *)curr->data)->macro_str);
2623 }
2624 }
2625
2626 cil_list_destroy(&trace, CIL_FALSE);
2627 }
2628
2629 static int cil_check_recursive_call(struct cil_tree_node *call_node, struct cil_tree_node *macro_node)
2630 {
2631 struct cil_tree_node *curr = NULL;
2632 struct cil_call * call = NULL;
2633 int rc = SEPOL_ERR;
2634
2635 for (curr = call_node; curr != NULL; curr = curr->parent) {
2636 if (curr->flavor == CIL_CALL) {
2637 if (curr == call_node) {
2638 continue;
2639 }
2640
2641 call = curr->data;
2642 if (call->macro != macro_node->data) {
2643 continue;
2644 }
2645 } else if (curr->flavor == CIL_MACRO) {
2646 if (curr != macro_node) {
2647 rc = SEPOL_OK;
2648 goto exit;
2649 }
2650 } else {
2651 continue;
2652 }
2653
2654 cil_log(CIL_ERR, "Recursive macro call found:\n");
2655 cil_print_recursive_call(call_node, curr);
2656
2657 rc = SEPOL_ERR;
2658 goto exit;
2659 }
2660
2661 rc = SEPOL_OK;
2662 exit:
2663 return rc;
2664 }
2665
2666 static int cil_build_call_args(struct cil_tree_node *call_node, struct cil_call *call, struct cil_macro *macro, struct cil_db *db)
2667 {
2668 struct cil_list_item *item;
2669 struct cil_args *arg = NULL;
2670 struct cil_tree_node *arg_node = NULL;
2671 int rc = SEPOL_ERR;
2672
2673 if (macro->params == NULL) {
2674 if (call->args_tree == NULL) {
2675 return SEPOL_OK;
2676 } else {
2677 cil_tree_log(call_node, CIL_ERR, "Unexpected arguments");
2678 return SEPOL_ERR;
2679 }
2680 }
2681 if (call->args_tree == NULL) {
2682 cil_tree_log(call_node, CIL_ERR, "Missing arguments");
2683 return SEPOL_ERR;
2684 }
2685
2686 arg_node = call->args_tree->root->cl_head;
2687
2688 cil_list_init(&call->args, CIL_LIST_ITEM);
2689
2690 cil_list_for_each(item, macro->params) {
2691 enum cil_flavor flavor = ((struct cil_param*)item->data)->flavor;
2692
2693 if (arg_node == NULL) {
2694 cil_tree_log(call_node, CIL_ERR, "Missing arguments");
2695 rc = SEPOL_ERR;
2696 goto exit;
2697 }
2698 if (item->flavor != CIL_PARAM) {
2699 rc = SEPOL_ERR;
2700 goto exit;
2701 }
2702
2703 cil_args_init(&arg);
2704
2705 switch (flavor) {
2706 case CIL_DECLARED_STRING: {
2707 struct cil_symtab_datum *string;
2708 if (arg_node->data == NULL) {
2709 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2710 cil_destroy_args(arg);
2711 rc = SEPOL_ERR;
2712 goto exit;
2713 }
2714 string = cil_gen_declared_string(db, arg_node->data, call_node);
2715 if (string) {
2716 arg->arg = string;
2717 } else {
2718 arg->arg_str = arg_node->data;
2719 }
2720 }
2721 break;
2722 case CIL_TYPE:
2723 if (arg_node->data == NULL) {
2724 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2725 cil_destroy_args(arg);
2726 rc = SEPOL_ERR;
2727 goto exit;
2728 }
2729 arg->arg_str = arg_node->data;
2730 break;
2731 case CIL_ROLE:
2732 if (arg_node->data == NULL) {
2733 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2734 cil_destroy_args(arg);
2735 rc = SEPOL_ERR;
2736 goto exit;
2737 }
2738 arg->arg_str = arg_node->data;
2739 break;
2740 case CIL_USER:
2741 if (arg_node->data == NULL) {
2742 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2743 cil_destroy_args(arg);
2744 rc = SEPOL_ERR;
2745 goto exit;
2746 }
2747 arg->arg_str = arg_node->data;
2748 break;
2749 case CIL_SENS:
2750 if (arg_node->data == NULL) {
2751 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2752 cil_destroy_args(arg);
2753 rc = SEPOL_ERR;
2754 goto exit;
2755 }
2756 arg->arg_str = arg_node->data;
2757 break;
2758 case CIL_CAT:
2759 if (arg_node->data == NULL) {
2760 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2761 cil_destroy_args(arg);
2762 rc = SEPOL_ERR;
2763 goto exit;
2764 }
2765 arg->arg_str = arg_node->data;
2766 break;
2767 case CIL_BOOL:
2768 if (arg_node->data == NULL) {
2769 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2770 cil_destroy_args(arg);
2771 rc = SEPOL_ERR;
2772 goto exit;
2773 }
2774 arg->arg_str = arg_node->data;
2775 break;
2776 case CIL_CATSET: {
2777 if (arg_node->cl_head != NULL) {
2778 struct cil_catset *catset = NULL;
2779 struct cil_tree_node *cat_node = NULL;
2780 cil_catset_init(&catset);
2781 rc = cil_fill_cats(arg_node, &catset->cats);
2782 if (rc != SEPOL_OK) {
2783 cil_destroy_catset(catset);
2784 cil_destroy_args(arg);
2785 goto exit;
2786 }
2787 cil_tree_node_init(&cat_node);
2788 cat_node->flavor = CIL_CATSET;
2789 cat_node->data = catset;
2790 cil_list_append(((struct cil_symtab_datum*)catset)->nodes,
2791 CIL_LIST_ITEM, cat_node);
2792 arg->arg = (struct cil_symtab_datum*)catset;
2793 } else if (arg_node->data == NULL) {
2794 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2795 cil_destroy_args(arg);
2796 rc = SEPOL_ERR;
2797 goto exit;
2798 } else {
2799 arg->arg_str = arg_node->data;
2800 }
2801
2802 break;
2803 }
2804 case CIL_LEVEL: {
2805 if (arg_node->cl_head != NULL) {
2806 struct cil_level *level = NULL;
2807 struct cil_tree_node *lvl_node = NULL;
2808 cil_level_init(&level);
2809
2810 rc = cil_fill_level(arg_node->cl_head, level);
2811 if (rc != SEPOL_OK) {
2812 cil_log(CIL_ERR, "Failed to create anonymous level, rc: %d\n", rc);
2813 cil_destroy_level(level);
2814 cil_destroy_args(arg);
2815 goto exit;
2816 }
2817 cil_tree_node_init(&lvl_node);
2818 lvl_node->flavor = CIL_LEVEL;
2819 lvl_node->data = level;
2820 cil_list_append(((struct cil_symtab_datum*)level)->nodes,
2821 CIL_LIST_ITEM, lvl_node);
2822 arg->arg = (struct cil_symtab_datum*)level;
2823 } else if (arg_node->data == NULL) {
2824 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2825 cil_destroy_args(arg);
2826 rc = SEPOL_ERR;
2827 goto exit;
2828 } else {
2829 arg->arg_str = arg_node->data;
2830 }
2831
2832 break;
2833 }
2834 case CIL_LEVELRANGE: {
2835 if (arg_node->cl_head != NULL) {
2836 struct cil_levelrange *range = NULL;
2837 struct cil_tree_node *range_node = NULL;
2838 cil_levelrange_init(&range);
2839
2840 rc = cil_fill_levelrange(arg_node->cl_head, range);
2841 if (rc != SEPOL_OK) {
2842 cil_log(CIL_ERR, "Failed to create anonymous levelrange, rc: %d\n", rc);
2843 cil_destroy_levelrange(range);
2844 cil_destroy_args(arg);
2845 goto exit;
2846 }
2847 cil_tree_node_init(&range_node);
2848 range_node->flavor = CIL_LEVELRANGE;
2849 range_node->data = range;
2850 cil_list_append(((struct cil_symtab_datum*)range)->nodes,
2851 CIL_LIST_ITEM, range_node);
2852 arg->arg = (struct cil_symtab_datum*)range;
2853 } else if (arg_node->data == NULL) {
2854 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2855 cil_destroy_args(arg);
2856 rc = SEPOL_ERR;
2857 goto exit;
2858 } else {
2859 arg->arg_str = arg_node->data;
2860 }
2861
2862 break;
2863 }
2864 case CIL_IPADDR: {
2865 if (arg_node->data == NULL) {
2866 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2867 cil_destroy_args(arg);
2868 rc = SEPOL_ERR;
2869 goto exit;
2870 } else if (strchr(arg_node->data, '.') || strchr(arg_node->data, ':')) {
2871 struct cil_ipaddr *ipaddr = NULL;
2872 struct cil_tree_node *addr_node = NULL;
2873 cil_ipaddr_init(&ipaddr);
2874 rc = cil_fill_ipaddr(arg_node, ipaddr);
2875 if (rc != SEPOL_OK) {
2876 cil_tree_log(call_node, CIL_ERR, "Failed to create anonymous ip address");
2877 cil_destroy_ipaddr(ipaddr);
2878 cil_destroy_args(arg);
2879 goto exit;
2880 }
2881 cil_tree_node_init(&addr_node);
2882 addr_node->flavor = CIL_IPADDR;
2883 addr_node->data = ipaddr;
2884 cil_list_append(DATUM(ipaddr)->nodes, CIL_LIST_ITEM, addr_node);
2885 arg->arg = DATUM(ipaddr);
2886 } else {
2887 arg->arg_str = arg_node->data;
2888 }
2889 break;
2890 }
2891 case CIL_CLASS:
2892 if (arg_node->data == NULL) {
2893 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2894 cil_destroy_args(arg);
2895 rc = SEPOL_ERR;
2896 goto exit;
2897 }
2898 arg->arg_str = arg_node->data;
2899 break;
2900 case CIL_MAP_CLASS:
2901 if (arg_node->data == NULL) {
2902 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2903 cil_destroy_args(arg);
2904 rc = SEPOL_ERR;
2905 goto exit;
2906 }
2907 arg->arg_str = arg_node->data;
2908 break;
2909 case CIL_CLASSPERMISSION: {
2910 if (arg_node->cl_head != NULL) {
2911 struct cil_classpermission *cp = NULL;
2912 struct cil_tree_node *cp_node = NULL;
2913
2914 cil_classpermission_init(&cp);
2915 rc = cil_fill_classperms_list(arg_node, &cp->classperms);
2916 if (rc != SEPOL_OK) {
2917 cil_log(CIL_ERR, "Failed to create anonymous classpermission\n");
2918 cil_destroy_classpermission(cp);
2919 cil_destroy_args(arg);
2920 goto exit;
2921 }
2922 cil_tree_node_init(&cp_node);
2923 cp_node->flavor = CIL_CLASSPERMISSION;
2924 cp_node->data = cp;
2925 cil_list_append(cp->datum.nodes, CIL_LIST_ITEM, cp_node);
2926 arg->arg = (struct cil_symtab_datum*)cp;
2927 } else if (arg_node->data == NULL) {
2928 cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
2929 cil_destroy_args(arg);
2930 rc = SEPOL_ERR;
2931 goto exit;
2932 } else {
2933 arg->arg_str = arg_node->data;
2934 }
2935 break;
2936 }
2937 default:
2938 cil_log(CIL_ERR, "Unexpected flavor: %d\n",
2939 (((struct cil_param*)item->data)->flavor));
2940 cil_destroy_args(arg);
2941 rc = SEPOL_ERR;
2942 goto exit;
2943 }
2944 arg->param_str = ((struct cil_param*)item->data)->str;
2945 arg->flavor = flavor;
2946
2947 cil_list_append(call->args, CIL_ARGS, arg);
2948
2949 arg_node = arg_node->next;
2950 }
2951
2952 if (arg_node != NULL) {
2953 cil_tree_log(call_node, CIL_ERR, "Unexpected arguments");
2954 rc = SEPOL_ERR;
2955 goto exit;
2956 }
2957
2958 return SEPOL_OK;
2959
2960 exit:
2961 return rc;
2962 }
2963
2964 static int cil_resolve_call(struct cil_tree_node *current, struct cil_db *db)
2965 {
2966 struct cil_call *call = current->data;
2967 struct cil_tree_node *macro_node = NULL;
2968 struct cil_symtab_datum *macro_datum = NULL;
2969 int rc = SEPOL_ERR;
2970
2971 if (call->copied) {
2972 return SEPOL_OK;
2973 }
2974
2975 rc = cil_resolve_name(current, call->macro_str, CIL_SYM_BLOCKS, db, &macro_datum);
2976 if (rc != SEPOL_OK) {
2977 goto exit;
2978 }
2979
2980 macro_node = NODE(macro_datum);
2981
2982 if (macro_node->flavor != CIL_MACRO) {
2983 cil_tree_log(current, CIL_ERR, "Failed to resolve %s to a macro", call->macro_str);
2984 rc = SEPOL_ERR;
2985 goto exit;
2986 }
2987 call->macro = (struct cil_macro*)macro_datum;
2988
2989 rc = cil_build_call_args(current, call, call->macro, db);
2990 if (rc != SEPOL_OK) {
2991 goto exit;
2992 }
2993
2994 rc = cil_check_recursive_call(current, macro_node);
2995 if (rc != SEPOL_OK) {
2996 goto exit;
2997 }
2998
2999 rc = cil_copy_ast(db, macro_node, current);
3000 if (rc != SEPOL_OK) {
3001 cil_tree_log(current, CIL_ERR, "Failed to copy macro %s to call", macro_datum->name);
3002 goto exit;
3003 }
3004
3005 call->copied = 1;
3006
3007 return SEPOL_OK;
3008
3009 exit:
3010 return rc;
3011 }
3012
3013 static int cil_resolve_call_args(struct cil_tree_node *current, struct cil_db *db)
3014 {
3015 struct cil_call *call = current->data;
3016 int rc = SEPOL_ERR;
3017 enum cil_sym_index sym_index = CIL_SYM_UNKNOWN;
3018 struct cil_list_item *item;
3019
3020 if (call->args == NULL) {
3021 rc = SEPOL_OK;
3022 goto exit;
3023 }
3024
3025 cil_list_for_each(item, call->args) {
3026 struct cil_args *arg = item->data;
3027 if (arg->arg == NULL && arg->arg_str == NULL) {
3028 cil_log(CIL_ERR, "Arguments not created correctly\n");
3029 rc = SEPOL_ERR;
3030 goto exit;
3031 }
3032
3033 switch (arg->flavor) {
3034 case CIL_DECLARED_STRING:
3035 if (arg->arg != NULL) {
3036 continue; /* No need to resolve */
3037 } else {
3038 sym_index = CIL_SYM_STRINGS;
3039 }
3040 break;
3041 case CIL_LEVEL:
3042 if (arg->arg_str == NULL && arg->arg != NULL) {
3043 continue; // anonymous, no need to resolve
3044 } else {
3045 sym_index = CIL_SYM_LEVELS;
3046 }
3047 break;
3048 case CIL_LEVELRANGE:
3049 if (arg->arg_str == NULL && arg->arg != NULL) {
3050 continue; // anonymous, no need to resolve
3051 } else {
3052 sym_index = CIL_SYM_LEVELRANGES;
3053 }
3054 break;
3055 case CIL_CATSET:
3056 if (arg->arg_str == NULL && arg->arg != NULL) {
3057 continue; // anonymous, no need to resolve
3058 } else {
3059 sym_index = CIL_SYM_CATS;
3060 }
3061 break;
3062 case CIL_IPADDR:
3063 if (arg->arg_str == NULL && arg->arg != NULL) {
3064 continue; // anonymous, no need to resolve
3065 } else {
3066 sym_index = CIL_SYM_IPADDRS;
3067 }
3068 break;
3069 case CIL_CLASSPERMISSION:
3070 if (arg->arg_str == NULL && arg->arg != NULL) {
3071 continue;
3072 } else {
3073 sym_index = CIL_SYM_CLASSPERMSETS;
3074 }
3075 break;
3076 case CIL_TYPE:
3077 if (arg->arg_str == NULL && arg->arg != NULL) {
3078 continue; // anonymous, no need to resolve
3079 } else {
3080 sym_index = CIL_SYM_TYPES;
3081 }
3082 break;
3083 case CIL_ROLE:
3084 sym_index = CIL_SYM_ROLES;
3085 break;
3086 case CIL_USER:
3087 sym_index = CIL_SYM_USERS;
3088 break;
3089 case CIL_SENS:
3090 sym_index = CIL_SYM_SENS;
3091 break;
3092 case CIL_CAT:
3093 sym_index = CIL_SYM_CATS;
3094 break;
3095 case CIL_CLASS:
3096 case CIL_MAP_CLASS:
3097 sym_index = CIL_SYM_CLASSES;
3098 break;
3099 case CIL_BOOL:
3100 sym_index = CIL_SYM_BOOLS;
3101 break;
3102 default:
3103 rc = SEPOL_ERR;
3104 goto exit;
3105 }
3106
3107 if (sym_index != CIL_SYM_UNKNOWN) {
3108 struct cil_symtab_datum *datum;
3109 struct cil_tree_node *n;
3110 rc = cil_resolve_name(current, arg->arg_str, sym_index, db, &datum);
3111 if (rc != SEPOL_OK) {
3112 cil_tree_log(current, CIL_ERR, "Failed to resolve %s in call argument list", arg->arg_str);
3113 goto exit;
3114 }
3115 arg->arg = datum;
3116 n = NODE(datum);
3117 while (n && n->flavor != CIL_ROOT) {
3118 if (n == current) {
3119 symtab_t *s = datum->symtab;
3120 /* Call arg should not resolve to declaration in the call
3121 * Need to remove datum temporarily to resolve to a datum outside
3122 * the call.
3123 */
3124 cil_symtab_remove_datum(datum);
3125 rc = cil_resolve_name(current, arg->arg_str, sym_index, db, &(arg->arg));
3126 if (rc != SEPOL_OK) {
3127 cil_tree_log(current, CIL_ERR, "Failed to resolve %s in call argument list", arg->arg_str);
3128 goto exit;
3129 }
3130 rc = cil_symtab_insert(s, datum->name, datum, NULL);
3131 if (rc != SEPOL_OK) {
3132 cil_tree_log(current, CIL_ERR, "Failed to re-insert datum while resolving %s in call argument list", arg->arg_str);
3133 goto exit;
3134 }
3135 break;
3136 }
3137 n = n->parent;
3138 }
3139 }
3140 }
3141
3142 return SEPOL_OK;
3143
3144 exit:
3145 return rc;
3146 }
3147
3148 int cil_resolve_name_call_args(struct cil_call *call, char *name, enum cil_sym_index sym_index, struct cil_symtab_datum **datum)
3149 {
3150 struct cil_list_item *item;
3151 enum cil_sym_index param_index = CIL_SYM_UNKNOWN;
3152 int rc = SEPOL_ERR;
3153
3154 if (call == NULL || name == NULL) {
3155 goto exit;
3156 }
3157
3158 if (call->args == NULL) {
3159 goto exit;
3160 }
3161
3162 cil_list_for_each(item, call->args) {
3163 struct cil_args * arg = item->data;
3164 rc = cil_flavor_to_symtab_index(arg->flavor, &param_index);
3165 if (param_index == sym_index) {
3166 if (name == arg->param_str) {
3167 *datum = arg->arg;
3168 rc = *datum ? SEPOL_OK : SEPOL_ERR;
3169 goto exit;
3170 }
3171 }
3172 }
3173
3174 return SEPOL_ERR;
3175
3176 exit:
3177 return rc;
3178 }
3179
3180 int cil_resolve_expr(enum cil_flavor expr_type, struct cil_list *str_expr, struct cil_list **datum_expr, struct cil_tree_node *parent, struct cil_db *db)
3181 {
3182 int rc = SEPOL_ERR;
3183 struct cil_list_item *curr;
3184 struct cil_symtab_datum *res_datum = NULL;
3185 enum cil_sym_index sym_index = CIL_SYM_UNKNOWN;
3186 struct cil_list *datum_sub_expr;
3187 enum cil_flavor op = CIL_NONE;
3188
3189 switch (str_expr->flavor) {
3190 case CIL_BOOL:
3191 sym_index = CIL_SYM_BOOLS;
3192 break;
3193 case CIL_TUNABLE:
3194 sym_index = CIL_SYM_TUNABLES;
3195 break;
3196 case CIL_TYPE:
3197 sym_index = CIL_SYM_TYPES;
3198 break;
3199 case CIL_ROLE:
3200 sym_index = CIL_SYM_ROLES;
3201 break;
3202 case CIL_USER:
3203 sym_index = CIL_SYM_USERS;
3204 break;
3205 case CIL_CAT:
3206 sym_index = CIL_SYM_CATS;
3207 break;
3208 default:
3209 break;
3210 }
3211
3212 cil_list_init(datum_expr, str_expr->flavor);
3213
3214 cil_list_for_each(curr, str_expr) {
3215 switch (curr->flavor) {
3216 case CIL_STRING:
3217 rc = cil_resolve_name(parent, curr->data, sym_index, db, &res_datum);
3218 if (rc != SEPOL_OK) {
3219 goto exit;
3220 }
3221 if (sym_index == CIL_SYM_CATS && NODE(res_datum)->flavor == CIL_CATSET) {
3222 struct cil_catset *catset = (struct cil_catset *)res_datum;
3223 if (op == CIL_RANGE) {
3224 cil_tree_log(parent, CIL_ERR, "Category set not allowed in category range");
3225 rc = SEPOL_ERR;
3226 goto exit;
3227 }
3228 if (!res_datum->name) {
3229 /* Anonymous category sets need to be resolved when encountered */
3230 if (!catset->cats->datum_expr) {
3231 rc = cil_resolve_expr(expr_type, catset->cats->str_expr, &catset->cats->datum_expr, parent, db);
3232 if (rc != SEPOL_OK) {
3233 goto exit;
3234 }
3235 }
3236 cil_copy_list(catset->cats->datum_expr, &datum_sub_expr);
3237 cil_list_append(*datum_expr, CIL_LIST, datum_sub_expr);
3238 } else {
3239 cil_list_append(*datum_expr, CIL_DATUM, res_datum);
3240 }
3241 } else {
3242 if (sym_index == CIL_SYM_TYPES && (expr_type == CIL_CONSTRAIN || expr_type == CIL_VALIDATETRANS)) {
3243 cil_type_used(res_datum, CIL_ATTR_CONSTRAINT);
3244 }
3245 cil_list_append(*datum_expr, CIL_DATUM, res_datum);
3246 }
3247 break;
3248 case CIL_LIST: {
3249 rc = cil_resolve_expr(expr_type, curr->data, &datum_sub_expr, parent, db);
3250 if (rc != SEPOL_OK) {
3251 goto exit;
3252 }
3253 cil_list_append(*datum_expr, CIL_LIST, datum_sub_expr);
3254 break;
3255 }
3256 default:
3257 if (curr->flavor == CIL_OP) {
3258 op = (enum cil_flavor)(uintptr_t)curr->data;
3259 }
3260 cil_list_append(*datum_expr, curr->flavor, curr->data);
3261 break;
3262 }
3263 }
3264 return SEPOL_OK;
3265
3266 exit:
3267 cil_list_destroy(datum_expr, CIL_FALSE);
3268 return rc;
3269 }
3270
3271 int cil_resolve_boolif(struct cil_tree_node *current, struct cil_db *db)
3272 {
3273 int rc = SEPOL_ERR;
3274 struct cil_booleanif *bif = (struct cil_booleanif*)current->data;
3275
3276 rc = cil_resolve_expr(CIL_BOOLEANIF, bif->str_expr, &bif->datum_expr, current, db);
3277 if (rc != SEPOL_OK) {
3278 goto exit;
3279 }
3280
3281 return SEPOL_OK;
3282
3283 exit:
3284 return rc;
3285 }
3286
3287 static int __cil_evaluate_tunable_expr(struct cil_list_item *curr);
3288
3289 static int __cil_evaluate_tunable_expr_helper(struct cil_list_item *curr)
3290 {
3291 if (curr == NULL) {
3292 return CIL_FALSE;
3293 } else if (curr->flavor == CIL_DATUM) {
3294 struct cil_tunable *tun = curr->data;
3295 return tun->value;
3296 } else if (curr->flavor == CIL_LIST) {
3297 struct cil_list *l = curr->data;
3298 return __cil_evaluate_tunable_expr(l->head);
3299 } else {
3300 return CIL_FALSE;
3301 }
3302 }
3303
3304 static int __cil_evaluate_tunable_expr(struct cil_list_item *curr)
3305 {
3306 /* Assumes expression is well-formed */
3307
3308 if (curr == NULL) {
3309 return CIL_FALSE;
3310 } else if (curr->flavor == CIL_OP) {
3311 uint16_t v1, v2;
3312 enum cil_flavor op_flavor = (enum cil_flavor)(uintptr_t)curr->data;
3313
3314 v1 = __cil_evaluate_tunable_expr_helper(curr->next);
3315
3316 if (op_flavor == CIL_NOT) return !v1;
3317
3318 v2 = __cil_evaluate_tunable_expr_helper(curr->next->next);
3319
3320 if (op_flavor == CIL_AND) return (v1 && v2);
3321 else if (op_flavor == CIL_OR) return (v1 || v2);
3322 else if (op_flavor == CIL_XOR) return (v1 ^ v2);
3323 else if (op_flavor == CIL_EQ) return (v1 == v2);
3324 else if (op_flavor == CIL_NEQ) return (v1 != v2);
3325 else return CIL_FALSE;
3326 } else {
3327 uint16_t v;
3328 for (;curr; curr = curr->next) {
3329 v = __cil_evaluate_tunable_expr_helper(curr);
3330 if (v) return v;
3331 }
3332 return CIL_FALSE;
3333 }
3334 }
3335
3336 int cil_resolve_tunif(struct cil_tree_node *current, struct cil_db *db)
3337 {
3338 int rc = SEPOL_ERR;
3339 struct cil_tunableif *tif = (struct cil_tunableif*)current->data;
3340 uint16_t result = CIL_FALSE;
3341 struct cil_tree_node *true_node = NULL;
3342 struct cil_tree_node *false_node = NULL;
3343 struct cil_condblock *cb = NULL;
3344
3345 rc = cil_resolve_expr(CIL_TUNABLEIF, tif->str_expr, &tif->datum_expr, current, db);
3346 if (rc != SEPOL_OK) {
3347 goto exit;
3348 }
3349
3350 result = __cil_evaluate_tunable_expr(tif->datum_expr->head);
3351
3352 if (current->cl_head != NULL && current->cl_head->flavor == CIL_CONDBLOCK) {
3353 cb = current->cl_head->data;
3354 if (cb->flavor == CIL_CONDTRUE) {
3355 true_node = current->cl_head;
3356 } else if (cb->flavor == CIL_CONDFALSE) {
3357 false_node = current->cl_head;
3358 }
3359 }
3360
3361 if (current->cl_head != NULL && current->cl_head->next != NULL && current->cl_head->next->flavor == CIL_CONDBLOCK) {
3362 cb = current->cl_head->next->data;
3363 if (cb->flavor == CIL_CONDTRUE) {
3364 true_node = current->cl_head->next;
3365 } else if (cb->flavor == CIL_CONDFALSE) {
3366 false_node = current->cl_head->next;
3367 }
3368 }
3369
3370 if (result == CIL_TRUE) {
3371 if (true_node != NULL) {
3372 rc = cil_copy_ast(db, true_node, current->parent);
3373 if (rc != SEPOL_OK) {
3374 goto exit;
3375 }
3376 }
3377 } else {
3378 if (false_node != NULL) {
3379 rc = cil_copy_ast(db, false_node, current->parent);
3380 if (rc != SEPOL_OK) {
3381 goto exit;
3382 }
3383 }
3384 }
3385
3386 cil_tree_children_destroy(current);
3387 current->cl_head = NULL;
3388 current->cl_tail = NULL;
3389
3390 return SEPOL_OK;
3391
3392 exit:
3393 return rc;
3394 }
3395
3396 int cil_resolve_userattributeset(struct cil_tree_node *current, struct cil_db *db)
3397 {
3398 int rc = SEPOL_ERR;
3399 struct cil_userattributeset *attrusers = current->data;
3400 struct cil_symtab_datum *attr_datum = NULL;
3401 struct cil_tree_node *attr_node = NULL;
3402 struct cil_userattribute *attr = NULL;
3403
3404 rc = cil_resolve_name(current, attrusers->attr_str, CIL_SYM_USERS, db, &attr_datum);
3405 if (rc != SEPOL_OK) {
3406 goto exit;
3407 }
3408 attr_node = NODE(attr_datum);
3409
3410 if (attr_node->flavor != CIL_USERATTRIBUTE) {
3411 rc = SEPOL_ERR;
3412 cil_log(CIL_ERR, "Attribute user not an attribute\n");
3413 goto exit;
3414 }
3415 attr = (struct cil_userattribute*)attr_datum;
3416 attrusers->attr = attr;
3417
3418 rc = cil_resolve_expr(CIL_USERATTRIBUTESET, attrusers->str_expr, &attrusers->datum_expr, current, db);
3419 if (rc != SEPOL_OK) {
3420 goto exit;
3421 }
3422
3423 if (attr->expr_list == NULL) {
3424 cil_list_init(&attr->expr_list, CIL_USERATTRIBUTE);
3425 }
3426
3427 cil_list_append(attr->expr_list, CIL_LIST, attrusers->datum_expr);
3428
3429 return SEPOL_OK;
3430
3431 exit:
3432 return rc;
3433 }
3434
3435 /*
3436 * Degenerate inheritance leads to exponential growth of the policy
3437 * It can take many forms, but here is one example.
3438 * ...
3439 * (blockinherit ba)
3440 * (block b0
3441 * (block b1
3442 * (block b2
3443 * (block b3
3444 * ...
3445 * )
3446 * (blockinherit b3)
3447 * )
3448 * (blockinherit b2)
3449 * )
3450 * (blockinherit b1)
3451 * )
3452 * (blockinherit b0)
3453 * ...
3454 * This leads to 2^4 copies of the content of block b3, 2^3 copies of the
3455 * contents of block b2, etc.
3456 */
3457 static unsigned cil_count_actual(struct cil_tree_node *node)
3458 {
3459 unsigned count = 0;
3460
3461 if (node->flavor == CIL_BLOCKINHERIT) {
3462 count += 1;
3463 }
3464
3465 for (node = node->cl_head; node; node = node->next) {
3466 count += cil_count_actual(node);
3467 }
3468
3469 return count;
3470 }
3471
3472 static int cil_check_inheritances(struct cil_tree_node *node, unsigned max, unsigned *count, struct cil_stack *stack, unsigned *loop)
3473 {
3474 int rc;
3475
3476 if (node->flavor == CIL_BLOCKINHERIT) {
3477 struct cil_blockinherit *bi = node->data;
3478 *count += 1;
3479 if (*count > max) {
3480 cil_tree_log(node, CIL_ERR, "Degenerate inheritance detected");
3481 return SEPOL_ERR;
3482 }
3483 if (bi->block) {
3484 struct cil_tree_node *block_node = NODE(bi->block);
3485 struct cil_stack_item *item;
3486 int i = 0;
3487 cil_stack_for_each(stack, i, item) {
3488 if (block_node == (struct cil_tree_node *)item->data) {
3489 *loop = CIL_TRUE;
3490 cil_tree_log(block_node, CIL_ERR, "Block inheritance loop found");
3491 cil_tree_log(node, CIL_ERR, " blockinherit");
3492 return SEPOL_ERR;
3493 }
3494 }
3495 cil_stack_push(stack, CIL_BLOCK, block_node);
3496 rc = cil_check_inheritances(block_node, max, count, stack, loop);
3497 cil_stack_pop(stack);
3498 if (rc != SEPOL_OK) {
3499 if (*loop == CIL_TRUE) {
3500 cil_tree_log(node, CIL_ERR, " blockinherit");
3501 }
3502 return SEPOL_ERR;
3503 }
3504 }
3505 }
3506
3507 for (node = node->cl_head; node; node = node->next) {
3508 rc = cil_check_inheritances(node, max, count, stack, loop);
3509 if (rc != SEPOL_OK) {
3510 return SEPOL_ERR;
3511 }
3512 }
3513
3514 return SEPOL_OK;
3515 }
3516
3517 static int cil_check_for_bad_inheritance(struct cil_tree_node *node)
3518 {
3519 unsigned num_actual, max;
3520 unsigned num_potential = 0;
3521 unsigned loop = CIL_FALSE;
3522 struct cil_stack *stack;
3523 int rc;
3524
3525 num_actual = cil_count_actual(node);
3526
3527 max = num_actual * CIL_DEGENERATE_INHERITANCE_GROWTH;
3528 if (max < CIL_DEGENERATE_INHERITANCE_MINIMUM) {
3529 max = CIL_DEGENERATE_INHERITANCE_MINIMUM;
3530 }
3531
3532 cil_stack_init(&stack);
3533 rc = cil_check_inheritances(node, max, &num_potential, stack, &loop);
3534 cil_stack_destroy(&stack);
3535
3536 return rc;
3537 }
3538
3539 static int __cil_resolve_ast_node(struct cil_tree_node *node, struct cil_args_resolve *args)
3540 {
3541 int rc = SEPOL_OK;
3542 struct cil_db *db = args->db;
3543 enum cil_pass pass = 0;
3544
3545 pass = args->pass;
3546 switch (pass) {
3547 case CIL_PASS_TIF:
3548 if (node->flavor == CIL_TUNABLEIF) {
3549 rc = cil_resolve_tunif(node, db);
3550 }
3551 break;
3552 case CIL_PASS_IN_BEFORE:
3553 if (node->flavor == CIL_IN) {
3554 // due to ordering issues, in statements are just gathered here and
3555 // resolved together in cil_resolve_in_list once all are found
3556 struct cil_in *in = node->data;
3557 if (in->is_after == CIL_FALSE) {
3558 cil_list_prepend(args->in_list_before, CIL_NODE, node);
3559 }
3560 }
3561 break;
3562 case CIL_PASS_BLKIN_LINK:
3563 if (node->flavor == CIL_BLOCKINHERIT) {
3564 rc = cil_resolve_blockinherit_link(node, db);
3565 }
3566 break;
3567 case CIL_PASS_BLKIN_COPY:
3568 if (node->flavor == CIL_BLOCK) {
3569 rc = cil_resolve_blockinherit_copy(node, db);
3570 }
3571 break;
3572 case CIL_PASS_BLKABS:
3573 if (node->flavor == CIL_BLOCKABSTRACT) {
3574 rc = cil_resolve_blockabstract(node, db, args->abstract_blocks);
3575 }
3576 break;
3577 case CIL_PASS_IN_AFTER:
3578 if (node->flavor == CIL_IN) {
3579 // due to ordering issues, in statements are just gathered here and
3580 // resolved together in cil_resolve_in_list once all are found
3581 struct cil_in *in = node->data;
3582 if (in->is_after == CIL_TRUE) {
3583 cil_list_prepend(args->in_list_after, CIL_NODE, node);
3584 }
3585 }
3586 break;
3587 case CIL_PASS_CALL1:
3588 if (node->flavor == CIL_CALL && args->macro == NULL) {
3589 rc = cil_resolve_call(node, db);
3590 }
3591 break;
3592 case CIL_PASS_CALL2:
3593 if (node->flavor == CIL_CALL && args->macro == NULL) {
3594 rc = cil_resolve_call_args(node, db);
3595 }
3596 break;
3597 case CIL_PASS_ALIAS1:
3598 switch (node->flavor) {
3599 case CIL_TYPEALIASACTUAL:
3600 rc = cil_resolve_aliasactual(node, db, CIL_TYPE, CIL_TYPEALIAS);
3601 break;
3602 case CIL_SENSALIASACTUAL:
3603 rc = cil_resolve_aliasactual(node, db, CIL_SENS, CIL_SENSALIAS);
3604 break;
3605 case CIL_CATALIASACTUAL:
3606 rc = cil_resolve_aliasactual(node, db, CIL_CAT, CIL_CATALIAS);
3607 break;
3608 default:
3609 break;
3610 }
3611 break;
3612 case CIL_PASS_ALIAS2:
3613 switch (node->flavor) {
3614 case CIL_TYPEALIAS:
3615 rc = cil_resolve_alias_to_actual(node, CIL_TYPE);
3616 break;
3617 case CIL_SENSALIAS:
3618 rc = cil_resolve_alias_to_actual(node, CIL_SENS);
3619 break;
3620 case CIL_CATALIAS:
3621 rc = cil_resolve_alias_to_actual(node, CIL_CAT);
3622 break;
3623 default:
3624 break;
3625 }
3626 break;
3627 case CIL_PASS_MISC1:
3628 switch (node->flavor) {
3629 case CIL_SIDORDER:
3630 rc = cil_resolve_sidorder(node, db, args->sidorder_lists);
3631 break;
3632 case CIL_CLASSORDER:
3633 rc = cil_resolve_classorder(node, db, args->classorder_lists, args->unordered_classorder_lists);
3634 break;
3635 case CIL_CATORDER:
3636 rc = cil_resolve_catorder(node, db, args->catorder_lists);
3637 break;
3638 case CIL_SENSITIVITYORDER:
3639 rc = cil_resolve_sensitivityorder(node, db, args->sensitivityorder_lists);
3640 break;
3641 case CIL_BOOLEANIF:
3642 rc = cil_resolve_boolif(node, db);
3643 break;
3644 default:
3645 break;
3646 }
3647 break;
3648 case CIL_PASS_MLS:
3649 switch (node->flavor) {
3650 case CIL_CATSET:
3651 rc = cil_resolve_catset(node, (struct cil_catset*)node->data, db);
3652 break;
3653 default:
3654 break;
3655 }
3656 break;
3657 case CIL_PASS_MISC2:
3658 switch (node->flavor) {
3659 case CIL_SENSCAT:
3660 rc = cil_resolve_senscat(node, db);
3661 break;
3662 case CIL_CLASSCOMMON:
3663 rc = cil_resolve_classcommon(node, db);
3664 break;
3665 default:
3666 break;
3667 }
3668 break;
3669 case CIL_PASS_MISC3:
3670 switch (node->flavor) {
3671 case CIL_TYPEATTRIBUTESET:
3672 rc = cil_resolve_typeattributeset(node, db);
3673 break;
3674 case CIL_EXPANDTYPEATTRIBUTE:
3675 rc = cil_resolve_expandtypeattribute(node, db);
3676 break;
3677 case CIL_TYPEBOUNDS:
3678 rc = cil_resolve_bounds(node, db, CIL_TYPE, CIL_TYPEATTRIBUTE);
3679 break;
3680 case CIL_TYPEPERMISSIVE:
3681 rc = cil_resolve_typepermissive(node, db);
3682 break;
3683 case CIL_TYPENEVERAUDIT:
3684 rc = cil_resolve_typeneveraudit(node, db);
3685 break;
3686 case CIL_NAMETYPETRANSITION:
3687 rc = cil_resolve_nametypetransition(node, db);
3688 break;
3689 case CIL_RANGETRANSITION:
3690 rc = cil_resolve_rangetransition(node, db);
3691 break;
3692 case CIL_CLASSPERMISSIONSET:
3693 rc = cil_resolve_classpermissionset(node, (struct cil_classpermissionset*)node->data, db);
3694 break;
3695 case CIL_CLASSMAPPING:
3696 rc = cil_resolve_classmapping(node, db);
3697 break;
3698 case CIL_AVRULE:
3699 case CIL_AVRULEX:
3700 rc = cil_resolve_avrule(node, db);
3701 break;
3702 case CIL_PERMISSIONX:
3703 rc = cil_resolve_permissionx(node, (struct cil_permissionx*)node->data, db);
3704 break;
3705 case CIL_DENY_RULE:
3706 rc = cil_resolve_deny_rule(node, db);
3707 break;
3708 case CIL_TYPE_RULE:
3709 rc = cil_resolve_type_rule(node, db);
3710 break;
3711 case CIL_USERROLE:
3712 rc = cil_resolve_userrole(node, db);
3713 break;
3714 case CIL_USERLEVEL:
3715 rc = cil_resolve_userlevel(node, db);
3716 break;
3717 case CIL_USERRANGE:
3718 rc = cil_resolve_userrange(node, db);
3719 break;
3720 case CIL_USERBOUNDS:
3721 rc = cil_resolve_bounds(node, db, CIL_USER, CIL_USERATTRIBUTE);
3722 break;
3723 case CIL_USERPREFIX:
3724 rc = cil_resolve_userprefix(node, db);
3725 break;
3726 case CIL_SELINUXUSER:
3727 case CIL_SELINUXUSERDEFAULT:
3728 rc = cil_resolve_selinuxuser(node, db);
3729 break;
3730 case CIL_ROLEATTRIBUTESET:
3731 rc = cil_resolve_roleattributeset(node, db);
3732 break;
3733 case CIL_ROLETYPE:
3734 rc = cil_resolve_roletype(node, db);
3735 break;
3736 case CIL_ROLETRANSITION:
3737 rc = cil_resolve_roletransition(node, db);
3738 break;
3739 case CIL_ROLEALLOW:
3740 rc = cil_resolve_roleallow(node, db);
3741 break;
3742 case CIL_ROLEBOUNDS:
3743 rc = cil_resolve_bounds(node, db, CIL_ROLE, CIL_ROLEATTRIBUTE);
3744 break;
3745 case CIL_LEVEL:
3746 rc = cil_resolve_level(node, (struct cil_level*)node->data, db);
3747 break;
3748 case CIL_LEVELRANGE:
3749 rc = cil_resolve_levelrange(node, (struct cil_levelrange*)node->data, db);
3750 break;
3751 case CIL_CONSTRAIN:
3752 rc = cil_resolve_constrain(node, db);
3753 break;
3754 case CIL_MLSCONSTRAIN:
3755 rc = cil_resolve_constrain(node, db);
3756 break;
3757 case CIL_VALIDATETRANS:
3758 case CIL_MLSVALIDATETRANS:
3759 rc = cil_resolve_validatetrans(node, db);
3760 break;
3761 case CIL_CONTEXT:
3762 rc = cil_resolve_context(node, (struct cil_context*)node->data, db);
3763 break;
3764 case CIL_FILECON:
3765 rc = cil_resolve_filecon(node, db);
3766 break;
3767 case CIL_IBPKEYCON:
3768 rc = cil_resolve_ibpkeycon(node, db);
3769 break;
3770 case CIL_PORTCON:
3771 rc = cil_resolve_portcon(node, db);
3772 break;
3773 case CIL_NODECON:
3774 rc = cil_resolve_nodecon(node, db);
3775 break;
3776 case CIL_GENFSCON:
3777 rc = cil_resolve_genfscon(node, db);
3778 break;
3779 case CIL_NETIFCON:
3780 rc = cil_resolve_netifcon(node, db);
3781 break;
3782 case CIL_IBENDPORTCON:
3783 rc = cil_resolve_ibendportcon(node, db);
3784 break;
3785 case CIL_PIRQCON:
3786 rc = cil_resolve_pirqcon(node, db);
3787 break;
3788 case CIL_IOMEMCON:
3789 rc = cil_resolve_iomemcon(node, db);
3790 break;
3791 case CIL_IOPORTCON:
3792 rc = cil_resolve_ioportcon(node, db);
3793 break;
3794 case CIL_PCIDEVICECON:
3795 rc = cil_resolve_pcidevicecon(node, db);
3796 break;
3797 case CIL_DEVICETREECON:
3798 rc = cil_resolve_devicetreecon(node, db);
3799 break;
3800 case CIL_FSUSE:
3801 rc = cil_resolve_fsuse(node, db);
3802 break;
3803 case CIL_SIDCONTEXT:
3804 rc = cil_resolve_sidcontext(node, db);
3805 break;
3806 case CIL_DEFAULTUSER:
3807 case CIL_DEFAULTROLE:
3808 case CIL_DEFAULTTYPE:
3809 rc = cil_resolve_default(node, db);
3810 break;
3811 case CIL_DEFAULTRANGE:
3812 rc = cil_resolve_defaultrange(node, db);
3813 break;
3814 case CIL_USERATTRIBUTESET:
3815 rc = cil_resolve_userattributeset(node, db);
3816 break;
3817 default:
3818 break;
3819 }
3820 break;
3821 default:
3822 break;
3823 }
3824
3825 return rc;
3826 }
3827
3828 static int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
3829 {
3830 int rc = SEPOL_OK;
3831 struct cil_args_resolve *args = extra_args;
3832 enum cil_pass pass = args->pass;
3833 struct cil_tree_node *block = args->block;
3834 struct cil_tree_node *macro = args->macro;
3835 struct cil_tree_node *optional = args->optional;
3836 struct cil_tree_node *boolif = args->boolif;
3837
3838 if (node == NULL) {
3839 goto exit;
3840 }
3841
3842 if (block != NULL) {
3843 if (node->flavor == CIL_CAT ||
3844 node->flavor == CIL_SENS) {
3845 cil_tree_log(node, CIL_ERR, "%s is not allowed in block", cil_node_to_string(node));
3846 rc = SEPOL_ERR;
3847 goto exit;
3848 }
3849 }
3850
3851 if (macro != NULL) {
3852 if (node->flavor == CIL_TUNABLE ||
3853 node->flavor == CIL_IN ||
3854 node->flavor == CIL_BLOCK ||
3855 node->flavor == CIL_BLOCKINHERIT ||
3856 node->flavor == CIL_BLOCKABSTRACT ||
3857 node->flavor == CIL_MACRO) {
3858 cil_tree_log(node, CIL_ERR, "%s is not allowed in macro", cil_node_to_string(node));
3859 rc = SEPOL_ERR;
3860 goto exit;
3861 }
3862 }
3863
3864 if (optional != NULL) {
3865 if (node->flavor == CIL_TUNABLE ||
3866 node->flavor == CIL_IN ||
3867 node->flavor == CIL_BLOCK ||
3868 node->flavor == CIL_BLOCKABSTRACT ||
3869 node->flavor == CIL_MACRO) {
3870 cil_tree_log(node, CIL_ERR, "%s is not allowed in optional", cil_node_to_string(node));
3871 rc = SEPOL_ERR;
3872 goto exit;
3873 }
3874 }
3875
3876 if (boolif != NULL) {
3877 if (node->flavor != CIL_TUNABLEIF &&
3878 node->flavor != CIL_CALL &&
3879 node->flavor != CIL_CONDBLOCK &&
3880 node->flavor != CIL_AVRULE &&
3881 node->flavor != CIL_TYPE_RULE &&
3882 node->flavor != CIL_NAMETYPETRANSITION &&
3883 node->flavor != CIL_SRC_INFO &&
3884 ((args->db->policy_version < POLICYDB_VERSION_COND_XPERMS) ||
3885 (node->flavor != CIL_AVRULEX))) {
3886 rc = SEPOL_ERR;
3887 } else if (node->flavor == CIL_AVRULE || node->flavor == CIL_AVRULEX) {
3888 struct cil_avrule *rule = node->data;
3889 if (rule->rule_kind == CIL_AVRULE_NEVERALLOW) {
3890 rc = SEPOL_ERR;
3891 }
3892 }
3893 if (rc == SEPOL_ERR) {
3894 if (((struct cil_booleanif*)boolif->data)->preserved_tunable) {
3895 cil_tree_log(node, CIL_ERR, "%s is not allowed in tunableif being treated as a booleanif", cil_node_to_string(node));
3896 } else {
3897 cil_tree_log(node, CIL_ERR, "%s is not allowed in booleanif", cil_node_to_string(node));
3898 }
3899 goto exit;
3900 }
3901 }
3902
3903 if (node->flavor == CIL_MACRO) {
3904 if (pass > CIL_PASS_IN_AFTER) {
3905 *finished = CIL_TREE_SKIP_HEAD;
3906 rc = SEPOL_OK;
3907 goto exit;
3908 }
3909 }
3910
3911 if (node->flavor == CIL_BLOCK && ((((struct cil_block*)node->data)->is_abstract == CIL_TRUE) && (pass > CIL_PASS_BLKABS))) {
3912 *finished = CIL_TREE_SKIP_HEAD;
3913 rc = SEPOL_OK;
3914 goto exit;
3915 }
3916
3917 rc = __cil_resolve_ast_node(node, args);
3918 if (rc == SEPOL_ENOENT) {
3919 if (optional == NULL) {
3920 cil_tree_log(node, CIL_ERR, "Failed to resolve %s statement", cil_node_to_string(node));
3921 } else {
3922 if (!args->disabled_optional) {
3923 args->disabled_optional = optional;
3924 }
3925 cil_tree_log(node, CIL_INFO, "Failed to resolve %s statement", cil_node_to_string(node));
3926 cil_tree_log(optional, CIL_INFO, "Disabling optional '%s'", DATUM(optional->data)->name);
3927 rc = SEPOL_OK;
3928 }
3929 goto exit;
3930 }
3931
3932 return rc;
3933
3934 exit:
3935 return rc;
3936 }
3937
3938 static int __cil_resolve_ast_first_child_helper(struct cil_tree_node *current, void *extra_args)
3939 {
3940 int rc = SEPOL_ERR;
3941 struct cil_args_resolve *args = extra_args;
3942 struct cil_tree_node *parent = NULL;
3943
3944 if (current == NULL || extra_args == NULL) {
3945 goto exit;
3946 }
3947
3948 parent = current->parent;
3949
3950 if (parent->flavor == CIL_BLOCK) {
3951 args->block = parent;
3952 } else if (parent->flavor == CIL_MACRO) {
3953 args->macro = parent;
3954 } else if (parent->flavor == CIL_OPTIONAL) {
3955 args->optional = parent;
3956 } else if (parent->flavor == CIL_BOOLEANIF) {
3957 args->boolif = parent;
3958 }
3959
3960 return SEPOL_OK;
3961
3962 exit:
3963 return rc;
3964
3965 }
3966
3967 static int __cil_resolve_ast_last_child_helper(struct cil_tree_node *current, void *extra_args)
3968 {
3969 int rc = SEPOL_ERR;
3970 struct cil_args_resolve *args = extra_args;
3971 struct cil_tree_node *parent = NULL;
3972
3973 if (current == NULL || extra_args == NULL) {
3974 goto exit;
3975 }
3976
3977 parent = current->parent;
3978
3979 if (parent->flavor == CIL_BLOCK) {
3980 struct cil_tree_node *n = parent->parent;
3981 args->block = NULL;
3982 while (n && n->flavor != CIL_ROOT) {
3983 if (n->flavor == CIL_BLOCK) {
3984 args->block = n;
3985 break;
3986 }
3987 n = n->parent;
3988 }
3989 } else if (parent->flavor == CIL_MACRO) {
3990 args->macro = NULL;
3991 } else if (parent->flavor == CIL_OPTIONAL) {
3992 struct cil_tree_node *n = parent->parent;
3993 if (args->disabled_optional == parent) {
3994 *(args->changed) = CIL_TRUE;
3995 cil_list_append(args->to_destroy, CIL_NODE, parent);
3996 args->disabled_optional = NULL;
3997 }
3998 args->optional = NULL;
3999 while (n && n->flavor != CIL_ROOT) {
4000 if (n->flavor == CIL_OPTIONAL) {
4001 args->optional = n;
4002 break;
4003 }
4004 n = n->parent;
4005 }
4006 } else if (parent->flavor == CIL_BOOLEANIF) {
4007 args->boolif = NULL;
4008 }
4009
4010 return SEPOL_OK;
4011
4012 exit:
4013 return rc;
4014 }
4015
4016 int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
4017 {
4018 int rc = SEPOL_ERR;
4019 struct cil_args_resolve extra_args;
4020 enum cil_pass pass = CIL_PASS_TIF;
4021 uint32_t changed = 0;
4022
4023 if (db == NULL || current == NULL) {
4024 return rc;
4025 }
4026
4027 extra_args.db = db;
4028 extra_args.pass = pass;
4029 extra_args.changed = &changed;
4030 extra_args.block = NULL;
4031 extra_args.macro = NULL;
4032 extra_args.optional = NULL;
4033 extra_args.disabled_optional = NULL;
4034 extra_args.boolif= NULL;
4035 extra_args.sidorder_lists = NULL;
4036 extra_args.classorder_lists = NULL;
4037 extra_args.unordered_classorder_lists = NULL;
4038 extra_args.catorder_lists = NULL;
4039 extra_args.sensitivityorder_lists = NULL;
4040 extra_args.in_list_before = NULL;
4041 extra_args.in_list_after = NULL;
4042 extra_args.abstract_blocks = NULL;
4043
4044 cil_list_init(&extra_args.to_destroy, CIL_NODE);
4045 cil_list_init(&extra_args.sidorder_lists, CIL_SIDORDER);
4046 cil_list_init(&extra_args.classorder_lists, CIL_CLASSORDER);
4047 cil_list_init(&extra_args.unordered_classorder_lists, CIL_CLASSORDER);
4048 cil_list_init(&extra_args.catorder_lists, CIL_CATORDER);
4049 cil_list_init(&extra_args.sensitivityorder_lists, CIL_SENSITIVITYORDER);
4050 cil_list_init(&extra_args.in_list_before, CIL_IN);
4051 cil_list_init(&extra_args.in_list_after, CIL_IN);
4052 cil_list_init(&extra_args.abstract_blocks, CIL_NODE);
4053
4054 for (pass = CIL_PASS_TIF; pass < CIL_PASS_NUM; pass++) {
4055 extra_args.pass = pass;
4056 rc = cil_tree_walk(current, __cil_resolve_ast_node_helper, __cil_resolve_ast_first_child_helper, __cil_resolve_ast_last_child_helper, &extra_args);
4057 if (rc != SEPOL_OK) {
4058 cil_log(CIL_INFO, "Pass %i of resolution failed\n", pass);
4059 goto exit;
4060 }
4061
4062 if (pass == CIL_PASS_IN_BEFORE) {
4063 rc = cil_resolve_in_list(extra_args.in_list_before, db);
4064 if (rc != SEPOL_OK) {
4065 goto exit;
4066 }
4067 cil_list_destroy(&extra_args.in_list_before, CIL_FALSE);
4068 } else if (pass == CIL_PASS_IN_AFTER) {
4069 rc = cil_resolve_in_list(extra_args.in_list_after, db);
4070 if (rc != SEPOL_OK) {
4071 goto exit;
4072 }
4073 cil_list_destroy(&extra_args.in_list_after, CIL_FALSE);
4074 }
4075
4076 if (pass == CIL_PASS_BLKABS) {
4077 struct cil_list_item *item;
4078 cil_list_for_each(item, extra_args.abstract_blocks) {
4079 cil_mark_subtree_abstract(item->data);
4080 }
4081 }
4082
4083 if (pass == CIL_PASS_BLKIN_LINK) {
4084 rc = cil_check_for_bad_inheritance(current);
4085 if (rc != SEPOL_OK) {
4086 rc = SEPOL_ERR;
4087 goto exit;
4088 }
4089 }
4090
4091 if (pass == CIL_PASS_MISC1) {
4092 db->sidorder = __cil_ordered_lists_merge_all(&extra_args.sidorder_lists, NULL);
4093 if (db->sidorder == NULL) {
4094 rc = SEPOL_ERR;
4095 goto exit;
4096 }
4097 db->classorder = __cil_ordered_lists_merge_all(&extra_args.classorder_lists, &extra_args.unordered_classorder_lists);
4098 if (db->classorder == NULL) {
4099 rc = SEPOL_ERR;
4100 goto exit;
4101 }
4102 db->catorder = __cil_ordered_lists_merge_all(&extra_args.catorder_lists, NULL);
4103 if (db->catorder == NULL) {
4104 rc = SEPOL_ERR;
4105 goto exit;
4106 }
4107 cil_set_cat_values(db->catorder, db);
4108 db->sensitivityorder = __cil_ordered_lists_merge_all(&extra_args.sensitivityorder_lists, NULL);
4109 if (db->sensitivityorder == NULL) {
4110 rc = SEPOL_ERR;
4111 goto exit;
4112 }
4113
4114 rc = __cil_verify_ordered(current, CIL_SID);
4115 if (rc != SEPOL_OK) {
4116 goto exit;
4117 }
4118
4119 rc = __cil_verify_ordered(current, CIL_CLASS);
4120 if (rc != SEPOL_OK) {
4121 goto exit;
4122 }
4123
4124 rc = __cil_verify_ordered(current, CIL_CAT);
4125 if (rc != SEPOL_OK) {
4126 goto exit;
4127 }
4128
4129 rc = __cil_verify_ordered(current, CIL_SENS);
4130 if (rc != SEPOL_OK) {
4131 goto exit;
4132 }
4133 }
4134
4135 if (changed) {
4136 struct cil_list_item *item;
4137 if (pass > CIL_PASS_CALL1) {
4138 int has_decls = CIL_FALSE;
4139
4140 cil_list_for_each(item, extra_args.to_destroy) {
4141 has_decls = cil_tree_subtree_has_decl(item->data);
4142 if (has_decls) {
4143 break;
4144 }
4145 }
4146
4147 if (has_decls) {
4148 /* Need to re-resolve because an optional was disabled that
4149 * contained one or more declarations.
4150 * Everything that needs to be reset comes after the
4151 * CIL_PASS_CALL2 pass. We set pass to CIL_PASS_CALL1 because
4152 * the pass++ will increment it to CIL_PASS_CALL2
4153 */
4154 cil_log(CIL_INFO, "Resetting declarations\n");
4155
4156 if (pass >= CIL_PASS_MISC1) {
4157 cil_list_destroy(&extra_args.sidorder_lists, CIL_FALSE);
4158 cil_list_destroy(&extra_args.classorder_lists, CIL_FALSE);
4159 cil_list_destroy(&extra_args.catorder_lists, CIL_FALSE);
4160 cil_list_destroy(&extra_args.sensitivityorder_lists, CIL_FALSE);
4161 cil_list_destroy(&extra_args.unordered_classorder_lists, CIL_FALSE);
4162 cil_list_init(&extra_args.sidorder_lists, CIL_SIDORDER);
4163 cil_list_init(&extra_args.classorder_lists, CIL_CLASSORDER);
4164 cil_list_init(&extra_args.unordered_classorder_lists, CIL_CLASSORDER);
4165 cil_list_init(&extra_args.catorder_lists, CIL_CATORDER);
4166 cil_list_init(&extra_args.sensitivityorder_lists, CIL_SENSITIVITYORDER);
4167 cil_list_destroy(&db->sidorder, CIL_FALSE);
4168 cil_list_destroy(&db->classorder, CIL_FALSE);
4169 cil_list_destroy(&db->catorder, CIL_FALSE);
4170 cil_list_destroy(&db->sensitivityorder, CIL_FALSE);
4171 }
4172
4173 pass = CIL_PASS_CALL1;
4174
4175 rc = cil_reset_ast(current);
4176 if (rc != SEPOL_OK) {
4177 cil_log(CIL_ERR, "Failed to reset declarations\n");
4178 goto exit;
4179 }
4180 }
4181 }
4182 cil_list_for_each(item, extra_args.to_destroy) {
4183 cil_tree_children_destroy(item->data);
4184 }
4185 cil_list_destroy(&extra_args.to_destroy, CIL_FALSE);
4186 cil_list_init(&extra_args.to_destroy, CIL_NODE);
4187 changed = 0;
4188 }
4189 }
4190
4191 rc = __cil_verify_initsids(db->sidorder);
4192 if (rc != SEPOL_OK) {
4193 goto exit;
4194 }
4195
4196 rc = SEPOL_OK;
4197 exit:
4198 cil_list_destroy(&extra_args.sidorder_lists, CIL_FALSE);
4199 cil_list_destroy(&extra_args.classorder_lists, CIL_FALSE);
4200 cil_list_destroy(&extra_args.catorder_lists, CIL_FALSE);
4201 cil_list_destroy(&extra_args.sensitivityorder_lists, CIL_FALSE);
4202 cil_list_destroy(&extra_args.unordered_classorder_lists, CIL_FALSE);
4203 cil_list_destroy(&extra_args.to_destroy, CIL_FALSE);
4204 cil_list_destroy(&extra_args.in_list_before, CIL_FALSE);
4205 cil_list_destroy(&extra_args.in_list_after, CIL_FALSE);
4206 cil_list_destroy(&extra_args.abstract_blocks, CIL_FALSE);
4207
4208 return rc;
4209 }
4210
4211 static int __cil_resolve_name_with_root(struct cil_db *db, char *name, enum cil_sym_index sym_index, struct cil_symtab_datum **datum)
4212 {
4213 symtab_t *symtab = &((struct cil_root *)db->ast->root->data)->symtab[sym_index];
4214
4215 return cil_symtab_get_datum(symtab, name, datum);
4216 }
4217
4218 static int __cil_resolve_name_with_parents(struct cil_tree_node *node, char *name, enum cil_sym_index sym_index, struct cil_symtab_datum **datum)
4219 {
4220 int rc = SEPOL_ERR;
4221 symtab_t *symtab = NULL;
4222
4223 while (node != NULL && rc != SEPOL_OK) {
4224 switch (node->flavor) {
4225 case CIL_ROOT:
4226 goto exit;
4227 break;
4228 case CIL_BLOCK: {
4229 struct cil_block *block = node->data;
4230 if (!block->is_abstract) {
4231 symtab = &block->symtab[sym_index];
4232 rc = cil_symtab_get_datum(symtab, name, datum);
4233 }
4234 }
4235 break;
4236 case CIL_BLOCKINHERIT: {
4237 struct cil_blockinherit *inherit = node->data;
4238 rc = __cil_resolve_name_with_parents(node->parent, name, sym_index, datum);
4239 if (rc != SEPOL_OK) {
4240 /* Continue search in original block's parent */
4241 rc = __cil_resolve_name_with_parents(NODE(inherit->block)->parent, name, sym_index, datum);
4242 goto exit;
4243 }
4244 }
4245 break;
4246 case CIL_MACRO: {
4247 struct cil_macro *macro = node->data;
4248 symtab = &macro->symtab[sym_index];
4249 rc = cil_symtab_get_datum(symtab, name, datum);
4250 }
4251 break;
4252 case CIL_CALL: {
4253 struct cil_call *call = node->data;
4254 struct cil_macro *macro = call->macro;
4255 symtab = &macro->symtab[sym_index];
4256 rc = cil_symtab_get_datum(symtab, name, datum);
4257 if (rc == SEPOL_OK) {
4258 /* If the name was declared in the macro, just look on the call side */
4259 rc = SEPOL_ERR;
4260 } else {
4261 rc = cil_resolve_name_call_args(call, name, sym_index, datum);
4262 if (rc != SEPOL_OK) {
4263 /* Continue search in macro's parent */
4264 rc = __cil_resolve_name_with_parents(NODE(call->macro)->parent, name, sym_index, datum);
4265 }
4266 }
4267 }
4268 break;
4269 case CIL_IN:
4270 /* In block symtabs only exist before resolving the AST */
4271 case CIL_CONDBLOCK:
4272 /* Cond block symtabs only exist before resolving the AST */
4273 default:
4274 break;
4275 }
4276
4277 node = node->parent;
4278 }
4279
4280 exit:
4281 return rc;
4282 }
4283
4284 static int __cil_resolve_name_helper(struct cil_db *db, struct cil_tree_node *node, char *name, enum cil_sym_index sym_index, struct cil_symtab_datum **datum)
4285 {
4286 int rc = SEPOL_ERR;
4287
4288 rc = __cil_resolve_name_with_parents(node, name, sym_index, datum);
4289 if (rc != SEPOL_OK) {
4290 rc = __cil_resolve_name_with_root(db, name, sym_index, datum);
4291 }
4292 return rc;
4293 }
4294
4295 int cil_resolve_name(struct cil_tree_node *ast_node, char *name, enum cil_sym_index sym_index, struct cil_db *db, struct cil_symtab_datum **datum)
4296 {
4297 int rc = SEPOL_ERR;
4298 struct cil_tree_node *node = NULL;
4299
4300 rc = cil_resolve_name_keep_aliases(ast_node, name, sym_index, db, datum);
4301 if (rc != SEPOL_OK) {
4302 goto exit;
4303 }
4304
4305 /* If this datum is an alias, then return the actual node
4306 * This depends on aliases already being processed
4307 */
4308 node = NODE(*datum);
4309 if (node->flavor == CIL_TYPEALIAS || node->flavor == CIL_SENSALIAS
4310 || node->flavor == CIL_CATALIAS) {
4311 struct cil_alias *alias = (struct cil_alias *)(*datum);
4312 if (alias->actual) {
4313 *datum = alias->actual;
4314 }
4315 }
4316
4317 rc = SEPOL_OK;
4318
4319 exit:
4320 return rc;
4321 }
4322
4323 int cil_resolve_name_keep_aliases(struct cil_tree_node *ast_node, char *name, enum cil_sym_index sym_index, struct cil_db *db, struct cil_symtab_datum **datum)
4324 {
4325 int rc = SEPOL_ERR;
4326 struct cil_tree_node *node = NULL;
4327
4328 if (name == NULL || sym_index >= CIL_SYM_NUM) {
4329 cil_log(CIL_ERR, "Invalid call to cil_resolve_name\n");
4330 goto exit;
4331 }
4332
4333 *datum = NULL;
4334
4335 if (db->qualified_names || strchr(name,'.') == NULL) {
4336 /* Using qualified names or No '.' in name */
4337 rc = __cil_resolve_name_helper(db, ast_node->parent, name, sym_index, datum);
4338 if (rc != SEPOL_OK) {
4339 goto exit;
4340 }
4341 } else {
4342 char *sp = NULL;
4343 char *name_dup = cil_strdup(name);
4344 char *current = strtok_r(name_dup, ".", &sp);
4345 char *next = strtok_r(NULL, ".", &sp);
4346 symtab_t *symtab = NULL;
4347
4348 if (current == NULL) {
4349 /* Only dots */
4350 cil_tree_log(ast_node, CIL_ERR, "Invalid name %s", name);
4351 free(name_dup);
4352 goto exit;
4353 }
4354
4355 node = ast_node;
4356 if (*name == '.') {
4357 /* Leading '.' */
4358 symtab = &((struct cil_root *)db->ast->root->data)->symtab[CIL_SYM_BLOCKS];
4359 } else {
4360 rc = __cil_resolve_name_helper(db, node->parent, current, CIL_SYM_BLOCKS, datum);
4361 if (rc != SEPOL_OK) {
4362 free(name_dup);
4363 goto exit;
4364 }
4365 symtab = (*datum)->symtab;
4366 }
4367 /* Keep looking up blocks by name until only last part of name remains */
4368 while (next != NULL) {
4369 rc = cil_symtab_get_datum(symtab, current, datum);
4370 if (rc != SEPOL_OK) {
4371 free(name_dup);
4372 goto exit;
4373 }
4374 node = NODE(*datum);
4375 if (node->flavor == CIL_BLOCK) {
4376 symtab = &((struct cil_block*)node->data)->symtab[CIL_SYM_BLOCKS];
4377 } else {
4378 if (ast_node->flavor != CIL_IN) {
4379 cil_log(CIL_WARN, "Can only use %s name for name resolution in \"in\" blocks\n", cil_node_to_string(node));
4380 free(name_dup);
4381 rc = SEPOL_ERR;
4382 goto exit;
4383 }
4384 if (node->flavor == CIL_MACRO) {
4385 struct cil_macro *macro = node->data;
4386 symtab = &macro->symtab[sym_index];
4387 }
4388 }
4389 current = next;
4390 next = strtok_r(NULL, ".", &sp);
4391 }
4392 symtab = &(symtab[sym_index]);
4393 rc = cil_symtab_get_datum(symtab, current, datum);
4394 free(name_dup);
4395 if (rc != SEPOL_OK) {
4396 goto exit;
4397 }
4398 }
4399
4400 rc = SEPOL_OK;
4401
4402 exit:
4403 if (rc != SEPOL_OK) {
4404 *datum = NULL;
4405 }
4406
4407 return rc;
4408 }