• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
2  *	    Joshua Brindle <jbrindle@tresys.com>
3  *          Jason Tang <jtang@tresys.com>
4  *
5  * Copyright (C) 2004-2005 Tresys Technology, LLC
6  * Copyright (C) 2007 Red Hat, Inc.
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #include <sepol/policydb/policydb.h>
24 #include <sepol/policydb/conditional.h>
25 #include <sepol/policydb/hashtab.h>
26 #include <sepol/policydb/avrule_block.h>
27 #include <sepol/policydb/link.h>
28 #include <sepol/policydb/util.h>
29 
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35 
36 #include "debug.h"
37 #include "private.h"
38 
39 #undef min
40 #define min(a,b) (((a) < (b)) ? (a) : (b))
41 
42 typedef struct policy_module {
43 	policydb_t *policy;
44 	uint32_t num_decls;
45 	uint32_t *map[SYM_NUM];
46 	uint32_t *avdecl_map;
47 	uint32_t **perm_map;
48 	uint32_t *perm_map_len;
49 
50 	/* a pointer to within the base module's avrule_block chain to
51 	 * where this module's global now resides */
52 	avrule_block_t *base_global;
53 } policy_module_t;
54 
55 typedef struct link_state {
56 	int verbose;
57 	policydb_t *base;
58 	avrule_block_t *last_avrule_block, *last_base_avrule_block;
59 	uint32_t next_decl_id, current_decl_id;
60 
61 	/* temporary variables, used during hashtab_map() calls */
62 	policy_module_t *cur;
63 	char *cur_mod_name;
64 	avrule_decl_t *dest_decl;
65 	class_datum_t *src_class, *dest_class;
66 	char *dest_class_name;
67 	char dest_class_req;	/* flag indicating the class was not declared */
68 	uint32_t symbol_num;
69 	/* used to report the name of the module if dependency error occurs */
70 	policydb_t **decl_to_mod;
71 
72 	/* error reporting fields */
73 	sepol_handle_t *handle;
74 } link_state_t;
75 
76 typedef struct missing_requirement {
77 	uint32_t symbol_type;
78 	uint32_t symbol_value;
79 	uint32_t perm_value;
80 } missing_requirement_t;
81 
82 static const char * const symtab_names[SYM_NUM] = {
83 	"common", "class", "role", "type/attribute", "user",
84 	"bool", "level", "category"
85 };
86 
87 /* Deallocates all elements within a module, but NOT the policydb_t
88  * structure within, as well as the pointer itself. */
policy_module_destroy(policy_module_t * mod)89 static void policy_module_destroy(policy_module_t * mod)
90 {
91 	unsigned int i;
92 	if (mod == NULL) {
93 		return;
94 	}
95 	for (i = 0; i < SYM_NUM; i++) {
96 		free(mod->map[i]);
97 	}
98 	for (i = 0; mod->perm_map != NULL && i < mod->policy->p_classes.nprim;
99 	     i++) {
100 		free(mod->perm_map[i]);
101 	}
102 	free(mod->perm_map);
103 	free(mod->perm_map_len);
104 	free(mod->avdecl_map);
105 	free(mod);
106 }
107 
108 /***** functions that copy identifiers from a module to base *****/
109 
110 /* Note: there is currently no scoping for permissions, which causes some
111  * strange side-effects. The current approach is this:
112  *
113  * a) perm is required and the class _and_ perm are declared in base: only add a mapping.
114  * b) perm is required and the class and perm are _not_ declared in base: simply add the permissions
115  *    to the object class. This means that the requirements for the decl are the union of the permissions
116  *    required for all decls, but who cares.
117  * c) perm is required, the class is declared in base, but the perm is not present. Nothing we can do
118  *    here because we can't mark a single permission as required, so we bail with a requirement error
119  *    _even_ if we are in an optional.
120  *
121  * A is correct behavior, b is wrong but not too bad, c is totall wrong for optionals. Fixing this requires
122  * a format change.
123  */
permission_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)124 static int permission_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
125 				    void *data)
126 {
127 	char *perm_id = key, *new_id = NULL;
128 	perm_datum_t *perm, *new_perm = NULL, *dest_perm;
129 	link_state_t *state = (link_state_t *) data;
130 
131 	class_datum_t *src_class = state->src_class;
132 	class_datum_t *dest_class = state->dest_class;
133 	policy_module_t *mod = state->cur;
134 	uint32_t sclassi = src_class->s.value - 1;
135 	int ret;
136 
137 	perm = (perm_datum_t *) datum;
138 	dest_perm = hashtab_search(dest_class->permissions.table, perm_id);
139 	if (dest_perm == NULL && dest_class->comdatum != NULL) {
140 		dest_perm =
141 		    hashtab_search(dest_class->comdatum->permissions.table,
142 				   perm_id);
143 	}
144 
145 	if (dest_perm == NULL) {
146 		/* If the object class was not declared in the base, add the perm
147 		 * to the object class. */
148 		if (state->dest_class_req) {
149 			/* If the class was required (not declared), insert the new permission */
150 			new_id = strdup(perm_id);
151 			if (new_id == NULL) {
152 				ERR(state->handle, "Memory error");
153 				ret = SEPOL_ERR;
154 				goto err;
155 			}
156 			new_perm =
157 			    (perm_datum_t *) calloc(1, sizeof(perm_datum_t));
158 			if (new_perm == NULL) {
159 				ERR(state->handle, "Memory error");
160 				ret = SEPOL_ERR;
161 				goto err;
162 			}
163 			ret = hashtab_insert(dest_class->permissions.table,
164 					     (hashtab_key_t) new_id,
165 					     (hashtab_datum_t) new_perm);
166 			if (ret) {
167 				ERR(state->handle,
168 				    "could not insert permission into class");
169 				goto err;
170 			}
171 			new_perm->s.value = dest_class->permissions.nprim + 1;
172 			dest_perm = new_perm;
173 		} else {
174 			/* this is case c from above */
175 			ERR(state->handle,
176 			    "Module %s depends on permission %s in class %s, not satisfied",
177 			    state->cur_mod_name, perm_id,
178 			    state->dest_class_name);
179 			return SEPOL_EREQ;
180 		}
181 	}
182 
183 	/* build the mapping for permissions encompassing this class.
184 	 * unlike symbols, the permission map translates between
185 	 * module permission bit to target permission bit.  that bit
186 	 * may have originated from the class -or- it could be from
187 	 * the class's common parent.*/
188 	if (perm->s.value > mod->perm_map_len[sclassi]) {
189 		uint32_t *newmap = calloc(perm->s.value, sizeof(*newmap));
190 		if (newmap == NULL) {
191 			ERR(state->handle, "Out of memory!");
192 			return -1;
193 		}
194 		if (mod->perm_map_len[sclassi] > 0) {
195 			memcpy(newmap, mod->perm_map[sclassi], mod->perm_map_len[sclassi] * sizeof(*newmap));
196 		}
197 		free(mod->perm_map[sclassi]);
198 		mod->perm_map[sclassi] = newmap;
199 		mod->perm_map_len[sclassi] = perm->s.value;
200 	}
201 	mod->perm_map[sclassi][perm->s.value - 1] = dest_perm->s.value;
202 
203 	return 0;
204       err:
205 	free(new_id);
206 	free(new_perm);
207 	return ret;
208 }
209 
class_copy_default_new_object(link_state_t * state,class_datum_t * olddatum,class_datum_t * newdatum)210 static int class_copy_default_new_object(link_state_t *state,
211 					 class_datum_t *olddatum,
212 					 class_datum_t *newdatum)
213 {
214 	if (olddatum->default_user) {
215 		if (newdatum->default_user && olddatum->default_user != newdatum->default_user) {
216 			ERR(state->handle, "Found conflicting default user definitions");
217 			return SEPOL_ENOTSUP;
218 		}
219 		newdatum->default_user = olddatum->default_user;
220 	}
221 	if (olddatum->default_role) {
222 		if (newdatum->default_role && olddatum->default_role != newdatum->default_role) {
223 			ERR(state->handle, "Found conflicting default role definitions");
224 			return SEPOL_ENOTSUP;
225 		}
226 		newdatum->default_role = olddatum->default_role;
227 	}
228 	if (olddatum->default_type) {
229 		if (newdatum->default_type && olddatum->default_type != newdatum->default_type) {
230 			ERR(state->handle, "Found conflicting default type definitions");
231 			return SEPOL_ENOTSUP;
232 		}
233 		newdatum->default_type = olddatum->default_type;
234 	}
235 	if (olddatum->default_range) {
236 		if (newdatum->default_range && olddatum->default_range != newdatum->default_range) {
237 			ERR(state->handle, "Found conflicting default range definitions");
238 			return SEPOL_ENOTSUP;
239 		}
240 		newdatum->default_range = olddatum->default_range;
241 	}
242 	return 0;
243 }
244 
class_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)245 static int class_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
246 			       void *data)
247 {
248 	char *id = key, *new_id = NULL;
249 	class_datum_t *cladatum, *new_class = NULL;
250 	link_state_t *state = (link_state_t *) data;
251 	scope_datum_t *scope = NULL;
252 	int ret;
253 
254 	cladatum = (class_datum_t *) datum;
255 	state->dest_class_req = 0;
256 
257 	new_class = hashtab_search(state->base->p_classes.table, id);
258 	/* If there is not an object class already in the base symtab that means
259 	 * that either a) a module is trying to declare a new object class (which
260 	 * the compiler should prevent) or b) an object class was required that is
261 	 * not in the base.
262 	 */
263 	if (new_class == NULL) {
264 		scope =
265 		    hashtab_search(state->cur->policy->p_classes_scope.table,
266 				   id);
267 		if (scope == NULL) {
268 			ret = SEPOL_ERR;
269 			goto err;
270 		}
271 		if (scope->scope == SCOPE_DECL) {
272 			/* disallow declarations in modules */
273 			ERR(state->handle,
274 			    "%s: Modules may not yet declare new classes.",
275 			    state->cur_mod_name);
276 			ret = SEPOL_ENOTSUP;
277 			goto err;
278 		} else {
279 			/* It would be nice to error early here because the requirement is
280 			 * not met, but we cannot because the decl might be optional (in which
281 			 * case we should record the requirement so that it is just turned
282 			 * off). Note: this will break horribly if modules can declare object
283 			 * classes because the class numbers will be all wrong (i.e., they
284 			 * might be assigned in the order they were required rather than the
285 			 * current scheme which ensures correct numbering by ordering the
286 			 * declarations properly). This can't be fixed until some infrastructure
287 			 * for querying the object class numbers is in place. */
288 			state->dest_class_req = 1;
289 			new_class =
290 			    (class_datum_t *) calloc(1, sizeof(class_datum_t));
291 			if (new_class == NULL) {
292 				ERR(state->handle, "Memory error");
293 				ret = SEPOL_ERR;
294 				goto err;
295 			}
296 			if (symtab_init
297 			    (&new_class->permissions, PERM_SYMTAB_SIZE)) {
298 				ret = SEPOL_ERR;
299 				goto err;
300 			}
301 			new_id = strdup(id);
302 			if (new_id == NULL) {
303 				ERR(state->handle, "Memory error");
304 				symtab_destroy(&new_class->permissions);
305 				ret = SEPOL_ERR;
306 				goto err;
307 			}
308 			ret = hashtab_insert(state->base->p_classes.table,
309 					     (hashtab_key_t) new_id,
310 					     (hashtab_datum_t) new_class);
311 			if (ret) {
312 				ERR(state->handle,
313 				    "could not insert new class into symtab");
314 				symtab_destroy(&new_class->permissions);
315 				goto err;
316 			}
317 			new_class->s.value = ++(state->base->p_classes.nprim);
318 		}
319 	}
320 
321 	state->cur->map[SYM_CLASSES][cladatum->s.value - 1] =
322 	    new_class->s.value;
323 
324 	/* copy permissions */
325 	state->src_class = cladatum;
326 	state->dest_class = new_class;
327 	state->dest_class_name = (char *)key;
328 
329 	/* copy default new object rules */
330 	ret = class_copy_default_new_object(state, cladatum, new_class);
331 	if (ret)
332 		return ret;
333 
334 	ret =
335 	    hashtab_map(cladatum->permissions.table, permission_copy_callback,
336 			state);
337 	if (ret != 0) {
338 		return ret;
339 	}
340 
341 	return 0;
342       err:
343 	free(new_class);
344 	free(new_id);
345 	return ret;
346 }
347 
role_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)348 static int role_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
349 			      void *data)
350 {
351 	int ret;
352 	char *id = key, *new_id = NULL;
353 	role_datum_t *role, *base_role, *new_role = NULL;
354 	link_state_t *state = (link_state_t *) data;
355 
356 	role = (role_datum_t *) datum;
357 
358 	base_role = hashtab_search(state->base->p_roles.table, id);
359 	if (base_role != NULL) {
360 		/* role already exists.  check that it is what this
361 		 * module expected.  duplicate declarations (e.g., two
362 		 * modules both declare role foo_r) is checked during
363 		 * scope_copy_callback(). */
364 		if (role->flavor == ROLE_ATTRIB
365 		    && base_role->flavor != ROLE_ATTRIB) {
366 			ERR(state->handle,
367 			    "%s: Expected %s to be a role attribute, but it was already declared as a regular role.",
368 			    state->cur_mod_name, id);
369 			return -1;
370 		} else if (role->flavor != ROLE_ATTRIB
371 			   && base_role->flavor == ROLE_ATTRIB) {
372 			ERR(state->handle,
373 			    "%s: Expected %s to be a regular role, but it was already declared as a role attribute.",
374 			    state->cur_mod_name, id);
375 			return -1;
376 		}
377 	} else {
378 		if (state->verbose)
379 			INFO(state->handle, "copying role %s", id);
380 
381 		if ((new_id = strdup(id)) == NULL) {
382 			goto cleanup;
383 		}
384 
385 		if ((new_role =
386 		     (role_datum_t *) malloc(sizeof(*new_role))) == NULL) {
387 			goto cleanup;
388 		}
389 		role_datum_init(new_role);
390 
391 		/* new_role's dominates, types and roles field will be copied
392 		 * during role_fix_callback() */
393 		new_role->flavor = role->flavor;
394 		new_role->s.value = state->base->p_roles.nprim + 1;
395 
396 		ret = hashtab_insert(state->base->p_roles.table,
397 				     (hashtab_key_t) new_id,
398 				     (hashtab_datum_t) new_role);
399 		if (ret) {
400 			goto cleanup;
401 		}
402 		state->base->p_roles.nprim++;
403 		base_role = new_role;
404 	}
405 
406 	if (state->dest_decl) {
407 		new_id = NULL;
408 		if ((new_role = malloc(sizeof(*new_role))) == NULL) {
409 			goto cleanup;
410 		}
411 		role_datum_init(new_role);
412 		new_role->flavor = base_role->flavor;
413 		new_role->s.value = base_role->s.value;
414 		if ((new_id = strdup(id)) == NULL) {
415 			goto cleanup;
416 		}
417 		if (hashtab_insert
418 		    (state->dest_decl->p_roles.table, new_id, new_role)) {
419 			goto cleanup;
420 		}
421 		state->dest_decl->p_roles.nprim++;
422 	}
423 
424 	state->cur->map[SYM_ROLES][role->s.value - 1] = base_role->s.value;
425 	return 0;
426 
427       cleanup:
428 	ERR(state->handle, "Out of memory!");
429 	role_datum_destroy(new_role);
430 	free(new_id);
431 	free(new_role);
432 	return -1;
433 }
434 
435 /* Copy types and attributes from a module into the base module. The
436  * attributes are copied, but the types that make up this attribute
437  * are delayed type_fix_callback(). */
type_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)438 static int type_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
439 			      void *data)
440 {
441 	int ret;
442 	char *id = key, *new_id = NULL;
443 	type_datum_t *type, *base_type, *new_type = NULL;
444 	link_state_t *state = (link_state_t *) data;
445 
446 	type = (type_datum_t *) datum;
447 	if ((type->flavor == TYPE_TYPE && !type->primary)
448 	    || type->flavor == TYPE_ALIAS) {
449 		/* aliases are handled later, in alias_copy_callback() */
450 		return 0;
451 	}
452 
453 	base_type = hashtab_search(state->base->p_types.table, id);
454 	if (base_type != NULL) {
455 		/* type already exists.  check that it is what this
456 		 * module expected.  duplicate declarations (e.g., two
457 		 * modules both declare type foo_t) is checked during
458 		 * scope_copy_callback(). */
459 		if (type->flavor == TYPE_ATTRIB
460 		    && base_type->flavor != TYPE_ATTRIB) {
461 			ERR(state->handle,
462 			    "%s: Expected %s to be an attribute, but it was already declared as a type.",
463 			    state->cur_mod_name, id);
464 			return -1;
465 		} else if (type->flavor != TYPE_ATTRIB
466 			   && base_type->flavor == TYPE_ATTRIB) {
467 			ERR(state->handle,
468 			    "%s: Expected %s to be a type, but it was already declared as an attribute.",
469 			    state->cur_mod_name, id);
470 			return -1;
471 		}
472 
473 		base_type->flags |= type->flags;
474 	} else {
475 		if (state->verbose)
476 			INFO(state->handle, "copying type %s", id);
477 
478 		if ((new_id = strdup(id)) == NULL) {
479 			goto cleanup;
480 		}
481 
482 		if ((new_type =
483 		     (type_datum_t *) calloc(1, sizeof(*new_type))) == NULL) {
484 			goto cleanup;
485 		}
486 		new_type->primary = type->primary;
487 		new_type->flags = type->flags;
488 		new_type->flavor = type->flavor;
489 		/* for attributes, the writing of new_type->types is
490 		   done in type_fix_callback() */
491 
492 		new_type->s.value = state->base->p_types.nprim + 1;
493 
494 		ret = hashtab_insert(state->base->p_types.table,
495 				     (hashtab_key_t) new_id,
496 				     (hashtab_datum_t) new_type);
497 		if (ret) {
498 			goto cleanup;
499 		}
500 		state->base->p_types.nprim++;
501 		base_type = new_type;
502 	}
503 
504 	if (state->dest_decl) {
505 		new_id = NULL;
506 		if ((new_type = calloc(1, sizeof(*new_type))) == NULL) {
507 			goto cleanup;
508 		}
509 		new_type->primary = type->primary;
510 		new_type->flavor = type->flavor;
511 		new_type->flags = type->flags;
512 		new_type->s.value = base_type->s.value;
513 		if ((new_id = strdup(id)) == NULL) {
514 			goto cleanup;
515 		}
516 		if (hashtab_insert
517 		    (state->dest_decl->p_types.table, new_id, new_type)) {
518 			goto cleanup;
519 		}
520 		state->dest_decl->p_types.nprim++;
521 	}
522 
523 	state->cur->map[SYM_TYPES][type->s.value - 1] = base_type->s.value;
524 	return 0;
525 
526       cleanup:
527 	ERR(state->handle, "Out of memory!");
528 	free(new_id);
529 	free(new_type);
530 	return -1;
531 }
532 
user_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)533 static int user_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
534 			      void *data)
535 {
536 	int ret;
537 	char *id = key, *new_id = NULL;
538 	user_datum_t *user, *base_user, *new_user = NULL;
539 	link_state_t *state = (link_state_t *) data;
540 
541 	user = (user_datum_t *) datum;
542 
543 	base_user = hashtab_search(state->base->p_users.table, id);
544 	if (base_user == NULL) {
545 		if (state->verbose)
546 			INFO(state->handle, "copying user %s", id);
547 
548 		if ((new_id = strdup(id)) == NULL) {
549 			goto cleanup;
550 		}
551 
552 		if ((new_user =
553 		     (user_datum_t *) malloc(sizeof(*new_user))) == NULL) {
554 			goto cleanup;
555 		}
556 		user_datum_init(new_user);
557 		/* new_users's roles and MLS fields will be copied during
558 		   user_fix_callback(). */
559 
560 		new_user->s.value = state->base->p_users.nprim + 1;
561 
562 		ret = hashtab_insert(state->base->p_users.table,
563 				     (hashtab_key_t) new_id,
564 				     (hashtab_datum_t) new_user);
565 		if (ret) {
566 			goto cleanup;
567 		}
568 		state->base->p_users.nprim++;
569 		base_user = new_user;
570 	}
571 
572 	if (state->dest_decl) {
573 		new_id = NULL;
574 		if ((new_user = malloc(sizeof(*new_user))) == NULL) {
575 			goto cleanup;
576 		}
577 		user_datum_init(new_user);
578 		new_user->s.value = base_user->s.value;
579 		if ((new_id = strdup(id)) == NULL) {
580 			goto cleanup;
581 		}
582 		if (hashtab_insert
583 		    (state->dest_decl->p_users.table, new_id, new_user)) {
584 			goto cleanup;
585 		}
586 		state->dest_decl->p_users.nprim++;
587 	}
588 
589 	state->cur->map[SYM_USERS][user->s.value - 1] = base_user->s.value;
590 	return 0;
591 
592       cleanup:
593 	ERR(state->handle, "Out of memory!");
594 	user_datum_destroy(new_user);
595 	free(new_id);
596 	free(new_user);
597 	return -1;
598 }
599 
bool_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)600 static int bool_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
601 			      void *data)
602 {
603 	int ret;
604 	char *id = key, *new_id = NULL;
605 	cond_bool_datum_t *booldatum, *base_bool, *new_bool = NULL;
606 	link_state_t *state = (link_state_t *) data;
607 	scope_datum_t *scope;
608 
609 	booldatum = (cond_bool_datum_t *) datum;
610 
611 	base_bool = hashtab_search(state->base->p_bools.table, id);
612 	if (base_bool == NULL) {
613 		if (state->verbose)
614 			INFO(state->handle, "copying boolean %s", id);
615 
616 		if ((new_id = strdup(id)) == NULL) {
617 			goto cleanup;
618 		}
619 
620 		if ((new_bool =
621 		     (cond_bool_datum_t *) malloc(sizeof(*new_bool))) == NULL) {
622 			goto cleanup;
623 		}
624 		new_bool->s.value = state->base->p_bools.nprim + 1;
625 
626 		ret = hashtab_insert(state->base->p_bools.table,
627 				     (hashtab_key_t) new_id,
628 				     (hashtab_datum_t) new_bool);
629 		if (ret) {
630 			goto cleanup;
631 		}
632 		state->base->p_bools.nprim++;
633 		base_bool = new_bool;
634 		base_bool->flags = booldatum->flags;
635 		base_bool->state = booldatum->state;
636 	} else if ((booldatum->flags & COND_BOOL_FLAGS_TUNABLE) !=
637 		   (base_bool->flags & COND_BOOL_FLAGS_TUNABLE)) {
638 			/* A mismatch between boolean/tunable declaration
639 			 * and usage(for example a boolean used in the
640 			 * tunable_policy() or vice versa).
641 			 *
642 			 * This is not allowed and bail out with errors */
643 			ERR(state->handle,
644 			    "%s: Mismatch between boolean/tunable definition "
645 			    "and usage for %s", state->cur_mod_name, id);
646 			return -1;
647 	}
648 
649 	/* Get the scope info for this boolean to see if this is the declaration,
650  	 * if so set the state */
651 	scope = hashtab_search(state->cur->policy->p_bools_scope.table, id);
652 	if (!scope)
653 		return SEPOL_ERR;
654 	if (scope->scope == SCOPE_DECL) {
655 		base_bool->state = booldatum->state;
656 		/* Only the declaration rather than requirement
657 		 * decides if it is a boolean or tunable. */
658 		base_bool->flags = booldatum->flags;
659 	}
660 	state->cur->map[SYM_BOOLS][booldatum->s.value - 1] = base_bool->s.value;
661 	return 0;
662 
663       cleanup:
664 	ERR(state->handle, "Out of memory!");
665 	cond_destroy_bool(new_id, new_bool, NULL);
666 	return -1;
667 }
668 
sens_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)669 static int sens_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
670 			      void *data)
671 {
672 	char *id = key;
673 	level_datum_t *level, *base_level;
674 	link_state_t *state = (link_state_t *) data;
675 	scope_datum_t *scope;
676 
677 	level = (level_datum_t *) datum;
678 
679 	base_level = hashtab_search(state->base->p_levels.table, id);
680 	if (!base_level) {
681 		scope =
682 		    hashtab_search(state->cur->policy->p_sens_scope.table, id);
683 		if (!scope)
684 			return SEPOL_ERR;
685 		if (scope->scope == SCOPE_DECL) {
686 			/* disallow declarations in modules */
687 			ERR(state->handle,
688 			    "%s: Modules may not declare new sensitivities.",
689 			    state->cur_mod_name);
690 			return SEPOL_ENOTSUP;
691 		} else if (scope->scope == SCOPE_REQ) {
692 			/* unmet requirement */
693 			ERR(state->handle,
694 			    "%s: Sensitivity %s not declared by base.",
695 			    state->cur_mod_name, id);
696 			return SEPOL_ENOTSUP;
697 		} else {
698 			ERR(state->handle,
699 			    "%s: has an unknown scope: %d",
700 			    state->cur_mod_name, scope->scope);
701 			return SEPOL_ENOTSUP;
702 		}
703 	}
704 
705 	state->cur->map[SYM_LEVELS][level->level->sens - 1] =
706 	    base_level->level->sens;
707 
708 	return 0;
709 }
710 
cat_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)711 static int cat_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
712 			     void *data)
713 {
714 	char *id = key;
715 	cat_datum_t *cat, *base_cat;
716 	link_state_t *state = (link_state_t *) data;
717 	scope_datum_t *scope;
718 
719 	cat = (cat_datum_t *) datum;
720 
721 	base_cat = hashtab_search(state->base->p_cats.table, id);
722 	if (!base_cat) {
723 		scope = hashtab_search(state->cur->policy->p_cat_scope.table, id);
724 		if (!scope)
725 			return SEPOL_ERR;
726 		if (scope->scope == SCOPE_DECL) {
727 			/* disallow declarations in modules */
728 			ERR(state->handle,
729 			    "%s: Modules may not declare new categories.",
730 			    state->cur_mod_name);
731 			return SEPOL_ENOTSUP;
732 		} else if (scope->scope == SCOPE_REQ) {
733 			/* unmet requirement */
734 			ERR(state->handle,
735 			    "%s: Category %s not declared by base.",
736 			    state->cur_mod_name, id);
737 			return SEPOL_ENOTSUP;
738 		} else {
739 			/* unknown scope?  malformed policy? */
740 			ERR(state->handle,
741 			    "%s: has an unknown scope: %d",
742 			    state->cur_mod_name, scope->scope);
743 			return SEPOL_ENOTSUP;
744 		}
745 	}
746 
747 	state->cur->map[SYM_CATS][cat->s.value - 1] = base_cat->s.value;
748 
749 	return 0;
750 }
751 
752 static int (*copy_callback_f[SYM_NUM]) (hashtab_key_t key,
753 					hashtab_datum_t datum, void *datap) = {
754 NULL, class_copy_callback, role_copy_callback, type_copy_callback,
755 	    user_copy_callback, bool_copy_callback, sens_copy_callback,
756 	    cat_copy_callback};
757 
758 /*
759  * The boundaries have to be copied after the types/roles/users are copied,
760  * because it refers hashtab to lookup destinated objects.
761  */
type_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)762 static int type_bounds_copy_callback(hashtab_key_t key,
763 				     hashtab_datum_t datum, void *data)
764 {
765 	link_state_t *state = (link_state_t *) data;
766 	type_datum_t *type = (type_datum_t *) datum;
767 	type_datum_t *dest;
768 	uint32_t bounds_val;
769 
770 	if (!type->bounds)
771 		return 0;
772 
773 	bounds_val = state->cur->map[SYM_TYPES][type->bounds - 1];
774 
775 	dest = hashtab_search(state->base->p_types.table, key);
776 	if (!dest) {
777 		ERR(state->handle,
778 		    "Type lookup failed for %s", (char *)key);
779 		return -1;
780 	}
781 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
782 		ERR(state->handle,
783 		    "Inconsistent boundary for %s", (char *)key);
784 		return -1;
785 	}
786 	dest->bounds = bounds_val;
787 
788 	return 0;
789 }
790 
role_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)791 static int role_bounds_copy_callback(hashtab_key_t key,
792 				     hashtab_datum_t datum, void *data)
793 {
794 	link_state_t *state = (link_state_t *) data;
795 	role_datum_t *role = (role_datum_t *) datum;
796 	role_datum_t *dest;
797 	uint32_t bounds_val;
798 
799 	if (!role->bounds)
800 		return 0;
801 
802 	bounds_val = state->cur->map[SYM_ROLES][role->bounds - 1];
803 
804 	dest = hashtab_search(state->base->p_roles.table, key);
805 	if (!dest) {
806 		ERR(state->handle,
807 		    "Role lookup failed for %s", (char *)key);
808 		return -1;
809 	}
810 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
811 		ERR(state->handle,
812 		    "Inconsistent boundary for %s", (char *)key);
813 		return -1;
814 	}
815 	dest->bounds = bounds_val;
816 
817 	return 0;
818 }
819 
user_bounds_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)820 static int user_bounds_copy_callback(hashtab_key_t key,
821 				     hashtab_datum_t datum, void *data)
822 {
823 	link_state_t *state = (link_state_t *) data;
824 	user_datum_t *user = (user_datum_t *) datum;
825 	user_datum_t *dest;
826 	uint32_t bounds_val;
827 
828 	if (!user->bounds)
829 		return 0;
830 
831 	bounds_val = state->cur->map[SYM_USERS][user->bounds - 1];
832 
833 	dest = hashtab_search(state->base->p_users.table, key);
834 	if (!dest) {
835 		ERR(state->handle,
836 		    "User lookup failed for %s", (char *)key);
837 		return -1;
838 	}
839 	if (dest->bounds != 0 && dest->bounds != bounds_val) {
840 		ERR(state->handle,
841 		    "Inconsistent boundary for %s", (char *)key);
842 		return -1;
843 	}
844 	dest->bounds = bounds_val;
845 
846 	return 0;
847 }
848 
849 /* The aliases have to be copied after the types and attributes to be
850  * certain that the base symbol table will have the type that the
851  * alias refers. Otherwise, we won't be able to find the type value
852  * for the alias. We can't depend on the declaration ordering because
853  * of the hash table.
854  */
alias_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)855 static int alias_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
856 			       void *data)
857 {
858 	char *id = key, *new_id = NULL, *target_id;
859 	type_datum_t *type, *base_type, *new_type = NULL, *target_type;
860 	link_state_t *state = (link_state_t *) data;
861 	policy_module_t *mod = state->cur;
862 	int primval;
863 
864 	type = (type_datum_t *) datum;
865 	/* there are 2 kinds of aliases. Ones with their own value (TYPE_ALIAS)
866 	 * and ones with the value of their primary (TYPE_TYPE && type->primary = 0)
867 	 */
868 	if (!
869 	    (type->flavor == TYPE_ALIAS
870 	     || (type->flavor == TYPE_TYPE && !type->primary))) {
871 		/* ignore types and attributes -- they were handled in
872 		 * type_copy_callback() */
873 		return 0;
874 	}
875 
876 	if (type->flavor == TYPE_ALIAS)
877 		primval = type->primary;
878 	else
879 		primval = type->s.value;
880 
881 	target_id = mod->policy->p_type_val_to_name[primval - 1];
882 	target_type = hashtab_search(state->base->p_types.table, target_id);
883 	if (target_type == NULL) {
884 		ERR(state->handle, "%s: Could not find type %s for alias %s.",
885 		    state->cur_mod_name, target_id, id);
886 		return -1;
887 	}
888 
889 	if (!strcmp(id, target_id)) {
890 		ERR(state->handle, "%s: Self aliasing of %s.",
891 		    state->cur_mod_name, id);
892 		return -1;
893 	}
894 
895 	target_type->flags |= type->flags;
896 
897 	base_type = hashtab_search(state->base->p_types.table, id);
898 	if (base_type == NULL) {
899 		if (state->verbose)
900 			INFO(state->handle, "copying alias %s", id);
901 
902 		if ((new_type =
903 		     (type_datum_t *) calloc(1, sizeof(*new_type))) == NULL) {
904 			goto cleanup;
905 		}
906 		/* the linked copy always has TYPE_ALIAS style aliases */
907 		new_type->primary = target_type->s.value;
908 		new_type->flags = target_type->flags;
909 		new_type->flavor = TYPE_ALIAS;
910 		new_type->s.value = state->base->p_types.nprim + 1;
911 		if ((new_id = strdup(id)) == NULL) {
912 			goto cleanup;
913 		}
914 		if (hashtab_insert
915 		    (state->base->p_types.table, new_id, new_type)) {
916 			goto cleanup;
917 		}
918 		state->base->p_types.nprim++;
919 		base_type = new_type;
920 	} else {
921 
922 		/* if this already exists and isn't an alias it was required by another module (or base)
923 		 * and inserted into the hashtable as a type, fix it up now */
924 
925 		if (base_type->flavor == TYPE_ALIAS) {
926 			/* error checking */
927 			assert(base_type->primary == target_type->s.value);
928 			assert(base_type->primary ==
929 			       mod->map[SYM_TYPES][primval - 1]);
930 			assert(mod->map[SYM_TYPES][type->s.value - 1] ==
931 			       base_type->primary);
932 			return 0;
933 		}
934 
935 		if (base_type->flavor == TYPE_ATTRIB) {
936 			ERR(state->handle,
937 			    "%s is an alias of an attribute, not allowed", id);
938 			return -1;
939 		}
940 
941 		base_type->flavor = TYPE_ALIAS;
942 		base_type->primary = target_type->s.value;
943 		base_type->flags |= target_type->flags;
944 
945 	}
946 	/* the aliases map points from its value to its primary so when this module
947 	 * references this type the value it gets back from the map is the primary */
948 	mod->map[SYM_TYPES][type->s.value - 1] = base_type->primary;
949 
950 	return 0;
951 
952       cleanup:
953 	ERR(state->handle, "Out of memory!");
954 	free(new_id);
955 	free(new_type);
956 	return -1;
957 }
958 
959 /*********** callbacks that fix bitmaps ***********/
960 
ebitmap_convert(const ebitmap_t * src,ebitmap_t * dst,const uint32_t * map)961 static int ebitmap_convert(const ebitmap_t *src, ebitmap_t *dst, const uint32_t *map)
962 {
963 	unsigned int bit;
964 	ebitmap_node_t *node;
965 	ebitmap_for_each_positive_bit(src, node, bit) {
966 		assert(map[bit]);
967 		if (ebitmap_set_bit(dst, map[bit] - 1, 1))
968 			return -1;
969 	}
970 
971 	return 0;
972 }
973 
type_set_convert(const type_set_t * types,type_set_t * dst,const policy_module_t * mod)974 static int type_set_convert(const type_set_t * types, type_set_t * dst,
975 			    const policy_module_t * mod)
976 {
977 	if (ebitmap_convert(&types->types, &dst->types, mod->map[SYM_TYPES]))
978 		goto cleanup;
979 
980 	if (ebitmap_convert(&types->negset, &dst->negset, mod->map[SYM_TYPES]))
981 		goto cleanup;
982 
983 	dst->flags = types->flags;
984 	return 0;
985 
986       cleanup:
987 	return -1;
988 }
989 
990 /* OR 2 typemaps together and at the same time map the src types to
991  * the correct values in the dst typeset.
992  */
type_set_or_convert(const type_set_t * types,type_set_t * dst,const policy_module_t * mod)993 static int type_set_or_convert(const type_set_t * types, type_set_t * dst,
994 			       const policy_module_t * mod)
995 {
996 	type_set_t ts_tmp;
997 
998 	type_set_init(&ts_tmp);
999 	if (type_set_convert(types, &ts_tmp, mod) == -1) {
1000 		goto cleanup;
1001 	}
1002 	if (type_set_or_eq(dst, &ts_tmp)) {
1003 		goto cleanup;
1004 	}
1005 	type_set_destroy(&ts_tmp);
1006 	return 0;
1007 
1008       cleanup:
1009 	type_set_destroy(&ts_tmp);
1010 	return -1;
1011 }
1012 
role_set_or_convert(role_set_t * roles,role_set_t * dst,policy_module_t * mod,link_state_t * state)1013 static int role_set_or_convert(role_set_t * roles, role_set_t * dst,
1014 			       policy_module_t * mod, link_state_t * state)
1015 {
1016 	ebitmap_t tmp;
1017 
1018 	ebitmap_init(&tmp);
1019 	if (ebitmap_convert(&roles->roles, &tmp, mod->map[SYM_ROLES]))
1020 		goto cleanup;
1021 	if (ebitmap_union(&dst->roles, &tmp)) {
1022 		goto cleanup;
1023 	}
1024 	dst->flags |= roles->flags;
1025 	ebitmap_destroy(&tmp);
1026 	return 0;
1027       cleanup:
1028 	ERR(state->handle, "Out of memory!");
1029 	ebitmap_destroy(&tmp);
1030 	return -1;
1031 }
1032 
mls_level_convert(mls_semantic_level_t * src,mls_semantic_level_t * dst,policy_module_t * mod,link_state_t * state)1033 static int mls_level_convert(mls_semantic_level_t * src, mls_semantic_level_t * dst,
1034 			     policy_module_t * mod, link_state_t * state)
1035 {
1036 	mls_semantic_cat_t *src_cat, *new_cat;
1037 
1038 	if (!mod->policy->mls)
1039 		return 0;
1040 
1041 	/* Required not declared. */
1042 	if (!src->sens)
1043 		return 0;
1044 
1045 	assert(mod->map[SYM_LEVELS][src->sens - 1]);
1046 	dst->sens = mod->map[SYM_LEVELS][src->sens - 1];
1047 
1048 	for (src_cat = src->cat; src_cat; src_cat = src_cat->next) {
1049 		new_cat =
1050 		    (mls_semantic_cat_t *) malloc(sizeof(mls_semantic_cat_t));
1051 		if (!new_cat) {
1052 			ERR(state->handle, "Out of memory");
1053 			return -1;
1054 		}
1055 		mls_semantic_cat_init(new_cat);
1056 
1057 		new_cat->next = dst->cat;
1058 		dst->cat = new_cat;
1059 
1060 		assert(mod->map[SYM_CATS][src_cat->low - 1]);
1061 		dst->cat->low = mod->map[SYM_CATS][src_cat->low - 1];
1062 		assert(mod->map[SYM_CATS][src_cat->high - 1]);
1063 		dst->cat->high = mod->map[SYM_CATS][src_cat->high - 1];
1064 	}
1065 
1066 	return 0;
1067 }
1068 
mls_range_convert(mls_semantic_range_t * src,mls_semantic_range_t * dst,policy_module_t * mod,link_state_t * state)1069 static int mls_range_convert(mls_semantic_range_t * src, mls_semantic_range_t * dst,
1070 			     policy_module_t * mod, link_state_t * state)
1071 {
1072 	int ret;
1073 	ret = mls_level_convert(&src->level[0], &dst->level[0], mod, state);
1074 	if (ret)
1075 		return ret;
1076 	ret = mls_level_convert(&src->level[1], &dst->level[1], mod, state);
1077 	if (ret)
1078 		return ret;
1079 	return 0;
1080 }
1081 
role_fix_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1082 static int role_fix_callback(hashtab_key_t key, hashtab_datum_t datum,
1083 			     void *data)
1084 {
1085 	char *id = key;
1086 	role_datum_t *role, *dest_role = NULL;
1087 	link_state_t *state = (link_state_t *) data;
1088 	ebitmap_t e_tmp;
1089 	policy_module_t *mod = state->cur;
1090 	hashtab_t role_tab;
1091 
1092 	role = (role_datum_t *) datum;
1093 	if (state->dest_decl == NULL)
1094 		role_tab = state->base->p_roles.table;
1095 	else
1096 		role_tab = state->dest_decl->p_roles.table;
1097 
1098 	dest_role = hashtab_search(role_tab, id);
1099 	assert(dest_role != NULL);
1100 
1101 	if (state->verbose) {
1102 		INFO(state->handle, "fixing role %s", id);
1103 	}
1104 
1105 	ebitmap_init(&e_tmp);
1106 	if (ebitmap_convert(&role->dominates, &e_tmp, mod->map[SYM_ROLES]))
1107 		goto cleanup;
1108 	if (ebitmap_union(&dest_role->dominates, &e_tmp)) {
1109 		goto cleanup;
1110 	}
1111 	if (type_set_or_convert(&role->types, &dest_role->types, mod)) {
1112 		goto cleanup;
1113 	}
1114 	ebitmap_destroy(&e_tmp);
1115 
1116 	if (role->flavor == ROLE_ATTRIB) {
1117 		ebitmap_init(&e_tmp);
1118 		if (ebitmap_convert(&role->roles, &e_tmp, mod->map[SYM_ROLES]))
1119 			goto cleanup;
1120 		if (ebitmap_union(&dest_role->roles, &e_tmp)) {
1121 			goto cleanup;
1122 		}
1123 		ebitmap_destroy(&e_tmp);
1124 	}
1125 
1126 	return 0;
1127 
1128       cleanup:
1129 	ERR(state->handle, "Out of memory!");
1130 	ebitmap_destroy(&e_tmp);
1131 	return -1;
1132 }
1133 
type_fix_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1134 static int type_fix_callback(hashtab_key_t key, hashtab_datum_t datum,
1135 			     void *data)
1136 {
1137 	char *id = key;
1138 	type_datum_t *type, *new_type = NULL;
1139 	link_state_t *state = (link_state_t *) data;
1140 	ebitmap_t e_tmp;
1141 	policy_module_t *mod = state->cur;
1142 	symtab_t *typetab;
1143 
1144 	type = (type_datum_t *) datum;
1145 
1146 	if (state->dest_decl == NULL)
1147 		typetab = &state->base->p_types;
1148 	else
1149 		typetab = &state->dest_decl->p_types;
1150 
1151 	/* only fix attributes */
1152 	if (type->flavor != TYPE_ATTRIB) {
1153 		return 0;
1154 	}
1155 
1156 	new_type = hashtab_search(typetab->table, id);
1157 	assert(new_type != NULL && new_type->flavor == TYPE_ATTRIB);
1158 
1159 	if (state->verbose) {
1160 		INFO(state->handle, "fixing attribute %s", id);
1161 	}
1162 
1163 	ebitmap_init(&e_tmp);
1164 	if (ebitmap_convert(&type->types, &e_tmp, mod->map[SYM_TYPES]))
1165 		goto cleanup;
1166 	if (ebitmap_union(&new_type->types, &e_tmp)) {
1167 		goto cleanup;
1168 	}
1169 	ebitmap_destroy(&e_tmp);
1170 	return 0;
1171 
1172       cleanup:
1173 	ERR(state->handle, "Out of memory!");
1174 	ebitmap_destroy(&e_tmp);
1175 	return -1;
1176 }
1177 
user_fix_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1178 static int user_fix_callback(hashtab_key_t key, hashtab_datum_t datum,
1179 			     void *data)
1180 {
1181 	char *id = key;
1182 	user_datum_t *user, *new_user = NULL;
1183 	link_state_t *state = (link_state_t *) data;
1184 	policy_module_t *mod = state->cur;
1185 	symtab_t *usertab;
1186 
1187 	user = (user_datum_t *) datum;
1188 
1189 	if (state->dest_decl == NULL)
1190 		usertab = &state->base->p_users;
1191 	else
1192 		usertab = &state->dest_decl->p_users;
1193 
1194 	new_user = hashtab_search(usertab->table, id);
1195 	assert(new_user != NULL);
1196 
1197 	if (state->verbose) {
1198 		INFO(state->handle, "fixing user %s", id);
1199 	}
1200 
1201 	if (role_set_or_convert(&user->roles, &new_user->roles, mod, state)) {
1202 		goto cleanup;
1203 	}
1204 
1205 	if (mls_range_convert(&user->range, &new_user->range, mod, state))
1206 		goto cleanup;
1207 
1208 	if (mls_level_convert(&user->dfltlevel, &new_user->dfltlevel, mod, state))
1209 		goto cleanup;
1210 
1211 	return 0;
1212 
1213       cleanup:
1214 	ERR(state->handle, "Out of memory!");
1215 	return -1;
1216 }
1217 
1218 static int (*fix_callback_f[SYM_NUM]) (hashtab_key_t key, hashtab_datum_t datum,
1219 				       void *datap) = {
1220 NULL, NULL, role_fix_callback, type_fix_callback, user_fix_callback,
1221 	    NULL, NULL, NULL};
1222 
1223 /*********** functions that copy AV rules ***********/
1224 
copy_avrule_list(avrule_t * list,avrule_t ** dst,policy_module_t * module,link_state_t * state)1225 static int copy_avrule_list(avrule_t * list, avrule_t ** dst,
1226 			    policy_module_t * module, link_state_t * state)
1227 {
1228 	unsigned int i;
1229 	avrule_t *cur, *new_rule = NULL, *tail;
1230 	class_perm_node_t *cur_perm, *new_perm, *tail_perm = NULL;
1231 
1232 	tail = *dst;
1233 	while (tail && tail->next) {
1234 		tail = tail->next;
1235 	}
1236 
1237 	cur = list;
1238 	while (cur) {
1239 		if ((new_rule = (avrule_t *) malloc(sizeof(avrule_t))) == NULL) {
1240 			goto cleanup;
1241 		}
1242 		avrule_init(new_rule);
1243 
1244 		new_rule->specified = cur->specified;
1245 		new_rule->flags = cur->flags;
1246 		if (type_set_convert
1247 		    (&cur->stypes, &new_rule->stypes, module) == -1
1248 		    || type_set_convert(&cur->ttypes, &new_rule->ttypes, module) == -1) {
1249 			goto cleanup;
1250 		}
1251 
1252 		cur_perm = cur->perms;
1253 		tail_perm = NULL;
1254 		while (cur_perm) {
1255 			if ((new_perm = (class_perm_node_t *)
1256 			     malloc(sizeof(class_perm_node_t))) == NULL) {
1257 				goto cleanup;
1258 			}
1259 			class_perm_node_init(new_perm);
1260 
1261 			new_perm->tclass =
1262 			    module->map[SYM_CLASSES][cur_perm->tclass - 1];
1263 			assert(new_perm->tclass);
1264 
1265 			if (new_rule->specified & AVRULE_AV) {
1266 				for (i = 0;
1267 				     i <
1268 				     module->perm_map_len[cur_perm->tclass - 1];
1269 				     i++) {
1270 					if (!(cur_perm->data & (UINT32_C(1) << i)))
1271 						continue;
1272 					new_perm->data |=
1273 					    (UINT32_C(1) <<
1274 					     (module->
1275 					      perm_map[cur_perm->tclass - 1][i] -
1276 					      1));
1277 				}
1278 			} else {
1279 				new_perm->data =
1280 				    module->map[SYM_TYPES][cur_perm->data - 1];
1281 			}
1282 
1283 			if (new_rule->perms == NULL) {
1284 				new_rule->perms = new_perm;
1285 			} else {
1286 				assert(tail_perm);
1287 				tail_perm->next = new_perm;
1288 			}
1289 			tail_perm = new_perm;
1290 			cur_perm = cur_perm->next;
1291 		}
1292 
1293 		if (cur->xperms) {
1294 			new_rule->xperms = calloc(1, sizeof(*new_rule->xperms));
1295 			if (!new_rule->xperms)
1296 				goto cleanup;
1297 			memcpy(new_rule->xperms, cur->xperms,
1298 			       sizeof(*new_rule->xperms));
1299 		}
1300 
1301 		new_rule->line = cur->line;
1302 		new_rule->source_line = cur->source_line;
1303 		if (cur->source_filename) {
1304 			new_rule->source_filename = strdup(cur->source_filename);
1305 			if (!new_rule->source_filename)
1306 				goto cleanup;
1307 		}
1308 
1309 		cur = cur->next;
1310 
1311 		if (*dst == NULL) {
1312 			*dst = new_rule;
1313 		} else {
1314 			tail->next = new_rule;
1315 		}
1316 		tail = new_rule;
1317 	}
1318 
1319 	return 0;
1320       cleanup:
1321 	ERR(state->handle, "Out of memory!");
1322 	avrule_destroy(new_rule);
1323 	free(new_rule);
1324 	return -1;
1325 }
1326 
copy_role_trans_list(role_trans_rule_t * list,role_trans_rule_t ** dst,policy_module_t * module,link_state_t * state)1327 static int copy_role_trans_list(role_trans_rule_t * list,
1328 				role_trans_rule_t ** dst,
1329 				policy_module_t * module, link_state_t * state)
1330 {
1331 	role_trans_rule_t *cur, *new_rule = NULL, *tail;
1332 
1333 	cur = list;
1334 	tail = *dst;
1335 	while (tail && tail->next) {
1336 		tail = tail->next;
1337 	}
1338 	while (cur) {
1339 		if ((new_rule =
1340 		     (role_trans_rule_t *) malloc(sizeof(role_trans_rule_t))) ==
1341 		    NULL) {
1342 			goto cleanup;
1343 		}
1344 		role_trans_rule_init(new_rule);
1345 
1346 		if (role_set_or_convert
1347 		    (&cur->roles, &new_rule->roles, module, state)
1348 		    || type_set_or_convert(&cur->types, &new_rule->types,
1349 					   module)) {
1350 			goto cleanup;
1351 		}
1352 
1353 		if (ebitmap_convert(&cur->classes, &new_rule->classes, module->map[SYM_CLASSES]))
1354 			goto cleanup;
1355 
1356 		new_rule->new_role = module->map[SYM_ROLES][cur->new_role - 1];
1357 
1358 		if (*dst == NULL) {
1359 			*dst = new_rule;
1360 		} else {
1361 			tail->next = new_rule;
1362 		}
1363 		tail = new_rule;
1364 		cur = cur->next;
1365 	}
1366 	return 0;
1367       cleanup:
1368 	ERR(state->handle, "Out of memory!");
1369 	role_trans_rule_list_destroy(new_rule);
1370 	return -1;
1371 }
1372 
copy_role_allow_list(role_allow_rule_t * list,role_allow_rule_t ** dst,policy_module_t * module,link_state_t * state)1373 static int copy_role_allow_list(role_allow_rule_t * list,
1374 				role_allow_rule_t ** dst,
1375 				policy_module_t * module, link_state_t * state)
1376 {
1377 	role_allow_rule_t *cur, *new_rule = NULL, *tail;
1378 
1379 	cur = list;
1380 	tail = *dst;
1381 	while (tail && tail->next) {
1382 		tail = tail->next;
1383 	}
1384 
1385 	while (cur) {
1386 		if ((new_rule =
1387 		     (role_allow_rule_t *) malloc(sizeof(role_allow_rule_t))) ==
1388 		    NULL) {
1389 			goto cleanup;
1390 		}
1391 		role_allow_rule_init(new_rule);
1392 
1393 		if (role_set_or_convert
1394 		    (&cur->roles, &new_rule->roles, module, state)
1395 		    || role_set_or_convert(&cur->new_roles,
1396 					   &new_rule->new_roles, module,
1397 					   state)) {
1398 			goto cleanup;
1399 		}
1400 		if (*dst == NULL) {
1401 			*dst = new_rule;
1402 		} else {
1403 			tail->next = new_rule;
1404 		}
1405 		tail = new_rule;
1406 		cur = cur->next;
1407 	}
1408 	return 0;
1409       cleanup:
1410 	ERR(state->handle, "Out of memory!");
1411 	role_allow_rule_list_destroy(new_rule);
1412 	return -1;
1413 }
1414 
copy_filename_trans_list(filename_trans_rule_t * list,filename_trans_rule_t ** dst,policy_module_t * module,link_state_t * state)1415 static int copy_filename_trans_list(filename_trans_rule_t * list,
1416 				    filename_trans_rule_t ** dst,
1417 				    policy_module_t * module,
1418 				    link_state_t * state)
1419 {
1420 	filename_trans_rule_t *cur, *new_rule, *tail;
1421 
1422 	cur = list;
1423 	tail = *dst;
1424 	while (tail && tail->next)
1425 		tail = tail->next;
1426 
1427 	while (cur) {
1428 		new_rule = malloc(sizeof(*new_rule));
1429 		if (!new_rule)
1430 			goto err;
1431 
1432 		filename_trans_rule_init(new_rule);
1433 
1434 		if (*dst == NULL)
1435 			*dst = new_rule;
1436 		else
1437 			tail->next = new_rule;
1438 		tail = new_rule;
1439 
1440 		new_rule->name = strdup(cur->name);
1441 		if (!new_rule->name)
1442 			goto err;
1443 
1444 		if (type_set_or_convert(&cur->stypes, &new_rule->stypes, module) ||
1445 		    type_set_or_convert(&cur->ttypes, &new_rule->ttypes, module))
1446 			goto err;
1447 
1448 		new_rule->tclass = module->map[SYM_CLASSES][cur->tclass - 1];
1449 		new_rule->otype = module->map[SYM_TYPES][cur->otype - 1];
1450 		new_rule->flags = cur->flags;
1451 
1452 		cur = cur->next;
1453 	}
1454 	return 0;
1455 err:
1456 	ERR(state->handle, "Out of memory!");
1457 	return -1;
1458 }
1459 
copy_range_trans_list(range_trans_rule_t * rules,range_trans_rule_t ** dst,policy_module_t * mod,link_state_t * state)1460 static int copy_range_trans_list(range_trans_rule_t * rules,
1461 				 range_trans_rule_t ** dst,
1462 				 policy_module_t * mod, link_state_t * state)
1463 {
1464 	range_trans_rule_t *rule, *new_rule = NULL;
1465 
1466 	for (rule = rules; rule; rule = rule->next) {
1467 		new_rule =
1468 		    (range_trans_rule_t *) malloc(sizeof(range_trans_rule_t));
1469 		if (!new_rule)
1470 			goto cleanup;
1471 
1472 		range_trans_rule_init(new_rule);
1473 
1474 		new_rule->next = *dst;
1475 		*dst = new_rule;
1476 
1477 		if (type_set_convert(&rule->stypes, &new_rule->stypes,
1478 				     mod))
1479 			goto cleanup;
1480 
1481 		if (type_set_convert(&rule->ttypes, &new_rule->ttypes,
1482 				     mod))
1483 			goto cleanup;
1484 
1485 		if (ebitmap_convert(&rule->tclasses, &new_rule->tclasses, mod->map[SYM_CLASSES]))
1486 			goto cleanup;
1487 
1488 		if (mls_range_convert(&rule->trange, &new_rule->trange, mod, state))
1489 			goto cleanup;
1490 	}
1491 	return 0;
1492 
1493       cleanup:
1494 	ERR(state->handle, "Out of memory!");
1495 	range_trans_rule_list_destroy(new_rule);
1496 	return -1;
1497 }
1498 
copy_cond_list(cond_node_t * list,cond_node_t ** dst,policy_module_t * module,link_state_t * state)1499 static int copy_cond_list(cond_node_t * list, cond_node_t ** dst,
1500 			  policy_module_t * module, link_state_t * state)
1501 {
1502 	unsigned i;
1503 	cond_node_t *cur, *new_node = NULL, *tail;
1504 	cond_expr_t *cur_expr;
1505 	tail = *dst;
1506 	while (tail && tail->next)
1507 		tail = tail->next;
1508 
1509 	cur = list;
1510 	while (cur) {
1511 		new_node = (cond_node_t *) malloc(sizeof(cond_node_t));
1512 		if (!new_node) {
1513 			goto cleanup;
1514 		}
1515 		memset(new_node, 0, sizeof(cond_node_t));
1516 
1517 		new_node->cur_state = cur->cur_state;
1518 		new_node->expr = cond_copy_expr(cur->expr);
1519 		if (!new_node->expr)
1520 			goto cleanup;
1521 		/* go back through and remap the expression */
1522 		for (cur_expr = new_node->expr; cur_expr != NULL;
1523 		     cur_expr = cur_expr->next) {
1524 			/* expression nodes don't have a bool value of 0 - don't map them */
1525 			if (cur_expr->expr_type != COND_BOOL)
1526 				continue;
1527 			assert(module->map[SYM_BOOLS][cur_expr->bool - 1] != 0);
1528 			cur_expr->bool =
1529 			    module->map[SYM_BOOLS][cur_expr->bool - 1];
1530 		}
1531 		new_node->nbools = cur->nbools;
1532 		/* FIXME should COND_MAX_BOOLS be used here? */
1533 		for (i = 0; i < min(cur->nbools, COND_MAX_BOOLS); i++) {
1534 			uint32_t remapped_id =
1535 			    module->map[SYM_BOOLS][cur->bool_ids[i] - 1];
1536 			assert(remapped_id != 0);
1537 			new_node->bool_ids[i] = remapped_id;
1538 		}
1539 		new_node->expr_pre_comp = cur->expr_pre_comp;
1540 
1541 		if (copy_avrule_list
1542 		    (cur->avtrue_list, &new_node->avtrue_list, module, state)
1543 		    || copy_avrule_list(cur->avfalse_list,
1544 					&new_node->avfalse_list, module,
1545 					state)) {
1546 			goto cleanup;
1547 		}
1548 
1549 		if (*dst == NULL) {
1550 			*dst = new_node;
1551 		} else {
1552 			tail->next = new_node;
1553 		}
1554 		tail = new_node;
1555 		cur = cur->next;
1556 	}
1557 	return 0;
1558       cleanup:
1559 	ERR(state->handle, "Out of memory!");
1560 	cond_node_destroy(new_node);
1561 	free(new_node);
1562 	return -1;
1563 
1564 }
1565 
1566 /*********** functions that copy avrule_decls from module to base ***********/
1567 
copy_identifiers(link_state_t * state,symtab_t * src_symtab,avrule_decl_t * dest_decl)1568 static int copy_identifiers(link_state_t * state, symtab_t * src_symtab,
1569 			    avrule_decl_t * dest_decl)
1570 {
1571 	int i, ret;
1572 
1573 	state->dest_decl = dest_decl;
1574 	for (i = 0; i < SYM_NUM; i++) {
1575 		if (copy_callback_f[i] != NULL) {
1576 			ret =
1577 			    hashtab_map(src_symtab[i].table, copy_callback_f[i],
1578 					state);
1579 			if (ret) {
1580 				return ret;
1581 			}
1582 		}
1583 	}
1584 
1585 	if (hashtab_map(src_symtab[SYM_TYPES].table,
1586 			type_bounds_copy_callback, state))
1587 		return -1;
1588 
1589 	if (hashtab_map(src_symtab[SYM_TYPES].table,
1590 			alias_copy_callback, state))
1591 		return -1;
1592 
1593 	if (hashtab_map(src_symtab[SYM_ROLES].table,
1594 			role_bounds_copy_callback, state))
1595 		return -1;
1596 
1597 	if (hashtab_map(src_symtab[SYM_USERS].table,
1598 			user_bounds_copy_callback, state))
1599 		return -1;
1600 
1601 	/* then fix bitmaps associated with those newly copied identifiers */
1602 	for (i = 0; i < SYM_NUM; i++) {
1603 		if (fix_callback_f[i] != NULL &&
1604 		    hashtab_map(src_symtab[i].table, fix_callback_f[i],
1605 				state)) {
1606 			return -1;
1607 		}
1608 	}
1609 	return 0;
1610 }
1611 
copy_scope_index(scope_index_t * src,scope_index_t * dest,policy_module_t * module,link_state_t * state)1612 static int copy_scope_index(scope_index_t * src, scope_index_t * dest,
1613 			    policy_module_t * module, link_state_t * state)
1614 {
1615 	unsigned int i, j;
1616 	uint32_t largest_mapped_class_value = 0;
1617 	ebitmap_node_t *node;
1618 	/* copy the scoping information for this avrule decl block */
1619 	for (i = 0; i < SYM_NUM; i++) {
1620 		ebitmap_t *srcmap = src->scope + i;
1621 		ebitmap_t *destmap = dest->scope + i;
1622 		if (copy_callback_f[i] == NULL) {
1623 			continue;
1624 		}
1625 		ebitmap_for_each_positive_bit(srcmap, node, j) {
1626 			assert(module->map[i][j] != 0);
1627 			if (ebitmap_set_bit
1628 			    (destmap, module->map[i][j] - 1, 1) != 0) {
1629 
1630 				goto cleanup;
1631 			}
1632 			if (i == SYM_CLASSES &&
1633 			    largest_mapped_class_value <
1634 			    module->map[SYM_CLASSES][j]) {
1635 				largest_mapped_class_value =
1636 				    module->map[SYM_CLASSES][j];
1637 			}
1638 		}
1639 	}
1640 
1641 	/* next copy the enabled permissions data  */
1642 	if ((dest->class_perms_map = calloc(largest_mapped_class_value,
1643 					    sizeof(*dest->class_perms_map))) == NULL) {
1644 		goto cleanup;
1645 	}
1646 	dest->class_perms_len = largest_mapped_class_value;
1647 	for (i = 0; i < src->class_perms_len; i++) {
1648 		const ebitmap_t *srcmap = src->class_perms_map + i;
1649 		ebitmap_t *destmap =
1650 		    dest->class_perms_map + module->map[SYM_CLASSES][i] - 1;
1651 
1652 		if (ebitmap_convert(srcmap, destmap, module->perm_map[i]))
1653 			goto cleanup;
1654 	}
1655 
1656 	return 0;
1657 
1658       cleanup:
1659 	ERR(state->handle, "Out of memory!");
1660 	return -1;
1661 }
1662 
copy_avrule_decl(link_state_t * state,policy_module_t * module,avrule_decl_t * src_decl,avrule_decl_t * dest_decl)1663 static int copy_avrule_decl(link_state_t * state, policy_module_t * module,
1664 			    avrule_decl_t * src_decl, avrule_decl_t * dest_decl)
1665 {
1666 	int ret;
1667 
1668 	/* copy all of the RBAC and TE rules */
1669 	if (copy_avrule_list
1670 	    (src_decl->avrules, &dest_decl->avrules, module, state) == -1
1671 	    || copy_role_trans_list(src_decl->role_tr_rules,
1672 				    &dest_decl->role_tr_rules, module,
1673 				    state) == -1
1674 	    || copy_role_allow_list(src_decl->role_allow_rules,
1675 				    &dest_decl->role_allow_rules, module,
1676 				    state) == -1
1677 	    || copy_cond_list(src_decl->cond_list, &dest_decl->cond_list,
1678 			      module, state) == -1) {
1679 		return -1;
1680 	}
1681 
1682 	if (copy_filename_trans_list(src_decl->filename_trans_rules,
1683 				     &dest_decl->filename_trans_rules,
1684 				     module, state))
1685 		return -1;
1686 
1687 	if (copy_range_trans_list(src_decl->range_tr_rules,
1688 				  &dest_decl->range_tr_rules, module, state))
1689 		return -1;
1690 
1691 	/* finally copy any identifiers local to this declaration */
1692 	ret = copy_identifiers(state, src_decl->symtab, dest_decl);
1693 	if (ret < 0) {
1694 		return ret;
1695 	}
1696 
1697 	/* then copy required and declared scope indices here */
1698 	if (copy_scope_index(&src_decl->required, &dest_decl->required,
1699 			     module, state) == -1 ||
1700 	    copy_scope_index(&src_decl->declared, &dest_decl->declared,
1701 			     module, state) == -1) {
1702 		return -1;
1703 	}
1704 
1705 	return 0;
1706 }
1707 
copy_avrule_block(link_state_t * state,policy_module_t * module,avrule_block_t * block)1708 static int copy_avrule_block(link_state_t * state, policy_module_t * module,
1709 			     avrule_block_t * block)
1710 {
1711 	avrule_block_t *new_block = avrule_block_create();
1712 	avrule_decl_t *decl, *last_decl = NULL;
1713 	int ret;
1714 
1715 	if (new_block == NULL) {
1716 		ERR(state->handle, "Out of memory!");
1717 		ret = -1;
1718 		goto cleanup;
1719 	}
1720 
1721 	new_block->flags = block->flags;
1722 
1723 	for (decl = block->branch_list; decl != NULL; decl = decl->next) {
1724 		avrule_decl_t *new_decl =
1725 		    avrule_decl_create(state->next_decl_id);
1726 		if (new_decl == NULL) {
1727 			ERR(state->handle, "Out of memory!");
1728 			ret = -1;
1729 			goto cleanup;
1730 		}
1731 
1732 		if (module->policy->name != NULL) {
1733 			new_decl->module_name = strdup(module->policy->name);
1734 			if (new_decl->module_name == NULL) {
1735 				ERR(state->handle, "Out of memory");
1736 				avrule_decl_destroy(new_decl);
1737 				ret = -1;
1738 				goto cleanup;
1739 			}
1740 		}
1741 
1742 		if (last_decl == NULL) {
1743 			new_block->branch_list = new_decl;
1744 		} else {
1745 			last_decl->next = new_decl;
1746 		}
1747 		last_decl = new_decl;
1748 		state->base->decl_val_to_struct[state->next_decl_id - 1] =
1749 		    new_decl;
1750 		state->decl_to_mod[state->next_decl_id] = module->policy;
1751 
1752 		module->avdecl_map[decl->decl_id] = new_decl->decl_id;
1753 
1754 		ret = copy_avrule_decl(state, module, decl, new_decl);
1755 		if (ret) {
1756 			avrule_decl_destroy(new_decl);
1757 			goto cleanup;
1758 		}
1759 
1760 		state->next_decl_id++;
1761 	}
1762 	state->last_avrule_block->next = new_block;
1763 	state->last_avrule_block = new_block;
1764 	return 0;
1765 
1766       cleanup:
1767 	avrule_block_list_destroy(new_block);
1768 	return ret;
1769 }
1770 
scope_copy_callback(hashtab_key_t key,hashtab_datum_t datum,void * data)1771 static int scope_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
1772 			       void *data)
1773 {
1774 	unsigned int i;
1775 	int ret;
1776 	char *id = key, *new_id = NULL;
1777 	scope_datum_t *scope, *base_scope;
1778 	link_state_t *state = (link_state_t *) data;
1779 	uint32_t symbol_num = state->symbol_num;
1780 	uint32_t *avdecl_map = state->cur->avdecl_map;
1781 
1782 	scope = (scope_datum_t *) datum;
1783 
1784 	/* check if the base already has a scope entry */
1785 	base_scope = hashtab_search(state->base->scope[symbol_num].table, id);
1786 	if (base_scope == NULL) {
1787 		scope_datum_t *new_scope;
1788 		if ((new_id = strdup(id)) == NULL) {
1789 			goto cleanup;
1790 		}
1791 
1792 		if ((new_scope =
1793 		     (scope_datum_t *) calloc(1, sizeof(*new_scope))) == NULL) {
1794 			free(new_id);
1795 			goto cleanup;
1796 		}
1797 		ret = hashtab_insert(state->base->scope[symbol_num].table,
1798 				     (hashtab_key_t) new_id,
1799 				     (hashtab_datum_t) new_scope);
1800 		if (ret) {
1801 			free(new_id);
1802 			free(new_scope);
1803 			goto cleanup;
1804 		}
1805 		new_scope->scope = SCOPE_REQ;	/* this is reset further down */
1806 		base_scope = new_scope;
1807 	}
1808 	if (base_scope->scope == SCOPE_REQ && scope->scope == SCOPE_DECL) {
1809 		/* this module declared symbol, so overwrite the old
1810 		 * list with the new decl ids */
1811 		base_scope->scope = SCOPE_DECL;
1812 		free(base_scope->decl_ids);
1813 		base_scope->decl_ids = NULL;
1814 		base_scope->decl_ids_len = 0;
1815 		for (i = 0; i < scope->decl_ids_len; i++) {
1816 			if (add_i_to_a(avdecl_map[scope->decl_ids[i]],
1817 				       &base_scope->decl_ids_len,
1818 				       &base_scope->decl_ids) == -1) {
1819 				goto cleanup;
1820 			}
1821 		}
1822 	} else if (base_scope->scope == SCOPE_DECL && scope->scope == SCOPE_REQ) {
1823 		/* this module depended on a symbol that now exists,
1824 		 * so don't do anything */
1825 	} else if (base_scope->scope == SCOPE_REQ && scope->scope == SCOPE_REQ) {
1826 		/* symbol is still required, so add to the list */
1827 		for (i = 0; i < scope->decl_ids_len; i++) {
1828 			if (add_i_to_a(avdecl_map[scope->decl_ids[i]],
1829 				       &base_scope->decl_ids_len,
1830 				       &base_scope->decl_ids) == -1) {
1831 				goto cleanup;
1832 			}
1833 		}
1834 	} else {
1835 		/* this module declared a symbol, and it was already
1836 		 * declared.  only roles and users may be multiply
1837 		 * declared; for all others this is an error. */
1838 		if (symbol_num != SYM_ROLES && symbol_num != SYM_USERS) {
1839 			ERR(state->handle,
1840 			    "%s: Duplicate declaration in module: %s %s",
1841 			    state->cur_mod_name,
1842 			    symtab_names[state->symbol_num], id);
1843 			return -1;
1844 		}
1845 		for (i = 0; i < scope->decl_ids_len; i++) {
1846 			if (add_i_to_a(avdecl_map[scope->decl_ids[i]],
1847 				       &base_scope->decl_ids_len,
1848 				       &base_scope->decl_ids) == -1) {
1849 				goto cleanup;
1850 			}
1851 		}
1852 	}
1853 	return 0;
1854 
1855       cleanup:
1856 	ERR(state->handle, "Out of memory!");
1857 	return -1;
1858 }
1859 
1860 /* Copy a module over to a base, remapping all values within.  After
1861  * all identifiers and rules are done, copy the scoping information.
1862  * This is when it checks for duplicate declarations. */
copy_module(link_state_t * state,policy_module_t * module)1863 static int copy_module(link_state_t * state, policy_module_t * module)
1864 {
1865 	int i, ret;
1866 	avrule_block_t *cur;
1867 	state->cur = module;
1868 	state->cur_mod_name = module->policy->name;
1869 
1870 	/* first copy all of the identifiers */
1871 	ret = copy_identifiers(state, module->policy->symtab, NULL);
1872 	if (ret) {
1873 		return ret;
1874 	}
1875 
1876 	/* next copy all of the avrule blocks */
1877 	for (cur = module->policy->global; cur != NULL; cur = cur->next) {
1878 		ret = copy_avrule_block(state, module, cur);
1879 		if (ret) {
1880 			return ret;
1881 		}
1882 	}
1883 
1884 	/* then copy the scoping tables */
1885 	for (i = 0; i < SYM_NUM; i++) {
1886 		state->symbol_num = i;
1887 		if (hashtab_map
1888 		    (module->policy->scope[i].table, scope_copy_callback,
1889 		     state)) {
1890 			return -1;
1891 		}
1892 	}
1893 
1894 	return 0;
1895 }
1896 
1897 /***** functions that check requirements and enable blocks in a module ******/
1898 
1899 /* borrowed from checkpolicy.c */
1900 
1901 struct find_perm_arg {
1902 	unsigned int valuep;
1903 	hashtab_key_t key;
1904 };
1905 
find_perm(hashtab_key_t key,hashtab_datum_t datum,void * varg)1906 static int find_perm(hashtab_key_t key, hashtab_datum_t datum, void *varg)
1907 {
1908 
1909 	struct find_perm_arg *arg = varg;
1910 
1911 	perm_datum_t *perdatum = (perm_datum_t *) datum;
1912 	if (arg->valuep == perdatum->s.value) {
1913 		arg->key = key;
1914 		return 1;
1915 	}
1916 
1917 	return 0;
1918 }
1919 
1920 /* Check if the requirements are met for a single declaration.  If all
1921  * are met return 1.  For the first requirement found to be missing,
1922  * if 'missing_sym_num' and 'missing_value' are both not NULL then
1923  * write to them the symbol number and value for the missing
1924  * declaration.  Then return 0 to indicate a missing declaration.
1925  * Note that if a declaration had no requirement at all (e.g., an ELSE
1926  * block) this returns 1. */
is_decl_requires_met(link_state_t * state,avrule_decl_t * decl,struct missing_requirement * req)1927 static int is_decl_requires_met(link_state_t * state,
1928 				avrule_decl_t * decl,
1929 				struct missing_requirement *req)
1930 {
1931 	/* (This algorithm is very unoptimized.  It performs many
1932 	 * redundant checks.  A very obvious improvement is to cache
1933 	 * which symbols have been verified, so that they do not need
1934 	 * to be re-checked.) */
1935 	unsigned int i, j;
1936 	ebitmap_t *bitmap;
1937 	char *id, *perm_id;
1938 	policydb_t *pol = state->base;
1939 	ebitmap_node_t *node;
1940 
1941 	/* check that all symbols have been satisfied */
1942 	for (i = 0; i < SYM_NUM; i++) {
1943 		if (i == SYM_CLASSES) {
1944 			/* classes will be checked during permissions
1945 			 * checking phase below */
1946 			continue;
1947 		}
1948 		bitmap = &decl->required.scope[i];
1949 		ebitmap_for_each_positive_bit(bitmap, node, j) {
1950 			/* check base's scope table */
1951 			id = pol->sym_val_to_name[i][j];
1952 			if (!is_id_enabled(id, state->base, i)) {
1953 				/* this symbol was not found */
1954 				if (req != NULL) {
1955 					req->symbol_type = i;
1956 					req->symbol_value = j + 1;
1957 				}
1958 				return 0;
1959 			}
1960 		}
1961 	}
1962 	/* check that all classes and permissions have been satisfied */
1963 	for (i = 0; i < decl->required.class_perms_len; i++) {
1964 
1965 		bitmap = decl->required.class_perms_map + i;
1966 		ebitmap_for_each_positive_bit(bitmap, node, j) {
1967 			struct find_perm_arg fparg;
1968 			class_datum_t *cladatum;
1969 			uint32_t perm_value = j + 1;
1970 			int rc;
1971 			scope_datum_t *scope;
1972 
1973 			id = pol->p_class_val_to_name[i];
1974 			cladatum = pol->class_val_to_struct[i];
1975 
1976 			scope =
1977 			    hashtab_search(state->base->p_classes_scope.table,
1978 					   id);
1979 			if (scope == NULL) {
1980 				ERR(state->handle,
1981 				    "Could not find scope information for class %s",
1982 				    id);
1983 				return -1;
1984 			}
1985 
1986 			fparg.valuep = perm_value;
1987 			fparg.key = NULL;
1988 
1989 			(void)hashtab_map(cladatum->permissions.table, find_perm,
1990 				    &fparg);
1991 			if (fparg.key == NULL && cladatum->comdatum != NULL) {
1992 				rc = hashtab_map(cladatum->comdatum->permissions.table,
1993 						 find_perm, &fparg);
1994 				assert(rc == 1);
1995 			}
1996 			perm_id = fparg.key;
1997 
1998 			assert(perm_id != NULL);
1999 			if (!is_perm_enabled(id, perm_id, state->base)) {
2000 				if (req != NULL) {
2001 					req->symbol_type = SYM_CLASSES;
2002 					req->symbol_value = i + 1;
2003 					req->perm_value = perm_value;
2004 				}
2005 				return 0;
2006 			}
2007 		}
2008 	}
2009 
2010 	/* all requirements have been met */
2011 	return 1;
2012 }
2013 
debug_requirements(link_state_t * state,policydb_t * p)2014 static int debug_requirements(link_state_t * state, policydb_t * p)
2015 {
2016 	int ret;
2017 	avrule_block_t *cur;
2018 	missing_requirement_t req;
2019 	memset(&req, 0, sizeof(req));
2020 
2021 	for (cur = p->global; cur != NULL; cur = cur->next) {
2022 		if (cur->enabled != NULL)
2023 			continue;
2024 
2025 		ret = is_decl_requires_met(state, cur->branch_list, &req);
2026 		if (ret < 0) {
2027 			return ret;
2028 		} else if (ret == 0) {
2029 			const char *mod_name = cur->branch_list->module_name ?
2030 			    cur->branch_list->module_name : "BASE";
2031 			if (req.symbol_type == SYM_CLASSES) {
2032 				struct find_perm_arg fparg;
2033 
2034 				class_datum_t *cladatum;
2035 				cladatum = p->class_val_to_struct[req.symbol_value - 1];
2036 
2037 				fparg.valuep = req.perm_value;
2038 				fparg.key = NULL;
2039 				(void)hashtab_map(cladatum->permissions.table,
2040 						  find_perm, &fparg);
2041 
2042 				if (cur->flags & AVRULE_OPTIONAL) {
2043 					ERR(state->handle,
2044 					    "%s[%d]'s optional requirements were not met: class %s, permission %s",
2045 					    mod_name, cur->branch_list->decl_id,
2046 					    p->p_class_val_to_name[req.symbol_value - 1],
2047 					    fparg.key);
2048 				} else {
2049 					ERR(state->handle,
2050 					    "%s[%d]'s global requirements were not met: class %s, permission %s",
2051 					    mod_name, cur->branch_list->decl_id,
2052 					    p->p_class_val_to_name[req.symbol_value - 1],
2053 					    fparg.key);
2054 				}
2055 			} else {
2056 				if (cur->flags & AVRULE_OPTIONAL) {
2057 					ERR(state->handle,
2058 					    "%s[%d]'s optional requirements were not met: %s %s",
2059 					    mod_name, cur->branch_list->decl_id,
2060 					    symtab_names[req.symbol_type],
2061 					    p->sym_val_to_name[req.
2062 							       symbol_type][req.
2063 									    symbol_value
2064 									    -
2065 									    1]);
2066 				} else {
2067 					ERR(state->handle,
2068 					    "%s[%d]'s global requirements were not met: %s %s",
2069 					    mod_name, cur->branch_list->decl_id,
2070 					    symtab_names[req.symbol_type],
2071 					    p->sym_val_to_name[req.
2072 							       symbol_type][req.
2073 									    symbol_value
2074 									    -
2075 									    1]);
2076 				}
2077 			}
2078 		}
2079 	}
2080 	return 0;
2081 }
2082 
print_missing_requirements(link_state_t * state,avrule_block_t * cur,missing_requirement_t * req)2083 static void print_missing_requirements(link_state_t * state,
2084 				       avrule_block_t * cur,
2085 				       missing_requirement_t * req)
2086 {
2087 	policydb_t *p = state->base;
2088 	const char *mod_name = cur->branch_list->module_name ?
2089 	    cur->branch_list->module_name : "BASE";
2090 
2091 	if (req->symbol_type == SYM_CLASSES) {
2092 
2093 		struct find_perm_arg fparg;
2094 
2095 		class_datum_t *cladatum;
2096 		cladatum = p->class_val_to_struct[req->symbol_value - 1];
2097 
2098 		fparg.valuep = req->perm_value;
2099 		fparg.key = NULL;
2100 		(void)hashtab_map(cladatum->permissions.table, find_perm, &fparg);
2101 
2102 		ERR(state->handle,
2103 		    "%s's global requirements were not met: class %s, permission %s",
2104 		    mod_name,
2105 		    p->p_class_val_to_name[req->symbol_value - 1], fparg.key);
2106 	} else {
2107 		ERR(state->handle,
2108 		    "%s's global requirements were not met: %s %s",
2109 		    mod_name,
2110 		    symtab_names[req->symbol_type],
2111 		    p->sym_val_to_name[req->symbol_type][req->symbol_value - 1]);
2112 	}
2113 }
2114 
2115 /* Enable all of the avrule_decl blocks for the policy. This simple
2116  * algorithm is the following:
2117  *
2118  * 1) Enable all of the non-else avrule_decls for all blocks.
2119  * 2) Iterate through the non-else decls looking for decls whose requirements
2120  *    are not met.
2121  *    2a) If the decl is non-optional, return immediately with an error.
2122  *    2b) If the decl is optional, disable the block and mark changed = 1
2123  * 3) If changed == 1 goto 2.
2124  * 4) Iterate through all blocks looking for those that have no enabled
2125  *    decl. If the block has an else decl, enable.
2126  *
2127  * This will correctly handle all dependencies, including mutual and
2128  * circular. The only downside is that it is slow.
2129  */
enable_avrules(link_state_t * state,policydb_t * pol)2130 static int enable_avrules(link_state_t * state, policydb_t * pol)
2131 {
2132 	int changed = 1;
2133 	avrule_block_t *block;
2134 	avrule_decl_t *decl;
2135 	missing_requirement_t req;
2136 	int ret = 0, rc;
2137 
2138 	if (state->verbose) {
2139 		INFO(state->handle, "Determining which avrules to enable.");
2140 	}
2141 
2142 	/* 1) enable all of the non-else blocks */
2143 	for (block = pol->global; block != NULL; block = block->next) {
2144 		block->enabled = block->branch_list;
2145 		block->enabled->enabled = 1;
2146 		for (decl = block->branch_list->next; decl != NULL;
2147 		     decl = decl->next)
2148 			decl->enabled = 0;
2149 	}
2150 
2151 	/* 2) Iterate */
2152 	while (changed) {
2153 		changed = 0;
2154 		for (block = pol->global; block != NULL; block = block->next) {
2155 			if (block->enabled == NULL) {
2156 				continue;
2157 			}
2158 			decl = block->branch_list;
2159 			if (state->verbose) {
2160 				const char *mod_name = decl->module_name ?
2161 				    decl->module_name : "BASE";
2162 				INFO(state->handle, "check module %s decl %d",
2163 				     mod_name, decl->decl_id);
2164 			}
2165 			rc = is_decl_requires_met(state, decl, &req);
2166 			if (rc < 0) {
2167 				ret = SEPOL_ERR;
2168 				goto out;
2169 			} else if (rc == 0) {
2170 				decl->enabled = 0;
2171 				block->enabled = NULL;
2172 				changed = 1;
2173 				if (!(block->flags & AVRULE_OPTIONAL)) {
2174 					print_missing_requirements(state, block,
2175 								   &req);
2176 					ret = SEPOL_EREQ;
2177 					goto out;
2178 				}
2179 			}
2180 		}
2181 	}
2182 
2183 	/* 4) else handling
2184 	 *
2185 	 * Iterate through all of the blocks skipping the first (which is the
2186 	 * global block, is required to be present, and cannot have an else).
2187 	 * If the block is disabled and has an else decl, enable that.
2188 	 *
2189 	 * This code assumes that the second block in the branch list is the else
2190 	 * block. This is currently supported by the compiler.
2191 	 */
2192 	for (block = pol->global->next; block != NULL; block = block->next) {
2193 		if (block->enabled == NULL) {
2194 			if (block->branch_list->next != NULL) {
2195 				block->enabled = block->branch_list->next;
2196 				block->branch_list->next->enabled = 1;
2197 			}
2198 		}
2199 	}
2200 
2201       out:
2202 	if (state->verbose)
2203 		debug_requirements(state, pol);
2204 
2205 	return ret;
2206 }
2207 
2208 /*********** the main linking functions ***********/
2209 
2210 /* Given a module's policy, normalize all conditional expressions
2211  * within.  Return 0 on success, -1 on error. */
cond_normalize(policydb_t * p)2212 static int cond_normalize(policydb_t * p)
2213 {
2214 	avrule_block_t *block;
2215 	for (block = p->global; block != NULL; block = block->next) {
2216 		avrule_decl_t *decl;
2217 		for (decl = block->branch_list; decl != NULL; decl = decl->next) {
2218 			cond_list_t *cond = decl->cond_list;
2219 			while (cond) {
2220 				if (cond_normalize_expr(p, cond) < 0)
2221 					return -1;
2222 				cond = cond->next;
2223 			}
2224 		}
2225 	}
2226 	return 0;
2227 }
2228 
2229 /* Allocate space for the various remapping arrays. */
prepare_module(link_state_t * state,policy_module_t * module)2230 static int prepare_module(link_state_t * state, policy_module_t * module)
2231 {
2232 	int i;
2233 	uint32_t items, num_decls = 0;
2234 	avrule_block_t *cur;
2235 
2236 	/* allocate the maps */
2237 	for (i = 0; i < SYM_NUM; i++) {
2238 		items = module->policy->symtab[i].nprim;
2239 		if ((module->map[i] =
2240 		     (uint32_t *) calloc(items,
2241 					 sizeof(*module->map[i]))) == NULL) {
2242 			ERR(state->handle, "Out of memory!");
2243 			return -1;
2244 		}
2245 	}
2246 
2247 	/* allocate the permissions remap here */
2248 	items = module->policy->p_classes.nprim;
2249 	if ((module->perm_map_len =
2250 	     calloc(items, sizeof(*module->perm_map_len))) == NULL) {
2251 		ERR(state->handle, "Out of memory!");
2252 		return -1;
2253 	}
2254 	if ((module->perm_map =
2255 	     calloc(items, sizeof(*module->perm_map))) == NULL) {
2256 		ERR(state->handle, "Out of memory!");
2257 		return -1;
2258 	}
2259 
2260 	/* allocate a map for avrule_decls */
2261 	for (cur = module->policy->global; cur != NULL; cur = cur->next) {
2262 		avrule_decl_t *decl;
2263 		for (decl = cur->branch_list; decl != NULL; decl = decl->next) {
2264 			if (decl->decl_id > num_decls) {
2265 				num_decls = decl->decl_id;
2266 			}
2267 		}
2268 	}
2269 	num_decls++;
2270 	if ((module->avdecl_map = calloc(num_decls, sizeof(uint32_t))) == NULL) {
2271 		ERR(state->handle, "Out of memory!");
2272 		return -1;
2273 	}
2274 	module->num_decls = num_decls;
2275 
2276 	/* normalize conditionals within */
2277 	if (cond_normalize(module->policy) < 0) {
2278 		ERR(state->handle,
2279 		    "Error while normalizing conditionals within the module %s.",
2280 		    module->policy->name);
2281 		return -1;
2282 	}
2283 	return 0;
2284 }
2285 
prepare_base(link_state_t * state,uint32_t num_mod_decls)2286 static int prepare_base(link_state_t * state, uint32_t num_mod_decls)
2287 {
2288 	avrule_block_t *cur = state->base->global;
2289 	assert(cur != NULL);
2290 	state->next_decl_id = 0;
2291 
2292 	/* iterate through all of the declarations in the base, to
2293 	   determine what the next decl_id should be */
2294 	while (cur != NULL) {
2295 		avrule_decl_t *decl;
2296 		for (decl = cur->branch_list; decl != NULL; decl = decl->next) {
2297 			if (decl->decl_id > state->next_decl_id) {
2298 				state->next_decl_id = decl->decl_id;
2299 			}
2300 		}
2301 		state->last_avrule_block = cur;
2302 		cur = cur->next;
2303 	}
2304 	state->last_base_avrule_block = state->last_avrule_block;
2305 	state->next_decl_id++;
2306 
2307 	/* allocate the table mapping from base's decl_id to its
2308 	 * avrule_decls and set the initial mappings */
2309 	free(state->base->decl_val_to_struct);
2310 	if ((state->base->decl_val_to_struct =
2311 	     calloc(state->next_decl_id + num_mod_decls,
2312 		    sizeof(*(state->base->decl_val_to_struct)))) == NULL) {
2313 		ERR(state->handle, "Out of memory!");
2314 		return -1;
2315 	}
2316 	/* This allocates the decl block to module mapping used for error reporting */
2317 	if ((state->decl_to_mod = calloc(state->next_decl_id + num_mod_decls,
2318 					 sizeof(*(state->decl_to_mod)))) ==
2319 	    NULL) {
2320 		ERR(state->handle, "Out of memory!");
2321 		return -1;
2322 	}
2323 	cur = state->base->global;
2324 	while (cur != NULL) {
2325 		avrule_decl_t *decl = cur->branch_list;
2326 		while (decl != NULL) {
2327 			state->base->decl_val_to_struct[decl->decl_id - 1] =
2328 			    decl;
2329 			state->decl_to_mod[decl->decl_id] = state->base;
2330 			decl = decl->next;
2331 		}
2332 		cur = cur->next;
2333 	}
2334 
2335 	/* normalize conditionals within */
2336 	if (cond_normalize(state->base) < 0) {
2337 		ERR(state->handle,
2338 		    "Error while normalizing conditionals within the base module.");
2339 		return -1;
2340 	}
2341 	return 0;
2342 }
2343 
expand_role_attributes(hashtab_key_t key,hashtab_datum_t datum,void * data)2344 static int expand_role_attributes(hashtab_key_t key, hashtab_datum_t datum,
2345 				  void * data)
2346 {
2347 	char *id;
2348 	role_datum_t *role, *sub_attr;
2349 	link_state_t *state;
2350 	unsigned int i;
2351 	ebitmap_node_t *rnode;
2352 
2353 	id = key;
2354 	role = (role_datum_t *)datum;
2355 	state = (link_state_t *)data;
2356 
2357 	if (strcmp(id, OBJECT_R) == 0){
2358 		/* object_r is never a role attribute by far */
2359 		return 0;
2360 	}
2361 
2362 	if (role->flavor != ROLE_ATTRIB)
2363 		return 0;
2364 
2365 	if (state->verbose)
2366 		INFO(state->handle, "expanding role attribute %s", id);
2367 
2368 restart:
2369 	ebitmap_for_each_positive_bit(&role->roles, rnode, i) {
2370 		sub_attr = state->base->role_val_to_struct[i];
2371 		if (sub_attr->flavor != ROLE_ATTRIB)
2372 			continue;
2373 
2374 		/* remove the sub role attribute from the parent
2375 		 * role attribute's roles ebitmap */
2376 		if (ebitmap_set_bit(&role->roles, i, 0))
2377 			return -1;
2378 
2379 		/* loop dependency of role attributes */
2380 		if (sub_attr->s.value == role->s.value)
2381 			continue;
2382 
2383 		/* now go on to expand a sub role attribute
2384 		 * by escalating its roles ebitmap */
2385 		if (ebitmap_union(&role->roles, &sub_attr->roles)) {
2386 			ERR(state->handle, "Out of memory!");
2387 			return -1;
2388 		}
2389 
2390 		/* sub_attr->roles may contain other role attributes,
2391 		 * re-scan the parent role attribute's roles ebitmap */
2392 		goto restart;
2393 	}
2394 
2395 	return 0;
2396 }
2397 
2398 /* For any role attribute in a declaration's local symtab[SYM_ROLES] table,
2399  * copy its roles ebitmap into its duplicate's in the base->p_roles.table.
2400  */
populate_decl_roleattributes(hashtab_key_t key,hashtab_datum_t datum,void * data)2401 static int populate_decl_roleattributes(hashtab_key_t key,
2402 					hashtab_datum_t datum,
2403 					void *data)
2404 {
2405 	char *id = key;
2406 	role_datum_t *decl_role, *base_role;
2407 	link_state_t *state = (link_state_t *)data;
2408 
2409 	decl_role = (role_datum_t *)datum;
2410 
2411 	if (strcmp(id, OBJECT_R) == 0) {
2412 		/* object_r is never a role attribute by far */
2413 		return 0;
2414 	}
2415 
2416 	if (decl_role->flavor != ROLE_ATTRIB)
2417 		return 0;
2418 
2419 	base_role = (role_datum_t *)hashtab_search(state->base->p_roles.table,
2420 						   id);
2421 	assert(base_role != NULL && base_role->flavor == ROLE_ATTRIB);
2422 
2423 	if (ebitmap_union(&base_role->roles, &decl_role->roles)) {
2424 		ERR(state->handle, "Out of memory!");
2425 		return -1;
2426 	}
2427 
2428 	return 0;
2429 }
2430 
populate_roleattributes(link_state_t * state,policydb_t * pol)2431 static int populate_roleattributes(link_state_t *state, policydb_t *pol)
2432 {
2433 	avrule_block_t *block;
2434 	avrule_decl_t *decl;
2435 
2436 	if (state->verbose)
2437 		INFO(state->handle, "Populating role-attribute relationship "
2438 			    "from enabled declarations' local symtab.");
2439 
2440 	/* Iterate through all of the blocks skipping the first(which is the
2441 	 * global block, is required to be present and can't have an else).
2442 	 * If the block is disabled or not having an enabled decl, skip it.
2443 	 */
2444 	for (block = pol->global->next; block != NULL; block = block->next)
2445 	{
2446 		decl = block->enabled;
2447 		if (decl == NULL || decl->enabled == 0)
2448 			continue;
2449 
2450 		if (hashtab_map(decl->symtab[SYM_ROLES].table,
2451 				populate_decl_roleattributes, state))
2452 			return -1;
2453 	}
2454 
2455 	return 0;
2456 }
2457 
2458 /* Link a set of modules into a base module. This process is somewhat
2459  * similar to an actual compiler: it requires a set of order dependent
2460  * steps.  The base and every module must have been indexed prior to
2461  * calling this function.
2462  */
link_modules(sepol_handle_t * handle,policydb_t * b,policydb_t ** mods,int len,int verbose)2463 int link_modules(sepol_handle_t * handle,
2464 		 policydb_t * b, policydb_t ** mods, int len, int verbose)
2465 {
2466 	int i, ret, retval = -1;
2467 	policy_module_t **modules = NULL;
2468 	link_state_t state;
2469 	uint32_t num_mod_decls = 0;
2470 
2471 	memset(&state, 0, sizeof(state));
2472 	state.base = b;
2473 	state.verbose = verbose;
2474 	state.handle = handle;
2475 
2476 	if (b->policy_type != POLICY_BASE) {
2477 		ERR(state.handle, "Target of link was not a base policy.");
2478 		return -1;
2479 	}
2480 
2481 	/* first allocate some space to hold the maps from module
2482 	 * symbol's value to the destination symbol value; then do
2483 	 * other preparation work */
2484 	if ((modules =
2485 	     (policy_module_t **) calloc(len, sizeof(*modules))) == NULL) {
2486 		ERR(state.handle, "Out of memory!");
2487 		return -1;
2488 	}
2489 	for (i = 0; i < len; i++) {
2490 		if (mods[i]->policy_type != POLICY_MOD) {
2491 			ERR(state.handle,
2492 			    "Tried to link in a policy that was not a module.");
2493 			goto cleanup;
2494 		}
2495 
2496 		if (mods[i]->mls != b->mls) {
2497 			if (b->mls)
2498 				ERR(state.handle,
2499 				    "Tried to link in a non-MLS module with an MLS base.");
2500 			else
2501 				ERR(state.handle,
2502 				    "Tried to link in an MLS module with a non-MLS base.");
2503 			goto cleanup;
2504 		}
2505 
2506 		if (mods[i]->policyvers > b->policyvers) {
2507 			WARN(state.handle,
2508 			     "Upgrading policy version from %u to %u", b->policyvers, mods[i]->policyvers);
2509 			b->policyvers = mods[i]->policyvers;
2510 		}
2511 
2512 		if ((modules[i] =
2513 		     (policy_module_t *) calloc(1,
2514 						sizeof(policy_module_t))) ==
2515 		    NULL) {
2516 			ERR(state.handle, "Out of memory!");
2517 			goto cleanup;
2518 		}
2519 		modules[i]->policy = mods[i];
2520 		if (prepare_module(&state, modules[i]) == -1) {
2521 			goto cleanup;
2522 		}
2523 		num_mod_decls += modules[i]->num_decls;
2524 	}
2525 	if (prepare_base(&state, num_mod_decls) == -1) {
2526 		goto cleanup;
2527 	}
2528 
2529 	/* copy and remap the module's data over to base */
2530 	for (i = 0; i < len; i++) {
2531 		state.cur = modules[i];
2532 		ret = copy_module(&state, modules[i]);
2533 		if (ret) {
2534 			retval = ret;
2535 			goto cleanup;
2536 		}
2537 	}
2538 
2539 	/* re-index base, for symbols were added to symbol tables  */
2540 	if (policydb_index_classes(state.base)) {
2541 		ERR(state.handle, "Error while indexing classes");
2542 		goto cleanup;
2543 	}
2544 	if (policydb_index_others(state.handle, state.base, 0)) {
2545 		ERR(state.handle, "Error while indexing others");
2546 		goto cleanup;
2547 	}
2548 
2549 	if (enable_avrules(&state, state.base)) {
2550 		retval = SEPOL_EREQ;
2551 		goto cleanup;
2552 	}
2553 
2554 	/* Now that all role attribute's roles ebitmap have been settled,
2555 	 * escalate sub role attribute's roles ebitmap into that of parent.
2556 	 *
2557 	 * First, since some role-attribute relationships could be recorded
2558 	 * in some decl's local symtab(see get_local_role()), we need to
2559 	 * populate them up to the base.p_roles table. */
2560 	if (populate_roleattributes(&state, state.base)) {
2561 		retval = SEPOL_EREQ;
2562 		goto cleanup;
2563 	}
2564 
2565 	/* Now do the escalation. */
2566 	if (hashtab_map(state.base->p_roles.table, expand_role_attributes,
2567 			&state))
2568 		goto cleanup;
2569 
2570 	retval = 0;
2571       cleanup:
2572 	for (i = 0; modules != NULL && i < len; i++) {
2573 		policy_module_destroy(modules[i]);
2574 	}
2575 	free(modules);
2576 	free(state.decl_to_mod);
2577 	return retval;
2578 }
2579