• 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!");
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 	/* Invalid sensitivity */
933 	if (sl->sens > p->p_levels.nprim || !p->p_sens_val_to_name[sl->sens - 1])
934 		return -1;
935 
936 	l->sens = sl->sens;
937 	levdatum = (level_datum_t *) hashtab_search(p->p_levels.table,
938 						    p->p_sens_val_to_name[l->sens - 1]);
939 	if (!levdatum) {
940 		ERR(h, "%s: Impossible situation found, nothing in p_levels.table.",
941 		    __func__);
942 		errno = ENOENT;
943 		return -1;
944 	}
945 	for (cat = sl->cat; cat; cat = cat->next) {
946 		if (cat->low > cat->high) {
947 			ERR(h, "Category range is not valid %s.%s",
948 			    p->p_cat_val_to_name[cat->low - 1],
949 			    p->p_cat_val_to_name[cat->high - 1]);
950 			return -1;
951 		}
952 		for (i = cat->low - 1; i < cat->high; i++) {
953 			if (!ebitmap_get_bit(&levdatum->level->cat, i)) {
954 				ERR(h, "Category %s can not be associated with "
955 				    "level %s",
956 				    p->p_cat_val_to_name[i],
957 				    p->p_sens_val_to_name[l->sens - 1]);
958 				return -1;
959 			}
960 			if (ebitmap_set_bit(&l->cat, i, 1)) {
961 				ERR(h, "Out of memory!");
962 				return -1;
963 			}
964 		}
965 	}
966 
967 	return 0;
968 }
969 
mls_semantic_range_expand(mls_semantic_range_t * sr,mls_range_t * r,policydb_t * p,sepol_handle_t * h)970 int mls_semantic_range_expand(mls_semantic_range_t * sr, mls_range_t * r,
971 			      policydb_t * p, sepol_handle_t * h)
972 {
973 	if (mls_semantic_level_expand(&sr->level[0], &r->level[0], p, h) < 0)
974 		return -1;
975 
976 	if (mls_semantic_level_expand(&sr->level[1], &r->level[1], p, h) < 0) {
977 		mls_level_destroy(&r->level[0]);
978 		return -1;
979 	}
980 
981 	if (!mls_level_dom(&r->level[1], &r->level[0])) {
982 		mls_range_destroy(r);
983 		ERR(h, "MLS range high level does not dominate low level");
984 		return -1;
985 	}
986 
987 	return 0;
988 }
989 
user_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)990 static int user_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
991 			      void *data)
992 {
993 	int ret;
994 	expand_state_t *state;
995 	user_datum_t *user;
996 	user_datum_t *new_user;
997 	char *id, *new_id;
998 	ebitmap_t tmp_union;
999 
1000 	id = key;
1001 	user = (user_datum_t *) datum;
1002 	state = (expand_state_t *) data;
1003 
1004 	if (!is_id_enabled(id, state->base, SYM_USERS)) {
1005 		/* identifier's scope is not enabled */
1006 		return 0;
1007 	}
1008 
1009 	if (state->verbose)
1010 		INFO(state->handle, "copying user %s", id);
1011 
1012 	new_user =
1013 	    (user_datum_t *) hashtab_search(state->out->p_users.table, id);
1014 	if (!new_user) {
1015 		new_user = (user_datum_t *) malloc(sizeof(user_datum_t));
1016 		if (!new_user) {
1017 			ERR(state->handle, "Out of memory!");
1018 			return -1;
1019 		}
1020 		memset(new_user, 0, sizeof(user_datum_t));
1021 
1022 		state->out->p_users.nprim++;
1023 		new_user->s.value = state->out->p_users.nprim;
1024 		state->usermap[user->s.value - 1] = new_user->s.value;
1025 
1026 		new_id = strdup(id);
1027 		if (!new_id) {
1028 			ERR(state->handle, "Out of memory!");
1029 			free(new_user);
1030 			return -1;
1031 		}
1032 		ret = hashtab_insert(state->out->p_users.table,
1033 				     (hashtab_key_t) new_id,
1034 				     (hashtab_datum_t) new_user);
1035 		if (ret) {
1036 			ERR(state->handle, "hashtab overflow");
1037 			user_datum_destroy(new_user);
1038 			free(new_user);
1039 			free(new_id);
1040 			return -1;
1041 		}
1042 
1043 		/* expand the semantic MLS info */
1044 		if (mls_semantic_range_expand(&user->range,
1045 					      &new_user->exp_range,
1046 					      state->out, state->handle)) {
1047 			return -1;
1048 		}
1049 		if (mls_semantic_level_expand(&user->dfltlevel,
1050 					      &new_user->exp_dfltlevel,
1051 					      state->out, state->handle)) {
1052 			return -1;
1053 		}
1054 		if (!mls_level_between(&new_user->exp_dfltlevel,
1055 				       &new_user->exp_range.level[0],
1056 				       &new_user->exp_range.level[1])) {
1057 			ERR(state->handle, "default level not within user "
1058 			    "range");
1059 			return -1;
1060 		}
1061 	} else {
1062 		/* require that the MLS info match */
1063 		mls_range_t tmp_range;
1064 		mls_level_t tmp_level;
1065 
1066 		if (mls_semantic_range_expand(&user->range, &tmp_range,
1067 					      state->out, state->handle)) {
1068 			return -1;
1069 		}
1070 		if (mls_semantic_level_expand(&user->dfltlevel, &tmp_level,
1071 					      state->out, state->handle)) {
1072 			mls_range_destroy(&tmp_range);
1073 			return -1;
1074 		}
1075 		if (!mls_range_eq(&new_user->exp_range, &tmp_range) ||
1076 		    !mls_level_eq(&new_user->exp_dfltlevel, &tmp_level)) {
1077 			mls_range_destroy(&tmp_range);
1078 			mls_level_destroy(&tmp_level);
1079 			return -1;
1080 		}
1081 		mls_range_destroy(&tmp_range);
1082 		mls_level_destroy(&tmp_level);
1083 	}
1084 
1085 	ebitmap_init(&tmp_union);
1086 
1087 	/* get global roles for this user */
1088 	if (role_set_expand(&user->roles, &tmp_union, state->out, state->base, state->rolemap)) {
1089 		ERR(state->handle, "Out of memory!");
1090 		ebitmap_destroy(&tmp_union);
1091 		return -1;
1092 	}
1093 
1094 	if (ebitmap_union(&new_user->roles.roles, &tmp_union)) {
1095 		ERR(state->handle, "Out of memory!");
1096 		ebitmap_destroy(&tmp_union);
1097 		return -1;
1098 	}
1099 	ebitmap_destroy(&tmp_union);
1100 
1101 	return 0;
1102 }
1103 
bool_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1104 static int bool_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1105 			      void *data)
1106 {
1107 	int ret;
1108 	expand_state_t *state;
1109 	cond_bool_datum_t *bool, *new_bool;
1110 	char *id, *new_id;
1111 
1112 	id = key;
1113 	bool = (cond_bool_datum_t *) datum;
1114 	state = (expand_state_t *) data;
1115 
1116 	if (!is_id_enabled(id, state->base, SYM_BOOLS)) {
1117 		/* identifier's scope is not enabled */
1118 		return 0;
1119 	}
1120 
1121 	if (bool->flags & COND_BOOL_FLAGS_TUNABLE) {
1122 		/* Skip tunables */
1123 		return 0;
1124 	}
1125 
1126 	if (state->verbose)
1127 		INFO(state->handle, "copying boolean %s", id);
1128 
1129 	new_bool = (cond_bool_datum_t *) malloc(sizeof(cond_bool_datum_t));
1130 	if (!new_bool) {
1131 		ERR(state->handle, "Out of memory!");
1132 		return -1;
1133 	}
1134 
1135 	new_id = strdup(id);
1136 	if (!new_id) {
1137 		ERR(state->handle, "Out of memory!");
1138 		free(new_bool);
1139 		return -1;
1140 	}
1141 
1142 	state->out->p_bools.nprim++;
1143 	new_bool->s.value = state->out->p_bools.nprim;
1144 
1145 	ret = hashtab_insert(state->out->p_bools.table,
1146 			     (hashtab_key_t) new_id,
1147 			     (hashtab_datum_t) new_bool);
1148 	if (ret) {
1149 		ERR(state->handle, "hashtab overflow");
1150 		free(new_bool);
1151 		free(new_id);
1152 		return -1;
1153 	}
1154 
1155 	state->boolmap[bool->s.value - 1] = new_bool->s.value;
1156 
1157 	new_bool->state = bool->state;
1158 	new_bool->flags = bool->flags;
1159 
1160 	return 0;
1161 }
1162 
sens_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1163 static int sens_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1164 			      void *data)
1165 {
1166 	expand_state_t *state = (expand_state_t *) data;
1167 	level_datum_t *level = (level_datum_t *) datum, *new_level = NULL;
1168 	char *id = (char *)key, *new_id = NULL;
1169 
1170 	if (!is_id_enabled(id, state->base, SYM_LEVELS)) {
1171 		/* identifier's scope is not enabled */
1172 		return 0;
1173 	}
1174 
1175 	if (state->verbose)
1176 		INFO(state->handle, "copying sensitivity level %s", id);
1177 
1178 	new_level = (level_datum_t *) malloc(sizeof(level_datum_t));
1179 	if (!new_level)
1180 		goto out_of_mem;
1181 	level_datum_init(new_level);
1182 	new_level->level = (mls_level_t *) malloc(sizeof(mls_level_t));
1183 	if (!new_level->level)
1184 		goto out_of_mem;
1185 	mls_level_init(new_level->level);
1186 	new_id = strdup(id);
1187 	if (!new_id)
1188 		goto out_of_mem;
1189 
1190 	if (mls_level_cpy(new_level->level, level->level)) {
1191 		goto out_of_mem;
1192 	}
1193 	new_level->isalias = level->isalias;
1194 	state->out->p_levels.nprim++;
1195 
1196 	if (hashtab_insert(state->out->p_levels.table,
1197 			   (hashtab_key_t) new_id,
1198 			   (hashtab_datum_t) new_level)) {
1199 		goto out_of_mem;
1200 	}
1201 	return 0;
1202 
1203       out_of_mem:
1204 	ERR(state->handle, "Out of memory!");
1205 	if (new_level != NULL && new_level->level != NULL) {
1206 		mls_level_destroy(new_level->level);
1207 		free(new_level->level);
1208 	}
1209 	level_datum_destroy(new_level);
1210 	free(new_level);
1211 	free(new_id);
1212 	return -1;
1213 }
1214 
cats_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1215 static int cats_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1216 			      void *data)
1217 {
1218 	expand_state_t *state = (expand_state_t *) data;
1219 	cat_datum_t *cat = (cat_datum_t *) datum, *new_cat = NULL;
1220 	char *id = (char *)key, *new_id = NULL;
1221 
1222 	if (!is_id_enabled(id, state->base, SYM_CATS)) {
1223 		/* identifier's scope is not enabled */
1224 		return 0;
1225 	}
1226 
1227 	if (state->verbose)
1228 		INFO(state->handle, "copying category attribute %s", id);
1229 
1230 	new_cat = (cat_datum_t *) malloc(sizeof(cat_datum_t));
1231 	if (!new_cat)
1232 		goto out_of_mem;
1233 	cat_datum_init(new_cat);
1234 	new_id = strdup(id);
1235 	if (!new_id)
1236 		goto out_of_mem;
1237 
1238 	new_cat->s.value = cat->s.value;
1239 	new_cat->isalias = cat->isalias;
1240 	state->out->p_cats.nprim++;
1241 	if (hashtab_insert(state->out->p_cats.table,
1242 			   (hashtab_key_t) new_id, (hashtab_datum_t) new_cat)) {
1243 		goto out_of_mem;
1244 	}
1245 
1246 	return 0;
1247 
1248       out_of_mem:
1249 	ERR(state->handle, "Out of memory!");
1250 	cat_datum_destroy(new_cat);
1251 	free(new_cat);
1252 	free(new_id);
1253 	return -1;
1254 }
1255 
copy_role_allows(expand_state_t * state,role_allow_rule_t * rules)1256 static int copy_role_allows(expand_state_t * state, role_allow_rule_t * rules)
1257 {
1258 	unsigned int i, j;
1259 	role_allow_t *cur_allow, *n, *l;
1260 	role_allow_rule_t *cur;
1261 	ebitmap_t roles, new_roles;
1262 	ebitmap_node_t *snode, *tnode;
1263 
1264 	/* start at the end of the list */
1265 	for (l = state->out->role_allow; l && l->next; l = l->next) ;
1266 
1267 	cur = rules;
1268 	while (cur) {
1269 		ebitmap_init(&roles);
1270 		ebitmap_init(&new_roles);
1271 
1272 		if (role_set_expand(&cur->roles, &roles, state->out, state->base, state->rolemap)) {
1273 			ERR(state->handle, "Out of memory!");
1274 			return -1;
1275 		}
1276 
1277 		if (role_set_expand(&cur->new_roles, &new_roles, state->out, state->base, state->rolemap)) {
1278 			ERR(state->handle, "Out of memory!");
1279 			return -1;
1280 		}
1281 
1282 		ebitmap_for_each_positive_bit(&roles, snode, i) {
1283 			ebitmap_for_each_positive_bit(&new_roles, tnode, j) {
1284 				/* check for duplicates */
1285 				cur_allow = state->out->role_allow;
1286 				while (cur_allow) {
1287 					if ((cur_allow->role == i + 1) &&
1288 					    (cur_allow->new_role == j + 1))
1289 						break;
1290 					cur_allow = cur_allow->next;
1291 				}
1292 				if (cur_allow)
1293 					continue;
1294 				n = (role_allow_t *)
1295 				    malloc(sizeof(role_allow_t));
1296 				if (!n) {
1297 					ERR(state->handle, "Out of memory!");
1298 					return -1;
1299 				}
1300 				memset(n, 0, sizeof(role_allow_t));
1301 				n->role = i + 1;
1302 				n->new_role = j + 1;
1303 				if (l) {
1304 					l->next = n;
1305 				} else {
1306 					state->out->role_allow = n;
1307 				}
1308 				l = n;
1309 			}
1310 		}
1311 
1312 		ebitmap_destroy(&roles);
1313 		ebitmap_destroy(&new_roles);
1314 
1315 		cur = cur->next;
1316 	}
1317 
1318 	return 0;
1319 }
1320 
copy_role_trans(expand_state_t * state,role_trans_rule_t * rules)1321 static int copy_role_trans(expand_state_t * state, role_trans_rule_t * rules)
1322 {
1323 	unsigned int i, j, k;
1324 	role_trans_t *n, *l, *cur_trans;
1325 	role_trans_rule_t *cur;
1326 	ebitmap_t roles, types;
1327 	ebitmap_node_t *rnode, *tnode, *cnode;
1328 
1329 	/* start at the end of the list */
1330 	for (l = state->out->role_tr; l && l->next; l = l->next) ;
1331 
1332 	cur = rules;
1333 	while (cur) {
1334 		ebitmap_init(&roles);
1335 		ebitmap_init(&types);
1336 
1337 		if (role_set_expand(&cur->roles, &roles, state->out, state->base, state->rolemap)) {
1338 			ERR(state->handle, "Out of memory!");
1339 			return -1;
1340 		}
1341 		if (expand_convert_type_set
1342 		    (state->out, state->typemap, &cur->types, &types, 1)) {
1343 			ERR(state->handle, "Out of memory!");
1344 			return -1;
1345 		}
1346 		ebitmap_for_each_positive_bit(&roles, rnode, i) {
1347 			ebitmap_for_each_positive_bit(&types, tnode, j) {
1348 				ebitmap_for_each_positive_bit(&cur->classes, cnode, k) {
1349 					cur_trans = state->out->role_tr;
1350 					while (cur_trans) {
1351 						unsigned int mapped_role;
1352 
1353 						mapped_role = state->rolemap[cur->new_role - 1];
1354 
1355 						if ((cur_trans->role ==
1356 								i + 1) &&
1357 						    (cur_trans->type ==
1358 								j + 1) &&
1359 						    (cur_trans->tclass ==
1360 								k + 1)) {
1361 							if (cur_trans->new_role == mapped_role) {
1362 								break;
1363 							} else {
1364 								ERR(state->handle,
1365 									"Conflicting role trans rule %s %s : %s { %s vs %s }",
1366 									state->out->p_role_val_to_name[i],
1367 									state->out->p_type_val_to_name[j],
1368 									state->out->p_class_val_to_name[k],
1369 									state->out->p_role_val_to_name[mapped_role - 1],
1370 									state->out->p_role_val_to_name[cur_trans->new_role - 1]);
1371 								return -1;
1372 							}
1373 						}
1374 						cur_trans = cur_trans->next;
1375 					}
1376 					if (cur_trans)
1377 						continue;
1378 
1379 					n = (role_trans_t *)
1380 						malloc(sizeof(role_trans_t));
1381 					if (!n) {
1382 						ERR(state->handle,
1383 							"Out of memory!");
1384 						return -1;
1385 					}
1386 					memset(n, 0, sizeof(role_trans_t));
1387 					n->role = i + 1;
1388 					n->type = j + 1;
1389 					n->tclass = k + 1;
1390 					n->new_role = state->rolemap
1391 							[cur->new_role - 1];
1392 					if (l)
1393 						l->next = n;
1394 					else
1395 						state->out->role_tr = n;
1396 
1397 					l = n;
1398 				}
1399 			}
1400 		}
1401 
1402 		ebitmap_destroy(&roles);
1403 		ebitmap_destroy(&types);
1404 
1405 		cur = cur->next;
1406 	}
1407 	return 0;
1408 }
1409 
expand_filename_trans(expand_state_t * state,filename_trans_rule_t * rules)1410 static int expand_filename_trans(expand_state_t *state, filename_trans_rule_t *rules)
1411 {
1412 	unsigned int i, j;
1413 	filename_trans_rule_t *cur_rule;
1414 	ebitmap_t stypes, ttypes;
1415 	ebitmap_node_t *snode, *tnode;
1416 	int rc;
1417 
1418 	cur_rule = rules;
1419 	while (cur_rule) {
1420 		uint32_t mapped_otype, present_otype;
1421 
1422 		ebitmap_init(&stypes);
1423 		ebitmap_init(&ttypes);
1424 
1425 		if (expand_convert_type_set(state->out, state->typemap,
1426 					    &cur_rule->stypes, &stypes, 1)) {
1427 			ERR(state->handle, "Out of memory!");
1428 			return -1;
1429 		}
1430 
1431 		if (expand_convert_type_set(state->out, state->typemap,
1432 					    &cur_rule->ttypes, &ttypes, 1)) {
1433 			ERR(state->handle, "Out of memory!");
1434 			return -1;
1435 		}
1436 
1437 		mapped_otype = state->typemap[cur_rule->otype - 1];
1438 
1439 		ebitmap_for_each_positive_bit(&stypes, snode, i) {
1440 			ebitmap_for_each_positive_bit(&ttypes, tnode, j) {
1441 				rc = policydb_filetrans_insert(
1442 					state->out, i + 1, j + 1,
1443 					cur_rule->tclass, cur_rule->name,
1444 					NULL, mapped_otype, &present_otype
1445 				);
1446 				if (rc == SEPOL_EEXIST) {
1447 					/* duplicate rule, ignore */
1448 					if (present_otype == mapped_otype)
1449 						continue;
1450 
1451 					ERR(state->handle, "Conflicting name-based type_transition %s %s:%s \"%s\":  %s vs %s",
1452 					    state->out->p_type_val_to_name[i],
1453 					    state->out->p_type_val_to_name[j],
1454 					    state->out->p_class_val_to_name[cur_rule->tclass - 1],
1455 					    cur_rule->name,
1456 					    state->out->p_type_val_to_name[present_otype - 1],
1457 					    state->out->p_type_val_to_name[mapped_otype - 1]);
1458 					return -1;
1459 				} else if (rc < 0) {
1460 					ERR(state->handle, "Out of memory!");
1461 					return -1;
1462 				}
1463 			}
1464 		}
1465 
1466 		ebitmap_destroy(&stypes);
1467 		ebitmap_destroy(&ttypes);
1468 
1469 		cur_rule = cur_rule->next;
1470 	}
1471 	return 0;
1472 }
1473 
exp_rangetr_helper(uint32_t stype,uint32_t ttype,uint32_t tclass,mls_semantic_range_t * trange,expand_state_t * state)1474 static int exp_rangetr_helper(uint32_t stype, uint32_t ttype, uint32_t tclass,
1475 			      mls_semantic_range_t * trange,
1476 			      expand_state_t * state)
1477 {
1478 	range_trans_t *rt = NULL, key;
1479 	mls_range_t *r, *exp_range = NULL;
1480 	int rc = -1;
1481 
1482 	exp_range = calloc(1, sizeof(*exp_range));
1483 	if (!exp_range) {
1484 		ERR(state->handle, "Out of memory!");
1485 		return -1;
1486 	}
1487 
1488 	if (mls_semantic_range_expand(trange, exp_range, state->out,
1489 				      state->handle))
1490 		goto err;
1491 
1492 	/* check for duplicates/conflicts */
1493 	key.source_type = stype;
1494 	key.target_type = ttype;
1495 	key.target_class = tclass;
1496 	r = hashtab_search(state->out->range_tr, (hashtab_key_t) &key);
1497 	if (r) {
1498 		if (mls_range_eq(r, exp_range)) {
1499 			/* duplicate, ignore */
1500 			mls_range_destroy(exp_range);
1501 			free(exp_range);
1502 			return 0;
1503 		}
1504 
1505 		/* conflict */
1506 		ERR(state->handle,
1507 		    "Conflicting range trans rule %s %s : %s",
1508 		    state->out->p_type_val_to_name[stype - 1],
1509 		    state->out->p_type_val_to_name[ttype - 1],
1510 		    state->out->p_class_val_to_name[tclass - 1]);
1511 		goto err;
1512 	}
1513 
1514 	rt = calloc(1, sizeof(*rt));
1515 	if (!rt) {
1516 		ERR(state->handle, "Out of memory!");
1517 		goto err;
1518 	}
1519 	rt->source_type = stype;
1520 	rt->target_type = ttype;
1521 	rt->target_class = tclass;
1522 
1523 	rc = hashtab_insert(state->out->range_tr, (hashtab_key_t) rt,
1524 			    exp_range);
1525 	if (rc) {
1526 		ERR(state->handle, "Out of memory!");
1527 		goto err;
1528 
1529 	}
1530 
1531 	return 0;
1532 err:
1533 	free(rt);
1534 	if (exp_range) {
1535 		mls_range_destroy(exp_range);
1536 		free(exp_range);
1537 	}
1538 	return -1;
1539 }
1540 
expand_range_trans(expand_state_t * state,range_trans_rule_t * rules)1541 static int expand_range_trans(expand_state_t * state,
1542 			      range_trans_rule_t * rules)
1543 {
1544 	unsigned int i, j, k;
1545 	range_trans_rule_t *rule;
1546 
1547 	ebitmap_t stypes, ttypes;
1548 	ebitmap_node_t *snode, *tnode, *cnode;
1549 
1550 	if (state->verbose)
1551 		INFO(state->handle, "expanding range transitions");
1552 
1553 	for (rule = rules; rule; rule = rule->next) {
1554 		ebitmap_init(&stypes);
1555 		ebitmap_init(&ttypes);
1556 
1557 		/* expand the type sets */
1558 		if (expand_convert_type_set(state->out, state->typemap,
1559 					    &rule->stypes, &stypes, 1)) {
1560 			ERR(state->handle, "Out of memory!");
1561 			return -1;
1562 		}
1563 		if (expand_convert_type_set(state->out, state->typemap,
1564 					    &rule->ttypes, &ttypes, 1)) {
1565 			ebitmap_destroy(&stypes);
1566 			ERR(state->handle, "Out of memory!");
1567 			return -1;
1568 		}
1569 
1570 		/* loop on source type */
1571 		ebitmap_for_each_positive_bit(&stypes, snode, i) {
1572 			/* loop on target type */
1573 			ebitmap_for_each_positive_bit(&ttypes, tnode, j) {
1574 				/* loop on target class */
1575 				ebitmap_for_each_positive_bit(&rule->tclasses, cnode, k) {
1576 					if (exp_rangetr_helper(i + 1,
1577 							       j + 1,
1578 							       k + 1,
1579 							       &rule->trange,
1580 							       state)) {
1581 						ebitmap_destroy(&stypes);
1582 						ebitmap_destroy(&ttypes);
1583 						return -1;
1584 					}
1585 				}
1586 			}
1587 		}
1588 
1589 		ebitmap_destroy(&stypes);
1590 		ebitmap_destroy(&ttypes);
1591 	}
1592 
1593 	return 0;
1594 }
1595 
1596 /* Search for an AV tab node within a hash table with the given key.
1597  * If the node does not exist, create it and return it; otherwise
1598  * return the pre-existing one.
1599 */
find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond,av_extended_perms_t * xperms)1600 static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,
1601 				   avtab_t * avtab, avtab_key_t * key,
1602 				   cond_av_list_t ** cond,
1603 				   av_extended_perms_t *xperms)
1604 {
1605 	avtab_ptr_t node;
1606 	avtab_datum_t avdatum;
1607 	cond_av_list_t *nl;
1608 	int match = 0;
1609 
1610 	/* AVTAB_XPERMS entries are not necessarily unique */
1611 	if (key->specified & AVTAB_XPERMS) {
1612 		if (xperms == NULL) {
1613 			ERR(handle, "searching xperms NULL");
1614 			node = NULL;
1615 		} else {
1616 			node = avtab_search_node(avtab, key);
1617 			while (node) {
1618 				if ((node->datum.xperms->specified == xperms->specified) &&
1619 					(node->datum.xperms->driver == xperms->driver)) {
1620 					match = 1;
1621 					break;
1622 				}
1623 				node = avtab_search_node_next(node, key->specified);
1624 			}
1625 			if (!match)
1626 				node = NULL;
1627 		}
1628 	} else {
1629 		node = avtab_search_node(avtab, key);
1630 	}
1631 
1632 	/* If this is for conditional policies, keep searching in case
1633 	   the node is part of my conditional avtab. */
1634 	if (cond) {
1635 		while (node) {
1636 			if (node->parse_context == cond)
1637 				break;
1638 			node = avtab_search_node_next(node, key->specified);
1639 		}
1640 	}
1641 
1642 	if (!node) {
1643 		memset(&avdatum, 0, sizeof avdatum);
1644 		/*
1645 		 * AUDITDENY, aka DONTAUDIT, are &= assigned, versus |= for
1646 		 * others. Initialize the data accordingly.
1647 		 */
1648 		avdatum.data = key->specified == AVTAB_AUDITDENY ? ~UINT32_C(0) : UINT32_C(0);
1649 		/* this is used to get the node - insertion is actually unique */
1650 		node = avtab_insert_nonunique(avtab, key, &avdatum);
1651 		if (!node) {
1652 			ERR(handle, "hash table overflow");
1653 			return NULL;
1654 		}
1655 		if (cond) {
1656 			node->parse_context = cond;
1657 			nl = (cond_av_list_t *) malloc(sizeof(cond_av_list_t));
1658 			if (!nl) {
1659 				ERR(handle, "Memory error");
1660 				return NULL;
1661 			}
1662 			memset(nl, 0, sizeof(cond_av_list_t));
1663 			nl->node = node;
1664 			nl->next = *cond;
1665 			*cond = nl;
1666 		}
1667 	}
1668 
1669 	return node;
1670 }
1671 
avrule_to_avtab_spec(uint32_t specification)1672 static uint32_t avrule_to_avtab_spec(uint32_t specification)
1673 {
1674 	return (specification == AVRULE_DONTAUDIT) ?
1675 		AVTAB_AUDITDENY : specification;
1676 }
1677 
1678 #define EXPAND_RULE_SUCCESS   1
1679 #define EXPAND_RULE_CONFLICT  0
1680 #define EXPAND_RULE_ERROR    -1
1681 
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)1682 static int expand_terule_helper(sepol_handle_t * handle,
1683 				policydb_t * p, uint32_t * typemap,
1684 				uint32_t specified, cond_av_list_t ** cond,
1685 				cond_av_list_t ** other, uint32_t stype,
1686 				uint32_t ttype, class_perm_node_t * perms,
1687 				avtab_t * avtab, int enabled)
1688 {
1689 	avtab_key_t avkey;
1690 	avtab_datum_t *avdatump;
1691 	avtab_ptr_t node;
1692 	class_perm_node_t *cur;
1693 	int conflict;
1694 	uint32_t oldtype = 0;
1695 
1696 	if (!(specified & (AVRULE_TRANSITION|AVRULE_MEMBER|AVRULE_CHANGE))) {
1697 		ERR(handle, "Invalid specification: %"PRIu32, specified);
1698 		return EXPAND_RULE_ERROR;
1699 	}
1700 
1701 	avkey.specified = avrule_to_avtab_spec(specified);
1702 	avkey.source_type = stype + 1;
1703 	avkey.target_type = ttype + 1;
1704 
1705 	cur = perms;
1706 	while (cur) {
1707 		uint32_t remapped_data =
1708 		    typemap ? typemap[cur->data - 1] : cur->data;
1709 		avkey.target_class = cur->tclass;
1710 
1711 		conflict = 0;
1712 		/* check to see if the expanded TE already exists --
1713 		 * either in the global scope or in another
1714 		 * conditional AV tab */
1715 		node = avtab_search_node(&p->te_avtab, &avkey);
1716 		if (node) {
1717 			conflict = 1;
1718 		} else {
1719 			node = avtab_search_node(&p->te_cond_avtab, &avkey);
1720 			if (node && node->parse_context != other) {
1721 				conflict = 2;
1722 			}
1723 		}
1724 
1725 		if (conflict) {
1726 			avdatump = &node->datum;
1727 			if (specified & AVRULE_TRANSITION) {
1728 				oldtype = avdatump->data;
1729 			} else if (specified & AVRULE_MEMBER) {
1730 				oldtype = avdatump->data;
1731 			} else if (specified & AVRULE_CHANGE) {
1732 				oldtype = avdatump->data;
1733 			}
1734 
1735 			if (oldtype == remapped_data) {
1736 				/* if the duplicate is inside the same scope (eg., unconditional
1737 				 * or in same conditional then ignore it */
1738 				if ((conflict == 1 && cond == NULL)
1739 				    || node->parse_context == cond)
1740 					return EXPAND_RULE_SUCCESS;
1741 				ERR(handle, "duplicate TE rule for %s %s:%s %s",
1742 				    p->p_type_val_to_name[avkey.source_type -
1743 							  1],
1744 				    p->p_type_val_to_name[avkey.target_type -
1745 							  1],
1746 				    p->p_class_val_to_name[avkey.target_class -
1747 							   1],
1748 				    p->p_type_val_to_name[oldtype - 1]);
1749 				return EXPAND_RULE_CONFLICT;
1750 			}
1751 			ERR(handle,
1752 			    "conflicting TE rule for (%s, %s:%s):  old was %s, new is %s",
1753 			    p->p_type_val_to_name[avkey.source_type - 1],
1754 			    p->p_type_val_to_name[avkey.target_type - 1],
1755 			    p->p_class_val_to_name[avkey.target_class - 1],
1756 			    p->p_type_val_to_name[oldtype - 1],
1757 			    p->p_type_val_to_name[remapped_data - 1]);
1758 			return EXPAND_RULE_CONFLICT;
1759 		}
1760 
1761 		node = find_avtab_node(handle, avtab, &avkey, cond, NULL);
1762 		if (!node)
1763 			return -1;
1764 		if (enabled) {
1765 			node->key.specified |= AVTAB_ENABLED;
1766 		} else {
1767 			node->key.specified &= ~AVTAB_ENABLED;
1768 		}
1769 
1770 		avdatump = &node->datum;
1771 		avdatump->data = remapped_data;
1772 
1773 		cur = cur->next;
1774 	}
1775 
1776 	return EXPAND_RULE_SUCCESS;
1777 }
1778 
1779 /* 0 for success -1 indicates failure */
allocate_xperms(sepol_handle_t * handle,avtab_datum_t * avdatump,av_extended_perms_t * extended_perms)1780 static int allocate_xperms(sepol_handle_t * handle, avtab_datum_t * avdatump,
1781 			   av_extended_perms_t * extended_perms)
1782 {
1783 	unsigned int i;
1784 
1785 	avtab_extended_perms_t *xperms = avdatump->xperms;
1786 	if (!xperms) {
1787 		xperms = (avtab_extended_perms_t *)
1788 			calloc(1, sizeof(avtab_extended_perms_t));
1789 		if (!xperms) {
1790 			ERR(handle, "Out of memory!");
1791 			return -1;
1792 		}
1793 		avdatump->xperms = xperms;
1794 	}
1795 
1796 	switch (extended_perms->specified) {
1797 	case AVRULE_XPERMS_IOCTLFUNCTION:
1798 		xperms->specified = AVTAB_XPERMS_IOCTLFUNCTION;
1799 		break;
1800 	case AVRULE_XPERMS_IOCTLDRIVER:
1801 		xperms->specified = AVTAB_XPERMS_IOCTLDRIVER;
1802 		break;
1803 	default:
1804 		return -1;
1805 	}
1806 
1807 	xperms->driver = extended_perms->driver;
1808 	for (i = 0; i < ARRAY_SIZE(xperms->perms); i++)
1809 		xperms->perms[i] |= extended_perms->perms[i];
1810 
1811 	return 0;
1812 }
1813 
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)1814 static int expand_avrule_helper(sepol_handle_t * handle,
1815 				uint32_t specified,
1816 				cond_av_list_t ** cond,
1817 				uint32_t stype, uint32_t ttype,
1818 				class_perm_node_t * perms, avtab_t * avtab,
1819 				int enabled, av_extended_perms_t *extended_perms)
1820 {
1821 	avtab_key_t avkey;
1822 	avtab_datum_t *avdatump;
1823 	avtab_ptr_t node;
1824 	class_perm_node_t *cur;
1825 
1826 	/* bail early if dontaudit's are disabled and it's a dontaudit rule */
1827 	if ((specified & (AVRULE_DONTAUDIT|AVRULE_XPERMS_DONTAUDIT))
1828 	     && handle && handle->disable_dontaudit)
1829 			return EXPAND_RULE_SUCCESS;
1830 
1831 	avkey.source_type = stype + 1;
1832 	avkey.target_type = ttype + 1;
1833 	avkey.specified = avrule_to_avtab_spec(specified);
1834 
1835 	cur = perms;
1836 	while (cur) {
1837 		avkey.target_class = cur->tclass;
1838 
1839 		node = find_avtab_node(handle, avtab, &avkey, cond, extended_perms);
1840 		if (!node)
1841 			return EXPAND_RULE_ERROR;
1842 		if (enabled) {
1843 			node->key.specified |= AVTAB_ENABLED;
1844 		} else {
1845 			node->key.specified &= ~AVTAB_ENABLED;
1846 		}
1847 
1848 		avdatump = &node->datum;
1849 		switch (specified) {
1850 		case AVRULE_ALLOWED:
1851 		case AVRULE_AUDITALLOW:
1852 		case AVRULE_NEVERALLOW:
1853 			avdatump->data |= cur->data;
1854 			break;
1855 		case AVRULE_DONTAUDIT:
1856 			avdatump->data &= ~cur->data;
1857 			break;
1858 		case AVRULE_AUDITDENY:
1859 			/* Since a '0' in an auditdeny mask represents
1860 			 * a permission we do NOT want to audit
1861 			 * (dontaudit), we use the '&' operand to
1862 			 * ensure that all '0's in the mask are
1863 			 * retained (much unlike the allow and
1864 			 * auditallow cases).
1865 			 */
1866 			avdatump->data &= cur->data;
1867 			break;
1868 		case AVRULE_XPERMS_ALLOWED:
1869 		case AVRULE_XPERMS_AUDITALLOW:
1870 		case AVRULE_XPERMS_DONTAUDIT:
1871 		case AVRULE_XPERMS_NEVERALLOW:
1872 			if (allocate_xperms(handle, avdatump, extended_perms))
1873 				return EXPAND_RULE_ERROR;
1874 			break;
1875 		default:
1876 			ERR(handle, "Unknown specification: %"PRIu32, specified);
1877 			return EXPAND_RULE_ERROR;
1878 		}
1879 
1880 		cur = cur->next;
1881 	}
1882 	return EXPAND_RULE_SUCCESS;
1883 }
1884 
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)1885 static int expand_rule_helper(sepol_handle_t * handle,
1886 			      policydb_t * p, uint32_t * typemap,
1887 			      avrule_t * source_rule, avtab_t * dest_avtab,
1888 			      cond_av_list_t ** cond, cond_av_list_t ** other,
1889 			      int enabled,
1890 			      ebitmap_t * stypes, ebitmap_t * ttypes)
1891 {
1892 	unsigned int i, j;
1893 	int retval;
1894 	ebitmap_node_t *snode, *tnode;
1895 
1896 	ebitmap_for_each_positive_bit(stypes, snode, i) {
1897 		if (source_rule->flags & RULE_SELF) {
1898 			if (source_rule->specified & (AVRULE_AV | AVRULE_XPERMS)) {
1899 				retval = expand_avrule_helper(handle, source_rule->specified,
1900 							      cond, i, i, source_rule->perms,
1901 							      dest_avtab, enabled, source_rule->xperms);
1902 				if (retval != EXPAND_RULE_SUCCESS)
1903 					return retval;
1904 			} else {
1905 				retval = expand_terule_helper(handle, p, typemap,
1906 							      source_rule->specified, cond,
1907 							      other, i, i, source_rule->perms,
1908 							      dest_avtab, enabled);
1909 				if (retval != EXPAND_RULE_SUCCESS)
1910 					return retval;
1911 			}
1912 		}
1913 		ebitmap_for_each_positive_bit(ttypes, tnode, j) {
1914 			if (source_rule->specified & (AVRULE_AV | AVRULE_XPERMS)) {
1915 				retval = expand_avrule_helper(handle, source_rule->specified,
1916 							      cond, i, j, source_rule->perms,
1917 							      dest_avtab, enabled, source_rule->xperms);
1918 				if (retval != EXPAND_RULE_SUCCESS)
1919 					return retval;
1920 			} else {
1921 				retval = expand_terule_helper(handle, p, typemap,
1922 							      source_rule->specified, cond,
1923 							      other, i, j, source_rule->perms,
1924 							      dest_avtab, enabled);
1925 				if (retval != EXPAND_RULE_SUCCESS)
1926 					return retval;
1927 			}
1928 		}
1929 	}
1930 
1931 	return EXPAND_RULE_SUCCESS;
1932 }
1933 
1934 /*
1935  * Expand a rule into a given avtab - checking for conflicting type
1936  * rules in the destination policy.  Return EXPAND_RULE_SUCCESS on
1937  * success, EXPAND_RULE_CONFLICT if the rule conflicts with something
1938  * (and hence was not added), or EXPAND_RULE_ERROR on error.
1939  */
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)1940 static int convert_and_expand_rule(sepol_handle_t * handle,
1941 				   policydb_t * dest_pol, uint32_t * typemap,
1942 				   avrule_t * source_rule, avtab_t * dest_avtab,
1943 				   cond_av_list_t ** cond,
1944 				   cond_av_list_t ** other, int enabled,
1945 				   int do_neverallow)
1946 {
1947 	int retval;
1948 	ebitmap_t stypes, ttypes;
1949 	unsigned char alwaysexpand;
1950 
1951 	if (!do_neverallow && source_rule->specified & AVRULE_NEVERALLOW)
1952 		return EXPAND_RULE_SUCCESS;
1953 	if (!do_neverallow && source_rule->specified & AVRULE_XPERMS_NEVERALLOW)
1954 		return EXPAND_RULE_SUCCESS;
1955 
1956 	ebitmap_init(&stypes);
1957 	ebitmap_init(&ttypes);
1958 
1959 	/* Force expansion for type rules and for self rules. */
1960 	alwaysexpand = ((source_rule->specified & AVRULE_TYPE) ||
1961 			(source_rule->flags & RULE_SELF));
1962 
1963 	if (expand_convert_type_set
1964 	    (dest_pol, typemap, &source_rule->stypes, &stypes, alwaysexpand))
1965 		return EXPAND_RULE_ERROR;
1966 	if (expand_convert_type_set
1967 	    (dest_pol, typemap, &source_rule->ttypes, &ttypes, alwaysexpand))
1968 		return EXPAND_RULE_ERROR;
1969 
1970 	retval = expand_rule_helper(handle, dest_pol, typemap,
1971 				    source_rule, dest_avtab,
1972 				    cond, other, enabled, &stypes, &ttypes);
1973 	ebitmap_destroy(&stypes);
1974 	ebitmap_destroy(&ttypes);
1975 	return retval;
1976 }
1977 
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)1978 static int cond_avrule_list_copy(policydb_t * dest_pol, avrule_t * source_rules,
1979 				 avtab_t * dest_avtab, cond_av_list_t ** list,
1980 				 cond_av_list_t ** other, uint32_t * typemap,
1981 				 int enabled, expand_state_t * state)
1982 {
1983 	avrule_t *cur;
1984 
1985 	cur = source_rules;
1986 	while (cur) {
1987 		if (convert_and_expand_rule(state->handle, dest_pol,
1988 					    typemap, cur, dest_avtab,
1989 					    list, other, enabled,
1990 					    0) != EXPAND_RULE_SUCCESS) {
1991 			return -1;
1992 		}
1993 
1994 		cur = cur->next;
1995 	}
1996 
1997 	return 0;
1998 }
1999 
cond_node_map_bools(expand_state_t * state,cond_node_t * cn)2000 static int cond_node_map_bools(expand_state_t * state, cond_node_t * cn)
2001 {
2002 	cond_expr_t *cur;
2003 	unsigned int i;
2004 
2005 	cur = cn->expr;
2006 	while (cur) {
2007 		if (cur->bool)
2008 			cur->bool = state->boolmap[cur->bool - 1];
2009 		cur = cur->next;
2010 	}
2011 
2012 	for (i = 0; i < min(cn->nbools, COND_MAX_BOOLS); i++)
2013 		cn->bool_ids[i] = state->boolmap[cn->bool_ids[i] - 1];
2014 
2015 	if (cond_normalize_expr(state->out, cn)) {
2016 		ERR(state->handle, "Error while normalizing conditional");
2017 		return -1;
2018 	}
2019 
2020 	return 0;
2021 }
2022 
2023 /* copy the nodes in *reverse* order -- the result is that the last
2024  * given conditional appears first in the policy, so as to match the
2025  * behavior of the upstream compiler */
cond_node_copy(expand_state_t * state,cond_node_t * cn)2026 static int cond_node_copy(expand_state_t * state, cond_node_t * cn)
2027 {
2028 	cond_node_t *new_cond, *tmp;
2029 
2030 	if (cn == NULL) {
2031 		return 0;
2032 	}
2033 	if (cond_node_copy(state, cn->next)) {
2034 		return -1;
2035 	}
2036 
2037 	/* If current cond_node_t is of tunable, its effective branch
2038 	 * has been appended to its home decl->avrules list during link
2039 	 * and now we should just skip it. */
2040 	if (cn->flags & COND_NODE_FLAGS_TUNABLE)
2041 		return 0;
2042 
2043 	if (cond_normalize_expr(state->base, cn)) {
2044 		ERR(state->handle, "Error while normalizing conditional");
2045 		return -1;
2046 	}
2047 
2048 	/* create a new temporary conditional node with the booleans
2049 	 * mapped */
2050 	tmp = cond_node_create(state->base, cn);
2051 	if (!tmp) {
2052 		ERR(state->handle, "Out of memory");
2053 		return -1;
2054 	}
2055 
2056 	if (cond_node_map_bools(state, tmp)) {
2057 		cond_node_destroy(tmp);
2058 		free(tmp);
2059 		ERR(state->handle, "Error mapping booleans");
2060 		return -1;
2061 	}
2062 
2063 	new_cond = cond_node_search(state->out, state->out->cond_list, tmp);
2064 	if (!new_cond) {
2065 		cond_node_destroy(tmp);
2066 		free(tmp);
2067 		ERR(state->handle, "Out of memory!");
2068 		return -1;
2069 	}
2070 	cond_node_destroy(tmp);
2071 	free(tmp);
2072 
2073 	if (cond_avrule_list_copy
2074 	    (state->out, cn->avtrue_list, &state->out->te_cond_avtab,
2075 	     &new_cond->true_list, &new_cond->false_list, state->typemap,
2076 	     new_cond->cur_state, state))
2077 		return -1;
2078 	if (cond_avrule_list_copy
2079 	    (state->out, cn->avfalse_list, &state->out->te_cond_avtab,
2080 	     &new_cond->false_list, &new_cond->true_list, state->typemap,
2081 	     !new_cond->cur_state, state))
2082 		return -1;
2083 
2084 	return 0;
2085 }
2086 
context_copy(context_struct_t * dst,context_struct_t * src,expand_state_t * state)2087 static int context_copy(context_struct_t * dst, context_struct_t * src,
2088 			expand_state_t * state)
2089 {
2090 	dst->user = state->usermap[src->user - 1];
2091 	dst->role = state->rolemap[src->role - 1];
2092 	dst->type = state->typemap[src->type - 1];
2093 	return mls_context_cpy(dst, src);
2094 }
2095 
ocontext_copy_xen(expand_state_t * state)2096 static int ocontext_copy_xen(expand_state_t *state)
2097 {
2098 	unsigned int i;
2099 	ocontext_t *c, *n, *l;
2100 
2101 	for (i = 0; i < OCON_NUM; i++) {
2102 		l = NULL;
2103 		for (c = state->base->ocontexts[i]; c; c = c->next) {
2104 			if (i == OCON_XEN_ISID && !c->context[0].user) {
2105 				INFO(state->handle,
2106 				     "No context assigned to SID %s, omitting from policy",
2107 				     c->u.name);
2108 				continue;
2109 			}
2110 			n = malloc(sizeof(ocontext_t));
2111 			if (!n) {
2112 				ERR(state->handle, "Out of memory!");
2113 				return -1;
2114 			}
2115 			memset(n, 0, sizeof(ocontext_t));
2116 			if (l)
2117 				l->next = n;
2118 			else
2119 				state->out->ocontexts[i] = n;
2120 			l = n;
2121 			switch (i) {
2122 			case OCON_XEN_ISID:
2123 				n->sid[0] = c->sid[0];
2124 				break;
2125 			case OCON_XEN_PIRQ:
2126 				n->u.pirq = c->u.pirq;
2127 				break;
2128 			case OCON_XEN_IOPORT:
2129 				n->u.ioport.low_ioport = c->u.ioport.low_ioport;
2130 				n->u.ioport.high_ioport =
2131 					c->u.ioport.high_ioport;
2132 				break;
2133 			case OCON_XEN_IOMEM:
2134 				n->u.iomem.low_iomem  = c->u.iomem.low_iomem;
2135 				n->u.iomem.high_iomem = c->u.iomem.high_iomem;
2136 				break;
2137 			case OCON_XEN_PCIDEVICE:
2138 				n->u.device = c->u.device;
2139 				break;
2140 			case OCON_XEN_DEVICETREE:
2141 				n->u.name = strdup(c->u.name);
2142 				if (!n->u.name) {
2143 					ERR(state->handle, "Out of memory!");
2144 					return -1;
2145 				}
2146 				break;
2147 			default:
2148 				/* shouldn't get here */
2149 				ERR(state->handle, "Unknown ocontext");
2150 				return -1;
2151 			}
2152 			if (context_copy(&n->context[0], &c->context[0],
2153 				state)) {
2154 				ERR(state->handle, "Out of memory!");
2155 				return -1;
2156 			}
2157 		}
2158 	}
2159 	return 0;
2160 }
2161 
ocontext_copy_selinux(expand_state_t * state)2162 static int ocontext_copy_selinux(expand_state_t *state)
2163 {
2164 	unsigned int i, j;
2165 	ocontext_t *c, *n, *l;
2166 
2167 	for (i = 0; i < OCON_NUM; i++) {
2168 		l = NULL;
2169 		for (c = state->base->ocontexts[i]; c; c = c->next) {
2170 			if (i == OCON_ISID && !c->context[0].user) {
2171 				INFO(state->handle,
2172 				     "No context assigned to SID %s, omitting from policy",
2173 				     c->u.name);
2174 				continue;
2175 			}
2176 			n = malloc(sizeof(ocontext_t));
2177 			if (!n) {
2178 				ERR(state->handle, "Out of memory!");
2179 				return -1;
2180 			}
2181 			memset(n, 0, sizeof(ocontext_t));
2182 			if (l)
2183 				l->next = n;
2184 			else
2185 				state->out->ocontexts[i] = n;
2186 			l = n;
2187 			switch (i) {
2188 			case OCON_ISID:
2189 				n->sid[0] = c->sid[0];
2190 				break;
2191 			case OCON_FS:	/* FALLTHROUGH */
2192 			case OCON_NETIF:
2193 				n->u.name = strdup(c->u.name);
2194 				if (!n->u.name) {
2195 					ERR(state->handle, "Out of memory!");
2196 					return -1;
2197 				}
2198 				if (context_copy
2199 				    (&n->context[1], &c->context[1], state)) {
2200 					ERR(state->handle, "Out of memory!");
2201 					return -1;
2202 				}
2203 				break;
2204 			case OCON_IBPKEY:
2205 				n->u.ibpkey.subnet_prefix = c->u.ibpkey.subnet_prefix;
2206 
2207 				n->u.ibpkey.low_pkey = c->u.ibpkey.low_pkey;
2208 				n->u.ibpkey.high_pkey = c->u.ibpkey.high_pkey;
2209 			break;
2210 			case OCON_IBENDPORT:
2211 				n->u.ibendport.dev_name = strdup(c->u.ibendport.dev_name);
2212 				if (!n->u.ibendport.dev_name) {
2213 					ERR(state->handle, "Out of memory!");
2214 					return -1;
2215 				}
2216 				n->u.ibendport.port = c->u.ibendport.port;
2217 				break;
2218 			case OCON_PORT:
2219 				n->u.port.protocol = c->u.port.protocol;
2220 				n->u.port.low_port = c->u.port.low_port;
2221 				n->u.port.high_port = c->u.port.high_port;
2222 				break;
2223 			case OCON_NODE:
2224 				n->u.node.addr = c->u.node.addr;
2225 				n->u.node.mask = c->u.node.mask;
2226 				break;
2227 			case OCON_FSUSE:
2228 				n->v.behavior = c->v.behavior;
2229 				n->u.name = strdup(c->u.name);
2230 				if (!n->u.name) {
2231 					ERR(state->handle, "Out of memory!");
2232 					return -1;
2233 				}
2234 				break;
2235 			case OCON_NODE6:
2236 				for (j = 0; j < 4; j++)
2237 					n->u.node6.addr[j] = c->u.node6.addr[j];
2238 				for (j = 0; j < 4; j++)
2239 					n->u.node6.mask[j] = c->u.node6.mask[j];
2240 				break;
2241 			default:
2242 				/* shouldn't get here */
2243 				ERR(state->handle, "Unknown ocontext");
2244 				return -1;
2245 			}
2246 			if (context_copy(&n->context[0], &c->context[0], state)) {
2247 				ERR(state->handle, "Out of memory!");
2248 				return -1;
2249 			}
2250 		}
2251 	}
2252 	return 0;
2253 }
2254 
ocontext_copy(expand_state_t * state,uint32_t target)2255 static int ocontext_copy(expand_state_t *state, uint32_t target)
2256 {
2257 	int rc = -1;
2258 	switch (target) {
2259 	case SEPOL_TARGET_SELINUX:
2260 		rc = ocontext_copy_selinux(state);
2261 		break;
2262 	case SEPOL_TARGET_XEN:
2263 		rc = ocontext_copy_xen(state);
2264 		break;
2265 	default:
2266 		ERR(state->handle, "Unknown target");
2267 		return -1;
2268 	}
2269 	return rc;
2270 }
2271 
genfs_copy(expand_state_t * state)2272 static int genfs_copy(expand_state_t * state)
2273 {
2274 	ocontext_t *c, *newc, *l;
2275 	genfs_t *genfs, *newgenfs, *end;
2276 
2277 	end = NULL;
2278 	for (genfs = state->base->genfs; genfs; genfs = genfs->next) {
2279 		newgenfs = malloc(sizeof(genfs_t));
2280 		if (!newgenfs) {
2281 			ERR(state->handle, "Out of memory!");
2282 			return -1;
2283 		}
2284 		memset(newgenfs, 0, sizeof(genfs_t));
2285 		newgenfs->fstype = strdup(genfs->fstype);
2286 		if (!newgenfs->fstype) {
2287 			free(newgenfs);
2288 			ERR(state->handle, "Out of memory!");
2289 			return -1;
2290 		}
2291 		if (!end)
2292 			state->out->genfs = newgenfs;
2293 		else
2294 			end->next = newgenfs;
2295 		end = newgenfs;
2296 
2297 		l = NULL;
2298 		for (c = genfs->head; c; c = c->next) {
2299 			newc = malloc(sizeof(ocontext_t));
2300 			if (!newc) {
2301 				ERR(state->handle, "Out of memory!");
2302 				return -1;
2303 			}
2304 			memset(newc, 0, sizeof(ocontext_t));
2305 			newc->u.name = strdup(c->u.name);
2306 			if (!newc->u.name) {
2307 				ERR(state->handle, "Out of memory!");
2308 				free(newc);
2309 				return -1;
2310 			}
2311 			newc->v.sclass = c->v.sclass;
2312 			context_copy(&newc->context[0], &c->context[0], state);
2313 			if (l)
2314 				l->next = newc;
2315 			else
2316 				newgenfs->head = newc;
2317 			l = newc;
2318 		}
2319 	}
2320 	return 0;
2321 }
2322 
type_attr_map(hashtab_key_t key,hashtab_datum_t datum,void * ptr)2323 static int type_attr_map(hashtab_key_t key
2324 			 __attribute__ ((unused)), hashtab_datum_t datum,
2325 			 void *ptr)
2326 {
2327 	type_datum_t *type;
2328 	expand_state_t *state = ptr;
2329 	policydb_t *p = state->out;
2330 	unsigned int i;
2331 	ebitmap_node_t *tnode;
2332 	int value;
2333 
2334 	type = (type_datum_t *) datum;
2335 	value = type->s.value;
2336 
2337 	if (type->flavor == TYPE_ATTRIB) {
2338 		if (!(type->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE)) {
2339 			if (ebitmap_cpy(&p->attr_type_map[value - 1], &type->types)) {
2340 				goto oom;
2341 			}
2342 			ebitmap_for_each_positive_bit(&type->types, tnode, i) {
2343 				if (ebitmap_set_bit(&p->type_attr_map[i], value - 1, 1)) {
2344 					goto oom;
2345 				}
2346 			}
2347 		} else {
2348 			/* Attribute is being expanded, so remove */
2349 			if (ebitmap_set_bit(&p->type_attr_map[value - 1], value - 1, 0)) {
2350 				goto oom;
2351 			}
2352 		}
2353 	} else {
2354 		if (ebitmap_set_bit(&p->attr_type_map[value - 1], value - 1, 1)) {
2355 			goto oom;
2356 		}
2357 	}
2358 
2359 	return 0;
2360 
2361 oom:
2362 	ERR(state->handle, "Out of memory!");
2363 	return -1;
2364 }
2365 
2366 /* converts typeset using typemap and expands into ebitmap_t types using the attributes in the passed in policy.
2367  * this should not be called until after all the blocks have been processed and the attributes in target policy
2368  * are complete. */
expand_convert_type_set(policydb_t * p,uint32_t * typemap,type_set_t * set,ebitmap_t * types,unsigned char alwaysexpand)2369 int expand_convert_type_set(policydb_t * p, uint32_t * typemap,
2370 			    type_set_t * set, ebitmap_t * types,
2371 			    unsigned char alwaysexpand)
2372 {
2373 	type_set_t tmpset;
2374 
2375 	type_set_init(&tmpset);
2376 
2377 	if (map_ebitmap(&set->types, &tmpset.types, typemap))
2378 		return -1;
2379 
2380 	if (map_ebitmap(&set->negset, &tmpset.negset, typemap))
2381 		return -1;
2382 
2383 	tmpset.flags = set->flags;
2384 
2385 	if (type_set_expand(&tmpset, types, p, alwaysexpand))
2386 		return -1;
2387 
2388 	type_set_destroy(&tmpset);
2389 
2390 	return 0;
2391 }
2392 
2393 /* Expand a rule into a given avtab - checking for conflicting type
2394  * rules.  Return 1 on success, 0 if the rule conflicts with something
2395  * (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)2396 int expand_rule(sepol_handle_t * handle,
2397 		policydb_t * source_pol,
2398 		avrule_t * source_rule, avtab_t * dest_avtab,
2399 		cond_av_list_t ** cond, cond_av_list_t ** other, int enabled)
2400 {
2401 	int retval;
2402 	ebitmap_t stypes, ttypes;
2403 
2404 	if ((source_rule->specified & AVRULE_NEVERALLOW)
2405 		|| (source_rule->specified & AVRULE_XPERMS_NEVERALLOW))
2406 		return 1;
2407 
2408 	ebitmap_init(&stypes);
2409 	ebitmap_init(&ttypes);
2410 
2411 	if (type_set_expand(&source_rule->stypes, &stypes, source_pol, 1))
2412 		return -1;
2413 	if (type_set_expand(&source_rule->ttypes, &ttypes, source_pol, 1))
2414 		return -1;
2415 	retval = expand_rule_helper(handle, source_pol, NULL,
2416 				    source_rule, dest_avtab,
2417 				    cond, other, enabled, &stypes, &ttypes);
2418 	ebitmap_destroy(&stypes);
2419 	ebitmap_destroy(&ttypes);
2420 	return retval;
2421 }
2422 
2423 /* Expand a role set into an ebitmap containing the roles.
2424  * This handles the attribute and flags.
2425  * Attribute expansion depends on if the rolemap is available.
2426  * During module compile the rolemap is not available, the
2427  * possible duplicates of a regular role and the role attribute
2428  * the regular role belongs to could be properly handled by
2429  * copy_role_trans and copy_role_allow.
2430  */
role_set_expand(role_set_t * x,ebitmap_t * r,policydb_t * out,policydb_t * base,uint32_t * rolemap)2431 int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_t * base, uint32_t * rolemap)
2432 {
2433 	unsigned int i;
2434 	ebitmap_node_t *rnode;
2435 	ebitmap_t mapped_roles, roles;
2436 	policydb_t *p = out;
2437 	role_datum_t *role;
2438 
2439 	ebitmap_init(r);
2440 
2441 	if (x->flags & ROLE_STAR) {
2442 		for (i = 0; i < p->p_roles.nprim; i++)
2443 			if (ebitmap_set_bit(r, i, 1))
2444 				return -1;
2445 		return 0;
2446 	}
2447 
2448 	ebitmap_init(&mapped_roles);
2449 	ebitmap_init(&roles);
2450 
2451 	if (rolemap) {
2452 		assert(base != NULL);
2453 		ebitmap_for_each_positive_bit(&x->roles, rnode, i) {
2454 			/* take advantage of p_role_val_to_struct[]
2455 			 * of the base module */
2456 			role = base->role_val_to_struct[i];
2457 			assert(role != NULL);
2458 			if (role->flavor == ROLE_ATTRIB) {
2459 				if (ebitmap_union(&roles,
2460 						  &role->roles))
2461 					goto bad;
2462 			} else {
2463 				if (ebitmap_set_bit(&roles, i, 1))
2464 					goto bad;
2465 			}
2466 		}
2467 		if (map_ebitmap(&roles, &mapped_roles, rolemap))
2468 			goto bad;
2469 	} else {
2470 		if (ebitmap_cpy(&mapped_roles, &x->roles))
2471 			goto bad;
2472 	}
2473 
2474 	ebitmap_for_each_positive_bit(&mapped_roles, rnode, i) {
2475 		if (ebitmap_set_bit(r, i, 1))
2476 			goto bad;
2477 	}
2478 
2479 	ebitmap_destroy(&mapped_roles);
2480 	ebitmap_destroy(&roles);
2481 
2482 	/* if role is to be complimented, invert the entire bitmap here */
2483 	if (x->flags & ROLE_COMP) {
2484 		for (i = 0; i < p->p_roles.nprim; i++) {
2485 			if (ebitmap_get_bit(r, i)) {
2486 				if (ebitmap_set_bit(r, i, 0))
2487 					return -1;
2488 			} else {
2489 				if (ebitmap_set_bit(r, i, 1))
2490 					return -1;
2491 			}
2492 		}
2493 	}
2494 	return 0;
2495 
2496 bad:
2497 	ebitmap_destroy(&mapped_roles);
2498 	ebitmap_destroy(&roles);
2499 	return -1;
2500 }
2501 
2502 /* Expand a type set into an ebitmap containing the types. This
2503  * handles the negset, attributes, and flags.
2504  * Attribute expansion depends on several factors:
2505  * - if alwaysexpand is 1, then they will be expanded,
2506  * - if the type set has a negset or flags, then they will be expanded,
2507  * - otherwise, they will not be expanded.
2508  */
type_set_expand(type_set_t * set,ebitmap_t * t,policydb_t * p,unsigned char alwaysexpand)2509 int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p,
2510 		    unsigned char alwaysexpand)
2511 {
2512 	unsigned int i;
2513 	ebitmap_t types, neg_types;
2514 	ebitmap_node_t *tnode;
2515 	unsigned char expand = alwaysexpand || !ebitmap_is_empty(&set->negset) || set->flags;
2516 	type_datum_t *type;
2517 	int rc =-1;
2518 
2519 	ebitmap_init(&types);
2520 	ebitmap_init(t);
2521 
2522 	/* First go through the types and OR all the attributes to types */
2523 	ebitmap_for_each_positive_bit(&set->types, tnode, i) {
2524 		/*
2525 		 * invalid policies might have more types set in the ebitmap than
2526 		 * what's available in the type_val_to_struct mapping
2527 		 */
2528 		if (i >= p->p_types.nprim)
2529 			goto err_types;
2530 
2531 		type = p->type_val_to_struct[i];
2532 
2533 		if (!type) {
2534 			goto err_types;
2535 		}
2536 
2537 		if (type->flavor == TYPE_ATTRIB &&
2538 		    (expand || (type->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE))) {
2539 			if (ebitmap_union(&types, &type->types)) {
2540 				goto err_types;
2541 			}
2542 		} else {
2543 			if (ebitmap_set_bit(&types, i, 1)) {
2544 				goto err_types;
2545 			}
2546 		}
2547 	}
2548 
2549 	/* Now do the same thing for negset */
2550 	ebitmap_init(&neg_types);
2551 	ebitmap_for_each_positive_bit(&set->negset, tnode, i) {
2552 		if (p->type_val_to_struct[i] &&
2553 		    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) {
2554 			if (ebitmap_union
2555 			    (&neg_types,
2556 			     &p->type_val_to_struct[i]->types)) {
2557 				goto err_neg;
2558 			}
2559 		} else {
2560 			if (ebitmap_set_bit(&neg_types, i, 1)) {
2561 				goto err_neg;
2562 			}
2563 		}
2564 	}
2565 
2566 	if (set->flags & TYPE_STAR) {
2567 		/* set all types not in neg_types */
2568 		for (i = 0; i < p->p_types.nprim; i++) {
2569 			if (ebitmap_get_bit(&neg_types, i))
2570 				continue;
2571 			if (p->type_val_to_struct[i] &&
2572 			    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB)
2573 				continue;
2574 			if (ebitmap_set_bit(t, i, 1))
2575 				goto err_neg;
2576 		}
2577 		goto out;
2578 	}
2579 
2580 	ebitmap_for_each_positive_bit(&types, tnode, i) {
2581 		if (!ebitmap_get_bit(&neg_types, i))
2582 			if (ebitmap_set_bit(t, i, 1))
2583 				goto err_neg;
2584 	}
2585 
2586 	if (set->flags & TYPE_COMP) {
2587 		for (i = 0; i < p->p_types.nprim; i++) {
2588 			if (p->type_val_to_struct[i] &&
2589 			    p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) {
2590 				assert(!ebitmap_get_bit(t, i));
2591 				continue;
2592 			}
2593 			if (ebitmap_get_bit(t, i)) {
2594 				if (ebitmap_set_bit(t, i, 0))
2595 					goto err_neg;
2596 			} else {
2597 				if (ebitmap_set_bit(t, i, 1))
2598 					goto err_neg;
2599 			}
2600 		}
2601 	}
2602 
2603 	  out:
2604 	rc = 0;
2605 
2606 	  err_neg:
2607 	ebitmap_destroy(&neg_types);
2608 	  err_types:
2609 	ebitmap_destroy(&types);
2610 
2611 	return rc;
2612 }
2613 
copy_neverallow(policydb_t * dest_pol,uint32_t * typemap,avrule_t * source_rule)2614 static int copy_neverallow(policydb_t * dest_pol, uint32_t * typemap,
2615 			   avrule_t * source_rule)
2616 {
2617 	ebitmap_t stypes, ttypes;
2618 	avrule_t *avrule;
2619 	class_perm_node_t *cur_perm, *new_perm, *tail_perm;
2620 	av_extended_perms_t *xperms = NULL;
2621 
2622 	ebitmap_init(&stypes);
2623 	ebitmap_init(&ttypes);
2624 
2625 	if (expand_convert_type_set
2626 	    (dest_pol, typemap, &source_rule->stypes, &stypes, 1))
2627 		return -1;
2628 	if (expand_convert_type_set
2629 	    (dest_pol, typemap, &source_rule->ttypes, &ttypes, 1))
2630 		return -1;
2631 
2632 	avrule = (avrule_t *) malloc(sizeof(avrule_t));
2633 	if (!avrule)
2634 		return -1;
2635 
2636 	avrule_init(avrule);
2637 	avrule->specified = source_rule->specified;
2638 	avrule->line = source_rule->line;
2639 	avrule->flags = source_rule->flags;
2640 	avrule->source_line = source_rule->source_line;
2641 	if (source_rule->source_filename) {
2642 		avrule->source_filename = strdup(source_rule->source_filename);
2643 		if (!avrule->source_filename)
2644 			goto err;
2645 	}
2646 
2647 	if (ebitmap_cpy(&avrule->stypes.types, &stypes))
2648 		goto err;
2649 
2650 	if (ebitmap_cpy(&avrule->ttypes.types, &ttypes))
2651 		goto err;
2652 
2653 	cur_perm = source_rule->perms;
2654 	tail_perm = NULL;
2655 	while (cur_perm) {
2656 		new_perm =
2657 		    (class_perm_node_t *) malloc(sizeof(class_perm_node_t));
2658 		if (!new_perm)
2659 			goto err;
2660 		class_perm_node_init(new_perm);
2661 		new_perm->tclass = cur_perm->tclass;
2662 		assert(new_perm->tclass);
2663 
2664 		/* once we have modules with permissions we'll need to map the permissions (and classes) */
2665 		new_perm->data = cur_perm->data;
2666 
2667 		if (!avrule->perms)
2668 			avrule->perms = new_perm;
2669 
2670 		if (tail_perm)
2671 			tail_perm->next = new_perm;
2672 		tail_perm = new_perm;
2673 		cur_perm = cur_perm->next;
2674 	}
2675 
2676 	/* copy over extended permissions */
2677 	if (source_rule->xperms) {
2678 		xperms = calloc(1, sizeof(av_extended_perms_t));
2679 		if (!xperms)
2680 			goto err;
2681 		memcpy(xperms, source_rule->xperms, sizeof(av_extended_perms_t));
2682 		avrule->xperms = xperms;
2683 	}
2684 
2685 	/* just prepend the avrule to the first branch; it'll never be
2686 	   written to disk */
2687 	if (!dest_pol->global->branch_list->avrules)
2688 		dest_pol->global->branch_list->avrules = avrule;
2689 	else {
2690 		avrule->next = dest_pol->global->branch_list->avrules;
2691 		dest_pol->global->branch_list->avrules = avrule;
2692 	}
2693 
2694 	ebitmap_destroy(&stypes);
2695 	ebitmap_destroy(&ttypes);
2696 
2697 	return 0;
2698 
2699       err:
2700 	ebitmap_destroy(&stypes);
2701 	ebitmap_destroy(&ttypes);
2702 	ebitmap_destroy(&avrule->stypes.types);
2703 	ebitmap_destroy(&avrule->ttypes.types);
2704 	cur_perm = avrule->perms;
2705 	while (cur_perm) {
2706 		tail_perm = cur_perm->next;
2707 		free(cur_perm);
2708 		cur_perm = tail_perm;
2709 	}
2710 	free(xperms);
2711 	free(avrule);
2712 	return -1;
2713 }
2714 
2715 /*
2716  * Expands the avrule blocks for a policy. RBAC rules are copied. Neverallow
2717  * rules are copied or expanded as per the settings in the state object; all
2718  * other AV rules are expanded.  If neverallow rules are expanded, they are not
2719  * copied, otherwise they are copied for later use by the assertion checker.
2720  */
copy_and_expand_avrule_block(expand_state_t * state)2721 static int copy_and_expand_avrule_block(expand_state_t * state)
2722 {
2723 	avrule_block_t *curblock = state->base->global;
2724 	avrule_block_t *prevblock;
2725 	int retval = -1;
2726 
2727 	if (avtab_alloc(&state->out->te_avtab, MAX_AVTAB_SIZE)) {
2728  		ERR(state->handle, "Out of Memory!");
2729  		return -1;
2730  	}
2731 
2732  	if (avtab_alloc(&state->out->te_cond_avtab, MAX_AVTAB_SIZE)) {
2733  		ERR(state->handle, "Out of Memory!");
2734  		return -1;
2735  	}
2736 
2737 	while (curblock) {
2738 		avrule_decl_t *decl = curblock->enabled;
2739 		avrule_t *cur_avrule;
2740 
2741 		if (decl == NULL) {
2742 			/* nothing was enabled within this block */
2743 			goto cont;
2744 		}
2745 
2746 		/* copy role allows and role trans */
2747 		if (copy_role_allows(state, decl->role_allow_rules) != 0 ||
2748 		    copy_role_trans(state, decl->role_tr_rules) != 0) {
2749 			goto cleanup;
2750 		}
2751 
2752 		if (expand_filename_trans(state, decl->filename_trans_rules))
2753 			goto cleanup;
2754 
2755 		/* expand the range transition rules */
2756 		if (expand_range_trans(state, decl->range_tr_rules))
2757 			goto cleanup;
2758 
2759 		/* copy rules */
2760 		cur_avrule = decl->avrules;
2761 		while (cur_avrule != NULL) {
2762 			if (!(state->expand_neverallow)
2763 			    && cur_avrule->specified & (AVRULE_NEVERALLOW | AVRULE_XPERMS_NEVERALLOW)) {
2764 				/* copy this over directly so that assertions are checked later */
2765 				if (copy_neverallow
2766 				    (state->out, state->typemap, cur_avrule))
2767 					ERR(state->handle,
2768 					    "Error while copying neverallow.");
2769 			} else {
2770 				if (cur_avrule->specified & (AVRULE_NEVERALLOW | AVRULE_XPERMS_NEVERALLOW))
2771 					state->out->unsupported_format = 1;
2772 				if (convert_and_expand_rule
2773 				    (state->handle, state->out, state->typemap,
2774 				     cur_avrule, &state->out->te_avtab, NULL,
2775 				     NULL, 0,
2776 				     state->expand_neverallow) !=
2777 				    EXPAND_RULE_SUCCESS) {
2778 					goto cleanup;
2779 				}
2780 			}
2781 			cur_avrule = cur_avrule->next;
2782 		}
2783 
2784 		/* copy conditional rules */
2785 		if (cond_node_copy(state, decl->cond_list))
2786 			goto cleanup;
2787 
2788       cont:
2789 		prevblock = curblock;
2790 		curblock = curblock->next;
2791 
2792 		if (state->handle && state->handle->expand_consume_base) {
2793 			/* set base top avrule block in case there
2794  			 * is an error condition and the policy needs
2795  			 * to be destroyed */
2796 			state->base->global = curblock;
2797 			avrule_block_destroy(prevblock);
2798 		}
2799 	}
2800 
2801 	retval = 0;
2802 
2803       cleanup:
2804 	return retval;
2805 }
2806 
2807 /*
2808  * This function allows external users of the library (such as setools) to
2809  * expand only the avrules and optionally perform expansion of neverallow rules
2810  * or expand into the same policy for analysis purposes.
2811  */
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)2812 int expand_module_avrules(sepol_handle_t * handle, policydb_t * base,
2813 			  policydb_t * out, uint32_t * typemap,
2814 			  uint32_t * boolmap, uint32_t * rolemap,
2815 			  uint32_t * usermap, int verbose,
2816 			  int expand_neverallow)
2817 {
2818 	expand_state_t state;
2819 
2820 	expand_state_init(&state);
2821 
2822 	state.base = base;
2823 	state.out = out;
2824 	state.typemap = typemap;
2825 	state.boolmap = boolmap;
2826 	state.rolemap = rolemap;
2827 	state.usermap = usermap;
2828 	state.handle = handle;
2829 	state.verbose = verbose;
2830 	state.expand_neverallow = expand_neverallow;
2831 
2832 	return copy_and_expand_avrule_block(&state);
2833 }
2834 
discard_tunables(sepol_handle_t * sh,policydb_t * pol)2835 static void discard_tunables(sepol_handle_t *sh, policydb_t *pol)
2836 {
2837 	avrule_block_t *block;
2838 	avrule_decl_t *decl;
2839 	cond_node_t *cur_node;
2840 	cond_expr_t *cur_expr;
2841 	int cur_state, preserve_tunables = 0;
2842 	avrule_t *tail, *to_be_appended;
2843 
2844 	if (sh && sh->preserve_tunables)
2845 		preserve_tunables = 1;
2846 
2847 	/* Iterate through all cond_node of all enabled decls, if a cond_node
2848 	 * is about tunable, calculate its state value and concatenate one of
2849 	 * its avrule list to the current decl->avrules list. On the other
2850 	 * hand, the disabled unused branch of a tunable would be discarded.
2851 	 *
2852 	 * Note, such tunable cond_node would be skipped over in expansion,
2853 	 * so we won't have to worry about removing it from decl->cond_list
2854 	 * here :-)
2855 	 *
2856 	 * If tunables are requested to be preserved then they would be
2857 	 * "transformed" as booleans by having their TUNABLE flag cleared.
2858 	 */
2859 	for (block = pol->global; block != NULL; block = block->next) {
2860 		decl = block->enabled;
2861 		if (decl == NULL || decl->enabled == 0)
2862 			continue;
2863 
2864 		tail = decl->avrules;
2865 		while (tail && tail->next)
2866 			tail = tail->next;
2867 
2868 		for (cur_node = decl->cond_list; cur_node != NULL;
2869 		     cur_node = cur_node->next) {
2870 			int booleans, tunables, i;
2871 			cond_bool_datum_t *booldatum;
2872 			cond_bool_datum_t *tmp[COND_EXPR_MAXDEPTH];
2873 
2874 			booleans = tunables = 0;
2875 			memset(tmp, 0, sizeof(cond_bool_datum_t *) * COND_EXPR_MAXDEPTH);
2876 
2877 			for (cur_expr = cur_node->expr; cur_expr != NULL;
2878 			     cur_expr = cur_expr->next) {
2879 				if (cur_expr->expr_type != COND_BOOL)
2880 					continue;
2881 				booldatum = pol->bool_val_to_struct[cur_expr->bool - 1];
2882 				if (booldatum->flags & COND_BOOL_FLAGS_TUNABLE)
2883 					tmp[tunables++] = booldatum;
2884 				else
2885 					booleans++;
2886 			}
2887 
2888 			/* bool_copy_callback() at link phase has ensured
2889 			 * that no mixture of tunables and booleans in one
2890 			 * expression. However, this would be broken by the
2891 			 * request to preserve tunables */
2892 			if (!preserve_tunables)
2893 				assert(!(booleans && tunables));
2894 
2895 			if (booleans || preserve_tunables) {
2896 				cur_node->flags &= ~COND_NODE_FLAGS_TUNABLE;
2897 				if (tunables) {
2898 					for (i = 0; i < tunables; i++)
2899 						tmp[i]->flags &= ~COND_BOOL_FLAGS_TUNABLE;
2900 				}
2901 			} else {
2902 				cur_node->flags |= COND_NODE_FLAGS_TUNABLE;
2903 				cur_state = cond_evaluate_expr(pol, cur_node->expr);
2904 				if (cur_state == -1) {
2905 					printf("Expression result was "
2906 					       "undefined, skipping all"
2907 					       "rules\n");
2908 					continue;
2909 				}
2910 
2911 				to_be_appended = (cur_state == 1) ?
2912 					cur_node->avtrue_list : cur_node->avfalse_list;
2913 
2914 				if (tail)
2915 					tail->next = to_be_appended;
2916 				else
2917 					tail = decl->avrules = to_be_appended;
2918 
2919 				/* Now that the effective branch has been
2920 				 * appended, neutralize its original pointer */
2921 				if (cur_state == 1)
2922 					cur_node->avtrue_list = NULL;
2923 				else
2924 					cur_node->avfalse_list = NULL;
2925 
2926 				/* Update the tail of decl->avrules for
2927 				 * further concatenation */
2928 				while (tail && tail->next)
2929 					tail = tail->next;
2930 			}
2931 		}
2932 	}
2933 }
2934 
2935 /* Linking should always be done before calling expand, even if
2936  * there is only a base since all optionals are dealt with at link time
2937  * the base passed in should be indexed and avrule blocks should be
2938  * enabled.
2939  */
expand_module(sepol_handle_t * handle,policydb_t * base,policydb_t * out,int verbose,int check)2940 int expand_module(sepol_handle_t * handle,
2941 		  policydb_t * base, policydb_t * out, int verbose, int check)
2942 {
2943 	int retval = -1;
2944 	unsigned int i;
2945 	expand_state_t state;
2946 	avrule_block_t *curblock;
2947 
2948 	/* Append tunable's avtrue_list or avfalse_list to the avrules list
2949 	 * of its home decl depending on its state value, so that the effect
2950 	 * rules of a tunable would be added to te_avtab permanently. Whereas
2951 	 * the disabled unused branch would be discarded.
2952 	 *
2953 	 * Originally this function is called at the very end of link phase,
2954 	 * however, we need to keep the linked policy intact for analysis
2955 	 * purpose. */
2956 	discard_tunables(handle, base);
2957 
2958 	expand_state_init(&state);
2959 
2960 	state.verbose = verbose;
2961 	state.typemap = NULL;
2962 	state.base = base;
2963 	state.out = out;
2964 	state.handle = handle;
2965 
2966 	if (base->policy_type != POLICY_BASE) {
2967 		ERR(handle, "Target of expand was not a base policy.");
2968 		return -1;
2969 	}
2970 
2971 	state.out->policy_type = POLICY_KERN;
2972 	state.out->policyvers = POLICYDB_VERSION_MAX;
2973 	if (state.base->name) {
2974 		state.out->name = strdup(state.base->name);
2975 	}
2976 
2977 	/* Copy mls state from base to out */
2978 	out->mls = base->mls;
2979 	out->handle_unknown = base->handle_unknown;
2980 
2981 	/* Copy target from base to out */
2982 	out->target_platform = base->target_platform;
2983 
2984 	/* Copy policy capabilities */
2985 	if (ebitmap_cpy(&out->policycaps, &base->policycaps)) {
2986 		ERR(handle, "Out of memory!");
2987 		goto cleanup;
2988 	}
2989 
2990 	if ((state.typemap =
2991 	     (uint32_t *) calloc(state.base->p_types.nprim,
2992 				 sizeof(uint32_t))) == NULL) {
2993 		ERR(handle, "Out of memory!");
2994 		goto cleanup;
2995 	}
2996 
2997 	state.boolmap = (uint32_t *)calloc(state.base->p_bools.nprim, sizeof(uint32_t));
2998 	if (!state.boolmap) {
2999 		ERR(handle, "Out of memory!");
3000 		goto cleanup;
3001 	}
3002 
3003 	state.rolemap = (uint32_t *)calloc(state.base->p_roles.nprim, sizeof(uint32_t));
3004 	if (!state.rolemap) {
3005 		ERR(handle, "Out of memory!");
3006 		goto cleanup;
3007 	}
3008 
3009 	state.usermap = (uint32_t *)calloc(state.base->p_users.nprim, sizeof(uint32_t));
3010 	if (!state.usermap) {
3011 		ERR(handle, "Out of memory!");
3012 		goto cleanup;
3013 	}
3014 
3015 	/* order is important - types must be first */
3016 
3017 	/* copy types */
3018 	if (hashtab_map(state.base->p_types.table, type_copy_callback, &state)) {
3019 		goto cleanup;
3020 	}
3021 
3022 	/* convert attribute type sets */
3023 	if (hashtab_map
3024 	    (state.base->p_types.table, attr_convert_callback, &state)) {
3025 		goto cleanup;
3026 	}
3027 
3028 	/* copy commons */
3029 	if (hashtab_map
3030 	    (state.base->p_commons.table, common_copy_callback, &state)) {
3031 		goto cleanup;
3032 	}
3033 
3034 	/* copy classes, note, this does not copy constraints, constraints can't be
3035 	 * copied until after all the blocks have been processed and attributes are complete */
3036 	if (hashtab_map
3037 	    (state.base->p_classes.table, class_copy_callback, &state)) {
3038 		goto cleanup;
3039 	}
3040 
3041 	/* copy type bounds */
3042 	if (hashtab_map(state.base->p_types.table,
3043 			type_bounds_copy_callback, &state))
3044 		goto cleanup;
3045 
3046 	/* copy aliases */
3047 	if (hashtab_map(state.base->p_types.table, alias_copy_callback, &state))
3048 		goto cleanup;
3049 
3050 	/* index here so that type indexes are available for role_copy_callback */
3051 	if (policydb_index_others(handle, out, verbose)) {
3052 		ERR(handle, "Error while indexing out symbols");
3053 		goto cleanup;
3054 	}
3055 
3056 	/* copy roles */
3057 	if (hashtab_map(state.base->p_roles.table, role_copy_callback, &state))
3058 		goto cleanup;
3059 	if (hashtab_map(state.base->p_roles.table,
3060 			role_bounds_copy_callback, &state))
3061 		goto cleanup;
3062 
3063 	/* copy MLS's sensitivity level and categories - this needs to be done
3064 	 * before expanding users (they need to be indexed too) */
3065 	if (hashtab_map(state.base->p_levels.table, sens_copy_callback, &state))
3066 		goto cleanup;
3067 	if (hashtab_map(state.base->p_cats.table, cats_copy_callback, &state))
3068 		goto cleanup;
3069 	if (policydb_index_others(handle, out, verbose)) {
3070 		ERR(handle, "Error while indexing out symbols");
3071 		goto cleanup;
3072 	}
3073 
3074 	/* copy users */
3075 	if (hashtab_map(state.base->p_users.table, user_copy_callback, &state))
3076 		goto cleanup;
3077 	if (hashtab_map(state.base->p_users.table,
3078 			user_bounds_copy_callback, &state))
3079 		goto cleanup;
3080 
3081 	/* copy bools */
3082 	if (hashtab_map(state.base->p_bools.table, bool_copy_callback, &state))
3083 		goto cleanup;
3084 
3085 	if (policydb_index_classes(out)) {
3086 		ERR(handle, "Error while indexing out classes");
3087 		goto cleanup;
3088 	}
3089 	if (policydb_index_others(handle, out, verbose)) {
3090 		ERR(handle, "Error while indexing out symbols");
3091 		goto cleanup;
3092 	}
3093 
3094 	/* loop through all decls and union attributes, roles, users */
3095 	for (curblock = state.base->global; curblock != NULL;
3096 	     curblock = curblock->next) {
3097 		avrule_decl_t *decl = curblock->enabled;
3098 
3099 		if (decl == NULL) {
3100 			/* nothing was enabled within this block */
3101 			continue;
3102 		}
3103 
3104 		/* convert attribute type sets */
3105 		if (hashtab_map
3106 		    (decl->p_types.table, attr_convert_callback, &state)) {
3107 			goto cleanup;
3108 		}
3109 
3110 		/* copy roles */
3111 		if (hashtab_map
3112 		    (decl->p_roles.table, role_copy_callback, &state))
3113 			goto cleanup;
3114 
3115 		/* copy users */
3116 		if (hashtab_map
3117 		    (decl->p_users.table, user_copy_callback, &state))
3118 			goto cleanup;
3119 
3120 	}
3121 
3122 	/* remap role dominates bitmaps */
3123 	 if (hashtab_map(state.out->p_roles.table, role_remap_dominates, &state)) {
3124 		goto cleanup;
3125 	}
3126 
3127 	/* escalate the type_set_t in a role attribute to all regular roles
3128 	 * that belongs to it. */
3129 	if (hashtab_map(state.base->p_roles.table, role_fix_callback, &state))
3130 		goto cleanup;
3131 
3132 	if (copy_and_expand_avrule_block(&state) < 0) {
3133 		ERR(handle, "Error during expand");
3134 		goto cleanup;
3135 	}
3136 
3137 	/* copy constraints */
3138 	if (hashtab_map
3139 	    (state.base->p_classes.table, constraint_copy_callback, &state)) {
3140 		goto cleanup;
3141 	}
3142 
3143 	cond_optimize_lists(state.out->cond_list);
3144 	if (evaluate_conds(state.out))
3145 		goto cleanup;
3146 
3147 	/* copy ocontexts */
3148 	if (ocontext_copy(&state, out->target_platform))
3149 		goto cleanup;
3150 
3151 	/* copy genfs */
3152 	if (genfs_copy(&state))
3153 		goto cleanup;
3154 
3155 	/* Build the type<->attribute maps and remove attributes. */
3156 	state.out->attr_type_map = mallocarray(state.out->p_types.nprim,
3157 					  sizeof(ebitmap_t));
3158 	state.out->type_attr_map = mallocarray(state.out->p_types.nprim,
3159 					  sizeof(ebitmap_t));
3160 	if (!state.out->attr_type_map || !state.out->type_attr_map) {
3161 		ERR(handle, "Out of memory!");
3162 		goto cleanup;
3163 	}
3164 	for (i = 0; i < state.out->p_types.nprim; i++) {
3165 		ebitmap_init(&state.out->type_attr_map[i]);
3166 		ebitmap_init(&state.out->attr_type_map[i]);
3167 		/* add the type itself as the degenerate case */
3168 		if (ebitmap_set_bit(&state.out->type_attr_map[i], i, 1)) {
3169 			ERR(handle, "Out of memory!");
3170 			goto cleanup;
3171 		}
3172 	}
3173 	if (hashtab_map(state.out->p_types.table, type_attr_map, &state))
3174 		goto cleanup;
3175 	if (check) {
3176 		if (hierarchy_check_constraints(handle, state.out))
3177 			goto cleanup;
3178 
3179 		if (check_assertions
3180 		    (handle, state.out,
3181 		     state.out->global->branch_list->avrules))
3182 			 goto cleanup;
3183 	}
3184 
3185 	retval = 0;
3186 
3187       cleanup:
3188 	free(state.typemap);
3189 	free(state.boolmap);
3190 	free(state.rolemap);
3191 	free(state.usermap);
3192 	return retval;
3193 }
3194 
expand_avtab_insert(avtab_t * a,avtab_key_t * k,avtab_datum_t * d)3195 static int expand_avtab_insert(avtab_t * a, avtab_key_t * k, avtab_datum_t * d)
3196 {
3197 	avtab_ptr_t node;
3198 	avtab_datum_t *avd;
3199 	avtab_extended_perms_t *xperms;
3200 	unsigned int i;
3201 	unsigned int match = 0;
3202 
3203 	if (k->specified & AVTAB_XPERMS) {
3204 		/*
3205 		 * AVTAB_XPERMS entries are not necessarily unique.
3206 		 * find node with matching xperms
3207 		 */
3208 		node = avtab_search_node(a, k);
3209 		while (node) {
3210 			if ((node->datum.xperms->specified == d->xperms->specified) &&
3211 				(node->datum.xperms->driver == d->xperms->driver)) {
3212 				match = 1;
3213 				break;
3214 			}
3215 			node = avtab_search_node_next(node, k->specified);
3216 		}
3217 		if (!match)
3218 			node = NULL;
3219 	} else {
3220 		node = avtab_search_node(a, k);
3221 	}
3222 
3223 	if (!node || ((k->specified & AVTAB_ENABLED) !=
3224 			(node->key.specified & AVTAB_ENABLED))) {
3225 		node = avtab_insert_nonunique(a, k, d);
3226 		if (!node) {
3227 			ERR(NULL, "Out of memory!");
3228 			return -1;
3229 		}
3230 		return 0;
3231 	}
3232 
3233 	avd = &node->datum;
3234 	xperms = node->datum.xperms;
3235 	switch (k->specified & ~AVTAB_ENABLED) {
3236 	case AVTAB_ALLOWED:
3237 	case AVTAB_AUDITALLOW:
3238 		avd->data |= d->data;
3239 		break;
3240 	case AVTAB_AUDITDENY:
3241 		avd->data &= d->data;
3242 		break;
3243 	case AVTAB_XPERMS_ALLOWED:
3244 	case AVTAB_XPERMS_AUDITALLOW:
3245 	case AVTAB_XPERMS_DONTAUDIT:
3246 		for (i = 0; i < ARRAY_SIZE(xperms->perms); i++)
3247 			xperms->perms[i] |= d->xperms->perms[i];
3248 		break;
3249 	default:
3250 		ERR(NULL, "Type conflict!");
3251 		return -1;
3252 	}
3253 
3254 	return 0;
3255 }
3256 
3257 struct expand_avtab_data {
3258 	avtab_t *expa;
3259 	policydb_t *p;
3260 
3261 };
3262 
expand_avtab_node(avtab_key_t * k,avtab_datum_t * d,void * args)3263 static int expand_avtab_node(avtab_key_t * k, avtab_datum_t * d, void *args)
3264 {
3265 	struct expand_avtab_data *ptr = args;
3266 	avtab_t *expa = ptr->expa;
3267 	policydb_t *p = ptr->p;
3268 	type_datum_t *stype = p->type_val_to_struct[k->source_type - 1];
3269 	type_datum_t *ttype = p->type_val_to_struct[k->target_type - 1];
3270 	ebitmap_t *sattr = &p->attr_type_map[k->source_type - 1];
3271 	ebitmap_t *tattr = &p->attr_type_map[k->target_type - 1];
3272 	ebitmap_node_t *snode, *tnode;
3273 	unsigned int i, j;
3274 	avtab_key_t newkey;
3275 	int rc;
3276 
3277 	newkey.target_class = k->target_class;
3278 	newkey.specified = k->specified;
3279 
3280 	if (stype && ttype && stype->flavor != TYPE_ATTRIB && ttype->flavor != TYPE_ATTRIB) {
3281 		/* Both are individual types, no expansion required. */
3282 		return expand_avtab_insert(expa, k, d);
3283 	}
3284 
3285 	if (stype && stype->flavor != TYPE_ATTRIB) {
3286 		/* Source is an individual type, target is an attribute. */
3287 		newkey.source_type = k->source_type;
3288 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3289 			newkey.target_type = j + 1;
3290 			rc = expand_avtab_insert(expa, &newkey, d);
3291 			if (rc)
3292 				return -1;
3293 		}
3294 		return 0;
3295 	}
3296 
3297 	if (ttype && ttype->flavor != TYPE_ATTRIB) {
3298 		/* Target is an individual type, source is an attribute. */
3299 		newkey.target_type = k->target_type;
3300 		ebitmap_for_each_positive_bit(sattr, snode, i) {
3301 			newkey.source_type = i + 1;
3302 			rc = expand_avtab_insert(expa, &newkey, d);
3303 			if (rc)
3304 				return -1;
3305 		}
3306 		return 0;
3307 	}
3308 
3309 	/* Both source and target type are attributes. */
3310 	ebitmap_for_each_positive_bit(sattr, snode, i) {
3311 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3312 			newkey.source_type = i + 1;
3313 			newkey.target_type = j + 1;
3314 			rc = expand_avtab_insert(expa, &newkey, d);
3315 			if (rc)
3316 				return -1;
3317 		}
3318 	}
3319 
3320 	return 0;
3321 }
3322 
expand_avtab(policydb_t * p,avtab_t * a,avtab_t * expa)3323 int expand_avtab(policydb_t * p, avtab_t * a, avtab_t * expa)
3324 {
3325 	struct expand_avtab_data data;
3326 
3327 	if (avtab_alloc(expa, MAX_AVTAB_SIZE)) {
3328 		ERR(NULL, "Out of memory!");
3329 		return -1;
3330 	}
3331 
3332 	data.expa = expa;
3333 	data.p = p;
3334 	return avtab_map(a, expand_avtab_node, &data);
3335 }
3336 
expand_cond_insert(cond_av_list_t ** l,avtab_t * expa,avtab_key_t * k,avtab_datum_t * d)3337 static int expand_cond_insert(cond_av_list_t ** l,
3338 			      avtab_t * expa,
3339 			      avtab_key_t * k, avtab_datum_t * d)
3340 {
3341 	avtab_ptr_t node;
3342 	avtab_datum_t *avd;
3343 	cond_av_list_t *nl;
3344 
3345 	node = avtab_search_node(expa, k);
3346 	if (!node ||
3347 	    (k->specified & AVTAB_ENABLED) !=
3348 	    (node->key.specified & AVTAB_ENABLED)) {
3349 		node = avtab_insert_nonunique(expa, k, d);
3350 		if (!node) {
3351 			ERR(NULL, "Out of memory!");
3352 			return -1;
3353 		}
3354 		node->parse_context = (void *)1;
3355 		nl = (cond_av_list_t *) malloc(sizeof(*nl));
3356 		if (!nl) {
3357 			ERR(NULL, "Out of memory!");
3358 			return -1;
3359 		}
3360 		memset(nl, 0, sizeof(*nl));
3361 		nl->node = node;
3362 		nl->next = *l;
3363 		*l = nl;
3364 		return 0;
3365 	}
3366 
3367 	avd = &node->datum;
3368 	switch (k->specified & ~AVTAB_ENABLED) {
3369 	case AVTAB_ALLOWED:
3370 	case AVTAB_AUDITALLOW:
3371 		avd->data |= d->data;
3372 		break;
3373 	case AVTAB_AUDITDENY:
3374 		avd->data &= d->data;
3375 		break;
3376 	default:
3377 		ERR(NULL, "Type conflict!");
3378 		return -1;
3379 	}
3380 
3381 	return 0;
3382 }
3383 
expand_cond_av_node(policydb_t * p,avtab_ptr_t node,cond_av_list_t ** newl,avtab_t * expa)3384 static int expand_cond_av_node(policydb_t * p,
3385 			       avtab_ptr_t node,
3386 			       cond_av_list_t ** newl, avtab_t * expa)
3387 {
3388 	avtab_key_t *k = &node->key;
3389 	avtab_datum_t *d = &node->datum;
3390 	type_datum_t *stype = p->type_val_to_struct[k->source_type - 1];
3391 	type_datum_t *ttype = p->type_val_to_struct[k->target_type - 1];
3392 	ebitmap_t *sattr = &p->attr_type_map[k->source_type - 1];
3393 	ebitmap_t *tattr = &p->attr_type_map[k->target_type - 1];
3394 	ebitmap_node_t *snode, *tnode;
3395 	unsigned int i, j;
3396 	avtab_key_t newkey;
3397 	int rc;
3398 
3399 	newkey.target_class = k->target_class;
3400 	newkey.specified = k->specified;
3401 
3402 	if (stype && ttype && stype->flavor != TYPE_ATTRIB && ttype->flavor != TYPE_ATTRIB) {
3403 		/* Both are individual types, no expansion required. */
3404 		return expand_cond_insert(newl, expa, k, d);
3405 	}
3406 
3407 	if (stype && stype->flavor != TYPE_ATTRIB) {
3408 		/* Source is an individual type, target is an attribute. */
3409 		newkey.source_type = k->source_type;
3410 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3411 			newkey.target_type = j + 1;
3412 			rc = expand_cond_insert(newl, expa, &newkey, d);
3413 			if (rc)
3414 				return -1;
3415 		}
3416 		return 0;
3417 	}
3418 
3419 	if (ttype && ttype->flavor != TYPE_ATTRIB) {
3420 		/* Target is an individual type, source is an attribute. */
3421 		newkey.target_type = k->target_type;
3422 		ebitmap_for_each_positive_bit(sattr, snode, i) {
3423 			newkey.source_type = i + 1;
3424 			rc = expand_cond_insert(newl, expa, &newkey, d);
3425 			if (rc)
3426 				return -1;
3427 		}
3428 		return 0;
3429 	}
3430 
3431 	/* Both source and target type are attributes. */
3432 	ebitmap_for_each_positive_bit(sattr, snode, i) {
3433 		ebitmap_for_each_positive_bit(tattr, tnode, j) {
3434 			newkey.source_type = i + 1;
3435 			newkey.target_type = j + 1;
3436 			rc = expand_cond_insert(newl, expa, &newkey, d);
3437 			if (rc)
3438 				return -1;
3439 		}
3440 	}
3441 
3442 	return 0;
3443 }
3444 
expand_cond_av_list(policydb_t * p,cond_av_list_t * l,cond_av_list_t ** newl,avtab_t * expa)3445 int expand_cond_av_list(policydb_t * p, cond_av_list_t * l,
3446 			cond_av_list_t ** newl, avtab_t * expa)
3447 {
3448 	cond_av_list_t *cur;
3449 	avtab_ptr_t node;
3450 	int rc;
3451 
3452 	if (avtab_alloc(expa, MAX_AVTAB_SIZE)) {
3453 		ERR(NULL, "Out of memory!");
3454 		return -1;
3455 	}
3456 
3457 	*newl = NULL;
3458 	for (cur = l; cur; cur = cur->next) {
3459 		node = cur->node;
3460 		rc = expand_cond_av_node(p, node, newl, expa);
3461 		if (rc)
3462 			return rc;
3463 	}
3464 
3465 	return 0;
3466 }
3467