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 int selinux_mls_enabled;
57 static unsigned int symtab_sizes[SYM_NUM] = {
68 struct policydb_compat_info {
74 /* These need to be updated if SYM_NUM or OCON_NUM changes */
75 static struct policydb_compat_info policydb_compat[] = {
77 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
82 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
87 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
92 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
97 .version = POLICYDB_VERSION_MLS,
102 .version = POLICYDB_VERSION_AVTAB,
104 .ocon_num = OCON_NUM,
107 .version = POLICYDB_VERSION_RANGETRANS,
109 .ocon_num = OCON_NUM,
112 .version = POLICYDB_VERSION_POLCAP,
114 .ocon_num = OCON_NUM,
117 .version = POLICYDB_VERSION_PERMISSIVE,
119 .ocon_num = OCON_NUM,
122 .version = POLICYDB_VERSION_BOUNDARY,
124 .ocon_num = OCON_NUM,
128 static struct policydb_compat_info *policydb_lookup_compat(int version)
131 struct policydb_compat_info *info = NULL;
133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
134 if (policydb_compat[i].version == version) {
135 info = &policydb_compat[i];
143 * Initialize the role table.
145 static int roles_init(struct policydb *p)
149 struct role_datum *role;
151 role = kzalloc(sizeof(*role), GFP_KERNEL);
156 role->value = ++p->p_roles.nprim;
157 if (role->value != OBJECT_R_VAL) {
161 key = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
166 strcpy(key, OBJECT_R);
167 rc = hashtab_insert(p->p_roles.table, key, role);
181 * Initialize a policy database structure.
183 static int policydb_init(struct policydb *p)
187 memset(p, 0, sizeof(*p));
189 for (i = 0; i < SYM_NUM; i++) {
190 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
192 goto out_free_symtab;
195 rc = avtab_init(&p->te_avtab);
197 goto out_free_symtab;
201 goto out_free_symtab;
203 rc = cond_policydb_init(p);
205 goto out_free_symtab;
207 ebitmap_init(&p->policycaps);
208 ebitmap_init(&p->permissive_map);
214 for (i = 0; i < SYM_NUM; i++)
215 hashtab_destroy(p->symtab[i].table);
220 * The following *_index functions are used to
221 * define the val_to_name and val_to_struct arrays
222 * in a policy database structure. The val_to_name
223 * arrays are used when converting security context
224 * structures into string representations. The
225 * val_to_struct arrays are used when the attributes
226 * of a class, role, or user are needed.
229 static int common_index(void *key, void *datum, void *datap)
232 struct common_datum *comdatum;
236 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
238 p->p_common_val_to_name[comdatum->value - 1] = key;
242 static int class_index(void *key, void *datum, void *datap)
245 struct class_datum *cladatum;
249 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
251 p->p_class_val_to_name[cladatum->value - 1] = key;
252 p->class_val_to_struct[cladatum->value - 1] = cladatum;
256 static int role_index(void *key, void *datum, void *datap)
259 struct role_datum *role;
264 || role->value > p->p_roles.nprim
265 || role->bounds > p->p_roles.nprim)
267 p->p_role_val_to_name[role->value - 1] = key;
268 p->role_val_to_struct[role->value - 1] = role;
272 static int type_index(void *key, void *datum, void *datap)
275 struct type_datum *typdatum;
280 if (typdatum->primary) {
282 || typdatum->value > p->p_types.nprim
283 || typdatum->bounds > p->p_types.nprim)
285 p->p_type_val_to_name[typdatum->value - 1] = key;
286 p->type_val_to_struct[typdatum->value - 1] = typdatum;
292 static int user_index(void *key, void *datum, void *datap)
295 struct user_datum *usrdatum;
300 || usrdatum->value > p->p_users.nprim
301 || usrdatum->bounds > p->p_users.nprim)
303 p->p_user_val_to_name[usrdatum->value - 1] = key;
304 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
308 static int sens_index(void *key, void *datum, void *datap)
311 struct level_datum *levdatum;
316 if (!levdatum->isalias) {
317 if (!levdatum->level->sens ||
318 levdatum->level->sens > p->p_levels.nprim)
320 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
326 static int cat_index(void *key, void *datum, void *datap)
329 struct cat_datum *catdatum;
334 if (!catdatum->isalias) {
335 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
337 p->p_cat_val_to_name[catdatum->value - 1] = key;
343 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
356 * Define the common val_to_name array and the class
357 * val_to_name and val_to_struct arrays in a policy
358 * database structure.
360 * Caller must clean up upon failure.
362 static int policydb_index_classes(struct policydb *p)
366 p->p_common_val_to_name =
367 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
368 if (!p->p_common_val_to_name) {
373 rc = hashtab_map(p->p_commons.table, common_index, p);
377 p->class_val_to_struct =
378 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
379 if (!p->class_val_to_struct) {
384 p->p_class_val_to_name =
385 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
386 if (!p->p_class_val_to_name) {
391 rc = hashtab_map(p->p_classes.table, class_index, p);
397 static void symtab_hash_eval(struct symtab *s)
401 for (i = 0; i < SYM_NUM; i++) {
402 struct hashtab *h = s[i].table;
403 struct hashtab_info info;
405 hashtab_stat(h, &info);
406 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
407 "longest chain length %d\n", symtab_name[i], h->nel,
408 info.slots_used, h->size, info.max_chain_len);
414 * Define the other val_to_name and val_to_struct arrays
415 * in a policy database structure.
417 * Caller must clean up on failure.
419 static int policydb_index_others(struct policydb *p)
423 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
424 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
425 if (selinux_mls_enabled)
426 printk(", %d sens, %d cats", p->p_levels.nprim,
430 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
431 p->p_classes.nprim, p->te_avtab.nel);
434 avtab_hash_eval(&p->te_avtab, "rules");
435 symtab_hash_eval(p->symtab);
438 p->role_val_to_struct =
439 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
441 if (!p->role_val_to_struct) {
446 p->user_val_to_struct =
447 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
449 if (!p->user_val_to_struct) {
454 p->type_val_to_struct =
455 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
457 if (!p->type_val_to_struct) {
462 if (cond_init_bool_indexes(p)) {
467 for (i = SYM_ROLES; i < SYM_NUM; i++) {
468 p->sym_val_to_name[i] =
469 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
470 if (!p->sym_val_to_name[i]) {
474 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
484 * The following *_destroy functions are used to
485 * free any memory allocated for each kind of
486 * symbol data in the policy database.
489 static int perm_destroy(void *key, void *datum, void *p)
496 static int common_destroy(void *key, void *datum, void *p)
498 struct common_datum *comdatum;
502 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
503 hashtab_destroy(comdatum->permissions.table);
508 static int cls_destroy(void *key, void *datum, void *p)
510 struct class_datum *cladatum;
511 struct constraint_node *constraint, *ctemp;
512 struct constraint_expr *e, *etmp;
516 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
517 hashtab_destroy(cladatum->permissions.table);
518 constraint = cladatum->constraints;
520 e = constraint->expr;
522 ebitmap_destroy(&e->names);
528 constraint = constraint->next;
532 constraint = cladatum->validatetrans;
534 e = constraint->expr;
536 ebitmap_destroy(&e->names);
542 constraint = constraint->next;
546 kfree(cladatum->comkey);
551 static int role_destroy(void *key, void *datum, void *p)
553 struct role_datum *role;
557 ebitmap_destroy(&role->dominates);
558 ebitmap_destroy(&role->types);
563 static int type_destroy(void *key, void *datum, void *p)
570 static int user_destroy(void *key, void *datum, void *p)
572 struct user_datum *usrdatum;
576 ebitmap_destroy(&usrdatum->roles);
577 ebitmap_destroy(&usrdatum->range.level[0].cat);
578 ebitmap_destroy(&usrdatum->range.level[1].cat);
579 ebitmap_destroy(&usrdatum->dfltlevel.cat);
584 static int sens_destroy(void *key, void *datum, void *p)
586 struct level_datum *levdatum;
590 ebitmap_destroy(&levdatum->level->cat);
591 kfree(levdatum->level);
596 static int cat_destroy(void *key, void *datum, void *p)
603 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
615 static void ocontext_destroy(struct ocontext *c, int i)
617 context_destroy(&c->context[0]);
618 context_destroy(&c->context[1]);
619 if (i == OCON_ISID || i == OCON_FS ||
620 i == OCON_NETIF || i == OCON_FSUSE)
626 * Free any memory allocated by a policy database structure.
628 void policydb_destroy(struct policydb *p)
630 struct ocontext *c, *ctmp;
631 struct genfs *g, *gtmp;
633 struct role_allow *ra, *lra = NULL;
634 struct role_trans *tr, *ltr = NULL;
635 struct range_trans *rt, *lrt = NULL;
637 for (i = 0; i < SYM_NUM; i++) {
639 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
640 hashtab_destroy(p->symtab[i].table);
643 for (i = 0; i < SYM_NUM; i++)
644 kfree(p->sym_val_to_name[i]);
646 kfree(p->class_val_to_struct);
647 kfree(p->role_val_to_struct);
648 kfree(p->user_val_to_struct);
649 kfree(p->type_val_to_struct);
651 avtab_destroy(&p->te_avtab);
653 for (i = 0; i < OCON_NUM; i++) {
659 ocontext_destroy(ctmp, i);
661 p->ocontexts[i] = NULL;
672 ocontext_destroy(ctmp, OCON_FSUSE);
680 cond_policydb_destroy(p);
682 for (tr = p->role_tr; tr; tr = tr->next) {
689 for (ra = p->role_allow; ra; ra = ra->next) {
696 for (rt = p->range_tr; rt; rt = rt->next) {
699 ebitmap_destroy(&lrt->target_range.level[0].cat);
700 ebitmap_destroy(&lrt->target_range.level[1].cat);
706 ebitmap_destroy(&lrt->target_range.level[0].cat);
707 ebitmap_destroy(&lrt->target_range.level[1].cat);
711 if (p->type_attr_map) {
712 for (i = 0; i < p->p_types.nprim; i++)
713 ebitmap_destroy(&p->type_attr_map[i]);
715 kfree(p->type_attr_map);
716 kfree(p->undefined_perms);
717 ebitmap_destroy(&p->policycaps);
718 ebitmap_destroy(&p->permissive_map);
724 * Load the initial SIDs specified in a policy database
725 * structure into a SID table.
727 int policydb_load_isids(struct policydb *p, struct sidtab *s)
729 struct ocontext *head, *c;
734 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
738 head = p->ocontexts[OCON_ISID];
739 for (c = head; c; c = c->next) {
740 if (!c->context[0].user) {
741 printk(KERN_ERR "SELinux: SID %s was never "
742 "defined.\n", c->u.name);
746 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
747 printk(KERN_ERR "SELinux: unable to load initial "
748 "SID %s.\n", c->u.name);
757 int policydb_class_isvalid(struct policydb *p, unsigned int class)
759 if (!class || class > p->p_classes.nprim)
764 int policydb_role_isvalid(struct policydb *p, unsigned int role)
766 if (!role || role > p->p_roles.nprim)
771 int policydb_type_isvalid(struct policydb *p, unsigned int type)
773 if (!type || type > p->p_types.nprim)
779 * Return 1 if the fields in the security context
780 * structure `c' are valid. Return 0 otherwise.
782 int policydb_context_isvalid(struct policydb *p, struct context *c)
784 struct role_datum *role;
785 struct user_datum *usrdatum;
787 if (!c->role || c->role > p->p_roles.nprim)
790 if (!c->user || c->user > p->p_users.nprim)
793 if (!c->type || c->type > p->p_types.nprim)
796 if (c->role != OBJECT_R_VAL) {
798 * Role must be authorized for the type.
800 role = p->role_val_to_struct[c->role - 1];
801 if (!ebitmap_get_bit(&role->types,
803 /* role may not be associated with type */
807 * User must be authorized for the role.
809 usrdatum = p->user_val_to_struct[c->user - 1];
813 if (!ebitmap_get_bit(&usrdatum->roles,
815 /* user may not be associated with role */
819 if (!mls_context_isvalid(p, c))
826 * Read a MLS range structure from a policydb binary
827 * representation file.
829 static int mls_read_range_helper(struct mls_range *r, void *fp)
835 rc = next_entry(buf, fp, sizeof(u32));
839 items = le32_to_cpu(buf[0]);
840 if (items > ARRAY_SIZE(buf)) {
841 printk(KERN_ERR "SELinux: mls: range overflow\n");
845 rc = next_entry(buf, fp, sizeof(u32) * items);
847 printk(KERN_ERR "SELinux: mls: truncated range\n");
850 r->level[0].sens = le32_to_cpu(buf[0]);
852 r->level[1].sens = le32_to_cpu(buf[1]);
854 r->level[1].sens = r->level[0].sens;
856 rc = ebitmap_read(&r->level[0].cat, fp);
858 printk(KERN_ERR "SELinux: mls: error reading low "
863 rc = ebitmap_read(&r->level[1].cat, fp);
865 printk(KERN_ERR "SELinux: mls: error reading high "
870 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
872 printk(KERN_ERR "SELinux: mls: out of memory\n");
881 ebitmap_destroy(&r->level[0].cat);
886 * Read and validate a security context structure
887 * from a policydb binary representation file.
889 static int context_read_and_validate(struct context *c,
896 rc = next_entry(buf, fp, sizeof buf);
898 printk(KERN_ERR "SELinux: context truncated\n");
901 c->user = le32_to_cpu(buf[0]);
902 c->role = le32_to_cpu(buf[1]);
903 c->type = le32_to_cpu(buf[2]);
904 if (p->policyvers >= POLICYDB_VERSION_MLS) {
905 if (mls_read_range_helper(&c->range, fp)) {
906 printk(KERN_ERR "SELinux: error reading MLS range of "
913 if (!policydb_context_isvalid(p, c)) {
914 printk(KERN_ERR "SELinux: invalid security context\n");
923 * The following *_read functions are used to
924 * read the symbol data from a policy database
925 * binary representation file.
928 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
931 struct perm_datum *perdatum;
936 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
942 rc = next_entry(buf, fp, sizeof buf);
946 len = le32_to_cpu(buf[0]);
947 perdatum->value = le32_to_cpu(buf[1]);
949 key = kmalloc(len + 1, GFP_KERNEL);
954 rc = next_entry(key, fp, len);
959 rc = hashtab_insert(h, key, perdatum);
965 perm_destroy(key, perdatum, NULL);
969 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
972 struct common_datum *comdatum;
977 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
983 rc = next_entry(buf, fp, sizeof buf);
987 len = le32_to_cpu(buf[0]);
988 comdatum->value = le32_to_cpu(buf[1]);
990 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
993 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
994 nel = le32_to_cpu(buf[3]);
996 key = kmalloc(len + 1, GFP_KERNEL);
1001 rc = next_entry(key, fp, len);
1006 for (i = 0; i < nel; i++) {
1007 rc = perm_read(p, comdatum->permissions.table, fp);
1012 rc = hashtab_insert(h, key, comdatum);
1018 common_destroy(key, comdatum, NULL);
1022 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1023 int allowxtarget, void *fp)
1025 struct constraint_node *c, *lc;
1026 struct constraint_expr *e, *le;
1029 int rc, i, j, depth;
1032 for (i = 0; i < ncons; i++) {
1033 c = kzalloc(sizeof(*c), GFP_KERNEL);
1042 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1045 c->permissions = le32_to_cpu(buf[0]);
1046 nexpr = le32_to_cpu(buf[1]);
1049 for (j = 0; j < nexpr; j++) {
1050 e = kzalloc(sizeof(*e), GFP_KERNEL);
1059 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1062 e->expr_type = le32_to_cpu(buf[0]);
1063 e->attr = le32_to_cpu(buf[1]);
1064 e->op = le32_to_cpu(buf[2]);
1066 switch (e->expr_type) {
1078 if (depth == (CEXPR_MAXDEPTH - 1))
1083 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1085 if (depth == (CEXPR_MAXDEPTH - 1))
1088 if (ebitmap_read(&e->names, fp))
1104 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1107 struct class_datum *cladatum;
1109 u32 len, len2, ncons, nel;
1112 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1118 rc = next_entry(buf, fp, sizeof(u32)*6);
1122 len = le32_to_cpu(buf[0]);
1123 len2 = le32_to_cpu(buf[1]);
1124 cladatum->value = le32_to_cpu(buf[2]);
1126 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1129 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1130 nel = le32_to_cpu(buf[4]);
1132 ncons = le32_to_cpu(buf[5]);
1134 key = kmalloc(len + 1, GFP_KERNEL);
1139 rc = next_entry(key, fp, len);
1145 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1146 if (!cladatum->comkey) {
1150 rc = next_entry(cladatum->comkey, fp, len2);
1153 cladatum->comkey[len2] = '\0';
1155 cladatum->comdatum = hashtab_search(p->p_commons.table,
1157 if (!cladatum->comdatum) {
1158 printk(KERN_ERR "SELinux: unknown common %s\n",
1164 for (i = 0; i < nel; i++) {
1165 rc = perm_read(p, cladatum->permissions.table, fp);
1170 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1174 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1175 /* grab the validatetrans rules */
1176 rc = next_entry(buf, fp, sizeof(u32));
1179 ncons = le32_to_cpu(buf[0]);
1180 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1185 rc = hashtab_insert(h, key, cladatum);
1193 cls_destroy(key, cladatum, NULL);
1197 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1200 struct role_datum *role;
1201 int rc, to_read = 2;
1205 role = kzalloc(sizeof(*role), GFP_KERNEL);
1211 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1214 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1218 len = le32_to_cpu(buf[0]);
1219 role->value = le32_to_cpu(buf[1]);
1220 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1221 role->bounds = le32_to_cpu(buf[2]);
1223 key = kmalloc(len + 1, GFP_KERNEL);
1228 rc = next_entry(key, fp, len);
1233 rc = ebitmap_read(&role->dominates, fp);
1237 rc = ebitmap_read(&role->types, fp);
1241 if (strcmp(key, OBJECT_R) == 0) {
1242 if (role->value != OBJECT_R_VAL) {
1243 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1244 OBJECT_R, role->value);
1252 rc = hashtab_insert(h, key, role);
1258 role_destroy(key, role, NULL);
1262 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1265 struct type_datum *typdatum;
1266 int rc, to_read = 3;
1270 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1276 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1279 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1283 len = le32_to_cpu(buf[0]);
1284 typdatum->value = le32_to_cpu(buf[1]);
1285 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1286 u32 prop = le32_to_cpu(buf[2]);
1288 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1289 typdatum->primary = 1;
1290 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1291 typdatum->attribute = 1;
1293 typdatum->bounds = le32_to_cpu(buf[3]);
1295 typdatum->primary = le32_to_cpu(buf[2]);
1298 key = kmalloc(len + 1, GFP_KERNEL);
1303 rc = next_entry(key, fp, len);
1308 rc = hashtab_insert(h, key, typdatum);
1314 type_destroy(key, typdatum, NULL);
1320 * Read a MLS level structure from a policydb binary
1321 * representation file.
1323 static int mls_read_level(struct mls_level *lp, void *fp)
1328 memset(lp, 0, sizeof(*lp));
1330 rc = next_entry(buf, fp, sizeof buf);
1332 printk(KERN_ERR "SELinux: mls: truncated level\n");
1335 lp->sens = le32_to_cpu(buf[0]);
1337 if (ebitmap_read(&lp->cat, fp)) {
1338 printk(KERN_ERR "SELinux: mls: error reading level "
1349 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1352 struct user_datum *usrdatum;
1353 int rc, to_read = 2;
1357 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1363 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1366 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1370 len = le32_to_cpu(buf[0]);
1371 usrdatum->value = le32_to_cpu(buf[1]);
1372 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1373 usrdatum->bounds = le32_to_cpu(buf[2]);
1375 key = kmalloc(len + 1, GFP_KERNEL);
1380 rc = next_entry(key, fp, len);
1385 rc = ebitmap_read(&usrdatum->roles, fp);
1389 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1390 rc = mls_read_range_helper(&usrdatum->range, fp);
1393 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1398 rc = hashtab_insert(h, key, usrdatum);
1404 user_destroy(key, usrdatum, NULL);
1408 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1411 struct level_datum *levdatum;
1416 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1422 rc = next_entry(buf, fp, sizeof buf);
1426 len = le32_to_cpu(buf[0]);
1427 levdatum->isalias = le32_to_cpu(buf[1]);
1429 key = kmalloc(len + 1, GFP_ATOMIC);
1434 rc = next_entry(key, fp, len);
1439 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1440 if (!levdatum->level) {
1444 if (mls_read_level(levdatum->level, fp)) {
1449 rc = hashtab_insert(h, key, levdatum);
1455 sens_destroy(key, levdatum, NULL);
1459 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1462 struct cat_datum *catdatum;
1467 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1473 rc = next_entry(buf, fp, sizeof buf);
1477 len = le32_to_cpu(buf[0]);
1478 catdatum->value = le32_to_cpu(buf[1]);
1479 catdatum->isalias = le32_to_cpu(buf[2]);
1481 key = kmalloc(len + 1, GFP_ATOMIC);
1486 rc = next_entry(key, fp, len);
1491 rc = hashtab_insert(h, key, catdatum);
1498 cat_destroy(key, catdatum, NULL);
1502 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1514 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1516 struct user_datum *upper, *user;
1517 struct policydb *p = datap;
1520 upper = user = datum;
1521 while (upper->bounds) {
1522 struct ebitmap_node *node;
1525 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1526 printk(KERN_ERR "SELinux: user %s: "
1527 "too deep or looped boundary",
1532 upper = p->user_val_to_struct[upper->bounds - 1];
1533 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1534 if (ebitmap_get_bit(&upper->roles, bit))
1538 "SELinux: boundary violated policy: "
1539 "user=%s role=%s bounds=%s\n",
1540 p->p_user_val_to_name[user->value - 1],
1541 p->p_role_val_to_name[bit],
1542 p->p_user_val_to_name[upper->value - 1]);
1551 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1553 struct role_datum *upper, *role;
1554 struct policydb *p = datap;
1557 upper = role = datum;
1558 while (upper->bounds) {
1559 struct ebitmap_node *node;
1562 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1563 printk(KERN_ERR "SELinux: role %s: "
1564 "too deep or looped bounds\n",
1569 upper = p->role_val_to_struct[upper->bounds - 1];
1570 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1571 if (ebitmap_get_bit(&upper->types, bit))
1575 "SELinux: boundary violated policy: "
1576 "role=%s type=%s bounds=%s\n",
1577 p->p_role_val_to_name[role->value - 1],
1578 p->p_type_val_to_name[bit],
1579 p->p_role_val_to_name[upper->value - 1]);
1588 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1590 struct type_datum *upper, *type;
1591 struct policydb *p = datap;
1594 upper = type = datum;
1595 while (upper->bounds) {
1596 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1597 printk(KERN_ERR "SELinux: type %s: "
1598 "too deep or looped boundary\n",
1603 upper = p->type_val_to_struct[upper->bounds - 1];
1604 if (upper->attribute) {
1605 printk(KERN_ERR "SELinux: type %s: "
1606 "bounded by attribute %s",
1608 p->p_type_val_to_name[upper->value - 1]);
1616 static int policydb_bounds_sanity_check(struct policydb *p)
1620 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1623 rc = hashtab_map(p->p_users.table,
1624 user_bounds_sanity_check, p);
1628 rc = hashtab_map(p->p_roles.table,
1629 role_bounds_sanity_check, p);
1633 rc = hashtab_map(p->p_types.table,
1634 type_bounds_sanity_check, p);
1641 extern int ss_initialized;
1644 * Read the configuration data from a policy database binary
1645 * representation file into a policy database structure.
1647 int policydb_read(struct policydb *p, void *fp)
1649 struct role_allow *ra, *lra;
1650 struct role_trans *tr, *ltr;
1651 struct ocontext *l, *c, *newc;
1652 struct genfs *genfs_p, *genfs, *newgenfs;
1656 u32 len, len2, config, nprim, nel, nel2;
1658 struct policydb_compat_info *info;
1659 struct range_trans *rt, *lrt;
1663 rc = policydb_init(p);
1667 /* Read the magic number and string length. */
1668 rc = next_entry(buf, fp, sizeof(u32) * 2);
1672 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1673 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
1674 "not match expected magic number 0x%x\n",
1675 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1679 len = le32_to_cpu(buf[1]);
1680 if (len != strlen(POLICYDB_STRING)) {
1681 printk(KERN_ERR "SELinux: policydb string length %d does not "
1682 "match expected length %Zu\n",
1683 len, strlen(POLICYDB_STRING));
1686 policydb_str = kmalloc(len + 1, GFP_KERNEL);
1687 if (!policydb_str) {
1688 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
1689 "string of length %d\n", len);
1693 rc = next_entry(policydb_str, fp, len);
1695 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
1696 kfree(policydb_str);
1699 policydb_str[len] = '\0';
1700 if (strcmp(policydb_str, POLICYDB_STRING)) {
1701 printk(KERN_ERR "SELinux: policydb string %s does not match "
1702 "my string %s\n", policydb_str, POLICYDB_STRING);
1703 kfree(policydb_str);
1706 /* Done with policydb_str. */
1707 kfree(policydb_str);
1708 policydb_str = NULL;
1710 /* Read the version, config, and table sizes. */
1711 rc = next_entry(buf, fp, sizeof(u32)*4);
1715 p->policyvers = le32_to_cpu(buf[0]);
1716 if (p->policyvers < POLICYDB_VERSION_MIN ||
1717 p->policyvers > POLICYDB_VERSION_MAX) {
1718 printk(KERN_ERR "SELinux: policydb version %d does not match "
1719 "my version range %d-%d\n",
1720 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1724 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1725 if (ss_initialized && !selinux_mls_enabled) {
1726 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1727 " and MLS policies\n");
1730 selinux_mls_enabled = 1;
1731 config |= POLICYDB_CONFIG_MLS;
1733 if (p->policyvers < POLICYDB_VERSION_MLS) {
1734 printk(KERN_ERR "SELinux: security policydb version %d "
1735 "(MLS) not backwards compatible\n",
1740 if (ss_initialized && selinux_mls_enabled) {
1741 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1742 " non-MLS policies\n");
1746 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1747 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
1749 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1750 ebitmap_read(&p->policycaps, fp) != 0)
1753 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1754 ebitmap_read(&p->permissive_map, fp) != 0)
1757 info = policydb_lookup_compat(p->policyvers);
1759 printk(KERN_ERR "SELinux: unable to find policy compat info "
1760 "for version %d\n", p->policyvers);
1764 if (le32_to_cpu(buf[2]) != info->sym_num ||
1765 le32_to_cpu(buf[3]) != info->ocon_num) {
1766 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
1767 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1768 le32_to_cpu(buf[3]),
1769 info->sym_num, info->ocon_num);
1773 for (i = 0; i < info->sym_num; i++) {
1774 rc = next_entry(buf, fp, sizeof(u32)*2);
1777 nprim = le32_to_cpu(buf[0]);
1778 nel = le32_to_cpu(buf[1]);
1779 for (j = 0; j < nel; j++) {
1780 rc = read_f[i](p, p->symtab[i].table, fp);
1785 p->symtab[i].nprim = nprim;
1788 rc = avtab_read(&p->te_avtab, fp, p);
1792 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1793 rc = cond_read_list(p, fp);
1798 rc = next_entry(buf, fp, sizeof(u32));
1801 nel = le32_to_cpu(buf[0]);
1803 for (i = 0; i < nel; i++) {
1804 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
1813 rc = next_entry(buf, fp, sizeof(u32)*3);
1816 tr->role = le32_to_cpu(buf[0]);
1817 tr->type = le32_to_cpu(buf[1]);
1818 tr->new_role = le32_to_cpu(buf[2]);
1819 if (!policydb_role_isvalid(p, tr->role) ||
1820 !policydb_type_isvalid(p, tr->type) ||
1821 !policydb_role_isvalid(p, tr->new_role)) {
1828 rc = next_entry(buf, fp, sizeof(u32));
1831 nel = le32_to_cpu(buf[0]);
1833 for (i = 0; i < nel; i++) {
1834 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1843 rc = next_entry(buf, fp, sizeof(u32)*2);
1846 ra->role = le32_to_cpu(buf[0]);
1847 ra->new_role = le32_to_cpu(buf[1]);
1848 if (!policydb_role_isvalid(p, ra->role) ||
1849 !policydb_role_isvalid(p, ra->new_role)) {
1856 rc = policydb_index_classes(p);
1860 rc = policydb_index_others(p);
1864 for (i = 0; i < info->ocon_num; i++) {
1865 rc = next_entry(buf, fp, sizeof(u32));
1868 nel = le32_to_cpu(buf[0]);
1870 for (j = 0; j < nel; j++) {
1871 c = kzalloc(sizeof(*c), GFP_KERNEL);
1879 p->ocontexts[i] = c;
1884 rc = next_entry(buf, fp, sizeof(u32));
1887 c->sid[0] = le32_to_cpu(buf[0]);
1888 rc = context_read_and_validate(&c->context[0], p, fp);
1894 rc = next_entry(buf, fp, sizeof(u32));
1897 len = le32_to_cpu(buf[0]);
1898 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1903 rc = next_entry(c->u.name, fp, len);
1907 rc = context_read_and_validate(&c->context[0], p, fp);
1910 rc = context_read_and_validate(&c->context[1], p, fp);
1915 rc = next_entry(buf, fp, sizeof(u32)*3);
1918 c->u.port.protocol = le32_to_cpu(buf[0]);
1919 c->u.port.low_port = le32_to_cpu(buf[1]);
1920 c->u.port.high_port = le32_to_cpu(buf[2]);
1921 rc = context_read_and_validate(&c->context[0], p, fp);
1926 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1929 c->u.node.addr = nodebuf[0]; /* network order */
1930 c->u.node.mask = nodebuf[1]; /* network order */
1931 rc = context_read_and_validate(&c->context[0], p, fp);
1936 rc = next_entry(buf, fp, sizeof(u32)*2);
1939 c->v.behavior = le32_to_cpu(buf[0]);
1940 if (c->v.behavior > SECURITY_FS_USE_NONE)
1942 len = le32_to_cpu(buf[1]);
1943 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1948 rc = next_entry(c->u.name, fp, len);
1952 rc = context_read_and_validate(&c->context[0], p, fp);
1959 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
1962 for (k = 0; k < 4; k++)
1963 c->u.node6.addr[k] = nodebuf[k];
1964 for (k = 0; k < 4; k++)
1965 c->u.node6.mask[k] = nodebuf[k+4];
1966 if (context_read_and_validate(&c->context[0], p, fp))
1974 rc = next_entry(buf, fp, sizeof(u32));
1977 nel = le32_to_cpu(buf[0]);
1980 for (i = 0; i < nel; i++) {
1981 rc = next_entry(buf, fp, sizeof(u32));
1984 len = le32_to_cpu(buf[0]);
1985 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1991 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1992 if (!newgenfs->fstype) {
1997 rc = next_entry(newgenfs->fstype, fp, len);
1999 kfree(newgenfs->fstype);
2003 newgenfs->fstype[len] = 0;
2004 for (genfs_p = NULL, genfs = p->genfs; genfs;
2005 genfs_p = genfs, genfs = genfs->next) {
2006 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2007 printk(KERN_ERR "SELinux: dup genfs "
2008 "fstype %s\n", newgenfs->fstype);
2009 kfree(newgenfs->fstype);
2013 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2016 newgenfs->next = genfs;
2018 genfs_p->next = newgenfs;
2020 p->genfs = newgenfs;
2021 rc = next_entry(buf, fp, sizeof(u32));
2024 nel2 = le32_to_cpu(buf[0]);
2025 for (j = 0; j < nel2; j++) {
2026 rc = next_entry(buf, fp, sizeof(u32));
2029 len = le32_to_cpu(buf[0]);
2031 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2037 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2038 if (!newc->u.name) {
2042 rc = next_entry(newc->u.name, fp, len);
2045 newc->u.name[len] = 0;
2046 rc = next_entry(buf, fp, sizeof(u32));
2049 newc->v.sclass = le32_to_cpu(buf[0]);
2050 if (context_read_and_validate(&newc->context[0], p, fp))
2052 for (l = NULL, c = newgenfs->head; c;
2053 l = c, c = c->next) {
2054 if (!strcmp(newc->u.name, c->u.name) &&
2055 (!c->v.sclass || !newc->v.sclass ||
2056 newc->v.sclass == c->v.sclass)) {
2057 printk(KERN_ERR "SELinux: dup genfs "
2059 newgenfs->fstype, c->u.name);
2062 len = strlen(newc->u.name);
2063 len2 = strlen(c->u.name);
2072 newgenfs->head = newc;
2076 if (p->policyvers >= POLICYDB_VERSION_MLS) {
2077 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
2078 rc = next_entry(buf, fp, sizeof(u32));
2081 nel = le32_to_cpu(buf[0]);
2083 for (i = 0; i < nel; i++) {
2084 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
2093 rc = next_entry(buf, fp, (sizeof(u32) * 2));
2096 rt->source_type = le32_to_cpu(buf[0]);
2097 rt->target_type = le32_to_cpu(buf[1]);
2099 rc = next_entry(buf, fp, sizeof(u32));
2102 rt->target_class = le32_to_cpu(buf[0]);
2104 rt->target_class = SECCLASS_PROCESS;
2105 if (!policydb_type_isvalid(p, rt->source_type) ||
2106 !policydb_type_isvalid(p, rt->target_type) ||
2107 !policydb_class_isvalid(p, rt->target_class)) {
2111 rc = mls_read_range_helper(&rt->target_range, fp);
2114 if (!mls_range_isvalid(p, &rt->target_range)) {
2115 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
2122 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2123 if (!p->type_attr_map)
2126 for (i = 0; i < p->p_types.nprim; i++) {
2127 ebitmap_init(&p->type_attr_map[i]);
2128 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2129 if (ebitmap_read(&p->type_attr_map[i], fp))
2132 /* add the type itself as the degenerate case */
2133 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2137 rc = policydb_bounds_sanity_check(p);
2145 ocontext_destroy(newc, OCON_FSUSE);
2149 policydb_destroy(p);