• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Author : Joshua Brindle <jbrindle@tresys.com>
2  *	    Karl MacMillan <kmacmillan@tresys.com>
3  *          Jason Tang     <jtang@tresys.com>
4  *	Added support for binary policy modules
5  *
6  * Copyright (C) 2004 - 2005 Tresys Technology, LLC
7  *	This program is free software; you can redistribute it and/or modify
8  *  	it under the terms of the GNU General Public License as published by
9  *	the Free Software Foundation, version 2.
10  */
11 
12 #include <assert.h>
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include <sepol/policydb/policydb.h>
18 #include <sepol/policydb/avrule_block.h>
19 #include <sepol/policydb/conditional.h>
20 
21 #include "queue.h"
22 #include "module_compiler.h"
23 
24 union stack_item_u {
25 	avrule_block_t *avrule;
26 	cond_list_t *cond_list;
27 };
28 
29 typedef struct scope_stack {
30 	union stack_item_u u;
31 	int type;		/* for above union: 1 = avrule block, 2 = conditional */
32 	avrule_decl_t *decl;	/* if in an avrule block, which
33 				 * declaration is current */
34 	avrule_t *last_avrule;
35 	int in_else;		/* if in an avrule block, within ELSE branch */
36 	int require_given;	/* 1 if this block had at least one require */
37 	struct scope_stack *parent, *child;
38 } scope_stack_t;
39 
40 extern policydb_t *policydbp;
41 extern queue_t id_queue;
42 extern int yyerror(const char *msg);
43 __attribute__ ((format(printf, 1, 2)))
44 extern void yyerror2(const char *fmt, ...);
45 
46 static int push_stack(int stack_type, ...);
47 static void pop_stack(void);
48 
49 /* keep track of the last item added to the stack */
50 static scope_stack_t *stack_top = NULL;
51 static avrule_block_t *last_block;
52 static uint32_t next_decl_id = 1;
53 
54 static const char * const flavor_str[SYM_NUM] = {
55 	[SYM_COMMONS] = "common",
56 	[SYM_CLASSES] = "class",
57 	[SYM_ROLES] = "role",
58 	[SYM_TYPES] = "type",
59 	[SYM_USERS] = "user",
60 	[SYM_BOOLS] = "bool",
61 	[SYM_LEVELS] = "level",
62 	[SYM_CATS] = "cat"
63 };
64 
print_error_msg(int ret,uint32_t symbol_type)65 static void print_error_msg(int ret, uint32_t symbol_type)
66 {
67 	switch (ret) {
68 	case -3:
69 		yyerror("Out of memory!");
70 		break;
71 	case -2:
72 		yyerror2("Duplicate declaration of %s", flavor_str[symbol_type]);
73 		break;
74 	case -1:
75 		yyerror2("Could not declare %s here", flavor_str[symbol_type]);
76 		break;
77 	default:
78 		yyerror("Unknown error");
79 	}
80 }
81 
define_policy(int pass,int module_header_given)82 int define_policy(int pass, int module_header_given)
83 {
84 	char *id;
85 
86 	if (module_header_given) {
87 		if (policydbp->policy_type != POLICY_MOD) {
88 			yyerror
89 			    ("Module specification found while not building a policy module.\n");
90 			return -1;
91 		}
92 
93 		if (pass == 2) {
94 			while ((id = queue_remove(id_queue)) != NULL)
95 				free(id);
96 		} else {
97 			id = (char *)queue_remove(id_queue);
98 			if (!id) {
99 				yyerror("no module name");
100 				return -1;
101 			}
102 			free(policydbp->name);
103 			policydbp->name = id;
104 			if ((policydbp->version =
105 			     queue_remove(id_queue)) == NULL) {
106 				yyerror
107 				    ("Expected a module version but none was found.");
108 				return -1;
109 			}
110 		}
111 	} else {
112 		if (policydbp->policy_type == POLICY_MOD) {
113 			yyerror
114 			    ("Building a policy module, but no module specification found.\n");
115 			return -1;
116 		}
117 	}
118 	/* the first declaration within the global avrule
119 	   block will always have an id of 1 */
120 	next_decl_id = 2;
121 
122 	/* reset the scoping stack */
123 	while (stack_top != NULL) {
124 		pop_stack();
125 	}
126 	if (push_stack(1, policydbp->global, policydbp->global->branch_list) ==
127 	    -1) {
128 		return -1;
129 	}
130 	last_block = policydbp->global;
131 	return 0;
132 }
133 
134 /* Given the current parse stack, returns 1 if a declaration or require would
135  * be allowed here or 0 if not.  For example, declarations and requirements are
136  * not allowed in conditionals, so if there are any conditionals in the
137  * current scope stack then this would return a 0.
138  */
is_creation_allowed(void)139 static int is_creation_allowed(void)
140 {
141 	if (stack_top->type != 1 || stack_top->in_else) {
142 		return 0;
143 	}
144 	return 1;
145 }
146 
147 /* Attempt to declare or require a symbol within the current scope.
148  * Returns:
149  *  0: Success - Symbol had not been previously created.
150  *  1: Success - Symbol had already been created and caller must free datum.
151  * -1: Failure - Symbol cannot be created here
152  * -2: Failure - Duplicate declaration or type/attribute mismatch
153  * -3: Failure - Out of memory or some other error
154  */
create_symbol(uint32_t symbol_type,hashtab_key_t key,hashtab_datum_t datum,uint32_t * dest_value,uint32_t scope)155 static int create_symbol(uint32_t symbol_type, hashtab_key_t key, hashtab_datum_t datum,
156 			 uint32_t * dest_value, uint32_t scope)
157 {
158 	avrule_decl_t *decl = stack_top->decl;
159 	int ret;
160 
161 	if (!is_creation_allowed()) {
162 		return -1;
163 	}
164 
165 	ret = symtab_insert(policydbp, symbol_type, key, datum, scope,
166 			    decl->decl_id, dest_value);
167 
168 	if (ret == 1 && dest_value) {
169 		hashtab_datum_t s =
170 			hashtab_search(policydbp->symtab[symbol_type].table,
171 				       key);
172 		assert(s != NULL);
173 
174 		if (symbol_type == SYM_LEVELS) {
175 			*dest_value = ((level_datum_t *)s)->level->sens;
176 		} else {
177 			*dest_value = ((symtab_datum_t *)s)->value;
178 		}
179 	} else if (ret == -2) {
180 		return -2;
181 	} else if (ret < 0) {
182 		return -3;
183 	}
184 
185 	return ret;
186 }
187 
188 /* Attempt to declare a symbol within the current declaration.  If
189  * currently within a non-conditional and in a non-else branch then
190  * insert the symbol, return 0 on success if symbol was undeclared.
191  * For roles and users, it is legal to have multiple declarations; as
192  * such return 1 to indicate that caller must free() the datum because
193  * it was not added.  If symbols may not be declared here return -1.
194  * For duplicate declarations return -2.  For all else, including out
195  * of memory, return -3.  Note that dest_value and datum_value might
196  * not be restricted pointers. */
declare_symbol(uint32_t symbol_type,hashtab_key_t key,hashtab_datum_t datum,uint32_t * dest_value,uint32_t * datum_value)197 int declare_symbol(uint32_t symbol_type,
198 		   hashtab_key_t key, hashtab_datum_t datum,
199 		   uint32_t * dest_value, uint32_t * datum_value)
200 {
201 	avrule_decl_t *decl = stack_top->decl;
202 	int ret = create_symbol(symbol_type, key, datum, dest_value, SCOPE_DECL);
203 
204 	if (ret < 0) {
205 		return ret;
206 	}
207 
208 	if (ebitmap_set_bit(decl->declared.scope + symbol_type,
209 			    *datum_value - 1, 1)) {
210 		return -3;
211 	}
212 
213 	return ret;
214 }
215 
role_implicit_bounds(hashtab_t roles_tab,char * role_id,role_datum_t * role)216 static int role_implicit_bounds(hashtab_t roles_tab,
217 				char *role_id, role_datum_t *role)
218 {
219 	role_datum_t *bounds;
220 	char *bounds_id, *delim;
221 
222 	delim = strrchr(role_id, '.');
223 	if (!delim)
224 		return 0;	/* no implicit boundary */
225 
226 	bounds_id = strdup(role_id);
227 	if (!bounds_id) {
228 		yyerror("out of memory");
229 		return -1;
230 	}
231 	bounds_id[(size_t)(delim - role_id)] = '\0';
232 
233 	bounds = hashtab_search(roles_tab, bounds_id);
234 	if (!bounds) {
235 		yyerror2("role %s doesn't exist, is implicit bounds of %s",
236 			 bounds_id, role_id);
237 		return -1;
238 	}
239 
240 	if (!role->bounds)
241 		role->bounds = bounds->s.value;
242 	else if (role->bounds != bounds->s.value) {
243 		yyerror2("role %s has inconsistent bounds %s/%s",
244 			 role_id, bounds_id,
245 			 policydbp->p_role_val_to_name[role->bounds - 1]);
246 		return -1;
247 	}
248 	free(bounds_id);
249 
250 	return 0;
251 }
252 
create_role(uint32_t scope,unsigned char isattr,role_datum_t ** role,char ** key)253 static int create_role(uint32_t scope, unsigned char isattr, role_datum_t **role, char **key)
254 {
255 	char *id = queue_remove(id_queue);
256 	role_datum_t *datum = NULL;
257 	int ret;
258 	uint32_t value;
259 
260 	*role = NULL;
261 	*key = NULL;
262 	isattr = isattr ? ROLE_ATTRIB : ROLE_ROLE;
263 
264 	if (id == NULL) {
265 		yyerror("no role name");
266 		return -1;
267 	}
268 
269 	datum = malloc(sizeof(*datum));
270 	if (datum == NULL) {
271 		yyerror("Out of memory!");
272 		free(id);
273 		return -1;
274 	}
275 
276 	role_datum_init(datum);
277 	datum->flavor = isattr;
278 
279 	if (scope == SCOPE_DECL) {
280 		ret = declare_symbol(SYM_ROLES, id, datum, &value, &value);
281 	} else {
282 		ret = require_symbol(SYM_ROLES, id, datum, &value, &value);
283 	}
284 
285 	datum->s.value = value;
286 
287 	if (ret == 0) {
288 		*role = datum;
289 		*key = strdup(id);
290 		if (*key == NULL) {
291 			yyerror("Out of memory!");
292 			return -1;
293 		}
294 	} else if (ret == 1) {
295 		*role = hashtab_search(policydbp->symtab[SYM_ROLES].table, id);
296 		if (*role && (isattr != (*role)->flavor)) {
297 			yyerror2("Identifier %s used as both an attribute and a role",
298 				 id);
299 			free(id);
300 			role_datum_destroy(datum);
301 			free(datum);
302 			return -1;
303 		}
304 		*role = datum;
305 		*key = id;
306 	} else {
307 		print_error_msg(ret, SYM_ROLES);
308 		free(id);
309 		role_datum_destroy(datum);
310 		free(datum);
311 	}
312 
313 	return ret;
314 }
315 
declare_role(unsigned char isattr)316 role_datum_t *declare_role(unsigned char isattr)
317 {
318 	char *key = NULL;
319 	role_datum_t *role = NULL;
320 	role_datum_t *dest_role = NULL;
321 	hashtab_t roles_tab;
322 	int ret, ret2;
323 
324 	ret = create_role(SCOPE_DECL, isattr, &role, &key);
325 	if (ret < 0) {
326 		return NULL;
327 	}
328 
329 	/* create a new role_datum_t for this decl, if necessary */
330 	assert(stack_top->type == 1);
331 
332 	if (stack_top->parent == NULL) {
333 		/* in parent, so use global symbol table */
334 		roles_tab = policydbp->p_roles.table;
335 	} else {
336 		roles_tab = stack_top->decl->p_roles.table;
337 	}
338 
339 	dest_role = hashtab_search(roles_tab, key);
340 	if (dest_role == NULL) {
341 		if (ret == 0) {
342 			dest_role = malloc(sizeof(*dest_role));
343 			if (dest_role == NULL) {
344 				yyerror("Out of memory!");
345 				free(key);
346 				return NULL;
347 			}
348 			role_datum_init(dest_role);
349 			dest_role->s.value = role->s.value;
350 			dest_role->flavor = role->flavor;
351 		} else {
352 			dest_role = role;
353 		}
354 		ret2 = role_implicit_bounds(roles_tab, key, dest_role);
355 		if (ret2 != 0) {
356 			free(key);
357 			role_datum_destroy(dest_role);
358 			free(dest_role);
359 			return NULL;
360 		}
361 		ret2 = hashtab_insert(roles_tab, key, dest_role);
362 		if (ret2 != 0) {
363 			yyerror("Out of memory!");
364 			free(key);
365 			role_datum_destroy(dest_role);
366 			free(dest_role);
367 			return NULL;
368 		}
369 	} else {
370 		free(key);
371 		if (ret == 1) {
372 			role_datum_destroy(role);
373 			free(role);
374 		}
375 	}
376 
377 	if (ret == 0) {
378 		ret2 = ebitmap_set_bit(&dest_role->dominates, dest_role->s.value - 1, 1);
379 		if (ret2 != 0) {
380 			yyerror("out of memory");
381 			return NULL;
382 		}
383 	}
384 
385 	return dest_role;
386 }
387 
create_type(uint32_t scope,unsigned char isattr,type_datum_t ** type)388 static int create_type(uint32_t scope, unsigned char isattr, type_datum_t **type)
389 {
390 	char *id;
391 	type_datum_t *datum;
392 	int ret;
393 	uint32_t value = 0;
394 
395 	*type = NULL;
396 	isattr = isattr ? TYPE_ATTRIB : TYPE_TYPE;
397 
398 	id = (char *)queue_remove(id_queue);
399 	if (!id) {
400 		yyerror("no type/attribute name?");
401 		return -1;
402 	}
403 	if (strcmp(id, "self") == 0) {
404 		yyerror("\"self\" is a reserved type name.");
405 		free(id);
406 		return -1;
407 	}
408 
409 	datum = malloc(sizeof(*datum));
410 	if (!datum) {
411 		yyerror("Out of memory!");
412 		free(id);
413 		return -1;
414 	}
415 	type_datum_init(datum);
416 	datum->primary = 1;
417 	datum->flavor = isattr;
418 
419 	if (scope == SCOPE_DECL) {
420 		ret = declare_symbol(SYM_TYPES, id, datum, &value, &value);
421 	} else {
422 		ret = require_symbol(SYM_TYPES, id, datum, &value, &value);
423 	}
424 
425 	if (ret == 0) {
426 		datum->s.value = value;
427 		*type = datum;
428 	} else if (ret == 1) {
429 		type_datum_destroy(datum);
430 		free(datum);
431 		*type = hashtab_search(policydbp->symtab[SYM_TYPES].table, id);
432 		if (*type && (isattr != (*type)->flavor)) {
433 			yyerror2("Identifier %s used as both an attribute and a type",
434 				 id);
435 			free(id);
436 			return -1;
437 		}
438 		free(id);
439 	} else {
440 		print_error_msg(ret, SYM_TYPES);
441 		free(id);
442 		type_datum_destroy(datum);
443 		free(datum);
444 	}
445 
446 	return ret;
447 }
448 
declare_type(unsigned char primary,unsigned char isattr)449 type_datum_t *declare_type(unsigned char primary, unsigned char isattr)
450 {
451 	type_datum_t *type = NULL;
452 	int ret = create_type(SCOPE_DECL, isattr, &type);
453 
454 	if (ret == 0) {
455 		type->primary = primary;
456 	}
457 
458 	return type;
459 }
460 
user_implicit_bounds(hashtab_t users_tab,char * user_id,user_datum_t * user)461 static int user_implicit_bounds(hashtab_t users_tab,
462 				char *user_id, user_datum_t *user)
463 {
464 	user_datum_t *bounds;
465 	char *bounds_id, *delim;
466 
467 	delim = strrchr(user_id, '.');
468 	if (!delim)
469 		return 0;	/* no implicit boundary */
470 
471 	bounds_id = strdup(user_id);
472 	if (!bounds_id) {
473 		yyerror("out of memory");
474 		return -1;
475 	}
476 	bounds_id[(size_t)(delim - user_id)] = '\0';
477 
478 	bounds = hashtab_search(users_tab, bounds_id);
479 	if (!bounds) {
480 		yyerror2("user %s doesn't exist, is implicit bounds of %s",
481 			 bounds_id, user_id);
482 		return -1;
483 	}
484 
485 	if (!user->bounds)
486 		user->bounds = bounds->s.value;
487 	else if (user->bounds != bounds->s.value) {
488 		yyerror2("user %s has inconsistent bounds %s/%s",
489 			 user_id, bounds_id,
490 			 policydbp->p_role_val_to_name[user->bounds - 1]);
491 		return -1;
492 	}
493 	free(bounds_id);
494 
495 	return 0;
496 }
497 
create_user(uint32_t scope,user_datum_t ** user,char ** key)498 static int create_user(uint32_t scope, user_datum_t **user, char **key)
499 {
500 	char *id = queue_remove(id_queue);
501 	user_datum_t *datum = NULL;
502 	int ret;
503 	uint32_t value;
504 
505 	*user = NULL;
506 	*key = NULL;
507 
508 	if (id == NULL) {
509 		yyerror("no user name");
510 		return -1;
511 	}
512 
513 	datum = malloc(sizeof(*datum));
514 	if (datum == NULL) {
515 		yyerror("Out of memory!");
516 		free(id);
517 		return -1;
518 	}
519 
520 	user_datum_init(datum);
521 
522 	if (scope == SCOPE_DECL) {
523 		ret = declare_symbol(SYM_USERS, id, datum, &value, &value);
524 	} else {
525 		ret = require_symbol(SYM_USERS, id, datum, &value, &value);
526 	}
527 
528 	datum->s.value = value;
529 
530 	if (ret == 0) {
531 		*user = datum;
532 		*key = strdup(id);
533 		if (*key == NULL) {
534 			yyerror("Out of memory!");
535 			return -1;
536 		}
537 	} else if (ret == 1) {
538 		*user = datum;
539 		*key = id;
540 	} else {
541 		print_error_msg(ret, SYM_USERS);
542 		free(id);
543 		user_datum_destroy(datum);
544 		free(datum);
545 	}
546 
547 	return ret;
548 }
549 
declare_user(void)550 user_datum_t *declare_user(void)
551 {
552 	char *key = NULL;
553 	user_datum_t *user = NULL;
554 	user_datum_t *dest_user = NULL;
555 	hashtab_t users_tab;
556 	int ret, ret2;
557 
558 	ret = create_user(SCOPE_DECL, &user, &key);
559 	if (ret < 0) {
560 		return NULL;
561 	}
562 
563 	/* create a new user_datum_t for this decl, if necessary */
564 	assert(stack_top->type == 1);
565 
566 	if (stack_top->parent == NULL) {
567 		/* in parent, so use global symbol table */
568 		users_tab = policydbp->p_users.table;
569 	} else {
570 		users_tab = stack_top->decl->p_users.table;
571 	}
572 
573 	dest_user = hashtab_search(users_tab, key);
574 	if (dest_user == NULL) {
575 		if (ret == 0) {
576 			dest_user = malloc(sizeof(*dest_user));
577 			if (dest_user == NULL) {
578 				yyerror("Out of memory!");
579 				free(key);
580 				return NULL;
581 			}
582 			user_datum_init(dest_user);
583 			dest_user->s.value = user->s.value;
584 		} else {
585 			dest_user = user;
586 		}
587 		ret2 = user_implicit_bounds(users_tab, key, dest_user);
588 		if (ret2 != 0) {
589 			free(key);
590 			user_datum_destroy(dest_user);
591 			free(dest_user);
592 			return NULL;
593 		}
594 		ret2 = hashtab_insert(users_tab, key, dest_user);
595 		if (ret2 != 0) {
596 			yyerror("Out of memory!");
597 			free(key);
598 			user_datum_destroy(dest_user);
599 			free(dest_user);
600 			return NULL;
601 		}
602 	} else {
603 		free(key);
604 		if (ret == 1) {
605 			user_datum_destroy(user);
606 			free(user);
607 		}
608 	}
609 
610 	return dest_user;
611 }
612 
613 /* Return a type_datum_t for the local avrule_decl with the given ID.
614  * If it does not exist, create one with the same value as 'value'.
615  * This function assumes that the ID is within scope.  c.f.,
616  * is_id_in_scope().
617  *
618  * NOTE: this function usurps ownership of id afterwards.  The caller
619  * shall not reference it nor free() it afterwards.
620  */
get_local_type(char * id,uint32_t value,unsigned char isattr)621 type_datum_t *get_local_type(char *id, uint32_t value, unsigned char isattr)
622 {
623 	type_datum_t *dest_typdatum;
624 	hashtab_t types_tab;
625 	assert(stack_top->type == 1);
626 	if (stack_top->parent == NULL) {
627 		/* in global, so use global symbol table */
628 		types_tab = policydbp->p_types.table;
629 	} else {
630 		types_tab = stack_top->decl->p_types.table;
631 	}
632 	dest_typdatum = hashtab_search(types_tab, id);
633 	if (!dest_typdatum) {
634 		dest_typdatum = (type_datum_t *) malloc(sizeof(type_datum_t));
635 		if (dest_typdatum == NULL) {
636 			free(id);
637 			return NULL;
638 		}
639 		type_datum_init(dest_typdatum);
640 		dest_typdatum->s.value = value;
641 		dest_typdatum->flavor = isattr ? TYPE_ATTRIB : TYPE_TYPE;
642 		dest_typdatum->primary = 1;
643 		if (hashtab_insert(types_tab, id, dest_typdatum)) {
644 			free(id);
645 			type_datum_destroy(dest_typdatum);
646 			free(dest_typdatum);
647 			return NULL;
648 		}
649 
650 	} else {
651 		free(id);
652 		if (dest_typdatum->flavor != isattr ? TYPE_ATTRIB : TYPE_TYPE) {
653 			return NULL;
654 		}
655 	}
656 	return dest_typdatum;
657 }
658 
659 /* Return a role_datum_t for the local avrule_decl with the given ID.
660  * If it does not exist, create one with the same value as 'value'.
661  * This function assumes that the ID is within scope.  c.f.,
662  * is_id_in_scope().
663  *
664  * NOTE: this function usurps ownership of id afterwards.  The caller
665  * shall not reference it nor free() it afterwards.
666  */
get_local_role(char * id,uint32_t value,unsigned char isattr)667 role_datum_t *get_local_role(char *id, uint32_t value, unsigned char isattr)
668 {
669 	role_datum_t *dest_roledatum;
670 	hashtab_t roles_tab;
671 
672 	assert(stack_top->type == 1);
673 
674 	if (stack_top->parent == NULL) {
675 		/* in global, so use global symbol table */
676 		roles_tab = policydbp->p_roles.table;
677 	} else {
678 		roles_tab = stack_top->decl->p_roles.table;
679 	}
680 
681 	dest_roledatum = hashtab_search(roles_tab, id);
682 	if (!dest_roledatum) {
683 		dest_roledatum = (role_datum_t *)malloc(sizeof(role_datum_t));
684 		if (dest_roledatum == NULL) {
685 			free(id);
686 			return NULL;
687 		}
688 
689 		role_datum_init(dest_roledatum);
690 		dest_roledatum->s.value = value;
691 		dest_roledatum->flavor = isattr ? ROLE_ATTRIB : ROLE_ROLE;
692 
693 		if (hashtab_insert(roles_tab, id, dest_roledatum)) {
694 			free(id);
695 			role_datum_destroy(dest_roledatum);
696 			free(dest_roledatum);
697 			return NULL;
698 		}
699 	} else {
700 		free(id);
701 		if (dest_roledatum->flavor != isattr ? ROLE_ATTRIB : ROLE_ROLE)
702 			return NULL;
703 	}
704 
705 	return dest_roledatum;
706 }
707 
708 /* Attempt to require a symbol within the current scope.  If currently
709  * within an optional (and not its else branch), add the symbol to the
710  * required list.  Return 0 on success, 1 if caller needs to free()
711  * datum.  If symbols may not be declared here return -1.  For duplicate
712  * declarations return -2.  For all else, including out of memory,
713  * return -3..  Note that dest_value and datum_value might not be
714  * restricted pointers.
715  */
require_symbol(uint32_t symbol_type,hashtab_key_t key,hashtab_datum_t datum,uint32_t * dest_value,uint32_t * datum_value)716 int require_symbol(uint32_t symbol_type,
717 		   hashtab_key_t key, hashtab_datum_t datum,
718 		   uint32_t * dest_value, uint32_t * datum_value)
719 {
720 	avrule_decl_t *decl = stack_top->decl;
721 	int ret = create_symbol(symbol_type, key, datum, dest_value, SCOPE_REQ);
722 
723 	if (ret < 0) {
724 		return ret;
725 	}
726 
727 	if (ebitmap_set_bit(decl->required.scope + symbol_type,
728 			    *datum_value - 1, 1)) {
729 		return -3;
730 	}
731 
732 	stack_top->require_given = 1;
733 	return ret;
734 }
735 
add_perm_to_class(uint32_t perm_value,uint32_t class_value)736 int add_perm_to_class(uint32_t perm_value, uint32_t class_value)
737 {
738 	avrule_decl_t *decl = stack_top->decl;
739 	scope_index_t *scope;
740 
741 	assert(perm_value >= 1);
742 	assert(class_value >= 1);
743 	scope = &decl->required;
744 	if (class_value > scope->class_perms_len) {
745 		uint32_t i;
746 		ebitmap_t *new_map = realloc(scope->class_perms_map,
747 					     class_value * sizeof(*new_map));
748 		if (new_map == NULL) {
749 			return -1;
750 		}
751 		scope->class_perms_map = new_map;
752 		for (i = scope->class_perms_len; i < class_value; i++) {
753 			ebitmap_init(scope->class_perms_map + i);
754 		}
755 		scope->class_perms_len = class_value;
756 	}
757 	if (ebitmap_set_bit(scope->class_perms_map + class_value - 1,
758 			    perm_value - 1, 1)) {
759 		return -1;
760 	}
761 	return 0;
762 }
763 
perm_destroy(hashtab_key_t key,hashtab_datum_t datum,void * p)764 static int perm_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
765 			__attribute__ ((unused)))
766 {
767 	if (key)
768 		free(key);
769 	free(datum);
770 	return 0;
771 }
772 
class_datum_destroy(class_datum_t * cladatum)773 static void class_datum_destroy(class_datum_t * cladatum)
774 {
775 	if (cladatum != NULL) {
776 		hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
777 		hashtab_destroy(cladatum->permissions.table);
778 		free(cladatum);
779 	}
780 }
781 
require_class(int pass)782 int require_class(int pass)
783 {
784 	char *class_id = queue_remove(id_queue);
785 	char *perm_id = NULL;
786 	class_datum_t *datum = NULL;
787 	perm_datum_t *perm = NULL;
788 	int ret;
789 
790 	if (pass == 2) {
791 		free(class_id);
792 		while ((perm_id = queue_remove(id_queue)) != NULL)
793 			free(perm_id);
794 		return 0;
795 	}
796 
797 	/* first add the class if it is not already there */
798 	if (class_id == NULL) {
799 		yyerror("no class name for class definition?");
800 		return -1;
801 	}
802 
803 	if ((datum = calloc(1, sizeof(*datum))) == NULL ||
804 	    symtab_init(&datum->permissions, PERM_SYMTAB_SIZE)) {
805 		yyerror("Out of memory!");
806 		class_datum_destroy(datum);
807 		return -1;
808 	}
809 	ret =
810 	    require_symbol(SYM_CLASSES, class_id, datum, &datum->s.value,
811 			   &datum->s.value);
812 	if (ret < 0) {
813 		print_error_msg(ret, SYM_CLASSES);
814 		free(class_id);
815 		class_datum_destroy(datum);
816 		return -1;
817 	}
818 
819 	if (ret == 0) {
820 		/* a new class was added; reindex everything */
821 		if (policydb_index_classes(policydbp)) {
822 			yyerror("Out of memory!");
823 			return -1;
824 		}
825 	} else {
826 		class_datum_destroy(datum);
827 		datum = hashtab_search(policydbp->p_classes.table, class_id);
828 		assert(datum);	/* the class datum should have existed */
829 		free(class_id);
830 	}
831 
832 	/* now add each of the permissions to this class's requirements */
833 	while ((perm_id = queue_remove(id_queue)) != NULL) {
834 		int allocated = 0;
835 
836 		/* Is the permission already in the table? */
837 		perm = hashtab_search(datum->permissions.table, perm_id);
838 		if (!perm && datum->comdatum)
839 			perm =
840 			    hashtab_search(datum->comdatum->permissions.table,
841 					   perm_id);
842 		if (perm) {
843 			/* Yes, drop the name. */
844 			free(perm_id);
845 		} else {
846 			/* No - allocate and insert an entry for it. */
847 			if (policydbp->policy_type == POLICY_BASE) {
848 				yyerror2
849 				    ("Base policy - require of permission %s without prior declaration.",
850 				     perm_id);
851 				free(perm_id);
852 				return -1;
853 			}
854 			allocated = 1;
855 			if ((perm = malloc(sizeof(*perm))) == NULL) {
856 				yyerror("Out of memory!");
857 				free(perm_id);
858 				return -1;
859 			}
860 			memset(perm, 0, sizeof(*perm));
861 			ret =
862 			    hashtab_insert(datum->permissions.table, perm_id,
863 					   perm);
864 			if (ret) {
865 				yyerror("Out of memory!");
866 				free(perm_id);
867 				free(perm);
868 				return -1;
869 			}
870 			perm->s.value = datum->permissions.nprim + 1;
871 		}
872 
873 		if (add_perm_to_class(perm->s.value, datum->s.value) == -1) {
874 			yyerror("Out of memory!");
875 			return -1;
876 		}
877 
878 		/* Update number of primitives if we allocated one. */
879 		if (allocated)
880 			datum->permissions.nprim++;
881 	}
882 	return 0;
883 }
884 
require_role_or_attribute(int pass,unsigned char isattr)885 static int require_role_or_attribute(int pass, unsigned char isattr)
886 {
887 	char *key = NULL;
888 	role_datum_t *role = NULL;
889 	int ret;
890 
891 	if (pass == 2) {
892 		free(queue_remove(id_queue));
893 		return 0;
894 	}
895 
896 	ret = create_role(SCOPE_REQ, isattr, &role, &key);
897 	if (ret < 0) {
898 		return -1;
899 	}
900 
901 	free(key);
902 
903 	if (ret == 0) {
904 		ret = ebitmap_set_bit(&role->dominates, role->s.value - 1, 1);
905 		if (ret != 0) {
906 			yyerror("Out of memory");
907 			return -1;
908 		}
909 	} else {
910 		role_datum_destroy(role);
911 		free(role);
912 	}
913 
914 	return 0;
915 }
916 
require_role(int pass)917 int require_role(int pass)
918 {
919 	return require_role_or_attribute(pass, 0);
920 }
921 
require_attribute_role(int pass)922 int require_attribute_role(int pass)
923 {
924 	return require_role_or_attribute(pass, 1);
925 }
926 
require_type_or_attribute(int pass,unsigned char isattr)927 static int require_type_or_attribute(int pass, unsigned char isattr)
928 {
929 	type_datum_t *type = NULL;
930 	int ret;
931 
932 	if (pass == 2) {
933 		free(queue_remove(id_queue));
934 		return 0;
935 	}
936 
937 	ret = create_type(SCOPE_REQ, isattr, &type);
938 
939 	if (ret < 0) {
940 		return -1;
941 	}
942 
943 	return 0;
944 }
945 
require_type(int pass)946 int require_type(int pass)
947 {
948 	return require_type_or_attribute(pass, 0);
949 }
950 
require_attribute(int pass)951 int require_attribute(int pass)
952 {
953 	return require_type_or_attribute(pass, 1);
954 }
955 
require_user(int pass)956 int require_user(int pass)
957 {
958 	char *key = NULL;
959 	user_datum_t *user = NULL;
960 	int ret;
961 
962 	if (pass == 1) {
963 		free(queue_remove(id_queue));
964 		return 0;
965 	}
966 
967 	ret = create_user(SCOPE_REQ, &user, &key);
968 	if (ret < 0) {
969 		return -1;
970 	}
971 
972 	free(key);
973 
974 	if (ret == 1) {
975 		user_datum_destroy(user);
976 		free(user);
977 	}
978 
979 	return 0;
980 }
981 
require_bool_tunable(int pass,int is_tunable)982 static int require_bool_tunable(int pass, int is_tunable)
983 {
984 	char *id = queue_remove(id_queue);
985 	cond_bool_datum_t *booldatum = NULL;
986 	int retval;
987 	if (pass == 2) {
988 		free(id);
989 		return 0;
990 	}
991 	if (id == NULL) {
992 		yyerror("no boolean name");
993 		return -1;
994 	}
995 	if ((booldatum = calloc(1, sizeof(*booldatum))) == NULL) {
996 		cond_destroy_bool(id, booldatum, NULL);
997 		yyerror("Out of memory!");
998 		return -1;
999 	}
1000 	if (is_tunable)
1001 		booldatum->flags |= COND_BOOL_FLAGS_TUNABLE;
1002 	retval =
1003 	    require_symbol(SYM_BOOLS, id, booldatum,
1004 			   &booldatum->s.value, &booldatum->s.value);
1005 	if (retval != 0) {
1006 		cond_destroy_bool(id, booldatum, NULL);
1007 		if (retval < 0) {
1008 			print_error_msg(retval, SYM_BOOLS);
1009 			return -1;
1010 		}
1011 	}
1012 
1013 	return 0;
1014 }
1015 
require_bool(int pass)1016 int require_bool(int pass)
1017 {
1018 	return require_bool_tunable(pass, 0);
1019 }
1020 
require_tunable(int pass)1021 int require_tunable(int pass)
1022 {
1023 	return require_bool_tunable(pass, 1);
1024 }
1025 
require_sens(int pass)1026 int require_sens(int pass)
1027 {
1028 	char *id = queue_remove(id_queue);
1029 	level_datum_t *level = NULL;
1030 	int retval;
1031 	if (pass == 2) {
1032 		free(id);
1033 		return 0;
1034 	}
1035 	if (!id) {
1036 		yyerror("no sensitivity name");
1037 		return -1;
1038 	}
1039 	level = malloc(sizeof(level_datum_t));
1040 	if (!level) {
1041 		free(id);
1042 		yyerror("Out of memory!");
1043 		return -1;
1044 	}
1045 	level_datum_init(level);
1046 	level->level = malloc(sizeof(mls_level_t));
1047 	if (!level->level) {
1048 		free(id);
1049 		level_datum_destroy(level);
1050 		free(level);
1051 		yyerror("Out of memory!");
1052 		return -1;
1053 	}
1054 	mls_level_init(level->level);
1055 	retval = require_symbol(SYM_LEVELS, id, level,
1056 				&level->level->sens, &level->level->sens);
1057 	if (retval != 0) {
1058 		free(id);
1059 		mls_level_destroy(level->level);
1060 		free(level->level);
1061 		level_datum_destroy(level);
1062 		free(level);
1063 		if (retval < 0) {
1064 			print_error_msg(retval, SYM_LEVELS);
1065 			return -1;
1066 		}
1067 	}
1068 
1069 	return 0;
1070 }
1071 
require_cat(int pass)1072 int require_cat(int pass)
1073 {
1074 	char *id = queue_remove(id_queue);
1075 	cat_datum_t *cat = NULL;
1076 	int retval;
1077 	if (pass == 2) {
1078 		free(id);
1079 		return 0;
1080 	}
1081 	if (!id) {
1082 		yyerror("no category name");
1083 		return -1;
1084 	}
1085 	cat = malloc(sizeof(cat_datum_t));
1086 	if (!cat) {
1087 		free(id);
1088 		yyerror("Out of memory!");
1089 		return -1;
1090 	}
1091 	cat_datum_init(cat);
1092 
1093 	retval = require_symbol(SYM_CATS, id, cat,
1094 				&cat->s.value, &cat->s.value);
1095 	if (retval != 0) {
1096 		free(id);
1097 		cat_datum_destroy(cat);
1098 		free(cat);
1099 		if (retval < 0) {
1100 			print_error_msg(retval, SYM_CATS);
1101 			return -1;
1102 		}
1103 	}
1104 
1105 	return 0;
1106 }
1107 
is_scope_in_stack(const scope_datum_t * scope,const scope_stack_t * stack)1108 static int is_scope_in_stack(const scope_datum_t * scope, const scope_stack_t * stack)
1109 {
1110 	uint32_t i;
1111 	if (stack == NULL) {
1112 		return 0;	/* no matching scope found */
1113 	}
1114 	if (stack->type == 1) {
1115 		const avrule_decl_t *decl = stack->decl;
1116 		for (i = 0; i < scope->decl_ids_len; i++) {
1117 			if (scope->decl_ids[i] == decl->decl_id) {
1118 				return 1;
1119 			}
1120 		}
1121 	} else {
1122 		/* note that conditionals can't declare or require
1123 		 * symbols, so skip this level */
1124 	}
1125 
1126 	/* not within scope of this stack, so try its parent */
1127 	return is_scope_in_stack(scope, stack->parent);
1128 }
1129 
is_id_in_scope(uint32_t symbol_type,const_hashtab_key_t id)1130 int is_id_in_scope(uint32_t symbol_type, const_hashtab_key_t id)
1131 {
1132 	const scope_datum_t *scope =
1133 	    (scope_datum_t *) hashtab_search(policydbp->scope[symbol_type].
1134 					     table, id);
1135 	if (scope == NULL) {
1136 		return 1;	/* id is not known, so return success */
1137 	}
1138 	return is_scope_in_stack(scope, stack_top);
1139 }
1140 
is_perm_in_scope_index(uint32_t perm_value,uint32_t class_value,const scope_index_t * scope)1141 static int is_perm_in_scope_index(uint32_t perm_value, uint32_t class_value,
1142 				  const scope_index_t * scope)
1143 {
1144 	if (class_value > scope->class_perms_len) {
1145 		return 1;
1146 	}
1147 	if (ebitmap_get_bit(scope->class_perms_map + class_value - 1,
1148 			    perm_value - 1)) {
1149 		return 1;
1150 	}
1151 	return 0;
1152 }
1153 
is_perm_in_stack(uint32_t perm_value,uint32_t class_value,const scope_stack_t * stack)1154 static int is_perm_in_stack(uint32_t perm_value, uint32_t class_value,
1155 			    const scope_stack_t * stack)
1156 {
1157 	if (stack == NULL) {
1158 		return 0;	/* no matching scope found */
1159 	}
1160 	if (stack->type == 1) {
1161 		avrule_decl_t *decl = stack->decl;
1162 		if (is_perm_in_scope_index
1163 		    (perm_value, class_value, &decl->required)
1164 		    || is_perm_in_scope_index(perm_value, class_value,
1165 					      &decl->declared)) {
1166 			return 1;
1167 		}
1168 	} else {
1169 		/* note that conditionals can't declare or require
1170 		 * symbols, so skip this level */
1171 	}
1172 
1173 	/* not within scope of this stack, so try its parent */
1174 	return is_perm_in_stack(perm_value, class_value, stack->parent);
1175 }
1176 
is_perm_in_scope(const_hashtab_key_t perm_id,const_hashtab_key_t class_id)1177 int is_perm_in_scope(const_hashtab_key_t perm_id, const_hashtab_key_t class_id)
1178 {
1179 	const class_datum_t *cladatum =
1180 	    (class_datum_t *) hashtab_search(policydbp->p_classes.table,
1181 					     class_id);
1182 	const perm_datum_t *perdatum;
1183 	if (cladatum == NULL) {
1184 		return 1;
1185 	}
1186 	perdatum = (perm_datum_t *) hashtab_search(cladatum->permissions.table,
1187 						   perm_id);
1188 	if (perdatum == NULL) {
1189 		return 1;
1190 	}
1191 	return is_perm_in_stack(perdatum->s.value, cladatum->s.value,
1192 				stack_top);
1193 }
1194 
get_current_cond_list(cond_list_t * cond)1195 cond_list_t *get_current_cond_list(cond_list_t * cond)
1196 {
1197 	/* FIX ME: do something different here if in a nested
1198 	 * conditional? */
1199 	avrule_decl_t *decl = stack_top->decl;
1200 	return get_decl_cond_list(policydbp, decl, cond);
1201 }
1202 
1203 /* Append the new conditional node to the existing ones.  During
1204  * expansion the list will be reversed -- i.e., the last AV rule will
1205  * be the first one listed in the policy.  This matches the behavior
1206  * of the upstream compiler. */
append_cond_list(cond_list_t * cond)1207 void append_cond_list(cond_list_t * cond)
1208 {
1209 	cond_list_t *old_cond = get_current_cond_list(cond);
1210 	avrule_t *tmp;
1211 	assert(old_cond != NULL);	/* probably out of memory */
1212 	if (old_cond->avtrue_list == NULL) {
1213 		old_cond->avtrue_list = cond->avtrue_list;
1214 	} else {
1215 		for (tmp = old_cond->avtrue_list; tmp->next != NULL;
1216 		     tmp = tmp->next) ;
1217 		tmp->next = cond->avtrue_list;
1218 	}
1219 	if (old_cond->avfalse_list == NULL) {
1220 		old_cond->avfalse_list = cond->avfalse_list;
1221 	} else {
1222 		for (tmp = old_cond->avfalse_list; tmp->next != NULL;
1223 		     tmp = tmp->next) ;
1224 		tmp->next = cond->avfalse_list;
1225 	}
1226 
1227 	old_cond->flags |= cond->flags;
1228 }
1229 
append_avrule(avrule_t * avrule)1230 void append_avrule(avrule_t * avrule)
1231 {
1232 	avrule_decl_t *decl = stack_top->decl;
1233 
1234 	/* currently avrules follow a completely different code path
1235 	 * for handling avrules and compute types
1236 	 * (define_cond_avrule_te_avtab, define_cond_compute_type);
1237 	 * therefore there ought never be a conditional on top of the
1238 	 * scope stack */
1239 	assert(stack_top->type == 1);
1240 
1241 	if (stack_top->last_avrule == NULL) {
1242 		decl->avrules = avrule;
1243 	} else {
1244 		stack_top->last_avrule->next = avrule;
1245 	}
1246 	stack_top->last_avrule = avrule;
1247 }
1248 
1249 /* this doesn't actually append, but really prepends it */
append_role_trans(role_trans_rule_t * role_tr_rules)1250 void append_role_trans(role_trans_rule_t * role_tr_rules)
1251 {
1252 	avrule_decl_t *decl = stack_top->decl;
1253 
1254 	/* role transitions are not allowed within conditionals */
1255 	assert(stack_top->type == 1);
1256 
1257 	role_tr_rules->next = decl->role_tr_rules;
1258 	decl->role_tr_rules = role_tr_rules;
1259 }
1260 
1261 /* this doesn't actually append, but really prepends it */
append_role_allow(role_allow_rule_t * role_allow_rules)1262 void append_role_allow(role_allow_rule_t * role_allow_rules)
1263 {
1264 	avrule_decl_t *decl = stack_top->decl;
1265 
1266 	/* role allows are not allowed within conditionals */
1267 	assert(stack_top->type == 1);
1268 
1269 	role_allow_rules->next = decl->role_allow_rules;
1270 	decl->role_allow_rules = role_allow_rules;
1271 }
1272 
1273 /* this doesn't actually append, but really prepends it */
append_filename_trans(filename_trans_rule_t * filename_trans_rules)1274 void append_filename_trans(filename_trans_rule_t * filename_trans_rules)
1275 {
1276 	avrule_decl_t *decl = stack_top->decl;
1277 
1278 	/* filename transitions are not allowed within conditionals */
1279 	assert(stack_top->type == 1);
1280 
1281 	filename_trans_rules->next = decl->filename_trans_rules;
1282 	decl->filename_trans_rules = filename_trans_rules;
1283 }
1284 
1285 /* this doesn't actually append, but really prepends it */
append_range_trans(range_trans_rule_t * range_tr_rules)1286 void append_range_trans(range_trans_rule_t * range_tr_rules)
1287 {
1288 	avrule_decl_t *decl = stack_top->decl;
1289 
1290 	/* range transitions are not allowed within conditionals */
1291 	assert(stack_top->type == 1);
1292 
1293 	range_tr_rules->next = decl->range_tr_rules;
1294 	decl->range_tr_rules = range_tr_rules;
1295 }
1296 
begin_optional(int pass)1297 int begin_optional(int pass)
1298 {
1299 	avrule_block_t *block = NULL;
1300 	avrule_decl_t *decl;
1301 	if (pass == 1) {
1302 		/* allocate a new avrule block for this optional block */
1303 		if ((block = avrule_block_create()) == NULL ||
1304 		    (decl = avrule_decl_create(next_decl_id)) == NULL) {
1305 			goto cleanup;
1306 		}
1307 		block->flags |= AVRULE_OPTIONAL;
1308 		block->branch_list = decl;
1309 		last_block->next = block;
1310 	} else {
1311 		/* select the next block from the chain built during pass 1 */
1312 		block = last_block->next;
1313 		assert(block != NULL &&
1314 		       block->branch_list != NULL &&
1315 		       block->branch_list->decl_id == next_decl_id);
1316 		decl = block->branch_list;
1317 	}
1318 	if (push_stack(1, block, decl) == -1) {
1319 		goto cleanup;
1320 	}
1321 	stack_top->last_avrule = NULL;
1322 	last_block = block;
1323 	next_decl_id++;
1324 	return 0;
1325       cleanup:
1326 	yyerror("Out of memory!");
1327 	avrule_block_destroy(block);
1328 	return -1;
1329 }
1330 
end_optional(int pass)1331 int end_optional(int pass __attribute__ ((unused)))
1332 {
1333 	/* once nested conditionals are allowed, do the stack unfolding here */
1334 	pop_stack();
1335 	return 0;
1336 }
1337 
begin_optional_else(int pass)1338 int begin_optional_else(int pass)
1339 {
1340 	avrule_decl_t *decl;
1341 	assert(stack_top->type == 1 && stack_top->in_else == 0);
1342 	if (pass == 1) {
1343 		/* allocate a new declaration and add it to the
1344 		 * current chain */
1345 		if ((decl = avrule_decl_create(next_decl_id)) == NULL) {
1346 			yyerror("Out of memory!");
1347 			return -1;
1348 		}
1349 		stack_top->decl->next = decl;
1350 	} else {
1351 		/* pick the (hopefully last) declaration of this
1352 		   avrule block, built from pass 1 */
1353 		decl = stack_top->decl->next;
1354 		assert(decl != NULL &&
1355 		       decl->next == NULL && decl->decl_id == next_decl_id);
1356 	}
1357 	stack_top->in_else = 1;
1358 	stack_top->decl = decl;
1359 	stack_top->last_avrule = NULL;
1360 	stack_top->require_given = 0;
1361 	next_decl_id++;
1362 	return 0;
1363 }
1364 
copy_requirements(avrule_decl_t * dest,const scope_stack_t * stack)1365 static int copy_requirements(avrule_decl_t * dest, const scope_stack_t * stack)
1366 {
1367 	uint32_t i;
1368 	if (stack == NULL) {
1369 		return 0;
1370 	}
1371 	if (stack->type == 1) {
1372 		const scope_index_t *src_scope = &stack->decl->required;
1373 		scope_index_t *dest_scope = &dest->required;
1374 		for (i = 0; i < SYM_NUM; i++) {
1375 			const ebitmap_t *src_bitmap = &src_scope->scope[i];
1376 			ebitmap_t *dest_bitmap = &dest_scope->scope[i];
1377 			if (ebitmap_union(dest_bitmap, src_bitmap)) {
1378 				yyerror("Out of memory!");
1379 				return -1;
1380 			}
1381 		}
1382 		/* now copy class permissions */
1383 		if (src_scope->class_perms_len > dest_scope->class_perms_len) {
1384 			ebitmap_t *new_map =
1385 			    realloc(dest_scope->class_perms_map,
1386 				    src_scope->class_perms_len *
1387 				    sizeof(*new_map));
1388 			if (new_map == NULL) {
1389 				yyerror("Out of memory!");
1390 				return -1;
1391 			}
1392 			dest_scope->class_perms_map = new_map;
1393 			for (i = dest_scope->class_perms_len;
1394 			     i < src_scope->class_perms_len; i++) {
1395 				ebitmap_init(dest_scope->class_perms_map + i);
1396 			}
1397 			dest_scope->class_perms_len =
1398 			    src_scope->class_perms_len;
1399 		}
1400 		for (i = 0; i < src_scope->class_perms_len; i++) {
1401 			const ebitmap_t *src_bitmap = &src_scope->class_perms_map[i];
1402 			ebitmap_t *dest_bitmap =
1403 			    &dest_scope->class_perms_map[i];
1404 			if (ebitmap_union(dest_bitmap, src_bitmap)) {
1405 				yyerror("Out of memory!");
1406 				return -1;
1407 			}
1408 		}
1409 	}
1410 	return copy_requirements(dest, stack->parent);
1411 }
1412 
1413 /* During pass 1, check that at least one thing was required within
1414  * this block, for those places where a REQUIRED is necessary.  During
1415  * pass 2, have this block inherit its parents' requirements.  Return
1416  * 0 on success, -1 on failure. */
end_avrule_block(int pass)1417 int end_avrule_block(int pass)
1418 {
1419 	avrule_decl_t *decl = stack_top->decl;
1420 	assert(stack_top->type == 1);
1421 	if (pass == 2) {
1422 		/* this avrule_decl inherits all of its parents'
1423 		 * requirements */
1424 		if (copy_requirements(decl, stack_top->parent) == -1) {
1425 			return -1;
1426 		}
1427 		return 0;
1428 	}
1429 	if (!stack_top->in_else && !stack_top->require_given) {
1430 		if (policydbp->policy_type == POLICY_BASE
1431 		    && stack_top->parent != NULL) {
1432 			/* if this is base no require should be in the global block */
1433 			return 0;
1434 		} else {
1435 			/* non-ELSE branches must have at least one thing required */
1436 			yyerror("This block has no require section.");
1437 			return -1;
1438 		}
1439 	}
1440 	return 0;
1441 }
1442 
1443 /* Push a new scope on to the stack and update the 'last' pointer.
1444  * Return 0 on success, -1 if out * of memory. */
push_stack(int stack_type,...)1445 static int push_stack(int stack_type, ...)
1446 {
1447 	scope_stack_t *s = calloc(1, sizeof(*s));
1448 	va_list ap;
1449 	if (s == NULL) {
1450 		return -1;
1451 	}
1452 	va_start(ap, stack_type);
1453 	switch (s->type = stack_type) {
1454 	case 1:{
1455 			s->u.avrule = va_arg(ap, avrule_block_t *);
1456 			s->decl = va_arg(ap, avrule_decl_t *);
1457 			break;
1458 		}
1459 	case 2:{
1460 			s->u.cond_list = va_arg(ap, cond_list_t *);
1461 			break;
1462 		}
1463 	default:
1464 		/* invalid stack type given */
1465 		assert(0);
1466 	}
1467 	va_end(ap);
1468 	s->parent = stack_top;
1469 	s->child = NULL;
1470 	stack_top = s;
1471 	return 0;
1472 }
1473 
1474 /* Pop off the most recently added from the stack.  Update the 'last'
1475  * pointer. */
pop_stack(void)1476 static void pop_stack(void)
1477 {
1478 	scope_stack_t *parent;
1479 	assert(stack_top != NULL);
1480 	parent = stack_top->parent;
1481 	if (parent != NULL) {
1482 		parent->child = NULL;
1483 	}
1484 	free(stack_top);
1485 	stack_top = parent;
1486 }
1487