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);
180 static u32 rangetr_hash(struct hashtab *h, const void *k)
182 const struct range_trans *key = k;
183 return (key->source_type + (key->target_type << 3) +
184 (key->target_class << 5)) & (h->size - 1);
187 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
189 const struct range_trans *key1 = k1, *key2 = k2;
190 return (key1->source_type != key2->source_type ||
191 key1->target_type != key2->target_type ||
192 key1->target_class != key2->target_class);
196 * Initialize a policy database structure.
198 static int policydb_init(struct policydb *p)
202 memset(p, 0, sizeof(*p));
204 for (i = 0; i < SYM_NUM; i++) {
205 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
207 goto out_free_symtab;
210 rc = avtab_init(&p->te_avtab);
212 goto out_free_symtab;
216 goto out_free_symtab;
218 rc = cond_policydb_init(p);
220 goto out_free_symtab;
222 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
224 goto out_free_symtab;
226 ebitmap_init(&p->policycaps);
227 ebitmap_init(&p->permissive_map);
233 for (i = 0; i < SYM_NUM; i++)
234 hashtab_destroy(p->symtab[i].table);
239 * The following *_index functions are used to
240 * define the val_to_name and val_to_struct arrays
241 * in a policy database structure. The val_to_name
242 * arrays are used when converting security context
243 * structures into string representations. The
244 * val_to_struct arrays are used when the attributes
245 * of a class, role, or user are needed.
248 static int common_index(void *key, void *datum, void *datap)
251 struct common_datum *comdatum;
255 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
257 p->p_common_val_to_name[comdatum->value - 1] = key;
261 static int class_index(void *key, void *datum, void *datap)
264 struct class_datum *cladatum;
268 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
270 p->p_class_val_to_name[cladatum->value - 1] = key;
271 p->class_val_to_struct[cladatum->value - 1] = cladatum;
275 static int role_index(void *key, void *datum, void *datap)
278 struct role_datum *role;
283 || role->value > p->p_roles.nprim
284 || role->bounds > p->p_roles.nprim)
286 p->p_role_val_to_name[role->value - 1] = key;
287 p->role_val_to_struct[role->value - 1] = role;
291 static int type_index(void *key, void *datum, void *datap)
294 struct type_datum *typdatum;
299 if (typdatum->primary) {
301 || typdatum->value > p->p_types.nprim
302 || typdatum->bounds > p->p_types.nprim)
304 p->p_type_val_to_name[typdatum->value - 1] = key;
305 p->type_val_to_struct[typdatum->value - 1] = typdatum;
311 static int user_index(void *key, void *datum, void *datap)
314 struct user_datum *usrdatum;
319 || usrdatum->value > p->p_users.nprim
320 || usrdatum->bounds > p->p_users.nprim)
322 p->p_user_val_to_name[usrdatum->value - 1] = key;
323 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
327 static int sens_index(void *key, void *datum, void *datap)
330 struct level_datum *levdatum;
335 if (!levdatum->isalias) {
336 if (!levdatum->level->sens ||
337 levdatum->level->sens > p->p_levels.nprim)
339 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
345 static int cat_index(void *key, void *datum, void *datap)
348 struct cat_datum *catdatum;
353 if (!catdatum->isalias) {
354 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
356 p->p_cat_val_to_name[catdatum->value - 1] = key;
362 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
375 * Define the common val_to_name array and the class
376 * val_to_name and val_to_struct arrays in a policy
377 * database structure.
379 * Caller must clean up upon failure.
381 static int policydb_index_classes(struct policydb *p)
385 p->p_common_val_to_name =
386 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
387 if (!p->p_common_val_to_name) {
392 rc = hashtab_map(p->p_commons.table, common_index, p);
396 p->class_val_to_struct =
397 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
398 if (!p->class_val_to_struct) {
403 p->p_class_val_to_name =
404 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
405 if (!p->p_class_val_to_name) {
410 rc = hashtab_map(p->p_classes.table, class_index, p);
416 static void symtab_hash_eval(struct symtab *s)
420 for (i = 0; i < SYM_NUM; i++) {
421 struct hashtab *h = s[i].table;
422 struct hashtab_info info;
424 hashtab_stat(h, &info);
425 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
426 "longest chain length %d\n", symtab_name[i], h->nel,
427 info.slots_used, h->size, info.max_chain_len);
431 static void rangetr_hash_eval(struct hashtab *h)
433 struct hashtab_info info;
435 hashtab_stat(h, &info);
436 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
437 "longest chain length %d\n", h->nel,
438 info.slots_used, h->size, info.max_chain_len);
441 static inline void rangetr_hash_eval(struct hashtab *h)
447 * Define the other val_to_name and val_to_struct arrays
448 * in a policy database structure.
450 * Caller must clean up on failure.
452 static int policydb_index_others(struct policydb *p)
456 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
457 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
458 if (selinux_mls_enabled)
459 printk(", %d sens, %d cats", p->p_levels.nprim,
463 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
464 p->p_classes.nprim, p->te_avtab.nel);
467 avtab_hash_eval(&p->te_avtab, "rules");
468 symtab_hash_eval(p->symtab);
471 p->role_val_to_struct =
472 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
474 if (!p->role_val_to_struct) {
479 p->user_val_to_struct =
480 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
482 if (!p->user_val_to_struct) {
487 p->type_val_to_struct =
488 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
490 if (!p->type_val_to_struct) {
495 if (cond_init_bool_indexes(p)) {
500 for (i = SYM_ROLES; i < SYM_NUM; i++) {
501 p->sym_val_to_name[i] =
502 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
503 if (!p->sym_val_to_name[i]) {
507 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
517 * The following *_destroy functions are used to
518 * free any memory allocated for each kind of
519 * symbol data in the policy database.
522 static int perm_destroy(void *key, void *datum, void *p)
529 static int common_destroy(void *key, void *datum, void *p)
531 struct common_datum *comdatum;
535 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
536 hashtab_destroy(comdatum->permissions.table);
541 static int cls_destroy(void *key, void *datum, void *p)
543 struct class_datum *cladatum;
544 struct constraint_node *constraint, *ctemp;
545 struct constraint_expr *e, *etmp;
549 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
550 hashtab_destroy(cladatum->permissions.table);
551 constraint = cladatum->constraints;
553 e = constraint->expr;
555 ebitmap_destroy(&e->names);
561 constraint = constraint->next;
565 constraint = cladatum->validatetrans;
567 e = constraint->expr;
569 ebitmap_destroy(&e->names);
575 constraint = constraint->next;
579 kfree(cladatum->comkey);
584 static int role_destroy(void *key, void *datum, void *p)
586 struct role_datum *role;
590 ebitmap_destroy(&role->dominates);
591 ebitmap_destroy(&role->types);
596 static int type_destroy(void *key, void *datum, void *p)
603 static int user_destroy(void *key, void *datum, void *p)
605 struct user_datum *usrdatum;
609 ebitmap_destroy(&usrdatum->roles);
610 ebitmap_destroy(&usrdatum->range.level[0].cat);
611 ebitmap_destroy(&usrdatum->range.level[1].cat);
612 ebitmap_destroy(&usrdatum->dfltlevel.cat);
617 static int sens_destroy(void *key, void *datum, void *p)
619 struct level_datum *levdatum;
623 ebitmap_destroy(&levdatum->level->cat);
624 kfree(levdatum->level);
629 static int cat_destroy(void *key, void *datum, void *p)
636 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
648 static int range_tr_destroy(void *key, void *datum, void *p)
650 struct mls_range *rt = datum;
652 ebitmap_destroy(&rt->level[0].cat);
653 ebitmap_destroy(&rt->level[1].cat);
659 static void ocontext_destroy(struct ocontext *c, int i)
661 context_destroy(&c->context[0]);
662 context_destroy(&c->context[1]);
663 if (i == OCON_ISID || i == OCON_FS ||
664 i == OCON_NETIF || i == OCON_FSUSE)
670 * Free any memory allocated by a policy database structure.
672 void policydb_destroy(struct policydb *p)
674 struct ocontext *c, *ctmp;
675 struct genfs *g, *gtmp;
677 struct role_allow *ra, *lra = NULL;
678 struct role_trans *tr, *ltr = NULL;
680 for (i = 0; i < SYM_NUM; i++) {
682 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
683 hashtab_destroy(p->symtab[i].table);
686 for (i = 0; i < SYM_NUM; i++)
687 kfree(p->sym_val_to_name[i]);
689 kfree(p->class_val_to_struct);
690 kfree(p->role_val_to_struct);
691 kfree(p->user_val_to_struct);
692 kfree(p->type_val_to_struct);
694 avtab_destroy(&p->te_avtab);
696 for (i = 0; i < OCON_NUM; i++) {
702 ocontext_destroy(ctmp, i);
704 p->ocontexts[i] = NULL;
715 ocontext_destroy(ctmp, OCON_FSUSE);
723 cond_policydb_destroy(p);
725 for (tr = p->role_tr; tr; tr = tr->next) {
732 for (ra = p->role_allow; ra; ra = ra->next) {
739 hashtab_map(p->range_tr, range_tr_destroy, NULL);
740 hashtab_destroy(p->range_tr);
742 if (p->type_attr_map) {
743 for (i = 0; i < p->p_types.nprim; i++)
744 ebitmap_destroy(&p->type_attr_map[i]);
746 kfree(p->type_attr_map);
747 ebitmap_destroy(&p->policycaps);
748 ebitmap_destroy(&p->permissive_map);
754 * Load the initial SIDs specified in a policy database
755 * structure into a SID table.
757 int policydb_load_isids(struct policydb *p, struct sidtab *s)
759 struct ocontext *head, *c;
764 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
768 head = p->ocontexts[OCON_ISID];
769 for (c = head; c; c = c->next) {
770 if (!c->context[0].user) {
771 printk(KERN_ERR "SELinux: SID %s was never "
772 "defined.\n", c->u.name);
776 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
777 printk(KERN_ERR "SELinux: unable to load initial "
778 "SID %s.\n", c->u.name);
787 int policydb_class_isvalid(struct policydb *p, unsigned int class)
789 if (!class || class > p->p_classes.nprim)
794 int policydb_role_isvalid(struct policydb *p, unsigned int role)
796 if (!role || role > p->p_roles.nprim)
801 int policydb_type_isvalid(struct policydb *p, unsigned int type)
803 if (!type || type > p->p_types.nprim)
809 * Return 1 if the fields in the security context
810 * structure `c' are valid. Return 0 otherwise.
812 int policydb_context_isvalid(struct policydb *p, struct context *c)
814 struct role_datum *role;
815 struct user_datum *usrdatum;
817 if (!c->role || c->role > p->p_roles.nprim)
820 if (!c->user || c->user > p->p_users.nprim)
823 if (!c->type || c->type > p->p_types.nprim)
826 if (c->role != OBJECT_R_VAL) {
828 * Role must be authorized for the type.
830 role = p->role_val_to_struct[c->role - 1];
831 if (!ebitmap_get_bit(&role->types,
833 /* role may not be associated with type */
837 * User must be authorized for the role.
839 usrdatum = p->user_val_to_struct[c->user - 1];
843 if (!ebitmap_get_bit(&usrdatum->roles,
845 /* user may not be associated with role */
849 if (!mls_context_isvalid(p, c))
856 * Read a MLS range structure from a policydb binary
857 * representation file.
859 static int mls_read_range_helper(struct mls_range *r, void *fp)
865 rc = next_entry(buf, fp, sizeof(u32));
869 items = le32_to_cpu(buf[0]);
870 if (items > ARRAY_SIZE(buf)) {
871 printk(KERN_ERR "SELinux: mls: range overflow\n");
875 rc = next_entry(buf, fp, sizeof(u32) * items);
877 printk(KERN_ERR "SELinux: mls: truncated range\n");
880 r->level[0].sens = le32_to_cpu(buf[0]);
882 r->level[1].sens = le32_to_cpu(buf[1]);
884 r->level[1].sens = r->level[0].sens;
886 rc = ebitmap_read(&r->level[0].cat, fp);
888 printk(KERN_ERR "SELinux: mls: error reading low "
893 rc = ebitmap_read(&r->level[1].cat, fp);
895 printk(KERN_ERR "SELinux: mls: error reading high "
900 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
902 printk(KERN_ERR "SELinux: mls: out of memory\n");
911 ebitmap_destroy(&r->level[0].cat);
916 * Read and validate a security context structure
917 * from a policydb binary representation file.
919 static int context_read_and_validate(struct context *c,
926 rc = next_entry(buf, fp, sizeof buf);
928 printk(KERN_ERR "SELinux: context truncated\n");
931 c->user = le32_to_cpu(buf[0]);
932 c->role = le32_to_cpu(buf[1]);
933 c->type = le32_to_cpu(buf[2]);
934 if (p->policyvers >= POLICYDB_VERSION_MLS) {
935 if (mls_read_range_helper(&c->range, fp)) {
936 printk(KERN_ERR "SELinux: error reading MLS range of "
943 if (!policydb_context_isvalid(p, c)) {
944 printk(KERN_ERR "SELinux: invalid security context\n");
953 * The following *_read functions are used to
954 * read the symbol data from a policy database
955 * binary representation file.
958 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
961 struct perm_datum *perdatum;
966 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
972 rc = next_entry(buf, fp, sizeof buf);
976 len = le32_to_cpu(buf[0]);
977 perdatum->value = le32_to_cpu(buf[1]);
979 key = kmalloc(len + 1, GFP_KERNEL);
984 rc = next_entry(key, fp, len);
989 rc = hashtab_insert(h, key, perdatum);
995 perm_destroy(key, perdatum, NULL);
999 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1002 struct common_datum *comdatum;
1007 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1013 rc = next_entry(buf, fp, sizeof buf);
1017 len = le32_to_cpu(buf[0]);
1018 comdatum->value = le32_to_cpu(buf[1]);
1020 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1023 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1024 nel = le32_to_cpu(buf[3]);
1026 key = kmalloc(len + 1, GFP_KERNEL);
1031 rc = next_entry(key, fp, len);
1036 for (i = 0; i < nel; i++) {
1037 rc = perm_read(p, comdatum->permissions.table, fp);
1042 rc = hashtab_insert(h, key, comdatum);
1048 common_destroy(key, comdatum, NULL);
1052 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1053 int allowxtarget, void *fp)
1055 struct constraint_node *c, *lc;
1056 struct constraint_expr *e, *le;
1059 int rc, i, j, depth;
1062 for (i = 0; i < ncons; i++) {
1063 c = kzalloc(sizeof(*c), GFP_KERNEL);
1072 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1075 c->permissions = le32_to_cpu(buf[0]);
1076 nexpr = le32_to_cpu(buf[1]);
1079 for (j = 0; j < nexpr; j++) {
1080 e = kzalloc(sizeof(*e), GFP_KERNEL);
1089 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1092 e->expr_type = le32_to_cpu(buf[0]);
1093 e->attr = le32_to_cpu(buf[1]);
1094 e->op = le32_to_cpu(buf[2]);
1096 switch (e->expr_type) {
1108 if (depth == (CEXPR_MAXDEPTH - 1))
1113 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1115 if (depth == (CEXPR_MAXDEPTH - 1))
1118 if (ebitmap_read(&e->names, fp))
1134 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1137 struct class_datum *cladatum;
1139 u32 len, len2, ncons, nel;
1142 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1148 rc = next_entry(buf, fp, sizeof(u32)*6);
1152 len = le32_to_cpu(buf[0]);
1153 len2 = le32_to_cpu(buf[1]);
1154 cladatum->value = le32_to_cpu(buf[2]);
1156 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1159 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1160 nel = le32_to_cpu(buf[4]);
1162 ncons = le32_to_cpu(buf[5]);
1164 key = kmalloc(len + 1, GFP_KERNEL);
1169 rc = next_entry(key, fp, len);
1175 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1176 if (!cladatum->comkey) {
1180 rc = next_entry(cladatum->comkey, fp, len2);
1183 cladatum->comkey[len2] = '\0';
1185 cladatum->comdatum = hashtab_search(p->p_commons.table,
1187 if (!cladatum->comdatum) {
1188 printk(KERN_ERR "SELinux: unknown common %s\n",
1194 for (i = 0; i < nel; i++) {
1195 rc = perm_read(p, cladatum->permissions.table, fp);
1200 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1204 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1205 /* grab the validatetrans rules */
1206 rc = next_entry(buf, fp, sizeof(u32));
1209 ncons = le32_to_cpu(buf[0]);
1210 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1215 rc = hashtab_insert(h, key, cladatum);
1223 cls_destroy(key, cladatum, NULL);
1227 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1230 struct role_datum *role;
1231 int rc, to_read = 2;
1235 role = kzalloc(sizeof(*role), GFP_KERNEL);
1241 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1244 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1248 len = le32_to_cpu(buf[0]);
1249 role->value = le32_to_cpu(buf[1]);
1250 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1251 role->bounds = le32_to_cpu(buf[2]);
1253 key = kmalloc(len + 1, GFP_KERNEL);
1258 rc = next_entry(key, fp, len);
1263 rc = ebitmap_read(&role->dominates, fp);
1267 rc = ebitmap_read(&role->types, fp);
1271 if (strcmp(key, OBJECT_R) == 0) {
1272 if (role->value != OBJECT_R_VAL) {
1273 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1274 OBJECT_R, role->value);
1282 rc = hashtab_insert(h, key, role);
1288 role_destroy(key, role, NULL);
1292 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1295 struct type_datum *typdatum;
1296 int rc, to_read = 3;
1300 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1306 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1309 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1313 len = le32_to_cpu(buf[0]);
1314 typdatum->value = le32_to_cpu(buf[1]);
1315 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1316 u32 prop = le32_to_cpu(buf[2]);
1318 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1319 typdatum->primary = 1;
1320 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1321 typdatum->attribute = 1;
1323 typdatum->bounds = le32_to_cpu(buf[3]);
1325 typdatum->primary = le32_to_cpu(buf[2]);
1328 key = kmalloc(len + 1, GFP_KERNEL);
1333 rc = next_entry(key, fp, len);
1338 rc = hashtab_insert(h, key, typdatum);
1344 type_destroy(key, typdatum, NULL);
1350 * Read a MLS level structure from a policydb binary
1351 * representation file.
1353 static int mls_read_level(struct mls_level *lp, void *fp)
1358 memset(lp, 0, sizeof(*lp));
1360 rc = next_entry(buf, fp, sizeof buf);
1362 printk(KERN_ERR "SELinux: mls: truncated level\n");
1365 lp->sens = le32_to_cpu(buf[0]);
1367 if (ebitmap_read(&lp->cat, fp)) {
1368 printk(KERN_ERR "SELinux: mls: error reading level "
1379 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1382 struct user_datum *usrdatum;
1383 int rc, to_read = 2;
1387 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1393 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1396 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1400 len = le32_to_cpu(buf[0]);
1401 usrdatum->value = le32_to_cpu(buf[1]);
1402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1403 usrdatum->bounds = le32_to_cpu(buf[2]);
1405 key = kmalloc(len + 1, GFP_KERNEL);
1410 rc = next_entry(key, fp, len);
1415 rc = ebitmap_read(&usrdatum->roles, fp);
1419 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1420 rc = mls_read_range_helper(&usrdatum->range, fp);
1423 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1428 rc = hashtab_insert(h, key, usrdatum);
1434 user_destroy(key, usrdatum, NULL);
1438 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1441 struct level_datum *levdatum;
1446 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1452 rc = next_entry(buf, fp, sizeof buf);
1456 len = le32_to_cpu(buf[0]);
1457 levdatum->isalias = le32_to_cpu(buf[1]);
1459 key = kmalloc(len + 1, GFP_ATOMIC);
1464 rc = next_entry(key, fp, len);
1469 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1470 if (!levdatum->level) {
1474 if (mls_read_level(levdatum->level, fp)) {
1479 rc = hashtab_insert(h, key, levdatum);
1485 sens_destroy(key, levdatum, NULL);
1489 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1492 struct cat_datum *catdatum;
1497 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1503 rc = next_entry(buf, fp, sizeof buf);
1507 len = le32_to_cpu(buf[0]);
1508 catdatum->value = le32_to_cpu(buf[1]);
1509 catdatum->isalias = le32_to_cpu(buf[2]);
1511 key = kmalloc(len + 1, GFP_ATOMIC);
1516 rc = next_entry(key, fp, len);
1521 rc = hashtab_insert(h, key, catdatum);
1528 cat_destroy(key, catdatum, NULL);
1532 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1544 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1546 struct user_datum *upper, *user;
1547 struct policydb *p = datap;
1550 upper = user = datum;
1551 while (upper->bounds) {
1552 struct ebitmap_node *node;
1555 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1556 printk(KERN_ERR "SELinux: user %s: "
1557 "too deep or looped boundary",
1562 upper = p->user_val_to_struct[upper->bounds - 1];
1563 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1564 if (ebitmap_get_bit(&upper->roles, bit))
1568 "SELinux: boundary violated policy: "
1569 "user=%s role=%s bounds=%s\n",
1570 p->p_user_val_to_name[user->value - 1],
1571 p->p_role_val_to_name[bit],
1572 p->p_user_val_to_name[upper->value - 1]);
1581 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1583 struct role_datum *upper, *role;
1584 struct policydb *p = datap;
1587 upper = role = datum;
1588 while (upper->bounds) {
1589 struct ebitmap_node *node;
1592 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1593 printk(KERN_ERR "SELinux: role %s: "
1594 "too deep or looped bounds\n",
1599 upper = p->role_val_to_struct[upper->bounds - 1];
1600 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1601 if (ebitmap_get_bit(&upper->types, bit))
1605 "SELinux: boundary violated policy: "
1606 "role=%s type=%s bounds=%s\n",
1607 p->p_role_val_to_name[role->value - 1],
1608 p->p_type_val_to_name[bit],
1609 p->p_role_val_to_name[upper->value - 1]);
1618 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1620 struct type_datum *upper, *type;
1621 struct policydb *p = datap;
1624 upper = type = datum;
1625 while (upper->bounds) {
1626 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1627 printk(KERN_ERR "SELinux: type %s: "
1628 "too deep or looped boundary\n",
1633 upper = p->type_val_to_struct[upper->bounds - 1];
1634 if (upper->attribute) {
1635 printk(KERN_ERR "SELinux: type %s: "
1636 "bounded by attribute %s",
1638 p->p_type_val_to_name[upper->value - 1]);
1646 static int policydb_bounds_sanity_check(struct policydb *p)
1650 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1653 rc = hashtab_map(p->p_users.table,
1654 user_bounds_sanity_check, p);
1658 rc = hashtab_map(p->p_roles.table,
1659 role_bounds_sanity_check, p);
1663 rc = hashtab_map(p->p_types.table,
1664 type_bounds_sanity_check, p);
1671 extern int ss_initialized;
1673 u16 string_to_security_class(struct policydb *p, const char *name)
1675 struct class_datum *cladatum;
1677 cladatum = hashtab_search(p->p_classes.table, name);
1681 return cladatum->value;
1684 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1686 struct class_datum *cladatum;
1687 struct perm_datum *perdatum = NULL;
1688 struct common_datum *comdatum;
1690 if (!tclass || tclass > p->p_classes.nprim)
1693 cladatum = p->class_val_to_struct[tclass-1];
1694 comdatum = cladatum->comdatum;
1696 perdatum = hashtab_search(comdatum->permissions.table,
1699 perdatum = hashtab_search(cladatum->permissions.table,
1704 return 1U << (perdatum->value-1);
1708 * Read the configuration data from a policy database binary
1709 * representation file into a policy database structure.
1711 int policydb_read(struct policydb *p, void *fp)
1713 struct role_allow *ra, *lra;
1714 struct role_trans *tr, *ltr;
1715 struct ocontext *l, *c, *newc;
1716 struct genfs *genfs_p, *genfs, *newgenfs;
1720 u32 len, len2, config, nprim, nel, nel2;
1722 struct policydb_compat_info *info;
1723 struct range_trans *rt;
1724 struct mls_range *r;
1728 rc = policydb_init(p);
1732 /* Read the magic number and string length. */
1733 rc = next_entry(buf, fp, sizeof(u32) * 2);
1737 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1738 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
1739 "not match expected magic number 0x%x\n",
1740 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1744 len = le32_to_cpu(buf[1]);
1745 if (len != strlen(POLICYDB_STRING)) {
1746 printk(KERN_ERR "SELinux: policydb string length %d does not "
1747 "match expected length %Zu\n",
1748 len, strlen(POLICYDB_STRING));
1751 policydb_str = kmalloc(len + 1, GFP_KERNEL);
1752 if (!policydb_str) {
1753 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
1754 "string of length %d\n", len);
1758 rc = next_entry(policydb_str, fp, len);
1760 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
1761 kfree(policydb_str);
1764 policydb_str[len] = '\0';
1765 if (strcmp(policydb_str, POLICYDB_STRING)) {
1766 printk(KERN_ERR "SELinux: policydb string %s does not match "
1767 "my string %s\n", policydb_str, POLICYDB_STRING);
1768 kfree(policydb_str);
1771 /* Done with policydb_str. */
1772 kfree(policydb_str);
1773 policydb_str = NULL;
1775 /* Read the version, config, and table sizes. */
1776 rc = next_entry(buf, fp, sizeof(u32)*4);
1780 p->policyvers = le32_to_cpu(buf[0]);
1781 if (p->policyvers < POLICYDB_VERSION_MIN ||
1782 p->policyvers > POLICYDB_VERSION_MAX) {
1783 printk(KERN_ERR "SELinux: policydb version %d does not match "
1784 "my version range %d-%d\n",
1785 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1789 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1790 if (ss_initialized && !selinux_mls_enabled) {
1791 printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
1792 " and MLS policies\n");
1795 selinux_mls_enabled = 1;
1796 config |= POLICYDB_CONFIG_MLS;
1798 if (p->policyvers < POLICYDB_VERSION_MLS) {
1799 printk(KERN_ERR "SELinux: security policydb version %d "
1800 "(MLS) not backwards compatible\n",
1805 if (ss_initialized && selinux_mls_enabled) {
1806 printk(KERN_ERR "SELinux: Cannot switch between MLS and"
1807 " non-MLS policies\n");
1811 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
1812 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
1814 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1815 ebitmap_read(&p->policycaps, fp) != 0)
1818 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
1819 ebitmap_read(&p->permissive_map, fp) != 0)
1822 info = policydb_lookup_compat(p->policyvers);
1824 printk(KERN_ERR "SELinux: unable to find policy compat info "
1825 "for version %d\n", p->policyvers);
1829 if (le32_to_cpu(buf[2]) != info->sym_num ||
1830 le32_to_cpu(buf[3]) != info->ocon_num) {
1831 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
1832 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1833 le32_to_cpu(buf[3]),
1834 info->sym_num, info->ocon_num);
1838 for (i = 0; i < info->sym_num; i++) {
1839 rc = next_entry(buf, fp, sizeof(u32)*2);
1842 nprim = le32_to_cpu(buf[0]);
1843 nel = le32_to_cpu(buf[1]);
1844 for (j = 0; j < nel; j++) {
1845 rc = read_f[i](p, p->symtab[i].table, fp);
1850 p->symtab[i].nprim = nprim;
1853 rc = avtab_read(&p->te_avtab, fp, p);
1857 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1858 rc = cond_read_list(p, fp);
1863 rc = next_entry(buf, fp, sizeof(u32));
1866 nel = le32_to_cpu(buf[0]);
1868 for (i = 0; i < nel; i++) {
1869 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
1878 rc = next_entry(buf, fp, sizeof(u32)*3);
1881 tr->role = le32_to_cpu(buf[0]);
1882 tr->type = le32_to_cpu(buf[1]);
1883 tr->new_role = le32_to_cpu(buf[2]);
1884 if (!policydb_role_isvalid(p, tr->role) ||
1885 !policydb_type_isvalid(p, tr->type) ||
1886 !policydb_role_isvalid(p, tr->new_role)) {
1893 rc = next_entry(buf, fp, sizeof(u32));
1896 nel = le32_to_cpu(buf[0]);
1898 for (i = 0; i < nel; i++) {
1899 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1908 rc = next_entry(buf, fp, sizeof(u32)*2);
1911 ra->role = le32_to_cpu(buf[0]);
1912 ra->new_role = le32_to_cpu(buf[1]);
1913 if (!policydb_role_isvalid(p, ra->role) ||
1914 !policydb_role_isvalid(p, ra->new_role)) {
1921 rc = policydb_index_classes(p);
1925 rc = policydb_index_others(p);
1929 p->process_class = string_to_security_class(p, "process");
1930 if (!p->process_class)
1932 p->process_trans_perms = string_to_av_perm(p, p->process_class,
1934 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
1936 if (!p->process_trans_perms)
1939 for (i = 0; i < info->ocon_num; i++) {
1940 rc = next_entry(buf, fp, sizeof(u32));
1943 nel = le32_to_cpu(buf[0]);
1945 for (j = 0; j < nel; j++) {
1946 c = kzalloc(sizeof(*c), GFP_KERNEL);
1954 p->ocontexts[i] = c;
1959 rc = next_entry(buf, fp, sizeof(u32));
1962 c->sid[0] = le32_to_cpu(buf[0]);
1963 rc = context_read_and_validate(&c->context[0], p, fp);
1969 rc = next_entry(buf, fp, sizeof(u32));
1972 len = le32_to_cpu(buf[0]);
1973 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1978 rc = next_entry(c->u.name, fp, len);
1982 rc = context_read_and_validate(&c->context[0], p, fp);
1985 rc = context_read_and_validate(&c->context[1], p, fp);
1990 rc = next_entry(buf, fp, sizeof(u32)*3);
1993 c->u.port.protocol = le32_to_cpu(buf[0]);
1994 c->u.port.low_port = le32_to_cpu(buf[1]);
1995 c->u.port.high_port = le32_to_cpu(buf[2]);
1996 rc = context_read_and_validate(&c->context[0], p, fp);
2001 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2004 c->u.node.addr = nodebuf[0]; /* network order */
2005 c->u.node.mask = nodebuf[1]; /* network order */
2006 rc = context_read_and_validate(&c->context[0], p, fp);
2011 rc = next_entry(buf, fp, sizeof(u32)*2);
2014 c->v.behavior = le32_to_cpu(buf[0]);
2015 if (c->v.behavior > SECURITY_FS_USE_NONE)
2017 len = le32_to_cpu(buf[1]);
2018 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2023 rc = next_entry(c->u.name, fp, len);
2027 rc = context_read_and_validate(&c->context[0], p, fp);
2034 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2037 for (k = 0; k < 4; k++)
2038 c->u.node6.addr[k] = nodebuf[k];
2039 for (k = 0; k < 4; k++)
2040 c->u.node6.mask[k] = nodebuf[k+4];
2041 if (context_read_and_validate(&c->context[0], p, fp))
2049 rc = next_entry(buf, fp, sizeof(u32));
2052 nel = le32_to_cpu(buf[0]);
2055 for (i = 0; i < nel; i++) {
2056 rc = next_entry(buf, fp, sizeof(u32));
2059 len = le32_to_cpu(buf[0]);
2060 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2066 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
2067 if (!newgenfs->fstype) {
2072 rc = next_entry(newgenfs->fstype, fp, len);
2074 kfree(newgenfs->fstype);
2078 newgenfs->fstype[len] = 0;
2079 for (genfs_p = NULL, genfs = p->genfs; genfs;
2080 genfs_p = genfs, genfs = genfs->next) {
2081 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2082 printk(KERN_ERR "SELinux: dup genfs "
2083 "fstype %s\n", newgenfs->fstype);
2084 kfree(newgenfs->fstype);
2088 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2091 newgenfs->next = genfs;
2093 genfs_p->next = newgenfs;
2095 p->genfs = newgenfs;
2096 rc = next_entry(buf, fp, sizeof(u32));
2099 nel2 = le32_to_cpu(buf[0]);
2100 for (j = 0; j < nel2; j++) {
2101 rc = next_entry(buf, fp, sizeof(u32));
2104 len = le32_to_cpu(buf[0]);
2106 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2112 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2113 if (!newc->u.name) {
2117 rc = next_entry(newc->u.name, fp, len);
2120 newc->u.name[len] = 0;
2121 rc = next_entry(buf, fp, sizeof(u32));
2124 newc->v.sclass = le32_to_cpu(buf[0]);
2125 if (context_read_and_validate(&newc->context[0], p, fp))
2127 for (l = NULL, c = newgenfs->head; c;
2128 l = c, c = c->next) {
2129 if (!strcmp(newc->u.name, c->u.name) &&
2130 (!c->v.sclass || !newc->v.sclass ||
2131 newc->v.sclass == c->v.sclass)) {
2132 printk(KERN_ERR "SELinux: dup genfs "
2134 newgenfs->fstype, c->u.name);
2137 len = strlen(newc->u.name);
2138 len2 = strlen(c->u.name);
2147 newgenfs->head = newc;
2151 if (p->policyvers >= POLICYDB_VERSION_MLS) {
2152 int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
2153 rc = next_entry(buf, fp, sizeof(u32));
2156 nel = le32_to_cpu(buf[0]);
2157 for (i = 0; i < nel; i++) {
2158 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
2163 rc = next_entry(buf, fp, (sizeof(u32) * 2));
2168 rt->source_type = le32_to_cpu(buf[0]);
2169 rt->target_type = le32_to_cpu(buf[1]);
2171 rc = next_entry(buf, fp, sizeof(u32));
2176 rt->target_class = le32_to_cpu(buf[0]);
2178 rt->target_class = p->process_class;
2179 if (!policydb_type_isvalid(p, rt->source_type) ||
2180 !policydb_type_isvalid(p, rt->target_type) ||
2181 !policydb_class_isvalid(p, rt->target_class)) {
2186 r = kzalloc(sizeof(*r), GFP_KERNEL);
2192 rc = mls_read_range_helper(r, fp);
2198 if (!mls_range_isvalid(p, r)) {
2199 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
2204 rc = hashtab_insert(p->range_tr, rt, r);
2211 rangetr_hash_eval(p->range_tr);
2214 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
2215 if (!p->type_attr_map)
2218 for (i = 0; i < p->p_types.nprim; i++) {
2219 ebitmap_init(&p->type_attr_map[i]);
2220 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2221 if (ebitmap_read(&p->type_attr_map[i], fp))
2224 /* add the type itself as the degenerate case */
2225 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2229 rc = policydb_bounds_sanity_check(p);
2237 ocontext_destroy(newc, OCON_FSUSE);
2241 policydb_destroy(p);