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;
270 * Initialize a policy database structure.
272 static int policydb_init(struct policydb *p)
276 memset(p, 0, sizeof(*p));
278 for (i = 0; i < SYM_NUM; i++) {
279 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
284 rc = avtab_init(&p->te_avtab);
292 rc = cond_policydb_init(p);
296 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
297 if (!p->filename_trans) {
302 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
308 ebitmap_init(&p->filename_trans_ttypes);
309 ebitmap_init(&p->policycaps);
310 ebitmap_init(&p->permissive_map);
314 hashtab_destroy(p->filename_trans);
315 hashtab_destroy(p->range_tr);
316 for (i = 0; i < SYM_NUM; i++)
317 hashtab_destroy(p->symtab[i].table);
322 * The following *_index functions are used to
323 * define the val_to_name and val_to_struct arrays
324 * in a policy database structure. The val_to_name
325 * arrays are used when converting security context
326 * structures into string representations. The
327 * val_to_struct arrays are used when the attributes
328 * of a class, role, or user are needed.
331 static int common_index(void *key, void *datum, void *datap)
334 struct common_datum *comdatum;
335 struct flex_array *fa;
339 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
342 fa = p->sym_val_to_name[SYM_COMMONS];
343 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
344 GFP_KERNEL | __GFP_ZERO))
349 static int class_index(void *key, void *datum, void *datap)
352 struct class_datum *cladatum;
353 struct flex_array *fa;
357 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
359 fa = p->sym_val_to_name[SYM_CLASSES];
360 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
361 GFP_KERNEL | __GFP_ZERO))
363 p->class_val_to_struct[cladatum->value - 1] = cladatum;
367 static int role_index(void *key, void *datum, void *datap)
370 struct role_datum *role;
371 struct flex_array *fa;
376 || role->value > p->p_roles.nprim
377 || role->bounds > p->p_roles.nprim)
380 fa = p->sym_val_to_name[SYM_ROLES];
381 if (flex_array_put_ptr(fa, role->value - 1, key,
382 GFP_KERNEL | __GFP_ZERO))
384 p->role_val_to_struct[role->value - 1] = role;
388 static int type_index(void *key, void *datum, void *datap)
391 struct type_datum *typdatum;
392 struct flex_array *fa;
397 if (typdatum->primary) {
399 || typdatum->value > p->p_types.nprim
400 || typdatum->bounds > p->p_types.nprim)
402 fa = p->sym_val_to_name[SYM_TYPES];
403 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
404 GFP_KERNEL | __GFP_ZERO))
407 fa = p->type_val_to_struct_array;
408 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
409 GFP_KERNEL | __GFP_ZERO))
416 static int user_index(void *key, void *datum, void *datap)
419 struct user_datum *usrdatum;
420 struct flex_array *fa;
425 || usrdatum->value > p->p_users.nprim
426 || usrdatum->bounds > p->p_users.nprim)
429 fa = p->sym_val_to_name[SYM_USERS];
430 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
431 GFP_KERNEL | __GFP_ZERO))
433 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
437 static int sens_index(void *key, void *datum, void *datap)
440 struct level_datum *levdatum;
441 struct flex_array *fa;
446 if (!levdatum->isalias) {
447 if (!levdatum->level->sens ||
448 levdatum->level->sens > p->p_levels.nprim)
450 fa = p->sym_val_to_name[SYM_LEVELS];
451 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
452 GFP_KERNEL | __GFP_ZERO))
459 static int cat_index(void *key, void *datum, void *datap)
462 struct cat_datum *catdatum;
463 struct flex_array *fa;
468 if (!catdatum->isalias) {
469 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
471 fa = p->sym_val_to_name[SYM_CATS];
472 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
473 GFP_KERNEL | __GFP_ZERO))
480 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
493 static void hash_eval(struct hashtab *h, const char *hash_name)
495 struct hashtab_info info;
497 hashtab_stat(h, &info);
498 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
499 "longest chain length %d\n", hash_name, h->nel,
500 info.slots_used, h->size, info.max_chain_len);
503 static void symtab_hash_eval(struct symtab *s)
507 for (i = 0; i < SYM_NUM; i++)
508 hash_eval(s[i].table, symtab_name[i]);
512 static inline void hash_eval(struct hashtab *h, char *hash_name)
518 * Define the other val_to_name and val_to_struct arrays
519 * in a policy database structure.
521 * Caller must clean up on failure.
523 static int policydb_index(struct policydb *p)
527 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
528 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
530 printk(KERN_CONT ", %d sens, %d cats", p->p_levels.nprim,
532 printk(KERN_CONT "\n");
534 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
535 p->p_classes.nprim, p->te_avtab.nel);
538 avtab_hash_eval(&p->te_avtab, "rules");
539 symtab_hash_eval(p->symtab);
543 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
544 sizeof(*p->class_val_to_struct),
546 if (!p->class_val_to_struct)
550 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
551 sizeof(*p->role_val_to_struct),
553 if (!p->role_val_to_struct)
557 p->user_val_to_struct = kcalloc(p->p_users.nprim,
558 sizeof(*p->user_val_to_struct),
560 if (!p->user_val_to_struct)
563 /* Yes, I want the sizeof the pointer, not the structure */
565 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
567 GFP_KERNEL | __GFP_ZERO);
568 if (!p->type_val_to_struct_array)
571 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
572 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
576 rc = cond_init_bool_indexes(p);
580 for (i = 0; i < SYM_NUM; i++) {
582 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
584 GFP_KERNEL | __GFP_ZERO);
585 if (!p->sym_val_to_name[i])
588 rc = flex_array_prealloc(p->sym_val_to_name[i],
589 0, p->symtab[i].nprim,
590 GFP_KERNEL | __GFP_ZERO);
594 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
604 * The following *_destroy functions are used to
605 * free any memory allocated for each kind of
606 * symbol data in the policy database.
609 static int perm_destroy(void *key, void *datum, void *p)
616 static int common_destroy(void *key, void *datum, void *p)
618 struct common_datum *comdatum;
623 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
624 hashtab_destroy(comdatum->permissions.table);
630 static void constraint_expr_destroy(struct constraint_expr *expr)
633 ebitmap_destroy(&expr->names);
634 if (expr->type_names) {
635 ebitmap_destroy(&expr->type_names->types);
636 ebitmap_destroy(&expr->type_names->negset);
637 kfree(expr->type_names);
643 static int cls_destroy(void *key, void *datum, void *p)
645 struct class_datum *cladatum;
646 struct constraint_node *constraint, *ctemp;
647 struct constraint_expr *e, *etmp;
652 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
653 hashtab_destroy(cladatum->permissions.table);
654 constraint = cladatum->constraints;
656 e = constraint->expr;
660 constraint_expr_destroy(etmp);
663 constraint = constraint->next;
667 constraint = cladatum->validatetrans;
669 e = constraint->expr;
673 constraint_expr_destroy(etmp);
676 constraint = constraint->next;
679 kfree(cladatum->comkey);
685 static int role_destroy(void *key, void *datum, void *p)
687 struct role_datum *role;
692 ebitmap_destroy(&role->dominates);
693 ebitmap_destroy(&role->types);
699 static int type_destroy(void *key, void *datum, void *p)
706 static int user_destroy(void *key, void *datum, void *p)
708 struct user_datum *usrdatum;
713 ebitmap_destroy(&usrdatum->roles);
714 ebitmap_destroy(&usrdatum->range.level[0].cat);
715 ebitmap_destroy(&usrdatum->range.level[1].cat);
716 ebitmap_destroy(&usrdatum->dfltlevel.cat);
722 static int sens_destroy(void *key, void *datum, void *p)
724 struct level_datum *levdatum;
729 ebitmap_destroy(&levdatum->level->cat);
730 kfree(levdatum->level);
736 static int cat_destroy(void *key, void *datum, void *p)
743 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
755 static int filenametr_destroy(void *key, void *datum, void *p)
757 struct filename_trans *ft = key;
765 static int range_tr_destroy(void *key, void *datum, void *p)
767 struct mls_range *rt = datum;
769 ebitmap_destroy(&rt->level[0].cat);
770 ebitmap_destroy(&rt->level[1].cat);
776 static void ocontext_destroy(struct ocontext *c, int i)
781 context_destroy(&c->context[0]);
782 context_destroy(&c->context[1]);
783 if (i == OCON_ISID || i == OCON_FS ||
784 i == OCON_NETIF || i == OCON_FSUSE)
790 * Free any memory allocated by a policy database structure.
792 void policydb_destroy(struct policydb *p)
794 struct ocontext *c, *ctmp;
795 struct genfs *g, *gtmp;
797 struct role_allow *ra, *lra = NULL;
798 struct role_trans *tr, *ltr = NULL;
800 for (i = 0; i < SYM_NUM; i++) {
802 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
803 hashtab_destroy(p->symtab[i].table);
806 for (i = 0; i < SYM_NUM; i++) {
807 if (p->sym_val_to_name[i])
808 flex_array_free(p->sym_val_to_name[i]);
811 kfree(p->class_val_to_struct);
812 kfree(p->role_val_to_struct);
813 kfree(p->user_val_to_struct);
814 if (p->type_val_to_struct_array)
815 flex_array_free(p->type_val_to_struct_array);
817 avtab_destroy(&p->te_avtab);
819 for (i = 0; i < OCON_NUM; i++) {
825 ocontext_destroy(ctmp, i);
827 p->ocontexts[i] = NULL;
838 ocontext_destroy(ctmp, OCON_FSUSE);
846 cond_policydb_destroy(p);
848 for (tr = p->role_tr; tr; tr = tr->next) {
855 for (ra = p->role_allow; ra; ra = ra->next) {
862 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
863 hashtab_destroy(p->filename_trans);
865 hashtab_map(p->range_tr, range_tr_destroy, NULL);
866 hashtab_destroy(p->range_tr);
868 if (p->type_attr_map_array) {
869 for (i = 0; i < p->p_types.nprim; i++) {
872 e = flex_array_get(p->type_attr_map_array, i);
877 flex_array_free(p->type_attr_map_array);
880 ebitmap_destroy(&p->filename_trans_ttypes);
881 ebitmap_destroy(&p->policycaps);
882 ebitmap_destroy(&p->permissive_map);
886 * Load the initial SIDs specified in a policy database
887 * structure into a SID table.
889 int policydb_load_isids(struct policydb *p, struct sidtab *s)
891 struct ocontext *head, *c;
896 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
900 head = p->ocontexts[OCON_ISID];
901 for (c = head; c; c = c->next) {
903 if (!c->context[0].user) {
904 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
909 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
911 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
921 int policydb_class_isvalid(struct policydb *p, unsigned int class)
923 if (!class || class > p->p_classes.nprim)
928 int policydb_role_isvalid(struct policydb *p, unsigned int role)
930 if (!role || role > p->p_roles.nprim)
935 int policydb_type_isvalid(struct policydb *p, unsigned int type)
937 if (!type || type > p->p_types.nprim)
943 * Return 1 if the fields in the security context
944 * structure `c' are valid. Return 0 otherwise.
946 int policydb_context_isvalid(struct policydb *p, struct context *c)
948 struct role_datum *role;
949 struct user_datum *usrdatum;
951 if (!c->role || c->role > p->p_roles.nprim)
954 if (!c->user || c->user > p->p_users.nprim)
957 if (!c->type || c->type > p->p_types.nprim)
960 if (c->role != OBJECT_R_VAL) {
962 * Role must be authorized for the type.
964 role = p->role_val_to_struct[c->role - 1];
965 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
966 /* role may not be associated with type */
970 * User must be authorized for the role.
972 usrdatum = p->user_val_to_struct[c->user - 1];
976 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
977 /* user may not be associated with role */
981 if (!mls_context_isvalid(p, c))
988 * Read a MLS range structure from a policydb binary
989 * representation file.
991 static int mls_read_range_helper(struct mls_range *r, void *fp)
997 rc = next_entry(buf, fp, sizeof(u32));
1002 items = le32_to_cpu(buf[0]);
1003 if (items > ARRAY_SIZE(buf)) {
1004 printk(KERN_ERR "SELinux: mls: range overflow\n");
1008 rc = next_entry(buf, fp, sizeof(u32) * items);
1010 printk(KERN_ERR "SELinux: mls: truncated range\n");
1014 r->level[0].sens = le32_to_cpu(buf[0]);
1016 r->level[1].sens = le32_to_cpu(buf[1]);
1018 r->level[1].sens = r->level[0].sens;
1020 rc = ebitmap_read(&r->level[0].cat, fp);
1022 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
1026 rc = ebitmap_read(&r->level[1].cat, fp);
1028 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1032 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1034 printk(KERN_ERR "SELinux: mls: out of memory\n");
1041 ebitmap_destroy(&r->level[0].cat);
1047 * Read and validate a security context structure
1048 * from a policydb binary representation file.
1050 static int context_read_and_validate(struct context *c,
1057 rc = next_entry(buf, fp, sizeof buf);
1059 printk(KERN_ERR "SELinux: context truncated\n");
1062 c->user = le32_to_cpu(buf[0]);
1063 c->role = le32_to_cpu(buf[1]);
1064 c->type = le32_to_cpu(buf[2]);
1065 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1066 rc = mls_read_range_helper(&c->range, fp);
1068 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1074 if (!policydb_context_isvalid(p, c)) {
1075 printk(KERN_ERR "SELinux: invalid security context\n");
1085 * The following *_read functions are used to
1086 * read the symbol data from a policy database
1087 * binary representation file.
1090 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1095 if ((len == 0) || (len == (u32)-1))
1098 str = kmalloc(len + 1, flags);
1102 /* it's expected the caller should free the str */
1105 rc = next_entry(str, fp, len);
1113 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1116 struct perm_datum *perdatum;
1121 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1125 rc = next_entry(buf, fp, sizeof buf);
1129 len = le32_to_cpu(buf[0]);
1130 perdatum->value = le32_to_cpu(buf[1]);
1132 rc = str_read(&key, GFP_KERNEL, fp, len);
1136 rc = hashtab_insert(h, key, perdatum);
1142 perm_destroy(key, perdatum, NULL);
1146 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1149 struct common_datum *comdatum;
1154 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1158 rc = next_entry(buf, fp, sizeof buf);
1162 len = le32_to_cpu(buf[0]);
1163 comdatum->value = le32_to_cpu(buf[1]);
1165 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1168 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1169 nel = le32_to_cpu(buf[3]);
1171 rc = str_read(&key, GFP_KERNEL, fp, len);
1175 for (i = 0; i < nel; i++) {
1176 rc = perm_read(p, comdatum->permissions.table, fp);
1181 rc = hashtab_insert(h, key, comdatum);
1186 common_destroy(key, comdatum, NULL);
1190 static void type_set_init(struct type_set *t)
1192 ebitmap_init(&t->types);
1193 ebitmap_init(&t->negset);
1196 static int type_set_read(struct type_set *t, void *fp)
1201 if (ebitmap_read(&t->types, fp))
1203 if (ebitmap_read(&t->negset, fp))
1206 rc = next_entry(buf, fp, sizeof(u32));
1209 t->flags = le32_to_cpu(buf[0]);
1215 static int read_cons_helper(struct policydb *p,
1216 struct constraint_node **nodep,
1217 int ncons, int allowxtarget, void *fp)
1219 struct constraint_node *c, *lc;
1220 struct constraint_expr *e, *le;
1223 int rc, i, j, depth;
1226 for (i = 0; i < ncons; i++) {
1227 c = kzalloc(sizeof(*c), GFP_KERNEL);
1236 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1239 c->permissions = le32_to_cpu(buf[0]);
1240 nexpr = le32_to_cpu(buf[1]);
1243 for (j = 0; j < nexpr; j++) {
1244 e = kzalloc(sizeof(*e), GFP_KERNEL);
1253 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1256 e->expr_type = le32_to_cpu(buf[0]);
1257 e->attr = le32_to_cpu(buf[1]);
1258 e->op = le32_to_cpu(buf[2]);
1260 switch (e->expr_type) {
1272 if (depth == (CEXPR_MAXDEPTH - 1))
1277 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1279 if (depth == (CEXPR_MAXDEPTH - 1))
1282 rc = ebitmap_read(&e->names, fp);
1285 if (p->policyvers >=
1286 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1287 e->type_names = kzalloc(sizeof
1292 type_set_init(e->type_names);
1293 rc = type_set_read(e->type_names, fp);
1311 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1314 struct class_datum *cladatum;
1316 u32 len, len2, ncons, nel;
1319 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1323 rc = next_entry(buf, fp, sizeof(u32)*6);
1327 len = le32_to_cpu(buf[0]);
1328 len2 = le32_to_cpu(buf[1]);
1329 cladatum->value = le32_to_cpu(buf[2]);
1331 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1334 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1335 nel = le32_to_cpu(buf[4]);
1337 ncons = le32_to_cpu(buf[5]);
1339 rc = str_read(&key, GFP_KERNEL, fp, len);
1344 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1349 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1350 if (!cladatum->comdatum) {
1351 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1355 for (i = 0; i < nel; i++) {
1356 rc = perm_read(p, cladatum->permissions.table, fp);
1361 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1365 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1366 /* grab the validatetrans rules */
1367 rc = next_entry(buf, fp, sizeof(u32));
1370 ncons = le32_to_cpu(buf[0]);
1371 rc = read_cons_helper(p, &cladatum->validatetrans,
1377 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1378 rc = next_entry(buf, fp, sizeof(u32) * 3);
1382 cladatum->default_user = le32_to_cpu(buf[0]);
1383 cladatum->default_role = le32_to_cpu(buf[1]);
1384 cladatum->default_range = le32_to_cpu(buf[2]);
1387 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1388 rc = next_entry(buf, fp, sizeof(u32) * 1);
1391 cladatum->default_type = le32_to_cpu(buf[0]);
1394 rc = hashtab_insert(h, key, cladatum);
1400 cls_destroy(key, cladatum, NULL);
1404 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1407 struct role_datum *role;
1408 int rc, to_read = 2;
1412 role = kzalloc(sizeof(*role), GFP_KERNEL);
1416 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1419 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1423 len = le32_to_cpu(buf[0]);
1424 role->value = le32_to_cpu(buf[1]);
1425 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1426 role->bounds = le32_to_cpu(buf[2]);
1428 rc = str_read(&key, GFP_KERNEL, fp, len);
1432 rc = ebitmap_read(&role->dominates, fp);
1436 rc = ebitmap_read(&role->types, fp);
1440 if (strcmp(key, OBJECT_R) == 0) {
1442 if (role->value != OBJECT_R_VAL) {
1443 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1444 OBJECT_R, role->value);
1451 rc = hashtab_insert(h, key, role);
1456 role_destroy(key, role, NULL);
1460 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1463 struct type_datum *typdatum;
1464 int rc, to_read = 3;
1468 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1472 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1475 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1479 len = le32_to_cpu(buf[0]);
1480 typdatum->value = le32_to_cpu(buf[1]);
1481 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1482 u32 prop = le32_to_cpu(buf[2]);
1484 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1485 typdatum->primary = 1;
1486 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1487 typdatum->attribute = 1;
1489 typdatum->bounds = le32_to_cpu(buf[3]);
1491 typdatum->primary = le32_to_cpu(buf[2]);
1494 rc = str_read(&key, GFP_KERNEL, fp, len);
1498 rc = hashtab_insert(h, key, typdatum);
1503 type_destroy(key, typdatum, NULL);
1509 * Read a MLS level structure from a policydb binary
1510 * representation file.
1512 static int mls_read_level(struct mls_level *lp, void *fp)
1517 memset(lp, 0, sizeof(*lp));
1519 rc = next_entry(buf, fp, sizeof buf);
1521 printk(KERN_ERR "SELinux: mls: truncated level\n");
1524 lp->sens = le32_to_cpu(buf[0]);
1526 rc = ebitmap_read(&lp->cat, fp);
1528 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1534 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1537 struct user_datum *usrdatum;
1538 int rc, to_read = 2;
1542 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1546 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1549 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1553 len = le32_to_cpu(buf[0]);
1554 usrdatum->value = le32_to_cpu(buf[1]);
1555 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1556 usrdatum->bounds = le32_to_cpu(buf[2]);
1558 rc = str_read(&key, GFP_KERNEL, fp, len);
1562 rc = ebitmap_read(&usrdatum->roles, fp);
1566 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1567 rc = mls_read_range_helper(&usrdatum->range, fp);
1570 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1575 rc = hashtab_insert(h, key, usrdatum);
1580 user_destroy(key, usrdatum, NULL);
1584 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1587 struct level_datum *levdatum;
1592 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1596 rc = next_entry(buf, fp, sizeof buf);
1600 len = le32_to_cpu(buf[0]);
1601 levdatum->isalias = le32_to_cpu(buf[1]);
1603 rc = str_read(&key, GFP_ATOMIC, fp, len);
1608 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1609 if (!levdatum->level)
1612 rc = mls_read_level(levdatum->level, fp);
1616 rc = hashtab_insert(h, key, levdatum);
1621 sens_destroy(key, levdatum, NULL);
1625 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1628 struct cat_datum *catdatum;
1633 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1637 rc = next_entry(buf, fp, sizeof buf);
1641 len = le32_to_cpu(buf[0]);
1642 catdatum->value = le32_to_cpu(buf[1]);
1643 catdatum->isalias = le32_to_cpu(buf[2]);
1645 rc = str_read(&key, GFP_ATOMIC, fp, len);
1649 rc = hashtab_insert(h, key, catdatum);
1654 cat_destroy(key, catdatum, NULL);
1658 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1670 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1672 struct user_datum *upper, *user;
1673 struct policydb *p = datap;
1676 upper = user = datum;
1677 while (upper->bounds) {
1678 struct ebitmap_node *node;
1681 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1682 printk(KERN_ERR "SELinux: user %s: "
1683 "too deep or looped boundary",
1688 upper = p->user_val_to_struct[upper->bounds - 1];
1689 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1690 if (ebitmap_get_bit(&upper->roles, bit))
1694 "SELinux: boundary violated policy: "
1695 "user=%s role=%s bounds=%s\n",
1696 sym_name(p, SYM_USERS, user->value - 1),
1697 sym_name(p, SYM_ROLES, bit),
1698 sym_name(p, SYM_USERS, upper->value - 1));
1707 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1709 struct role_datum *upper, *role;
1710 struct policydb *p = datap;
1713 upper = role = datum;
1714 while (upper->bounds) {
1715 struct ebitmap_node *node;
1718 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1719 printk(KERN_ERR "SELinux: role %s: "
1720 "too deep or looped bounds\n",
1725 upper = p->role_val_to_struct[upper->bounds - 1];
1726 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1727 if (ebitmap_get_bit(&upper->types, bit))
1731 "SELinux: boundary violated policy: "
1732 "role=%s type=%s bounds=%s\n",
1733 sym_name(p, SYM_ROLES, role->value - 1),
1734 sym_name(p, SYM_TYPES, bit),
1735 sym_name(p, SYM_ROLES, upper->value - 1));
1744 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1746 struct type_datum *upper;
1747 struct policydb *p = datap;
1751 while (upper->bounds) {
1752 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1753 printk(KERN_ERR "SELinux: type %s: "
1754 "too deep or looped boundary\n",
1759 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1763 if (upper->attribute) {
1764 printk(KERN_ERR "SELinux: type %s: "
1765 "bounded by attribute %s",
1767 sym_name(p, SYM_TYPES, upper->value - 1));
1775 static int policydb_bounds_sanity_check(struct policydb *p)
1779 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1782 rc = hashtab_map(p->p_users.table,
1783 user_bounds_sanity_check, p);
1787 rc = hashtab_map(p->p_roles.table,
1788 role_bounds_sanity_check, p);
1792 rc = hashtab_map(p->p_types.table,
1793 type_bounds_sanity_check, p);
1800 u16 string_to_security_class(struct policydb *p, const char *name)
1802 struct class_datum *cladatum;
1804 cladatum = hashtab_search(p->p_classes.table, name);
1808 return cladatum->value;
1811 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1813 struct class_datum *cladatum;
1814 struct perm_datum *perdatum = NULL;
1815 struct common_datum *comdatum;
1817 if (!tclass || tclass > p->p_classes.nprim)
1820 cladatum = p->class_val_to_struct[tclass-1];
1821 comdatum = cladatum->comdatum;
1823 perdatum = hashtab_search(comdatum->permissions.table,
1826 perdatum = hashtab_search(cladatum->permissions.table,
1831 return 1U << (perdatum->value-1);
1834 static int range_read(struct policydb *p, void *fp)
1836 struct range_trans *rt;
1837 struct mls_range *r = NULL;
1842 if (p->policyvers < POLICYDB_VERSION_MLS)
1845 rc = next_entry(buf, fp, sizeof(u32));
1849 nel = le32_to_cpu(buf[0]);
1850 for (i = 0; i < nel; i++) {
1852 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1856 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1860 rt->source_type = le32_to_cpu(buf[0]);
1861 rt->target_type = le32_to_cpu(buf[1]);
1862 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1863 rc = next_entry(buf, fp, sizeof(u32));
1866 rt->target_class = le32_to_cpu(buf[0]);
1868 rt->target_class = p->process_class;
1871 if (!policydb_type_isvalid(p, rt->source_type) ||
1872 !policydb_type_isvalid(p, rt->target_type) ||
1873 !policydb_class_isvalid(p, rt->target_class))
1877 r = kzalloc(sizeof(*r), GFP_KERNEL);
1881 rc = mls_read_range_helper(r, fp);
1886 if (!mls_range_isvalid(p, r)) {
1887 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1891 rc = hashtab_insert(p->range_tr, rt, r);
1898 hash_eval(p->range_tr, "rangetr");
1906 static int filename_trans_read(struct policydb *p, void *fp)
1908 struct filename_trans *ft;
1909 struct filename_trans_datum *otype;
1915 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1918 rc = next_entry(buf, fp, sizeof(u32));
1921 nel = le32_to_cpu(buf[0]);
1923 for (i = 0; i < nel; i++) {
1928 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1933 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1937 /* length of the path component string */
1938 rc = next_entry(buf, fp, sizeof(u32));
1941 len = le32_to_cpu(buf[0]);
1943 /* path component string */
1944 rc = str_read(&name, GFP_KERNEL, fp, len);
1950 rc = next_entry(buf, fp, sizeof(u32) * 4);
1954 ft->stype = le32_to_cpu(buf[0]);
1955 ft->ttype = le32_to_cpu(buf[1]);
1956 ft->tclass = le32_to_cpu(buf[2]);
1958 otype->otype = le32_to_cpu(buf[3]);
1960 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1964 rc = hashtab_insert(p->filename_trans, ft, otype);
1967 * Do not return -EEXIST to the caller, or the system
1972 /* But free memory to avoid memory leak. */
1978 hash_eval(p->filename_trans, "filenametr");
1988 static int genfs_read(struct policydb *p, void *fp)
1991 u32 nel, nel2, len, len2;
1993 struct ocontext *l, *c;
1994 struct ocontext *newc = NULL;
1995 struct genfs *genfs_p, *genfs;
1996 struct genfs *newgenfs = NULL;
1998 rc = next_entry(buf, fp, sizeof(u32));
2001 nel = le32_to_cpu(buf[0]);
2003 for (i = 0; i < nel; i++) {
2004 rc = next_entry(buf, fp, sizeof(u32));
2007 len = le32_to_cpu(buf[0]);
2010 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2014 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2018 for (genfs_p = NULL, genfs = p->genfs; genfs;
2019 genfs_p = genfs, genfs = genfs->next) {
2021 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2022 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2026 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2029 newgenfs->next = genfs;
2031 genfs_p->next = newgenfs;
2033 p->genfs = newgenfs;
2037 rc = next_entry(buf, fp, sizeof(u32));
2041 nel2 = le32_to_cpu(buf[0]);
2042 for (j = 0; j < nel2; j++) {
2043 rc = next_entry(buf, fp, sizeof(u32));
2046 len = le32_to_cpu(buf[0]);
2049 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2053 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2057 rc = next_entry(buf, fp, sizeof(u32));
2061 newc->v.sclass = le32_to_cpu(buf[0]);
2062 rc = context_read_and_validate(&newc->context[0], p, fp);
2066 for (l = NULL, c = genfs->head; c;
2067 l = c, c = c->next) {
2069 if (!strcmp(newc->u.name, c->u.name) &&
2070 (!c->v.sclass || !newc->v.sclass ||
2071 newc->v.sclass == c->v.sclass)) {
2072 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2073 genfs->fstype, c->u.name);
2076 len = strlen(newc->u.name);
2077 len2 = strlen(c->u.name);
2093 kfree(newgenfs->fstype);
2096 ocontext_destroy(newc, OCON_FSUSE);
2101 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2107 struct ocontext *l, *c;
2110 for (i = 0; i < info->ocon_num; i++) {
2111 rc = next_entry(buf, fp, sizeof(u32));
2114 nel = le32_to_cpu(buf[0]);
2117 for (j = 0; j < nel; j++) {
2119 c = kzalloc(sizeof(*c), GFP_KERNEL);
2125 p->ocontexts[i] = c;
2130 rc = next_entry(buf, fp, sizeof(u32));
2134 c->sid[0] = le32_to_cpu(buf[0]);
2135 rc = context_read_and_validate(&c->context[0], p, fp);
2141 rc = next_entry(buf, fp, sizeof(u32));
2144 len = le32_to_cpu(buf[0]);
2146 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2150 rc = context_read_and_validate(&c->context[0], p, fp);
2153 rc = context_read_and_validate(&c->context[1], p, fp);
2158 rc = next_entry(buf, fp, sizeof(u32)*3);
2161 c->u.port.protocol = le32_to_cpu(buf[0]);
2162 c->u.port.low_port = le32_to_cpu(buf[1]);
2163 c->u.port.high_port = le32_to_cpu(buf[2]);
2164 rc = context_read_and_validate(&c->context[0], p, fp);
2169 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2172 c->u.node.addr = nodebuf[0]; /* network order */
2173 c->u.node.mask = nodebuf[1]; /* network order */
2174 rc = context_read_and_validate(&c->context[0], p, fp);
2179 rc = next_entry(buf, fp, sizeof(u32)*2);
2184 c->v.behavior = le32_to_cpu(buf[0]);
2185 /* Determined at runtime, not in policy DB. */
2186 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2188 if (c->v.behavior > SECURITY_FS_USE_MAX)
2191 len = le32_to_cpu(buf[1]);
2192 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2196 rc = context_read_and_validate(&c->context[0], p, fp);
2203 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2206 for (k = 0; k < 4; k++)
2207 c->u.node6.addr[k] = nodebuf[k];
2208 for (k = 0; k < 4; k++)
2209 c->u.node6.mask[k] = nodebuf[k+4];
2210 rc = context_read_and_validate(&c->context[0], p, fp);
2224 * Read the configuration data from a policy database binary
2225 * representation file into a policy database structure.
2227 int policydb_read(struct policydb *p, void *fp)
2229 struct role_allow *ra, *lra;
2230 struct role_trans *tr, *ltr;
2233 u32 len, nprim, nel;
2236 struct policydb_compat_info *info;
2238 rc = policydb_init(p);
2242 /* Read the magic number and string length. */
2243 rc = next_entry(buf, fp, sizeof(u32) * 2);
2248 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2249 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2250 "not match expected magic number 0x%x\n",
2251 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2256 len = le32_to_cpu(buf[1]);
2257 if (len != strlen(POLICYDB_STRING)) {
2258 printk(KERN_ERR "SELinux: policydb string length %d does not "
2259 "match expected length %zu\n",
2260 len, strlen(POLICYDB_STRING));
2265 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2266 if (!policydb_str) {
2267 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2268 "string of length %d\n", len);
2272 rc = next_entry(policydb_str, fp, len);
2274 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2275 kfree(policydb_str);
2280 policydb_str[len] = '\0';
2281 if (strcmp(policydb_str, POLICYDB_STRING)) {
2282 printk(KERN_ERR "SELinux: policydb string %s does not match "
2283 "my string %s\n", policydb_str, POLICYDB_STRING);
2284 kfree(policydb_str);
2287 /* Done with policydb_str. */
2288 kfree(policydb_str);
2289 policydb_str = NULL;
2291 /* Read the version and table sizes. */
2292 rc = next_entry(buf, fp, sizeof(u32)*4);
2297 p->policyvers = le32_to_cpu(buf[0]);
2298 if (p->policyvers < POLICYDB_VERSION_MIN ||
2299 p->policyvers > POLICYDB_VERSION_MAX) {
2300 printk(KERN_ERR "SELinux: policydb version %d does not match "
2301 "my version range %d-%d\n",
2302 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2306 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2310 if (p->policyvers < POLICYDB_VERSION_MLS) {
2311 printk(KERN_ERR "SELinux: security policydb version %d "
2312 "(MLS) not backwards compatible\n",
2317 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2318 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2320 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2321 rc = ebitmap_read(&p->policycaps, fp);
2326 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2327 rc = ebitmap_read(&p->permissive_map, fp);
2333 info = policydb_lookup_compat(p->policyvers);
2335 printk(KERN_ERR "SELinux: unable to find policy compat info "
2336 "for version %d\n", p->policyvers);
2341 if (le32_to_cpu(buf[2]) != info->sym_num ||
2342 le32_to_cpu(buf[3]) != info->ocon_num) {
2343 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2344 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2345 le32_to_cpu(buf[3]),
2346 info->sym_num, info->ocon_num);
2350 for (i = 0; i < info->sym_num; i++) {
2351 rc = next_entry(buf, fp, sizeof(u32)*2);
2354 nprim = le32_to_cpu(buf[0]);
2355 nel = le32_to_cpu(buf[1]);
2356 for (j = 0; j < nel; j++) {
2357 rc = read_f[i](p, p->symtab[i].table, fp);
2362 p->symtab[i].nprim = nprim;
2366 p->process_class = string_to_security_class(p, "process");
2367 if (!p->process_class)
2370 rc = avtab_read(&p->te_avtab, fp, p);
2374 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2375 rc = cond_read_list(p, fp);
2380 rc = next_entry(buf, fp, sizeof(u32));
2383 nel = le32_to_cpu(buf[0]);
2385 for (i = 0; i < nel; i++) {
2387 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2394 rc = next_entry(buf, fp, sizeof(u32)*3);
2399 tr->role = le32_to_cpu(buf[0]);
2400 tr->type = le32_to_cpu(buf[1]);
2401 tr->new_role = le32_to_cpu(buf[2]);
2402 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2403 rc = next_entry(buf, fp, sizeof(u32));
2406 tr->tclass = le32_to_cpu(buf[0]);
2408 tr->tclass = p->process_class;
2411 if (!policydb_role_isvalid(p, tr->role) ||
2412 !policydb_type_isvalid(p, tr->type) ||
2413 !policydb_class_isvalid(p, tr->tclass) ||
2414 !policydb_role_isvalid(p, tr->new_role))
2419 rc = next_entry(buf, fp, sizeof(u32));
2422 nel = le32_to_cpu(buf[0]);
2424 for (i = 0; i < nel; i++) {
2426 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2433 rc = next_entry(buf, fp, sizeof(u32)*2);
2438 ra->role = le32_to_cpu(buf[0]);
2439 ra->new_role = le32_to_cpu(buf[1]);
2440 if (!policydb_role_isvalid(p, ra->role) ||
2441 !policydb_role_isvalid(p, ra->new_role))
2446 rc = filename_trans_read(p, fp);
2450 rc = policydb_index(p);
2455 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2456 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2457 if (!p->process_trans_perms)
2460 rc = ocontext_read(p, info, fp);
2464 rc = genfs_read(p, fp);
2468 rc = range_read(p, fp);
2473 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2475 GFP_KERNEL | __GFP_ZERO);
2476 if (!p->type_attr_map_array)
2479 /* preallocate so we don't have to worry about the put ever failing */
2480 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2481 GFP_KERNEL | __GFP_ZERO);
2485 for (i = 0; i < p->p_types.nprim; i++) {
2486 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2490 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2491 rc = ebitmap_read(e, fp);
2495 /* add the type itself as the degenerate case */
2496 rc = ebitmap_set_bit(e, i, 1);
2501 rc = policydb_bounds_sanity_check(p);
2509 policydb_destroy(p);
2514 * Write a MLS level structure to a policydb binary
2515 * representation file.
2517 static int mls_write_level(struct mls_level *l, void *fp)
2522 buf[0] = cpu_to_le32(l->sens);
2523 rc = put_entry(buf, sizeof(u32), 1, fp);
2527 rc = ebitmap_write(&l->cat, fp);
2535 * Write a MLS range structure to a policydb binary
2536 * representation file.
2538 static int mls_write_range_helper(struct mls_range *r, void *fp)
2544 eq = mls_level_eq(&r->level[1], &r->level[0]);
2550 buf[0] = cpu_to_le32(items-1);
2551 buf[1] = cpu_to_le32(r->level[0].sens);
2553 buf[2] = cpu_to_le32(r->level[1].sens);
2555 BUG_ON(items > ARRAY_SIZE(buf));
2557 rc = put_entry(buf, sizeof(u32), items, fp);
2561 rc = ebitmap_write(&r->level[0].cat, fp);
2565 rc = ebitmap_write(&r->level[1].cat, fp);
2573 static int sens_write(void *vkey, void *datum, void *ptr)
2576 struct level_datum *levdatum = datum;
2577 struct policy_data *pd = ptr;
2584 buf[0] = cpu_to_le32(len);
2585 buf[1] = cpu_to_le32(levdatum->isalias);
2586 rc = put_entry(buf, sizeof(u32), 2, fp);
2590 rc = put_entry(key, 1, len, fp);
2594 rc = mls_write_level(levdatum->level, fp);
2601 static int cat_write(void *vkey, void *datum, void *ptr)
2604 struct cat_datum *catdatum = datum;
2605 struct policy_data *pd = ptr;
2612 buf[0] = cpu_to_le32(len);
2613 buf[1] = cpu_to_le32(catdatum->value);
2614 buf[2] = cpu_to_le32(catdatum->isalias);
2615 rc = put_entry(buf, sizeof(u32), 3, fp);
2619 rc = put_entry(key, 1, len, fp);
2626 static int role_trans_write(struct policydb *p, void *fp)
2628 struct role_trans *r = p->role_tr;
2629 struct role_trans *tr;
2635 for (tr = r; tr; tr = tr->next)
2637 buf[0] = cpu_to_le32(nel);
2638 rc = put_entry(buf, sizeof(u32), 1, fp);
2641 for (tr = r; tr; tr = tr->next) {
2642 buf[0] = cpu_to_le32(tr->role);
2643 buf[1] = cpu_to_le32(tr->type);
2644 buf[2] = cpu_to_le32(tr->new_role);
2645 rc = put_entry(buf, sizeof(u32), 3, fp);
2648 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2649 buf[0] = cpu_to_le32(tr->tclass);
2650 rc = put_entry(buf, sizeof(u32), 1, fp);
2659 static int role_allow_write(struct role_allow *r, void *fp)
2661 struct role_allow *ra;
2667 for (ra = r; ra; ra = ra->next)
2669 buf[0] = cpu_to_le32(nel);
2670 rc = put_entry(buf, sizeof(u32), 1, fp);
2673 for (ra = r; ra; ra = ra->next) {
2674 buf[0] = cpu_to_le32(ra->role);
2675 buf[1] = cpu_to_le32(ra->new_role);
2676 rc = put_entry(buf, sizeof(u32), 2, fp);
2684 * Write a security context structure
2685 * to a policydb binary representation file.
2687 static int context_write(struct policydb *p, struct context *c,
2693 buf[0] = cpu_to_le32(c->user);
2694 buf[1] = cpu_to_le32(c->role);
2695 buf[2] = cpu_to_le32(c->type);
2697 rc = put_entry(buf, sizeof(u32), 3, fp);
2701 rc = mls_write_range_helper(&c->range, fp);
2709 * The following *_write functions are used to
2710 * write the symbol data to a policy database
2711 * binary representation file.
2714 static int perm_write(void *vkey, void *datum, void *fp)
2717 struct perm_datum *perdatum = datum;
2723 buf[0] = cpu_to_le32(len);
2724 buf[1] = cpu_to_le32(perdatum->value);
2725 rc = put_entry(buf, sizeof(u32), 2, fp);
2729 rc = put_entry(key, 1, len, fp);
2736 static int common_write(void *vkey, void *datum, void *ptr)
2739 struct common_datum *comdatum = datum;
2740 struct policy_data *pd = ptr;
2747 buf[0] = cpu_to_le32(len);
2748 buf[1] = cpu_to_le32(comdatum->value);
2749 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2750 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2751 rc = put_entry(buf, sizeof(u32), 4, fp);
2755 rc = put_entry(key, 1, len, fp);
2759 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2766 static int type_set_write(struct type_set *t, void *fp)
2771 if (ebitmap_write(&t->types, fp))
2773 if (ebitmap_write(&t->negset, fp))
2776 buf[0] = cpu_to_le32(t->flags);
2777 rc = put_entry(buf, sizeof(u32), 1, fp);
2784 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2787 struct constraint_node *c;
2788 struct constraint_expr *e;
2793 for (c = node; c; c = c->next) {
2795 for (e = c->expr; e; e = e->next)
2797 buf[0] = cpu_to_le32(c->permissions);
2798 buf[1] = cpu_to_le32(nel);
2799 rc = put_entry(buf, sizeof(u32), 2, fp);
2802 for (e = c->expr; e; e = e->next) {
2803 buf[0] = cpu_to_le32(e->expr_type);
2804 buf[1] = cpu_to_le32(e->attr);
2805 buf[2] = cpu_to_le32(e->op);
2806 rc = put_entry(buf, sizeof(u32), 3, fp);
2810 switch (e->expr_type) {
2812 rc = ebitmap_write(&e->names, fp);
2815 if (p->policyvers >=
2816 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2817 rc = type_set_write(e->type_names, fp);
2831 static int class_write(void *vkey, void *datum, void *ptr)
2834 struct class_datum *cladatum = datum;
2835 struct policy_data *pd = ptr;
2837 struct policydb *p = pd->p;
2838 struct constraint_node *c;
2845 if (cladatum->comkey)
2846 len2 = strlen(cladatum->comkey);
2851 for (c = cladatum->constraints; c; c = c->next)
2854 buf[0] = cpu_to_le32(len);
2855 buf[1] = cpu_to_le32(len2);
2856 buf[2] = cpu_to_le32(cladatum->value);
2857 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2858 if (cladatum->permissions.table)
2859 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2862 buf[5] = cpu_to_le32(ncons);
2863 rc = put_entry(buf, sizeof(u32), 6, fp);
2867 rc = put_entry(key, 1, len, fp);
2871 if (cladatum->comkey) {
2872 rc = put_entry(cladatum->comkey, 1, len2, fp);
2877 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2881 rc = write_cons_helper(p, cladatum->constraints, fp);
2885 /* write out the validatetrans rule */
2887 for (c = cladatum->validatetrans; c; c = c->next)
2890 buf[0] = cpu_to_le32(ncons);
2891 rc = put_entry(buf, sizeof(u32), 1, fp);
2895 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2899 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2900 buf[0] = cpu_to_le32(cladatum->default_user);
2901 buf[1] = cpu_to_le32(cladatum->default_role);
2902 buf[2] = cpu_to_le32(cladatum->default_range);
2904 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2909 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2910 buf[0] = cpu_to_le32(cladatum->default_type);
2911 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2919 static int role_write(void *vkey, void *datum, void *ptr)
2922 struct role_datum *role = datum;
2923 struct policy_data *pd = ptr;
2925 struct policydb *p = pd->p;
2932 buf[items++] = cpu_to_le32(len);
2933 buf[items++] = cpu_to_le32(role->value);
2934 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2935 buf[items++] = cpu_to_le32(role->bounds);
2937 BUG_ON(items > ARRAY_SIZE(buf));
2939 rc = put_entry(buf, sizeof(u32), items, fp);
2943 rc = put_entry(key, 1, len, fp);
2947 rc = ebitmap_write(&role->dominates, fp);
2951 rc = ebitmap_write(&role->types, fp);
2958 static int type_write(void *vkey, void *datum, void *ptr)
2961 struct type_datum *typdatum = datum;
2962 struct policy_data *pd = ptr;
2963 struct policydb *p = pd->p;
2971 buf[items++] = cpu_to_le32(len);
2972 buf[items++] = cpu_to_le32(typdatum->value);
2973 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2976 if (typdatum->primary)
2977 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2979 if (typdatum->attribute)
2980 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2982 buf[items++] = cpu_to_le32(properties);
2983 buf[items++] = cpu_to_le32(typdatum->bounds);
2985 buf[items++] = cpu_to_le32(typdatum->primary);
2987 BUG_ON(items > ARRAY_SIZE(buf));
2988 rc = put_entry(buf, sizeof(u32), items, fp);
2992 rc = put_entry(key, 1, len, fp);
2999 static int user_write(void *vkey, void *datum, void *ptr)
3002 struct user_datum *usrdatum = datum;
3003 struct policy_data *pd = ptr;
3004 struct policydb *p = pd->p;
3012 buf[items++] = cpu_to_le32(len);
3013 buf[items++] = cpu_to_le32(usrdatum->value);
3014 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3015 buf[items++] = cpu_to_le32(usrdatum->bounds);
3016 BUG_ON(items > ARRAY_SIZE(buf));
3017 rc = put_entry(buf, sizeof(u32), items, fp);
3021 rc = put_entry(key, 1, len, fp);
3025 rc = ebitmap_write(&usrdatum->roles, fp);
3029 rc = mls_write_range_helper(&usrdatum->range, fp);
3033 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3040 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3053 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3056 unsigned int i, j, rc;
3061 for (i = 0; i < info->ocon_num; i++) {
3063 for (c = p->ocontexts[i]; c; c = c->next)
3065 buf[0] = cpu_to_le32(nel);
3066 rc = put_entry(buf, sizeof(u32), 1, fp);
3069 for (c = p->ocontexts[i]; c; c = c->next) {
3072 buf[0] = cpu_to_le32(c->sid[0]);
3073 rc = put_entry(buf, sizeof(u32), 1, fp);
3076 rc = context_write(p, &c->context[0], fp);
3082 len = strlen(c->u.name);
3083 buf[0] = cpu_to_le32(len);
3084 rc = put_entry(buf, sizeof(u32), 1, fp);
3087 rc = put_entry(c->u.name, 1, len, fp);
3090 rc = context_write(p, &c->context[0], fp);
3093 rc = context_write(p, &c->context[1], fp);
3098 buf[0] = cpu_to_le32(c->u.port.protocol);
3099 buf[1] = cpu_to_le32(c->u.port.low_port);
3100 buf[2] = cpu_to_le32(c->u.port.high_port);
3101 rc = put_entry(buf, sizeof(u32), 3, fp);
3104 rc = context_write(p, &c->context[0], fp);
3109 nodebuf[0] = c->u.node.addr; /* network order */
3110 nodebuf[1] = c->u.node.mask; /* network order */
3111 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3114 rc = context_write(p, &c->context[0], fp);
3119 buf[0] = cpu_to_le32(c->v.behavior);
3120 len = strlen(c->u.name);
3121 buf[1] = cpu_to_le32(len);
3122 rc = put_entry(buf, sizeof(u32), 2, fp);
3125 rc = put_entry(c->u.name, 1, len, fp);
3128 rc = context_write(p, &c->context[0], fp);
3133 for (j = 0; j < 4; j++)
3134 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3135 for (j = 0; j < 4; j++)
3136 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3137 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3140 rc = context_write(p, &c->context[0], fp);
3150 static int genfs_write(struct policydb *p, void *fp)
3152 struct genfs *genfs;
3159 for (genfs = p->genfs; genfs; genfs = genfs->next)
3161 buf[0] = cpu_to_le32(len);
3162 rc = put_entry(buf, sizeof(u32), 1, fp);
3165 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3166 len = strlen(genfs->fstype);
3167 buf[0] = cpu_to_le32(len);
3168 rc = put_entry(buf, sizeof(u32), 1, fp);
3171 rc = put_entry(genfs->fstype, 1, len, fp);
3175 for (c = genfs->head; c; c = c->next)
3177 buf[0] = cpu_to_le32(len);
3178 rc = put_entry(buf, sizeof(u32), 1, fp);
3181 for (c = genfs->head; c; c = c->next) {
3182 len = strlen(c->u.name);
3183 buf[0] = cpu_to_le32(len);
3184 rc = put_entry(buf, sizeof(u32), 1, fp);
3187 rc = put_entry(c->u.name, 1, len, fp);
3190 buf[0] = cpu_to_le32(c->v.sclass);
3191 rc = put_entry(buf, sizeof(u32), 1, fp);
3194 rc = context_write(p, &c->context[0], fp);
3202 static int hashtab_cnt(void *key, void *data, void *ptr)
3210 static int range_write_helper(void *key, void *data, void *ptr)
3213 struct range_trans *rt = key;
3214 struct mls_range *r = data;
3215 struct policy_data *pd = ptr;
3217 struct policydb *p = pd->p;
3220 buf[0] = cpu_to_le32(rt->source_type);
3221 buf[1] = cpu_to_le32(rt->target_type);
3222 rc = put_entry(buf, sizeof(u32), 2, fp);
3225 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3226 buf[0] = cpu_to_le32(rt->target_class);
3227 rc = put_entry(buf, sizeof(u32), 1, fp);
3231 rc = mls_write_range_helper(r, fp);
3238 static int range_write(struct policydb *p, void *fp)
3242 struct policy_data pd;
3247 /* count the number of entries in the hashtab */
3249 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3253 buf[0] = cpu_to_le32(nel);
3254 rc = put_entry(buf, sizeof(u32), 1, fp);
3258 /* actually write all of the entries */
3259 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3266 static int filename_write_helper(void *key, void *data, void *ptr)
3269 struct filename_trans *ft = key;
3270 struct filename_trans_datum *otype = data;
3275 len = strlen(ft->name);
3276 buf[0] = cpu_to_le32(len);
3277 rc = put_entry(buf, sizeof(u32), 1, fp);
3281 rc = put_entry(ft->name, sizeof(char), len, fp);
3285 buf[0] = cpu_to_le32(ft->stype);
3286 buf[1] = cpu_to_le32(ft->ttype);
3287 buf[2] = cpu_to_le32(ft->tclass);
3288 buf[3] = cpu_to_le32(otype->otype);
3290 rc = put_entry(buf, sizeof(u32), 4, fp);
3297 static int filename_trans_write(struct policydb *p, void *fp)
3303 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3307 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3311 buf[0] = cpu_to_le32(nel);
3312 rc = put_entry(buf, sizeof(u32), 1, fp);
3316 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3324 * Write the configuration data in a policy database
3325 * structure to a policy database binary representation
3328 int policydb_write(struct policydb *p, void *fp)
3330 unsigned int i, num_syms;
3335 struct policydb_compat_info *info;
3338 * refuse to write policy older than compressed avtab
3339 * to simplify the writer. There are other tests dropped
3340 * since we assume this throughout the writer code. Be
3341 * careful if you ever try to remove this restriction
3343 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3344 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3345 " Because it is less than version %d\n", p->policyvers,
3346 POLICYDB_VERSION_AVTAB);
3352 config |= POLICYDB_CONFIG_MLS;
3354 if (p->reject_unknown)
3355 config |= REJECT_UNKNOWN;
3356 if (p->allow_unknown)
3357 config |= ALLOW_UNKNOWN;
3359 /* Write the magic number and string identifiers. */
3360 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3361 len = strlen(POLICYDB_STRING);
3362 buf[1] = cpu_to_le32(len);
3363 rc = put_entry(buf, sizeof(u32), 2, fp);
3366 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3370 /* Write the version, config, and table sizes. */
3371 info = policydb_lookup_compat(p->policyvers);
3373 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3374 "version %d", p->policyvers);
3378 buf[0] = cpu_to_le32(p->policyvers);
3379 buf[1] = cpu_to_le32(config);
3380 buf[2] = cpu_to_le32(info->sym_num);
3381 buf[3] = cpu_to_le32(info->ocon_num);
3383 rc = put_entry(buf, sizeof(u32), 4, fp);
3387 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3388 rc = ebitmap_write(&p->policycaps, fp);
3393 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3394 rc = ebitmap_write(&p->permissive_map, fp);
3399 num_syms = info->sym_num;
3400 for (i = 0; i < num_syms; i++) {
3401 struct policy_data pd;
3406 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3407 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3409 rc = put_entry(buf, sizeof(u32), 2, fp);
3412 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3417 rc = avtab_write(p, &p->te_avtab, fp);
3421 rc = cond_write_list(p, p->cond_list, fp);
3425 rc = role_trans_write(p, fp);
3429 rc = role_allow_write(p->role_allow, fp);
3433 rc = filename_trans_write(p, fp);
3437 rc = ocontext_write(p, info, fp);
3441 rc = genfs_write(p, fp);
3445 rc = range_write(p, fp);
3449 for (i = 0; i < p->p_types.nprim; i++) {
3450 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3453 rc = ebitmap_write(e, fp);