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,
153 static struct policydb_compat_info *policydb_lookup_compat(int version)
156 struct policydb_compat_info *info = NULL;
158 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
159 if (policydb_compat[i].version == version) {
160 info = &policydb_compat[i];
168 * Initialize the role table.
170 static int roles_init(struct policydb *p)
174 struct role_datum *role;
177 role = kzalloc(sizeof(*role), GFP_KERNEL);
182 role->value = ++p->p_roles.nprim;
183 if (role->value != OBJECT_R_VAL)
187 key = kstrdup(OBJECT_R, GFP_KERNEL);
191 rc = hashtab_insert(p->p_roles.table, key, role);
202 static u32 filenametr_hash(struct hashtab *h, const void *k)
204 const struct filename_trans *ft = k;
206 unsigned int byte_num;
209 hash = ft->stype ^ ft->ttype ^ ft->tclass;
212 while ((focus = ft->name[byte_num++]))
213 hash = partial_name_hash(focus, hash);
214 return hash & (h->size - 1);
217 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
219 const struct filename_trans *ft1 = k1;
220 const struct filename_trans *ft2 = k2;
223 v = ft1->stype - ft2->stype;
227 v = ft1->ttype - ft2->ttype;
231 v = ft1->tclass - ft2->tclass;
235 return strcmp(ft1->name, ft2->name);
239 static u32 rangetr_hash(struct hashtab *h, const void *k)
241 const struct range_trans *key = k;
242 return (key->source_type + (key->target_type << 3) +
243 (key->target_class << 5)) & (h->size - 1);
246 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
248 const struct range_trans *key1 = k1, *key2 = k2;
251 v = key1->source_type - key2->source_type;
255 v = key1->target_type - key2->target_type;
259 v = key1->target_class - key2->target_class;
265 * Initialize a policy database structure.
267 static int policydb_init(struct policydb *p)
271 memset(p, 0, sizeof(*p));
273 for (i = 0; i < SYM_NUM; i++) {
274 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
279 rc = avtab_init(&p->te_avtab);
287 rc = cond_policydb_init(p);
291 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
292 if (!p->filename_trans)
295 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
299 ebitmap_init(&p->filename_trans_ttypes);
300 ebitmap_init(&p->policycaps);
301 ebitmap_init(&p->permissive_map);
305 hashtab_destroy(p->filename_trans);
306 hashtab_destroy(p->range_tr);
307 for (i = 0; i < SYM_NUM; i++)
308 hashtab_destroy(p->symtab[i].table);
313 * The following *_index functions are used to
314 * define the val_to_name and val_to_struct arrays
315 * in a policy database structure. The val_to_name
316 * arrays are used when converting security context
317 * structures into string representations. The
318 * val_to_struct arrays are used when the attributes
319 * of a class, role, or user are needed.
322 static int common_index(void *key, void *datum, void *datap)
325 struct common_datum *comdatum;
326 struct flex_array *fa;
330 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
333 fa = p->sym_val_to_name[SYM_COMMONS];
334 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
335 GFP_KERNEL | __GFP_ZERO))
340 static int class_index(void *key, void *datum, void *datap)
343 struct class_datum *cladatum;
344 struct flex_array *fa;
348 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
350 fa = p->sym_val_to_name[SYM_CLASSES];
351 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
352 GFP_KERNEL | __GFP_ZERO))
354 p->class_val_to_struct[cladatum->value - 1] = cladatum;
358 static int role_index(void *key, void *datum, void *datap)
361 struct role_datum *role;
362 struct flex_array *fa;
367 || role->value > p->p_roles.nprim
368 || role->bounds > p->p_roles.nprim)
371 fa = p->sym_val_to_name[SYM_ROLES];
372 if (flex_array_put_ptr(fa, role->value - 1, key,
373 GFP_KERNEL | __GFP_ZERO))
375 p->role_val_to_struct[role->value - 1] = role;
379 static int type_index(void *key, void *datum, void *datap)
382 struct type_datum *typdatum;
383 struct flex_array *fa;
388 if (typdatum->primary) {
390 || typdatum->value > p->p_types.nprim
391 || typdatum->bounds > p->p_types.nprim)
393 fa = p->sym_val_to_name[SYM_TYPES];
394 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
398 fa = p->type_val_to_struct_array;
399 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
400 GFP_KERNEL | __GFP_ZERO))
407 static int user_index(void *key, void *datum, void *datap)
410 struct user_datum *usrdatum;
411 struct flex_array *fa;
416 || usrdatum->value > p->p_users.nprim
417 || usrdatum->bounds > p->p_users.nprim)
420 fa = p->sym_val_to_name[SYM_USERS];
421 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
422 GFP_KERNEL | __GFP_ZERO))
424 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
428 static int sens_index(void *key, void *datum, void *datap)
431 struct level_datum *levdatum;
432 struct flex_array *fa;
437 if (!levdatum->isalias) {
438 if (!levdatum->level->sens ||
439 levdatum->level->sens > p->p_levels.nprim)
441 fa = p->sym_val_to_name[SYM_LEVELS];
442 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
443 GFP_KERNEL | __GFP_ZERO))
450 static int cat_index(void *key, void *datum, void *datap)
453 struct cat_datum *catdatum;
454 struct flex_array *fa;
459 if (!catdatum->isalias) {
460 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
462 fa = p->sym_val_to_name[SYM_CATS];
463 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
464 GFP_KERNEL | __GFP_ZERO))
471 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
484 static void hash_eval(struct hashtab *h, const char *hash_name)
486 struct hashtab_info info;
488 hashtab_stat(h, &info);
489 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
490 "longest chain length %d\n", hash_name, h->nel,
491 info.slots_used, h->size, info.max_chain_len);
494 static void symtab_hash_eval(struct symtab *s)
498 for (i = 0; i < SYM_NUM; i++)
499 hash_eval(s[i].table, symtab_name[i]);
503 static inline void hash_eval(struct hashtab *h, char *hash_name)
509 * Define the other val_to_name and val_to_struct arrays
510 * in a policy database structure.
512 * Caller must clean up on failure.
514 static int policydb_index(struct policydb *p)
518 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
519 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
521 printk(", %d sens, %d cats", p->p_levels.nprim,
525 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
526 p->p_classes.nprim, p->te_avtab.nel);
529 avtab_hash_eval(&p->te_avtab, "rules");
530 symtab_hash_eval(p->symtab);
534 p->class_val_to_struct =
535 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
537 if (!p->class_val_to_struct)
541 p->role_val_to_struct =
542 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
544 if (!p->role_val_to_struct)
548 p->user_val_to_struct =
549 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
551 if (!p->user_val_to_struct)
554 /* Yes, I want the sizeof the pointer, not the structure */
556 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
558 GFP_KERNEL | __GFP_ZERO);
559 if (!p->type_val_to_struct_array)
562 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
563 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
567 rc = cond_init_bool_indexes(p);
571 for (i = 0; i < SYM_NUM; i++) {
573 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
575 GFP_KERNEL | __GFP_ZERO);
576 if (!p->sym_val_to_name[i])
579 rc = flex_array_prealloc(p->sym_val_to_name[i],
580 0, p->symtab[i].nprim,
581 GFP_KERNEL | __GFP_ZERO);
585 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
595 * The following *_destroy functions are used to
596 * free any memory allocated for each kind of
597 * symbol data in the policy database.
600 static int perm_destroy(void *key, void *datum, void *p)
607 static int common_destroy(void *key, void *datum, void *p)
609 struct common_datum *comdatum;
614 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
615 hashtab_destroy(comdatum->permissions.table);
621 static void constraint_expr_destroy(struct constraint_expr *expr)
624 ebitmap_destroy(&expr->names);
625 if (expr->type_names) {
626 ebitmap_destroy(&expr->type_names->types);
627 ebitmap_destroy(&expr->type_names->negset);
628 kfree(expr->type_names);
634 static int cls_destroy(void *key, void *datum, void *p)
636 struct class_datum *cladatum;
637 struct constraint_node *constraint, *ctemp;
638 struct constraint_expr *e, *etmp;
643 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
644 hashtab_destroy(cladatum->permissions.table);
645 constraint = cladatum->constraints;
647 e = constraint->expr;
651 constraint_expr_destroy(etmp);
654 constraint = constraint->next;
658 constraint = cladatum->validatetrans;
660 e = constraint->expr;
664 constraint_expr_destroy(etmp);
667 constraint = constraint->next;
670 kfree(cladatum->comkey);
676 static int role_destroy(void *key, void *datum, void *p)
678 struct role_datum *role;
683 ebitmap_destroy(&role->dominates);
684 ebitmap_destroy(&role->types);
690 static int type_destroy(void *key, void *datum, void *p)
697 static int user_destroy(void *key, void *datum, void *p)
699 struct user_datum *usrdatum;
704 ebitmap_destroy(&usrdatum->roles);
705 ebitmap_destroy(&usrdatum->range.level[0].cat);
706 ebitmap_destroy(&usrdatum->range.level[1].cat);
707 ebitmap_destroy(&usrdatum->dfltlevel.cat);
713 static int sens_destroy(void *key, void *datum, void *p)
715 struct level_datum *levdatum;
720 ebitmap_destroy(&levdatum->level->cat);
721 kfree(levdatum->level);
727 static int cat_destroy(void *key, void *datum, void *p)
734 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
746 static int filenametr_destroy(void *key, void *datum, void *p)
748 struct filename_trans *ft = key;
756 static int range_tr_destroy(void *key, void *datum, void *p)
758 struct mls_range *rt = datum;
760 ebitmap_destroy(&rt->level[0].cat);
761 ebitmap_destroy(&rt->level[1].cat);
767 static void ocontext_destroy(struct ocontext *c, int i)
772 context_destroy(&c->context[0]);
773 context_destroy(&c->context[1]);
774 if (i == OCON_ISID || i == OCON_FS ||
775 i == OCON_NETIF || i == OCON_FSUSE)
781 * Free any memory allocated by a policy database structure.
783 void policydb_destroy(struct policydb *p)
785 struct ocontext *c, *ctmp;
786 struct genfs *g, *gtmp;
788 struct role_allow *ra, *lra = NULL;
789 struct role_trans *tr, *ltr = NULL;
791 for (i = 0; i < SYM_NUM; i++) {
793 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
794 hashtab_destroy(p->symtab[i].table);
797 for (i = 0; i < SYM_NUM; i++) {
798 if (p->sym_val_to_name[i])
799 flex_array_free(p->sym_val_to_name[i]);
802 kfree(p->class_val_to_struct);
803 kfree(p->role_val_to_struct);
804 kfree(p->user_val_to_struct);
805 if (p->type_val_to_struct_array)
806 flex_array_free(p->type_val_to_struct_array);
808 avtab_destroy(&p->te_avtab);
810 for (i = 0; i < OCON_NUM; i++) {
816 ocontext_destroy(ctmp, i);
818 p->ocontexts[i] = NULL;
829 ocontext_destroy(ctmp, OCON_FSUSE);
837 cond_policydb_destroy(p);
839 for (tr = p->role_tr; tr; tr = tr->next) {
846 for (ra = p->role_allow; ra; ra = ra->next) {
853 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
854 hashtab_destroy(p->filename_trans);
856 hashtab_map(p->range_tr, range_tr_destroy, NULL);
857 hashtab_destroy(p->range_tr);
859 if (p->type_attr_map_array) {
860 for (i = 0; i < p->p_types.nprim; i++) {
863 e = flex_array_get(p->type_attr_map_array, i);
868 flex_array_free(p->type_attr_map_array);
871 ebitmap_destroy(&p->filename_trans_ttypes);
872 ebitmap_destroy(&p->policycaps);
873 ebitmap_destroy(&p->permissive_map);
879 * Load the initial SIDs specified in a policy database
880 * structure into a SID table.
882 int policydb_load_isids(struct policydb *p, struct sidtab *s)
884 struct ocontext *head, *c;
889 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
893 head = p->ocontexts[OCON_ISID];
894 for (c = head; c; c = c->next) {
896 if (!c->context[0].user) {
897 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
902 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
904 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
914 int policydb_class_isvalid(struct policydb *p, unsigned int class)
916 if (!class || class > p->p_classes.nprim)
921 int policydb_role_isvalid(struct policydb *p, unsigned int role)
923 if (!role || role > p->p_roles.nprim)
928 int policydb_type_isvalid(struct policydb *p, unsigned int type)
930 if (!type || type > p->p_types.nprim)
936 * Return 1 if the fields in the security context
937 * structure `c' are valid. Return 0 otherwise.
939 int policydb_context_isvalid(struct policydb *p, struct context *c)
941 struct role_datum *role;
942 struct user_datum *usrdatum;
944 if (!c->role || c->role > p->p_roles.nprim)
947 if (!c->user || c->user > p->p_users.nprim)
950 if (!c->type || c->type > p->p_types.nprim)
953 if (c->role != OBJECT_R_VAL) {
955 * Role must be authorized for the type.
957 role = p->role_val_to_struct[c->role - 1];
958 if (!ebitmap_get_bit(&role->types, c->type - 1))
959 /* role may not be associated with type */
963 * User must be authorized for the role.
965 usrdatum = p->user_val_to_struct[c->user - 1];
969 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
970 /* user may not be associated with role */
974 if (!mls_context_isvalid(p, c))
981 * Read a MLS range structure from a policydb binary
982 * representation file.
984 static int mls_read_range_helper(struct mls_range *r, void *fp)
990 rc = next_entry(buf, fp, sizeof(u32));
995 items = le32_to_cpu(buf[0]);
996 if (items > ARRAY_SIZE(buf)) {
997 printk(KERN_ERR "SELinux: mls: range overflow\n");
1001 rc = next_entry(buf, fp, sizeof(u32) * items);
1003 printk(KERN_ERR "SELinux: mls: truncated range\n");
1007 r->level[0].sens = le32_to_cpu(buf[0]);
1009 r->level[1].sens = le32_to_cpu(buf[1]);
1011 r->level[1].sens = r->level[0].sens;
1013 rc = ebitmap_read(&r->level[0].cat, fp);
1015 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
1019 rc = ebitmap_read(&r->level[1].cat, fp);
1021 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1025 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1027 printk(KERN_ERR "SELinux: mls: out of memory\n");
1034 ebitmap_destroy(&r->level[0].cat);
1040 * Read and validate a security context structure
1041 * from a policydb binary representation file.
1043 static int context_read_and_validate(struct context *c,
1050 rc = next_entry(buf, fp, sizeof buf);
1052 printk(KERN_ERR "SELinux: context truncated\n");
1055 c->user = le32_to_cpu(buf[0]);
1056 c->role = le32_to_cpu(buf[1]);
1057 c->type = le32_to_cpu(buf[2]);
1058 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1059 rc = mls_read_range_helper(&c->range, fp);
1061 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1067 if (!policydb_context_isvalid(p, c)) {
1068 printk(KERN_ERR "SELinux: invalid security context\n");
1078 * The following *_read functions are used to
1079 * read the symbol data from a policy database
1080 * binary representation file.
1083 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1086 struct perm_datum *perdatum;
1092 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1096 rc = next_entry(buf, fp, sizeof buf);
1100 len = le32_to_cpu(buf[0]);
1101 perdatum->value = le32_to_cpu(buf[1]);
1104 key = kmalloc(len + 1, GFP_KERNEL);
1108 rc = next_entry(key, fp, len);
1113 rc = hashtab_insert(h, key, perdatum);
1119 perm_destroy(key, perdatum, NULL);
1123 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1126 struct common_datum *comdatum;
1132 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1136 rc = next_entry(buf, fp, sizeof buf);
1140 len = le32_to_cpu(buf[0]);
1141 comdatum->value = le32_to_cpu(buf[1]);
1143 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1146 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1147 nel = le32_to_cpu(buf[3]);
1150 key = kmalloc(len + 1, GFP_KERNEL);
1154 rc = next_entry(key, fp, len);
1159 for (i = 0; i < nel; i++) {
1160 rc = perm_read(p, comdatum->permissions.table, fp);
1165 rc = hashtab_insert(h, key, comdatum);
1170 common_destroy(key, comdatum, NULL);
1174 static void type_set_init(struct type_set *t)
1176 ebitmap_init(&t->types);
1177 ebitmap_init(&t->negset);
1180 static int type_set_read(struct type_set *t, void *fp)
1185 if (ebitmap_read(&t->types, fp))
1187 if (ebitmap_read(&t->negset, fp))
1190 rc = next_entry(buf, fp, sizeof(u32));
1193 t->flags = le32_to_cpu(buf[0]);
1199 static int read_cons_helper(struct policydb *p,
1200 struct constraint_node **nodep,
1201 int ncons, int allowxtarget, void *fp)
1203 struct constraint_node *c, *lc;
1204 struct constraint_expr *e, *le;
1207 int rc, i, j, depth;
1210 for (i = 0; i < ncons; i++) {
1211 c = kzalloc(sizeof(*c), GFP_KERNEL);
1220 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1223 c->permissions = le32_to_cpu(buf[0]);
1224 nexpr = le32_to_cpu(buf[1]);
1227 for (j = 0; j < nexpr; j++) {
1228 e = kzalloc(sizeof(*e), GFP_KERNEL);
1237 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1240 e->expr_type = le32_to_cpu(buf[0]);
1241 e->attr = le32_to_cpu(buf[1]);
1242 e->op = le32_to_cpu(buf[2]);
1244 switch (e->expr_type) {
1256 if (depth == (CEXPR_MAXDEPTH - 1))
1261 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1263 if (depth == (CEXPR_MAXDEPTH - 1))
1266 rc = ebitmap_read(&e->names, fp);
1269 if (p->policyvers >=
1270 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1271 e->type_names = kzalloc(sizeof
1276 type_set_init(e->type_names);
1277 rc = type_set_read(e->type_names, fp);
1295 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1298 struct class_datum *cladatum;
1300 u32 len, len2, ncons, nel;
1304 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1308 rc = next_entry(buf, fp, sizeof(u32)*6);
1312 len = le32_to_cpu(buf[0]);
1313 len2 = le32_to_cpu(buf[1]);
1314 cladatum->value = le32_to_cpu(buf[2]);
1316 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1319 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1320 nel = le32_to_cpu(buf[4]);
1322 ncons = le32_to_cpu(buf[5]);
1325 key = kmalloc(len + 1, GFP_KERNEL);
1329 rc = next_entry(key, fp, len);
1336 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1337 if (!cladatum->comkey)
1339 rc = next_entry(cladatum->comkey, fp, len2);
1342 cladatum->comkey[len2] = '\0';
1345 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1346 if (!cladatum->comdatum) {
1347 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1351 for (i = 0; i < nel; i++) {
1352 rc = perm_read(p, cladatum->permissions.table, fp);
1357 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1361 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1362 /* grab the validatetrans rules */
1363 rc = next_entry(buf, fp, sizeof(u32));
1366 ncons = le32_to_cpu(buf[0]);
1367 rc = read_cons_helper(p, &cladatum->validatetrans,
1373 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1374 rc = next_entry(buf, fp, sizeof(u32) * 3);
1378 cladatum->default_user = le32_to_cpu(buf[0]);
1379 cladatum->default_role = le32_to_cpu(buf[1]);
1380 cladatum->default_range = le32_to_cpu(buf[2]);
1383 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1384 rc = next_entry(buf, fp, sizeof(u32) * 1);
1387 cladatum->default_type = le32_to_cpu(buf[0]);
1390 rc = hashtab_insert(h, key, cladatum);
1396 cls_destroy(key, cladatum, NULL);
1400 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1403 struct role_datum *role;
1404 int rc, to_read = 2;
1409 role = kzalloc(sizeof(*role), GFP_KERNEL);
1413 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1416 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1420 len = le32_to_cpu(buf[0]);
1421 role->value = le32_to_cpu(buf[1]);
1422 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1423 role->bounds = le32_to_cpu(buf[2]);
1426 key = kmalloc(len + 1, GFP_KERNEL);
1430 rc = next_entry(key, fp, len);
1435 rc = ebitmap_read(&role->dominates, fp);
1439 rc = ebitmap_read(&role->types, fp);
1443 if (strcmp(key, OBJECT_R) == 0) {
1445 if (role->value != OBJECT_R_VAL) {
1446 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1447 OBJECT_R, role->value);
1454 rc = hashtab_insert(h, key, role);
1459 role_destroy(key, role, NULL);
1463 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1466 struct type_datum *typdatum;
1467 int rc, to_read = 3;
1472 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1476 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1479 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1483 len = le32_to_cpu(buf[0]);
1484 typdatum->value = le32_to_cpu(buf[1]);
1485 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1486 u32 prop = le32_to_cpu(buf[2]);
1488 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1489 typdatum->primary = 1;
1490 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1491 typdatum->attribute = 1;
1493 typdatum->bounds = le32_to_cpu(buf[3]);
1495 typdatum->primary = le32_to_cpu(buf[2]);
1499 key = kmalloc(len + 1, GFP_KERNEL);
1502 rc = next_entry(key, 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]);
1569 key = kmalloc(len + 1, GFP_KERNEL);
1572 rc = next_entry(key, fp, len);
1577 rc = ebitmap_read(&usrdatum->roles, fp);
1581 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1582 rc = mls_read_range_helper(&usrdatum->range, fp);
1585 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1590 rc = hashtab_insert(h, key, usrdatum);
1595 user_destroy(key, usrdatum, NULL);
1599 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1602 struct level_datum *levdatum;
1608 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1612 rc = next_entry(buf, fp, sizeof buf);
1616 len = le32_to_cpu(buf[0]);
1617 levdatum->isalias = le32_to_cpu(buf[1]);
1620 key = kmalloc(len + 1, GFP_ATOMIC);
1623 rc = next_entry(key, fp, len);
1629 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1630 if (!levdatum->level)
1633 rc = mls_read_level(levdatum->level, fp);
1637 rc = hashtab_insert(h, key, levdatum);
1642 sens_destroy(key, levdatum, NULL);
1646 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1649 struct cat_datum *catdatum;
1655 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1659 rc = next_entry(buf, fp, sizeof buf);
1663 len = le32_to_cpu(buf[0]);
1664 catdatum->value = le32_to_cpu(buf[1]);
1665 catdatum->isalias = le32_to_cpu(buf[2]);
1668 key = kmalloc(len + 1, GFP_ATOMIC);
1671 rc = next_entry(key, fp, len);
1676 rc = hashtab_insert(h, key, catdatum);
1681 cat_destroy(key, catdatum, NULL);
1685 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1697 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1699 struct user_datum *upper, *user;
1700 struct policydb *p = datap;
1703 upper = user = datum;
1704 while (upper->bounds) {
1705 struct ebitmap_node *node;
1708 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1709 printk(KERN_ERR "SELinux: user %s: "
1710 "too deep or looped boundary",
1715 upper = p->user_val_to_struct[upper->bounds - 1];
1716 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1717 if (ebitmap_get_bit(&upper->roles, bit))
1721 "SELinux: boundary violated policy: "
1722 "user=%s role=%s bounds=%s\n",
1723 sym_name(p, SYM_USERS, user->value - 1),
1724 sym_name(p, SYM_ROLES, bit),
1725 sym_name(p, SYM_USERS, upper->value - 1));
1734 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1736 struct role_datum *upper, *role;
1737 struct policydb *p = datap;
1740 upper = role = datum;
1741 while (upper->bounds) {
1742 struct ebitmap_node *node;
1745 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1746 printk(KERN_ERR "SELinux: role %s: "
1747 "too deep or looped bounds\n",
1752 upper = p->role_val_to_struct[upper->bounds - 1];
1753 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1754 if (ebitmap_get_bit(&upper->types, bit))
1758 "SELinux: boundary violated policy: "
1759 "role=%s type=%s bounds=%s\n",
1760 sym_name(p, SYM_ROLES, role->value - 1),
1761 sym_name(p, SYM_TYPES, bit),
1762 sym_name(p, SYM_ROLES, upper->value - 1));
1771 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1773 struct type_datum *upper;
1774 struct policydb *p = datap;
1778 while (upper->bounds) {
1779 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1780 printk(KERN_ERR "SELinux: type %s: "
1781 "too deep or looped boundary\n",
1786 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1790 if (upper->attribute) {
1791 printk(KERN_ERR "SELinux: type %s: "
1792 "bounded by attribute %s",
1794 sym_name(p, SYM_TYPES, upper->value - 1));
1802 static int policydb_bounds_sanity_check(struct policydb *p)
1806 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1809 rc = hashtab_map(p->p_users.table,
1810 user_bounds_sanity_check, p);
1814 rc = hashtab_map(p->p_roles.table,
1815 role_bounds_sanity_check, p);
1819 rc = hashtab_map(p->p_types.table,
1820 type_bounds_sanity_check, p);
1827 u16 string_to_security_class(struct policydb *p, const char *name)
1829 struct class_datum *cladatum;
1831 cladatum = hashtab_search(p->p_classes.table, name);
1835 return cladatum->value;
1838 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1840 struct class_datum *cladatum;
1841 struct perm_datum *perdatum = NULL;
1842 struct common_datum *comdatum;
1844 if (!tclass || tclass > p->p_classes.nprim)
1847 cladatum = p->class_val_to_struct[tclass-1];
1848 comdatum = cladatum->comdatum;
1850 perdatum = hashtab_search(comdatum->permissions.table,
1853 perdatum = hashtab_search(cladatum->permissions.table,
1858 return 1U << (perdatum->value-1);
1861 static int range_read(struct policydb *p, void *fp)
1863 struct range_trans *rt = NULL;
1864 struct mls_range *r = NULL;
1869 if (p->policyvers < POLICYDB_VERSION_MLS)
1872 rc = next_entry(buf, fp, sizeof(u32));
1876 nel = le32_to_cpu(buf[0]);
1877 for (i = 0; i < nel; i++) {
1879 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1883 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1887 rt->source_type = le32_to_cpu(buf[0]);
1888 rt->target_type = le32_to_cpu(buf[1]);
1889 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1890 rc = next_entry(buf, fp, sizeof(u32));
1893 rt->target_class = le32_to_cpu(buf[0]);
1895 rt->target_class = p->process_class;
1898 if (!policydb_type_isvalid(p, rt->source_type) ||
1899 !policydb_type_isvalid(p, rt->target_type) ||
1900 !policydb_class_isvalid(p, rt->target_class))
1904 r = kzalloc(sizeof(*r), GFP_KERNEL);
1908 rc = mls_read_range_helper(r, fp);
1913 if (!mls_range_isvalid(p, r)) {
1914 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1918 rc = hashtab_insert(p->range_tr, rt, r);
1925 hash_eval(p->range_tr, "rangetr");
1933 static int filename_trans_read(struct policydb *p, void *fp)
1935 struct filename_trans *ft;
1936 struct filename_trans_datum *otype;
1942 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1945 rc = next_entry(buf, fp, sizeof(u32));
1948 nel = le32_to_cpu(buf[0]);
1950 for (i = 0; i < nel; i++) {
1956 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1961 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1965 /* length of the path component string */
1966 rc = next_entry(buf, fp, sizeof(u32));
1969 len = le32_to_cpu(buf[0]);
1972 name = kmalloc(len + 1, GFP_KERNEL);
1978 /* path component string */
1979 rc = next_entry(name, fp, len);
1984 rc = next_entry(buf, fp, sizeof(u32) * 4);
1988 ft->stype = le32_to_cpu(buf[0]);
1989 ft->ttype = le32_to_cpu(buf[1]);
1990 ft->tclass = le32_to_cpu(buf[2]);
1992 otype->otype = le32_to_cpu(buf[3]);
1994 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1998 hashtab_insert(p->filename_trans, ft, otype);
2000 hash_eval(p->filename_trans, "filenametr");
2010 static int genfs_read(struct policydb *p, void *fp)
2013 u32 nel, nel2, len, len2;
2015 struct ocontext *l, *c;
2016 struct ocontext *newc = NULL;
2017 struct genfs *genfs_p, *genfs;
2018 struct genfs *newgenfs = NULL;
2020 rc = next_entry(buf, fp, sizeof(u32));
2023 nel = le32_to_cpu(buf[0]);
2025 for (i = 0; i < nel; i++) {
2026 rc = next_entry(buf, fp, sizeof(u32));
2029 len = le32_to_cpu(buf[0]);
2032 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2037 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
2038 if (!newgenfs->fstype)
2041 rc = next_entry(newgenfs->fstype, fp, len);
2045 newgenfs->fstype[len] = 0;
2047 for (genfs_p = NULL, genfs = p->genfs; genfs;
2048 genfs_p = genfs, genfs = genfs->next) {
2050 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2051 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2055 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2058 newgenfs->next = genfs;
2060 genfs_p->next = newgenfs;
2062 p->genfs = newgenfs;
2066 rc = next_entry(buf, fp, sizeof(u32));
2070 nel2 = le32_to_cpu(buf[0]);
2071 for (j = 0; j < nel2; j++) {
2072 rc = next_entry(buf, fp, sizeof(u32));
2075 len = le32_to_cpu(buf[0]);
2078 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2083 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2087 rc = next_entry(newc->u.name, fp, len);
2090 newc->u.name[len] = 0;
2092 rc = next_entry(buf, fp, sizeof(u32));
2096 newc->v.sclass = le32_to_cpu(buf[0]);
2097 rc = context_read_and_validate(&newc->context[0], p, fp);
2101 for (l = NULL, c = genfs->head; c;
2102 l = c, c = c->next) {
2104 if (!strcmp(newc->u.name, c->u.name) &&
2105 (!c->v.sclass || !newc->v.sclass ||
2106 newc->v.sclass == c->v.sclass)) {
2107 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2108 genfs->fstype, c->u.name);
2111 len = strlen(newc->u.name);
2112 len2 = strlen(c->u.name);
2128 kfree(newgenfs->fstype);
2130 ocontext_destroy(newc, OCON_FSUSE);
2135 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2141 struct ocontext *l, *c;
2144 for (i = 0; i < info->ocon_num; i++) {
2145 rc = next_entry(buf, fp, sizeof(u32));
2148 nel = le32_to_cpu(buf[0]);
2151 for (j = 0; j < nel; j++) {
2153 c = kzalloc(sizeof(*c), GFP_KERNEL);
2159 p->ocontexts[i] = c;
2164 rc = next_entry(buf, fp, sizeof(u32));
2168 c->sid[0] = le32_to_cpu(buf[0]);
2169 rc = context_read_and_validate(&c->context[0], p, fp);
2175 rc = next_entry(buf, fp, sizeof(u32));
2178 len = le32_to_cpu(buf[0]);
2181 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2185 rc = next_entry(c->u.name, fp, len);
2190 rc = context_read_and_validate(&c->context[0], p, fp);
2193 rc = context_read_and_validate(&c->context[1], p, fp);
2198 rc = next_entry(buf, fp, sizeof(u32)*3);
2201 c->u.port.protocol = le32_to_cpu(buf[0]);
2202 c->u.port.low_port = le32_to_cpu(buf[1]);
2203 c->u.port.high_port = le32_to_cpu(buf[2]);
2204 rc = context_read_and_validate(&c->context[0], p, fp);
2209 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2212 c->u.node.addr = nodebuf[0]; /* network order */
2213 c->u.node.mask = nodebuf[1]; /* network order */
2214 rc = context_read_and_validate(&c->context[0], p, fp);
2219 rc = next_entry(buf, fp, sizeof(u32)*2);
2224 c->v.behavior = le32_to_cpu(buf[0]);
2225 if (c->v.behavior > SECURITY_FS_USE_NONE)
2229 len = le32_to_cpu(buf[1]);
2230 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2234 rc = next_entry(c->u.name, fp, len);
2238 rc = context_read_and_validate(&c->context[0], p, fp);
2245 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2248 for (k = 0; k < 4; k++)
2249 c->u.node6.addr[k] = nodebuf[k];
2250 for (k = 0; k < 4; k++)
2251 c->u.node6.mask[k] = nodebuf[k+4];
2252 rc = context_read_and_validate(&c->context[0], p, fp);
2266 * Read the configuration data from a policy database binary
2267 * representation file into a policy database structure.
2269 int policydb_read(struct policydb *p, void *fp)
2271 struct role_allow *ra, *lra;
2272 struct role_trans *tr, *ltr;
2275 u32 len, nprim, nel;
2278 struct policydb_compat_info *info;
2280 rc = policydb_init(p);
2284 /* Read the magic number and string length. */
2285 rc = next_entry(buf, fp, sizeof(u32) * 2);
2290 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2291 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2292 "not match expected magic number 0x%x\n",
2293 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2298 len = le32_to_cpu(buf[1]);
2299 if (len != strlen(POLICYDB_STRING)) {
2300 printk(KERN_ERR "SELinux: policydb string length %d does not "
2301 "match expected length %Zu\n",
2302 len, strlen(POLICYDB_STRING));
2307 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2308 if (!policydb_str) {
2309 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2310 "string of length %d\n", len);
2314 rc = next_entry(policydb_str, fp, len);
2316 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2317 kfree(policydb_str);
2322 policydb_str[len] = '\0';
2323 if (strcmp(policydb_str, POLICYDB_STRING)) {
2324 printk(KERN_ERR "SELinux: policydb string %s does not match "
2325 "my string %s\n", policydb_str, POLICYDB_STRING);
2326 kfree(policydb_str);
2329 /* Done with policydb_str. */
2330 kfree(policydb_str);
2331 policydb_str = NULL;
2333 /* Read the version and table sizes. */
2334 rc = next_entry(buf, fp, sizeof(u32)*4);
2339 p->policyvers = le32_to_cpu(buf[0]);
2340 if (p->policyvers < POLICYDB_VERSION_MIN ||
2341 p->policyvers > POLICYDB_VERSION_MAX) {
2342 printk(KERN_ERR "SELinux: policydb version %d does not match "
2343 "my version range %d-%d\n",
2344 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2348 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2352 if (p->policyvers < POLICYDB_VERSION_MLS) {
2353 printk(KERN_ERR "SELinux: security policydb version %d "
2354 "(MLS) not backwards compatible\n",
2359 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2360 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2362 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2363 rc = ebitmap_read(&p->policycaps, fp);
2368 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2369 rc = ebitmap_read(&p->permissive_map, fp);
2375 info = policydb_lookup_compat(p->policyvers);
2377 printk(KERN_ERR "SELinux: unable to find policy compat info "
2378 "for version %d\n", p->policyvers);
2383 if (le32_to_cpu(buf[2]) != info->sym_num ||
2384 le32_to_cpu(buf[3]) != info->ocon_num) {
2385 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2386 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2387 le32_to_cpu(buf[3]),
2388 info->sym_num, info->ocon_num);
2392 for (i = 0; i < info->sym_num; i++) {
2393 rc = next_entry(buf, fp, sizeof(u32)*2);
2396 nprim = le32_to_cpu(buf[0]);
2397 nel = le32_to_cpu(buf[1]);
2398 for (j = 0; j < nel; j++) {
2399 rc = read_f[i](p, p->symtab[i].table, fp);
2404 p->symtab[i].nprim = nprim;
2408 p->process_class = string_to_security_class(p, "process");
2409 if (!p->process_class)
2412 rc = avtab_read(&p->te_avtab, fp, p);
2416 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2417 rc = cond_read_list(p, fp);
2422 rc = next_entry(buf, fp, sizeof(u32));
2425 nel = le32_to_cpu(buf[0]);
2427 for (i = 0; i < nel; i++) {
2429 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2436 rc = next_entry(buf, fp, sizeof(u32)*3);
2441 tr->role = le32_to_cpu(buf[0]);
2442 tr->type = le32_to_cpu(buf[1]);
2443 tr->new_role = le32_to_cpu(buf[2]);
2444 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2445 rc = next_entry(buf, fp, sizeof(u32));
2448 tr->tclass = le32_to_cpu(buf[0]);
2450 tr->tclass = p->process_class;
2452 if (!policydb_role_isvalid(p, tr->role) ||
2453 !policydb_type_isvalid(p, tr->type) ||
2454 !policydb_class_isvalid(p, tr->tclass) ||
2455 !policydb_role_isvalid(p, tr->new_role))
2460 rc = next_entry(buf, fp, sizeof(u32));
2463 nel = le32_to_cpu(buf[0]);
2465 for (i = 0; i < nel; i++) {
2467 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2474 rc = next_entry(buf, fp, sizeof(u32)*2);
2479 ra->role = le32_to_cpu(buf[0]);
2480 ra->new_role = le32_to_cpu(buf[1]);
2481 if (!policydb_role_isvalid(p, ra->role) ||
2482 !policydb_role_isvalid(p, ra->new_role))
2487 rc = filename_trans_read(p, fp);
2491 rc = policydb_index(p);
2496 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2497 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2498 if (!p->process_trans_perms)
2501 rc = ocontext_read(p, info, fp);
2505 rc = genfs_read(p, fp);
2509 rc = range_read(p, fp);
2514 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2516 GFP_KERNEL | __GFP_ZERO);
2517 if (!p->type_attr_map_array)
2520 /* preallocate so we don't have to worry about the put ever failing */
2521 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2522 GFP_KERNEL | __GFP_ZERO);
2526 for (i = 0; i < p->p_types.nprim; i++) {
2527 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2531 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2532 rc = ebitmap_read(e, fp);
2536 /* add the type itself as the degenerate case */
2537 rc = ebitmap_set_bit(e, i, 1);
2542 rc = policydb_bounds_sanity_check(p);
2550 policydb_destroy(p);
2555 * Write a MLS level structure to a policydb binary
2556 * representation file.
2558 static int mls_write_level(struct mls_level *l, void *fp)
2563 buf[0] = cpu_to_le32(l->sens);
2564 rc = put_entry(buf, sizeof(u32), 1, fp);
2568 rc = ebitmap_write(&l->cat, fp);
2576 * Write a MLS range structure to a policydb binary
2577 * representation file.
2579 static int mls_write_range_helper(struct mls_range *r, void *fp)
2585 eq = mls_level_eq(&r->level[1], &r->level[0]);
2591 buf[0] = cpu_to_le32(items-1);
2592 buf[1] = cpu_to_le32(r->level[0].sens);
2594 buf[2] = cpu_to_le32(r->level[1].sens);
2596 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2598 rc = put_entry(buf, sizeof(u32), items, fp);
2602 rc = ebitmap_write(&r->level[0].cat, fp);
2606 rc = ebitmap_write(&r->level[1].cat, fp);
2614 static int sens_write(void *vkey, void *datum, void *ptr)
2617 struct level_datum *levdatum = datum;
2618 struct policy_data *pd = ptr;
2625 buf[0] = cpu_to_le32(len);
2626 buf[1] = cpu_to_le32(levdatum->isalias);
2627 rc = put_entry(buf, sizeof(u32), 2, fp);
2631 rc = put_entry(key, 1, len, fp);
2635 rc = mls_write_level(levdatum->level, fp);
2642 static int cat_write(void *vkey, void *datum, void *ptr)
2645 struct cat_datum *catdatum = datum;
2646 struct policy_data *pd = ptr;
2653 buf[0] = cpu_to_le32(len);
2654 buf[1] = cpu_to_le32(catdatum->value);
2655 buf[2] = cpu_to_le32(catdatum->isalias);
2656 rc = put_entry(buf, sizeof(u32), 3, fp);
2660 rc = put_entry(key, 1, len, fp);
2667 static int role_trans_write(struct policydb *p, void *fp)
2669 struct role_trans *r = p->role_tr;
2670 struct role_trans *tr;
2676 for (tr = r; tr; tr = tr->next)
2678 buf[0] = cpu_to_le32(nel);
2679 rc = put_entry(buf, sizeof(u32), 1, fp);
2682 for (tr = r; tr; tr = tr->next) {
2683 buf[0] = cpu_to_le32(tr->role);
2684 buf[1] = cpu_to_le32(tr->type);
2685 buf[2] = cpu_to_le32(tr->new_role);
2686 rc = put_entry(buf, sizeof(u32), 3, fp);
2689 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2690 buf[0] = cpu_to_le32(tr->tclass);
2691 rc = put_entry(buf, sizeof(u32), 1, fp);
2700 static int role_allow_write(struct role_allow *r, void *fp)
2702 struct role_allow *ra;
2708 for (ra = r; ra; ra = ra->next)
2710 buf[0] = cpu_to_le32(nel);
2711 rc = put_entry(buf, sizeof(u32), 1, fp);
2714 for (ra = r; ra; ra = ra->next) {
2715 buf[0] = cpu_to_le32(ra->role);
2716 buf[1] = cpu_to_le32(ra->new_role);
2717 rc = put_entry(buf, sizeof(u32), 2, fp);
2725 * Write a security context structure
2726 * to a policydb binary representation file.
2728 static int context_write(struct policydb *p, struct context *c,
2734 buf[0] = cpu_to_le32(c->user);
2735 buf[1] = cpu_to_le32(c->role);
2736 buf[2] = cpu_to_le32(c->type);
2738 rc = put_entry(buf, sizeof(u32), 3, fp);
2742 rc = mls_write_range_helper(&c->range, fp);
2750 * The following *_write functions are used to
2751 * write the symbol data to a policy database
2752 * binary representation file.
2755 static int perm_write(void *vkey, void *datum, void *fp)
2758 struct perm_datum *perdatum = datum;
2764 buf[0] = cpu_to_le32(len);
2765 buf[1] = cpu_to_le32(perdatum->value);
2766 rc = put_entry(buf, sizeof(u32), 2, fp);
2770 rc = put_entry(key, 1, len, fp);
2777 static int common_write(void *vkey, void *datum, void *ptr)
2780 struct common_datum *comdatum = datum;
2781 struct policy_data *pd = ptr;
2788 buf[0] = cpu_to_le32(len);
2789 buf[1] = cpu_to_le32(comdatum->value);
2790 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2791 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2792 rc = put_entry(buf, sizeof(u32), 4, fp);
2796 rc = put_entry(key, 1, len, fp);
2800 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2807 static int type_set_write(struct type_set *t, void *fp)
2812 if (ebitmap_write(&t->types, fp))
2814 if (ebitmap_write(&t->negset, fp))
2817 buf[0] = cpu_to_le32(t->flags);
2818 rc = put_entry(buf, sizeof(u32), 1, fp);
2825 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2828 struct constraint_node *c;
2829 struct constraint_expr *e;
2834 for (c = node; c; c = c->next) {
2836 for (e = c->expr; e; e = e->next)
2838 buf[0] = cpu_to_le32(c->permissions);
2839 buf[1] = cpu_to_le32(nel);
2840 rc = put_entry(buf, sizeof(u32), 2, fp);
2843 for (e = c->expr; e; e = e->next) {
2844 buf[0] = cpu_to_le32(e->expr_type);
2845 buf[1] = cpu_to_le32(e->attr);
2846 buf[2] = cpu_to_le32(e->op);
2847 rc = put_entry(buf, sizeof(u32), 3, fp);
2851 switch (e->expr_type) {
2853 rc = ebitmap_write(&e->names, fp);
2856 if (p->policyvers >=
2857 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2858 rc = type_set_write(e->type_names, fp);
2872 static int class_write(void *vkey, void *datum, void *ptr)
2875 struct class_datum *cladatum = datum;
2876 struct policy_data *pd = ptr;
2878 struct policydb *p = pd->p;
2879 struct constraint_node *c;
2886 if (cladatum->comkey)
2887 len2 = strlen(cladatum->comkey);
2892 for (c = cladatum->constraints; c; c = c->next)
2895 buf[0] = cpu_to_le32(len);
2896 buf[1] = cpu_to_le32(len2);
2897 buf[2] = cpu_to_le32(cladatum->value);
2898 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2899 if (cladatum->permissions.table)
2900 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2903 buf[5] = cpu_to_le32(ncons);
2904 rc = put_entry(buf, sizeof(u32), 6, fp);
2908 rc = put_entry(key, 1, len, fp);
2912 if (cladatum->comkey) {
2913 rc = put_entry(cladatum->comkey, 1, len2, fp);
2918 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2922 rc = write_cons_helper(p, cladatum->constraints, fp);
2926 /* write out the validatetrans rule */
2928 for (c = cladatum->validatetrans; c; c = c->next)
2931 buf[0] = cpu_to_le32(ncons);
2932 rc = put_entry(buf, sizeof(u32), 1, fp);
2936 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2940 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2941 buf[0] = cpu_to_le32(cladatum->default_user);
2942 buf[1] = cpu_to_le32(cladatum->default_role);
2943 buf[2] = cpu_to_le32(cladatum->default_range);
2945 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2950 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2951 buf[0] = cpu_to_le32(cladatum->default_type);
2952 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2960 static int role_write(void *vkey, void *datum, void *ptr)
2963 struct role_datum *role = datum;
2964 struct policy_data *pd = ptr;
2966 struct policydb *p = pd->p;
2973 buf[items++] = cpu_to_le32(len);
2974 buf[items++] = cpu_to_le32(role->value);
2975 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2976 buf[items++] = cpu_to_le32(role->bounds);
2978 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2980 rc = put_entry(buf, sizeof(u32), items, fp);
2984 rc = put_entry(key, 1, len, fp);
2988 rc = ebitmap_write(&role->dominates, fp);
2992 rc = ebitmap_write(&role->types, fp);
2999 static int type_write(void *vkey, void *datum, void *ptr)
3002 struct type_datum *typdatum = 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(typdatum->value);
3014 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3017 if (typdatum->primary)
3018 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3020 if (typdatum->attribute)
3021 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3023 buf[items++] = cpu_to_le32(properties);
3024 buf[items++] = cpu_to_le32(typdatum->bounds);
3026 buf[items++] = cpu_to_le32(typdatum->primary);
3028 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
3029 rc = put_entry(buf, sizeof(u32), items, fp);
3033 rc = put_entry(key, 1, len, fp);
3040 static int user_write(void *vkey, void *datum, void *ptr)
3043 struct user_datum *usrdatum = datum;
3044 struct policy_data *pd = ptr;
3045 struct policydb *p = pd->p;
3053 buf[items++] = cpu_to_le32(len);
3054 buf[items++] = cpu_to_le32(usrdatum->value);
3055 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3056 buf[items++] = cpu_to_le32(usrdatum->bounds);
3057 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
3058 rc = put_entry(buf, sizeof(u32), items, fp);
3062 rc = put_entry(key, 1, len, fp);
3066 rc = ebitmap_write(&usrdatum->roles, fp);
3070 rc = mls_write_range_helper(&usrdatum->range, fp);
3074 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3081 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3094 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3097 unsigned int i, j, rc;
3102 for (i = 0; i < info->ocon_num; i++) {
3104 for (c = p->ocontexts[i]; c; c = c->next)
3106 buf[0] = cpu_to_le32(nel);
3107 rc = put_entry(buf, sizeof(u32), 1, fp);
3110 for (c = p->ocontexts[i]; c; c = c->next) {
3113 buf[0] = cpu_to_le32(c->sid[0]);
3114 rc = put_entry(buf, sizeof(u32), 1, fp);
3117 rc = context_write(p, &c->context[0], fp);
3123 len = strlen(c->u.name);
3124 buf[0] = cpu_to_le32(len);
3125 rc = put_entry(buf, sizeof(u32), 1, fp);
3128 rc = put_entry(c->u.name, 1, len, fp);
3131 rc = context_write(p, &c->context[0], fp);
3134 rc = context_write(p, &c->context[1], fp);
3139 buf[0] = cpu_to_le32(c->u.port.protocol);
3140 buf[1] = cpu_to_le32(c->u.port.low_port);
3141 buf[2] = cpu_to_le32(c->u.port.high_port);
3142 rc = put_entry(buf, sizeof(u32), 3, fp);
3145 rc = context_write(p, &c->context[0], fp);
3150 nodebuf[0] = c->u.node.addr; /* network order */
3151 nodebuf[1] = c->u.node.mask; /* network order */
3152 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3155 rc = context_write(p, &c->context[0], fp);
3160 buf[0] = cpu_to_le32(c->v.behavior);
3161 len = strlen(c->u.name);
3162 buf[1] = cpu_to_le32(len);
3163 rc = put_entry(buf, sizeof(u32), 2, fp);
3166 rc = put_entry(c->u.name, 1, len, fp);
3169 rc = context_write(p, &c->context[0], fp);
3174 for (j = 0; j < 4; j++)
3175 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3176 for (j = 0; j < 4; j++)
3177 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3178 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3181 rc = context_write(p, &c->context[0], fp);
3191 static int genfs_write(struct policydb *p, void *fp)
3193 struct genfs *genfs;
3200 for (genfs = p->genfs; genfs; genfs = genfs->next)
3202 buf[0] = cpu_to_le32(len);
3203 rc = put_entry(buf, sizeof(u32), 1, fp);
3206 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3207 len = strlen(genfs->fstype);
3208 buf[0] = cpu_to_le32(len);
3209 rc = put_entry(buf, sizeof(u32), 1, fp);
3212 rc = put_entry(genfs->fstype, 1, len, fp);
3216 for (c = genfs->head; c; c = c->next)
3218 buf[0] = cpu_to_le32(len);
3219 rc = put_entry(buf, sizeof(u32), 1, fp);
3222 for (c = genfs->head; c; c = c->next) {
3223 len = strlen(c->u.name);
3224 buf[0] = cpu_to_le32(len);
3225 rc = put_entry(buf, sizeof(u32), 1, fp);
3228 rc = put_entry(c->u.name, 1, len, fp);
3231 buf[0] = cpu_to_le32(c->v.sclass);
3232 rc = put_entry(buf, sizeof(u32), 1, fp);
3235 rc = context_write(p, &c->context[0], fp);
3243 static int hashtab_cnt(void *key, void *data, void *ptr)
3251 static int range_write_helper(void *key, void *data, void *ptr)
3254 struct range_trans *rt = key;
3255 struct mls_range *r = data;
3256 struct policy_data *pd = ptr;
3258 struct policydb *p = pd->p;
3261 buf[0] = cpu_to_le32(rt->source_type);
3262 buf[1] = cpu_to_le32(rt->target_type);
3263 rc = put_entry(buf, sizeof(u32), 2, fp);
3266 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3267 buf[0] = cpu_to_le32(rt->target_class);
3268 rc = put_entry(buf, sizeof(u32), 1, fp);
3272 rc = mls_write_range_helper(r, fp);
3279 static int range_write(struct policydb *p, void *fp)
3284 struct policy_data pd;
3289 /* count the number of entries in the hashtab */
3291 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3295 buf[0] = cpu_to_le32(nel);
3296 rc = put_entry(buf, sizeof(u32), 1, fp);
3300 /* actually write all of the entries */
3301 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3308 static int filename_write_helper(void *key, void *data, void *ptr)
3311 struct filename_trans *ft = key;
3312 struct filename_trans_datum *otype = data;
3317 len = strlen(ft->name);
3318 buf[0] = cpu_to_le32(len);
3319 rc = put_entry(buf, sizeof(u32), 1, fp);
3323 rc = put_entry(ft->name, sizeof(char), len, fp);
3329 buf[2] = ft->tclass;
3330 buf[3] = otype->otype;
3332 rc = put_entry(buf, sizeof(u32), 4, fp);
3339 static int filename_trans_write(struct policydb *p, void *fp)
3345 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3349 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3353 buf[0] = cpu_to_le32(nel);
3354 rc = put_entry(buf, sizeof(u32), 1, fp);
3358 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3366 * Write the configuration data in a policy database
3367 * structure to a policy database binary representation
3370 int policydb_write(struct policydb *p, void *fp)
3372 unsigned int i, num_syms;
3377 struct policydb_compat_info *info;
3380 * refuse to write policy older than compressed avtab
3381 * to simplify the writer. There are other tests dropped
3382 * since we assume this throughout the writer code. Be
3383 * careful if you ever try to remove this restriction
3385 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3386 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3387 " Because it is less than version %d\n", p->policyvers,
3388 POLICYDB_VERSION_AVTAB);
3394 config |= POLICYDB_CONFIG_MLS;
3396 if (p->reject_unknown)
3397 config |= REJECT_UNKNOWN;
3398 if (p->allow_unknown)
3399 config |= ALLOW_UNKNOWN;
3401 /* Write the magic number and string identifiers. */
3402 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3403 len = strlen(POLICYDB_STRING);
3404 buf[1] = cpu_to_le32(len);
3405 rc = put_entry(buf, sizeof(u32), 2, fp);
3408 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3412 /* Write the version, config, and table sizes. */
3413 info = policydb_lookup_compat(p->policyvers);
3415 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3416 "version %d", p->policyvers);
3420 buf[0] = cpu_to_le32(p->policyvers);
3421 buf[1] = cpu_to_le32(config);
3422 buf[2] = cpu_to_le32(info->sym_num);
3423 buf[3] = cpu_to_le32(info->ocon_num);
3425 rc = put_entry(buf, sizeof(u32), 4, fp);
3429 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3430 rc = ebitmap_write(&p->policycaps, fp);
3435 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3436 rc = ebitmap_write(&p->permissive_map, fp);
3441 num_syms = info->sym_num;
3442 for (i = 0; i < num_syms; i++) {
3443 struct policy_data pd;
3448 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3449 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3451 rc = put_entry(buf, sizeof(u32), 2, fp);
3454 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3459 rc = avtab_write(p, &p->te_avtab, fp);
3463 rc = cond_write_list(p, p->cond_list, fp);
3467 rc = role_trans_write(p, fp);
3471 rc = role_allow_write(p->role_allow, fp);
3475 rc = filename_trans_write(p, fp);
3479 rc = ocontext_write(p, info, fp);
3483 rc = genfs_write(p, fp);
3487 rc = range_write(p, fp);
3491 for (i = 0; i < p->p_types.nprim; i++) {
3492 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3495 rc = ebitmap_write(e, fp);