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