• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
2  *          Jason Tang <jtang@tresys.com>
3  *	    Joshua Brindle <jbrindle@tresys.com>
4  *
5  * Copyright (C) 2004-2005 Tresys Technology, LLC
6  * Copyright (C) 2007 Red Hat, Inc.
7  * Copyright (C) 2017 Mellanox Technologies, Inc.
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2.1 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23 
24 #include "context.h"
25 #include <sepol/policydb/policydb.h>
26 #include <sepol/policydb/conditional.h>
27 #include <sepol/policydb/hashtab.h>
28 #include <sepol/policydb/expand.h>
29 #include <sepol/policydb/hierarchy.h>
30 #include <sepol/policydb/avrule_block.h>
31 
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <inttypes.h>
38 
39 #include "debug.h"
40 #include "private.h"
41 
42 typedef struct expand_state {
43 	int verbose;
44 	uint32_t *typemap;
45 	uint32_t *boolmap;
46 	uint32_t *rolemap;
47 	uint32_t *usermap;
48 	policydb_t *base;
49 	policydb_t *out;
50 	sepol_handle_t *handle;
51 	int expand_neverallow;
52 } expand_state_t;
53 
expand_state_init(expand_state_t * state)54 static void expand_state_init(expand_state_t * state)
55 {
56 	memset(state, 0, sizeof(expand_state_t));
57 }
58 
map_ebitmap(ebitmap_t * src,ebitmap_t * dst,uint32_t * map)59 static int map_ebitmap(ebitmap_t * src, ebitmap_t * dst, uint32_t * map)
60 {
61 	unsigned int i;
62 	ebitmap_node_t *tnode;
63 	ebitmap_init(dst);
64 
65 	ebitmap_for_each_positive_bit(src, tnode, i) {
66 		if (!map[i])
67 			continue;
68 		if (ebitmap_set_bit(dst, map[i] - 1, 1))
69 			return -1;
70 	}
71 	return 0;
72 }
73 
ebitmap_expand_roles(policydb_t * p,ebitmap_t * roles)74 static int ebitmap_expand_roles(policydb_t *p, ebitmap_t *roles)
75 {
76 	ebitmap_node_t *node;
77 	unsigned int bit;
78 	role_datum_t *role;
79 	ebitmap_t tmp;
80 
81 	ebitmap_init(&tmp);
82 	ebitmap_for_each_positive_bit(roles, node, bit) {
83 		role = p->role_val_to_struct[bit];
84 		assert(role);
85 		if (role->flavor != ROLE_ATTRIB) {
86 			if (ebitmap_set_bit(&tmp, bit, 1)) {
87 				ebitmap_destroy(&tmp);
88 				return -1;
89 			}
90 		} else {
91 			if (ebitmap_union(&tmp, &role->roles)) {
92 				ebitmap_destroy(&tmp);
93 				return -1;
94 			}
95 		}
96 	}
97 	ebitmap_destroy(roles);
98 	if (ebitmap_cpy(roles, &tmp)) {
99 		ebitmap_destroy(&tmp);
100 		return -1;
101 	}
102 	ebitmap_destroy(&tmp);
103 	return 0;
104 }
105 
type_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)106 static int type_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
107 			      void *data)
108 {
109 	int ret;
110 	char *id, *new_id;
111 	type_datum_t *type, *new_type;
112 	expand_state_t *state;
113 
114 	id = (char *)key;
115 	type = (type_datum_t *) datum;
116 	state = (expand_state_t *) data;
117 
118 	if ((type->flavor == TYPE_TYPE && !type->primary)
119 	    || type->flavor == TYPE_ALIAS) {
120 		/* aliases are handled later */
121 		return 0;
122 	}
123 	if (!is_id_enabled(id, state->base, SYM_TYPES)) {
124 		/* identifier's scope is not enabled */
125 		return 0;
126 	}
127 
128 	if (state->verbose)
129 		INFO(state->handle, "copying type or attribute %s", id);
130 
131 	new_id = strdup(id);
132 	if (new_id == NULL) {
133 		ERR(state->handle, "Out of memory!");
134 		return -1;
135 	}
136 
137 	new_type = (type_datum_t *) malloc(sizeof(type_datum_t));
138 	if (!new_type) {
139 		ERR(state->handle, "Out of memory!");
140 		free(new_id);
141 		return SEPOL_ENOMEM;
142 	}
143 	memset(new_type, 0, sizeof(type_datum_t));
144 
145 	new_type->flavor = type->flavor;
146 	new_type->flags = type->flags;
147 	new_type->s.value = ++state->out->p_types.nprim;
148 	if (new_type->s.value > UINT16_MAX) {
149 		free(new_id);
150 		free(new_type);
151 		ERR(state->handle, "type space overflow");
152 		return -1;
153 	}
154 	new_type->primary = 1;
155 	state->typemap[type->s.value - 1] = new_type->s.value;
156 
157 	ret = hashtab_insert(state->out->p_types.table,
158 			     (hashtab_key_t) new_id,
159 			     (hashtab_datum_t) new_type);
160 	if (ret) {
161 		free(new_id);
162 		free(new_type);
163 		ERR(state->handle, "hashtab overflow");
164 		return -1;
165 	}
166 
167 	if (new_type->flags & TYPE_FLAGS_PERMISSIVE)
168 		if (ebitmap_set_bit(&state->out->permissive_map, new_type->s.value, 1)) {
169 			ERR(state->handle, "Out of memory!\n");
170 			return -1;
171 		}
172 
173 	return 0;
174 }
175 
attr_convert_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)176 static int attr_convert_callback(hashtab_key_t key, hashtab_datum_t datum,
177 				 void *data)
178 {
179 	char *id;
180 	type_datum_t *type, *new_type;
181 	expand_state_t *state;
182 	ebitmap_t tmp_union;
183 
184 	id = (char *)key;
185 	type = (type_datum_t *) datum;
186 	state = (expand_state_t *) data;
187 
188 	if (type->flavor != TYPE_ATTRIB)
189 		return 0;
190 
191 	if (!is_id_enabled(id, state->base, SYM_TYPES)) {
192 		/* identifier's scope is not enabled */
193 		return 0;
194 	}
195 
196 	if (state->verbose)
197 		INFO(state->handle, "converting attribute %s", id);
198 
199 	new_type = hashtab_search(state->out->p_types.table, id);
200 	if (!new_type) {
201 		ERR(state->handle, "attribute %s vanished!", id);
202 		return -1;
203 	}
204 	if (map_ebitmap(&type->types, &tmp_union, state->typemap)) {
205 		ERR(state->handle, "out of memory");
206 		return -1;
207 	}
208 
209 	/* then union tmp_union onto &new_type->types */
210 	if (ebitmap_union(&new_type->types, &tmp_union)) {
211 		ERR(state->handle, "Out of memory!");
212 		return -1;
213 	}
214 	ebitmap_destroy(&tmp_union);
215 
216 	return 0;
217 }
218 
perm_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)219 static int perm_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
220 			      void *data)
221 {
222 	int ret;
223 	char *id, *new_id;
224 	symtab_t *s;
225 	perm_datum_t *perm, *new_perm;
226 
227 	id = key;
228 	perm = (perm_datum_t *) datum;
229 	s = (symtab_t *) data;
230 
231 	new_perm = (perm_datum_t *) malloc(sizeof(perm_datum_t));
232 	if (!new_perm) {
233 		return -1;
234 	}
235 	memset(new_perm, 0, sizeof(perm_datum_t));
236 
237 	new_id = strdup(id);
238 	if (!new_id) {
239 		free(new_perm);
240 		return -1;
241 	}
242 
243 	new_perm->s.value = perm->s.value;
244 	s->nprim++;
245 
246 	ret = hashtab_insert(s->table, new_id, (hashtab_datum_t) new_perm);
247 	if (ret) {
248 		free(new_id);
249 		free(new_perm);
250 		return -1;
251 	}
252 
253 	return 0;
254 }
255 
common_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)256 static int common_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
257 				void *data)
258 {
259 	int ret;
260 	char *id, *new_id;
261 	common_datum_t *common, *new_common;
262 	expand_state_t *state;
263 
264 	id = (char *)key;
265 	common = (common_datum_t *) datum;
266 	state = (expand_state_t *) data;
267 
268 	if (state->verbose)
269 		INFO(state->handle, "copying common %s", id);
270 
271 	new_common = (common_datum_t *) malloc(sizeof(common_datum_t));
272 	if (!new_common) {
273 		ERR(state->handle, "Out of memory!");
274 		return -1;
275 	}
276 	memset(new_common, 0, sizeof(common_datum_t));
277 	if (symtab_init(&new_common->permissions, PERM_SYMTAB_SIZE)) {
278 		ERR(state->handle, "Out of memory!");
279 		free(new_common);
280 		return -1;
281 	}
282 
283 	new_id = strdup(id);
284 	if (!new_id) {
285 		ERR(state->handle, "Out of memory!");
286 		/* free memory created by symtab_init first, then free new_common */
287 		symtab_destroy(&new_common->permissions);
288 		free(new_common);
289 		return -1;
290 	}
291 
292 	new_common->s.value = common->s.value;
293 	state->out->p_commons.nprim++;
294 
295 	ret =
296 	    hashtab_insert(state->out->p_commons.table, new_id,
297 			   (hashtab_datum_t) new_common);
298 	if (ret) {
299 		ERR(state->handle, "hashtab overflow");
300 		free(new_common);
301 		free(new_id);
302 		return -1;
303 	}
304 
305 	if (hashtab_map
306 	    (common->permissions.table, perm_copy_callback,
307 	     &new_common->permissions)) {
308 		ERR(state->handle, "Out of memory!");
309 		return -1;
310 	}
311 
312 	return 0;
313 }
314 
constraint_node_clone(constraint_node_t ** dst,constraint_node_t * src,expand_state_t * state)315 static int constraint_node_clone(constraint_node_t ** dst,
316 				 constraint_node_t * src,
317 				 expand_state_t * state)
318 {
319 	constraint_node_t *new_con = NULL, *last_new_con = NULL;
320 	constraint_expr_t *new_expr = NULL;
321 	*dst = NULL;
322 	while (src != NULL) {
323 		constraint_expr_t *expr, *expr_l = NULL;
324 		new_con =
325 		    (constraint_node_t *) malloc(sizeof(constraint_node_t));
326 		if (!new_con) {
327 			goto out_of_mem;
328 		}
329 		memset(new_con, 0, sizeof(constraint_node_t));
330 		new_con->permissions = src->permissions;
331 		for (expr = src->expr; expr; expr = expr->next) {
332 			if ((new_expr = calloc(1, sizeof(*new_expr))) == NULL) {
333 				goto out_of_mem;
334 			}
335 			if (constraint_expr_init(new_expr) == -1) {
336 				goto out_of_mem;
337 			}
338 			new_expr->expr_type = expr->expr_type;
339 			new_expr->attr = expr->attr;
340 			new_expr->op = expr->op;
341 			if (new_expr->expr_type == CEXPR_NAMES) {
342 				if (new_expr->attr & CEXPR_TYPE) {
343 					/*
344 					 * Copy over constraint policy source types and/or
345 					 * attributes for sepol_compute_av_reason_buffer(3)
346 					 * so that utilities can analyse constraint errors.
347 					 */
348 					if (map_ebitmap(&expr->type_names->types,
349 							&new_expr->type_names->types,
350 							state->typemap)) {
351 						ERR(NULL, "Failed to map type_names->types");
352 						goto out_of_mem;
353 					}
354 					/* Type sets require expansion and conversion. */
355 					if (expand_convert_type_set(state->out,
356 								    state->
357 								    typemap,
358 								    expr->
359 								    type_names,
360 								    &new_expr->
361 								    names, 1)) {
362 						goto out_of_mem;
363 					}
364 				} else if (new_expr->attr & CEXPR_ROLE) {
365 					if (map_ebitmap(&expr->names, &new_expr->names, state->rolemap)) {
366 						goto out_of_mem;
367 					}
368 					if (ebitmap_expand_roles(state->out, &new_expr->names)) {
369 						goto out_of_mem;
370 					}
371 				} else if (new_expr->attr & CEXPR_USER) {
372 					if (map_ebitmap(&expr->names, &new_expr->names, state->usermap)) {
373 						goto out_of_mem;
374 					}
375 				} else {
376 					/* Other kinds of sets do not. */
377 					if (ebitmap_cpy(&new_expr->names,
378 							&expr->names)) {
379 						goto out_of_mem;
380 					}
381 				}
382 			}
383 			if (expr_l) {
384 				expr_l->next = new_expr;
385 			} else {
386 				new_con->expr = new_expr;
387 			}
388 			expr_l = new_expr;
389 			new_expr = NULL;
390 		}
391 		if (last_new_con == NULL) {
392 			*dst = new_con;
393 		} else {
394 			last_new_con->next = new_con;
395 		}
396 		last_new_con = new_con;
397 		src = src->next;
398 	}
399 
400 	return 0;
401       out_of_mem:
402 	ERR(state->handle, "Out of memory!");
403 	if (new_con)
404 		free(new_con);
405 	constraint_expr_destroy(new_expr);
406 	return -1;
407 }
408 
class_copy_default_new_object(expand_state_t * state,class_datum_t * olddatum,class_datum_t * newdatum)409 static int class_copy_default_new_object(expand_state_t *state,
410 					 class_datum_t *olddatum,
411 					 class_datum_t *newdatum)
412 {
413 	if (olddatum->default_user) {
414 		if (newdatum->default_user && olddatum->default_user != newdatum->default_user) {
415 			ERR(state->handle, "Found conflicting default user definitions");
416 			return SEPOL_ENOTSUP;
417 		}
418 		newdatum->default_user = olddatum->default_user;
419 
420 	}
421 	if (olddatum->default_role) {
422 		if (newdatum->default_role && olddatum->default_role != newdatum->default_role) {
423 			ERR(state->handle, "Found conflicting default role definitions");
424 			return SEPOL_ENOTSUP;
425 		}
426 		newdatum->default_role = olddatum->default_role;
427 	}
428 	if (olddatum->default_type) {
429 		if (newdatum->default_type && olddatum->default_type != newdatum->default_type) {
430 			ERR(state->handle, "Found conflicting default type definitions");
431 			return SEPOL_ENOTSUP;
432 		}
433 		newdatum->default_type = olddatum->default_type;
434 	}
435 	if (olddatum->default_range) {
436 		if (newdatum->default_range && olddatum->default_range != newdatum->default_range) {
437 			ERR(state->handle, "Found conflicting default range definitions");
438 			return SEPOL_ENOTSUP;
439 		}
440 		newdatum->default_range = olddatum->default_range;
441 	}
442 	return 0;
443 }
444 
class_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)445 static int class_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
446 			       void *data)
447 {
448 	int ret;
449 	char *id, *new_id;
450 	class_datum_t *class, *new_class;
451 	expand_state_t *state;
452 
453 	id = (char *)key;
454 	class = (class_datum_t *) datum;
455 	state = (expand_state_t *) data;
456 
457 	if (!is_id_enabled(id, state->base, SYM_CLASSES)) {
458 		/* identifier's scope is not enabled */
459 		return 0;
460 	}
461 
462 	if (state->verbose)
463 		INFO(state->handle, "copying class %s", id);
464 
465 	new_class = (class_datum_t *) malloc(sizeof(class_datum_t));
466 	if (!new_class) {
467 		ERR(state->handle, "Out of memory!");
468 		return -1;
469 	}
470 	memset(new_class, 0, sizeof(class_datum_t));
471 	if (symtab_init(&new_class->permissions, PERM_SYMTAB_SIZE)) {
472 		ERR(state->handle, "Out of memory!");
473 		free(new_class);
474 		return -1;
475 	}
476 
477 	new_class->s.value = class->s.value;
478 	state->out->p_classes.nprim++;
479 
480 	ret = class_copy_default_new_object(state, class, new_class);
481 	if (ret) {
482 		free(new_class);
483 		return ret;
484 	}
485 
486 	new_id = strdup(id);
487 	if (!new_id) {
488 		ERR(state->handle, "Out of memory!");
489 		free(new_class);
490 		return -1;
491 	}
492 
493 	ret =
494 	    hashtab_insert(state->out->p_classes.table, new_id,
495 			   (hashtab_datum_t) new_class);
496 	if (ret) {
497 		ERR(state->handle, "hashtab overflow");
498 		free(new_class);
499 		free(new_id);
500 		return -1;
501 	}
502 
503 	if (hashtab_map
504 	    (class->permissions.table, perm_copy_callback,
505 	     &new_class->permissions)) {
506 		ERR(state->handle, "hashtab overflow");
507 		return -1;
508 	}
509 
510 	if (class->comkey) {
511 		new_class->comkey = strdup(class->comkey);
512 		if (!new_class->comkey) {
513 			ERR(state->handle, "Out of memory!");
514 			return -1;
515 		}
516 
517 		new_class->comdatum =
518 		    hashtab_search(state->out->p_commons.table,
519 				   new_class->comkey);
520 		if (!new_class->comdatum) {
521 			ERR(state->handle, "could not find common datum %s",
522 			    new_class->comkey);
523 			return -1;
524 		}
525 		new_class->permissions.nprim +=
526 		    new_class->comdatum->permissions.nprim;
527 	}
528 
529 	return 0;
530 }
531 
constraint_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)532 static int constraint_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
533 				    void *data)
534 {
535 	char *id;
536 	class_datum_t *class, *new_class;
537 	expand_state_t *state;
538 
539 	id = (char *)key;
540 	class = (class_datum_t *) datum;
541 	state = (expand_state_t *) data;
542 
543 	new_class = hashtab_search(state->out->p_classes.table, id);
544 	if (!new_class) {
545 		ERR(state->handle, "class %s vanished", id);
546 		return -1;
547 	}
548 
549 	/* constraints */
550 	if (constraint_node_clone
551 	    (&new_class->constraints, class->constraints, state) == -1
552 	    || constraint_node_clone(&new_class->validatetrans,
553 				     class->validatetrans, state) == -1) {
554 		return -1;
555 	}
556 	return 0;
557 }
558 
559 /*
560  * The boundaries have to be copied after the types/roles/users are copied,
561  * because it refers hashtab to lookup destinated objects.
562  */
type_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)563 static int type_bounds_copy_callback(hashtab_key_t key,
564 				     hashtab_datum_t datum, void *data)
565 {
566 	expand_state_t *state = (expand_state_t *) data;
567 	type_datum_t *type = (type_datum_t *) datum;
568 	type_datum_t *dest;
569 	uint32_t bounds_val;
570 
571 	if (!type->bounds)
572 		return 0;
573 
574 	if (!is_id_enabled((char *)key, state->base, SYM_TYPES))
575 		return 0;
576 
577 	bounds_val = state->typemap[type->bounds - 1];
578 
579 	dest = hashtab_search(state->out->p_types.table, (char *)key);
580 	if (!dest) {
581 		ERR(state->handle, "Type lookup failed for %s", (char *)key);
582 		return -1;
583 	}
584 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
585 		ERR(state->handle, "Inconsistent boundary for %s", (char *)key);
586 		return -1;
587 	}
588 	dest->bounds = bounds_val;
589 
590 	return 0;
591 }
592 
role_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)593 static int role_bounds_copy_callback(hashtab_key_t key,
594 				     hashtab_datum_t datum, void *data)
595 {
596 	expand_state_t *state = (expand_state_t *) data;
597 	role_datum_t *role = (role_datum_t *) datum;
598 	role_datum_t *dest;
599 	uint32_t bounds_val;
600 
601 	if (!role->bounds)
602 		return 0;
603 
604 	if (!is_id_enabled((char *)key, state->base, SYM_ROLES))
605 		return 0;
606 
607 	bounds_val = state->rolemap[role->bounds - 1];
608 
609 	dest = hashtab_search(state->out->p_roles.table, (char *)key);
610 	if (!dest) {
611 		ERR(state->handle, "Role lookup failed for %s", (char *)key);
612 		return -1;
613 	}
614 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
615 		ERR(state->handle, "Inconsistent boundary for %s", (char *)key);
616 		return -1;
617 	}
618 	dest->bounds = bounds_val;
619 
620 	return 0;
621 }
622 
user_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)623 static int user_bounds_copy_callback(hashtab_key_t key,
624 				     hashtab_datum_t datum, void *data)
625 {
626 	expand_state_t *state = (expand_state_t *) data;
627 	user_datum_t *user = (user_datum_t *) datum;
628 	user_datum_t *dest;
629 	uint32_t bounds_val;
630 
631 	if (!user->bounds)
632 		return 0;
633 
634 	if (!is_id_enabled((char *)key, state->base, SYM_USERS))
635 		return 0;
636 
637 	bounds_val = state->usermap[user->bounds - 1];
638 
639 	dest = hashtab_search(state->out->p_users.table, (char *)key);
640 	if (!dest) {
641 		ERR(state->handle, "User lookup failed for %s", (char *)key);
642 		return -1;
643 	}
644 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
645 		ERR(state->handle, "Inconsistent boundary for %s", (char *)key);
646 		return -1;
647 	}
648 	dest->bounds = bounds_val;
649 
650 	return 0;
651 }
652 
653 /* The aliases have to be copied after the types and attributes to be certain that
654  * the out symbol table will have the type that the alias refers. Otherwise, we
655  * won't be able to find the type value for the alias. We can't depend on the
656  * declaration ordering because of the hash table.
657  */
alias_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)658 static int alias_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
659 			       void *data)
660 {
661 	int ret;
662 	char *id, *new_id;
663 	type_datum_t *alias, *new_alias;
664 	expand_state_t *state;
665 	uint32_t prival;
666 
667 	id = (char *)key;
668 	alias = (type_datum_t *) datum;
669 	state = (expand_state_t *) data;
670 
671 	/* ignore regular types */
672 	if (alias->flavor == TYPE_TYPE && alias->primary)
673 		return 0;
674 
675 	/* ignore attributes */
676 	if (alias->flavor == TYPE_ATTRIB)
677 		return 0;
678 
679 	if (alias->flavor == TYPE_ALIAS)
680 		prival = alias->primary;
681 	else
682 		prival = alias->s.value;
683 
684 	if (!is_id_enabled(state->base->p_type_val_to_name[prival - 1],
685 			state->base, SYM_TYPES)) {
686 		/* The primary type for this alias is not enabled, the alias
687  		 * shouldn't be either */
688 		return 0;
689 	}
690 
691 	if (state->verbose)
692 		INFO(state->handle, "copying alias %s", id);
693 
694 	new_id = strdup(id);
695 	if (!new_id) {
696 		ERR(state->handle, "Out of memory!");
697 		return -1;
698 	}
699 
700 	new_alias = (type_datum_t *) malloc(sizeof(type_datum_t));
701 	if (!new_alias) {
702 		ERR(state->handle, "Out of memory!");
703 		free(new_id);
704 		return SEPOL_ENOMEM;
705 	}
706 	memset(new_alias, 0, sizeof(type_datum_t));
707 	if (alias->flavor == TYPE_TYPE)
708 		new_alias->s.value = state->typemap[alias->s.value - 1];
709 	else if (alias->flavor == TYPE_ALIAS)
710 		new_alias->s.value = state->typemap[alias->primary - 1];
711 	else
712 		assert(0);	/* unreachable */
713 
714 	new_alias->flags = alias->flags;
715 
716 	ret = hashtab_insert(state->out->p_types.table,
717 			     (hashtab_key_t) new_id,
718 			     (hashtab_datum_t) new_alias);
719 
720 	if (ret) {
721 		ERR(state->handle, "hashtab overflow");
722 		free(new_alias);
723 		free(new_id);
724 		return -1;
725 	}
726 
727 	state->typemap[alias->s.value - 1] = new_alias->s.value;
728 
729 	if (new_alias->flags & TYPE_FLAGS_PERMISSIVE)
730 		if (ebitmap_set_bit(&state->out->permissive_map, new_alias->s.value, 1)) {
731 			ERR(state->handle, "Out of memory!");
732 			return -1;
733 		}
734 
735 	return 0;
736 }
737 
role_remap_dominates(hashtab_key_t key,hashtab_datum_t datum,void * data)738 static int role_remap_dominates(hashtab_key_t key __attribute__ ((unused)), hashtab_datum_t datum, void *data)
739 {
740 	ebitmap_t mapped_roles;
741 	role_datum_t *role = (role_datum_t *) datum;
742 	expand_state_t *state = (expand_state_t *) data;
743 
744 	if (map_ebitmap(&role->dominates, &mapped_roles, state->rolemap))
745 		return -1;
746 
747 	ebitmap_destroy(&role->dominates);
748 
749 	if (ebitmap_cpy(&role->dominates, &mapped_roles))
750 		return -1;
751 
752 	ebitmap_destroy(&mapped_roles);
753 
754 	return 0;
755 }
756 
757 /* For the role attribute in the base module, escalate its counterpart's
758  * types.types ebitmap in the out module to the counterparts of all the
759  * regular role that belongs to the current role attribute. Note, must be
760  * invoked after role_copy_callback so that state->rolemap is available.
761  */
role_fix_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)762 static int role_fix_callback(hashtab_key_t key, hashtab_datum_t datum,
763 			     void *data)
764 {
765 	char *id, *base_reg_role_id;
766 	role_datum_t *role, *new_role, *regular_role;
767 	expand_state_t *state;
768 	ebitmap_node_t *rnode;
769 	unsigned int i;
770 	ebitmap_t mapped_roles;
771 
772 	id = key;
773 	role = (role_datum_t *)datum;
774 	state = (expand_state_t *)data;
775 
776 	if (strcmp(id, OBJECT_R) == 0) {
777 		/* object_r is never a role attribute by far */
778 		return 0;
779 	}
780 
781 	if (!is_id_enabled(id, state->base, SYM_ROLES)) {
782 		/* identifier's scope is not enabled */
783 		return 0;
784 	}
785 
786 	if (role->flavor != ROLE_ATTRIB)
787 		return 0;
788 
789 	if (state->verbose)
790 		INFO(state->handle, "fixing role attribute %s", id);
791 
792 	new_role =
793 		(role_datum_t *)hashtab_search(state->out->p_roles.table, id);
794 
795 	assert(new_role != NULL && new_role->flavor == ROLE_ATTRIB);
796 
797 	ebitmap_init(&mapped_roles);
798 	if (map_ebitmap(&role->roles, &mapped_roles, state->rolemap))
799 		return -1;
800 	if (ebitmap_union(&new_role->roles, &mapped_roles)) {
801 		ERR(state->handle, "Out of memory!");
802 		ebitmap_destroy(&mapped_roles);
803 		return -1;
804 	}
805 	ebitmap_destroy(&mapped_roles);
806 
807 	ebitmap_for_each_positive_bit(&role->roles, rnode, i) {
808 		/* take advantage of sym_val_to_name[]
809 		 * of the base module */
810 		base_reg_role_id = state->base->p_role_val_to_name[i];
811 		regular_role = (role_datum_t *)hashtab_search(
812 					state->out->p_roles.table,
813 					base_reg_role_id);
814 		assert(regular_role != NULL &&
815 		       regular_role->flavor == ROLE_ROLE);
816 
817 		if (ebitmap_union(&regular_role->types.types,
818 				  &new_role->types.types)) {
819 			ERR(state->handle, "Out of memory!");
820 			return -1;
821 		}
822 	}
823 
824 	return 0;
825 }
826 
role_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)827 static int role_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
828 			      void *data)
829 {
830 	int ret;
831 	char *id, *new_id;
832 	role_datum_t *role;
833 	role_datum_t *new_role;
834 	expand_state_t *state;
835 	ebitmap_t tmp_union_types;
836 
837 	id = key;
838 	role = (role_datum_t *) datum;
839 	state = (expand_state_t *) data;
840 
841 	if (strcmp(id, OBJECT_R) == 0) {
842 		/* object_r is always value 1 */
843 		state->rolemap[role->s.value - 1] = 1;
844 		return 0;
845 	}
846 
847 	if (!is_id_enabled(id, state->base, SYM_ROLES)) {
848 		/* identifier's scope is not enabled */
849 		return 0;
850 	}
851 
852 	if (state->verbose)
853 		INFO(state->handle, "copying role %s", id);
854 
855 	new_role =
856 	    (role_datum_t *) hashtab_search(state->out->p_roles.table, id);
857 	if (!new_role) {
858 		new_role = (role_datum_t *) malloc(sizeof(role_datum_t));
859 		if (!new_role) {
860 			ERR(state->handle, "Out of memory!");
861 			return -1;
862 		}
863 		memset(new_role, 0, sizeof(role_datum_t));
864 
865 		new_id = strdup(id);
866 		if (!new_id) {
867 			ERR(state->handle, "Out of memory!");
868 			free(new_role);
869 			return -1;
870 		}
871 
872 		state->out->p_roles.nprim++;
873 		new_role->flavor = role->flavor;
874 		new_role->s.value = state->out->p_roles.nprim;
875 		state->rolemap[role->s.value - 1] = new_role->s.value;
876 		ret = hashtab_insert(state->out->p_roles.table,
877 				     (hashtab_key_t) new_id,
878 				     (hashtab_datum_t) new_role);
879 
880 		if (ret) {
881 			ERR(state->handle, "hashtab overflow");
882 			free(new_role);
883 			free(new_id);
884 			return -1;
885 		}
886 	}
887 
888 	/* The dominates bitmap is going to be wrong for the moment,
889  	 * we'll come back later and remap them, after we are sure all
890  	 * the roles have been added */
891 	if (ebitmap_union(&new_role->dominates, &role->dominates)) {
892 		ERR(state->handle, "Out of memory!");
893 		return -1;
894 	}
895 
896 	ebitmap_init(&tmp_union_types);
897 
898 	/* convert types in the role datum in the global symtab */
899 	if (expand_convert_type_set
900 	    (state->out, state->typemap, &role->types, &tmp_union_types, 1)) {
901 		ebitmap_destroy(&tmp_union_types);
902 		ERR(state->handle, "Out of memory!");
903 		return -1;
904 	}
905 
906 	if (ebitmap_union(&new_role->types.types, &tmp_union_types)) {
907 		ERR(state->handle, "Out of memory!");
908 		ebitmap_destroy(&tmp_union_types);
909 		return -1;
910 	}
911 	ebitmap_destroy(&tmp_union_types);
912 
913 	return 0;
914 }
915 
mls_semantic_level_expand(mls_semantic_level_t * sl,mls_level_t * l,policydb_t * p,sepol_handle_t * h)916 int mls_semantic_level_expand(mls_semantic_level_t * sl, mls_level_t * l,
917 			      policydb_t * p, sepol_handle_t * h)
918 {
919 	mls_semantic_cat_t *cat;
920 	level_datum_t *levdatum;
921 	unsigned int i;
922 
923 	mls_level_init(l);
924 
925 	if (!p->mls)
926 		return 0;
927 
928 	/* Required not declared. */
929 	if (!sl->sens)
930 		return 0;
931 
932 	l->sens = sl->sens;
933 	levdatum = (level_datum_t *) hashtab_search(p->p_levels.table,
934 						    p->p_sens_val_to_name[l->sens - 1]);
935 	if (!levdatum) {
936 		ERR(h, "%s: Impossible situation found, nothing in p_levels.table.\n",
937 		    __func__);
938 		errno = ENOENT;
939 		return -1;
940 	}
941 	for (cat = sl->cat; cat; cat = cat->next) {
942 		if (cat->low > cat->high) {
943 			ERR(h, "Category range is not valid %s.%s",
944 			    p->p_cat_val_to_name[cat->low - 1],
945 			    p->p_cat_val_to_name[cat->high - 1]);
946 			return -1;
947 		}
948 		for (i = cat->low - 1; i < cat->high; i++) {
949 			if (!ebitmap_get_bit(&levdatum->level->cat, i)) {
950 				ERR(h, "Category %s can not be associated with "
951 				    "level %s",
952 				    p->p_cat_val_to_name[i],
953 				    p->p_sens_val_to_name[l->sens - 1]);
954 				return -1;
955 			}
956 			if (ebitmap_set_bit(&l->cat, i, 1)) {
957 				ERR(h, "Out of memory!");
958 				return -1;
959 			}
960 		}
961 	}
962 
963 	return 0;
964 }
965 
mls_semantic_range_expand(mls_semantic_range_t * sr,mls_range_t * r,policydb_t * p,sepol_handle_t * h)966 int mls_semantic_range_expand(mls_semantic_range_t * sr, mls_range_t * r,
967 			      policydb_t * p, sepol_handle_t * h)
968 {
969 	if (mls_semantic_level_expand(&sr->level[0], &r->level[0], p, h) < 0)
970 		return -1;
971 
972 	if (mls_semantic_level_expand(&sr->level[1], &r->level[1], p, h) < 0) {
973 		mls_level_destroy(&r->level[0]);
974 		return -1;
975 	}
976 
977 	if (!mls_level_dom(&r->level[1], &r->level[0])) {
978 		mls_range_destroy(r);
979 		ERR(h, "MLS range high level does not dominate low level");
980 		return -1;
981 	}
982 
983 	return 0;
984 }
985 
user_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)986 static int user_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
987 			      void *data)
988 {
989 	int ret;
990 	expand_state_t *state;
991 	user_datum_t *user;
992 	user_datum_t *new_user;
993 	char *id, *new_id;
994 	ebitmap_t tmp_union;
995 
996 	id = key;
997 	user = (user_datum_t *) datum;
998 	state = (expand_state_t *) data;
999 
1000 	if (!is_id_enabled(id, state->base, SYM_USERS)) {
1001 		/* identifier's scope is not enabled */
1002 		return 0;
1003 	}
1004 
1005 	if (state->verbose)
1006 		INFO(state->handle, "copying user %s", id);
1007 
1008 	new_user =
1009 	    (user_datum_t *) hashtab_search(state->out->p_users.table, id);
1010 	if (!new_user) {
1011 		new_user = (user_datum_t *) malloc(sizeof(user_datum_t));
1012 		if (!new_user) {
1013 			ERR(state->handle, "Out of memory!");
1014 			return -1;
1015 		}
1016 		memset(new_user, 0, sizeof(user_datum_t));
1017 
1018 		state->out->p_users.nprim++;
1019 		new_user->s.value = state->out->p_users.nprim;
1020 		state->usermap[user->s.value - 1] = new_user->s.value;
1021 
1022 		new_id = strdup(id);
1023 		if (!new_id) {
1024 			ERR(state->handle, "Out of memory!");
1025 			free(new_user);
1026 			return -1;
1027 		}
1028 		ret = hashtab_insert(state->out->p_users.table,
1029 				     (hashtab_key_t) new_id,
1030 				     (hashtab_datum_t) new_user);
1031 		if (ret) {
1032 			ERR(state->handle, "hashtab overflow");
1033 			user_datum_destroy(new_user);
1034 			free(new_user);
1035 			free(new_id);
1036 			return -1;
1037 		}
1038 
1039 		/* expand the semantic MLS info */
1040 		if (mls_semantic_range_expand(&user->range,
1041 					      &new_user->exp_range,
1042 					      state->out, state->handle)) {
1043 			return -1;
1044 		}
1045 		if (mls_semantic_level_expand(&user->dfltlevel,
1046 					      &new_user->exp_dfltlevel,
1047 					      state->out, state->handle)) {
1048 			return -1;
1049 		}
1050 		if (!mls_level_between(&new_user->exp_dfltlevel,
1051 				       &new_user->exp_range.level[0],
1052 				       &new_user->exp_range.level[1])) {
1053 			ERR(state->handle, "default level not within user "
1054 			    "range");
1055 			return -1;
1056 		}
1057 	} else {
1058 		/* require that the MLS info match */
1059 		mls_range_t tmp_range;
1060 		mls_level_t tmp_level;
1061 
1062 		if (mls_semantic_range_expand(&user->range, &tmp_range,
1063 					      state->out, state->handle)) {
1064 			return -1;
1065 		}
1066 		if (mls_semantic_level_expand(&user->dfltlevel, &tmp_level,
1067 					      state->out, state->handle)) {
1068 			mls_range_destroy(&tmp_range);
1069 			return -1;
1070 		}
1071 		if (!mls_range_eq(&new_user->exp_range, &tmp_range) ||
1072 		    !mls_level_eq(&new_user->exp_dfltlevel, &tmp_level)) {
1073 			mls_range_destroy(&tmp_range);
1074 			mls_level_destroy(&tmp_level);
1075 			return -1;
1076 		}
1077 		mls_range_destroy(&tmp_range);
1078 		mls_level_destroy(&tmp_level);
1079 	}
1080 
1081 	ebitmap_init(&tmp_union);
1082 
1083 	/* get global roles for this user */
1084 	if (role_set_expand(&user->roles, &tmp_union, state->out, state->base, state->rolemap)) {
1085 		ERR(state->handle, "Out of memory!");
1086 		ebitmap_destroy(&tmp_union);
1087 		return -1;
1088 	}
1089 
1090 	if (ebitmap_union(&new_user->roles.roles, &tmp_union)) {
1091 		ERR(state->handle, "Out of memory!");
1092 		ebitmap_destroy(&tmp_union);
1093 		return -1;
1094 	}
1095 	ebitmap_destroy(&tmp_union);
1096 
1097 	return 0;
1098 }
1099 
bool_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1100 static int bool_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1101 			      void *data)
1102 {
1103 	int ret;
1104 	expand_state_t *state;
1105 	cond_bool_datum_t *bool, *new_bool;
1106 	char *id, *new_id;
1107 
1108 	id = key;
1109 	bool = (cond_bool_datum_t *) datum;
1110 	state = (expand_state_t *) data;
1111 
1112 	if (!is_id_enabled(id, state->base, SYM_BOOLS)) {
1113 		/* identifier's scope is not enabled */
1114 		return 0;
1115 	}
1116 
1117 	if (bool->flags & COND_BOOL_FLAGS_TUNABLE) {
1118 		/* Skip tunables */
1119 		return 0;
1120 	}
1121 
1122 	if (state->verbose)
1123 		INFO(state->handle, "copying boolean %s", id);
1124 
1125 	new_bool = (cond_bool_datum_t *) malloc(sizeof(cond_bool_datum_t));
1126 	if (!new_bool) {
1127 		ERR(state->handle, "Out of memory!");
1128 		return -1;
1129 	}
1130 
1131 	new_id = strdup(id);
1132 	if (!new_id) {
1133 		ERR(state->handle, "Out of memory!");
1134 		free(new_bool);
1135 		return -1;
1136 	}
1137 
1138 	state->out->p_bools.nprim++;
1139 	new_bool->s.value = state->out->p_bools.nprim;
1140 
1141 	ret = hashtab_insert(state->out->p_bools.table,
1142 			     (hashtab_key_t) new_id,
1143 			     (hashtab_datum_t) new_bool);
1144 	if (ret) {
1145 		ERR(state->handle, "hashtab overflow");
1146 		free(new_bool);
1147 		free(new_id);
1148 		return -1;
1149 	}
1150 
1151 	state->boolmap[bool->s.value - 1] = new_bool->s.value;
1152 
1153 	new_bool->state = bool->state;
1154 	new_bool->flags = bool->flags;
1155 
1156 	return 0;
1157 }
1158 
sens_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1159 static int sens_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1160 			      void *data)
1161 {
1162 	expand_state_t *state = (expand_state_t *) data;
1163 	level_datum_t *level = (level_datum_t *) datum, *new_level = NULL;
1164 	char *id = (char *)key, *new_id = NULL;
1165 
1166 	if (!is_id_enabled(id, state->base, SYM_LEVELS)) {
1167 		/* identifier's scope is not enabled */
1168 		return 0;
1169 	}
1170 
1171 	if (state->verbose)
1172 		INFO(state->handle, "copying sensitivity level %s", id);
1173 
1174 	new_level = (level_datum_t *) malloc(sizeof(level_datum_t));
1175 	if (!new_level)
1176 		goto out_of_mem;
1177 	level_datum_init(new_level);
1178 	new_level->level = (mls_level_t *) malloc(sizeof(mls_level_t));
1179 	if (!new_level->level)
1180 		goto out_of_mem;
1181 	mls_level_init(new_level->level);
1182 	new_id = strdup(id);
1183 	if (!new_id)
1184 		goto out_of_mem;
1185 
1186 	if (mls_level_cpy(new_level->level, level->level)) {
1187 		goto out_of_mem;
1188 	}
1189 	new_level->isalias = level->isalias;
1190 	state->out->p_levels.nprim++;
1191 
1192 	if (hashtab_insert(state->out->p_levels.table,
1193 			   (hashtab_key_t) new_id,
1194 			   (hashtab_datum_t) new_level)) {
1195 		goto out_of_mem;
1196 	}
1197 	return 0;
1198 
1199       out_of_mem:
1200 	ERR(state->handle, "Out of memory!");
1201 	if (new_level != NULL && new_level->level != NULL) {
1202 		mls_level_destroy(new_level->level);
1203 		free(new_level->level);
1204 	}
1205 	level_datum_destroy(new_level);
1206 	free(new_level);
1207 	free(new_id);
1208 	return -1;
1209 }
1210 
cats_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1211 static int cats_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1212 			      void *data)
1213 {
1214 	expand_state_t *state = (expand_state_t *) data;
1215 	cat_datum_t *cat = (cat_datum_t *) datum, *new_cat = NULL;
1216 	char *id = (char *)key, *new_id = NULL;
1217 
1218 	if (!is_id_enabled(id, state->base, SYM_CATS)) {
1219 		/* identifier's scope is not enabled */
1220 		return 0;
1221 	}
1222 
1223 	if (state->verbose)
1224 		INFO(state->handle, "copying category attribute %s", id);
1225 
1226 	new_cat = (cat_datum_t *) malloc(sizeof(cat_datum_t));
1227 	if (!new_cat)
1228 		goto out_of_mem;
1229 	cat_datum_init(new_cat);
1230 	new_id = strdup(id);
1231 	if (!new_id)
1232 		goto out_of_mem;
1233 
1234 	new_cat->s.value = cat->s.value;
1235 	new_cat->isalias = cat->isalias;
1236 	state->out->p_cats.nprim++;
1237 	if (hashtab_insert(state->out->p_cats.table,
1238 			   (hashtab_key_t) new_id, (hashtab_datum_t) new_cat)) {
1239 		goto out_of_mem;
1240 	}
1241 
1242 	return 0;
1243 
1244       out_of_mem:
1245 	ERR(state->handle, "Out of memory!");
1246 	cat_datum_destroy(new_cat);
1247 	free(new_cat);
1248 	free(new_id);
1249 	return -1;
1250 }
1251 
copy_role_allows(expand_state_t * state,role_allow_rule_t * rules)1252 static int copy_role_allows(expand_state_t * state, role_allow_rule_t * rules)
1253 {
1254 	unsigned int i, j;
1255 	role_allow_t *cur_allow, *n, *l;
1256 	role_allow_rule_t *cur;
1257 	ebitmap_t roles, new_roles;
1258 	ebitmap_node_t *snode, *tnode;
1259 
1260 	/* start at the end of the list */
1261 	for (l = state->out->role_allow; l && l->next; l = l->next) ;
1262 
1263 	cur = rules;
1264 	while (cur) {
1265 		ebitmap_init(&roles);
1266 		ebitmap_init(&new_roles);
1267 
1268 		if (role_set_expand(&cur->roles, &roles, state->out, state->base, state->rolemap)) {
1269 			ERR(state->handle, "Out of memory!");
1270 			return -1;
1271 		}
1272 
1273 		if (role_set_expand(&cur->new_roles, &new_roles, state->out, state->base, state->rolemap)) {
1274 			ERR(state->handle, "Out of memory!");
1275 			return -1;
1276 		}
1277 
1278 		ebitmap_for_each_positive_bit(&roles, snode, i) {
1279 			ebitmap_for_each_positive_bit(&new_roles, tnode, j) {
1280 				/* check for duplicates */
1281 				cur_allow = state->out->role_allow;
1282 				while (cur_allow) {
1283 					if ((cur_allow->role == i + 1) &&
1284 					    (cur_allow->new_role == j + 1))
1285 						break;
1286 					cur_allow = cur_allow->next;
1287 				}
1288 				if (cur_allow)
1289 					continue;
1290 				n = (role_allow_t *)
1291 				    malloc(sizeof(role_allow_t));
1292 				if (!n) {
1293 					ERR(state->handle, "Out of memory!");
1294 					return -1;
1295 				}
1296 				memset(n, 0, sizeof(role_allow_t));
1297 				n->role = i + 1;
1298 				n->new_role = j + 1;
1299 				if (l) {
1300 					l->next = n;
1301 				} else {
1302 					state->out->role_allow = n;
1303 				}
1304 				l = n;
1305 			}
1306 		}
1307 
1308 		ebitmap_destroy(&roles);
1309 		ebitmap_destroy(&new_roles);
1310 
1311 		cur = cur->next;
1312 	}
1313 
1314 	return 0;
1315 }
1316 
copy_role_trans(expand_state_t * state,role_trans_rule_t * rules)1317 static int copy_role_trans(expand_state_t * state, role_trans_rule_t * rules)
1318 {
1319 	unsigned int i, j, k;
1320 	role_trans_t *n, *l, *cur_trans;
1321 	role_trans_rule_t *cur;
1322 	ebitmap_t roles, types;
1323 	ebitmap_node_t *rnode, *tnode, *cnode;
1324 
1325 	/* start at the end of the list */
1326 	for (l = state->out->role_tr; l && l->next; l = l->next) ;
1327 
1328 	cur = rules;
1329 	while (cur) {
1330 		ebitmap_init(&roles);
1331 		ebitmap_init(&types);
1332 
1333 		if (role_set_expand(&cur->roles, &roles, state->out, state->base, state->rolemap)) {
1334 			ERR(state->handle, "Out of memory!");
1335 			return -1;
1336 		}
1337 		if (expand_convert_type_set
1338 		    (state->out, state->typemap, &cur->types, &types, 1)) {
1339 			ERR(state->handle, "Out of memory!");
1340 			return -1;
1341 		}
1342 		ebitmap_for_each_positive_bit(&roles, rnode, i) {
1343 			ebitmap_for_each_positive_bit(&types, tnode, j) {
1344 				ebitmap_for_each_positive_bit(&cur->classes, cnode, k) {
1345 					cur_trans = state->out->role_tr;
1346 					while (cur_trans) {
1347 						unsigned int mapped_role;
1348 
1349 						mapped_role = state->rolemap[cur->new_role - 1];
1350 
1351 						if ((cur_trans->role ==
1352 								i + 1) &&
1353 						    (cur_trans->type ==
1354 								j + 1) &&
1355 						    (cur_trans->tclass ==
1356 								k + 1)) {
1357 							if (cur_trans->new_role == mapped_role) {
1358 								break;
1359 							} else {
1360 								ERR(state->handle,
1361 									"Conflicting role trans rule %s %s : %s { %s vs %s }",
1362 									state->out->p_role_val_to_name[i],
1363 									state->out->p_type_val_to_name[j],
1364 									state->out->p_class_val_to_name[k],
1365 									state->out->p_role_val_to_name[mapped_role - 1],
1366 									state->out->p_role_val_to_name[cur_trans->new_role - 1]);
1367 								return -1;
1368 							}
1369 						}
1370 						cur_trans = cur_trans->next;
1371 					}
1372 					if (cur_trans)
1373 						continue;
1374 
1375 					n = (role_trans_t *)
1376 						malloc(sizeof(role_trans_t));
1377 					if (!n) {
1378 						ERR(state->handle,
1379 							"Out of memory!");
1380 						return -1;
1381 					}
1382 					memset(n, 0, sizeof(role_trans_t));
1383 					n->role = i + 1;
1384 					n->type = j + 1;
1385 					n->tclass = k + 1;
1386 					n->new_role = state->rolemap
1387 							[cur->new_role - 1];
1388 					if (l)
1389 						l->next = n;
1390 					else
1391 						state->out->role_tr = n;
1392 
1393 					l = n;
1394 				}
1395 			}
1396 		}
1397 
1398 		ebitmap_destroy(&roles);
1399 		ebitmap_destroy(&types);
1400 
1401 		cur = cur->next;
1402 	}
1403 	return 0;
1404 }
1405 
expand_filename_trans(expand_state_t * state,filename_trans_rule_t * rules)1406 static int expand_filename_trans(expand_state_t *state, filename_trans_rule_t *rules)
1407 {
1408 	unsigned int i, j;
1409 	filename_trans_rule_t *cur_rule;
1410 	ebitmap_t stypes, ttypes;
1411 	ebitmap_node_t *snode, *tnode;
1412 	int rc;
1413 
1414 	cur_rule = rules;
1415 	while (cur_rule) {
1416 		uint32_t mapped_otype, present_otype;
1417 
1418 		ebitmap_init(&stypes);
1419 		ebitmap_init(&ttypes);
1420 
1421 		if (expand_convert_type_set(state->out, state->typemap,
1422 					    &cur_rule->stypes, &stypes, 1)) {
1423 			ERR(state->handle, "Out of memory!");
1424 			return -1;
1425 		}
1426 
1427 		if (expand_convert_type_set(state->out, state->typemap,
1428 					    &cur_rule->ttypes, &ttypes, 1)) {
1429 			ERR(state->handle, "Out of memory!");
1430 			return -1;
1431 		}
1432 
1433 		mapped_otype = state->typemap[cur_rule->otype - 1];
1434 
1435 		ebitmap_for_each_positive_bit(&stypes, snode, i) {
1436 			ebitmap_for_each_positive_bit(&ttypes, tnode, j) {
1437 				rc = policydb_filetrans_insert(
1438 					state->out, i + 1, j + 1,
1439 					cur_rule->tclass, cur_rule->name,
1440 					NULL, mapped_otype, &present_otype
1441 				);
1442 				if (rc == SEPOL_EEXIST) {
1443 					/* duplicate rule, ignore */
1444 					if (present_otype == mapped_otype)
1445 						continue;
1446 
1447 					ERR(state->handle, "Conflicting name-based type_transition %s %s:%s \"%s\":  %s vs %s",
1448 					    state->out->p_type_val_to_name[i],
1449 					    state->out->p_type_val_to_name[j],
1450 					    state->out->p_class_val_to_name[cur_rule->tclass - 1],
1451 					    cur_rule->name,
1452 					    state->out->p_type_val_to_name[present_otype - 1],
1453 					    state->out->p_type_val_to_name[mapped_otype - 1]);
1454 					return -1;
1455 				} else if (rc < 0) {
1456 					ERR(state->handle, "Out of memory!");
1457 					return -1;
1458 				}
1459 			}
1460 		}
1461 
1462 		ebitmap_destroy(&stypes);
1463 		ebitmap_destroy(&ttypes);
1464 
1465 		cur_rule = cur_rule->next;
1466 	}
1467 	return 0;
1468 }
1469 
exp_rangetr_helper(uint32_t stype,uint32_t ttype,uint32_t tclass,mls_semantic_range_t * trange,expand_state_t * state)1470 static int exp_rangetr_helper(uint32_t stype, uint32_t ttype, uint32_t tclass,
1471 			      mls_semantic_range_t * trange,
1472 			      expand_state_t * state)
1473 {
1474 	range_trans_t *rt = NULL, key;
1475 	mls_range_t *r, *exp_range = NULL;
1476 	int rc = -1;
1477 
1478 	exp_range = calloc(1, sizeof(*exp_range));
1479 	if (!exp_range) {
1480 		ERR(state->handle, "Out of memory!");
1481 		return -1;
1482 	}
1483 
1484 	if (mls_semantic_range_expand(trange, exp_range, state->out,
1485 				      state->handle))
1486 		goto err;
1487 
1488 	/* check for duplicates/conflicts */
1489 	key.source_type = stype;
1490 	key.target_type = ttype;
1491 	key.target_class = tclass;
1492 	r = hashtab_search(state->out->range_tr, (hashtab_key_t) &key);
1493 	if (r) {
1494 		if (mls_range_eq(r, exp_range)) {
1495 			/* duplicate, ignore */
1496 			mls_range_destroy(exp_range);
1497 			free(exp_range);
1498 			return 0;
1499 		}
1500 
1501 		/* conflict */
1502 		ERR(state->handle,
1503 		    "Conflicting range trans rule %s %s : %s",
1504 		    state->out->p_type_val_to_name[stype - 1],
1505 		    state->out->p_type_val_to_name[ttype - 1],
1506 		    state->out->p_class_val_to_name[tclass - 1]);
1507 		goto err;
1508 	}
1509 
1510 	rt = calloc(1, sizeof(*rt));
1511 	if (!rt) {
1512 		ERR(state->handle, "Out of memory!");
1513 		goto err;
1514 	}
1515 	rt->source_type = stype;
1516 	rt->target_type = ttype;
1517 	rt->target_class = tclass;
1518 
1519 	rc = hashtab_insert(state->out->range_tr, (hashtab_key_t) rt,
1520 			    exp_range);
1521 	if (rc) {
1522 		ERR(state->handle, "Out of memory!");
1523 		goto err;
1524 
1525 	}
1526 
1527 	return 0;
1528 err:
1529 	free(rt);
1530 	if (exp_range) {
1531 		mls_range_destroy(exp_range);
1532 		free(exp_range);
1533 	}
1534 	return -1;
1535 }
1536 
expand_range_trans(expand_state_t * state,range_trans_rule_t * rules)1537 static int expand_range_trans(expand_state_t * state,
1538 			      range_trans_rule_t * rules)
1539 {
1540 	unsigned int i, j, k;
1541 	range_trans_rule_t *rule;
1542 
1543 	ebitmap_t stypes, ttypes;
1544 	ebitmap_node_t *snode, *tnode, *cnode;
1545 
1546 	if (state->verbose)
1547 		INFO(state->handle, "expanding range transitions");
1548 
1549 	for (rule = rules; rule; rule = rule->next) {
1550 		ebitmap_init(&stypes);
1551 		ebitmap_init(&ttypes);
1552 
1553 		/* expand the type sets */
1554 		if (expand_convert_type_set(state->out, state->typemap,
1555 					    &rule->stypes, &stypes, 1)) {
1556 			ERR(state->handle, "Out of memory!");
1557 			return -1;
1558 		}
1559 		if (expand_convert_type_set(state->out, state->typemap,
1560 					    &rule->ttypes, &ttypes, 1)) {
1561 			ebitmap_destroy(&stypes);
1562 			ERR(state->handle, "Out of memory!");
1563 			return -1;
1564 		}
1565 
1566 		/* loop on source type */
1567 		ebitmap_for_each_positive_bit(&stypes, snode, i) {
1568 			/* loop on target type */
1569 			ebitmap_for_each_positive_bit(&ttypes, tnode, j) {
1570 				/* loop on target class */
1571 				ebitmap_for_each_positive_bit(&rule->tclasses, cnode, k) {
1572 					if (exp_rangetr_helper(i + 1,
1573 							       j + 1,
1574 							       k + 1,
1575 							       &rule->trange,
1576 							       state)) {
1577 						ebitmap_destroy(&stypes);
1578 						ebitmap_destroy(&ttypes);
1579 						return -1;
1580 					}
1581 				}
1582 			}
1583 		}
1584 
1585 		ebitmap_destroy(&stypes);
1586 		ebitmap_destroy(&ttypes);
1587 	}
1588 
1589 	return 0;
1590 }
1591 
1592 /* Search for an AV tab node within a hash table with the given key.
1593  * If the node does not exist, create it and return it; otherwise
1594  * return the pre-existing one.
1595 */
find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond,av_extended_perms_t * xperms)1596 static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,
1597 				   avtab_t * avtab, avtab_key_t * key,
1598 				   cond_av_list_t ** cond,
1599 				   av_extended_perms_t *xperms)
1600 {
1601 	avtab_ptr_t node;
1602 	avtab_datum_t avdatum;
1603 	cond_av_list_t *nl;
1604 	int match = 0;
1605 
1606 	/* AVTAB_XPERMS entries are not necessarily unique */
1607 	if (key->specified & AVTAB_XPERMS) {
1608 		if (xperms == NULL) {
1609 			ERR(handle, "searching xperms NULL");
1610 			node = NULL;
1611 		} else {
1612 			node = avtab_search_node(avtab, key);
1613 			while (node) {
1614 				if ((node->datum.xperms->specified == xperms->specified) &&
1615 					(node->datum.xperms->driver == xperms->driver)) {
1616 					match = 1;
1617 					break;
1618 				}
1619 				node = avtab_search_node_next(node, key->specified);
1620 			}
1621 			if (!match)
1622 				node = NULL;
1623 		}
1624 	} else {
1625 		node = avtab_search_node(avtab, key);
1626 	}
1627 
1628 	/* If this is for conditional policies, keep searching in case
1629 	   the node is part of my conditional avtab. */
1630 	if (cond) {
1631 		while (node) {
1632 			if (node->parse_context == cond)
1633 				break;
1634 			node = avtab_search_node_next(node, key->specified);
1635 		}
1636 	}
1637 
1638 	if (!node) {
1639 		memset(&avdatum, 0, sizeof avdatum);
1640 		/*
1641 		 * AUDITDENY, aka DONTAUDIT, are &= assigned, versus |= for
1642 		 * others. Initialize the data accordingly.
1643 		 */
1644 		avdatum.data = key->specified == AVTAB_AUDITDENY ? ~UINT32_C(0) : UINT32_C(0);
1645 		/* this is used to get the node - insertion is actually unique */
1646 		node = avtab_insert_nonunique(avtab, key, &avdatum);
1647 		if (!node) {
1648 			ERR(handle, "hash table overflow");
1649 			return NULL;
1650 		}
1651 		if (cond) {
1652 			node->parse_context = cond;
1653 			nl = (cond_av_list_t *) malloc(sizeof(cond_av_list_t));
1654 			if (!nl) {
1655 				ERR(handle, "Memory error");
1656 				return NULL;
1657 			}
1658 			memset(nl, 0, sizeof(cond_av_list_t));
1659 			nl->node = node;
1660 			nl->next = *cond;
1661 			*cond = nl;
1662 		}
1663 	}
1664 
1665 	return node;
1666 }
1667 
avrule_to_avtab_spec(uint32_t specification)1668 static uint32_t avrule_to_avtab_spec(uint32_t specification)
1669 {
1670 	return (specification == AVRULE_DONTAUDIT) ?
1671 		AVTAB_AUDITDENY : specification;
1672 }
1673 
1674 #define EXPAND_RULE_SUCCESS   1
1675 #define EXPAND_RULE_CONFLICT  0
1676 #define EXPAND_RULE_ERROR    -1
1677 
expand_terule_helper(sepol_handle_t * handle,policydb_t * p,uint32_t * typemap,uint32_t specified,cond_av_list_t ** cond,cond_av_list_t ** other,uint32_t stype,uint32_t ttype,class_perm_node_t * perms,avtab_t * avtab,int enabled)1678 static int expand_terule_helper(sepol_handle_t * handle,
1679 				policydb_t * p, uint32_t * typemap,
1680 				uint32_t specified, cond_av_list_t ** cond,
1681 				cond_av_list_t ** other, uint32_t stype,
1682 				uint32_t ttype, class_perm_node_t * perms,
1683 				avtab_t * avtab, int enabled)
1684 {
1685 	avtab_key_t avkey;
1686 	avtab_datum_t *avdatump;
1687 	avtab_ptr_t node;
1688 	class_perm_node_t *cur;
1689 	int conflict;
1690 	uint32_t oldtype = 0;
1691 
1692 	if (!(specified & (AVRULE_TRANSITION|AVRULE_MEMBER|AVRULE_CHANGE))) {
1693 		ERR(handle, "Invalid specification: %"PRIu32"\n", specified);
1694 		return EXPAND_RULE_ERROR;
1695 	}
1696 
1697 	avkey.specified = avrule_to_avtab_spec(specified);
1698 	avkey.source_type = stype + 1;
1699 	avkey.target_type = ttype + 1;
1700 
1701 	cur = perms;
1702 	while (cur) {
1703 		uint32_t remapped_data =
1704 		    typemap ? typemap[cur->data - 1] : cur->data;
1705 		avkey.target_class = cur->tclass;
1706 
1707 		conflict = 0;
1708 		/* check to see if the expanded TE already exists --
1709 		 * either in the global scope or in another
1710 		 * conditional AV tab */
1711 		node = avtab_search_node(&p->te_avtab, &avkey);
1712 		if (node) {
1713 			conflict = 1;
1714 		} else {
1715 			node = avtab_search_node(&p->te_cond_avtab, &avkey);
1716 			if (node && node->parse_context != other) {
1717 				conflict = 2;
1718 			}
1719 		}
1720 
1721 		if (conflict) {
1722 			avdatump = &node->datum;
1723 			if (specified & AVRULE_TRANSITION) {
1724 				oldtype = avdatump->data;
1725 			} else if (specified & AVRULE_MEMBER) {
1726 				oldtype = avdatump->data;
1727 			} else if (specified & AVRULE_CHANGE) {
1728 				oldtype = avdatump->data;
1729 			}
1730 
1731 			if (oldtype == remapped_data) {
1732 				/* if the duplicate is inside the same scope (eg., unconditional
1733 				 * or in same conditional then ignore it */
1734 				if ((conflict == 1 && cond == NULL)
1735 				    || node->parse_context == cond)
1736 					return EXPAND_RULE_SUCCESS;
1737 				ERR(handle, "duplicate TE rule for %s %s:%s %s",
1738 				    p->p_type_val_to_name[avkey.source_type -
1739 							  1],
1740 				    p->p_type_val_to_name[avkey.target_type -
1741 							  1],
1742 				    p->p_class_val_to_name[avkey.target_class -
1743 							   1],
1744 				    p->p_type_val_to_name[oldtype - 1]);
1745 				return EXPAND_RULE_CONFLICT;
1746 			}
1747 			ERR(handle,
1748 			    "conflicting TE rule for (%s, %s:%s):  old was %s, new is %s",
1749 			    p->p_type_val_to_name[avkey.source_type - 1],
1750 			    p->p_type_val_to_name[avkey.target_type - 1],
1751 			    p->p_class_val_to_name[avkey.target_class - 1],
1752 			    p->p_type_val_to_name[oldtype - 1],
1753 			    p->p_type_val_to_name[remapped_data - 1]);
1754 			return EXPAND_RULE_CONFLICT;
1755 		}
1756 
1757 		node = find_avtab_node(handle, avtab, &avkey, cond, NULL);
1758 		if (!node)
1759 			return -1;
1760 		if (enabled) {
1761 			node->key.specified |= AVTAB_ENABLED;
1762 		} else {
1763 			node->key.specified &= ~AVTAB_ENABLED;
1764 		}
1765 
1766 		avdatump = &node->datum;
1767 		avdatump->data = remapped_data;
1768 
1769 		cur = cur->next;
1770 	}
1771 
1772 	return EXPAND_RULE_SUCCESS;
1773 }
1774 
1775 /* 0 for success -1 indicates failure */
allocate_xperms(sepol_handle_t * handle,avtab_datum_t * avdatump,av_extended_perms_t * extended_perms)1776 static int allocate_xperms(sepol_handle_t * handle, avtab_datum_t * avdatump,
1777 			   av_extended_perms_t * extended_perms)
1778 {
1779 	unsigned int i;
1780 
1781 	avtab_extended_perms_t *xperms = avdatump->xperms;
1782 	if (!xperms) {
1783 		xperms = (avtab_extended_perms_t *)
1784 			calloc(1, sizeof(avtab_extended_perms_t));
1785 		if (!xperms) {
1786 			ERR(handle, "Out of memory!");
1787 			return -1;
1788 		}
1789 		avdatump->xperms = xperms;
1790 	}
1791 
1792 	switch (extended_perms->specified) {
1793 	case AVRULE_XPERMS_IOCTLFUNCTION:
1794 		xperms->specified = AVTAB_XPERMS_IOCTLFUNCTION;
1795 		break;
1796 	case AVRULE_XPERMS_IOCTLDRIVER:
1797 		xperms->specified = AVTAB_XPERMS_IOCTLDRIVER;
1798 		break;
1799 	default:
1800 		return -1;
1801 	}
1802 
1803 	xperms->driver = extended_perms->driver;
1804 	for (i = 0; i < ARRAY_SIZE(xperms->perms); i++)
1805 		xperms->perms[i] |= extended_perms->perms[i];
1806 
1807 	return 0;
1808 }
1809 
expand_avrule_helper(sepol_handle_t * handle,uint32_t specified,cond_av_list_t ** cond,uint32_t stype,uint32_t ttype,class_perm_node_t * perms,avtab_t * avtab,int enabled,av_extended_perms_t * extended_perms)1810 static int expand_avrule_helper(sepol_handle_t * handle,
1811 				uint32_t specified,
1812 				cond_av_list_t ** cond,
1813 				uint32_t stype, uint32_t ttype,
1814 				class_perm_node_t * perms, avtab_t * avtab,
1815 				int enabled, av_extended_perms_t *extended_perms)
1816 {
1817 	avtab_key_t avkey;
1818 	avtab_datum_t *avdatump;
1819 	avtab_ptr_t node;
1820 	class_perm_node_t *cur;
1821 
1822 	/* bail early if dontaudit's are disabled and it's a dontaudit rule */
1823 	if ((specified & (AVRULE_DONTAUDIT|AVRULE_XPERMS_DONTAUDIT))
1824 	     && handle && handle->disable_dontaudit)
1825 			return EXPAND_RULE_SUCCESS;
1826 
1827 	avkey.source_type = stype + 1;
1828 	avkey.target_type = ttype + 1;
1829 	avkey.specified = avrule_to_avtab_spec(specified);
1830 
1831 	cur = perms;
1832 	while (cur) {
1833 		avkey.target_class = cur->tclass;
1834 
1835 		node = find_avtab_node(handle, avtab, &avkey, cond, extended_perms);
1836 		if (!node)
1837 			return EXPAND_RULE_ERROR;
1838 		if (enabled) {
1839 			node->key.specified |= AVTAB_ENABLED;
1840 		} else {
1841 			node->key.specified &= ~AVTAB_ENABLED;
1842 		}
1843 
1844 		avdatump = &node->datum;
1845 		switch (specified) {
1846 		case AVRULE_ALLOWED:
1847 		case AVRULE_AUDITALLOW:
1848 		case AVRULE_NEVERALLOW:
1849 			avdatump->data |= cur->data;
1850 			break;
1851 		case AVRULE_DONTAUDIT:
1852 			avdatump->data &= ~cur->data;
1853 			break;
1854 		case AVRULE_AUDITDENY:
1855 			/* Since a '0' in an auditdeny mask represents
1856 			 * a permission we do NOT want to audit
1857 			 * (dontaudit), we use the '&' operand to
1858 			 * ensure that all '0's in the mask are
1859 			 * retained (much unlike the allow and
1860 			 * auditallow cases).
1861 			 */
1862 			avdatump->data &= cur->data;
1863 			break;
1864 		case AVRULE_XPERMS_ALLOWED:
1865 		case AVRULE_XPERMS_AUDITALLOW:
1866 		case AVRULE_XPERMS_DONTAUDIT:
1867 		case AVRULE_XPERMS_NEVERALLOW:
1868 			if (allocate_xperms(handle, avdatump, extended_perms))
1869 				return EXPAND_RULE_ERROR;
1870 			break;
1871 		default:
1872 			ERR(handle, "Unknown specification: %"PRIu32"\n", specified);
1873 			return EXPAND_RULE_ERROR;
1874 		}
1875 
1876 		cur = cur->next;
1877 	}
1878 	return EXPAND_RULE_SUCCESS;
1879 }
1880 
expand_rule_helper(sepol_handle_t * handle,policydb_t * p,uint32_t * typemap,avrule_t * source_rule,avtab_t * dest_avtab,cond_av_list_t ** cond,cond_av_list_t ** other,int enabled,ebitmap_t * stypes,ebitmap_t * ttypes)1881 static int expand_rule_helper(sepol_handle_t * handle,
1882 			      policydb_t * p, uint32_t * typemap,
1883 			      avrule_t * source_rule, avtab_t * dest_avtab,
1884 			      cond_av_list_t ** cond, cond_av_list_t ** other,
1885 			      int enabled,
1886 			      ebitmap_t * stypes, ebitmap_t * ttypes)
1887 {
1888 	unsigned int i, j;
1889 	int retval;
1890 	ebitmap_node_t *snode, *tnode;
1891 
1892 	ebitmap_for_each_positive_bit(stypes, snode, i) {
1893 		if (source_rule->flags & RULE_SELF) {
1894 			if (source_rule->specified & (AVRULE_AV | AVRULE_XPERMS)) {
1895 				retval = expand_avrule_helper(handle, source_rule->specified,
1896 							      cond, i, i, source_rule->perms,
1897 							      dest_avtab, enabled, source_rule->xperms);
1898 				if (retval != EXPAND_RULE_SUCCESS)
1899 					return retval;
1900 			} else {
1901 				retval = expand_terule_helper(handle, p, typemap,
1902 							      source_rule->specified, cond,
1903 							      other, i, i, source_rule->perms,
1904 							      dest_avtab, enabled);
1905 				if (retval != EXPAND_RULE_SUCCESS)
1906 					return retval;
1907 			}
1908 		}
1909 		ebitmap_for_each_positive_bit(ttypes, tnode, j) {
1910 			if (source_rule->specified & (AVRULE_AV | AVRULE_XPERMS)) {
1911 				retval = expand_avrule_helper(handle, source_rule->specified,
1912 							      cond, i, j, source_rule->perms,
1913 							      dest_avtab, enabled, source_rule->xperms);
1914 				if (retval != EXPAND_RULE_SUCCESS)
1915 					return retval;
1916 			} else {
1917 				retval = expand_terule_helper(handle, p, typemap,
1918 							      source_rule->specified, cond,
1919 							      other, i, j, source_rule->perms,
1920 							      dest_avtab, enabled);
1921 				if (retval != EXPAND_RULE_SUCCESS)
1922 					return retval;
1923 			}
1924 		}
1925 	}
1926 
1927 	return EXPAND_RULE_SUCCESS;
1928 }
1929 
1930 /*
1931  * Expand a rule into a given avtab - checking for conflicting type
1932  * rules in the destination policy.  Return EXPAND_RULE_SUCCESS on
1933  * success, EXPAND_RULE_CONFLICT if the rule conflicts with something
1934  * (and hence was not added), or EXPAND_RULE_ERROR on error.
1935  */
convert_and_expand_rule(sepol_handle_t * handle,policydb_t * dest_pol,uint32_t * typemap,avrule_t * source_rule,avtab_t * dest_avtab,cond_av_list_t ** cond,cond_av_list_t ** other,int enabled,int do_neverallow)1936 static int convert_and_expand_rule(sepol_handle_t * handle,
1937 				   policydb_t * dest_pol, uint32_t * typemap,
1938 				   avrule_t * source_rule, avtab_t * dest_avtab,
1939 				   cond_av_list_t ** cond,
1940 				   cond_av_list_t ** other, int enabled,
1941 				   int do_neverallow)
1942 {
1943 	int retval;
1944 	ebitmap_t stypes, ttypes;
1945 	unsigned char alwaysexpand;
1946 
1947 	if (!do_neverallow && source_rule->specified & AVRULE_NEVERALLOW)
1948 		return EXPAND_RULE_SUCCESS;
1949 	if (!do_neverallow && source_rule->specified & AVRULE_XPERMS_NEVERALLOW)
1950 		return EXPAND_RULE_SUCCESS;
1951 
1952 	ebitmap_init(&stypes);
1953 	ebitmap_init(&ttypes);
1954 
1955 	/* Force expansion for type rules and for self rules. */
1956 	alwaysexpand = ((source_rule->specified & AVRULE_TYPE) ||
1957 			(source_rule->flags & RULE_SELF));
1958 
1959 	if (expand_convert_type_set
1960 	    (dest_pol, typemap, &source_rule->stypes, &stypes, alwaysexpand))
1961 		return EXPAND_RULE_ERROR;
1962 	if (expand_convert_type_set
1963 	    (dest_pol, typemap, &source_rule->ttypes, &ttypes, alwaysexpand))
1964 		return EXPAND_RULE_ERROR;
1965 
1966 	retval = expand_rule_helper(handle, dest_pol, typemap,
1967 				    source_rule, dest_avtab,
1968 				    cond, other, enabled, &stypes, &ttypes);
1969 	ebitmap_destroy(&stypes);
1970 	ebitmap_destroy(&ttypes);
1971 	return retval;
1972 }
1973 
cond_avrule_list_copy(policydb_t * dest_pol,avrule_t * source_rules,avtab_t * dest_avtab,cond_av_list_t ** list,cond_av_list_t ** other,uint32_t * typemap,int enabled,expand_state_t * state)1974 static int cond_avrule_list_copy(policydb_t * dest_pol, avrule_t * source_rules,
1975 				 avtab_t * dest_avtab, cond_av_list_t ** list,
1976 				 cond_av_list_t ** other, uint32_t * typemap,
1977 				 int enabled, expand_state_t * state)
1978 {
1979 	avrule_t *cur;
1980 
1981 	cur = source_rules;
1982 	while (cur) {
1983 		if (convert_and_expand_rule(state->handle, dest_pol,
1984 					    typemap, cur, dest_avtab,
1985 					    list, other, enabled,
1986 					    0) != EXPAND_RULE_SUCCESS) {
1987 			return -1;
1988 		}
1989 
1990 		cur = cur->next;
1991 	}
1992 
1993 	return 0;
1994 }
1995 
cond_node_map_bools(expand_state_t * state,cond_node_t * cn)1996 static int cond_node_map_bools(expand_state_t * state, cond_node_t * cn)
1997 {
1998 	cond_expr_t *cur;
1999 	unsigned int i;
2000 
2001 	cur = cn->expr;
2002 	while (cur) {
2003 		if (cur->bool)
2004 			cur->bool = state->boolmap[cur->bool - 1];
2005 		cur = cur->next;
2006 	}
2007 
2008 	for (i = 0; i < min(cn->nbools, COND_MAX_BOOLS); i++)
2009 		cn->bool_ids[i] = state->boolmap[cn->bool_ids[i] - 1];
2010 
2011 	if (cond_normalize_expr(state->out, cn)) {
2012 		ERR(state->handle, "Error while normalizing conditional");
2013 		return -1;
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 /* copy the nodes in *reverse* order -- the result is that the last
2020  * given conditional appears first in the policy, so as to match the
2021  * behavior of the upstream compiler */
cond_node_copy(expand_state_t * state,cond_node_t * cn)2022 static int cond_node_copy(expand_state_t * state, cond_node_t * cn)
2023 {
2024 	cond_node_t *new_cond, *tmp;
2025 
2026 	if (cn == NULL) {
2027 		return 0;
2028 	}
2029 	if (cond_node_copy(state, cn->next)) {
2030 		return -1;
2031 	}
2032 
2033 	/* If current cond_node_t is of tunable, its effective branch
2034 	 * has been appended to its home decl->avrules list during link
2035 	 * and now we should just skip it. */
2036 	if (cn->flags & COND_NODE_FLAGS_TUNABLE)
2037 		return 0;
2038 
2039 	if (cond_normalize_expr(state->base, cn)) {
2040 		ERR(state->handle, "Error while normalizing conditional");
2041 		return -1;
2042 	}
2043 
2044 	/* create a new temporary conditional node with the booleans
2045 	 * mapped */
2046 	tmp = cond_node_create(state->base, cn);
2047 	if (!tmp) {
2048 		ERR(state->handle, "Out of memory");
2049 		return -1;
2050 	}
2051 
2052 	if (cond_node_map_bools(state, tmp)) {
2053 		cond_node_destroy(tmp);
2054 		free(tmp);
2055 		ERR(state->handle, "Error mapping booleans");
2056 		return -1;
2057 	}
2058 
2059 	new_cond = cond_node_search(state->out, state->out->cond_list, tmp);
2060 	if (!new_cond) {
2061 		cond_node_destroy(tmp);
2062 		free(tmp);
2063 		ERR(state->handle, "Out of memory!");
2064 		return -1;
2065 	}
2066 	cond_node_destroy(tmp);
2067 	free(tmp);
2068 
2069 	if (cond_avrule_list_copy
2070 	    (state->out, cn->avtrue_list, &state->out->te_cond_avtab,
2071 	     &new_cond->true_list, &new_cond->false_list, state->typemap,
2072 	     new_cond->cur_state, state))
2073 		return -1;
2074 	if (cond_avrule_list_copy
2075 	    (state->out, cn->avfalse_list, &state->out->te_cond_avtab,
2076 	     &new_cond->false_list, &new_cond->true_list, state->typemap,
2077 	     !new_cond->cur_state, state))
2078 		return -1;
2079 
2080 	return 0;
2081 }
2082 
context_copy(context_struct_t * dst,context_struct_t * src,expand_state_t * state)2083 static int context_copy(context_struct_t * dst, context_struct_t * src,
2084 			expand_state_t * state)
2085 {
2086 	dst->user = state->usermap[src->user - 1];
2087 	dst->role = state->rolemap[src->role - 1];
2088 	dst->type = state->typemap[src->type - 1];
2089 	return mls_context_cpy(dst, src);
2090 }
2091 
ocontext_copy_xen(expand_state_t * state)2092 static int ocontext_copy_xen(expand_state_t *state)
2093 {
2094 	unsigned int i;
2095 	ocontext_t *c, *n, *l;
2096 
2097 	for (i = 0; i < OCON_NUM; i++) {
2098 		l = NULL;
2099 		for (c = state->base->ocontexts[i]; c; c = c->next) {
2100 			if (i == OCON_XEN_ISID && !c->context[0].user) {
2101 				INFO(state->handle,
2102 				     "No context assigned to SID %s, omitting from policy",
2103 				     c->u.name);
2104 				continue;
2105 			}
2106 			n = malloc(sizeof(ocontext_t));
2107 			if (!n) {
2108 				ERR(state->handle, "Out of memory!");
2109 				return -1;
2110 			}
2111 			memset(n, 0, sizeof(ocontext_t));
2112 			if (l)
2113 				l->next = n;
2114 			else
2115 				state->out->ocontexts[i] = n;
2116 			l = n;
2117 			switch (i) {
2118 			case OCON_XEN_ISID:
2119 				n->sid[0] = c->sid[0];
2120 				break;
2121 			case OCON_XEN_PIRQ:
2122 				n->u.pirq = c->u.pirq;
2123 				break;
2124 			case OCON_XEN_IOPORT:
2125 				n->u.ioport.low_ioport = c->u.ioport.low_ioport;
2126 				n->u.ioport.high_ioport =
2127 					c->u.ioport.high_ioport;
2128 				break;
2129 			case OCON_XEN_IOMEM:
2130 				n->u.iomem.low_iomem  = c->u.iomem.low_iomem;
2131 				n->u.iomem.high_iomem = c->u.iomem.high_iomem;
2132 				break;
2133 			case OCON_XEN_PCIDEVICE:
2134 				n->u.device = c->u.device;
2135 				break;
2136 			case OCON_XEN_DEVICETREE:
2137 				n->u.name = strdup(c->u.name);
2138 				if (!n->u.name) {
2139 					ERR(state->handle, "Out of memory!");
2140 					return -1;
2141 				}
2142 				break;
2143 			default:
2144 				/* shouldn't get here */
2145 				ERR(state->handle, "Unknown ocontext");
2146 				return -1;
2147 			}
2148 			if (context_copy(&n->context[0], &c->context[0],
2149 				state)) {
2150 				ERR(state->handle, "Out of memory!");
2151 				return -1;
2152 			}
2153 		}
2154 	}
2155 	return 0;
2156 }
2157 
ocontext_copy_selinux(expand_state_t * state)2158 static int ocontext_copy_selinux(expand_state_t *state)
2159 {
2160 	unsigned int i, j;
2161 	ocontext_t *c, *n, *l;
2162 
2163 	for (i = 0; i < OCON_NUM; i++) {
2164 		l = NULL;
2165 		for (c = state->base->ocontexts[i]; c; c = c->next) {
2166 			if (i == OCON_ISID && !c->context[0].user) {
2167 				INFO(state->handle,
2168 				     "No context assigned to SID %s, omitting from policy",
2169 				     c->u.name);
2170 				continue;
2171 			}
2172 			n = malloc(sizeof(ocontext_t));
2173 			if (!n) {
2174 				ERR(state->handle, "Out of memory!");
2175 				return -1;
2176 			}
2177 			memset(n, 0, sizeof(ocontext_t));
2178 			if (l)
2179 				l->next = n;
2180 			else
2181 				state->out->ocontexts[i] = n;
2182 			l = n;
2183 			switch (i) {
2184 			case OCON_ISID:
2185 				n->sid[0] = c->sid[0];
2186 				break;
2187 			case OCON_FS:	/* FALLTHROUGH */
2188 			case OCON_NETIF:
2189 				n->u.name = strdup(c->u.name);
2190 				if (!n->u.name) {
2191 					ERR(state->handle, "Out of memory!");
2192 					return -1;
2193 				}
2194 				if (context_copy
2195 				    (&n->context[1], &c->context[1], state)) {
2196 					ERR(state->handle, "Out of memory!");
2197 					return -1;
2198 				}
2199 				break;
2200 			case OCON_IBPKEY:
2201 				n->u.ibpkey.subnet_prefix = c->u.ibpkey.subnet_prefix;
2202 
2203 				n->u.ibpkey.low_pkey = c->u.ibpkey.low_pkey;
2204 				n->u.ibpkey.high_pkey = c->u.ibpkey.high_pkey;
2205 			break;
2206 			case OCON_IBENDPORT:
2207 				n->u.ibendport.dev_name = strdup(c->u.ibendport.dev_name);
2208 				if (!n->u.ibendport.dev_name) {
2209 					ERR(state->handle, "Out of memory!");
2210 					return -1;
2211 				}
2212 				n->u.ibendport.port = c->u.ibendport.port;
2213 				break;
2214 			case OCON_PORT:
2215 				n->u.port.protocol = c->u.port.protocol;
2216 				n->u.port.low_port = c->u.port.low_port;
2217 				n->u.port.high_port = c->u.port.high_port;
2218 				break;
2219 			case OCON_NODE:
2220 				n->u.node.addr = c->u.node.addr;
2221 				n->u.node.mask = c->u.node.mask;
2222 				break;
2223 			case OCON_FSUSE:
2224 				n->v.behavior = c->v.behavior;
2225 				n->u.name = strdup(c->u.name);
2226 				if (!n->u.name) {
2227 					ERR(state->handle, "Out of memory!");
2228 					return -1;
2229 				}
2230 				break;
2231 			case OCON_NODE6:
2232 				for (j = 0; j < 4; j++)
2233 					n->u.node6.addr[j] = c->u.node6.addr[j];
2234 				for (j = 0; j < 4; j++)
2235 					n->u.node6.mask[j] = c->u.node6.mask[j];
2236 				break;
2237 			default:
2238 				/* shouldn't get here */
2239 				ERR(state->handle, "Unknown ocontext");
2240 				return -1;
2241 			}
2242 			if (context_copy(&n->context[0], &c->context[0], state)) {
2243 				ERR(state->handle, "Out of memory!");
2244 				return -1;
2245 			}
2246 		}
2247 	}
2248 	return 0;
2249 }
2250 
ocontext_copy(expand_state_t * state,uint32_t target)2251 static int ocontext_copy(expand_state_t *state, uint32_t target)
2252 {
2253 	int rc = -1;
2254 	switch (target) {
2255 	case SEPOL_TARGET_SELINUX:
2256 		rc = ocontext_copy_selinux(state);
2257 		break;
2258 	case SEPOL_TARGET_XEN:
2259 		rc = ocontext_copy_xen(state);
2260 		break;
2261 	default:
2262 		ERR(state->handle, "Unknown target");
2263 		return -1;
2264 	}
2265 	return rc;
2266 }
2267 
genfs_copy(expand_state_t * state)2268 static int genfs_copy(expand_state_t * state)
2269 {
2270 	ocontext_t *c, *newc, *l;
2271 	genfs_t *genfs, *newgenfs, *end;
2272 
2273 	end = NULL;
2274 	for (genfs = state->base->genfs; genfs; genfs = genfs->next) {
2275 		newgenfs = malloc(sizeof(genfs_t));
2276 		if (!newgenfs) {
2277 			ERR(state->handle, "Out of memory!");
2278 			return -1;
2279 		}
2280 		memset(newgenfs, 0, sizeof(genfs_t));
2281 		newgenfs->fstype = strdup(genfs->fstype);
2282 		if (!newgenfs->fstype) {
2283 			free(newgenfs);
2284 			ERR(state->handle, "Out of memory!");
2285 			return -1;
2286 		}
2287 		if (!end)
2288 			state->out->genfs = newgenfs;
2289 		else
2290 			end->next = newgenfs;
2291 		end = newgenfs;
2292 
2293 		l = NULL;
2294 		for (c = genfs->head; c; c = c->next) {
2295 			newc = malloc(sizeof(ocontext_t));
2296 			if (!newc) {
2297 				ERR(state->handle, "Out of memory!");
2298 				return -1;
2299 			}
2300 			memset(newc, 0, sizeof(ocontext_t));
2301 			newc->u.name = strdup(c->u.name);
2302 			if (!newc->u.name) {
2303 				ERR(state->handle, "Out of memory!");
2304 				free(newc);
2305 				return -1;
2306 			}
2307 			newc->v.sclass = c->v.sclass;
2308 			context_copy(&newc->context[0], &c->context[0], state);
2309 			if (l)
2310 				l->next = newc;
2311 			else
2312 				newgenfs->head = newc;
2313 			l = newc;
2314 		}
2315 	}
2316 	return 0;
2317 }
2318 
type_attr_map(hashtab_key_t key,hashtab_datum_t datum,void * ptr)2319 static int type_attr_map(hashtab_key_t key
2320 			 __attribute__ ((unused)), hashtab_datum_t datum,
2321 			 void *ptr)
2322 {
2323 	type_datum_t *type;
2324 	expand_state_t *state = ptr;
2325 	policydb_t *p = state->out;
2326 	unsigned int i;
2327 	ebitmap_node_t *tnode;
2328 	int value;
2329 
2330 	type = (type_datum_t *) datum;
2331 	value = type->s.value;
2332 
2333 	if (type->flavor == TYPE_ATTRIB) {
2334 		if (!(type->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE)) {
2335 			if (ebitmap_cpy(&p->attr_type_map[value - 1], &type->types)) {
2336 				goto oom;
2337 			}
2338 			ebitmap_for_each_positive_bit(&type->types, tnode, i) {
2339 				if (ebitmap_set_bit(&p->type_attr_map[i], value - 1, 1)) {
2340 					goto oom;
2341 				}
2342 			}
2343 		} else {
2344 			/* Attribute is being expanded, so remove */
2345 			if (ebitmap_set_bit(&p->type_attr_map[value - 1], value - 1, 0)) {
2346 				goto oom;
2347 			}
2348 		}
2349 	} else {
2350 		if (ebitmap_set_bit(&p->attr_type_map[value - 1], value - 1, 1)) {
2351 			goto oom;
2352 		}
2353 	}
2354 
2355 	return 0;
2356 
2357 oom:
2358 	ERR(state->handle, "Out of memory!");
2359 	return -1;
2360 }
2361 
2362 /* converts typeset using typemap and expands into ebitmap_t types using the attributes in the passed in policy.
2363  * this should not be called until after all the blocks have been processed and the attributes in target policy
2364  * are complete. */
expand_convert_type_set(policydb_t * p,uint32_t * typemap,type_set_t * set,ebitmap_t * types,unsigned char alwaysexpand)2365 int expand_convert_type_set(policydb_t * p, uint32_t * typemap,
2366 			    type_set_t * set, ebitmap_t * types,
2367 			    unsigned char alwaysexpand)
2368 {
2369 	type_set_t tmpset;
2370 
2371 	type_set_init(&tmpset);
2372 
2373 	if (map_ebitmap(&set->types, &tmpset.types, typemap))
2374 		return -1;
2375 
2376 	if (map_ebitmap(&set->negset, &tmpset.negset, typemap))
2377 		return -1;
2378 
2379 	tmpset.flags = set->flags;
2380 
2381 	if (type_set_expand(&tmpset, types, p, alwaysexpand))
2382 		return -1;
2383 
2384 	type_set_destroy(&tmpset);
2385 
2386 	return 0;
2387 }
2388 
2389 /* Expand a rule into a given avtab - checking for conflicting type
2390  * rules.  Return 1 on success, 0 if the rule conflicts with something
2391  * (and hence was not added), or -1 on error. */
expand_rule(sepol_handle_t * handle,policydb_t * source_pol,avrule_t * source_rule,avtab_t * dest_avtab,cond_av_list_t ** cond,cond_av_list_t ** other,int enabled)2392 int expand_rule(sepol_handle_t * handle,
2393 		policydb_t * source_pol,
2394 		avrule_t * source_rule, avtab_t * dest_avtab,
2395 		cond_av_list_t ** cond, cond_av_list_t ** other, int enabled)
2396 {
2397 	int retval;
2398 	ebitmap_t stypes, ttypes;
2399 
2400 	if ((source_rule->specified & AVRULE_NEVERALLOW)
2401 		|| (source_rule->specified & AVRULE_XPERMS_NEVERALLOW))
2402 		return 1;
2403 
2404 	ebitmap_init(&stypes);
2405 	ebitmap_init(&ttypes);
2406 
2407 	if (type_set_expand(&source_rule->stypes, &stypes, source_pol, 1))
2408 		return -1;
2409 	if (type_set_expand(&source_rule->ttypes, &ttypes, source_pol, 1))
2410 		return -1;
2411 	retval = expand_rule_helper(handle, source_pol, NULL,
2412 				    source_rule, dest_avtab,
2413 				    cond, other, enabled, &stypes, &ttypes);
2414 	ebitmap_destroy(&stypes);
2415 	ebitmap_destroy(&ttypes);
2416 	return retval;
2417 }
2418 
2419 /* Expand a role set into an ebitmap containing the roles.
2420  * This handles the attribute and flags.
2421  * Attribute expansion depends on if the rolemap is available.
2422  * During module compile the rolemap is not available, the
2423  * possible duplicates of a regular role and the role attribute
2424  * the regular role belongs to could be properly handled by
2425  * copy_role_trans and copy_role_allow.
2426  */
role_set_expand(role_set_t * x,ebitmap_t * r,policydb_t * out,policydb_t * base,uint32_t * rolemap)2427 int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_t * base, uint32_t * rolemap)
2428 {
2429 	unsigned int i;
2430 	ebitmap_node_t *rnode;
2431 	ebitmap_t mapped_roles, roles;
2432 	policydb_t *p = out;
2433 	role_datum_t *role;
2434 
2435 	ebitmap_init(r);
2436 
2437 	if (x->flags & ROLE_STAR) {
2438 		for (i = 0; i < p->p_roles.nprim; i++)
2439 			if (ebitmap_set_bit(r, i, 1))
2440 				return -1;
2441 		return 0;
2442 	}
2443 
2444 	ebitmap_init(&mapped_roles);
2445 	ebitmap_init(&roles);
2446 
2447 	if (rolemap) {
2448 		assert(base != NULL);
2449 		ebitmap_for_each_positive_bit(&x->roles, rnode, i) {
2450 			/* take advantage of p_role_val_to_struct[]
2451 			 * of the base module */
2452 			role = base->role_val_to_struct[i];
2453 			assert(role != NULL);
2454 			if (role->flavor == ROLE_ATTRIB) {
2455 				if (ebitmap_union(&roles,
2456 						  &role->roles))
2457 					goto bad;
2458 			} else {
2459 				if (ebitmap_set_bit(&roles, i, 1))
2460 					goto bad;
2461 			}
2462 		}
2463 		if (map_ebitmap(&roles, &mapped_roles, rolemap))
2464 			goto bad;
2465 	} else {
2466 		if (ebitmap_cpy(&mapped_roles, &x->roles))
2467 			goto bad;
2468 	}
2469 
2470 	ebitmap_for_each_positive_bit(&mapped_roles, rnode, i) {
2471 		if (ebitmap_set_bit(r, i, 1))
2472 			goto bad;
2473 	}
2474 
2475 	ebitmap_destroy(&mapped_roles);
2476 	ebitmap_destroy(&roles);
2477 
2478 	/* if role is to be complimented, invert the entire bitmap here */
2479 	if (x->flags & ROLE_COMP) {
2480 		for (i = 0; i < ebitmap_length(r); i++) {
2481 			if (ebitmap_get_bit(r, i)) {
2482 				if (ebitmap_set_bit(r, i, 0))
2483 					return -1;
2484 			} else {
2485 				if (ebitmap_set_bit(r, i, 1))
2486 					return -1;
2487 			}
2488 		}
2489 	}
2490 	return 0;
2491 
2492 bad:
2493 	ebitmap_destroy(&mapped_roles);
2494 	ebitmap_destroy(&roles);
2495 	return -1;
2496 }
2497 
2498 /* Expand a type set into an ebitmap containing the types. This
2499  * handles the negset, attributes, and flags.
2500  * Attribute expansion depends on several factors:
2501  * - if alwaysexpand is 1, then they will be expanded,
2502  * - if the type set has a negset or flags, then they will be expanded,
2503  * - otherwise, they will not be expanded.
2504  */
type_set_expand(type_set_t * set,ebitmap_t * t,policydb_t * p,unsigned char alwaysexpand)2505 int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p,
2506 		    unsigned char alwaysexpand)
2507 {
2508 	unsigned int i;
2509 	ebitmap_t types, neg_types;
2510 	ebitmap_node_t *tnode;
2511 	unsigned char expand = alwaysexpand || !ebitmap_is_empty(&set->negset) || set->flags;
2512 	type_datum_t *type;
2513 	int rc =-1;
2514 
2515 	ebitmap_init(&types);
2516 	ebitmap_init(t);
2517 
2518 	/* First go through the types and OR all the attributes to types */
2519 	ebitmap_for_each_positive_bit(&set->types, tnode, i) {
2520 		/*
2521 		 * invalid policies might have more types set in the ebitmap than
2522 		 * what's available in the type_val_to_struct mapping
2523 		 */
2524 		if (i >= p->p_types.nprim)
2525 			goto err_types;
2526 
2527 		type = p->type_val_to_struct[i];
2528 
2529 		if (!type) {
2530 			goto err_types;
2531 		}
2532 
2533 		if (type->flavor == TYPE_ATTRIB &&
2534 		    (expand || (type->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE))) {
2535 			if (ebitmap_union(&types, &type->types)) {
2536 				goto err_types;
2537 			}
2538 		} else {
2539 			if (ebitmap_set_bit(&types, i, 1)) {
2540 				goto err_types;
2541 			}
2542 		}
2543 	}
2544 
2545 	/* Now do the same thing for negset */
2546 	ebitmap_init(&neg_types);
2547 	ebitmap_for_each_positive_bit(&set->negset, tnode, i) {
2548 		if (p->type_val_to_struct[i] &&
2549 		    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) {
2550 			if (ebitmap_union
2551 			    (&neg_types,
2552 			     &p->type_val_to_struct[i]->types)) {
2553 				goto err_neg;
2554 			}
2555 		} else {
2556 			if (ebitmap_set_bit(&neg_types, i, 1)) {
2557 				goto err_neg;
2558 			}
2559 		}
2560 	}
2561 
2562 	if (set->flags & TYPE_STAR) {
2563 		/* set all types not in neg_types */
2564 		for (i = 0; i < p->p_types.nprim; i++) {
2565 			if (ebitmap_get_bit(&neg_types, i))
2566 				continue;
2567 			if (p->type_val_to_struct[i] &&
2568 			    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB)
2569 				continue;
2570 			if (ebitmap_set_bit(t, i, 1))
2571 				goto err_neg;
2572 		}
2573 		goto out;
2574 	}
2575 
2576 	ebitmap_for_each_positive_bit(&types, tnode, i) {
2577 		if (!ebitmap_get_bit(&neg_types, i))
2578 			if (ebitmap_set_bit(t, i, 1))
2579 				goto err_neg;
2580 	}
2581 
2582 	if (set->flags & TYPE_COMP) {
2583 		for (i = 0; i < p->p_types.nprim; i++) {
2584 			if (p->type_val_to_struct[i] &&
2585 			    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) {
2586 				assert(!ebitmap_get_bit(t, i));
2587 				continue;
2588 			}
2589 			if (ebitmap_get_bit(t, i)) {
2590 				if (ebitmap_set_bit(t, i, 0))
2591 					goto err_neg;
2592 			} else {
2593 				if (ebitmap_set_bit(t, i, 1))
2594 					goto err_neg;
2595 			}
2596 		}
2597 	}
2598 
2599 	  out:
2600 	rc = 0;
2601 
2602 	  err_neg:
2603 	ebitmap_destroy(&neg_types);
2604 	  err_types:
2605 	ebitmap_destroy(&types);
2606 
2607 	return rc;
2608 }
2609 
copy_neverallow(policydb_t * dest_pol,uint32_t * typemap,avrule_t * source_rule)2610 static int copy_neverallow(policydb_t * dest_pol, uint32_t * typemap,
2611 			   avrule_t * source_rule)
2612 {
2613 	ebitmap_t stypes, ttypes;
2614 	avrule_t *avrule;
2615 	class_perm_node_t *cur_perm, *new_perm, *tail_perm;
2616 	av_extended_perms_t *xperms = NULL;
2617 
2618 	ebitmap_init(&stypes);
2619 	ebitmap_init(&ttypes);
2620 
2621 	if (expand_convert_type_set
2622 	    (dest_pol, typemap, &source_rule->stypes, &stypes, 1))
2623 		return -1;
2624 	if (expand_convert_type_set
2625 	    (dest_pol, typemap, &source_rule->ttypes, &ttypes, 1))
2626 		return -1;
2627 
2628 	avrule = (avrule_t *) malloc(sizeof(avrule_t));
2629 	if (!avrule)
2630 		return -1;
2631 
2632 	avrule_init(avrule);
2633 	avrule->specified = source_rule->specified;
2634 	avrule->line = source_rule->line;
2635 	avrule->flags = source_rule->flags;
2636 	avrule->source_line = source_rule->source_line;
2637 	if (source_rule->source_filename) {
2638 		avrule->source_filename = strdup(source_rule->source_filename);
2639 		if (!avrule->source_filename)
2640 			goto err;
2641 	}
2642 
2643 	if (ebitmap_cpy(&avrule->stypes.types, &stypes))
2644 		goto err;
2645 
2646 	if (ebitmap_cpy(&avrule->ttypes.types, &ttypes))
2647 		goto err;
2648 
2649 	cur_perm = source_rule->perms;
2650 	tail_perm = NULL;
2651 	while (cur_perm) {
2652 		new_perm =
2653 		    (class_perm_node_t *) malloc(sizeof(class_perm_node_t));
2654 		if (!new_perm)
2655 			goto err;
2656 		class_perm_node_init(new_perm);
2657 		new_perm->tclass = cur_perm->tclass;
2658 		assert(new_perm->tclass);
2659 
2660 		/* once we have modules with permissions we'll need to map the permissions (and classes) */
2661 		new_perm->data = cur_perm->data;
2662 
2663 		if (!avrule->perms)
2664 			avrule->perms = new_perm;
2665 
2666 		if (tail_perm)
2667 			tail_perm->next = new_perm;
2668 		tail_perm = new_perm;
2669 		cur_perm = cur_perm->next;
2670 	}
2671 
2672 	/* copy over extended permissions */
2673 	if (source_rule->xperms) {
2674 		xperms = calloc(1, sizeof(av_extended_perms_t));
2675 		if (!xperms)
2676 			goto err;
2677 		memcpy(xperms, source_rule->xperms, sizeof(av_extended_perms_t));
2678 		avrule->xperms = xperms;
2679 	}
2680 
2681 	/* just prepend the avrule to the first branch; it'll never be
2682 	   written to disk */
2683 	if (!dest_pol->global->branch_list->avrules)
2684 		dest_pol->global->branch_list->avrules = avrule;
2685 	else {
2686 		avrule->next = dest_pol->global->branch_list->avrules;
2687 		dest_pol->global->branch_list->avrules = avrule;
2688 	}
2689 
2690 	ebitmap_destroy(&stypes);
2691 	ebitmap_destroy(&ttypes);
2692 
2693 	return 0;
2694 
2695       err:
2696 	ebitmap_destroy(&stypes);
2697 	ebitmap_destroy(&ttypes);
2698 	ebitmap_destroy(&avrule->stypes.types);
2699 	ebitmap_destroy(&avrule->ttypes.types);
2700 	cur_perm = avrule->perms;
2701 	while (cur_perm) {
2702 		tail_perm = cur_perm->next;
2703 		free(cur_perm);
2704 		cur_perm = tail_perm;
2705 	}
2706 	free(xperms);
2707 	free(avrule);
2708 	return -1;
2709 }
2710 
2711 /*
2712  * Expands the avrule blocks for a policy. RBAC rules are copied. Neverallow
2713  * rules are copied or expanded as per the settings in the state object; all
2714  * other AV rules are expanded.  If neverallow rules are expanded, they are not
2715  * copied, otherwise they are copied for later use by the assertion checker.
2716  */
copy_and_expand_avrule_block(expand_state_t * state)2717 static int copy_and_expand_avrule_block(expand_state_t * state)
2718 {
2719 	avrule_block_t *curblock = state->base->global;
2720 	avrule_block_t *prevblock;
2721 	int retval = -1;
2722 
2723 	if (avtab_alloc(&state->out->te_avtab, MAX_AVTAB_SIZE)) {
2724  		ERR(state->handle, "Out of Memory!");
2725  		return -1;
2726  	}
2727 
2728  	if (avtab_alloc(&state->out->te_cond_avtab, MAX_AVTAB_SIZE)) {
2729  		ERR(state->handle, "Out of Memory!");
2730  		return -1;
2731  	}
2732 
2733 	while (curblock) {
2734 		avrule_decl_t *decl = curblock->enabled;
2735 		avrule_t *cur_avrule;
2736 
2737 		if (decl == NULL) {
2738 			/* nothing was enabled within this block */
2739 			goto cont;
2740 		}
2741 
2742 		/* copy role allows and role trans */
2743 		if (copy_role_allows(state, decl->role_allow_rules) != 0 ||
2744 		    copy_role_trans(state, decl->role_tr_rules) != 0) {
2745 			goto cleanup;
2746 		}
2747 
2748 		if (expand_filename_trans(state, decl->filename_trans_rules))
2749 			goto cleanup;
2750 
2751 		/* expand the range transition rules */
2752 		if (expand_range_trans(state, decl->range_tr_rules))
2753 			goto cleanup;
2754 
2755 		/* copy rules */
2756 		cur_avrule = decl->avrules;
2757 		while (cur_avrule != NULL) {
2758 			if (!(state->expand_neverallow)
2759 			    && cur_avrule->specified & (AVRULE_NEVERALLOW | AVRULE_XPERMS_NEVERALLOW)) {
2760 				/* copy this over directly so that assertions are checked later */
2761 				if (copy_neverallow
2762 				    (state->out, state->typemap, cur_avrule))
2763 					ERR(state->handle,
2764 					    "Error while copying neverallow.");
2765 			} else {
2766 				if (cur_avrule->specified & (AVRULE_NEVERALLOW | AVRULE_XPERMS_NEVERALLOW))
2767 					state->out->unsupported_format = 1;
2768 				if (convert_and_expand_rule
2769 				    (state->handle, state->out, state->typemap,
2770 				     cur_avrule, &state->out->te_avtab, NULL,
2771 				     NULL, 0,
2772 				     state->expand_neverallow) !=
2773 				    EXPAND_RULE_SUCCESS) {
2774 					goto cleanup;
2775 				}
2776 			}
2777 			cur_avrule = cur_avrule->next;
2778 		}
2779 
2780 		/* copy conditional rules */
2781 		if (cond_node_copy(state, decl->cond_list))
2782 			goto cleanup;
2783 
2784       cont:
2785 		prevblock = curblock;
2786 		curblock = curblock->next;
2787 
2788 		if (state->handle && state->handle->expand_consume_base) {
2789 			/* set base top avrule block in case there
2790  			 * is an error condition and the policy needs
2791  			 * to be destroyed */
2792 			state->base->global = curblock;
2793 			avrule_block_destroy(prevblock);
2794 		}
2795 	}
2796 
2797 	retval = 0;
2798 
2799       cleanup:
2800 	return retval;
2801 }
2802 
2803 /*
2804  * This function allows external users of the library (such as setools) to
2805  * expand only the avrules and optionally perform expansion of neverallow rules
2806  * or expand into the same policy for analysis purposes.
2807  */
expand_module_avrules(sepol_handle_t * handle,policydb_t * base,policydb_t * out,uint32_t * typemap,uint32_t * boolmap,uint32_t * rolemap,uint32_t * usermap,int verbose,int expand_neverallow)2808 int expand_module_avrules(sepol_handle_t * handle, policydb_t * base,
2809 			  policydb_t * out, uint32_t * typemap,
2810 			  uint32_t * boolmap, uint32_t * rolemap,
2811 			  uint32_t * usermap, int verbose,
2812 			  int expand_neverallow)
2813 {
2814 	expand_state_t state;
2815 
2816 	expand_state_init(&state);
2817 
2818 	state.base = base;
2819 	state.out = out;
2820 	state.typemap = typemap;
2821 	state.boolmap = boolmap;
2822 	state.rolemap = rolemap;
2823 	state.usermap = usermap;
2824 	state.handle = handle;
2825 	state.verbose = verbose;
2826 	state.expand_neverallow = expand_neverallow;
2827 
2828 	return copy_and_expand_avrule_block(&state);
2829 }
2830 
discard_tunables(sepol_handle_t * sh,policydb_t * pol)2831 static void discard_tunables(sepol_handle_t *sh, policydb_t *pol)
2832 {
2833 	avrule_block_t *block;
2834 	avrule_decl_t *decl;
2835 	cond_node_t *cur_node;
2836 	cond_expr_t *cur_expr;
2837 	int cur_state, preserve_tunables = 0;
2838 	avrule_t *tail, *to_be_appended;
2839 
2840 	if (sh && sh->preserve_tunables)
2841 		preserve_tunables = 1;
2842 
2843 	/* Iterate through all cond_node of all enabled decls, if a cond_node
2844 	 * is about tunable, calculate its state value and concatenate one of
2845 	 * its avrule list to the current decl->avrules list. On the other
2846 	 * hand, the disabled unused branch of a tunable would be discarded.
2847 	 *
2848 	 * Note, such tunable cond_node would be skipped over in expansion,
2849 	 * so we won't have to worry about removing it from decl->cond_list
2850 	 * here :-)
2851 	 *
2852 	 * If tunables are requested to be preserved then they would be
2853 	 * "transformed" as booleans by having their TUNABLE flag cleared.
2854 	 */
2855 	for (block = pol->global; block != NULL; block = block->next) {
2856 		decl = block->enabled;
2857 		if (decl == NULL || decl->enabled == 0)
2858 			continue;
2859 
2860 		tail = decl->avrules;
2861 		while (tail && tail->next)
2862 			tail = tail->next;
2863 
2864 		for (cur_node = decl->cond_list; cur_node != NULL;
2865 		     cur_node = cur_node->next) {
2866 			int booleans, tunables, i;
2867 			cond_bool_datum_t *booldatum;
2868 			cond_bool_datum_t *tmp[COND_EXPR_MAXDEPTH];
2869 
2870 			booleans = tunables = 0;
2871 			memset(tmp, 0, sizeof(cond_bool_datum_t *) * COND_EXPR_MAXDEPTH);
2872 
2873 			for (cur_expr = cur_node->expr; cur_expr != NULL;
2874 			     cur_expr = cur_expr->next) {
2875 				if (cur_expr->expr_type != COND_BOOL)
2876 					continue;
2877 				booldatum = pol->bool_val_to_struct[cur_expr->bool - 1];
2878 				if (booldatum->flags & COND_BOOL_FLAGS_TUNABLE)
2879 					tmp[tunables++] = booldatum;
2880 				else
2881 					booleans++;
2882 			}
2883 
2884 			/* bool_copy_callback() at link phase has ensured
2885 			 * that no mixture of tunables and booleans in one
2886 			 * expression. However, this would be broken by the
2887 			 * request to preserve tunables */
2888 			if (!preserve_tunables)
2889 				assert(!(booleans && tunables));
2890 
2891 			if (booleans || preserve_tunables) {
2892 				cur_node->flags &= ~COND_NODE_FLAGS_TUNABLE;
2893 				if (tunables) {
2894 					for (i = 0; i < tunables; i++)
2895 						tmp[i]->flags &= ~COND_BOOL_FLAGS_TUNABLE;
2896 				}
2897 			} else {
2898 				cur_node->flags |= COND_NODE_FLAGS_TUNABLE;
2899 				cur_state = cond_evaluate_expr(pol, cur_node->expr);
2900 				if (cur_state == -1) {
2901 					printf("Expression result was "
2902 					       "undefined, skipping all"
2903 					       "rules\n");
2904 					continue;
2905 				}
2906 
2907 				to_be_appended = (cur_state == 1) ?
2908 					cur_node->avtrue_list : cur_node->avfalse_list;
2909 
2910 				if (tail)
2911 					tail->next = to_be_appended;
2912 				else
2913 					tail = decl->avrules = to_be_appended;
2914 
2915 				/* Now that the effective branch has been
2916 				 * appended, neutralize its original pointer */
2917 				if (cur_state == 1)
2918 					cur_node->avtrue_list = NULL;
2919 				else
2920 					cur_node->avfalse_list = NULL;
2921 
2922 				/* Update the tail of decl->avrules for
2923 				 * further concatenation */
2924 				while (tail && tail->next)
2925 					tail = tail->next;
2926 			}
2927 		}
2928 	}
2929 }
2930 
2931 /* Linking should always be done before calling expand, even if
2932  * there is only a base since all optionals are dealt with at link time
2933  * the base passed in should be indexed and avrule blocks should be
2934  * enabled.
2935  */
expand_module(sepol_handle_t * handle,policydb_t * base,policydb_t * out,int verbose,int check)2936 int expand_module(sepol_handle_t * handle,
2937 		  policydb_t * base, policydb_t * out, int verbose, int check)
2938 {
2939 	int retval = -1;
2940 	unsigned int i;
2941 	expand_state_t state;
2942 	avrule_block_t *curblock;
2943 
2944 	/* Append tunable's avtrue_list or avfalse_list to the avrules list
2945 	 * of its home decl depending on its state value, so that the effect
2946 	 * rules of a tunable would be added to te_avtab permanently. Whereas
2947 	 * the disabled unused branch would be discarded.
2948 	 *
2949 	 * Originally this function is called at the very end of link phase,
2950 	 * however, we need to keep the linked policy intact for analysis
2951 	 * purpose. */
2952 	discard_tunables(handle, base);
2953 
2954 	expand_state_init(&state);
2955 
2956 	state.verbose = verbose;
2957 	state.typemap = NULL;
2958 	state.base = base;
2959 	state.out = out;
2960 	state.handle = handle;
2961 
2962 	if (base->policy_type != POLICY_BASE) {
2963 		ERR(handle, "Target of expand was not a base policy.");
2964 		return -1;
2965 	}
2966 
2967 	state.out->policy_type = POLICY_KERN;
2968 	state.out->policyvers = POLICYDB_VERSION_MAX;
2969 
2970 	/* Copy mls state from base to out */
2971 	out->mls = base->mls;
2972 	out->handle_unknown = base->handle_unknown;
2973 
2974 	/* Copy target from base to out */
2975 	out->target_platform = base->target_platform;
2976 
2977 	/* Copy policy capabilities */
2978 	if (ebitmap_cpy(&out->policycaps, &base->policycaps)) {
2979 		ERR(handle, "Out of memory!");
2980 		goto cleanup;
2981 	}
2982 
2983 	if ((state.typemap =
2984 	     (uint32_t *) calloc(state.base->p_types.nprim,
2985 				 sizeof(uint32_t))) == NULL) {
2986 		ERR(handle, "Out of memory!");
2987 		goto cleanup;
2988 	}
2989 
2990 	state.boolmap = (uint32_t *)calloc(state.base->p_bools.nprim, sizeof(uint32_t));
2991 	if (!state.boolmap) {
2992 		ERR(handle, "Out of memory!");
2993 		goto cleanup;
2994 	}
2995 
2996 	state.rolemap = (uint32_t *)calloc(state.base->p_roles.nprim, sizeof(uint32_t));
2997 	if (!state.rolemap) {
2998 		ERR(handle, "Out of memory!");
2999 		goto cleanup;
3000 	}
3001 
3002 	state.usermap = (uint32_t *)calloc(state.base->p_users.nprim, sizeof(uint32_t));
3003 	if (!state.usermap) {
3004 		ERR(handle, "Out of memory!");
3005 		goto cleanup;
3006 	}
3007 
3008 	/* order is important - types must be first */
3009 
3010 	/* copy types */
3011 	if (hashtab_map(state.base->p_types.table, type_copy_callback, &state)) {
3012 		goto cleanup;
3013 	}
3014 
3015 	/* convert attribute type sets */
3016 	if (hashtab_map
3017 	    (state.base->p_types.table, attr_convert_callback, &state)) {
3018 		goto cleanup;
3019 	}
3020 
3021 	/* copy commons */
3022 	if (hashtab_map
3023 	    (state.base->p_commons.table, common_copy_callback, &state)) {
3024 		goto cleanup;
3025 	}
3026 
3027 	/* copy classes, note, this does not copy constraints, constraints can't be
3028 	 * copied until after all the blocks have been processed and attributes are complete */
3029 	if (hashtab_map
3030 	    (state.base->p_classes.table, class_copy_callback, &state)) {
3031 		goto cleanup;
3032 	}
3033 
3034 	/* copy type bounds */
3035 	if (hashtab_map(state.base->p_types.table,
3036 			type_bounds_copy_callback, &state))
3037 		goto cleanup;
3038 
3039 	/* copy aliases */
3040 	if (hashtab_map(state.base->p_types.table, alias_copy_callback, &state))
3041 		goto cleanup;
3042 
3043 	/* index here so that type indexes are available for role_copy_callback */
3044 	if (policydb_index_others(handle, out, verbose)) {
3045 		ERR(handle, "Error while indexing out symbols");
3046 		goto cleanup;
3047 	}
3048 
3049 	/* copy roles */
3050 	if (hashtab_map(state.base->p_roles.table, role_copy_callback, &state))
3051 		goto cleanup;
3052 	if (hashtab_map(state.base->p_roles.table,
3053 			role_bounds_copy_callback, &state))
3054 		goto cleanup;
3055 
3056 	/* copy MLS's sensitivity level and categories - this needs to be done
3057 	 * before expanding users (they need to be indexed too) */
3058 	if (hashtab_map(state.base->p_levels.table, sens_copy_callback, &state))
3059 		goto cleanup;
3060 	if (hashtab_map(state.base->p_cats.table, cats_copy_callback, &state))
3061 		goto cleanup;
3062 	if (policydb_index_others(handle, out, verbose)) {
3063 		ERR(handle, "Error while indexing out symbols");
3064 		goto cleanup;
3065 	}
3066 
3067 	/* copy users */
3068 	if (hashtab_map(state.base->p_users.table, user_copy_callback, &state))
3069 		goto cleanup;
3070 	if (hashtab_map(state.base->p_users.table,
3071 			user_bounds_copy_callback, &state))
3072 		goto cleanup;
3073 
3074 	/* copy bools */
3075 	if (hashtab_map(state.base->p_bools.table, bool_copy_callback, &state))
3076 		goto cleanup;
3077 
3078 	if (policydb_index_classes(out)) {
3079 		ERR(handle, "Error while indexing out classes");
3080 		goto cleanup;
3081 	}
3082 	if (policydb_index_others(handle, out, verbose)) {
3083 		ERR(handle, "Error while indexing out symbols");
3084 		goto cleanup;
3085 	}
3086 
3087 	/* loop through all decls and union attributes, roles, users */
3088 	for (curblock = state.base->global; curblock != NULL;
3089 	     curblock = curblock->next) {
3090 		avrule_decl_t *decl = curblock->enabled;
3091 
3092 		if (decl == NULL) {
3093 			/* nothing was enabled within this block */
3094 			continue;
3095 		}
3096 
3097 		/* convert attribute type sets */
3098 		if (hashtab_map
3099 		    (decl->p_types.table, attr_convert_callback, &state)) {
3100 			goto cleanup;
3101 		}
3102 
3103 		/* copy roles */
3104 		if (hashtab_map
3105 		    (decl->p_roles.table, role_copy_callback, &state))
3106 			goto cleanup;
3107 
3108 		/* copy users */
3109 		if (hashtab_map
3110 		    (decl->p_users.table, user_copy_callback, &state))
3111 			goto cleanup;
3112 
3113 	}
3114 
3115 	/* remap role dominates bitmaps */
3116 	 if (hashtab_map(state.out->p_roles.table, role_remap_dominates, &state)) {
3117 		goto cleanup;
3118 	}
3119 
3120 	/* escalate the type_set_t in a role attribute to all regular roles
3121 	 * that belongs to it. */
3122 	if (hashtab_map(state.base->p_roles.table, role_fix_callback, &state))
3123 		goto cleanup;
3124 
3125 	if (copy_and_expand_avrule_block(&state) < 0) {
3126 		ERR(handle, "Error during expand");
3127 		goto cleanup;
3128 	}
3129 
3130 	/* copy constraints */
3131 	if (hashtab_map
3132 	    (state.base->p_classes.table, constraint_copy_callback, &state)) {
3133 		goto cleanup;
3134 	}
3135 
3136 	cond_optimize_lists(state.out->cond_list);
3137 	if (evaluate_conds(state.out))
3138 		goto cleanup;
3139 
3140 	/* copy ocontexts */
3141 	if (ocontext_copy(&state, out->target_platform))
3142 		goto cleanup;
3143 
3144 	/* copy genfs */
3145 	if (genfs_copy(&state))
3146 		goto cleanup;
3147 
3148 	/* Build the type<->attribute maps and remove attributes. */
3149 	state.out->attr_type_map = malloc(state.out->p_types.nprim *
3150 					  sizeof(ebitmap_t));
3151 	state.out->type_attr_map = malloc(state.out->p_types.nprim *
3152 					  sizeof(ebitmap_t));
3153 	if (!state.out->attr_type_map || !state.out->type_attr_map) {
3154 		ERR(handle, "Out of memory!");
3155 		goto cleanup;
3156 	}
3157 	for (i = 0; i < state.out->p_types.nprim; i++) {
3158 		ebitmap_init(&state.out->type_attr_map[i]);
3159 		ebitmap_init(&state.out->attr_type_map[i]);
3160 		/* add the type itself as the degenerate case */
3161 		if (ebitmap_set_bit(&state.out->type_attr_map[i], i, 1)) {
3162 			ERR(handle, "Out of memory!");
3163 			goto cleanup;
3164 		}
3165 	}
3166 	if (hashtab_map(state.out->p_types.table, type_attr_map, &state))
3167 		goto cleanup;
3168 	if (check) {
3169 		if (hierarchy_check_constraints(handle, state.out))
3170 			goto cleanup;
3171 
3172 		if (check_assertions
3173 		    (handle, state.out,
3174 		     state.out->global->branch_list->avrules))
3175 			 goto cleanup;
3176 	}
3177 
3178 	retval = 0;
3179 
3180       cleanup:
3181 	free(state.typemap);
3182 	free(state.boolmap);
3183 	free(state.rolemap);
3184 	free(state.usermap);
3185 	return retval;
3186 }
3187 
expand_avtab_insert(avtab_t * a,avtab_key_t * k,avtab_datum_t * d)3188 static int expand_avtab_insert(avtab_t * a, avtab_key_t * k, avtab_datum_t * d)
3189 {
3190 	avtab_ptr_t node;
3191 	avtab_datum_t *avd;
3192 	avtab_extended_perms_t *xperms;
3193 	unsigned int i;
3194 	unsigned int match = 0;
3195 
3196 	if (k->specified & AVTAB_XPERMS) {
3197 		/*
3198 		 * AVTAB_XPERMS entries are not necessarily unique.
3199 		 * find node with matching xperms
3200 		 */
3201 		node = avtab_search_node(a, k);
3202 		while (node) {
3203 			if ((node->datum.xperms->specified == d->xperms->specified) &&
3204 				(node->datum.xperms->driver == d->xperms->driver)) {
3205 				match = 1;
3206 				break;
3207 			}
3208 			node = avtab_search_node_next(node, k->specified);
3209 		}
3210 		if (!match)
3211 			node = NULL;
3212 	} else {
3213 		node = avtab_search_node(a, k);
3214 	}
3215 
3216 	if (!node || ((k->specified & AVTAB_ENABLED) !=
3217 			(node->key.specified & AVTAB_ENABLED))) {
3218 		node = avtab_insert_nonunique(a, k, d);
3219 		if (!node) {
3220 			ERR(NULL, "Out of memory!");
3221 			return -1;
3222 		}
3223 		return 0;
3224 	}
3225 
3226 	avd = &node->datum;
3227 	xperms = node->datum.xperms;
3228 	switch (k->specified & ~AVTAB_ENABLED) {
3229 	case AVTAB_ALLOWED:
3230 	case AVTAB_AUDITALLOW:
3231 		avd->data |= d->data;
3232 		break;
3233 	case AVTAB_AUDITDENY:
3234 		avd->data &= d->data;
3235 		break;
3236 	case AVTAB_XPERMS_ALLOWED:
3237 	case AVTAB_XPERMS_AUDITALLOW:
3238 	case AVTAB_XPERMS_DONTAUDIT:
3239 		for (i = 0; i < ARRAY_SIZE(xperms->perms); i++)
3240 			xperms->perms[i] |= d->xperms->perms[i];
3241 		break;
3242 	default:
3243 		ERR(NULL, "Type conflict!");
3244 		return -1;
3245 	}
3246 
3247 	return 0;
3248 }
3249 
3250 struct expand_avtab_data {
3251 	avtab_t *expa;
3252 	policydb_t *p;
3253 
3254 };
3255 
expand_avtab_node(avtab_key_t * k,avtab_datum_t * d,void * args)3256 static int expand_avtab_node(avtab_key_t * k, avtab_datum_t * d, void *args)
3257 {
3258 	struct expand_avtab_data *ptr = args;
3259 	avtab_t *expa = ptr->expa;
3260 	policydb_t *p = ptr->p;
3261 	type_datum_t *stype = p->type_val_to_struct[k->source_type - 1];
3262 	type_datum_t *ttype = p->type_val_to_struct[k->target_type - 1];
3263 	ebitmap_t *sattr = &p->attr_type_map[k->source_type - 1];
3264 	ebitmap_t *tattr = &p->attr_type_map[k->target_type - 1];
3265 	ebitmap_node_t *snode, *tnode;
3266 	unsigned int i, j;
3267 	avtab_key_t newkey;
3268 	int rc;
3269 
3270 	newkey.target_class = k->target_class;
3271 	newkey.specified = k->specified;
3272 
3273 	if (stype && ttype && stype->flavor != TYPE_ATTRIB && ttype->flavor != TYPE_ATTRIB) {
3274 		/* Both are individual types, no expansion required. */
3275 		return expand_avtab_insert(expa, k, d);
3276 	}
3277 
3278 	if (stype && stype->flavor != TYPE_ATTRIB) {
3279 		/* Source is an individual type, target is an attribute. */
3280 		newkey.source_type = k->source_type;
3281 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3282 			newkey.target_type = j + 1;
3283 			rc = expand_avtab_insert(expa, &newkey, d);
3284 			if (rc)
3285 				return -1;
3286 		}
3287 		return 0;
3288 	}
3289 
3290 	if (ttype && ttype->flavor != TYPE_ATTRIB) {
3291 		/* Target is an individual type, source is an attribute. */
3292 		newkey.target_type = k->target_type;
3293 		ebitmap_for_each_positive_bit(sattr, snode, i) {
3294 			newkey.source_type = i + 1;
3295 			rc = expand_avtab_insert(expa, &newkey, d);
3296 			if (rc)
3297 				return -1;
3298 		}
3299 		return 0;
3300 	}
3301 
3302 	/* Both source and target type are attributes. */
3303 	ebitmap_for_each_positive_bit(sattr, snode, i) {
3304 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3305 			newkey.source_type = i + 1;
3306 			newkey.target_type = j + 1;
3307 			rc = expand_avtab_insert(expa, &newkey, d);
3308 			if (rc)
3309 				return -1;
3310 		}
3311 	}
3312 
3313 	return 0;
3314 }
3315 
expand_avtab(policydb_t * p,avtab_t * a,avtab_t * expa)3316 int expand_avtab(policydb_t * p, avtab_t * a, avtab_t * expa)
3317 {
3318 	struct expand_avtab_data data;
3319 
3320 	if (avtab_alloc(expa, MAX_AVTAB_SIZE)) {
3321 		ERR(NULL, "Out of memory!");
3322 		return -1;
3323 	}
3324 
3325 	data.expa = expa;
3326 	data.p = p;
3327 	return avtab_map(a, expand_avtab_node, &data);
3328 }
3329 
expand_cond_insert(cond_av_list_t ** l,avtab_t * expa,avtab_key_t * k,avtab_datum_t * d)3330 static int expand_cond_insert(cond_av_list_t ** l,
3331 			      avtab_t * expa,
3332 			      avtab_key_t * k, avtab_datum_t * d)
3333 {
3334 	avtab_ptr_t node;
3335 	avtab_datum_t *avd;
3336 	cond_av_list_t *nl;
3337 
3338 	node = avtab_search_node(expa, k);
3339 	if (!node ||
3340 	    (k->specified & AVTAB_ENABLED) !=
3341 	    (node->key.specified & AVTAB_ENABLED)) {
3342 		node = avtab_insert_nonunique(expa, k, d);
3343 		if (!node) {
3344 			ERR(NULL, "Out of memory!");
3345 			return -1;
3346 		}
3347 		node->parse_context = (void *)1;
3348 		nl = (cond_av_list_t *) malloc(sizeof(*nl));
3349 		if (!nl) {
3350 			ERR(NULL, "Out of memory!");
3351 			return -1;
3352 		}
3353 		memset(nl, 0, sizeof(*nl));
3354 		nl->node = node;
3355 		nl->next = *l;
3356 		*l = nl;
3357 		return 0;
3358 	}
3359 
3360 	avd = &node->datum;
3361 	switch (k->specified & ~AVTAB_ENABLED) {
3362 	case AVTAB_ALLOWED:
3363 	case AVTAB_AUDITALLOW:
3364 		avd->data |= d->data;
3365 		break;
3366 	case AVTAB_AUDITDENY:
3367 		avd->data &= d->data;
3368 		break;
3369 	default:
3370 		ERR(NULL, "Type conflict!");
3371 		return -1;
3372 	}
3373 
3374 	return 0;
3375 }
3376 
expand_cond_av_node(policydb_t * p,avtab_ptr_t node,cond_av_list_t ** newl,avtab_t * expa)3377 static int expand_cond_av_node(policydb_t * p,
3378 			       avtab_ptr_t node,
3379 			       cond_av_list_t ** newl, avtab_t * expa)
3380 {
3381 	avtab_key_t *k = &node->key;
3382 	avtab_datum_t *d = &node->datum;
3383 	type_datum_t *stype = p->type_val_to_struct[k->source_type - 1];
3384 	type_datum_t *ttype = p->type_val_to_struct[k->target_type - 1];
3385 	ebitmap_t *sattr = &p->attr_type_map[k->source_type - 1];
3386 	ebitmap_t *tattr = &p->attr_type_map[k->target_type - 1];
3387 	ebitmap_node_t *snode, *tnode;
3388 	unsigned int i, j;
3389 	avtab_key_t newkey;
3390 	int rc;
3391 
3392 	newkey.target_class = k->target_class;
3393 	newkey.specified = k->specified;
3394 
3395 	if (stype && ttype && stype->flavor != TYPE_ATTRIB && ttype->flavor != TYPE_ATTRIB) {
3396 		/* Both are individual types, no expansion required. */
3397 		return expand_cond_insert(newl, expa, k, d);
3398 	}
3399 
3400 	if (stype && stype->flavor != TYPE_ATTRIB) {
3401 		/* Source is an individual type, target is an attribute. */
3402 		newkey.source_type = k->source_type;
3403 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3404 			newkey.target_type = j + 1;
3405 			rc = expand_cond_insert(newl, expa, &newkey, d);
3406 			if (rc)
3407 				return -1;
3408 		}
3409 		return 0;
3410 	}
3411 
3412 	if (ttype && ttype->flavor != TYPE_ATTRIB) {
3413 		/* Target is an individual type, source is an attribute. */
3414 		newkey.target_type = k->target_type;
3415 		ebitmap_for_each_positive_bit(sattr, snode, i) {
3416 			newkey.source_type = i + 1;
3417 			rc = expand_cond_insert(newl, expa, &newkey, d);
3418 			if (rc)
3419 				return -1;
3420 		}
3421 		return 0;
3422 	}
3423 
3424 	/* Both source and target type are attributes. */
3425 	ebitmap_for_each_positive_bit(sattr, snode, i) {
3426 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3427 			newkey.source_type = i + 1;
3428 			newkey.target_type = j + 1;
3429 			rc = expand_cond_insert(newl, expa, &newkey, d);
3430 			if (rc)
3431 				return -1;
3432 		}
3433 	}
3434 
3435 	return 0;
3436 }
3437 
expand_cond_av_list(policydb_t * p,cond_av_list_t * l,cond_av_list_t ** newl,avtab_t * expa)3438 int expand_cond_av_list(policydb_t * p, cond_av_list_t * l,
3439 			cond_av_list_t ** newl, avtab_t * expa)
3440 {
3441 	cond_av_list_t *cur;
3442 	avtab_ptr_t node;
3443 	int rc;
3444 
3445 	if (avtab_alloc(expa, MAX_AVTAB_SIZE)) {
3446 		ERR(NULL, "Out of memory!");
3447 		return -1;
3448 	}
3449 
3450 	*newl = NULL;
3451 	for (cur = l; cur; cur = cur->next) {
3452 		node = cur->node;
3453 		rc = expand_cond_av_node(p, node, newl, expa);
3454 		if (rc)
3455 			return rc;
3456 	}
3457 
3458 	return 0;
3459 }
3460