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/sched.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
31 #include "conditional.h"
37 static char *symtab_name[SYM_NUM] = {
49 int selinux_mls_enabled = 0;
51 static unsigned int symtab_sizes[SYM_NUM] = {
62 struct policydb_compat_info {
68 /* These need to be updated if SYM_NUM or OCON_NUM changes */
69 static struct policydb_compat_info policydb_compat[] = {
71 .version = POLICYDB_VERSION_BASE,
72 .sym_num = SYM_NUM - 3,
73 .ocon_num = OCON_NUM - 1,
76 .version = POLICYDB_VERSION_BOOL,
77 .sym_num = SYM_NUM - 2,
78 .ocon_num = OCON_NUM - 1,
81 .version = POLICYDB_VERSION_IPV6,
82 .sym_num = SYM_NUM - 2,
86 .version = POLICYDB_VERSION_NLCLASS,
87 .sym_num = SYM_NUM - 2,
91 .version = POLICYDB_VERSION_MLS,
96 .version = POLICYDB_VERSION_AVTAB,
101 .version = POLICYDB_VERSION_RANGETRANS,
103 .ocon_num = OCON_NUM,
107 static struct policydb_compat_info *policydb_lookup_compat(int version)
110 struct policydb_compat_info *info = NULL;
112 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
113 if (policydb_compat[i].version == version) {
114 info = &policydb_compat[i];
122 * Initialize the role table.
124 static int roles_init(struct policydb *p)
128 struct role_datum *role;
130 role = kzalloc(sizeof(*role), GFP_KERNEL);
135 role->value = ++p->p_roles.nprim;
136 if (role->value != OBJECT_R_VAL) {
140 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
145 strcpy(key, OBJECT_R);
146 rc = hashtab_insert(p->p_roles.table, key, role);
160 * Initialize a policy database structure.
162 static int policydb_init(struct policydb *p)
166 memset(p, 0, sizeof(*p));
168 for (i = 0; i < SYM_NUM; i++) {
169 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
171 goto out_free_symtab;
174 rc = avtab_init(&p->te_avtab);
176 goto out_free_symtab;
180 goto out_free_symtab;
182 rc = cond_policydb_init(p);
184 goto out_free_symtab;
190 for (i = 0; i < SYM_NUM; i++)
191 hashtab_destroy(p->symtab[i].table);
196 * The following *_index functions are used to
197 * define the val_to_name and val_to_struct arrays
198 * in a policy database structure. The val_to_name
199 * arrays are used when converting security context
200 * structures into string representations. The
201 * val_to_struct arrays are used when the attributes
202 * of a class, role, or user are needed.
205 static int common_index(void *key, void *datum, void *datap)
208 struct common_datum *comdatum;
212 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
214 p->p_common_val_to_name[comdatum->value - 1] = key;
218 static int class_index(void *key, void *datum, void *datap)
221 struct class_datum *cladatum;
225 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
227 p->p_class_val_to_name[cladatum->value - 1] = key;
228 p->class_val_to_struct[cladatum->value - 1] = cladatum;
232 static int role_index(void *key, void *datum, void *datap)
235 struct role_datum *role;
239 if (!role->value || role->value > p->p_roles.nprim)
241 p->p_role_val_to_name[role->value - 1] = key;
242 p->role_val_to_struct[role->value - 1] = role;
246 static int type_index(void *key, void *datum, void *datap)
249 struct type_datum *typdatum;
254 if (typdatum->primary) {
255 if (!typdatum->value || typdatum->value > p->p_types.nprim)
257 p->p_type_val_to_name[typdatum->value - 1] = key;
263 static int user_index(void *key, void *datum, void *datap)
266 struct user_datum *usrdatum;
270 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
272 p->p_user_val_to_name[usrdatum->value - 1] = key;
273 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
277 static int sens_index(void *key, void *datum, void *datap)
280 struct level_datum *levdatum;
285 if (!levdatum->isalias) {
286 if (!levdatum->level->sens ||
287 levdatum->level->sens > p->p_levels.nprim)
289 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
295 static int cat_index(void *key, void *datum, void *datap)
298 struct cat_datum *catdatum;
303 if (!catdatum->isalias) {
304 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
306 p->p_cat_val_to_name[catdatum->value - 1] = key;
312 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
325 * Define the common val_to_name array and the class
326 * val_to_name and val_to_struct arrays in a policy
327 * database structure.
329 * Caller must clean up upon failure.
331 static int policydb_index_classes(struct policydb *p)
335 p->p_common_val_to_name =
336 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
337 if (!p->p_common_val_to_name) {
342 rc = hashtab_map(p->p_commons.table, common_index, p);
346 p->class_val_to_struct =
347 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
348 if (!p->class_val_to_struct) {
353 p->p_class_val_to_name =
354 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
355 if (!p->p_class_val_to_name) {
360 rc = hashtab_map(p->p_classes.table, class_index, p);
366 static void symtab_hash_eval(struct symtab *s)
370 for (i = 0; i < SYM_NUM; i++) {
371 struct hashtab *h = s[i].table;
372 struct hashtab_info info;
374 hashtab_stat(h, &info);
375 printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, "
376 "longest chain length %d\n", symtab_name[i], h->nel,
377 info.slots_used, h->size, info.max_chain_len);
383 * Define the other val_to_name and val_to_struct arrays
384 * in a policy database structure.
386 * Caller must clean up on failure.
388 static int policydb_index_others(struct policydb *p)
392 printk(KERN_DEBUG "security: %d users, %d roles, %d types, %d bools",
393 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
394 if (selinux_mls_enabled)
395 printk(", %d sens, %d cats", p->p_levels.nprim,
399 printk(KERN_DEBUG "security: %d classes, %d rules\n",
400 p->p_classes.nprim, p->te_avtab.nel);
403 avtab_hash_eval(&p->te_avtab, "rules");
404 symtab_hash_eval(p->symtab);
407 p->role_val_to_struct =
408 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
410 if (!p->role_val_to_struct) {
415 p->user_val_to_struct =
416 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
418 if (!p->user_val_to_struct) {
423 if (cond_init_bool_indexes(p)) {
428 for (i = SYM_ROLES; i < SYM_NUM; i++) {
429 p->sym_val_to_name[i] =
430 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
431 if (!p->sym_val_to_name[i]) {
435 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
445 * The following *_destroy functions are used to
446 * free any memory allocated for each kind of
447 * symbol data in the policy database.
450 static int perm_destroy(void *key, void *datum, void *p)
457 static int common_destroy(void *key, void *datum, void *p)
459 struct common_datum *comdatum;
463 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
464 hashtab_destroy(comdatum->permissions.table);
469 static int cls_destroy(void *key, void *datum, void *p)
471 struct class_datum *cladatum;
472 struct constraint_node *constraint, *ctemp;
473 struct constraint_expr *e, *etmp;
477 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
478 hashtab_destroy(cladatum->permissions.table);
479 constraint = cladatum->constraints;
481 e = constraint->expr;
483 ebitmap_destroy(&e->names);
489 constraint = constraint->next;
493 constraint = cladatum->validatetrans;
495 e = constraint->expr;
497 ebitmap_destroy(&e->names);
503 constraint = constraint->next;
507 kfree(cladatum->comkey);
512 static int role_destroy(void *key, void *datum, void *p)
514 struct role_datum *role;
518 ebitmap_destroy(&role->dominates);
519 ebitmap_destroy(&role->types);
524 static int type_destroy(void *key, void *datum, void *p)
531 static int user_destroy(void *key, void *datum, void *p)
533 struct user_datum *usrdatum;
537 ebitmap_destroy(&usrdatum->roles);
538 ebitmap_destroy(&usrdatum->range.level[0].cat);
539 ebitmap_destroy(&usrdatum->range.level[1].cat);
540 ebitmap_destroy(&usrdatum->dfltlevel.cat);
545 static int sens_destroy(void *key, void *datum, void *p)
547 struct level_datum *levdatum;
551 ebitmap_destroy(&levdatum->level->cat);
552 kfree(levdatum->level);
557 static int cat_destroy(void *key, void *datum, void *p)
564 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
576 static void ocontext_destroy(struct ocontext *c, int i)
578 context_destroy(&c->context[0]);
579 context_destroy(&c->context[1]);
580 if (i == OCON_ISID || i == OCON_FS ||
581 i == OCON_NETIF || i == OCON_FSUSE)
587 * Free any memory allocated by a policy database structure.
589 void policydb_destroy(struct policydb *p)
591 struct ocontext *c, *ctmp;
592 struct genfs *g, *gtmp;
594 struct role_allow *ra, *lra = NULL;
595 struct role_trans *tr, *ltr = NULL;
596 struct range_trans *rt, *lrt = NULL;
598 for (i = 0; i < SYM_NUM; i++) {
600 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
601 hashtab_destroy(p->symtab[i].table);
604 for (i = 0; i < SYM_NUM; i++)
605 kfree(p->sym_val_to_name[i]);
607 kfree(p->class_val_to_struct);
608 kfree(p->role_val_to_struct);
609 kfree(p->user_val_to_struct);
611 avtab_destroy(&p->te_avtab);
613 for (i = 0; i < OCON_NUM; i++) {
619 ocontext_destroy(ctmp,i);
621 p->ocontexts[i] = NULL;
632 ocontext_destroy(ctmp,OCON_FSUSE);
640 cond_policydb_destroy(p);
642 for (tr = p->role_tr; tr; tr = tr->next) {
649 for (ra = p->role_allow; ra; ra = ra -> next) {
656 for (rt = p->range_tr; rt; rt = rt -> next) {
659 ebitmap_destroy(&lrt->target_range.level[0].cat);
660 ebitmap_destroy(&lrt->target_range.level[1].cat);
666 ebitmap_destroy(&lrt->target_range.level[0].cat);
667 ebitmap_destroy(&lrt->target_range.level[1].cat);
671 if (p->type_attr_map) {
672 for (i = 0; i < p->p_types.nprim; i++)
673 ebitmap_destroy(&p->type_attr_map[i]);
675 kfree(p->type_attr_map);
681 * Load the initial SIDs specified in a policy database
682 * structure into a SID table.
684 int policydb_load_isids(struct policydb *p, struct sidtab *s)
686 struct ocontext *head, *c;
691 printk(KERN_ERR "security: out of memory on SID table init\n");
695 head = p->ocontexts[OCON_ISID];
696 for (c = head; c; c = c->next) {
697 if (!c->context[0].user) {
698 printk(KERN_ERR "security: SID %s was never "
699 "defined.\n", c->u.name);
703 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
704 printk(KERN_ERR "security: unable to load initial "
705 "SID %s.\n", c->u.name);
715 * Return 1 if the fields in the security context
716 * structure `c' are valid. Return 0 otherwise.
718 int policydb_context_isvalid(struct policydb *p, struct context *c)
720 struct role_datum *role;
721 struct user_datum *usrdatum;
723 if (!c->role || c->role > p->p_roles.nprim)
726 if (!c->user || c->user > p->p_users.nprim)
729 if (!c->type || c->type > p->p_types.nprim)
732 if (c->role != OBJECT_R_VAL) {
734 * Role must be authorized for the type.
736 role = p->role_val_to_struct[c->role - 1];
737 if (!ebitmap_get_bit(&role->types,
739 /* role may not be associated with type */
743 * User must be authorized for the role.
745 usrdatum = p->user_val_to_struct[c->user - 1];
749 if (!ebitmap_get_bit(&usrdatum->roles,
751 /* user may not be associated with role */
755 if (!mls_context_isvalid(p, c))
762 * Read a MLS range structure from a policydb binary
763 * representation file.
765 static int mls_read_range_helper(struct mls_range *r, void *fp)
771 rc = next_entry(buf, fp, sizeof(u32));
775 items = le32_to_cpu(buf[0]);
776 if (items > ARRAY_SIZE(buf)) {
777 printk(KERN_ERR "security: mls: range overflow\n");
781 rc = next_entry(buf, fp, sizeof(u32) * items);
783 printk(KERN_ERR "security: mls: truncated range\n");
786 r->level[0].sens = le32_to_cpu(buf[0]);
788 r->level[1].sens = le32_to_cpu(buf[1]);
790 r->level[1].sens = r->level[0].sens;
792 rc = ebitmap_read(&r->level[0].cat, fp);
794 printk(KERN_ERR "security: mls: error reading low "
799 rc = ebitmap_read(&r->level[1].cat, fp);
801 printk(KERN_ERR "security: mls: error reading high "
806 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
808 printk(KERN_ERR "security: mls: out of memory\n");
817 ebitmap_destroy(&r->level[0].cat);
822 * Read and validate a security context structure
823 * from a policydb binary representation file.
825 static int context_read_and_validate(struct context *c,
832 rc = next_entry(buf, fp, sizeof buf);
834 printk(KERN_ERR "security: context truncated\n");
837 c->user = le32_to_cpu(buf[0]);
838 c->role = le32_to_cpu(buf[1]);
839 c->type = le32_to_cpu(buf[2]);
840 if (p->policyvers >= POLICYDB_VERSION_MLS) {
841 if (mls_read_range_helper(&c->range, fp)) {
842 printk(KERN_ERR "security: error reading MLS range of "
849 if (!policydb_context_isvalid(p, c)) {
850 printk(KERN_ERR "security: invalid security context\n");
859 * The following *_read functions are used to
860 * read the symbol data from a policy database
861 * binary representation file.
864 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
867 struct perm_datum *perdatum;
872 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
878 rc = next_entry(buf, fp, sizeof buf);
882 len = le32_to_cpu(buf[0]);
883 perdatum->value = le32_to_cpu(buf[1]);
885 key = kmalloc(len + 1,GFP_KERNEL);
890 rc = next_entry(key, fp, len);
895 rc = hashtab_insert(h, key, perdatum);
901 perm_destroy(key, perdatum, NULL);
905 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
908 struct common_datum *comdatum;
913 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
919 rc = next_entry(buf, fp, sizeof buf);
923 len = le32_to_cpu(buf[0]);
924 comdatum->value = le32_to_cpu(buf[1]);
926 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
929 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
930 nel = le32_to_cpu(buf[3]);
932 key = kmalloc(len + 1,GFP_KERNEL);
937 rc = next_entry(key, fp, len);
942 for (i = 0; i < nel; i++) {
943 rc = perm_read(p, comdatum->permissions.table, fp);
948 rc = hashtab_insert(h, key, comdatum);
954 common_destroy(key, comdatum, NULL);
958 static int read_cons_helper(struct constraint_node **nodep, int ncons,
959 int allowxtarget, void *fp)
961 struct constraint_node *c, *lc;
962 struct constraint_expr *e, *le;
968 for (i = 0; i < ncons; i++) {
969 c = kzalloc(sizeof(*c), GFP_KERNEL);
979 rc = next_entry(buf, fp, (sizeof(u32) * 2));
982 c->permissions = le32_to_cpu(buf[0]);
983 nexpr = le32_to_cpu(buf[1]);
986 for (j = 0; j < nexpr; j++) {
987 e = kzalloc(sizeof(*e), GFP_KERNEL);
997 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1000 e->expr_type = le32_to_cpu(buf[0]);
1001 e->attr = le32_to_cpu(buf[1]);
1002 e->op = le32_to_cpu(buf[2]);
1004 switch (e->expr_type) {
1016 if (depth == (CEXPR_MAXDEPTH - 1))
1021 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1023 if (depth == (CEXPR_MAXDEPTH - 1))
1026 if (ebitmap_read(&e->names, fp))
1042 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1045 struct class_datum *cladatum;
1047 u32 len, len2, ncons, nel;
1050 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1056 rc = next_entry(buf, fp, sizeof(u32)*6);
1060 len = le32_to_cpu(buf[0]);
1061 len2 = le32_to_cpu(buf[1]);
1062 cladatum->value = le32_to_cpu(buf[2]);
1064 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1067 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1068 nel = le32_to_cpu(buf[4]);
1070 ncons = le32_to_cpu(buf[5]);
1072 key = kmalloc(len + 1,GFP_KERNEL);
1077 rc = next_entry(key, fp, len);
1083 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1084 if (!cladatum->comkey) {
1088 rc = next_entry(cladatum->comkey, fp, len2);
1091 cladatum->comkey[len2] = 0;
1093 cladatum->comdatum = hashtab_search(p->p_commons.table,
1095 if (!cladatum->comdatum) {
1096 printk(KERN_ERR "security: unknown common %s\n",
1102 for (i = 0; i < nel; i++) {
1103 rc = perm_read(p, cladatum->permissions.table, fp);
1108 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1112 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1113 /* grab the validatetrans rules */
1114 rc = next_entry(buf, fp, sizeof(u32));
1117 ncons = le32_to_cpu(buf[0]);
1118 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1123 rc = hashtab_insert(h, key, cladatum);
1131 cls_destroy(key, cladatum, NULL);
1135 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1138 struct role_datum *role;
1143 role = kzalloc(sizeof(*role), GFP_KERNEL);
1149 rc = next_entry(buf, fp, sizeof buf);
1153 len = le32_to_cpu(buf[0]);
1154 role->value = le32_to_cpu(buf[1]);
1156 key = kmalloc(len + 1,GFP_KERNEL);
1161 rc = next_entry(key, fp, len);
1166 rc = ebitmap_read(&role->dominates, fp);
1170 rc = ebitmap_read(&role->types, fp);
1174 if (strcmp(key, OBJECT_R) == 0) {
1175 if (role->value != OBJECT_R_VAL) {
1176 printk(KERN_ERR "Role %s has wrong value %d\n",
1177 OBJECT_R, role->value);
1185 rc = hashtab_insert(h, key, role);
1191 role_destroy(key, role, NULL);
1195 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1198 struct type_datum *typdatum;
1203 typdatum = kzalloc(sizeof(*typdatum),GFP_KERNEL);
1209 rc = next_entry(buf, fp, sizeof buf);
1213 len = le32_to_cpu(buf[0]);
1214 typdatum->value = le32_to_cpu(buf[1]);
1215 typdatum->primary = le32_to_cpu(buf[2]);
1217 key = kmalloc(len + 1,GFP_KERNEL);
1222 rc = next_entry(key, fp, len);
1227 rc = hashtab_insert(h, key, typdatum);
1233 type_destroy(key, typdatum, NULL);
1239 * Read a MLS level structure from a policydb binary
1240 * representation file.
1242 static int mls_read_level(struct mls_level *lp, void *fp)
1247 memset(lp, 0, sizeof(*lp));
1249 rc = next_entry(buf, fp, sizeof buf);
1251 printk(KERN_ERR "security: mls: truncated level\n");
1254 lp->sens = le32_to_cpu(buf[0]);
1256 if (ebitmap_read(&lp->cat, fp)) {
1257 printk(KERN_ERR "security: mls: error reading level "
1267 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1270 struct user_datum *usrdatum;
1275 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1281 rc = next_entry(buf, fp, sizeof buf);
1285 len = le32_to_cpu(buf[0]);
1286 usrdatum->value = le32_to_cpu(buf[1]);
1288 key = kmalloc(len + 1,GFP_KERNEL);
1293 rc = next_entry(key, fp, len);
1298 rc = ebitmap_read(&usrdatum->roles, fp);
1302 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1303 rc = mls_read_range_helper(&usrdatum->range, fp);
1306 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1311 rc = hashtab_insert(h, key, usrdatum);
1317 user_destroy(key, usrdatum, NULL);
1321 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1324 struct level_datum *levdatum;
1329 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1335 rc = next_entry(buf, fp, sizeof buf);
1339 len = le32_to_cpu(buf[0]);
1340 levdatum->isalias = le32_to_cpu(buf[1]);
1342 key = kmalloc(len + 1,GFP_ATOMIC);
1347 rc = next_entry(key, fp, len);
1352 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1353 if (!levdatum->level) {
1357 if (mls_read_level(levdatum->level, fp)) {
1362 rc = hashtab_insert(h, key, levdatum);
1368 sens_destroy(key, levdatum, NULL);
1372 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1375 struct cat_datum *catdatum;
1380 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1386 rc = next_entry(buf, fp, sizeof buf);
1390 len = le32_to_cpu(buf[0]);
1391 catdatum->value = le32_to_cpu(buf[1]);
1392 catdatum->isalias = le32_to_cpu(buf[2]);
1394 key = kmalloc(len + 1,GFP_ATOMIC);
1399 rc = next_entry(key, fp, len);
1404 rc = hashtab_insert(h, key, catdatum);
1411 cat_destroy(key, catdatum, NULL);
1415 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1427 extern int ss_initialized;
1430 * Read the configuration data from a policy database binary
1431 * representation file into a policy database structure.
1433 int policydb_read(struct policydb *p, void *fp)
1435 struct role_allow *ra, *lra;
1436 struct role_trans *tr, *ltr;
1437 struct ocontext *l, *c, *newc;
1438 struct genfs *genfs_p, *genfs, *newgenfs;
1441 u32 len, len2, config, nprim, nel, nel2;
1443 struct policydb_compat_info *info;
1444 struct range_trans *rt, *lrt;
1448 rc = policydb_init(p);
1452 /* Read the magic number and string length. */
1453 rc = next_entry(buf, fp, sizeof(u32)* 2);
1457 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1458 printk(KERN_ERR "security: policydb magic number 0x%x does "
1459 "not match expected magic number 0x%x\n",
1460 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1464 len = le32_to_cpu(buf[1]);
1465 if (len != strlen(POLICYDB_STRING)) {
1466 printk(KERN_ERR "security: policydb string length %d does not "
1467 "match expected length %Zu\n",
1468 len, strlen(POLICYDB_STRING));
1471 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1472 if (!policydb_str) {
1473 printk(KERN_ERR "security: unable to allocate memory for policydb "
1474 "string of length %d\n", len);
1478 rc = next_entry(policydb_str, fp, len);
1480 printk(KERN_ERR "security: truncated policydb string identifier\n");
1481 kfree(policydb_str);
1484 policydb_str[len] = 0;
1485 if (strcmp(policydb_str, POLICYDB_STRING)) {
1486 printk(KERN_ERR "security: policydb string %s does not match "
1487 "my string %s\n", policydb_str, POLICYDB_STRING);
1488 kfree(policydb_str);
1491 /* Done with policydb_str. */
1492 kfree(policydb_str);
1493 policydb_str = NULL;
1495 /* Read the version, config, and table sizes. */
1496 rc = next_entry(buf, fp, sizeof(u32)*4);
1500 p->policyvers = le32_to_cpu(buf[0]);
1501 if (p->policyvers < POLICYDB_VERSION_MIN ||
1502 p->policyvers > POLICYDB_VERSION_MAX) {
1503 printk(KERN_ERR "security: policydb version %d does not match "
1504 "my version range %d-%d\n",
1505 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1509 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1510 if (ss_initialized && !selinux_mls_enabled) {
1511 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1515 selinux_mls_enabled = 1;
1516 config |= POLICYDB_CONFIG_MLS;
1518 if (p->policyvers < POLICYDB_VERSION_MLS) {
1519 printk(KERN_ERR "security policydb version %d (MLS) "
1520 "not backwards compatible\n", p->policyvers);
1524 if (ss_initialized && selinux_mls_enabled) {
1525 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1531 info = policydb_lookup_compat(p->policyvers);
1533 printk(KERN_ERR "security: unable to find policy compat info "
1534 "for version %d\n", p->policyvers);
1538 if (le32_to_cpu(buf[2]) != info->sym_num ||
1539 le32_to_cpu(buf[3]) != info->ocon_num) {
1540 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1541 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1542 le32_to_cpu(buf[3]),
1543 info->sym_num, info->ocon_num);
1547 for (i = 0; i < info->sym_num; i++) {
1548 rc = next_entry(buf, fp, sizeof(u32)*2);
1551 nprim = le32_to_cpu(buf[0]);
1552 nel = le32_to_cpu(buf[1]);
1553 for (j = 0; j < nel; j++) {
1554 rc = read_f[i](p, p->symtab[i].table, fp);
1559 p->symtab[i].nprim = nprim;
1562 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1566 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1567 rc = cond_read_list(p, fp);
1572 rc = next_entry(buf, fp, sizeof(u32));
1575 nel = le32_to_cpu(buf[0]);
1577 for (i = 0; i < nel; i++) {
1578 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
1588 rc = next_entry(buf, fp, sizeof(u32)*3);
1591 tr->role = le32_to_cpu(buf[0]);
1592 tr->type = le32_to_cpu(buf[1]);
1593 tr->new_role = le32_to_cpu(buf[2]);
1597 rc = next_entry(buf, fp, sizeof(u32));
1600 nel = le32_to_cpu(buf[0]);
1602 for (i = 0; i < nel; i++) {
1603 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1613 rc = next_entry(buf, fp, sizeof(u32)*2);
1616 ra->role = le32_to_cpu(buf[0]);
1617 ra->new_role = le32_to_cpu(buf[1]);
1621 rc = policydb_index_classes(p);
1625 rc = policydb_index_others(p);
1629 for (i = 0; i < info->ocon_num; i++) {
1630 rc = next_entry(buf, fp, sizeof(u32));
1633 nel = le32_to_cpu(buf[0]);
1635 for (j = 0; j < nel; j++) {
1636 c = kzalloc(sizeof(*c), GFP_KERNEL);
1644 p->ocontexts[i] = c;
1650 rc = next_entry(buf, fp, sizeof(u32));
1653 c->sid[0] = le32_to_cpu(buf[0]);
1654 rc = context_read_and_validate(&c->context[0], p, fp);
1660 rc = next_entry(buf, fp, sizeof(u32));
1663 len = le32_to_cpu(buf[0]);
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);
1676 rc = context_read_and_validate(&c->context[1], p, fp);
1681 rc = next_entry(buf, fp, sizeof(u32)*3);
1684 c->u.port.protocol = le32_to_cpu(buf[0]);
1685 c->u.port.low_port = le32_to_cpu(buf[1]);
1686 c->u.port.high_port = le32_to_cpu(buf[2]);
1687 rc = context_read_and_validate(&c->context[0], p, fp);
1692 rc = next_entry(buf, fp, sizeof(u32)* 2);
1695 c->u.node.addr = le32_to_cpu(buf[0]);
1696 c->u.node.mask = le32_to_cpu(buf[1]);
1697 rc = context_read_and_validate(&c->context[0], p, fp);
1702 rc = next_entry(buf, fp, sizeof(u32)*2);
1705 c->v.behavior = le32_to_cpu(buf[0]);
1706 if (c->v.behavior > SECURITY_FS_USE_NONE)
1708 len = le32_to_cpu(buf[1]);
1709 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1714 rc = next_entry(c->u.name, fp, len);
1718 rc = context_read_and_validate(&c->context[0], p, fp);
1725 rc = next_entry(buf, fp, sizeof(u32) * 8);
1728 for (k = 0; k < 4; k++)
1729 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1730 for (k = 0; k < 4; k++)
1731 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1732 if (context_read_and_validate(&c->context[0], p, fp))
1740 rc = next_entry(buf, fp, sizeof(u32));
1743 nel = le32_to_cpu(buf[0]);
1746 for (i = 0; i < nel; i++) {
1747 rc = next_entry(buf, fp, sizeof(u32));
1750 len = le32_to_cpu(buf[0]);
1751 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1757 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1758 if (!newgenfs->fstype) {
1763 rc = next_entry(newgenfs->fstype, fp, len);
1765 kfree(newgenfs->fstype);
1769 newgenfs->fstype[len] = 0;
1770 for (genfs_p = NULL, genfs = p->genfs; genfs;
1771 genfs_p = genfs, genfs = genfs->next) {
1772 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1773 printk(KERN_ERR "security: dup genfs "
1774 "fstype %s\n", newgenfs->fstype);
1775 kfree(newgenfs->fstype);
1779 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1782 newgenfs->next = genfs;
1784 genfs_p->next = newgenfs;
1786 p->genfs = newgenfs;
1787 rc = next_entry(buf, fp, sizeof(u32));
1790 nel2 = le32_to_cpu(buf[0]);
1791 for (j = 0; j < nel2; j++) {
1792 rc = next_entry(buf, fp, sizeof(u32));
1795 len = le32_to_cpu(buf[0]);
1797 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1803 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1804 if (!newc->u.name) {
1808 rc = next_entry(newc->u.name, fp, len);
1811 newc->u.name[len] = 0;
1812 rc = next_entry(buf, fp, sizeof(u32));
1815 newc->v.sclass = le32_to_cpu(buf[0]);
1816 if (context_read_and_validate(&newc->context[0], p, fp))
1818 for (l = NULL, c = newgenfs->head; c;
1819 l = c, c = c->next) {
1820 if (!strcmp(newc->u.name, c->u.name) &&
1821 (!c->v.sclass || !newc->v.sclass ||
1822 newc->v.sclass == c->v.sclass)) {
1823 printk(KERN_ERR "security: dup genfs "
1825 newgenfs->fstype, c->u.name);
1828 len = strlen(newc->u.name);
1829 len2 = strlen(c->u.name);
1838 newgenfs->head = newc;
1842 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1843 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
1844 rc = next_entry(buf, fp, sizeof(u32));
1847 nel = le32_to_cpu(buf[0]);
1849 for (i = 0; i < nel; i++) {
1850 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1859 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1862 rt->source_type = le32_to_cpu(buf[0]);
1863 rt->target_type = le32_to_cpu(buf[1]);
1865 rc = next_entry(buf, fp, sizeof(u32));
1868 rt->target_class = le32_to_cpu(buf[0]);
1870 rt->target_class = SECCLASS_PROCESS;
1871 rc = mls_read_range_helper(&rt->target_range, fp);
1878 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1879 if (!p->type_attr_map)
1882 for (i = 0; i < p->p_types.nprim; i++) {
1883 ebitmap_init(&p->type_attr_map[i]);
1884 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1885 if (ebitmap_read(&p->type_attr_map[i], fp))
1888 /* add the type itself as the degenerate case */
1889 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1897 ocontext_destroy(newc,OCON_FSUSE);
1901 policydb_destroy(p);