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>
33 #include <linux/audit.h>
37 #include "conditional.h"
43 static char *symtab_name[SYM_NUM] = {
55 static unsigned int symtab_sizes[SYM_NUM] = {
66 struct policydb_compat_info {
72 /* These need to be updated if SYM_NUM or OCON_NUM changes */
73 static struct policydb_compat_info policydb_compat[] = {
75 .version = POLICYDB_VERSION_BASE,
76 .sym_num = SYM_NUM - 3,
77 .ocon_num = OCON_NUM - 1,
80 .version = POLICYDB_VERSION_BOOL,
81 .sym_num = SYM_NUM - 2,
82 .ocon_num = OCON_NUM - 1,
85 .version = POLICYDB_VERSION_IPV6,
86 .sym_num = SYM_NUM - 2,
90 .version = POLICYDB_VERSION_NLCLASS,
91 .sym_num = SYM_NUM - 2,
95 .version = POLICYDB_VERSION_MLS,
100 .version = POLICYDB_VERSION_AVTAB,
102 .ocon_num = OCON_NUM,
105 .version = POLICYDB_VERSION_RANGETRANS,
107 .ocon_num = OCON_NUM,
110 .version = POLICYDB_VERSION_POLCAP,
112 .ocon_num = OCON_NUM,
115 .version = POLICYDB_VERSION_PERMISSIVE,
117 .ocon_num = OCON_NUM,
120 .version = POLICYDB_VERSION_BOUNDARY,
122 .ocon_num = OCON_NUM,
126 static struct policydb_compat_info *policydb_lookup_compat(int version)
129 struct policydb_compat_info *info = NULL;
131 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
132 if (policydb_compat[i].version == version) {
133 info = &policydb_compat[i];
141 * Initialize the role table.
143 static int roles_init(struct policydb *p)
147 struct role_datum *role;
149 role = kzalloc(sizeof(*role), GFP_KERNEL);
154 role->value = ++p->p_roles.nprim;
155 if (role->value != OBJECT_R_VAL) {
159 key = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
164 strcpy(key, OBJECT_R);
165 rc = hashtab_insert(p->p_roles.table, key, role);
178 static u32 rangetr_hash(struct hashtab *h, const void *k)
180 const struct range_trans *key = k;
181 return (key->source_type + (key->target_type << 3) +
182 (key->target_class << 5)) & (h->size - 1);
185 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
187 const struct range_trans *key1 = k1, *key2 = k2;
188 return (key1->source_type != key2->source_type ||
189 key1->target_type != key2->target_type ||
190 key1->target_class != key2->target_class);
194 * Initialize a policy database structure.
196 static int policydb_init(struct policydb *p)
200 memset(p, 0, sizeof(*p));
202 for (i = 0; i < SYM_NUM; i++) {
203 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
205 goto out_free_symtab;
208 rc = avtab_init(&p->te_avtab);
210 goto out_free_symtab;
214 goto out_free_symtab;
216 rc = cond_policydb_init(p);
218 goto out_free_symtab;
220 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
222 goto out_free_symtab;
224 ebitmap_init(&p->policycaps);
225 ebitmap_init(&p->permissive_map);
231 for (i = 0; i < SYM_NUM; i++)
232 hashtab_destroy(p->symtab[i].table);
237 * The following *_index functions are used to
238 * define the val_to_name and val_to_struct arrays
239 * in a policy database structure. The val_to_name
240 * arrays are used when converting security context
241 * structures into string representations. The
242 * val_to_struct arrays are used when the attributes
243 * of a class, role, or user are needed.
246 static int common_index(void *key, void *datum, void *datap)
249 struct common_datum *comdatum;
253 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
255 p->p_common_val_to_name[comdatum->value - 1] = key;
259 static int class_index(void *key, void *datum, void *datap)
262 struct class_datum *cladatum;
266 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
268 p->p_class_val_to_name[cladatum->value - 1] = key;
269 p->class_val_to_struct[cladatum->value - 1] = cladatum;
273 static int role_index(void *key, void *datum, void *datap)
276 struct role_datum *role;
281 || role->value > p->p_roles.nprim
282 || role->bounds > p->p_roles.nprim)
284 p->p_role_val_to_name[role->value - 1] = key;
285 p->role_val_to_struct[role->value - 1] = role;
289 static int type_index(void *key, void *datum, void *datap)
292 struct type_datum *typdatum;
297 if (typdatum->primary) {
299 || typdatum->value > p->p_types.nprim
300 || typdatum->bounds > p->p_types.nprim)
302 p->p_type_val_to_name[typdatum->value - 1] = key;
303 p->type_val_to_struct[typdatum->value - 1] = typdatum;
309 static int user_index(void *key, void *datum, void *datap)
312 struct user_datum *usrdatum;
317 || usrdatum->value > p->p_users.nprim
318 || usrdatum->bounds > p->p_users.nprim)
320 p->p_user_val_to_name[usrdatum->value - 1] = key;
321 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
325 static int sens_index(void *key, void *datum, void *datap)
328 struct level_datum *levdatum;
333 if (!levdatum->isalias) {
334 if (!levdatum->level->sens ||
335 levdatum->level->sens > p->p_levels.nprim)
337 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
343 static int cat_index(void *key, void *datum, void *datap)
346 struct cat_datum *catdatum;
351 if (!catdatum->isalias) {
352 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
354 p->p_cat_val_to_name[catdatum->value - 1] = key;
360 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
373 * Define the common val_to_name array and the class
374 * val_to_name and val_to_struct arrays in a policy
375 * database structure.
377 * Caller must clean up upon failure.
379 static int policydb_index_classes(struct policydb *p)
383 p->p_common_val_to_name =
384 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
385 if (!p->p_common_val_to_name) {
390 rc = hashtab_map(p->p_commons.table, common_index, p);
394 p->class_val_to_struct =
395 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
396 if (!p->class_val_to_struct) {
401 p->p_class_val_to_name =
402 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
403 if (!p->p_class_val_to_name) {
408 rc = hashtab_map(p->p_classes.table, class_index, p);
414 static void symtab_hash_eval(struct symtab *s)
418 for (i = 0; i < SYM_NUM; i++) {
419 struct hashtab *h = s[i].table;
420 struct hashtab_info info;
422 hashtab_stat(h, &info);
423 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
424 "longest chain length %d\n", symtab_name[i], h->nel,
425 info.slots_used, h->size, info.max_chain_len);
429 static void rangetr_hash_eval(struct hashtab *h)
431 struct hashtab_info info;
433 hashtab_stat(h, &info);
434 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
435 "longest chain length %d\n", h->nel,
436 info.slots_used, h->size, info.max_chain_len);
439 static inline void rangetr_hash_eval(struct hashtab *h)
445 * Define the other val_to_name and val_to_struct arrays
446 * in a policy database structure.
448 * Caller must clean up on failure.
450 static int policydb_index_others(struct policydb *p)
454 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
455 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
457 printk(", %d sens, %d cats", p->p_levels.nprim,
461 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
462 p->p_classes.nprim, p->te_avtab.nel);
465 avtab_hash_eval(&p->te_avtab, "rules");
466 symtab_hash_eval(p->symtab);
469 p->role_val_to_struct =
470 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
472 if (!p->role_val_to_struct) {
477 p->user_val_to_struct =
478 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
480 if (!p->user_val_to_struct) {
485 p->type_val_to_struct =
486 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
488 if (!p->type_val_to_struct) {
493 if (cond_init_bool_indexes(p)) {
498 for (i = SYM_ROLES; i < SYM_NUM; i++) {
499 p->sym_val_to_name[i] =
500 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
501 if (!p->sym_val_to_name[i]) {
505 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
515 * The following *_destroy functions are used to
516 * free any memory allocated for each kind of
517 * symbol data in the policy database.
520 static int perm_destroy(void *key, void *datum, void *p)
527 static int common_destroy(void *key, void *datum, void *p)
529 struct common_datum *comdatum;
533 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
534 hashtab_destroy(comdatum->permissions.table);
539 static int cls_destroy(void *key, void *datum, void *p)
541 struct class_datum *cladatum;
542 struct constraint_node *constraint, *ctemp;
543 struct constraint_expr *e, *etmp;
547 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
548 hashtab_destroy(cladatum->permissions.table);
549 constraint = cladatum->constraints;
551 e = constraint->expr;
553 ebitmap_destroy(&e->names);
559 constraint = constraint->next;
563 constraint = cladatum->validatetrans;
565 e = constraint->expr;
567 ebitmap_destroy(&e->names);
573 constraint = constraint->next;
577 kfree(cladatum->comkey);
582 static int role_destroy(void *key, void *datum, void *p)
584 struct role_datum *role;
588 ebitmap_destroy(&role->dominates);
589 ebitmap_destroy(&role->types);
594 static int type_destroy(void *key, void *datum, void *p)
601 static int user_destroy(void *key, void *datum, void *p)
603 struct user_datum *usrdatum;
607 ebitmap_destroy(&usrdatum->roles);
608 ebitmap_destroy(&usrdatum->range.level[0].cat);
609 ebitmap_destroy(&usrdatum->range.level[1].cat);
610 ebitmap_destroy(&usrdatum->dfltlevel.cat);
615 static int sens_destroy(void *key, void *datum, void *p)
617 struct level_datum *levdatum;
621 ebitmap_destroy(&levdatum->level->cat);
622 kfree(levdatum->level);
627 static int cat_destroy(void *key, void *datum, void *p)
634 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
646 static int range_tr_destroy(void *key, void *datum, void *p)
648 struct mls_range *rt = datum;
650 ebitmap_destroy(&rt->level[0].cat);
651 ebitmap_destroy(&rt->level[1].cat);
657 static void ocontext_destroy(struct ocontext *c, int i)
659 context_destroy(&c->context[0]);
660 context_destroy(&c->context[1]);
661 if (i == OCON_ISID || i == OCON_FS ||
662 i == OCON_NETIF || i == OCON_FSUSE)
668 * Free any memory allocated by a policy database structure.
670 void policydb_destroy(struct policydb *p)
672 struct ocontext *c, *ctmp;
673 struct genfs *g, *gtmp;
675 struct role_allow *ra, *lra = NULL;
676 struct role_trans *tr, *ltr = NULL;
678 for (i = 0; i < SYM_NUM; i++) {
680 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
681 hashtab_destroy(p->symtab[i].table);
684 for (i = 0; i < SYM_NUM; i++)
685 kfree(p->sym_val_to_name[i]);
687 kfree(p->class_val_to_struct);
688 kfree(p->role_val_to_struct);
689 kfree(p->user_val_to_struct);
690 kfree(p->type_val_to_struct);
692 avtab_destroy(&p->te_avtab);
694 for (i = 0; i < OCON_NUM; i++) {
700 ocontext_destroy(ctmp, i);
702 p->ocontexts[i] = NULL;
713 ocontext_destroy(ctmp, OCON_FSUSE);
721 cond_policydb_destroy(p);
723 for (tr = p->role_tr; tr; tr = tr->next) {
730 for (ra = p->role_allow; ra; ra = ra->next) {
737 hashtab_map(p->range_tr, range_tr_destroy, NULL);
738 hashtab_destroy(p->range_tr);
740 if (p->type_attr_map) {
741 for (i = 0; i < p->p_types.nprim; i++)
742 ebitmap_destroy(&p->type_attr_map[i]);
744 kfree(p->type_attr_map);
745 ebitmap_destroy(&p->policycaps);
746 ebitmap_destroy(&p->permissive_map);
752 * Load the initial SIDs specified in a policy database
753 * structure into a SID table.
755 int policydb_load_isids(struct policydb *p, struct sidtab *s)
757 struct ocontext *head, *c;
762 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
766 head = p->ocontexts[OCON_ISID];
767 for (c = head; c; c = c->next) {
768 if (!c->context[0].user) {
769 printk(KERN_ERR "SELinux: SID %s was never "
770 "defined.\n", c->u.name);
774 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
775 printk(KERN_ERR "SELinux: unable to load initial "
776 "SID %s.\n", c->u.name);
785 int policydb_class_isvalid(struct policydb *p, unsigned int class)
787 if (!class || class > p->p_classes.nprim)
792 int policydb_role_isvalid(struct policydb *p, unsigned int role)
794 if (!role || role > p->p_roles.nprim)
799 int policydb_type_isvalid(struct policydb *p, unsigned int type)
801 if (!type || type > p->p_types.nprim)
807 * Return 1 if the fields in the security context
808 * structure `c' are valid. Return 0 otherwise.
810 int policydb_context_isvalid(struct policydb *p, struct context *c)
812 struct role_datum *role;
813 struct user_datum *usrdatum;
815 if (!c->role || c->role > p->p_roles.nprim)
818 if (!c->user || c->user > p->p_users.nprim)
821 if (!c->type || c->type > p->p_types.nprim)
824 if (c->role != OBJECT_R_VAL) {
826 * Role must be authorized for the type.
828 role = p->role_val_to_struct[c->role - 1];
829 if (!ebitmap_get_bit(&role->types,
831 /* role may not be associated with type */
835 * User must be authorized for the role.
837 usrdatum = p->user_val_to_struct[c->user - 1];
841 if (!ebitmap_get_bit(&usrdatum->roles,
843 /* user may not be associated with role */
847 if (!mls_context_isvalid(p, c))
854 * Read a MLS range structure from a policydb binary
855 * representation file.
857 static int mls_read_range_helper(struct mls_range *r, void *fp)
863 rc = next_entry(buf, fp, sizeof(u32));
867 items = le32_to_cpu(buf[0]);
868 if (items > ARRAY_SIZE(buf)) {
869 printk(KERN_ERR "SELinux: mls: range overflow\n");
873 rc = next_entry(buf, fp, sizeof(u32) * items);
875 printk(KERN_ERR "SELinux: mls: truncated range\n");
878 r->level[0].sens = le32_to_cpu(buf[0]);
880 r->level[1].sens = le32_to_cpu(buf[1]);
882 r->level[1].sens = r->level[0].sens;
884 rc = ebitmap_read(&r->level[0].cat, fp);
886 printk(KERN_ERR "SELinux: mls: error reading low "
891 rc = ebitmap_read(&r->level[1].cat, fp);
893 printk(KERN_ERR "SELinux: mls: error reading high "
898 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
900 printk(KERN_ERR "SELinux: mls: out of memory\n");
909 ebitmap_destroy(&r->level[0].cat);
914 * Read and validate a security context structure
915 * from a policydb binary representation file.
917 static int context_read_and_validate(struct context *c,
924 rc = next_entry(buf, fp, sizeof buf);
926 printk(KERN_ERR "SELinux: context truncated\n");
929 c->user = le32_to_cpu(buf[0]);
930 c->role = le32_to_cpu(buf[1]);
931 c->type = le32_to_cpu(buf[2]);
932 if (p->policyvers >= POLICYDB_VERSION_MLS) {
933 if (mls_read_range_helper(&c->range, fp)) {
934 printk(KERN_ERR "SELinux: error reading MLS range of "
941 if (!policydb_context_isvalid(p, c)) {
942 printk(KERN_ERR "SELinux: invalid security context\n");
951 * The following *_read functions are used to
952 * read the symbol data from a policy database
953 * binary representation file.
956 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
959 struct perm_datum *perdatum;
964 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
970 rc = next_entry(buf, fp, sizeof buf);
974 len = le32_to_cpu(buf[0]);
975 perdatum->value = le32_to_cpu(buf[1]);
977 key = kmalloc(len + 1, GFP_KERNEL);
982 rc = next_entry(key, fp, len);
987 rc = hashtab_insert(h, key, perdatum);
993 perm_destroy(key, perdatum, NULL);
997 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1000 struct common_datum *comdatum;
1005 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1011 rc = next_entry(buf, fp, sizeof buf);
1015 len = le32_to_cpu(buf[0]);
1016 comdatum->value = le32_to_cpu(buf[1]);
1018 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1021 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1022 nel = le32_to_cpu(buf[3]);
1024 key = kmalloc(len + 1, GFP_KERNEL);
1029 rc = next_entry(key, fp, len);
1034 for (i = 0; i < nel; i++) {
1035 rc = perm_read(p, comdatum->permissions.table, fp);
1040 rc = hashtab_insert(h, key, comdatum);
1046 common_destroy(key, comdatum, NULL);
1050 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1051 int allowxtarget, void *fp)
1053 struct constraint_node *c, *lc;
1054 struct constraint_expr *e, *le;
1057 int rc, i, j, depth;
1060 for (i = 0; i < ncons; i++) {
1061 c = kzalloc(sizeof(*c), GFP_KERNEL);
1070 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1073 c->permissions = le32_to_cpu(buf[0]);
1074 nexpr = le32_to_cpu(buf[1]);
1077 for (j = 0; j < nexpr; j++) {
1078 e = kzalloc(sizeof(*e), GFP_KERNEL);
1087 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1090 e->expr_type = le32_to_cpu(buf[0]);
1091 e->attr = le32_to_cpu(buf[1]);
1092 e->op = le32_to_cpu(buf[2]);
1094 switch (e->expr_type) {
1106 if (depth == (CEXPR_MAXDEPTH - 1))
1111 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1113 if (depth == (CEXPR_MAXDEPTH - 1))
1116 if (ebitmap_read(&e->names, fp))
1132 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1135 struct class_datum *cladatum;
1137 u32 len, len2, ncons, nel;
1140 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1146 rc = next_entry(buf, fp, sizeof(u32)*6);
1150 len = le32_to_cpu(buf[0]);
1151 len2 = le32_to_cpu(buf[1]);
1152 cladatum->value = le32_to_cpu(buf[2]);
1154 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1157 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1158 nel = le32_to_cpu(buf[4]);
1160 ncons = le32_to_cpu(buf[5]);
1162 key = kmalloc(len + 1, GFP_KERNEL);
1167 rc = next_entry(key, fp, len);
1173 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1174 if (!cladatum->comkey) {
1178 rc = next_entry(cladatum->comkey, fp, len2);
1181 cladatum->comkey[len2] = '\0';
1183 cladatum->comdatum = hashtab_search(p->p_commons.table,
1185 if (!cladatum->comdatum) {
1186 printk(KERN_ERR "SELinux: unknown common %s\n",
1192 for (i = 0; i < nel; i++) {
1193 rc = perm_read(p, cladatum->permissions.table, fp);
1198 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1202 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1203 /* grab the validatetrans rules */
1204 rc = next_entry(buf, fp, sizeof(u32));
1207 ncons = le32_to_cpu(buf[0]);
1208 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1213 rc = hashtab_insert(h, key, cladatum);
1221 cls_destroy(key, cladatum, NULL);
1225 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1228 struct role_datum *role;
1229 int rc, to_read = 2;
1233 role = kzalloc(sizeof(*role), GFP_KERNEL);
1239 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1242 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1246 len = le32_to_cpu(buf[0]);
1247 role->value = le32_to_cpu(buf[1]);
1248 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1249 role->bounds = le32_to_cpu(buf[2]);
1251 key = kmalloc(len + 1, GFP_KERNEL);
1256 rc = next_entry(key, fp, len);
1261 rc = ebitmap_read(&role->dominates, fp);
1265 rc = ebitmap_read(&role->types, fp);
1269 if (strcmp(key, OBJECT_R) == 0) {
1270 if (role->value != OBJECT_R_VAL) {
1271 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1272 OBJECT_R, role->value);
1280 rc = hashtab_insert(h, key, role);
1286 role_destroy(key, role, NULL);
1290 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1293 struct type_datum *typdatum;
1294 int rc, to_read = 3;
1298 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1304 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1307 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1311 len = le32_to_cpu(buf[0]);
1312 typdatum->value = le32_to_cpu(buf[1]);
1313 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1314 u32 prop = le32_to_cpu(buf[2]);
1316 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1317 typdatum->primary = 1;
1318 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1319 typdatum->attribute = 1;
1321 typdatum->bounds = le32_to_cpu(buf[3]);
1323 typdatum->primary = le32_to_cpu(buf[2]);
1326 key = kmalloc(len + 1, GFP_KERNEL);
1331 rc = next_entry(key, fp, len);
1336 rc = hashtab_insert(h, key, typdatum);
1342 type_destroy(key, typdatum, NULL);
1348 * Read a MLS level structure from a policydb binary
1349 * representation file.
1351 static int mls_read_level(struct mls_level *lp, void *fp)
1356 memset(lp, 0, sizeof(*lp));
1358 rc = next_entry(buf, fp, sizeof buf);
1360 printk(KERN_ERR "SELinux: mls: truncated level\n");
1363 lp->sens = le32_to_cpu(buf[0]);
1365 if (ebitmap_read(&lp->cat, fp)) {
1366 printk(KERN_ERR "SELinux: mls: error reading level "
1377 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1380 struct user_datum *usrdatum;
1381 int rc, to_read = 2;
1385 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1391 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1394 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1398 len = le32_to_cpu(buf[0]);
1399 usrdatum->value = le32_to_cpu(buf[1]);
1400 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1401 usrdatum->bounds = le32_to_cpu(buf[2]);
1403 key = kmalloc(len + 1, GFP_KERNEL);
1408 rc = next_entry(key, fp, len);
1413 rc = ebitmap_read(&usrdatum->roles, fp);
1417 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1418 rc = mls_read_range_helper(&usrdatum->range, fp);
1421 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1426 rc = hashtab_insert(h, key, usrdatum);
1432 user_destroy(key, usrdatum, NULL);
1436 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1439 struct level_datum *levdatum;
1444 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1450 rc = next_entry(buf, fp, sizeof buf);
1454 len = le32_to_cpu(buf[0]);
1455 levdatum->isalias = le32_to_cpu(buf[1]);
1457 key = kmalloc(len + 1, GFP_ATOMIC);
1462 rc = next_entry(key, fp, len);
1467 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1468 if (!levdatum->level) {
1472 if (mls_read_level(levdatum->level, fp)) {
1477 rc = hashtab_insert(h, key, levdatum);
1483 sens_destroy(key, levdatum, NULL);
1487 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1490 struct cat_datum *catdatum;
1495 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1501 rc = next_entry(buf, fp, sizeof buf);
1505 len = le32_to_cpu(buf[0]);
1506 catdatum->value = le32_to_cpu(buf[1]);
1507 catdatum->isalias = le32_to_cpu(buf[2]);
1509 key = kmalloc(len + 1, GFP_ATOMIC);
1514 rc = next_entry(key, fp, len);
1519 rc = hashtab_insert(h, key, catdatum);
1526 cat_destroy(key, catdatum, NULL);
1530 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1542 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1544 struct user_datum *upper, *user;
1545 struct policydb *p = datap;
1548 upper = user = datum;
1549 while (upper->bounds) {
1550 struct ebitmap_node *node;
1553 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1554 printk(KERN_ERR "SELinux: user %s: "
1555 "too deep or looped boundary",
1560 upper = p->user_val_to_struct[upper->bounds - 1];
1561 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1562 if (ebitmap_get_bit(&upper->roles, bit))
1566 "SELinux: boundary violated policy: "
1567 "user=%s role=%s bounds=%s\n",
1568 p->p_user_val_to_name[user->value - 1],
1569 p->p_role_val_to_name[bit],
1570 p->p_user_val_to_name[upper->value - 1]);
1579 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1581 struct role_datum *upper, *role;
1582 struct policydb *p = datap;
1585 upper = role = datum;
1586 while (upper->bounds) {
1587 struct ebitmap_node *node;
1590 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1591 printk(KERN_ERR "SELinux: role %s: "
1592 "too deep or looped bounds\n",
1597 upper = p->role_val_to_struct[upper->bounds - 1];
1598 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1599 if (ebitmap_get_bit(&upper->types, bit))
1603 "SELinux: boundary violated policy: "
1604 "role=%s type=%s bounds=%s\n",
1605 p->p_role_val_to_name[role->value - 1],
1606 p->p_type_val_to_name[bit],
1607 p->p_role_val_to_name[upper->value - 1]);
1616 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1618 struct type_datum *upper, *type;
1619 struct policydb *p = datap;
1622 upper = type = datum;
1623 while (upper->bounds) {
1624 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1625 printk(KERN_ERR "SELinux: type %s: "
1626 "too deep or looped boundary\n",
1631 upper = p->type_val_to_struct[upper->bounds - 1];
1632 if (upper->attribute) {
1633 printk(KERN_ERR "SELinux: type %s: "
1634 "bounded by attribute %s",
1636 p->p_type_val_to_name[upper->value - 1]);
1644 static int policydb_bounds_sanity_check(struct policydb *p)
1648 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1651 rc = hashtab_map(p->p_users.table,
1652 user_bounds_sanity_check, p);
1656 rc = hashtab_map(p->p_roles.table,
1657 role_bounds_sanity_check, p);
1661 rc = hashtab_map(p->p_types.table,
1662 type_bounds_sanity_check, p);
1669 extern int ss_initialized;
1671 u16 string_to_security_class(struct policydb *p, const char *name)
1673 struct class_datum *cladatum;
1675 cladatum = hashtab_search(p->p_classes.table, name);
1679 return cladatum->value;
1682 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1684 struct class_datum *cladatum;
1685 struct perm_datum *perdatum = NULL;
1686 struct common_datum *comdatum;
1688 if (!tclass || tclass > p->p_classes.nprim)
1691 cladatum = p->class_val_to_struct[tclass-1];
1692 comdatum = cladatum->comdatum;
1694 perdatum = hashtab_search(comdatum->permissions.table,
1697 perdatum = hashtab_search(cladatum->permissions.table,
1702 return 1U << (perdatum->value-1);
1706 * Read the configuration data from a policy database binary
1707 * representation file into a policy database structure.
1709 int policydb_read(struct policydb *p, void *fp)
1711 struct role_allow *ra, *lra;
1712 struct role_trans *tr, *ltr;
1713 struct ocontext *l, *c, *newc;
1714 struct genfs *genfs_p, *genfs, *newgenfs;
1718 u32 len, len2, nprim, nel, nel2;
1720 struct policydb_compat_info *info;
1721 struct range_trans *rt;
1722 struct mls_range *r;
1724 rc = policydb_init(p);
1728 /* Read the magic number and string length. */
1729 rc = next_entry(buf, fp, sizeof(u32) * 2);
1733 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1734 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
1735 "not match expected magic number 0x%x\n",
1736 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1740 len = le32_to_cpu(buf[1]);
1741 if (len != strlen(POLICYDB_STRING)) {
1742 printk(KERN_ERR "SELinux: policydb string length %d does not "
1743 "match expected length %Zu\n",
1744 len, strlen(POLICYDB_STRING));
1747 policydb_str = kmalloc(len + 1, GFP_KERNEL);
1748 if (!policydb_str) {
1749 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
1750 "string of length %d\n", len);
1754 rc = next_entry(policydb_str, fp, len);
1756 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
1757 kfree(policydb_str);
1760 policydb_str[len] = '\0';
1761 if (strcmp(policydb_str, POLICYDB_STRING)) {
1762 printk(KERN_ERR "SELinux: policydb string %s does not match "
1763 "my string %s\n", policydb_str, POLICYDB_STRING);
1764 kfree(policydb_str);
1767 /* Done with policydb_str. */
1768 kfree(policydb_str);
1769 policydb_str = NULL;
1771 /* Read the version and table sizes. */
1772 rc = next_entry(buf, fp, sizeof(u32)*4);
1776 p->policyvers = le32_to_cpu(buf[0]);
1777 if (p->policyvers < POLICYDB_VERSION_MIN ||
1778 p->policyvers > POLICYDB_VERSION_MAX) {
1779 printk(KERN_ERR "SELinux: policydb version %d does not match "
1780 "my version range %d-%d\n",
1781 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1785 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1788 if (p->policyvers < POLICYDB_VERSION_MLS) {
1789 printk(KERN_ERR "SELinux: security policydb version %d "
1790 "(MLS) not backwards compatible\n",
1795 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1796 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
1798 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1799 ebitmap_read(&p->policycaps, fp) != 0)
1802 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1803 ebitmap_read(&p->permissive_map, fp) != 0)
1806 info = policydb_lookup_compat(p->policyvers);
1808 printk(KERN_ERR "SELinux: unable to find policy compat info "
1809 "for version %d\n", p->policyvers);
1813 if (le32_to_cpu(buf[2]) != info->sym_num ||
1814 le32_to_cpu(buf[3]) != info->ocon_num) {
1815 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
1816 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1817 le32_to_cpu(buf[3]),
1818 info->sym_num, info->ocon_num);
1822 for (i = 0; i < info->sym_num; i++) {
1823 rc = next_entry(buf, fp, sizeof(u32)*2);
1826 nprim = le32_to_cpu(buf[0]);
1827 nel = le32_to_cpu(buf[1]);
1828 for (j = 0; j < nel; j++) {
1829 rc = read_f[i](p, p->symtab[i].table, fp);
1834 p->symtab[i].nprim = nprim;
1837 rc = avtab_read(&p->te_avtab, fp, p);
1841 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1842 rc = cond_read_list(p, fp);
1847 rc = next_entry(buf, fp, sizeof(u32));
1850 nel = le32_to_cpu(buf[0]);
1852 for (i = 0; i < nel; i++) {
1853 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
1862 rc = next_entry(buf, fp, sizeof(u32)*3);
1865 tr->role = le32_to_cpu(buf[0]);
1866 tr->type = le32_to_cpu(buf[1]);
1867 tr->new_role = le32_to_cpu(buf[2]);
1868 if (!policydb_role_isvalid(p, tr->role) ||
1869 !policydb_type_isvalid(p, tr->type) ||
1870 !policydb_role_isvalid(p, tr->new_role)) {
1877 rc = next_entry(buf, fp, sizeof(u32));
1880 nel = le32_to_cpu(buf[0]);
1882 for (i = 0; i < nel; i++) {
1883 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1892 rc = next_entry(buf, fp, sizeof(u32)*2);
1895 ra->role = le32_to_cpu(buf[0]);
1896 ra->new_role = le32_to_cpu(buf[1]);
1897 if (!policydb_role_isvalid(p, ra->role) ||
1898 !policydb_role_isvalid(p, ra->new_role)) {
1905 rc = policydb_index_classes(p);
1909 rc = policydb_index_others(p);
1913 p->process_class = string_to_security_class(p, "process");
1914 if (!p->process_class)
1916 p->process_trans_perms = string_to_av_perm(p, p->process_class,
1918 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
1920 if (!p->process_trans_perms)
1923 for (i = 0; i < info->ocon_num; i++) {
1924 rc = next_entry(buf, fp, sizeof(u32));
1927 nel = le32_to_cpu(buf[0]);
1929 for (j = 0; j < nel; j++) {
1930 c = kzalloc(sizeof(*c), GFP_KERNEL);
1938 p->ocontexts[i] = c;
1943 rc = next_entry(buf, fp, sizeof(u32));
1946 c->sid[0] = le32_to_cpu(buf[0]);
1947 rc = context_read_and_validate(&c->context[0], p, fp);
1953 rc = next_entry(buf, fp, sizeof(u32));
1956 len = le32_to_cpu(buf[0]);
1957 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1962 rc = next_entry(c->u.name, fp, len);
1966 rc = context_read_and_validate(&c->context[0], p, fp);
1969 rc = context_read_and_validate(&c->context[1], p, fp);
1974 rc = next_entry(buf, fp, sizeof(u32)*3);
1977 c->u.port.protocol = le32_to_cpu(buf[0]);
1978 c->u.port.low_port = le32_to_cpu(buf[1]);
1979 c->u.port.high_port = le32_to_cpu(buf[2]);
1980 rc = context_read_and_validate(&c->context[0], p, fp);
1985 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1988 c->u.node.addr = nodebuf[0]; /* network order */
1989 c->u.node.mask = nodebuf[1]; /* network order */
1990 rc = context_read_and_validate(&c->context[0], p, fp);
1995 rc = next_entry(buf, fp, sizeof(u32)*2);
1998 c->v.behavior = le32_to_cpu(buf[0]);
1999 if (c->v.behavior > SECURITY_FS_USE_NONE)
2001 len = le32_to_cpu(buf[1]);
2002 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2007 rc = next_entry(c->u.name, fp, len);
2011 rc = context_read_and_validate(&c->context[0], p, fp);
2018 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2021 for (k = 0; k < 4; k++)
2022 c->u.node6.addr[k] = nodebuf[k];
2023 for (k = 0; k < 4; k++)
2024 c->u.node6.mask[k] = nodebuf[k+4];
2025 if (context_read_and_validate(&c->context[0], p, fp))
2033 rc = next_entry(buf, fp, sizeof(u32));
2036 nel = le32_to_cpu(buf[0]);
2039 for (i = 0; i < nel; i++) {
2040 rc = next_entry(buf, fp, sizeof(u32));
2043 len = le32_to_cpu(buf[0]);
2044 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2050 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
2051 if (!newgenfs->fstype) {
2056 rc = next_entry(newgenfs->fstype, fp, len);
2058 kfree(newgenfs->fstype);
2062 newgenfs->fstype[len] = 0;
2063 for (genfs_p = NULL, genfs = p->genfs; genfs;
2064 genfs_p = genfs, genfs = genfs->next) {
2065 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2066 printk(KERN_ERR "SELinux: dup genfs "
2067 "fstype %s\n", newgenfs->fstype);
2068 kfree(newgenfs->fstype);
2072 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2075 newgenfs->next = genfs;
2077 genfs_p->next = newgenfs;
2079 p->genfs = newgenfs;
2080 rc = next_entry(buf, fp, sizeof(u32));
2083 nel2 = le32_to_cpu(buf[0]);
2084 for (j = 0; j < nel2; j++) {
2085 rc = next_entry(buf, fp, sizeof(u32));
2088 len = le32_to_cpu(buf[0]);
2090 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2096 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2097 if (!newc->u.name) {
2101 rc = next_entry(newc->u.name, fp, len);
2104 newc->u.name[len] = 0;
2105 rc = next_entry(buf, fp, sizeof(u32));
2108 newc->v.sclass = le32_to_cpu(buf[0]);
2109 if (context_read_and_validate(&newc->context[0], p, fp))
2111 for (l = NULL, c = newgenfs->head; c;
2112 l = c, c = c->next) {
2113 if (!strcmp(newc->u.name, c->u.name) &&
2114 (!c->v.sclass || !newc->v.sclass ||
2115 newc->v.sclass == c->v.sclass)) {
2116 printk(KERN_ERR "SELinux: dup genfs "
2118 newgenfs->fstype, c->u.name);
2121 len = strlen(newc->u.name);
2122 len2 = strlen(c->u.name);
2131 newgenfs->head = newc;
2135 if (p->policyvers >= POLICYDB_VERSION_MLS) {
2136 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
2137 rc = next_entry(buf, fp, sizeof(u32));
2140 nel = le32_to_cpu(buf[0]);
2141 for (i = 0; i < nel; i++) {
2142 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
2147 rc = next_entry(buf, fp, (sizeof(u32) * 2));
2152 rt->source_type = le32_to_cpu(buf[0]);
2153 rt->target_type = le32_to_cpu(buf[1]);
2155 rc = next_entry(buf, fp, sizeof(u32));
2160 rt->target_class = le32_to_cpu(buf[0]);
2162 rt->target_class = p->process_class;
2163 if (!policydb_type_isvalid(p, rt->source_type) ||
2164 !policydb_type_isvalid(p, rt->target_type) ||
2165 !policydb_class_isvalid(p, rt->target_class)) {
2170 r = kzalloc(sizeof(*r), GFP_KERNEL);
2176 rc = mls_read_range_helper(r, fp);
2182 if (!mls_range_isvalid(p, r)) {
2183 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
2188 rc = hashtab_insert(p->range_tr, rt, r);
2195 rangetr_hash_eval(p->range_tr);
2198 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2199 if (!p->type_attr_map)
2202 for (i = 0; i < p->p_types.nprim; i++) {
2203 ebitmap_init(&p->type_attr_map[i]);
2204 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2205 if (ebitmap_read(&p->type_attr_map[i], fp))
2208 /* add the type itself as the degenerate case */
2209 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2213 rc = policydb_bounds_sanity_check(p);
2221 ocontext_destroy(newc, OCON_FSUSE);
2225 policydb_destroy(p);