Debian Codegraph @

cil/src/cil_policy.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 <inttypes.h>
36
37 #include <sepol/policydb/conditional.h>
38 #include <sepol/errcodes.h>
39
40 #include "cil_internal.h"
41 #include "cil_flavor.h"
42 #include "cil_find.h"
43 #include "cil_mem.h"
44 #include "cil_policy.h"
45 #include "cil_tree.h"
46 #include "cil_list.h"
47 #include "cil_symtab.h"
48
49
50 enum cil_statement_list {
51 CIL_LIST_COMMON = 1,
52 CIL_LIST_DEFAULT_USER,
53 CIL_LIST_DEFAULT_ROLE,
54 CIL_LIST_DEFAULT_TYPE,
55 CIL_LIST_DEFAULT_RANGE,
56 CIL_LIST_SENSALIAS,
57 CIL_LIST_CATALIAS,
58 CIL_LIST_MLSCONSTRAIN,
59 CIL_LIST_MLSVALIDATETRANS,
60 CIL_LIST_POLICYCAP,
61 CIL_LIST_TYPEATTRIBUTE,
62 CIL_LIST_ROLEATTRIBUTE,
63 CIL_LIST_BOOL,
64 CIL_LIST_TYPE,
65 CIL_LIST_TYPEALIAS,
66 CIL_LIST_ROLE,
67 CIL_LIST_ROLEALLOW,
68 CIL_LIST_ROLETRANSITION,
69 CIL_LIST_USER,
70 CIL_LIST_CONSTRAINT,
71 CIL_LIST_VALIDATETRANS,
72 CIL_LIST_NUM_LISTS
73 };
74
75 static int __cil_gather_statements_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
76 {
77 struct cil_list **lists;
78 int kind = 0;
79
80 lists = (struct cil_list **)extra_args;
81
82 switch (node->flavor) {
83 case CIL_BLOCK: {
84 struct cil_block *blk = node->data;
85 if (blk->is_abstract == CIL_TRUE) {
86 *finished = CIL_TREE_SKIP_HEAD;
87 }
88 break;
89 }
90 case CIL_MACRO:
91 *finished = CIL_TREE_SKIP_HEAD;
92 break;
93 case CIL_BOOLEANIF:
94 *finished = CIL_TREE_SKIP_HEAD;
95 break;
96 case CIL_COMMON:
97 kind = CIL_LIST_COMMON;
98 break;
99 case CIL_DEFAULTUSER:
100 kind = CIL_LIST_DEFAULT_USER;
101 break;
102 case CIL_DEFAULTROLE:
103 kind = CIL_LIST_DEFAULT_ROLE;
104 break;
105 case CIL_DEFAULTTYPE:
106 kind = CIL_LIST_DEFAULT_TYPE;
107 break;
108 case CIL_DEFAULTRANGE:
109 kind = CIL_LIST_DEFAULT_RANGE;
110 break;
111 case CIL_SENSALIAS:
112 kind = CIL_LIST_SENSALIAS;
113 break;
114 case CIL_CATALIAS:
115 kind = CIL_LIST_CATALIAS;
116 break;
117 case CIL_MLSCONSTRAIN:
118 kind = CIL_LIST_MLSCONSTRAIN;
119 break;
120 case CIL_MLSVALIDATETRANS:
121 kind = CIL_LIST_MLSVALIDATETRANS;
122 break;
123 case CIL_POLICYCAP:
124 kind = CIL_LIST_POLICYCAP;
125 break;
126 case CIL_TYPEATTRIBUTE: {
127 struct cil_typeattribute *attr = node->data;
128 if (strcmp(attr->datum.fqn, "cil_gen_require") != 0) {
129 kind = CIL_LIST_TYPEATTRIBUTE;
130 }
131 break;
132 }
133 case CIL_ROLEATTRIBUTE: {
134 struct cil_roleattribute *attr = node->data;
135 if (strcmp(attr->datum.fqn, "cil_gen_require") != 0) {
136 kind = CIL_LIST_ROLEATTRIBUTE;
137 }
138 break;
139 }
140 case CIL_BOOL:
141 kind = CIL_LIST_BOOL;
142 break;
143 case CIL_TYPE:
144 kind = CIL_LIST_TYPE;
145 break;
146 case CIL_TYPEALIAS:
147 kind = CIL_LIST_TYPEALIAS;
148 break;
149 case CIL_ROLE: {
150 struct cil_role *role = node->data;
151 if (strcmp(role->datum.fqn, "object_r") != 0) {
152 kind = CIL_LIST_ROLE;
153 }
154 break;
155 }
156 case CIL_ROLEALLOW:
157 kind = CIL_LIST_ROLEALLOW;
158 break;
159 case CIL_ROLETRANSITION:
160 kind = CIL_LIST_ROLETRANSITION;
161 break;
162 case CIL_USER:
163 kind = CIL_LIST_USER;
164 break;
165 case CIL_CONSTRAIN:
166 kind = CIL_LIST_CONSTRAINT;
167 break;
168 case CIL_VALIDATETRANS:
169 kind = CIL_LIST_VALIDATETRANS;
170 break;
171 default:
172 break;
173 }
174
175 if (kind > 0) {
176 cil_list_append(lists[kind], node->flavor, node->data);
177 }
178
179 return SEPOL_OK;
180 }
181
182 static void cil_gather_statements(struct cil_tree_node *start, struct cil_list *lists[])
183 {
184 cil_tree_walk(start, __cil_gather_statements_helper, NULL, NULL, lists);
185 }
186
187 static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, const char *kind)
188 {
189 struct cil_list_item *i1;
190
191 cil_list_for_each(i1, rules) {
192 fprintf(out, "%s %s;\n", kind, DATUM(i1->data)->fqn);
193 }
194 }
195
196 static void cil_cats_to_policy(FILE *out, struct cil_cats *cats)
197 {
198 const char *lead = "";
199 struct cil_cat *first = NULL, *last = NULL, *cat;
200 struct cil_list_item *i1;
201
202 cil_list_for_each(i1, cats->datum_expr) {
203 cat = i1->data;
204 if (first == NULL) {
205 first = cat;
206 } else if (last == NULL) {
207 if (cat->value == first->value + 1) {
208 last = cat;
209 } else {
210 fprintf(out, "%s%s", lead, DATUM(first)->fqn);
211 lead = ",";
212 first = cat;
213 }
214 } else if (cat->value == last->value + 1) {
215 last = cat;
216 } else {
217 fprintf(out, "%s%s", lead, DATUM(first)->fqn);
218 lead = ",";
219 if (last->value >= first->value + 1) {
220 fprintf(out, ".");
221 } else {
222 fprintf(out, ",");
223 }
224 fprintf(out, "%s", DATUM(last)->fqn);
225 first = cat;
226 last = NULL;
227 }
228 }
229 if (first) {
230 fprintf(out, "%s%s", lead, DATUM(first)->fqn);
231 if (last != NULL) {
232 if (last->value >= first->value + 1) {
233 fprintf(out, ".");
234 } else {
235 fprintf(out, ",");
236 }
237 fprintf(out, "%s", DATUM(last)->fqn);
238 }
239 }
240 }
241
242 static void cil_level_to_policy(FILE *out, struct cil_level *level)
243 {
244 fprintf(out, "%s", DATUM(level->sens)->fqn);
245 if (level->cats != NULL) {
246 fprintf(out, ":");
247 cil_cats_to_policy(out, level->cats);
248 }
249 }
250
251 static int cil_levels_simple_and_equal(struct cil_level *l1, struct cil_level *l2)
252 {
253 /* Mostly just want to detect s0 - s0 ranges */
254 if (l1 == l2)
255 return CIL_TRUE;
256
257 if (l1->sens == l2->sens && (l1->cats == NULL && l2->cats == NULL))
258 return CIL_TRUE;
259
260 return CIL_FALSE;
261 }
262
263 static void cil_levelrange_to_policy(FILE *out, struct cil_levelrange *lvlrange)
264 {
265 cil_level_to_policy(out, lvlrange->low);
266 if (cil_levels_simple_and_equal(lvlrange->low, lvlrange->high) == CIL_FALSE) {
267 fprintf(out, " - ");
268 cil_level_to_policy(out, lvlrange->high);
269 }
270 }
271
272 static void cil_context_to_policy(FILE *out, struct cil_context *context, int mls)
273 {
274 fprintf(out, "%s:", DATUM(context->user)->fqn);
275 fprintf(out, "%s:", DATUM(context->role)->fqn);
276 fprintf(out, "%s", DATUM(context->type)->fqn);
277 if (mls) {
278 fprintf(out, ":");
279 cil_levelrange_to_policy(out, context->range);
280 }
281 }
282
283 static void cil_cond_expr_to_policy(FILE *out, struct cil_list *expr, int first)
284 {
285 struct cil_list_item *i1 = expr->head;
286
287 if (i1->flavor == CIL_OP) {
288 enum cil_flavor op = (enum cil_flavor)(uintptr_t)i1->data;
289 fprintf(out, "(");
290 switch (op) {
291 case CIL_NOT:
292 fprintf(out, "! ");
293 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
294 break;
295 case CIL_OR:
296 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
297 fprintf(out, " || ");
298 cil_cond_expr_to_policy(out, i1->next->next->data, CIL_FALSE);
299 break;
300 case CIL_AND:
301 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
302 fprintf(out, " && ");
303 cil_cond_expr_to_policy(out, i1->next->next->data, CIL_FALSE);
304 break;
305 case CIL_XOR:
306 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
307 fprintf(out, " ^ ");
308 cil_cond_expr_to_policy(out, i1->next->next->data, CIL_FALSE);
309 break;
310 case CIL_EQ:
311 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
312 fprintf(out, " == ");
313 cil_cond_expr_to_policy(out, i1->next->next->data, CIL_FALSE);
314 break;
315 case CIL_NEQ:
316 cil_cond_expr_to_policy(out, i1->next->data, CIL_FALSE);
317 fprintf(out, " != ");
318 cil_cond_expr_to_policy(out, i1->next->next->data, CIL_FALSE);
319 break;
320 default:
321 fprintf(out, "???");
322 break;
323 }
324 fprintf(out, ")");
325 } else if (i1->flavor == CIL_DATUM) {
326 if (first == CIL_TRUE) {
327 fprintf(out, "(");
328 }
329 fprintf(out, "%s", DATUM(i1->data)->fqn);
330 if (first == CIL_TRUE) {
331 fprintf(out, ")");
332 }
333 } else if (i1->flavor == CIL_LIST) {
334 cil_cond_expr_to_policy(out, i1->data, CIL_FALSE);
335 } else {
336 fprintf(out, "???");
337 }
338 }
339
340 static size_t __cil_userattribute_len(struct cil_db *db, struct cil_userattribute *attr)
341 {
342 ebitmap_node_t *unode;
343 unsigned int i;
344 size_t len = 0;
345
346 ebitmap_for_each_positive_bit(attr->users, unode, i) {
347 len += strlen(DATUM(db->val_to_user[i])->fqn);
348 len++;
349 }
350
351 return len;
352 }
353
354 static size_t __cil_cons_leaf_operand_len(struct cil_db *db, struct cil_list_item *operand)
355 {
356 struct cil_list_item *i1;
357 enum cil_flavor flavor = operand->flavor;
358 size_t len = 0;
359
360 if (flavor == CIL_CONS_OPERAND) {
361 len = 2;
362 } else if (flavor == CIL_DATUM) {
363 struct cil_tree_node *node = NODE(operand->data);
364 if (node->flavor == CIL_USERATTRIBUTE) {
365 len = __cil_userattribute_len(db, operand->data);
366 len++; /* "{" */
367 } else {
368 len = strlen(DATUM(operand->data)->fqn);
369 }
370 } else if (flavor == CIL_LIST) {
371 len = 1; /* "{" */
372 cil_list_for_each(i1, (struct cil_list *)operand->data) {
373 struct cil_tree_node *node = NODE(operand->data);
374 if (node->flavor == CIL_USERATTRIBUTE) {
375 len = __cil_userattribute_len(db, operand->data);
376 } else {
377 len += strlen(DATUM(operand->data)->fqn);
378 len++; /* " " or "}" */
379 }
380 }
381 }
382
383 return len;
384 }
385
386 static size_t __cil_cons_leaf_op_len(struct cil_list_item *op)
387 {
388 enum cil_flavor flavor = (enum cil_flavor)(uintptr_t)op->data;
389 size_t len;
390
391 switch (flavor) {
392 case CIL_EQ:
393 len = 4; /* " == " */
394 break;
395 case CIL_NEQ:
396 len = 4; /* " != " */
397 break;
398 case CIL_CONS_DOM:
399 len = 5; /* " dom " */
400 break;
401 case CIL_CONS_DOMBY:
402 len = 7; /* " domby " */
403 break;
404 case CIL_CONS_INCOMP:
405 len = 8; /* " incomp " */
406 break;
407 default:
408 /* Should be impossible to be here */
409 len = 5; /* " ??? " */
410 }
411
412 return len;
413 }
414
415 static size_t cil_cons_expr_len(struct cil_db *db, struct cil_list *cons_expr)
416 {
417 struct cil_list_item *i1;
418 enum cil_flavor op;
419 size_t len;
420
421 i1 = cons_expr->head;
422
423 op = (enum cil_flavor)(uintptr_t)i1->data;
424 switch (op) {
425 case CIL_NOT:
426 len = 6; /* "(not )" */
427 len += cil_cons_expr_len(db, i1->next->data);
428 break;
429 case CIL_AND:
430 len = 7; /* "( and )" */
431 len += cil_cons_expr_len(db, i1->next->data);
432 len += cil_cons_expr_len(db, i1->next->next->data);
433 break;
434 case CIL_OR:
435 len = 6; /* "( or )" */
436 len += cil_cons_expr_len(db, i1->next->data);
437 len += cil_cons_expr_len(db, i1->next->next->data);
438 break;
439 default:
440 len = 2; /* "()" */
441 len += __cil_cons_leaf_operand_len(db, i1->next);
442 len += __cil_cons_leaf_op_len(i1);
443 len += __cil_cons_leaf_operand_len(db, i1->next->next);
444 }
445
446 return len;
447 }
448
449 static char *__cil_userattribute_to_string(struct cil_db *db, struct cil_userattribute *attr, char *new)
450 {
451 ebitmap_node_t *unode;
452 unsigned int i;
453 char *str;
454 size_t len;
455
456 ebitmap_for_each_positive_bit(attr->users, unode, i) {
457 str = DATUM(db->val_to_user[i])->fqn;
458 len = strlen(str);
459 memcpy(new, str, len);
460 new += len;
461 *new++ = ' ';
462 }
463
464 return new;
465 }
466
467 static char *__cil_cons_leaf_operand_to_string(struct cil_db *db, struct cil_list_item *operand, char *new)
468 {
469 struct cil_list_item *i1;
470 enum cil_flavor flavor = operand->flavor;
471 const char *o_str;
472 size_t o_len;
473
474 if (flavor == CIL_CONS_OPERAND) {
475 enum cil_flavor o_flavor = (enum cil_flavor)(uintptr_t)operand->data;
476 switch (o_flavor) {
477 case CIL_CONS_U1:
478 o_str = "u1";
479 break;
480 case CIL_CONS_U2:
481 o_str = "u2";
482 break;
483 case CIL_CONS_U3:
484 o_str = "u3";
485 break;
486 case CIL_CONS_R1:
487 o_str = "r1";
488 break;
489 case CIL_CONS_R2:
490 o_str = "r2";
491 break;
492 case CIL_CONS_R3:
493 o_str = "r3";
494 break;
495 case CIL_CONS_T1:
496 o_str = "t1";
497 break;
498 case CIL_CONS_T2:
499 o_str = "t2";
500 break;
501 case CIL_CONS_T3:
502 o_str = "t3";
503 break;
504 case CIL_CONS_L1:
505 o_str = "l1";
506 break;
507 case CIL_CONS_L2:
508 o_str = "l2";
509 break;
510 case CIL_CONS_H1:
511 o_str = "h1";
512 break;
513 case CIL_CONS_H2:
514 o_str = "h2";
515 break;
516 default:
517 /* Impossible */
518 o_str = "??";
519 }
520 strcpy(new, o_str);
521 new += 2;
522 } else if (flavor == CIL_DATUM) {
523 struct cil_tree_node *node = NODE(operand->data);
524 if (node->flavor == CIL_USERATTRIBUTE) {
525 *new++ = '{';
526 new = __cil_userattribute_to_string(db, operand->data, new);
527 new--;
528 *new++ = '}';
529 } else {
530 o_str = DATUM(operand->data)->fqn;
531 o_len = strlen(o_str);
532 memcpy(new, o_str, o_len);
533 new += o_len;
534 }
535 } else if (flavor == CIL_LIST) {
536 *new++ = '{';
537 cil_list_for_each(i1, (struct cil_list *)operand->data) {
538 struct cil_tree_node *node = NODE(operand->data);
539 if (node->flavor == CIL_USERATTRIBUTE) {
540 new = __cil_userattribute_to_string(db, operand->data, new);
541 } else {
542 o_str = DATUM(operand->data)->fqn;
543 o_len = strlen(o_str);
544 memcpy(new, o_str, o_len);
545 new += o_len;
546 *new++ = ' ';
547 }
548 }
549 new--;
550 *new++ = '}';
551 }
552
553 return new;
554 }
555
556 static char *__cil_cons_leaf_op_to_string(struct cil_list_item *op, char *new)
557 {
558 enum cil_flavor flavor = (enum cil_flavor)(uintptr_t)op->data;
559 const char *op_str;
560 size_t len;
561
562 switch (flavor) {
563 case CIL_EQ:
564 op_str = " == ";
565 len = 4;
566 break;
567 case CIL_NEQ:
568 op_str = " != ";
569 len = 4;
570 break;
571 case CIL_CONS_DOM:
572 op_str = " dom ";
573 len = 5;
574 break;
575 case CIL_CONS_DOMBY:
576 op_str = " domby ";
577 len = 7;
578 break;
579 case CIL_CONS_INCOMP:
580 op_str = " incomp ";
581 len = 8;
582 break;
583 default:
584 /* Should be impossible to be here */
585 op_str = " ??? ";
586 len = 5;
587 }
588
589 strcpy(new, op_str);
590 new += len;
591
592 return new;
593 }
594
595 static char *__cil_cons_expr_to_string(struct cil_db *db, struct cil_list *cons_expr, char *new)
596 {
597 struct cil_list_item *i1;
598 enum cil_flavor op;
599
600 i1 = cons_expr->head;
601
602 op = (enum cil_flavor)(uintptr_t)i1->data;
603 switch (op) {
604 case CIL_NOT:
605 *new++ = '(';
606 strcpy(new, "not ");
607 new += 4;
608 new = __cil_cons_expr_to_string(db, i1->next->data, new);
609 *new++ = ')';
610 break;
611 case CIL_AND:
612 *new++ = '(';
613 new = __cil_cons_expr_to_string(db, i1->next->data, new);
614 strcpy(new, " and ");
615 new += 5;
616 new = __cil_cons_expr_to_string(db, i1->next->next->data, new);
617 *new++ = ')';
618 break;
619 case CIL_OR:
620 *new++ = '(';
621 new = __cil_cons_expr_to_string(db, i1->next->data, new);
622 strcpy(new, " or ");
623 new += 4;
624 new = __cil_cons_expr_to_string(db, i1->next->next->data, new);
625 *new++ = ')';
626 break;
627 default:
628 *new++ = '(';
629 new = __cil_cons_leaf_operand_to_string(db, i1->next, new);
630 new = __cil_cons_leaf_op_to_string(i1, new);
631 new = __cil_cons_leaf_operand_to_string(db, i1->next->next, new);
632 *new++ = ')';
633 }
634
635 return new;
636 }
637
638 static char *cil_cons_expr_to_string(struct cil_db *db, struct cil_list *cons_expr)
639 {
640 char *new, *tail;
641 size_t len = cil_cons_expr_len(db, cons_expr);
642
643 new = cil_malloc(len+1);
644 tail = __cil_cons_expr_to_string(db, cons_expr, new);
645 *tail = '\0';
646
647 return new;
648 }
649
650 static void cil_classperms_to_string(struct cil_classperms *classperms, struct cil_list *classperms_strs)
651 {
652 struct cil_list_item *i1;
653 size_t len = 0;
654 char *new, *curr;
655
656 len += strlen(DATUM(classperms->class)->fqn) + 1;
657 cil_list_for_each(i1, classperms->perms) {
658 len += strlen(DATUM(i1->data)->fqn) + 1;
659 }
660 len += 4; /* for "{ " and " }" */
661
662 new = cil_malloc(len);
663 curr = new;
664
665 curr[len-1] = '\0';
666
667 len = strlen(DATUM(classperms->class)->fqn);
668 memcpy(curr, DATUM(classperms->class)->fqn, len);
669 curr += len;
670 *curr++ = ' ';
671
672 *curr++ = '{';
673 *curr++ = ' ';
674 cil_list_for_each(i1, classperms->perms) {
675 len = strlen(DATUM(i1->data)->fqn);
676 memcpy(curr, DATUM(i1->data)->fqn, len);
677 curr += len;
678 *curr++ = ' ';
679 }
680 *curr++ = '}';
681
682 cil_list_append(classperms_strs, CIL_STRING, new);
683 }
684
685 static void cil_classperms_to_strings(struct cil_list *classperms, struct cil_list *classperms_strs)
686 {
687 struct cil_list_item *i1;
688
689 cil_list_for_each(i1, classperms) {
690 if (i1->flavor == CIL_CLASSPERMS) {
691 struct cil_classperms *cp = i1->data;
692 if (FLAVOR(cp->class) == CIL_CLASS) {
693 cil_classperms_to_string(cp, classperms_strs);
694 } else { /* MAP */
695 struct cil_list_item *i2 = NULL;
696 cil_list_for_each(i2, cp->perms) {
697 struct cil_perm *cmp = i2->data;
698 cil_classperms_to_strings(cmp->classperms, classperms_strs);
699 }
700 }
701 } else { /* SET */
702 struct cil_classperms_set *cp_set = i1->data;
703 struct cil_classpermission *cp = cp_set->set;
704 cil_classperms_to_strings(cp->classperms, classperms_strs);
705 }
706 }
707 }
708
709 static void cil_class_decls_to_policy(FILE *out, struct cil_list *classorder)
710 {
711 struct cil_list_item *i1;
712
713 cil_list_for_each(i1, classorder) {
714 fprintf(out, "class %s\n", DATUM(i1->data)->fqn);
715 }
716 }
717
718 static void cil_sid_decls_to_policy(FILE *out, struct cil_list *sidorder)
719 {
720 struct cil_list_item *i1;
721
722 cil_list_for_each(i1, sidorder) {
723 fprintf(out, "sid %s\n", DATUM(i1->data)->fqn);
724 }
725 }
726
727 static void cil_commons_to_policy(FILE *out, struct cil_list *commons)
728 {
729 struct cil_list_item *i1;
730 struct cil_class* common;
731 struct cil_tree_node *node;
732 struct cil_tree_node *perm;
733
734 cil_list_for_each(i1, commons) {
735 common = i1->data;
736 node = NODE(&common->datum);
737 perm = node->cl_head;
738
739 fprintf(out, "common %s {", common->datum.fqn);
740 while (perm != NULL) {
741 fprintf(out, "%s ", DATUM(perm->data)->fqn);
742 perm = perm->next;
743 }
744 fprintf(out, "}\n");
745 }
746 }
747
748 static void cil_classes_to_policy(FILE *out, struct cil_list *classorder)
749 {
750 struct cil_list_item *i1;
751 struct cil_class *class;
752 struct cil_tree_node *node;
753
754 cil_list_for_each(i1, classorder) {
755 class = i1->data;
756 node = NODE(&class->datum);
757
758 fprintf(out, "class %s", class->datum.fqn);
759 if (class->common != NULL) {
760 fprintf(out, " inherits %s", class->common->datum.fqn);
761 }
762 if (node->cl_head != NULL) {
763 struct cil_tree_node *perm = node->cl_head;
764 fprintf(out, " {");
765 while (perm != NULL) {
766 fprintf(out, " %s", DATUM(perm->data)->fqn);
767 perm = perm->next;
768 }
769 fprintf(out, " }");
770 }
771 fprintf(out, "\n");
772 }
773 }
774
775 static void cil_defaults_to_policy(FILE *out, struct cil_list *defaults, const char *kind)
776 {
777 struct cil_list_item *i1, *i2, *i3;
778 struct cil_default *def;
779 struct cil_list *class_list;
780
781 cil_list_for_each(i1, defaults) {
782 def = i1->data;
783 fprintf(out, "%s {",kind);
784 cil_list_for_each(i2, def->class_datums) {
785 class_list = cil_expand_class(i2->data);
786 cil_list_for_each(i3, class_list) {
787 fprintf(out, " %s", DATUM(i3->data)->fqn);
788 }
789 cil_list_destroy(&class_list, CIL_FALSE);
790 }
791 fprintf(out, " }");
792 if (def->object == CIL_DEFAULT_SOURCE) {
793 fprintf(out," %s",CIL_KEY_SOURCE);
794 } else if (def->object == CIL_DEFAULT_TARGET) {
795 fprintf(out," %s",CIL_KEY_TARGET);
796 }
797 fprintf(out,";\n");
798 }
799 }
800
801 static void cil_default_ranges_to_policy(FILE *out, struct cil_list *defaults)
802 {
803 struct cil_list_item *i1, *i2, *i3;
804 struct cil_defaultrange *def;
805 struct cil_list *class_list;
806
807 cil_list_for_each(i1, defaults) {
808 def = i1->data;
809 fprintf(out, "default_range {");
810 cil_list_for_each(i2, def->class_datums) {
811 class_list = cil_expand_class(i2->data);
812 cil_list_for_each(i3, class_list) {
813 fprintf(out, " %s", DATUM(i3->data)->fqn);
814 }
815 cil_list_destroy(&class_list, CIL_FALSE);
816 }
817 fprintf(out, " }");
818
819 switch (def->object_range) {
820 case CIL_DEFAULT_SOURCE_LOW:
821 fprintf(out," %s %s", CIL_KEY_SOURCE, CIL_KEY_LOW);
822 break;
823 case CIL_DEFAULT_SOURCE_HIGH:
824 fprintf(out," %s %s", CIL_KEY_SOURCE, CIL_KEY_HIGH);
825 break;
826 case CIL_DEFAULT_SOURCE_LOW_HIGH:
827 fprintf(out," %s %s", CIL_KEY_SOURCE, CIL_KEY_LOW_HIGH);
828 break;
829 case CIL_DEFAULT_TARGET_LOW:
830 fprintf(out," %s %s", CIL_KEY_TARGET, CIL_KEY_LOW);
831 break;
832 case CIL_DEFAULT_TARGET_HIGH:
833 fprintf(out," %s %s", CIL_KEY_TARGET, CIL_KEY_HIGH);
834 break;
835 case CIL_DEFAULT_TARGET_LOW_HIGH:
836 fprintf(out," %s %s", CIL_KEY_TARGET, CIL_KEY_LOW_HIGH);
837 break;
838 case CIL_DEFAULT_GLBLUB:
839 fprintf(out," %s", CIL_KEY_GLBLUB);
840 break;
841 default:
842 break;
843 }
844 fprintf(out,";\n");
845 }
846 }
847
848 static void cil_sensitivities_to_policy(FILE *out, struct cil_list *sensorder, struct cil_list *all_aliases)
849 {
850 struct cil_list_item *i1, *i2;
851 struct cil_sens *sens;
852 struct cil_list *aliases = NULL;
853 struct cil_alias *alias;
854 struct cil_sens *actual;
855 int num_aliases;
856
857 cil_list_for_each(i1, sensorder) {
858 sens = i1->data;
859 num_aliases = 0;
860 cil_list_for_each(i2, all_aliases) {
861 alias = i2->data;
862 actual = alias->actual;
863 if (sens == actual) {
864 if (num_aliases == 0) {
865 cil_list_init(&aliases, CIL_LIST);
866 }
867 cil_list_append(aliases, CIL_SENSALIAS, alias);
868 num_aliases++;
869 }
870 }
871 fprintf(out, "sensitivity %s", sens->datum.fqn);
872 if (num_aliases > 0) {
873 fprintf(out, " alias");
874 if (num_aliases > 1) {
875 fprintf(out, " {");
876 }
877 cil_list_for_each(i2, aliases) {
878 alias = i2->data;
879 fprintf(out, " %s", alias->datum.fqn);
880 }
881 if (num_aliases > 1) {
882 fprintf(out, " }");
883 }
884 cil_list_destroy(&aliases, CIL_FALSE);
885 }
886 fprintf(out, ";\n");
887 }
888 }
889
890 static void cil_dominance_to_policy(FILE *out, struct cil_list *sensorder)
891 {
892 struct cil_list_item *item;
893 struct cil_sens *sens;
894
895 fprintf(out, "dominance {");
896 cil_list_for_each(item, sensorder) {
897 sens = item->data;
898 fprintf(out, " %s", sens->datum.fqn);
899 }
900 fprintf(out, " }\n");
901 }
902
903 static void cil_categories_to_policy(FILE *out, struct cil_list *catorder, struct cil_list *all_aliases)
904 {
905 struct cil_list_item *i1, *i2;
906 struct cil_sens *cat;
907 struct cil_list *aliases = NULL;
908 struct cil_alias *alias;
909 struct cil_sens *actual;
910 int num_aliases;
911
912 cil_list_for_each(i1, catorder) {
913 cat = i1->data;
914 num_aliases = 0;
915 cil_list_for_each(i2, all_aliases) {
916 alias = i2->data;
917 actual = alias->actual;
918 if (cat == actual) {
919 if (num_aliases == 0) {
920 cil_list_init(&aliases, CIL_LIST);
921 }
922 cil_list_append(aliases, CIL_CATALIAS, alias);
923 num_aliases++;
924 }
925 }
926 fprintf(out, "category %s",cat->datum.fqn);
927 if (num_aliases > 0) {
928 fprintf(out, " alias");
929 if (num_aliases > 1) {
930 fprintf(out, " { ");
931 }
932 cil_list_for_each(i2, aliases) {
933 alias = i2->data;
934 fprintf(out, " %s", alias->datum.fqn);
935 }
936 if (num_aliases > 1) {
937 fprintf(out, " }");
938 }
939 cil_list_destroy(&aliases, CIL_FALSE);
940 }
941 fprintf(out, ";\n");
942 }
943 }
944
945 static void cil_levels_to_policy(FILE *out, struct cil_list *sensorder)
946 {
947 struct cil_list_item *i1, *i2;
948 struct cil_sens *sens;
949
950 cil_list_for_each(i1, sensorder) {
951 sens = i1->data;
952 if (sens->cats_list) {
953 cil_list_for_each(i2, sens->cats_list) {
954 fprintf(out, "level %s:",sens->datum.fqn);
955 cil_cats_to_policy(out, i2->data);
956 fprintf(out,";\n");
957 }
958 } else {
959 fprintf(out, "level %s;\n",sens->datum.fqn);
960 }
961 }
962 }
963
964 static void cil_mlsconstrains_to_policy(FILE *out, struct cil_db *db, struct cil_list *mlsconstrains)
965 {
966 struct cil_list_item *i1, *i2;
967 struct cil_constrain *cons;
968 struct cil_list *classperms_strs;
969 char *cp_str;
970 char *expr_str;
971
972 cil_list_for_each(i1, mlsconstrains) {
973 cons = i1->data;
974 cil_list_init(&classperms_strs, CIL_LIST);
975 cil_classperms_to_strings(cons->classperms, classperms_strs);
976 expr_str = cil_cons_expr_to_string(db, cons->datum_expr);
977 cil_list_for_each(i2, classperms_strs) {
978 cp_str = i2->data;
979 fprintf(out, "mlsconstrain %s %s;\n", cp_str, expr_str);
980 free(cp_str);
981 }
982 free(expr_str);
983 cil_list_destroy(&classperms_strs, CIL_FALSE);
984 }
985 }
986
987 static void cil_validatetrans_to_policy(FILE *out, struct cil_db *db, struct cil_list *validatetrans, char *kind)
988 {
989 struct cil_list_item *i1, *i2;
990 struct cil_validatetrans *trans;
991 struct cil_list *class_list;
992 struct cil_class *class;
993 char *expr_str;
994
995 cil_list_for_each(i1, validatetrans) {
996 trans = i1->data;
997 class_list = cil_expand_class(trans->class);
998 expr_str = cil_cons_expr_to_string(db, trans->datum_expr);
999 cil_list_for_each(i2, class_list) {
1000 class = i2->data;
1001 fprintf(out, "%s %s %s;\n", kind, class->datum.fqn, expr_str);
1002 }
1003 free(expr_str);
1004 cil_list_destroy(&class_list, CIL_FALSE);
1005 }
1006 }
1007
1008 static void cil_bools_to_policy(FILE *out, struct cil_list *bools)
1009 {
1010 struct cil_list_item *i1;
1011 struct cil_bool *boolean;
1012 const char *value;
1013
1014 cil_list_for_each(i1, bools) {
1015 boolean = i1->data;
1016 value = boolean->value ? "true" : "false";
1017 fprintf(out, "bool %s %s;\n", boolean->datum.fqn, value);
1018 }
1019 }
1020
1021 static void cil_typealiases_to_policy(FILE *out, struct cil_list *types, struct cil_list *all_aliases)
1022 {
1023 struct cil_list_item *i1, *i2;
1024 struct cil_type *type;
1025 struct cil_list *aliases = NULL;
1026 struct cil_alias *alias;
1027 struct cil_type *actual;
1028 int num_aliases;
1029
1030 cil_list_for_each(i1, types) {
1031 type = i1->data;
1032 num_aliases = 0;
1033 cil_list_for_each(i2, all_aliases) {
1034 alias = i2->data;
1035 actual = alias->actual;
1036 if (type == actual) {
1037 if (num_aliases == 0) {
1038 cil_list_init(&aliases, CIL_LIST);
1039 }
1040 cil_list_append(aliases, CIL_TYPEALIAS, alias);
1041 num_aliases++;
1042 }
1043 }
1044 if (num_aliases > 0) {
1045 fprintf(out, "typealias %s alias", type->datum.fqn);
1046 if (num_aliases > 1) {
1047 fprintf(out, " {");
1048 }
1049 cil_list_for_each(i2, aliases) {
1050 alias = i2->data;
1051 fprintf(out, " %s", alias->datum.fqn);
1052 }
1053 if (num_aliases > 1) {
1054 fprintf(out, " }");
1055 }
1056 fprintf(out, ";\n");
1057 cil_list_destroy(&aliases, CIL_FALSE);
1058 }
1059 }
1060 }
1061
1062 static void cil_typebounds_to_policy(FILE *out, struct cil_list *types)
1063 {
1064 struct cil_list_item *i1;
1065 struct cil_type *child;
1066 struct cil_type *parent;
1067
1068 cil_list_for_each(i1, types) {
1069 child = i1->data;
1070 if (child->bounds != NULL) {
1071 parent = child->bounds;
1072 fprintf(out, "typebounds %s %s;\n", parent->datum.fqn, child->datum.fqn);
1073 }
1074 }
1075 }
1076
1077 static void cil_typeattributes_to_policy(FILE *out, struct cil_list *types, struct cil_list *attributes)
1078 {
1079 struct cil_list_item *i1, *i2;
1080 struct cil_type *type;
1081 struct cil_typeattribute *attribute;
1082 int first = CIL_TRUE;
1083
1084 cil_list_for_each(i1, types) {
1085 type = i1->data;
1086 cil_list_for_each(i2, attributes) {
1087 attribute = i2->data;
1088 if (!attribute->keep)
1089 continue;
1090 if (ebitmap_get_bit(attribute->types, type->value)) {
1091 if (first) {
1092 fprintf(out, "typeattribute %s %s", type->datum.fqn, attribute->datum.fqn);
1093 first = CIL_FALSE;
1094 } else {
1095 fprintf(out, ", %s", attribute->datum.fqn);
1096 }
1097 }
1098 }
1099 if (!first) {
1100 fprintf(out, ";\n");
1101 first = CIL_TRUE;
1102 }
1103 }
1104 }
1105
1106 static void cil_xperms_to_policy(FILE *out, struct cil_permissionx *permx)
1107 {
1108 ebitmap_node_t *node;
1109 unsigned int i, first = 0, last = 0;
1110 int need_first = CIL_TRUE, need_last = CIL_TRUE;
1111 const char *kind;
1112
1113 if (permx->kind == CIL_PERMX_KIND_IOCTL) {
1114 kind = "ioctl";
1115 } else if (permx->kind == CIL_PERMX_KIND_NLMSG) {
1116 kind = "nlmsg";
1117 } else {
1118 kind = "???";
1119 }
1120
1121 fprintf(out, "%s %s {", DATUM(permx->obj)->fqn, kind);
1122
1123 ebitmap_for_each_positive_bit(permx->perms, node, i) {
1124 if (need_first == CIL_TRUE) {
1125 first = i;
1126 need_first = CIL_FALSE;
1127 } else if (need_last == CIL_TRUE) {
1128 if (i == first+1) {
1129 last = i;
1130 need_last = CIL_FALSE;
1131 } else {
1132 fprintf(out, " 0x%x", first);
1133 first = i;
1134 }
1135 } else if (i == last+1) {
1136 last = i;
1137 } else {
1138 if (last > first+1) {
1139 fprintf(out, " 0x%x-0x%x", first, last);
1140 } else {
1141 fprintf(out, " 0x%x 0x%x", first, last);
1142 }
1143 first = i;
1144 need_last = CIL_TRUE;
1145 }
1146 }
1147 if (need_first == CIL_FALSE) {
1148 if (need_last == CIL_FALSE) {
1149 fprintf(out, " 0x%x-0x%x", first, last);
1150 } else {
1151 fprintf(out, " 0x%x", first);
1152 }
1153 }
1154 fprintf(out," }");
1155 }
1156
1157 static void cil_av_rulex_to_policy(FILE *out, struct cil_avrule *rule)
1158 {
1159 const char *kind;
1160 struct cil_symtab_datum *src, *tgt;
1161
1162 src = rule->src;
1163 tgt = rule->tgt;
1164
1165 switch (rule->rule_kind) {
1166 case CIL_AVRULE_ALLOWED:
1167 kind = "allowxperm";
1168 break;
1169 case CIL_AVRULE_AUDITALLOW:
1170 kind = "auditallowxperm";
1171 break;
1172 case CIL_AVRULE_DONTAUDIT:
1173 kind = "dontauditxperm";
1174 break;
1175 case CIL_AVRULE_NEVERALLOW:
1176 kind = "neverallowxperm";
1177 break;
1178 default:
1179 kind = "???";
1180 break;
1181 }
1182
1183 fprintf(out, "%s %s %s : ", kind, src->fqn, tgt->fqn);
1184 cil_xperms_to_policy(out, rule->perms.x.permx);
1185 fprintf(out, ";\n");
1186 }
1187
1188 static void cil_av_rule_to_policy(FILE *out, struct cil_avrule *rule)
1189 {
1190 const char *kind;
1191 struct cil_symtab_datum *src, *tgt;
1192 struct cil_list *classperms_strs;
1193 struct cil_list_item *i1;
1194
1195 src = rule->src;
1196 tgt = rule->tgt;
1197
1198 switch (rule->rule_kind) {
1199 case CIL_AVRULE_ALLOWED:
1200 kind = "allow";
1201 break;
1202 case CIL_AVRULE_AUDITALLOW:
1203 kind = "auditallow";
1204 break;
1205 case CIL_AVRULE_DONTAUDIT:
1206 kind = "dontaudit";
1207 break;
1208 case CIL_AVRULE_NEVERALLOW:
1209 kind = "neverallow";
1210 break;
1211 default:
1212 kind = "???";
1213 break;
1214 }
1215
1216 cil_list_init(&classperms_strs, CIL_LIST);
1217 cil_classperms_to_strings(rule->perms.classperms, classperms_strs);
1218 cil_list_for_each(i1, classperms_strs) {
1219 char *cp_str = i1->data;
1220 fprintf(out, "%s %s %s : %s;\n", kind, src->fqn, tgt->fqn, cp_str);
1221 free(cp_str);
1222 }
1223 cil_list_destroy(&classperms_strs, CIL_FALSE);
1224 }
1225
1226 static void cil_type_rule_to_policy(FILE *out, struct cil_type_rule *rule)
1227 {
1228 const char *kind;
1229 struct cil_symtab_datum *src, *tgt, *res;
1230 struct cil_list *class_list;
1231 struct cil_list_item *i1;
1232
1233 src = rule->src;
1234 tgt = rule->tgt;
1235 res = rule->result;
1236
1237 switch (rule->rule_kind) {
1238 case CIL_TYPE_TRANSITION:
1239 kind = "type_transition";
1240 break;
1241 case CIL_TYPE_MEMBER:
1242 kind = "type_member";
1243 break;
1244 case CIL_TYPE_CHANGE:
1245 kind = "type_change";
1246 break;
1247 default:
1248 kind = "???";
1249 break;
1250 }
1251
1252 class_list = cil_expand_class(rule->obj);
1253 cil_list_for_each(i1, class_list) {
1254 fprintf(out, "%s %s %s : %s %s;\n", kind, src->fqn, tgt->fqn, DATUM(i1->data)->fqn, res->fqn);
1255 }
1256 cil_list_destroy(&class_list, CIL_FALSE);
1257 }
1258
1259 static void cil_nametypetransition_to_policy(FILE *out, struct cil_nametypetransition *trans)
1260 {
1261 struct cil_symtab_datum *src, *tgt, *name, *res;
1262 struct cil_list *class_list;
1263 struct cil_list_item *i1;
1264
1265 src = trans->src;
1266 tgt = trans->tgt;
1267 name = trans->name;
1268 res = trans->result;
1269
1270 class_list = cil_expand_class(trans->obj);
1271 cil_list_for_each(i1, class_list) {
1272 fprintf(out, "type_transition %s %s : %s %s \"%s\";\n", src->fqn, tgt->fqn, DATUM(i1->data)->fqn, res->fqn, name->fqn);
1273 }
1274 cil_list_destroy(&class_list, CIL_FALSE);
1275 }
1276
1277 static void cil_rangetransition_to_policy(FILE *out, struct cil_rangetransition *trans)
1278 {
1279 struct cil_symtab_datum *src, *exec;
1280 struct cil_list *class_list;
1281 struct cil_list_item *i1;
1282
1283 src = trans->src;
1284 exec = trans->exec;
1285
1286 class_list = cil_expand_class(trans->obj);
1287 cil_list_for_each(i1, class_list) {
1288 fprintf(out, "range_transition %s %s : %s ", src->fqn, exec->fqn, DATUM(i1->data)->fqn);
1289 cil_levelrange_to_policy(out, trans->range);
1290 fprintf(out, ";\n");
1291 }
1292 cil_list_destroy(&class_list, CIL_FALSE);
1293 }
1294
1295 static void cil_typepermissive_to_policy(FILE *out, struct cil_typepermissive *rule)
1296 {
1297 fprintf(out, "permissive %s;\n", DATUM(rule->type)->fqn);
1298 }
1299
1300 static void cil_typeneveraudit_to_policy(FILE *out, struct cil_typeneveraudit *rule)
1301 {
1302 fprintf(out, "neveraudit %s;\n", DATUM(rule->type)->fqn);
1303 }
1304
1305 struct block_te_rules_extra {
1306 FILE *out;
1307 enum cil_flavor flavor;
1308 uint32_t rule_kind;
1309 };
1310
1311 static int __cil_block_te_rules_to_policy_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1312 {
1313 struct block_te_rules_extra *args = extra_args;
1314
1315 switch (node->flavor) {
1316 case CIL_BLOCK: {
1317 struct cil_block *blk = node->data;
1318 if (blk->is_abstract == CIL_TRUE) {
1319 *finished = CIL_TREE_SKIP_HEAD;
1320 }
1321 break;
1322 }
1323 case CIL_MACRO:
1324 *finished = CIL_TREE_SKIP_HEAD;
1325 break;
1326 case CIL_BOOLEANIF:
1327 *finished = CIL_TREE_SKIP_HEAD;
1328 break;
1329 case CIL_AVRULE:
1330 case CIL_AVRULEX:
1331 if (args->flavor == node->flavor) {
1332 struct cil_avrule *rule = node->data;
1333 if (args->rule_kind == rule->rule_kind) {
1334 if (rule->is_extended) {
1335 cil_av_rulex_to_policy(args->out, rule);
1336 } else {
1337 cil_av_rule_to_policy(args->out, rule);
1338 }
1339 }
1340 }
1341 break;
1342 case CIL_TYPE_RULE:
1343 if (args->flavor == node->flavor) {
1344 struct cil_type_rule *rule = node->data;
1345 if (args->rule_kind == rule->rule_kind) {
1346 cil_type_rule_to_policy(args->out, rule);
1347 }
1348 }
1349
1350 break;
1351 case CIL_NAMETYPETRANSITION:
1352 if (args->flavor == node->flavor) {
1353 cil_nametypetransition_to_policy(args->out, node->data);
1354 }
1355 break;
1356 case CIL_RANGETRANSITION:
1357 if (args->flavor == node->flavor) {
1358 cil_rangetransition_to_policy(args->out, node->data);
1359 }
1360
1361 break;
1362 case CIL_TYPEPERMISSIVE:
1363 if (args->flavor == node->flavor) {
1364 cil_typepermissive_to_policy(args->out, node->data);
1365 }
1366 break;
1367 case CIL_TYPENEVERAUDIT:
1368 if (args->flavor == node->flavor) {
1369 cil_typeneveraudit_to_policy(args->out, node->data);
1370 }
1371 break;
1372 default:
1373 break;
1374 }
1375
1376 return SEPOL_OK;
1377 }
1378
1379 static void cil_block_te_rules_to_policy(FILE *out, struct cil_tree_node *start, int mls)
1380 {
1381 struct block_te_rules_extra args;
1382
1383 args.out = out;
1384
1385 args.flavor = CIL_TYPEPERMISSIVE;
1386 args.rule_kind = 0;
1387 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1388
1389 args.flavor = CIL_TYPENEVERAUDIT;
1390 args.rule_kind = 0;
1391 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1392
1393 args.flavor = CIL_AVRULE;
1394 args.rule_kind = CIL_AVRULE_ALLOWED;
1395 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1396 args.rule_kind = CIL_AVRULE_AUDITALLOW;
1397 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1398 args.rule_kind = CIL_AVRULE_DONTAUDIT;
1399 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1400 args.rule_kind = CIL_AVRULE_NEVERALLOW;
1401 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1402
1403 args.flavor = CIL_AVRULEX;
1404 args.rule_kind = CIL_AVRULE_ALLOWED;
1405 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1406 args.rule_kind = CIL_AVRULE_AUDITALLOW;
1407 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1408 args.rule_kind = CIL_AVRULE_DONTAUDIT;
1409 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1410 args.rule_kind = CIL_AVRULE_NEVERALLOW;
1411 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1412
1413 args.flavor = CIL_TYPE_RULE;
1414 args.rule_kind = CIL_TYPE_TRANSITION;
1415 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1416 args.rule_kind = CIL_TYPE_MEMBER;
1417 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1418 args.rule_kind = CIL_TYPE_CHANGE;
1419 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1420 args.rule_kind = CIL_AVRULE_TYPE;
1421 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1422
1423 args.flavor = CIL_NAMETYPETRANSITION;
1424 args.rule_kind = 0;
1425 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1426
1427 if (mls == CIL_TRUE) {
1428 args.flavor = CIL_RANGETRANSITION;
1429 args.rule_kind = 0;
1430 cil_tree_walk(start, __cil_block_te_rules_to_policy_helper, NULL, NULL, &args);
1431 }
1432 }
1433
1434 struct te_rules_extra {
1435 FILE *out;
1436 int mls;
1437 };
1438
1439 static int __cil_te_rules_to_policy_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1440 {
1441 struct te_rules_extra *args = extra_args;
1442
1443 switch (node->flavor) {
1444 case CIL_BLOCK: {
1445 struct cil_block *blk = node->data;
1446 if (blk->is_abstract == CIL_TRUE) {
1447 *finished = CIL_TREE_SKIP_HEAD;
1448 }
1449 break;
1450 }
1451 case CIL_MACRO:
1452 *finished = CIL_TREE_SKIP_HEAD;
1453 break;
1454 case CIL_BOOLEANIF: {
1455 struct cil_booleanif *boolean = node->data;
1456 struct cil_tree_node *n;
1457 struct cil_condblock *cb;
1458
1459 fprintf(args->out, "if ");
1460 cil_cond_expr_to_policy(args->out, boolean->datum_expr, CIL_TRUE);
1461 fprintf(args->out," {\n");
1462 n = node->cl_head;
1463 cb = n != NULL ? n->data : NULL;
1464 if (cb && cb->flavor == CIL_CONDTRUE) {
1465 cil_block_te_rules_to_policy(args->out, n, args->mls);
1466 n = n->next;
1467 cb = n != NULL ? n->data : NULL;
1468 }
1469 if (cb && cb->flavor == CIL_CONDFALSE) {
1470 fprintf(args->out,"} else {\n");
1471 cil_block_te_rules_to_policy(args->out, n, args->mls);
1472 }
1473 fprintf(args->out,"}\n");
1474 *finished = CIL_TREE_SKIP_HEAD;
1475 break;
1476 }
1477 default:
1478 break;
1479 }
1480
1481 return SEPOL_OK;
1482 }
1483
1484 static void cil_te_rules_to_policy(FILE *out, struct cil_tree_node *head, int mls)
1485 {
1486 struct te_rules_extra args;
1487
1488 args.out = out;
1489 args.mls = mls;
1490
1491 cil_block_te_rules_to_policy(out, head, mls);
1492 cil_tree_walk(head, __cil_te_rules_to_policy_helper, NULL, NULL, &args);
1493 }
1494
1495 static void cil_roles_to_policy(FILE *out, struct cil_list *rules)
1496 {
1497 struct cil_list_item *i1;
1498 struct cil_role *role;
1499
1500 cil_list_for_each(i1, rules) {
1501 role = i1->data;
1502 if (strcmp(role->datum.fqn,"object_r") == 0)
1503 continue;
1504 fprintf(out, "role %s;\n", role->datum.fqn);
1505 }
1506 }
1507
1508 static void cil_role_types_to_policy(FILE *out, struct cil_list *roles, struct cil_list *types)
1509 {
1510 struct cil_list_item *i1, *i2;
1511 struct cil_role *role;
1512 struct cil_type *type;
1513 int first = CIL_TRUE;
1514
1515 cil_list_for_each(i1, roles) {
1516 role = i1->data;
1517 if (strcmp(role->datum.fqn,"object_r") == 0)
1518 continue;
1519 if (role->types) {
1520 cil_list_for_each(i2, types) {
1521 type = i2->data;
1522 if (ebitmap_get_bit(role->types, type->value)) {
1523 if (first) {
1524 fprintf(out, "role %s types { %s", role->datum.fqn, type->datum.fqn);
1525 first = CIL_FALSE;
1526 } else {
1527 fprintf(out, " %s", type->datum.fqn);
1528 }
1529 }
1530 }
1531 if (!first) {
1532 fprintf(out, " }");
1533 first = CIL_TRUE;
1534 }
1535 fprintf(out, ";\n");
1536 }
1537 }
1538 }
1539
1540 static void cil_roleattributes_to_policy(FILE *out, struct cil_list *roles, struct cil_list *attributes)
1541 {
1542 struct cil_list_item *i1, *i2;
1543 struct cil_role *role;
1544 struct cil_roleattribute *attribute;
1545 int first = CIL_TRUE;
1546
1547 cil_list_for_each(i1, roles) {
1548 role = i1->data;
1549 if (strcmp(role->datum.fqn,"object_r") == 0)
1550 continue;
1551 cil_list_for_each(i2, attributes) {
1552 attribute = i2->data;
1553 if (ebitmap_get_bit(attribute->roles, role->value)) {
1554 if (first) {
1555 fprintf(out, "roleattribute %s %s", role->datum.fqn, attribute->datum.fqn);
1556 first = CIL_FALSE;
1557 } else {
1558 fprintf(out, ", %s", attribute->datum.fqn);
1559 }
1560 }
1561 }
1562 if (!first) {
1563 fprintf(out, ";\n");
1564 first = CIL_TRUE;
1565 }
1566 }
1567 }
1568
1569 static void cil_roleallows_to_policy(FILE *out, struct cil_list *roleallows)
1570 {
1571 struct cil_list_item *i1;
1572 struct cil_roleallow *allow;
1573
1574 cil_list_for_each(i1, roleallows) {
1575 allow = i1->data;
1576 fprintf(out, "allow %s %s;\n", DATUM(allow->src)->fqn, DATUM(allow->tgt)->fqn);
1577 }
1578 }
1579
1580 static void cil_roletransitions_to_policy(FILE *out, struct cil_list *roletransitions)
1581 {
1582 struct cil_list_item *i1, *i2;
1583 struct cil_list *class_list;
1584 struct cil_roletransition *trans;
1585
1586
1587 cil_list_for_each(i1, roletransitions) {
1588 trans = i1->data;
1589 class_list = cil_expand_class(trans->obj);
1590 cil_list_for_each(i2, class_list) {
1591 fprintf(out, "role_transition %s %s : %s %s;\n", DATUM(trans->src)->fqn, DATUM(trans->tgt)->fqn, DATUM(i2->data)->fqn, DATUM(trans->result)->fqn);
1592 }
1593 cil_list_destroy(&class_list, CIL_FALSE);
1594 }
1595 }
1596
1597 static void cil_users_to_policy(FILE *out, int mls, struct cil_list *users, struct cil_list *all_roles)
1598 {
1599 struct cil_list_item *i1, *i2;
1600 struct cil_user *user;
1601 struct cil_list *roles = NULL;
1602 struct cil_role *role;
1603 int num_roles;
1604
1605 cil_list_for_each(i1, users) {
1606 user = i1->data;
1607 num_roles = 0;
1608 fprintf(out, "user %s",user->datum.fqn);
1609 cil_list_for_each(i2, all_roles) {
1610 role = i2->data;
1611 if (ebitmap_get_bit(user->roles, role->value)) {
1612 if (num_roles == 0) {
1613 cil_list_init(&roles, CIL_LIST);
1614 }
1615 cil_list_append(roles, CIL_ROLE, role);
1616 num_roles++;
1617 }
1618 }
1619 if (num_roles > 0) {
1620 fprintf(out, " roles");
1621 if (num_roles > 1) {
1622 fprintf(out, " {");
1623 }
1624 cil_list_for_each(i2, roles) {
1625 role = i2->data;
1626 fprintf(out, " %s", role->datum.fqn);
1627 }
1628 if (num_roles > 1) {
1629 fprintf(out, " }");
1630 }
1631 cil_list_destroy(&roles, CIL_FALSE);
1632 }
1633
1634 if (mls == CIL_TRUE && user->dftlevel != NULL) {
1635 fprintf(out, " level ");
1636 cil_level_to_policy(out, user->dftlevel);
1637 }
1638
1639 if (mls == CIL_TRUE && user->range != NULL) {
1640 fprintf(out, " range ");
1641 cil_levelrange_to_policy(out, user->range);
1642 }
1643
1644 fprintf(out,";\n");
1645 }
1646 }
1647
1648 static void cil_constrains_to_policy(FILE *out, struct cil_db *db, struct cil_list *constrains)
1649 {
1650 struct cil_list_item *i1, *i2;
1651 struct cil_constrain *cons;
1652 struct cil_list *classperms_strs;
1653 char *cp_str;
1654 char *expr_str;
1655
1656 cil_list_for_each(i1, constrains) {
1657 cons = i1->data;
1658 cil_list_init(&classperms_strs, CIL_LIST);
1659 cil_classperms_to_strings(cons->classperms, classperms_strs);
1660 expr_str = cil_cons_expr_to_string(db, cons->datum_expr);
1661 cil_list_for_each(i2, classperms_strs) {
1662 cp_str = i2->data;
1663 fprintf(out, "constrain %s %s;\n",cp_str, expr_str);
1664 free(cp_str);
1665 }
1666 free(expr_str);
1667 cil_list_destroy(&classperms_strs, CIL_FALSE);
1668 }
1669 }
1670
1671 static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls)
1672 {
1673 struct cil_list_item *i1;
1674 struct cil_sid *sid;
1675
1676 cil_list_for_each(i1, sids) {
1677 sid = i1->data;
1678 if (sid->context) {
1679 fprintf(out, "sid %s ", sid->datum.fqn);
1680 cil_context_to_policy(out, sid->context, mls);
1681 fprintf(out,"\n");
1682 }
1683 }
1684 }
1685
1686 static void cil_fsuses_to_policy(FILE *out, struct cil_sort *fsuses, int mls)
1687 {
1688 unsigned i;
1689 struct cil_fsuse *fsuse;
1690
1691 for (i=0; i<fsuses->count; i++) {
1692 fsuse = fsuses->array[i];
1693 if (fsuse->type == CIL_FSUSE_XATTR) {
1694 fprintf(out, "fs_use_xattr %s ", fsuse->fs_str);
1695 cil_context_to_policy(out, fsuse->context, mls);
1696 fprintf(out,";\n");
1697 }
1698 }
1699
1700 for (i=0; i<fsuses->count; i++) {
1701 fsuse = fsuses->array[i];
1702 if (fsuse->type == CIL_FSUSE_TASK) {
1703 fprintf(out, "fs_use_task %s ", fsuse->fs_str);
1704 cil_context_to_policy(out, fsuse->context, mls);
1705 fprintf(out,";\n");
1706 }
1707 }
1708
1709 for (i=0; i<fsuses->count; i++) {
1710 fsuse = fsuses->array[i];
1711 if (fsuse->type == CIL_FSUSE_TRANS) {
1712 fprintf(out, "fs_use_trans %s ", fsuse->fs_str);
1713 cil_context_to_policy(out, fsuse->context, mls);
1714 fprintf(out,";\n");
1715 }
1716 }
1717 }
1718
1719 static void cil_genfscons_to_policy(FILE *out, struct cil_sort *genfscons, int mls)
1720 {
1721 unsigned i;
1722 struct cil_genfscon *genfscon;
1723
1724 for (i=0; i<genfscons->count; i++) {
1725 genfscon = genfscons->array[i];
1726 fprintf(out, "genfscon %s %s ", genfscon->fs_str, genfscon->path_str);
1727 cil_context_to_policy(out, genfscon->context, mls);
1728 fprintf(out, "\n");
1729 }
1730 }
1731
1732 static void cil_ibpkeycons_to_policy(FILE *out, struct cil_sort *ibpkeycons, int mls)
1733 {
1734 uint32_t i = 0;
1735
1736 for (i = 0; i < ibpkeycons->count; i++) {
1737 struct cil_ibpkeycon *ibpkeycon = (struct cil_ibpkeycon *)ibpkeycons->array[i];
1738
1739 fprintf(out, "ibpkeycon %s ", ibpkeycon->subnet_prefix_str);
1740 fprintf(out, "%d ", ibpkeycon->pkey_low);
1741 fprintf(out, "%d ", ibpkeycon->pkey_high);
1742 cil_context_to_policy(out, ibpkeycon->context, mls);
1743 fprintf(out, "\n");
1744 }
1745 }
1746
1747 static void cil_ibendportcons_to_policy(FILE *out, struct cil_sort *ibendportcons, int mls)
1748 {
1749 uint32_t i;
1750
1751 for (i = 0; i < ibendportcons->count; i++) {
1752 struct cil_ibendportcon *ibendportcon = (struct cil_ibendportcon *)ibendportcons->array[i];
1753
1754 fprintf(out, "ibendportcon %s ", ibendportcon->dev_name_str);
1755 fprintf(out, "%u ", ibendportcon->port);
1756 cil_context_to_policy(out, ibendportcon->context, mls);
1757 fprintf(out, "\n");
1758 }
1759 }
1760
1761 static void cil_portcons_to_policy(FILE *out, struct cil_sort *portcons, int mls)
1762 {
1763 unsigned i;
1764 struct cil_portcon *portcon;
1765
1766 for (i=0; i<portcons->count; i++) {
1767 portcon = portcons->array[i];
1768 fprintf(out, "portcon ");
1769 if (portcon->proto == CIL_PROTOCOL_UDP) {
1770 fprintf(out, "udp ");
1771 } else if (portcon->proto == CIL_PROTOCOL_TCP) {
1772 fprintf(out, "tcp ");
1773 } else if (portcon->proto == CIL_PROTOCOL_DCCP) {
1774 fprintf(out, "dccp ");
1775 } else if (portcon->proto == CIL_PROTOCOL_SCTP) {
1776 fprintf(out, "sctp ");
1777 }
1778 if (portcon->port_low == portcon->port_high) {
1779 fprintf(out, "%d ", portcon->port_low);
1780 } else {
1781 fprintf(out, "%d-%d ", portcon->port_low, portcon->port_high);
1782 }
1783 cil_context_to_policy(out, portcon->context, mls);
1784 fprintf(out, "\n");
1785 }
1786 }
1787
1788 static void cil_netifcons_to_policy(FILE *out, struct cil_sort *netifcons, int mls)
1789 {
1790 unsigned i;
1791 struct cil_netifcon *netifcon;
1792
1793 for (i=0; i<netifcons->count; i++) {
1794 netifcon = netifcons->array[i];
1795 fprintf(out, "netifcon %s ", netifcon->interface_str);
1796 cil_context_to_policy(out, netifcon->if_context, mls);
1797 fprintf(out, " ");
1798 cil_context_to_policy(out, netifcon->packet_context, mls);
1799 fprintf(out, "\n");
1800 }
1801 }
1802
1803 static void cil_nodecons_to_policy(FILE *out, struct cil_sort *nodecons, int mls)
1804 {
1805 unsigned i;
1806 struct cil_nodecon *nodecon;
1807 char *addr, *mask;
1808
1809 for (i=0; i<nodecons->count; i++) {
1810 nodecon = nodecons->array[i];
1811 fprintf(out, "nodecon ");
1812
1813 if (nodecon->addr->family == AF_INET) {
1814 errno = 0;
1815 addr = cil_malloc(INET_ADDRSTRLEN);
1816 inet_ntop(nodecon->addr->family, &nodecon->addr->ip.v4, addr, INET_ADDRSTRLEN);
1817 if (errno == 0) {
1818 fprintf(out, "%s ",addr);
1819 } else {
1820 fprintf(out, "[INVALID] ");
1821 }
1822 free(addr);
1823
1824 errno = 0;
1825 mask = cil_malloc(INET_ADDRSTRLEN);
1826 inet_ntop(nodecon->mask->family, &nodecon->mask->ip.v4, mask, INET_ADDRSTRLEN);
1827 if (errno == 0) {
1828 fprintf(out, "%s ",mask);
1829 } else {
1830 fprintf(out, "[INVALID] ");
1831 }
1832 free(mask);
1833 } else {
1834 errno = 0;
1835 addr = cil_malloc(INET6_ADDRSTRLEN);
1836 inet_ntop(nodecon->addr->family, &nodecon->addr->ip.v6, addr, INET6_ADDRSTRLEN);
1837 if (errno == 0) {
1838 fprintf(out, "%s ",addr);
1839 } else {
1840 fprintf(out, "[INVALID] ");
1841 }
1842 free(addr);
1843
1844 errno = 0;
1845 mask = cil_malloc(INET6_ADDRSTRLEN);
1846 inet_ntop(nodecon->mask->family, &nodecon->mask->ip.v6, mask, INET6_ADDRSTRLEN);
1847 if (errno == 0) {
1848 fprintf(out, "%s ",mask);
1849 } else {
1850 fprintf(out, "[INVALID] ");
1851 }
1852 free(mask);
1853 }
1854
1855 cil_context_to_policy(out, nodecon->context, mls);
1856 fprintf(out, "\n");
1857 }
1858 }
1859
1860 static void cil_pirqcons_to_policy(FILE *out, struct cil_sort *pirqcons, int mls)
1861 {
1862 unsigned i;
1863 struct cil_pirqcon *pirqcon;
1864
1865 for (i = 0; i<pirqcons->count; i++) {
1866 pirqcon = pirqcons->array[i];
1867 fprintf(out, "pirqcon %d ", pirqcon->pirq);
1868 cil_context_to_policy(out, pirqcon->context, mls);
1869 fprintf(out, ";\n");
1870 }
1871 }
1872
1873 static void cil_iomemcons_to_policy(FILE *out, struct cil_sort *iomemcons, int mls)
1874 {
1875 unsigned i;
1876 struct cil_iomemcon *iomemcon;
1877
1878 for (i = 0; i<iomemcons->count; i++) {
1879 iomemcon = iomemcons->array[i];
1880 if (iomemcon->iomem_low == iomemcon->iomem_high) {
1881 fprintf(out, "iomemcon %"PRIx64" ", iomemcon->iomem_low);
1882 } else {
1883 fprintf(out, "iomemcon %"PRIx64"-%"PRIx64" ", iomemcon->iomem_low, iomemcon->iomem_high);
1884 }
1885 cil_context_to_policy(out, iomemcon->context, mls);
1886 fprintf(out, ";\n");
1887 }
1888 }
1889
1890 static void cil_ioportcons_to_policy(FILE *out, struct cil_sort *ioportcons, int mls)
1891 {
1892 unsigned i;
1893 struct cil_ioportcon *ioportcon;
1894
1895 for (i = 0; i < ioportcons->count; i++) {
1896 ioportcon = ioportcons->array[i];
1897 fprintf(out, "ioportcon 0x%x-0x%x ", ioportcon->ioport_low, ioportcon->ioport_high);
1898 cil_context_to_policy(out, ioportcon->context, mls);
1899 fprintf(out, ";\n");
1900 }
1901 }
1902
1903 static void cil_pcidevicecons_to_policy(FILE *out, struct cil_sort *pcidevicecons, int mls)
1904 {
1905 unsigned i;
1906 struct cil_pcidevicecon *pcidevicecon;
1907
1908 for (i = 0; i < pcidevicecons->count; i++) {
1909 pcidevicecon = pcidevicecons->array[i];
1910 fprintf(out, "pcidevicecon 0x%x ", pcidevicecon->dev);
1911 cil_context_to_policy(out, pcidevicecon->context, mls);
1912 fprintf(out, ";\n");
1913 }
1914 }
1915
1916 static void cil_devicetreecons_to_policy(FILE *out, struct cil_sort *devicetreecons, int mls)
1917 {
1918 unsigned i;
1919 struct cil_devicetreecon *devicetreecon;
1920
1921 for (i = 0; i < devicetreecons->count; i++) {
1922 devicetreecon = devicetreecons->array[i];
1923 fprintf(out, "devicetreecon %s ", devicetreecon->path);
1924 cil_context_to_policy(out, devicetreecon->context, mls);
1925 fprintf(out, ";\n");
1926 }
1927 }
1928
1929 void cil_gen_policy(FILE *out, struct cil_db *db)
1930 {
1931 unsigned i;
1932 struct cil_tree_node *head = db->ast->root;
1933 struct cil_list *lists[CIL_LIST_NUM_LISTS];
1934
1935 for (i=0; i<CIL_LIST_NUM_LISTS; i++) {
1936 cil_list_init(&lists[i], CIL_LIST);
1937 }
1938
1939 cil_gather_statements(head, lists);
1940
1941 cil_class_decls_to_policy(out, db->classorder);
1942
1943 cil_sid_decls_to_policy(out, db->sidorder);
1944
1945 cil_commons_to_policy(out, lists[CIL_LIST_COMMON]);
1946 cil_classes_to_policy(out, db->classorder);
1947
1948 cil_defaults_to_policy(out, lists[CIL_LIST_DEFAULT_USER], "default_user");
1949 cil_defaults_to_policy(out, lists[CIL_LIST_DEFAULT_ROLE], "default_role");
1950 cil_defaults_to_policy(out, lists[CIL_LIST_DEFAULT_TYPE], "default_type");
1951
1952 if (db->mls == CIL_TRUE) {
1953 cil_default_ranges_to_policy(out, lists[CIL_LIST_DEFAULT_RANGE]);
1954 cil_sensitivities_to_policy(out, db->sensitivityorder, lists[CIL_LIST_SENSALIAS]);
1955 cil_dominance_to_policy(out, db->sensitivityorder);
1956 cil_categories_to_policy(out, db->catorder, lists[CIL_LIST_CATALIAS]);
1957 cil_levels_to_policy(out, db->sensitivityorder);
1958 cil_mlsconstrains_to_policy(out, db, lists[CIL_LIST_MLSCONSTRAIN]);
1959 cil_validatetrans_to_policy(out, db, lists[CIL_LIST_MLSVALIDATETRANS], CIL_KEY_MLSVALIDATETRANS);
1960 }
1961
1962 cil_simple_rules_to_policy(out, lists[CIL_LIST_POLICYCAP], CIL_KEY_POLICYCAP);
1963
1964 cil_simple_rules_to_policy(out, lists[CIL_LIST_TYPEATTRIBUTE], "attribute");
1965 cil_simple_rules_to_policy(out, lists[CIL_LIST_ROLEATTRIBUTE], "attribute_role");
1966
1967 cil_bools_to_policy(out, lists[CIL_LIST_BOOL]);
1968
1969 cil_simple_rules_to_policy(out, lists[CIL_LIST_TYPE], "type");
1970 cil_typealiases_to_policy(out, lists[CIL_LIST_TYPE], lists[CIL_LIST_TYPEALIAS]);
1971 cil_typebounds_to_policy(out, lists[CIL_LIST_TYPE]);
1972 cil_typeattributes_to_policy(out, lists[CIL_LIST_TYPE], lists[CIL_LIST_TYPEATTRIBUTE]);
1973 cil_te_rules_to_policy(out, head, db->mls);
1974
1975 cil_roles_to_policy(out, lists[CIL_LIST_ROLE]);
1976 cil_role_types_to_policy(out, lists[CIL_LIST_ROLE], lists[CIL_LIST_TYPE]);
1977 cil_roleattributes_to_policy(out, lists[CIL_LIST_ROLE], lists[CIL_LIST_ROLEATTRIBUTE]);
1978 cil_roleallows_to_policy(out, lists[CIL_LIST_ROLEALLOW]);
1979 cil_roletransitions_to_policy(out, lists[CIL_LIST_ROLETRANSITION]);
1980
1981 cil_users_to_policy(out, db->mls, lists[CIL_LIST_USER], lists[CIL_LIST_ROLE]);
1982
1983 cil_constrains_to_policy(out, db, lists[CIL_LIST_CONSTRAINT]);
1984 cil_validatetrans_to_policy(out, db, lists[CIL_LIST_VALIDATETRANS], CIL_KEY_VALIDATETRANS);
1985
1986 cil_sid_contexts_to_policy(out, db->sidorder, db->mls);
1987 cil_fsuses_to_policy(out, db->fsuse, db->mls);
1988 cil_genfscons_to_policy(out, db->genfscon, db->mls);
1989 cil_portcons_to_policy(out, db->portcon, db->mls);
1990 cil_netifcons_to_policy(out, db->netifcon, db->mls);
1991 cil_ibpkeycons_to_policy(out, db->ibpkeycon, db->mls);
1992 cil_ibendportcons_to_policy(out, db->ibendportcon, db->mls);
1993 cil_nodecons_to_policy(out, db->nodecon, db->mls);
1994 cil_pirqcons_to_policy(out, db->pirqcon, db->mls);
1995 cil_iomemcons_to_policy(out, db->iomemcon, db->mls);
1996 cil_ioportcons_to_policy(out, db->ioportcon, db->mls);
1997 cil_pcidevicecons_to_policy(out, db->pcidevicecon, db->mls);
1998 cil_devicetreecons_to_policy(out, db->devicetreecon, db->mls);
1999
2000 for (i=0; i<CIL_LIST_NUM_LISTS; i++) {
2001 cil_list_destroy(&lists[i], CIL_FALSE);
2002 }
2003
2004 }