debuggers.hg

view xen/xsm/flask/ss/policydb.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 183d253550f3
line source
1 /*
2 * Implementation of the policy database.
3 *
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
7 /*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 *
10 * Support for enhanced MLS infrastructure.
11 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
14 * Added conditional policy language extensions
15 *
16 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
21 */
23 /* Ported to Xen 3.0, George Coker, <gscoker@alpha.ncsc.mil> */
25 #include <asm/byteorder.h>
26 #include <xen/lib.h>
27 #include <xen/types.h>
28 #include <xen/xmalloc.h>
29 #include <xen/string.h>
30 #include <xen/errno.h>
31 #include "security.h"
33 #include "policydb.h"
34 #include "conditional.h"
35 #include "mls.h"
37 #define _DEBUG_HASHES
39 #ifdef DEBUG_HASHES
40 static char *symtab_name[SYM_NUM] = {
41 "common prefixes",
42 "classes",
43 "roles",
44 "types",
45 "users",
46 "bools",
47 "levels",
48 "categories",
49 };
50 #endif
52 int flask_mls_enabled = 0;
54 static unsigned int symtab_sizes[SYM_NUM] = {
55 2,
56 32,
57 16,
58 512,
59 128,
60 16,
61 16,
62 16,
63 };
65 struct policydb_compat_info {
66 int version;
67 int sym_num;
68 int ocon_num;
69 };
71 /* These need to be updated if SYM_NUM or OCON_NUM changes */
72 static struct policydb_compat_info policydb_compat[] = {
73 {
74 .version = POLICYDB_VERSION_BASE,
75 .sym_num = SYM_NUM - 3,
76 .ocon_num = OCON_NUM - 1,
77 },
78 {
79 .version = POLICYDB_VERSION_BOOL,
80 .sym_num = SYM_NUM - 2,
81 .ocon_num = OCON_NUM - 1,
82 },
83 {
84 .version = POLICYDB_VERSION_IPV6,
85 .sym_num = SYM_NUM - 2,
86 .ocon_num = OCON_NUM,
87 },
88 {
89 .version = POLICYDB_VERSION_NLCLASS,
90 .sym_num = SYM_NUM - 2,
91 .ocon_num = OCON_NUM,
92 },
93 {
94 .version = POLICYDB_VERSION_MLS,
95 .sym_num = SYM_NUM,
96 .ocon_num = OCON_NUM,
97 },
98 {
99 .version = POLICYDB_VERSION_AVTAB,
100 .sym_num = SYM_NUM,
101 .ocon_num = OCON_NUM,
102 },
103 };
105 static struct policydb_compat_info *policydb_lookup_compat(int version)
106 {
107 int i;
108 struct policydb_compat_info *info = NULL;
110 for ( i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++ )
111 {
112 if ( policydb_compat[i].version == version )
113 {
114 info = &policydb_compat[i];
115 break;
116 }
117 }
118 return info;
119 }
121 /*
122 * Initialize the role table.
123 */
124 static int roles_init(struct policydb *p)
125 {
126 char *key = NULL;
127 int rc;
128 struct role_datum *role;
130 role = xmalloc(struct role_datum);
131 if ( !role )
132 {
133 rc = -ENOMEM;
134 goto out;
135 }
136 memset(role, 0, sizeof(*role));
137 role->value = ++p->p_roles.nprim;
138 if ( role->value != OBJECT_R_VAL )
139 {
140 rc = -EINVAL;
141 goto out_free_role;
142 }
143 key = xmalloc_array(char, strlen(OBJECT_R)+1);
144 if ( !key )
145 {
146 rc = -ENOMEM;
147 goto out_free_role;
148 }
149 strlcpy(key, OBJECT_R, strlen(OBJECT_R)+1);
150 rc = hashtab_insert(p->p_roles.table, key, role);
151 if ( rc )
152 goto out_free_key;
153 out:
154 return rc;
156 out_free_key:
157 xfree(key);
158 out_free_role:
159 xfree(role);
160 goto out;
161 }
163 /*
164 * Initialize a policy database structure.
165 */
166 static int policydb_init(struct policydb *p)
167 {
168 int i, rc;
170 memset(p, 0, sizeof(*p));
172 for ( i = 0; i < SYM_NUM; i++ )
173 {
174 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
175 if ( rc )
176 goto out_free_symtab;
177 }
179 rc = avtab_init(&p->te_avtab);
180 if ( rc )
181 goto out_free_symtab;
183 rc = roles_init(p);
184 if ( rc )
185 goto out_free_avtab;
187 rc = cond_policydb_init(p);
188 if ( rc )
189 goto out_free_avtab;
191 out:
192 return rc;
194 out_free_avtab:
195 avtab_destroy(&p->te_avtab);
197 out_free_symtab:
198 for ( i = 0; i < SYM_NUM; i++ )
199 hashtab_destroy(p->symtab[i].table);
200 goto out;
201 }
203 /*
204 * The following *_index functions are used to
205 * define the val_to_name and val_to_struct arrays
206 * in a policy database structure. The val_to_name
207 * arrays are used when converting security context
208 * structures into string representations. The
209 * val_to_struct arrays are used when the attributes
210 * of a class, role, or user are needed.
211 */
213 static int common_index(void *key, void *datum, void *datap)
214 {
215 struct policydb *p;
216 struct common_datum *comdatum;
218 comdatum = datum;
219 p = datap;
220 if ( !comdatum->value || comdatum->value > p->p_commons.nprim )
221 return -EINVAL;
222 p->p_common_val_to_name[comdatum->value - 1] = key;
223 return 0;
224 }
226 static int class_index(void *key, void *datum, void *datap)
227 {
228 struct policydb *p;
229 struct class_datum *cladatum;
231 cladatum = datum;
232 p = datap;
233 if ( !cladatum->value || cladatum->value > p->p_classes.nprim )
234 return -EINVAL;
235 p->p_class_val_to_name[cladatum->value - 1] = key;
236 p->class_val_to_struct[cladatum->value - 1] = cladatum;
237 return 0;
238 }
240 static int role_index(void *key, void *datum, void *datap)
241 {
242 struct policydb *p;
243 struct role_datum *role;
245 role = datum;
246 p = datap;
247 if ( !role->value || role->value > p->p_roles.nprim )
248 return -EINVAL;
249 p->p_role_val_to_name[role->value - 1] = key;
250 p->role_val_to_struct[role->value - 1] = role;
251 return 0;
252 }
254 static int type_index(void *key, void *datum, void *datap)
255 {
256 struct policydb *p;
257 struct type_datum *typdatum;
259 typdatum = datum;
260 p = datap;
262 if ( typdatum->primary )
263 {
264 if ( !typdatum->value || typdatum->value > p->p_types.nprim )
265 return -EINVAL;
266 p->p_type_val_to_name[typdatum->value - 1] = key;
267 }
269 return 0;
270 }
272 static int user_index(void *key, void *datum, void *datap)
273 {
274 struct policydb *p;
275 struct user_datum *usrdatum;
277 usrdatum = datum;
278 p = datap;
279 if ( !usrdatum->value || usrdatum->value > p->p_users.nprim )
280 return -EINVAL;
281 p->p_user_val_to_name[usrdatum->value - 1] = key;
282 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
283 return 0;
284 }
286 static int sens_index(void *key, void *datum, void *datap)
287 {
288 struct policydb *p;
289 struct level_datum *levdatum;
291 levdatum = datum;
292 p = datap;
294 if ( !levdatum->isalias )
295 {
296 if ( !levdatum->level->sens || levdatum->level->sens >
297 p->p_levels.nprim )
298 return -EINVAL;
299 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
300 }
302 return 0;
303 }
305 static int cat_index(void *key, void *datum, void *datap)
306 {
307 struct policydb *p;
308 struct cat_datum *catdatum;
310 catdatum = datum;
311 p = datap;
313 if ( !catdatum->isalias )
314 {
315 if ( !catdatum->value || catdatum->value > p->p_cats.nprim )
316 return -EINVAL;
317 p->p_cat_val_to_name[catdatum->value - 1] = key;
318 }
320 return 0;
321 }
323 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
324 {
325 common_index,
326 class_index,
327 role_index,
328 type_index,
329 user_index,
330 cond_index_bool,
331 sens_index,
332 cat_index,
333 };
335 /*
336 * Define the common val_to_name array and the class
337 * val_to_name and val_to_struct arrays in a policy
338 * database structure.
339 *
340 * Caller must clean up upon failure.
341 */
342 static int policydb_index_classes(struct policydb *p)
343 {
344 int rc;
346 p->p_common_val_to_name =
347 xmalloc_array(char *, p->p_commons.nprim);
348 if ( !p->p_common_val_to_name )
349 {
350 rc = -ENOMEM;
351 goto out;
352 }
354 rc = hashtab_map(p->p_commons.table, common_index, p);
355 if ( rc )
356 goto out;
358 p->class_val_to_struct =
359 (void *)xmalloc_array(struct class_datum, p->p_classes.nprim);
360 if ( !p->class_val_to_struct )
361 {
362 rc = -ENOMEM;
363 goto out;
364 }
366 p->p_class_val_to_name =
367 xmalloc_array(char *, p->p_classes.nprim);
368 if ( !p->p_class_val_to_name )
369 {
370 rc = -ENOMEM;
371 goto out;
372 }
374 rc = hashtab_map(p->p_classes.table, class_index, p);
375 out:
376 return rc;
377 }
379 #ifdef DEBUG_HASHES
380 static void symtab_hash_eval(struct symtab *s)
381 {
382 int i;
384 for ( i = 0; i < SYM_NUM; i++ )
385 {
386 struct hashtab *h = s[i].table;
387 struct hashtab_info info;
389 hashtab_stat(h, &info);
390 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
391 "longest chain length %d\n", symtab_name[i], h->nel,
392 info.slots_used, h->size, info.max_chain_len);
393 }
394 }
395 #endif
397 /*
398 * Define the other val_to_name and val_to_struct arrays
399 * in a policy database structure.
400 *
401 * Caller must clean up on failure.
402 */
403 static int policydb_index_others(struct policydb *p)
404 {
405 int i, rc = 0;
407 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
408 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
409 if ( flask_mls_enabled )
410 printk(", %d sens, %d cats", p->p_levels.nprim, p->p_cats.nprim);
412 printk("\n");
414 printk(KERN_INFO "security: %d classes, %d rules\n",
415 p->p_classes.nprim, p->te_avtab.nel);
417 #ifdef DEBUG_HASHES
418 avtab_hash_eval(&p->te_avtab, "rules");
419 symtab_hash_eval(p->symtab);
420 #endif
422 p->role_val_to_struct =
423 (void *)xmalloc_array(struct role_datum, p->p_roles.nprim);
424 if ( !p->role_val_to_struct )
425 {
426 rc = -ENOMEM;
427 goto out;
428 }
430 p->user_val_to_struct =
431 (void *)xmalloc_array(struct user_datum, p->p_users.nprim);
432 if ( !p->user_val_to_struct )
433 {
434 rc = -ENOMEM;
435 goto out;
436 }
438 if ( cond_init_bool_indexes(p) )
439 {
440 rc = -ENOMEM;
441 goto out;
442 }
444 for ( i = SYM_ROLES; i < SYM_NUM; i++ )
445 {
446 p->sym_val_to_name[i] =
447 xmalloc_array(char *, p->symtab[i].nprim);
448 if ( !p->sym_val_to_name[i] )
449 {
450 rc = -ENOMEM;
451 goto out;
452 }
453 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
454 if ( rc )
455 goto out;
456 }
458 out:
459 return rc;
460 }
462 /*
463 * The following *_destroy functions are used to
464 * free any memory allocated for each kind of
465 * symbol data in the policy database.
466 */
468 static int perm_destroy(void *key, void *datum, void *p)
469 {
470 xfree(key);
471 xfree(datum);
472 return 0;
473 }
475 static int common_destroy(void *key, void *datum, void *p)
476 {
477 struct common_datum *comdatum;
479 xfree(key);
480 comdatum = datum;
481 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
482 hashtab_destroy(comdatum->permissions.table);
483 xfree(datum);
484 return 0;
485 }
487 static int class_destroy(void *key, void *datum, void *p)
488 {
489 struct class_datum *cladatum;
490 struct constraint_node *constraint, *ctemp;
491 struct constraint_expr *e, *etmp;
493 xfree(key);
494 cladatum = datum;
495 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
496 hashtab_destroy(cladatum->permissions.table);
497 constraint = cladatum->constraints;
498 while ( constraint )
499 {
500 e = constraint->expr;
501 while ( e )
502 {
503 ebitmap_destroy(&e->names);
504 etmp = e;
505 e = e->next;
506 xfree(etmp);
507 }
508 ctemp = constraint;
509 constraint = constraint->next;
510 xfree(ctemp);
511 }
513 constraint = cladatum->validatetrans;
514 while ( constraint )
515 {
516 e = constraint->expr;
517 while ( e )
518 {
519 ebitmap_destroy(&e->names);
520 etmp = e;
521 e = e->next;
522 xfree(etmp);
523 }
524 ctemp = constraint;
525 constraint = constraint->next;
526 xfree(ctemp);
527 }
529 xfree(cladatum->comkey);
530 xfree(datum);
531 return 0;
532 }
534 static int role_destroy(void *key, void *datum, void *p)
535 {
536 struct role_datum *role;
538 xfree(key);
539 role = datum;
540 ebitmap_destroy(&role->dominates);
541 ebitmap_destroy(&role->types);
542 xfree(datum);
543 return 0;
544 }
546 static int type_destroy(void *key, void *datum, void *p)
547 {
548 xfree(key);
549 xfree(datum);
550 return 0;
551 }
553 static int user_destroy(void *key, void *datum, void *p)
554 {
555 struct user_datum *usrdatum;
557 xfree(key);
558 usrdatum = datum;
559 ebitmap_destroy(&usrdatum->roles);
560 ebitmap_destroy(&usrdatum->range.level[0].cat);
561 ebitmap_destroy(&usrdatum->range.level[1].cat);
562 ebitmap_destroy(&usrdatum->dfltlevel.cat);
563 xfree(datum);
564 return 0;
565 }
567 static int sens_destroy(void *key, void *datum, void *p)
568 {
569 struct level_datum *levdatum;
571 xfree(key);
572 levdatum = datum;
573 ebitmap_destroy(&levdatum->level->cat);
574 xfree(levdatum->level);
575 xfree(datum);
576 return 0;
577 }
579 static int cat_destroy(void *key, void *datum, void *p)
580 {
581 xfree(key);
582 xfree(datum);
583 return 0;
584 }
586 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
587 {
588 common_destroy,
589 class_destroy,
590 role_destroy,
591 type_destroy,
592 user_destroy,
593 cond_destroy_bool,
594 sens_destroy,
595 cat_destroy,
596 };
598 static void ocontext_destroy(struct ocontext *c, int i)
599 {
600 context_destroy(&c->context[0]);
601 context_destroy(&c->context[1]);
602 if ( i == OCON_ISID )
603 xfree(c->u.name);
604 xfree(c);
605 }
607 /*
608 * Free any memory allocated by a policy database structure.
609 */
610 void policydb_destroy(struct policydb *p)
611 {
612 struct ocontext *c, *ctmp;
613 int i;
614 struct role_allow *ra, *lra = NULL;
615 struct role_trans *tr, *ltr = NULL;
616 struct range_trans *rt, *lrt = NULL;
618 for ( i = 0; i < SYM_NUM; i++ )
619 {
620 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
621 hashtab_destroy(p->symtab[i].table);
622 }
624 for ( i = 0; i < SYM_NUM; i++ )
625 xfree(p->sym_val_to_name[i]);
627 xfree(p->class_val_to_struct);
628 xfree(p->role_val_to_struct);
629 xfree(p->user_val_to_struct);
631 avtab_destroy(&p->te_avtab);
633 for ( i = 0; i < OCON_NUM; i++ )
634 {
635 c = p->ocontexts[i];
636 while ( c )
637 {
638 ctmp = c;
639 c = c->next;
640 ocontext_destroy(ctmp,i);
641 }
642 }
644 cond_policydb_destroy(p);
646 for ( tr = p->role_tr; tr; tr = tr->next )
647 {
648 if ( ltr ) xfree(ltr);
649 ltr = tr;
650 }
651 if ( ltr ) xfree(ltr);
653 for ( ra = p->role_allow; ra; ra = ra -> next )
654 {
655 if ( lra ) xfree(lra);
656 lra = ra;
657 }
658 if ( lra ) xfree(lra);
660 for ( rt = p->range_tr; rt; rt = rt -> next )
661 {
662 if ( lrt ) xfree(lrt);
663 lrt = rt;
664 }
665 if ( lrt ) xfree(lrt);
667 for ( i = 0; i < p->p_types.nprim; i++ )
668 ebitmap_destroy(&p->type_attr_map[i]);
669 xfree(p->type_attr_map);
671 return;
672 }
674 /*
675 * Load the initial SIDs specified in a policy database
676 * structure into a SID table.
677 */
678 int policydb_load_isids(struct policydb *p, struct sidtab *s)
679 {
680 struct ocontext *head, *c;
681 int rc;
683 rc = sidtab_init(s);
684 if ( rc )
685 {
686 printk(KERN_ERR "security: out of memory on SID table init\n");
687 goto out;
688 }
690 head = p->ocontexts[OCON_ISID];
691 for ( c = head; c; c = c->next )
692 {
693 if ( !c->context[0].user )
694 {
695 printk(KERN_ERR "security: SID %s was never "
696 "defined.\n", c->u.name);
697 rc = -EINVAL;
698 goto out;
699 }
700 if ( sidtab_insert(s, c->sid[0], &c->context[0]) )
701 {
702 printk(KERN_ERR "security: unable to load initial "
703 "SID %s.\n", c->u.name);
704 rc = -EINVAL;
705 goto out;
706 }
707 }
708 out:
709 return rc;
710 }
712 /*
713 * Return 1 if the fields in the security context
714 * structure `c' are valid. Return 0 otherwise.
715 */
716 int policydb_context_isvalid(struct policydb *p, struct context *c)
717 {
718 struct role_datum *role;
719 struct user_datum *usrdatum;
721 if ( !c->role || c->role > p->p_roles.nprim )
722 return 0;
724 if ( !c->user || c->user > p->p_users.nprim )
725 return 0;
727 if ( !c->type || c->type > p->p_types.nprim )
728 return 0;
730 if ( c->role != OBJECT_R_VAL )
731 {
732 /*
733 * Role must be authorized for the type.
734 */
735 role = p->role_val_to_struct[c->role - 1];
736 if ( !ebitmap_get_bit(&role->types, c->type - 1) )
737 /* role may not be associated with type */
738 return 0;
740 /*
741 * User must be authorized for the role.
742 */
743 usrdatum = p->user_val_to_struct[c->user - 1];
744 if ( !usrdatum )
745 return 0;
747 if ( !ebitmap_get_bit(&usrdatum->roles, c->role - 1) )
748 /* user may not be associated with role */
749 return 0;
750 }
752 if ( !mls_context_isvalid(p, c) )
753 return 0;
755 return 1;
756 }
758 /*
759 * Read a MLS range structure from a policydb binary
760 * representation file.
761 */
762 static int mls_read_range_helper(struct mls_range *r, void *fp)
763 {
764 __le32 buf[2];
765 u32 items;
766 int rc;
768 rc = next_entry(buf, fp, sizeof(u32));
769 if ( rc < 0 )
770 goto out;
772 items = le32_to_cpu(buf[0]);
773 if ( items > ARRAY_SIZE(buf) )
774 {
775 printk(KERN_ERR "security: mls: range overflow\n");
776 rc = -EINVAL;
777 goto out;
778 }
779 rc = next_entry(buf, fp, sizeof(u32) * items);
780 if ( rc < 0 )
781 {
782 printk(KERN_ERR "security: mls: truncated range\n");
783 goto out;
784 }
785 r->level[0].sens = le32_to_cpu(buf[0]);
786 if ( items > 1 )
787 r->level[1].sens = le32_to_cpu(buf[1]);
788 else
789 r->level[1].sens = r->level[0].sens;
791 rc = ebitmap_read(&r->level[0].cat, fp);
792 if ( rc )
793 {
794 printk(KERN_ERR "security: mls: error reading low "
795 "categories\n");
796 goto out;
797 }
798 if ( items > 1 )
799 {
800 rc = ebitmap_read(&r->level[1].cat, fp);
801 if ( rc )
802 {
803 printk(KERN_ERR "security: mls: error reading high "
804 "categories\n");
805 goto bad_high;
806 }
807 }
808 else
809 {
810 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
811 if ( rc )
812 {
813 printk(KERN_ERR "security: mls: out of memory\n");
814 goto bad_high;
815 }
816 }
818 rc = 0;
819 out:
820 return rc;
821 bad_high:
822 ebitmap_destroy(&r->level[0].cat);
823 goto out;
824 }
826 /*
827 * Read and validate a security context structure
828 * from a policydb binary representation file.
829 */
830 static int context_read_and_validate(struct context *c, struct policydb *p,
831 void *fp)
832 {
833 __le32 buf[3];
834 int rc;
836 rc = next_entry(buf, fp, sizeof buf);
837 if ( rc < 0 )
838 {
839 printk(KERN_ERR "security: context truncated\n");
840 goto out;
841 }
842 c->user = le32_to_cpu(buf[0]);
843 c->role = le32_to_cpu(buf[1]);
844 c->type = le32_to_cpu(buf[2]);
845 if ( p->policyvers >= POLICYDB_VERSION_MLS )
846 {
847 if ( mls_read_range_helper(&c->range, fp) )
848 {
849 printk(KERN_ERR "security: error reading MLS range of "
850 "context\n");
851 rc = -EINVAL;
852 goto out;
853 }
854 }
856 if ( !policydb_context_isvalid(p, c) )
857 {
858 printk(KERN_ERR "security: invalid security context\n");
859 context_destroy(c);
860 rc = -EINVAL;
861 }
862 out:
863 return rc;
864 }
866 /*
867 * The following *_read functions are used to
868 * read the symbol data from a policy database
869 * binary representation file.
870 */
872 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
873 {
874 char *key = NULL;
875 struct perm_datum *perdatum;
876 int rc;
877 __le32 buf[2];
878 u32 len;
880 perdatum = xmalloc(struct perm_datum);
881 if ( !perdatum )
882 {
883 rc = -ENOMEM;
884 goto out;
885 }
886 memset(perdatum, 0, sizeof(*perdatum));
888 rc = next_entry(buf, fp, sizeof buf);
889 if ( rc < 0 )
890 goto bad;
892 len = le32_to_cpu(buf[0]);
893 perdatum->value = le32_to_cpu(buf[1]);
895 key = xmalloc_array(char, len + 1);
896 if ( !key )
897 {
898 rc = -ENOMEM;
899 goto bad;
900 }
901 rc = next_entry(key, fp, len);
902 if ( rc < 0 )
903 goto bad;
904 key[len] = 0;
906 rc = hashtab_insert(h, key, perdatum);
907 if ( rc )
908 goto bad;
909 out:
910 return rc;
911 bad:
912 perm_destroy(key, perdatum, NULL);
913 goto out;
914 }
916 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
917 {
918 char *key = NULL;
919 struct common_datum *comdatum;
920 __le32 buf[4];
921 u32 len, nel;
922 int i, rc;
924 comdatum = xmalloc(struct common_datum);
925 if ( !comdatum )
926 {
927 rc = -ENOMEM;
928 goto out;
929 }
930 memset(comdatum, 0, sizeof(*comdatum));
932 rc = next_entry(buf, fp, sizeof buf);
933 if ( rc < 0 )
934 goto bad;
936 len = le32_to_cpu(buf[0]);
937 comdatum->value = le32_to_cpu(buf[1]);
939 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
940 if ( rc )
941 goto bad;
942 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
943 nel = le32_to_cpu(buf[3]);
945 key = xmalloc_array(char, len + 1);
946 if ( !key )
947 {
948 rc = -ENOMEM;
949 goto bad;
950 }
951 rc = next_entry(key, fp, len);
952 if ( rc < 0 )
953 goto bad;
954 key[len] = 0;
956 for ( i = 0; i < nel; i++ )
957 {
958 rc = perm_read(p, comdatum->permissions.table, fp);
959 if ( rc )
960 goto bad;
961 }
963 rc = hashtab_insert(h, key, comdatum);
964 if ( rc )
965 goto bad;
966 out:
967 return rc;
968 bad:
969 common_destroy(key, comdatum, NULL);
970 goto out;
971 }
973 static int read_cons_helper(struct constraint_node **nodep, int ncons,
974 int allowxtarget, void *fp)
975 {
976 struct constraint_node *c, *lc;
977 struct constraint_expr *e, *le;
978 __le32 buf[3];
979 u32 nexpr;
980 int rc, i, j, depth;
982 lc = NULL;
983 for ( i = 0; i < ncons; i++ )
984 {
985 c = xmalloc(struct constraint_node);
986 if ( !c )
987 return -ENOMEM;
988 memset(c, 0, sizeof(*c));
990 if ( lc )
991 {
992 lc->next = c;
993 }
994 else
995 {
996 *nodep = c;
997 }
999 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1000 if ( rc < 0 )
1001 return rc;
1002 c->permissions = le32_to_cpu(buf[0]);
1003 nexpr = le32_to_cpu(buf[1]);
1004 le = NULL;
1005 depth = -1;
1006 for ( j = 0; j < nexpr; j++ )
1008 e = xmalloc(struct constraint_expr);
1009 if ( !e )
1010 return -ENOMEM;
1011 memset(e, 0, sizeof(*e));
1013 if ( le )
1014 le->next = e;
1015 else
1016 c->expr = e;
1018 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1019 if ( rc < 0 )
1020 return rc;
1021 e->expr_type = le32_to_cpu(buf[0]);
1022 e->attr = le32_to_cpu(buf[1]);
1023 e->op = le32_to_cpu(buf[2]);
1025 switch ( e->expr_type )
1027 case CEXPR_NOT:
1028 if ( depth < 0 )
1029 return -EINVAL;
1030 break;
1031 case CEXPR_AND:
1032 case CEXPR_OR:
1033 if ( depth < 1 )
1034 return -EINVAL;
1035 depth--;
1036 break;
1037 case CEXPR_ATTR:
1038 if ( depth == (CEXPR_MAXDEPTH - 1) )
1039 return -EINVAL;
1040 depth++;
1041 break;
1042 case CEXPR_NAMES:
1043 if ( !allowxtarget && (e->attr & CEXPR_XTARGET) )
1044 return -EINVAL;
1045 if ( depth == (CEXPR_MAXDEPTH - 1) )
1046 return -EINVAL;
1047 depth++;
1048 if ( ebitmap_read(&e->names, fp) )
1049 return -EINVAL;
1050 break;
1051 default:
1052 return -EINVAL;
1054 le = e;
1056 if ( depth != 0 )
1057 return -EINVAL;
1058 lc = c;
1061 return 0;
1064 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1066 char *key = NULL;
1067 struct class_datum *cladatum;
1068 __le32 buf[6];
1069 u32 len, len2, ncons, nel;
1070 int i, rc;
1072 cladatum = xmalloc(struct class_datum);
1073 if ( !cladatum )
1075 rc = -ENOMEM;
1076 goto out;
1078 memset(cladatum, 0, sizeof(*cladatum));
1080 rc = next_entry(buf, fp, sizeof(u32)*6);
1081 if ( rc < 0 )
1082 goto bad;
1084 len = le32_to_cpu(buf[0]);
1085 len2 = le32_to_cpu(buf[1]);
1086 cladatum->value = le32_to_cpu(buf[2]);
1088 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1089 if ( rc )
1090 goto bad;
1091 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1092 nel = le32_to_cpu(buf[4]);
1094 ncons = le32_to_cpu(buf[5]);
1096 key = xmalloc_array(char, len + 1);
1097 if ( !key )
1099 rc = -ENOMEM;
1100 goto bad;
1102 rc = next_entry(key, fp, len);
1103 if ( rc < 0 )
1104 goto bad;
1105 key[len] = 0;
1107 if ( len2 )
1109 cladatum->comkey = xmalloc_array(char, len2 + 1);
1110 if ( !cladatum->comkey )
1112 rc = -ENOMEM;
1113 goto bad;
1115 rc = next_entry(cladatum->comkey, fp, len2);
1116 if ( rc < 0 )
1117 goto bad;
1118 cladatum->comkey[len2] = 0;
1120 cladatum->comdatum = hashtab_search(p->p_commons.table,
1121 cladatum->comkey);
1122 if ( !cladatum->comdatum )
1124 printk(KERN_ERR "security: unknown common %s\n",
1125 cladatum->comkey);
1126 rc = -EINVAL;
1127 goto bad;
1130 for ( i = 0; i < nel; i++ )
1132 rc = perm_read(p, cladatum->permissions.table, fp);
1133 if ( rc )
1134 goto bad;
1137 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1138 if ( rc )
1139 goto bad;
1141 if ( p->policyvers >= POLICYDB_VERSION_VALIDATETRANS )
1143 /* grab the validatetrans rules */
1144 rc = next_entry(buf, fp, sizeof(u32));
1145 if ( rc < 0 )
1146 goto bad;
1147 ncons = le32_to_cpu(buf[0]);
1148 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1149 if ( rc )
1150 goto bad;
1153 rc = hashtab_insert(h, key, cladatum);
1154 if ( rc )
1155 goto bad;
1157 rc = 0;
1158 out:
1159 return rc;
1160 bad:
1161 class_destroy(key, cladatum, NULL);
1162 goto out;
1165 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1167 char *key = NULL;
1168 struct role_datum *role;
1169 int rc;
1170 __le32 buf[2];
1171 u32 len;
1173 role = xmalloc(struct role_datum);
1174 if ( !role )
1176 rc = -ENOMEM;
1177 goto out;
1179 memset(role, 0, sizeof(*role));
1181 rc = next_entry(buf, fp, sizeof buf);
1182 if ( rc < 0 )
1183 goto bad;
1185 len = le32_to_cpu(buf[0]);
1186 role->value = le32_to_cpu(buf[1]);
1188 key = xmalloc_array(char, len + 1);
1189 if ( !key )
1191 rc = -ENOMEM;
1192 goto bad;
1194 rc = next_entry(key, fp, len);
1195 if ( rc < 0 )
1196 goto bad;
1197 key[len] = 0;
1199 rc = ebitmap_read(&role->dominates, fp);
1200 if ( rc )
1201 goto bad;
1203 rc = ebitmap_read(&role->types, fp);
1204 if ( rc )
1205 goto bad;
1207 if ( strcmp(key, OBJECT_R) == 0 )
1209 if ( role->value != OBJECT_R_VAL )
1211 printk(KERN_ERR "Role %s has wrong value %d\n", OBJECT_R,
1212 role->value);
1213 rc = -EINVAL;
1214 goto bad;
1216 rc = 0;
1217 goto bad;
1220 rc = hashtab_insert(h, key, role);
1221 if ( rc )
1222 goto bad;
1223 out:
1224 return rc;
1225 bad:
1226 role_destroy(key, role, NULL);
1227 goto out;
1230 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1232 char *key = NULL;
1233 struct type_datum *typdatum;
1234 int rc;
1235 __le32 buf[3];
1236 u32 len;
1238 typdatum = xmalloc(struct type_datum);
1239 if ( !typdatum )
1241 rc = -ENOMEM;
1242 return rc;
1244 memset(typdatum, 0, sizeof(*typdatum));
1246 rc = next_entry(buf, fp, sizeof buf);
1247 if ( rc < 0 )
1248 goto bad;
1250 len = le32_to_cpu(buf[0]);
1251 typdatum->value = le32_to_cpu(buf[1]);
1252 typdatum->primary = le32_to_cpu(buf[2]);
1254 key = xmalloc_array(char, len + 1);
1255 if ( !key )
1257 rc = -ENOMEM;
1258 goto bad;
1260 rc = next_entry(key, fp, len);
1261 if ( rc < 0 )
1262 goto bad;
1263 key[len] = 0;
1265 rc = hashtab_insert(h, key, typdatum);
1266 if ( rc )
1267 goto bad;
1268 out:
1269 return rc;
1270 bad:
1271 type_destroy(key, typdatum, NULL);
1272 goto out;
1276 /*
1277 * Read a MLS level structure from a policydb binary
1278 * representation file.
1279 */
1280 static int mls_read_level(struct mls_level *lp, void *fp)
1282 __le32 buf[1];
1283 int rc;
1285 memset(lp, 0, sizeof(*lp));
1287 rc = next_entry(buf, fp, sizeof buf);
1288 if ( rc < 0 )
1290 printk(KERN_ERR "security: mls: truncated level\n");
1291 goto bad;
1293 lp->sens = le32_to_cpu(buf[0]);
1295 if ( ebitmap_read(&lp->cat, fp) )
1297 printk(KERN_ERR "security: mls: error reading level categories\n");
1298 goto bad;
1300 return 0;
1302 bad:
1303 return -EINVAL;
1306 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1308 char *key = NULL;
1309 struct user_datum *usrdatum;
1310 int rc;
1311 __le32 buf[2];
1312 u32 len;
1314 usrdatum = xmalloc(struct user_datum);
1315 if ( !usrdatum )
1317 rc = -ENOMEM;
1318 goto out;
1320 memset(usrdatum, 0, sizeof(*usrdatum));
1322 rc = next_entry(buf, fp, sizeof buf);
1323 if ( rc < 0 )
1324 goto bad;
1326 len = le32_to_cpu(buf[0]);
1327 usrdatum->value = le32_to_cpu(buf[1]);
1329 key = xmalloc_array(char, len + 1);
1330 if ( !key )
1332 rc = -ENOMEM;
1333 goto bad;
1335 rc = next_entry(key, fp, len);
1336 if ( rc < 0 )
1337 goto bad;
1338 key[len] = 0;
1340 rc = ebitmap_read(&usrdatum->roles, fp);
1341 if ( rc )
1342 goto bad;
1344 if ( p->policyvers >= POLICYDB_VERSION_MLS )
1346 rc = mls_read_range_helper(&usrdatum->range, fp);
1347 if ( rc )
1348 goto bad;
1349 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1350 if ( rc )
1351 goto bad;
1354 rc = hashtab_insert(h, key, usrdatum);
1355 if ( rc )
1356 goto bad;
1357 out:
1358 return rc;
1359 bad:
1360 user_destroy(key, usrdatum, NULL);
1361 goto out;
1364 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1366 char *key = NULL;
1367 struct level_datum *levdatum;
1368 int rc;
1369 __le32 buf[2];
1370 u32 len;
1372 levdatum = xmalloc(struct level_datum);
1373 if ( !levdatum )
1375 rc = -ENOMEM;
1376 goto out;
1378 memset(levdatum, 0, sizeof(*levdatum));
1380 rc = next_entry(buf, fp, sizeof buf);
1381 if ( rc < 0 )
1382 goto bad;
1384 len = le32_to_cpu(buf[0]);
1385 levdatum->isalias = le32_to_cpu(buf[1]);
1387 key = xmalloc_array(char, len + 1);
1388 if ( !key )
1390 rc = -ENOMEM;
1391 goto bad;
1393 rc = next_entry(key, fp, len);
1394 if ( rc < 0 )
1395 goto bad;
1396 key[len] = 0;
1398 levdatum->level = xmalloc(struct mls_level);
1399 if ( !levdatum->level )
1401 rc = -ENOMEM;
1402 goto bad;
1404 if ( mls_read_level(levdatum->level, fp) )
1406 rc = -EINVAL;
1407 goto bad;
1410 rc = hashtab_insert(h, key, levdatum);
1411 if ( rc )
1412 goto bad;
1413 out:
1414 return rc;
1415 bad:
1416 sens_destroy(key, levdatum, NULL);
1417 goto out;
1420 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1422 char *key = NULL;
1423 struct cat_datum *catdatum;
1424 int rc;
1425 __le32 buf[3];
1426 u32 len;
1428 catdatum = xmalloc(struct cat_datum);
1429 if ( !catdatum )
1431 rc = -ENOMEM;
1432 goto out;
1434 memset(catdatum, 0, sizeof(*catdatum));
1436 rc = next_entry(buf, fp, sizeof buf);
1437 if ( rc < 0 )
1438 goto bad;
1440 len = le32_to_cpu(buf[0]);
1441 catdatum->value = le32_to_cpu(buf[1]);
1442 catdatum->isalias = le32_to_cpu(buf[2]);
1444 key = xmalloc_array(char, len + 1);
1445 if ( !key )
1447 rc = -ENOMEM;
1448 goto bad;
1450 rc = next_entry(key, fp, len);
1451 if ( rc < 0 )
1452 goto bad;
1453 key[len] = 0;
1455 rc = hashtab_insert(h, key, catdatum);
1456 if ( rc )
1457 goto bad;
1458 out:
1459 return rc;
1461 bad:
1462 cat_destroy(key, catdatum, NULL);
1463 goto out;
1466 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1468 common_read,
1469 class_read,
1470 role_read,
1471 type_read,
1472 user_read,
1473 cond_read_bool,
1474 sens_read,
1475 cat_read,
1476 };
1478 extern int ss_initialized;
1480 /*
1481 * Read the configuration data from a policy database binary
1482 * representation file into a policy database structure.
1483 */
1484 int policydb_read(struct policydb *p, void *fp)
1486 struct role_allow *ra, *lra;
1487 struct role_trans *tr, *ltr;
1488 struct ocontext *l, *c /*, *newc*/;
1489 int i, j, rc;
1490 __le32 buf[8];
1491 u32 len, /*len2,*/ config, nprim, nel /*, nel2*/;
1492 char *policydb_str;
1493 struct policydb_compat_info *info;
1494 struct range_trans *rt, *lrt;
1496 config = 0;
1497 rc = policydb_init(p);
1498 if ( rc )
1499 goto out;
1501 /* Read the magic number and string length. */
1502 rc = next_entry(buf, fp, sizeof(u32)* 2);
1503 if ( rc < 0 )
1504 goto bad;
1506 if ( le32_to_cpu(buf[0]) != POLICYDB_MAGIC )
1508 printk(KERN_ERR "security: policydb magic number 0x%x does "
1509 "not match expected magic number 0x%x\n",
1510 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1511 goto bad;
1514 len = le32_to_cpu(buf[1]);
1515 if ( len != strlen(POLICYDB_STRING) )
1517 printk(KERN_ERR "security: policydb string length %d does not "
1518 "match expected length %Zu\n",
1519 len, (u32) strlen(POLICYDB_STRING));
1520 goto bad;
1522 policydb_str = xmalloc_array(char, len + 1);
1523 if ( !policydb_str )
1525 printk(KERN_ERR "security: unable to allocate memory for policydb "
1526 "string of length %d\n", len);
1527 rc = -ENOMEM;
1528 goto bad;
1530 rc = next_entry(policydb_str, fp, len);
1531 if ( rc < 0 )
1533 printk(KERN_ERR "security: truncated policydb string identifier\n");
1534 xfree(policydb_str);
1535 goto bad;
1537 policydb_str[len] = 0;
1538 if ( strcmp(policydb_str, POLICYDB_STRING) )
1540 printk(KERN_ERR "security: policydb string %s does not match "
1541 "my string %s\n", policydb_str, POLICYDB_STRING);
1542 xfree(policydb_str);
1543 goto bad;
1545 /* Done with policydb_str. */
1546 xfree(policydb_str);
1547 policydb_str = NULL;
1549 /* Read the version, config, and table sizes. */
1550 rc = next_entry(buf, fp, sizeof(u32)*4);
1551 if ( rc < 0 )
1552 goto bad;
1554 p->policyvers = le32_to_cpu(buf[0]);
1555 if ( p->policyvers < POLICYDB_VERSION_MIN ||
1556 p->policyvers > POLICYDB_VERSION_MAX )
1558 printk(KERN_ERR "security: policydb version %d does not match "
1559 "my version range %d-%d\n",
1560 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1561 goto bad;
1564 if ( (le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS) )
1566 if ( ss_initialized && !flask_mls_enabled )
1568 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1569 "policies\n");
1570 goto bad;
1572 flask_mls_enabled = 1;
1573 config |= POLICYDB_CONFIG_MLS;
1575 if ( p->policyvers < POLICYDB_VERSION_MLS )
1577 printk(KERN_ERR "security policydb version %d (MLS) "
1578 "not backwards compatible\n", p->policyvers);
1579 goto bad;
1582 else
1584 if ( ss_initialized && flask_mls_enabled )
1586 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1587 "policies\n");
1588 goto bad;
1592 info = policydb_lookup_compat(p->policyvers);
1593 if ( !info )
1595 printk(KERN_ERR "security: unable to find policy compat info "
1596 "for version %d\n", p->policyvers);
1597 goto bad;
1600 if ( le32_to_cpu(buf[2]) != info->sym_num ||
1601 le32_to_cpu(buf[3]) != info->ocon_num )
1603 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1604 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1605 le32_to_cpu(buf[3]),
1606 info->sym_num, info->ocon_num);
1607 goto bad;
1610 for ( i = 0; i < info->sym_num; i++ )
1612 rc = next_entry(buf, fp, sizeof(u32)*2);
1613 if ( rc < 0 )
1614 goto bad;
1615 nprim = le32_to_cpu(buf[0]);
1616 nel = le32_to_cpu(buf[1]);
1617 for ( j = 0; j < nel; j++ )
1619 rc = read_f[i](p, p->symtab[i].table, fp);
1620 if ( rc )
1621 goto bad;
1624 p->symtab[i].nprim = nprim;
1627 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1628 if ( rc )
1629 goto bad;
1631 if ( p->policyvers >= POLICYDB_VERSION_BOOL )
1633 rc = cond_read_list(p, fp);
1634 if ( rc )
1635 goto bad;
1638 rc = next_entry(buf, fp, sizeof(u32));
1639 if ( rc < 0 )
1640 goto bad;
1641 nel = le32_to_cpu(buf[0]);
1642 ltr = NULL;
1643 for ( i = 0; i < nel; i++ )
1645 tr = xmalloc(struct role_trans);
1646 if ( !tr )
1648 rc = -ENOMEM;
1649 goto bad;
1651 memset(tr, 0, sizeof(*tr));
1652 if ( ltr )
1653 ltr->next = tr;
1654 else
1655 p->role_tr = tr;
1656 rc = next_entry(buf, fp, sizeof(u32)*3);
1657 if ( rc < 0 )
1658 goto bad;
1659 tr->role = le32_to_cpu(buf[0]);
1660 tr->type = le32_to_cpu(buf[1]);
1661 tr->new_role = le32_to_cpu(buf[2]);
1662 ltr = tr;
1665 rc = next_entry(buf, fp, sizeof(u32));
1666 if ( rc < 0 )
1667 goto bad;
1668 nel = le32_to_cpu(buf[0]);
1669 lra = NULL;
1670 for ( i = 0; i < nel; i++ )
1672 ra = xmalloc(struct role_allow);
1673 if ( !ra )
1675 rc = -ENOMEM;
1676 goto bad;
1678 memset(ra, 0, sizeof(*ra));
1679 if ( lra )
1680 lra->next = ra;
1681 else
1682 p->role_allow = ra;
1683 rc = next_entry(buf, fp, sizeof(u32)*2);
1684 if ( rc < 0 )
1685 goto bad;
1686 ra->role = le32_to_cpu(buf[0]);
1687 ra->new_role = le32_to_cpu(buf[1]);
1688 lra = ra;
1691 rc = policydb_index_classes(p);
1692 if ( rc )
1693 goto bad;
1695 rc = policydb_index_others(p);
1696 if ( rc )
1697 goto bad;
1699 for ( i = 0; i < info->ocon_num; i++ )
1701 rc = next_entry(buf, fp, sizeof(u32));
1702 if ( rc < 0 )
1703 goto bad;
1704 nel = le32_to_cpu(buf[0]);
1705 l = NULL;
1706 for ( j = 0; j < nel; j++ )
1708 c = xmalloc(struct ocontext);
1709 if ( !c )
1711 rc = -ENOMEM;
1712 goto bad;
1714 memset(c, 0, sizeof(*c));
1715 if ( l )
1716 l->next = c;
1717 else
1718 p->ocontexts[i] = c;
1719 l = c;
1720 rc = -EINVAL;
1721 switch ( i )
1723 case OCON_ISID:
1724 rc = next_entry(buf, fp, sizeof(u32));
1725 if ( rc < 0 )
1726 goto bad;
1727 c->sid[0] = le32_to_cpu(buf[0]);
1728 rc = context_read_and_validate(&c->context[0], p, fp);
1729 if ( rc )
1730 goto bad;
1731 break;
1736 rc = next_entry(buf, fp, sizeof(u32));
1737 if ( rc < 0 )
1738 goto bad;
1739 nel = le32_to_cpu(buf[0]);
1741 if ( p->policyvers >= POLICYDB_VERSION_MLS )
1743 rc = next_entry(buf, fp, sizeof(u32));
1744 if ( rc < 0 )
1745 goto bad;
1746 nel = le32_to_cpu(buf[0]);
1747 lrt = NULL;
1748 for ( i = 0; i < nel; i++ )
1750 rt = xmalloc(struct range_trans);
1751 if ( !rt )
1753 rc = -ENOMEM;
1754 goto bad;
1756 memset(rt, 0, sizeof(*rt));
1757 if ( lrt )
1758 lrt->next = rt;
1759 else
1760 p->range_tr = rt;
1761 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1762 if ( rc < 0 )
1763 goto bad;
1764 rt->dom = le32_to_cpu(buf[0]);
1765 rt->type = le32_to_cpu(buf[1]);
1766 rc = mls_read_range_helper(&rt->range, fp);
1767 if ( rc )
1768 goto bad;
1769 lrt = rt;
1773 p->type_attr_map = xmalloc_array(struct ebitmap, p->p_types.nprim);
1774 if ( !p->type_attr_map )
1775 goto bad;
1777 for ( i = 0; i < p->p_types.nprim; i++ )
1779 ebitmap_init(&p->type_attr_map[i]);
1780 if ( p->policyvers >= POLICYDB_VERSION_AVTAB )
1782 if ( ebitmap_read(&p->type_attr_map[i], fp) )
1783 goto bad;
1785 /* add the type itself as the degenerate case */
1786 if ( ebitmap_set_bit(&p->type_attr_map[i], i, 1) )
1787 goto bad;
1790 rc = 0;
1791 out:
1792 return rc;
1793 bad:
1794 if ( !rc )
1795 rc = -EINVAL;
1796 policydb_destroy(p);
1797 goto out;