[PATCH] selinux: endian notations
[linux-ginger.git] / security / selinux / ss / policydb.c
blob0a758323a9cf61b10678d7308a87fc05316129cf
1 /*
2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
7 /*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
10 * Support for enhanced MLS infrastructure.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
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.
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include "security.h"
29 #include "policydb.h"
30 #include "conditional.h"
31 #include "mls.h"
33 #define _DEBUG_HASHES
35 #ifdef DEBUG_HASHES
36 static char *symtab_name[SYM_NUM] = {
37 "common prefixes",
38 "classes",
39 "roles",
40 "types",
41 "users",
42 "bools",
43 "levels",
44 "categories",
46 #endif
48 int selinux_mls_enabled = 0;
50 static unsigned int symtab_sizes[SYM_NUM] = {
52 32,
53 16,
54 512,
55 128,
56 16,
57 16,
58 16,
61 struct policydb_compat_info {
62 int version;
63 int sym_num;
64 int ocon_num;
67 /* These need to be updated if SYM_NUM or OCON_NUM changes */
68 static struct policydb_compat_info policydb_compat[] = {
70 .version = POLICYDB_VERSION_BASE,
71 .sym_num = SYM_NUM - 3,
72 .ocon_num = OCON_NUM - 1,
75 .version = POLICYDB_VERSION_BOOL,
76 .sym_num = SYM_NUM - 2,
77 .ocon_num = OCON_NUM - 1,
80 .version = POLICYDB_VERSION_IPV6,
81 .sym_num = SYM_NUM - 2,
82 .ocon_num = OCON_NUM,
85 .version = POLICYDB_VERSION_NLCLASS,
86 .sym_num = SYM_NUM - 2,
87 .ocon_num = OCON_NUM,
90 .version = POLICYDB_VERSION_MLS,
91 .sym_num = SYM_NUM,
92 .ocon_num = OCON_NUM,
95 .version = POLICYDB_VERSION_AVTAB,
96 .sym_num = SYM_NUM,
97 .ocon_num = OCON_NUM,
101 static struct policydb_compat_info *policydb_lookup_compat(int version)
103 int i;
104 struct policydb_compat_info *info = NULL;
106 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
107 if (policydb_compat[i].version == version) {
108 info = &policydb_compat[i];
109 break;
112 return info;
116 * Initialize the role table.
118 static int roles_init(struct policydb *p)
120 char *key = NULL;
121 int rc;
122 struct role_datum *role;
124 role = kmalloc(sizeof(*role), GFP_KERNEL);
125 if (!role) {
126 rc = -ENOMEM;
127 goto out;
129 memset(role, 0, sizeof(*role));
130 role->value = ++p->p_roles.nprim;
131 if (role->value != OBJECT_R_VAL) {
132 rc = -EINVAL;
133 goto out_free_role;
135 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
136 if (!key) {
137 rc = -ENOMEM;
138 goto out_free_role;
140 strcpy(key, OBJECT_R);
141 rc = hashtab_insert(p->p_roles.table, key, role);
142 if (rc)
143 goto out_free_key;
144 out:
145 return rc;
147 out_free_key:
148 kfree(key);
149 out_free_role:
150 kfree(role);
151 goto out;
155 * Initialize a policy database structure.
157 static int policydb_init(struct policydb *p)
159 int i, rc;
161 memset(p, 0, sizeof(*p));
163 for (i = 0; i < SYM_NUM; i++) {
164 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
165 if (rc)
166 goto out_free_symtab;
169 rc = avtab_init(&p->te_avtab);
170 if (rc)
171 goto out_free_symtab;
173 rc = roles_init(p);
174 if (rc)
175 goto out_free_avtab;
177 rc = cond_policydb_init(p);
178 if (rc)
179 goto out_free_avtab;
181 out:
182 return rc;
184 out_free_avtab:
185 avtab_destroy(&p->te_avtab);
187 out_free_symtab:
188 for (i = 0; i < SYM_NUM; i++)
189 hashtab_destroy(p->symtab[i].table);
190 goto out;
194 * The following *_index functions are used to
195 * define the val_to_name and val_to_struct arrays
196 * in a policy database structure. The val_to_name
197 * arrays are used when converting security context
198 * structures into string representations. The
199 * val_to_struct arrays are used when the attributes
200 * of a class, role, or user are needed.
203 static int common_index(void *key, void *datum, void *datap)
205 struct policydb *p;
206 struct common_datum *comdatum;
208 comdatum = datum;
209 p = datap;
210 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
211 return -EINVAL;
212 p->p_common_val_to_name[comdatum->value - 1] = key;
213 return 0;
216 static int class_index(void *key, void *datum, void *datap)
218 struct policydb *p;
219 struct class_datum *cladatum;
221 cladatum = datum;
222 p = datap;
223 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
224 return -EINVAL;
225 p->p_class_val_to_name[cladatum->value - 1] = key;
226 p->class_val_to_struct[cladatum->value - 1] = cladatum;
227 return 0;
230 static int role_index(void *key, void *datum, void *datap)
232 struct policydb *p;
233 struct role_datum *role;
235 role = datum;
236 p = datap;
237 if (!role->value || role->value > p->p_roles.nprim)
238 return -EINVAL;
239 p->p_role_val_to_name[role->value - 1] = key;
240 p->role_val_to_struct[role->value - 1] = role;
241 return 0;
244 static int type_index(void *key, void *datum, void *datap)
246 struct policydb *p;
247 struct type_datum *typdatum;
249 typdatum = datum;
250 p = datap;
252 if (typdatum->primary) {
253 if (!typdatum->value || typdatum->value > p->p_types.nprim)
254 return -EINVAL;
255 p->p_type_val_to_name[typdatum->value - 1] = key;
258 return 0;
261 static int user_index(void *key, void *datum, void *datap)
263 struct policydb *p;
264 struct user_datum *usrdatum;
266 usrdatum = datum;
267 p = datap;
268 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
269 return -EINVAL;
270 p->p_user_val_to_name[usrdatum->value - 1] = key;
271 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
272 return 0;
275 static int sens_index(void *key, void *datum, void *datap)
277 struct policydb *p;
278 struct level_datum *levdatum;
280 levdatum = datum;
281 p = datap;
283 if (!levdatum->isalias) {
284 if (!levdatum->level->sens ||
285 levdatum->level->sens > p->p_levels.nprim)
286 return -EINVAL;
287 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
290 return 0;
293 static int cat_index(void *key, void *datum, void *datap)
295 struct policydb *p;
296 struct cat_datum *catdatum;
298 catdatum = datum;
299 p = datap;
301 if (!catdatum->isalias) {
302 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
303 return -EINVAL;
304 p->p_cat_val_to_name[catdatum->value - 1] = key;
307 return 0;
310 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
312 common_index,
313 class_index,
314 role_index,
315 type_index,
316 user_index,
317 cond_index_bool,
318 sens_index,
319 cat_index,
323 * Define the common val_to_name array and the class
324 * val_to_name and val_to_struct arrays in a policy
325 * database structure.
327 * Caller must clean up upon failure.
329 static int policydb_index_classes(struct policydb *p)
331 int rc;
333 p->p_common_val_to_name =
334 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
335 if (!p->p_common_val_to_name) {
336 rc = -ENOMEM;
337 goto out;
340 rc = hashtab_map(p->p_commons.table, common_index, p);
341 if (rc)
342 goto out;
344 p->class_val_to_struct =
345 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
346 if (!p->class_val_to_struct) {
347 rc = -ENOMEM;
348 goto out;
351 p->p_class_val_to_name =
352 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
353 if (!p->p_class_val_to_name) {
354 rc = -ENOMEM;
355 goto out;
358 rc = hashtab_map(p->p_classes.table, class_index, p);
359 out:
360 return rc;
363 #ifdef DEBUG_HASHES
364 static void symtab_hash_eval(struct symtab *s)
366 int i;
368 for (i = 0; i < SYM_NUM; i++) {
369 struct hashtab *h = s[i].table;
370 struct hashtab_info info;
372 hashtab_stat(h, &info);
373 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
374 "longest chain length %d\n", symtab_name[i], h->nel,
375 info.slots_used, h->size, info.max_chain_len);
378 #endif
381 * Define the other val_to_name and val_to_struct arrays
382 * in a policy database structure.
384 * Caller must clean up on failure.
386 static int policydb_index_others(struct policydb *p)
388 int i, rc = 0;
390 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
391 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
392 if (selinux_mls_enabled)
393 printk(", %d sens, %d cats", p->p_levels.nprim,
394 p->p_cats.nprim);
395 printk("\n");
397 printk(KERN_INFO "security: %d classes, %d rules\n",
398 p->p_classes.nprim, p->te_avtab.nel);
400 #ifdef DEBUG_HASHES
401 avtab_hash_eval(&p->te_avtab, "rules");
402 symtab_hash_eval(p->symtab);
403 #endif
405 p->role_val_to_struct =
406 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
407 GFP_KERNEL);
408 if (!p->role_val_to_struct) {
409 rc = -ENOMEM;
410 goto out;
413 p->user_val_to_struct =
414 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
415 GFP_KERNEL);
416 if (!p->user_val_to_struct) {
417 rc = -ENOMEM;
418 goto out;
421 if (cond_init_bool_indexes(p)) {
422 rc = -ENOMEM;
423 goto out;
426 for (i = SYM_ROLES; i < SYM_NUM; i++) {
427 p->sym_val_to_name[i] =
428 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
429 if (!p->sym_val_to_name[i]) {
430 rc = -ENOMEM;
431 goto out;
433 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
434 if (rc)
435 goto out;
438 out:
439 return rc;
443 * The following *_destroy functions are used to
444 * free any memory allocated for each kind of
445 * symbol data in the policy database.
448 static int perm_destroy(void *key, void *datum, void *p)
450 kfree(key);
451 kfree(datum);
452 return 0;
455 static int common_destroy(void *key, void *datum, void *p)
457 struct common_datum *comdatum;
459 kfree(key);
460 comdatum = datum;
461 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
462 hashtab_destroy(comdatum->permissions.table);
463 kfree(datum);
464 return 0;
467 static int class_destroy(void *key, void *datum, void *p)
469 struct class_datum *cladatum;
470 struct constraint_node *constraint, *ctemp;
471 struct constraint_expr *e, *etmp;
473 kfree(key);
474 cladatum = datum;
475 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
476 hashtab_destroy(cladatum->permissions.table);
477 constraint = cladatum->constraints;
478 while (constraint) {
479 e = constraint->expr;
480 while (e) {
481 ebitmap_destroy(&e->names);
482 etmp = e;
483 e = e->next;
484 kfree(etmp);
486 ctemp = constraint;
487 constraint = constraint->next;
488 kfree(ctemp);
491 constraint = cladatum->validatetrans;
492 while (constraint) {
493 e = constraint->expr;
494 while (e) {
495 ebitmap_destroy(&e->names);
496 etmp = e;
497 e = e->next;
498 kfree(etmp);
500 ctemp = constraint;
501 constraint = constraint->next;
502 kfree(ctemp);
505 kfree(cladatum->comkey);
506 kfree(datum);
507 return 0;
510 static int role_destroy(void *key, void *datum, void *p)
512 struct role_datum *role;
514 kfree(key);
515 role = datum;
516 ebitmap_destroy(&role->dominates);
517 ebitmap_destroy(&role->types);
518 kfree(datum);
519 return 0;
522 static int type_destroy(void *key, void *datum, void *p)
524 kfree(key);
525 kfree(datum);
526 return 0;
529 static int user_destroy(void *key, void *datum, void *p)
531 struct user_datum *usrdatum;
533 kfree(key);
534 usrdatum = datum;
535 ebitmap_destroy(&usrdatum->roles);
536 ebitmap_destroy(&usrdatum->range.level[0].cat);
537 ebitmap_destroy(&usrdatum->range.level[1].cat);
538 ebitmap_destroy(&usrdatum->dfltlevel.cat);
539 kfree(datum);
540 return 0;
543 static int sens_destroy(void *key, void *datum, void *p)
545 struct level_datum *levdatum;
547 kfree(key);
548 levdatum = datum;
549 ebitmap_destroy(&levdatum->level->cat);
550 kfree(levdatum->level);
551 kfree(datum);
552 return 0;
555 static int cat_destroy(void *key, void *datum, void *p)
557 kfree(key);
558 kfree(datum);
559 return 0;
562 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
564 common_destroy,
565 class_destroy,
566 role_destroy,
567 type_destroy,
568 user_destroy,
569 cond_destroy_bool,
570 sens_destroy,
571 cat_destroy,
574 static void ocontext_destroy(struct ocontext *c, int i)
576 context_destroy(&c->context[0]);
577 context_destroy(&c->context[1]);
578 if (i == OCON_ISID || i == OCON_FS ||
579 i == OCON_NETIF || i == OCON_FSUSE)
580 kfree(c->u.name);
581 kfree(c);
585 * Free any memory allocated by a policy database structure.
587 void policydb_destroy(struct policydb *p)
589 struct ocontext *c, *ctmp;
590 struct genfs *g, *gtmp;
591 int i;
592 struct role_allow *ra, *lra = NULL;
593 struct role_trans *tr, *ltr = NULL;
594 struct range_trans *rt, *lrt = NULL;
596 for (i = 0; i < SYM_NUM; i++) {
597 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
598 hashtab_destroy(p->symtab[i].table);
601 for (i = 0; i < SYM_NUM; i++)
602 kfree(p->sym_val_to_name[i]);
604 kfree(p->class_val_to_struct);
605 kfree(p->role_val_to_struct);
606 kfree(p->user_val_to_struct);
608 avtab_destroy(&p->te_avtab);
610 for (i = 0; i < OCON_NUM; i++) {
611 c = p->ocontexts[i];
612 while (c) {
613 ctmp = c;
614 c = c->next;
615 ocontext_destroy(ctmp,i);
619 g = p->genfs;
620 while (g) {
621 kfree(g->fstype);
622 c = g->head;
623 while (c) {
624 ctmp = c;
625 c = c->next;
626 ocontext_destroy(ctmp,OCON_FSUSE);
628 gtmp = g;
629 g = g->next;
630 kfree(gtmp);
633 cond_policydb_destroy(p);
635 for (tr = p->role_tr; tr; tr = tr->next) {
636 if (ltr) kfree(ltr);
637 ltr = tr;
639 if (ltr) kfree(ltr);
641 for (ra = p->role_allow; ra; ra = ra -> next) {
642 if (lra) kfree(lra);
643 lra = ra;
645 if (lra) kfree(lra);
647 for (rt = p->range_tr; rt; rt = rt -> next) {
648 if (lrt) kfree(lrt);
649 lrt = rt;
651 if (lrt) kfree(lrt);
653 for (i = 0; i < p->p_types.nprim; i++)
654 ebitmap_destroy(&p->type_attr_map[i]);
655 kfree(p->type_attr_map);
657 return;
661 * Load the initial SIDs specified in a policy database
662 * structure into a SID table.
664 int policydb_load_isids(struct policydb *p, struct sidtab *s)
666 struct ocontext *head, *c;
667 int rc;
669 rc = sidtab_init(s);
670 if (rc) {
671 printk(KERN_ERR "security: out of memory on SID table init\n");
672 goto out;
675 head = p->ocontexts[OCON_ISID];
676 for (c = head; c; c = c->next) {
677 if (!c->context[0].user) {
678 printk(KERN_ERR "security: SID %s was never "
679 "defined.\n", c->u.name);
680 rc = -EINVAL;
681 goto out;
683 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
684 printk(KERN_ERR "security: unable to load initial "
685 "SID %s.\n", c->u.name);
686 rc = -EINVAL;
687 goto out;
690 out:
691 return rc;
695 * Return 1 if the fields in the security context
696 * structure `c' are valid. Return 0 otherwise.
698 int policydb_context_isvalid(struct policydb *p, struct context *c)
700 struct role_datum *role;
701 struct user_datum *usrdatum;
703 if (!c->role || c->role > p->p_roles.nprim)
704 return 0;
706 if (!c->user || c->user > p->p_users.nprim)
707 return 0;
709 if (!c->type || c->type > p->p_types.nprim)
710 return 0;
712 if (c->role != OBJECT_R_VAL) {
714 * Role must be authorized for the type.
716 role = p->role_val_to_struct[c->role - 1];
717 if (!ebitmap_get_bit(&role->types,
718 c->type - 1))
719 /* role may not be associated with type */
720 return 0;
723 * User must be authorized for the role.
725 usrdatum = p->user_val_to_struct[c->user - 1];
726 if (!usrdatum)
727 return 0;
729 if (!ebitmap_get_bit(&usrdatum->roles,
730 c->role - 1))
731 /* user may not be associated with role */
732 return 0;
735 if (!mls_context_isvalid(p, c))
736 return 0;
738 return 1;
742 * Read a MLS range structure from a policydb binary
743 * representation file.
745 static int mls_read_range_helper(struct mls_range *r, void *fp)
747 __le32 buf[2];
748 u32 items;
749 int rc;
751 rc = next_entry(buf, fp, sizeof(u32));
752 if (rc < 0)
753 goto out;
755 items = le32_to_cpu(buf[0]);
756 if (items > ARRAY_SIZE(buf)) {
757 printk(KERN_ERR "security: mls: range overflow\n");
758 rc = -EINVAL;
759 goto out;
761 rc = next_entry(buf, fp, sizeof(u32) * items);
762 if (rc < 0) {
763 printk(KERN_ERR "security: mls: truncated range\n");
764 goto out;
766 r->level[0].sens = le32_to_cpu(buf[0]);
767 if (items > 1)
768 r->level[1].sens = le32_to_cpu(buf[1]);
769 else
770 r->level[1].sens = r->level[0].sens;
772 rc = ebitmap_read(&r->level[0].cat, fp);
773 if (rc) {
774 printk(KERN_ERR "security: mls: error reading low "
775 "categories\n");
776 goto out;
778 if (items > 1) {
779 rc = ebitmap_read(&r->level[1].cat, fp);
780 if (rc) {
781 printk(KERN_ERR "security: mls: error reading high "
782 "categories\n");
783 goto bad_high;
785 } else {
786 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
787 if (rc) {
788 printk(KERN_ERR "security: mls: out of memory\n");
789 goto bad_high;
793 rc = 0;
794 out:
795 return rc;
796 bad_high:
797 ebitmap_destroy(&r->level[0].cat);
798 goto out;
802 * Read and validate a security context structure
803 * from a policydb binary representation file.
805 static int context_read_and_validate(struct context *c,
806 struct policydb *p,
807 void *fp)
809 __le32 buf[3];
810 int rc;
812 rc = next_entry(buf, fp, sizeof buf);
813 if (rc < 0) {
814 printk(KERN_ERR "security: context truncated\n");
815 goto out;
817 c->user = le32_to_cpu(buf[0]);
818 c->role = le32_to_cpu(buf[1]);
819 c->type = le32_to_cpu(buf[2]);
820 if (p->policyvers >= POLICYDB_VERSION_MLS) {
821 if (mls_read_range_helper(&c->range, fp)) {
822 printk(KERN_ERR "security: error reading MLS range of "
823 "context\n");
824 rc = -EINVAL;
825 goto out;
829 if (!policydb_context_isvalid(p, c)) {
830 printk(KERN_ERR "security: invalid security context\n");
831 context_destroy(c);
832 rc = -EINVAL;
834 out:
835 return rc;
839 * The following *_read functions are used to
840 * read the symbol data from a policy database
841 * binary representation file.
844 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
846 char *key = NULL;
847 struct perm_datum *perdatum;
848 int rc;
849 __le32 buf[2];
850 u32 len;
852 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
853 if (!perdatum) {
854 rc = -ENOMEM;
855 goto out;
857 memset(perdatum, 0, sizeof(*perdatum));
859 rc = next_entry(buf, fp, sizeof buf);
860 if (rc < 0)
861 goto bad;
863 len = le32_to_cpu(buf[0]);
864 perdatum->value = le32_to_cpu(buf[1]);
866 key = kmalloc(len + 1,GFP_KERNEL);
867 if (!key) {
868 rc = -ENOMEM;
869 goto bad;
871 rc = next_entry(key, fp, len);
872 if (rc < 0)
873 goto bad;
874 key[len] = 0;
876 rc = hashtab_insert(h, key, perdatum);
877 if (rc)
878 goto bad;
879 out:
880 return rc;
881 bad:
882 perm_destroy(key, perdatum, NULL);
883 goto out;
886 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
888 char *key = NULL;
889 struct common_datum *comdatum;
890 __le32 buf[4];
891 u32 len, nel;
892 int i, rc;
894 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
895 if (!comdatum) {
896 rc = -ENOMEM;
897 goto out;
899 memset(comdatum, 0, sizeof(*comdatum));
901 rc = next_entry(buf, fp, sizeof buf);
902 if (rc < 0)
903 goto bad;
905 len = le32_to_cpu(buf[0]);
906 comdatum->value = le32_to_cpu(buf[1]);
908 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
909 if (rc)
910 goto bad;
911 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
912 nel = le32_to_cpu(buf[3]);
914 key = kmalloc(len + 1,GFP_KERNEL);
915 if (!key) {
916 rc = -ENOMEM;
917 goto bad;
919 rc = next_entry(key, fp, len);
920 if (rc < 0)
921 goto bad;
922 key[len] = 0;
924 for (i = 0; i < nel; i++) {
925 rc = perm_read(p, comdatum->permissions.table, fp);
926 if (rc)
927 goto bad;
930 rc = hashtab_insert(h, key, comdatum);
931 if (rc)
932 goto bad;
933 out:
934 return rc;
935 bad:
936 common_destroy(key, comdatum, NULL);
937 goto out;
940 static int read_cons_helper(struct constraint_node **nodep, int ncons,
941 int allowxtarget, void *fp)
943 struct constraint_node *c, *lc;
944 struct constraint_expr *e, *le;
945 __le32 buf[3];
946 u32 nexpr;
947 int rc, i, j, depth;
949 lc = NULL;
950 for (i = 0; i < ncons; i++) {
951 c = kmalloc(sizeof(*c), GFP_KERNEL);
952 if (!c)
953 return -ENOMEM;
954 memset(c, 0, sizeof(*c));
956 if (lc) {
957 lc->next = c;
958 } else {
959 *nodep = c;
962 rc = next_entry(buf, fp, (sizeof(u32) * 2));
963 if (rc < 0)
964 return rc;
965 c->permissions = le32_to_cpu(buf[0]);
966 nexpr = le32_to_cpu(buf[1]);
967 le = NULL;
968 depth = -1;
969 for (j = 0; j < nexpr; j++) {
970 e = kmalloc(sizeof(*e), GFP_KERNEL);
971 if (!e)
972 return -ENOMEM;
973 memset(e, 0, sizeof(*e));
975 if (le) {
976 le->next = e;
977 } else {
978 c->expr = e;
981 rc = next_entry(buf, fp, (sizeof(u32) * 3));
982 if (rc < 0)
983 return rc;
984 e->expr_type = le32_to_cpu(buf[0]);
985 e->attr = le32_to_cpu(buf[1]);
986 e->op = le32_to_cpu(buf[2]);
988 switch (e->expr_type) {
989 case CEXPR_NOT:
990 if (depth < 0)
991 return -EINVAL;
992 break;
993 case CEXPR_AND:
994 case CEXPR_OR:
995 if (depth < 1)
996 return -EINVAL;
997 depth--;
998 break;
999 case CEXPR_ATTR:
1000 if (depth == (CEXPR_MAXDEPTH - 1))
1001 return -EINVAL;
1002 depth++;
1003 break;
1004 case CEXPR_NAMES:
1005 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1006 return -EINVAL;
1007 if (depth == (CEXPR_MAXDEPTH - 1))
1008 return -EINVAL;
1009 depth++;
1010 if (ebitmap_read(&e->names, fp))
1011 return -EINVAL;
1012 break;
1013 default:
1014 return -EINVAL;
1016 le = e;
1018 if (depth != 0)
1019 return -EINVAL;
1020 lc = c;
1023 return 0;
1026 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1028 char *key = NULL;
1029 struct class_datum *cladatum;
1030 __le32 buf[6];
1031 u32 len, len2, ncons, nel;
1032 int i, rc;
1034 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1035 if (!cladatum) {
1036 rc = -ENOMEM;
1037 goto out;
1039 memset(cladatum, 0, sizeof(*cladatum));
1041 rc = next_entry(buf, fp, sizeof(u32)*6);
1042 if (rc < 0)
1043 goto bad;
1045 len = le32_to_cpu(buf[0]);
1046 len2 = le32_to_cpu(buf[1]);
1047 cladatum->value = le32_to_cpu(buf[2]);
1049 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1050 if (rc)
1051 goto bad;
1052 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1053 nel = le32_to_cpu(buf[4]);
1055 ncons = le32_to_cpu(buf[5]);
1057 key = kmalloc(len + 1,GFP_KERNEL);
1058 if (!key) {
1059 rc = -ENOMEM;
1060 goto bad;
1062 rc = next_entry(key, fp, len);
1063 if (rc < 0)
1064 goto bad;
1065 key[len] = 0;
1067 if (len2) {
1068 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1069 if (!cladatum->comkey) {
1070 rc = -ENOMEM;
1071 goto bad;
1073 rc = next_entry(cladatum->comkey, fp, len2);
1074 if (rc < 0)
1075 goto bad;
1076 cladatum->comkey[len2] = 0;
1078 cladatum->comdatum = hashtab_search(p->p_commons.table,
1079 cladatum->comkey);
1080 if (!cladatum->comdatum) {
1081 printk(KERN_ERR "security: unknown common %s\n",
1082 cladatum->comkey);
1083 rc = -EINVAL;
1084 goto bad;
1087 for (i = 0; i < nel; i++) {
1088 rc = perm_read(p, cladatum->permissions.table, fp);
1089 if (rc)
1090 goto bad;
1093 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1094 if (rc)
1095 goto bad;
1097 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1098 /* grab the validatetrans rules */
1099 rc = next_entry(buf, fp, sizeof(u32));
1100 if (rc < 0)
1101 goto bad;
1102 ncons = le32_to_cpu(buf[0]);
1103 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1104 if (rc)
1105 goto bad;
1108 rc = hashtab_insert(h, key, cladatum);
1109 if (rc)
1110 goto bad;
1112 rc = 0;
1113 out:
1114 return rc;
1115 bad:
1116 class_destroy(key, cladatum, NULL);
1117 goto out;
1120 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1122 char *key = NULL;
1123 struct role_datum *role;
1124 int rc;
1125 __le32 buf[2];
1126 u32 len;
1128 role = kmalloc(sizeof(*role), GFP_KERNEL);
1129 if (!role) {
1130 rc = -ENOMEM;
1131 goto out;
1133 memset(role, 0, sizeof(*role));
1135 rc = next_entry(buf, fp, sizeof buf);
1136 if (rc < 0)
1137 goto bad;
1139 len = le32_to_cpu(buf[0]);
1140 role->value = le32_to_cpu(buf[1]);
1142 key = kmalloc(len + 1,GFP_KERNEL);
1143 if (!key) {
1144 rc = -ENOMEM;
1145 goto bad;
1147 rc = next_entry(key, fp, len);
1148 if (rc < 0)
1149 goto bad;
1150 key[len] = 0;
1152 rc = ebitmap_read(&role->dominates, fp);
1153 if (rc)
1154 goto bad;
1156 rc = ebitmap_read(&role->types, fp);
1157 if (rc)
1158 goto bad;
1160 if (strcmp(key, OBJECT_R) == 0) {
1161 if (role->value != OBJECT_R_VAL) {
1162 printk(KERN_ERR "Role %s has wrong value %d\n",
1163 OBJECT_R, role->value);
1164 rc = -EINVAL;
1165 goto bad;
1167 rc = 0;
1168 goto bad;
1171 rc = hashtab_insert(h, key, role);
1172 if (rc)
1173 goto bad;
1174 out:
1175 return rc;
1176 bad:
1177 role_destroy(key, role, NULL);
1178 goto out;
1181 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1183 char *key = NULL;
1184 struct type_datum *typdatum;
1185 int rc;
1186 __le32 buf[3];
1187 u32 len;
1189 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1190 if (!typdatum) {
1191 rc = -ENOMEM;
1192 return rc;
1194 memset(typdatum, 0, sizeof(*typdatum));
1196 rc = next_entry(buf, fp, sizeof buf);
1197 if (rc < 0)
1198 goto bad;
1200 len = le32_to_cpu(buf[0]);
1201 typdatum->value = le32_to_cpu(buf[1]);
1202 typdatum->primary = le32_to_cpu(buf[2]);
1204 key = kmalloc(len + 1,GFP_KERNEL);
1205 if (!key) {
1206 rc = -ENOMEM;
1207 goto bad;
1209 rc = next_entry(key, fp, len);
1210 if (rc < 0)
1211 goto bad;
1212 key[len] = 0;
1214 rc = hashtab_insert(h, key, typdatum);
1215 if (rc)
1216 goto bad;
1217 out:
1218 return rc;
1219 bad:
1220 type_destroy(key, typdatum, NULL);
1221 goto out;
1226 * Read a MLS level structure from a policydb binary
1227 * representation file.
1229 static int mls_read_level(struct mls_level *lp, void *fp)
1231 __le32 buf[1];
1232 int rc;
1234 memset(lp, 0, sizeof(*lp));
1236 rc = next_entry(buf, fp, sizeof buf);
1237 if (rc < 0) {
1238 printk(KERN_ERR "security: mls: truncated level\n");
1239 goto bad;
1241 lp->sens = le32_to_cpu(buf[0]);
1243 if (ebitmap_read(&lp->cat, fp)) {
1244 printk(KERN_ERR "security: mls: error reading level "
1245 "categories\n");
1246 goto bad;
1248 return 0;
1250 bad:
1251 return -EINVAL;
1254 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1256 char *key = NULL;
1257 struct user_datum *usrdatum;
1258 int rc;
1259 __le32 buf[2];
1260 u32 len;
1262 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1263 if (!usrdatum) {
1264 rc = -ENOMEM;
1265 goto out;
1267 memset(usrdatum, 0, sizeof(*usrdatum));
1269 rc = next_entry(buf, fp, sizeof buf);
1270 if (rc < 0)
1271 goto bad;
1273 len = le32_to_cpu(buf[0]);
1274 usrdatum->value = le32_to_cpu(buf[1]);
1276 key = kmalloc(len + 1,GFP_KERNEL);
1277 if (!key) {
1278 rc = -ENOMEM;
1279 goto bad;
1281 rc = next_entry(key, fp, len);
1282 if (rc < 0)
1283 goto bad;
1284 key[len] = 0;
1286 rc = ebitmap_read(&usrdatum->roles, fp);
1287 if (rc)
1288 goto bad;
1290 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1291 rc = mls_read_range_helper(&usrdatum->range, fp);
1292 if (rc)
1293 goto bad;
1294 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1295 if (rc)
1296 goto bad;
1299 rc = hashtab_insert(h, key, usrdatum);
1300 if (rc)
1301 goto bad;
1302 out:
1303 return rc;
1304 bad:
1305 user_destroy(key, usrdatum, NULL);
1306 goto out;
1309 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1311 char *key = NULL;
1312 struct level_datum *levdatum;
1313 int rc;
1314 __le32 buf[2];
1315 u32 len;
1317 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1318 if (!levdatum) {
1319 rc = -ENOMEM;
1320 goto out;
1322 memset(levdatum, 0, sizeof(*levdatum));
1324 rc = next_entry(buf, fp, sizeof buf);
1325 if (rc < 0)
1326 goto bad;
1328 len = le32_to_cpu(buf[0]);
1329 levdatum->isalias = le32_to_cpu(buf[1]);
1331 key = kmalloc(len + 1,GFP_ATOMIC);
1332 if (!key) {
1333 rc = -ENOMEM;
1334 goto bad;
1336 rc = next_entry(key, fp, len);
1337 if (rc < 0)
1338 goto bad;
1339 key[len] = 0;
1341 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1342 if (!levdatum->level) {
1343 rc = -ENOMEM;
1344 goto bad;
1346 if (mls_read_level(levdatum->level, fp)) {
1347 rc = -EINVAL;
1348 goto bad;
1351 rc = hashtab_insert(h, key, levdatum);
1352 if (rc)
1353 goto bad;
1354 out:
1355 return rc;
1356 bad:
1357 sens_destroy(key, levdatum, NULL);
1358 goto out;
1361 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1363 char *key = NULL;
1364 struct cat_datum *catdatum;
1365 int rc;
1366 __le32 buf[3];
1367 u32 len;
1369 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1370 if (!catdatum) {
1371 rc = -ENOMEM;
1372 goto out;
1374 memset(catdatum, 0, sizeof(*catdatum));
1376 rc = next_entry(buf, fp, sizeof buf);
1377 if (rc < 0)
1378 goto bad;
1380 len = le32_to_cpu(buf[0]);
1381 catdatum->value = le32_to_cpu(buf[1]);
1382 catdatum->isalias = le32_to_cpu(buf[2]);
1384 key = kmalloc(len + 1,GFP_ATOMIC);
1385 if (!key) {
1386 rc = -ENOMEM;
1387 goto bad;
1389 rc = next_entry(key, fp, len);
1390 if (rc < 0)
1391 goto bad;
1392 key[len] = 0;
1394 rc = hashtab_insert(h, key, catdatum);
1395 if (rc)
1396 goto bad;
1397 out:
1398 return rc;
1400 bad:
1401 cat_destroy(key, catdatum, NULL);
1402 goto out;
1405 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1407 common_read,
1408 class_read,
1409 role_read,
1410 type_read,
1411 user_read,
1412 cond_read_bool,
1413 sens_read,
1414 cat_read,
1417 extern int ss_initialized;
1420 * Read the configuration data from a policy database binary
1421 * representation file into a policy database structure.
1423 int policydb_read(struct policydb *p, void *fp)
1425 struct role_allow *ra, *lra;
1426 struct role_trans *tr, *ltr;
1427 struct ocontext *l, *c, *newc;
1428 struct genfs *genfs_p, *genfs, *newgenfs;
1429 int i, j, rc;
1430 __le32 buf[8];
1431 u32 len, len2, config, nprim, nel, nel2;
1432 char *policydb_str;
1433 struct policydb_compat_info *info;
1434 struct range_trans *rt, *lrt;
1436 config = 0;
1438 rc = policydb_init(p);
1439 if (rc)
1440 goto out;
1442 /* Read the magic number and string length. */
1443 rc = next_entry(buf, fp, sizeof(u32)* 2);
1444 if (rc < 0)
1445 goto bad;
1447 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1448 printk(KERN_ERR "security: policydb magic number 0x%x does "
1449 "not match expected magic number 0x%x\n",
1450 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1451 goto bad;
1454 len = le32_to_cpu(buf[1]);
1455 if (len != strlen(POLICYDB_STRING)) {
1456 printk(KERN_ERR "security: policydb string length %d does not "
1457 "match expected length %Zu\n",
1458 len, strlen(POLICYDB_STRING));
1459 goto bad;
1461 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1462 if (!policydb_str) {
1463 printk(KERN_ERR "security: unable to allocate memory for policydb "
1464 "string of length %d\n", len);
1465 rc = -ENOMEM;
1466 goto bad;
1468 rc = next_entry(policydb_str, fp, len);
1469 if (rc < 0) {
1470 printk(KERN_ERR "security: truncated policydb string identifier\n");
1471 kfree(policydb_str);
1472 goto bad;
1474 policydb_str[len] = 0;
1475 if (strcmp(policydb_str, POLICYDB_STRING)) {
1476 printk(KERN_ERR "security: policydb string %s does not match "
1477 "my string %s\n", policydb_str, POLICYDB_STRING);
1478 kfree(policydb_str);
1479 goto bad;
1481 /* Done with policydb_str. */
1482 kfree(policydb_str);
1483 policydb_str = NULL;
1485 /* Read the version, config, and table sizes. */
1486 rc = next_entry(buf, fp, sizeof(u32)*4);
1487 if (rc < 0)
1488 goto bad;
1490 p->policyvers = le32_to_cpu(buf[0]);
1491 if (p->policyvers < POLICYDB_VERSION_MIN ||
1492 p->policyvers > POLICYDB_VERSION_MAX) {
1493 printk(KERN_ERR "security: policydb version %d does not match "
1494 "my version range %d-%d\n",
1495 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1496 goto bad;
1499 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1500 if (ss_initialized && !selinux_mls_enabled) {
1501 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1502 "policies\n");
1503 goto bad;
1505 selinux_mls_enabled = 1;
1506 config |= POLICYDB_CONFIG_MLS;
1508 if (p->policyvers < POLICYDB_VERSION_MLS) {
1509 printk(KERN_ERR "security policydb version %d (MLS) "
1510 "not backwards compatible\n", p->policyvers);
1511 goto bad;
1513 } else {
1514 if (ss_initialized && selinux_mls_enabled) {
1515 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1516 "policies\n");
1517 goto bad;
1521 info = policydb_lookup_compat(p->policyvers);
1522 if (!info) {
1523 printk(KERN_ERR "security: unable to find policy compat info "
1524 "for version %d\n", p->policyvers);
1525 goto bad;
1528 if (le32_to_cpu(buf[2]) != info->sym_num ||
1529 le32_to_cpu(buf[3]) != info->ocon_num) {
1530 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1531 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1532 le32_to_cpu(buf[3]),
1533 info->sym_num, info->ocon_num);
1534 goto bad;
1537 for (i = 0; i < info->sym_num; i++) {
1538 rc = next_entry(buf, fp, sizeof(u32)*2);
1539 if (rc < 0)
1540 goto bad;
1541 nprim = le32_to_cpu(buf[0]);
1542 nel = le32_to_cpu(buf[1]);
1543 for (j = 0; j < nel; j++) {
1544 rc = read_f[i](p, p->symtab[i].table, fp);
1545 if (rc)
1546 goto bad;
1549 p->symtab[i].nprim = nprim;
1552 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1553 if (rc)
1554 goto bad;
1556 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1557 rc = cond_read_list(p, fp);
1558 if (rc)
1559 goto bad;
1562 rc = next_entry(buf, fp, sizeof(u32));
1563 if (rc < 0)
1564 goto bad;
1565 nel = le32_to_cpu(buf[0]);
1566 ltr = NULL;
1567 for (i = 0; i < nel; i++) {
1568 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1569 if (!tr) {
1570 rc = -ENOMEM;
1571 goto bad;
1573 memset(tr, 0, sizeof(*tr));
1574 if (ltr) {
1575 ltr->next = tr;
1576 } else {
1577 p->role_tr = tr;
1579 rc = next_entry(buf, fp, sizeof(u32)*3);
1580 if (rc < 0)
1581 goto bad;
1582 tr->role = le32_to_cpu(buf[0]);
1583 tr->type = le32_to_cpu(buf[1]);
1584 tr->new_role = le32_to_cpu(buf[2]);
1585 ltr = tr;
1588 rc = next_entry(buf, fp, sizeof(u32));
1589 if (rc < 0)
1590 goto bad;
1591 nel = le32_to_cpu(buf[0]);
1592 lra = NULL;
1593 for (i = 0; i < nel; i++) {
1594 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1595 if (!ra) {
1596 rc = -ENOMEM;
1597 goto bad;
1599 memset(ra, 0, sizeof(*ra));
1600 if (lra) {
1601 lra->next = ra;
1602 } else {
1603 p->role_allow = ra;
1605 rc = next_entry(buf, fp, sizeof(u32)*2);
1606 if (rc < 0)
1607 goto bad;
1608 ra->role = le32_to_cpu(buf[0]);
1609 ra->new_role = le32_to_cpu(buf[1]);
1610 lra = ra;
1613 rc = policydb_index_classes(p);
1614 if (rc)
1615 goto bad;
1617 rc = policydb_index_others(p);
1618 if (rc)
1619 goto bad;
1621 for (i = 0; i < info->ocon_num; i++) {
1622 rc = next_entry(buf, fp, sizeof(u32));
1623 if (rc < 0)
1624 goto bad;
1625 nel = le32_to_cpu(buf[0]);
1626 l = NULL;
1627 for (j = 0; j < nel; j++) {
1628 c = kmalloc(sizeof(*c), GFP_KERNEL);
1629 if (!c) {
1630 rc = -ENOMEM;
1631 goto bad;
1633 memset(c, 0, sizeof(*c));
1634 if (l) {
1635 l->next = c;
1636 } else {
1637 p->ocontexts[i] = c;
1639 l = c;
1640 rc = -EINVAL;
1641 switch (i) {
1642 case OCON_ISID:
1643 rc = next_entry(buf, fp, sizeof(u32));
1644 if (rc < 0)
1645 goto bad;
1646 c->sid[0] = le32_to_cpu(buf[0]);
1647 rc = context_read_and_validate(&c->context[0], p, fp);
1648 if (rc)
1649 goto bad;
1650 break;
1651 case OCON_FS:
1652 case OCON_NETIF:
1653 rc = next_entry(buf, fp, sizeof(u32));
1654 if (rc < 0)
1655 goto bad;
1656 len = le32_to_cpu(buf[0]);
1657 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1658 if (!c->u.name) {
1659 rc = -ENOMEM;
1660 goto bad;
1662 rc = next_entry(c->u.name, fp, len);
1663 if (rc < 0)
1664 goto bad;
1665 c->u.name[len] = 0;
1666 rc = context_read_and_validate(&c->context[0], p, fp);
1667 if (rc)
1668 goto bad;
1669 rc = context_read_and_validate(&c->context[1], p, fp);
1670 if (rc)
1671 goto bad;
1672 break;
1673 case OCON_PORT:
1674 rc = next_entry(buf, fp, sizeof(u32)*3);
1675 if (rc < 0)
1676 goto bad;
1677 c->u.port.protocol = le32_to_cpu(buf[0]);
1678 c->u.port.low_port = le32_to_cpu(buf[1]);
1679 c->u.port.high_port = le32_to_cpu(buf[2]);
1680 rc = context_read_and_validate(&c->context[0], p, fp);
1681 if (rc)
1682 goto bad;
1683 break;
1684 case OCON_NODE:
1685 rc = next_entry(buf, fp, sizeof(u32)* 2);
1686 if (rc < 0)
1687 goto bad;
1688 c->u.node.addr = le32_to_cpu(buf[0]);
1689 c->u.node.mask = le32_to_cpu(buf[1]);
1690 rc = context_read_and_validate(&c->context[0], p, fp);
1691 if (rc)
1692 goto bad;
1693 break;
1694 case OCON_FSUSE:
1695 rc = next_entry(buf, fp, sizeof(u32)*2);
1696 if (rc < 0)
1697 goto bad;
1698 c->v.behavior = le32_to_cpu(buf[0]);
1699 if (c->v.behavior > SECURITY_FS_USE_NONE)
1700 goto bad;
1701 len = le32_to_cpu(buf[1]);
1702 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1703 if (!c->u.name) {
1704 rc = -ENOMEM;
1705 goto bad;
1707 rc = next_entry(c->u.name, fp, len);
1708 if (rc < 0)
1709 goto bad;
1710 c->u.name[len] = 0;
1711 rc = context_read_and_validate(&c->context[0], p, fp);
1712 if (rc)
1713 goto bad;
1714 break;
1715 case OCON_NODE6: {
1716 int k;
1718 rc = next_entry(buf, fp, sizeof(u32) * 8);
1719 if (rc < 0)
1720 goto bad;
1721 for (k = 0; k < 4; k++)
1722 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1723 for (k = 0; k < 4; k++)
1724 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1725 if (context_read_and_validate(&c->context[0], p, fp))
1726 goto bad;
1727 break;
1733 rc = next_entry(buf, fp, sizeof(u32));
1734 if (rc < 0)
1735 goto bad;
1736 nel = le32_to_cpu(buf[0]);
1737 genfs_p = NULL;
1738 rc = -EINVAL;
1739 for (i = 0; i < nel; i++) {
1740 rc = next_entry(buf, fp, sizeof(u32));
1741 if (rc < 0)
1742 goto bad;
1743 len = le32_to_cpu(buf[0]);
1744 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1745 if (!newgenfs) {
1746 rc = -ENOMEM;
1747 goto bad;
1749 memset(newgenfs, 0, sizeof(*newgenfs));
1751 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1752 if (!newgenfs->fstype) {
1753 rc = -ENOMEM;
1754 kfree(newgenfs);
1755 goto bad;
1757 rc = next_entry(newgenfs->fstype, fp, len);
1758 if (rc < 0) {
1759 kfree(newgenfs->fstype);
1760 kfree(newgenfs);
1761 goto bad;
1763 newgenfs->fstype[len] = 0;
1764 for (genfs_p = NULL, genfs = p->genfs; genfs;
1765 genfs_p = genfs, genfs = genfs->next) {
1766 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1767 printk(KERN_ERR "security: dup genfs "
1768 "fstype %s\n", newgenfs->fstype);
1769 kfree(newgenfs->fstype);
1770 kfree(newgenfs);
1771 goto bad;
1773 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1774 break;
1776 newgenfs->next = genfs;
1777 if (genfs_p)
1778 genfs_p->next = newgenfs;
1779 else
1780 p->genfs = newgenfs;
1781 rc = next_entry(buf, fp, sizeof(u32));
1782 if (rc < 0)
1783 goto bad;
1784 nel2 = le32_to_cpu(buf[0]);
1785 for (j = 0; j < nel2; j++) {
1786 rc = next_entry(buf, fp, sizeof(u32));
1787 if (rc < 0)
1788 goto bad;
1789 len = le32_to_cpu(buf[0]);
1791 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1792 if (!newc) {
1793 rc = -ENOMEM;
1794 goto bad;
1796 memset(newc, 0, sizeof(*newc));
1798 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1799 if (!newc->u.name) {
1800 rc = -ENOMEM;
1801 goto bad_newc;
1803 rc = next_entry(newc->u.name, fp, len);
1804 if (rc < 0)
1805 goto bad_newc;
1806 newc->u.name[len] = 0;
1807 rc = next_entry(buf, fp, sizeof(u32));
1808 if (rc < 0)
1809 goto bad_newc;
1810 newc->v.sclass = le32_to_cpu(buf[0]);
1811 if (context_read_and_validate(&newc->context[0], p, fp))
1812 goto bad_newc;
1813 for (l = NULL, c = newgenfs->head; c;
1814 l = c, c = c->next) {
1815 if (!strcmp(newc->u.name, c->u.name) &&
1816 (!c->v.sclass || !newc->v.sclass ||
1817 newc->v.sclass == c->v.sclass)) {
1818 printk(KERN_ERR "security: dup genfs "
1819 "entry (%s,%s)\n",
1820 newgenfs->fstype, c->u.name);
1821 goto bad_newc;
1823 len = strlen(newc->u.name);
1824 len2 = strlen(c->u.name);
1825 if (len > len2)
1826 break;
1829 newc->next = c;
1830 if (l)
1831 l->next = newc;
1832 else
1833 newgenfs->head = newc;
1837 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1838 rc = next_entry(buf, fp, sizeof(u32));
1839 if (rc < 0)
1840 goto bad;
1841 nel = le32_to_cpu(buf[0]);
1842 lrt = NULL;
1843 for (i = 0; i < nel; i++) {
1844 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1845 if (!rt) {
1846 rc = -ENOMEM;
1847 goto bad;
1849 memset(rt, 0, sizeof(*rt));
1850 if (lrt)
1851 lrt->next = rt;
1852 else
1853 p->range_tr = rt;
1854 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1855 if (rc < 0)
1856 goto bad;
1857 rt->dom = le32_to_cpu(buf[0]);
1858 rt->type = le32_to_cpu(buf[1]);
1859 rc = mls_read_range_helper(&rt->range, fp);
1860 if (rc)
1861 goto bad;
1862 lrt = rt;
1866 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1867 if (!p->type_attr_map)
1868 goto bad;
1870 for (i = 0; i < p->p_types.nprim; i++) {
1871 ebitmap_init(&p->type_attr_map[i]);
1872 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1873 if (ebitmap_read(&p->type_attr_map[i], fp))
1874 goto bad;
1876 /* add the type itself as the degenerate case */
1877 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1878 goto bad;
1881 rc = 0;
1882 out:
1883 return rc;
1884 bad_newc:
1885 ocontext_destroy(newc,OCON_FSUSE);
1886 bad:
1887 if (!rc)
1888 rc = -EINVAL;
1889 policydb_destroy(p);
1890 goto out;