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(", %d sens, %d cats", p->p_levels.nprim,
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 =
544 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
546 if (!p->class_val_to_struct)
550 p->role_val_to_struct =
551 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
553 if (!p->role_val_to_struct)
557 p->user_val_to_struct =
558 kmalloc(p->p_users.nprim * 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);
888 * Load the initial SIDs specified in a policy database
889 * structure into a SID table.
891 int policydb_load_isids(struct policydb *p, struct sidtab *s)
893 struct ocontext *head, *c;
898 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
902 head = p->ocontexts[OCON_ISID];
903 for (c = head; c; c = c->next) {
905 if (!c->context[0].user) {
906 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
911 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
913 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
923 int policydb_class_isvalid(struct policydb *p, unsigned int class)
925 if (!class || class > p->p_classes.nprim)
930 int policydb_role_isvalid(struct policydb *p, unsigned int role)
932 if (!role || role > p->p_roles.nprim)
937 int policydb_type_isvalid(struct policydb *p, unsigned int type)
939 if (!type || type > p->p_types.nprim)
945 * Return 1 if the fields in the security context
946 * structure `c' are valid. Return 0 otherwise.
948 int policydb_context_isvalid(struct policydb *p, struct context *c)
950 struct role_datum *role;
951 struct user_datum *usrdatum;
953 if (!c->role || c->role > p->p_roles.nprim)
956 if (!c->user || c->user > p->p_users.nprim)
959 if (!c->type || c->type > p->p_types.nprim)
962 if (c->role != OBJECT_R_VAL) {
964 * Role must be authorized for the type.
966 role = p->role_val_to_struct[c->role - 1];
967 if (!ebitmap_get_bit(&role->types, c->type - 1))
968 /* role may not be associated with type */
972 * User must be authorized for the role.
974 usrdatum = p->user_val_to_struct[c->user - 1];
978 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
979 /* user may not be associated with role */
983 if (!mls_context_isvalid(p, c))
990 * Read a MLS range structure from a policydb binary
991 * representation file.
993 static int mls_read_range_helper(struct mls_range *r, void *fp)
999 rc = next_entry(buf, fp, sizeof(u32));
1004 items = le32_to_cpu(buf[0]);
1005 if (items > ARRAY_SIZE(buf)) {
1006 printk(KERN_ERR "SELinux: mls: range overflow\n");
1010 rc = next_entry(buf, fp, sizeof(u32) * items);
1012 printk(KERN_ERR "SELinux: mls: truncated range\n");
1016 r->level[0].sens = le32_to_cpu(buf[0]);
1018 r->level[1].sens = le32_to_cpu(buf[1]);
1020 r->level[1].sens = r->level[0].sens;
1022 rc = ebitmap_read(&r->level[0].cat, fp);
1024 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
1028 rc = ebitmap_read(&r->level[1].cat, fp);
1030 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1034 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1036 printk(KERN_ERR "SELinux: mls: out of memory\n");
1043 ebitmap_destroy(&r->level[0].cat);
1049 * Read and validate a security context structure
1050 * from a policydb binary representation file.
1052 static int context_read_and_validate(struct context *c,
1059 rc = next_entry(buf, fp, sizeof buf);
1061 printk(KERN_ERR "SELinux: context truncated\n");
1064 c->user = le32_to_cpu(buf[0]);
1065 c->role = le32_to_cpu(buf[1]);
1066 c->type = le32_to_cpu(buf[2]);
1067 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1068 rc = mls_read_range_helper(&c->range, fp);
1070 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1076 if (!policydb_context_isvalid(p, c)) {
1077 printk(KERN_ERR "SELinux: invalid security context\n");
1087 * The following *_read functions are used to
1088 * read the symbol data from a policy database
1089 * binary representation file.
1092 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1097 str = kmalloc(len + 1, flags);
1101 /* it's expected the caller should free the str */
1104 rc = next_entry(str, fp, len);
1112 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1115 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;
1155 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1159 rc = next_entry(buf, fp, sizeof buf);
1163 len = le32_to_cpu(buf[0]);
1164 comdatum->value = le32_to_cpu(buf[1]);
1166 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1169 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1170 nel = le32_to_cpu(buf[3]);
1172 rc = str_read(&key, GFP_KERNEL, fp, len);
1176 for (i = 0; i < nel; i++) {
1177 rc = perm_read(p, comdatum->permissions.table, fp);
1182 rc = hashtab_insert(h, key, comdatum);
1187 common_destroy(key, comdatum, NULL);
1191 static void type_set_init(struct type_set *t)
1193 ebitmap_init(&t->types);
1194 ebitmap_init(&t->negset);
1197 static int type_set_read(struct type_set *t, void *fp)
1202 if (ebitmap_read(&t->types, fp))
1204 if (ebitmap_read(&t->negset, fp))
1207 rc = next_entry(buf, fp, sizeof(u32));
1210 t->flags = le32_to_cpu(buf[0]);
1216 static int read_cons_helper(struct policydb *p,
1217 struct constraint_node **nodep,
1218 int ncons, int allowxtarget, void *fp)
1220 struct constraint_node *c, *lc;
1221 struct constraint_expr *e, *le;
1224 int rc, i, j, depth;
1227 for (i = 0; i < ncons; i++) {
1228 c = kzalloc(sizeof(*c), GFP_KERNEL);
1237 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1240 c->permissions = le32_to_cpu(buf[0]);
1241 nexpr = le32_to_cpu(buf[1]);
1244 for (j = 0; j < nexpr; j++) {
1245 e = kzalloc(sizeof(*e), GFP_KERNEL);
1254 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1257 e->expr_type = le32_to_cpu(buf[0]);
1258 e->attr = le32_to_cpu(buf[1]);
1259 e->op = le32_to_cpu(buf[2]);
1261 switch (e->expr_type) {
1273 if (depth == (CEXPR_MAXDEPTH - 1))
1278 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1280 if (depth == (CEXPR_MAXDEPTH - 1))
1283 rc = ebitmap_read(&e->names, fp);
1286 if (p->policyvers >=
1287 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1288 e->type_names = kzalloc(sizeof
1293 type_set_init(e->type_names);
1294 rc = type_set_read(e->type_names, fp);
1312 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1315 struct class_datum *cladatum;
1317 u32 len, len2, ncons, nel;
1321 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1325 rc = next_entry(buf, fp, sizeof(u32)*6);
1329 len = le32_to_cpu(buf[0]);
1330 len2 = le32_to_cpu(buf[1]);
1331 cladatum->value = le32_to_cpu(buf[2]);
1333 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1336 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1337 nel = le32_to_cpu(buf[4]);
1339 ncons = le32_to_cpu(buf[5]);
1341 rc = str_read(&key, GFP_KERNEL, fp, len);
1346 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1351 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1352 if (!cladatum->comdatum) {
1353 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1357 for (i = 0; i < nel; i++) {
1358 rc = perm_read(p, cladatum->permissions.table, fp);
1363 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1367 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1368 /* grab the validatetrans rules */
1369 rc = next_entry(buf, fp, sizeof(u32));
1372 ncons = le32_to_cpu(buf[0]);
1373 rc = read_cons_helper(p, &cladatum->validatetrans,
1379 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1380 rc = next_entry(buf, fp, sizeof(u32) * 3);
1384 cladatum->default_user = le32_to_cpu(buf[0]);
1385 cladatum->default_role = le32_to_cpu(buf[1]);
1386 cladatum->default_range = le32_to_cpu(buf[2]);
1389 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1390 rc = next_entry(buf, fp, sizeof(u32) * 1);
1393 cladatum->default_type = le32_to_cpu(buf[0]);
1396 rc = hashtab_insert(h, key, cladatum);
1402 cls_destroy(key, cladatum, NULL);
1406 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1409 struct role_datum *role;
1410 int rc, to_read = 2;
1415 role = kzalloc(sizeof(*role), GFP_KERNEL);
1419 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1422 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1426 len = le32_to_cpu(buf[0]);
1427 role->value = le32_to_cpu(buf[1]);
1428 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1429 role->bounds = le32_to_cpu(buf[2]);
1431 rc = str_read(&key, GFP_KERNEL, 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]);
1498 rc = str_read(&key, GFP_KERNEL, fp, len);
1502 rc = hashtab_insert(h, key, typdatum);
1507 type_destroy(key, typdatum, NULL);
1513 * Read a MLS level structure from a policydb binary
1514 * representation file.
1516 static int mls_read_level(struct mls_level *lp, void *fp)
1521 memset(lp, 0, sizeof(*lp));
1523 rc = next_entry(buf, fp, sizeof buf);
1525 printk(KERN_ERR "SELinux: mls: truncated level\n");
1528 lp->sens = le32_to_cpu(buf[0]);
1530 rc = ebitmap_read(&lp->cat, fp);
1532 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1538 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1541 struct user_datum *usrdatum;
1542 int rc, to_read = 2;
1547 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1551 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1554 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1558 len = le32_to_cpu(buf[0]);
1559 usrdatum->value = le32_to_cpu(buf[1]);
1560 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1561 usrdatum->bounds = le32_to_cpu(buf[2]);
1563 rc = str_read(&key, GFP_KERNEL, fp, len);
1567 rc = ebitmap_read(&usrdatum->roles, fp);
1571 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1572 rc = mls_read_range_helper(&usrdatum->range, fp);
1575 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1580 rc = hashtab_insert(h, key, usrdatum);
1585 user_destroy(key, usrdatum, NULL);
1589 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1592 struct level_datum *levdatum;
1598 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1602 rc = next_entry(buf, fp, sizeof buf);
1606 len = le32_to_cpu(buf[0]);
1607 levdatum->isalias = le32_to_cpu(buf[1]);
1609 rc = str_read(&key, GFP_ATOMIC, fp, len);
1614 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1615 if (!levdatum->level)
1618 rc = mls_read_level(levdatum->level, fp);
1622 rc = hashtab_insert(h, key, levdatum);
1627 sens_destroy(key, levdatum, NULL);
1631 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1634 struct cat_datum *catdatum;
1640 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1644 rc = next_entry(buf, fp, sizeof buf);
1648 len = le32_to_cpu(buf[0]);
1649 catdatum->value = le32_to_cpu(buf[1]);
1650 catdatum->isalias = le32_to_cpu(buf[2]);
1652 rc = str_read(&key, GFP_ATOMIC, fp, len);
1656 rc = hashtab_insert(h, key, catdatum);
1661 cat_destroy(key, catdatum, NULL);
1665 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1677 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1679 struct user_datum *upper, *user;
1680 struct policydb *p = datap;
1683 upper = user = datum;
1684 while (upper->bounds) {
1685 struct ebitmap_node *node;
1688 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1689 printk(KERN_ERR "SELinux: user %s: "
1690 "too deep or looped boundary",
1695 upper = p->user_val_to_struct[upper->bounds - 1];
1696 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1697 if (ebitmap_get_bit(&upper->roles, bit))
1701 "SELinux: boundary violated policy: "
1702 "user=%s role=%s bounds=%s\n",
1703 sym_name(p, SYM_USERS, user->value - 1),
1704 sym_name(p, SYM_ROLES, bit),
1705 sym_name(p, SYM_USERS, upper->value - 1));
1714 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1716 struct role_datum *upper, *role;
1717 struct policydb *p = datap;
1720 upper = role = datum;
1721 while (upper->bounds) {
1722 struct ebitmap_node *node;
1725 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1726 printk(KERN_ERR "SELinux: role %s: "
1727 "too deep or looped bounds\n",
1732 upper = p->role_val_to_struct[upper->bounds - 1];
1733 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1734 if (ebitmap_get_bit(&upper->types, bit))
1738 "SELinux: boundary violated policy: "
1739 "role=%s type=%s bounds=%s\n",
1740 sym_name(p, SYM_ROLES, role->value - 1),
1741 sym_name(p, SYM_TYPES, bit),
1742 sym_name(p, SYM_ROLES, upper->value - 1));
1751 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1753 struct type_datum *upper;
1754 struct policydb *p = datap;
1758 while (upper->bounds) {
1759 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1760 printk(KERN_ERR "SELinux: type %s: "
1761 "too deep or looped boundary\n",
1766 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1770 if (upper->attribute) {
1771 printk(KERN_ERR "SELinux: type %s: "
1772 "bounded by attribute %s",
1774 sym_name(p, SYM_TYPES, upper->value - 1));
1782 static int policydb_bounds_sanity_check(struct policydb *p)
1786 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1789 rc = hashtab_map(p->p_users.table,
1790 user_bounds_sanity_check, p);
1794 rc = hashtab_map(p->p_roles.table,
1795 role_bounds_sanity_check, p);
1799 rc = hashtab_map(p->p_types.table,
1800 type_bounds_sanity_check, p);
1807 u16 string_to_security_class(struct policydb *p, const char *name)
1809 struct class_datum *cladatum;
1811 cladatum = hashtab_search(p->p_classes.table, name);
1815 return cladatum->value;
1818 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1820 struct class_datum *cladatum;
1821 struct perm_datum *perdatum = NULL;
1822 struct common_datum *comdatum;
1824 if (!tclass || tclass > p->p_classes.nprim)
1827 cladatum = p->class_val_to_struct[tclass-1];
1828 comdatum = cladatum->comdatum;
1830 perdatum = hashtab_search(comdatum->permissions.table,
1833 perdatum = hashtab_search(cladatum->permissions.table,
1838 return 1U << (perdatum->value-1);
1841 static int range_read(struct policydb *p, void *fp)
1843 struct range_trans *rt = NULL;
1844 struct mls_range *r = NULL;
1849 if (p->policyvers < POLICYDB_VERSION_MLS)
1852 rc = next_entry(buf, fp, sizeof(u32));
1856 nel = le32_to_cpu(buf[0]);
1857 for (i = 0; i < nel; i++) {
1859 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1863 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1867 rt->source_type = le32_to_cpu(buf[0]);
1868 rt->target_type = le32_to_cpu(buf[1]);
1869 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1870 rc = next_entry(buf, fp, sizeof(u32));
1873 rt->target_class = le32_to_cpu(buf[0]);
1875 rt->target_class = p->process_class;
1878 if (!policydb_type_isvalid(p, rt->source_type) ||
1879 !policydb_type_isvalid(p, rt->target_type) ||
1880 !policydb_class_isvalid(p, rt->target_class))
1884 r = kzalloc(sizeof(*r), GFP_KERNEL);
1888 rc = mls_read_range_helper(r, fp);
1893 if (!mls_range_isvalid(p, r)) {
1894 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1898 rc = hashtab_insert(p->range_tr, rt, r);
1905 hash_eval(p->range_tr, "rangetr");
1913 static int filename_trans_read(struct policydb *p, void *fp)
1915 struct filename_trans *ft;
1916 struct filename_trans_datum *otype;
1922 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1925 rc = next_entry(buf, fp, sizeof(u32));
1928 nel = le32_to_cpu(buf[0]);
1930 for (i = 0; i < nel; i++) {
1936 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1941 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1945 /* length of the path component string */
1946 rc = next_entry(buf, fp, sizeof(u32));
1949 len = le32_to_cpu(buf[0]);
1951 /* path component string */
1952 rc = str_read(&name, GFP_KERNEL, fp, len);
1958 rc = next_entry(buf, fp, sizeof(u32) * 4);
1962 ft->stype = le32_to_cpu(buf[0]);
1963 ft->ttype = le32_to_cpu(buf[1]);
1964 ft->tclass = le32_to_cpu(buf[2]);
1966 otype->otype = le32_to_cpu(buf[3]);
1968 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1972 rc = hashtab_insert(p->filename_trans, ft, otype);
1975 * Do not return -EEXIST to the caller, or the system
1980 /* But free memory to avoid memory leak. */
1986 hash_eval(p->filename_trans, "filenametr");
1996 static int genfs_read(struct policydb *p, void *fp)
1999 u32 nel, nel2, len, len2;
2001 struct ocontext *l, *c;
2002 struct ocontext *newc = NULL;
2003 struct genfs *genfs_p, *genfs;
2004 struct genfs *newgenfs = NULL;
2006 rc = next_entry(buf, fp, sizeof(u32));
2009 nel = le32_to_cpu(buf[0]);
2011 for (i = 0; i < nel; i++) {
2012 rc = next_entry(buf, fp, sizeof(u32));
2015 len = le32_to_cpu(buf[0]);
2018 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2022 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2026 for (genfs_p = NULL, genfs = p->genfs; genfs;
2027 genfs_p = genfs, genfs = genfs->next) {
2029 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2030 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2034 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2037 newgenfs->next = genfs;
2039 genfs_p->next = newgenfs;
2041 p->genfs = newgenfs;
2045 rc = next_entry(buf, fp, sizeof(u32));
2049 nel2 = le32_to_cpu(buf[0]);
2050 for (j = 0; j < nel2; j++) {
2051 rc = next_entry(buf, fp, sizeof(u32));
2054 len = le32_to_cpu(buf[0]);
2057 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2061 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2065 rc = next_entry(buf, fp, sizeof(u32));
2069 newc->v.sclass = le32_to_cpu(buf[0]);
2070 rc = context_read_and_validate(&newc->context[0], p, fp);
2074 for (l = NULL, c = genfs->head; c;
2075 l = c, c = c->next) {
2077 if (!strcmp(newc->u.name, c->u.name) &&
2078 (!c->v.sclass || !newc->v.sclass ||
2079 newc->v.sclass == c->v.sclass)) {
2080 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2081 genfs->fstype, c->u.name);
2084 len = strlen(newc->u.name);
2085 len2 = strlen(c->u.name);
2101 kfree(newgenfs->fstype);
2103 ocontext_destroy(newc, OCON_FSUSE);
2108 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2114 struct ocontext *l, *c;
2117 for (i = 0; i < info->ocon_num; i++) {
2118 rc = next_entry(buf, fp, sizeof(u32));
2121 nel = le32_to_cpu(buf[0]);
2124 for (j = 0; j < nel; j++) {
2126 c = kzalloc(sizeof(*c), GFP_KERNEL);
2132 p->ocontexts[i] = c;
2137 rc = next_entry(buf, fp, sizeof(u32));
2141 c->sid[0] = le32_to_cpu(buf[0]);
2142 rc = context_read_and_validate(&c->context[0], p, fp);
2148 rc = next_entry(buf, fp, sizeof(u32));
2151 len = le32_to_cpu(buf[0]);
2153 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2157 rc = context_read_and_validate(&c->context[0], p, fp);
2160 rc = context_read_and_validate(&c->context[1], p, fp);
2165 rc = next_entry(buf, fp, sizeof(u32)*3);
2168 c->u.port.protocol = le32_to_cpu(buf[0]);
2169 c->u.port.low_port = le32_to_cpu(buf[1]);
2170 c->u.port.high_port = le32_to_cpu(buf[2]);
2171 rc = context_read_and_validate(&c->context[0], p, fp);
2176 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2179 c->u.node.addr = nodebuf[0]; /* network order */
2180 c->u.node.mask = nodebuf[1]; /* network order */
2181 rc = context_read_and_validate(&c->context[0], p, fp);
2186 rc = next_entry(buf, fp, sizeof(u32)*2);
2191 c->v.behavior = le32_to_cpu(buf[0]);
2192 /* Determined at runtime, not in policy DB. */
2193 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2195 if (c->v.behavior > SECURITY_FS_USE_MAX)
2198 len = le32_to_cpu(buf[1]);
2199 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2203 rc = context_read_and_validate(&c->context[0], p, fp);
2210 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2213 for (k = 0; k < 4; k++)
2214 c->u.node6.addr[k] = nodebuf[k];
2215 for (k = 0; k < 4; k++)
2216 c->u.node6.mask[k] = nodebuf[k+4];
2217 rc = context_read_and_validate(&c->context[0], p, fp);
2231 * Read the configuration data from a policy database binary
2232 * representation file into a policy database structure.
2234 int policydb_read(struct policydb *p, void *fp)
2236 struct role_allow *ra, *lra;
2237 struct role_trans *tr, *ltr;
2240 u32 len, nprim, nel;
2243 struct policydb_compat_info *info;
2245 rc = policydb_init(p);
2249 /* Read the magic number and string length. */
2250 rc = next_entry(buf, fp, sizeof(u32) * 2);
2255 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2256 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2257 "not match expected magic number 0x%x\n",
2258 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2263 len = le32_to_cpu(buf[1]);
2264 if (len != strlen(POLICYDB_STRING)) {
2265 printk(KERN_ERR "SELinux: policydb string length %d does not "
2266 "match expected length %Zu\n",
2267 len, strlen(POLICYDB_STRING));
2272 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2273 if (!policydb_str) {
2274 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2275 "string of length %d\n", len);
2279 rc = next_entry(policydb_str, fp, len);
2281 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2282 kfree(policydb_str);
2287 policydb_str[len] = '\0';
2288 if (strcmp(policydb_str, POLICYDB_STRING)) {
2289 printk(KERN_ERR "SELinux: policydb string %s does not match "
2290 "my string %s\n", policydb_str, POLICYDB_STRING);
2291 kfree(policydb_str);
2294 /* Done with policydb_str. */
2295 kfree(policydb_str);
2296 policydb_str = NULL;
2298 /* Read the version and table sizes. */
2299 rc = next_entry(buf, fp, sizeof(u32)*4);
2304 p->policyvers = le32_to_cpu(buf[0]);
2305 if (p->policyvers < POLICYDB_VERSION_MIN ||
2306 p->policyvers > POLICYDB_VERSION_MAX) {
2307 printk(KERN_ERR "SELinux: policydb version %d does not match "
2308 "my version range %d-%d\n",
2309 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2313 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2317 if (p->policyvers < POLICYDB_VERSION_MLS) {
2318 printk(KERN_ERR "SELinux: security policydb version %d "
2319 "(MLS) not backwards compatible\n",
2324 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2325 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2327 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2328 rc = ebitmap_read(&p->policycaps, fp);
2333 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2334 rc = ebitmap_read(&p->permissive_map, fp);
2340 info = policydb_lookup_compat(p->policyvers);
2342 printk(KERN_ERR "SELinux: unable to find policy compat info "
2343 "for version %d\n", p->policyvers);
2348 if (le32_to_cpu(buf[2]) != info->sym_num ||
2349 le32_to_cpu(buf[3]) != info->ocon_num) {
2350 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2351 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2352 le32_to_cpu(buf[3]),
2353 info->sym_num, info->ocon_num);
2357 for (i = 0; i < info->sym_num; i++) {
2358 rc = next_entry(buf, fp, sizeof(u32)*2);
2361 nprim = le32_to_cpu(buf[0]);
2362 nel = le32_to_cpu(buf[1]);
2363 for (j = 0; j < nel; j++) {
2364 rc = read_f[i](p, p->symtab[i].table, fp);
2369 p->symtab[i].nprim = nprim;
2373 p->process_class = string_to_security_class(p, "process");
2374 if (!p->process_class)
2377 rc = avtab_read(&p->te_avtab, fp, p);
2381 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2382 rc = cond_read_list(p, fp);
2387 rc = next_entry(buf, fp, sizeof(u32));
2390 nel = le32_to_cpu(buf[0]);
2392 for (i = 0; i < nel; i++) {
2394 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2401 rc = next_entry(buf, fp, sizeof(u32)*3);
2406 tr->role = le32_to_cpu(buf[0]);
2407 tr->type = le32_to_cpu(buf[1]);
2408 tr->new_role = le32_to_cpu(buf[2]);
2409 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2410 rc = next_entry(buf, fp, sizeof(u32));
2413 tr->tclass = le32_to_cpu(buf[0]);
2415 tr->tclass = p->process_class;
2417 if (!policydb_role_isvalid(p, tr->role) ||
2418 !policydb_type_isvalid(p, tr->type) ||
2419 !policydb_class_isvalid(p, tr->tclass) ||
2420 !policydb_role_isvalid(p, tr->new_role))
2425 rc = next_entry(buf, fp, sizeof(u32));
2428 nel = le32_to_cpu(buf[0]);
2430 for (i = 0; i < nel; i++) {
2432 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2439 rc = next_entry(buf, fp, sizeof(u32)*2);
2444 ra->role = le32_to_cpu(buf[0]);
2445 ra->new_role = le32_to_cpu(buf[1]);
2446 if (!policydb_role_isvalid(p, ra->role) ||
2447 !policydb_role_isvalid(p, ra->new_role))
2452 rc = filename_trans_read(p, fp);
2456 rc = policydb_index(p);
2461 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2462 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2463 if (!p->process_trans_perms)
2466 rc = ocontext_read(p, info, fp);
2470 rc = genfs_read(p, fp);
2474 rc = range_read(p, fp);
2479 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2481 GFP_KERNEL | __GFP_ZERO);
2482 if (!p->type_attr_map_array)
2485 /* preallocate so we don't have to worry about the put ever failing */
2486 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2487 GFP_KERNEL | __GFP_ZERO);
2491 for (i = 0; i < p->p_types.nprim; i++) {
2492 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2496 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2497 rc = ebitmap_read(e, fp);
2501 /* add the type itself as the degenerate case */
2502 rc = ebitmap_set_bit(e, i, 1);
2507 rc = policydb_bounds_sanity_check(p);
2515 policydb_destroy(p);
2520 * Write a MLS level structure to a policydb binary
2521 * representation file.
2523 static int mls_write_level(struct mls_level *l, void *fp)
2528 buf[0] = cpu_to_le32(l->sens);
2529 rc = put_entry(buf, sizeof(u32), 1, fp);
2533 rc = ebitmap_write(&l->cat, fp);
2541 * Write a MLS range structure to a policydb binary
2542 * representation file.
2544 static int mls_write_range_helper(struct mls_range *r, void *fp)
2550 eq = mls_level_eq(&r->level[1], &r->level[0]);
2556 buf[0] = cpu_to_le32(items-1);
2557 buf[1] = cpu_to_le32(r->level[0].sens);
2559 buf[2] = cpu_to_le32(r->level[1].sens);
2561 BUG_ON(items > ARRAY_SIZE(buf));
2563 rc = put_entry(buf, sizeof(u32), items, fp);
2567 rc = ebitmap_write(&r->level[0].cat, fp);
2571 rc = ebitmap_write(&r->level[1].cat, fp);
2579 static int sens_write(void *vkey, void *datum, void *ptr)
2582 struct level_datum *levdatum = datum;
2583 struct policy_data *pd = ptr;
2590 buf[0] = cpu_to_le32(len);
2591 buf[1] = cpu_to_le32(levdatum->isalias);
2592 rc = put_entry(buf, sizeof(u32), 2, fp);
2596 rc = put_entry(key, 1, len, fp);
2600 rc = mls_write_level(levdatum->level, fp);
2607 static int cat_write(void *vkey, void *datum, void *ptr)
2610 struct cat_datum *catdatum = datum;
2611 struct policy_data *pd = ptr;
2618 buf[0] = cpu_to_le32(len);
2619 buf[1] = cpu_to_le32(catdatum->value);
2620 buf[2] = cpu_to_le32(catdatum->isalias);
2621 rc = put_entry(buf, sizeof(u32), 3, fp);
2625 rc = put_entry(key, 1, len, fp);
2632 static int role_trans_write(struct policydb *p, void *fp)
2634 struct role_trans *r = p->role_tr;
2635 struct role_trans *tr;
2641 for (tr = r; tr; tr = tr->next)
2643 buf[0] = cpu_to_le32(nel);
2644 rc = put_entry(buf, sizeof(u32), 1, fp);
2647 for (tr = r; tr; tr = tr->next) {
2648 buf[0] = cpu_to_le32(tr->role);
2649 buf[1] = cpu_to_le32(tr->type);
2650 buf[2] = cpu_to_le32(tr->new_role);
2651 rc = put_entry(buf, sizeof(u32), 3, fp);
2654 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2655 buf[0] = cpu_to_le32(tr->tclass);
2656 rc = put_entry(buf, sizeof(u32), 1, fp);
2665 static int role_allow_write(struct role_allow *r, void *fp)
2667 struct role_allow *ra;
2673 for (ra = r; ra; ra = ra->next)
2675 buf[0] = cpu_to_le32(nel);
2676 rc = put_entry(buf, sizeof(u32), 1, fp);
2679 for (ra = r; ra; ra = ra->next) {
2680 buf[0] = cpu_to_le32(ra->role);
2681 buf[1] = cpu_to_le32(ra->new_role);
2682 rc = put_entry(buf, sizeof(u32), 2, fp);
2690 * Write a security context structure
2691 * to a policydb binary representation file.
2693 static int context_write(struct policydb *p, struct context *c,
2699 buf[0] = cpu_to_le32(c->user);
2700 buf[1] = cpu_to_le32(c->role);
2701 buf[2] = cpu_to_le32(c->type);
2703 rc = put_entry(buf, sizeof(u32), 3, fp);
2707 rc = mls_write_range_helper(&c->range, fp);
2715 * The following *_write functions are used to
2716 * write the symbol data to a policy database
2717 * binary representation file.
2720 static int perm_write(void *vkey, void *datum, void *fp)
2723 struct perm_datum *perdatum = datum;
2729 buf[0] = cpu_to_le32(len);
2730 buf[1] = cpu_to_le32(perdatum->value);
2731 rc = put_entry(buf, sizeof(u32), 2, fp);
2735 rc = put_entry(key, 1, len, fp);
2742 static int common_write(void *vkey, void *datum, void *ptr)
2745 struct common_datum *comdatum = datum;
2746 struct policy_data *pd = ptr;
2753 buf[0] = cpu_to_le32(len);
2754 buf[1] = cpu_to_le32(comdatum->value);
2755 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2756 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2757 rc = put_entry(buf, sizeof(u32), 4, fp);
2761 rc = put_entry(key, 1, len, fp);
2765 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2772 static int type_set_write(struct type_set *t, void *fp)
2777 if (ebitmap_write(&t->types, fp))
2779 if (ebitmap_write(&t->negset, fp))
2782 buf[0] = cpu_to_le32(t->flags);
2783 rc = put_entry(buf, sizeof(u32), 1, fp);
2790 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2793 struct constraint_node *c;
2794 struct constraint_expr *e;
2799 for (c = node; c; c = c->next) {
2801 for (e = c->expr; e; e = e->next)
2803 buf[0] = cpu_to_le32(c->permissions);
2804 buf[1] = cpu_to_le32(nel);
2805 rc = put_entry(buf, sizeof(u32), 2, fp);
2808 for (e = c->expr; e; e = e->next) {
2809 buf[0] = cpu_to_le32(e->expr_type);
2810 buf[1] = cpu_to_le32(e->attr);
2811 buf[2] = cpu_to_le32(e->op);
2812 rc = put_entry(buf, sizeof(u32), 3, fp);
2816 switch (e->expr_type) {
2818 rc = ebitmap_write(&e->names, fp);
2821 if (p->policyvers >=
2822 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2823 rc = type_set_write(e->type_names, fp);
2837 static int class_write(void *vkey, void *datum, void *ptr)
2840 struct class_datum *cladatum = datum;
2841 struct policy_data *pd = ptr;
2843 struct policydb *p = pd->p;
2844 struct constraint_node *c;
2851 if (cladatum->comkey)
2852 len2 = strlen(cladatum->comkey);
2857 for (c = cladatum->constraints; c; c = c->next)
2860 buf[0] = cpu_to_le32(len);
2861 buf[1] = cpu_to_le32(len2);
2862 buf[2] = cpu_to_le32(cladatum->value);
2863 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2864 if (cladatum->permissions.table)
2865 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2868 buf[5] = cpu_to_le32(ncons);
2869 rc = put_entry(buf, sizeof(u32), 6, fp);
2873 rc = put_entry(key, 1, len, fp);
2877 if (cladatum->comkey) {
2878 rc = put_entry(cladatum->comkey, 1, len2, fp);
2883 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2887 rc = write_cons_helper(p, cladatum->constraints, fp);
2891 /* write out the validatetrans rule */
2893 for (c = cladatum->validatetrans; c; c = c->next)
2896 buf[0] = cpu_to_le32(ncons);
2897 rc = put_entry(buf, sizeof(u32), 1, fp);
2901 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2905 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2906 buf[0] = cpu_to_le32(cladatum->default_user);
2907 buf[1] = cpu_to_le32(cladatum->default_role);
2908 buf[2] = cpu_to_le32(cladatum->default_range);
2910 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2915 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2916 buf[0] = cpu_to_le32(cladatum->default_type);
2917 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2925 static int role_write(void *vkey, void *datum, void *ptr)
2928 struct role_datum *role = datum;
2929 struct policy_data *pd = ptr;
2931 struct policydb *p = pd->p;
2938 buf[items++] = cpu_to_le32(len);
2939 buf[items++] = cpu_to_le32(role->value);
2940 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2941 buf[items++] = cpu_to_le32(role->bounds);
2943 BUG_ON(items > ARRAY_SIZE(buf));
2945 rc = put_entry(buf, sizeof(u32), items, fp);
2949 rc = put_entry(key, 1, len, fp);
2953 rc = ebitmap_write(&role->dominates, fp);
2957 rc = ebitmap_write(&role->types, fp);
2964 static int type_write(void *vkey, void *datum, void *ptr)
2967 struct type_datum *typdatum = datum;
2968 struct policy_data *pd = ptr;
2969 struct policydb *p = pd->p;
2977 buf[items++] = cpu_to_le32(len);
2978 buf[items++] = cpu_to_le32(typdatum->value);
2979 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2982 if (typdatum->primary)
2983 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2985 if (typdatum->attribute)
2986 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2988 buf[items++] = cpu_to_le32(properties);
2989 buf[items++] = cpu_to_le32(typdatum->bounds);
2991 buf[items++] = cpu_to_le32(typdatum->primary);
2993 BUG_ON(items > ARRAY_SIZE(buf));
2994 rc = put_entry(buf, sizeof(u32), items, fp);
2998 rc = put_entry(key, 1, len, fp);
3005 static int user_write(void *vkey, void *datum, void *ptr)
3008 struct user_datum *usrdatum = datum;
3009 struct policy_data *pd = ptr;
3010 struct policydb *p = pd->p;
3018 buf[items++] = cpu_to_le32(len);
3019 buf[items++] = cpu_to_le32(usrdatum->value);
3020 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3021 buf[items++] = cpu_to_le32(usrdatum->bounds);
3022 BUG_ON(items > ARRAY_SIZE(buf));
3023 rc = put_entry(buf, sizeof(u32), items, fp);
3027 rc = put_entry(key, 1, len, fp);
3031 rc = ebitmap_write(&usrdatum->roles, fp);
3035 rc = mls_write_range_helper(&usrdatum->range, fp);
3039 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3046 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3059 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3062 unsigned int i, j, rc;
3067 for (i = 0; i < info->ocon_num; i++) {
3069 for (c = p->ocontexts[i]; c; c = c->next)
3071 buf[0] = cpu_to_le32(nel);
3072 rc = put_entry(buf, sizeof(u32), 1, fp);
3075 for (c = p->ocontexts[i]; c; c = c->next) {
3078 buf[0] = cpu_to_le32(c->sid[0]);
3079 rc = put_entry(buf, sizeof(u32), 1, fp);
3082 rc = context_write(p, &c->context[0], fp);
3088 len = strlen(c->u.name);
3089 buf[0] = cpu_to_le32(len);
3090 rc = put_entry(buf, sizeof(u32), 1, fp);
3093 rc = put_entry(c->u.name, 1, len, fp);
3096 rc = context_write(p, &c->context[0], fp);
3099 rc = context_write(p, &c->context[1], fp);
3104 buf[0] = cpu_to_le32(c->u.port.protocol);
3105 buf[1] = cpu_to_le32(c->u.port.low_port);
3106 buf[2] = cpu_to_le32(c->u.port.high_port);
3107 rc = put_entry(buf, sizeof(u32), 3, fp);
3110 rc = context_write(p, &c->context[0], fp);
3115 nodebuf[0] = c->u.node.addr; /* network order */
3116 nodebuf[1] = c->u.node.mask; /* network order */
3117 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3120 rc = context_write(p, &c->context[0], fp);
3125 buf[0] = cpu_to_le32(c->v.behavior);
3126 len = strlen(c->u.name);
3127 buf[1] = cpu_to_le32(len);
3128 rc = put_entry(buf, sizeof(u32), 2, fp);
3131 rc = put_entry(c->u.name, 1, len, fp);
3134 rc = context_write(p, &c->context[0], fp);
3139 for (j = 0; j < 4; j++)
3140 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3141 for (j = 0; j < 4; j++)
3142 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3143 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3146 rc = context_write(p, &c->context[0], fp);
3156 static int genfs_write(struct policydb *p, void *fp)
3158 struct genfs *genfs;
3165 for (genfs = p->genfs; genfs; genfs = genfs->next)
3167 buf[0] = cpu_to_le32(len);
3168 rc = put_entry(buf, sizeof(u32), 1, fp);
3171 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3172 len = strlen(genfs->fstype);
3173 buf[0] = cpu_to_le32(len);
3174 rc = put_entry(buf, sizeof(u32), 1, fp);
3177 rc = put_entry(genfs->fstype, 1, len, fp);
3181 for (c = genfs->head; c; c = c->next)
3183 buf[0] = cpu_to_le32(len);
3184 rc = put_entry(buf, sizeof(u32), 1, fp);
3187 for (c = genfs->head; c; c = c->next) {
3188 len = strlen(c->u.name);
3189 buf[0] = cpu_to_le32(len);
3190 rc = put_entry(buf, sizeof(u32), 1, fp);
3193 rc = put_entry(c->u.name, 1, len, fp);
3196 buf[0] = cpu_to_le32(c->v.sclass);
3197 rc = put_entry(buf, sizeof(u32), 1, fp);
3200 rc = context_write(p, &c->context[0], fp);
3208 static int hashtab_cnt(void *key, void *data, void *ptr)
3216 static int range_write_helper(void *key, void *data, void *ptr)
3219 struct range_trans *rt = key;
3220 struct mls_range *r = data;
3221 struct policy_data *pd = ptr;
3223 struct policydb *p = pd->p;
3226 buf[0] = cpu_to_le32(rt->source_type);
3227 buf[1] = cpu_to_le32(rt->target_type);
3228 rc = put_entry(buf, sizeof(u32), 2, fp);
3231 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3232 buf[0] = cpu_to_le32(rt->target_class);
3233 rc = put_entry(buf, sizeof(u32), 1, fp);
3237 rc = mls_write_range_helper(r, fp);
3244 static int range_write(struct policydb *p, void *fp)
3248 struct policy_data pd;
3253 /* count the number of entries in the hashtab */
3255 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3259 buf[0] = cpu_to_le32(nel);
3260 rc = put_entry(buf, sizeof(u32), 1, fp);
3264 /* actually write all of the entries */
3265 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3272 static int filename_write_helper(void *key, void *data, void *ptr)
3275 struct filename_trans *ft = key;
3276 struct filename_trans_datum *otype = data;
3281 len = strlen(ft->name);
3282 buf[0] = cpu_to_le32(len);
3283 rc = put_entry(buf, sizeof(u32), 1, fp);
3287 rc = put_entry(ft->name, sizeof(char), len, fp);
3291 buf[0] = cpu_to_le32(ft->stype);
3292 buf[1] = cpu_to_le32(ft->ttype);
3293 buf[2] = cpu_to_le32(ft->tclass);
3294 buf[3] = cpu_to_le32(otype->otype);
3296 rc = put_entry(buf, sizeof(u32), 4, fp);
3303 static int filename_trans_write(struct policydb *p, void *fp)
3309 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3313 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3317 buf[0] = cpu_to_le32(nel);
3318 rc = put_entry(buf, sizeof(u32), 1, fp);
3322 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3330 * Write the configuration data in a policy database
3331 * structure to a policy database binary representation
3334 int policydb_write(struct policydb *p, void *fp)
3336 unsigned int i, num_syms;
3341 struct policydb_compat_info *info;
3344 * refuse to write policy older than compressed avtab
3345 * to simplify the writer. There are other tests dropped
3346 * since we assume this throughout the writer code. Be
3347 * careful if you ever try to remove this restriction
3349 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3350 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3351 " Because it is less than version %d\n", p->policyvers,
3352 POLICYDB_VERSION_AVTAB);
3358 config |= POLICYDB_CONFIG_MLS;
3360 if (p->reject_unknown)
3361 config |= REJECT_UNKNOWN;
3362 if (p->allow_unknown)
3363 config |= ALLOW_UNKNOWN;
3365 /* Write the magic number and string identifiers. */
3366 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3367 len = strlen(POLICYDB_STRING);
3368 buf[1] = cpu_to_le32(len);
3369 rc = put_entry(buf, sizeof(u32), 2, fp);
3372 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3376 /* Write the version, config, and table sizes. */
3377 info = policydb_lookup_compat(p->policyvers);
3379 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3380 "version %d", p->policyvers);
3384 buf[0] = cpu_to_le32(p->policyvers);
3385 buf[1] = cpu_to_le32(config);
3386 buf[2] = cpu_to_le32(info->sym_num);
3387 buf[3] = cpu_to_le32(info->ocon_num);
3389 rc = put_entry(buf, sizeof(u32), 4, fp);
3393 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3394 rc = ebitmap_write(&p->policycaps, fp);
3399 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3400 rc = ebitmap_write(&p->permissive_map, fp);
3405 num_syms = info->sym_num;
3406 for (i = 0; i < num_syms; i++) {
3407 struct policy_data pd;
3412 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3413 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3415 rc = put_entry(buf, sizeof(u32), 2, fp);
3418 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3423 rc = avtab_write(p, &p->te_avtab, fp);
3427 rc = cond_write_list(p, p->cond_list, fp);
3431 rc = role_trans_write(p, fp);
3435 rc = role_allow_write(p->role_allow, fp);
3439 rc = filename_trans_write(p, fp);
3443 rc = ocontext_write(p, info, fp);
3447 rc = genfs_write(p, fp);
3451 rc = range_write(p, fp);
3455 for (i = 0; i < p->p_types.nprim; i++) {
3456 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3459 rc = ebitmap_write(e, fp);