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 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for the policy capability bitmap
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
21 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, version 2.
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
36 #include "conditional.h"
42 static char *symtab_name[SYM_NUM] = {
54 int selinux_mls_enabled = 0;
56 static unsigned int symtab_sizes[SYM_NUM] = {
67 struct policydb_compat_info {
73 /* These need to be updated if SYM_NUM or OCON_NUM changes */
74 static struct policydb_compat_info policydb_compat[] = {
76 .version = POLICYDB_VERSION_BASE,
77 .sym_num = SYM_NUM - 3,
78 .ocon_num = OCON_NUM - 1,
81 .version = POLICYDB_VERSION_BOOL,
82 .sym_num = SYM_NUM - 2,
83 .ocon_num = OCON_NUM - 1,
86 .version = POLICYDB_VERSION_IPV6,
87 .sym_num = SYM_NUM - 2,
91 .version = POLICYDB_VERSION_NLCLASS,
92 .sym_num = SYM_NUM - 2,
96 .version = POLICYDB_VERSION_MLS,
101 .version = POLICYDB_VERSION_AVTAB,
103 .ocon_num = OCON_NUM,
106 .version = POLICYDB_VERSION_RANGETRANS,
108 .ocon_num = OCON_NUM,
111 .version = POLICYDB_VERSION_POLCAP,
113 .ocon_num = OCON_NUM,
117 static struct policydb_compat_info *policydb_lookup_compat(int version)
120 struct policydb_compat_info *info = NULL;
122 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
123 if (policydb_compat[i].version == version) {
124 info = &policydb_compat[i];
132 * Initialize the role table.
134 static int roles_init(struct policydb *p)
138 struct role_datum *role;
140 role = kzalloc(sizeof(*role), GFP_KERNEL);
145 role->value = ++p->p_roles.nprim;
146 if (role->value != OBJECT_R_VAL) {
150 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
155 strcpy(key, OBJECT_R);
156 rc = hashtab_insert(p->p_roles.table, key, role);
170 * Initialize a policy database structure.
172 static int policydb_init(struct policydb *p)
176 memset(p, 0, sizeof(*p));
178 for (i = 0; i < SYM_NUM; i++) {
179 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
181 goto out_free_symtab;
184 rc = avtab_init(&p->te_avtab);
186 goto out_free_symtab;
190 goto out_free_symtab;
192 rc = cond_policydb_init(p);
194 goto out_free_symtab;
196 ebitmap_init(&p->policycaps);
202 for (i = 0; i < SYM_NUM; i++)
203 hashtab_destroy(p->symtab[i].table);
208 * The following *_index functions are used to
209 * define the val_to_name and val_to_struct arrays
210 * in a policy database structure. The val_to_name
211 * arrays are used when converting security context
212 * structures into string representations. The
213 * val_to_struct arrays are used when the attributes
214 * of a class, role, or user are needed.
217 static int common_index(void *key, void *datum, void *datap)
220 struct common_datum *comdatum;
224 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
226 p->p_common_val_to_name[comdatum->value - 1] = key;
230 static int class_index(void *key, void *datum, void *datap)
233 struct class_datum *cladatum;
237 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
239 p->p_class_val_to_name[cladatum->value - 1] = key;
240 p->class_val_to_struct[cladatum->value - 1] = cladatum;
244 static int role_index(void *key, void *datum, void *datap)
247 struct role_datum *role;
251 if (!role->value || role->value > p->p_roles.nprim)
253 p->p_role_val_to_name[role->value - 1] = key;
254 p->role_val_to_struct[role->value - 1] = role;
258 static int type_index(void *key, void *datum, void *datap)
261 struct type_datum *typdatum;
266 if (typdatum->primary) {
267 if (!typdatum->value || typdatum->value > p->p_types.nprim)
269 p->p_type_val_to_name[typdatum->value - 1] = key;
275 static int user_index(void *key, void *datum, void *datap)
278 struct user_datum *usrdatum;
282 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
284 p->p_user_val_to_name[usrdatum->value - 1] = key;
285 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
289 static int sens_index(void *key, void *datum, void *datap)
292 struct level_datum *levdatum;
297 if (!levdatum->isalias) {
298 if (!levdatum->level->sens ||
299 levdatum->level->sens > p->p_levels.nprim)
301 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
307 static int cat_index(void *key, void *datum, void *datap)
310 struct cat_datum *catdatum;
315 if (!catdatum->isalias) {
316 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
318 p->p_cat_val_to_name[catdatum->value - 1] = key;
324 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
337 * Define the common val_to_name array and the class
338 * val_to_name and val_to_struct arrays in a policy
339 * database structure.
341 * Caller must clean up upon failure.
343 static int policydb_index_classes(struct policydb *p)
347 p->p_common_val_to_name =
348 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
349 if (!p->p_common_val_to_name) {
354 rc = hashtab_map(p->p_commons.table, common_index, p);
358 p->class_val_to_struct =
359 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
360 if (!p->class_val_to_struct) {
365 p->p_class_val_to_name =
366 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
367 if (!p->p_class_val_to_name) {
372 rc = hashtab_map(p->p_classes.table, class_index, p);
378 static void symtab_hash_eval(struct symtab *s)
382 for (i = 0; i < SYM_NUM; i++) {
383 struct hashtab *h = s[i].table;
384 struct hashtab_info info;
386 hashtab_stat(h, &info);
387 printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, "
388 "longest chain length %d\n", symtab_name[i], h->nel,
389 info.slots_used, h->size, info.max_chain_len);
395 * Define the other val_to_name and val_to_struct arrays
396 * in a policy database structure.
398 * Caller must clean up on failure.
400 static int policydb_index_others(struct policydb *p)
404 printk(KERN_DEBUG "security: %d users, %d roles, %d types, %d bools",
405 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
406 if (selinux_mls_enabled)
407 printk(", %d sens, %d cats", p->p_levels.nprim,
411 printk(KERN_DEBUG "security: %d classes, %d rules\n",
412 p->p_classes.nprim, p->te_avtab.nel);
415 avtab_hash_eval(&p->te_avtab, "rules");
416 symtab_hash_eval(p->symtab);
419 p->role_val_to_struct =
420 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
422 if (!p->role_val_to_struct) {
427 p->user_val_to_struct =
428 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
430 if (!p->user_val_to_struct) {
435 if (cond_init_bool_indexes(p)) {
440 for (i = SYM_ROLES; i < SYM_NUM; i++) {
441 p->sym_val_to_name[i] =
442 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
443 if (!p->sym_val_to_name[i]) {
447 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
457 * The following *_destroy functions are used to
458 * free any memory allocated for each kind of
459 * symbol data in the policy database.
462 static int perm_destroy(void *key, void *datum, void *p)
469 static int common_destroy(void *key, void *datum, void *p)
471 struct common_datum *comdatum;
475 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
476 hashtab_destroy(comdatum->permissions.table);
481 static int cls_destroy(void *key, void *datum, void *p)
483 struct class_datum *cladatum;
484 struct constraint_node *constraint, *ctemp;
485 struct constraint_expr *e, *etmp;
489 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
490 hashtab_destroy(cladatum->permissions.table);
491 constraint = cladatum->constraints;
493 e = constraint->expr;
495 ebitmap_destroy(&e->names);
501 constraint = constraint->next;
505 constraint = cladatum->validatetrans;
507 e = constraint->expr;
509 ebitmap_destroy(&e->names);
515 constraint = constraint->next;
519 kfree(cladatum->comkey);
524 static int role_destroy(void *key, void *datum, void *p)
526 struct role_datum *role;
530 ebitmap_destroy(&role->dominates);
531 ebitmap_destroy(&role->types);
536 static int type_destroy(void *key, void *datum, void *p)
543 static int user_destroy(void *key, void *datum, void *p)
545 struct user_datum *usrdatum;
549 ebitmap_destroy(&usrdatum->roles);
550 ebitmap_destroy(&usrdatum->range.level[0].cat);
551 ebitmap_destroy(&usrdatum->range.level[1].cat);
552 ebitmap_destroy(&usrdatum->dfltlevel.cat);
557 static int sens_destroy(void *key, void *datum, void *p)
559 struct level_datum *levdatum;
563 ebitmap_destroy(&levdatum->level->cat);
564 kfree(levdatum->level);
569 static int cat_destroy(void *key, void *datum, void *p)
576 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
588 static void ocontext_destroy(struct ocontext *c, int i)
590 context_destroy(&c->context[0]);
591 context_destroy(&c->context[1]);
592 if (i == OCON_ISID || i == OCON_FS ||
593 i == OCON_NETIF || i == OCON_FSUSE)
599 * Free any memory allocated by a policy database structure.
601 void policydb_destroy(struct policydb *p)
603 struct ocontext *c, *ctmp;
604 struct genfs *g, *gtmp;
606 struct role_allow *ra, *lra = NULL;
607 struct role_trans *tr, *ltr = NULL;
608 struct range_trans *rt, *lrt = NULL;
610 for (i = 0; i < SYM_NUM; i++) {
612 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
613 hashtab_destroy(p->symtab[i].table);
616 for (i = 0; i < SYM_NUM; i++)
617 kfree(p->sym_val_to_name[i]);
619 kfree(p->class_val_to_struct);
620 kfree(p->role_val_to_struct);
621 kfree(p->user_val_to_struct);
623 avtab_destroy(&p->te_avtab);
625 for (i = 0; i < OCON_NUM; i++) {
631 ocontext_destroy(ctmp,i);
633 p->ocontexts[i] = NULL;
644 ocontext_destroy(ctmp,OCON_FSUSE);
652 cond_policydb_destroy(p);
654 for (tr = p->role_tr; tr; tr = tr->next) {
661 for (ra = p->role_allow; ra; ra = ra -> next) {
668 for (rt = p->range_tr; rt; rt = rt -> next) {
671 ebitmap_destroy(&lrt->target_range.level[0].cat);
672 ebitmap_destroy(&lrt->target_range.level[1].cat);
678 ebitmap_destroy(&lrt->target_range.level[0].cat);
679 ebitmap_destroy(&lrt->target_range.level[1].cat);
683 if (p->type_attr_map) {
684 for (i = 0; i < p->p_types.nprim; i++)
685 ebitmap_destroy(&p->type_attr_map[i]);
687 kfree(p->type_attr_map);
688 kfree(p->undefined_perms);
689 ebitmap_destroy(&p->policycaps);
695 * Load the initial SIDs specified in a policy database
696 * structure into a SID table.
698 int policydb_load_isids(struct policydb *p, struct sidtab *s)
700 struct ocontext *head, *c;
705 printk(KERN_ERR "security: out of memory on SID table init\n");
709 head = p->ocontexts[OCON_ISID];
710 for (c = head; c; c = c->next) {
711 if (!c->context[0].user) {
712 printk(KERN_ERR "security: SID %s was never "
713 "defined.\n", c->u.name);
717 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
718 printk(KERN_ERR "security: unable to load initial "
719 "SID %s.\n", c->u.name);
728 int policydb_class_isvalid(struct policydb *p, unsigned int class)
730 if (!class || class > p->p_classes.nprim)
735 int policydb_role_isvalid(struct policydb *p, unsigned int role)
737 if (!role || role > p->p_roles.nprim)
742 int policydb_type_isvalid(struct policydb *p, unsigned int type)
744 if (!type || type > p->p_types.nprim)
750 * Return 1 if the fields in the security context
751 * structure `c' are valid. Return 0 otherwise.
753 int policydb_context_isvalid(struct policydb *p, struct context *c)
755 struct role_datum *role;
756 struct user_datum *usrdatum;
758 if (!c->role || c->role > p->p_roles.nprim)
761 if (!c->user || c->user > p->p_users.nprim)
764 if (!c->type || c->type > p->p_types.nprim)
767 if (c->role != OBJECT_R_VAL) {
769 * Role must be authorized for the type.
771 role = p->role_val_to_struct[c->role - 1];
772 if (!ebitmap_get_bit(&role->types,
774 /* role may not be associated with type */
778 * User must be authorized for the role.
780 usrdatum = p->user_val_to_struct[c->user - 1];
784 if (!ebitmap_get_bit(&usrdatum->roles,
786 /* user may not be associated with role */
790 if (!mls_context_isvalid(p, c))
797 * Read a MLS range structure from a policydb binary
798 * representation file.
800 static int mls_read_range_helper(struct mls_range *r, void *fp)
806 rc = next_entry(buf, fp, sizeof(u32));
810 items = le32_to_cpu(buf[0]);
811 if (items > ARRAY_SIZE(buf)) {
812 printk(KERN_ERR "security: mls: range overflow\n");
816 rc = next_entry(buf, fp, sizeof(u32) * items);
818 printk(KERN_ERR "security: mls: truncated range\n");
821 r->level[0].sens = le32_to_cpu(buf[0]);
823 r->level[1].sens = le32_to_cpu(buf[1]);
825 r->level[1].sens = r->level[0].sens;
827 rc = ebitmap_read(&r->level[0].cat, fp);
829 printk(KERN_ERR "security: mls: error reading low "
834 rc = ebitmap_read(&r->level[1].cat, fp);
836 printk(KERN_ERR "security: mls: error reading high "
841 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
843 printk(KERN_ERR "security: mls: out of memory\n");
852 ebitmap_destroy(&r->level[0].cat);
857 * Read and validate a security context structure
858 * from a policydb binary representation file.
860 static int context_read_and_validate(struct context *c,
867 rc = next_entry(buf, fp, sizeof buf);
869 printk(KERN_ERR "security: context truncated\n");
872 c->user = le32_to_cpu(buf[0]);
873 c->role = le32_to_cpu(buf[1]);
874 c->type = le32_to_cpu(buf[2]);
875 if (p->policyvers >= POLICYDB_VERSION_MLS) {
876 if (mls_read_range_helper(&c->range, fp)) {
877 printk(KERN_ERR "security: error reading MLS range of "
884 if (!policydb_context_isvalid(p, c)) {
885 printk(KERN_ERR "security: invalid security context\n");
894 * The following *_read functions are used to
895 * read the symbol data from a policy database
896 * binary representation file.
899 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
902 struct perm_datum *perdatum;
907 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
913 rc = next_entry(buf, fp, sizeof buf);
917 len = le32_to_cpu(buf[0]);
918 perdatum->value = le32_to_cpu(buf[1]);
920 key = kmalloc(len + 1,GFP_KERNEL);
925 rc = next_entry(key, fp, len);
930 rc = hashtab_insert(h, key, perdatum);
936 perm_destroy(key, perdatum, NULL);
940 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
943 struct common_datum *comdatum;
948 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
954 rc = next_entry(buf, fp, sizeof buf);
958 len = le32_to_cpu(buf[0]);
959 comdatum->value = le32_to_cpu(buf[1]);
961 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
964 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
965 nel = le32_to_cpu(buf[3]);
967 key = kmalloc(len + 1,GFP_KERNEL);
972 rc = next_entry(key, fp, len);
977 for (i = 0; i < nel; i++) {
978 rc = perm_read(p, comdatum->permissions.table, fp);
983 rc = hashtab_insert(h, key, comdatum);
989 common_destroy(key, comdatum, NULL);
993 static int read_cons_helper(struct constraint_node **nodep, int ncons,
994 int allowxtarget, void *fp)
996 struct constraint_node *c, *lc;
997 struct constraint_expr *e, *le;
1000 int rc, i, j, depth;
1003 for (i = 0; i < ncons; i++) {
1004 c = kzalloc(sizeof(*c), GFP_KERNEL);
1014 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1017 c->permissions = le32_to_cpu(buf[0]);
1018 nexpr = le32_to_cpu(buf[1]);
1021 for (j = 0; j < nexpr; j++) {
1022 e = kzalloc(sizeof(*e), GFP_KERNEL);
1032 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1035 e->expr_type = le32_to_cpu(buf[0]);
1036 e->attr = le32_to_cpu(buf[1]);
1037 e->op = le32_to_cpu(buf[2]);
1039 switch (e->expr_type) {
1051 if (depth == (CEXPR_MAXDEPTH - 1))
1056 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1058 if (depth == (CEXPR_MAXDEPTH - 1))
1061 if (ebitmap_read(&e->names, fp))
1077 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1080 struct class_datum *cladatum;
1082 u32 len, len2, ncons, nel;
1085 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1091 rc = next_entry(buf, fp, sizeof(u32)*6);
1095 len = le32_to_cpu(buf[0]);
1096 len2 = le32_to_cpu(buf[1]);
1097 cladatum->value = le32_to_cpu(buf[2]);
1099 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1102 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1103 nel = le32_to_cpu(buf[4]);
1105 ncons = le32_to_cpu(buf[5]);
1107 key = kmalloc(len + 1,GFP_KERNEL);
1112 rc = next_entry(key, fp, len);
1118 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1119 if (!cladatum->comkey) {
1123 rc = next_entry(cladatum->comkey, fp, len2);
1126 cladatum->comkey[len2] = 0;
1128 cladatum->comdatum = hashtab_search(p->p_commons.table,
1130 if (!cladatum->comdatum) {
1131 printk(KERN_ERR "security: unknown common %s\n",
1137 for (i = 0; i < nel; i++) {
1138 rc = perm_read(p, cladatum->permissions.table, fp);
1143 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1147 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1148 /* grab the validatetrans rules */
1149 rc = next_entry(buf, fp, sizeof(u32));
1152 ncons = le32_to_cpu(buf[0]);
1153 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1158 rc = hashtab_insert(h, key, cladatum);
1166 cls_destroy(key, cladatum, NULL);
1170 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1173 struct role_datum *role;
1178 role = kzalloc(sizeof(*role), GFP_KERNEL);
1184 rc = next_entry(buf, fp, sizeof buf);
1188 len = le32_to_cpu(buf[0]);
1189 role->value = le32_to_cpu(buf[1]);
1191 key = kmalloc(len + 1,GFP_KERNEL);
1196 rc = next_entry(key, fp, len);
1201 rc = ebitmap_read(&role->dominates, fp);
1205 rc = ebitmap_read(&role->types, fp);
1209 if (strcmp(key, OBJECT_R) == 0) {
1210 if (role->value != OBJECT_R_VAL) {
1211 printk(KERN_ERR "Role %s has wrong value %d\n",
1212 OBJECT_R, role->value);
1220 rc = hashtab_insert(h, key, role);
1226 role_destroy(key, role, NULL);
1230 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1233 struct type_datum *typdatum;
1238 typdatum = kzalloc(sizeof(*typdatum),GFP_KERNEL);
1244 rc = next_entry(buf, fp, sizeof buf);
1248 len = le32_to_cpu(buf[0]);
1249 typdatum->value = le32_to_cpu(buf[1]);
1250 typdatum->primary = le32_to_cpu(buf[2]);
1252 key = kmalloc(len + 1,GFP_KERNEL);
1257 rc = next_entry(key, fp, len);
1262 rc = hashtab_insert(h, key, typdatum);
1268 type_destroy(key, typdatum, NULL);
1274 * Read a MLS level structure from a policydb binary
1275 * representation file.
1277 static int mls_read_level(struct mls_level *lp, void *fp)
1282 memset(lp, 0, sizeof(*lp));
1284 rc = next_entry(buf, fp, sizeof buf);
1286 printk(KERN_ERR "security: mls: truncated level\n");
1289 lp->sens = le32_to_cpu(buf[0]);
1291 if (ebitmap_read(&lp->cat, fp)) {
1292 printk(KERN_ERR "security: mls: error reading level "
1303 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1306 struct user_datum *usrdatum;
1311 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1317 rc = next_entry(buf, fp, sizeof buf);
1321 len = le32_to_cpu(buf[0]);
1322 usrdatum->value = le32_to_cpu(buf[1]);
1324 key = kmalloc(len + 1,GFP_KERNEL);
1329 rc = next_entry(key, fp, len);
1334 rc = ebitmap_read(&usrdatum->roles, fp);
1338 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1339 rc = mls_read_range_helper(&usrdatum->range, fp);
1342 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1347 rc = hashtab_insert(h, key, usrdatum);
1353 user_destroy(key, usrdatum, NULL);
1357 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1360 struct level_datum *levdatum;
1365 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1371 rc = next_entry(buf, fp, sizeof buf);
1375 len = le32_to_cpu(buf[0]);
1376 levdatum->isalias = le32_to_cpu(buf[1]);
1378 key = kmalloc(len + 1,GFP_ATOMIC);
1383 rc = next_entry(key, fp, len);
1388 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1389 if (!levdatum->level) {
1393 if (mls_read_level(levdatum->level, fp)) {
1398 rc = hashtab_insert(h, key, levdatum);
1404 sens_destroy(key, levdatum, NULL);
1408 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1411 struct cat_datum *catdatum;
1416 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1422 rc = next_entry(buf, fp, sizeof buf);
1426 len = le32_to_cpu(buf[0]);
1427 catdatum->value = le32_to_cpu(buf[1]);
1428 catdatum->isalias = le32_to_cpu(buf[2]);
1430 key = kmalloc(len + 1,GFP_ATOMIC);
1435 rc = next_entry(key, fp, len);
1440 rc = hashtab_insert(h, key, catdatum);
1447 cat_destroy(key, catdatum, NULL);
1451 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1463 extern int ss_initialized;
1466 * Read the configuration data from a policy database binary
1467 * representation file into a policy database structure.
1469 int policydb_read(struct policydb *p, void *fp)
1471 struct role_allow *ra, *lra;
1472 struct role_trans *tr, *ltr;
1473 struct ocontext *l, *c, *newc;
1474 struct genfs *genfs_p, *genfs, *newgenfs;
1477 u32 len, len2, config, nprim, nel, nel2;
1479 struct policydb_compat_info *info;
1480 struct range_trans *rt, *lrt;
1484 rc = policydb_init(p);
1488 /* Read the magic number and string length. */
1489 rc = next_entry(buf, fp, sizeof(u32)* 2);
1493 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1494 printk(KERN_ERR "security: policydb magic number 0x%x does "
1495 "not match expected magic number 0x%x\n",
1496 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1500 len = le32_to_cpu(buf[1]);
1501 if (len != strlen(POLICYDB_STRING)) {
1502 printk(KERN_ERR "security: policydb string length %d does not "
1503 "match expected length %Zu\n",
1504 len, strlen(POLICYDB_STRING));
1507 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1508 if (!policydb_str) {
1509 printk(KERN_ERR "security: unable to allocate memory for policydb "
1510 "string of length %d\n", len);
1514 rc = next_entry(policydb_str, fp, len);
1516 printk(KERN_ERR "security: truncated policydb string identifier\n");
1517 kfree(policydb_str);
1520 policydb_str[len] = 0;
1521 if (strcmp(policydb_str, POLICYDB_STRING)) {
1522 printk(KERN_ERR "security: policydb string %s does not match "
1523 "my string %s\n", policydb_str, POLICYDB_STRING);
1524 kfree(policydb_str);
1527 /* Done with policydb_str. */
1528 kfree(policydb_str);
1529 policydb_str = NULL;
1531 /* Read the version, config, and table sizes. */
1532 rc = next_entry(buf, fp, sizeof(u32)*4);
1536 p->policyvers = le32_to_cpu(buf[0]);
1537 if (p->policyvers < POLICYDB_VERSION_MIN ||
1538 p->policyvers > POLICYDB_VERSION_MAX) {
1539 printk(KERN_ERR "security: policydb version %d does not match "
1540 "my version range %d-%d\n",
1541 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1545 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1546 if (ss_initialized && !selinux_mls_enabled) {
1547 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1551 selinux_mls_enabled = 1;
1552 config |= POLICYDB_CONFIG_MLS;
1554 if (p->policyvers < POLICYDB_VERSION_MLS) {
1555 printk(KERN_ERR "security policydb version %d (MLS) "
1556 "not backwards compatible\n", p->policyvers);
1560 if (ss_initialized && selinux_mls_enabled) {
1561 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1566 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1567 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
1569 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1570 ebitmap_read(&p->policycaps, fp) != 0)
1573 info = policydb_lookup_compat(p->policyvers);
1575 printk(KERN_ERR "security: unable to find policy compat info "
1576 "for version %d\n", p->policyvers);
1580 if (le32_to_cpu(buf[2]) != info->sym_num ||
1581 le32_to_cpu(buf[3]) != info->ocon_num) {
1582 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1583 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1584 le32_to_cpu(buf[3]),
1585 info->sym_num, info->ocon_num);
1589 for (i = 0; i < info->sym_num; i++) {
1590 rc = next_entry(buf, fp, sizeof(u32)*2);
1593 nprim = le32_to_cpu(buf[0]);
1594 nel = le32_to_cpu(buf[1]);
1595 for (j = 0; j < nel; j++) {
1596 rc = read_f[i](p, p->symtab[i].table, fp);
1601 p->symtab[i].nprim = nprim;
1604 rc = avtab_read(&p->te_avtab, fp, p);
1608 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1609 rc = cond_read_list(p, fp);
1614 rc = next_entry(buf, fp, sizeof(u32));
1617 nel = le32_to_cpu(buf[0]);
1619 for (i = 0; i < nel; i++) {
1620 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
1630 rc = next_entry(buf, fp, sizeof(u32)*3);
1633 tr->role = le32_to_cpu(buf[0]);
1634 tr->type = le32_to_cpu(buf[1]);
1635 tr->new_role = le32_to_cpu(buf[2]);
1636 if (!policydb_role_isvalid(p, tr->role) ||
1637 !policydb_type_isvalid(p, tr->type) ||
1638 !policydb_role_isvalid(p, tr->new_role)) {
1645 rc = next_entry(buf, fp, sizeof(u32));
1648 nel = le32_to_cpu(buf[0]);
1650 for (i = 0; i < nel; i++) {
1651 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1661 rc = next_entry(buf, fp, sizeof(u32)*2);
1664 ra->role = le32_to_cpu(buf[0]);
1665 ra->new_role = le32_to_cpu(buf[1]);
1666 if (!policydb_role_isvalid(p, ra->role) ||
1667 !policydb_role_isvalid(p, ra->new_role)) {
1674 rc = policydb_index_classes(p);
1678 rc = policydb_index_others(p);
1682 for (i = 0; i < info->ocon_num; i++) {
1683 rc = next_entry(buf, fp, sizeof(u32));
1686 nel = le32_to_cpu(buf[0]);
1688 for (j = 0; j < nel; j++) {
1689 c = kzalloc(sizeof(*c), GFP_KERNEL);
1697 p->ocontexts[i] = c;
1703 rc = next_entry(buf, fp, sizeof(u32));
1706 c->sid[0] = le32_to_cpu(buf[0]);
1707 rc = context_read_and_validate(&c->context[0], p, fp);
1713 rc = next_entry(buf, fp, sizeof(u32));
1716 len = le32_to_cpu(buf[0]);
1717 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1722 rc = next_entry(c->u.name, fp, len);
1726 rc = context_read_and_validate(&c->context[0], p, fp);
1729 rc = context_read_and_validate(&c->context[1], p, fp);
1734 rc = next_entry(buf, fp, sizeof(u32)*3);
1737 c->u.port.protocol = le32_to_cpu(buf[0]);
1738 c->u.port.low_port = le32_to_cpu(buf[1]);
1739 c->u.port.high_port = le32_to_cpu(buf[2]);
1740 rc = context_read_and_validate(&c->context[0], p, fp);
1745 rc = next_entry(buf, fp, sizeof(u32)* 2);
1748 c->u.node.addr = le32_to_cpu(buf[0]);
1749 c->u.node.mask = le32_to_cpu(buf[1]);
1750 rc = context_read_and_validate(&c->context[0], p, fp);
1755 rc = next_entry(buf, fp, sizeof(u32)*2);
1758 c->v.behavior = le32_to_cpu(buf[0]);
1759 if (c->v.behavior > SECURITY_FS_USE_NONE)
1761 len = le32_to_cpu(buf[1]);
1762 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1767 rc = next_entry(c->u.name, fp, len);
1771 rc = context_read_and_validate(&c->context[0], p, fp);
1778 rc = next_entry(buf, fp, sizeof(u32) * 8);
1781 for (k = 0; k < 4; k++)
1782 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1783 for (k = 0; k < 4; k++)
1784 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1785 if (context_read_and_validate(&c->context[0], p, fp))
1793 rc = next_entry(buf, fp, sizeof(u32));
1796 nel = le32_to_cpu(buf[0]);
1799 for (i = 0; i < nel; i++) {
1800 rc = next_entry(buf, fp, sizeof(u32));
1803 len = le32_to_cpu(buf[0]);
1804 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1810 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1811 if (!newgenfs->fstype) {
1816 rc = next_entry(newgenfs->fstype, fp, len);
1818 kfree(newgenfs->fstype);
1822 newgenfs->fstype[len] = 0;
1823 for (genfs_p = NULL, genfs = p->genfs; genfs;
1824 genfs_p = genfs, genfs = genfs->next) {
1825 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1826 printk(KERN_ERR "security: dup genfs "
1827 "fstype %s\n", newgenfs->fstype);
1828 kfree(newgenfs->fstype);
1832 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1835 newgenfs->next = genfs;
1837 genfs_p->next = newgenfs;
1839 p->genfs = newgenfs;
1840 rc = next_entry(buf, fp, sizeof(u32));
1843 nel2 = le32_to_cpu(buf[0]);
1844 for (j = 0; j < nel2; j++) {
1845 rc = next_entry(buf, fp, sizeof(u32));
1848 len = le32_to_cpu(buf[0]);
1850 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1856 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1857 if (!newc->u.name) {
1861 rc = next_entry(newc->u.name, fp, len);
1864 newc->u.name[len] = 0;
1865 rc = next_entry(buf, fp, sizeof(u32));
1868 newc->v.sclass = le32_to_cpu(buf[0]);
1869 if (context_read_and_validate(&newc->context[0], p, fp))
1871 for (l = NULL, c = newgenfs->head; c;
1872 l = c, c = c->next) {
1873 if (!strcmp(newc->u.name, c->u.name) &&
1874 (!c->v.sclass || !newc->v.sclass ||
1875 newc->v.sclass == c->v.sclass)) {
1876 printk(KERN_ERR "security: dup genfs "
1878 newgenfs->fstype, c->u.name);
1881 len = strlen(newc->u.name);
1882 len2 = strlen(c->u.name);
1891 newgenfs->head = newc;
1895 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1896 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
1897 rc = next_entry(buf, fp, sizeof(u32));
1900 nel = le32_to_cpu(buf[0]);
1902 for (i = 0; i < nel; i++) {
1903 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1912 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1915 rt->source_type = le32_to_cpu(buf[0]);
1916 rt->target_type = le32_to_cpu(buf[1]);
1918 rc = next_entry(buf, fp, sizeof(u32));
1921 rt->target_class = le32_to_cpu(buf[0]);
1923 rt->target_class = SECCLASS_PROCESS;
1924 if (!policydb_type_isvalid(p, rt->source_type) ||
1925 !policydb_type_isvalid(p, rt->target_type) ||
1926 !policydb_class_isvalid(p, rt->target_class)) {
1930 rc = mls_read_range_helper(&rt->target_range, fp);
1933 if (!mls_range_isvalid(p, &rt->target_range)) {
1934 printk(KERN_WARNING "security: rangetrans: invalid range\n");
1941 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1942 if (!p->type_attr_map)
1945 for (i = 0; i < p->p_types.nprim; i++) {
1946 ebitmap_init(&p->type_attr_map[i]);
1947 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1948 if (ebitmap_read(&p->type_attr_map[i], fp))
1951 /* add the type itself as the degenerate case */
1952 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1960 ocontext_destroy(newc,OCON_FSUSE);
1964 policydb_destroy(p);