2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
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>
30 #include "conditional.h"
36 static char *symtab_name[SYM_NUM] = {
48 int selinux_mls_enabled = 0;
50 static unsigned int symtab_sizes[SYM_NUM] = {
61 struct policydb_compat_info {
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,
85 .version = POLICYDB_VERSION_NLCLASS,
86 .sym_num = SYM_NUM - 2,
90 .version = POLICYDB_VERSION_MLS,
96 static struct policydb_compat_info *policydb_lookup_compat(int version)
99 struct policydb_compat_info *info = NULL;
101 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
102 if (policydb_compat[i].version == version) {
103 info = &policydb_compat[i];
111 * Initialize the role table.
113 static int roles_init(struct policydb *p)
117 struct role_datum *role;
119 role = kmalloc(sizeof(*role), GFP_KERNEL);
124 memset(role, 0, sizeof(*role));
125 role->value = ++p->p_roles.nprim;
126 if (role->value != OBJECT_R_VAL) {
130 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
135 strcpy(key, OBJECT_R);
136 rc = hashtab_insert(p->p_roles.table, key, role);
150 * Initialize a policy database structure.
152 static int policydb_init(struct policydb *p)
156 memset(p, 0, sizeof(*p));
158 for (i = 0; i < SYM_NUM; i++) {
159 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
161 goto out_free_symtab;
164 rc = avtab_init(&p->te_avtab);
166 goto out_free_symtab;
172 rc = cond_policydb_init(p);
180 avtab_destroy(&p->te_avtab);
183 for (i = 0; i < SYM_NUM; i++)
184 hashtab_destroy(p->symtab[i].table);
189 * The following *_index functions are used to
190 * define the val_to_name and val_to_struct arrays
191 * in a policy database structure. The val_to_name
192 * arrays are used when converting security context
193 * structures into string representations. The
194 * val_to_struct arrays are used when the attributes
195 * of a class, role, or user are needed.
198 static int common_index(void *key, void *datum, void *datap)
201 struct common_datum *comdatum;
205 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
207 p->p_common_val_to_name[comdatum->value - 1] = key;
211 static int class_index(void *key, void *datum, void *datap)
214 struct class_datum *cladatum;
218 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
220 p->p_class_val_to_name[cladatum->value - 1] = key;
221 p->class_val_to_struct[cladatum->value - 1] = cladatum;
225 static int role_index(void *key, void *datum, void *datap)
228 struct role_datum *role;
232 if (!role->value || role->value > p->p_roles.nprim)
234 p->p_role_val_to_name[role->value - 1] = key;
235 p->role_val_to_struct[role->value - 1] = role;
239 static int type_index(void *key, void *datum, void *datap)
242 struct type_datum *typdatum;
247 if (typdatum->primary) {
248 if (!typdatum->value || typdatum->value > p->p_types.nprim)
250 p->p_type_val_to_name[typdatum->value - 1] = key;
256 static int user_index(void *key, void *datum, void *datap)
259 struct user_datum *usrdatum;
263 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
265 p->p_user_val_to_name[usrdatum->value - 1] = key;
266 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
270 static int sens_index(void *key, void *datum, void *datap)
273 struct level_datum *levdatum;
278 if (!levdatum->isalias) {
279 if (!levdatum->level->sens ||
280 levdatum->level->sens > p->p_levels.nprim)
282 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
288 static int cat_index(void *key, void *datum, void *datap)
291 struct cat_datum *catdatum;
296 if (!catdatum->isalias) {
297 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
299 p->p_cat_val_to_name[catdatum->value - 1] = key;
305 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
318 * Define the common val_to_name array and the class
319 * val_to_name and val_to_struct arrays in a policy
320 * database structure.
322 * Caller must clean up upon failure.
324 static int policydb_index_classes(struct policydb *p)
328 p->p_common_val_to_name =
329 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
330 if (!p->p_common_val_to_name) {
335 rc = hashtab_map(p->p_commons.table, common_index, p);
339 p->class_val_to_struct =
340 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
341 if (!p->class_val_to_struct) {
346 p->p_class_val_to_name =
347 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
348 if (!p->p_class_val_to_name) {
353 rc = hashtab_map(p->p_classes.table, class_index, p);
359 static void symtab_hash_eval(struct symtab *s)
363 for (i = 0; i < SYM_NUM; i++) {
364 struct hashtab *h = s[i].table;
365 struct hashtab_info info;
367 hashtab_stat(h, &info);
368 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
369 "longest chain length %d\n", symtab_name[i], h->nel,
370 info.slots_used, h->size, info.max_chain_len);
376 * Define the other val_to_name and val_to_struct arrays
377 * in a policy database structure.
379 * Caller must clean up on failure.
381 static int policydb_index_others(struct policydb *p)
385 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
386 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
387 if (selinux_mls_enabled)
388 printk(", %d sens, %d cats", p->p_levels.nprim,
392 printk(KERN_INFO "security: %d classes, %d rules\n",
393 p->p_classes.nprim, p->te_avtab.nel);
396 avtab_hash_eval(&p->te_avtab, "rules");
397 symtab_hash_eval(p->symtab);
400 p->role_val_to_struct =
401 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
403 if (!p->role_val_to_struct) {
408 p->user_val_to_struct =
409 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
411 if (!p->user_val_to_struct) {
416 if (cond_init_bool_indexes(p)) {
421 for (i = SYM_ROLES; i < SYM_NUM; i++) {
422 p->sym_val_to_name[i] =
423 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
424 if (!p->sym_val_to_name[i]) {
428 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
438 * The following *_destroy functions are used to
439 * free any memory allocated for each kind of
440 * symbol data in the policy database.
443 static int perm_destroy(void *key, void *datum, void *p)
450 static int common_destroy(void *key, void *datum, void *p)
452 struct common_datum *comdatum;
456 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
457 hashtab_destroy(comdatum->permissions.table);
462 static int class_destroy(void *key, void *datum, void *p)
464 struct class_datum *cladatum;
465 struct constraint_node *constraint, *ctemp;
466 struct constraint_expr *e, *etmp;
470 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
471 hashtab_destroy(cladatum->permissions.table);
472 constraint = cladatum->constraints;
474 e = constraint->expr;
476 ebitmap_destroy(&e->names);
482 constraint = constraint->next;
486 constraint = cladatum->validatetrans;
488 e = constraint->expr;
490 ebitmap_destroy(&e->names);
496 constraint = constraint->next;
500 kfree(cladatum->comkey);
505 static int role_destroy(void *key, void *datum, void *p)
507 struct role_datum *role;
511 ebitmap_destroy(&role->dominates);
512 ebitmap_destroy(&role->types);
517 static int type_destroy(void *key, void *datum, void *p)
524 static int user_destroy(void *key, void *datum, void *p)
526 struct user_datum *usrdatum;
530 ebitmap_destroy(&usrdatum->roles);
531 ebitmap_destroy(&usrdatum->range.level[0].cat);
532 ebitmap_destroy(&usrdatum->range.level[1].cat);
533 ebitmap_destroy(&usrdatum->dfltlevel.cat);
538 static int sens_destroy(void *key, void *datum, void *p)
540 struct level_datum *levdatum;
544 ebitmap_destroy(&levdatum->level->cat);
545 kfree(levdatum->level);
550 static int cat_destroy(void *key, void *datum, void *p)
557 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
569 static void ocontext_destroy(struct ocontext *c, int i)
571 context_destroy(&c->context[0]);
572 context_destroy(&c->context[1]);
573 if (i == OCON_ISID || i == OCON_FS ||
574 i == OCON_NETIF || i == OCON_FSUSE)
580 * Free any memory allocated by a policy database structure.
582 void policydb_destroy(struct policydb *p)
584 struct ocontext *c, *ctmp;
585 struct genfs *g, *gtmp;
588 for (i = 0; i < SYM_NUM; i++) {
589 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
590 hashtab_destroy(p->symtab[i].table);
593 for (i = 0; i < SYM_NUM; i++)
594 kfree(p->sym_val_to_name[i]);
596 kfree(p->class_val_to_struct);
597 kfree(p->role_val_to_struct);
598 kfree(p->user_val_to_struct);
600 avtab_destroy(&p->te_avtab);
602 for (i = 0; i < OCON_NUM; i++) {
607 ocontext_destroy(ctmp,i);
618 ocontext_destroy(ctmp,OCON_FSUSE);
625 cond_policydb_destroy(p);
631 * Load the initial SIDs specified in a policy database
632 * structure into a SID table.
634 int policydb_load_isids(struct policydb *p, struct sidtab *s)
636 struct ocontext *head, *c;
641 printk(KERN_ERR "security: out of memory on SID table init\n");
645 head = p->ocontexts[OCON_ISID];
646 for (c = head; c; c = c->next) {
647 if (!c->context[0].user) {
648 printk(KERN_ERR "security: SID %s was never "
649 "defined.\n", c->u.name);
653 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
654 printk(KERN_ERR "security: unable to load initial "
655 "SID %s.\n", c->u.name);
665 * Return 1 if the fields in the security context
666 * structure `c' are valid. Return 0 otherwise.
668 int policydb_context_isvalid(struct policydb *p, struct context *c)
670 struct role_datum *role;
671 struct user_datum *usrdatum;
673 if (!c->role || c->role > p->p_roles.nprim)
676 if (!c->user || c->user > p->p_users.nprim)
679 if (!c->type || c->type > p->p_types.nprim)
682 if (c->role != OBJECT_R_VAL) {
684 * Role must be authorized for the type.
686 role = p->role_val_to_struct[c->role - 1];
687 if (!ebitmap_get_bit(&role->types,
689 /* role may not be associated with type */
693 * User must be authorized for the role.
695 usrdatum = p->user_val_to_struct[c->user - 1];
699 if (!ebitmap_get_bit(&usrdatum->roles,
701 /* user may not be associated with role */
705 if (!mls_context_isvalid(p, c))
712 * Read a MLS range structure from a policydb binary
713 * representation file.
715 static int mls_read_range_helper(struct mls_range *r, void *fp)
720 rc = next_entry(buf, fp, sizeof(u32));
724 items = le32_to_cpu(buf[0]);
725 if (items > ARRAY_SIZE(buf)) {
726 printk(KERN_ERR "security: mls: range overflow\n");
730 rc = next_entry(buf, fp, sizeof(u32) * items);
732 printk(KERN_ERR "security: mls: truncated range\n");
735 r->level[0].sens = le32_to_cpu(buf[0]);
737 r->level[1].sens = le32_to_cpu(buf[1]);
739 r->level[1].sens = r->level[0].sens;
741 rc = ebitmap_read(&r->level[0].cat, fp);
743 printk(KERN_ERR "security: mls: error reading low "
748 rc = ebitmap_read(&r->level[1].cat, fp);
750 printk(KERN_ERR "security: mls: error reading high "
755 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
757 printk(KERN_ERR "security: mls: out of memory\n");
766 ebitmap_destroy(&r->level[0].cat);
771 * Read and validate a security context structure
772 * from a policydb binary representation file.
774 static int context_read_and_validate(struct context *c,
781 rc = next_entry(buf, fp, sizeof buf);
783 printk(KERN_ERR "security: context truncated\n");
786 c->user = le32_to_cpu(buf[0]);
787 c->role = le32_to_cpu(buf[1]);
788 c->type = le32_to_cpu(buf[2]);
789 if (p->policyvers >= POLICYDB_VERSION_MLS) {
790 if (mls_read_range_helper(&c->range, fp)) {
791 printk(KERN_ERR "security: error reading MLS range of "
798 if (!policydb_context_isvalid(p, c)) {
799 printk(KERN_ERR "security: invalid security context\n");
808 * The following *_read functions are used to
809 * read the symbol data from a policy database
810 * binary representation file.
813 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
816 struct perm_datum *perdatum;
820 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
825 memset(perdatum, 0, sizeof(*perdatum));
827 rc = next_entry(buf, fp, sizeof buf);
831 len = le32_to_cpu(buf[0]);
832 perdatum->value = le32_to_cpu(buf[1]);
834 key = kmalloc(len + 1,GFP_KERNEL);
839 rc = next_entry(key, fp, len);
844 rc = hashtab_insert(h, key, perdatum);
850 perm_destroy(key, perdatum, NULL);
854 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
857 struct common_datum *comdatum;
858 u32 buf[4], len, nel;
861 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
866 memset(comdatum, 0, sizeof(*comdatum));
868 rc = next_entry(buf, fp, sizeof buf);
872 len = le32_to_cpu(buf[0]);
873 comdatum->value = le32_to_cpu(buf[1]);
875 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
878 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
879 nel = le32_to_cpu(buf[3]);
881 key = kmalloc(len + 1,GFP_KERNEL);
886 rc = next_entry(key, fp, len);
891 for (i = 0; i < nel; i++) {
892 rc = perm_read(p, comdatum->permissions.table, fp);
897 rc = hashtab_insert(h, key, comdatum);
903 common_destroy(key, comdatum, NULL);
907 static int read_cons_helper(struct constraint_node **nodep, int ncons,
908 int allowxtarget, void *fp)
910 struct constraint_node *c, *lc;
911 struct constraint_expr *e, *le;
916 for (i = 0; i < ncons; i++) {
917 c = kmalloc(sizeof(*c), GFP_KERNEL);
920 memset(c, 0, sizeof(*c));
928 rc = next_entry(buf, fp, (sizeof(u32) * 2));
931 c->permissions = le32_to_cpu(buf[0]);
932 nexpr = le32_to_cpu(buf[1]);
935 for (j = 0; j < nexpr; j++) {
936 e = kmalloc(sizeof(*e), GFP_KERNEL);
939 memset(e, 0, sizeof(*e));
947 rc = next_entry(buf, fp, (sizeof(u32) * 3));
950 e->expr_type = le32_to_cpu(buf[0]);
951 e->attr = le32_to_cpu(buf[1]);
952 e->op = le32_to_cpu(buf[2]);
954 switch (e->expr_type) {
966 if (depth == (CEXPR_MAXDEPTH - 1))
971 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
973 if (depth == (CEXPR_MAXDEPTH - 1))
976 if (ebitmap_read(&e->names, fp))
992 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
995 struct class_datum *cladatum;
996 u32 buf[6], len, len2, ncons, nel;
999 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1004 memset(cladatum, 0, sizeof(*cladatum));
1006 rc = next_entry(buf, fp, sizeof(u32)*6);
1010 len = le32_to_cpu(buf[0]);
1011 len2 = le32_to_cpu(buf[1]);
1012 cladatum->value = le32_to_cpu(buf[2]);
1014 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1017 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1018 nel = le32_to_cpu(buf[4]);
1020 ncons = le32_to_cpu(buf[5]);
1022 key = kmalloc(len + 1,GFP_KERNEL);
1027 rc = next_entry(key, fp, len);
1033 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1034 if (!cladatum->comkey) {
1038 rc = next_entry(cladatum->comkey, fp, len2);
1041 cladatum->comkey[len2] = 0;
1043 cladatum->comdatum = hashtab_search(p->p_commons.table,
1045 if (!cladatum->comdatum) {
1046 printk(KERN_ERR "security: unknown common %s\n",
1052 for (i = 0; i < nel; i++) {
1053 rc = perm_read(p, cladatum->permissions.table, fp);
1058 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1062 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1063 /* grab the validatetrans rules */
1064 rc = next_entry(buf, fp, sizeof(u32));
1067 ncons = le32_to_cpu(buf[0]);
1068 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1073 rc = hashtab_insert(h, key, cladatum);
1081 class_destroy(key, cladatum, NULL);
1085 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1088 struct role_datum *role;
1092 role = kmalloc(sizeof(*role), GFP_KERNEL);
1097 memset(role, 0, sizeof(*role));
1099 rc = next_entry(buf, fp, sizeof buf);
1103 len = le32_to_cpu(buf[0]);
1104 role->value = le32_to_cpu(buf[1]);
1106 key = kmalloc(len + 1,GFP_KERNEL);
1111 rc = next_entry(key, fp, len);
1116 rc = ebitmap_read(&role->dominates, fp);
1120 rc = ebitmap_read(&role->types, fp);
1124 if (strcmp(key, OBJECT_R) == 0) {
1125 if (role->value != OBJECT_R_VAL) {
1126 printk(KERN_ERR "Role %s has wrong value %d\n",
1127 OBJECT_R, role->value);
1135 rc = hashtab_insert(h, key, role);
1141 role_destroy(key, role, NULL);
1145 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1148 struct type_datum *typdatum;
1152 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1157 memset(typdatum, 0, sizeof(*typdatum));
1159 rc = next_entry(buf, fp, sizeof buf);
1163 len = le32_to_cpu(buf[0]);
1164 typdatum->value = le32_to_cpu(buf[1]);
1165 typdatum->primary = le32_to_cpu(buf[2]);
1167 key = kmalloc(len + 1,GFP_KERNEL);
1172 rc = next_entry(key, fp, len);
1177 rc = hashtab_insert(h, key, typdatum);
1183 type_destroy(key, typdatum, NULL);
1189 * Read a MLS level structure from a policydb binary
1190 * representation file.
1192 static int mls_read_level(struct mls_level *lp, void *fp)
1197 memset(lp, 0, sizeof(*lp));
1199 rc = next_entry(buf, fp, sizeof buf);
1201 printk(KERN_ERR "security: mls: truncated level\n");
1204 lp->sens = le32_to_cpu(buf[0]);
1206 if (ebitmap_read(&lp->cat, fp)) {
1207 printk(KERN_ERR "security: mls: error reading level "
1217 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1220 struct user_datum *usrdatum;
1224 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1229 memset(usrdatum, 0, sizeof(*usrdatum));
1231 rc = next_entry(buf, fp, sizeof buf);
1235 len = le32_to_cpu(buf[0]);
1236 usrdatum->value = le32_to_cpu(buf[1]);
1238 key = kmalloc(len + 1,GFP_KERNEL);
1243 rc = next_entry(key, fp, len);
1248 rc = ebitmap_read(&usrdatum->roles, fp);
1252 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1253 rc = mls_read_range_helper(&usrdatum->range, fp);
1256 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1261 rc = hashtab_insert(h, key, usrdatum);
1267 user_destroy(key, usrdatum, NULL);
1271 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1274 struct level_datum *levdatum;
1278 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1283 memset(levdatum, 0, sizeof(*levdatum));
1285 rc = next_entry(buf, fp, sizeof buf);
1289 len = le32_to_cpu(buf[0]);
1290 levdatum->isalias = le32_to_cpu(buf[1]);
1292 key = kmalloc(len + 1,GFP_ATOMIC);
1297 rc = next_entry(key, fp, len);
1302 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1303 if (!levdatum->level) {
1307 if (mls_read_level(levdatum->level, fp)) {
1312 rc = hashtab_insert(h, key, levdatum);
1318 sens_destroy(key, levdatum, NULL);
1322 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1325 struct cat_datum *catdatum;
1329 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1334 memset(catdatum, 0, sizeof(*catdatum));
1336 rc = next_entry(buf, fp, sizeof buf);
1340 len = le32_to_cpu(buf[0]);
1341 catdatum->value = le32_to_cpu(buf[1]);
1342 catdatum->isalias = le32_to_cpu(buf[2]);
1344 key = kmalloc(len + 1,GFP_ATOMIC);
1349 rc = next_entry(key, fp, len);
1354 rc = hashtab_insert(h, key, catdatum);
1361 cat_destroy(key, catdatum, NULL);
1365 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1377 extern int ss_initialized;
1380 * Read the configuration data from a policy database binary
1381 * representation file into a policy database structure.
1383 int policydb_read(struct policydb *p, void *fp)
1385 struct role_allow *ra, *lra;
1386 struct role_trans *tr, *ltr;
1387 struct ocontext *l, *c, *newc;
1388 struct genfs *genfs_p, *genfs, *newgenfs;
1390 u32 buf[8], len, len2, config, nprim, nel, nel2;
1392 struct policydb_compat_info *info;
1393 struct range_trans *rt, *lrt;
1397 rc = policydb_init(p);
1401 /* Read the magic number and string length. */
1402 rc = next_entry(buf, fp, sizeof(u32)* 2);
1406 for (i = 0; i < 2; i++)
1407 buf[i] = le32_to_cpu(buf[i]);
1409 if (buf[0] != POLICYDB_MAGIC) {
1410 printk(KERN_ERR "security: policydb magic number 0x%x does "
1411 "not match expected magic number 0x%x\n",
1412 buf[0], POLICYDB_MAGIC);
1417 if (len != strlen(POLICYDB_STRING)) {
1418 printk(KERN_ERR "security: policydb string length %d does not "
1419 "match expected length %Zu\n",
1420 len, strlen(POLICYDB_STRING));
1423 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1424 if (!policydb_str) {
1425 printk(KERN_ERR "security: unable to allocate memory for policydb "
1426 "string of length %d\n", len);
1430 rc = next_entry(policydb_str, fp, len);
1432 printk(KERN_ERR "security: truncated policydb string identifier\n");
1433 kfree(policydb_str);
1436 policydb_str[len] = 0;
1437 if (strcmp(policydb_str, POLICYDB_STRING)) {
1438 printk(KERN_ERR "security: policydb string %s does not match "
1439 "my string %s\n", policydb_str, POLICYDB_STRING);
1440 kfree(policydb_str);
1443 /* Done with policydb_str. */
1444 kfree(policydb_str);
1445 policydb_str = NULL;
1447 /* Read the version, config, and table sizes. */
1448 rc = next_entry(buf, fp, sizeof(u32)*4);
1451 for (i = 0; i < 4; i++)
1452 buf[i] = le32_to_cpu(buf[i]);
1454 p->policyvers = buf[0];
1455 if (p->policyvers < POLICYDB_VERSION_MIN ||
1456 p->policyvers > POLICYDB_VERSION_MAX) {
1457 printk(KERN_ERR "security: policydb version %d does not match "
1458 "my version range %d-%d\n",
1459 buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1463 if ((buf[1] & POLICYDB_CONFIG_MLS)) {
1464 if (ss_initialized && !selinux_mls_enabled) {
1465 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1469 selinux_mls_enabled = 1;
1470 config |= POLICYDB_CONFIG_MLS;
1472 if (p->policyvers < POLICYDB_VERSION_MLS) {
1473 printk(KERN_ERR "security policydb version %d (MLS) "
1474 "not backwards compatible\n", p->policyvers);
1478 if (ss_initialized && selinux_mls_enabled) {
1479 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1485 info = policydb_lookup_compat(p->policyvers);
1487 printk(KERN_ERR "security: unable to find policy compat info "
1488 "for version %d\n", p->policyvers);
1492 if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1493 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1494 "not match mine (%d,%d)\n", buf[2], buf[3],
1495 info->sym_num, info->ocon_num);
1499 for (i = 0; i < info->sym_num; i++) {
1500 rc = next_entry(buf, fp, sizeof(u32)*2);
1503 nprim = le32_to_cpu(buf[0]);
1504 nel = le32_to_cpu(buf[1]);
1505 for (j = 0; j < nel; j++) {
1506 rc = read_f[i](p, p->symtab[i].table, fp);
1511 p->symtab[i].nprim = nprim;
1514 rc = avtab_read(&p->te_avtab, fp, config);
1518 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1519 rc = cond_read_list(p, fp);
1524 rc = next_entry(buf, fp, sizeof(u32));
1527 nel = le32_to_cpu(buf[0]);
1529 for (i = 0; i < nel; i++) {
1530 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1535 memset(tr, 0, sizeof(*tr));
1541 rc = next_entry(buf, fp, sizeof(u32)*3);
1544 tr->role = le32_to_cpu(buf[0]);
1545 tr->type = le32_to_cpu(buf[1]);
1546 tr->new_role = le32_to_cpu(buf[2]);
1550 rc = next_entry(buf, fp, sizeof(u32));
1553 nel = le32_to_cpu(buf[0]);
1555 for (i = 0; i < nel; i++) {
1556 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1561 memset(ra, 0, sizeof(*ra));
1567 rc = next_entry(buf, fp, sizeof(u32)*2);
1570 ra->role = le32_to_cpu(buf[0]);
1571 ra->new_role = le32_to_cpu(buf[1]);
1575 rc = policydb_index_classes(p);
1579 rc = policydb_index_others(p);
1583 for (i = 0; i < info->ocon_num; i++) {
1584 rc = next_entry(buf, fp, sizeof(u32));
1587 nel = le32_to_cpu(buf[0]);
1589 for (j = 0; j < nel; j++) {
1590 c = kmalloc(sizeof(*c), GFP_KERNEL);
1595 memset(c, 0, sizeof(*c));
1599 p->ocontexts[i] = c;
1605 rc = next_entry(buf, fp, sizeof(u32));
1608 c->sid[0] = le32_to_cpu(buf[0]);
1609 rc = context_read_and_validate(&c->context[0], p, fp);
1615 rc = next_entry(buf, fp, sizeof(u32));
1618 len = le32_to_cpu(buf[0]);
1619 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1624 rc = next_entry(c->u.name, fp, len);
1628 rc = context_read_and_validate(&c->context[0], p, fp);
1631 rc = context_read_and_validate(&c->context[1], p, fp);
1636 rc = next_entry(buf, fp, sizeof(u32)*3);
1639 c->u.port.protocol = le32_to_cpu(buf[0]);
1640 c->u.port.low_port = le32_to_cpu(buf[1]);
1641 c->u.port.high_port = le32_to_cpu(buf[2]);
1642 rc = context_read_and_validate(&c->context[0], p, fp);
1647 rc = next_entry(buf, fp, sizeof(u32)* 2);
1650 c->u.node.addr = le32_to_cpu(buf[0]);
1651 c->u.node.mask = le32_to_cpu(buf[1]);
1652 rc = context_read_and_validate(&c->context[0], p, fp);
1657 rc = next_entry(buf, fp, sizeof(u32)*2);
1660 c->v.behavior = le32_to_cpu(buf[0]);
1661 if (c->v.behavior > SECURITY_FS_USE_NONE)
1663 len = le32_to_cpu(buf[1]);
1664 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1669 rc = next_entry(c->u.name, fp, len);
1673 rc = context_read_and_validate(&c->context[0], p, fp);
1680 rc = next_entry(buf, fp, sizeof(u32) * 8);
1683 for (k = 0; k < 4; k++)
1684 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1685 for (k = 0; k < 4; k++)
1686 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1687 if (context_read_and_validate(&c->context[0], p, fp))
1695 rc = next_entry(buf, fp, sizeof(u32));
1698 nel = le32_to_cpu(buf[0]);
1701 for (i = 0; i < nel; i++) {
1702 rc = next_entry(buf, fp, sizeof(u32));
1705 len = le32_to_cpu(buf[0]);
1706 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1711 memset(newgenfs, 0, sizeof(*newgenfs));
1713 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1714 if (!newgenfs->fstype) {
1719 rc = next_entry(newgenfs->fstype, fp, len);
1721 kfree(newgenfs->fstype);
1725 newgenfs->fstype[len] = 0;
1726 for (genfs_p = NULL, genfs = p->genfs; genfs;
1727 genfs_p = genfs, genfs = genfs->next) {
1728 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1729 printk(KERN_ERR "security: dup genfs "
1730 "fstype %s\n", newgenfs->fstype);
1731 kfree(newgenfs->fstype);
1735 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1738 newgenfs->next = genfs;
1740 genfs_p->next = newgenfs;
1742 p->genfs = newgenfs;
1743 rc = next_entry(buf, fp, sizeof(u32));
1746 nel2 = le32_to_cpu(buf[0]);
1747 for (j = 0; j < nel2; j++) {
1748 rc = next_entry(buf, fp, sizeof(u32));
1751 len = le32_to_cpu(buf[0]);
1753 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1758 memset(newc, 0, sizeof(*newc));
1760 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1761 if (!newc->u.name) {
1765 rc = next_entry(newc->u.name, fp, len);
1768 newc->u.name[len] = 0;
1769 rc = next_entry(buf, fp, sizeof(u32));
1772 newc->v.sclass = le32_to_cpu(buf[0]);
1773 if (context_read_and_validate(&newc->context[0], p, fp))
1775 for (l = NULL, c = newgenfs->head; c;
1776 l = c, c = c->next) {
1777 if (!strcmp(newc->u.name, c->u.name) &&
1778 (!c->v.sclass || !newc->v.sclass ||
1779 newc->v.sclass == c->v.sclass)) {
1780 printk(KERN_ERR "security: dup genfs "
1782 newgenfs->fstype, c->u.name);
1785 len = strlen(newc->u.name);
1786 len2 = strlen(c->u.name);
1795 newgenfs->head = newc;
1799 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1800 rc = next_entry(buf, fp, sizeof(u32));
1803 nel = le32_to_cpu(buf[0]);
1805 for (i = 0; i < nel; i++) {
1806 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1811 memset(rt, 0, sizeof(*rt));
1816 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1819 rt->dom = le32_to_cpu(buf[0]);
1820 rt->type = le32_to_cpu(buf[1]);
1821 rc = mls_read_range_helper(&rt->range, fp);
1832 ocontext_destroy(newc,OCON_FSUSE);
1836 policydb_destroy(p);