Debian Codegraph @

cil/src/cil_binary.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 <assert.h>
33 #include <netinet/in.h>
34 #ifndef IPPROTO_DCCP
35 #define IPPROTO_DCCP 33
36 #endif
37 #ifndef IPPROTO_SCTP
38 #define IPPROTO_SCTP 132
39 #endif
40
41 #include <sepol/policydb/policydb.h>
42 #include <sepol/policydb/polcaps.h>
43 #include <sepol/policydb/conditional.h>
44 #include <sepol/policydb/constraint.h>
45 #include <sepol/policydb/expand.h>
46 #include <sepol/policydb/hierarchy.h>
47
48 #include "cil_internal.h"
49 #include "cil_flavor.h"
50 #include "cil_log.h"
51 #include "cil_mem.h"
52 #include "cil_tree.h"
53 #include "cil_binary.h"
54 #include "cil_symtab.h"
55 #include "cil_find.h"
56 #include "cil_build_ast.h"
57
58 #define ROLE_TRANS_TABLE_SIZE (1 << 10)
59 #define AVRULEX_TABLE_SIZE (1 << 10)
60 #define PERMS_PER_CLASS 32
61
62 struct cil_args_xperm_tables {
63 hashtab_t ioctl;
64 hashtab_t nlmsg;
65 };
66
67 struct cil_args_binary {
68 const struct cil_db *db;
69 policydb_t *pdb;
70 struct cil_list *neverallows;
71 int pass;
72 hashtab_t role_trans_table;
73 struct cil_args_xperm_tables avrulex_xperm_tables;
74 void **type_value_to_cil;
75 };
76
77 struct cil_args_booleanif {
78 const struct cil_db *db;
79 policydb_t *pdb;
80 cond_node_t *cond_node;
81 enum cil_flavor cond_flavor;
82 struct cil_args_xperm_tables avrulex_xperm_tables;
83 };
84
85 static unsigned int avrulex_hash(__attribute__((unused)) hashtab_t h, const_hashtab_key_t key);
86 static int avrulex_compare(hashtab_t h
87 __attribute__ ((unused)), const_hashtab_key_t key1,
88 const_hashtab_key_t key2);
89
90 static int __cil_get_sepol_user_datum(policydb_t *pdb, struct cil_symtab_datum *datum, user_datum_t **sepol_user)
91 {
92 *sepol_user = hashtab_search(pdb->p_users.table, datum->fqn);
93 if (*sepol_user == NULL) {
94 cil_log(CIL_INFO, "Failed to find user %s in sepol hashtab\n", datum->fqn);
95 return SEPOL_ERR;
96 }
97
98 return SEPOL_OK;
99 }
100
101 static int __cil_get_sepol_role_datum(policydb_t *pdb, struct cil_symtab_datum *datum, role_datum_t **sepol_role)
102 {
103 *sepol_role = hashtab_search(pdb->p_roles.table, datum->fqn);
104 if (*sepol_role == NULL) {
105 cil_log(CIL_INFO, "Failed to find role %s in sepol hashtab\n", datum->fqn);
106 return SEPOL_ERR;
107 }
108
109 return SEPOL_OK;
110 }
111
112 static int __cil_get_sepol_type_datum(policydb_t *pdb, struct cil_symtab_datum *datum, type_datum_t **sepol_type)
113 {
114 *sepol_type = hashtab_search(pdb->p_types.table, datum->fqn);
115 if (*sepol_type == NULL) {
116 cil_log(CIL_INFO, "Failed to find type %s in sepol hashtab\n", datum->fqn);
117 return SEPOL_ERR;
118 }
119
120 return SEPOL_OK;
121 }
122
123 static int __cil_get_sepol_class_datum(policydb_t *pdb, struct cil_symtab_datum *datum, class_datum_t **sepol_class)
124 {
125 *sepol_class = hashtab_search(pdb->p_classes.table, datum->fqn);
126 if (*sepol_class == NULL) {
127 cil_log(CIL_INFO, "Failed to find class %s in sepol hashtab\n", datum->fqn);
128 return SEPOL_ERR;
129 }
130
131 return SEPOL_OK;
132 }
133
134 static int __cil_get_sepol_cat_datum(policydb_t *pdb, struct cil_symtab_datum *datum, cat_datum_t **sepol_cat)
135 {
136 *sepol_cat = hashtab_search(pdb->p_cats.table, datum->fqn);
137 if (*sepol_cat == NULL) {
138 cil_log(CIL_INFO, "Failed to find category %s in sepol hashtab\n", datum->fqn);
139 return SEPOL_ERR;
140 }
141
142 return SEPOL_OK;
143 }
144
145 static int __cil_get_sepol_level_datum(policydb_t *pdb, struct cil_symtab_datum *datum, level_datum_t **sepol_level)
146 {
147 *sepol_level = hashtab_search(pdb->p_levels.table, datum->fqn);
148 if (*sepol_level == NULL) {
149 cil_log(CIL_INFO, "Failed to find level %s in sepol hashtab\n", datum->fqn);
150 return SEPOL_ERR;
151 }
152
153 return SEPOL_OK;
154 }
155
156 static int __cil_expand_user(struct cil_symtab_datum *datum, ebitmap_t *new)
157 {
158 struct cil_tree_node *node = NODE(datum);
159 struct cil_user *user = NULL;
160 struct cil_userattribute *attr = NULL;
161
162 if (node->flavor == CIL_USERATTRIBUTE) {
163 attr = (struct cil_userattribute *)datum;
164 if (ebitmap_cpy(new, attr->users)) {
165 cil_log(CIL_ERR, "Failed to copy user bits\n");
166 goto exit;
167 }
168 } else {
169 user = (struct cil_user *)datum;
170 ebitmap_init(new);
171 if (ebitmap_set_bit(new, user->value, 1)) {
172 cil_log(CIL_ERR, "Failed to set user bit\n");
173 ebitmap_destroy(new);
174 goto exit;
175 }
176 }
177
178 return SEPOL_OK;
179
180 exit:
181 return SEPOL_ERR;
182 }
183
184 static int __cil_expand_role(struct cil_symtab_datum *datum, ebitmap_t *new)
185 {
186 struct cil_tree_node *node = NODE(datum);
187
188 if (node->flavor == CIL_ROLEATTRIBUTE) {
189 struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
190 if (ebitmap_cpy(new, attr->roles)) {
191 cil_log(CIL_ERR, "Failed to copy role bits\n");
192 goto exit;
193 }
194 } else {
195 struct cil_role *role = (struct cil_role *)datum;
196 ebitmap_init(new);
197 if (ebitmap_set_bit(new, role->value, 1)) {
198 cil_log(CIL_ERR, "Failed to set role bit\n");
199 ebitmap_destroy(new);
200 goto exit;
201 }
202 }
203
204 return SEPOL_OK;
205
206 exit:
207 return SEPOL_ERR;
208 }
209
210 static int __cil_expand_type(struct cil_symtab_datum *datum, ebitmap_t *new)
211 {
212 struct cil_tree_node *node = NODE(datum);
213
214 if (node->flavor == CIL_TYPEATTRIBUTE) {
215 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
216 if (ebitmap_cpy(new, attr->types)) {
217 cil_log(CIL_ERR, "Failed to copy type bits\n");
218 goto exit;
219 }
220 } else {
221 struct cil_type *type = (struct cil_type *)datum;
222 ebitmap_init(new);
223 if (ebitmap_set_bit(new, type->value, 1)) {
224 cil_log(CIL_ERR, "Failed to set type bit\n");
225 ebitmap_destroy(new);
226 goto exit;
227 }
228 }
229
230 return SEPOL_OK;
231
232 exit:
233 return SEPOL_ERR;
234 }
235
236 static ocontext_t *cil_add_ocontext(ocontext_t **head, ocontext_t **tail)
237 {
238 ocontext_t *new = cil_malloc(sizeof(ocontext_t));
239 memset(new, 0, sizeof(ocontext_t));
240 if (*tail) {
241 (*tail)->next = new;
242 } else {
243 *head = new;
244 }
245 *tail = new;
246
247 return new;
248 }
249
250 int cil_common_to_policydb(policydb_t *pdb, struct cil_class *cil_common, common_datum_t **common_out)
251 {
252 int rc = SEPOL_ERR;
253 uint32_t value = 0;
254 char *key = NULL;
255 struct cil_tree_node *node = cil_common->datum.nodes->head->data;
256 struct cil_tree_node *cil_perm = node->cl_head;
257 common_datum_t *sepol_common = cil_malloc(sizeof(*sepol_common));
258 memset(sepol_common, 0, sizeof(common_datum_t));
259
260 key = cil_strdup(cil_common->datum.fqn);
261 rc = symtab_insert(pdb, SYM_COMMONS, key, sepol_common, SCOPE_DECL, 0, &value);
262 if (rc != SEPOL_OK) {
263 free(sepol_common);
264 goto exit;
265 }
266 sepol_common->s.value = value;
267
268 rc = symtab_init(&sepol_common->permissions, PERM_SYMTAB_SIZE);
269 if (rc != SEPOL_OK) {
270 goto exit;
271 }
272
273 while (cil_perm != NULL) {
274 struct cil_perm *curr = cil_perm->data;
275 perm_datum_t *sepol_perm = cil_malloc(sizeof(*sepol_perm));
276 memset(sepol_perm, 0, sizeof(perm_datum_t));
277
278 key = cil_strdup(curr->datum.fqn);
279 rc = hashtab_insert(sepol_common->permissions.table, key, sepol_perm);
280 if (rc != SEPOL_OK) {
281 free(sepol_perm);
282 goto exit;
283 }
284 sepol_perm->s.value = sepol_common->permissions.nprim + 1;
285 sepol_common->permissions.nprim++;
286 cil_perm = cil_perm->next;
287 }
288
289 *common_out = sepol_common;
290
291 return SEPOL_OK;
292
293 exit:
294 free(key);
295 return rc;
296 }
297
298 static int cil_classorder_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_class *class_value_to_cil[], struct cil_perm **perm_value_to_cil[])
299 {
300 int rc = SEPOL_ERR;
301 struct cil_list_item *curr_class;
302
303 cil_list_for_each(curr_class, db->classorder) {
304 struct cil_class *cil_class = curr_class->data;
305 uint32_t value = 0;
306 char *key = NULL;
307 int class_index;
308 struct cil_tree_node *curr;
309 common_datum_t *sepol_common = NULL;
310 class_datum_t *sepol_class = cil_malloc(sizeof(*sepol_class));
311 memset(sepol_class, 0, sizeof(class_datum_t));
312
313 key = cil_strdup(cil_class->datum.fqn);
314 rc = symtab_insert(pdb, SYM_CLASSES, key, sepol_class, SCOPE_DECL, 0, &value);
315 if (rc != SEPOL_OK) {
316 free(sepol_class);
317 free(key);
318 goto exit;
319 }
320 sepol_class->s.value = value;
321 class_index = value;
322 class_value_to_cil[class_index] = cil_class;
323
324 rc = symtab_init(&sepol_class->permissions, PERM_SYMTAB_SIZE);
325 if (rc != SEPOL_OK) {
326 goto exit;
327 }
328
329 if (cil_class->common != NULL) {
330 int i;
331 struct cil_class *cil_common = cil_class->common;
332
333 key = cil_class->common->datum.fqn;
334 sepol_common = hashtab_search(pdb->p_commons.table, key);
335 if (sepol_common == NULL) {
336 rc = cil_common_to_policydb(pdb, cil_common, &sepol_common);
337 if (rc != SEPOL_OK) {
338 goto exit;
339 }
340 }
341 sepol_class->comdatum = sepol_common;
342 sepol_class->comkey = cil_strdup(key);
343 sepol_class->permissions.nprim += sepol_common->permissions.nprim;
344
345 for (curr = NODE(cil_class->common)->cl_head, i = 1; curr; curr = curr->next, i++) {
346 struct cil_perm *cil_perm = curr->data;
347 perm_value_to_cil[class_index][i] = cil_perm;
348 }
349 }
350
351 for (curr = NODE(cil_class)->cl_head; curr; curr = curr->next) {
352 struct cil_perm *cil_perm = curr->data;
353 perm_datum_t *sepol_perm = cil_malloc(sizeof(*sepol_perm));
354 memset(sepol_perm, 0, sizeof(perm_datum_t));
355
356 key = cil_strdup(cil_perm->datum.fqn);
357 rc = hashtab_insert(sepol_class->permissions.table, key, sepol_perm);
358 if (rc != SEPOL_OK) {
359 free(sepol_perm);
360 free(key);
361 goto exit;
362 }
363 sepol_perm->s.value = sepol_class->permissions.nprim + 1;
364 sepol_class->permissions.nprim++;
365 perm_value_to_cil[class_index][sepol_perm->s.value] = cil_perm;
366 }
367 }
368
369 return SEPOL_OK;
370
371 exit:
372 return rc;
373 }
374
375 int cil_role_to_policydb(policydb_t *pdb, struct cil_role *cil_role)
376 {
377 int rc = SEPOL_ERR;
378 uint32_t value = 0;
379 char *key = NULL;
380 role_datum_t *sepol_role = cil_malloc(sizeof(*sepol_role));
381 role_datum_init(sepol_role);
382
383 if (cil_role->datum.fqn == CIL_KEY_OBJECT_R) {
384 /* special case
385 * object_r defaults to 1 in libsepol symtab */
386 rc = SEPOL_OK;
387 goto exit;
388 }
389
390 key = cil_strdup(cil_role->datum.fqn);
391 rc = symtab_insert(pdb, SYM_ROLES, (hashtab_key_t)key, sepol_role, SCOPE_DECL, 0, &value);
392 if (rc != SEPOL_OK) {
393 goto exit;
394 }
395 if (ebitmap_set_bit(&sepol_role->dominates, value - 1, 1)) {
396 cil_log(CIL_INFO, "Failed to set dominates bit for role\n");
397 rc = SEPOL_ERR;
398 goto exit;
399 }
400 sepol_role->s.value = value;
401 return SEPOL_OK;
402
403 exit:
404 free(key);
405 role_datum_destroy(sepol_role);
406 free(sepol_role);
407 return rc;
408 }
409
410 static int cil_role_bounds_to_policydb(policydb_t *pdb, struct cil_role *cil_role)
411 {
412 int rc = SEPOL_ERR;
413 role_datum_t *sepol_role = NULL;
414 role_datum_t *sepol_parent = NULL;
415
416 if (cil_role->bounds) {
417 rc = __cil_get_sepol_role_datum(pdb, DATUM(cil_role), &sepol_role);
418 if (rc != SEPOL_OK) goto exit;
419
420 rc = __cil_get_sepol_role_datum(pdb, DATUM(cil_role->bounds), &sepol_parent);
421 if (rc != SEPOL_OK) goto exit;
422
423 sepol_role->bounds = sepol_parent->s.value;
424 }
425
426 return SEPOL_OK;
427
428 exit:
429 cil_log(CIL_ERR, "Failed to insert role bounds for role %s\n", cil_role->datum.fqn);
430 return SEPOL_ERR;
431 }
432
433 int cil_roletype_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_role *role)
434 {
435 int rc = SEPOL_ERR;
436
437 if (role->types) {
438 role_datum_t *sepol_role = NULL;
439 type_datum_t *sepol_type = NULL;
440 ebitmap_node_t *tnode;
441 unsigned int i;
442
443 rc = __cil_get_sepol_role_datum(pdb, DATUM(role), &sepol_role);
444 if (rc != SEPOL_OK) goto exit;
445
446 ebitmap_for_each_positive_bit(role->types, tnode, i) {
447 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_type);
448 if (rc != SEPOL_OK) goto exit;
449
450 if (ebitmap_set_bit(&sepol_role->types.types, sepol_type->s.value - 1, 1)) {
451 cil_log(CIL_INFO, "Failed to set type bit for role\n");
452 rc = SEPOL_ERR;
453 goto exit;
454 }
455 }
456 }
457
458 return SEPOL_OK;
459
460 exit:
461 return rc;
462 }
463
464 int cil_type_to_policydb(policydb_t *pdb, struct cil_type *cil_type, void *type_value_to_cil[])
465 {
466 int rc = SEPOL_ERR;
467 uint32_t value = 0;
468 char *key = NULL;
469 type_datum_t *sepol_type = cil_malloc(sizeof(*sepol_type));
470 type_datum_init(sepol_type);
471
472 sepol_type->flavor = TYPE_TYPE;
473
474 key = cil_strdup(cil_type->datum.fqn);
475 rc = symtab_insert(pdb, SYM_TYPES, key, sepol_type, SCOPE_DECL, 0, &value);
476 if (rc != SEPOL_OK) {
477 goto exit;
478 }
479 sepol_type->s.value = value;
480 sepol_type->primary = 1;
481
482 type_value_to_cil[value] = cil_type;
483
484 return SEPOL_OK;
485
486 exit:
487 free(key);
488 type_datum_destroy(sepol_type);
489 free(sepol_type);
490 return rc;
491 }
492
493 static int cil_type_bounds_to_policydb(policydb_t *pdb, struct cil_type *cil_type)
494 {
495 int rc = SEPOL_ERR;
496 type_datum_t *sepol_type = NULL;
497 type_datum_t *sepol_parent = NULL;
498
499 if (cil_type->bounds) {
500 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_type), &sepol_type);
501 if (rc != SEPOL_OK) goto exit;
502
503 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_type->bounds), &sepol_parent);
504 if (rc != SEPOL_OK) goto exit;
505
506 sepol_type->bounds = sepol_parent->s.value;
507 }
508
509 return SEPOL_OK;
510
511 exit:
512 cil_log(CIL_ERR, "Failed to insert type bounds for type %s\n", cil_type->datum.fqn);
513 return SEPOL_ERR;
514 }
515
516 int cil_typealias_to_policydb(policydb_t *pdb, struct cil_alias *cil_alias)
517 {
518 int rc = SEPOL_ERR;
519 char *key = NULL;
520 type_datum_t *sepol_type = NULL;
521 type_datum_t *sepol_alias = cil_malloc(sizeof(*sepol_alias));
522 type_datum_init(sepol_alias);
523
524 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_alias->actual), &sepol_type);
525 if (rc != SEPOL_OK) goto exit;
526
527 sepol_alias->flavor = TYPE_TYPE;
528
529 key = cil_strdup(cil_alias->datum.fqn);
530 rc = symtab_insert(pdb, SYM_TYPES, key, sepol_alias, SCOPE_DECL, 0, NULL);
531 if (rc != SEPOL_OK) {
532 goto exit;
533 }
534 sepol_alias->s.value = sepol_type->s.value;
535 sepol_alias->primary = 0;
536
537 return SEPOL_OK;
538
539 exit:
540 free(key);
541 type_datum_destroy(sepol_alias);
542 free(sepol_alias);
543 return rc;
544 }
545
546 int cil_typepermissive_to_policydb(policydb_t *pdb, struct cil_typepermissive *cil_typeperm)
547 {
548 int rc = SEPOL_ERR;
549 type_datum_t *sepol_type = NULL;
550
551 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_typeperm->type), &sepol_type);
552 if (rc != SEPOL_OK) goto exit;
553
554 if (ebitmap_set_bit(&pdb->permissive_map, sepol_type->s.value, 1)) {
555 goto exit;
556 }
557
558 return SEPOL_OK;
559
560 exit:
561 type_datum_destroy(sepol_type);
562 free(sepol_type);
563 return rc;
564
565 }
566
567 int cil_typeneveraudit_to_policydb(policydb_t *pdb, struct cil_typeneveraudit *cil_typeperm)
568 {
569 int rc = SEPOL_ERR;
570 type_datum_t *sepol_type = NULL;
571
572 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_typeperm->type), &sepol_type);
573 if (rc != SEPOL_OK) goto exit;
574
575 if (ebitmap_set_bit(&pdb->neveraudit_map, sepol_type->s.value, 1)) {
576 goto exit;
577 }
578
579 return SEPOL_OK;
580
581 exit:
582 type_datum_destroy(sepol_type);
583 free(sepol_type);
584 return rc;
585
586 }
587
588 int cil_typeattribute_to_policydb(policydb_t *pdb, struct cil_typeattribute *cil_attr, void *type_value_to_cil[])
589 {
590 int rc = SEPOL_ERR;
591 uint32_t value = 0;
592 char *key = NULL;
593 type_datum_t *sepol_attr = NULL;
594
595 if (!cil_attr->keep) {
596 return SEPOL_OK;
597 }
598
599 sepol_attr = cil_malloc(sizeof(*sepol_attr));
600 type_datum_init(sepol_attr);
601
602 sepol_attr->flavor = TYPE_ATTRIB;
603
604 key = cil_strdup(cil_attr->datum.fqn);
605 rc = symtab_insert(pdb, SYM_TYPES, key, sepol_attr, SCOPE_DECL, 0, &value);
606 if (rc != SEPOL_OK) {
607 goto exit;
608 }
609 sepol_attr->s.value = value;
610 sepol_attr->primary = 1;
611
612 type_value_to_cil[value] = cil_attr;
613
614 return SEPOL_OK;
615
616 exit:
617 type_datum_destroy(sepol_attr);
618 free(sepol_attr);
619 return rc;
620 }
621
622 static int __cil_typeattr_bitmap_init(policydb_t *pdb)
623 {
624 int rc = SEPOL_ERR;
625 uint32_t i;
626
627 pdb->type_attr_map = cil_malloc(pdb->p_types.nprim * sizeof(ebitmap_t));
628 pdb->attr_type_map = cil_malloc(pdb->p_types.nprim * sizeof(ebitmap_t));
629
630 for (i = 0; i < pdb->p_types.nprim; i++) {
631 ebitmap_init(&pdb->type_attr_map[i]);
632 ebitmap_init(&pdb->attr_type_map[i]);
633 if (ebitmap_set_bit(&pdb->type_attr_map[i], i, 1)) {
634 rc = SEPOL_ERR;
635 goto exit;
636 }
637 if (pdb->type_val_to_struct[i] && pdb->type_val_to_struct[i]->flavor != TYPE_ATTRIB) {
638 if (ebitmap_set_bit(&pdb->attr_type_map[i], i, 1)) {
639 rc = SEPOL_ERR;
640 goto exit;
641 }
642 }
643
644 }
645
646 return SEPOL_OK;
647
648 exit:
649 return rc;
650 }
651
652 int cil_typeattribute_to_bitmap(policydb_t *pdb, const struct cil_db *db, struct cil_typeattribute *cil_attr)
653 {
654 int rc = SEPOL_ERR;
655 uint32_t value = 0;
656 type_datum_t *sepol_type = NULL;
657 ebitmap_node_t *tnode;
658 unsigned int i;
659
660 if (!cil_attr->keep) {
661 return SEPOL_OK;
662 }
663
664 if (pdb->type_attr_map == NULL) {
665 rc = __cil_typeattr_bitmap_init(pdb);
666 if (rc != SEPOL_OK) {
667 goto exit;
668 }
669 }
670
671 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_attr), &sepol_type);
672 if (rc != SEPOL_OK) goto exit;
673
674 value = sepol_type->s.value;
675
676 ebitmap_for_each_positive_bit(cil_attr->types, tnode, i) {
677 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_type);
678 if (rc != SEPOL_OK) goto exit;
679
680 ebitmap_set_bit(&pdb->type_attr_map[sepol_type->s.value - 1], value - 1, 1);
681 ebitmap_set_bit(&pdb->attr_type_map[value - 1], sepol_type->s.value - 1, 1);
682 }
683
684 rc = SEPOL_OK;
685 exit:
686 return rc;
687 }
688
689 int cil_policycap_to_policydb(policydb_t *pdb, struct cil_policycap *cil_polcap)
690 {
691 int rc = SEPOL_ERR;
692 int capnum;
693
694 capnum = sepol_polcap_getnum(cil_polcap->datum.fqn);
695 if (capnum == -1) {
696 goto exit;
697 }
698
699 if (ebitmap_set_bit(&pdb->policycaps, capnum, 1)) {
700 goto exit;
701 }
702
703 return SEPOL_OK;
704
705 exit:
706 return rc;
707 }
708
709 int cil_user_to_policydb(policydb_t *pdb, struct cil_user *cil_user)
710 {
711 int rc = SEPOL_ERR;
712 uint32_t value = 0;
713 char *key = NULL;
714 user_datum_t *sepol_user = cil_malloc(sizeof(*sepol_user));
715 user_datum_init(sepol_user);
716
717 key = cil_strdup(cil_user->datum.fqn);
718 rc = symtab_insert(pdb, SYM_USERS, key, sepol_user, SCOPE_DECL, 0, &value);
719 if (rc != SEPOL_OK) {
720 goto exit;
721 }
722 sepol_user->s.value = value;
723
724 return SEPOL_OK;
725
726 exit:
727 free(key);
728 user_datum_destroy(sepol_user);
729 free(sepol_user);
730 return rc;
731 }
732
733 static int cil_user_bounds_to_policydb(policydb_t *pdb, struct cil_user *cil_user)
734 {
735 int rc = SEPOL_ERR;
736 user_datum_t *sepol_user = NULL;
737 user_datum_t *sepol_parent = NULL;
738
739 if (cil_user->bounds) {
740 rc = __cil_get_sepol_user_datum(pdb, DATUM(cil_user), &sepol_user);
741 if (rc != SEPOL_OK) goto exit;
742
743 rc = __cil_get_sepol_user_datum(pdb, DATUM(cil_user->bounds), &sepol_parent);
744 if (rc != SEPOL_OK) goto exit;
745
746 sepol_user->bounds = sepol_parent->s.value;
747 }
748
749 return SEPOL_OK;
750
751 exit:
752 cil_log(CIL_ERR, "Failed to insert user bounds for user %s\n", cil_user->datum.fqn);
753 return SEPOL_ERR;
754 }
755
756 int cil_userrole_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_user *user)
757 {
758 int rc = SEPOL_ERR;
759 user_datum_t *sepol_user = NULL;
760 role_datum_t *sepol_role = NULL;
761 ebitmap_node_t *rnode = NULL;
762 unsigned int i;
763
764 if (user->roles) {
765 rc = __cil_get_sepol_user_datum(pdb, DATUM(user), &sepol_user);
766 if (rc != SEPOL_OK) {
767 goto exit;
768 }
769
770 ebitmap_for_each_positive_bit(user->roles, rnode, i) {
771 rc = __cil_get_sepol_role_datum(pdb, DATUM(db->val_to_role[i]), &sepol_role);
772 if (rc != SEPOL_OK) {
773 goto exit;
774 }
775
776 if (sepol_role->s.value == 1) {
777 // role is object_r, ignore it since it is implicitly associated
778 // with all users
779 continue;
780 }
781
782 if (ebitmap_set_bit(&sepol_user->roles.roles, sepol_role->s.value - 1, 1)) {
783 cil_log(CIL_INFO, "Failed to set role bit for user\n");
784 rc = SEPOL_ERR;
785 goto exit;
786 }
787 }
788 }
789
790 rc = SEPOL_OK;
791
792 exit:
793 return rc;
794 }
795
796 int cil_bool_to_policydb(policydb_t *pdb, struct cil_bool *cil_bool)
797 {
798 int rc = SEPOL_ERR;
799 uint32_t value = 0;
800 char *key = NULL;
801 cond_bool_datum_t *sepol_bool = cil_malloc(sizeof(*sepol_bool));
802 memset(sepol_bool, 0, sizeof(cond_bool_datum_t));
803
804 key = cil_strdup(cil_bool->datum.fqn);
805 rc = symtab_insert(pdb, SYM_BOOLS, key, sepol_bool, SCOPE_DECL, 0, &value);
806 if (rc != SEPOL_OK) {
807 goto exit;
808 }
809 sepol_bool->s.value = value;
810 sepol_bool->state = cil_bool->value;
811
812 return SEPOL_OK;
813
814 exit:
815 free(key);
816 free(sepol_bool);
817 return rc;
818 }
819
820 int cil_catorder_to_policydb(policydb_t *pdb, const struct cil_db *db)
821 {
822 int rc = SEPOL_ERR;
823 uint32_t value = 0;
824 char *key = NULL;
825 struct cil_list_item *curr_cat;
826 struct cil_cat *cil_cat = NULL;
827 cat_datum_t *sepol_cat = NULL;
828
829 cil_list_for_each(curr_cat, db->catorder) {
830 cil_cat = curr_cat->data;
831 sepol_cat = cil_malloc(sizeof(*sepol_cat));
832 cat_datum_init(sepol_cat);
833
834 key = cil_strdup(cil_cat->datum.fqn);
835 rc = symtab_insert(pdb, SYM_CATS, key, sepol_cat, SCOPE_DECL, 0, &value);
836 if (rc != SEPOL_OK) {
837 goto exit;
838 }
839 sepol_cat->s.value = value;
840 }
841
842 return SEPOL_OK;
843
844 exit:
845 free(key);
846 cat_datum_destroy(sepol_cat);
847 free(sepol_cat);
848 return rc;
849 }
850
851 int cil_catalias_to_policydb(policydb_t *pdb, struct cil_alias *cil_alias)
852 {
853 int rc = SEPOL_ERR;
854 char *key = NULL;
855 cat_datum_t *sepol_cat;
856 cat_datum_t *sepol_alias = cil_malloc(sizeof(*sepol_cat));
857 cat_datum_init(sepol_alias);
858
859 rc = __cil_get_sepol_cat_datum(pdb, DATUM(cil_alias->actual), &sepol_cat);
860 if (rc != SEPOL_OK) goto exit;
861
862 key = cil_strdup(cil_alias->datum.fqn);
863 rc = symtab_insert(pdb, SYM_CATS, key, sepol_alias, SCOPE_DECL, 0, NULL);
864 if (rc != SEPOL_OK) {
865 goto exit;
866 }
867 sepol_alias->s.value = sepol_cat->s.value;
868 sepol_alias->isalias = 1;
869
870 return SEPOL_OK;
871
872 exit:
873 free(key);
874 cat_datum_destroy(sepol_alias);
875 free(sepol_alias);
876 return rc;
877 }
878
879 int cil_sensitivityorder_to_policydb(policydb_t *pdb, const struct cil_db *db)
880 {
881 int rc = SEPOL_ERR;
882 uint32_t value = 0;
883 char *key = NULL;
884 struct cil_list_item *curr;
885 struct cil_sens *cil_sens = NULL;
886 level_datum_t *sepol_level = NULL;
887 mls_level_t *mls_level = NULL;
888
889 cil_list_for_each(curr, db->sensitivityorder) {
890 cil_sens = curr->data;
891 sepol_level = cil_malloc(sizeof(*sepol_level));
892 mls_level = cil_malloc(sizeof(*mls_level));
893 level_datum_init(sepol_level);
894 mls_level_init(mls_level);
895
896 key = cil_strdup(cil_sens->datum.fqn);
897 rc = symtab_insert(pdb, SYM_LEVELS, key, sepol_level, SCOPE_DECL, 0, &value);
898 if (rc != SEPOL_OK) {
899 goto exit;
900 }
901 mls_level->sens = value;
902 sepol_level->level = mls_level;
903 }
904
905 return SEPOL_OK;
906
907 exit:
908 level_datum_destroy(sepol_level);
909 mls_level_destroy(mls_level);
910 free(sepol_level);
911 free(mls_level);
912 free(key);
913 return rc;
914 }
915
916 static int cil_sensalias_to_policydb(policydb_t *pdb, struct cil_alias *cil_alias)
917 {
918 int rc = SEPOL_ERR;
919 char *key = NULL;
920 mls_level_t *mls_level = NULL;
921 level_datum_t *sepol_level = NULL;
922 level_datum_t *sepol_alias = cil_malloc(sizeof(*sepol_alias));
923 level_datum_init(sepol_alias);
924
925 rc = __cil_get_sepol_level_datum(pdb, DATUM(cil_alias->actual), &sepol_level);
926 if (rc != SEPOL_OK) goto exit;
927
928 key = cil_strdup(cil_alias->datum.fqn);
929 rc = symtab_insert(pdb, SYM_LEVELS, key, sepol_alias, SCOPE_DECL, 0, NULL);
930 if (rc != SEPOL_OK) {
931 goto exit;
932 }
933
934 mls_level = cil_malloc(sizeof(*mls_level));
935 mls_level_init(mls_level);
936
937 rc = mls_level_cpy(mls_level, sepol_level->level);
938 if (rc != SEPOL_OK) {
939 free(mls_level);
940 goto exit;
941 }
942 sepol_alias->level = mls_level;
943 sepol_alias->isalias = 1;
944
945 return SEPOL_OK;
946
947 exit:
948 level_datum_destroy(sepol_alias);
949 free(sepol_alias);
950 free(key);
951 return rc;
952 }
953
954 static int __cil_cond_insert_rule(avtab_t *avtab, avtab_key_t *avtab_key, avtab_datum_t *avtab_datum, cond_node_t *cond_node, enum cil_flavor cond_flavor)
955 {
956 int rc = SEPOL_OK;
957 avtab_ptr_t avtab_ptr = NULL;
958 cond_av_list_t *cond_list = NULL;
959
960 avtab_ptr = avtab_insert_nonunique(avtab, avtab_key, avtab_datum);
961 if (!avtab_ptr) {
962 rc = SEPOL_ERR;
963 goto exit;
964 }
965
966 // parse_context needs to be non-NULL for conditional rules to be
967 // written to the binary. it is normally used for finding duplicates,
968 // but cil checks that earlier, so we don't use it. it just needs to be
969 // set
970 avtab_ptr->parse_context = (void*)1;
971
972 cond_list = cil_malloc(sizeof(cond_av_list_t));
973 memset(cond_list, 0, sizeof(cond_av_list_t));
974
975 cond_list->node = avtab_ptr;
976
977 if (cond_flavor == CIL_CONDTRUE) {
978 cond_list->next = cond_node->true_list;
979 cond_node->true_list = cond_list;
980 } else {
981 cond_list->next = cond_node->false_list;
982 cond_node->false_list = cond_list;
983 }
984
985 exit:
986 return rc;
987 }
988
989 static avtab_datum_t *cil_cond_av_list_search(avtab_key_t *key, cond_av_list_t *cond_list)
990 {
991 cond_av_list_t *cur_av;
992
993 for (cur_av = cond_list; cur_av != NULL; cur_av = cur_av->next) {
994 if (cur_av->node->key.source_type == key->source_type &&
995 cur_av->node->key.target_type == key->target_type &&
996 cur_av->node->key.target_class == key->target_class &&
997 (cur_av->node->key.specified & key->specified))
998
999 return &cur_av->node->datum;
1000
1001 }
1002 return NULL;
1003 }
1004
1005 static int __cil_insert_type_rule(policydb_t *pdb, uint32_t kind, uint32_t src, uint32_t tgt, uint32_t obj, uint32_t res, struct cil_type_rule *cil_rule, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1006 {
1007 int rc = SEPOL_OK;
1008 avtab_key_t avtab_key;
1009 avtab_datum_t avtab_datum = { .data = res, .xperms = NULL };
1010 avtab_ptr_t existing;
1011
1012 avtab_key.source_type = src;
1013 avtab_key.target_type = tgt;
1014 avtab_key.target_class = obj;
1015
1016 switch (kind) {
1017 case CIL_TYPE_TRANSITION:
1018 avtab_key.specified = AVTAB_TRANSITION;
1019 break;
1020 case CIL_TYPE_CHANGE:
1021 avtab_key.specified = AVTAB_CHANGE;
1022 break;
1023 case CIL_TYPE_MEMBER:
1024 avtab_key.specified = AVTAB_MEMBER;
1025 break;
1026 default:
1027 rc = SEPOL_ERR;
1028 goto exit;
1029 }
1030
1031 existing = avtab_search_node(&pdb->te_avtab, &avtab_key);
1032 if (existing) {
1033 /* Don't add duplicate type rule and warn if they conflict.
1034 * A warning should have been previously given if there is a
1035 * non-duplicate rule using the same key.
1036 */
1037 if (existing->datum.data != res) {
1038 cil_log(CIL_ERR, "Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n",
1039 pdb->p_type_val_to_name[src - 1],
1040 pdb->p_type_val_to_name[tgt - 1],
1041 pdb->p_class_val_to_name[obj - 1],
1042 pdb->p_type_val_to_name[res - 1],
1043 pdb->p_type_val_to_name[existing->datum.data - 1]);
1044 cil_log(CIL_ERR, "Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n",
1045 cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str);
1046 rc = SEPOL_ERR;
1047 }
1048 goto exit;
1049 }
1050
1051 if (!cond_node) {
1052 rc = avtab_insert(&pdb->te_avtab, &avtab_key, &avtab_datum);
1053 } else {
1054 existing = avtab_search_node(&pdb->te_cond_avtab, &avtab_key);
1055 if (existing) {
1056 cond_av_list_t *this_list;
1057 cond_av_list_t *other_list;
1058 avtab_datum_t *search_datum;
1059
1060 if (cond_flavor == CIL_CONDTRUE) {
1061 this_list = cond_node->true_list;
1062 other_list = cond_node->false_list;
1063 } else {
1064 this_list = cond_node->false_list;
1065 other_list = cond_node->true_list;
1066 }
1067
1068 search_datum = cil_cond_av_list_search(&avtab_key, other_list);
1069 if (search_datum == NULL) {
1070 if (existing->datum.data != res) {
1071 cil_log(CIL_ERR, "Conflicting type rules (scontext=%s tcontext=%s tclass=%s result=%s), existing=%s\n",
1072 pdb->p_type_val_to_name[src - 1],
1073 pdb->p_type_val_to_name[tgt - 1],
1074 pdb->p_class_val_to_name[obj - 1],
1075 pdb->p_type_val_to_name[res - 1],
1076 pdb->p_type_val_to_name[existing->datum.data - 1]);
1077 cil_log(CIL_ERR, "Expanded from type rule (scontext=%s tcontext=%s tclass=%s result=%s)\n",
1078 cil_rule->src_str, cil_rule->tgt_str, cil_rule->obj_str, cil_rule->result_str);
1079 rc = SEPOL_ERR;
1080 goto exit;
1081 }
1082
1083 search_datum = cil_cond_av_list_search(&avtab_key, this_list);
1084 if (search_datum) {
1085 goto exit;
1086 }
1087 }
1088 }
1089 rc = __cil_cond_insert_rule(&pdb->te_cond_avtab, &avtab_key, &avtab_datum, cond_node, cond_flavor);
1090 }
1091
1092 exit:
1093 return rc;
1094 }
1095
1096 static int __cil_type_rule_to_avtab_helper(policydb_t *pdb,
1097 type_datum_t *sepol_src,
1098 type_datum_t *sepol_tgt,
1099 struct cil_list *class_list,
1100 type_datum_t *sepol_result,
1101 struct cil_type_rule *cil_rule,
1102 cond_node_t *cond_node,
1103 enum cil_flavor cond_flavor)
1104 {
1105 int rc;
1106 class_datum_t *sepol_obj = NULL;
1107 struct cil_list_item *c;
1108
1109 cil_list_for_each(c, class_list) {
1110 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_obj);
1111 if (rc != SEPOL_OK) return rc;
1112
1113 rc = __cil_insert_type_rule(
1114 pdb, cil_rule->rule_kind, sepol_src->s.value,
1115 sepol_tgt->s.value, sepol_obj->s.value,
1116 sepol_result->s.value, cil_rule, cond_node, cond_flavor
1117 );
1118 if (rc != SEPOL_OK) return rc;
1119 }
1120 return SEPOL_OK;
1121 }
1122
1123 static int __cil_type_rule_to_avtab(policydb_t *pdb, const struct cil_db *db, struct cil_type_rule *cil_rule, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1124 {
1125 int rc = SEPOL_ERR;
1126 struct cil_symtab_datum *src = NULL;
1127 struct cil_symtab_datum *tgt = NULL;
1128 type_datum_t *sepol_src = NULL;
1129 type_datum_t *sepol_tgt = NULL;
1130 struct cil_list *class_list = NULL;
1131 type_datum_t *sepol_result = NULL;
1132 ebitmap_t src_bitmap, tgt_bitmap;
1133 ebitmap_node_t *node1, *node2;
1134 unsigned int i, j;
1135
1136 ebitmap_init(&src_bitmap);
1137 ebitmap_init(&tgt_bitmap);
1138
1139 src = cil_rule->src;
1140 tgt = cil_rule->tgt;
1141
1142 rc = __cil_expand_type(src, &src_bitmap);
1143 if (rc != SEPOL_OK) goto exit;
1144
1145 class_list = cil_expand_class(cil_rule->obj);
1146
1147 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_rule->result), &sepol_result);
1148 if (rc != SEPOL_OK) goto exit;
1149
1150 if (tgt->fqn == CIL_KEY_SELF) {
1151 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
1152 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_src);
1153 if (rc != SEPOL_OK) goto exit;
1154
1155 rc = __cil_type_rule_to_avtab_helper(
1156 pdb, sepol_src, sepol_src, class_list,
1157 sepol_result, cil_rule, cond_node, cond_flavor
1158 );
1159 if (rc != SEPOL_OK) goto exit;
1160 }
1161 } else {
1162 rc = __cil_expand_type(tgt, &tgt_bitmap);
1163 if (rc != SEPOL_OK) goto exit;
1164
1165 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
1166 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_src);
1167 if (rc != SEPOL_OK) goto exit;
1168
1169 ebitmap_for_each_positive_bit(&tgt_bitmap, node2, j) {
1170 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[j]), &sepol_tgt);
1171 if (rc != SEPOL_OK) goto exit;
1172
1173 rc = __cil_type_rule_to_avtab_helper(
1174 pdb, sepol_src, sepol_tgt, class_list,
1175 sepol_result, cil_rule, cond_node,
1176 cond_flavor
1177 );
1178 if (rc != SEPOL_OK) goto exit;
1179 }
1180 }
1181 }
1182
1183 rc = SEPOL_OK;
1184
1185 exit:
1186 ebitmap_destroy(&src_bitmap);
1187 ebitmap_destroy(&tgt_bitmap);
1188 cil_list_destroy(&class_list, CIL_FALSE);
1189 return rc;
1190 }
1191
1192 int cil_type_rule_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_type_rule *cil_rule)
1193 {
1194 return __cil_type_rule_to_avtab(pdb, db, cil_rule, NULL, CIL_FALSE);
1195 }
1196
1197 static int __cil_typetransition_to_avtab_helper(policydb_t *pdb,
1198 type_datum_t *sepol_src,
1199 type_datum_t *sepol_tgt,
1200 struct cil_list *class_list,
1201 char *name,
1202 type_datum_t *sepol_result)
1203 {
1204 int rc;
1205 class_datum_t *sepol_obj = NULL;
1206 uint32_t otype;
1207 struct cil_list_item *c;
1208
1209 cil_list_for_each(c, class_list) {
1210 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_obj);
1211 if (rc != SEPOL_OK) return rc;
1212
1213 rc = policydb_filetrans_insert(
1214 pdb, sepol_src->s.value, sepol_tgt->s.value,
1215 sepol_obj->s.value, name, NULL,
1216 sepol_result->s.value, &otype
1217 );
1218 if (rc != SEPOL_OK) {
1219 if (rc == SEPOL_EEXIST) {
1220 if (sepol_result->s.value!= otype) {
1221 cil_log(CIL_ERR, "Conflicting name type transition rules\n");
1222 } else {
1223 rc = SEPOL_OK;
1224 }
1225 } else {
1226 cil_log(CIL_ERR, "Out of memory\n");
1227 }
1228 if (rc != SEPOL_OK) {
1229 return rc;
1230 }
1231 }
1232 }
1233 return SEPOL_OK;
1234 }
1235
1236 static int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *db, struct cil_nametypetransition *typetrans, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1237 {
1238 int rc = SEPOL_ERR;
1239 struct cil_symtab_datum *src = NULL;
1240 struct cil_symtab_datum *tgt = NULL;
1241 type_datum_t *sepol_src = NULL;
1242 type_datum_t *sepol_tgt = NULL;
1243 struct cil_list *class_list = NULL;
1244 type_datum_t *sepol_result = NULL;
1245 ebitmap_t src_bitmap, tgt_bitmap;
1246 ebitmap_node_t *node1, *node2;
1247 unsigned int i, j;
1248 char *name = DATUM(typetrans->name)->name;
1249
1250 if (name == CIL_KEY_STAR) {
1251 struct cil_type_rule trans;
1252 trans.rule_kind = CIL_TYPE_TRANSITION;
1253 trans.src = typetrans->src;
1254 trans.tgt = typetrans->tgt;
1255 trans.obj = typetrans->obj;
1256 trans.result = typetrans->result;
1257 trans.src_str = typetrans->src_str;
1258 trans.tgt_str = typetrans->tgt_str;
1259 trans.obj_str = typetrans->obj_str;
1260 trans.result_str = typetrans->result_str;
1261 return __cil_type_rule_to_avtab(pdb, db, &trans, cond_node, cond_flavor);
1262 }
1263
1264 ebitmap_init(&src_bitmap);
1265 ebitmap_init(&tgt_bitmap);
1266
1267 src = typetrans->src;
1268 tgt = typetrans->tgt;
1269
1270 rc = __cil_expand_type(src, &src_bitmap);
1271 if (rc != SEPOL_OK) goto exit;
1272
1273 class_list = cil_expand_class(typetrans->obj);
1274
1275 rc = __cil_get_sepol_type_datum(pdb, DATUM(typetrans->result), &sepol_result);
1276 if (rc != SEPOL_OK) goto exit;
1277
1278 if (tgt->fqn == CIL_KEY_SELF) {
1279 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
1280 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_src);
1281 if (rc != SEPOL_OK) goto exit;
1282
1283 rc = __cil_typetransition_to_avtab_helper(
1284 pdb, sepol_src, sepol_src, class_list,
1285 name, sepol_result
1286 );
1287 if (rc != SEPOL_OK) goto exit;
1288 }
1289 } else {
1290 rc = __cil_expand_type(tgt, &tgt_bitmap);
1291 if (rc != SEPOL_OK) goto exit;
1292
1293 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
1294 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_src);
1295 if (rc != SEPOL_OK) goto exit;
1296
1297 ebitmap_for_each_positive_bit(&tgt_bitmap, node2, j) {
1298 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[j]), &sepol_tgt);
1299 if (rc != SEPOL_OK) goto exit;
1300
1301 rc = __cil_typetransition_to_avtab_helper(
1302 pdb, sepol_src, sepol_tgt, class_list,
1303 name, sepol_result
1304 );
1305 if (rc != SEPOL_OK) goto exit;
1306 }
1307 }
1308 }
1309
1310 rc = SEPOL_OK;
1311
1312 exit:
1313 ebitmap_destroy(&src_bitmap);
1314 ebitmap_destroy(&tgt_bitmap);
1315 cil_list_destroy(&class_list, CIL_FALSE);
1316 return rc;
1317 }
1318
1319 int cil_typetransition_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_nametypetransition *typetrans)
1320 {
1321 return __cil_typetransition_to_avtab(pdb, db, typetrans, NULL, CIL_FALSE);
1322 }
1323
1324 static int __perm_str_to_datum(char *perm_str, class_datum_t *sepol_class, uint32_t *datum)
1325 {
1326 int rc;
1327 perm_datum_t *sepol_perm;
1328 common_datum_t *sepol_common;
1329
1330 sepol_perm = hashtab_search(sepol_class->permissions.table, perm_str);
1331 if (sepol_perm == NULL) {
1332 sepol_common = sepol_class->comdatum;
1333 sepol_perm = hashtab_search(sepol_common->permissions.table, perm_str);
1334 if (sepol_perm == NULL) {
1335 cil_log(CIL_ERR, "Failed to find datum for perm %s\n", perm_str);
1336 rc = SEPOL_ERR;
1337 goto exit;
1338 }
1339 }
1340 *datum |= UINT32_C(1) << (sepol_perm->s.value - 1);
1341
1342 return SEPOL_OK;
1343
1344 exit:
1345 return rc;
1346 }
1347
1348 static int __cil_perms_to_datum(struct cil_list *perms, class_datum_t *sepol_class, uint32_t *datum)
1349 {
1350 int rc = SEPOL_ERR;
1351 char *key = NULL;
1352 struct cil_list_item *curr_perm;
1353 struct cil_perm *cil_perm;
1354 uint32_t data = 0;
1355
1356 cil_list_for_each(curr_perm, perms) {
1357 cil_perm = curr_perm->data;
1358 key = cil_perm->datum.fqn;
1359
1360 rc = __perm_str_to_datum(key, sepol_class, &data);
1361 if (rc != SEPOL_OK) {
1362 goto exit;
1363 }
1364 }
1365
1366 *datum = data;
1367
1368 return SEPOL_OK;
1369
1370 exit:
1371 return rc;
1372 }
1373
1374 static int __cil_insert_avrule(policydb_t *pdb, uint32_t kind, uint32_t src, uint32_t tgt, uint32_t obj, uint32_t data, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1375 {
1376 int rc = SEPOL_OK;
1377 avtab_key_t avtab_key;
1378 avtab_datum_t avtab_datum = { .data = data, .xperms = NULL };
1379 avtab_datum_t *avtab_dup = NULL;
1380
1381 avtab_key.source_type = src;
1382 avtab_key.target_type = tgt;
1383 avtab_key.target_class = obj;
1384
1385 switch (kind) {
1386 case CIL_AVRULE_ALLOWED:
1387 avtab_key.specified = AVTAB_ALLOWED;
1388 break;
1389 case CIL_AVRULE_AUDITALLOW:
1390 avtab_key.specified = AVTAB_AUDITALLOW;
1391 break;
1392 case CIL_AVRULE_DONTAUDIT:
1393 avtab_key.specified = AVTAB_AUDITDENY;
1394 break;
1395 default:
1396 rc = SEPOL_ERR;
1397 goto exit;
1398 break;
1399 }
1400
1401 if (!cond_node) {
1402 avtab_dup = avtab_search(&pdb->te_avtab, &avtab_key);
1403 if (!avtab_dup) {
1404 rc = avtab_insert(&pdb->te_avtab, &avtab_key, &avtab_datum);
1405 } else {
1406 if (kind == CIL_AVRULE_DONTAUDIT)
1407 avtab_dup->data &= data;
1408 else
1409 avtab_dup->data |= data;
1410 }
1411 } else {
1412 rc = __cil_cond_insert_rule(&pdb->te_cond_avtab, &avtab_key, &avtab_datum, cond_node, cond_flavor);
1413 }
1414
1415 exit:
1416 return rc;
1417 }
1418
1419 static int __cil_avrule_expand_helper(policydb_t *pdb, uint16_t kind, struct cil_symtab_datum *src, struct cil_symtab_datum *tgt, struct cil_classperms *cp, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1420 {
1421 int rc = SEPOL_ERR;
1422 type_datum_t *sepol_src = NULL;
1423 type_datum_t *sepol_tgt = NULL;
1424 class_datum_t *sepol_class = NULL;
1425 uint32_t data = 0;
1426
1427 rc = __cil_get_sepol_class_datum(pdb, DATUM(cp->class), &sepol_class);
1428 if (rc != SEPOL_OK) goto exit;
1429
1430 rc = __cil_perms_to_datum(cp->perms, sepol_class, &data);
1431 if (rc != SEPOL_OK) goto exit;
1432
1433 if (data == 0) {
1434 /* No permissions, so don't insert rule. Maybe should return an error? */
1435 return SEPOL_OK;
1436 }
1437
1438 if (kind == CIL_AVRULE_DONTAUDIT) {
1439 data = ~data;
1440 }
1441
1442 rc = __cil_get_sepol_type_datum(pdb, src, &sepol_src);
1443 if (rc != SEPOL_OK) goto exit;
1444
1445 rc = __cil_get_sepol_type_datum(pdb, tgt, &sepol_tgt);
1446 if (rc != SEPOL_OK) goto exit;
1447
1448 rc = __cil_insert_avrule(pdb, kind, sepol_src->s.value, sepol_tgt->s.value, sepol_class->s.value, data, cond_node, cond_flavor);
1449 if (rc != SEPOL_OK) {
1450 goto exit;
1451 }
1452
1453 return SEPOL_OK;
1454
1455 exit:
1456 return rc;
1457 }
1458
1459
1460 static int __cil_avrule_expand(policydb_t *pdb, uint16_t kind, struct cil_symtab_datum *src, struct cil_symtab_datum *tgt, struct cil_list *classperms, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1461 {
1462 int rc = SEPOL_ERR;
1463 struct cil_list_item *curr;
1464
1465 cil_list_for_each(curr, classperms) {
1466 if (curr->flavor == CIL_CLASSPERMS) {
1467 struct cil_classperms *cp = curr->data;
1468 if (FLAVOR(cp->class) == CIL_CLASS) {
1469 rc = __cil_avrule_expand_helper(pdb, kind, src, tgt, cp, cond_node, cond_flavor);
1470 if (rc != SEPOL_OK) {
1471 goto exit;
1472 }
1473 } else { /* MAP */
1474 struct cil_list_item *i = NULL;
1475 cil_list_for_each(i, cp->perms) {
1476 struct cil_perm *cmp = i->data;
1477 rc = __cil_avrule_expand(pdb, kind, src, tgt, cmp->classperms, cond_node, cond_flavor);
1478 if (rc != SEPOL_OK) {
1479 goto exit;
1480 }
1481 }
1482 }
1483 } else { /* SET */
1484 struct cil_classperms_set *cp_set = curr->data;
1485 struct cil_classpermission *cp = cp_set->set;
1486 rc = __cil_avrule_expand(pdb, kind, src, tgt, cp->classperms, cond_node, cond_flavor);
1487 if (rc != SEPOL_OK) {
1488 goto exit;
1489 }
1490 }
1491 }
1492
1493 return SEPOL_OK;
1494
1495 exit:
1496 return rc;
1497 }
1498
1499 static int __cil_should_expand_attribute( const struct cil_db *db, struct cil_symtab_datum *datum)
1500 {
1501 struct cil_tree_node *node;
1502 struct cil_typeattribute *attr;
1503
1504 node = NODE(datum);
1505
1506 if (node->flavor != CIL_TYPEATTRIBUTE) {
1507 return CIL_FALSE;
1508 }
1509
1510 attr = (struct cil_typeattribute *)datum;
1511
1512 return !attr->keep || (ebitmap_cardinality(attr->types) < db->attrs_expand_size);
1513 }
1514
1515 static int __cil_avrule_to_avtab(policydb_t *pdb, const struct cil_db *db, struct cil_avrule *cil_avrule, cond_node_t *cond_node, enum cil_flavor cond_flavor)
1516 {
1517 int rc = SEPOL_ERR;
1518 uint16_t kind = cil_avrule->rule_kind;
1519 struct cil_symtab_datum *src = NULL;
1520 struct cil_symtab_datum *tgt = NULL;
1521 struct cil_list *classperms = cil_avrule->perms.classperms;
1522 ebitmap_t src_bitmap, tgt_bitmap;
1523 ebitmap_node_t *snode, *tnode;
1524 unsigned int s,t;
1525
1526 if (cil_avrule->rule_kind == CIL_AVRULE_DONTAUDIT && db->disable_dontaudit == CIL_TRUE) {
1527 // Do not add dontaudit rules to binary
1528 rc = SEPOL_OK;
1529 goto exit;
1530 }
1531
1532 src = cil_avrule->src;
1533 tgt = cil_avrule->tgt;
1534
1535 if (tgt->fqn == CIL_KEY_SELF) {
1536 rc = __cil_expand_type(src, &src_bitmap);
1537 if (rc != SEPOL_OK) {
1538 goto exit;
1539 }
1540
1541 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1542 src = DATUM(db->val_to_type[s]);
1543 rc = __cil_avrule_expand(pdb, kind, src, src, classperms, cond_node, cond_flavor);
1544 if (rc != SEPOL_OK) {
1545 ebitmap_destroy(&src_bitmap);
1546 goto exit;
1547 }
1548 }
1549 ebitmap_destroy(&src_bitmap);
1550 } else if (tgt->fqn == CIL_KEY_NOTSELF) {
1551 rc = __cil_expand_type(src, &src_bitmap);
1552 if (rc != SEPOL_OK) {
1553 goto exit;
1554 }
1555
1556 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1557 src = DATUM(db->val_to_type[s]);
1558 for (t = 0; t < (unsigned int)db->num_types; t++) {
1559 if (s != t) {
1560 tgt = DATUM(db->val_to_type[t]);
1561 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1562 if (rc != SEPOL_OK) {
1563 ebitmap_destroy(&src_bitmap);
1564 goto exit;
1565 }
1566 }
1567 }
1568 }
1569 ebitmap_destroy(&src_bitmap);
1570 } else if (tgt->fqn == CIL_KEY_OTHER) {
1571 rc = __cil_expand_type(src, &src_bitmap);
1572 if (rc != SEPOL_OK) {
1573 goto exit;
1574 }
1575
1576 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1577 src = DATUM(db->val_to_type[s]);
1578 ebitmap_for_each_positive_bit(&src_bitmap, tnode, t) {
1579 if (s != t) {
1580 tgt = DATUM(db->val_to_type[t]);
1581 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1582 if (rc != SEPOL_OK) {
1583 ebitmap_destroy(&src_bitmap);
1584 goto exit;
1585 }
1586 }
1587 }
1588 }
1589 ebitmap_destroy(&src_bitmap);
1590 } else {
1591 int expand_src = __cil_should_expand_attribute(db, src);
1592 int expand_tgt = __cil_should_expand_attribute(db, tgt);
1593 if (!expand_src && !expand_tgt) {
1594 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1595 if (rc != SEPOL_OK) {
1596 goto exit;
1597 }
1598 } else if (expand_src && expand_tgt) {
1599 rc = __cil_expand_type(src, &src_bitmap);
1600 if (rc != SEPOL_OK) {
1601 goto exit;
1602 }
1603
1604 rc = __cil_expand_type(tgt, &tgt_bitmap);
1605 if (rc != SEPOL_OK) {
1606 ebitmap_destroy(&src_bitmap);
1607 goto exit;
1608 }
1609
1610 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1611 src = DATUM(db->val_to_type[s]);
1612 ebitmap_for_each_positive_bit(&tgt_bitmap, tnode, t) {
1613 tgt = DATUM(db->val_to_type[t]);
1614
1615 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1616 if (rc != SEPOL_OK) {
1617 ebitmap_destroy(&src_bitmap);
1618 ebitmap_destroy(&tgt_bitmap);
1619 goto exit;
1620 }
1621 }
1622 }
1623 ebitmap_destroy(&src_bitmap);
1624 ebitmap_destroy(&tgt_bitmap);
1625 } else if (expand_src) {
1626 rc = __cil_expand_type(src, &src_bitmap);
1627 if (rc != SEPOL_OK) {
1628 goto exit;
1629 }
1630
1631 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1632 src = DATUM(db->val_to_type[s]);
1633
1634 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1635 if (rc != SEPOL_OK) {
1636 ebitmap_destroy(&src_bitmap);
1637 goto exit;
1638 }
1639 }
1640 ebitmap_destroy(&src_bitmap);
1641 } else { /* expand_tgt */
1642 rc = __cil_expand_type(tgt, &tgt_bitmap);
1643 if (rc != SEPOL_OK) {
1644 goto exit;
1645 }
1646
1647 ebitmap_for_each_positive_bit(&tgt_bitmap, tnode, t) {
1648 tgt = DATUM(db->val_to_type[t]);
1649
1650 rc = __cil_avrule_expand(pdb, kind, src, tgt, classperms, cond_node, cond_flavor);
1651 if (rc != SEPOL_OK) {
1652 ebitmap_destroy(&tgt_bitmap);
1653 goto exit;
1654 }
1655 }
1656 ebitmap_destroy(&tgt_bitmap);
1657 }
1658 }
1659
1660 return SEPOL_OK;
1661
1662 exit:
1663 return rc;
1664 }
1665
1666 int cil_avrule_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_avrule *cil_avrule)
1667 {
1668 return __cil_avrule_to_avtab(pdb, db, cil_avrule, NULL, CIL_FALSE);
1669 }
1670
1671 // Copied from checkpolicy/policy_define.c
1672
1673 /* index of the u32 containing the permission */
1674 #define XPERM_IDX(x) (x >> 5)
1675 /* set bits 0 through x-1 within the u32 */
1676 #define XPERM_SETBITS(x) ((UINT32_C(1) << (x & 0x1f)) - 1)
1677 /* low value for this u32 */
1678 #define XPERM_LOW(x) (x << 5)
1679 /* high value for this u32 */
1680 #define XPERM_HIGH(x) (((x + 1) << 5) - 1)
1681 static void __avrule_xperm_setrangebits(uint16_t low, uint16_t high, struct avtab_extended_perms *xperms)
1682 {
1683 unsigned int i;
1684 uint16_t h = high + 1;
1685 /* for each u32 that this low-high range touches, set driver permissions */
1686 for (i = XPERM_IDX(low); i <= XPERM_IDX(high); i++) {
1687 /* set all bits in u32 */
1688 if ((low <= XPERM_LOW(i)) && (high >= XPERM_HIGH(i)))
1689 xperms->perms[i] |= ~0U;
1690 /* set low bits */
1691 else if ((low <= XPERM_LOW(i)) && (high < XPERM_HIGH(i)))
1692 xperms->perms[i] |= XPERM_SETBITS(h);
1693 /* set high bits */
1694 else if ((low > XPERM_LOW(i)) && (high >= XPERM_HIGH(i)))
1695 xperms->perms[i] |= ~0U - XPERM_SETBITS(low);
1696 /* set middle bits */
1697 else if ((low > XPERM_LOW(i)) && (high <= XPERM_HIGH(i)))
1698 xperms->perms[i] |= XPERM_SETBITS(h) - XPERM_SETBITS(low);
1699 }
1700 }
1701
1702 static char* __cil_xperm_kind_to_str(uint32_t xperm_kind)
1703 {
1704 switch (xperm_kind) {
1705 case CIL_PERMX_KIND_IOCTL:
1706 return CIL_KEY_IOCTL;
1707 case CIL_PERMX_KIND_NLMSG:
1708 return CIL_KEY_NLMSG;
1709 default:
1710 return (char *) "unknown";
1711 }
1712 }
1713
1714 #define IOC_DRIV(x) (x >> 8)
1715 #define IOC_FUNC(x) (x & 0xff)
1716
1717 static int __cil_permx_bitmap_to_sepol_xperms_list(uint32_t kind, ebitmap_t *xperms, struct cil_list **xperms_list)
1718 {
1719 ebitmap_node_t *node;
1720 unsigned int i;
1721 uint16_t low = 0, high = 0;
1722 struct avtab_extended_perms *partial = NULL;
1723 struct avtab_extended_perms *complete = NULL;
1724 int start_new_range;
1725
1726 cil_list_init(xperms_list, CIL_NONE);
1727
1728 start_new_range = 1;
1729
1730 ebitmap_for_each_positive_bit(xperms, node, i) {
1731 if (start_new_range) {
1732 low = i;
1733 start_new_range = 0;
1734 }
1735
1736 // continue if the current bit isn't the end of the driver function or the next bit is set
1737 if (IOC_FUNC(i) != 0xff && ebitmap_get_bit(xperms, i + 1)) {
1738 continue;
1739 }
1740
1741 // if we got here, i is the end of this range (either because the func
1742 // is 0xff or the next bit isn't set). The next time around we are
1743 // going to need a start a new range
1744 high = i;
1745 start_new_range = 1;
1746
1747 if (kind == CIL_PERMX_KIND_IOCTL && IOC_FUNC(low) == 0x00 && IOC_FUNC(high) == 0xff) {
1748 if (!complete) {
1749 complete = cil_calloc(1, sizeof(*complete));
1750 complete->driver = 0x0;
1751 complete->specified = AVTAB_XPERMS_IOCTLDRIVER;
1752 }
1753
1754 __avrule_xperm_setrangebits(IOC_DRIV(low), IOC_DRIV(low), complete);
1755 } else {
1756 if (partial && partial->driver != IOC_DRIV(low)) {
1757 cil_list_append(*xperms_list, CIL_NONE, partial);
1758 partial = NULL;
1759 }
1760
1761 if (!partial) {
1762 partial = cil_calloc(1, sizeof(*partial));
1763 partial->driver = IOC_DRIV(low);
1764 switch (kind) {
1765 case CIL_PERMX_KIND_IOCTL:
1766 partial->specified = AVTAB_XPERMS_IOCTLFUNCTION;
1767 break;
1768 case CIL_PERMX_KIND_NLMSG:
1769 partial->specified = AVTAB_XPERMS_NLMSG;
1770 break;
1771 }
1772 }
1773
1774 __avrule_xperm_setrangebits(IOC_FUNC(low), IOC_FUNC(high), partial);
1775 }
1776 }
1777
1778 if (partial) {
1779 cil_list_append(*xperms_list, CIL_NONE, partial);
1780 }
1781
1782 if (complete) {
1783 cil_list_append(*xperms_list, CIL_NONE, complete);
1784 }
1785
1786 return SEPOL_OK;
1787 }
1788
1789 static int __cil_avrulex_xperm_to_policydb(hashtab_key_t k, hashtab_datum_t datum, uint32_t xperm_kind, void *args)
1790 {
1791 int rc = SEPOL_OK;
1792 struct cil_args_booleanif *booleanif_args = args;
1793 struct policydb *pdb = booleanif_args->pdb;
1794 cond_node_t *cond_node = booleanif_args->cond_node;
1795 enum cil_flavor cond_flavor = booleanif_args->cond_flavor;
1796 avtab_key_t *avtab_key;
1797 avtab_datum_t avtab_datum;
1798 struct cil_list *xperms_list = NULL;
1799 struct cil_list_item *item;
1800 class_datum_t *sepol_obj;
1801 uint32_t data = 0;
1802 char *kind = NULL;
1803
1804 avtab_key = (avtab_key_t *)k;
1805
1806 sepol_obj = pdb->class_val_to_struct[avtab_key->target_class - 1];
1807
1808 // setting the data for an extended avtab isn't really necessary because
1809 // it is ignored by the kernel. However, neverallow checking requires that
1810 // the data value be set, so set it for that to work.
1811 kind = __cil_xperm_kind_to_str(xperm_kind);
1812 rc = __perm_str_to_datum(kind, sepol_obj, &data);
1813 if (rc != SEPOL_OK) {
1814 goto exit;
1815 }
1816 avtab_datum.data = data;
1817
1818 rc = __cil_permx_bitmap_to_sepol_xperms_list(xperm_kind, datum, &xperms_list);
1819 if (rc != SEPOL_OK) {
1820 goto exit;
1821 }
1822
1823 cil_list_for_each(item, xperms_list) {
1824 avtab_datum.xperms = item->data;
1825
1826 if (cond_node) {
1827 rc = __cil_cond_insert_rule(&pdb->te_cond_avtab, avtab_key, &avtab_datum, cond_node, cond_flavor);
1828 } else {
1829 rc = avtab_insert(&pdb->te_avtab, avtab_key, &avtab_datum);
1830 }
1831 if (rc != SEPOL_OK) {
1832 goto exit;
1833 }
1834 }
1835
1836 rc = SEPOL_OK;
1837
1838 exit:
1839 if (xperms_list != NULL) {
1840 cil_list_for_each(item, xperms_list) {
1841 free(item->data);
1842 }
1843 cil_list_destroy(&xperms_list, CIL_FALSE);
1844 }
1845 return rc;
1846 }
1847
1848 static int __cil_avrulex_ioctl_to_policydb(hashtab_key_t k, hashtab_datum_t datum, void *args) {
1849 return __cil_avrulex_xperm_to_policydb(k, datum, CIL_PERMX_KIND_IOCTL, args);
1850 }
1851
1852 static int __cil_avrulex_nlmsg_to_policydb(hashtab_key_t k, hashtab_datum_t datum, void *args) {
1853 return __cil_avrulex_xperm_to_policydb(k, datum, CIL_PERMX_KIND_NLMSG, args);
1854 }
1855
1856 static int __cil_avrulex_xperm_to_hashtable(hashtab_t h, uint16_t kind, uint32_t src, uint32_t tgt, uint32_t obj, ebitmap_t *xperms)
1857 {
1858 uint16_t specified;
1859 avtab_key_t *avtab_key;
1860 ebitmap_t *hashtab_xperms;
1861 int rc = SEPOL_ERR;
1862
1863 switch (kind) {
1864 case CIL_AVRULE_ALLOWED:
1865 specified = AVTAB_XPERMS_ALLOWED;
1866 break;
1867 case CIL_AVRULE_AUDITALLOW:
1868 specified = AVTAB_XPERMS_AUDITALLOW;
1869 break;
1870 case CIL_AVRULE_DONTAUDIT:
1871 specified = AVTAB_XPERMS_DONTAUDIT;
1872 break;
1873 default:
1874 rc = SEPOL_ERR;
1875 goto exit;
1876 }
1877
1878 avtab_key = cil_malloc(sizeof(*avtab_key));
1879 avtab_key->source_type = src;
1880 avtab_key->target_type = tgt;
1881 avtab_key->target_class = obj;
1882 avtab_key->specified = specified;
1883
1884 hashtab_xperms = (ebitmap_t *)hashtab_search(h, (hashtab_key_t)avtab_key);
1885 if (!hashtab_xperms) {
1886 hashtab_xperms = cil_malloc(sizeof(*hashtab_xperms));
1887 rc = ebitmap_cpy(hashtab_xperms, xperms);
1888 if (rc != SEPOL_OK) {
1889 free(hashtab_xperms);
1890 free(avtab_key);
1891 goto exit;
1892 }
1893 rc = hashtab_insert(h, (hashtab_key_t)avtab_key, hashtab_xperms);
1894 if (rc != SEPOL_OK) {
1895 free(hashtab_xperms);
1896 free(avtab_key);
1897 goto exit;
1898 }
1899 } else {
1900 free(avtab_key);
1901 rc = ebitmap_union(hashtab_xperms, xperms);
1902 if (rc != SEPOL_OK) {
1903 goto exit;
1904 }
1905 }
1906
1907 return SEPOL_OK;
1908
1909 exit:
1910 return rc;
1911 }
1912
1913 static int __cil_avrulex_to_hashtable_helper(policydb_t *pdb, uint16_t kind, struct cil_symtab_datum *src, struct cil_symtab_datum *tgt, struct cil_permissionx *permx, struct cil_args_xperm_tables *xt)
1914 {
1915 int rc = SEPOL_ERR;
1916 type_datum_t *sepol_src = NULL;
1917 type_datum_t *sepol_tgt = NULL;
1918 class_datum_t *sepol_obj = NULL;
1919 struct cil_list *class_list = NULL;
1920 struct cil_list_item *c;
1921
1922 rc = __cil_get_sepol_type_datum(pdb, src, &sepol_src);
1923 if (rc != SEPOL_OK) goto exit;
1924
1925 rc = __cil_get_sepol_type_datum(pdb, tgt, &sepol_tgt);
1926 if (rc != SEPOL_OK) goto exit;
1927
1928 class_list = cil_expand_class(permx->obj);
1929
1930 cil_list_for_each(c, class_list) {
1931 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_obj);
1932 if (rc != SEPOL_OK) goto exit;
1933
1934 switch (permx->kind) {
1935 case CIL_PERMX_KIND_IOCTL:
1936 rc = __cil_avrulex_xperm_to_hashtable(xt->ioctl, kind, sepol_src->s.value, sepol_tgt->s.value, sepol_obj->s.value, permx->perms);
1937 if (rc != SEPOL_OK) goto exit;
1938 break;
1939 case CIL_PERMX_KIND_NLMSG:
1940 rc = __cil_avrulex_xperm_to_hashtable(xt->nlmsg, kind, sepol_src->s.value, sepol_tgt->s.value, sepol_obj->s.value, permx->perms);
1941 if (rc != SEPOL_OK) goto exit;
1942 break;
1943 default:
1944 rc = SEPOL_ERR;
1945 goto exit;
1946 }
1947 }
1948
1949 rc = SEPOL_OK;
1950
1951 exit:
1952 cil_list_destroy(&class_list, CIL_FALSE);
1953
1954 return rc;
1955 }
1956
1957 static int cil_avrulex_to_hashtable(policydb_t *pdb, const struct cil_db *db, struct cil_avrule *cil_avrulex, struct cil_args_xperm_tables *xt)
1958 {
1959 int rc = SEPOL_ERR;
1960 uint16_t kind;
1961 struct cil_symtab_datum *src = NULL;
1962 struct cil_symtab_datum *tgt = NULL;
1963 ebitmap_t src_bitmap, tgt_bitmap;
1964 ebitmap_node_t *snode, *tnode;
1965 unsigned int s,t;
1966
1967 if (cil_avrulex->rule_kind == CIL_AVRULE_DONTAUDIT && db->disable_dontaudit == CIL_TRUE) {
1968 // Do not add dontaudit rules to binary
1969 rc = SEPOL_OK;
1970 goto exit;
1971 }
1972
1973 kind = cil_avrulex->rule_kind;
1974 src = cil_avrulex->src;
1975 tgt = cil_avrulex->tgt;
1976
1977 if (tgt->fqn == CIL_KEY_SELF) {
1978 rc = __cil_expand_type(src, &src_bitmap);
1979 if (rc != SEPOL_OK) goto exit;
1980
1981 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1982 src = DATUM(db->val_to_type[s]);
1983 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, src, cil_avrulex->perms.x.permx, xt);
1984 if (rc != SEPOL_OK) {
1985 ebitmap_destroy(&src_bitmap);
1986 goto exit;
1987 }
1988 }
1989 ebitmap_destroy(&src_bitmap);
1990 } else if (tgt->fqn == CIL_KEY_NOTSELF) {
1991 rc = __cil_expand_type(src, &src_bitmap);
1992 if (rc != SEPOL_OK) {
1993 goto exit;
1994 }
1995
1996 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
1997 src = DATUM(db->val_to_type[s]);
1998 for (t = 0; t < (unsigned int)db->num_types; t++) {
1999 if (s != t) {
2000 tgt = DATUM(db->val_to_type[t]);
2001 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2002 if (rc != SEPOL_OK) {
2003 ebitmap_destroy(&src_bitmap);
2004 goto exit;
2005 }
2006 }
2007 }
2008 }
2009 ebitmap_destroy(&src_bitmap);
2010 } else if (tgt->fqn == CIL_KEY_OTHER) {
2011 rc = __cil_expand_type(src, &src_bitmap);
2012 if (rc != SEPOL_OK) {
2013 goto exit;
2014 }
2015
2016 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
2017 src = DATUM(db->val_to_type[s]);
2018 ebitmap_for_each_positive_bit(&src_bitmap, tnode, t) {
2019 if (s != t) {
2020 tgt = DATUM(db->val_to_type[t]);
2021 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2022 if (rc != SEPOL_OK) {
2023 ebitmap_destroy(&src_bitmap);
2024 goto exit;
2025 }
2026 }
2027 }
2028 }
2029 ebitmap_destroy(&src_bitmap);
2030 } else {
2031 int expand_src = __cil_should_expand_attribute(db, src);
2032 int expand_tgt = __cil_should_expand_attribute(db, tgt);
2033
2034 if (!expand_src && !expand_tgt) {
2035 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2036 if (rc != SEPOL_OK) {
2037 goto exit;
2038 }
2039 } else if (expand_src && expand_tgt) {
2040 rc = __cil_expand_type(src, &src_bitmap);
2041 if (rc != SEPOL_OK) {
2042 goto exit;
2043 }
2044
2045 rc = __cil_expand_type(tgt, &tgt_bitmap);
2046 if (rc != SEPOL_OK) {
2047 ebitmap_destroy(&src_bitmap);
2048 goto exit;
2049 }
2050
2051 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
2052 src = DATUM(db->val_to_type[s]);
2053 ebitmap_for_each_positive_bit(&tgt_bitmap, tnode, t) {
2054 tgt = DATUM(db->val_to_type[t]);
2055
2056 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2057 if (rc != SEPOL_OK) {
2058 ebitmap_destroy(&src_bitmap);
2059 ebitmap_destroy(&tgt_bitmap);
2060 goto exit;
2061 }
2062 }
2063 }
2064 ebitmap_destroy(&src_bitmap);
2065 ebitmap_destroy(&tgt_bitmap);
2066 } else if (expand_src) {
2067 rc = __cil_expand_type(src, &src_bitmap);
2068 if (rc != SEPOL_OK) {
2069 goto exit;
2070 }
2071
2072 ebitmap_for_each_positive_bit(&src_bitmap, snode, s) {
2073 src = DATUM(db->val_to_type[s]);
2074
2075 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2076 if (rc != SEPOL_OK) {
2077 ebitmap_destroy(&src_bitmap);
2078 goto exit;
2079 }
2080 }
2081 ebitmap_destroy(&src_bitmap);
2082 } else { /* expand_tgt */
2083 rc = __cil_expand_type(tgt, &tgt_bitmap);
2084 if (rc != SEPOL_OK) {
2085 goto exit;
2086 }
2087
2088 ebitmap_for_each_positive_bit(&tgt_bitmap, tnode, t) {
2089 tgt = DATUM(db->val_to_type[t]);
2090
2091 rc = __cil_avrulex_to_hashtable_helper(pdb, kind, src, tgt, cil_avrulex->perms.x.permx, xt);
2092 if (rc != SEPOL_OK) {
2093 ebitmap_destroy(&tgt_bitmap);
2094 goto exit;
2095 }
2096 }
2097 ebitmap_destroy(&tgt_bitmap);
2098 }
2099 }
2100
2101 return SEPOL_OK;
2102
2103 exit:
2104 return rc;
2105 }
2106
2107 static int __cil_avrulex_xperm_destroy(hashtab_key_t k, hashtab_datum_t datum, __attribute__((unused)) void *args)
2108 {
2109 free(k);
2110 ebitmap_destroy(datum);
2111 free(datum);
2112
2113 return SEPOL_OK;
2114 }
2115
2116 static int __cil_cond_to_policydb_helper(struct cil_tree_node *node, __attribute__((unused)) uint32_t *finished, void *extra_args)
2117 {
2118 int rc;
2119 enum cil_flavor flavor;
2120 struct cil_args_booleanif *args = extra_args;
2121 const struct cil_db *db = args->db;
2122 policydb_t *pdb = args->pdb;
2123 cond_node_t *cond_node = args->cond_node;
2124 enum cil_flavor cond_flavor = args->cond_flavor;
2125 struct cil_type_rule *cil_type_rule;
2126 struct cil_avrule *cil_avrule;
2127 struct cil_nametypetransition *cil_typetrans;
2128
2129 flavor = node->flavor;
2130 switch (flavor) {
2131 case CIL_NAMETYPETRANSITION:
2132 cil_typetrans = (struct cil_nametypetransition*)node->data;
2133 if (DATUM(cil_typetrans->name)->fqn != CIL_KEY_STAR) {
2134 cil_log(CIL_ERR, "typetransition with file name not allowed within a booleanif block.\n");
2135 cil_tree_log(node, CIL_ERR,"Invalid typetransition statement");
2136 goto exit;
2137 }
2138 rc = __cil_typetransition_to_avtab(pdb, db, cil_typetrans, cond_node, cond_flavor);
2139 if (rc != SEPOL_OK) {
2140 cil_tree_log(node, CIL_ERR, "Failed to insert type transition into avtab");
2141 goto exit;
2142 }
2143 break;
2144 case CIL_TYPE_RULE:
2145 cil_type_rule = node->data;
2146 rc = __cil_type_rule_to_avtab(pdb, db, cil_type_rule, cond_node, cond_flavor);
2147 if (rc != SEPOL_OK) {
2148 cil_tree_log(node, CIL_ERR, "Failed to insert typerule into avtab");
2149 goto exit;
2150 }
2151 break;
2152 case CIL_AVRULE:
2153 cil_avrule = node->data;
2154 rc = __cil_avrule_to_avtab(pdb, db, cil_avrule, cond_node, cond_flavor);
2155 if (rc != SEPOL_OK) {
2156 cil_tree_log(node, CIL_ERR, "Failed to insert avrule into avtab");
2157 goto exit;
2158 }
2159 break;
2160 case CIL_AVRULEX:
2161 if (db->policy_version < POLICYDB_VERSION_COND_XPERMS) {
2162 cil_tree_log(node, CIL_ERR, "Extended permission rules are not supported in policy version %d\n",
2163 db->policy_version);
2164 rc = SEPOL_ERR;
2165 goto exit;
2166 }
2167 cil_avrule = node->data;
2168 rc = cil_avrulex_to_hashtable(pdb, db, cil_avrule, &args->avrulex_xperm_tables);
2169 if (rc != SEPOL_OK) {
2170 cil_tree_log(node, CIL_ERR, "Failed to insert avrulex into avtab");
2171 goto exit;
2172 }
2173 break;
2174
2175 case CIL_CALL:
2176 case CIL_TUNABLEIF:
2177 case CIL_SRC_INFO:
2178 break;
2179 default:
2180 cil_tree_log(node, CIL_ERR, "Invalid statement within booleanif");
2181 goto exit;
2182 }
2183
2184 return SEPOL_OK;
2185
2186 exit:
2187 return SEPOL_ERR;
2188 }
2189
2190 static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor, char **out);
2191
2192 static void __cil_expr_to_string_helper(struct cil_list_item *curr, enum cil_flavor flavor, char **out)
2193 {
2194 char *c;
2195
2196 if (curr->flavor == CIL_DATUM) {
2197 *out = cil_strdup(DATUM(curr->data)->fqn);
2198 } else if (curr->flavor == CIL_LIST) {
2199 __cil_expr_to_string(curr->data, flavor, &c);
2200 cil_asprintf(out, "(%s)", c);
2201 free(c);
2202 } else if (flavor == CIL_PERMISSIONX) {
2203 // permissionx expressions aren't resolved into anything, so curr->flavor
2204 // is just a CIL_STRING, not a CIL_DATUM, so just check on flavor for those
2205 *out = cil_strdup(curr->data);
2206 }
2207 }
2208
2209 static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor, char **out)
2210 {
2211 struct cil_list_item *curr;
2212 char *s1 = NULL;
2213 char *s2 = NULL;
2214 enum cil_flavor op;
2215
2216 if (expr == NULL || expr->head == NULL) {
2217 *out = cil_strdup("");
2218 return;
2219 }
2220
2221 curr = expr->head;
2222
2223 if (curr->flavor == CIL_OP) {
2224 op = (enum cil_flavor)(uintptr_t)curr->data;
2225
2226 if (op == CIL_ALL) {
2227 *out = cil_strdup(CIL_KEY_ALL);
2228 } else if (op == CIL_RANGE) {
2229 __cil_expr_to_string_helper(curr->next, flavor, &s1);
2230 __cil_expr_to_string_helper(curr->next->next, flavor, &s2);
2231 cil_asprintf(out, "%s %s %s", CIL_KEY_RANGE, s1, s2);
2232 free(s1);
2233 free(s2);
2234 } else {
2235 __cil_expr_to_string_helper(curr->next, flavor, &s1);
2236
2237 if (op == CIL_NOT) {
2238 cil_asprintf(out, "%s %s", CIL_KEY_NOT, s1);
2239 free(s1);
2240 } else {
2241 const char *opstr = "";
2242
2243 __cil_expr_to_string_helper(curr->next->next, flavor, &s2);
2244
2245 if (op == CIL_OR) {
2246 opstr = CIL_KEY_OR;
2247 } else if (op == CIL_AND) {
2248 opstr = CIL_KEY_AND;
2249 } else if (op == CIL_XOR) {
2250 opstr = CIL_KEY_XOR;
2251 }
2252
2253 cil_asprintf(out, "%s %s %s", opstr, s1, s2);
2254 free(s1);
2255 free(s2);
2256 }
2257 }
2258 } else {
2259 char *c1 = NULL;
2260 char *c2 = NULL;
2261 __cil_expr_to_string_helper(curr, flavor, &c1);
2262 for (curr = curr->next; curr; curr = curr->next) {
2263 s1 = NULL;
2264 __cil_expr_to_string_helper(curr, flavor, &s1);
2265 cil_asprintf(&c2, "%s %s", c1, s1);
2266 free(c1);
2267 free(s1);
2268 c1 = c2;
2269 }
2270 *out = c1;
2271 }
2272 }
2273
2274 static int __cil_cond_expr_to_sepol_expr_helper(policydb_t *pdb, struct cil_list *cil_expr, cond_expr_t **head, cond_expr_t **tail);
2275
2276 static int __cil_cond_item_to_sepol_expr(policydb_t *pdb, struct cil_list_item *item, cond_expr_t **head, cond_expr_t **tail)
2277 {
2278 if (item == NULL) {
2279 goto exit;
2280 } else if (item->flavor == CIL_DATUM) {
2281 char *key = DATUM(item->data)->fqn;
2282 cond_bool_datum_t *sepol_bool = hashtab_search(pdb->p_bools.table, key);
2283 if (sepol_bool == NULL) {
2284 cil_log(CIL_INFO, "Failed to find boolean\n");
2285 goto exit;
2286 }
2287 *head = cil_malloc(sizeof(cond_expr_t));
2288 (*head)->next = NULL;
2289 (*head)->expr_type = COND_BOOL;
2290 (*head)->boolean = sepol_bool->s.value;
2291 *tail = *head;
2292 } else if (item->flavor == CIL_LIST) {
2293 struct cil_list *l = item->data;
2294 int rc = __cil_cond_expr_to_sepol_expr_helper(pdb, l, head, tail);
2295 if (rc != SEPOL_OK) {
2296 goto exit;
2297 }
2298 } else {
2299 goto exit;
2300 }
2301
2302 return SEPOL_OK;
2303
2304 exit:
2305 return SEPOL_ERR;
2306 }
2307
2308 static int __cil_cond_expr_to_sepol_expr_helper(policydb_t *pdb, struct cil_list *cil_expr, cond_expr_t **head, cond_expr_t **tail)
2309 {
2310 int rc = SEPOL_ERR;
2311 struct cil_list_item *item = cil_expr->head;
2312 enum cil_flavor flavor = cil_expr->flavor;
2313 cond_expr_t *op, *h1, *h2, *t1, *t2;
2314
2315 if (flavor != CIL_BOOL) {
2316 cil_log(CIL_INFO, "Expected boolean expression\n");
2317 goto exit;
2318 }
2319
2320 if (item == NULL) {
2321 goto exit;
2322 } else if (item->flavor == CIL_OP) {
2323 enum cil_flavor cil_op = (enum cil_flavor)(uintptr_t)item->data;
2324
2325 op = cil_malloc(sizeof(*op));
2326 op->boolean = 0;
2327 op->next = NULL;
2328
2329 switch (cil_op) {
2330 case CIL_NOT:
2331 op->expr_type = COND_NOT;
2332 break;
2333 case CIL_OR:
2334 op->expr_type = COND_OR;
2335 break;
2336 case CIL_AND:
2337 op->expr_type = COND_AND;
2338 break;
2339 case CIL_XOR:
2340 op->expr_type = COND_XOR;
2341 break;
2342 case CIL_EQ:
2343 op->expr_type = COND_EQ;
2344 break;
2345 case CIL_NEQ:
2346 op->expr_type = COND_NEQ;
2347 break;
2348 default:
2349 free(op);
2350 goto exit;
2351 }
2352
2353 rc = __cil_cond_item_to_sepol_expr(pdb, item->next, &h1, &t1);
2354 if (rc != SEPOL_OK) {
2355 cil_log(CIL_INFO, "Failed to get first operand of conditional expression\n");
2356 free(op);
2357 goto exit;
2358 }
2359
2360 if (cil_op == CIL_NOT) {
2361 *head = h1;
2362 t1->next = op;
2363 *tail = op;
2364 } else {
2365 rc = __cil_cond_item_to_sepol_expr(pdb, item->next->next, &h2, &t2);
2366 if (rc != SEPOL_OK) {
2367 cil_log(CIL_INFO, "Failed to get second operand of conditional expression\n");
2368 free(op);
2369 cond_expr_destroy(h1);
2370 goto exit;
2371 }
2372
2373 *head = h1;
2374 t1->next = h2;
2375 t2->next = op;
2376 *tail = op;
2377 }
2378 } else {
2379 rc = __cil_cond_item_to_sepol_expr(pdb, item, &h1, &t1);
2380 if (rc != SEPOL_OK) {
2381 cil_log(CIL_INFO, "Failed to get initial item in conditional list\n");
2382 goto exit;
2383 }
2384 *head = h1;
2385 for (item = item->next; item; item = item->next) {
2386 rc = __cil_cond_item_to_sepol_expr(pdb, item, &h2, &t2);
2387 if (rc != SEPOL_OK) {
2388 cil_log(CIL_INFO, "Failed to get item in conditional list\n");
2389 cond_expr_destroy(*head);
2390 goto exit;
2391 }
2392 op = cil_malloc(sizeof(*op));
2393 op->boolean = 0;
2394 op->next = NULL;
2395 op->expr_type = COND_OR;
2396 t1->next = h2;
2397 t2->next = op;
2398 t1 = op;
2399 }
2400 *tail = t1;
2401 }
2402
2403 return SEPOL_OK;
2404
2405 exit:
2406 return SEPOL_ERR;
2407 }
2408
2409 static int __cil_cond_expr_to_sepol_expr(policydb_t *pdb, struct cil_list *cil_expr, cond_expr_t **sepol_expr)
2410 {
2411 int rc;
2412 cond_expr_t *head, *tail;
2413
2414 rc = __cil_cond_expr_to_sepol_expr_helper(pdb, cil_expr, &head, &tail);
2415 if (rc != SEPOL_OK) {
2416 return SEPOL_ERR;
2417 }
2418 *sepol_expr = head;
2419
2420 return SEPOL_OK;
2421 }
2422
2423 static int __cil_validate_cond_expr(cond_expr_t *cond_expr)
2424 {
2425 cond_expr_t *e;
2426 int depth = -1;
2427
2428 for (e = cond_expr; e != NULL; e = e->next) {
2429 switch (e->expr_type) {
2430 case COND_BOOL:
2431 if (depth == (COND_EXPR_MAXDEPTH - 1)) {
2432 cil_log(CIL_ERR,"Conditional expression exceeded max allowable depth\n");
2433 return SEPOL_ERR;
2434 }
2435 depth++;
2436 break;
2437 case COND_NOT:
2438 if (depth < 0) {
2439 cil_log(CIL_ERR,"Invalid conditional expression\n");
2440 return SEPOL_ERR;
2441 }
2442 break;
2443 case COND_OR:
2444 case COND_AND:
2445 case COND_XOR:
2446 case COND_EQ:
2447 case COND_NEQ:
2448 if (depth < 1) {
2449 cil_log(CIL_ERR,"Invalid conditional expression\n");
2450 return SEPOL_ERR;
2451 }
2452 depth--;
2453 break;
2454 default:
2455 cil_log(CIL_ERR,"Invalid conditional expression\n");
2456 return SEPOL_ERR;
2457 }
2458 }
2459
2460 if (depth != 0) {
2461 cil_log(CIL_ERR,"Invalid conditional expression\n");
2462 return SEPOL_ERR;
2463 }
2464
2465 return SEPOL_OK;
2466 }
2467
2468 int cil_booleanif_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_tree_node *node)
2469 {
2470 int rc = SEPOL_ERR;
2471 struct cil_args_booleanif bool_args;
2472 struct cil_booleanif *cil_boolif = (struct cil_booleanif*)node->data;
2473 struct cil_tree_node *cb_node;
2474 struct cil_tree_node *true_node = NULL;
2475 struct cil_tree_node *false_node = NULL;
2476 struct cil_tree_node *tmp_node = NULL;
2477 cond_node_t *tmp_cond = NULL;
2478 cond_node_t *cond_node = NULL;
2479 int was_created;
2480 int swapped = CIL_FALSE;
2481 cond_av_list_t tmp_cl;
2482 hashtab_t avrulex_ioctl_table = NULL;
2483 hashtab_t avrulex_nlmsg_table = NULL;
2484
2485 tmp_cond = cond_node_create(pdb, NULL);
2486 if (tmp_cond == NULL) {
2487 rc = SEPOL_ERR;
2488 cil_tree_log(node, CIL_INFO, "Failed to create sepol conditional node");
2489 goto exit;
2490 }
2491
2492 rc = __cil_cond_expr_to_sepol_expr(pdb, cil_boolif->datum_expr, &tmp_cond->expr);
2493 if (rc != SEPOL_OK) {
2494 cil_tree_log(node, CIL_INFO, "Failed to convert CIL conditional expression to sepol expression");
2495 goto exit;
2496 }
2497
2498 rc = __cil_validate_cond_expr(tmp_cond->expr);
2499 if (rc != SEPOL_OK) {
2500 goto exit;
2501 }
2502
2503 tmp_cond->true_list = &tmp_cl;
2504
2505 rc = cond_normalize_expr(pdb, tmp_cond);
2506 if (rc != SEPOL_OK) {
2507 goto exit;
2508 }
2509
2510 if (tmp_cond->false_list != NULL) {
2511 tmp_cond->true_list = NULL;
2512 swapped = CIL_TRUE;
2513 }
2514
2515 cond_node = cond_node_find(pdb, tmp_cond, pdb->cond_list, &was_created);
2516 if (cond_node == NULL) {
2517 rc = SEPOL_ERR;
2518 goto exit;
2519 }
2520
2521 if (was_created) {
2522 cond_node->next = pdb->cond_list;
2523 pdb->cond_list = cond_node;
2524 }
2525
2526 cond_expr_destroy(tmp_cond->expr);
2527 free(tmp_cond);
2528 tmp_cond = NULL;
2529
2530 for (cb_node = node->cl_head; cb_node != NULL; cb_node = cb_node->next) {
2531 if (cb_node->flavor == CIL_CONDBLOCK) {
2532 struct cil_condblock *cb = cb_node->data;
2533 if (cb->flavor == CIL_CONDTRUE) {
2534 true_node = cb_node;
2535 } else if (cb->flavor == CIL_CONDFALSE) {
2536 false_node = cb_node;
2537 }
2538 }
2539 }
2540
2541 if (swapped) {
2542 tmp_node = true_node;
2543 true_node = false_node;
2544 false_node = tmp_node;
2545 }
2546
2547 bool_args.db = db;
2548 bool_args.pdb = pdb;
2549 bool_args.cond_node = cond_node;
2550
2551 if (true_node != NULL) {
2552 avrulex_ioctl_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
2553 if (!avrulex_ioctl_table) {
2554 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
2555 goto exit;
2556 }
2557
2558 avrulex_nlmsg_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
2559 if (!avrulex_nlmsg_table) {
2560 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
2561 goto exit;
2562 }
2563
2564 bool_args.cond_flavor = CIL_CONDTRUE;
2565 bool_args.avrulex_xperm_tables.ioctl = avrulex_ioctl_table;
2566 bool_args.avrulex_xperm_tables.nlmsg = avrulex_nlmsg_table;
2567 rc = cil_tree_walk(true_node, __cil_cond_to_policydb_helper, NULL, NULL, &bool_args);
2568 if (rc != SEPOL_OK) {
2569 cil_tree_log(true_node, CIL_ERR, "Failure while walking true conditional block");
2570 goto exit;
2571 }
2572
2573 rc = hashtab_map(avrulex_ioctl_table, __cil_avrulex_ioctl_to_policydb, &bool_args);
2574 if (rc != SEPOL_OK) {
2575 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
2576 goto exit;
2577 }
2578
2579 rc = hashtab_map(avrulex_nlmsg_table, __cil_avrulex_nlmsg_to_policydb, &bool_args);
2580 if (rc != SEPOL_OK) {
2581 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
2582 goto exit;
2583 }
2584
2585 hashtab_map(avrulex_nlmsg_table, __cil_avrulex_xperm_destroy, NULL);
2586 hashtab_destroy(avrulex_nlmsg_table);
2587 avrulex_nlmsg_table = NULL;
2588
2589 hashtab_map(avrulex_ioctl_table, __cil_avrulex_xperm_destroy, NULL);
2590 hashtab_destroy(avrulex_ioctl_table);
2591 avrulex_ioctl_table = NULL;
2592 }
2593
2594 if (false_node != NULL) {
2595 avrulex_ioctl_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
2596 if (!avrulex_ioctl_table) {
2597 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
2598 goto exit;
2599 }
2600
2601 avrulex_nlmsg_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
2602 if (!avrulex_nlmsg_table) {
2603 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
2604 goto exit;
2605 }
2606
2607 bool_args.cond_flavor = CIL_CONDFALSE;
2608 bool_args.avrulex_xperm_tables.ioctl = avrulex_ioctl_table;
2609 bool_args.avrulex_xperm_tables.nlmsg = avrulex_nlmsg_table;
2610 rc = cil_tree_walk(false_node, __cil_cond_to_policydb_helper, NULL, NULL, &bool_args);
2611 if (rc != SEPOL_OK) {
2612 cil_tree_log(false_node, CIL_ERR, "Failure while walking false conditional block");
2613 goto exit;
2614 }
2615
2616 rc = hashtab_map(avrulex_ioctl_table, __cil_avrulex_ioctl_to_policydb, &bool_args);
2617 if (rc != SEPOL_OK) {
2618 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
2619 goto exit;
2620 }
2621
2622 rc = hashtab_map(avrulex_nlmsg_table, __cil_avrulex_nlmsg_to_policydb, &bool_args);
2623 if (rc != SEPOL_OK) {
2624 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
2625 goto exit;
2626 }
2627
2628 hashtab_map(avrulex_nlmsg_table, __cil_avrulex_xperm_destroy, NULL);
2629 hashtab_destroy(avrulex_nlmsg_table);
2630 avrulex_nlmsg_table = NULL;
2631
2632
2633 hashtab_map(avrulex_ioctl_table, __cil_avrulex_xperm_destroy, NULL);
2634 hashtab_destroy(avrulex_ioctl_table);
2635 avrulex_ioctl_table = NULL;
2636 }
2637
2638 return SEPOL_OK;
2639
2640 exit:
2641 hashtab_map(avrulex_nlmsg_table, __cil_avrulex_xperm_destroy, NULL);
2642 hashtab_destroy(avrulex_nlmsg_table);
2643 hashtab_map(avrulex_ioctl_table, __cil_avrulex_xperm_destroy, NULL);
2644 hashtab_destroy(avrulex_ioctl_table);
2645 if (tmp_cond) {
2646 if (tmp_cond->expr)
2647 cond_expr_destroy(tmp_cond->expr);
2648 free(tmp_cond);
2649 }
2650 return rc;
2651 }
2652
2653 int cil_roletrans_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_roletransition *roletrans, hashtab_t role_trans_table)
2654 {
2655 int rc = SEPOL_ERR;
2656 role_datum_t *sepol_src = NULL;
2657 type_datum_t *sepol_tgt = NULL;
2658 class_datum_t *sepol_obj = NULL;
2659 struct cil_list *class_list = NULL;
2660 role_datum_t *sepol_result = NULL;
2661 role_trans_t *new = NULL;
2662 uint32_t *new_role = NULL;
2663 ebitmap_t role_bitmap, type_bitmap;
2664 ebitmap_node_t *rnode, *tnode;
2665 unsigned int i, j;
2666 struct cil_list_item *c;
2667
2668 rc = __cil_expand_role(DATUM(roletrans->src), &role_bitmap);
2669 if (rc != SEPOL_OK) goto exit;
2670
2671 rc = __cil_expand_type(roletrans->tgt, &type_bitmap);
2672 if (rc != SEPOL_OK) goto exit;
2673
2674 class_list = cil_expand_class(roletrans->obj);
2675
2676 rc = __cil_get_sepol_role_datum(pdb, DATUM(roletrans->result), &sepol_result);
2677 if (rc != SEPOL_OK) goto exit;
2678
2679 ebitmap_for_each_positive_bit(&role_bitmap, rnode, i) {
2680 rc = __cil_get_sepol_role_datum(pdb, DATUM(db->val_to_role[i]), &sepol_src);
2681 if (rc != SEPOL_OK) goto exit;
2682
2683 ebitmap_for_each_positive_bit(&type_bitmap, tnode, j) {
2684 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[j]), &sepol_tgt);
2685 if (rc != SEPOL_OK) goto exit;
2686
2687 cil_list_for_each(c, class_list) {
2688 int add = CIL_TRUE;
2689 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_obj);
2690 if (rc != SEPOL_OK) goto exit;
2691
2692 new = cil_malloc(sizeof(*new));
2693 memset(new, 0, sizeof(*new));
2694 new->role = sepol_src->s.value;
2695 new->type = sepol_tgt->s.value;
2696 new->tclass = sepol_obj->s.value;
2697 new->new_role = sepol_result->s.value;
2698
2699 rc = hashtab_insert(role_trans_table, (hashtab_key_t)new, &(new->new_role));
2700 if (rc != SEPOL_OK) {
2701 if (rc == SEPOL_EEXIST) {
2702 add = CIL_FALSE;
2703 new_role = hashtab_search(role_trans_table, (hashtab_key_t)new);
2704 if (new->new_role != *new_role) {
2705 cil_log(CIL_ERR, "Conflicting role transition rules\n");
2706 } else {
2707 rc = SEPOL_OK;
2708 }
2709 } else {
2710 cil_log(CIL_ERR, "Out of memory\n");
2711 }
2712 }
2713
2714 if (add == CIL_TRUE) {
2715 new->next = pdb->role_tr;
2716 pdb->role_tr = new;
2717 } else {
2718 free(new);
2719 if (rc != SEPOL_OK) {
2720 goto exit;
2721 }
2722 }
2723 }
2724 }
2725 }
2726
2727 rc = SEPOL_OK;
2728
2729 exit:
2730 ebitmap_destroy(&role_bitmap);
2731 ebitmap_destroy(&type_bitmap);
2732 cil_list_destroy(&class_list, CIL_FALSE);
2733 return rc;
2734 }
2735
2736 int cil_roleallow_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_roleallow *roleallow)
2737 {
2738 int rc = SEPOL_ERR;
2739 role_datum_t *sepol_src = NULL;
2740 role_datum_t *sepol_tgt = NULL;
2741 role_allow_t *sepol_roleallow = NULL;
2742 ebitmap_t src_bitmap, tgt_bitmap;
2743 ebitmap_node_t *node1, *node2;
2744 unsigned int i, j;
2745
2746 rc = __cil_expand_role(roleallow->src, &src_bitmap);
2747 if (rc != SEPOL_OK) goto exit;
2748
2749 rc = __cil_expand_role(roleallow->tgt, &tgt_bitmap);
2750 if (rc != SEPOL_OK) goto exit;
2751
2752 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
2753 rc = __cil_get_sepol_role_datum(pdb, DATUM(db->val_to_role[i]), &sepol_src);
2754 if (rc != SEPOL_OK) goto exit;
2755
2756 ebitmap_for_each_positive_bit(&tgt_bitmap, node2, j) {
2757 rc = __cil_get_sepol_role_datum(pdb, DATUM(db->val_to_role[j]), &sepol_tgt);
2758 if (rc != SEPOL_OK) goto exit;
2759
2760 sepol_roleallow = cil_malloc(sizeof(*sepol_roleallow));
2761 memset(sepol_roleallow, 0, sizeof(role_allow_t));
2762 sepol_roleallow->role = sepol_src->s.value;
2763 sepol_roleallow->new_role = sepol_tgt->s.value;
2764
2765 sepol_roleallow->next = pdb->role_allow;
2766 pdb->role_allow = sepol_roleallow;
2767 }
2768 }
2769
2770 rc = SEPOL_OK;
2771
2772 exit:
2773 ebitmap_destroy(&src_bitmap);
2774 ebitmap_destroy(&tgt_bitmap);
2775 return rc;
2776 }
2777
2778 static int __cil_constrain_expr_datum_to_sepol_expr(policydb_t *pdb, const struct cil_db *db, struct cil_list_item *item, enum cil_flavor expr_flavor, constraint_expr_t *expr)
2779 {
2780 int rc = SEPOL_ERR;
2781
2782 if (expr_flavor == CIL_USER) {
2783 user_datum_t *sepol_user = NULL;
2784 ebitmap_t user_bitmap;
2785 ebitmap_node_t *unode;
2786 unsigned int i;
2787
2788 rc = __cil_expand_user(item->data, &user_bitmap);
2789 if (rc != SEPOL_OK) goto exit;
2790
2791 ebitmap_for_each_positive_bit(&user_bitmap, unode, i) {
2792 rc = __cil_get_sepol_user_datum(pdb, DATUM(db->val_to_user[i]), &sepol_user);
2793 if (rc != SEPOL_OK) {
2794 ebitmap_destroy(&user_bitmap);
2795 goto exit;
2796 }
2797
2798 if (ebitmap_set_bit(&expr->names, sepol_user->s.value - 1, 1)) {
2799 ebitmap_destroy(&user_bitmap);
2800 goto exit;
2801 }
2802 }
2803 ebitmap_destroy(&user_bitmap);
2804 } else if (expr_flavor == CIL_ROLE) {
2805 role_datum_t *sepol_role = NULL;
2806 ebitmap_t role_bitmap;
2807 ebitmap_node_t *rnode;
2808 unsigned int i;
2809
2810 rc = __cil_expand_role(item->data, &role_bitmap);
2811 if (rc != SEPOL_OK) goto exit;
2812
2813 ebitmap_for_each_positive_bit(&role_bitmap, rnode, i) {
2814 rc = __cil_get_sepol_role_datum(pdb, DATUM(db->val_to_role[i]), &sepol_role);
2815 if (rc != SEPOL_OK) {
2816 ebitmap_destroy(&role_bitmap);
2817 goto exit;
2818 }
2819
2820 if (ebitmap_set_bit(&expr->names, sepol_role->s.value - 1, 1)) {
2821 ebitmap_destroy(&role_bitmap);
2822 goto exit;
2823 }
2824 }
2825 ebitmap_destroy(&role_bitmap);
2826 } else if (expr_flavor == CIL_TYPE) {
2827 type_datum_t *sepol_type = NULL;
2828 ebitmap_t type_bitmap;
2829 ebitmap_node_t *tnode;
2830 unsigned int i;
2831
2832 if (pdb->policyvers >= POLICYDB_VERSION_CONSTRAINT_NAMES) {
2833 rc = __cil_get_sepol_type_datum(pdb, item->data, &sepol_type);
2834 if (rc != SEPOL_OK) {
2835 if (FLAVOR(item->data) == CIL_TYPEATTRIBUTE) {
2836 struct cil_typeattribute *attr = item->data;
2837 if (!attr->keep) {
2838 rc = 0;
2839 }
2840 }
2841 }
2842
2843 if (sepol_type) {
2844 rc = ebitmap_set_bit(&expr->type_names->types, sepol_type->s.value - 1, 1);
2845 }
2846
2847 if (rc != SEPOL_OK) {
2848 goto exit;
2849 }
2850 }
2851
2852 rc = __cil_expand_type(item->data, &type_bitmap);
2853 if (rc != SEPOL_OK) goto exit;
2854
2855 ebitmap_for_each_positive_bit(&type_bitmap, tnode, i) {
2856 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_type);
2857 if (rc != SEPOL_OK) {
2858 ebitmap_destroy(&type_bitmap);
2859 goto exit;
2860 }
2861
2862 if (ebitmap_set_bit(&expr->names, sepol_type->s.value - 1, 1)) {
2863 ebitmap_destroy(&type_bitmap);
2864 goto exit;
2865 }
2866 }
2867 ebitmap_destroy(&type_bitmap);
2868 } else {
2869 goto exit;
2870 }
2871
2872 return SEPOL_OK;
2873
2874 exit:
2875 return SEPOL_ERR;
2876 }
2877
2878 static int __cil_constrain_expr_leaf_to_sepol_expr(policydb_t *pdb, const struct cil_db *db, struct cil_list_item *op_item, enum cil_flavor expr_flavor, constraint_expr_t *expr)
2879 {
2880 int rc = SEPOL_ERR;
2881 struct cil_list_item *l_item = op_item->next;
2882 struct cil_list_item *r_item = op_item->next->next;
2883
2884 enum cil_flavor l_operand = (enum cil_flavor)(uintptr_t)l_item->data;
2885
2886 switch (l_operand) {
2887 case CIL_CONS_U1:
2888 expr->attr = CEXPR_USER;
2889 break;
2890 case CIL_CONS_U2:
2891 expr->attr = CEXPR_USER | CEXPR_TARGET;
2892 break;
2893 case CIL_CONS_U3:
2894 expr->attr = CEXPR_USER | CEXPR_XTARGET;
2895 break;
2896 case CIL_CONS_R1:
2897 expr->attr = CEXPR_ROLE;
2898 break;
2899 case CIL_CONS_R2:
2900 expr->attr = CEXPR_ROLE | CEXPR_TARGET;
2901 break;
2902 case CIL_CONS_R3:
2903 expr->attr = CEXPR_ROLE | CEXPR_XTARGET;
2904 break;
2905 case CIL_CONS_T1:
2906 expr->attr = CEXPR_TYPE;
2907 break;
2908 case CIL_CONS_T2:
2909 expr->attr = CEXPR_TYPE | CEXPR_TARGET;
2910 break;
2911 case CIL_CONS_T3:
2912 expr->attr = CEXPR_TYPE | CEXPR_XTARGET;
2913 break;
2914 case CIL_CONS_L1: {
2915 enum cil_flavor r_operand = (enum cil_flavor)(uintptr_t)r_item->data;
2916
2917 if (r_operand == CIL_CONS_L2) {
2918 expr->attr = CEXPR_L1L2;
2919 } else if (r_operand == CIL_CONS_H1) {
2920 expr->attr = CEXPR_L1H1;
2921 } else {
2922 expr->attr = CEXPR_L1H2;
2923 }
2924 break;
2925 }
2926 case CIL_CONS_L2:
2927 expr->attr = CEXPR_L2H2;
2928 break;
2929 case CIL_CONS_H1: {
2930 enum cil_flavor r_operand = (enum cil_flavor)(uintptr_t)r_item->data;
2931 if (r_operand == CIL_CONS_L2) {
2932 expr->attr = CEXPR_H1L2;
2933 } else {
2934 expr->attr = CEXPR_H1H2;
2935 }
2936 break;
2937 }
2938 default:
2939 goto exit;
2940 break;
2941 }
2942
2943 if (r_item->flavor == CIL_CONS_OPERAND) {
2944 expr->expr_type = CEXPR_ATTR;
2945 } else {
2946 expr->expr_type = CEXPR_NAMES;
2947 if (r_item->flavor == CIL_DATUM) {
2948 rc = __cil_constrain_expr_datum_to_sepol_expr(pdb, db, r_item, expr_flavor, expr);
2949 if (rc != SEPOL_OK) {
2950 goto exit;
2951 }
2952 } else if (r_item->flavor == CIL_LIST) {
2953 struct cil_list *r_expr = r_item->data;
2954 struct cil_list_item *curr;
2955 cil_list_for_each(curr, r_expr) {
2956 rc = __cil_constrain_expr_datum_to_sepol_expr(pdb, db, curr, expr_flavor, expr);
2957 if (rc != SEPOL_OK) {
2958 goto exit;
2959 }
2960 }
2961 } else {
2962 rc = SEPOL_ERR;
2963 goto exit;
2964 }
2965 }
2966
2967 return SEPOL_OK;
2968
2969 exit:
2970 return rc;
2971 }
2972
2973 static int __cil_constrain_expr_to_sepol_expr_helper(policydb_t *pdb, const struct cil_db *db, const struct cil_list *cil_expr, constraint_expr_t **head, constraint_expr_t **tail)
2974 {
2975 int rc = SEPOL_ERR;
2976 struct cil_list_item *item;
2977 enum cil_flavor flavor;
2978 enum cil_flavor cil_op;
2979 constraint_expr_t *op, *h1, *h2, *t1, *t2;
2980 int is_leaf = CIL_FALSE;
2981
2982 if (cil_expr == NULL) {
2983 return SEPOL_ERR;
2984 }
2985
2986 item = cil_expr->head;
2987 flavor = cil_expr->flavor;
2988
2989 op = cil_malloc(sizeof(constraint_expr_t));
2990 rc = constraint_expr_init(op);
2991 if (rc != SEPOL_OK) {
2992 goto exit;
2993 }
2994
2995 cil_op = (enum cil_flavor)(uintptr_t)item->data;
2996 switch (cil_op) {
2997 case CIL_NOT:
2998 op->expr_type = CEXPR_NOT;
2999 break;
3000 case CIL_AND:
3001 op->expr_type = CEXPR_AND;
3002 break;
3003 case CIL_OR:
3004 op->expr_type = CEXPR_OR;
3005 break;
3006 case CIL_EQ:
3007 op->op = CEXPR_EQ;
3008 is_leaf = CIL_TRUE;
3009 break;
3010 case CIL_NEQ:
3011 op->op = CEXPR_NEQ;
3012 is_leaf = CIL_TRUE;
3013 break;
3014 case CIL_CONS_DOM:
3015 op->op = CEXPR_DOM;
3016 is_leaf = CIL_TRUE;
3017 break;
3018 case CIL_CONS_DOMBY:
3019 op->op = CEXPR_DOMBY;
3020 is_leaf = CIL_TRUE;
3021 break;
3022 case CIL_CONS_INCOMP:
3023 op->op = CEXPR_INCOMP;
3024 is_leaf = CIL_TRUE;
3025 break;
3026 default:
3027 goto exit;
3028 }
3029
3030 if (is_leaf == CIL_TRUE) {
3031 rc = __cil_constrain_expr_leaf_to_sepol_expr(pdb, db, item, flavor, op);
3032 if (rc != SEPOL_OK) {
3033 goto exit;
3034 }
3035 *head = op;
3036 *tail = op;
3037 } else if (cil_op == CIL_NOT) {
3038 struct cil_list *l_expr = item->next->data;
3039 rc = __cil_constrain_expr_to_sepol_expr_helper(pdb, db, l_expr, &h1, &t1);
3040 if (rc != SEPOL_OK) {
3041 goto exit;
3042 }
3043 t1->next = op;
3044 *head = h1;
3045 *tail = op;
3046 } else {
3047 struct cil_list *l_expr = item->next->data;
3048 struct cil_list *r_expr = item->next->next->data;
3049 rc = __cil_constrain_expr_to_sepol_expr_helper(pdb, db, l_expr, &h1, &t1);
3050 if (rc != SEPOL_OK) {
3051 goto exit;
3052 }
3053 rc = __cil_constrain_expr_to_sepol_expr_helper(pdb, db, r_expr, &h2, &t2);
3054 if (rc != SEPOL_OK) {
3055 constraint_expr_destroy(h1);
3056 goto exit;
3057 }
3058 t1->next = h2;
3059 t2->next = op;
3060 *head = h1;
3061 *tail = op;
3062 }
3063
3064 return SEPOL_OK;
3065
3066 exit:
3067 constraint_expr_destroy(op);
3068 return SEPOL_ERR;
3069 }
3070
3071 static int __cil_constrain_expr_to_sepol_expr(policydb_t *pdb, const struct cil_db *db, const struct cil_list *cil_expr, constraint_expr_t **sepol_expr)
3072 {
3073 int rc;
3074 constraint_expr_t *head, *tail;
3075
3076 rc = __cil_constrain_expr_to_sepol_expr_helper(pdb, db, cil_expr, &head, &tail);
3077 if (rc != SEPOL_OK) {
3078 return SEPOL_ERR;
3079 }
3080
3081 *sepol_expr = head;
3082
3083 return SEPOL_OK;
3084 }
3085
3086 static int __cil_validate_constrain_expr(constraint_expr_t *sepol_expr)
3087 {
3088 constraint_expr_t *e;
3089 int depth = -1;
3090
3091 for (e = sepol_expr; e != NULL; e = e->next) {
3092 switch (e->expr_type) {
3093 case CEXPR_NOT:
3094 if (depth < 0) {
3095 cil_log(CIL_ERR,"Invalid constraint expression\n");
3096 return SEPOL_ERR;
3097 }
3098 break;
3099 case CEXPR_AND:
3100 case CEXPR_OR:
3101 if (depth < 1) {
3102 cil_log(CIL_ERR,"Invalid constraint expression\n");
3103 return SEPOL_ERR;
3104 }
3105 depth--;
3106 break;
3107 case CEXPR_ATTR:
3108 case CEXPR_NAMES:
3109 if (depth == (CEXPR_MAXDEPTH - 1)) {
3110 cil_log(CIL_ERR,"Constraint expression exceeded max allowable depth\n");
3111 return SEPOL_ERR;
3112 }
3113 depth++;
3114 break;
3115 default:
3116 cil_log(CIL_ERR,"Invalid constraint expression\n");
3117 return SEPOL_ERR;
3118 }
3119 }
3120
3121 if (depth != 0) {
3122 cil_log(CIL_ERR,"Invalid constraint expression\n");
3123 return SEPOL_ERR;
3124 }
3125
3126 return SEPOL_OK;
3127 }
3128
3129 static int cil_constrain_to_policydb_helper(policydb_t *pdb, const struct cil_db *db, struct cil_symtab_datum *class, struct cil_list *perms, struct cil_list *expr)
3130 {
3131 int rc = SEPOL_ERR;
3132 constraint_node_t *sepol_constrain = NULL;
3133 constraint_expr_t *sepol_expr = NULL;
3134 class_datum_t *sepol_class = NULL;
3135
3136 sepol_constrain = cil_malloc(sizeof(*sepol_constrain));
3137 memset(sepol_constrain, 0, sizeof(constraint_node_t));
3138
3139 rc = __cil_get_sepol_class_datum(pdb, class, &sepol_class);
3140 if (rc != SEPOL_OK) goto exit;
3141
3142 rc = __cil_perms_to_datum(perms, sepol_class, &sepol_constrain->permissions);
3143 if (rc != SEPOL_OK) {
3144 goto exit;
3145 }
3146
3147 if (sepol_constrain->permissions == 0) {
3148 /* No permissions, so don't insert rule. */
3149 free(sepol_constrain);
3150 return SEPOL_OK;
3151 }
3152
3153 rc = __cil_constrain_expr_to_sepol_expr(pdb, db, expr, &sepol_expr);
3154 if (rc != SEPOL_OK) {
3155 goto exit;
3156 }
3157
3158 rc = __cil_validate_constrain_expr(sepol_expr);
3159 if (rc != SEPOL_OK) {
3160 goto exit;
3161 }
3162
3163 sepol_constrain->expr = sepol_expr;
3164 sepol_constrain->next = sepol_class->constraints;
3165 sepol_class->constraints = sepol_constrain;
3166
3167 return SEPOL_OK;
3168
3169 exit:
3170 constraint_expr_destroy(sepol_expr);
3171 free(sepol_constrain);
3172 return rc;
3173 }
3174
3175 static int cil_constrain_expand(policydb_t *pdb, const struct cil_db *db, struct cil_list *classperms, struct cil_list *expr)
3176 {
3177 int rc = SEPOL_ERR;
3178 struct cil_list_item *curr;
3179
3180 cil_list_for_each(curr, classperms) {
3181 if (curr->flavor == CIL_CLASSPERMS) {
3182 struct cil_classperms *cp = curr->data;
3183 if (FLAVOR(cp->class) == CIL_CLASS) {
3184 rc = cil_constrain_to_policydb_helper(pdb, db, DATUM(cp->class), cp->perms, expr);
3185 if (rc != SEPOL_OK) {
3186 goto exit;
3187 }
3188 } else { /* MAP */
3189 struct cil_list_item *i = NULL;
3190 cil_list_for_each(i, cp->perms) {
3191 struct cil_perm *cmp = i->data;
3192 rc = cil_constrain_expand(pdb, db, cmp->classperms, expr);
3193 if (rc != SEPOL_OK) {
3194 goto exit;
3195 }
3196 }
3197 }
3198 } else { /* SET */
3199 struct cil_classperms_set *cp_set = curr->data;
3200 struct cil_classpermission *cp = cp_set->set;
3201 rc = cil_constrain_expand(pdb, db, cp->classperms, expr);
3202 if (rc != SEPOL_OK) {
3203 goto exit;
3204 }
3205 }
3206 }
3207
3208 return SEPOL_OK;
3209
3210 exit:
3211 return rc;
3212 }
3213
3214 int cil_constrain_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_constrain *cil_constrain)
3215 {
3216 int rc = SEPOL_ERR;
3217 rc = cil_constrain_expand(pdb, db, cil_constrain->classperms, cil_constrain->datum_expr);
3218 if (rc != SEPOL_OK) {
3219 goto exit;
3220 }
3221
3222 return SEPOL_OK;
3223
3224 exit:
3225 cil_log(CIL_ERR, "Failed to insert constraint into policydb\n");
3226 return rc;
3227 }
3228
3229 static int cil_validatetrans_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_validatetrans *cil_validatetrans)
3230 {
3231 int rc = SEPOL_ERR;
3232 struct cil_list *expr = cil_validatetrans->datum_expr;
3233 class_datum_t *sepol_class = NULL;
3234 struct cil_list *class_list;
3235 constraint_node_t *sepol_validatetrans = NULL;
3236 constraint_expr_t *sepol_expr = NULL;
3237 struct cil_list_item *c;
3238
3239 class_list = cil_expand_class(cil_validatetrans->class);
3240
3241 cil_list_for_each(c, class_list) {
3242 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_class);
3243 if (rc != SEPOL_OK) goto exit;
3244
3245 sepol_validatetrans = cil_malloc(sizeof(*sepol_validatetrans));
3246 memset(sepol_validatetrans, 0, sizeof(constraint_node_t));
3247
3248 rc = __cil_constrain_expr_to_sepol_expr(pdb, db, expr, &sepol_expr);
3249 if (rc != SEPOL_OK) {
3250 free(sepol_validatetrans);
3251 goto exit;
3252 }
3253 sepol_validatetrans->expr = sepol_expr;
3254
3255 sepol_validatetrans->next = sepol_class->validatetrans;
3256 sepol_class->validatetrans = sepol_validatetrans;
3257 }
3258
3259 rc = SEPOL_OK;
3260
3261 exit:
3262 cil_list_destroy(&class_list, CIL_FALSE);
3263 return rc;
3264 }
3265
3266 static int __cil_cats_to_mls_level(policydb_t *pdb, struct cil_cats *cats, mls_level_t *mls_level)
3267 {
3268 int rc = SEPOL_ERR;
3269 struct cil_list_item *i;
3270 cat_datum_t *sepol_cat = NULL;
3271
3272 cil_list_for_each(i, cats->datum_expr) {
3273 struct cil_tree_node *node = NODE(i->data);
3274 if (node->flavor == CIL_CATSET) {
3275 struct cil_list_item *j;
3276 struct cil_catset *cs = i->data;
3277 cil_list_for_each(j, cs->cats->datum_expr) {
3278 rc = __cil_get_sepol_cat_datum(pdb, j->data, &sepol_cat);
3279 if (rc != SEPOL_OK) goto exit;
3280
3281 rc = ebitmap_set_bit(&mls_level->cat, sepol_cat->s.value - 1, 1);
3282 if (rc != SEPOL_OK) goto exit;
3283 }
3284 } else {
3285 rc = __cil_get_sepol_cat_datum(pdb, i->data, &sepol_cat);
3286 if (rc != SEPOL_OK) goto exit;
3287
3288 rc = ebitmap_set_bit(&mls_level->cat, sepol_cat->s.value - 1, 1);
3289 if (rc != SEPOL_OK) goto exit;
3290 }
3291 }
3292
3293 return SEPOL_OK;
3294
3295 exit:
3296 return SEPOL_ERR;
3297 }
3298
3299 int cil_sepol_level_define(policydb_t *pdb, struct cil_sens *cil_sens)
3300 {
3301 int rc = SEPOL_ERR;
3302 struct cil_list_item *curr;
3303 level_datum_t *sepol_level = NULL;
3304 mls_level_t *mls_level = NULL;
3305
3306 rc = __cil_get_sepol_level_datum(pdb, DATUM(cil_sens), &sepol_level);
3307 if (rc != SEPOL_OK) goto exit;
3308
3309 mls_level = sepol_level->level;
3310
3311 ebitmap_init(&mls_level->cat);
3312
3313 if (cil_sens->cats_list) {
3314 cil_list_for_each(curr, cil_sens->cats_list) {
3315 struct cil_cats *cats = curr->data;
3316 rc = __cil_cats_to_mls_level(pdb, cats, mls_level);
3317 if (rc != SEPOL_OK) {
3318 cil_log(CIL_INFO, "Failed to insert category set into sepol mls level\n");
3319 goto exit;
3320 }
3321 }
3322 }
3323
3324 return SEPOL_OK;
3325
3326 exit:
3327 return rc;
3328 }
3329
3330 int cil_level_to_mls_level(policydb_t *pdb, struct cil_level *cil_level, mls_level_t *mls_level)
3331 {
3332 int rc = SEPOL_ERR;
3333 struct cil_sens *cil_sens = cil_level->sens;
3334 struct cil_cats *cats = cil_level->cats;
3335 level_datum_t *sepol_level = NULL;
3336
3337 rc = __cil_get_sepol_level_datum(pdb, DATUM(cil_sens), &sepol_level);
3338 if (rc != SEPOL_OK) goto exit;
3339
3340 mls_level->sens = sepol_level->level->sens;
3341
3342 ebitmap_init(&mls_level->cat);
3343
3344 if (cats != NULL) {
3345 rc = __cil_cats_to_mls_level(pdb, cats, mls_level);
3346 if (rc != SEPOL_OK) {
3347 cil_log(CIL_INFO, "Failed to insert category set into sepol mls level\n");
3348 goto exit;
3349 }
3350 }
3351
3352 rc = SEPOL_OK;
3353 exit:
3354 return rc;
3355 }
3356
3357 static int __cil_levelrange_to_mls_range(policydb_t *pdb, struct cil_levelrange *cil_lvlrange, mls_range_t *mls_range)
3358 {
3359 int rc = SEPOL_ERR;
3360 struct cil_level *low = cil_lvlrange->low;
3361 struct cil_level *high = cil_lvlrange->high;
3362 mls_level_t *mls_level = NULL;
3363
3364 mls_level = &mls_range->level[0];
3365
3366 rc = cil_level_to_mls_level(pdb, low, mls_level);
3367 if (rc != SEPOL_OK) {
3368 goto exit;
3369 }
3370
3371 mls_level = &mls_range->level[1];
3372
3373 rc = cil_level_to_mls_level(pdb, high, mls_level);
3374 if (rc != SEPOL_OK) {
3375 goto exit;
3376 }
3377
3378 return SEPOL_OK;
3379
3380 exit:
3381 return rc;
3382 }
3383
3384 static int cil_userlevel_userrange_to_policydb(policydb_t *pdb, struct cil_user *cil_user)
3385 {
3386 int rc = SEPOL_ERR;
3387 struct cil_level *cil_level = cil_user->dftlevel;
3388 struct cil_levelrange *cil_levelrange = cil_user->range;
3389 user_datum_t *sepol_user = NULL;
3390
3391 rc = __cil_get_sepol_user_datum(pdb, DATUM(cil_user), &sepol_user);
3392 if (rc != SEPOL_OK) goto exit;
3393
3394 rc = cil_level_to_mls_level(pdb, cil_level, &sepol_user->exp_dfltlevel);
3395 if (rc != SEPOL_OK) {
3396 goto exit;
3397 }
3398
3399 rc = __cil_levelrange_to_mls_range(pdb, cil_levelrange, &sepol_user->exp_range);
3400 if (rc != SEPOL_OK) {
3401 goto exit;
3402 }
3403
3404 return SEPOL_OK;
3405
3406 exit:
3407 return rc;
3408 }
3409
3410 static int __cil_context_to_sepol_context(policydb_t *pdb, struct cil_context *cil_context, context_struct_t *sepol_context)
3411 {
3412 int rc = SEPOL_ERR;
3413 struct cil_levelrange *cil_lvlrange = cil_context->range;
3414 user_datum_t *sepol_user = NULL;
3415 role_datum_t *sepol_role = NULL;
3416 type_datum_t *sepol_type = NULL;
3417
3418 rc = __cil_get_sepol_user_datum(pdb, DATUM(cil_context->user), &sepol_user);
3419 if (rc != SEPOL_OK) goto exit;
3420
3421 rc = __cil_get_sepol_role_datum(pdb, DATUM(cil_context->role), &sepol_role);
3422 if (rc != SEPOL_OK) goto exit;
3423
3424 rc = __cil_get_sepol_type_datum(pdb, DATUM(cil_context->type), &sepol_type);
3425 if (rc != SEPOL_OK) goto exit;
3426
3427 sepol_context->user = sepol_user->s.value;
3428 sepol_context->role = sepol_role->s.value;
3429 sepol_context->type = sepol_type->s.value;
3430
3431 if (pdb->mls == CIL_TRUE) {
3432 mls_context_init(sepol_context);
3433
3434 rc = __cil_levelrange_to_mls_range(pdb, cil_lvlrange, &sepol_context->range);
3435 if (rc != SEPOL_OK) {
3436 cil_log(CIL_ERR,"Problem with MLS\n");
3437 mls_context_destroy(sepol_context);
3438 goto exit;
3439 }
3440 }
3441
3442 return SEPOL_OK;
3443
3444 exit:
3445 return rc;
3446 }
3447
3448 static int cil_sidorder_to_policydb(policydb_t *pdb, const struct cil_db *db)
3449 {
3450 int rc = SEPOL_ERR;
3451 struct cil_list_item *curr;
3452 unsigned count = 0;
3453 ocontext_t *tail = NULL;
3454
3455 if (db->sidorder == NULL || db->sidorder->head == NULL) {
3456 cil_log(CIL_WARN, "No sidorder statement in policy\n");
3457 return SEPOL_OK;
3458 }
3459
3460 cil_list_for_each(curr, db->sidorder) {
3461 struct cil_sid *cil_sid = (struct cil_sid*)curr->data;
3462 struct cil_context *cil_context = cil_sid->context;
3463
3464 /* even if no context, we must preserve initial SID values */
3465 count++;
3466
3467 if (cil_context != NULL) {
3468 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_ISID], &tail);
3469 new_ocon->sid[0] = count;
3470 new_ocon->u.name = cil_strdup(cil_sid->datum.fqn);
3471 rc = __cil_context_to_sepol_context(pdb, cil_context, &new_ocon->context[0]);
3472 if (rc != SEPOL_OK) {
3473 cil_log(CIL_ERR,"Problem with context for SID %s\n",cil_sid->datum.fqn);
3474 goto exit;
3475 }
3476 }
3477 }
3478
3479 return SEPOL_OK;
3480
3481 exit:
3482 return rc;
3483 }
3484
3485 int cil_rangetransition_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_rangetransition *rangetrans)
3486 {
3487 int rc = SEPOL_ERR;
3488 type_datum_t *sepol_src = NULL;
3489 type_datum_t *sepol_tgt = NULL;
3490 class_datum_t *sepol_class = NULL;
3491 struct cil_list *class_list = NULL;
3492 range_trans_t *newkey = NULL;
3493 struct mls_range *newdatum = NULL;
3494 ebitmap_t src_bitmap, tgt_bitmap;
3495 ebitmap_node_t *node1, *node2;
3496 unsigned int i, j;
3497 struct cil_list_item *c;
3498 struct mls_range *o_range = NULL;
3499
3500 rc = __cil_expand_type(rangetrans->src, &src_bitmap);
3501 if (rc != SEPOL_OK) goto exit;
3502
3503 rc = __cil_expand_type(rangetrans->exec, &tgt_bitmap);
3504 if (rc != SEPOL_OK) goto exit;
3505
3506 class_list = cil_expand_class(rangetrans->obj);
3507
3508 ebitmap_for_each_positive_bit(&src_bitmap, node1, i) {
3509 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[i]), &sepol_src);
3510 if (rc != SEPOL_OK) goto exit;
3511
3512 ebitmap_for_each_positive_bit(&tgt_bitmap, node2, j) {
3513 rc = __cil_get_sepol_type_datum(pdb, DATUM(db->val_to_type[j]), &sepol_tgt);
3514 if (rc != SEPOL_OK) goto exit;
3515
3516 cil_list_for_each(c, class_list) {
3517 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_class);
3518 if (rc != SEPOL_OK) goto exit;
3519
3520 newkey = cil_calloc(1, sizeof(*newkey));
3521 newdatum = cil_calloc(1, sizeof(*newdatum));
3522 newkey->source_type = sepol_src->s.value;
3523 newkey->target_type = sepol_tgt->s.value;
3524 newkey->target_class = sepol_class->s.value;
3525 rc = __cil_levelrange_to_mls_range(pdb, rangetrans->range, newdatum);
3526 if (rc != SEPOL_OK) {
3527 free(newkey);
3528 free(newdatum);
3529 goto exit;
3530 }
3531
3532 rc = hashtab_insert(pdb->range_tr, (hashtab_key_t)newkey, newdatum);
3533 if (rc != SEPOL_OK) {
3534 if (rc == SEPOL_EEXIST) {
3535 o_range = hashtab_search(pdb->range_tr, (hashtab_key_t)newkey);
3536 if (!mls_range_eq(newdatum, o_range)) {
3537 cil_log(CIL_ERR, "Conflicting Range transition rules\n");
3538 } else {
3539 rc = SEPOL_OK;
3540 }
3541 } else {
3542 cil_log(CIL_ERR, "Out of memory\n");
3543 }
3544 // TODO: add upper version bound once fixed in upstream GCC
3545 #if defined(__GNUC__) && (__GNUC__ >= 12)
3546 # pragma GCC diagnostic push
3547 # pragma GCC diagnostic ignored "-Warray-bounds"
3548 # pragma GCC diagnostic ignored "-Wstringop-overflow"
3549 #endif
3550 mls_range_destroy(newdatum);
3551 #if defined(__GNUC__) && (__GNUC__ >= 12)
3552 # pragma GCC diagnostic pop
3553 #endif
3554 free(newdatum);
3555 free(newkey);
3556 if (rc != SEPOL_OK) {
3557 goto exit;
3558 }
3559 }
3560 }
3561 }
3562 }
3563
3564 rc = SEPOL_OK;
3565
3566 exit:
3567 ebitmap_destroy(&src_bitmap);
3568 ebitmap_destroy(&tgt_bitmap);
3569 cil_list_destroy(&class_list, CIL_FALSE);
3570 return rc;
3571 }
3572
3573 int cil_ibpkeycon_to_policydb(policydb_t *pdb, struct cil_sort *ibpkeycons)
3574 {
3575 int rc = SEPOL_ERR;
3576 uint32_t i = 0;
3577 ocontext_t *tail = NULL;
3578 struct in6_addr subnet_prefix;
3579
3580 for (i = 0; i < ibpkeycons->count; i++) {
3581 struct cil_ibpkeycon *cil_ibpkeycon = ibpkeycons->array[i];
3582 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_IBPKEY], &tail);
3583
3584 rc = inet_pton(AF_INET6, cil_ibpkeycon->subnet_prefix_str, &subnet_prefix);
3585 if (rc != 1) {
3586 cil_log(CIL_ERR, "ibpkeycon subnet prefix not in valid IPV6 format\n");
3587 rc = SEPOL_ERR;
3588 goto exit;
3589 }
3590
3591 memcpy(&new_ocon->u.ibpkey.subnet_prefix, &subnet_prefix.s6_addr[0],
3592 sizeof(new_ocon->u.ibpkey.subnet_prefix));
3593 new_ocon->u.ibpkey.low_pkey = cil_ibpkeycon->pkey_low;
3594 new_ocon->u.ibpkey.high_pkey = cil_ibpkeycon->pkey_high;
3595
3596 rc = __cil_context_to_sepol_context(pdb, cil_ibpkeycon->context, &new_ocon->context[0]);
3597 if (rc != SEPOL_OK)
3598 goto exit;
3599 }
3600
3601 return SEPOL_OK;
3602
3603 exit:
3604 return rc;
3605 }
3606
3607 int cil_portcon_to_policydb(policydb_t *pdb, struct cil_sort *portcons)
3608 {
3609 int rc = SEPOL_ERR;
3610 uint32_t i = 0;
3611 ocontext_t *tail = NULL;
3612
3613 for (i = 0; i < portcons->count; i++) {
3614 struct cil_portcon *cil_portcon = portcons->array[i];
3615 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_PORT], &tail);
3616
3617 switch (cil_portcon->proto) {
3618 case CIL_PROTOCOL_UDP:
3619 new_ocon->u.port.protocol = IPPROTO_UDP;
3620 break;
3621 case CIL_PROTOCOL_TCP:
3622 new_ocon->u.port.protocol = IPPROTO_TCP;
3623 break;
3624 case CIL_PROTOCOL_DCCP:
3625 new_ocon->u.port.protocol = IPPROTO_DCCP;
3626 break;
3627 case CIL_PROTOCOL_SCTP:
3628 new_ocon->u.port.protocol = IPPROTO_SCTP;
3629 break;
3630 default:
3631 /* should not get here */
3632 rc = SEPOL_ERR;
3633 goto exit;
3634 }
3635
3636 new_ocon->u.port.low_port = cil_portcon->port_low;
3637 new_ocon->u.port.high_port = cil_portcon->port_high;
3638
3639 rc = __cil_context_to_sepol_context(pdb, cil_portcon->context, &new_ocon->context[0]);
3640 if (rc != SEPOL_OK) {
3641 goto exit;
3642 }
3643 }
3644
3645 return SEPOL_OK;
3646
3647 exit:
3648 return rc;
3649 }
3650
3651 int cil_netifcon_to_policydb(policydb_t *pdb, struct cil_sort *netifcons)
3652 {
3653 int rc = SEPOL_ERR;
3654 uint32_t i = 0;
3655 ocontext_t *tail = NULL;
3656
3657 for (i = 0; i < netifcons->count; i++) {
3658 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_NETIF], &tail);
3659 struct cil_netifcon *cil_netifcon = netifcons->array[i];
3660
3661 new_ocon->u.name = cil_strdup(cil_netifcon->interface_str);
3662
3663 rc = __cil_context_to_sepol_context(pdb, cil_netifcon->if_context, &new_ocon->context[0]);
3664 if (rc != SEPOL_OK) {
3665 goto exit;
3666 }
3667
3668 rc = __cil_context_to_sepol_context(pdb, cil_netifcon->packet_context, &new_ocon->context[1]);
3669 if (rc != SEPOL_OK) {
3670 context_destroy(&new_ocon->context[0]);
3671 goto exit;
3672 }
3673 }
3674
3675 return SEPOL_OK;
3676
3677 exit:
3678 return rc;
3679 }
3680
3681 int cil_ibendportcon_to_policydb(policydb_t *pdb, struct cil_sort *ibendportcons)
3682 {
3683 int rc = SEPOL_ERR;
3684 uint32_t i;
3685 ocontext_t *tail = NULL;
3686
3687 for (i = 0; i < ibendportcons->count; i++) {
3688 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_IBENDPORT], &tail);
3689 struct cil_ibendportcon *cil_ibendportcon = ibendportcons->array[i];
3690
3691 new_ocon->u.ibendport.dev_name = cil_strdup(cil_ibendportcon->dev_name_str);
3692 new_ocon->u.ibendport.port = cil_ibendportcon->port;
3693
3694 rc = __cil_context_to_sepol_context(pdb, cil_ibendportcon->context, &new_ocon->context[0]);
3695 if (rc != SEPOL_OK)
3696 goto exit;
3697 }
3698
3699 return SEPOL_OK;
3700
3701 exit:
3702 return rc;
3703 }
3704
3705 int cil_nodecon_to_policydb(policydb_t *pdb, struct cil_sort *nodecons)
3706 {
3707 int rc = SEPOL_ERR;
3708 uint32_t i = 0;
3709 ocontext_t *tail = NULL;
3710 ocontext_t *tail6 = NULL;
3711
3712 for (i = 0; i < nodecons->count; i++) {
3713 ocontext_t *new_ocon = NULL;
3714 struct cil_nodecon *cil_nodecon = nodecons->array[i];
3715
3716 if (cil_nodecon->addr->family == AF_INET) {
3717 new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_NODE], &tail);
3718 new_ocon->u.node.addr = cil_nodecon->addr->ip.v4.s_addr;
3719 new_ocon->u.node.mask = cil_nodecon->mask->ip.v4.s_addr;
3720 } else if (cil_nodecon->addr->family == AF_INET6) {
3721 new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_NODE6], &tail6);
3722 memcpy(new_ocon->u.node6.addr, &cil_nodecon->addr->ip.v6.s6_addr[0], 16);
3723 memcpy(new_ocon->u.node6.mask, &cil_nodecon->mask->ip.v6.s6_addr[0], 16);
3724 } else {
3725 /* should not get here */
3726 rc = SEPOL_ERR;
3727 goto exit;
3728 }
3729
3730 rc = __cil_context_to_sepol_context(pdb, cil_nodecon->context, &new_ocon->context[0]);
3731 if (rc != SEPOL_OK) {
3732 goto exit;
3733 }
3734 }
3735
3736 return SEPOL_OK;
3737
3738 exit:
3739 return rc;
3740 }
3741
3742 int cil_fsuse_to_policydb(policydb_t *pdb, struct cil_sort *fsuses)
3743 {
3744 int rc = SEPOL_ERR;
3745 uint32_t i = 0;
3746 ocontext_t *tail = NULL;
3747
3748 for (i = 0; i < fsuses->count; i++) {
3749 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_FSUSE], &tail);
3750 struct cil_fsuse *cil_fsuse = fsuses->array[i];
3751
3752 new_ocon->u.name = cil_strdup(cil_fsuse->fs_str);
3753 new_ocon->v.behavior = cil_fsuse->type;
3754
3755 rc = __cil_context_to_sepol_context(pdb, cil_fsuse->context, &new_ocon->context[0]);
3756 if (rc != SEPOL_OK) {
3757 goto exit;
3758 }
3759 }
3760
3761 return SEPOL_OK;
3762
3763 exit:
3764 return rc;
3765 }
3766
3767 int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)
3768 {
3769 int rc = SEPOL_ERR;
3770 uint32_t i = 0;
3771 genfs_t *genfs_tail = NULL;
3772 ocontext_t *ocon_tail = NULL;
3773 int wildcard = ebitmap_get_bit(&pdb->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
3774
3775 for (i = 0; i < genfscons->count; i++) {
3776 struct cil_genfscon *cil_genfscon = genfscons->array[i];
3777 ocontext_t *new_ocon = cil_malloc(sizeof(ocontext_t));
3778 memset(new_ocon, 0, sizeof(ocontext_t));
3779
3780 if (genfs_tail && strcmp(genfs_tail->fstype, cil_genfscon->fs_str) == 0) {
3781 ocon_tail->next = new_ocon;
3782 } else {
3783 genfs_t *new_genfs = cil_malloc(sizeof(genfs_t));
3784 memset(new_genfs, 0, sizeof(genfs_t));
3785 new_genfs->fstype = cil_strdup(cil_genfscon->fs_str);
3786 new_genfs->head = new_ocon;
3787
3788 if (genfs_tail) {
3789 genfs_tail->next = new_genfs;
3790 } else {
3791 pdb->genfs = new_genfs;
3792 }
3793 genfs_tail = new_genfs;
3794 }
3795
3796 ocon_tail = new_ocon;
3797
3798 if (wildcard) {
3799 size_t name_len = strlen(cil_genfscon->path_str);
3800 new_ocon->u.name = cil_malloc(name_len + 2);
3801 memcpy(new_ocon->u.name, cil_genfscon->path_str, name_len);
3802 new_ocon->u.name[name_len] = '*';
3803 new_ocon->u.name[name_len + 1] = '\0';
3804 } else {
3805 new_ocon->u.name = cil_strdup(cil_genfscon->path_str);
3806 }
3807
3808 if (cil_genfscon->file_type != CIL_FILECON_ANY) {
3809 class_datum_t *class_datum;
3810 const char *class_name;
3811 switch (cil_genfscon->file_type) {
3812 case CIL_FILECON_FILE:
3813 class_name = "file";
3814 break;
3815 case CIL_FILECON_DIR:
3816 class_name = "dir";
3817 break;
3818 case CIL_FILECON_CHAR:
3819 class_name = "chr_file";
3820 break;
3821 case CIL_FILECON_BLOCK:
3822 class_name = "blk_file";
3823 break;
3824 case CIL_FILECON_SOCKET:
3825 class_name = "sock_file";
3826 break;
3827 case CIL_FILECON_PIPE:
3828 class_name = "fifo_file";
3829 break;
3830 case CIL_FILECON_SYMLINK:
3831 class_name = "lnk_file";
3832 break;
3833 default:
3834 rc = SEPOL_ERR;
3835 goto exit;
3836 }
3837 class_datum = hashtab_search(pdb->p_classes.table, class_name);
3838 if (!class_datum) {
3839 rc = SEPOL_ERR;
3840 goto exit;
3841 }
3842 new_ocon->v.sclass = class_datum->s.value;
3843 }
3844
3845 rc = __cil_context_to_sepol_context(pdb, cil_genfscon->context, &new_ocon->context[0]);
3846 if (rc != SEPOL_OK) {
3847 goto exit;
3848 }
3849 }
3850
3851 return SEPOL_OK;
3852
3853 exit:
3854 return rc;
3855 }
3856
3857 int cil_pirqcon_to_policydb(policydb_t *pdb, struct cil_sort *pirqcons)
3858 {
3859 int rc = SEPOL_ERR;
3860 uint32_t i = 0;
3861 ocontext_t *tail = NULL;
3862
3863 for (i = 0; i < pirqcons->count; i++) {
3864 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_XEN_PIRQ], &tail);
3865 struct cil_pirqcon *cil_pirqcon = pirqcons->array[i];
3866
3867 new_ocon->u.pirq = cil_pirqcon->pirq;
3868
3869 rc = __cil_context_to_sepol_context(pdb, cil_pirqcon->context, &new_ocon->context[0]);
3870 if (rc != SEPOL_OK) {
3871 goto exit;
3872 }
3873 }
3874
3875 return SEPOL_OK;
3876
3877 exit:
3878 return rc;
3879 }
3880
3881 int cil_iomemcon_to_policydb(policydb_t *pdb, struct cil_sort *iomemcons)
3882 {
3883 int rc = SEPOL_ERR;
3884 uint32_t i = 0;
3885 ocontext_t *tail = NULL;
3886
3887 for (i = 0; i < iomemcons->count; i++) {
3888 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_XEN_IOMEM], &tail);
3889 struct cil_iomemcon *cil_iomemcon = iomemcons->array[i];
3890
3891 new_ocon->u.iomem.low_iomem = cil_iomemcon->iomem_low;
3892 new_ocon->u.iomem.high_iomem = cil_iomemcon->iomem_high;
3893
3894 rc = __cil_context_to_sepol_context(pdb, cil_iomemcon->context, &new_ocon->context[0]);
3895 if (rc != SEPOL_OK) {
3896 goto exit;
3897 }
3898 }
3899
3900 return SEPOL_OK;
3901
3902 exit:
3903 return rc;
3904 }
3905
3906 int cil_ioportcon_to_policydb(policydb_t *pdb, struct cil_sort *ioportcons)
3907 {
3908 int rc = SEPOL_ERR;
3909 uint32_t i = 0;
3910 ocontext_t *tail = NULL;
3911
3912 for (i = 0; i < ioportcons->count; i++) {
3913 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_XEN_IOPORT], &tail);
3914 struct cil_ioportcon *cil_ioportcon = ioportcons->array[i];
3915
3916 new_ocon->u.ioport.low_ioport = cil_ioportcon->ioport_low;
3917 new_ocon->u.ioport.high_ioport = cil_ioportcon->ioport_high;
3918
3919 rc = __cil_context_to_sepol_context(pdb, cil_ioportcon->context, &new_ocon->context[0]);
3920 if (rc != SEPOL_OK) {
3921 goto exit;
3922 }
3923 }
3924
3925 return SEPOL_OK;
3926
3927 exit:
3928 return rc;
3929 }
3930
3931 int cil_pcidevicecon_to_policydb(policydb_t *pdb, struct cil_sort *pcidevicecons)
3932 {
3933 int rc = SEPOL_ERR;
3934 uint32_t i = 0;
3935 ocontext_t *tail = NULL;
3936
3937 for (i = 0; i < pcidevicecons->count; i++) {
3938 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_XEN_PCIDEVICE], &tail);
3939 struct cil_pcidevicecon *cil_pcidevicecon = pcidevicecons->array[i];
3940
3941 new_ocon->u.device = cil_pcidevicecon->dev;
3942
3943 rc = __cil_context_to_sepol_context(pdb, cil_pcidevicecon->context, &new_ocon->context[0]);
3944 if (rc != SEPOL_OK) {
3945 goto exit;
3946 }
3947 }
3948
3949 return SEPOL_OK;
3950
3951 exit:
3952 return rc;
3953 }
3954
3955 static int cil_devicetreecon_to_policydb(policydb_t *pdb, struct cil_sort *devicetreecons)
3956 {
3957 int rc = SEPOL_ERR;
3958 uint32_t i = 0;
3959 ocontext_t *tail = NULL;
3960
3961 for (i = 0; i < devicetreecons->count; i++) {
3962 ocontext_t *new_ocon = cil_add_ocontext(&pdb->ocontexts[OCON_XEN_DEVICETREE], &tail);
3963 struct cil_devicetreecon *cil_devicetreecon = devicetreecons->array[i];
3964
3965 new_ocon->u.name = cil_strdup(cil_devicetreecon->path);
3966
3967 rc = __cil_context_to_sepol_context(pdb, cil_devicetreecon->context, &new_ocon->context[0]);
3968 if (rc != SEPOL_OK) {
3969 goto exit;
3970 }
3971 }
3972
3973 return SEPOL_OK;
3974
3975 exit:
3976 return rc;
3977 }
3978
3979 static int cil_default_to_policydb(policydb_t *pdb, struct cil_default *def)
3980 {
3981 struct cil_list_item *curr;
3982 class_datum_t *sepol_class;
3983 struct cil_list *class_list = NULL;
3984
3985 cil_list_for_each(curr, def->class_datums) {
3986 struct cil_list_item *c;
3987
3988 class_list = cil_expand_class(curr->data);
3989
3990 cil_list_for_each(c, class_list) {
3991 int rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_class);
3992 if (rc != SEPOL_OK) goto exit;
3993
3994 switch (def->flavor) {
3995 case CIL_DEFAULTUSER:
3996 if (!sepol_class->default_user) {
3997 sepol_class->default_user = def->object;
3998 } else if (sepol_class->default_user != (char)def->object) {
3999 cil_log(CIL_ERR,"User default labeling for class %s already specified\n",DATUM(c->data)->fqn);
4000 goto exit;
4001 }
4002 break;
4003 case CIL_DEFAULTROLE:
4004 if (!sepol_class->default_role) {
4005 sepol_class->default_role = def->object;
4006 } else if (sepol_class->default_role != (char)def->object) {
4007 cil_log(CIL_ERR,"Role default labeling for class %s already specified\n",DATUM(c->data)->fqn);
4008 goto exit;
4009 }
4010 break;
4011 case CIL_DEFAULTTYPE:
4012 if (!sepol_class->default_type) {
4013 sepol_class->default_type = def->object;
4014 } else if (sepol_class->default_type != (char)def->object) {
4015 cil_log(CIL_ERR,"Type default labeling for class %s already specified\n",DATUM(c->data)->fqn);
4016 goto exit;
4017 }
4018 break;
4019 default:
4020 goto exit;
4021 }
4022 }
4023
4024 cil_list_destroy(&class_list, CIL_FALSE);
4025 }
4026
4027 return SEPOL_OK;
4028
4029 exit:
4030 cil_list_destroy(&class_list, CIL_FALSE);
4031 return SEPOL_ERR;
4032 }
4033
4034 static int cil_defaultrange_to_policydb(policydb_t *pdb, struct cil_defaultrange *def)
4035 {
4036 struct cil_list_item *curr;
4037 class_datum_t *sepol_class;
4038 struct cil_list *class_list = NULL;
4039
4040 cil_list_for_each(curr, def->class_datums) {
4041 struct cil_list_item *c;
4042
4043 class_list = cil_expand_class(curr->data);
4044
4045 cil_list_for_each(c, class_list) {
4046 int rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_class);
4047 if (rc != SEPOL_OK) goto exit;
4048
4049 if (!sepol_class->default_range) {
4050 sepol_class->default_range = def->object_range;
4051 } else if (sepol_class->default_range != (char)def->object_range) {
4052 cil_log(CIL_ERR,"Range default labeling for class %s already specified\n", DATUM(curr->data)->fqn);
4053 goto exit;
4054 }
4055 }
4056
4057 cil_list_destroy(&class_list, CIL_FALSE);
4058 }
4059
4060 return SEPOL_OK;
4061
4062 exit:
4063 cil_list_destroy(&class_list, CIL_FALSE);
4064 return SEPOL_ERR;
4065 }
4066
4067 static int __cil_node_to_policydb(struct cil_tree_node *node, void *extra_args)
4068 {
4069 int rc = SEPOL_OK;
4070 int pass;
4071 struct cil_args_binary *args = extra_args;
4072 const struct cil_db *db;
4073 policydb_t *pdb;
4074 hashtab_t role_trans_table;
4075 void **type_value_to_cil;
4076
4077 db = args->db;
4078 pdb = args->pdb;
4079 pass = args->pass;
4080 role_trans_table = args->role_trans_table;
4081 type_value_to_cil = args->type_value_to_cil;
4082
4083 if (node->flavor >= CIL_MIN_DECLARATIVE) {
4084 if (node != NODE(node->data)) {
4085 goto exit;
4086 }
4087 }
4088
4089 switch (pass) {
4090 case 1:
4091 switch (node->flavor) {
4092 case CIL_ROLE:
4093 rc = cil_role_to_policydb(pdb, node->data);
4094 break;
4095 case CIL_TYPE:
4096 rc = cil_type_to_policydb(pdb, node->data, type_value_to_cil);
4097 break;
4098 case CIL_TYPEATTRIBUTE:
4099 rc = cil_typeattribute_to_policydb(pdb, node->data, type_value_to_cil);
4100 break;
4101 case CIL_POLICYCAP:
4102 rc = cil_policycap_to_policydb(pdb, node->data);
4103 break;
4104 case CIL_USER:
4105 rc = cil_user_to_policydb(pdb, node->data);
4106 break;
4107 case CIL_BOOL:
4108 rc = cil_bool_to_policydb(pdb, node->data);
4109 break;
4110 case CIL_CATALIAS:
4111 if (pdb->mls == CIL_TRUE) {
4112 rc = cil_catalias_to_policydb(pdb, node->data);
4113 }
4114 break;
4115 case CIL_SENS:
4116 if (pdb->mls == CIL_TRUE) {
4117 rc = cil_sepol_level_define(pdb, node->data);
4118 }
4119 break;
4120 default:
4121 break;
4122 }
4123 break;
4124 case 2:
4125 switch (node->flavor) {
4126 case CIL_TYPE:
4127 rc = cil_type_bounds_to_policydb(pdb, node->data);
4128 break;
4129 case CIL_TYPEALIAS:
4130 rc = cil_typealias_to_policydb(pdb, node->data);
4131 break;
4132 case CIL_TYPEPERMISSIVE:
4133 rc = cil_typepermissive_to_policydb(pdb, node->data);
4134 break;
4135 case CIL_TYPENEVERAUDIT:
4136 rc = cil_typeneveraudit_to_policydb(pdb, node->data);
4137 break;
4138 case CIL_TYPEATTRIBUTE:
4139 rc = cil_typeattribute_to_bitmap(pdb, db, node->data);
4140 break;
4141 case CIL_SENSALIAS:
4142 if (pdb->mls == CIL_TRUE) {
4143 rc = cil_sensalias_to_policydb(pdb, node->data);
4144 }
4145 break;
4146 case CIL_ROLE:
4147 rc = cil_role_bounds_to_policydb(pdb, node->data);
4148 if (rc != SEPOL_OK) goto exit;
4149 rc = cil_roletype_to_policydb(pdb, db, node->data);
4150 break;
4151 case CIL_USER:
4152 rc = cil_user_bounds_to_policydb(pdb, node->data);
4153 if (rc != SEPOL_OK) goto exit;
4154 if (pdb->mls == CIL_TRUE) {
4155 rc = cil_userlevel_userrange_to_policydb(pdb, node->data);
4156 if (rc != SEPOL_OK) {
4157 goto exit;
4158 }
4159 }
4160 rc = cil_userrole_to_policydb(pdb, db, node->data);
4161 break;
4162 case CIL_TYPE_RULE:
4163 rc = cil_type_rule_to_policydb(pdb, db, node->data);
4164 break;
4165 case CIL_AVRULE:
4166 case CIL_AVRULEX: {
4167 struct cil_avrule *rule = node->data;
4168 if (db->disable_neverallow != CIL_TRUE && rule->rule_kind == CIL_AVRULE_NEVERALLOW) {
4169 struct cil_list *neverallows = args->neverallows;
4170 cil_list_prepend(neverallows, CIL_LIST_ITEM, node);
4171 }
4172 break;
4173 }
4174 case CIL_ROLETRANSITION:
4175 rc = cil_roletrans_to_policydb(pdb, db, node->data, role_trans_table);
4176 break;
4177 case CIL_ROLEATTRIBUTESET:
4178 /*rc = cil_roleattributeset_to_policydb(pdb, node->data);*/
4179 break;
4180 case CIL_NAMETYPETRANSITION:
4181 rc = cil_typetransition_to_policydb(pdb, db, node->data);
4182 break;
4183 case CIL_CONSTRAIN:
4184 rc = cil_constrain_to_policydb(pdb, db, node->data);
4185 break;
4186 case CIL_MLSCONSTRAIN:
4187 if (pdb->mls == CIL_TRUE) {
4188 rc = cil_constrain_to_policydb(pdb, db, node->data);
4189 }
4190 break;
4191 case CIL_VALIDATETRANS:
4192 rc = cil_validatetrans_to_policydb(pdb, db, node->data);
4193 break;
4194 case CIL_MLSVALIDATETRANS:
4195 if (pdb->mls == CIL_TRUE) {
4196 rc = cil_validatetrans_to_policydb(pdb, db, node->data);
4197 }
4198 break;
4199 case CIL_RANGETRANSITION:
4200 if (pdb->mls == CIL_TRUE) {
4201 rc = cil_rangetransition_to_policydb(pdb, db, node->data);
4202 }
4203 break;
4204 case CIL_DEFAULTUSER:
4205 case CIL_DEFAULTROLE:
4206 case CIL_DEFAULTTYPE:
4207 rc = cil_default_to_policydb(pdb, node->data);
4208 break;
4209 case CIL_DEFAULTRANGE:
4210 rc = cil_defaultrange_to_policydb(pdb, node->data);
4211 break;
4212 default:
4213 break;
4214 }
4215 break;
4216 case 3:
4217 switch (node->flavor) {
4218 case CIL_BOOLEANIF:
4219 rc = cil_booleanif_to_policydb(pdb, db, node);
4220 break;
4221 case CIL_AVRULE: {
4222 struct cil_avrule *rule = node->data;
4223 if (rule->rule_kind != CIL_AVRULE_NEVERALLOW) {
4224 rc = cil_avrule_to_policydb(pdb, db, node->data);
4225 }
4226 }
4227 break;
4228 case CIL_AVRULEX: {
4229 struct cil_avrule *rule = node->data;
4230 if (rule->rule_kind != CIL_AVRULE_NEVERALLOW) {
4231 rc = cil_avrulex_to_hashtable(pdb, db, node->data, &args->avrulex_xperm_tables);
4232 }
4233 }
4234 break;
4235 case CIL_ROLEALLOW:
4236 rc = cil_roleallow_to_policydb(pdb, db, node->data);
4237 break;
4238 default:
4239 break;
4240 }
4241 default:
4242 break;
4243 }
4244
4245 exit:
4246 if (rc != SEPOL_OK) {
4247 cil_tree_log(node, CIL_ERR, "Binary policy creation failed");
4248 }
4249 return rc;
4250 }
4251
4252 static int __cil_binary_create_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
4253 {
4254 int rc = SEPOL_ERR;
4255
4256 if (node->flavor == CIL_BLOCK) {
4257 struct cil_block *blk = node->data;
4258 if (blk->is_abstract == CIL_TRUE) {
4259 *finished = CIL_TREE_SKIP_HEAD;
4260 rc = SEPOL_OK;
4261 goto exit;
4262 }
4263 } else if (node->flavor == CIL_MACRO) {
4264 *finished = CIL_TREE_SKIP_HEAD;
4265 rc = SEPOL_OK;
4266 goto exit;
4267 } else if (node->flavor == CIL_BOOLEANIF) {
4268 *finished = CIL_TREE_SKIP_HEAD;
4269 }
4270
4271 rc = __cil_node_to_policydb(node, extra_args);
4272 if (rc != SEPOL_OK) {
4273 goto exit;
4274 }
4275
4276 exit:
4277 return rc;
4278 }
4279
4280 static int __cil_contexts_to_policydb(policydb_t *pdb, const struct cil_db *db)
4281 {
4282 int rc = SEPOL_ERR;
4283
4284 rc = cil_portcon_to_policydb(pdb, db->portcon);
4285 if (rc != SEPOL_OK) {
4286 goto exit;
4287 }
4288
4289 rc = cil_netifcon_to_policydb(pdb, db->netifcon);
4290 if (rc != SEPOL_OK) {
4291 goto exit;
4292 }
4293
4294 rc = cil_nodecon_to_policydb(pdb, db->nodecon);
4295 if (rc != SEPOL_OK) {
4296 goto exit;
4297 }
4298
4299 rc = cil_fsuse_to_policydb(pdb, db->fsuse);
4300 if (rc != SEPOL_OK) {
4301 goto exit;
4302 }
4303
4304 rc = cil_genfscon_to_policydb(pdb, db->genfscon);
4305 if (rc != SEPOL_OK) {
4306 goto exit;
4307 }
4308
4309 rc = cil_ibpkeycon_to_policydb(pdb, db->ibpkeycon);
4310 if (rc != SEPOL_OK) {
4311 goto exit;
4312 }
4313
4314 rc = cil_ibendportcon_to_policydb(pdb, db->ibendportcon);
4315 if (rc != SEPOL_OK) {
4316 goto exit;
4317 }
4318
4319 if (db->target_platform == SEPOL_TARGET_XEN) {
4320 rc = cil_pirqcon_to_policydb(pdb, db->pirqcon);
4321 if (rc != SEPOL_OK) {
4322 goto exit;
4323 }
4324
4325 rc = cil_iomemcon_to_policydb(pdb, db->iomemcon);
4326 if (rc != SEPOL_OK) {
4327 goto exit;
4328 }
4329
4330 rc = cil_ioportcon_to_policydb(pdb, db->ioportcon);
4331 if (rc != SEPOL_OK) {
4332 goto exit;
4333 }
4334
4335 rc = cil_pcidevicecon_to_policydb(pdb, db->pcidevicecon);
4336 if (rc != SEPOL_OK) {
4337 goto exit;
4338 }
4339
4340 rc = cil_devicetreecon_to_policydb(pdb, db->devicetreecon);
4341 if (rc != SEPOL_OK) {
4342 goto exit;
4343 }
4344 }
4345 return SEPOL_OK;
4346 exit:
4347 return rc;
4348 }
4349
4350 static int __cil_common_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4351 {
4352 policydb_t *pdb = data;
4353 common_datum_t *common = (common_datum_t *)datum;
4354
4355 if (common->s.value < 1 || common->s.value > pdb->p_commons.nprim) {
4356 return -EINVAL;
4357 }
4358 pdb->p_common_val_to_name[common->s.value - 1] = (char *)key;
4359
4360 return 0;
4361 }
4362
4363 static int __cil_class_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4364 {
4365 policydb_t *pdb = data;
4366 class_datum_t *class = (class_datum_t *)datum;
4367
4368 if (class->s.value < 1 || class->s.value > pdb->p_classes.nprim) {
4369 return -EINVAL;
4370 }
4371 pdb->p_class_val_to_name[class->s.value - 1] = (char *)key;
4372 pdb->class_val_to_struct[class->s.value - 1] = class;
4373
4374 return 0;
4375 }
4376
4377 static int __cil_role_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4378 {
4379 policydb_t *pdb = data;
4380 role_datum_t *role = (role_datum_t *)datum;
4381
4382 if (role->s.value < 1 || role->s.value > pdb->p_roles.nprim) {
4383 return -EINVAL;
4384 }
4385 pdb->p_role_val_to_name[role->s.value - 1] = (char *)key;
4386 pdb->role_val_to_struct[role->s.value - 1] = role;
4387
4388 return 0;
4389 }
4390
4391 static int __cil_type_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4392 {
4393 policydb_t *pdb = data;
4394 type_datum_t *type = (type_datum_t *)datum;
4395
4396 if (type->s.value < 1 || type->s.value > pdb->p_types.nprim) {
4397 return -EINVAL;
4398 }
4399 pdb->p_type_val_to_name[type->s.value - 1] = (char *)key;
4400 pdb->type_val_to_struct[type->s.value - 1] = type;
4401
4402 return 0;
4403 }
4404
4405 static int __cil_user_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4406 {
4407 policydb_t *pdb = data;
4408 user_datum_t *user = (user_datum_t *)datum;
4409
4410 if (user->s.value < 1 || user->s.value > pdb->p_users.nprim) {
4411 return -EINVAL;
4412 }
4413 pdb->p_user_val_to_name[user->s.value - 1] = (char *)key;
4414 pdb->user_val_to_struct[user->s.value - 1] = user;
4415
4416 return 0;
4417 }
4418
4419 static int __cil_bool_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4420 {
4421 policydb_t *pdb = data;
4422 cond_bool_datum_t *boolean = (cond_bool_datum_t *)datum;
4423
4424 if (boolean->s.value < 1 || boolean->s.value > pdb->p_bools.nprim) {
4425 return -EINVAL;
4426 }
4427 pdb->p_bool_val_to_name[boolean->s.value - 1] = (char *)key;
4428 pdb->bool_val_to_struct[boolean->s.value - 1] = boolean;
4429
4430 return 0;
4431 }
4432
4433 static int __cil_level_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4434 {
4435 policydb_t *pdb = data;
4436 level_datum_t *level = (level_datum_t *)datum;
4437
4438 if (level->level->sens < 1 || level->level->sens > pdb->p_levels.nprim) {
4439 return -EINVAL;
4440 }
4441 pdb->p_sens_val_to_name[level->level->sens - 1] = (char *)key;
4442
4443 return 0;
4444 }
4445
4446 static int __cil_cat_val_array_insert(hashtab_key_t key, hashtab_datum_t datum, void *data)
4447 {
4448 policydb_t *pdb = data;
4449 cat_datum_t *cat = (cat_datum_t *)datum;
4450
4451 if (cat->s.value < 1 || cat->s.value > pdb->p_cats.nprim) {
4452 return -EINVAL;
4453 }
4454 pdb->p_cat_val_to_name[cat->s.value - 1] = (char *)key;
4455
4456 return 0;
4457 }
4458
4459 static int __cil_policydb_val_arrays_create(policydb_t *policydb)
4460 {
4461 int rc = SEPOL_ERR;
4462
4463 policydb->p_common_val_to_name = cil_malloc(sizeof(char *) * policydb->p_commons.nprim);
4464 rc = hashtab_map(policydb->p_commons.table, &__cil_common_val_array_insert, policydb);
4465 if (rc != SEPOL_OK) {
4466 goto exit;
4467 }
4468
4469 policydb->p_class_val_to_name = cil_malloc(sizeof(char *) * policydb->p_classes.nprim);
4470 policydb->class_val_to_struct = cil_malloc(sizeof(class_datum_t *) * policydb->p_classes.nprim);
4471 rc = hashtab_map(policydb->p_classes.table, &__cil_class_val_array_insert, policydb);
4472 if (rc != SEPOL_OK) {
4473 goto exit;
4474 }
4475
4476 policydb->p_role_val_to_name = cil_malloc(sizeof(char *) * policydb->p_roles.nprim);
4477 policydb->role_val_to_struct = cil_malloc(sizeof(role_datum_t *) * policydb->p_roles.nprim);
4478 rc = hashtab_map(policydb->p_roles.table, &__cil_role_val_array_insert, policydb);
4479 if (rc != SEPOL_OK) {
4480 goto exit;
4481 }
4482
4483 policydb->p_type_val_to_name = cil_malloc(sizeof(char *) * policydb->p_types.nprim);
4484 policydb->type_val_to_struct = cil_malloc(sizeof(type_datum_t *) * policydb->p_types.nprim);
4485 rc = hashtab_map(policydb->p_types.table, &__cil_type_val_array_insert, policydb);
4486 if (rc != SEPOL_OK) {
4487 goto exit;
4488 }
4489
4490 policydb->p_user_val_to_name = cil_malloc(sizeof(char *) * policydb->p_users.nprim);
4491 policydb->user_val_to_struct = cil_malloc(sizeof(user_datum_t *) * policydb->p_users.nprim);
4492 rc = hashtab_map(policydb->p_users.table, &__cil_user_val_array_insert, policydb);
4493 if (rc != SEPOL_OK) {
4494 goto exit;
4495 }
4496
4497 policydb->p_bool_val_to_name = cil_malloc(sizeof(char *) * policydb->p_bools.nprim);
4498 policydb->bool_val_to_struct = cil_malloc(sizeof(cond_bool_datum_t *) * policydb->p_bools.nprim);
4499 rc = hashtab_map(policydb->p_bools.table, &__cil_bool_val_array_insert, policydb);
4500 if (rc != SEPOL_OK) {
4501 goto exit;
4502 }
4503
4504 policydb->p_sens_val_to_name = cil_malloc(sizeof(char *) * policydb->p_levels.nprim);
4505 rc = hashtab_map(policydb->p_levels.table, &__cil_level_val_array_insert, policydb);
4506 if (rc != SEPOL_OK) {
4507 goto exit;
4508 }
4509
4510 policydb->p_cat_val_to_name = cil_malloc(sizeof(char *) * policydb->p_cats.nprim);
4511 rc = hashtab_map(policydb->p_cats.table, &__cil_cat_val_array_insert, policydb);
4512 if (rc != SEPOL_OK) {
4513 goto exit;
4514 }
4515
4516 exit:
4517 return rc;
4518 }
4519
4520 static void __cil_set_conditional_state_and_flags(policydb_t *pdb)
4521 {
4522 cond_node_t *cur;
4523
4524 for (cur = pdb->cond_list; cur != NULL; cur = cur->next) {
4525 int new_state;
4526 cond_av_list_t *c;
4527
4528 new_state = cond_evaluate_expr(pdb, cur->expr);
4529
4530 cur->cur_state = new_state;
4531
4532 if (new_state == -1) {
4533 cil_log(CIL_WARN, "Expression result was undefined - disabling all rules\n");
4534 }
4535
4536 for (c = cur->true_list; c != NULL; c = c->next) {
4537 if (new_state <= 0) {
4538 c->node->key.specified &= ~AVTAB_ENABLED;
4539 } else {
4540 c->node->key.specified |= AVTAB_ENABLED;
4541 }
4542 }
4543
4544 for (c = cur->false_list; c != NULL; c = c->next) {
4545 if (new_state) { /* -1 or 1 */
4546 c->node->key.specified &= ~AVTAB_ENABLED;
4547 } else {
4548 c->node->key.specified |= AVTAB_ENABLED;
4549 }
4550 }
4551 }
4552 }
4553
4554 static int __cil_policydb_create(const struct cil_db *db, struct sepol_policydb **spdb)
4555 {
4556 int rc;
4557 struct policydb *pdb = NULL;
4558
4559 rc = sepol_policydb_create(spdb);
4560 if (rc < 0) {
4561 cil_log(CIL_ERR, "Failed to create policy db\n");
4562 // spdb could be a dangling pointer at this point, so reset it so
4563 // callers of this function don't need to worry about freeing garbage
4564 *spdb = NULL;
4565 goto exit;
4566 }
4567
4568 pdb = &(*spdb)->p;
4569
4570 pdb->policy_type = POLICY_KERN;
4571 pdb->target_platform = db->target_platform;
4572 pdb->policyvers = db->policy_version;
4573 pdb->handle_unknown = db->handle_unknown;
4574 pdb->mls = db->mls;
4575
4576 return SEPOL_OK;
4577
4578 exit:
4579 return rc;
4580 }
4581
4582
4583 static int __cil_policydb_init(policydb_t *pdb, const struct cil_db *db, struct cil_class *class_value_to_cil[], struct cil_perm **perm_value_to_cil[])
4584 {
4585 int rc = SEPOL_ERR;
4586
4587 // these flags should get set in __cil_policydb_create. However, for
4588 // backwards compatibility, it is possible that __cil_policydb_create is
4589 // never called. So, they must also be set here.
4590 pdb->handle_unknown = db->handle_unknown;
4591 pdb->mls = db->mls;
4592
4593 rc = cil_classorder_to_policydb(pdb, db, class_value_to_cil, perm_value_to_cil);
4594 if (rc != SEPOL_OK) {
4595 goto exit;
4596 }
4597
4598 if (pdb->mls == CIL_TRUE) {
4599 rc = cil_catorder_to_policydb(pdb, db);
4600 if (rc != SEPOL_OK) {
4601 goto exit;
4602 }
4603
4604 rc = cil_sensitivityorder_to_policydb(pdb, db);
4605 if (rc != SEPOL_OK) {
4606 goto exit;
4607 }
4608 }
4609
4610 rc = avtab_alloc(&pdb->te_avtab, MAX_AVTAB_SIZE);
4611 if (rc != SEPOL_OK) {
4612 goto exit;
4613 }
4614
4615 rc = avtab_alloc(&pdb->te_cond_avtab, MAX_AVTAB_SIZE);
4616 if (rc != SEPOL_OK) {
4617 goto exit;
4618 }
4619
4620 return SEPOL_OK;
4621
4622 exit:
4623
4624 return rc;
4625 }
4626
4627 static unsigned int role_trans_hash(hashtab_t h, const_hashtab_key_t key)
4628 {
4629 const role_trans_t *k = (const role_trans_t *)key;
4630 return ((k->role + (k->type << 2) +
4631 (k->tclass << 5)) & (h->size - 1));
4632 }
4633
4634 static int role_trans_compare(hashtab_t h
4635 __attribute__ ((unused)), const_hashtab_key_t key1,
4636 const_hashtab_key_t key2)
4637 {
4638 const role_trans_t *a = (const role_trans_t *)key1;
4639 const role_trans_t *b = (const role_trans_t *)key2;
4640
4641 return a->role != b->role || a->type != b->type || a->tclass != b->tclass;
4642 }
4643
4644 /* Based on MurmurHash3, written by Austin Appleby and placed in the
4645 * public domain.
4646 */
4647 static unsigned int avrulex_hash(__attribute__((unused)) hashtab_t h, const_hashtab_key_t key)
4648 {
4649 const avtab_key_t *k = (const avtab_key_t *)key;
4650
4651 static const uint32_t c1 = 0xcc9e2d51;
4652 static const uint32_t c2 = 0x1b873593;
4653 static const uint32_t r1 = 15;
4654 static const uint32_t r2 = 13;
4655 static const uint32_t m = 5;
4656 static const uint32_t n = 0xe6546b64;
4657
4658 uint32_t hash = 0;
4659
4660 #define mix(input) do { \
4661 uint32_t v = input; \
4662 v *= c1; \
4663 v = (v << r1) | (v >> (32 - r1)); \
4664 v *= c2; \
4665 hash ^= v; \
4666 hash = (hash << r2) | (hash >> (32 - r2)); \
4667 hash = hash * m + n; \
4668 } while (0)
4669
4670 mix(k->target_class);
4671 mix(k->target_type);
4672 mix(k->source_type);
4673 mix(k->specified);
4674
4675 #undef mix
4676
4677 hash ^= hash >> 16;
4678 hash *= 0x85ebca6b;
4679 hash ^= hash >> 13;
4680 hash *= 0xc2b2ae35;
4681 hash ^= hash >> 16;
4682
4683 return hash & (AVRULEX_TABLE_SIZE - 1);
4684 }
4685
4686 static int avrulex_compare(hashtab_t h
4687 __attribute__ ((unused)), const_hashtab_key_t key1,
4688 const_hashtab_key_t key2)
4689 {
4690 const avtab_key_t *a = (const avtab_key_t *)key1;
4691 const avtab_key_t *b = (const avtab_key_t *)key2;
4692
4693 return a->source_type != b->source_type || a->target_type != b->target_type || a->target_class != b->target_class || a->specified != b->specified;
4694 }
4695
4696 int cil_binary_create(const struct cil_db *db, sepol_policydb_t **policydb)
4697 {
4698 int rc = SEPOL_ERR;
4699 struct sepol_policydb *pdb = NULL;
4700
4701 rc = __cil_policydb_create(db, &pdb);
4702 if (rc != SEPOL_OK) {
4703 goto exit;
4704 }
4705
4706 rc = cil_binary_create_allocated_pdb(db, pdb);
4707 if (rc != SEPOL_OK) {
4708 goto exit;
4709 }
4710
4711 *policydb = pdb;
4712
4713 return SEPOL_OK;
4714
4715 exit:
4716 sepol_policydb_free(pdb);
4717
4718 return rc;
4719 }
4720
4721 static void __cil_destroy_sepol_class_perms(class_perm_node_t *curr)
4722 {
4723 class_perm_node_t *next;
4724
4725 while (curr) {
4726 next = curr->next;
4727 free(curr);
4728 curr = next;
4729 }
4730 }
4731
4732 static int __cil_rule_to_sepol_class_perms(policydb_t *pdb, struct cil_list *classperms, class_perm_node_t **sepol_class_perms)
4733 {
4734 int rc = SEPOL_ERR;
4735 struct cil_list_item *i;
4736 cil_list_for_each(i, classperms) {
4737 if (i->flavor == CIL_CLASSPERMS) {
4738 struct cil_classperms *cp = i->data;
4739 if (FLAVOR(cp->class) == CIL_CLASS) {
4740 class_perm_node_t *cpn = NULL;
4741 class_datum_t *sepol_class = NULL;
4742 uint32_t data = 0;
4743
4744 rc = __cil_get_sepol_class_datum(pdb, DATUM(cp->class), &sepol_class);
4745 if (rc != SEPOL_OK) goto exit;
4746
4747 rc = __cil_perms_to_datum(cp->perms, sepol_class, &data);
4748 if (rc != SEPOL_OK) goto exit;
4749 if (data != 0) { /* Only add if there are permissions */
4750 cpn = cil_malloc(sizeof(class_perm_node_t));
4751 cpn->tclass = sepol_class->s.value;
4752 cpn->data = data;
4753 cpn->next = *sepol_class_perms;
4754 *sepol_class_perms = cpn;
4755 }
4756 } else { /* MAP */
4757 struct cil_list_item *j = NULL;
4758 cil_list_for_each(j, cp->perms) {
4759 struct cil_perm *cmp = j->data;
4760 rc = __cil_rule_to_sepol_class_perms(pdb, cmp->classperms, sepol_class_perms);
4761 if (rc != SEPOL_OK) {
4762 goto exit;
4763 }
4764 }
4765 }
4766 } else { /* SET */
4767 struct cil_classperms_set *cp_set = i->data;
4768 struct cil_classpermission *cp = cp_set->set;
4769 rc = __cil_rule_to_sepol_class_perms(pdb, cp->classperms, sepol_class_perms);
4770 if (rc != SEPOL_OK) {
4771 goto exit;
4772 }
4773 }
4774 }
4775 return SEPOL_OK;
4776
4777 exit:
4778 return rc;
4779 }
4780
4781 static int __cil_permx_to_sepol_class_perms(policydb_t *pdb, struct cil_permissionx *permx, class_perm_node_t **sepol_class_perms)
4782 {
4783 int rc = SEPOL_OK;
4784 struct cil_list *class_list = NULL;
4785 struct cil_list_item *c;
4786 class_datum_t *sepol_obj = NULL;
4787 class_perm_node_t *cpn;
4788 uint32_t data = 0;
4789 char *perm_str = NULL;
4790
4791 class_list = cil_expand_class(permx->obj);
4792
4793 cil_list_for_each(c, class_list) {
4794 rc = __cil_get_sepol_class_datum(pdb, DATUM(c->data), &sepol_obj);
4795 if (rc != SEPOL_OK) {
4796 goto exit;
4797 }
4798
4799 switch (permx->kind) {
4800 case CIL_PERMX_KIND_IOCTL:
4801 perm_str = CIL_KEY_IOCTL;
4802 break;
4803 case CIL_PERMX_KIND_NLMSG:
4804 perm_str = CIL_KEY_NLMSG;
4805 break;
4806 default:
4807 rc = SEPOL_ERR;
4808 goto exit;
4809 }
4810
4811 rc = __perm_str_to_datum(perm_str, sepol_obj, &data);
4812 if (rc != SEPOL_OK) {
4813 goto exit;
4814 }
4815
4816 cpn = cil_malloc(sizeof(*cpn));
4817 cpn->tclass = sepol_obj->s.value;
4818 cpn->data = data;
4819 cpn->next = *sepol_class_perms;
4820 *sepol_class_perms = cpn;
4821 }
4822
4823 exit:
4824 cil_list_destroy(&class_list, CIL_FALSE);
4825
4826 return rc;
4827 }
4828
4829 static void __cil_init_sepol_type_set(type_set_t *t)
4830 {
4831 ebitmap_init(&t->types);
4832 ebitmap_init(&t->negset);
4833 t->flags = 0;
4834 }
4835
4836 static int __cil_add_sepol_type(policydb_t *pdb, const struct cil_db *db, struct cil_symtab_datum *datum, ebitmap_t *map)
4837 {
4838 int rc = SEPOL_ERR;
4839 struct cil_tree_node *n = NODE(datum);
4840 type_datum_t *sepol_datum = NULL;
4841
4842 if (n->flavor == CIL_TYPEATTRIBUTE) {
4843 ebitmap_node_t *tnode;
4844 unsigned int i;
4845 struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
4846 ebitmap_for_each_positive_bit(attr->types, tnode, i) {
4847 datum = DATUM(db->val_to_type[i]);
4848 rc = __cil_get_sepol_type_datum(pdb, datum, &sepol_datum);
4849 if (rc != SEPOL_OK) goto exit;
4850 ebitmap_set_bit(map, sepol_datum->s.value - 1, 1);
4851 }
4852 } else {
4853 rc = __cil_get_sepol_type_datum(pdb, datum, &sepol_datum);
4854 if (rc != SEPOL_OK) goto exit;
4855 ebitmap_set_bit(map, sepol_datum->s.value - 1, 1);
4856 }
4857
4858 return SEPOL_OK;
4859
4860 exit:
4861 return rc;
4862 }
4863
4864 static avrule_t *__cil_init_sepol_avrule(uint32_t kind, struct cil_tree_node *node)
4865 {
4866 avrule_t *avrule;
4867 struct cil_tree_node *source_node;
4868 char *source_path;
4869 char *lm_kind;
4870 uint32_t hll_line;
4871
4872 avrule = cil_malloc(sizeof(avrule_t));
4873 avrule->specified = kind;
4874 avrule->flags = 0;
4875 __cil_init_sepol_type_set(&avrule->stypes);
4876 __cil_init_sepol_type_set(&avrule->ttypes);
4877 avrule->perms = NULL;
4878 avrule->line = node->line;
4879
4880 avrule->source_filename = NULL;
4881 avrule->source_line = node->line;
4882 source_node = cil_tree_get_next_path(node, &lm_kind, &hll_line, &source_path);
4883 if (source_node) {
4884 avrule->source_filename = source_path;
4885 if (lm_kind != CIL_KEY_SRC_CIL) {
4886 avrule->source_line = hll_line + node->hll_offset - source_node->hll_offset - 1;
4887 }
4888 }
4889
4890 avrule->next = NULL;
4891 return avrule;
4892 }
4893
4894 static void __cil_destroy_sepol_avrules(avrule_t *curr)
4895 {
4896 avrule_t *next;
4897
4898 while (curr) {
4899 next = curr->next;
4900 ebitmap_destroy(&curr->stypes.types);
4901 ebitmap_destroy(&curr->stypes.negset);
4902 ebitmap_destroy(&curr->ttypes.types);
4903 ebitmap_destroy(&curr->ttypes.negset);
4904 __cil_destroy_sepol_class_perms(curr->perms);
4905 free(curr);
4906 curr = next;
4907 }
4908 }
4909
4910 static void __cil_print_parents(const char *pad, struct cil_tree_node *n)
4911 {
4912 if (!n) return;
4913
4914 __cil_print_parents(pad, n->parent);
4915
4916 if (n->flavor != CIL_SRC_INFO) {
4917 cil_tree_log(n, CIL_ERR,"%s%s", pad, cil_node_to_string(n));
4918 }
4919 }
4920
4921 static void __cil_print_classperm(struct cil_list *cp_list)
4922 {
4923 struct cil_list_item *i1, *i2;
4924
4925 i1 = cp_list->head;
4926 if (i1->flavor == CIL_CLASSPERMS) {
4927 struct cil_classperms *cp = i1->data;
4928 cil_log(CIL_ERR,"(%s (", DATUM(cp->class)->fqn);
4929 cil_list_for_each(i2, cp->perms) {
4930 cil_log(CIL_ERR,"%s",DATUM(i2->data)->fqn);
4931 if (i2 != cp->perms->tail) {
4932 cil_log(CIL_ERR," ");
4933 } else {
4934 cil_log(CIL_ERR,"))");
4935 }
4936 }
4937 } else {
4938 struct cil_classperms_set *cp_set = i1->data;
4939 cil_log(CIL_ERR,"%s", DATUM(cp_set->set)->fqn);
4940 }
4941 }
4942
4943 static void __cil_print_permissionx(struct cil_permissionx *px)
4944 {
4945 const char *kind_str = NULL;
4946 char *expr_str;
4947
4948 kind_str = __cil_xperm_kind_to_str(px->kind);
4949
4950 __cil_expr_to_string(px->expr_str, CIL_PERMISSIONX, &expr_str);
4951
4952 cil_log(CIL_ERR, "%s %s (%s)", kind_str, DATUM(px->obj)->fqn, expr_str);
4953
4954 free(expr_str);
4955 }
4956
4957 static void __cil_print_rule(const char *pad, const char *kind, struct cil_avrule *avrule)
4958 {
4959 cil_log(CIL_ERR,"%s(%s ", pad, kind);
4960 cil_log(CIL_ERR,"%s %s ", DATUM(avrule->src)->fqn, DATUM(avrule->tgt)->fqn);
4961
4962 if (!avrule->is_extended) {
4963 __cil_print_classperm(avrule->perms.classperms);
4964 } else {
4965 cil_log(CIL_ERR, "(");
4966 __cil_print_permissionx(avrule->perms.x.permx);
4967 cil_log(CIL_ERR, ")");
4968 }
4969
4970 cil_log(CIL_ERR,")\n");
4971 }
4972
4973 static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tree_node *node)
4974 {
4975 int rc;
4976 struct cil_list_item *i2;
4977 struct cil_list *matching;
4978 struct cil_avrule *cil_rule = node->data;
4979 struct cil_avrule target;
4980 struct cil_tree_node *n2;
4981 struct cil_avrule *r2;
4982 char *neverallow_str;
4983 char *allow_str;
4984 enum cil_flavor avrule_flavor;
4985 int num_matching = 0;
4986 int count_matching = 0;
4987 enum cil_log_level log_level = cil_get_log_level();
4988
4989 target.rule_kind = CIL_AVRULE_ALLOWED;
4990 target.is_extended = cil_rule->is_extended;
4991 target.src = cil_rule->src;
4992 target.tgt = cil_rule->tgt;
4993 target.perms = cil_rule->perms;
4994
4995 if (!cil_rule->is_extended) {
4996 neverallow_str = CIL_KEY_NEVERALLOW;
4997 allow_str = CIL_KEY_ALLOW;
4998 avrule_flavor = CIL_AVRULE;
4999 } else {
5000 neverallow_str = CIL_KEY_NEVERALLOWX;
5001 allow_str = CIL_KEY_ALLOWX;
5002 avrule_flavor = CIL_AVRULEX;
5003 }
5004 cil_tree_log(node, CIL_ERR, "%s check failed", neverallow_str);
5005 __cil_print_rule(" ", neverallow_str, cil_rule);
5006 cil_list_init(&matching, CIL_NODE);
5007 rc = cil_find_matching_avrule_in_ast(db->ast->root, avrule_flavor, &target, matching, CIL_FALSE);
5008 if (rc) {
5009 cil_log(CIL_ERR, "Error occurred while checking %s rules\n", neverallow_str);
5010 cil_list_destroy(&matching, CIL_FALSE);
5011 goto exit;
5012 }
5013
5014 cil_list_for_each(i2, matching) {
5015 num_matching++;
5016 }
5017 cil_list_for_each(i2, matching) {
5018 n2 = i2->data;
5019 r2 = n2->data;
5020 __cil_print_parents(" ", n2);
5021 __cil_print_rule(" ", allow_str, r2);
5022 count_matching++;
5023 if (count_matching >= 4 && num_matching > 4 && log_level == CIL_ERR) {
5024 cil_log(CIL_ERR, " Only first 4 of %d matching rules shown (use \"-v\" to show all)\n", num_matching);
5025 break;
5026 }
5027 }
5028 cil_log(CIL_ERR,"\n");
5029 cil_list_destroy(&matching, CIL_FALSE);
5030
5031 exit:
5032 return rc;
5033 }
5034
5035 static int cil_check_neverallow(const struct cil_db *db, policydb_t *pdb, struct cil_tree_node *node, int *violation)
5036 {
5037 int rc = SEPOL_OK;
5038 struct cil_avrule *cil_rule = node->data;
5039 struct cil_symtab_datum *tgt = cil_rule->tgt;
5040 uint32_t kind;
5041 avrule_t *rule;
5042 struct cil_list *xperms = NULL;
5043 struct cil_list_item *item;
5044
5045 if (!cil_rule->is_extended) {
5046 kind = AVRULE_NEVERALLOW;
5047 } else {
5048 kind = AVRULE_XPERMS_NEVERALLOW;
5049 }
5050
5051 rule = __cil_init_sepol_avrule(kind, node);
5052 rule->next = NULL;
5053
5054 rc = __cil_add_sepol_type(pdb, db, cil_rule->src, &rule->stypes.types);
5055 if (rc != SEPOL_OK) {
5056 goto exit;
5057 }
5058
5059 if (tgt->fqn == CIL_KEY_SELF) {
5060 rule->flags = RULE_SELF;
5061 } else if (tgt->fqn == CIL_KEY_NOTSELF) {
5062 rule->flags = RULE_NOTSELF;
5063 } else if (tgt->fqn == CIL_KEY_OTHER) {
5064 rule->flags = RULE_NOTSELF;
5065 rc = __cil_add_sepol_type(pdb, db, cil_rule->src, &rule->ttypes.types);
5066 if (rc != SEPOL_OK) {
5067 goto exit;
5068 }
5069 } else {
5070 rc = __cil_add_sepol_type(pdb, db, tgt, &rule->ttypes.types);
5071 if (rc != SEPOL_OK) {
5072 goto exit;
5073 }
5074 }
5075
5076 if (!cil_rule->is_extended) {
5077 rc = __cil_rule_to_sepol_class_perms(pdb, cil_rule->perms.classperms, &rule->perms);
5078 if (rc != SEPOL_OK) {
5079 goto exit;
5080 }
5081
5082 rc = check_assertion(pdb, rule);
5083 if (rc == CIL_TRUE) {
5084 *violation = CIL_TRUE;
5085 rc = __cil_print_neverallow_failure(db, node);
5086 if (rc != SEPOL_OK) {
5087 goto exit;
5088 }
5089 }
5090
5091 } else {
5092 rc = __cil_permx_to_sepol_class_perms(pdb, cil_rule->perms.x.permx, &rule->perms);
5093 if (rc != SEPOL_OK) {
5094 goto exit;
5095 }
5096
5097 rc = __cil_permx_bitmap_to_sepol_xperms_list(cil_rule->perms.x.permx->kind, cil_rule->perms.x.permx->perms, &xperms);
5098 if (rc != SEPOL_OK) {
5099 goto exit;
5100 }
5101
5102 cil_list_for_each(item, xperms) {
5103 rule->xperms = item->data;
5104 rc = check_assertion(pdb, rule);
5105 if (rc == CIL_TRUE) {
5106 *violation = CIL_TRUE;
5107 rc = __cil_print_neverallow_failure(db, node);
5108 if (rc != SEPOL_OK) {
5109 goto exit;
5110 }
5111 }
5112 }
5113 }
5114
5115 exit:
5116 if (xperms != NULL) {
5117 cil_list_for_each(item, xperms) {
5118 free(item->data);
5119 item->data = NULL;
5120 }
5121 cil_list_destroy(&xperms, CIL_FALSE);
5122 }
5123
5124 rule->xperms = NULL;
5125 __cil_destroy_sepol_avrules(rule);
5126
5127 return rc;
5128 }
5129
5130 static int cil_check_neverallows(const struct cil_db *db, policydb_t *pdb, struct cil_list *neverallows, int *violation)
5131 {
5132 int rc = SEPOL_OK;
5133 struct cil_list_item *item;
5134
5135 cil_list_for_each(item, neverallows) {
5136 rc = cil_check_neverallow(db, pdb, item->data, violation);
5137 if (rc != SEPOL_OK) {
5138 goto exit;
5139 }
5140 }
5141
5142 exit:
5143 return rc;
5144 }
5145
5146 static struct cil_list *cil_classperms_from_sepol(policydb_t *pdb, uint16_t class, uint32_t data, struct cil_class *class_value_to_cil[], struct cil_perm **perm_value_to_cil[])
5147 {
5148 struct cil_classperms *cp;
5149 struct cil_list *cp_list;
5150 class_datum_t *sepol_class = pdb->class_val_to_struct[class - 1];
5151 unsigned i;
5152
5153 cil_classperms_init(&cp);
5154
5155 cp->class = class_value_to_cil[class];
5156 if (!cp->class) goto exit;
5157
5158 cil_list_init(&cp->perms, CIL_PERM);
5159 for (i = 0; i < sepol_class->permissions.nprim; i++) {
5160 struct cil_perm *perm;
5161 if ((data & (UINT32_C(1) << i)) == 0) continue;
5162 perm = perm_value_to_cil[class][i+1];
5163 if (!perm) goto exit;
5164 cil_list_append(cp->perms, CIL_PERM, perm);
5165 }
5166
5167 cil_list_init(&cp_list, CIL_CLASSPERMS);
5168 cil_list_append(cp_list, CIL_CLASSPERMS, cp);
5169
5170 return cp_list;
5171
5172 exit:
5173 cil_destroy_classperms(cp);
5174 cil_log(CIL_ERR,"Failed to create CIL class-permissions from sepol values\n");
5175 return NULL;
5176 }
5177
5178 static int cil_avrule_from_sepol(policydb_t *pdb, avtab_ptr_t sepol_rule, struct cil_avrule *cil_rule, void *type_value_to_cil[], struct cil_class *class_value_to_cil[], struct cil_perm **perm_value_to_cil[])
5179 {
5180 int rc = SEPOL_ERR;
5181 avtab_key_t *k = &sepol_rule->key;
5182 avtab_datum_t *d = &sepol_rule->datum;
5183 cil_rule->src = type_value_to_cil[k->source_type];
5184 if (!cil_rule->src) goto exit;
5185
5186 cil_rule->tgt = type_value_to_cil[k->target_type];
5187 if (!cil_rule->tgt) goto exit;
5188
5189 cil_rule->perms.classperms = cil_classperms_from_sepol(pdb, k->target_class, d->data, class_value_to_cil, perm_value_to_cil);
5190 if (!cil_rule->perms.classperms) goto exit;
5191
5192 return SEPOL_OK;
5193
5194 exit:
5195 cil_log(CIL_ERR,"Failed to create CIL AV rule from sepol values\n");
5196 return rc;
5197 }
5198
5199 static int cil_check_type_bounds(const struct cil_db *db, policydb_t *pdb, void *type_value_to_cil, struct cil_class *class_value_to_cil[], struct cil_perm **perm_value_to_cil[], int *violation)
5200 {
5201 int rc = SEPOL_OK;
5202 int i;
5203
5204 for (i = 0; i < db->num_types; i++) {
5205 type_datum_t *child;
5206 type_datum_t *parent;
5207 avtab_ptr_t bad = NULL;
5208 int numbad = 0;
5209 struct cil_type *t = db->val_to_type[i];
5210
5211 if (!t->bounds) continue;
5212
5213 rc = __cil_get_sepol_type_datum(pdb, DATUM(t), &child);
5214 if (rc != SEPOL_OK) goto exit;
5215
5216 rc = __cil_get_sepol_type_datum(pdb, DATUM(t->bounds), &parent);
5217 if (rc != SEPOL_OK) goto exit;
5218
5219 rc = bounds_check_type(NULL, pdb, child->s.value, parent->s.value, &bad, &numbad);
5220 if (rc != SEPOL_OK) goto exit;
5221
5222 if (bad) {
5223 avtab_ptr_t cur;
5224 struct cil_avrule target;
5225 struct cil_tree_node *n1 = NULL;
5226 int count_bad = 0;
5227 enum cil_log_level log_level = cil_get_log_level();
5228
5229 *violation = CIL_TRUE;
5230
5231 target.is_extended = 0;
5232 target.rule_kind = CIL_AVRULE_ALLOWED;
5233 target.src_str = NULL;
5234 target.tgt_str = NULL;
5235
5236 cil_log(CIL_ERR, "Child type %s exceeds bounds of parent %s\n",
5237 t->datum.fqn, t->bounds->datum.fqn);
5238 for (cur = bad; cur; cur = cur->next) {
5239 struct cil_list_item *i2;
5240 struct cil_list *matching;
5241 int num_matching = 0;
5242 int count_matching = 0;
5243
5244 rc = cil_avrule_from_sepol(pdb, cur, &target, type_value_to_cil, class_value_to_cil, perm_value_to_cil);
5245 if (rc != SEPOL_OK) {
5246 cil_log(CIL_ERR, "Failed to convert sepol avrule to CIL\n");
5247 bounds_destroy_bad(bad);
5248 goto exit;
5249 }
5250 __cil_print_rule(" ", "allow", &target);
5251 cil_list_init(&matching, CIL_NODE);
5252 rc = cil_find_matching_avrule_in_ast(db->ast->root, CIL_AVRULE, &target, matching, CIL_TRUE);
5253 if (rc) {
5254 cil_log(CIL_ERR, "Error occurred while checking type bounds\n");
5255 cil_list_destroy(&matching, CIL_FALSE);
5256 cil_list_destroy(&target.perms.classperms, CIL_TRUE);
5257 bounds_destroy_bad(bad);
5258 goto exit;
5259 }
5260 cil_list_for_each(i2, matching) {
5261 num_matching++;
5262 }
5263 cil_list_for_each(i2, matching) {
5264 struct cil_tree_node *n2 = i2->data;
5265 struct cil_avrule *r2 = n2->data;
5266 if (n1 == n2) {
5267 cil_log(CIL_ERR, " <See previous>\n");
5268 } else {
5269 n1 = n2;
5270 __cil_print_parents(" ", n2);
5271 __cil_print_rule(" ", "allow", r2);
5272 }
5273 count_matching++;
5274 if (count_matching >= 2 && num_matching > 2 && log_level == CIL_ERR) {
5275 cil_log(CIL_ERR, " Only first 2 of %d matching rules shown (use \"-v\" to show all)\n", num_matching);
5276 break;
5277 }
5278 }
5279 cil_list_destroy(&matching, CIL_FALSE);
5280 cil_list_destroy(&target.perms.classperms, CIL_TRUE);
5281 count_bad++;
5282 if (count_bad >= 4 && numbad > 4 && log_level == CIL_ERR) {
5283 cil_log(CIL_ERR, " Only first 4 of %d bad rules shown (use \"-v\" to show all)\n", numbad);
5284 break;
5285 }
5286 }
5287 bounds_destroy_bad(bad);
5288 }
5289 }
5290
5291 exit:
5292 return rc;
5293 }
5294
5295 // assumes policydb is already allocated and initialized properly with things
5296 // like policy type set to kernel and version set appropriately
5297 int cil_binary_create_allocated_pdb(const struct cil_db *db, sepol_policydb_t *policydb)
5298 {
5299 int rc = SEPOL_ERR;
5300 int i;
5301 struct cil_args_binary extra_args;
5302 struct cil_args_booleanif booleanif_args;
5303 policydb_t *pdb = &policydb->p;
5304 struct cil_list *neverallows = NULL;
5305 hashtab_t role_trans_table = NULL;
5306 hashtab_t avrulex_ioctl_table = NULL;
5307 hashtab_t avrulex_nlmsg_table = NULL;
5308 void **type_value_to_cil = NULL;
5309 struct cil_class **class_value_to_cil = NULL;
5310 struct cil_perm ***perm_value_to_cil = NULL;
5311
5312 if (db == NULL || policydb == NULL) {
5313 if (db == NULL) {
5314 cil_log(CIL_ERR,"db == NULL\n");
5315 } else if (policydb == NULL) {
5316 cil_log(CIL_ERR,"policydb == NULL\n");
5317 }
5318 return SEPOL_ERR;
5319 }
5320
5321 /* libsepol values start at 1. Just allocate extra memory rather than
5322 * subtract 1 from the sepol value.
5323 */
5324 type_value_to_cil = calloc(db->num_types_and_attrs+1, sizeof(*type_value_to_cil));
5325 if (!type_value_to_cil) goto exit;
5326
5327 class_value_to_cil = calloc(db->num_classes+1, sizeof(*class_value_to_cil));
5328 if (!class_value_to_cil) goto exit;
5329
5330 perm_value_to_cil = calloc(db->num_classes+1, sizeof(*perm_value_to_cil));
5331 if (!perm_value_to_cil) goto exit;
5332 for (i=1; i < db->num_classes+1; i++) {
5333 perm_value_to_cil[i] = calloc(PERMS_PER_CLASS+1, sizeof(*perm_value_to_cil[i]));
5334 if (!perm_value_to_cil[i]) goto exit;
5335 }
5336
5337 rc = __cil_policydb_init(pdb, db, class_value_to_cil, perm_value_to_cil);
5338 if (rc != SEPOL_OK) {
5339 cil_log(CIL_ERR,"Problem in policydb_init\n");
5340 goto exit;
5341 }
5342
5343 role_trans_table = hashtab_create(role_trans_hash, role_trans_compare, ROLE_TRANS_TABLE_SIZE);
5344 if (!role_trans_table) {
5345 cil_log(CIL_INFO, "Failure to create hashtab for role_trans\n");
5346 goto exit;
5347 }
5348
5349 avrulex_ioctl_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
5350 if (!avrulex_ioctl_table) {
5351 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
5352 goto exit;
5353 }
5354
5355 avrulex_nlmsg_table = hashtab_create(avrulex_hash, avrulex_compare, AVRULEX_TABLE_SIZE);
5356 if (!avrulex_nlmsg_table) {
5357 cil_log(CIL_INFO, "Failure to create hashtab for avrulex\n");
5358 goto exit;
5359 }
5360
5361 cil_list_init(&neverallows, CIL_LIST_ITEM);
5362
5363 extra_args.db = db;
5364 extra_args.pdb = pdb;
5365 extra_args.neverallows = neverallows;
5366 extra_args.role_trans_table = role_trans_table;
5367 extra_args.avrulex_xperm_tables.ioctl = avrulex_ioctl_table;
5368 extra_args.avrulex_xperm_tables.nlmsg = avrulex_nlmsg_table;
5369 extra_args.type_value_to_cil = type_value_to_cil;
5370
5371 booleanif_args.db = db;
5372 booleanif_args.pdb = pdb;
5373 booleanif_args.cond_node = NULL;
5374 booleanif_args.cond_flavor = CIL_NONE;
5375
5376 for (i = 1; i <= 3; i++) {
5377 extra_args.pass = i;
5378
5379 rc = cil_tree_walk(db->ast->root, __cil_binary_create_helper, NULL, NULL, &extra_args);
5380 if (rc != SEPOL_OK) {
5381 cil_log(CIL_INFO, "Failure while walking cil database\n");
5382 goto exit;
5383 }
5384
5385 if (i == 1) {
5386 rc = __cil_policydb_val_arrays_create(pdb);
5387 if (rc != SEPOL_OK) {
5388 cil_log(CIL_INFO, "Failure creating val_to_{struct,name} arrays\n");
5389 goto exit;
5390 }
5391 }
5392
5393 if (i == 3) {
5394 rc = hashtab_map(avrulex_ioctl_table, __cil_avrulex_ioctl_to_policydb, &booleanif_args);
5395 if (rc != SEPOL_OK) {
5396 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
5397 goto exit;
5398 }
5399 rc = hashtab_map(avrulex_nlmsg_table, __cil_avrulex_nlmsg_to_policydb, &booleanif_args);
5400 if (rc != SEPOL_OK) {
5401 cil_log(CIL_INFO, "Failure creating avrulex rules\n");
5402 goto exit;
5403 }
5404 }
5405 }
5406
5407 rc = cil_sidorder_to_policydb(pdb, db);
5408 if (rc != SEPOL_OK) {
5409 goto exit;
5410 }
5411
5412 rc = __cil_contexts_to_policydb(pdb, db);
5413 if (rc != SEPOL_OK) {
5414 cil_log(CIL_INFO, "Failure while inserting cil contexts into sepol policydb\n");
5415 goto exit;
5416 }
5417
5418 if (pdb->type_attr_map == NULL) {
5419 rc = __cil_typeattr_bitmap_init(pdb);
5420 if (rc != SEPOL_OK) {
5421 cil_log(CIL_INFO, "Failure while initializing typeattribute bitmap\n");
5422 goto exit;
5423 }
5424 }
5425
5426 cond_optimize_lists(pdb->cond_list);
5427 __cil_set_conditional_state_and_flags(pdb);
5428
5429 if (db->disable_neverallow != CIL_TRUE) {
5430 int violation = CIL_FALSE;
5431 cil_log(CIL_INFO, "Checking Neverallows\n");
5432 rc = cil_check_neverallows(db, pdb, neverallows, &violation);
5433 if (rc != SEPOL_OK) goto exit;
5434
5435 cil_log(CIL_INFO, "Checking User Bounds\n");
5436 rc = bounds_check_users(NULL, pdb);
5437 if (rc) {
5438 violation = CIL_TRUE;
5439 }
5440
5441 cil_log(CIL_INFO, "Checking Role Bounds\n");
5442 rc = bounds_check_roles(NULL, pdb);
5443 if (rc) {
5444 violation = CIL_TRUE;
5445 }
5446
5447 cil_log(CIL_INFO, "Checking Type Bounds\n");
5448 rc = cil_check_type_bounds(db, pdb, type_value_to_cil, class_value_to_cil, perm_value_to_cil, &violation);
5449 if (rc != SEPOL_OK) goto exit;
5450
5451 if (violation == CIL_TRUE) {
5452 rc = SEPOL_ERR;
5453 goto exit;
5454 }
5455
5456 }
5457
5458 /* This pre-expands the roles and users for context validity checking */
5459 if (hashtab_map(pdb->p_roles.table, policydb_role_cache, pdb)) {
5460 cil_log(CIL_INFO, "Failure creating roles cache");
5461 rc = SEPOL_ERR;
5462 goto exit;
5463 }
5464
5465 if (hashtab_map(pdb->p_users.table, policydb_user_cache, pdb)) {
5466 cil_log(CIL_INFO, "Failure creating users cache");
5467 rc = SEPOL_ERR;
5468 goto exit;
5469 }
5470
5471 rc = SEPOL_OK;
5472
5473 exit:
5474 hashtab_destroy(role_trans_table);
5475 hashtab_map(avrulex_ioctl_table, __cil_avrulex_xperm_destroy, NULL);
5476 hashtab_destroy(avrulex_ioctl_table);
5477 hashtab_map(avrulex_nlmsg_table, __cil_avrulex_xperm_destroy, NULL);
5478 hashtab_destroy(avrulex_nlmsg_table);
5479 free(type_value_to_cil);
5480 free(class_value_to_cil);
5481 if (perm_value_to_cil != NULL) {
5482 /* Range is because libsepol values start at 1. */
5483 for (i=1; i < db->num_classes+1; i++) {
5484 free(perm_value_to_cil[i]);
5485 }
5486 free(perm_value_to_cil);
5487 }
5488 cil_list_destroy(&neverallows, CIL_FALSE);
5489
5490 return rc;
5491 }