Debian Codegraph @

cil/src/cil_verify.c

raw
1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <unistd.h>
35 #include <ctype.h>
36
37 #include <sepol/policydb/polcaps.h>
38 #include <sepol/errcodes.h>
39
40 #include "cil_internal.h"
41 #include "cil_flavor.h"
42 #include "cil_log.h"
43 #include "cil_mem.h"
44 #include "cil_tree.h"
45 #include "cil_list.h"
46 #include "cil_find.h"
47 #include "cil_stack.h"
48
49 #include "cil_verify.h"
50
51 static int __cil_is_reserved_name(const char *name, enum cil_flavor flavor)
52 {
53 switch (flavor) {
54 case CIL_BOOL:
55 case CIL_TUNABLE:
56 if ((name == CIL_KEY_EQ) || (name == CIL_KEY_NEQ))
57 return CIL_TRUE;
58 break;
59 case CIL_PERM:
60 case CIL_MAP_PERM:
61 case CIL_USER:
62 case CIL_USERATTRIBUTE:
63 case CIL_ROLE:
64 case CIL_ROLEATTRIBUTE:
65 if (name == CIL_KEY_ALL)
66 return CIL_TRUE;
67 break;
68 case CIL_TYPE:
69 case CIL_TYPEATTRIBUTE:
70 case CIL_TYPEALIAS:
71 if ((name == CIL_KEY_ALL) || (name == CIL_KEY_SELF) || (name == CIL_KEY_NOTSELF)
72 || (name == CIL_KEY_OTHER))
73 return CIL_TRUE;
74 break;
75 case CIL_CAT:
76 case CIL_CATSET:
77 case CIL_CATALIAS:
78 case CIL_PERMISSIONX:
79 if ((name == CIL_KEY_ALL) || (name == CIL_KEY_RANGE))
80 return CIL_TRUE;
81 break;
82 default:
83 /* All of these are not used in expressions */
84 return CIL_FALSE;
85 break;
86 }
87
88 /* Everything not under the default case is also checked for these */
89 if ((name == CIL_KEY_AND) || (name == CIL_KEY_OR) || (name == CIL_KEY_NOT) || (name == CIL_KEY_XOR)) {
90 return CIL_TRUE;
91 }
92
93 return CIL_FALSE;
94 }
95
96 int cil_verify_name(const struct cil_db *db, const char *name, enum cil_flavor flavor)
97 {
98 int rc = SEPOL_ERR;
99 int len;
100 int i = 0;
101
102 if (name == NULL) {
103 cil_log(CIL_ERR, "Name is NULL\n");
104 goto exit;
105 }
106
107 len = strlen(name);
108 if (len >= CIL_MAX_NAME_LENGTH) {
109 cil_log(CIL_ERR, "Name length greater than max name length of %d",
110 CIL_MAX_NAME_LENGTH);
111 rc = SEPOL_ERR;
112 goto exit;
113 }
114
115 if (!isalpha(name[0])) {
116 cil_log(CIL_ERR, "First character in %s is not a letter\n", name);
117 goto exit;
118 }
119
120 if (db->qualified_names == CIL_FALSE) {
121 for (i = 1; i < len; i++) {
122 if (!isalnum(name[i]) && name[i] != '_' && name[i] != '-') {
123 cil_log(CIL_ERR, "Invalid character \"%c\" in %s\n", name[i], name);
124 goto exit;
125 }
126 }
127 } else {
128 for (i = 1; i < len; i++) {
129 if (!isalnum(name[i]) && name[i] != '_' && name[i] != '-' && name[i] != '.') {
130 cil_log(CIL_ERR, "Invalid character \"%c\" in %s\n", name[i], name);
131 goto exit;
132 }
133 }
134 }
135
136 if (__cil_is_reserved_name(name, flavor)) {
137 cil_log(CIL_ERR, "Name %s is a reserved word\n", name);
138 goto exit;
139 }
140
141 return SEPOL_OK;
142
143 exit:
144 cil_log(CIL_ERR, "Invalid name\n");
145 return rc;
146 }
147
148 int __cil_verify_syntax(struct cil_tree_node *parse_current, enum cil_syntax s[], size_t len)
149 {
150 struct cil_tree_node *c = parse_current;
151 size_t i = 0;
152
153 while (i < len && c != NULL) {
154 if ((s[i] & CIL_SYN_STRING) && c->data != NULL && c->cl_head == NULL) {
155 c = c->next;
156 i++;
157 } else if ((s[i] & CIL_SYN_LIST) && c->data == NULL && c->cl_head != NULL) {
158 c = c->next;
159 i++;
160 } else if ((s[i] & CIL_SYN_EMPTY_LIST) && c->data == NULL && c->cl_head == NULL) {
161 c = c->next;
162 i++;
163 } else if ((s[i] & CIL_SYN_N_LISTS) || (s[i] & CIL_SYN_N_STRINGS)) {
164 while (c != NULL) {
165 if ((s[i] & CIL_SYN_N_LISTS) && c->data == NULL && c->cl_head != NULL) {
166 c = c->next;
167 } else if ((s[i] & CIL_SYN_N_STRINGS) && c->data != NULL && c->cl_head == NULL) {
168 c = c->next;
169 } else {
170 goto exit;
171 }
172 }
173 i++;
174 break; /* Only CIL_SYN_END allowed after these */
175 } else {
176 goto exit;
177 }
178 }
179
180 if (i < len && (s[i] & CIL_SYN_END) && c == NULL) {
181 return SEPOL_OK;
182 }
183
184 exit:
185 cil_log(CIL_ERR, "Invalid syntax\n");
186 return SEPOL_ERR;
187 }
188
189 int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, enum cil_flavor expr_flavor)
190 {
191 int rc;
192 enum cil_syntax syntax[] = {
193 CIL_SYN_STRING,
194 CIL_SYN_STRING | CIL_SYN_LIST,
195 CIL_SYN_STRING | CIL_SYN_LIST,
196 CIL_SYN_END
197 };
198 int syntax_len = sizeof(syntax)/sizeof(*syntax);
199
200 switch (op) {
201 case CIL_NOT:
202 syntax[2] = CIL_SYN_END;
203 syntax_len = 3;
204 break;
205 case CIL_AND:
206 case CIL_OR:
207 case CIL_XOR:
208 break;
209 case CIL_EQ:
210 case CIL_NEQ:
211 if (expr_flavor != CIL_BOOL && expr_flavor != CIL_TUNABLE ) {
212 cil_log(CIL_ERR,"Invalid operator (%s) for set expression\n", (char*)current->data);
213 goto exit;
214 }
215 break;
216 case CIL_ALL:
217 if (expr_flavor == CIL_BOOL || expr_flavor == CIL_TUNABLE) {
218 cil_log(CIL_ERR,"Invalid operator (%s) for boolean or tunable expression\n", (char*)current->data);
219 goto exit;
220 }
221 syntax[1] = CIL_SYN_END;
222 syntax_len = 2;
223 break;
224 case CIL_RANGE:
225 if (expr_flavor != CIL_CAT && expr_flavor != CIL_PERMISSIONX) {
226 cil_log(CIL_ERR,"Operator (%s) only valid for catset and permissionx expression\n", (char*)current->data);
227 goto exit;
228 }
229 syntax[1] = CIL_SYN_STRING;
230 syntax[2] = CIL_SYN_STRING;
231 break;
232 case CIL_NONE: /* String or List */
233 syntax[0] = CIL_SYN_N_STRINGS | CIL_SYN_N_LISTS;
234 syntax[1] = CIL_SYN_END;
235 syntax_len = 2;
236 break;
237 default:
238 cil_log(CIL_ERR,"Unexpected value (%s) for expression operator\n", (char*)current->data);
239 goto exit;
240 }
241
242 rc = __cil_verify_syntax(current, syntax, syntax_len);
243 if (rc != SEPOL_OK) {
244 goto exit;
245 }
246
247 return SEPOL_OK;
248
249 exit:
250 return SEPOL_ERR;
251 }
252
253 int cil_verify_constraint_leaf_expr_syntax(enum cil_flavor l_flavor, enum cil_flavor r_flavor, enum cil_flavor op, enum cil_flavor expr_flavor)
254 {
255 if (r_flavor == CIL_STRING || r_flavor == CIL_LIST) {
256 if (l_flavor == CIL_CONS_L1 || l_flavor == CIL_CONS_L2 || l_flavor == CIL_CONS_H1 || l_flavor == CIL_CONS_H2 ) {
257 cil_log(CIL_ERR, "l1, l2, h1, and h2 cannot be used on the left side with a string or list on the right side\n");
258 goto exit;
259 } else if (l_flavor == CIL_CONS_U3 || l_flavor == CIL_CONS_R3 || l_flavor == CIL_CONS_T3) {
260 if (expr_flavor != CIL_VALIDATETRANS && expr_flavor != CIL_MLSVALIDATETRANS) {
261 cil_log(CIL_ERR, "u3, r3, and t3 can only be used with (mls)validatetrans rules\n");
262 goto exit;
263 }
264 }
265 } else {
266 if (r_flavor == CIL_CONS_U1 || r_flavor == CIL_CONS_R1 || r_flavor == CIL_CONS_T1) {
267 cil_log(CIL_ERR, "u1, r1, and t1 are not allowed on the right side\n");
268 goto exit;
269 } else if (r_flavor == CIL_CONS_U3 || r_flavor == CIL_CONS_R3 || r_flavor == CIL_CONS_T3) {
270 cil_log(CIL_ERR, "u3, r3, and t3 are not allowed on the right side\n");
271 goto exit;
272 } else if (r_flavor == CIL_CONS_U2) {
273 if (op != CIL_EQ && op != CIL_NEQ) {
274 cil_log(CIL_ERR, "u2 on the right side must be used with eq or neq as the operator\n");
275 goto exit;
276 } else if (l_flavor != CIL_CONS_U1) {
277 cil_log(CIL_ERR, "u2 on the right side must be used with u1 on the left\n");
278 goto exit;
279 }
280 } else if (r_flavor == CIL_CONS_R2) {
281 if (l_flavor != CIL_CONS_R1) {
282 cil_log(CIL_ERR, "r2 on the right side must be used with r1 on the left\n");
283 goto exit;
284 }
285 } else if (r_flavor == CIL_CONS_T2) {
286 if (op != CIL_EQ && op != CIL_NEQ) {
287 cil_log(CIL_ERR, "t2 on the right side must be used with eq or neq as the operator\n");
288 goto exit;
289 } else if (l_flavor != CIL_CONS_T1) {
290 cil_log(CIL_ERR, "t2 on the right side must be used with t1 on the left\n");
291 goto exit;
292 }
293 } else if (r_flavor == CIL_CONS_L2) {
294 if (l_flavor != CIL_CONS_L1 && l_flavor != CIL_CONS_H1) {
295 cil_log(CIL_ERR, "l2 on the right side must be used with l1 or h1 on the left\n");
296 goto exit;
297 }
298 } else if (r_flavor == CIL_CONS_H2) {
299 if (l_flavor != CIL_CONS_L1 && l_flavor != CIL_CONS_L2 && l_flavor != CIL_CONS_H1 ) {
300 cil_log(CIL_ERR, "h2 on the right side must be used with l1, l2, or h1 on the left\n");
301 goto exit;
302 }
303 } else if (r_flavor == CIL_CONS_H1) {
304 if (l_flavor != CIL_CONS_L1) {
305 cil_log(CIL_ERR, "h1 on the right side must be used with l1 on the left\n");
306 goto exit;
307 }
308 }
309 }
310
311 return SEPOL_OK;
312
313 exit:
314 return SEPOL_ERR;
315 }
316
317 int cil_verify_constraint_expr_syntax(struct cil_tree_node *current, enum cil_flavor op)
318 {
319 int rc;
320 enum cil_syntax syntax[] = {
321 CIL_SYN_STRING,
322 CIL_SYN_END,
323 CIL_SYN_END,
324 CIL_SYN_END
325 };
326 int syntax_len = sizeof(syntax)/sizeof(*syntax);
327
328 switch (op) {
329 case CIL_NOT:
330 syntax[1] = CIL_SYN_LIST;
331 syntax_len--;
332 break;
333 case CIL_AND:
334 case CIL_OR:
335 syntax[1] = CIL_SYN_LIST;
336 syntax[2] = CIL_SYN_LIST;
337 break;
338 case CIL_EQ:
339 case CIL_NEQ:
340 syntax[1] = CIL_SYN_STRING;
341 syntax[2] = CIL_SYN_STRING | CIL_SYN_LIST;
342 break;
343 case CIL_CONS_DOM:
344 case CIL_CONS_DOMBY:
345 case CIL_CONS_INCOMP:
346 syntax[1] = CIL_SYN_STRING;
347 syntax[2] = CIL_SYN_STRING;
348 break;
349 default:
350 cil_log(CIL_ERR, "Invalid operator (%s) for constraint expression\n", (char*)current->data);
351 goto exit;
352 }
353
354 rc = __cil_verify_syntax(current, syntax, syntax_len);
355 if (rc != SEPOL_OK) {
356 cil_log(CIL_ERR, "Invalid constraint syntax\n");
357 goto exit;
358 }
359
360 return SEPOL_OK;
361
362 exit:
363 return SEPOL_ERR;
364 }
365
366 int cil_verify_conditional_blocks(struct cil_tree_node *current)
367 {
368 int found_true = CIL_FALSE;
369 int found_false = CIL_FALSE;
370
371 if (current->cl_head->data == CIL_KEY_CONDTRUE) {
372 found_true = CIL_TRUE;
373 } else if (current->cl_head->data == CIL_KEY_CONDFALSE) {
374 found_false = CIL_TRUE;
375 } else {
376 cil_tree_log(current, CIL_ERR, "Expected true or false block in conditional");
377 return SEPOL_ERR;
378 }
379
380 current = current->next;
381 if (current != NULL) {
382 if (current->cl_head->data == CIL_KEY_CONDTRUE) {
383 if (found_true) {
384 cil_tree_log(current, CIL_ERR, "More than one true block in conditional");
385 return SEPOL_ERR;
386 }
387 } else if (current->cl_head->data == CIL_KEY_CONDFALSE) {
388 if (found_false) {
389 cil_tree_log(current, CIL_ERR, "More than one false block in conditional");
390 return SEPOL_ERR;
391 }
392 } else {
393 cil_tree_log(current, CIL_ERR, "Expected true or false block in conditional");
394 return SEPOL_ERR;
395 }
396 }
397
398 return SEPOL_OK;
399 }
400
401 int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, struct cil_tree_node *node, const char *name)
402 {
403 struct cil_list_item *item;
404 struct cil_list *param_list = macro->params;
405 if (param_list != NULL) {
406 cil_list_for_each(item, param_list) {
407 struct cil_param *param = item->data;
408 if (param->str == name) {
409 if (param->flavor == node->flavor) {
410 cil_log(CIL_ERR, "Declaration of %s %s shadows a macro parameter with the same flavor\n", cil_node_to_string(node), name);
411 return SEPOL_ERR;
412 } else {
413 cil_log(CIL_WARN, "Declaration of %s %s has same name as a macro parameter with a different flavor\n", cil_node_to_string(node), name);
414 }
415 }
416 }
417 }
418 return SEPOL_OK;
419 }
420
421 static int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_stack *stack);
422
423 static int __verify_no_self_reference_in_expr(struct cil_list *expr, struct cil_stack *stack)
424 {
425 struct cil_list_item *item;
426 int rc = SEPOL_OK;
427
428 if (!expr) {
429 return SEPOL_OK;
430 }
431
432 cil_list_for_each(item, expr) {
433 if (item->flavor == CIL_DATUM) {
434 struct cil_symtab_datum* datum = item->data;
435 rc = cil_verify_no_self_reference(FLAVOR(datum), datum, stack);
436 } else if (item->flavor == CIL_LIST) {
437 rc = __verify_no_self_reference_in_expr(item->data, stack);
438 }
439 if (rc != SEPOL_OK) {
440 return SEPOL_ERR;
441 }
442 }
443
444 return SEPOL_OK;
445 }
446
447 static int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_stack *stack)
448 {
449 struct cil_stack_item *item;
450 int i = 0;
451 int rc = SEPOL_OK;
452
453 cil_stack_for_each(stack, i, item) {
454 struct cil_symtab_datum *prev = item->data;
455 if (datum == prev) {
456 cil_tree_log(NODE(datum), CIL_ERR, "Self-reference found for %s", datum->name);
457 return SEPOL_ERR;
458 }
459 }
460
461 switch (flavor) {
462 case CIL_USERATTRIBUTE: {
463 struct cil_userattribute *attr = (struct cil_userattribute *)datum;
464 cil_stack_push(stack, CIL_DATUM, datum);
465 rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
466 cil_stack_pop(stack);
467 break;
468 }
469 case CIL_ROLEATTRIBUTE: {
470 struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
471 cil_stack_push(stack, CIL_DATUM, datum);
472 rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
473 cil_stack_pop(stack);
474 break;
475 }
476 case CIL_TYPEATTRIBUTE: {
477 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
478 cil_stack_push(stack, CIL_DATUM, datum);
479 rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
480 cil_stack_pop(stack);
481 break;
482 }
483 case CIL_CATSET: {
484 struct cil_catset *set = (struct cil_catset *)datum;
485 cil_stack_push(stack, CIL_DATUM, datum);
486 rc = __verify_no_self_reference_in_expr(set->cats->datum_expr, stack);
487 cil_stack_pop(stack);
488 break;
489 }
490 default:
491 break;
492 }
493
494 return rc;
495 }
496
497 int __cil_verify_ranges(struct cil_list *list)
498 {
499 int rc = SEPOL_ERR;
500 struct cil_list_item *curr;
501 struct cil_list_item *range = NULL;
502
503 if (list == NULL || list->head == NULL) {
504 goto exit;
505 }
506
507 cil_list_for_each(curr, list) {
508 /* range */
509 if (curr->flavor == CIL_LIST) {
510 range = ((struct cil_list*)curr->data)->head;
511 if (range == NULL || range->next == NULL || range->next->next != NULL) {
512 goto exit;
513 }
514 }
515 }
516
517 return SEPOL_OK;
518
519 exit:
520 cil_log(CIL_ERR,"Invalid Range syntax\n");
521 return rc;
522 }
523
524 int cil_verify_completed_ordered_list(struct cil_list *complete, struct cil_list *ordered_lists)
525 {
526 struct cil_list_item *cprev, *ccurr, *cnext;
527 int found_prev, found_next;
528 int rc = SEPOL_OK;
529
530 found_prev = CIL_FALSE;
531 found_next = CIL_FALSE;
532 cprev = NULL;
533 ccurr = complete->head;
534 cnext = ccurr ? ccurr->next : NULL;
535 while (ccurr) {
536 struct cil_tree_node *node;
537 struct cil_ordered *ordered;
538 struct cil_list_item *curr_list, *oprev, *ocurr, *onext;
539 int change = CIL_FALSE;
540 cil_list_for_each(curr_list, ordered_lists) {
541 node = curr_list->data;
542 ordered = node->data;
543 oprev = NULL;
544 cil_list_for_each(ocurr, ordered->datums) {
545 onext = ocurr ? ocurr->next : NULL;
546 if (ccurr->data == ocurr->data) {
547 if (found_prev == CIL_FALSE && ((!cprev && !oprev) ||
548 (cprev && oprev && cprev->data == oprev->data))) {
549 found_prev = CIL_TRUE;
550 change = CIL_TRUE;
551 }
552 if (found_next == CIL_FALSE && ((!cnext && !onext) ||
553 (cnext && onext && cnext->data == onext->data))) {
554 found_next = CIL_TRUE;
555 change = CIL_TRUE;
556 }
557 if (found_prev && found_next) {
558 cprev = ccurr;
559 ccurr = cnext;
560 cnext = ccurr ? ccurr->next : NULL;
561 found_prev = CIL_FALSE;
562 found_next = CIL_FALSE;
563 if (!ccurr) {
564 /* Went through the whole list */
565 return rc;
566 }
567 }
568 }
569 oprev = ocurr;
570 }
571 }
572 if (!change) {
573 rc = SEPOL_ERR;
574 cil_log(CIL_ERR, "Unable to verify the order of %s\n", DATUM(ccurr->data)->fqn);
575 cil_log(CIL_ERR, "Found in the following ordering rules:\n");
576 cil_list_for_each(curr_list, ordered_lists) {
577 node = curr_list->data;
578 ordered = node->data;
579 cil_list_for_each(ocurr, ordered->datums) {
580 if (ccurr->data == ocurr->data) {
581 cil_tree_log(node, CIL_ERR, " ");
582 }
583 }
584 }
585 cprev = ccurr;
586 ccurr = cnext;
587 cnext = ccurr ? ccurr->next : NULL;
588 found_prev = CIL_FALSE;
589 found_next = CIL_FALSE;
590 }
591 }
592
593 return rc;
594 }
595
596 struct cil_args_verify_order {
597 uint32_t *flavor;
598 };
599
600 int __cil_verify_ordered_node_helper(struct cil_tree_node *node, __attribute__((unused)) uint32_t *finished, void *extra_args)
601 {
602 struct cil_args_verify_order *args = extra_args;
603 uint32_t *flavor = args->flavor;
604
605 if (node->flavor == *flavor) {
606 if (node->flavor == CIL_SID) {
607 struct cil_sid *sid = node->data;
608 if (sid->ordered == CIL_FALSE) {
609 cil_tree_log(node, CIL_ERR, "SID %s not in sidorder statement", sid->datum.name);
610 return SEPOL_ERR;
611 }
612 } else if (node->flavor == CIL_CLASS) {
613 struct cil_class *class = node->data;
614 if (class->ordered == CIL_FALSE) {
615 cil_tree_log(node, CIL_ERR, "Class %s not in classorder statement", class->datum.name);
616 return SEPOL_ERR;
617 }
618 } else if (node->flavor == CIL_CAT) {
619 struct cil_cat *cat = node->data;
620 if (cat->ordered == CIL_FALSE) {
621 cil_tree_log(node, CIL_ERR, "Category %s not in categoryorder statement", cat->datum.name);
622 return SEPOL_ERR;
623 }
624 } else if (node->flavor == CIL_SENS) {
625 struct cil_sens *sens = node->data;
626 if (sens->ordered == CIL_FALSE) {
627 cil_tree_log(node, CIL_ERR, "Sensitivity %s not in sensitivityorder statement", sens->datum.name);
628 return SEPOL_ERR;
629 }
630 }
631 }
632
633 return SEPOL_OK;
634 }
635
636 int __cil_verify_ordered(struct cil_tree_node *current, enum cil_flavor flavor)
637 {
638 struct cil_args_verify_order extra_args;
639 int rc = SEPOL_ERR;
640
641 extra_args.flavor = &flavor;
642
643 rc = cil_tree_walk(current, __cil_verify_ordered_node_helper, NULL, NULL, &extra_args);
644
645 return rc;
646 }
647
648 int __cil_verify_initsids(struct cil_list *sids)
649 {
650 int rc = SEPOL_OK;
651 struct cil_list_item *i;
652
653 if (sids->head == NULL) {
654 cil_log(CIL_ERR, "At least one initial sid must be defined in the policy\n");
655 return SEPOL_ERR;
656 }
657
658 cil_list_for_each(i, sids) {
659 struct cil_sid *sid = i->data;
660 if (sid->context == NULL) {
661 struct cil_tree_node *node = sid->datum.nodes->head->data;
662 cil_tree_log(node, CIL_INFO, "No context assigned to SID %s, omitting from policy",sid->datum.name);
663 }
664 }
665
666 return rc;
667 }
668
669 static int __cil_is_cat_in_cats(struct cil_cat *cat, struct cil_cats *cats)
670 {
671 struct cil_list_item *i;
672
673 cil_list_for_each(i, cats->datum_expr) {
674 struct cil_cat *c = i->data;
675 if (c == cat) {
676 return CIL_TRUE;
677 }
678 }
679
680 return CIL_FALSE;
681 }
682
683
684 static int __cil_verify_cat_in_cats(struct cil_cat *cat, struct cil_cats *cats)
685 {
686 if (__cil_is_cat_in_cats(cat, cats) != CIL_TRUE) {
687 cil_log(CIL_ERR, "Failed to find category %s in category list\n", cat->datum.name);
688 return SEPOL_ERR;
689 }
690
691 return SEPOL_OK;
692 }
693
694 static int __cil_verify_cats_associated_with_sens(struct cil_sens *sens, struct cil_cats *cats)
695 {
696 int rc = SEPOL_OK;
697 struct cil_list_item *i, *j;
698
699 if (!cats) {
700 return SEPOL_OK;
701 }
702
703 if (!sens->cats_list) {
704 cil_log(CIL_ERR, "No categories can be used with sensitivity %s\n", sens->datum.name);
705 return SEPOL_ERR;
706 }
707
708 cil_list_for_each(i, cats->datum_expr) {
709 struct cil_cat *cat = i->data;
710 int ok = CIL_FALSE;
711 cil_list_for_each(j, sens->cats_list) {
712 if (__cil_is_cat_in_cats(cat, j->data) == CIL_TRUE) {
713 ok = CIL_TRUE;
714 break;
715 }
716 }
717
718 if (ok != CIL_TRUE) {
719 cil_log(CIL_ERR, "Category %s cannot be used with sensitivity %s\n",
720 cat->datum.name, sens->datum.name);
721 rc = SEPOL_ERR;
722 }
723 }
724
725 return rc;
726 }
727
728 static int __cil_verify_levelrange_sensitivity(struct cil_db *db, struct cil_sens *low, struct cil_sens *high)
729 {
730 struct cil_list_item *curr;
731 int found = CIL_FALSE;
732 int rc = SEPOL_ERR;
733
734 cil_list_for_each(curr, db->sensitivityorder) {
735 if (curr->data == low) {
736 found = CIL_TRUE;
737 }
738
739 if ((found == CIL_TRUE) && (curr->data == high)) {
740 break;
741 }
742 }
743
744 if (found != CIL_TRUE || curr == NULL) {
745 goto exit;
746 }
747
748 return SEPOL_OK;
749
750 exit:
751 cil_log(CIL_ERR, "Sensitivity %s does not dominate %s\n",
752 high->datum.name, low->datum.name);
753 return rc;
754
755 }
756
757 static int __cil_verify_levelrange_cats(struct cil_cats *low, struct cil_cats *high)
758 {
759 int rc = SEPOL_ERR;
760 struct cil_list_item *item;
761
762 if (low == NULL || (low == NULL && high == NULL)) {
763 return SEPOL_OK;
764 }
765
766 if (high == NULL) {
767 rc = SEPOL_ERR;
768 goto exit;
769 }
770
771 cil_list_for_each(item, low->datum_expr) {
772 rc = __cil_verify_cat_in_cats(item->data, high);
773 if (rc != SEPOL_OK) {
774 goto exit;
775 }
776 }
777
778 return SEPOL_OK;
779
780 exit:
781 cil_log(CIL_ERR, "Low level category set must be a subset of the high level category set\n");
782 return rc;
783 }
784
785 static int __cil_verify_levelrange(struct cil_db *db, struct cil_levelrange *lr)
786 {
787 int rc = SEPOL_ERR;
788
789 rc = __cil_verify_levelrange_sensitivity(db, lr->low->sens, lr->high->sens);
790 if (rc != SEPOL_OK) {
791 goto exit;
792 }
793
794 rc = __cil_verify_levelrange_cats(lr->low->cats, lr->high->cats);
795 if (rc != SEPOL_OK) {
796 goto exit;
797 }
798
799 rc = __cil_verify_cats_associated_with_sens(lr->low->sens, lr->low->cats);
800 if (rc != SEPOL_OK) {
801 cil_log(CIL_ERR, "Low level sensitivity and categories are not associated\n");
802 goto exit;
803 }
804
805 rc = __cil_verify_cats_associated_with_sens(lr->high->sens, lr->high->cats);
806 if (rc != SEPOL_OK) {
807 cil_log(CIL_ERR, "High level sensitivity and categories are not associated\n");
808 goto exit;
809 }
810
811 return SEPOL_OK;
812
813 exit:
814 return rc;
815 }
816
817 static int __cil_verify_named_levelrange(struct cil_db *db, struct cil_tree_node *node)
818 {
819 int rc = SEPOL_ERR;
820 struct cil_levelrange *lr = node->data;
821
822 rc = __cil_verify_levelrange(db, lr);
823 if (rc != SEPOL_OK) {
824 goto exit;
825 }
826
827 return SEPOL_OK;
828 exit:
829 cil_tree_log(node, CIL_ERR, "Invalid named range");
830 return rc;
831 }
832
833 static int __cil_verify_user_pre_eval(struct cil_tree_node *node)
834 {
835 int rc = SEPOL_ERR;
836 struct cil_user *user = node->data;
837
838 if (user->dftlevel == NULL) {
839 cil_log(CIL_ERR, "User %s does not have a default level\n", user->datum.name);
840 goto exit;
841 } else if (user->range == NULL) {
842 cil_log(CIL_ERR, "User %s does not have a level range\n", user->datum.name);
843 goto exit;
844 } else if (user->bounds != NULL) {
845 int steps = 0;
846 int limit = 2;
847 struct cil_user *u1 = user;
848 struct cil_user *u2 = user->bounds;
849
850 while (u2 != NULL) {
851 if (u1 == u2) {
852 cil_log(CIL_ERR, "Circular bounds found for user %s\n", u1->datum.name);
853 goto exit;
854 }
855
856 if (steps == limit) {
857 steps = 0;
858 limit *= 2;
859 u1 = u2;
860 }
861
862 u2 = u2->bounds;
863 steps++;
864 }
865 }
866
867 return SEPOL_OK;
868 exit:
869 cil_tree_log(node, CIL_ERR, "Invalid user");
870 return rc;
871 }
872
873 static int __cil_verify_user_post_eval(struct cil_db *db, struct cil_tree_node *node)
874 {
875 int rc = SEPOL_ERR;
876 struct cil_user *user = node->data;
877
878 /* Verify user range only if anonymous */
879 if (user->range->datum.name == NULL) {
880 rc = __cil_verify_levelrange(db, user->range);
881 if (rc != SEPOL_OK) {
882 goto exit;
883 }
884 }
885
886 return SEPOL_OK;
887 exit:
888 cil_tree_log(node, CIL_ERR, "Invalid user");
889 return rc;
890 }
891
892 static int __cil_verify_role(struct cil_tree_node *node)
893 {
894 int rc = SEPOL_ERR;
895 struct cil_role *role = node->data;
896 int steps = 0;
897 int limit = 2;
898 struct cil_role *r1 = role;
899 struct cil_role *r2 = role->bounds;
900
901 while (r2 != NULL) {
902 if (r1 == r2) {
903 cil_log(CIL_ERR, "Circular bounds found for role %s\n", r1->datum.name);
904 goto exit;
905 }
906
907 if (steps == limit) {
908 steps = 0;
909 limit *= 2;
910 r1 = r2;
911 }
912
913 r2 = r2->bounds;
914 steps++;
915 }
916
917 return SEPOL_OK;
918 exit:
919 cil_tree_log(node, CIL_ERR, "Invalid role");
920 return rc;
921 }
922
923 static int __cil_verify_type(struct cil_tree_node *node)
924 {
925 int rc = SEPOL_ERR;
926 struct cil_type *type = node->data;
927 int steps = 0;
928 int limit = 2;
929 struct cil_type *t1 = type;
930 struct cil_type *t2 = type->bounds;
931
932 while (t2 != NULL) {
933 if (t1 == t2) {
934 cil_log(CIL_ERR, "Circular bounds found for type %s\n", t1->datum.name);
935 goto exit;
936 }
937
938 if (steps == limit) {
939 steps = 0;
940 limit *= 2;
941 t1 = t2;
942 }
943
944 t2 = t2->bounds;
945 steps++;
946 }
947
948 return SEPOL_OK;
949 exit:
950 cil_tree_log(node, CIL_ERR, "Invalid type");
951 return rc;
952 }
953
954 static int __cil_verify_context(struct cil_db *db, struct cil_context *ctx)
955 {
956 int rc = SEPOL_ERR;
957 struct cil_user *user = ctx->user;
958 struct cil_role *role = ctx->role;
959 struct cil_type *type = ctx->type;
960 struct cil_level *user_low = user->range->low;
961 struct cil_level *user_high = user->range->high;
962 struct cil_level *ctx_low = ctx->range->low;
963 struct cil_level *ctx_high = ctx->range->high;
964 struct cil_list *sensitivityorder = db->sensitivityorder;
965 struct cil_list_item *curr;
966 int found = CIL_FALSE;
967
968 if (user->roles != NULL) {
969 if (!ebitmap_get_bit(user->roles, role->value)) {
970 cil_log(CIL_ERR, "Role %s is invalid for user %s\n", ctx->role_str, ctx->user_str);
971 rc = SEPOL_ERR;
972 goto exit;
973 }
974 } else {
975 cil_log(CIL_ERR, "No roles given to the user %s\n", ctx->user_str);
976 rc = SEPOL_ERR;
977 goto exit;
978 }
979
980 if (role->types != NULL) {
981 if (!ebitmap_get_bit(role->types, type->value)) {
982 cil_log(CIL_ERR, "Type %s is invalid for role %s\n", ctx->type_str, ctx->role_str);
983 rc = SEPOL_ERR;
984 goto exit;
985 }
986 } else {
987 cil_log(CIL_ERR, "No types associated with role %s\n", ctx->role_str);
988 rc = SEPOL_ERR;
989 goto exit;
990 }
991
992 /* Verify range only when anonymous */
993 if (ctx->range->datum.name == NULL) {
994 rc = __cil_verify_levelrange(db, ctx->range);
995 if (rc != SEPOL_OK) {
996 goto exit;
997 }
998 }
999
1000 for (curr = sensitivityorder->head; curr != NULL; curr = curr->next) {
1001 struct cil_sens *sens = curr->data;
1002
1003 if (found == CIL_FALSE) {
1004 if (sens == user_low->sens) {
1005 found = CIL_TRUE;
1006 } else if (sens == ctx_low->sens) {
1007 cil_log(CIL_ERR, "Range %s is invalid for user %s\n",
1008 ctx->range_str, ctx->user_str);
1009 rc = SEPOL_ERR;
1010 goto exit;
1011 }
1012 }
1013
1014 if (found == CIL_TRUE) {
1015 if (sens == ctx_high->sens) {
1016 break;
1017 } else if (sens == user_high->sens) {
1018 cil_log(CIL_ERR, "Range %s is invalid for user %s\n",
1019 ctx->range_str, ctx->user_str);
1020 rc = SEPOL_ERR;
1021 goto exit;
1022 }
1023 }
1024 }
1025
1026 return SEPOL_OK;
1027 exit:
1028 cil_log(CIL_ERR, "Invalid context\n");
1029 return rc;
1030 }
1031
1032 static int __cil_verify_named_context(struct cil_db *db, struct cil_tree_node *node)
1033 {
1034 int rc = SEPOL_ERR;
1035 struct cil_context *ctx = node->data;
1036
1037 rc = __cil_verify_context(db, ctx);
1038 if (rc != SEPOL_OK) {
1039 goto exit;
1040 }
1041
1042 return SEPOL_OK;
1043 exit:
1044 cil_tree_log(node, CIL_ERR, "Invalid named context");
1045 return rc;
1046 }
1047
1048 /*
1049 static int __cil_verify_rule(struct cil_tree_node *node, struct cil_complex_symtab *symtab)
1050 {
1051
1052 int rc = SEPOL_ERR;
1053 struct cil_type_rule *typerule = NULL;
1054 struct cil_roletransition *roletrans = NULL;
1055 struct cil_complex_symtab_key ckey;
1056
1057 switch (node->flavor) {
1058 case CIL_ROLETRANSITION: {
1059 roletrans = node->data;
1060 ckey.key1 = (intptr_t)roletrans->src;
1061 ckey.key2 = (intptr_t)roletrans->tgt;
1062 ckey.key3 = (intptr_t)roletrans->obj;
1063 ckey.key4 = CIL_ROLETRANSITION;
1064 break;
1065 }
1066 case CIL_TYPE_RULE: {
1067 typerule = node->data;
1068 ckey.key1 = (intptr_t)typerule->src;
1069 ckey.key2 = (intptr_t)typerule->tgt;
1070 ckey.key3 = (intptr_t)typerule->obj;
1071 ckey.key4 = (intptr_t)typerule->rule_kind;
1072 break;
1073 }
1074 default:
1075 break;
1076 }
1077
1078
1079 rc = cil_complex_symtab_insert(symtab, &ckey, NULL);
1080 if (rc == SEPOL_EEXIST) {
1081 struct cil_complex_symtab_datum *datum = NULL;
1082 cil_complex_symtab_search(symtab, &ckey, &datum);
1083 if (datum == NULL) {
1084 cil_tree_log(node, CIL_ERR, "Duplicate rule defined");
1085 rc = SEPOL_ERR;
1086 goto exit;
1087 }
1088 }
1089
1090 return SEPOL_OK;
1091 exit:
1092 cil_tree_log(node, CIL_ERR, "Invalid rule");
1093 return rc;
1094 }
1095 */
1096
1097 static int __cil_verify_booleanif_helper(struct cil_tree_node *node, __attribute__((unused)) uint32_t *finished, __attribute__((unused)) void *extra_args)
1098 {
1099 int rc = SEPOL_ERR;
1100 struct cil_tree_node *rule_node = node;
1101 struct cil_booleanif *bif = node->parent->parent->data;
1102
1103 switch (rule_node->flavor) {
1104 case CIL_AVRULE:
1105 case CIL_AVRULEX: {
1106 struct cil_avrule *avrule = NULL;
1107 avrule = rule_node->data;
1108 if (avrule->rule_kind == CIL_AVRULE_NEVERALLOW) {
1109 if (bif->preserved_tunable) {
1110 cil_tree_log(node, CIL_ERR, "Neverallow found in tunableif block (treated as a booleanif due to preserve-tunables)");
1111 } else {
1112 cil_tree_log(node, CIL_ERR, "Neverallow found in booleanif block");
1113 }
1114 rc = SEPOL_ERR;
1115 goto exit;
1116 }
1117 break;
1118 }
1119 case CIL_DENY_RULE:
1120 if (bif->preserved_tunable) {
1121 cil_tree_log(node, CIL_ERR, "Not allowed to have a deny rule in a tunableif block (treated as a booleanif due to preserve-tunables)");
1122 } else {
1123 cil_tree_log(node, CIL_ERR, "Not allowed to have deny rule in a booleanif block");
1124 }
1125 rc = SEPOL_ERR;
1126 goto exit;
1127 break;
1128 case CIL_TYPE_RULE: /*
1129 struct cil_type_rule *typerule = NULL;
1130 struct cil_tree_node *temp_node = NULL;
1131 struct cil_complex_symtab *symtab = extra_args;
1132 struct cil_complex_symtab_key ckey;
1133 struct cil_complex_symtab_datum datum;
1134 typerule = rule_node->data;
1135
1136 ckey.key1 = (intptr_t)typerule->src;
1137 ckey.key2 = (intptr_t)typerule->tgt;
1138 ckey.key3 = (intptr_t)typerule->obj;
1139 ckey.key4 = (intptr_t)typerule->rule_kind;
1140
1141 datum.data = node;
1142
1143 rc = cil_complex_symtab_insert(symtab, &ckey, &datum);
1144 if (rc != SEPOL_OK) {
1145 goto exit;
1146 }
1147
1148 for (temp_node = rule_node->next;
1149 temp_node != NULL;
1150 temp_node = temp_node->next) {
1151
1152 if (temp_node->flavor == CIL_TYPE_RULE) {
1153 typerule = temp_node->data;
1154 if ((intptr_t)typerule->src == ckey.key1 &&
1155 (intptr_t)typerule->tgt == ckey.key2 &&
1156 (intptr_t)typerule->obj == ckey.key3 &&
1157 (intptr_t)typerule->rule_kind == ckey.key4) {
1158 cil_log(CIL_ERR, "Duplicate type rule found (line: %d)\n", node->line);
1159 rc = SEPOL_ERR;
1160 goto exit;
1161 }
1162 }
1163 }
1164 break;*/
1165
1166 //TODO Fix duplicate type_rule detection
1167 break;
1168 case CIL_CALL:
1169 //Fall through to check content of call
1170 break;
1171 case CIL_TUNABLEIF:
1172 //Fall through
1173 break;
1174 case CIL_NAMETYPETRANSITION:
1175 /* While type transitions with file component are not allowed in
1176 booleanif statements if they don't have "*" as the file. We
1177 can't check that here. Or at least we won't right now. */
1178 break;
1179 case CIL_SRC_INFO:
1180 //Fall through
1181 break;
1182 default: {
1183 const char * flavor = cil_node_to_string(node);
1184 if (bif->preserved_tunable) {
1185 cil_tree_log(node, CIL_ERR, "Invalid %s statement in tunableif (treated as a booleanif due to preserve-tunables)", flavor);
1186 } else {
1187 cil_tree_log(node, CIL_ERR, "Invalid %s statement in booleanif", flavor);
1188 }
1189 goto exit;
1190 }
1191 }
1192
1193 rc = SEPOL_OK;
1194 exit:
1195 return rc;
1196 }
1197
1198 static int __cil_verify_booleanif(struct cil_tree_node *node, struct cil_complex_symtab *symtab)
1199 {
1200 int rc = SEPOL_ERR;
1201 struct cil_booleanif *bif = (struct cil_booleanif*)node->data;
1202 struct cil_tree_node *cond_block = node->cl_head;
1203
1204 while (cond_block != NULL) {
1205 rc = cil_tree_walk(cond_block, __cil_verify_booleanif_helper, NULL, NULL, symtab);
1206 if (rc != SEPOL_OK) {
1207 goto exit;
1208 }
1209 cond_block = cond_block->next;
1210 }
1211
1212 return SEPOL_OK;
1213 exit:
1214 if (bif->preserved_tunable) {
1215 cil_tree_log(node, CIL_ERR, "Invalid tunableif (treated as a booleanif due to preserve-tunables)");
1216 } else {
1217 cil_tree_log(node, CIL_ERR, "Invalid booleanif");
1218 }
1219 return rc;
1220 }
1221
1222 static int __cil_verify_netifcon(struct cil_db *db, struct cil_tree_node *node)
1223 {
1224 int rc = SEPOL_ERR;
1225 struct cil_netifcon *netif = node->data;
1226 struct cil_context *if_ctx = netif->if_context;
1227 struct cil_context *pkt_ctx = netif->packet_context;
1228
1229 /* Verify only when anonymous */
1230 if (if_ctx->datum.name == NULL) {
1231 rc = __cil_verify_context(db, if_ctx);
1232 if (rc != SEPOL_OK) {
1233 goto exit;
1234 }
1235 }
1236
1237 /* Verify only when anonymous */
1238 if (pkt_ctx->datum.name == NULL) {
1239 rc = __cil_verify_context(db, pkt_ctx);
1240 if (rc != SEPOL_OK) {
1241 goto exit;
1242 }
1243 }
1244
1245 if (strcmp(netif->interface_str, "?") == 0 ||
1246 strcmp(netif->interface_str, "*") == 0) {
1247 goto exit;
1248 }
1249
1250 return SEPOL_OK;
1251
1252 exit:
1253 cil_tree_log(node, CIL_ERR, "Invalid netifcon");
1254 return rc;
1255 }
1256
1257 static int __cil_verify_ibendportcon(struct cil_db *db, struct cil_tree_node *node)
1258 {
1259 int rc = SEPOL_ERR;
1260 struct cil_ibendportcon *ib_end_port = node->data;
1261 struct cil_context *ctx = ib_end_port->context;
1262
1263 /* Verify only when anonymous */
1264 if (!ctx->datum.name) {
1265 rc = __cil_verify_context(db, ctx);
1266 if (rc != SEPOL_OK)
1267 goto exit;
1268 }
1269
1270 return SEPOL_OK;
1271
1272 exit:
1273 cil_tree_log(node, CIL_ERR, "Invalid ibendportcon");
1274 return rc;
1275 }
1276
1277 static int __cil_verify_genfscon(struct cil_db *db, struct cil_tree_node *node)
1278 {
1279 int rc = SEPOL_ERR;
1280 struct cil_genfscon *genfs = node->data;
1281 struct cil_context *ctx = genfs->context;
1282
1283 /* Verify only when anonymous */
1284 if (ctx->datum.name == NULL) {
1285 rc = __cil_verify_context(db, ctx);
1286 if (rc != SEPOL_OK) {
1287 goto exit;
1288 }
1289 }
1290
1291 return SEPOL_OK;
1292
1293 exit:
1294 cil_tree_log(node, CIL_ERR, "Invalid genfscon");
1295 return rc;
1296 }
1297
1298 static int __cil_verify_filecon(struct cil_db *db, struct cil_tree_node *node)
1299 {
1300 int rc = SEPOL_ERR;
1301 struct cil_filecon *file = node->data;
1302 struct cil_context *ctx = file->context;
1303
1304 if (ctx == NULL) {
1305 rc = SEPOL_OK;
1306 goto exit;
1307 }
1308
1309 /* Verify only when anonymous */
1310 if (ctx->datum.name == NULL) {
1311 rc = __cil_verify_context(db, ctx);
1312 if (rc != SEPOL_OK) {
1313 cil_tree_log(node, CIL_ERR, "Invalid filecon");
1314 goto exit;
1315 }
1316 }
1317
1318 return SEPOL_OK;
1319
1320 exit:
1321 return rc;
1322 }
1323
1324 static int __cil_verify_nodecon(struct cil_db *db, struct cil_tree_node *node)
1325 {
1326 int rc = SEPOL_ERR;
1327 struct cil_nodecon *nodecon = node->data;
1328 struct cil_context *ctx = nodecon->context;
1329
1330 /* Verify only when anonymous */
1331 if (ctx->datum.name == NULL) {
1332 rc = __cil_verify_context(db, ctx);
1333 if (rc != SEPOL_OK) {
1334 goto exit;
1335 }
1336 }
1337
1338 return SEPOL_OK;
1339
1340 exit:
1341 cil_tree_log(node, CIL_ERR, "Invalid nodecon");
1342 return rc;
1343 }
1344
1345 static int __cil_verify_ibpkeycon(struct cil_db *db, struct cil_tree_node *node)
1346 {
1347 int rc = SEPOL_ERR;
1348 struct cil_ibpkeycon *pkey = node->data;
1349 struct cil_context *ctx = pkey->context;
1350
1351 /* Verify only when anonymous */
1352 if (!ctx->datum.name) {
1353 rc = __cil_verify_context(db, ctx);
1354 if (rc != SEPOL_OK)
1355 goto exit;
1356 }
1357
1358 return SEPOL_OK;
1359
1360 exit:
1361 cil_tree_log(node, CIL_ERR, "Invalid ibpkeycon");
1362 return rc;
1363 }
1364
1365 static int __cil_verify_portcon(struct cil_db *db, struct cil_tree_node *node)
1366 {
1367 int rc = SEPOL_ERR;
1368 struct cil_portcon *port = node->data;
1369 struct cil_context *ctx = port->context;
1370
1371 /* Verify only when anonymous */
1372 if (ctx->datum.name == NULL) {
1373 rc = __cil_verify_context(db, ctx);
1374 if (rc != SEPOL_OK) {
1375 goto exit;
1376 }
1377 }
1378
1379 return SEPOL_OK;
1380
1381 exit:
1382 cil_tree_log(node, CIL_ERR, "Invalid portcon");
1383 return rc;
1384 }
1385
1386 static int __cil_verify_pirqcon(struct cil_db *db, struct cil_tree_node *node)
1387 {
1388 int rc = SEPOL_ERR;
1389 struct cil_pirqcon *pirq = node->data;
1390 struct cil_context *ctx = pirq->context;
1391
1392 /* Verify only when anonymous */
1393 if (ctx->datum.name == NULL) {
1394 rc = __cil_verify_context(db, ctx);
1395 if (rc != SEPOL_OK) {
1396 goto exit;
1397 }
1398 }
1399
1400 return SEPOL_OK;
1401
1402 exit:
1403 cil_tree_log(node, CIL_ERR, "Invalid pirqcon");
1404 return rc;
1405 }
1406
1407 static int __cil_verify_iomemcon(struct cil_db *db, struct cil_tree_node *node)
1408 {
1409 int rc = SEPOL_ERR;
1410 struct cil_iomemcon *iomem = node->data;
1411 struct cil_context *ctx = iomem->context;
1412
1413 /* Verify only when anonymous */
1414 if (ctx->datum.name == NULL) {
1415 rc = __cil_verify_context(db, ctx);
1416 if (rc != SEPOL_OK) {
1417 goto exit;
1418 }
1419 }
1420
1421 return SEPOL_OK;
1422
1423 exit:
1424 cil_tree_log(node, CIL_ERR, "Invalid iomemcon");
1425 return rc;
1426 }
1427
1428 static int __cil_verify_ioportcon(struct cil_db *db, struct cil_tree_node *node)
1429 {
1430 int rc = SEPOL_ERR;
1431 struct cil_ioportcon *ioport = node->data;
1432 struct cil_context *ctx = ioport->context;
1433
1434 /* Verify only when anonymous */
1435 if (ctx->datum.name == NULL) {
1436 rc = __cil_verify_context(db, ctx);
1437 if (rc != SEPOL_OK) {
1438 goto exit;
1439 }
1440 }
1441
1442 return SEPOL_OK;
1443
1444 exit:
1445 cil_tree_log(node, CIL_ERR, "Invalid ioportcon");
1446 return rc;
1447 }
1448
1449 static int __cil_verify_pcidevicecon(struct cil_db *db, struct cil_tree_node *node)
1450 {
1451 int rc = SEPOL_ERR;
1452 struct cil_pcidevicecon *pcidev = node->data;
1453 struct cil_context *ctx = pcidev->context;
1454
1455 /* Verify only when anonymous */
1456 if (ctx->datum.name == NULL) {
1457 rc = __cil_verify_context(db, ctx);
1458 if (rc != SEPOL_OK) {
1459 goto exit;
1460 }
1461 }
1462
1463 return SEPOL_OK;
1464
1465 exit:
1466 cil_tree_log(node, CIL_ERR, "Invalid pcidevicecon");
1467 return rc;
1468 }
1469
1470 static int __cil_verify_devicetreecon(struct cil_db *db, struct cil_tree_node *node)
1471 {
1472 int rc = SEPOL_ERR;
1473 struct cil_devicetreecon *dt = node->data;
1474 struct cil_context *ctx = dt->context;
1475
1476 /* Verify only when anonymous */
1477 if (ctx->datum.name == NULL) {
1478 rc = __cil_verify_context(db, ctx);
1479 if (rc != SEPOL_OK) {
1480 goto exit;
1481 }
1482 }
1483
1484 return SEPOL_OK;
1485
1486 exit:
1487 cil_tree_log(node, CIL_ERR, "Invalid devicetreecon");
1488 return rc;
1489 }
1490
1491 static int __cil_verify_fsuse(struct cil_db *db, struct cil_tree_node *node)
1492 {
1493 int rc = SEPOL_ERR;
1494 struct cil_fsuse *fsuse = node->data;
1495 struct cil_context *ctx = fsuse->context;
1496
1497 /* Verify only when anonymous */
1498 if (ctx->datum.name == NULL) {
1499 rc = __cil_verify_context(db, ctx);
1500 if (rc != SEPOL_OK) {
1501 goto exit;
1502 }
1503 }
1504
1505 return SEPOL_OK;
1506
1507 exit:
1508 cil_tree_log(node, CIL_ERR, "Invalid fsuse");
1509 return rc;
1510 }
1511
1512 static int __cil_verify_permissionx(struct cil_permissionx *permx, struct cil_tree_node *node)
1513 {
1514 int rc;
1515 struct cil_list *classes = NULL;
1516 struct cil_list_item *item;
1517 struct cil_class *class;
1518 struct cil_symtab_datum *perm_datum;
1519 char *kind_str;
1520
1521 switch (permx->kind) {
1522 case CIL_PERMX_KIND_IOCTL:
1523 kind_str = CIL_KEY_IOCTL;
1524 break;
1525 case CIL_PERMX_KIND_NLMSG:
1526 kind_str = CIL_KEY_NLMSG;
1527 break;
1528 default:
1529 cil_tree_log(node, CIL_ERR, "Invalid permissionx kind (%d)", permx->kind);
1530 rc = SEPOL_ERR;
1531 goto exit;
1532 }
1533
1534 classes = cil_expand_class(permx->obj);
1535
1536 cil_list_for_each(item, classes) {
1537 class = item->data;
1538 rc = cil_symtab_get_datum(&class->perms, kind_str, &perm_datum);
1539 if (rc == SEPOL_ENOENT) {
1540 if (class->common != NULL) {
1541 rc = cil_symtab_get_datum(&class->common->perms, kind_str, &perm_datum);
1542 }
1543
1544 if (rc == SEPOL_ENOENT) {
1545 cil_tree_log(node, CIL_ERR, "Invalid permissionx: %s is not a permission of class %s", kind_str, class->datum.name);
1546 rc = SEPOL_ERR;
1547 goto exit;
1548 }
1549 }
1550 }
1551
1552 rc = SEPOL_OK;
1553
1554 exit:
1555 if (classes != NULL) {
1556 cil_list_destroy(&classes, CIL_FALSE);
1557 }
1558
1559 return rc;
1560 }
1561
1562 static int __cil_verify_avrulex(struct cil_tree_node *node)
1563 {
1564 struct cil_avrule *avrulex = node->data;
1565 return __cil_verify_permissionx(avrulex->perms.x.permx, node);
1566 }
1567
1568 static int __cil_verify_class(struct cil_tree_node *node)
1569 {
1570 int rc = SEPOL_ERR;
1571 struct cil_class *class = node->data;
1572
1573 if (class->common != NULL) {
1574 struct cil_class *common = class->common;
1575 struct cil_tree_node *common_node = common->datum.nodes->head->data;
1576 struct cil_tree_node *curr_com_perm = NULL;
1577
1578 for (curr_com_perm = common_node->cl_head;
1579 curr_com_perm != NULL;
1580 curr_com_perm = curr_com_perm->next) {
1581 struct cil_perm *com_perm = curr_com_perm->data;
1582 struct cil_tree_node *curr_class_perm = NULL;
1583
1584 for (curr_class_perm = node->cl_head;
1585 curr_class_perm != NULL;
1586 curr_class_perm = curr_class_perm->next) {
1587 struct cil_perm *class_perm = curr_class_perm->data;
1588
1589 if (com_perm->datum.name == class_perm->datum.name) {
1590 cil_log(CIL_ERR, "Duplicate permissions between %s common and class declarations\n", class_perm->datum.name);
1591 goto exit;
1592 }
1593 }
1594 }
1595 }
1596
1597 return SEPOL_OK;
1598
1599 exit:
1600 cil_tree_log(node, CIL_ERR, "Invalid class");
1601 return rc;
1602 }
1603
1604 static int __cil_verify_policycap(struct cil_tree_node *node)
1605 {
1606 int rc;
1607 struct cil_policycap *polcap = node->data;
1608
1609 rc = sepol_polcap_getnum((const char*)polcap->datum.name);
1610 if (rc == SEPOL_ERR) {
1611 goto exit;
1612 }
1613
1614 return SEPOL_OK;
1615
1616 exit:
1617 cil_tree_log(node, CIL_ERR, "Invalid policycap (%s)", (const char*)polcap->datum.name);
1618 return rc;
1619 }
1620
1621 int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1622 {
1623 int rc = SEPOL_ERR;
1624 int *avrule_cnt = 0;
1625 int *handleunknown;
1626 int *mls;
1627 int *nseuserdflt = 0;
1628 int *pass = 0;
1629 struct cil_args_verify *args = extra_args;
1630 struct cil_complex_symtab *csymtab = NULL;
1631 struct cil_db *db = NULL;
1632
1633 if (node == NULL || extra_args == NULL) {
1634 goto exit;
1635 }
1636
1637 db = args->db;
1638 avrule_cnt = args->avrule_cnt;
1639 handleunknown = args->handleunknown;
1640 mls = args->mls;
1641 nseuserdflt = args->nseuserdflt;
1642 csymtab = args->csymtab;
1643 pass = args->pass;
1644
1645 if (node->flavor == CIL_MACRO) {
1646 *finished = CIL_TREE_SKIP_HEAD;
1647 rc = SEPOL_OK;
1648 goto exit;
1649 } else if (node->flavor == CIL_BLOCK) {
1650 struct cil_block *blk = node->data;
1651 if (blk->is_abstract == CIL_TRUE) {
1652 *finished = CIL_TREE_SKIP_HEAD;
1653 }
1654 rc = SEPOL_OK;
1655 goto exit;
1656 }
1657
1658 switch (*pass) {
1659 case 0: {
1660 switch (node->flavor) {
1661 case CIL_USER:
1662 rc = __cil_verify_user_post_eval(db, node);
1663 break;
1664 case CIL_SELINUXUSERDEFAULT:
1665 (*nseuserdflt)++;
1666 rc = SEPOL_OK;
1667 break;
1668 case CIL_ROLE:
1669 rc = __cil_verify_role(node);
1670 break;
1671 case CIL_TYPE:
1672 rc = __cil_verify_type(node);
1673 break;
1674 case CIL_AVRULE:
1675 (*avrule_cnt)++;
1676 rc = SEPOL_OK;
1677 break;
1678 case CIL_HANDLEUNKNOWN:
1679 if (*handleunknown != -1) {
1680 cil_log(CIL_ERR, "Policy can not have more than one handleunknown\n");
1681 rc = SEPOL_ERR;
1682 } else {
1683 *handleunknown = ((struct cil_handleunknown*)node->data)->handle_unknown;
1684 rc = SEPOL_OK;
1685 }
1686 break;
1687 case CIL_MLS:
1688 if (*mls != -1) {
1689 cil_log(CIL_ERR, "Policy can not have more than one mls\n");
1690 rc = SEPOL_ERR;
1691 } else {
1692 *mls = ((struct cil_mls*)node->data)->value;
1693 rc = SEPOL_OK;
1694 }
1695 break;
1696 case CIL_ROLETRANSITION:
1697 rc = SEPOL_OK; //TODO __cil_verify_rule doesn't work quite right
1698 //rc = __cil_verify_rule(node, csymtab);
1699 break;
1700 case CIL_TYPE_RULE:
1701 rc = SEPOL_OK; //TODO __cil_verify_rule doesn't work quite right
1702 //rc = __cil_verify_rule(node, csymtab);
1703 break;
1704 case CIL_BOOLEANIF:
1705 rc = __cil_verify_booleanif(node, csymtab);
1706 *finished = CIL_TREE_SKIP_HEAD;
1707 break;
1708 case CIL_LEVELRANGE:
1709 rc = __cil_verify_named_levelrange(db, node);
1710 break;
1711 case CIL_CLASS:
1712 rc = __cil_verify_class(node);
1713 break;
1714 case CIL_POLICYCAP:
1715 rc = __cil_verify_policycap(node);
1716 break;
1717 default:
1718 rc = SEPOL_OK;
1719 break;
1720 }
1721 break;
1722 }
1723 case 1: {
1724 switch (node->flavor) {
1725 case CIL_CONTEXT:
1726 rc = __cil_verify_named_context(db, node);
1727 break;
1728 case CIL_NETIFCON:
1729 rc = __cil_verify_netifcon(db, node);
1730 break;
1731 case CIL_GENFSCON:
1732 rc = __cil_verify_genfscon(db, node);
1733 break;
1734 case CIL_FILECON:
1735 rc = __cil_verify_filecon(db, node);
1736 break;
1737 case CIL_NODECON:
1738 rc = __cil_verify_nodecon(db, node);
1739 break;
1740 case CIL_IBPKEYCON:
1741 rc = __cil_verify_ibpkeycon(db, node);
1742 break;
1743 case CIL_IBENDPORTCON:
1744 rc = __cil_verify_ibendportcon(db, node);
1745 break;
1746 case CIL_PORTCON:
1747 rc = __cil_verify_portcon(db, node);
1748 break;
1749 case CIL_PIRQCON:
1750 rc = __cil_verify_pirqcon(db, node);
1751 break;
1752 case CIL_IOMEMCON:
1753 rc = __cil_verify_iomemcon(db, node);
1754 break;
1755 case CIL_IOPORTCON:
1756 rc = __cil_verify_ioportcon(db, node);
1757 break;
1758 case CIL_PCIDEVICECON:
1759 rc = __cil_verify_pcidevicecon(db, node);
1760 break;
1761 case CIL_DEVICETREECON:
1762 rc = __cil_verify_devicetreecon(db, node);
1763 break;
1764 case CIL_FSUSE:
1765 rc = __cil_verify_fsuse(db, node);
1766 break;
1767 case CIL_AVRULEX:
1768 rc = __cil_verify_avrulex(node);
1769 break;
1770 case CIL_PERMISSIONX:
1771 rc = __cil_verify_permissionx(node->data, node);
1772 break;
1773 case CIL_RANGETRANSITION:
1774 rc = SEPOL_OK;
1775 break;
1776 default:
1777 rc = SEPOL_OK;
1778 break;
1779 }
1780 break;
1781 }
1782 default:
1783 rc = SEPOL_ERR;
1784 }
1785
1786 exit:
1787 return rc;
1788 }
1789
1790 static int __add_perm_to_list(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
1791 {
1792 struct cil_list *perm_list = (struct cil_list *)args;
1793
1794 cil_list_append(perm_list, CIL_DATUM, d);
1795
1796 return SEPOL_OK;
1797 }
1798
1799 static int __cil_verify_classperms(struct cil_list *classperms, struct cil_symtab_datum *orig, struct cil_symtab_datum *cur, unsigned steps, unsigned limit);
1800
1801 static int __cil_verify_map_perm(struct cil_class *class, struct cil_perm *perm, struct cil_symtab_datum *orig, unsigned steps, unsigned limit)
1802 {
1803 int rc;
1804
1805 if (!perm->classperms) {
1806 cil_tree_log(NODE(class), CIL_ERR, "No class permissions for map class %s, permission %s", DATUM(class)->name, DATUM(perm)->name);
1807 goto exit;
1808 }
1809
1810 rc = __cil_verify_classperms(perm->classperms, orig, &perm->datum, steps, limit);
1811 if (rc != SEPOL_OK) {
1812 cil_tree_log(NODE(class), CIL_ERR, "There was an error verifying class permissions for map class %s, permission %s", DATUM(class)->name, DATUM(perm)->name);
1813 goto exit;
1814 }
1815
1816 return SEPOL_OK;
1817
1818 exit:
1819 return SEPOL_ERR;
1820 }
1821
1822
1823 static int __cil_verify_perms(struct cil_class *class, struct cil_list *perms, struct cil_symtab_datum *orig, unsigned steps, unsigned limit)
1824 {
1825 int rc = SEPOL_ERR;
1826 int count = 0;
1827 struct cil_list_item *i = NULL;
1828
1829 if (!perms) {
1830 cil_tree_log(NODE(class), CIL_ERR, "No permissions for class %s in class permissions", DATUM(class)->name);
1831 goto exit;
1832 }
1833
1834 cil_list_for_each(i, perms) {
1835 count++;
1836 if (i->flavor == CIL_LIST) {
1837 rc = __cil_verify_perms(class, i->data, orig, steps, limit);
1838 if (rc != SEPOL_OK) {
1839 goto exit;
1840 }
1841 } else if (i->flavor == CIL_DATUM) {
1842 struct cil_perm *perm = i->data;
1843 if (FLAVOR(perm) == CIL_MAP_PERM) {
1844 rc = __cil_verify_map_perm(class, perm, orig, steps, limit);
1845 if (rc != SEPOL_OK) {
1846 goto exit;
1847 }
1848 }
1849 } else if (i->flavor == CIL_OP) {
1850 enum cil_flavor op = (enum cil_flavor)(uintptr_t)i->data;
1851 if (op == CIL_ALL) {
1852 struct cil_list *perm_list;
1853 struct cil_list_item *j = NULL;
1854 int count2 = 0;
1855 cil_list_init(&perm_list, CIL_MAP_PERM);
1856 cil_symtab_map(&class->perms, __add_perm_to_list, perm_list);
1857 if (class->common != NULL) {
1858 cil_symtab_map(&class->common->perms, __add_perm_to_list, perm_list);
1859 }
1860 cil_list_for_each(j, perm_list) {
1861 count2++;
1862 struct cil_perm *perm = j->data;
1863 if (FLAVOR(perm) == CIL_MAP_PERM) {
1864 rc = __cil_verify_map_perm(class, perm, orig, steps, limit);
1865 if (rc != SEPOL_OK) {
1866 cil_list_destroy(&perm_list, CIL_FALSE);
1867 goto exit;
1868 }
1869 }
1870 }
1871 cil_list_destroy(&perm_list, CIL_FALSE);
1872 if (count2 == 0) {
1873 cil_tree_log(NODE(class), CIL_ERR, "Operator \"all\" used for %s which has no permissions associated with it", DATUM(class)->name);
1874 goto exit;
1875 }
1876 }
1877 } else {
1878 cil_tree_log(NODE(class), CIL_ERR, "Permission list for %s has an unexpected flavor: %d", DATUM(class)->name, i->flavor);
1879 goto exit;
1880 }
1881 }
1882
1883 if (count == 0) {
1884 cil_tree_log(NODE(class), CIL_ERR, "Empty permissions list for class %s in class permissions", DATUM(class)->name);
1885 goto exit;
1886 }
1887
1888 return SEPOL_OK;
1889
1890 exit:
1891 return SEPOL_ERR;
1892 }
1893
1894 static int __cil_verify_classperms(struct cil_list *classperms, struct cil_symtab_datum *orig, struct cil_symtab_datum *cur, unsigned steps, unsigned limit)
1895 {
1896 int rc;
1897 struct cil_list_item *i;
1898
1899 if (classperms == NULL) {
1900 goto exit;
1901 }
1902
1903 if (steps > 0 && orig == cur) {
1904 cil_tree_log(NODE(cur), CIL_ERR, "Found circular class permissions involving %s", cur->name);
1905 goto exit;
1906 } else {
1907 steps++;
1908 if (steps > limit) {
1909 steps = 1;
1910 limit *= 2;
1911 orig = cur;
1912 }
1913 }
1914
1915 cil_list_for_each(i, classperms) {
1916 if (i->flavor == CIL_CLASSPERMS) {
1917 struct cil_classperms *cp = i->data;
1918 rc = __cil_verify_perms(cp->class, cp->perms, orig, steps, limit);
1919 if (rc != SEPOL_OK) {
1920 goto exit;
1921 }
1922 } else { /* SET */
1923 struct cil_classperms_set *cp_set = i->data;
1924 struct cil_classpermission *cp = cp_set->set;
1925 if (!cp->classperms) {
1926 cil_tree_log(NODE(cur), CIL_ERR, "Classpermission %s does not have a classpermissionset", DATUM(cp)->name);
1927 }
1928 rc = __cil_verify_classperms(cp->classperms, orig, &cp->datum, steps, limit);
1929 if (rc != SEPOL_OK) {
1930 goto exit;
1931 }
1932 }
1933 }
1934
1935 return SEPOL_OK;
1936
1937 exit:
1938 return SEPOL_ERR;
1939 }
1940
1941 static int __cil_verify_classpermission(struct cil_tree_node *node)
1942 {
1943 int rc;
1944 struct cil_classpermission *cp = node->data;
1945
1946 rc = __cil_verify_classperms(cp->classperms, &cp->datum, &cp->datum, 0, 2);
1947 if (rc != SEPOL_OK) {
1948 cil_tree_log(node, CIL_ERR, "Error verifying class permissions for classpermission %s", DATUM(cp)->name);
1949 }
1950
1951 return rc;
1952 }
1953
1954 struct cil_verify_map_args {
1955 struct cil_class *class;
1956 struct cil_tree_node *node;
1957 int rc;
1958 };
1959
1960 static int __verify_map_perm_classperms(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
1961 {
1962 struct cil_verify_map_args *map_args = args;
1963 struct cil_perm *cmp = (struct cil_perm *)d;
1964 int rc;
1965
1966 rc = __cil_verify_classperms(cmp->classperms, &cmp->datum, &cmp->datum, 0, 2);
1967 if (rc != SEPOL_OK) {
1968 cil_tree_log(NODE(cmp), CIL_ERR, "Error verifying class permissions for map class %s, permission %s", DATUM(map_args->class)->name, DATUM(cmp)->name);
1969 map_args->rc = rc;
1970 }
1971
1972 return SEPOL_OK;
1973 }
1974
1975 static int __cil_verify_map_class(struct cil_tree_node *node)
1976 {
1977 struct cil_class *mc = node->data;
1978 struct cil_verify_map_args map_args;
1979
1980 map_args.class = mc;
1981 map_args.node = node;
1982 map_args.rc = SEPOL_OK;
1983
1984 cil_symtab_map(&mc->perms, __verify_map_perm_classperms, &map_args);
1985
1986 if (map_args.rc != SEPOL_OK) {
1987 return SEPOL_ERR;
1988 }
1989
1990 return SEPOL_OK;
1991 }
1992
1993 int __cil_pre_verify_helper(struct cil_tree_node *node, uint32_t *finished, __attribute__((unused)) void *extra_args)
1994 {
1995 int rc = SEPOL_OK;
1996
1997 switch (node->flavor) {
1998 case CIL_MACRO: {
1999 *finished = CIL_TREE_SKIP_HEAD;
2000 break;
2001 }
2002 case CIL_BLOCK: {
2003 struct cil_block *blk = node->data;
2004 if (blk->is_abstract == CIL_TRUE) {
2005 *finished = CIL_TREE_SKIP_HEAD;
2006 }
2007 break;
2008 }
2009 case CIL_USER:
2010 rc = __cil_verify_user_pre_eval(node);
2011 break;
2012 case CIL_MAP_CLASS:
2013 rc = __cil_verify_map_class(node);
2014 break;
2015 case CIL_CLASSPERMISSION:
2016 rc = __cil_verify_classpermission(node);
2017 break;
2018 case CIL_USERATTRIBUTE:
2019 case CIL_ROLEATTRIBUTE:
2020 case CIL_TYPEATTRIBUTE:
2021 case CIL_CATSET: {
2022 struct cil_stack *stack;
2023 cil_stack_init(&stack);
2024 rc = cil_verify_no_self_reference(node->flavor, node->data, stack);
2025 cil_stack_destroy(&stack);
2026 break;
2027 }
2028 default:
2029 rc = SEPOL_OK;
2030 break;
2031 }
2032
2033 return rc;
2034 }