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@paul-moore.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"
45 static const char *symtab_name[SYM_NUM] = {
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,
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
129 .ocon_num = OCON_NUM,
132 .version = POLICYDB_VERSION_ROLETRANS,
134 .ocon_num = OCON_NUM,
137 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
139 .ocon_num = OCON_NUM,
142 .version = POLICYDB_VERSION_DEFAULT_TYPE,
144 .ocon_num = OCON_NUM,
147 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
149 .ocon_num = OCON_NUM,
152 .version = POLICYDB_VERSION_XPERMS_IOCTL,
154 .ocon_num = OCON_NUM,
158 static struct policydb_compat_info *policydb_lookup_compat(int version)
161 struct policydb_compat_info *info = NULL;
163 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
164 if (policydb_compat[i].version == version) {
165 info = &policydb_compat[i];
173 * Initialize the role table.
175 static int roles_init(struct policydb *p)
179 struct role_datum *role;
182 role = kzalloc(sizeof(*role), GFP_KERNEL);
187 role->value = ++p->p_roles.nprim;
188 if (role->value != OBJECT_R_VAL)
192 key = kstrdup(OBJECT_R, GFP_KERNEL);
196 rc = hashtab_insert(p->p_roles.table, key, role);
207 static u32 filenametr_hash(struct hashtab *h, const void *k)
209 const struct filename_trans *ft = k;
211 unsigned int byte_num;
214 hash = ft->stype ^ ft->ttype ^ ft->tclass;
217 while ((focus = ft->name[byte_num++]))
218 hash = partial_name_hash(focus, hash);
219 return hash & (h->size - 1);
222 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
224 const struct filename_trans *ft1 = k1;
225 const struct filename_trans *ft2 = k2;
228 v = ft1->stype - ft2->stype;
232 v = ft1->ttype - ft2->ttype;
236 v = ft1->tclass - ft2->tclass;
240 return strcmp(ft1->name, ft2->name);
244 static u32 rangetr_hash(struct hashtab *h, const void *k)
246 const struct range_trans *key = k;
247 return (key->source_type + (key->target_type << 3) +
248 (key->target_class << 5)) & (h->size - 1);
251 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
253 const struct range_trans *key1 = k1, *key2 = k2;
256 v = key1->source_type - key2->source_type;
260 v = key1->target_type - key2->target_type;
264 v = key1->target_class - key2->target_class;
269 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
272 * Initialize a policy database structure.
274 static int policydb_init(struct policydb *p)
278 memset(p, 0, sizeof(*p));
280 for (i = 0; i < SYM_NUM; i++) {
281 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
286 rc = avtab_init(&p->te_avtab);
294 rc = cond_policydb_init(p);
298 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
299 if (!p->filename_trans) {
304 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
310 ebitmap_init(&p->filename_trans_ttypes);
311 ebitmap_init(&p->policycaps);
312 ebitmap_init(&p->permissive_map);
316 hashtab_destroy(p->filename_trans);
317 hashtab_destroy(p->range_tr);
318 for (i = 0; i < SYM_NUM; i++) {
319 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
320 hashtab_destroy(p->symtab[i].table);
326 * The following *_index functions are used to
327 * define the val_to_name and val_to_struct arrays
328 * in a policy database structure. The val_to_name
329 * arrays are used when converting security context
330 * structures into string representations. The
331 * val_to_struct arrays are used when the attributes
332 * of a class, role, or user are needed.
335 static int common_index(void *key, void *datum, void *datap)
338 struct common_datum *comdatum;
339 struct flex_array *fa;
343 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
346 fa = p->sym_val_to_name[SYM_COMMONS];
347 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
348 GFP_KERNEL | __GFP_ZERO))
353 static int class_index(void *key, void *datum, void *datap)
356 struct class_datum *cladatum;
357 struct flex_array *fa;
361 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
363 fa = p->sym_val_to_name[SYM_CLASSES];
364 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
365 GFP_KERNEL | __GFP_ZERO))
367 p->class_val_to_struct[cladatum->value - 1] = cladatum;
371 static int role_index(void *key, void *datum, void *datap)
374 struct role_datum *role;
375 struct flex_array *fa;
380 || role->value > p->p_roles.nprim
381 || role->bounds > p->p_roles.nprim)
384 fa = p->sym_val_to_name[SYM_ROLES];
385 if (flex_array_put_ptr(fa, role->value - 1, key,
386 GFP_KERNEL | __GFP_ZERO))
388 p->role_val_to_struct[role->value - 1] = role;
392 static int type_index(void *key, void *datum, void *datap)
395 struct type_datum *typdatum;
396 struct flex_array *fa;
401 if (typdatum->primary) {
403 || typdatum->value > p->p_types.nprim
404 || typdatum->bounds > p->p_types.nprim)
406 fa = p->sym_val_to_name[SYM_TYPES];
407 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
408 GFP_KERNEL | __GFP_ZERO))
411 fa = p->type_val_to_struct_array;
412 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
413 GFP_KERNEL | __GFP_ZERO))
420 static int user_index(void *key, void *datum, void *datap)
423 struct user_datum *usrdatum;
424 struct flex_array *fa;
429 || usrdatum->value > p->p_users.nprim
430 || usrdatum->bounds > p->p_users.nprim)
433 fa = p->sym_val_to_name[SYM_USERS];
434 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
435 GFP_KERNEL | __GFP_ZERO))
437 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
441 static int sens_index(void *key, void *datum, void *datap)
444 struct level_datum *levdatum;
445 struct flex_array *fa;
450 if (!levdatum->isalias) {
451 if (!levdatum->level->sens ||
452 levdatum->level->sens > p->p_levels.nprim)
454 fa = p->sym_val_to_name[SYM_LEVELS];
455 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
456 GFP_KERNEL | __GFP_ZERO))
463 static int cat_index(void *key, void *datum, void *datap)
466 struct cat_datum *catdatum;
467 struct flex_array *fa;
472 if (!catdatum->isalias) {
473 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
475 fa = p->sym_val_to_name[SYM_CATS];
476 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
477 GFP_KERNEL | __GFP_ZERO))
484 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
497 static void hash_eval(struct hashtab *h, const char *hash_name)
499 struct hashtab_info info;
501 hashtab_stat(h, &info);
502 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
503 "longest chain length %d\n", hash_name, h->nel,
504 info.slots_used, h->size, info.max_chain_len);
507 static void symtab_hash_eval(struct symtab *s)
511 for (i = 0; i < SYM_NUM; i++)
512 hash_eval(s[i].table, symtab_name[i]);
516 static inline void hash_eval(struct hashtab *h, char *hash_name)
522 * Define the other val_to_name and val_to_struct arrays
523 * in a policy database structure.
525 * Caller must clean up on failure.
527 static int policydb_index(struct policydb *p)
531 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
532 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
534 printk(", %d sens, %d cats", p->p_levels.nprim,
538 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
539 p->p_classes.nprim, p->te_avtab.nel);
542 avtab_hash_eval(&p->te_avtab, "rules");
543 symtab_hash_eval(p->symtab);
547 p->class_val_to_struct =
548 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
550 if (!p->class_val_to_struct)
554 p->role_val_to_struct =
555 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
557 if (!p->role_val_to_struct)
561 p->user_val_to_struct =
562 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
564 if (!p->user_val_to_struct)
567 /* Yes, I want the sizeof the pointer, not the structure */
569 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
571 GFP_KERNEL | __GFP_ZERO);
572 if (!p->type_val_to_struct_array)
575 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
576 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
580 rc = cond_init_bool_indexes(p);
584 for (i = 0; i < SYM_NUM; i++) {
586 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
588 GFP_KERNEL | __GFP_ZERO);
589 if (!p->sym_val_to_name[i])
592 rc = flex_array_prealloc(p->sym_val_to_name[i],
593 0, p->symtab[i].nprim,
594 GFP_KERNEL | __GFP_ZERO);
598 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
608 * The following *_destroy functions are used to
609 * free any memory allocated for each kind of
610 * symbol data in the policy database.
613 static int perm_destroy(void *key, void *datum, void *p)
620 static int common_destroy(void *key, void *datum, void *p)
622 struct common_datum *comdatum;
627 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
628 hashtab_destroy(comdatum->permissions.table);
634 static void constraint_expr_destroy(struct constraint_expr *expr)
637 ebitmap_destroy(&expr->names);
638 if (expr->type_names) {
639 ebitmap_destroy(&expr->type_names->types);
640 ebitmap_destroy(&expr->type_names->negset);
641 kfree(expr->type_names);
647 static int cls_destroy(void *key, void *datum, void *p)
649 struct class_datum *cladatum;
650 struct constraint_node *constraint, *ctemp;
651 struct constraint_expr *e, *etmp;
656 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
657 hashtab_destroy(cladatum->permissions.table);
658 constraint = cladatum->constraints;
660 e = constraint->expr;
664 constraint_expr_destroy(etmp);
667 constraint = constraint->next;
671 constraint = cladatum->validatetrans;
673 e = constraint->expr;
677 constraint_expr_destroy(etmp);
680 constraint = constraint->next;
683 kfree(cladatum->comkey);
689 static int role_destroy(void *key, void *datum, void *p)
691 struct role_datum *role;
696 ebitmap_destroy(&role->dominates);
697 ebitmap_destroy(&role->types);
703 static int type_destroy(void *key, void *datum, void *p)
710 static int user_destroy(void *key, void *datum, void *p)
712 struct user_datum *usrdatum;
717 ebitmap_destroy(&usrdatum->roles);
718 ebitmap_destroy(&usrdatum->range.level[0].cat);
719 ebitmap_destroy(&usrdatum->range.level[1].cat);
720 ebitmap_destroy(&usrdatum->dfltlevel.cat);
726 static int sens_destroy(void *key, void *datum, void *p)
728 struct level_datum *levdatum;
734 ebitmap_destroy(&levdatum->level->cat);
735 kfree(levdatum->level);
741 static int cat_destroy(void *key, void *datum, void *p)
748 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
760 static int filenametr_destroy(void *key, void *datum, void *p)
762 struct filename_trans *ft = key;
770 static int range_tr_destroy(void *key, void *datum, void *p)
772 struct mls_range *rt = datum;
774 ebitmap_destroy(&rt->level[0].cat);
775 ebitmap_destroy(&rt->level[1].cat);
781 static void ocontext_destroy(struct ocontext *c, int i)
786 context_destroy(&c->context[0]);
787 context_destroy(&c->context[1]);
788 if (i == OCON_ISID || i == OCON_FS ||
789 i == OCON_NETIF || i == OCON_FSUSE)
795 * Free any memory allocated by a policy database structure.
797 void policydb_destroy(struct policydb *p)
799 struct ocontext *c, *ctmp;
800 struct genfs *g, *gtmp;
802 struct role_allow *ra, *lra = NULL;
803 struct role_trans *tr, *ltr = NULL;
805 for (i = 0; i < SYM_NUM; i++) {
807 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
808 hashtab_destroy(p->symtab[i].table);
811 for (i = 0; i < SYM_NUM; i++) {
812 if (p->sym_val_to_name[i])
813 flex_array_free(p->sym_val_to_name[i]);
816 kfree(p->class_val_to_struct);
817 kfree(p->role_val_to_struct);
818 kfree(p->user_val_to_struct);
819 if (p->type_val_to_struct_array)
820 flex_array_free(p->type_val_to_struct_array);
822 avtab_destroy(&p->te_avtab);
824 for (i = 0; i < OCON_NUM; i++) {
830 ocontext_destroy(ctmp, i);
832 p->ocontexts[i] = NULL;
843 ocontext_destroy(ctmp, OCON_FSUSE);
851 cond_policydb_destroy(p);
853 for (tr = p->role_tr; tr; tr = tr->next) {
860 for (ra = p->role_allow; ra; ra = ra->next) {
867 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
868 hashtab_destroy(p->filename_trans);
870 hashtab_map(p->range_tr, range_tr_destroy, NULL);
871 hashtab_destroy(p->range_tr);
873 if (p->type_attr_map_array) {
874 for (i = 0; i < p->p_types.nprim; i++) {
877 e = flex_array_get(p->type_attr_map_array, i);
882 flex_array_free(p->type_attr_map_array);
885 ebitmap_destroy(&p->filename_trans_ttypes);
886 ebitmap_destroy(&p->policycaps);
887 ebitmap_destroy(&p->permissive_map);
893 * Load the initial SIDs specified in a policy database
894 * structure into a SID table.
896 int policydb_load_isids(struct policydb *p, struct sidtab *s)
898 struct ocontext *head, *c;
903 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
907 head = p->ocontexts[OCON_ISID];
908 for (c = head; c; c = c->next) {
910 if (!c->context[0].user) {
911 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
916 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
918 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
928 int policydb_class_isvalid(struct policydb *p, unsigned int class)
930 if (!class || class > p->p_classes.nprim)
935 int policydb_role_isvalid(struct policydb *p, unsigned int role)
937 if (!role || role > p->p_roles.nprim)
942 int policydb_type_isvalid(struct policydb *p, unsigned int type)
944 if (!type || type > p->p_types.nprim)
950 * Return 1 if the fields in the security context
951 * structure `c' are valid. Return 0 otherwise.
953 int policydb_context_isvalid(struct policydb *p, struct context *c)
955 struct role_datum *role;
956 struct user_datum *usrdatum;
958 if (!c->role || c->role > p->p_roles.nprim)
961 if (!c->user || c->user > p->p_users.nprim)
964 if (!c->type || c->type > p->p_types.nprim)
967 if (c->role != OBJECT_R_VAL) {
969 * Role must be authorized for the type.
971 role = p->role_val_to_struct[c->role - 1];
972 if (!ebitmap_get_bit(&role->types, c->type - 1))
973 /* role may not be associated with type */
977 * User must be authorized for the role.
979 usrdatum = p->user_val_to_struct[c->user - 1];
983 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
984 /* user may not be associated with role */
988 if (!mls_context_isvalid(p, c))
995 * Read a MLS range structure from a policydb binary
996 * representation file.
998 static int mls_read_range_helper(struct mls_range *r, void *fp)
1004 rc = next_entry(buf, fp, sizeof(u32));
1009 items = le32_to_cpu(buf[0]);
1010 if (items > ARRAY_SIZE(buf)) {
1011 printk(KERN_ERR "SELinux: mls: range overflow\n");
1015 rc = next_entry(buf, fp, sizeof(u32) * items);
1017 printk(KERN_ERR "SELinux: mls: truncated range\n");
1021 r->level[0].sens = le32_to_cpu(buf[0]);
1023 r->level[1].sens = le32_to_cpu(buf[1]);
1025 r->level[1].sens = r->level[0].sens;
1027 rc = ebitmap_read(&r->level[0].cat, fp);
1029 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
1033 rc = ebitmap_read(&r->level[1].cat, fp);
1035 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1039 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1041 printk(KERN_ERR "SELinux: mls: out of memory\n");
1048 ebitmap_destroy(&r->level[0].cat);
1054 * Read and validate a security context structure
1055 * from a policydb binary representation file.
1057 static int context_read_and_validate(struct context *c,
1064 rc = next_entry(buf, fp, sizeof buf);
1066 printk(KERN_ERR "SELinux: context truncated\n");
1069 c->user = le32_to_cpu(buf[0]);
1070 c->role = le32_to_cpu(buf[1]);
1071 c->type = le32_to_cpu(buf[2]);
1072 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1073 rc = mls_read_range_helper(&c->range, fp);
1075 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1081 if (!policydb_context_isvalid(p, c)) {
1082 printk(KERN_ERR "SELinux: invalid security context\n");
1092 * The following *_read functions are used to
1093 * read the symbol data from a policy database
1094 * binary representation file.
1097 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1102 str = kmalloc(len + 1, flags);
1106 /* it's expected the caller should free the str */
1109 rc = next_entry(str, fp, len);
1117 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1120 struct perm_datum *perdatum;
1126 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1130 rc = next_entry(buf, fp, sizeof buf);
1134 len = le32_to_cpu(buf[0]);
1135 perdatum->value = le32_to_cpu(buf[1]);
1137 rc = str_read(&key, GFP_KERNEL, fp, len);
1141 rc = hashtab_insert(h, key, perdatum);
1147 perm_destroy(key, perdatum, NULL);
1151 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1154 struct common_datum *comdatum;
1160 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1164 rc = next_entry(buf, fp, sizeof buf);
1168 len = le32_to_cpu(buf[0]);
1169 comdatum->value = le32_to_cpu(buf[1]);
1171 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1174 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1175 nel = le32_to_cpu(buf[3]);
1177 rc = str_read(&key, GFP_KERNEL, fp, len);
1181 for (i = 0; i < nel; i++) {
1182 rc = perm_read(p, comdatum->permissions.table, fp);
1187 rc = hashtab_insert(h, key, comdatum);
1192 common_destroy(key, comdatum, NULL);
1196 static void type_set_init(struct type_set *t)
1198 ebitmap_init(&t->types);
1199 ebitmap_init(&t->negset);
1202 static int type_set_read(struct type_set *t, void *fp)
1207 if (ebitmap_read(&t->types, fp))
1209 if (ebitmap_read(&t->negset, fp))
1212 rc = next_entry(buf, fp, sizeof(u32));
1215 t->flags = le32_to_cpu(buf[0]);
1221 static int read_cons_helper(struct policydb *p,
1222 struct constraint_node **nodep,
1223 int ncons, int allowxtarget, void *fp)
1225 struct constraint_node *c, *lc;
1226 struct constraint_expr *e, *le;
1229 int rc, i, j, depth;
1232 for (i = 0; i < ncons; i++) {
1233 c = kzalloc(sizeof(*c), GFP_KERNEL);
1242 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1245 c->permissions = le32_to_cpu(buf[0]);
1246 nexpr = le32_to_cpu(buf[1]);
1249 for (j = 0; j < nexpr; j++) {
1250 e = kzalloc(sizeof(*e), GFP_KERNEL);
1259 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1262 e->expr_type = le32_to_cpu(buf[0]);
1263 e->attr = le32_to_cpu(buf[1]);
1264 e->op = le32_to_cpu(buf[2]);
1266 switch (e->expr_type) {
1278 if (depth == (CEXPR_MAXDEPTH - 1))
1283 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1285 if (depth == (CEXPR_MAXDEPTH - 1))
1288 rc = ebitmap_read(&e->names, fp);
1291 if (p->policyvers >=
1292 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1293 e->type_names = kzalloc(sizeof
1298 type_set_init(e->type_names);
1299 rc = type_set_read(e->type_names, fp);
1317 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1320 struct class_datum *cladatum;
1322 u32 len, len2, ncons, nel;
1326 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1330 rc = next_entry(buf, fp, sizeof(u32)*6);
1334 len = le32_to_cpu(buf[0]);
1335 len2 = le32_to_cpu(buf[1]);
1336 cladatum->value = le32_to_cpu(buf[2]);
1338 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1341 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1342 nel = le32_to_cpu(buf[4]);
1344 ncons = le32_to_cpu(buf[5]);
1346 rc = str_read(&key, GFP_KERNEL, fp, len);
1351 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1356 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1357 if (!cladatum->comdatum) {
1358 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1362 for (i = 0; i < nel; i++) {
1363 rc = perm_read(p, cladatum->permissions.table, fp);
1368 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1372 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1373 /* grab the validatetrans rules */
1374 rc = next_entry(buf, fp, sizeof(u32));
1377 ncons = le32_to_cpu(buf[0]);
1378 rc = read_cons_helper(p, &cladatum->validatetrans,
1384 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1385 rc = next_entry(buf, fp, sizeof(u32) * 3);
1389 cladatum->default_user = le32_to_cpu(buf[0]);
1390 cladatum->default_role = le32_to_cpu(buf[1]);
1391 cladatum->default_range = le32_to_cpu(buf[2]);
1394 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1395 rc = next_entry(buf, fp, sizeof(u32) * 1);
1398 cladatum->default_type = le32_to_cpu(buf[0]);
1401 rc = hashtab_insert(h, key, cladatum);
1407 cls_destroy(key, cladatum, NULL);
1411 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1414 struct role_datum *role;
1415 int rc, to_read = 2;
1420 role = kzalloc(sizeof(*role), GFP_KERNEL);
1424 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1427 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1431 len = le32_to_cpu(buf[0]);
1432 role->value = le32_to_cpu(buf[1]);
1433 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1434 role->bounds = le32_to_cpu(buf[2]);
1436 rc = str_read(&key, GFP_KERNEL, fp, len);
1440 rc = ebitmap_read(&role->dominates, fp);
1444 rc = ebitmap_read(&role->types, fp);
1448 if (strcmp(key, OBJECT_R) == 0) {
1450 if (role->value != OBJECT_R_VAL) {
1451 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1452 OBJECT_R, role->value);
1459 rc = hashtab_insert(h, key, role);
1464 role_destroy(key, role, NULL);
1468 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1471 struct type_datum *typdatum;
1472 int rc, to_read = 3;
1477 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1481 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1484 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1488 len = le32_to_cpu(buf[0]);
1489 typdatum->value = le32_to_cpu(buf[1]);
1490 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1491 u32 prop = le32_to_cpu(buf[2]);
1493 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1494 typdatum->primary = 1;
1495 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1496 typdatum->attribute = 1;
1498 typdatum->bounds = le32_to_cpu(buf[3]);
1500 typdatum->primary = le32_to_cpu(buf[2]);
1503 rc = str_read(&key, GFP_KERNEL, fp, len);
1507 rc = hashtab_insert(h, key, typdatum);
1512 type_destroy(key, typdatum, NULL);
1518 * Read a MLS level structure from a policydb binary
1519 * representation file.
1521 static int mls_read_level(struct mls_level *lp, void *fp)
1526 memset(lp, 0, sizeof(*lp));
1528 rc = next_entry(buf, fp, sizeof buf);
1530 printk(KERN_ERR "SELinux: mls: truncated level\n");
1533 lp->sens = le32_to_cpu(buf[0]);
1535 rc = ebitmap_read(&lp->cat, fp);
1537 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1543 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1546 struct user_datum *usrdatum;
1547 int rc, to_read = 2;
1552 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1556 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1559 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1563 len = le32_to_cpu(buf[0]);
1564 usrdatum->value = le32_to_cpu(buf[1]);
1565 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1566 usrdatum->bounds = le32_to_cpu(buf[2]);
1568 rc = str_read(&key, GFP_KERNEL, fp, len);
1572 rc = ebitmap_read(&usrdatum->roles, fp);
1576 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1577 rc = mls_read_range_helper(&usrdatum->range, fp);
1580 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1585 rc = hashtab_insert(h, key, usrdatum);
1590 user_destroy(key, usrdatum, NULL);
1594 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1597 struct level_datum *levdatum;
1603 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1607 rc = next_entry(buf, fp, sizeof buf);
1611 len = le32_to_cpu(buf[0]);
1612 levdatum->isalias = le32_to_cpu(buf[1]);
1614 rc = str_read(&key, GFP_ATOMIC, fp, len);
1619 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1620 if (!levdatum->level)
1623 rc = mls_read_level(levdatum->level, fp);
1627 rc = hashtab_insert(h, key, levdatum);
1632 sens_destroy(key, levdatum, NULL);
1636 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1639 struct cat_datum *catdatum;
1645 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1649 rc = next_entry(buf, fp, sizeof buf);
1653 len = le32_to_cpu(buf[0]);
1654 catdatum->value = le32_to_cpu(buf[1]);
1655 catdatum->isalias = le32_to_cpu(buf[2]);
1657 rc = str_read(&key, GFP_ATOMIC, fp, len);
1661 rc = hashtab_insert(h, key, catdatum);
1666 cat_destroy(key, catdatum, NULL);
1670 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1682 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1684 struct user_datum *upper, *user;
1685 struct policydb *p = datap;
1688 upper = user = datum;
1689 while (upper->bounds) {
1690 struct ebitmap_node *node;
1693 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1694 printk(KERN_ERR "SELinux: user %s: "
1695 "too deep or looped boundary",
1700 upper = p->user_val_to_struct[upper->bounds - 1];
1701 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1702 if (ebitmap_get_bit(&upper->roles, bit))
1706 "SELinux: boundary violated policy: "
1707 "user=%s role=%s bounds=%s\n",
1708 sym_name(p, SYM_USERS, user->value - 1),
1709 sym_name(p, SYM_ROLES, bit),
1710 sym_name(p, SYM_USERS, upper->value - 1));
1719 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1721 struct role_datum *upper, *role;
1722 struct policydb *p = datap;
1725 upper = role = datum;
1726 while (upper->bounds) {
1727 struct ebitmap_node *node;
1730 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1731 printk(KERN_ERR "SELinux: role %s: "
1732 "too deep or looped bounds\n",
1737 upper = p->role_val_to_struct[upper->bounds - 1];
1738 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1739 if (ebitmap_get_bit(&upper->types, bit))
1743 "SELinux: boundary violated policy: "
1744 "role=%s type=%s bounds=%s\n",
1745 sym_name(p, SYM_ROLES, role->value - 1),
1746 sym_name(p, SYM_TYPES, bit),
1747 sym_name(p, SYM_ROLES, upper->value - 1));
1756 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1758 struct type_datum *upper;
1759 struct policydb *p = datap;
1763 while (upper->bounds) {
1764 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1765 printk(KERN_ERR "SELinux: type %s: "
1766 "too deep or looped boundary\n",
1771 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1775 if (upper->attribute) {
1776 printk(KERN_ERR "SELinux: type %s: "
1777 "bounded by attribute %s",
1779 sym_name(p, SYM_TYPES, upper->value - 1));
1787 static int policydb_bounds_sanity_check(struct policydb *p)
1791 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1794 rc = hashtab_map(p->p_users.table,
1795 user_bounds_sanity_check, p);
1799 rc = hashtab_map(p->p_roles.table,
1800 role_bounds_sanity_check, p);
1804 rc = hashtab_map(p->p_types.table,
1805 type_bounds_sanity_check, p);
1812 u16 string_to_security_class(struct policydb *p, const char *name)
1814 struct class_datum *cladatum;
1816 cladatum = hashtab_search(p->p_classes.table, name);
1820 return cladatum->value;
1823 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1825 struct class_datum *cladatum;
1826 struct perm_datum *perdatum = NULL;
1827 struct common_datum *comdatum;
1829 if (!tclass || tclass > p->p_classes.nprim)
1832 cladatum = p->class_val_to_struct[tclass-1];
1833 comdatum = cladatum->comdatum;
1835 perdatum = hashtab_search(comdatum->permissions.table,
1838 perdatum = hashtab_search(cladatum->permissions.table,
1843 return 1U << (perdatum->value-1);
1846 static int range_read(struct policydb *p, void *fp)
1848 struct range_trans *rt = NULL;
1849 struct mls_range *r = NULL;
1854 if (p->policyvers < POLICYDB_VERSION_MLS)
1857 rc = next_entry(buf, fp, sizeof(u32));
1861 nel = le32_to_cpu(buf[0]);
1862 for (i = 0; i < nel; i++) {
1864 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1868 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1872 rt->source_type = le32_to_cpu(buf[0]);
1873 rt->target_type = le32_to_cpu(buf[1]);
1874 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1875 rc = next_entry(buf, fp, sizeof(u32));
1878 rt->target_class = le32_to_cpu(buf[0]);
1880 rt->target_class = p->process_class;
1883 if (!policydb_type_isvalid(p, rt->source_type) ||
1884 !policydb_type_isvalid(p, rt->target_type) ||
1885 !policydb_class_isvalid(p, rt->target_class))
1889 r = kzalloc(sizeof(*r), GFP_KERNEL);
1893 rc = mls_read_range_helper(r, fp);
1898 if (!mls_range_isvalid(p, r)) {
1899 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1903 rc = hashtab_insert(p->range_tr, rt, r);
1910 hash_eval(p->range_tr, "rangetr");
1918 static int filename_trans_read(struct policydb *p, void *fp)
1920 struct filename_trans *ft;
1921 struct filename_trans_datum *otype;
1927 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1930 rc = next_entry(buf, fp, sizeof(u32));
1933 nel = le32_to_cpu(buf[0]);
1935 for (i = 0; i < nel; i++) {
1941 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1946 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1950 /* length of the path component string */
1951 rc = next_entry(buf, fp, sizeof(u32));
1954 len = le32_to_cpu(buf[0]);
1956 /* path component string */
1957 rc = str_read(&name, GFP_KERNEL, fp, len);
1963 rc = next_entry(buf, fp, sizeof(u32) * 4);
1967 ft->stype = le32_to_cpu(buf[0]);
1968 ft->ttype = le32_to_cpu(buf[1]);
1969 ft->tclass = le32_to_cpu(buf[2]);
1971 otype->otype = le32_to_cpu(buf[3]);
1973 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1977 rc = hashtab_insert(p->filename_trans, ft, otype);
1980 * Do not return -EEXIST to the caller, or the system
1985 /* But free memory to avoid memory leak. */
1991 hash_eval(p->filename_trans, "filenametr");
2001 static int genfs_read(struct policydb *p, void *fp)
2004 u32 nel, nel2, len, len2;
2006 struct ocontext *l, *c;
2007 struct ocontext *newc = NULL;
2008 struct genfs *genfs_p, *genfs;
2009 struct genfs *newgenfs = NULL;
2011 rc = next_entry(buf, fp, sizeof(u32));
2014 nel = le32_to_cpu(buf[0]);
2016 for (i = 0; i < nel; i++) {
2017 rc = next_entry(buf, fp, sizeof(u32));
2020 len = le32_to_cpu(buf[0]);
2023 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2027 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2031 for (genfs_p = NULL, genfs = p->genfs; genfs;
2032 genfs_p = genfs, genfs = genfs->next) {
2034 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2035 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2039 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2042 newgenfs->next = genfs;
2044 genfs_p->next = newgenfs;
2046 p->genfs = newgenfs;
2050 rc = next_entry(buf, fp, sizeof(u32));
2054 nel2 = le32_to_cpu(buf[0]);
2055 for (j = 0; j < nel2; j++) {
2056 rc = next_entry(buf, fp, sizeof(u32));
2059 len = le32_to_cpu(buf[0]);
2062 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2066 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2070 rc = next_entry(buf, fp, sizeof(u32));
2074 newc->v.sclass = le32_to_cpu(buf[0]);
2075 rc = context_read_and_validate(&newc->context[0], p, fp);
2079 for (l = NULL, c = genfs->head; c;
2080 l = c, c = c->next) {
2082 if (!strcmp(newc->u.name, c->u.name) &&
2083 (!c->v.sclass || !newc->v.sclass ||
2084 newc->v.sclass == c->v.sclass)) {
2085 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2086 genfs->fstype, c->u.name);
2089 len = strlen(newc->u.name);
2090 len2 = strlen(c->u.name);
2106 kfree(newgenfs->fstype);
2108 ocontext_destroy(newc, OCON_FSUSE);
2113 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2119 struct ocontext *l, *c;
2122 for (i = 0; i < info->ocon_num; i++) {
2123 rc = next_entry(buf, fp, sizeof(u32));
2126 nel = le32_to_cpu(buf[0]);
2129 for (j = 0; j < nel; j++) {
2131 c = kzalloc(sizeof(*c), GFP_KERNEL);
2137 p->ocontexts[i] = c;
2142 rc = next_entry(buf, fp, sizeof(u32));
2146 c->sid[0] = le32_to_cpu(buf[0]);
2147 rc = context_read_and_validate(&c->context[0], p, fp);
2153 rc = next_entry(buf, fp, sizeof(u32));
2156 len = le32_to_cpu(buf[0]);
2158 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2162 rc = context_read_and_validate(&c->context[0], p, fp);
2165 rc = context_read_and_validate(&c->context[1], p, fp);
2170 rc = next_entry(buf, fp, sizeof(u32)*3);
2173 c->u.port.protocol = le32_to_cpu(buf[0]);
2174 c->u.port.low_port = le32_to_cpu(buf[1]);
2175 c->u.port.high_port = le32_to_cpu(buf[2]);
2176 rc = context_read_and_validate(&c->context[0], p, fp);
2181 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2184 c->u.node.addr = nodebuf[0]; /* network order */
2185 c->u.node.mask = nodebuf[1]; /* network order */
2186 rc = context_read_and_validate(&c->context[0], p, fp);
2191 rc = next_entry(buf, fp, sizeof(u32)*2);
2196 c->v.behavior = le32_to_cpu(buf[0]);
2197 /* Determined at runtime, not in policy DB. */
2198 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2200 if (c->v.behavior > SECURITY_FS_USE_MAX)
2203 len = le32_to_cpu(buf[1]);
2204 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2208 rc = context_read_and_validate(&c->context[0], p, fp);
2215 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2218 for (k = 0; k < 4; k++)
2219 c->u.node6.addr[k] = nodebuf[k];
2220 for (k = 0; k < 4; k++)
2221 c->u.node6.mask[k] = nodebuf[k+4];
2222 rc = context_read_and_validate(&c->context[0], p, fp);
2236 * Read the configuration data from a policy database binary
2237 * representation file into a policy database structure.
2239 int policydb_read(struct policydb *p, void *fp)
2241 struct role_allow *ra, *lra;
2242 struct role_trans *tr, *ltr;
2245 u32 len, nprim, nel;
2248 struct policydb_compat_info *info;
2250 rc = policydb_init(p);
2254 /* Read the magic number and string length. */
2255 rc = next_entry(buf, fp, sizeof(u32) * 2);
2260 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2261 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2262 "not match expected magic number 0x%x\n",
2263 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2268 len = le32_to_cpu(buf[1]);
2269 if (len != strlen(POLICYDB_STRING)) {
2270 printk(KERN_ERR "SELinux: policydb string length %d does not "
2271 "match expected length %Zu\n",
2272 len, strlen(POLICYDB_STRING));
2277 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2278 if (!policydb_str) {
2279 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2280 "string of length %d\n", len);
2284 rc = next_entry(policydb_str, fp, len);
2286 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2287 kfree(policydb_str);
2292 policydb_str[len] = '\0';
2293 if (strcmp(policydb_str, POLICYDB_STRING)) {
2294 printk(KERN_ERR "SELinux: policydb string %s does not match "
2295 "my string %s\n", policydb_str, POLICYDB_STRING);
2296 kfree(policydb_str);
2299 /* Done with policydb_str. */
2300 kfree(policydb_str);
2301 policydb_str = NULL;
2303 /* Read the version and table sizes. */
2304 rc = next_entry(buf, fp, sizeof(u32)*4);
2309 p->policyvers = le32_to_cpu(buf[0]);
2310 if (p->policyvers < POLICYDB_VERSION_MIN ||
2311 p->policyvers > POLICYDB_VERSION_MAX) {
2312 printk(KERN_ERR "SELinux: policydb version %d does not match "
2313 "my version range %d-%d\n",
2314 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2318 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2322 if (p->policyvers < POLICYDB_VERSION_MLS) {
2323 printk(KERN_ERR "SELinux: security policydb version %d "
2324 "(MLS) not backwards compatible\n",
2329 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2330 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2332 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2333 rc = ebitmap_read(&p->policycaps, fp);
2338 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2339 rc = ebitmap_read(&p->permissive_map, fp);
2345 info = policydb_lookup_compat(p->policyvers);
2347 printk(KERN_ERR "SELinux: unable to find policy compat info "
2348 "for version %d\n", p->policyvers);
2353 if (le32_to_cpu(buf[2]) != info->sym_num ||
2354 le32_to_cpu(buf[3]) != info->ocon_num) {
2355 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2356 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2357 le32_to_cpu(buf[3]),
2358 info->sym_num, info->ocon_num);
2362 for (i = 0; i < info->sym_num; i++) {
2363 rc = next_entry(buf, fp, sizeof(u32)*2);
2366 nprim = le32_to_cpu(buf[0]);
2367 nel = le32_to_cpu(buf[1]);
2368 for (j = 0; j < nel; j++) {
2369 rc = read_f[i](p, p->symtab[i].table, fp);
2374 p->symtab[i].nprim = nprim;
2378 p->process_class = string_to_security_class(p, "process");
2379 if (!p->process_class)
2382 rc = avtab_read(&p->te_avtab, fp, p);
2386 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2387 rc = cond_read_list(p, fp);
2392 rc = next_entry(buf, fp, sizeof(u32));
2395 nel = le32_to_cpu(buf[0]);
2397 for (i = 0; i < nel; i++) {
2399 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2406 rc = next_entry(buf, fp, sizeof(u32)*3);
2411 tr->role = le32_to_cpu(buf[0]);
2412 tr->type = le32_to_cpu(buf[1]);
2413 tr->new_role = le32_to_cpu(buf[2]);
2414 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2415 rc = next_entry(buf, fp, sizeof(u32));
2418 tr->tclass = le32_to_cpu(buf[0]);
2420 tr->tclass = p->process_class;
2422 if (!policydb_role_isvalid(p, tr->role) ||
2423 !policydb_type_isvalid(p, tr->type) ||
2424 !policydb_class_isvalid(p, tr->tclass) ||
2425 !policydb_role_isvalid(p, tr->new_role))
2430 rc = next_entry(buf, fp, sizeof(u32));
2433 nel = le32_to_cpu(buf[0]);
2435 for (i = 0; i < nel; i++) {
2437 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2444 rc = next_entry(buf, fp, sizeof(u32)*2);
2449 ra->role = le32_to_cpu(buf[0]);
2450 ra->new_role = le32_to_cpu(buf[1]);
2451 if (!policydb_role_isvalid(p, ra->role) ||
2452 !policydb_role_isvalid(p, ra->new_role))
2457 rc = filename_trans_read(p, fp);
2461 rc = policydb_index(p);
2466 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2467 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2468 if (!p->process_trans_perms)
2471 rc = ocontext_read(p, info, fp);
2475 rc = genfs_read(p, fp);
2479 rc = range_read(p, fp);
2484 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2486 GFP_KERNEL | __GFP_ZERO);
2487 if (!p->type_attr_map_array)
2490 /* preallocate so we don't have to worry about the put ever failing */
2491 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2492 GFP_KERNEL | __GFP_ZERO);
2496 for (i = 0; i < p->p_types.nprim; i++) {
2497 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2501 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2502 rc = ebitmap_read(e, fp);
2506 /* add the type itself as the degenerate case */
2507 rc = ebitmap_set_bit(e, i, 1);
2512 rc = policydb_bounds_sanity_check(p);
2520 policydb_destroy(p);
2525 * Write a MLS level structure to a policydb binary
2526 * representation file.
2528 static int mls_write_level(struct mls_level *l, void *fp)
2533 buf[0] = cpu_to_le32(l->sens);
2534 rc = put_entry(buf, sizeof(u32), 1, fp);
2538 rc = ebitmap_write(&l->cat, fp);
2546 * Write a MLS range structure to a policydb binary
2547 * representation file.
2549 static int mls_write_range_helper(struct mls_range *r, void *fp)
2555 eq = mls_level_eq(&r->level[1], &r->level[0]);
2561 buf[0] = cpu_to_le32(items-1);
2562 buf[1] = cpu_to_le32(r->level[0].sens);
2564 buf[2] = cpu_to_le32(r->level[1].sens);
2566 BUG_ON(items > ARRAY_SIZE(buf));
2568 rc = put_entry(buf, sizeof(u32), items, fp);
2572 rc = ebitmap_write(&r->level[0].cat, fp);
2576 rc = ebitmap_write(&r->level[1].cat, fp);
2584 static int sens_write(void *vkey, void *datum, void *ptr)
2587 struct level_datum *levdatum = datum;
2588 struct policy_data *pd = ptr;
2595 buf[0] = cpu_to_le32(len);
2596 buf[1] = cpu_to_le32(levdatum->isalias);
2597 rc = put_entry(buf, sizeof(u32), 2, fp);
2601 rc = put_entry(key, 1, len, fp);
2605 rc = mls_write_level(levdatum->level, fp);
2612 static int cat_write(void *vkey, void *datum, void *ptr)
2615 struct cat_datum *catdatum = datum;
2616 struct policy_data *pd = ptr;
2623 buf[0] = cpu_to_le32(len);
2624 buf[1] = cpu_to_le32(catdatum->value);
2625 buf[2] = cpu_to_le32(catdatum->isalias);
2626 rc = put_entry(buf, sizeof(u32), 3, fp);
2630 rc = put_entry(key, 1, len, fp);
2637 static int role_trans_write(struct policydb *p, void *fp)
2639 struct role_trans *r = p->role_tr;
2640 struct role_trans *tr;
2646 for (tr = r; tr; tr = tr->next)
2648 buf[0] = cpu_to_le32(nel);
2649 rc = put_entry(buf, sizeof(u32), 1, fp);
2652 for (tr = r; tr; tr = tr->next) {
2653 buf[0] = cpu_to_le32(tr->role);
2654 buf[1] = cpu_to_le32(tr->type);
2655 buf[2] = cpu_to_le32(tr->new_role);
2656 rc = put_entry(buf, sizeof(u32), 3, fp);
2659 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2660 buf[0] = cpu_to_le32(tr->tclass);
2661 rc = put_entry(buf, sizeof(u32), 1, fp);
2670 static int role_allow_write(struct role_allow *r, void *fp)
2672 struct role_allow *ra;
2678 for (ra = r; ra; ra = ra->next)
2680 buf[0] = cpu_to_le32(nel);
2681 rc = put_entry(buf, sizeof(u32), 1, fp);
2684 for (ra = r; ra; ra = ra->next) {
2685 buf[0] = cpu_to_le32(ra->role);
2686 buf[1] = cpu_to_le32(ra->new_role);
2687 rc = put_entry(buf, sizeof(u32), 2, fp);
2695 * Write a security context structure
2696 * to a policydb binary representation file.
2698 static int context_write(struct policydb *p, struct context *c,
2704 buf[0] = cpu_to_le32(c->user);
2705 buf[1] = cpu_to_le32(c->role);
2706 buf[2] = cpu_to_le32(c->type);
2708 rc = put_entry(buf, sizeof(u32), 3, fp);
2712 rc = mls_write_range_helper(&c->range, fp);
2720 * The following *_write functions are used to
2721 * write the symbol data to a policy database
2722 * binary representation file.
2725 static int perm_write(void *vkey, void *datum, void *fp)
2728 struct perm_datum *perdatum = datum;
2734 buf[0] = cpu_to_le32(len);
2735 buf[1] = cpu_to_le32(perdatum->value);
2736 rc = put_entry(buf, sizeof(u32), 2, fp);
2740 rc = put_entry(key, 1, len, fp);
2747 static int common_write(void *vkey, void *datum, void *ptr)
2750 struct common_datum *comdatum = datum;
2751 struct policy_data *pd = ptr;
2758 buf[0] = cpu_to_le32(len);
2759 buf[1] = cpu_to_le32(comdatum->value);
2760 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2761 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2762 rc = put_entry(buf, sizeof(u32), 4, fp);
2766 rc = put_entry(key, 1, len, fp);
2770 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2777 static int type_set_write(struct type_set *t, void *fp)
2782 if (ebitmap_write(&t->types, fp))
2784 if (ebitmap_write(&t->negset, fp))
2787 buf[0] = cpu_to_le32(t->flags);
2788 rc = put_entry(buf, sizeof(u32), 1, fp);
2795 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2798 struct constraint_node *c;
2799 struct constraint_expr *e;
2804 for (c = node; c; c = c->next) {
2806 for (e = c->expr; e; e = e->next)
2808 buf[0] = cpu_to_le32(c->permissions);
2809 buf[1] = cpu_to_le32(nel);
2810 rc = put_entry(buf, sizeof(u32), 2, fp);
2813 for (e = c->expr; e; e = e->next) {
2814 buf[0] = cpu_to_le32(e->expr_type);
2815 buf[1] = cpu_to_le32(e->attr);
2816 buf[2] = cpu_to_le32(e->op);
2817 rc = put_entry(buf, sizeof(u32), 3, fp);
2821 switch (e->expr_type) {
2823 rc = ebitmap_write(&e->names, fp);
2826 if (p->policyvers >=
2827 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2828 rc = type_set_write(e->type_names, fp);
2842 static int class_write(void *vkey, void *datum, void *ptr)
2845 struct class_datum *cladatum = datum;
2846 struct policy_data *pd = ptr;
2848 struct policydb *p = pd->p;
2849 struct constraint_node *c;
2856 if (cladatum->comkey)
2857 len2 = strlen(cladatum->comkey);
2862 for (c = cladatum->constraints; c; c = c->next)
2865 buf[0] = cpu_to_le32(len);
2866 buf[1] = cpu_to_le32(len2);
2867 buf[2] = cpu_to_le32(cladatum->value);
2868 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2869 if (cladatum->permissions.table)
2870 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2873 buf[5] = cpu_to_le32(ncons);
2874 rc = put_entry(buf, sizeof(u32), 6, fp);
2878 rc = put_entry(key, 1, len, fp);
2882 if (cladatum->comkey) {
2883 rc = put_entry(cladatum->comkey, 1, len2, fp);
2888 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2892 rc = write_cons_helper(p, cladatum->constraints, fp);
2896 /* write out the validatetrans rule */
2898 for (c = cladatum->validatetrans; c; c = c->next)
2901 buf[0] = cpu_to_le32(ncons);
2902 rc = put_entry(buf, sizeof(u32), 1, fp);
2906 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2910 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2911 buf[0] = cpu_to_le32(cladatum->default_user);
2912 buf[1] = cpu_to_le32(cladatum->default_role);
2913 buf[2] = cpu_to_le32(cladatum->default_range);
2915 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2920 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2921 buf[0] = cpu_to_le32(cladatum->default_type);
2922 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2930 static int role_write(void *vkey, void *datum, void *ptr)
2933 struct role_datum *role = datum;
2934 struct policy_data *pd = ptr;
2936 struct policydb *p = pd->p;
2943 buf[items++] = cpu_to_le32(len);
2944 buf[items++] = cpu_to_le32(role->value);
2945 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2946 buf[items++] = cpu_to_le32(role->bounds);
2948 BUG_ON(items > ARRAY_SIZE(buf));
2950 rc = put_entry(buf, sizeof(u32), items, fp);
2954 rc = put_entry(key, 1, len, fp);
2958 rc = ebitmap_write(&role->dominates, fp);
2962 rc = ebitmap_write(&role->types, fp);
2969 static int type_write(void *vkey, void *datum, void *ptr)
2972 struct type_datum *typdatum = datum;
2973 struct policy_data *pd = ptr;
2974 struct policydb *p = pd->p;
2982 buf[items++] = cpu_to_le32(len);
2983 buf[items++] = cpu_to_le32(typdatum->value);
2984 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2987 if (typdatum->primary)
2988 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2990 if (typdatum->attribute)
2991 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2993 buf[items++] = cpu_to_le32(properties);
2994 buf[items++] = cpu_to_le32(typdatum->bounds);
2996 buf[items++] = cpu_to_le32(typdatum->primary);
2998 BUG_ON(items > ARRAY_SIZE(buf));
2999 rc = put_entry(buf, sizeof(u32), items, fp);
3003 rc = put_entry(key, 1, len, fp);
3010 static int user_write(void *vkey, void *datum, void *ptr)
3013 struct user_datum *usrdatum = datum;
3014 struct policy_data *pd = ptr;
3015 struct policydb *p = pd->p;
3023 buf[items++] = cpu_to_le32(len);
3024 buf[items++] = cpu_to_le32(usrdatum->value);
3025 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3026 buf[items++] = cpu_to_le32(usrdatum->bounds);
3027 BUG_ON(items > ARRAY_SIZE(buf));
3028 rc = put_entry(buf, sizeof(u32), items, fp);
3032 rc = put_entry(key, 1, len, fp);
3036 rc = ebitmap_write(&usrdatum->roles, fp);
3040 rc = mls_write_range_helper(&usrdatum->range, fp);
3044 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3051 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3064 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3067 unsigned int i, j, rc;
3072 for (i = 0; i < info->ocon_num; i++) {
3074 for (c = p->ocontexts[i]; c; c = c->next)
3076 buf[0] = cpu_to_le32(nel);
3077 rc = put_entry(buf, sizeof(u32), 1, fp);
3080 for (c = p->ocontexts[i]; c; c = c->next) {
3083 buf[0] = cpu_to_le32(c->sid[0]);
3084 rc = put_entry(buf, sizeof(u32), 1, fp);
3087 rc = context_write(p, &c->context[0], fp);
3093 len = strlen(c->u.name);
3094 buf[0] = cpu_to_le32(len);
3095 rc = put_entry(buf, sizeof(u32), 1, fp);
3098 rc = put_entry(c->u.name, 1, len, fp);
3101 rc = context_write(p, &c->context[0], fp);
3104 rc = context_write(p, &c->context[1], fp);
3109 buf[0] = cpu_to_le32(c->u.port.protocol);
3110 buf[1] = cpu_to_le32(c->u.port.low_port);
3111 buf[2] = cpu_to_le32(c->u.port.high_port);
3112 rc = put_entry(buf, sizeof(u32), 3, fp);
3115 rc = context_write(p, &c->context[0], fp);
3120 nodebuf[0] = c->u.node.addr; /* network order */
3121 nodebuf[1] = c->u.node.mask; /* network order */
3122 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3125 rc = context_write(p, &c->context[0], fp);
3130 buf[0] = cpu_to_le32(c->v.behavior);
3131 len = strlen(c->u.name);
3132 buf[1] = cpu_to_le32(len);
3133 rc = put_entry(buf, sizeof(u32), 2, fp);
3136 rc = put_entry(c->u.name, 1, len, fp);
3139 rc = context_write(p, &c->context[0], fp);
3144 for (j = 0; j < 4; j++)
3145 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3146 for (j = 0; j < 4; j++)
3147 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3148 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3151 rc = context_write(p, &c->context[0], fp);
3161 static int genfs_write(struct policydb *p, void *fp)
3163 struct genfs *genfs;
3170 for (genfs = p->genfs; genfs; genfs = genfs->next)
3172 buf[0] = cpu_to_le32(len);
3173 rc = put_entry(buf, sizeof(u32), 1, fp);
3176 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3177 len = strlen(genfs->fstype);
3178 buf[0] = cpu_to_le32(len);
3179 rc = put_entry(buf, sizeof(u32), 1, fp);
3182 rc = put_entry(genfs->fstype, 1, len, fp);
3186 for (c = genfs->head; c; c = c->next)
3188 buf[0] = cpu_to_le32(len);
3189 rc = put_entry(buf, sizeof(u32), 1, fp);
3192 for (c = genfs->head; c; c = c->next) {
3193 len = strlen(c->u.name);
3194 buf[0] = cpu_to_le32(len);
3195 rc = put_entry(buf, sizeof(u32), 1, fp);
3198 rc = put_entry(c->u.name, 1, len, fp);
3201 buf[0] = cpu_to_le32(c->v.sclass);
3202 rc = put_entry(buf, sizeof(u32), 1, fp);
3205 rc = context_write(p, &c->context[0], fp);
3213 static int hashtab_cnt(void *key, void *data, void *ptr)
3221 static int range_write_helper(void *key, void *data, void *ptr)
3224 struct range_trans *rt = key;
3225 struct mls_range *r = data;
3226 struct policy_data *pd = ptr;
3228 struct policydb *p = pd->p;
3231 buf[0] = cpu_to_le32(rt->source_type);
3232 buf[1] = cpu_to_le32(rt->target_type);
3233 rc = put_entry(buf, sizeof(u32), 2, fp);
3236 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3237 buf[0] = cpu_to_le32(rt->target_class);
3238 rc = put_entry(buf, sizeof(u32), 1, fp);
3242 rc = mls_write_range_helper(r, fp);
3249 static int range_write(struct policydb *p, void *fp)
3253 struct policy_data pd;
3258 /* count the number of entries in the hashtab */
3260 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3264 buf[0] = cpu_to_le32(nel);
3265 rc = put_entry(buf, sizeof(u32), 1, fp);
3269 /* actually write all of the entries */
3270 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3277 static int filename_write_helper(void *key, void *data, void *ptr)
3280 struct filename_trans *ft = key;
3281 struct filename_trans_datum *otype = data;
3286 len = strlen(ft->name);
3287 buf[0] = cpu_to_le32(len);
3288 rc = put_entry(buf, sizeof(u32), 1, fp);
3292 rc = put_entry(ft->name, sizeof(char), len, fp);
3296 buf[0] = cpu_to_le32(ft->stype);
3297 buf[1] = cpu_to_le32(ft->ttype);
3298 buf[2] = cpu_to_le32(ft->tclass);
3299 buf[3] = cpu_to_le32(otype->otype);
3301 rc = put_entry(buf, sizeof(u32), 4, fp);
3308 static int filename_trans_write(struct policydb *p, void *fp)
3314 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3318 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3322 buf[0] = cpu_to_le32(nel);
3323 rc = put_entry(buf, sizeof(u32), 1, fp);
3327 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3335 * Write the configuration data in a policy database
3336 * structure to a policy database binary representation
3339 int policydb_write(struct policydb *p, void *fp)
3341 unsigned int i, num_syms;
3346 struct policydb_compat_info *info;
3349 * refuse to write policy older than compressed avtab
3350 * to simplify the writer. There are other tests dropped
3351 * since we assume this throughout the writer code. Be
3352 * careful if you ever try to remove this restriction
3354 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3355 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3356 " Because it is less than version %d\n", p->policyvers,
3357 POLICYDB_VERSION_AVTAB);
3363 config |= POLICYDB_CONFIG_MLS;
3365 if (p->reject_unknown)
3366 config |= REJECT_UNKNOWN;
3367 if (p->allow_unknown)
3368 config |= ALLOW_UNKNOWN;
3370 /* Write the magic number and string identifiers. */
3371 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3372 len = strlen(POLICYDB_STRING);
3373 buf[1] = cpu_to_le32(len);
3374 rc = put_entry(buf, sizeof(u32), 2, fp);
3377 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3381 /* Write the version, config, and table sizes. */
3382 info = policydb_lookup_compat(p->policyvers);
3384 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3385 "version %d", p->policyvers);
3389 buf[0] = cpu_to_le32(p->policyvers);
3390 buf[1] = cpu_to_le32(config);
3391 buf[2] = cpu_to_le32(info->sym_num);
3392 buf[3] = cpu_to_le32(info->ocon_num);
3394 rc = put_entry(buf, sizeof(u32), 4, fp);
3398 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3399 rc = ebitmap_write(&p->policycaps, fp);
3404 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3405 rc = ebitmap_write(&p->permissive_map, fp);
3410 num_syms = info->sym_num;
3411 for (i = 0; i < num_syms; i++) {
3412 struct policy_data pd;
3417 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3418 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3420 rc = put_entry(buf, sizeof(u32), 2, fp);
3423 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3428 rc = avtab_write(p, &p->te_avtab, fp);
3432 rc = cond_write_list(p, p->cond_list, fp);
3436 rc = role_trans_write(p, fp);
3440 rc = role_allow_write(p->role_allow, fp);
3444 rc = filename_trans_write(p, fp);
3448 rc = ocontext_write(p, info, fp);
3452 rc = genfs_write(p, fp);
3456 rc = range_write(p, fp);
3460 for (i = 0; i < p->p_types.nprim; i++) {
3461 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3464 rc = ebitmap_write(e, fp);