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