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>
34 #include <linux/flex_array.h>
38 #include "conditional.h"
44 static const char *symtab_name[SYM_NUM] = {
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,
116 .version = POLICYDB_VERSION_PERMISSIVE,
118 .ocon_num = OCON_NUM,
121 .version = POLICYDB_VERSION_BOUNDARY,
123 .ocon_num = OCON_NUM,
127 static struct policydb_compat_info *policydb_lookup_compat(int version)
130 struct policydb_compat_info *info = NULL;
132 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
133 if (policydb_compat[i].version == version) {
134 info = &policydb_compat[i];
142 * Initialize the role table.
144 static int roles_init(struct policydb *p)
148 struct role_datum *role;
150 role = kzalloc(sizeof(*role), GFP_KERNEL);
155 role->value = ++p->p_roles.nprim;
156 if (role->value != OBJECT_R_VAL) {
160 key = kstrdup(OBJECT_R, GFP_KERNEL);
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;
190 v = key1->source_type - key2->source_type;
194 v = key1->target_type - key2->target_type;
198 v = key1->target_class - key2->target_class;
204 * Initialize a policy database structure.
206 static int policydb_init(struct policydb *p)
210 memset(p, 0, sizeof(*p));
212 for (i = 0; i < SYM_NUM; i++) {
213 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
215 goto out_free_symtab;
218 rc = avtab_init(&p->te_avtab);
220 goto out_free_symtab;
224 goto out_free_symtab;
226 rc = cond_policydb_init(p);
228 goto out_free_symtab;
230 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
232 goto out_free_symtab;
234 ebitmap_init(&p->policycaps);
235 ebitmap_init(&p->permissive_map);
241 for (i = 0; i < SYM_NUM; i++)
242 hashtab_destroy(p->symtab[i].table);
247 * The following *_index functions are used to
248 * define the val_to_name and val_to_struct arrays
249 * in a policy database structure. The val_to_name
250 * arrays are used when converting security context
251 * structures into string representations. The
252 * val_to_struct arrays are used when the attributes
253 * of a class, role, or user are needed.
256 static int common_index(void *key, void *datum, void *datap)
259 struct common_datum *comdatum;
263 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
265 p->p_common_val_to_name[comdatum->value - 1] = key;
269 static int class_index(void *key, void *datum, void *datap)
272 struct class_datum *cladatum;
276 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
278 p->p_class_val_to_name[cladatum->value - 1] = key;
279 p->class_val_to_struct[cladatum->value - 1] = cladatum;
283 static int role_index(void *key, void *datum, void *datap)
286 struct role_datum *role;
291 || role->value > p->p_roles.nprim
292 || role->bounds > p->p_roles.nprim)
294 p->p_role_val_to_name[role->value - 1] = key;
295 p->role_val_to_struct[role->value - 1] = role;
299 static int type_index(void *key, void *datum, void *datap)
302 struct type_datum *typdatum;
307 if (typdatum->primary) {
309 || typdatum->value > p->p_types.nprim
310 || typdatum->bounds > p->p_types.nprim)
312 p->p_type_val_to_name[typdatum->value - 1] = key;
313 p->type_val_to_struct[typdatum->value - 1] = typdatum;
319 static int user_index(void *key, void *datum, void *datap)
322 struct user_datum *usrdatum;
327 || usrdatum->value > p->p_users.nprim
328 || usrdatum->bounds > p->p_users.nprim)
330 p->p_user_val_to_name[usrdatum->value - 1] = key;
331 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
335 static int sens_index(void *key, void *datum, void *datap)
338 struct level_datum *levdatum;
343 if (!levdatum->isalias) {
344 if (!levdatum->level->sens ||
345 levdatum->level->sens > p->p_levels.nprim)
347 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
353 static int cat_index(void *key, void *datum, void *datap)
356 struct cat_datum *catdatum;
361 if (!catdatum->isalias) {
362 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
364 p->p_cat_val_to_name[catdatum->value - 1] = key;
370 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
383 * Define the common val_to_name array and the class
384 * val_to_name and val_to_struct arrays in a policy
385 * database structure.
387 * Caller must clean up upon failure.
389 static int policydb_index_classes(struct policydb *p)
393 p->p_common_val_to_name =
394 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
395 if (!p->p_common_val_to_name) {
400 rc = hashtab_map(p->p_commons.table, common_index, p);
404 p->class_val_to_struct =
405 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
406 if (!p->class_val_to_struct) {
411 p->p_class_val_to_name =
412 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
413 if (!p->p_class_val_to_name) {
418 rc = hashtab_map(p->p_classes.table, class_index, p);
424 static void symtab_hash_eval(struct symtab *s)
428 for (i = 0; i < SYM_NUM; i++) {
429 struct hashtab *h = s[i].table;
430 struct hashtab_info info;
432 hashtab_stat(h, &info);
433 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
434 "longest chain length %d\n", symtab_name[i], h->nel,
435 info.slots_used, h->size, info.max_chain_len);
439 static void rangetr_hash_eval(struct hashtab *h)
441 struct hashtab_info info;
443 hashtab_stat(h, &info);
444 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
445 "longest chain length %d\n", h->nel,
446 info.slots_used, h->size, info.max_chain_len);
449 static inline void rangetr_hash_eval(struct hashtab *h)
455 * Define the other val_to_name and val_to_struct arrays
456 * in a policy database structure.
458 * Caller must clean up on failure.
460 static int policydb_index_others(struct policydb *p)
464 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
465 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
467 printk(", %d sens, %d cats", p->p_levels.nprim,
471 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
472 p->p_classes.nprim, p->te_avtab.nel);
475 avtab_hash_eval(&p->te_avtab, "rules");
476 symtab_hash_eval(p->symtab);
479 p->role_val_to_struct =
480 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
482 if (!p->role_val_to_struct) {
487 p->user_val_to_struct =
488 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
490 if (!p->user_val_to_struct) {
495 p->type_val_to_struct =
496 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
498 if (!p->type_val_to_struct) {
503 if (cond_init_bool_indexes(p)) {
508 for (i = SYM_ROLES; i < SYM_NUM; i++) {
509 p->sym_val_to_name[i] =
510 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
511 if (!p->sym_val_to_name[i]) {
515 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
525 * The following *_destroy functions are used to
526 * free any memory allocated for each kind of
527 * symbol data in the policy database.
530 static int perm_destroy(void *key, void *datum, void *p)
537 static int common_destroy(void *key, void *datum, void *p)
539 struct common_datum *comdatum;
543 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
544 hashtab_destroy(comdatum->permissions.table);
549 static int cls_destroy(void *key, void *datum, void *p)
551 struct class_datum *cladatum;
552 struct constraint_node *constraint, *ctemp;
553 struct constraint_expr *e, *etmp;
557 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
558 hashtab_destroy(cladatum->permissions.table);
559 constraint = cladatum->constraints;
561 e = constraint->expr;
563 ebitmap_destroy(&e->names);
569 constraint = constraint->next;
573 constraint = cladatum->validatetrans;
575 e = constraint->expr;
577 ebitmap_destroy(&e->names);
583 constraint = constraint->next;
587 kfree(cladatum->comkey);
592 static int role_destroy(void *key, void *datum, void *p)
594 struct role_datum *role;
598 ebitmap_destroy(&role->dominates);
599 ebitmap_destroy(&role->types);
604 static int type_destroy(void *key, void *datum, void *p)
611 static int user_destroy(void *key, void *datum, void *p)
613 struct user_datum *usrdatum;
617 ebitmap_destroy(&usrdatum->roles);
618 ebitmap_destroy(&usrdatum->range.level[0].cat);
619 ebitmap_destroy(&usrdatum->range.level[1].cat);
620 ebitmap_destroy(&usrdatum->dfltlevel.cat);
625 static int sens_destroy(void *key, void *datum, void *p)
627 struct level_datum *levdatum;
631 ebitmap_destroy(&levdatum->level->cat);
632 kfree(levdatum->level);
637 static int cat_destroy(void *key, void *datum, void *p)
644 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
656 static int range_tr_destroy(void *key, void *datum, void *p)
658 struct mls_range *rt = datum;
660 ebitmap_destroy(&rt->level[0].cat);
661 ebitmap_destroy(&rt->level[1].cat);
667 static void ocontext_destroy(struct ocontext *c, int i)
672 context_destroy(&c->context[0]);
673 context_destroy(&c->context[1]);
674 if (i == OCON_ISID || i == OCON_FS ||
675 i == OCON_NETIF || i == OCON_FSUSE)
681 * Free any memory allocated by a policy database structure.
683 void policydb_destroy(struct policydb *p)
685 struct ocontext *c, *ctmp;
686 struct genfs *g, *gtmp;
688 struct role_allow *ra, *lra = NULL;
689 struct role_trans *tr, *ltr = NULL;
691 for (i = 0; i < SYM_NUM; i++) {
693 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
694 hashtab_destroy(p->symtab[i].table);
697 for (i = 0; i < SYM_NUM; i++)
698 kfree(p->sym_val_to_name[i]);
700 kfree(p->class_val_to_struct);
701 kfree(p->role_val_to_struct);
702 kfree(p->user_val_to_struct);
703 kfree(p->type_val_to_struct);
705 avtab_destroy(&p->te_avtab);
707 for (i = 0; i < OCON_NUM; i++) {
713 ocontext_destroy(ctmp, i);
715 p->ocontexts[i] = NULL;
726 ocontext_destroy(ctmp, OCON_FSUSE);
734 cond_policydb_destroy(p);
736 for (tr = p->role_tr; tr; tr = tr->next) {
743 for (ra = p->role_allow; ra; ra = ra->next) {
750 hashtab_map(p->range_tr, range_tr_destroy, NULL);
751 hashtab_destroy(p->range_tr);
753 if (p->type_attr_map_array) {
754 for (i = 0; i < p->p_types.nprim; i++) {
757 e = flex_array_get(p->type_attr_map_array, i);
762 flex_array_free(p->type_attr_map_array);
764 ebitmap_destroy(&p->policycaps);
765 ebitmap_destroy(&p->permissive_map);
771 * Load the initial SIDs specified in a policy database
772 * structure into a SID table.
774 int policydb_load_isids(struct policydb *p, struct sidtab *s)
776 struct ocontext *head, *c;
781 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
785 head = p->ocontexts[OCON_ISID];
786 for (c = head; c; c = c->next) {
787 if (!c->context[0].user) {
788 printk(KERN_ERR "SELinux: SID %s was never "
789 "defined.\n", c->u.name);
793 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
794 printk(KERN_ERR "SELinux: unable to load initial "
795 "SID %s.\n", c->u.name);
804 int policydb_class_isvalid(struct policydb *p, unsigned int class)
806 if (!class || class > p->p_classes.nprim)
811 int policydb_role_isvalid(struct policydb *p, unsigned int role)
813 if (!role || role > p->p_roles.nprim)
818 int policydb_type_isvalid(struct policydb *p, unsigned int type)
820 if (!type || type > p->p_types.nprim)
826 * Return 1 if the fields in the security context
827 * structure `c' are valid. Return 0 otherwise.
829 int policydb_context_isvalid(struct policydb *p, struct context *c)
831 struct role_datum *role;
832 struct user_datum *usrdatum;
834 if (!c->role || c->role > p->p_roles.nprim)
837 if (!c->user || c->user > p->p_users.nprim)
840 if (!c->type || c->type > p->p_types.nprim)
843 if (c->role != OBJECT_R_VAL) {
845 * Role must be authorized for the type.
847 role = p->role_val_to_struct[c->role - 1];
848 if (!ebitmap_get_bit(&role->types,
850 /* role may not be associated with type */
854 * User must be authorized for the role.
856 usrdatum = p->user_val_to_struct[c->user - 1];
860 if (!ebitmap_get_bit(&usrdatum->roles,
862 /* user may not be associated with role */
866 if (!mls_context_isvalid(p, c))
873 * Read a MLS range structure from a policydb binary
874 * representation file.
876 static int mls_read_range_helper(struct mls_range *r, void *fp)
882 rc = next_entry(buf, fp, sizeof(u32));
886 items = le32_to_cpu(buf[0]);
887 if (items > ARRAY_SIZE(buf)) {
888 printk(KERN_ERR "SELinux: mls: range overflow\n");
892 rc = next_entry(buf, fp, sizeof(u32) * items);
894 printk(KERN_ERR "SELinux: mls: truncated range\n");
897 r->level[0].sens = le32_to_cpu(buf[0]);
899 r->level[1].sens = le32_to_cpu(buf[1]);
901 r->level[1].sens = r->level[0].sens;
903 rc = ebitmap_read(&r->level[0].cat, fp);
905 printk(KERN_ERR "SELinux: mls: error reading low "
910 rc = ebitmap_read(&r->level[1].cat, fp);
912 printk(KERN_ERR "SELinux: mls: error reading high "
917 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
919 printk(KERN_ERR "SELinux: mls: out of memory\n");
928 ebitmap_destroy(&r->level[0].cat);
933 * Read and validate a security context structure
934 * from a policydb binary representation file.
936 static int context_read_and_validate(struct context *c,
943 rc = next_entry(buf, fp, sizeof buf);
945 printk(KERN_ERR "SELinux: context truncated\n");
948 c->user = le32_to_cpu(buf[0]);
949 c->role = le32_to_cpu(buf[1]);
950 c->type = le32_to_cpu(buf[2]);
951 if (p->policyvers >= POLICYDB_VERSION_MLS) {
952 if (mls_read_range_helper(&c->range, fp)) {
953 printk(KERN_ERR "SELinux: error reading MLS range of "
960 if (!policydb_context_isvalid(p, c)) {
961 printk(KERN_ERR "SELinux: invalid security context\n");
970 * The following *_read functions are used to
971 * read the symbol data from a policy database
972 * binary representation file.
975 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
978 struct perm_datum *perdatum;
983 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
989 rc = next_entry(buf, fp, sizeof buf);
993 len = le32_to_cpu(buf[0]);
994 perdatum->value = le32_to_cpu(buf[1]);
996 key = kmalloc(len + 1, GFP_KERNEL);
1001 rc = next_entry(key, fp, len);
1006 rc = hashtab_insert(h, key, perdatum);
1012 perm_destroy(key, perdatum, NULL);
1016 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1019 struct common_datum *comdatum;
1024 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1030 rc = next_entry(buf, fp, sizeof buf);
1034 len = le32_to_cpu(buf[0]);
1035 comdatum->value = le32_to_cpu(buf[1]);
1037 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1040 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1041 nel = le32_to_cpu(buf[3]);
1043 key = kmalloc(len + 1, GFP_KERNEL);
1048 rc = next_entry(key, fp, len);
1053 for (i = 0; i < nel; i++) {
1054 rc = perm_read(p, comdatum->permissions.table, fp);
1059 rc = hashtab_insert(h, key, comdatum);
1065 common_destroy(key, comdatum, NULL);
1069 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1070 int allowxtarget, void *fp)
1072 struct constraint_node *c, *lc;
1073 struct constraint_expr *e, *le;
1076 int rc, i, j, depth;
1079 for (i = 0; i < ncons; i++) {
1080 c = kzalloc(sizeof(*c), GFP_KERNEL);
1089 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1092 c->permissions = le32_to_cpu(buf[0]);
1093 nexpr = le32_to_cpu(buf[1]);
1096 for (j = 0; j < nexpr; j++) {
1097 e = kzalloc(sizeof(*e), GFP_KERNEL);
1106 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1109 e->expr_type = le32_to_cpu(buf[0]);
1110 e->attr = le32_to_cpu(buf[1]);
1111 e->op = le32_to_cpu(buf[2]);
1113 switch (e->expr_type) {
1125 if (depth == (CEXPR_MAXDEPTH - 1))
1130 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1132 if (depth == (CEXPR_MAXDEPTH - 1))
1135 if (ebitmap_read(&e->names, fp))
1151 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1154 struct class_datum *cladatum;
1156 u32 len, len2, ncons, nel;
1159 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1165 rc = next_entry(buf, fp, sizeof(u32)*6);
1169 len = le32_to_cpu(buf[0]);
1170 len2 = le32_to_cpu(buf[1]);
1171 cladatum->value = le32_to_cpu(buf[2]);
1173 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1176 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1177 nel = le32_to_cpu(buf[4]);
1179 ncons = le32_to_cpu(buf[5]);
1181 key = kmalloc(len + 1, GFP_KERNEL);
1186 rc = next_entry(key, fp, len);
1192 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1193 if (!cladatum->comkey) {
1197 rc = next_entry(cladatum->comkey, fp, len2);
1200 cladatum->comkey[len2] = '\0';
1202 cladatum->comdatum = hashtab_search(p->p_commons.table,
1204 if (!cladatum->comdatum) {
1205 printk(KERN_ERR "SELinux: unknown common %s\n",
1211 for (i = 0; i < nel; i++) {
1212 rc = perm_read(p, cladatum->permissions.table, fp);
1217 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1221 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1222 /* grab the validatetrans rules */
1223 rc = next_entry(buf, fp, sizeof(u32));
1226 ncons = le32_to_cpu(buf[0]);
1227 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1232 rc = hashtab_insert(h, key, cladatum);
1240 cls_destroy(key, cladatum, NULL);
1244 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1247 struct role_datum *role;
1248 int rc, to_read = 2;
1252 role = kzalloc(sizeof(*role), GFP_KERNEL);
1258 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1261 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1265 len = le32_to_cpu(buf[0]);
1266 role->value = le32_to_cpu(buf[1]);
1267 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1268 role->bounds = le32_to_cpu(buf[2]);
1270 key = kmalloc(len + 1, GFP_KERNEL);
1275 rc = next_entry(key, fp, len);
1280 rc = ebitmap_read(&role->dominates, fp);
1284 rc = ebitmap_read(&role->types, fp);
1288 if (strcmp(key, OBJECT_R) == 0) {
1289 if (role->value != OBJECT_R_VAL) {
1290 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1291 OBJECT_R, role->value);
1299 rc = hashtab_insert(h, key, role);
1305 role_destroy(key, role, NULL);
1309 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1312 struct type_datum *typdatum;
1313 int rc, to_read = 3;
1317 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1323 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1326 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1330 len = le32_to_cpu(buf[0]);
1331 typdatum->value = le32_to_cpu(buf[1]);
1332 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1333 u32 prop = le32_to_cpu(buf[2]);
1335 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1336 typdatum->primary = 1;
1337 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1338 typdatum->attribute = 1;
1340 typdatum->bounds = le32_to_cpu(buf[3]);
1342 typdatum->primary = le32_to_cpu(buf[2]);
1345 key = kmalloc(len + 1, GFP_KERNEL);
1350 rc = next_entry(key, fp, len);
1355 rc = hashtab_insert(h, key, typdatum);
1361 type_destroy(key, typdatum, NULL);
1367 * Read a MLS level structure from a policydb binary
1368 * representation file.
1370 static int mls_read_level(struct mls_level *lp, void *fp)
1375 memset(lp, 0, sizeof(*lp));
1377 rc = next_entry(buf, fp, sizeof buf);
1379 printk(KERN_ERR "SELinux: mls: truncated level\n");
1382 lp->sens = le32_to_cpu(buf[0]);
1384 if (ebitmap_read(&lp->cat, fp)) {
1385 printk(KERN_ERR "SELinux: mls: error reading level "
1396 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1399 struct user_datum *usrdatum;
1400 int rc, to_read = 2;
1404 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1410 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1413 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1417 len = le32_to_cpu(buf[0]);
1418 usrdatum->value = le32_to_cpu(buf[1]);
1419 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1420 usrdatum->bounds = le32_to_cpu(buf[2]);
1422 key = kmalloc(len + 1, GFP_KERNEL);
1427 rc = next_entry(key, fp, len);
1432 rc = ebitmap_read(&usrdatum->roles, fp);
1436 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1437 rc = mls_read_range_helper(&usrdatum->range, fp);
1440 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1445 rc = hashtab_insert(h, key, usrdatum);
1451 user_destroy(key, usrdatum, NULL);
1455 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1458 struct level_datum *levdatum;
1463 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1469 rc = next_entry(buf, fp, sizeof buf);
1473 len = le32_to_cpu(buf[0]);
1474 levdatum->isalias = le32_to_cpu(buf[1]);
1476 key = kmalloc(len + 1, GFP_ATOMIC);
1481 rc = next_entry(key, fp, len);
1486 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1487 if (!levdatum->level) {
1491 if (mls_read_level(levdatum->level, fp)) {
1496 rc = hashtab_insert(h, key, levdatum);
1502 sens_destroy(key, levdatum, NULL);
1506 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1509 struct cat_datum *catdatum;
1514 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1520 rc = next_entry(buf, fp, sizeof buf);
1524 len = le32_to_cpu(buf[0]);
1525 catdatum->value = le32_to_cpu(buf[1]);
1526 catdatum->isalias = le32_to_cpu(buf[2]);
1528 key = kmalloc(len + 1, GFP_ATOMIC);
1533 rc = next_entry(key, fp, len);
1538 rc = hashtab_insert(h, key, catdatum);
1545 cat_destroy(key, catdatum, NULL);
1549 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1561 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1563 struct user_datum *upper, *user;
1564 struct policydb *p = datap;
1567 upper = user = datum;
1568 while (upper->bounds) {
1569 struct ebitmap_node *node;
1572 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1573 printk(KERN_ERR "SELinux: user %s: "
1574 "too deep or looped boundary",
1579 upper = p->user_val_to_struct[upper->bounds - 1];
1580 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1581 if (ebitmap_get_bit(&upper->roles, bit))
1585 "SELinux: boundary violated policy: "
1586 "user=%s role=%s bounds=%s\n",
1587 p->p_user_val_to_name[user->value - 1],
1588 p->p_role_val_to_name[bit],
1589 p->p_user_val_to_name[upper->value - 1]);
1598 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1600 struct role_datum *upper, *role;
1601 struct policydb *p = datap;
1604 upper = role = datum;
1605 while (upper->bounds) {
1606 struct ebitmap_node *node;
1609 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1610 printk(KERN_ERR "SELinux: role %s: "
1611 "too deep or looped bounds\n",
1616 upper = p->role_val_to_struct[upper->bounds - 1];
1617 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1618 if (ebitmap_get_bit(&upper->types, bit))
1622 "SELinux: boundary violated policy: "
1623 "role=%s type=%s bounds=%s\n",
1624 p->p_role_val_to_name[role->value - 1],
1625 p->p_type_val_to_name[bit],
1626 p->p_role_val_to_name[upper->value - 1]);
1635 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1637 struct type_datum *upper;
1638 struct policydb *p = datap;
1642 while (upper->bounds) {
1643 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1644 printk(KERN_ERR "SELinux: type %s: "
1645 "too deep or looped boundary\n",
1650 upper = p->type_val_to_struct[upper->bounds - 1];
1651 if (upper->attribute) {
1652 printk(KERN_ERR "SELinux: type %s: "
1653 "bounded by attribute %s",
1655 p->p_type_val_to_name[upper->value - 1]);
1663 static int policydb_bounds_sanity_check(struct policydb *p)
1667 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1670 rc = hashtab_map(p->p_users.table,
1671 user_bounds_sanity_check, p);
1675 rc = hashtab_map(p->p_roles.table,
1676 role_bounds_sanity_check, p);
1680 rc = hashtab_map(p->p_types.table,
1681 type_bounds_sanity_check, p);
1688 extern int ss_initialized;
1690 u16 string_to_security_class(struct policydb *p, const char *name)
1692 struct class_datum *cladatum;
1694 cladatum = hashtab_search(p->p_classes.table, name);
1698 return cladatum->value;
1701 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1703 struct class_datum *cladatum;
1704 struct perm_datum *perdatum = NULL;
1705 struct common_datum *comdatum;
1707 if (!tclass || tclass > p->p_classes.nprim)
1710 cladatum = p->class_val_to_struct[tclass-1];
1711 comdatum = cladatum->comdatum;
1713 perdatum = hashtab_search(comdatum->permissions.table,
1716 perdatum = hashtab_search(cladatum->permissions.table,
1721 return 1U << (perdatum->value-1);
1724 static int range_read(struct policydb *p, void *fp)
1726 struct range_trans *rt = NULL;
1727 struct mls_range *r = NULL;
1732 if (p->policyvers < POLICYDB_VERSION_MLS)
1735 rc = next_entry(buf, fp, sizeof(u32));
1739 nel = le32_to_cpu(buf[0]);
1740 for (i = 0; i < nel; i++) {
1742 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1746 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1750 rt->source_type = le32_to_cpu(buf[0]);
1751 rt->target_type = le32_to_cpu(buf[1]);
1752 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1753 rc = next_entry(buf, fp, sizeof(u32));
1756 rt->target_class = le32_to_cpu(buf[0]);
1758 rt->target_class = p->process_class;
1761 if (!policydb_type_isvalid(p, rt->source_type) ||
1762 !policydb_type_isvalid(p, rt->target_type) ||
1763 !policydb_class_isvalid(p, rt->target_class))
1767 r = kzalloc(sizeof(*r), GFP_KERNEL);
1771 rc = mls_read_range_helper(r, fp);
1776 if (!mls_range_isvalid(p, r)) {
1777 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1781 rc = hashtab_insert(p->range_tr, rt, r);
1788 rangetr_hash_eval(p->range_tr);
1796 static int genfs_read(struct policydb *p, void *fp)
1799 u32 nel, nel2, len, len2;
1801 struct ocontext *l, *c;
1802 struct ocontext *newc = NULL;
1803 struct genfs *genfs_p, *genfs;
1804 struct genfs *newgenfs = NULL;
1806 rc = next_entry(buf, fp, sizeof(u32));
1809 nel = le32_to_cpu(buf[0]);
1811 for (i = 0; i < nel; i++) {
1812 rc = next_entry(buf, fp, sizeof(u32));
1815 len = le32_to_cpu(buf[0]);
1818 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1823 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1824 if (!newgenfs->fstype)
1827 rc = next_entry(newgenfs->fstype, fp, len);
1831 newgenfs->fstype[len] = 0;
1833 for (genfs_p = NULL, genfs = p->genfs; genfs;
1834 genfs_p = genfs, genfs = genfs->next) {
1836 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1837 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1841 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1844 newgenfs->next = genfs;
1846 genfs_p->next = newgenfs;
1848 p->genfs = newgenfs;
1852 rc = next_entry(buf, fp, sizeof(u32));
1856 nel2 = le32_to_cpu(buf[0]);
1857 for (j = 0; j < nel2; j++) {
1858 rc = next_entry(buf, fp, sizeof(u32));
1861 len = le32_to_cpu(buf[0]);
1864 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1869 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1873 rc = next_entry(newc->u.name, fp, len);
1876 newc->u.name[len] = 0;
1878 rc = next_entry(buf, fp, sizeof(u32));
1882 newc->v.sclass = le32_to_cpu(buf[0]);
1883 rc = context_read_and_validate(&newc->context[0], p, fp);
1887 for (l = NULL, c = genfs->head; c;
1888 l = c, c = c->next) {
1890 if (!strcmp(newc->u.name, c->u.name) &&
1891 (!c->v.sclass || !newc->v.sclass ||
1892 newc->v.sclass == c->v.sclass)) {
1893 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1894 genfs->fstype, c->u.name);
1897 len = strlen(newc->u.name);
1898 len2 = strlen(c->u.name);
1914 kfree(newgenfs->fstype);
1916 ocontext_destroy(newc, OCON_FSUSE);
1921 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1927 struct ocontext *l, *c;
1930 for (i = 0; i < info->ocon_num; i++) {
1931 rc = next_entry(buf, fp, sizeof(u32));
1934 nel = le32_to_cpu(buf[0]);
1937 for (j = 0; j < nel; j++) {
1939 c = kzalloc(sizeof(*c), GFP_KERNEL);
1945 p->ocontexts[i] = c;
1950 rc = next_entry(buf, fp, sizeof(u32));
1954 c->sid[0] = le32_to_cpu(buf[0]);
1955 rc = context_read_and_validate(&c->context[0], p, fp);
1961 rc = next_entry(buf, fp, sizeof(u32));
1964 len = le32_to_cpu(buf[0]);
1967 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1971 rc = next_entry(c->u.name, fp, len);
1976 rc = context_read_and_validate(&c->context[0], p, fp);
1979 rc = context_read_and_validate(&c->context[1], p, fp);
1984 rc = next_entry(buf, fp, sizeof(u32)*3);
1987 c->u.port.protocol = le32_to_cpu(buf[0]);
1988 c->u.port.low_port = le32_to_cpu(buf[1]);
1989 c->u.port.high_port = le32_to_cpu(buf[2]);
1990 rc = context_read_and_validate(&c->context[0], p, fp);
1995 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1998 c->u.node.addr = nodebuf[0]; /* network order */
1999 c->u.node.mask = nodebuf[1]; /* network order */
2000 rc = context_read_and_validate(&c->context[0], p, fp);
2005 rc = next_entry(buf, fp, sizeof(u32)*2);
2010 c->v.behavior = le32_to_cpu(buf[0]);
2011 if (c->v.behavior > SECURITY_FS_USE_NONE)
2015 len = le32_to_cpu(buf[1]);
2016 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2020 rc = next_entry(c->u.name, fp, len);
2024 rc = context_read_and_validate(&c->context[0], p, fp);
2031 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2034 for (k = 0; k < 4; k++)
2035 c->u.node6.addr[k] = nodebuf[k];
2036 for (k = 0; k < 4; k++)
2037 c->u.node6.mask[k] = nodebuf[k+4];
2038 rc = context_read_and_validate(&c->context[0], p, fp);
2052 * Read the configuration data from a policy database binary
2053 * representation file into a policy database structure.
2055 int policydb_read(struct policydb *p, void *fp)
2057 struct role_allow *ra, *lra;
2058 struct role_trans *tr, *ltr;
2061 u32 len, nprim, nel;
2064 struct policydb_compat_info *info;
2066 rc = policydb_init(p);
2070 /* Read the magic number and string length. */
2071 rc = next_entry(buf, fp, sizeof(u32) * 2);
2075 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2076 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2077 "not match expected magic number 0x%x\n",
2078 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2082 len = le32_to_cpu(buf[1]);
2083 if (len != strlen(POLICYDB_STRING)) {
2084 printk(KERN_ERR "SELinux: policydb string length %d does not "
2085 "match expected length %Zu\n",
2086 len, strlen(POLICYDB_STRING));
2089 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2090 if (!policydb_str) {
2091 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2092 "string of length %d\n", len);
2096 rc = next_entry(policydb_str, fp, len);
2098 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2099 kfree(policydb_str);
2102 policydb_str[len] = '\0';
2103 if (strcmp(policydb_str, POLICYDB_STRING)) {
2104 printk(KERN_ERR "SELinux: policydb string %s does not match "
2105 "my string %s\n", policydb_str, POLICYDB_STRING);
2106 kfree(policydb_str);
2109 /* Done with policydb_str. */
2110 kfree(policydb_str);
2111 policydb_str = NULL;
2113 /* Read the version and table sizes. */
2114 rc = next_entry(buf, fp, sizeof(u32)*4);
2118 p->policyvers = le32_to_cpu(buf[0]);
2119 if (p->policyvers < POLICYDB_VERSION_MIN ||
2120 p->policyvers > POLICYDB_VERSION_MAX) {
2121 printk(KERN_ERR "SELinux: policydb version %d does not match "
2122 "my version range %d-%d\n",
2123 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2127 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2130 if (p->policyvers < POLICYDB_VERSION_MLS) {
2131 printk(KERN_ERR "SELinux: security policydb version %d "
2132 "(MLS) not backwards compatible\n",
2137 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2138 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2140 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
2141 ebitmap_read(&p->policycaps, fp) != 0)
2144 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
2145 ebitmap_read(&p->permissive_map, fp) != 0)
2148 info = policydb_lookup_compat(p->policyvers);
2150 printk(KERN_ERR "SELinux: unable to find policy compat info "
2151 "for version %d\n", p->policyvers);
2155 if (le32_to_cpu(buf[2]) != info->sym_num ||
2156 le32_to_cpu(buf[3]) != info->ocon_num) {
2157 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2158 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2159 le32_to_cpu(buf[3]),
2160 info->sym_num, info->ocon_num);
2164 for (i = 0; i < info->sym_num; i++) {
2165 rc = next_entry(buf, fp, sizeof(u32)*2);
2168 nprim = le32_to_cpu(buf[0]);
2169 nel = le32_to_cpu(buf[1]);
2170 for (j = 0; j < nel; j++) {
2171 rc = read_f[i](p, p->symtab[i].table, fp);
2176 p->symtab[i].nprim = nprim;
2179 rc = avtab_read(&p->te_avtab, fp, p);
2183 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2184 rc = cond_read_list(p, fp);
2189 rc = next_entry(buf, fp, sizeof(u32));
2192 nel = le32_to_cpu(buf[0]);
2194 for (i = 0; i < nel; i++) {
2195 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2204 rc = next_entry(buf, fp, sizeof(u32)*3);
2207 tr->role = le32_to_cpu(buf[0]);
2208 tr->type = le32_to_cpu(buf[1]);
2209 tr->new_role = le32_to_cpu(buf[2]);
2210 if (!policydb_role_isvalid(p, tr->role) ||
2211 !policydb_type_isvalid(p, tr->type) ||
2212 !policydb_role_isvalid(p, tr->new_role)) {
2219 rc = next_entry(buf, fp, sizeof(u32));
2222 nel = le32_to_cpu(buf[0]);
2224 for (i = 0; i < nel; i++) {
2225 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2234 rc = next_entry(buf, fp, sizeof(u32)*2);
2237 ra->role = le32_to_cpu(buf[0]);
2238 ra->new_role = le32_to_cpu(buf[1]);
2239 if (!policydb_role_isvalid(p, ra->role) ||
2240 !policydb_role_isvalid(p, ra->new_role)) {
2247 rc = policydb_index_classes(p);
2251 rc = policydb_index_others(p);
2255 p->process_class = string_to_security_class(p, "process");
2256 if (!p->process_class)
2258 p->process_trans_perms = string_to_av_perm(p, p->process_class,
2260 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
2262 if (!p->process_trans_perms)
2265 rc = ocontext_read(p, info, fp);
2269 rc = genfs_read(p, fp);
2273 rc = range_read(p, fp);
2278 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2280 GFP_KERNEL | __GFP_ZERO);
2281 if (!p->type_attr_map_array)
2284 /* preallocate so we don't have to worry about the put ever failing */
2285 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2286 GFP_KERNEL | __GFP_ZERO);
2290 for (i = 0; i < p->p_types.nprim; i++) {
2291 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2295 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2296 rc = ebitmap_read(e, fp);
2300 /* add the type itself as the degenerate case */
2301 rc = ebitmap_set_bit(e, i, 1);
2306 rc = policydb_bounds_sanity_check(p);
2316 policydb_destroy(p);