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,
95 .version = POLICYDB_VERSION_AVTAB,
101 static struct policydb_compat_info *policydb_lookup_compat(int version)
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];
116 * Initialize the role table.
118 static int roles_init(struct policydb *p)
122 struct role_datum *role;
124 role = kmalloc(sizeof(*role), GFP_KERNEL);
129 memset(role, 0, sizeof(*role));
130 role->value = ++p->p_roles.nprim;
131 if (role->value != OBJECT_R_VAL) {
135 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
140 strcpy(key, OBJECT_R);
141 rc = hashtab_insert(p->p_roles.table, key, role);
155 * Initialize a policy database structure.
157 static int policydb_init(struct policydb *p)
161 memset(p, 0, sizeof(*p));
163 for (i = 0; i < SYM_NUM; i++) {
164 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
166 goto out_free_symtab;
169 rc = avtab_init(&p->te_avtab);
171 goto out_free_symtab;
177 rc = cond_policydb_init(p);
185 avtab_destroy(&p->te_avtab);
188 for (i = 0; i < SYM_NUM; i++)
189 hashtab_destroy(p->symtab[i].table);
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)
206 struct common_datum *comdatum;
210 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
212 p->p_common_val_to_name[comdatum->value - 1] = key;
216 static int class_index(void *key, void *datum, void *datap)
219 struct class_datum *cladatum;
223 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
225 p->p_class_val_to_name[cladatum->value - 1] = key;
226 p->class_val_to_struct[cladatum->value - 1] = cladatum;
230 static int role_index(void *key, void *datum, void *datap)
233 struct role_datum *role;
237 if (!role->value || role->value > p->p_roles.nprim)
239 p->p_role_val_to_name[role->value - 1] = key;
240 p->role_val_to_struct[role->value - 1] = role;
244 static int type_index(void *key, void *datum, void *datap)
247 struct type_datum *typdatum;
252 if (typdatum->primary) {
253 if (!typdatum->value || typdatum->value > p->p_types.nprim)
255 p->p_type_val_to_name[typdatum->value - 1] = key;
261 static int user_index(void *key, void *datum, void *datap)
264 struct user_datum *usrdatum;
268 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
270 p->p_user_val_to_name[usrdatum->value - 1] = key;
271 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
275 static int sens_index(void *key, void *datum, void *datap)
278 struct level_datum *levdatum;
283 if (!levdatum->isalias) {
284 if (!levdatum->level->sens ||
285 levdatum->level->sens > p->p_levels.nprim)
287 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
293 static int cat_index(void *key, void *datum, void *datap)
296 struct cat_datum *catdatum;
301 if (!catdatum->isalias) {
302 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
304 p->p_cat_val_to_name[catdatum->value - 1] = key;
310 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
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)
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) {
340 rc = hashtab_map(p->p_commons.table, common_index, p);
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) {
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) {
358 rc = hashtab_map(p->p_classes.table, class_index, p);
364 static void symtab_hash_eval(struct symtab *s)
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);
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)
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,
397 printk(KERN_INFO "security: %d classes, %d rules\n",
398 p->p_classes.nprim, p->te_avtab.nel);
401 avtab_hash_eval(&p->te_avtab, "rules");
402 symtab_hash_eval(p->symtab);
405 p->role_val_to_struct =
406 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
408 if (!p->role_val_to_struct) {
413 p->user_val_to_struct =
414 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
416 if (!p->user_val_to_struct) {
421 if (cond_init_bool_indexes(p)) {
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]) {
433 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
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)
455 static int common_destroy(void *key, void *datum, void *p)
457 struct common_datum *comdatum;
461 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
462 hashtab_destroy(comdatum->permissions.table);
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;
475 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
476 hashtab_destroy(cladatum->permissions.table);
477 constraint = cladatum->constraints;
479 e = constraint->expr;
481 ebitmap_destroy(&e->names);
487 constraint = constraint->next;
491 constraint = cladatum->validatetrans;
493 e = constraint->expr;
495 ebitmap_destroy(&e->names);
501 constraint = constraint->next;
505 kfree(cladatum->comkey);
510 static int role_destroy(void *key, void *datum, void *p)
512 struct role_datum *role;
516 ebitmap_destroy(&role->dominates);
517 ebitmap_destroy(&role->types);
522 static int type_destroy(void *key, void *datum, void *p)
529 static int user_destroy(void *key, void *datum, void *p)
531 struct user_datum *usrdatum;
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);
543 static int sens_destroy(void *key, void *datum, void *p)
545 struct level_datum *levdatum;
549 ebitmap_destroy(&levdatum->level->cat);
550 kfree(levdatum->level);
555 static int cat_destroy(void *key, void *datum, void *p)
562 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
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)
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;
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++) {
615 ocontext_destroy(ctmp,i);
626 ocontext_destroy(ctmp,OCON_FSUSE);
633 cond_policydb_destroy(p);
635 for (tr = p->role_tr; tr; tr = tr->next) {
641 for (ra = p->role_allow; ra; ra = ra -> next) {
647 for (rt = p->range_tr; rt; rt = rt -> next) {
653 for (i = 0; i < p->p_types.nprim; i++)
654 ebitmap_destroy(&p->type_attr_map[i]);
655 kfree(p->type_attr_map);
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;
671 printk(KERN_ERR "security: out of memory on SID table init\n");
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);
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);
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)
706 if (!c->user || c->user > p->p_users.nprim)
709 if (!c->type || c->type > p->p_types.nprim)
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,
719 /* role may not be associated with type */
723 * User must be authorized for the role.
725 usrdatum = p->user_val_to_struct[c->user - 1];
729 if (!ebitmap_get_bit(&usrdatum->roles,
731 /* user may not be associated with role */
735 if (!mls_context_isvalid(p, c))
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)
751 rc = next_entry(buf, fp, sizeof(u32));
755 items = le32_to_cpu(buf[0]);
756 if (items > ARRAY_SIZE(buf)) {
757 printk(KERN_ERR "security: mls: range overflow\n");
761 rc = next_entry(buf, fp, sizeof(u32) * items);
763 printk(KERN_ERR "security: mls: truncated range\n");
766 r->level[0].sens = le32_to_cpu(buf[0]);
768 r->level[1].sens = le32_to_cpu(buf[1]);
770 r->level[1].sens = r->level[0].sens;
772 rc = ebitmap_read(&r->level[0].cat, fp);
774 printk(KERN_ERR "security: mls: error reading low "
779 rc = ebitmap_read(&r->level[1].cat, fp);
781 printk(KERN_ERR "security: mls: error reading high "
786 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
788 printk(KERN_ERR "security: mls: out of memory\n");
797 ebitmap_destroy(&r->level[0].cat);
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,
812 rc = next_entry(buf, fp, sizeof buf);
814 printk(KERN_ERR "security: context truncated\n");
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 "
829 if (!policydb_context_isvalid(p, c)) {
830 printk(KERN_ERR "security: invalid security context\n");
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)
847 struct perm_datum *perdatum;
852 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
857 memset(perdatum, 0, sizeof(*perdatum));
859 rc = next_entry(buf, fp, sizeof buf);
863 len = le32_to_cpu(buf[0]);
864 perdatum->value = le32_to_cpu(buf[1]);
866 key = kmalloc(len + 1,GFP_KERNEL);
871 rc = next_entry(key, fp, len);
876 rc = hashtab_insert(h, key, perdatum);
882 perm_destroy(key, perdatum, NULL);
886 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
889 struct common_datum *comdatum;
894 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
899 memset(comdatum, 0, sizeof(*comdatum));
901 rc = next_entry(buf, fp, sizeof buf);
905 len = le32_to_cpu(buf[0]);
906 comdatum->value = le32_to_cpu(buf[1]);
908 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
911 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
912 nel = le32_to_cpu(buf[3]);
914 key = kmalloc(len + 1,GFP_KERNEL);
919 rc = next_entry(key, fp, len);
924 for (i = 0; i < nel; i++) {
925 rc = perm_read(p, comdatum->permissions.table, fp);
930 rc = hashtab_insert(h, key, comdatum);
936 common_destroy(key, comdatum, NULL);
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;
950 for (i = 0; i < ncons; i++) {
951 c = kmalloc(sizeof(*c), GFP_KERNEL);
954 memset(c, 0, sizeof(*c));
962 rc = next_entry(buf, fp, (sizeof(u32) * 2));
965 c->permissions = le32_to_cpu(buf[0]);
966 nexpr = le32_to_cpu(buf[1]);
969 for (j = 0; j < nexpr; j++) {
970 e = kmalloc(sizeof(*e), GFP_KERNEL);
973 memset(e, 0, sizeof(*e));
981 rc = next_entry(buf, fp, (sizeof(u32) * 3));
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) {
1000 if (depth == (CEXPR_MAXDEPTH - 1))
1005 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1007 if (depth == (CEXPR_MAXDEPTH - 1))
1010 if (ebitmap_read(&e->names, fp))
1026 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1029 struct class_datum *cladatum;
1031 u32 len, len2, ncons, nel;
1034 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1039 memset(cladatum, 0, sizeof(*cladatum));
1041 rc = next_entry(buf, fp, sizeof(u32)*6);
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);
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);
1062 rc = next_entry(key, fp, len);
1068 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1069 if (!cladatum->comkey) {
1073 rc = next_entry(cladatum->comkey, fp, len2);
1076 cladatum->comkey[len2] = 0;
1078 cladatum->comdatum = hashtab_search(p->p_commons.table,
1080 if (!cladatum->comdatum) {
1081 printk(KERN_ERR "security: unknown common %s\n",
1087 for (i = 0; i < nel; i++) {
1088 rc = perm_read(p, cladatum->permissions.table, fp);
1093 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1097 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1098 /* grab the validatetrans rules */
1099 rc = next_entry(buf, fp, sizeof(u32));
1102 ncons = le32_to_cpu(buf[0]);
1103 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1108 rc = hashtab_insert(h, key, cladatum);
1116 class_destroy(key, cladatum, NULL);
1120 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1123 struct role_datum *role;
1128 role = kmalloc(sizeof(*role), GFP_KERNEL);
1133 memset(role, 0, sizeof(*role));
1135 rc = next_entry(buf, fp, sizeof buf);
1139 len = le32_to_cpu(buf[0]);
1140 role->value = le32_to_cpu(buf[1]);
1142 key = kmalloc(len + 1,GFP_KERNEL);
1147 rc = next_entry(key, fp, len);
1152 rc = ebitmap_read(&role->dominates, fp);
1156 rc = ebitmap_read(&role->types, fp);
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);
1171 rc = hashtab_insert(h, key, role);
1177 role_destroy(key, role, NULL);
1181 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1184 struct type_datum *typdatum;
1189 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1194 memset(typdatum, 0, sizeof(*typdatum));
1196 rc = next_entry(buf, fp, sizeof buf);
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);
1209 rc = next_entry(key, fp, len);
1214 rc = hashtab_insert(h, key, typdatum);
1220 type_destroy(key, typdatum, NULL);
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)
1234 memset(lp, 0, sizeof(*lp));
1236 rc = next_entry(buf, fp, sizeof buf);
1238 printk(KERN_ERR "security: mls: truncated level\n");
1241 lp->sens = le32_to_cpu(buf[0]);
1243 if (ebitmap_read(&lp->cat, fp)) {
1244 printk(KERN_ERR "security: mls: error reading level "
1254 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1257 struct user_datum *usrdatum;
1262 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1267 memset(usrdatum, 0, sizeof(*usrdatum));
1269 rc = next_entry(buf, fp, sizeof buf);
1273 len = le32_to_cpu(buf[0]);
1274 usrdatum->value = le32_to_cpu(buf[1]);
1276 key = kmalloc(len + 1,GFP_KERNEL);
1281 rc = next_entry(key, fp, len);
1286 rc = ebitmap_read(&usrdatum->roles, fp);
1290 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1291 rc = mls_read_range_helper(&usrdatum->range, fp);
1294 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1299 rc = hashtab_insert(h, key, usrdatum);
1305 user_destroy(key, usrdatum, NULL);
1309 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1312 struct level_datum *levdatum;
1317 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1322 memset(levdatum, 0, sizeof(*levdatum));
1324 rc = next_entry(buf, fp, sizeof buf);
1328 len = le32_to_cpu(buf[0]);
1329 levdatum->isalias = le32_to_cpu(buf[1]);
1331 key = kmalloc(len + 1,GFP_ATOMIC);
1336 rc = next_entry(key, fp, len);
1341 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1342 if (!levdatum->level) {
1346 if (mls_read_level(levdatum->level, fp)) {
1351 rc = hashtab_insert(h, key, levdatum);
1357 sens_destroy(key, levdatum, NULL);
1361 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1364 struct cat_datum *catdatum;
1369 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1374 memset(catdatum, 0, sizeof(*catdatum));
1376 rc = next_entry(buf, fp, sizeof buf);
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);
1389 rc = next_entry(key, fp, len);
1394 rc = hashtab_insert(h, key, catdatum);
1401 cat_destroy(key, catdatum, NULL);
1405 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
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;
1431 u32 len, len2, config, nprim, nel, nel2;
1433 struct policydb_compat_info *info;
1434 struct range_trans *rt, *lrt;
1438 rc = policydb_init(p);
1442 /* Read the magic number and string length. */
1443 rc = next_entry(buf, fp, sizeof(u32)* 2);
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);
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));
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);
1468 rc = next_entry(policydb_str, fp, len);
1470 printk(KERN_ERR "security: truncated policydb string identifier\n");
1471 kfree(policydb_str);
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);
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);
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);
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 "
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);
1514 if (ss_initialized && selinux_mls_enabled) {
1515 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1521 info = policydb_lookup_compat(p->policyvers);
1523 printk(KERN_ERR "security: unable to find policy compat info "
1524 "for version %d\n", p->policyvers);
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);
1537 for (i = 0; i < info->sym_num; i++) {
1538 rc = next_entry(buf, fp, sizeof(u32)*2);
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);
1549 p->symtab[i].nprim = nprim;
1552 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1556 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1557 rc = cond_read_list(p, fp);
1562 rc = next_entry(buf, fp, sizeof(u32));
1565 nel = le32_to_cpu(buf[0]);
1567 for (i = 0; i < nel; i++) {
1568 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1573 memset(tr, 0, sizeof(*tr));
1579 rc = next_entry(buf, fp, sizeof(u32)*3);
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]);
1588 rc = next_entry(buf, fp, sizeof(u32));
1591 nel = le32_to_cpu(buf[0]);
1593 for (i = 0; i < nel; i++) {
1594 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1599 memset(ra, 0, sizeof(*ra));
1605 rc = next_entry(buf, fp, sizeof(u32)*2);
1608 ra->role = le32_to_cpu(buf[0]);
1609 ra->new_role = le32_to_cpu(buf[1]);
1613 rc = policydb_index_classes(p);
1617 rc = policydb_index_others(p);
1621 for (i = 0; i < info->ocon_num; i++) {
1622 rc = next_entry(buf, fp, sizeof(u32));
1625 nel = le32_to_cpu(buf[0]);
1627 for (j = 0; j < nel; j++) {
1628 c = kmalloc(sizeof(*c), GFP_KERNEL);
1633 memset(c, 0, sizeof(*c));
1637 p->ocontexts[i] = c;
1643 rc = next_entry(buf, fp, sizeof(u32));
1646 c->sid[0] = le32_to_cpu(buf[0]);
1647 rc = context_read_and_validate(&c->context[0], p, fp);
1653 rc = next_entry(buf, fp, sizeof(u32));
1656 len = le32_to_cpu(buf[0]);
1657 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1662 rc = next_entry(c->u.name, fp, len);
1666 rc = context_read_and_validate(&c->context[0], p, fp);
1669 rc = context_read_and_validate(&c->context[1], p, fp);
1674 rc = next_entry(buf, fp, sizeof(u32)*3);
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);
1685 rc = next_entry(buf, fp, sizeof(u32)* 2);
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);
1695 rc = next_entry(buf, fp, sizeof(u32)*2);
1698 c->v.behavior = le32_to_cpu(buf[0]);
1699 if (c->v.behavior > SECURITY_FS_USE_NONE)
1701 len = le32_to_cpu(buf[1]);
1702 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1707 rc = next_entry(c->u.name, fp, len);
1711 rc = context_read_and_validate(&c->context[0], p, fp);
1718 rc = next_entry(buf, fp, sizeof(u32) * 8);
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))
1733 rc = next_entry(buf, fp, sizeof(u32));
1736 nel = le32_to_cpu(buf[0]);
1739 for (i = 0; i < nel; i++) {
1740 rc = next_entry(buf, fp, sizeof(u32));
1743 len = le32_to_cpu(buf[0]);
1744 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1749 memset(newgenfs, 0, sizeof(*newgenfs));
1751 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1752 if (!newgenfs->fstype) {
1757 rc = next_entry(newgenfs->fstype, fp, len);
1759 kfree(newgenfs->fstype);
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);
1773 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1776 newgenfs->next = genfs;
1778 genfs_p->next = newgenfs;
1780 p->genfs = newgenfs;
1781 rc = next_entry(buf, fp, sizeof(u32));
1784 nel2 = le32_to_cpu(buf[0]);
1785 for (j = 0; j < nel2; j++) {
1786 rc = next_entry(buf, fp, sizeof(u32));
1789 len = le32_to_cpu(buf[0]);
1791 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1796 memset(newc, 0, sizeof(*newc));
1798 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1799 if (!newc->u.name) {
1803 rc = next_entry(newc->u.name, fp, len);
1806 newc->u.name[len] = 0;
1807 rc = next_entry(buf, fp, sizeof(u32));
1810 newc->v.sclass = le32_to_cpu(buf[0]);
1811 if (context_read_and_validate(&newc->context[0], p, fp))
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 "
1820 newgenfs->fstype, c->u.name);
1823 len = strlen(newc->u.name);
1824 len2 = strlen(c->u.name);
1833 newgenfs->head = newc;
1837 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1838 rc = next_entry(buf, fp, sizeof(u32));
1841 nel = le32_to_cpu(buf[0]);
1843 for (i = 0; i < nel; i++) {
1844 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1849 memset(rt, 0, sizeof(*rt));
1854 rc = next_entry(buf, fp, (sizeof(u32) * 2));
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);
1866 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1867 if (!p->type_attr_map)
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))
1876 /* add the type itself as the degenerate case */
1877 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1885 ocontext_destroy(newc,OCON_FSUSE);
1889 policydb_destroy(p);