• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 
37 #include <sepol/policydb/conditional.h>
38 #include <sepol/errcodes.h>
39 
40 #include "cil_internal.h"
41 #include "cil_flavor.h"
42 #include "cil_log.h"
43 #include "cil_mem.h"
44 #include "cil_tree.h"
45 #include "cil_list.h"
46 #include "cil_post.h"
47 #include "cil_policy.h"
48 #include "cil_verify.h"
49 #include "cil_symtab.h"
50 
51 #define GEN_REQUIRE_ATTR "cil_gen_require" /* Also in libsepol/src/module_to_cil.c */
52 #define TYPEATTR_INFIX "_typeattr_"        /* Also in libsepol/src/module_to_cil.c */
53 
54 struct fc_data {
55 	unsigned int meta;
56 	size_t stem_len;
57 	size_t str_len;
58 };
59 
60 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db);
61 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db);
62 
cats_compare(struct cil_cats * a,struct cil_cats * b)63 static int cats_compare(struct cil_cats *a, struct cil_cats *b)
64 {
65 	struct cil_list_item *i, *j;
66 	int rc;
67 
68 	if (a == b) return 0;
69 	if (!a) return -1;
70 	if (!b) return 1;
71 
72 	/* Expects cat expression to have been evaluated */
73 	cil_list_for_each(i, a->datum_expr) {
74 		cil_list_for_each(j, b->datum_expr) {
75 			rc = strcmp(DATUM(i->data)->fqn, DATUM(j->data)->fqn);
76 			if (!rc) return rc;
77 		}
78 	}
79 	return 0;
80 }
81 
level_compare(struct cil_level * a,struct cil_level * b)82 static int level_compare(struct cil_level *a, struct cil_level *b)
83 {
84 	int rc;
85 
86 	if (a == b) return 0;
87 	if (!a) return -1;
88 	if (!b) return 1;
89 
90 	if (a->sens != b->sens) {
91 		rc = strcmp(DATUM(a->sens)->fqn, DATUM(b->sens)->fqn);
92 		if (rc != 0) return rc;
93 	}
94 	if (a->cats != b->cats) {
95 		return cats_compare(a->cats, b->cats);
96 	}
97 	return 0;
98 }
99 
range_compare(struct cil_levelrange * a,struct cil_levelrange * b)100 static int range_compare(struct cil_levelrange *a, struct cil_levelrange *b)
101 {
102 	int rc;
103 
104 	if (a == b) return 0;
105 	if (!a) return -1;
106 	if (!b) return 1;
107 
108 	if (a->low != b->low) {
109 		rc = level_compare(a->low, b->low);
110 		if (rc != 0) return rc;
111 	}
112 	if (a->high != b->high) {
113 		return level_compare(a->high, b->high);
114 	}
115 	return 0;
116 }
117 
context_compare(struct cil_context * a,struct cil_context * b)118 static int context_compare(struct cil_context *a, struct cil_context *b)
119 {
120 	int rc;
121 
122 	if (a->user != b->user) {
123 		rc = strcmp(DATUM(a->user)->fqn, DATUM(b->user)->fqn);
124 		if (rc != 0) return rc;
125 	}
126 	if (a->role != b->role) {
127 		rc = strcmp(DATUM(a->role)->fqn, DATUM(b->role)->fqn);
128 		if (rc != 0) return rc;
129 	}
130 	if (a->type != b->type) {
131 		rc = strcmp(DATUM(a->type)->fqn, DATUM(b->type)->fqn);
132 		if (rc != 0) return rc;
133 	}
134 	if (a->range != b->range) {
135 		return range_compare(a->range, b->range);
136 	}
137 	return 0;
138 }
139 
cil_verify_is_list(struct cil_list * list,enum cil_flavor flavor)140 static int cil_verify_is_list(struct cil_list *list, enum cil_flavor flavor)
141 {
142 	struct cil_list_item *curr;
143 
144 	cil_list_for_each(curr, list) {
145 		switch (curr->flavor) {
146 		case CIL_LIST:
147 			return CIL_FALSE;
148 			break;
149 		case CIL_OP:
150 			return CIL_FALSE;
151 			break;
152 		default:
153 			if (flavor == CIL_CAT) {
154 				struct cil_symtab_datum *d = curr->data;
155 				struct cil_tree_node *n = d->nodes->head->data;
156 				if (n->flavor == CIL_CATSET) {
157 					return CIL_FALSE;
158 				}
159 			}
160 			break;
161 		}
162 	}
163 	return CIL_TRUE;
164 }
165 
cil_post_fc_fill_data(struct fc_data * fc,const char * path)166 static void cil_post_fc_fill_data(struct fc_data *fc, const char *path)
167 {
168 	size_t c = 0;
169 	fc->meta = 0;
170 	fc->stem_len = 0;
171 	fc->str_len = 0;
172 
173 	while (path[c] != '\0') {
174 		switch (path[c]) {
175 		case '.':
176 		case '^':
177 		case '$':
178 		case '?':
179 		case '*':
180 		case '+':
181 		case '|':
182 		case '[':
183 		case '(':
184 		case '{':
185 			fc->meta = 1;
186 			break;
187 		case '\\':
188 			c++;
189 			if (path[c] == '\0') {
190 				if (!fc->meta) {
191 					fc->stem_len++;
192 				}
193 				fc->str_len++;
194 				return;
195 			}
196 			/* FALLTHRU */
197 		default:
198 			if (!fc->meta) {
199 				fc->stem_len++;
200 			}
201 			break;
202 		}
203 		fc->str_len++;
204 		c++;
205 	}
206 }
207 
cil_post_filecon_compare(const void * a,const void * b)208 int cil_post_filecon_compare(const void *a, const void *b)
209 {
210 	int rc = 0;
211 	struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
212 	struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
213 	struct fc_data *a_data = cil_malloc(sizeof(*a_data));
214 	struct fc_data *b_data = cil_malloc(sizeof(*b_data));
215 	char *a_path = cil_malloc(strlen(a_filecon->path_str) + 1);
216 	char *b_path = cil_malloc(strlen(b_filecon->path_str) + 1);
217 	a_path[0] = '\0';
218 	b_path[0] = '\0';
219 	strcat(a_path, a_filecon->path_str);
220 	strcat(b_path, b_filecon->path_str);
221 	cil_post_fc_fill_data(a_data, a_path);
222 	cil_post_fc_fill_data(b_data, b_path);
223 	if (a_data->meta && !b_data->meta) {
224 		rc = -1;
225 	} else if (b_data->meta && !a_data->meta) {
226 		rc = 1;
227 	} else if (a_data->stem_len < b_data->stem_len) {
228 		rc = -1;
229 	} else if (b_data->stem_len < a_data->stem_len) {
230 		rc = 1;
231 	} else if (a_data->str_len < b_data->str_len) {
232 		rc = -1;
233 	} else if (b_data->str_len < a_data->str_len) {
234 		rc = 1;
235 	} else if (a_filecon->type < b_filecon->type) {
236 		rc = -1;
237 	} else if (b_filecon->type < a_filecon->type) {
238 		rc = 1;
239 	} else {
240 		rc = strcmp(a_filecon->path_str, b_filecon->path_str);
241 	}
242 
243 	free(a_path);
244 	free(b_path);
245 	free(a_data);
246 	free(b_data);
247 
248 	return rc;
249 }
250 
cil_post_ibpkeycon_compare(const void * a,const void * b)251 int cil_post_ibpkeycon_compare(const void *a, const void *b)
252 {
253 	int rc = SEPOL_ERR;
254 	struct cil_ibpkeycon *aibpkeycon = *(struct cil_ibpkeycon **)a;
255 	struct cil_ibpkeycon *bibpkeycon = *(struct cil_ibpkeycon **)b;
256 
257 	rc = strcmp(aibpkeycon->subnet_prefix_str, bibpkeycon->subnet_prefix_str);
258 	if (rc)
259 		return rc;
260 
261 	rc = (aibpkeycon->pkey_high - aibpkeycon->pkey_low)
262 		- (bibpkeycon->pkey_high - bibpkeycon->pkey_low);
263 	if (rc == 0) {
264 		if (aibpkeycon->pkey_low < bibpkeycon->pkey_low)
265 			rc = -1;
266 		else if (bibpkeycon->pkey_low < aibpkeycon->pkey_low)
267 			rc = 1;
268 	}
269 
270 	return rc;
271 }
272 
cil_post_portcon_compare(const void * a,const void * b)273 int cil_post_portcon_compare(const void *a, const void *b)
274 {
275 	int rc = SEPOL_ERR;
276 	struct cil_portcon *aportcon = *(struct cil_portcon**)a;
277 	struct cil_portcon *bportcon = *(struct cil_portcon**)b;
278 
279 	rc = (aportcon->port_high - aportcon->port_low)
280 		- (bportcon->port_high - bportcon->port_low);
281 	if (rc == 0) {
282 		if (aportcon->port_low < bportcon->port_low) {
283 			rc = -1;
284 		} else if (bportcon->port_low < aportcon->port_low) {
285 			rc = 1;
286 		} else if (aportcon->proto < bportcon->proto) {
287 			rc = -1;
288 		} else if (aportcon->proto > bportcon->proto) {
289 			rc = 1;
290 		}
291 	}
292 
293 	return rc;
294 }
295 
cil_post_genfscon_compare(const void * a,const void * b)296 int cil_post_genfscon_compare(const void *a, const void *b)
297 {
298 	int rc = SEPOL_ERR;
299 	struct cil_genfscon *agenfscon = *(struct cil_genfscon**)a;
300 	struct cil_genfscon *bgenfscon = *(struct cil_genfscon**)b;
301 
302 	rc = strcmp(agenfscon->fs_str, bgenfscon->fs_str);
303 	if (rc == 0) {
304 		rc = strcmp(agenfscon->path_str, bgenfscon->path_str);
305 	}
306 
307 	return rc;
308 }
309 
cil_post_netifcon_compare(const void * a,const void * b)310 int cil_post_netifcon_compare(const void *a, const void *b)
311 {
312 	struct cil_netifcon *anetifcon = *(struct cil_netifcon**)a;
313 	struct cil_netifcon *bnetifcon = *(struct cil_netifcon**)b;
314 
315 	return  strcmp(anetifcon->interface_str, bnetifcon->interface_str);
316 }
317 
cil_post_ibendportcon_compare(const void * a,const void * b)318 int cil_post_ibendportcon_compare(const void *a, const void *b)
319 {
320 	int rc = SEPOL_ERR;
321 
322 	struct cil_ibendportcon *aibendportcon = *(struct cil_ibendportcon **)a;
323 	struct cil_ibendportcon *bibendportcon = *(struct cil_ibendportcon **)b;
324 
325 	rc = strcmp(aibendportcon->dev_name_str, bibendportcon->dev_name_str);
326 	if (rc)
327 		return rc;
328 
329 	if (aibendportcon->port < bibendportcon->port)
330 		return -1;
331 	else if (bibendportcon->port < aibendportcon->port)
332 		return 1;
333 
334 	return rc;
335 }
336 
cil_post_nodecon_compare(const void * a,const void * b)337 int cil_post_nodecon_compare(const void *a, const void *b)
338 {
339 	struct cil_nodecon *anodecon;
340 	struct cil_nodecon *bnodecon;
341 	anodecon = *(struct cil_nodecon**)a;
342 	bnodecon = *(struct cil_nodecon**)b;
343 
344 	/* sort ipv4 before ipv6 */
345 	if (anodecon->addr->family != bnodecon->addr->family) {
346 		if (anodecon->addr->family == AF_INET) {
347 			return -1;
348 		} else {
349 			return 1;
350 		}
351 	}
352 
353 	/* most specific netmask goes first, then order by ip addr */
354 	if (anodecon->addr->family == AF_INET) {
355 		int rc = memcmp(&anodecon->mask->ip.v4, &bnodecon->mask->ip.v4, sizeof(anodecon->mask->ip.v4));
356 		if (rc != 0) {
357 			return -1 * rc;
358 		}
359 		return memcmp(&anodecon->addr->ip.v4, &bnodecon->addr->ip.v4, sizeof(anodecon->addr->ip.v4));
360 	} else {
361 		int rc = memcmp(&anodecon->mask->ip.v6, &bnodecon->mask->ip.v6, sizeof(anodecon->mask->ip.v6));
362 		if (rc != 0) {
363 			return -1 * rc;
364 		}
365 		return memcmp(&anodecon->addr->ip.v6, &bnodecon->addr->ip.v6, sizeof(anodecon->addr->ip.v6));
366 	}
367 }
368 
cil_post_pirqcon_compare(const void * a,const void * b)369 static int cil_post_pirqcon_compare(const void *a, const void *b)
370 {
371 	int rc = SEPOL_ERR;
372 	struct cil_pirqcon *apirqcon = *(struct cil_pirqcon**)a;
373 	struct cil_pirqcon *bpirqcon = *(struct cil_pirqcon**)b;
374 
375 	if (apirqcon->pirq < bpirqcon->pirq) {
376 		rc = -1;
377 	} else if (bpirqcon->pirq < apirqcon->pirq) {
378 		rc = 1;
379 	} else {
380 		rc = 0;
381 	}
382 
383 	return rc;
384 }
385 
cil_post_iomemcon_compare(const void * a,const void * b)386 static int cil_post_iomemcon_compare(const void *a, const void *b)
387 {
388 	int rc = SEPOL_ERR;
389 	struct cil_iomemcon *aiomemcon = *(struct cil_iomemcon**)a;
390 	struct cil_iomemcon *biomemcon = *(struct cil_iomemcon**)b;
391 
392 	rc = (aiomemcon->iomem_high - aiomemcon->iomem_low)
393 		- (biomemcon->iomem_high - biomemcon->iomem_low);
394 	if (rc == 0) {
395 		if (aiomemcon->iomem_low < biomemcon->iomem_low) {
396 			rc = -1;
397 		} else if (biomemcon->iomem_low < aiomemcon->iomem_low) {
398 			rc = 1;
399 		}
400 	}
401 
402 	return rc;
403 }
404 
cil_post_ioportcon_compare(const void * a,const void * b)405 static int cil_post_ioportcon_compare(const void *a, const void *b)
406 {
407 	int rc = SEPOL_ERR;
408 	struct cil_ioportcon *aioportcon = *(struct cil_ioportcon**)a;
409 	struct cil_ioportcon *bioportcon = *(struct cil_ioportcon**)b;
410 
411 	rc = (aioportcon->ioport_high - aioportcon->ioport_low)
412 		- (bioportcon->ioport_high - bioportcon->ioport_low);
413 	if (rc == 0) {
414 		if (aioportcon->ioport_low < bioportcon->ioport_low) {
415 			rc = -1;
416 		} else if (bioportcon->ioport_low < aioportcon->ioport_low) {
417 			rc = 1;
418 		}
419 	}
420 
421 	return rc;
422 }
423 
cil_post_pcidevicecon_compare(const void * a,const void * b)424 static int cil_post_pcidevicecon_compare(const void *a, const void *b)
425 {
426 	int rc = SEPOL_ERR;
427 	struct cil_pcidevicecon *apcidevicecon = *(struct cil_pcidevicecon**)a;
428 	struct cil_pcidevicecon *bpcidevicecon = *(struct cil_pcidevicecon**)b;
429 
430 	if (apcidevicecon->dev < bpcidevicecon->dev) {
431 		rc = -1;
432 	} else if (bpcidevicecon->dev < apcidevicecon->dev) {
433 		rc = 1;
434 	} else {
435 		rc = 0;
436 	}
437 
438 	return rc;
439 }
440 
cil_post_devicetreecon_compare(const void * a,const void * b)441 static int cil_post_devicetreecon_compare(const void *a, const void *b)
442 {
443 	int rc = SEPOL_ERR;
444 	struct cil_devicetreecon *adevicetreecon = *(struct cil_devicetreecon**)a;
445 	struct cil_devicetreecon *bdevicetreecon = *(struct cil_devicetreecon**)b;
446 
447 	rc = strcmp(adevicetreecon->path, bdevicetreecon->path);
448 
449 	return rc;
450 }
451 
cil_post_fsuse_compare(const void * a,const void * b)452 int cil_post_fsuse_compare(const void *a, const void *b)
453 {
454 	int rc;
455 	struct cil_fsuse *afsuse;
456 	struct cil_fsuse *bfsuse;
457 	afsuse = *(struct cil_fsuse**)a;
458 	bfsuse = *(struct cil_fsuse**)b;
459 	if (afsuse->type < bfsuse->type) {
460 		rc = -1;
461 	} else if (bfsuse->type < afsuse->type) {
462 		rc = 1;
463 	} else {
464 		rc = strcmp(afsuse->fs_str, bfsuse->fs_str);
465 	}
466 	return rc;
467 }
468 
cil_post_filecon_context_compare(const void * a,const void * b)469 static int cil_post_filecon_context_compare(const void *a, const void *b)
470 {
471 	struct cil_filecon *a_filecon = *(struct cil_filecon**)a;
472 	struct cil_filecon *b_filecon = *(struct cil_filecon**)b;
473 	return context_compare(a_filecon->context, b_filecon->context);
474 }
475 
cil_post_ibpkeycon_context_compare(const void * a,const void * b)476 static int cil_post_ibpkeycon_context_compare(const void *a, const void *b)
477 {
478 	struct cil_ibpkeycon *a_ibpkeycon = *(struct cil_ibpkeycon **)a;
479 	struct cil_ibpkeycon *b_ibpkeycon = *(struct cil_ibpkeycon **)b;
480 	return context_compare(a_ibpkeycon->context, b_ibpkeycon->context);
481 }
482 
cil_post_portcon_context_compare(const void * a,const void * b)483 static int cil_post_portcon_context_compare(const void *a, const void *b)
484 {
485 	struct cil_portcon *a_portcon = *(struct cil_portcon**)a;
486 	struct cil_portcon *b_portcon = *(struct cil_portcon**)b;
487 	return context_compare(a_portcon->context, b_portcon->context);
488 }
489 
cil_post_genfscon_context_compare(const void * a,const void * b)490 static int cil_post_genfscon_context_compare(const void *a, const void *b)
491 {
492 	struct cil_genfscon *a_genfscon = *(struct cil_genfscon**)a;
493 	struct cil_genfscon *b_genfscon = *(struct cil_genfscon**)b;
494 	return context_compare(a_genfscon->context, b_genfscon->context);
495 }
496 
cil_post_netifcon_context_compare(const void * a,const void * b)497 static int cil_post_netifcon_context_compare(const void *a, const void *b)
498 {
499 	int rc;
500 	struct cil_netifcon *a_netifcon = *(struct cil_netifcon**)a;
501 	struct cil_netifcon *b_netifcon = *(struct cil_netifcon**)b;
502 	rc = context_compare(a_netifcon->if_context, b_netifcon->if_context);
503 	if (rc != 0) {
504 		return rc;
505 	}
506 	return context_compare(a_netifcon->packet_context, b_netifcon->packet_context);
507 }
508 
cil_post_ibendportcon_context_compare(const void * a,const void * b)509 static int cil_post_ibendportcon_context_compare(const void *a, const void *b)
510 {
511 	struct cil_ibendportcon *a_ibendportcon = *(struct cil_ibendportcon **)a;
512 	struct cil_ibendportcon *b_ibendportcon = *(struct cil_ibendportcon **)b;
513 	return context_compare(a_ibendportcon->context, b_ibendportcon->context);
514 }
515 
cil_post_nodecon_context_compare(const void * a,const void * b)516 static int cil_post_nodecon_context_compare(const void *a, const void *b)
517 {
518 	struct cil_nodecon *a_nodecon = *(struct cil_nodecon **)a;
519 	struct cil_nodecon *b_nodecon = *(struct cil_nodecon **)b;
520 	return context_compare(a_nodecon->context, b_nodecon->context);
521 }
522 
cil_post_pirqcon_context_compare(const void * a,const void * b)523 static int cil_post_pirqcon_context_compare(const void *a, const void *b)
524 {
525 	struct cil_pirqcon *a_pirqcon = *(struct cil_pirqcon**)a;
526 	struct cil_pirqcon *b_pirqcon = *(struct cil_pirqcon**)b;
527 	return context_compare(a_pirqcon->context, b_pirqcon->context);
528 }
529 
cil_post_iomemcon_context_compare(const void * a,const void * b)530 static int cil_post_iomemcon_context_compare(const void *a, const void *b)
531 {
532 	struct cil_iomemcon *a_iomemcon = *(struct cil_iomemcon**)a;
533 	struct cil_iomemcon *b_iomemcon = *(struct cil_iomemcon**)b;
534 	return context_compare(a_iomemcon->context, b_iomemcon->context);
535 }
536 
cil_post_ioportcon_context_compare(const void * a,const void * b)537 static int cil_post_ioportcon_context_compare(const void *a, const void *b)
538 {
539 	struct cil_ioportcon *a_ioportcon = *(struct cil_ioportcon**)a;
540 	struct cil_ioportcon *b_ioportcon = *(struct cil_ioportcon**)b;
541 	return context_compare(a_ioportcon->context, b_ioportcon->context);
542 }
543 
cil_post_pcidevicecon_context_compare(const void * a,const void * b)544 static int cil_post_pcidevicecon_context_compare(const void *a, const void *b)
545 {
546 	struct cil_pcidevicecon *a_pcidevicecon = *(struct cil_pcidevicecon**)a;
547 	struct cil_pcidevicecon *b_pcidevicecon = *(struct cil_pcidevicecon**)b;
548 	return context_compare(a_pcidevicecon->context, b_pcidevicecon->context);
549 }
550 
cil_post_devicetreecon_context_compare(const void * a,const void * b)551 static int cil_post_devicetreecon_context_compare(const void *a, const void *b)
552 {
553 	struct cil_devicetreecon *a_devicetreecon = *(struct cil_devicetreecon**)a;
554 	struct cil_devicetreecon *b_devicetreecon = *(struct cil_devicetreecon**)b;
555 	return context_compare(a_devicetreecon->context, b_devicetreecon->context);
556 }
557 
cil_post_fsuse_context_compare(const void * a,const void * b)558 static int cil_post_fsuse_context_compare(const void *a, const void *b)
559 {
560 	struct cil_fsuse *a_fsuse = *(struct cil_fsuse**)a;
561 	struct cil_fsuse *b_fsuse = *(struct cil_fsuse**)b;
562 	return context_compare(a_fsuse->context, b_fsuse->context);
563 }
564 
__cil_post_db_count_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)565 static int __cil_post_db_count_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
566 {
567 	struct cil_db *db = extra_args;
568 
569 	switch(node->flavor) {
570 	case CIL_BLOCK: {
571 		struct cil_block *blk = node->data;
572 		if (blk->is_abstract == CIL_TRUE) {
573 			*finished = CIL_TREE_SKIP_HEAD;
574 		}
575 		break;
576 	}
577 	case CIL_MACRO:
578 		*finished = CIL_TREE_SKIP_HEAD;
579 		break;
580 	case CIL_CLASS: {
581 		struct cil_class *class = node->data;
582 		if (class->datum.nodes->head->data == node) {
583 			// Multiple nodes can point to the same datum. Only count once.
584 			db->num_classes++;
585 		}
586 		break;
587 	}
588 	case CIL_TYPE: {
589 		struct cil_type *type = node->data;
590 		if (type->datum.nodes->head->data == node) {
591 			// Multiple nodes can point to the same datum. Only count once.
592 			type->value = db->num_types;
593 			db->num_types++;
594 			db->num_types_and_attrs++;
595 		}
596 		break;
597 	}
598 	case CIL_TYPEATTRIBUTE: {
599 		struct cil_typeattribute *attr = node->data;
600 		if (attr->datum.nodes->head->data == node) {
601 			// Multiple nodes can point to the same datum. Only count once.
602 			db->num_types_and_attrs++;
603 		}
604 		break;
605 	}
606 
607 	case CIL_ROLE: {
608 		struct cil_role *role = node->data;
609 		if (role->datum.nodes->head->data == node) {
610 			// Multiple nodes can point to the same datum. Only count once.
611 			role->value = db->num_roles;
612 			db->num_roles++;
613 		}
614 		break;
615 	}
616 	case CIL_USER: {
617 		struct cil_user *user = node->data;
618 		if (user->datum.nodes->head->data == node) {
619 			// multiple AST nodes can point to the same cil_user data (like if
620 			// copied from a macro). This check ensures we only count the
621 			// duplicates once
622 			user->value = db->num_users;
623 			db->num_users++;
624 		}
625 		break;
626 	}
627 	case CIL_NETIFCON:
628 		db->netifcon->count++;
629 		break;
630 	case CIL_GENFSCON:
631 		db->genfscon->count++;
632 		break;
633 	case CIL_FILECON:
634 		db->filecon->count++;
635 		break;
636 	case CIL_NODECON:
637 		db->nodecon->count++;
638 		break;
639 	case CIL_IBPKEYCON:
640 		db->ibpkeycon->count++;
641 		break;
642 	case CIL_IBENDPORTCON:
643 		db->ibendportcon->count++;
644 		break;
645 	case CIL_PORTCON:
646 		db->portcon->count++;
647 		break;
648 	case CIL_PIRQCON:
649 		db->pirqcon->count++;
650 		break;
651 	case CIL_IOMEMCON:
652 		db->iomemcon->count++;
653 		break;
654 	case CIL_IOPORTCON:
655 		db->ioportcon->count++;
656 		break;
657 	case CIL_PCIDEVICECON:
658 		db->pcidevicecon->count++;
659 		break;
660 	case CIL_DEVICETREECON:
661 		db->devicetreecon->count++;
662 		break;
663 	case CIL_FSUSE:
664 		db->fsuse->count++;
665 		break;
666 	default:
667 		break;
668 	}
669 
670 	return SEPOL_OK;
671 }
672 
__cil_post_db_array_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)673 static int __cil_post_db_array_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
674 {
675 	struct cil_db *db = extra_args;
676 
677 	switch(node->flavor) {
678 	case CIL_BLOCK: {
679 		struct cil_block *blk = node->data;
680 		if (blk->is_abstract == CIL_TRUE) {
681 			*finished = CIL_TREE_SKIP_HEAD;
682 		}
683 		break;
684 	}
685 	case CIL_MACRO:
686 		*finished = CIL_TREE_SKIP_HEAD;
687 		break;
688 	case CIL_TYPE: {
689 		struct cil_type *type = node->data;
690 		if (db->val_to_type == NULL) {
691 			db->val_to_type = cil_malloc(sizeof(*db->val_to_type) * db->num_types);
692 		}
693 		db->val_to_type[type->value] = type;
694 		break;
695 	}
696 	case CIL_ROLE: {
697 		struct cil_role *role = node->data;
698 		if (db->val_to_role == NULL) {
699 			db->val_to_role = cil_malloc(sizeof(*db->val_to_role) * db->num_roles);
700 		}
701 		db->val_to_role[role->value] = role;
702 		break;
703 	}
704 	case CIL_USER: {
705 		struct cil_user *user= node->data;
706 		if (db->val_to_user == NULL) {
707 			db->val_to_user = cil_malloc(sizeof(*db->val_to_user) * db->num_users);
708 		}
709 		db->val_to_user[user->value] = user;
710 		break;
711 	}
712 	case CIL_USERPREFIX: {
713 		cil_list_append(db->userprefixes, CIL_USERPREFIX, node->data);
714 		break;
715 	}
716 	case CIL_SELINUXUSER: {
717 		cil_list_prepend(db->selinuxusers, CIL_SELINUXUSER, node->data);
718 		break;
719 	}
720 	case CIL_SELINUXUSERDEFAULT: {
721 		cil_list_append(db->selinuxusers, CIL_SELINUXUSERDEFAULT, node->data);
722 		break;
723 	}
724 	case CIL_NETIFCON: {
725 		struct cil_sort *sort = db->netifcon;
726 		uint32_t count = sort->count;
727 		uint32_t i = sort->index;
728 		if (sort->array == NULL) {
729 			sort->array = cil_malloc(sizeof(*sort->array)*count);
730 		}
731 		sort->array[i] = node->data;
732 		sort->index++;
733 		break;
734 	}
735 	case CIL_IBENDPORTCON: {
736 		struct cil_sort *sort = db->ibendportcon;
737 		uint32_t count = sort->count;
738 		uint32_t i = sort->index;
739 
740 		if (!sort->array)
741 			sort->array = cil_malloc(sizeof(*sort->array) * count);
742 		sort->array[i] = node->data;
743 		sort->index++;
744 		break;
745 	}
746 	case CIL_FSUSE: {
747 		struct cil_sort *sort = db->fsuse;
748 		uint32_t count = sort->count;
749 		uint32_t i = sort->index;
750 		if (sort->array == NULL) {
751 			sort->array = cil_malloc(sizeof(*sort->array)*count);
752 		}
753 		sort->array[i] = node->data;
754 		sort->index++;
755 		break;
756 	}
757 	case CIL_GENFSCON: {
758 		struct cil_sort *sort = db->genfscon;
759 		uint32_t count = sort->count;
760 		uint32_t i = sort->index;
761 		if (sort->array == NULL) {
762 			sort->array = cil_malloc(sizeof(*sort->array)*count);
763 		}
764 		sort->array[i] = node->data;
765 		sort->index++;
766 		break;
767 	}
768 	case CIL_FILECON: {
769 		struct cil_sort *sort = db->filecon;
770 		uint32_t count = sort->count;
771 		uint32_t i = sort->index;
772 		if (sort->array == NULL) {
773 		sort->array = cil_malloc(sizeof(*sort->array)*count);
774 		}
775 		sort->array[i] = node->data;
776 		sort->index++;
777 		break;
778 	}
779 	case CIL_NODECON: {
780 		struct cil_sort *sort = db->nodecon;
781 		uint32_t count = sort->count;
782 		uint32_t i = sort->index;
783 		if (sort->array == NULL) {
784 			sort->array = cil_malloc(sizeof(*sort->array)*count);
785 		}
786 		sort->array[i] = node->data;
787 		sort->index++;
788 		break;
789 	}
790 	case CIL_IBPKEYCON: {
791 		struct cil_sort *sort = db->ibpkeycon;
792 		uint32_t count = sort->count;
793 		uint32_t i = sort->index;
794 
795 		if (!sort->array)
796 			sort->array = cil_malloc(sizeof(*sort->array) * count);
797 		sort->array[i] = node->data;
798 		sort->index++;
799 		break;
800 	}
801 	case CIL_PORTCON: {
802 		struct cil_sort *sort = db->portcon;
803 		uint32_t count = sort->count;
804 		uint32_t i = sort->index;
805 		if (sort->array == NULL) {
806 			sort->array = cil_malloc(sizeof(*sort->array)*count);
807 		}
808 		sort->array[i] = node->data;
809 		sort->index++;
810 		break;
811 	}
812 	case CIL_PIRQCON: {
813 		struct cil_sort *sort = db->pirqcon;
814 		uint32_t count = sort->count;
815 		uint32_t i = sort->index;
816 		if (sort->array == NULL) {
817 			sort->array = cil_malloc(sizeof(*sort->array)*count);
818 		}
819 		sort->array[i] = node->data;
820 		sort->index++;
821 		break;
822 	}
823 	case CIL_IOMEMCON: {
824 		struct cil_sort *sort = db->iomemcon;
825 		uint32_t count = sort->count;
826 		uint32_t i = sort->index;
827 		if (sort->array == NULL) {
828 			sort->array = cil_malloc(sizeof(*sort->array)*count);
829 		}
830 		sort->array[i] = node->data;
831 		sort->index++;
832 		break;
833 	}
834 	case CIL_IOPORTCON: {
835 		struct cil_sort *sort = db->ioportcon;
836 		uint32_t count = sort->count;
837 		uint32_t i = sort->index;
838 		if (sort->array == NULL) {
839 			sort->array = cil_malloc(sizeof(*sort->array)*count);
840 		}
841 		sort->array[i] = node->data;
842 		sort->index++;
843 		break;
844 	}
845 	case CIL_PCIDEVICECON: {
846 		struct cil_sort *sort = db->pcidevicecon;
847 		uint32_t count = sort->count;
848 		uint32_t i = sort->index;
849 		if (sort->array == NULL) {
850 			sort->array = cil_malloc(sizeof(*sort->array)*count);
851 		}
852 		sort->array[i] = node->data;
853 		sort->index++;
854 		break;
855 	}
856 	case CIL_DEVICETREECON: {
857 		struct cil_sort *sort = db->devicetreecon;
858 		uint32_t count = sort->count;
859 		uint32_t i = sort->index;
860 		if (sort->array == NULL) {
861 			sort->array = cil_malloc(sizeof(*sort->array)*count);
862 		}
863 		sort->array[i] = node->data;
864 		sort->index++;
865 		break;
866 	}
867 	default:
868 		break;
869 	}
870 
871 	return SEPOL_OK;
872 }
873 
__evaluate_type_expression(struct cil_typeattribute * attr,struct cil_db * db)874 static int __evaluate_type_expression(struct cil_typeattribute *attr, struct cil_db *db)
875 {
876 	int rc;
877 
878 	attr->types = cil_malloc(sizeof(*attr->types));
879 	rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->types, db->num_types, db);
880 	if (rc != SEPOL_OK) {
881 		cil_log(CIL_ERR, "Failed to expand type attribute to bitmap\n");
882 		ebitmap_destroy(attr->types);
883 		free(attr->types);
884 		attr->types = NULL;
885 	}
886 	return rc;
887 }
888 
__cil_type_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)889 static int __cil_type_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
890 {
891 	int rc = SEPOL_ERR;
892 	struct cil_tree_node *node = datum->nodes->head->data;
893 
894 	ebitmap_init(bitmap);
895 
896 	if (node->flavor == CIL_TYPEATTRIBUTE) {
897 		struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
898 		if (attr->types == NULL) {
899 			rc = __evaluate_type_expression(attr, db);
900 			if (rc != SEPOL_OK) goto exit;
901 		}
902 		ebitmap_union(bitmap, attr->types);
903 	} else if (node->flavor == CIL_TYPEALIAS) {
904 		struct cil_alias *alias = (struct cil_alias *)datum;
905 		struct cil_type *type = alias->actual;
906 		if (ebitmap_set_bit(bitmap, type->value, 1)) {
907 			cil_log(CIL_ERR, "Failed to set type bit\n");
908 			ebitmap_destroy(bitmap);
909 			goto exit;
910 		}
911 	} else {
912 		struct cil_type *type = (struct cil_type *)datum;
913 		if (ebitmap_set_bit(bitmap, type->value, 1)) {
914 			cil_log(CIL_ERR, "Failed to set type bit\n");
915 			ebitmap_destroy(bitmap);
916 			goto exit;
917 		}
918 	}
919 
920 	return SEPOL_OK;
921 
922 exit:
923 	return rc;
924 }
925 
__evaluate_user_expression(struct cil_userattribute * attr,struct cil_db * db)926 static int __evaluate_user_expression(struct cil_userattribute *attr, struct cil_db *db)
927 {
928 	int rc;
929 
930 	attr->users = cil_malloc(sizeof(*attr->users));
931 	rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->users, db->num_users, db);
932 	if (rc != SEPOL_OK) {
933 		cil_log(CIL_ERR, "Failed to expand user attribute to bitmap\n");
934 		ebitmap_destroy(attr->users);
935 		free(attr->users);
936 		attr->users = NULL;
937 	}
938 	return rc;
939 }
940 
__cil_user_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)941 static int __cil_user_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
942 {
943 	int rc = SEPOL_ERR;
944 	struct cil_tree_node *node = datum->nodes->head->data;
945 	struct cil_userattribute *attr = NULL;
946 	struct cil_user *user = NULL;
947 
948 	ebitmap_init(bitmap);
949 
950 	if (node->flavor == CIL_USERATTRIBUTE) {
951 		attr = (struct cil_userattribute *)datum;
952 		if (attr->users == NULL) {
953 			rc = __evaluate_user_expression(attr, db);
954 			if (rc != SEPOL_OK) {
955 				goto exit;
956 			}
957 		}
958 		ebitmap_union(bitmap, attr->users);
959 	} else {
960 		user = (struct cil_user *)datum;
961 		if (ebitmap_set_bit(bitmap, user->value, 1)) {
962 			cil_log(CIL_ERR, "Failed to set user bit\n");
963 			ebitmap_destroy(bitmap);
964 			goto exit;
965 		}
966 	}
967 
968 	return SEPOL_OK;
969 
970 exit:
971 	return rc;
972 }
973 
__evaluate_role_expression(struct cil_roleattribute * attr,struct cil_db * db)974 static int __evaluate_role_expression(struct cil_roleattribute *attr, struct cil_db *db)
975 {
976 	int rc;
977 
978 	attr->roles = cil_malloc(sizeof(*attr->roles));
979 	rc = __cil_expr_list_to_bitmap(attr->expr_list, attr->roles, db->num_roles, db);
980 	if (rc != SEPOL_OK) {
981 		cil_log(CIL_ERR, "Failed to expand role attribute to bitmap\n");
982 		ebitmap_destroy(attr->roles);
983 		free(attr->roles);
984 		attr->roles = NULL;
985 	}
986 	return rc;
987 }
988 
__cil_role_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)989 static int __cil_role_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
990 {
991 	int rc = SEPOL_ERR;
992 	struct cil_tree_node *node = datum->nodes->head->data;
993 
994 	ebitmap_init(bitmap);
995 
996 	if (node->flavor == CIL_ROLEATTRIBUTE) {
997 		struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
998 		if (attr->roles == NULL) {
999 			rc = __evaluate_role_expression(attr, db);
1000 			if (rc != SEPOL_OK) goto exit;
1001 		}
1002 		ebitmap_union(bitmap, attr->roles);
1003 	} else {
1004 		struct cil_role *role = (struct cil_role *)datum;
1005 		if (ebitmap_set_bit(bitmap, role->value, 1)) {
1006 			cil_log(CIL_ERR, "Failed to set role bit\n");
1007 			ebitmap_destroy(bitmap);
1008 			goto exit;
1009 		}
1010 	}
1011 
1012 	return SEPOL_OK;
1013 
1014 exit:
1015 	return rc;
1016 }
1017 
__evaluate_permissionx_expression(struct cil_permissionx * permx,struct cil_db * db)1018 static int __evaluate_permissionx_expression(struct cil_permissionx *permx, struct cil_db *db)
1019 {
1020 	int rc;
1021 
1022 	permx->perms = cil_malloc(sizeof(*permx->perms));
1023 	ebitmap_init(permx->perms);
1024 
1025 	rc = __cil_expr_to_bitmap(permx->expr_str, permx->perms, 0x10000, db); // max is one more than 0xFFFF
1026 	if (rc != SEPOL_OK) {
1027 		cil_log(CIL_ERR, "Failed to expand permissionx expression\n");
1028 		ebitmap_destroy(permx->perms);
1029 		free(permx->perms);
1030 		permx->perms = NULL;
1031 	}
1032 
1033 	return rc;
1034 }
1035 
__cil_permx_str_to_int(char * permx_str,uint16_t * val)1036 static int __cil_permx_str_to_int(char *permx_str, uint16_t *val)
1037 {
1038 	char *endptr = NULL;
1039 	long lval = strtol(permx_str, &endptr, 0);
1040 
1041 	if (*endptr != '\0') {
1042 		cil_log(CIL_ERR, "permissionx value %s not valid number\n", permx_str);
1043 		goto exit;
1044 	}
1045 	if (lval < 0x0000 || lval > 0xFFFF) {
1046 		cil_log(CIL_ERR, "permissionx value %s must be between 0x0000 and 0xFFFF\n", permx_str);
1047 		goto exit;
1048 	}
1049 
1050 	*val = (uint16_t)lval;
1051 
1052 	return SEPOL_OK;
1053 
1054 exit:
1055 	return SEPOL_ERR;
1056 }
1057 
__cil_permx_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1058 static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1059 {
1060 	int rc = SEPOL_ERR;
1061 	uint16_t val;
1062 
1063 	rc = __cil_permx_str_to_int((char*)datum, &val);
1064 	if (rc != SEPOL_OK) {
1065 		goto exit;
1066 	}
1067 
1068 	ebitmap_init(bitmap);
1069 	if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
1070 		cil_log(CIL_ERR, "Failed to set permissionx bit\n");
1071 		ebitmap_destroy(bitmap);
1072 		goto exit;
1073 	}
1074 
1075 	return SEPOL_OK;
1076 
1077 exit:
1078 	return rc;
1079 }
1080 
__cil_perm_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1081 static int __cil_perm_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, __attribute__((unused)) struct cil_db *db)
1082 {
1083 	struct cil_perm *perm = (struct cil_perm *)datum;
1084 	unsigned int value = perm->value;
1085 
1086 	ebitmap_init(bitmap);
1087 	if (ebitmap_set_bit(bitmap, value, 1)) {
1088 		cil_log(CIL_INFO, "Failed to set perm bit\n");
1089 		ebitmap_destroy(bitmap);
1090 		return SEPOL_ERR;
1091 	}
1092 
1093 	return SEPOL_OK;
1094 }
1095 
__evaluate_cat_expression(struct cil_cats * cats,struct cil_db * db)1096 static int __evaluate_cat_expression(struct cil_cats *cats, struct cil_db *db)
1097 {
1098 	int rc = SEPOL_ERR;
1099 	ebitmap_t bitmap;
1100 	struct cil_list *new;
1101 	struct cil_list_item *curr;
1102 
1103 	if (cats->evaluated == CIL_TRUE) {
1104 		return SEPOL_OK;
1105 	}
1106 
1107 	if (cil_verify_is_list(cats->datum_expr, CIL_CAT)) {
1108 		return SEPOL_OK;
1109 	}
1110 
1111 	ebitmap_init(&bitmap);
1112 	rc = __cil_expr_to_bitmap(cats->datum_expr, &bitmap, db->num_cats, db);
1113 	if (rc != SEPOL_OK) {
1114 		cil_log(CIL_ERR, "Failed to expand category expression to bitmap\n");
1115 		ebitmap_destroy(&bitmap);
1116 		goto exit;
1117 	}
1118 
1119 	cil_list_init(&new, CIL_CAT);
1120 
1121 	cil_list_for_each(curr, db->catorder) {
1122 		struct cil_cat *cat = curr->data;
1123 		if (ebitmap_get_bit(&bitmap, cat->value)) {
1124 			cil_list_append(new, CIL_DATUM, cat);
1125 		}
1126 	}
1127 
1128 	ebitmap_destroy(&bitmap);
1129 	cil_list_destroy(&cats->datum_expr, CIL_FALSE);
1130 	cats->datum_expr = new;
1131 
1132 	cats->evaluated = CIL_TRUE;
1133 
1134 	return SEPOL_OK;
1135 
1136 exit:
1137 	return rc;
1138 }
1139 
__cil_cat_to_bitmap(struct cil_symtab_datum * datum,ebitmap_t * bitmap,struct cil_db * db)1140 static int __cil_cat_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitmap, struct cil_db *db)
1141 {
1142 	int rc = SEPOL_ERR;
1143 	struct cil_tree_node *node = datum->nodes->head->data;
1144 
1145 	ebitmap_init(bitmap);
1146 
1147 	if (node->flavor == CIL_CATSET) {
1148 		struct cil_catset *catset = (struct cil_catset *)datum;
1149 		struct cil_list_item *curr;
1150 		if (catset->cats->evaluated == CIL_FALSE) {
1151 			rc = __evaluate_cat_expression(catset->cats, db);
1152 			if (rc != SEPOL_OK) goto exit;
1153 		}
1154 		for (curr = catset->cats->datum_expr->head; curr; curr = curr->next) {
1155 			struct cil_cat *cat = (struct cil_cat *)curr->data;
1156 			if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1157 				cil_log(CIL_ERR, "Failed to set cat bit\n");
1158 				ebitmap_destroy(bitmap);
1159 				goto exit;
1160 			}
1161 		}
1162 	} else if (node->flavor == CIL_CATALIAS) {
1163 		struct cil_alias *alias = (struct cil_alias *)datum;
1164 		struct cil_cat *cat = alias->actual;
1165 		if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1166 			cil_log(CIL_ERR, "Failed to set cat bit\n");
1167 			ebitmap_destroy(bitmap);
1168 			goto exit;
1169 		}
1170 	} else {
1171 		struct cil_cat *cat = (struct cil_cat *)datum;
1172 		if (ebitmap_set_bit(bitmap, cat->value, 1)) {
1173 			cil_log(CIL_ERR, "Failed to set cat bit\n");
1174 			ebitmap_destroy(bitmap);
1175 			goto exit;
1176 		}
1177 	}
1178 
1179 	return SEPOL_OK;
1180 
1181 exit:
1182 	return rc;
1183 }
1184 
__cil_cat_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)1185 static int __cil_cat_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1186 {
1187 	int rc = SEPOL_ERR;
1188 	struct cil_symtab_datum *d1 = i1->data;
1189 	struct cil_symtab_datum *d2 = i2->data;
1190 	struct cil_tree_node *n1 = d1->nodes->head->data;
1191 	struct cil_tree_node *n2 = d2->nodes->head->data;
1192 	struct cil_cat *c1 = (struct cil_cat *)d1;
1193 	struct cil_cat *c2 = (struct cil_cat *)d2;
1194 
1195 	if (n1->flavor == CIL_CATSET || n2->flavor == CIL_CATSET) {
1196 		cil_log(CIL_ERR, "Category sets cannot be used in a category range\n");
1197 		goto exit;
1198 	}
1199 
1200 	if (n1->flavor == CIL_CATALIAS) {
1201 		struct cil_alias *alias = (struct cil_alias *)d1;
1202 		c1 = alias->actual;
1203 	}
1204 
1205 	if (n2->flavor == CIL_CATALIAS) {
1206 		struct cil_alias *alias = (struct cil_alias *)d2;
1207 		c2 = alias->actual;
1208 	}
1209 
1210 	if (c1->value > c2->value) {
1211 		cil_log(CIL_ERR, "Invalid category range\n");
1212 		goto exit;
1213 	}
1214 
1215 	if (ebitmap_init_range(bitmap, c1->value, c2->value)) {
1216 		cil_log(CIL_ERR, "Failed to set cat bit\n");
1217 		ebitmap_destroy(bitmap);
1218 		goto exit;
1219 	}
1220 
1221 	return SEPOL_OK;
1222 
1223 exit:
1224 	return rc;
1225 }
1226 
__cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item * i1,struct cil_list_item * i2,ebitmap_t * bitmap)1227 static int __cil_permissionx_expr_range_to_bitmap_helper(struct cil_list_item *i1, struct cil_list_item *i2, ebitmap_t *bitmap)
1228 {
1229 	int rc = SEPOL_ERR;
1230 	char *p1 = i1->data;
1231 	char *p2 = i2->data;
1232 	uint16_t v1;
1233 	uint16_t v2;
1234 
1235 	rc = __cil_permx_str_to_int(p1, &v1);
1236 	if (rc != SEPOL_OK) {
1237 		goto exit;
1238 	}
1239 
1240 	rc = __cil_permx_str_to_int(p2, &v2);
1241 	if (rc != SEPOL_OK) {
1242 		goto exit;
1243 	}
1244 
1245 	if (ebitmap_init_range(bitmap, v1, v2)) {
1246 		cil_log(CIL_ERR, "Failed to set permissionx bits\n");
1247 		ebitmap_destroy(bitmap);
1248 		goto exit;
1249 	}
1250 
1251 	return SEPOL_OK;
1252 
1253 exit:
1254 	return rc;
1255 }
1256 
__cil_expr_to_bitmap_helper(struct cil_list_item * curr,enum cil_flavor flavor,ebitmap_t * bitmap,int max,struct cil_db * db)1257 static int __cil_expr_to_bitmap_helper(struct cil_list_item *curr, enum cil_flavor flavor, ebitmap_t *bitmap, int max, struct cil_db *db)
1258 {
1259 	int rc = SEPOL_ERR;
1260 
1261 	if (curr->flavor == CIL_DATUM) {
1262 		switch (flavor) {
1263 		case CIL_TYPE:
1264 			rc = __cil_type_to_bitmap(curr->data, bitmap, db);
1265 			break;
1266 		case CIL_ROLE:
1267 			rc = __cil_role_to_bitmap(curr->data, bitmap, db);
1268 			break;
1269 		case CIL_USER:
1270 			rc = __cil_user_to_bitmap(curr->data, bitmap, db);
1271 			break;
1272 		case CIL_PERM:
1273 			rc = __cil_perm_to_bitmap(curr->data, bitmap, db);
1274 			break;
1275 		case CIL_CAT:
1276 			rc = __cil_cat_to_bitmap(curr->data, bitmap, db);
1277 			break;
1278 		default:
1279 			rc = SEPOL_ERR;
1280 		}
1281 	} else if (curr->flavor == CIL_LIST) {
1282 		struct cil_list *l = curr->data;
1283 		ebitmap_init(bitmap);
1284 		rc = __cil_expr_to_bitmap(l, bitmap, max, db);
1285 		if (rc != SEPOL_OK) {
1286 			ebitmap_destroy(bitmap);
1287 		}
1288 	} else if (flavor == CIL_PERMISSIONX) {
1289 		// permissionx expressions aren't resolved into anything, so curr->flavor
1290 		// is just a CIL_STRING, not a CIL_DATUM, so just check on flavor for those
1291 		rc = __cil_permx_to_bitmap(curr->data, bitmap, db);
1292 	}
1293 
1294 	return rc;
1295 }
1296 
__cil_expr_to_bitmap(struct cil_list * expr,ebitmap_t * out,int max,struct cil_db * db)1297 static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max, struct cil_db *db)
1298 {
1299 	int rc = SEPOL_ERR;
1300 	struct cil_list_item *curr;
1301 	enum cil_flavor flavor;
1302 	ebitmap_t tmp, b1, b2;
1303 
1304 	if (expr == NULL || expr->head == NULL) {
1305 		return SEPOL_OK;
1306 	}
1307 
1308 	curr = expr->head;
1309 	flavor = expr->flavor;
1310 
1311 	if (curr->flavor == CIL_OP) {
1312 		enum cil_flavor op = (enum cil_flavor)(uintptr_t)curr->data;
1313 
1314 		if (op == CIL_ALL) {
1315 			rc = ebitmap_init_range(&tmp, 0, max - 1);
1316 			if (rc != SEPOL_OK) {
1317 				cil_log(CIL_INFO, "Failed to expand 'all' operator\n");
1318 				ebitmap_destroy(&tmp);
1319 				goto exit;
1320 			}
1321 		} else if (op == CIL_RANGE) {
1322 			if (flavor == CIL_CAT) {
1323 				rc = __cil_cat_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1324 				if (rc != SEPOL_OK) {
1325 					cil_log(CIL_INFO, "Failed to expand category range\n");
1326 					goto exit;
1327 				}
1328 			} else if (flavor == CIL_PERMISSIONX) {
1329 				rc = __cil_permissionx_expr_range_to_bitmap_helper(curr->next, curr->next->next, &tmp);
1330 				if (rc != SEPOL_OK) {
1331 					cil_log(CIL_INFO, "Failed to expand category range\n");
1332 					goto exit;
1333 				}
1334 			} else {
1335 				cil_log(CIL_INFO, "Range operation only supported for categories permissionx\n");
1336 				rc = SEPOL_ERR;
1337 				goto exit;
1338 			}
1339 		} else {
1340 			rc = __cil_expr_to_bitmap_helper(curr->next, flavor, &b1, max, db);
1341 			if (rc != SEPOL_OK) {
1342 				cil_log(CIL_INFO, "Failed to get first operand bitmap\n");
1343 				goto exit;
1344 			}
1345 
1346 			if (op == CIL_NOT) {
1347 				rc = ebitmap_not(&tmp, &b1, max);
1348 				ebitmap_destroy(&b1);
1349 				if (rc != SEPOL_OK) {
1350 					cil_log(CIL_INFO, "Failed to NOT bitmap\n");
1351 					ebitmap_destroy(&tmp);
1352 					goto exit;
1353 				}
1354 			} else {
1355 				rc = __cil_expr_to_bitmap_helper(curr->next->next, flavor, &b2, max, db);
1356 				if (rc != SEPOL_OK) {
1357 					cil_log(CIL_INFO, "Failed to get second operand bitmap\n");
1358 					ebitmap_destroy(&b1);
1359 					goto exit;
1360 				}
1361 
1362 				if (op == CIL_OR) {
1363 					rc = ebitmap_or(&tmp, &b1, &b2);
1364 				} else if (op == CIL_AND) {
1365 					rc = ebitmap_and(&tmp, &b1, &b2);
1366 				} else if (op == CIL_XOR) {
1367 					rc = ebitmap_xor(&tmp, &b1, &b2);
1368 				} else {
1369 					rc = SEPOL_ERR;
1370 				}
1371 				ebitmap_destroy(&b1);
1372 				ebitmap_destroy(&b2);
1373 				if (rc != SEPOL_OK) {
1374 					cil_log(CIL_INFO, "Failed to apply operator to bitmaps\n");
1375 					ebitmap_destroy(&tmp);
1376 					goto exit;
1377 				}
1378 			}
1379 		}
1380 	} else {
1381 		ebitmap_init(&tmp);
1382 		for (;curr; curr = curr->next) {
1383 			rc = __cil_expr_to_bitmap_helper(curr, flavor, &b2, max, db);
1384 			if (rc != SEPOL_OK) {
1385 				cil_log(CIL_INFO, "Failed to get operand in list\n");
1386 				ebitmap_destroy(&tmp);
1387 				goto exit;
1388 			}
1389 			b1 = tmp;
1390 			rc = ebitmap_or(&tmp, &b1, &b2);
1391 			ebitmap_destroy(&b1);
1392 			ebitmap_destroy(&b2);
1393 			if (rc != SEPOL_OK) {
1394 				cil_log(CIL_INFO, "Failed to OR operands in list\n");
1395 				ebitmap_destroy(&tmp);
1396 				goto exit;
1397 			}
1398 
1399 		}
1400 	}
1401 
1402 	ebitmap_union(out, &tmp);
1403 	ebitmap_destroy(&tmp);
1404 
1405 	return SEPOL_OK;
1406 
1407 exit:
1408 	return rc;
1409 }
1410 
__cil_expr_list_to_bitmap(struct cil_list * expr_list,ebitmap_t * out,int max,struct cil_db * db)1411 static int __cil_expr_list_to_bitmap(struct cil_list *expr_list, ebitmap_t *out, int max, struct cil_db *db)
1412 {
1413 	int rc = SEPOL_ERR;
1414 	struct cil_list_item *expr;
1415 
1416 	ebitmap_init(out);
1417 
1418 	if (expr_list == NULL) {
1419 		return SEPOL_OK;
1420 	}
1421 
1422 	cil_list_for_each(expr, expr_list) {
1423 		ebitmap_t bitmap;
1424 		struct cil_list *l = (struct cil_list *)expr->data;
1425 		ebitmap_init(&bitmap);
1426 		rc = __cil_expr_to_bitmap(l, &bitmap, max, db);
1427 		if (rc != SEPOL_OK) {
1428 			cil_log(CIL_INFO, "Failed to expand expression list to bitmap\n");
1429 			ebitmap_destroy(&bitmap);
1430 			goto exit;
1431 		}
1432 		ebitmap_union(out, &bitmap);
1433 		ebitmap_destroy(&bitmap);
1434 	}
1435 
1436 	return SEPOL_OK;
1437 
1438 exit:
1439 	return SEPOL_ERR;
1440 }
1441 
cil_typeattribute_used(struct cil_typeattribute * attr,struct cil_db * db)1442 static int cil_typeattribute_used(struct cil_typeattribute *attr, struct cil_db *db)
1443 {
1444 	if (!attr->used) {
1445 		return CIL_FALSE;
1446 	}
1447 
1448 	if (attr->used & CIL_ATTR_EXPAND_FALSE) {
1449 		return CIL_TRUE;
1450 	}
1451 
1452 	if (attr->used & CIL_ATTR_EXPAND_TRUE) {
1453 		return CIL_FALSE;
1454 	}
1455 
1456 	if (attr->used & CIL_ATTR_CONSTRAINT) {
1457 		return CIL_TRUE;
1458 	}
1459 
1460 	if (db->attrs_expand_generated || attr->used == CIL_ATTR_NEVERALLOW) {
1461 		if (strcmp(DATUM(attr)->name, GEN_REQUIRE_ATTR) == 0) {
1462 			return CIL_FALSE;
1463 		} else if (strstr(DATUM(attr)->name, TYPEATTR_INFIX) != NULL) {
1464 			return CIL_FALSE;
1465 		}
1466 
1467 		if (attr->used == CIL_ATTR_NEVERALLOW) {
1468 			return CIL_TRUE;
1469 		}
1470 	}
1471 
1472 	if (attr->used == CIL_ATTR_AVRULE) {
1473 		if (ebitmap_cardinality(attr->types) < db->attrs_expand_size) {
1474 			return CIL_FALSE;
1475 		}
1476 	}
1477 
1478 	return CIL_TRUE;
1479 }
1480 
__mark_neverallow_attrs(struct cil_list * expr_list)1481 static void __mark_neverallow_attrs(struct cil_list *expr_list)
1482 {
1483 	struct cil_list_item *curr;
1484 
1485 	if (!expr_list) {
1486 		return;
1487 	}
1488 
1489 	cil_list_for_each(curr, expr_list) {
1490 		if (curr->flavor == CIL_DATUM) {
1491 			if (FLAVOR(curr->data) == CIL_TYPEATTRIBUTE) {
1492 				struct cil_typeattribute *attr = curr->data;
1493 				if (strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1494 					__mark_neverallow_attrs(attr->expr_list);
1495 				} else {
1496 					attr->used |= CIL_ATTR_NEVERALLOW;
1497 				}
1498 			}
1499 		} else if (curr->flavor == CIL_LIST) {
1500 			 __mark_neverallow_attrs(curr->data);
1501 		}
1502 	}
1503 }
1504 
__cil_post_db_neverallow_attr_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1505 static int __cil_post_db_neverallow_attr_helper(struct cil_tree_node *node, uint32_t *finished, __attribute__((unused)) void *extra_args)
1506 {
1507 	switch (node->flavor) {
1508 	case CIL_BLOCK: {
1509 		struct cil_block *blk = node->data;
1510 		if (blk->is_abstract == CIL_TRUE) {
1511 			*finished = CIL_TREE_SKIP_HEAD;
1512 		}
1513 		break;
1514 	}
1515 	case CIL_MACRO: {
1516 		*finished = CIL_TREE_SKIP_HEAD;
1517 		break;
1518 	}
1519 	case CIL_TYPEATTRIBUTE: {
1520 		struct cil_typeattribute *attr = node->data;
1521 		if ((attr->used & CIL_ATTR_NEVERALLOW) &&
1522 		    strstr(DATUM(attr)->name, TYPEATTR_INFIX)) {
1523 			__mark_neverallow_attrs(attr->expr_list);
1524 		}
1525 		break;
1526 	}
1527 	default:
1528 		break;
1529 	}
1530 
1531 	return SEPOL_OK;
1532 }
1533 
__cil_post_db_attr_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1534 static int __cil_post_db_attr_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1535 {
1536 	int rc = SEPOL_ERR;
1537 	struct cil_db *db = extra_args;
1538 
1539 	switch (node->flavor) {
1540 	case CIL_BLOCK: {
1541 		struct cil_block *blk = node->data;
1542 		if (blk->is_abstract == CIL_TRUE) {
1543 			*finished = CIL_TREE_SKIP_HEAD;
1544 		}
1545 		break;
1546 	}
1547 	case CIL_MACRO: {
1548 		*finished = CIL_TREE_SKIP_HEAD;
1549 		break;
1550 	}
1551 	case CIL_TYPEATTRIBUTE: {
1552 		struct cil_typeattribute *attr = node->data;
1553 		if (attr->types == NULL) {
1554 			rc = __evaluate_type_expression(attr, db);
1555 			if (rc != SEPOL_OK) goto exit;
1556 		}
1557 		attr->keep = cil_typeattribute_used(attr, db);
1558 		break;
1559 	}
1560 	case CIL_ROLEATTRIBUTE: {
1561 		struct cil_roleattribute *attr = node->data;
1562 		if (attr->roles == NULL) {
1563 			rc = __evaluate_role_expression(attr, db);
1564 			if (rc != SEPOL_OK) goto exit;
1565 		}
1566 		break;
1567 	}
1568 	case CIL_AVRULEX: {
1569 		struct cil_avrule *rule = node->data;
1570 		if (rule->perms.x.permx_str == NULL) {
1571 			rc = __evaluate_permissionx_expression(rule->perms.x.permx, db);
1572 			if (rc != SEPOL_OK) goto exit;
1573 		}
1574 		break;
1575 	}
1576 	case CIL_PERMISSIONX: {
1577 		struct cil_permissionx *permx = node->data;
1578 		rc = __evaluate_permissionx_expression(permx, db);
1579 		if (rc != SEPOL_OK) goto exit;
1580 		break;
1581 	}
1582 	case CIL_USERATTRIBUTE: {
1583 		struct cil_userattribute *attr = node->data;
1584 		if (attr->users == NULL) {
1585 			rc = __evaluate_user_expression(attr, db);
1586 			if (rc != SEPOL_OK) {
1587 				goto exit;
1588 			}
1589 		}
1590 		break;
1591 	}
1592 	default:
1593 		break;
1594 	}
1595 
1596 	return SEPOL_OK;
1597 
1598 exit:
1599 	return rc;
1600 }
1601 
__cil_role_assign_types(struct cil_role * role,struct cil_symtab_datum * datum)1602 static int __cil_role_assign_types(struct cil_role *role, struct cil_symtab_datum *datum)
1603 {
1604 	struct cil_tree_node *node = datum->nodes->head->data;
1605 
1606 	if (role->types == NULL) {
1607 		role->types = cil_malloc(sizeof(*role->types));
1608 		ebitmap_init(role->types);
1609 	}
1610 
1611 	if (node->flavor == CIL_TYPE) {
1612 		struct cil_type *type = (struct cil_type *)datum;
1613 		if (ebitmap_set_bit(role->types, type->value, 1)) {
1614 			cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1615 			goto exit;
1616 		}
1617 	} else if (node->flavor == CIL_TYPEALIAS) {
1618 		struct cil_alias *alias = (struct cil_alias *)datum;
1619 		struct cil_type *type = alias->actual;
1620 		if (ebitmap_set_bit(role->types, type->value, 1)) {
1621 			cil_log(CIL_INFO, "Failed to set bit in role types bitmap\n");
1622 			goto exit;
1623 		}
1624 	} else if (node->flavor == CIL_TYPEATTRIBUTE) {
1625 		struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
1626 		ebitmap_union(role->types, attr->types);
1627 	}
1628 
1629 	return SEPOL_OK;
1630 
1631 exit:
1632 	return SEPOL_ERR;
1633 }
1634 
__cil_post_db_roletype_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1635 static int __cil_post_db_roletype_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1636 {
1637 	int rc = SEPOL_ERR;
1638 	struct cil_db *db = extra_args;
1639 
1640 	switch (node->flavor) {
1641 	case CIL_BLOCK: {
1642 		struct cil_block *blk = node->data;
1643 		if (blk->is_abstract == CIL_TRUE) {
1644 			*finished = CIL_TREE_SKIP_HEAD;
1645 		}
1646 		break;
1647 	}
1648 	case CIL_MACRO: {
1649 		*finished = CIL_TREE_SKIP_HEAD;
1650 		break;
1651 	}
1652 	case CIL_ROLETYPE: {
1653 		struct cil_roletype *roletype = node->data;
1654 		struct cil_symtab_datum *role_datum = roletype->role;
1655 		struct cil_symtab_datum *type_datum = roletype->type;
1656 		struct cil_tree_node *role_node = role_datum->nodes->head->data;
1657 
1658 		if (role_node->flavor == CIL_ROLEATTRIBUTE) {
1659 			struct cil_roleattribute *attr = roletype->role;
1660 			ebitmap_node_t *rnode;
1661 			unsigned int i;
1662 
1663 			ebitmap_for_each_positive_bit(attr->roles, rnode, i) {
1664 				struct cil_role *role = NULL;
1665 
1666 				role = db->val_to_role[i];
1667 
1668 				rc = __cil_role_assign_types(role, type_datum);
1669 				if (rc != SEPOL_OK) {
1670 					goto exit;
1671 				}
1672 			}
1673 		} else {
1674 			struct cil_role *role = roletype->role;
1675 
1676 			rc = __cil_role_assign_types(role, type_datum);
1677 			if (rc != SEPOL_OK) {
1678 				goto exit;
1679 			}
1680 		}
1681 		break;
1682 	}
1683 	default:
1684 		break;
1685 	}
1686 
1687 	return SEPOL_OK;
1688 exit:
1689 	cil_log(CIL_INFO, "cil_post_db_roletype_helper failed\n");
1690 	return rc;
1691 }
1692 
__cil_user_assign_roles(struct cil_user * user,struct cil_symtab_datum * datum)1693 static int __cil_user_assign_roles(struct cil_user *user, struct cil_symtab_datum *datum)
1694 {
1695 	struct cil_tree_node *node = datum->nodes->head->data;
1696 	struct cil_role *role = NULL;
1697 	struct cil_roleattribute *attr = NULL;
1698 
1699 	if (user->roles == NULL) {
1700 		user->roles = cil_malloc(sizeof(*user->roles));
1701 		ebitmap_init(user->roles);
1702 	}
1703 
1704 	if (node->flavor == CIL_ROLE) {
1705 		role = (struct cil_role *)datum;
1706 		if (ebitmap_set_bit(user->roles, role->value, 1)) {
1707 			cil_log(CIL_INFO, "Failed to set bit in user roles bitmap\n");
1708 			goto exit;
1709 		}
1710 	} else if (node->flavor == CIL_ROLEATTRIBUTE) {
1711 		attr = (struct cil_roleattribute *)datum;
1712 		ebitmap_union(user->roles, attr->roles);
1713 	}
1714 
1715 	return SEPOL_OK;
1716 
1717 exit:
1718 	return SEPOL_ERR;
1719 }
1720 
__cil_post_db_userrole_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1721 static int __cil_post_db_userrole_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1722 {
1723 	int rc = SEPOL_ERR;
1724 	struct cil_db *db = extra_args;
1725 	struct cil_block *blk = NULL;
1726 	struct cil_userrole *userrole = NULL;
1727 	struct cil_symtab_datum *user_datum = NULL;
1728 	struct cil_symtab_datum *role_datum = NULL;
1729 	struct cil_tree_node *user_node = NULL;
1730 	struct cil_userattribute *u_attr = NULL;
1731 	unsigned int i;
1732 	struct cil_user *user = NULL;
1733 	ebitmap_node_t *unode = NULL;
1734 
1735 	switch (node->flavor) {
1736 	case CIL_BLOCK: {
1737 		blk = node->data;
1738 		if (blk->is_abstract == CIL_TRUE) {
1739 			*finished = CIL_TREE_SKIP_HEAD;
1740 		}
1741 		break;
1742 	}
1743 	case CIL_MACRO: {
1744 		*finished = CIL_TREE_SKIP_HEAD;
1745 		break;
1746 	}
1747 	case CIL_USERROLE: {
1748 		userrole = node->data;
1749 		user_datum = userrole->user;
1750 		role_datum = userrole->role;
1751 		user_node = user_datum->nodes->head->data;
1752 
1753 		if (user_node->flavor == CIL_USERATTRIBUTE) {
1754 			u_attr = userrole->user;
1755 
1756 			ebitmap_for_each_positive_bit(u_attr->users, unode, i) {
1757 				user = db->val_to_user[i];
1758 
1759 				rc = __cil_user_assign_roles(user, role_datum);
1760 				if (rc != SEPOL_OK) {
1761 					goto exit;
1762 				}
1763 			}
1764 		} else {
1765 			user = userrole->user;
1766 
1767 			rc = __cil_user_assign_roles(user, role_datum);
1768 			if (rc != SEPOL_OK) {
1769 				goto exit;
1770 			}
1771 		}
1772 
1773 		break;
1774 	}
1775 	default:
1776 		break;
1777 	}
1778 
1779 	return SEPOL_OK;
1780 exit:
1781 	cil_log(CIL_INFO, "cil_post_db_userrole_helper failed\n");
1782 	return rc;
1783 }
1784 
__evaluate_level_expression(struct cil_level * level,struct cil_db * db)1785 static int __evaluate_level_expression(struct cil_level *level, struct cil_db *db)
1786 {
1787 	if (level->cats != NULL) {
1788 		return __evaluate_cat_expression(level->cats, db);
1789 	}
1790 
1791 	return SEPOL_OK;
1792 }
1793 
__evaluate_levelrange_expression(struct cil_levelrange * levelrange,struct cil_db * db)1794 static int __evaluate_levelrange_expression(struct cil_levelrange *levelrange, struct cil_db *db)
1795 {
1796 	int rc = SEPOL_OK;
1797 
1798 	if (levelrange->low != NULL && levelrange->low->cats != NULL) {
1799 		rc =  __evaluate_cat_expression(levelrange->low->cats, db);
1800 		if (rc != SEPOL_OK) {
1801 			goto exit;
1802 		}
1803 	}
1804 	if (levelrange->high != NULL && levelrange->high->cats != NULL) {
1805 		rc = __evaluate_cat_expression(levelrange->high->cats, db);
1806 		if (rc != SEPOL_OK) {
1807 			goto exit;
1808 		}
1809 	}
1810 
1811 exit:
1812 	return rc;
1813 }
1814 
__cil_post_db_cat_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)1815 static int __cil_post_db_cat_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1816 {
1817 	int rc = SEPOL_ERR;
1818 	struct cil_db *db = extra_args;
1819 
1820 	switch (node->flavor) {
1821 	case CIL_BLOCK: {
1822 		struct cil_block *blk = node->data;
1823 		if (blk->is_abstract == CIL_TRUE) {
1824 			*finished = CIL_TREE_SKIP_HEAD;
1825 		}
1826 		break;
1827 	}
1828 	case CIL_MACRO: {
1829 		*finished = CIL_TREE_SKIP_HEAD;
1830 		break;
1831 	}
1832 	case CIL_CATSET: {
1833 		struct cil_catset *catset = node->data;
1834 		rc = __evaluate_cat_expression(catset->cats, db);
1835 		if (rc != SEPOL_OK) {
1836 			goto exit;
1837 		}
1838 		break;
1839 	}
1840 	case CIL_SENSCAT: {
1841 		struct cil_senscat *senscat = node->data;
1842 		rc = __evaluate_cat_expression(senscat->cats, db);
1843 		if (rc != SEPOL_OK) {
1844 			goto exit;
1845 		}
1846 		break;
1847 	}
1848 	case CIL_LEVEL: {
1849 		rc = __evaluate_level_expression(node->data, db);
1850 		if (rc != SEPOL_OK) {
1851 			goto exit;
1852 		}
1853 		break;
1854 	}
1855 	case CIL_LEVELRANGE: {
1856 		rc = __evaluate_levelrange_expression(node->data, db);
1857 		if (rc != SEPOL_OK) {
1858 			goto exit;
1859 		}
1860 		break;
1861 	}
1862 	case CIL_USER: {
1863 		struct cil_user *user = node->data;
1864 		rc = __evaluate_level_expression(user->dftlevel, db);
1865 		if (rc != SEPOL_OK) {
1866 			goto exit;
1867 		}
1868 		rc = __evaluate_levelrange_expression(user->range, db);
1869 		if (rc != SEPOL_OK) {
1870 			goto exit;
1871 		}
1872 		break;
1873 	}
1874 	case CIL_SELINUXUSERDEFAULT:
1875 	case CIL_SELINUXUSER: {
1876 		struct cil_selinuxuser *selinuxuser = node->data;
1877 		rc = __evaluate_levelrange_expression(selinuxuser->range, db);
1878 		if (rc != SEPOL_OK) {
1879 			goto exit;
1880 		}
1881 		break;
1882 	}
1883 	case CIL_RANGETRANSITION: {
1884 		struct cil_rangetransition *rangetrans = node->data;
1885 		rc = __evaluate_levelrange_expression(rangetrans->range, db);
1886 		if (rc != SEPOL_OK) {
1887 			goto exit;
1888 		}
1889 		break;
1890 	}
1891 	case CIL_CONTEXT: {
1892 		struct cil_context *context = node->data;
1893 		rc = __evaluate_levelrange_expression(context->range, db);
1894 		if (rc != SEPOL_OK) {
1895 			goto exit;
1896 		}
1897 		break;
1898 	}
1899 	case CIL_SIDCONTEXT: {
1900 		struct cil_sidcontext *sidcontext = node->data;
1901 		rc = __evaluate_levelrange_expression(sidcontext->context->range, db);
1902 		if (rc != SEPOL_OK) {
1903 			goto exit;
1904 		}
1905 		break;
1906 	}
1907 	case CIL_FILECON: {
1908 		struct cil_filecon *filecon = node->data;
1909 		if (filecon->context) {
1910 			rc = __evaluate_levelrange_expression(filecon->context->range, db);
1911 			if (rc != SEPOL_OK) {
1912 				goto exit;
1913 			}
1914 		}
1915 		break;
1916 	}
1917 	case CIL_IBPKEYCON: {
1918 		struct cil_ibpkeycon *ibpkeycon = node->data;
1919 
1920 		rc = __evaluate_levelrange_expression(ibpkeycon->context->range, db);
1921 		if (rc != SEPOL_OK)
1922 			goto exit;
1923 		break;
1924 	}
1925 	case CIL_IBENDPORTCON: {
1926 		struct cil_ibendportcon *ibendportcon = node->data;
1927 
1928 		rc = __evaluate_levelrange_expression(ibendportcon->context->range, db);
1929 		if (rc != SEPOL_OK)
1930 			goto exit;
1931 		break;
1932 	}
1933 	case CIL_PORTCON: {
1934 		struct cil_portcon *portcon = node->data;
1935 		rc = __evaluate_levelrange_expression(portcon->context->range, db);
1936 		if (rc != SEPOL_OK) {
1937 			goto exit;
1938 		}
1939 		break;
1940 	}
1941 	case CIL_NODECON: {
1942 		struct cil_nodecon *nodecon = node->data;
1943 		rc = __evaluate_levelrange_expression(nodecon->context->range, db);
1944 		if (rc != SEPOL_OK) {
1945 			goto exit;
1946 		}
1947 		break;
1948 	}
1949 	case CIL_GENFSCON: {
1950 		struct cil_genfscon *genfscon = node->data;
1951 		rc = __evaluate_levelrange_expression(genfscon->context->range, db);
1952 		if (rc != SEPOL_OK) {
1953 			goto exit;
1954 		}
1955 		break;
1956 	}
1957 	case CIL_NETIFCON: {
1958 		struct cil_netifcon *netifcon = node->data;
1959 		rc = __evaluate_levelrange_expression(netifcon->if_context->range, db);
1960 		if (rc != SEPOL_OK) {
1961 			goto exit;
1962 		}
1963 		rc = __evaluate_levelrange_expression(netifcon->packet_context->range, db);
1964 		if (rc != SEPOL_OK) {
1965 			goto exit;
1966 		}
1967 		break;
1968 	}
1969 	case CIL_PIRQCON: {
1970 		struct cil_pirqcon *pirqcon = node->data;
1971 		rc = __evaluate_levelrange_expression(pirqcon->context->range, db);
1972 		if (rc != SEPOL_OK) {
1973 			goto exit;
1974 		}
1975 		break;
1976 	}
1977 	case CIL_IOMEMCON: {
1978 		struct cil_iomemcon *iomemcon = node->data;
1979 		rc = __evaluate_levelrange_expression(iomemcon->context->range, db);
1980 		if (rc != SEPOL_OK) {
1981 			goto exit;
1982 		}
1983 		break;
1984 	}
1985 	case CIL_IOPORTCON: {
1986 		struct cil_ioportcon *ioportcon = node->data;
1987 		rc = __evaluate_levelrange_expression(ioportcon->context->range, db);
1988 		if (rc != SEPOL_OK) {
1989 			goto exit;
1990 		}
1991 		break;
1992 	}
1993 	case CIL_PCIDEVICECON: {
1994 		struct cil_pcidevicecon *pcidevicecon = node->data;
1995 		rc = __evaluate_levelrange_expression(pcidevicecon->context->range, db);
1996 		if (rc != SEPOL_OK) {
1997 			goto exit;
1998 		}
1999 		break;
2000 	}
2001 	case CIL_DEVICETREECON: {
2002 		struct cil_devicetreecon *devicetreecon = node->data;
2003 		rc = __evaluate_levelrange_expression(devicetreecon->context->range, db);
2004 		if (rc != SEPOL_OK) {
2005 			goto exit;
2006 		}
2007 		break;
2008 	}
2009 	case CIL_FSUSE: {
2010 		struct cil_fsuse *fsuse = node->data;
2011 		rc = __evaluate_levelrange_expression(fsuse->context->range, db);
2012 		if (rc != SEPOL_OK) {
2013 			goto exit;
2014 		}
2015 		break;
2016 	}
2017 	default:
2018 		break;
2019 	}
2020 
2021 	return SEPOL_OK;
2022 
2023 exit:
2024 	return rc;
2025 }
2026 
2027 struct perm_to_list {
2028 	enum cil_flavor flavor;
2029 	ebitmap_t *perms;
2030 	struct cil_list *new_list;
2031 };
2032 
__perm_bits_to_list(hashtab_key_t k,hashtab_datum_t d,void * args)2033 static int __perm_bits_to_list(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2034 {
2035 	struct perm_to_list *perm_args = (struct perm_to_list *)args;
2036 	ebitmap_t *perms = perm_args->perms;
2037 	struct cil_list *new_list = perm_args->new_list;
2038 	struct cil_perm *perm = (struct cil_perm *)d;
2039 	unsigned int value = perm->value;
2040 
2041 	if (!ebitmap_get_bit(perms, value)) {
2042 		return SEPOL_OK;
2043 	}
2044 
2045 	cil_list_append(new_list, CIL_DATUM, d);
2046 
2047 	return SEPOL_OK;
2048 }
2049 
__evaluate_perm_expression(struct cil_list * perms,enum cil_flavor flavor,symtab_t * class_symtab,symtab_t * common_symtab,unsigned int num_perms,struct cil_list ** new_list,struct cil_db * db)2050 static int __evaluate_perm_expression(struct cil_list *perms, enum cil_flavor flavor, symtab_t *class_symtab, symtab_t *common_symtab, unsigned int num_perms, struct cil_list **new_list, struct cil_db *db)
2051 {
2052 	int rc = SEPOL_ERR;
2053 	struct perm_to_list args;
2054 	ebitmap_t bitmap;
2055 
2056 	if (cil_verify_is_list(perms, CIL_PERM)) {
2057 		return SEPOL_OK;
2058 	}
2059 
2060 	ebitmap_init(&bitmap);
2061 	rc = __cil_expr_to_bitmap(perms, &bitmap, num_perms, db);
2062 	if (rc != SEPOL_OK) {
2063 		ebitmap_destroy(&bitmap);
2064 		goto exit;
2065 	}
2066 
2067 	cil_list_init(new_list, flavor);
2068 
2069 	args.flavor = flavor;
2070 	args.perms = &bitmap;
2071 	args.new_list = *new_list;
2072 
2073 	cil_symtab_map(class_symtab, __perm_bits_to_list, &args);
2074 
2075 	if (common_symtab != NULL) {
2076 		cil_symtab_map(common_symtab, __perm_bits_to_list, &args);
2077 	}
2078 
2079 	ebitmap_destroy(&bitmap);
2080 	return SEPOL_OK;
2081 
2082 exit:
2083 	return rc;
2084 }
2085 
__evaluate_classperms(struct cil_classperms * cp,struct cil_db * db)2086 static int __evaluate_classperms(struct cil_classperms *cp, struct cil_db *db)
2087 {
2088 	int rc = SEPOL_ERR;
2089 	struct cil_class *class = cp->class;
2090 	struct cil_class *common = class->common;
2091 	symtab_t *common_symtab = NULL;
2092 	struct cil_list *new_list = NULL;
2093 
2094 	if (common) {
2095 		common_symtab = &common->perms;
2096 	}
2097 
2098 	rc = __evaluate_perm_expression(cp->perms, CIL_PERM, &class->perms, common_symtab, class->num_perms, &new_list, db);
2099 	if (rc != SEPOL_OK) {
2100 		goto exit;
2101 	}
2102 
2103 	if (new_list == NULL) {
2104 		return SEPOL_OK;
2105 	}
2106 
2107 	cil_list_destroy(&cp->perms, CIL_FALSE);
2108 
2109 	cp->perms = new_list;
2110 
2111 	return SEPOL_OK;
2112 
2113 exit:
2114 	return rc;
2115 }
2116 
__evaluate_classperms_list(struct cil_list * classperms,struct cil_db * db)2117 static int __evaluate_classperms_list(struct cil_list *classperms, struct cil_db *db)
2118 {
2119 	int rc = SEPOL_ERR;
2120 	struct cil_list_item *curr;
2121 
2122 	cil_list_for_each(curr, classperms) {
2123 		if (curr->flavor == CIL_CLASSPERMS) {
2124 			struct cil_classperms *cp = curr->data;
2125 			if (FLAVOR(cp->class) == CIL_CLASS) {
2126 				rc = __evaluate_classperms(cp, db);
2127 				if (rc != SEPOL_OK) {
2128 					goto exit;
2129 				}
2130 			} else { /* MAP */
2131 				struct cil_list_item *i = NULL;
2132 				rc = __evaluate_classperms(cp, db);
2133 				if (rc != SEPOL_OK) {
2134 					goto exit;
2135 				}
2136 				cil_list_for_each(i, cp->perms) {
2137 					struct cil_perm *cmp = i->data;
2138 					rc = __evaluate_classperms_list(cmp->classperms, db);
2139 					if (rc != SEPOL_OK) {
2140 						goto exit;
2141 					}
2142 				}
2143 			}
2144 		} else { /* SET */
2145 			struct cil_classperms_set *cp_set = curr->data;
2146 			struct cil_classpermission *cp = cp_set->set;
2147 			rc = __evaluate_classperms_list(cp->classperms, db);
2148 			if (rc != SEPOL_OK) {
2149 				goto exit;
2150 			}
2151 		}
2152 	}
2153 
2154 	return SEPOL_OK;
2155 
2156 exit:
2157 	return rc;
2158 }
2159 
2160 struct class_map_args {
2161 	struct cil_db *db;
2162 	int rc;
2163 };
2164 
__evaluate_map_perm_classperms(hashtab_key_t k,hashtab_datum_t d,void * args)2165 static int __evaluate_map_perm_classperms(__attribute__((unused)) hashtab_key_t k, hashtab_datum_t d, void *args)
2166 {
2167 	struct class_map_args *map_args = args;
2168 	struct cil_perm *cmp = (struct cil_perm *)d;
2169 
2170 	int rc = __evaluate_classperms_list(cmp->classperms, map_args->db);
2171 
2172 	if (rc != SEPOL_OK) {
2173 		map_args->rc = rc;
2174 	}
2175 
2176 	return SEPOL_OK;
2177 }
2178 
__evaluate_map_class(struct cil_class * mc,struct cil_db * db)2179 static int __evaluate_map_class(struct cil_class *mc, struct cil_db *db)
2180 {
2181 	struct class_map_args map_args;
2182 
2183 	map_args.db = db;
2184 	map_args.rc = SEPOL_OK;
2185 	cil_symtab_map(&mc->perms, __evaluate_map_perm_classperms, &map_args);
2186 
2187 	return map_args.rc;
2188 }
2189 
__cil_post_db_classperms_helper(struct cil_tree_node * node,uint32_t * finished,void * extra_args)2190 static int __cil_post_db_classperms_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2191 {
2192 	int rc = SEPOL_ERR;
2193 	struct cil_db *db = extra_args;
2194 
2195 	switch (node->flavor) {
2196 	case CIL_BLOCK: {
2197 		struct cil_block *blk = node->data;
2198 		if (blk->is_abstract == CIL_TRUE) {
2199 			*finished = CIL_TREE_SKIP_HEAD;
2200 		}
2201 		break;
2202 	}
2203 	case CIL_MACRO:
2204 		*finished = CIL_TREE_SKIP_HEAD;
2205 		break;
2206 	case CIL_MAP_CLASS: {
2207 		rc = __evaluate_map_class(node->data, db);
2208 		if (rc != SEPOL_OK) {
2209 			goto exit;
2210 		}
2211 		break;
2212 	}
2213 	case CIL_CLASSPERMISSION: {
2214 		struct cil_classpermission *cp = node->data;
2215 		rc = __evaluate_classperms_list(cp->classperms, db);
2216 		if (rc != SEPOL_OK) {
2217 			goto exit;
2218 		}
2219 		break;
2220 	}
2221 	case CIL_AVRULE: {
2222 		struct cil_avrule *avrule = node->data;
2223 		rc = __evaluate_classperms_list(avrule->perms.classperms, db);
2224 		if (rc != SEPOL_OK) {
2225 			goto exit;
2226 		}
2227 		break;
2228 	}
2229 	case CIL_CONSTRAIN:
2230 	case CIL_MLSCONSTRAIN: {
2231 		struct cil_constrain *constrain = node->data;
2232 		rc = __evaluate_classperms_list(constrain->classperms, db);
2233 		if (rc != SEPOL_OK) {
2234 			goto exit;
2235 		}
2236 		break;
2237 	}
2238 	default:
2239 		break;
2240 	}
2241 
2242 	return SEPOL_OK;
2243 
2244 exit:
2245 	return rc;
2246 }
2247 
__cil_post_report_conflict(struct cil_tree_node * node,uint32_t * finished,void * extra_args)2248 static int __cil_post_report_conflict(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
2249 {
2250 	struct cil_list_item *li = extra_args;
2251 
2252 	if (node->flavor == CIL_BLOCK) {
2253 		struct cil_block *blk = node->data;
2254 		if (blk->is_abstract == CIL_TRUE) {
2255 			*finished = CIL_TREE_SKIP_HEAD;
2256 		}
2257 	} else if (node->flavor == CIL_MACRO) {
2258 		*finished = CIL_TREE_SKIP_HEAD;
2259 	} else if (node->flavor == li->flavor) {
2260 		if (node->data == li->data) {
2261 			char *path = cil_tree_get_cil_path(node);
2262 			cil_log(CIL_WARN, "  at %s:%d\n", path, node->line);
2263 		}
2264 	}
2265 	return SEPOL_OK;
2266 }
2267 
__cil_post_process_context_rules(struct cil_sort * sort,int (* compar)(const void *,const void *),int (* concompar)(const void *,const void *),struct cil_db * db,enum cil_flavor flavor,const char * flavor_str)2268 static int __cil_post_process_context_rules(struct cil_sort *sort, int (*compar)(const void *, const void *), int (*concompar)(const void *, const void *), struct cil_db *db, enum cil_flavor flavor, const char *flavor_str)
2269 {
2270 	uint32_t count = sort->count;
2271 	uint32_t i = 0, j, removed = 0;
2272 	int conflicting = 0;
2273 	int rc = SEPOL_OK;
2274 	enum cil_log_level log_level = cil_get_log_level();
2275 
2276 	if (count < 2) {
2277 		return SEPOL_OK;
2278 	}
2279 
2280 	qsort(sort->array, sort->count, sizeof(sort->array), compar);
2281 
2282 	for (j=1; j<count; j++) {
2283 		if (compar(&sort->array[i], &sort->array[j]) != 0) {
2284 			i++;
2285 			if (conflicting >= 4) {
2286 				/* 2 rules were written when conflicting == 1 */
2287 				cil_log(CIL_WARN, "  Only first 4 of %d conflicting rules shown\n", conflicting);
2288 			}
2289 			conflicting = 0;
2290 		} else {
2291 			removed++;
2292 			if (!db->multiple_decls || concompar(&sort->array[i], &sort->array[j]) != 0) {
2293 				rc = SEPOL_ERR;
2294 				conflicting++;
2295 				if (log_level >= CIL_WARN) {
2296 					struct cil_list_item li;
2297 					int rc2;
2298 					li.flavor = flavor;
2299 					if (conflicting == 1) {
2300 						cil_log(CIL_WARN, "Found conflicting %s rules\n", flavor_str);
2301 						li.data = sort->array[i];
2302 						rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2303 											NULL, NULL, &li);
2304 						if (rc2 != SEPOL_OK) goto exit;
2305 					}
2306 					if (conflicting < 4 || log_level > CIL_WARN) {
2307 						li.data = sort->array[j];
2308 						rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
2309 											NULL, NULL, &li);
2310 						if (rc2 != SEPOL_OK) goto exit;
2311 					}
2312 				}
2313 			}
2314 		}
2315 		if (i != j && !conflicting) {
2316 			sort->array[i] = sort->array[j];
2317 		}
2318 	}
2319 	sort->count = count - removed;
2320 
2321 exit:
2322 	return rc;
2323 }
2324 
cil_post_db(struct cil_db * db)2325 static int cil_post_db(struct cil_db *db)
2326 {
2327 	int rc = SEPOL_ERR;
2328 
2329 	rc = cil_tree_walk(db->ast->root, __cil_post_db_count_helper, NULL, NULL, db);
2330 	if (rc != SEPOL_OK) {
2331 		cil_log(CIL_INFO, "Failure during cil database count helper\n");
2332 		goto exit;
2333 	}
2334 
2335 	rc = cil_tree_walk(db->ast->root, __cil_post_db_array_helper, NULL, NULL, db);
2336 	if (rc != SEPOL_OK) {
2337 		cil_log(CIL_INFO, "Failure during cil database array helper\n");
2338 		goto exit;
2339 	}
2340 
2341 	rc = cil_tree_walk(db->ast->root, __cil_post_db_neverallow_attr_helper, NULL, NULL, db);
2342 	if (rc != SEPOL_OK) {
2343 		cil_log(CIL_INFO, "Failed to mark attributes used by generated attributes used in neverallow rules\n");
2344 		goto exit;
2345 	}
2346 
2347 	rc = cil_tree_walk(db->ast->root, __cil_post_db_attr_helper, NULL, NULL, db);
2348 	if (rc != SEPOL_OK) {
2349 		cil_log(CIL_INFO, "Failed to create attribute bitmaps\n");
2350 		goto exit;
2351 	}
2352 
2353 	rc = cil_tree_walk(db->ast->root, __cil_post_db_roletype_helper, NULL, NULL, db);
2354 	if (rc != SEPOL_OK) {
2355 		cil_log(CIL_INFO, "Failed during roletype association\n");
2356 		goto exit;
2357 	}
2358 
2359 	rc = cil_tree_walk(db->ast->root, __cil_post_db_userrole_helper, NULL, NULL, db);
2360 	if (rc != SEPOL_OK) {
2361 		cil_log(CIL_INFO, "Failed during userrole association\n");
2362 		goto exit;
2363 	}
2364 
2365 	rc = cil_tree_walk(db->ast->root, __cil_post_db_classperms_helper, NULL, NULL, db);
2366 	if (rc != SEPOL_OK) {
2367 		cil_log(CIL_INFO, "Failed to evaluate class mapping permissions expressions\n");
2368 		goto exit;
2369 	}
2370 
2371 	rc = cil_tree_walk(db->ast->root, __cil_post_db_cat_helper, NULL, NULL, db);
2372 	if (rc != SEPOL_OK) {
2373 		cil_log(CIL_INFO, "Failed to evaluate category expressions\n");
2374 		goto exit;
2375 	}
2376 
2377 	rc = __cil_post_process_context_rules(db->netifcon, cil_post_netifcon_compare, cil_post_netifcon_context_compare, db, CIL_NETIFCON, CIL_KEY_NETIFCON);
2378 	if (rc != SEPOL_OK) {
2379 		cil_log(CIL_ERR, "Problems processing netifcon rules\n");
2380 		goto exit;
2381 	}
2382 
2383 	rc = __cil_post_process_context_rules(db->genfscon, cil_post_genfscon_compare, cil_post_genfscon_context_compare, db, CIL_GENFSCON, CIL_KEY_GENFSCON);
2384 	if (rc != SEPOL_OK) {
2385 		cil_log(CIL_ERR, "Problems processing genfscon rules\n");
2386 		goto exit;
2387 	}
2388 
2389 	rc = __cil_post_process_context_rules(db->ibpkeycon, cil_post_ibpkeycon_compare, cil_post_ibpkeycon_context_compare, db, CIL_IBPKEYCON, CIL_KEY_IBPKEYCON);
2390 	if (rc != SEPOL_OK) {
2391 		cil_log(CIL_ERR, "Problems processing ibpkeycon rules\n");
2392 		goto exit;
2393 	}
2394 
2395 	rc = __cil_post_process_context_rules(db->ibendportcon, cil_post_ibendportcon_compare, cil_post_ibendportcon_context_compare, db, CIL_IBENDPORTCON, CIL_KEY_IBENDPORTCON);
2396 	if (rc != SEPOL_OK) {
2397 		cil_log(CIL_ERR, "Problems processing ibendportcon rules\n");
2398 		goto exit;
2399 	}
2400 
2401 	rc = __cil_post_process_context_rules(db->portcon, cil_post_portcon_compare, cil_post_portcon_context_compare, db, CIL_PORTCON, CIL_KEY_PORTCON);
2402 	if (rc != SEPOL_OK) {
2403 		cil_log(CIL_ERR, "Problems processing portcon rules\n");
2404 		goto exit;
2405 	}
2406 
2407 	rc = __cil_post_process_context_rules(db->nodecon, cil_post_nodecon_compare, cil_post_nodecon_context_compare, db, CIL_NODECON, CIL_KEY_NODECON);
2408 	if (rc != SEPOL_OK) {
2409 		cil_log(CIL_ERR, "Problems processing nodecon rules\n");
2410 		goto exit;
2411 	}
2412 
2413 	rc = __cil_post_process_context_rules(db->fsuse, cil_post_fsuse_compare, cil_post_fsuse_context_compare, db, CIL_FSUSE, CIL_KEY_FSUSE);
2414 	if (rc != SEPOL_OK) {
2415 		cil_log(CIL_ERR, "Problems processing fsuse rules\n");
2416 		goto exit;
2417 	}
2418 
2419 	rc = __cil_post_process_context_rules(db->filecon, cil_post_filecon_compare, cil_post_filecon_context_compare, db, CIL_FILECON, CIL_KEY_FILECON);
2420 	if (rc != SEPOL_OK) {
2421 		cil_log(CIL_ERR, "Problems processing filecon rules\n");
2422 		goto exit;
2423 	}
2424 
2425 	rc = __cil_post_process_context_rules(db->pirqcon, cil_post_pirqcon_compare, cil_post_pirqcon_context_compare, db, CIL_PIRQCON, CIL_KEY_IOMEMCON);
2426 	if (rc != SEPOL_OK) {
2427 		cil_log(CIL_ERR, "Problems processing pirqcon rules\n");
2428 		goto exit;
2429 	}
2430 
2431 	rc = __cil_post_process_context_rules(db->iomemcon, cil_post_iomemcon_compare, cil_post_iomemcon_context_compare, db, CIL_IOMEMCON, CIL_KEY_IOMEMCON);
2432 	if (rc != SEPOL_OK) {
2433 		cil_log(CIL_ERR, "Problems processing iomemcon rules\n");
2434 		goto exit;
2435 	}
2436 
2437 	rc = __cil_post_process_context_rules(db->ioportcon, cil_post_ioportcon_compare, cil_post_ioportcon_context_compare, db, CIL_IOPORTCON, CIL_KEY_IOPORTCON);
2438 	if (rc != SEPOL_OK) {
2439 		cil_log(CIL_ERR, "Problems processing ioportcon rules\n");
2440 		goto exit;
2441 	}
2442 
2443 	rc = __cil_post_process_context_rules(db->pcidevicecon, cil_post_pcidevicecon_compare, cil_post_pcidevicecon_context_compare, db, CIL_PCIDEVICECON, CIL_KEY_PCIDEVICECON);
2444 	if (rc != SEPOL_OK) {
2445 		cil_log(CIL_ERR, "Problems processing pcidevicecon rules\n");
2446 		goto exit;
2447 	}
2448 
2449 	rc = __cil_post_process_context_rules(db->devicetreecon, cil_post_devicetreecon_compare, cil_post_devicetreecon_context_compare, db, CIL_DEVICETREECON, CIL_KEY_DEVICETREECON);
2450 	if (rc != SEPOL_OK) {
2451 		cil_log(CIL_ERR, "Problems processing devicetreecon rules\n");
2452 		goto exit;
2453 	}
2454 
2455 exit:
2456 	return rc;
2457 }
2458 
cil_post_verify(struct cil_db * db)2459 static int cil_post_verify(struct cil_db *db)
2460 {
2461 	int rc = SEPOL_ERR;
2462 	int avrule_cnt = 0;
2463 	int handleunknown = -1;
2464 	int mls = -1;
2465 	int nseuserdflt = 0;
2466 	int pass = 0;
2467 	struct cil_args_verify extra_args;
2468 	struct cil_complex_symtab csymtab;
2469 
2470 	cil_complex_symtab_init(&csymtab, CIL_CLASS_SYM_SIZE);
2471 
2472 	extra_args.db = db;
2473 	extra_args.csymtab = &csymtab;
2474 	extra_args.avrule_cnt = &avrule_cnt;
2475 	extra_args.handleunknown = &handleunknown;
2476 	extra_args.mls = &mls;
2477 	extra_args.nseuserdflt = &nseuserdflt;
2478 	extra_args.pass = &pass;
2479 
2480 	for (pass = 0; pass < 2; pass++) {
2481 		rc = cil_tree_walk(db->ast->root, __cil_verify_helper, NULL, NULL, &extra_args);
2482 		if (rc != SEPOL_OK) {
2483 			cil_log(CIL_ERR, "Failed to verify cil database\n");
2484 			goto exit;
2485 		}
2486 	}
2487 
2488 	if (db->handle_unknown == -1) {
2489 		if (handleunknown == -1) {
2490 			db->handle_unknown = SEPOL_DENY_UNKNOWN;
2491 		} else {
2492 			db->handle_unknown = handleunknown;
2493 		}
2494 	}
2495 
2496 	if (db->mls == -1) {
2497 		if (mls == -1) {
2498 			db->mls = CIL_FALSE;
2499 		} else {
2500 			db->mls = mls;
2501 		}
2502 	}
2503 
2504 	if (avrule_cnt == 0) {
2505 		cil_log(CIL_ERR, "Policy must include at least one avrule\n");
2506 		rc = SEPOL_ERR;
2507 		goto exit;
2508 	}
2509 
2510 	if (nseuserdflt > 1) {
2511 		cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
2512 		rc = SEPOL_ERR;
2513 		goto exit;
2514 	}
2515 
2516 exit:
2517 	cil_complex_symtab_destroy(&csymtab);
2518 	return rc;
2519 }
2520 
cil_pre_verify(struct cil_db * db)2521 static int cil_pre_verify(struct cil_db *db)
2522 {
2523 	int rc = SEPOL_ERR;
2524 	struct cil_args_verify extra_args;
2525 
2526 	extra_args.db = db;
2527 
2528 	rc = cil_tree_walk(db->ast->root, __cil_pre_verify_helper, NULL, NULL, &extra_args);
2529 	if (rc != SEPOL_OK) {
2530 		cil_log(CIL_ERR, "Failed to verify cil database\n");
2531 		goto exit;
2532 	}
2533 
2534 exit:
2535 	return rc;
2536 }
2537 
cil_post_process(struct cil_db * db)2538 int cil_post_process(struct cil_db *db)
2539 {
2540 	int rc = SEPOL_ERR;
2541 
2542 	rc = cil_pre_verify(db);
2543 	if (rc != SEPOL_OK) {
2544 		cil_log(CIL_ERR, "Failed to verify cil database\n");
2545 		goto exit;
2546 	}
2547 
2548 	rc = cil_post_db(db);
2549 	if (rc != SEPOL_OK) {
2550 		cil_log(CIL_ERR, "Failed post db handling\n");
2551 		goto exit;
2552 	}
2553 
2554 	rc = cil_post_verify(db);
2555 	if (rc != SEPOL_OK) {
2556 		cil_log(CIL_ERR, "Failed to verify cil database\n");
2557 		goto exit;
2558 	}
2559 
2560 exit:
2561 	return rc;
2562 
2563 }
2564