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,
148 static struct policydb_compat_info *policydb_lookup_compat(int version)
151 struct policydb_compat_info *info = NULL;
153 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
154 if (policydb_compat[i].version == version) {
155 info = &policydb_compat[i];
163 * Initialize the role table.
165 static int roles_init(struct policydb *p)
169 struct role_datum *role;
172 role = kzalloc(sizeof(*role), GFP_KERNEL);
177 role->value = ++p->p_roles.nprim;
178 if (role->value != OBJECT_R_VAL)
182 key = kstrdup(OBJECT_R, GFP_KERNEL);
186 rc = hashtab_insert(p->p_roles.table, key, role);
197 static u32 filenametr_hash(struct hashtab *h, const void *k)
199 const struct filename_trans *ft = k;
201 unsigned int byte_num;
204 hash = ft->stype ^ ft->ttype ^ ft->tclass;
207 while ((focus = ft->name[byte_num++]))
208 hash = partial_name_hash(focus, hash);
209 return hash & (h->size - 1);
212 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
214 const struct filename_trans *ft1 = k1;
215 const struct filename_trans *ft2 = k2;
218 v = ft1->stype - ft2->stype;
222 v = ft1->ttype - ft2->ttype;
226 v = ft1->tclass - ft2->tclass;
230 return strcmp(ft1->name, ft2->name);
234 static u32 rangetr_hash(struct hashtab *h, const void *k)
236 const struct range_trans *key = k;
237 return (key->source_type + (key->target_type << 3) +
238 (key->target_class << 5)) & (h->size - 1);
241 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
243 const struct range_trans *key1 = k1, *key2 = k2;
246 v = key1->source_type - key2->source_type;
250 v = key1->target_type - key2->target_type;
254 v = key1->target_class - key2->target_class;
260 * Initialize a policy database structure.
262 static int policydb_init(struct policydb *p)
266 memset(p, 0, sizeof(*p));
268 for (i = 0; i < SYM_NUM; i++) {
269 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
274 rc = avtab_init(&p->te_avtab);
282 rc = cond_policydb_init(p);
286 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
287 if (!p->filename_trans)
290 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
294 ebitmap_init(&p->filename_trans_ttypes);
295 ebitmap_init(&p->policycaps);
296 ebitmap_init(&p->permissive_map);
300 hashtab_destroy(p->filename_trans);
301 hashtab_destroy(p->range_tr);
302 for (i = 0; i < SYM_NUM; i++)
303 hashtab_destroy(p->symtab[i].table);
308 * The following *_index functions are used to
309 * define the val_to_name and val_to_struct arrays
310 * in a policy database structure. The val_to_name
311 * arrays are used when converting security context
312 * structures into string representations. The
313 * val_to_struct arrays are used when the attributes
314 * of a class, role, or user are needed.
317 static int common_index(void *key, void *datum, void *datap)
320 struct common_datum *comdatum;
321 struct flex_array *fa;
325 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
328 fa = p->sym_val_to_name[SYM_COMMONS];
329 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
330 GFP_KERNEL | __GFP_ZERO))
335 static int class_index(void *key, void *datum, void *datap)
338 struct class_datum *cladatum;
339 struct flex_array *fa;
343 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
345 fa = p->sym_val_to_name[SYM_CLASSES];
346 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
347 GFP_KERNEL | __GFP_ZERO))
349 p->class_val_to_struct[cladatum->value - 1] = cladatum;
353 static int role_index(void *key, void *datum, void *datap)
356 struct role_datum *role;
357 struct flex_array *fa;
362 || role->value > p->p_roles.nprim
363 || role->bounds > p->p_roles.nprim)
366 fa = p->sym_val_to_name[SYM_ROLES];
367 if (flex_array_put_ptr(fa, role->value - 1, key,
368 GFP_KERNEL | __GFP_ZERO))
370 p->role_val_to_struct[role->value - 1] = role;
374 static int type_index(void *key, void *datum, void *datap)
377 struct type_datum *typdatum;
378 struct flex_array *fa;
383 if (typdatum->primary) {
385 || typdatum->value > p->p_types.nprim
386 || typdatum->bounds > p->p_types.nprim)
388 fa = p->sym_val_to_name[SYM_TYPES];
389 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
390 GFP_KERNEL | __GFP_ZERO))
393 fa = p->type_val_to_struct_array;
394 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
395 GFP_KERNEL | __GFP_ZERO))
402 static int user_index(void *key, void *datum, void *datap)
405 struct user_datum *usrdatum;
406 struct flex_array *fa;
411 || usrdatum->value > p->p_users.nprim
412 || usrdatum->bounds > p->p_users.nprim)
415 fa = p->sym_val_to_name[SYM_USERS];
416 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
417 GFP_KERNEL | __GFP_ZERO))
419 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
423 static int sens_index(void *key, void *datum, void *datap)
426 struct level_datum *levdatum;
427 struct flex_array *fa;
432 if (!levdatum->isalias) {
433 if (!levdatum->level->sens ||
434 levdatum->level->sens > p->p_levels.nprim)
436 fa = p->sym_val_to_name[SYM_LEVELS];
437 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
438 GFP_KERNEL | __GFP_ZERO))
445 static int cat_index(void *key, void *datum, void *datap)
448 struct cat_datum *catdatum;
449 struct flex_array *fa;
454 if (!catdatum->isalias) {
455 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
457 fa = p->sym_val_to_name[SYM_CATS];
458 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
459 GFP_KERNEL | __GFP_ZERO))
466 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
479 static void hash_eval(struct hashtab *h, const char *hash_name)
481 struct hashtab_info info;
483 hashtab_stat(h, &info);
484 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
485 "longest chain length %d\n", hash_name, h->nel,
486 info.slots_used, h->size, info.max_chain_len);
489 static void symtab_hash_eval(struct symtab *s)
493 for (i = 0; i < SYM_NUM; i++)
494 hash_eval(s[i].table, symtab_name[i]);
498 static inline void hash_eval(struct hashtab *h, char *hash_name)
504 * Define the other val_to_name and val_to_struct arrays
505 * in a policy database structure.
507 * Caller must clean up on failure.
509 static int policydb_index(struct policydb *p)
513 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
514 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
516 printk(", %d sens, %d cats", p->p_levels.nprim,
520 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
521 p->p_classes.nprim, p->te_avtab.nel);
524 avtab_hash_eval(&p->te_avtab, "rules");
525 symtab_hash_eval(p->symtab);
529 p->class_val_to_struct =
530 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
532 if (!p->class_val_to_struct)
536 p->role_val_to_struct =
537 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
539 if (!p->role_val_to_struct)
543 p->user_val_to_struct =
544 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
546 if (!p->user_val_to_struct)
549 /* Yes, I want the sizeof the pointer, not the structure */
551 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
553 GFP_KERNEL | __GFP_ZERO);
554 if (!p->type_val_to_struct_array)
557 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
558 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
562 rc = cond_init_bool_indexes(p);
566 for (i = 0; i < SYM_NUM; i++) {
568 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
570 GFP_KERNEL | __GFP_ZERO);
571 if (!p->sym_val_to_name[i])
574 rc = flex_array_prealloc(p->sym_val_to_name[i],
575 0, p->symtab[i].nprim,
576 GFP_KERNEL | __GFP_ZERO);
580 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
590 * The following *_destroy functions are used to
591 * free any memory allocated for each kind of
592 * symbol data in the policy database.
595 static int perm_destroy(void *key, void *datum, void *p)
602 static int common_destroy(void *key, void *datum, void *p)
604 struct common_datum *comdatum;
609 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
610 hashtab_destroy(comdatum->permissions.table);
616 static int cls_destroy(void *key, void *datum, void *p)
618 struct class_datum *cladatum;
619 struct constraint_node *constraint, *ctemp;
620 struct constraint_expr *e, *etmp;
625 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
626 hashtab_destroy(cladatum->permissions.table);
627 constraint = cladatum->constraints;
629 e = constraint->expr;
631 ebitmap_destroy(&e->names);
637 constraint = constraint->next;
641 constraint = cladatum->validatetrans;
643 e = constraint->expr;
645 ebitmap_destroy(&e->names);
651 constraint = constraint->next;
655 kfree(cladatum->comkey);
661 static int role_destroy(void *key, void *datum, void *p)
663 struct role_datum *role;
668 ebitmap_destroy(&role->dominates);
669 ebitmap_destroy(&role->types);
675 static int type_destroy(void *key, void *datum, void *p)
682 static int user_destroy(void *key, void *datum, void *p)
684 struct user_datum *usrdatum;
689 ebitmap_destroy(&usrdatum->roles);
690 ebitmap_destroy(&usrdatum->range.level[0].cat);
691 ebitmap_destroy(&usrdatum->range.level[1].cat);
692 ebitmap_destroy(&usrdatum->dfltlevel.cat);
698 static int sens_destroy(void *key, void *datum, void *p)
700 struct level_datum *levdatum;
705 ebitmap_destroy(&levdatum->level->cat);
706 kfree(levdatum->level);
712 static int cat_destroy(void *key, void *datum, void *p)
719 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
731 static int filenametr_destroy(void *key, void *datum, void *p)
733 struct filename_trans *ft = key;
741 static int range_tr_destroy(void *key, void *datum, void *p)
743 struct mls_range *rt = datum;
745 ebitmap_destroy(&rt->level[0].cat);
746 ebitmap_destroy(&rt->level[1].cat);
752 static void ocontext_destroy(struct ocontext *c, int i)
757 context_destroy(&c->context[0]);
758 context_destroy(&c->context[1]);
759 if (i == OCON_ISID || i == OCON_FS ||
760 i == OCON_NETIF || i == OCON_FSUSE)
766 * Free any memory allocated by a policy database structure.
768 void policydb_destroy(struct policydb *p)
770 struct ocontext *c, *ctmp;
771 struct genfs *g, *gtmp;
773 struct role_allow *ra, *lra = NULL;
774 struct role_trans *tr, *ltr = NULL;
776 for (i = 0; i < SYM_NUM; i++) {
778 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
779 hashtab_destroy(p->symtab[i].table);
782 for (i = 0; i < SYM_NUM; i++) {
783 if (p->sym_val_to_name[i])
784 flex_array_free(p->sym_val_to_name[i]);
787 kfree(p->class_val_to_struct);
788 kfree(p->role_val_to_struct);
789 kfree(p->user_val_to_struct);
790 if (p->type_val_to_struct_array)
791 flex_array_free(p->type_val_to_struct_array);
793 avtab_destroy(&p->te_avtab);
795 for (i = 0; i < OCON_NUM; i++) {
801 ocontext_destroy(ctmp, i);
803 p->ocontexts[i] = NULL;
814 ocontext_destroy(ctmp, OCON_FSUSE);
822 cond_policydb_destroy(p);
824 for (tr = p->role_tr; tr; tr = tr->next) {
831 for (ra = p->role_allow; ra; ra = ra->next) {
838 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
839 hashtab_destroy(p->filename_trans);
841 hashtab_map(p->range_tr, range_tr_destroy, NULL);
842 hashtab_destroy(p->range_tr);
844 if (p->type_attr_map_array) {
845 for (i = 0; i < p->p_types.nprim; i++) {
848 e = flex_array_get(p->type_attr_map_array, i);
853 flex_array_free(p->type_attr_map_array);
856 ebitmap_destroy(&p->filename_trans_ttypes);
857 ebitmap_destroy(&p->policycaps);
858 ebitmap_destroy(&p->permissive_map);
864 * Load the initial SIDs specified in a policy database
865 * structure into a SID table.
867 int policydb_load_isids(struct policydb *p, struct sidtab *s)
869 struct ocontext *head, *c;
874 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
878 head = p->ocontexts[OCON_ISID];
879 for (c = head; c; c = c->next) {
881 if (!c->context[0].user) {
882 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
887 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
889 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
899 int policydb_class_isvalid(struct policydb *p, unsigned int class)
901 if (!class || class > p->p_classes.nprim)
906 int policydb_role_isvalid(struct policydb *p, unsigned int role)
908 if (!role || role > p->p_roles.nprim)
913 int policydb_type_isvalid(struct policydb *p, unsigned int type)
915 if (!type || type > p->p_types.nprim)
921 * Return 1 if the fields in the security context
922 * structure `c' are valid. Return 0 otherwise.
924 int policydb_context_isvalid(struct policydb *p, struct context *c)
926 struct role_datum *role;
927 struct user_datum *usrdatum;
929 if (!c->role || c->role > p->p_roles.nprim)
932 if (!c->user || c->user > p->p_users.nprim)
935 if (!c->type || c->type > p->p_types.nprim)
938 if (c->role != OBJECT_R_VAL) {
940 * Role must be authorized for the type.
942 role = p->role_val_to_struct[c->role - 1];
943 if (!ebitmap_get_bit(&role->types, c->type - 1))
944 /* role may not be associated with type */
948 * User must be authorized for the role.
950 usrdatum = p->user_val_to_struct[c->user - 1];
954 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
955 /* user may not be associated with role */
959 if (!mls_context_isvalid(p, c))
966 * Read a MLS range structure from a policydb binary
967 * representation file.
969 static int mls_read_range_helper(struct mls_range *r, void *fp)
975 rc = next_entry(buf, fp, sizeof(u32));
980 items = le32_to_cpu(buf[0]);
981 if (items > ARRAY_SIZE(buf)) {
982 printk(KERN_ERR "SELinux: mls: range overflow\n");
986 rc = next_entry(buf, fp, sizeof(u32) * items);
988 printk(KERN_ERR "SELinux: mls: truncated range\n");
992 r->level[0].sens = le32_to_cpu(buf[0]);
994 r->level[1].sens = le32_to_cpu(buf[1]);
996 r->level[1].sens = r->level[0].sens;
998 rc = ebitmap_read(&r->level[0].cat, fp);
1000 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
1004 rc = ebitmap_read(&r->level[1].cat, fp);
1006 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1010 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1012 printk(KERN_ERR "SELinux: mls: out of memory\n");
1019 ebitmap_destroy(&r->level[0].cat);
1025 * Read and validate a security context structure
1026 * from a policydb binary representation file.
1028 static int context_read_and_validate(struct context *c,
1035 rc = next_entry(buf, fp, sizeof buf);
1037 printk(KERN_ERR "SELinux: context truncated\n");
1040 c->user = le32_to_cpu(buf[0]);
1041 c->role = le32_to_cpu(buf[1]);
1042 c->type = le32_to_cpu(buf[2]);
1043 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1044 rc = mls_read_range_helper(&c->range, fp);
1046 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1052 if (!policydb_context_isvalid(p, c)) {
1053 printk(KERN_ERR "SELinux: invalid security context\n");
1063 * The following *_read functions are used to
1064 * read the symbol data from a policy database
1065 * binary representation file.
1068 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1071 struct perm_datum *perdatum;
1077 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1081 rc = next_entry(buf, fp, sizeof buf);
1085 len = le32_to_cpu(buf[0]);
1086 perdatum->value = le32_to_cpu(buf[1]);
1089 key = kmalloc(len + 1, GFP_KERNEL);
1093 rc = next_entry(key, fp, len);
1098 rc = hashtab_insert(h, key, perdatum);
1104 perm_destroy(key, perdatum, NULL);
1108 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1111 struct common_datum *comdatum;
1117 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1121 rc = next_entry(buf, fp, sizeof buf);
1125 len = le32_to_cpu(buf[0]);
1126 comdatum->value = le32_to_cpu(buf[1]);
1128 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1131 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1132 nel = le32_to_cpu(buf[3]);
1135 key = kmalloc(len + 1, GFP_KERNEL);
1139 rc = next_entry(key, fp, len);
1144 for (i = 0; i < nel; i++) {
1145 rc = perm_read(p, comdatum->permissions.table, fp);
1150 rc = hashtab_insert(h, key, comdatum);
1155 common_destroy(key, comdatum, NULL);
1159 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1160 int allowxtarget, void *fp)
1162 struct constraint_node *c, *lc;
1163 struct constraint_expr *e, *le;
1166 int rc, i, j, depth;
1169 for (i = 0; i < ncons; i++) {
1170 c = kzalloc(sizeof(*c), GFP_KERNEL);
1179 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1182 c->permissions = le32_to_cpu(buf[0]);
1183 nexpr = le32_to_cpu(buf[1]);
1186 for (j = 0; j < nexpr; j++) {
1187 e = kzalloc(sizeof(*e), GFP_KERNEL);
1196 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1199 e->expr_type = le32_to_cpu(buf[0]);
1200 e->attr = le32_to_cpu(buf[1]);
1201 e->op = le32_to_cpu(buf[2]);
1203 switch (e->expr_type) {
1215 if (depth == (CEXPR_MAXDEPTH - 1))
1220 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1222 if (depth == (CEXPR_MAXDEPTH - 1))
1225 rc = ebitmap_read(&e->names, fp);
1242 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1245 struct class_datum *cladatum;
1247 u32 len, len2, ncons, nel;
1251 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1255 rc = next_entry(buf, fp, sizeof(u32)*6);
1259 len = le32_to_cpu(buf[0]);
1260 len2 = le32_to_cpu(buf[1]);
1261 cladatum->value = le32_to_cpu(buf[2]);
1263 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1266 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1267 nel = le32_to_cpu(buf[4]);
1269 ncons = le32_to_cpu(buf[5]);
1272 key = kmalloc(len + 1, GFP_KERNEL);
1276 rc = next_entry(key, fp, len);
1283 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1284 if (!cladatum->comkey)
1286 rc = next_entry(cladatum->comkey, fp, len2);
1289 cladatum->comkey[len2] = '\0';
1292 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1293 if (!cladatum->comdatum) {
1294 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1298 for (i = 0; i < nel; i++) {
1299 rc = perm_read(p, cladatum->permissions.table, fp);
1304 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1308 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1309 /* grab the validatetrans rules */
1310 rc = next_entry(buf, fp, sizeof(u32));
1313 ncons = le32_to_cpu(buf[0]);
1314 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1319 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1320 rc = next_entry(buf, fp, sizeof(u32) * 3);
1324 cladatum->default_user = le32_to_cpu(buf[0]);
1325 cladatum->default_role = le32_to_cpu(buf[1]);
1326 cladatum->default_range = le32_to_cpu(buf[2]);
1329 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1330 rc = next_entry(buf, fp, sizeof(u32) * 1);
1333 cladatum->default_type = le32_to_cpu(buf[0]);
1336 rc = hashtab_insert(h, key, cladatum);
1342 cls_destroy(key, cladatum, NULL);
1346 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1349 struct role_datum *role;
1350 int rc, to_read = 2;
1355 role = kzalloc(sizeof(*role), GFP_KERNEL);
1359 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1362 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1366 len = le32_to_cpu(buf[0]);
1367 role->value = le32_to_cpu(buf[1]);
1368 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1369 role->bounds = le32_to_cpu(buf[2]);
1372 key = kmalloc(len + 1, GFP_KERNEL);
1376 rc = next_entry(key, fp, len);
1381 rc = ebitmap_read(&role->dominates, fp);
1385 rc = ebitmap_read(&role->types, fp);
1389 if (strcmp(key, OBJECT_R) == 0) {
1391 if (role->value != OBJECT_R_VAL) {
1392 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1393 OBJECT_R, role->value);
1400 rc = hashtab_insert(h, key, role);
1405 role_destroy(key, role, NULL);
1409 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1412 struct type_datum *typdatum;
1413 int rc, to_read = 3;
1418 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1422 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1425 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1429 len = le32_to_cpu(buf[0]);
1430 typdatum->value = le32_to_cpu(buf[1]);
1431 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1432 u32 prop = le32_to_cpu(buf[2]);
1434 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1435 typdatum->primary = 1;
1436 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1437 typdatum->attribute = 1;
1439 typdatum->bounds = le32_to_cpu(buf[3]);
1441 typdatum->primary = le32_to_cpu(buf[2]);
1445 key = kmalloc(len + 1, GFP_KERNEL);
1448 rc = next_entry(key, fp, len);
1453 rc = hashtab_insert(h, key, typdatum);
1458 type_destroy(key, typdatum, NULL);
1464 * Read a MLS level structure from a policydb binary
1465 * representation file.
1467 static int mls_read_level(struct mls_level *lp, void *fp)
1472 memset(lp, 0, sizeof(*lp));
1474 rc = next_entry(buf, fp, sizeof buf);
1476 printk(KERN_ERR "SELinux: mls: truncated level\n");
1479 lp->sens = le32_to_cpu(buf[0]);
1481 rc = ebitmap_read(&lp->cat, fp);
1483 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1489 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1492 struct user_datum *usrdatum;
1493 int rc, to_read = 2;
1498 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1502 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1505 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1509 len = le32_to_cpu(buf[0]);
1510 usrdatum->value = le32_to_cpu(buf[1]);
1511 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1512 usrdatum->bounds = le32_to_cpu(buf[2]);
1515 key = kmalloc(len + 1, GFP_KERNEL);
1518 rc = next_entry(key, fp, len);
1523 rc = ebitmap_read(&usrdatum->roles, fp);
1527 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1528 rc = mls_read_range_helper(&usrdatum->range, fp);
1531 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1536 rc = hashtab_insert(h, key, usrdatum);
1541 user_destroy(key, usrdatum, NULL);
1545 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1548 struct level_datum *levdatum;
1554 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1558 rc = next_entry(buf, fp, sizeof buf);
1562 len = le32_to_cpu(buf[0]);
1563 levdatum->isalias = le32_to_cpu(buf[1]);
1566 key = kmalloc(len + 1, GFP_ATOMIC);
1569 rc = next_entry(key, fp, len);
1575 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1576 if (!levdatum->level)
1579 rc = mls_read_level(levdatum->level, fp);
1583 rc = hashtab_insert(h, key, levdatum);
1588 sens_destroy(key, levdatum, NULL);
1592 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1595 struct cat_datum *catdatum;
1601 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1605 rc = next_entry(buf, fp, sizeof buf);
1609 len = le32_to_cpu(buf[0]);
1610 catdatum->value = le32_to_cpu(buf[1]);
1611 catdatum->isalias = le32_to_cpu(buf[2]);
1614 key = kmalloc(len + 1, GFP_ATOMIC);
1617 rc = next_entry(key, fp, len);
1622 rc = hashtab_insert(h, key, catdatum);
1627 cat_destroy(key, catdatum, NULL);
1631 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1643 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1645 struct user_datum *upper, *user;
1646 struct policydb *p = datap;
1649 upper = user = datum;
1650 while (upper->bounds) {
1651 struct ebitmap_node *node;
1654 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1655 printk(KERN_ERR "SELinux: user %s: "
1656 "too deep or looped boundary",
1661 upper = p->user_val_to_struct[upper->bounds - 1];
1662 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1663 if (ebitmap_get_bit(&upper->roles, bit))
1667 "SELinux: boundary violated policy: "
1668 "user=%s role=%s bounds=%s\n",
1669 sym_name(p, SYM_USERS, user->value - 1),
1670 sym_name(p, SYM_ROLES, bit),
1671 sym_name(p, SYM_USERS, upper->value - 1));
1680 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1682 struct role_datum *upper, *role;
1683 struct policydb *p = datap;
1686 upper = role = datum;
1687 while (upper->bounds) {
1688 struct ebitmap_node *node;
1691 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1692 printk(KERN_ERR "SELinux: role %s: "
1693 "too deep or looped bounds\n",
1698 upper = p->role_val_to_struct[upper->bounds - 1];
1699 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1700 if (ebitmap_get_bit(&upper->types, bit))
1704 "SELinux: boundary violated policy: "
1705 "role=%s type=%s bounds=%s\n",
1706 sym_name(p, SYM_ROLES, role->value - 1),
1707 sym_name(p, SYM_TYPES, bit),
1708 sym_name(p, SYM_ROLES, upper->value - 1));
1717 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1719 struct type_datum *upper;
1720 struct policydb *p = datap;
1724 while (upper->bounds) {
1725 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1726 printk(KERN_ERR "SELinux: type %s: "
1727 "too deep or looped boundary\n",
1732 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1736 if (upper->attribute) {
1737 printk(KERN_ERR "SELinux: type %s: "
1738 "bounded by attribute %s",
1740 sym_name(p, SYM_TYPES, upper->value - 1));
1748 static int policydb_bounds_sanity_check(struct policydb *p)
1752 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1755 rc = hashtab_map(p->p_users.table,
1756 user_bounds_sanity_check, p);
1760 rc = hashtab_map(p->p_roles.table,
1761 role_bounds_sanity_check, p);
1765 rc = hashtab_map(p->p_types.table,
1766 type_bounds_sanity_check, p);
1773 u16 string_to_security_class(struct policydb *p, const char *name)
1775 struct class_datum *cladatum;
1777 cladatum = hashtab_search(p->p_classes.table, name);
1781 return cladatum->value;
1784 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1786 struct class_datum *cladatum;
1787 struct perm_datum *perdatum = NULL;
1788 struct common_datum *comdatum;
1790 if (!tclass || tclass > p->p_classes.nprim)
1793 cladatum = p->class_val_to_struct[tclass-1];
1794 comdatum = cladatum->comdatum;
1796 perdatum = hashtab_search(comdatum->permissions.table,
1799 perdatum = hashtab_search(cladatum->permissions.table,
1804 return 1U << (perdatum->value-1);
1807 static int range_read(struct policydb *p, void *fp)
1809 struct range_trans *rt = NULL;
1810 struct mls_range *r = NULL;
1815 if (p->policyvers < POLICYDB_VERSION_MLS)
1818 rc = next_entry(buf, fp, sizeof(u32));
1822 nel = le32_to_cpu(buf[0]);
1823 for (i = 0; i < nel; i++) {
1825 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1829 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1833 rt->source_type = le32_to_cpu(buf[0]);
1834 rt->target_type = le32_to_cpu(buf[1]);
1835 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1836 rc = next_entry(buf, fp, sizeof(u32));
1839 rt->target_class = le32_to_cpu(buf[0]);
1841 rt->target_class = p->process_class;
1844 if (!policydb_type_isvalid(p, rt->source_type) ||
1845 !policydb_type_isvalid(p, rt->target_type) ||
1846 !policydb_class_isvalid(p, rt->target_class))
1850 r = kzalloc(sizeof(*r), GFP_KERNEL);
1854 rc = mls_read_range_helper(r, fp);
1859 if (!mls_range_isvalid(p, r)) {
1860 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1864 rc = hashtab_insert(p->range_tr, rt, r);
1871 hash_eval(p->range_tr, "rangetr");
1879 static int filename_trans_read(struct policydb *p, void *fp)
1881 struct filename_trans *ft;
1882 struct filename_trans_datum *otype;
1888 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1891 rc = next_entry(buf, fp, sizeof(u32));
1894 nel = le32_to_cpu(buf[0]);
1896 for (i = 0; i < nel; i++) {
1902 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1907 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1911 /* length of the path component string */
1912 rc = next_entry(buf, fp, sizeof(u32));
1915 len = le32_to_cpu(buf[0]);
1918 name = kmalloc(len + 1, GFP_KERNEL);
1924 /* path component string */
1925 rc = next_entry(name, fp, len);
1930 rc = next_entry(buf, fp, sizeof(u32) * 4);
1934 ft->stype = le32_to_cpu(buf[0]);
1935 ft->ttype = le32_to_cpu(buf[1]);
1936 ft->tclass = le32_to_cpu(buf[2]);
1938 otype->otype = le32_to_cpu(buf[3]);
1940 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1944 rc = hashtab_insert(p->filename_trans, ft, otype);
1947 * Do not return -EEXIST to the caller, or the system
1952 /* But free memory to avoid memory leak. */
1958 hash_eval(p->filename_trans, "filenametr");
1968 static int genfs_read(struct policydb *p, void *fp)
1971 u32 nel, nel2, len, len2;
1973 struct ocontext *l, *c;
1974 struct ocontext *newc = NULL;
1975 struct genfs *genfs_p, *genfs;
1976 struct genfs *newgenfs = NULL;
1978 rc = next_entry(buf, fp, sizeof(u32));
1981 nel = le32_to_cpu(buf[0]);
1983 for (i = 0; i < nel; i++) {
1984 rc = next_entry(buf, fp, sizeof(u32));
1987 len = le32_to_cpu(buf[0]);
1990 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1995 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1996 if (!newgenfs->fstype)
1999 rc = next_entry(newgenfs->fstype, fp, len);
2003 newgenfs->fstype[len] = 0;
2005 for (genfs_p = NULL, genfs = p->genfs; genfs;
2006 genfs_p = genfs, genfs = genfs->next) {
2008 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2009 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2013 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2016 newgenfs->next = genfs;
2018 genfs_p->next = newgenfs;
2020 p->genfs = newgenfs;
2024 rc = next_entry(buf, fp, sizeof(u32));
2028 nel2 = le32_to_cpu(buf[0]);
2029 for (j = 0; j < nel2; j++) {
2030 rc = next_entry(buf, fp, sizeof(u32));
2033 len = le32_to_cpu(buf[0]);
2036 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2041 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2045 rc = next_entry(newc->u.name, fp, len);
2048 newc->u.name[len] = 0;
2050 rc = next_entry(buf, fp, sizeof(u32));
2054 newc->v.sclass = le32_to_cpu(buf[0]);
2055 rc = context_read_and_validate(&newc->context[0], p, fp);
2059 for (l = NULL, c = genfs->head; c;
2060 l = c, c = c->next) {
2062 if (!strcmp(newc->u.name, c->u.name) &&
2063 (!c->v.sclass || !newc->v.sclass ||
2064 newc->v.sclass == c->v.sclass)) {
2065 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2066 genfs->fstype, c->u.name);
2069 len = strlen(newc->u.name);
2070 len2 = strlen(c->u.name);
2086 kfree(newgenfs->fstype);
2088 ocontext_destroy(newc, OCON_FSUSE);
2093 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2099 struct ocontext *l, *c;
2102 for (i = 0; i < info->ocon_num; i++) {
2103 rc = next_entry(buf, fp, sizeof(u32));
2106 nel = le32_to_cpu(buf[0]);
2109 for (j = 0; j < nel; j++) {
2111 c = kzalloc(sizeof(*c), GFP_KERNEL);
2117 p->ocontexts[i] = c;
2122 rc = next_entry(buf, fp, sizeof(u32));
2126 c->sid[0] = le32_to_cpu(buf[0]);
2127 rc = context_read_and_validate(&c->context[0], p, fp);
2133 rc = next_entry(buf, fp, sizeof(u32));
2136 len = le32_to_cpu(buf[0]);
2139 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2143 rc = next_entry(c->u.name, fp, len);
2148 rc = context_read_and_validate(&c->context[0], p, fp);
2151 rc = context_read_and_validate(&c->context[1], p, fp);
2156 rc = next_entry(buf, fp, sizeof(u32)*3);
2159 c->u.port.protocol = le32_to_cpu(buf[0]);
2160 c->u.port.low_port = le32_to_cpu(buf[1]);
2161 c->u.port.high_port = le32_to_cpu(buf[2]);
2162 rc = context_read_and_validate(&c->context[0], p, fp);
2167 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2170 c->u.node.addr = nodebuf[0]; /* network order */
2171 c->u.node.mask = nodebuf[1]; /* network order */
2172 rc = context_read_and_validate(&c->context[0], p, fp);
2177 rc = next_entry(buf, fp, sizeof(u32)*2);
2182 c->v.behavior = le32_to_cpu(buf[0]);
2183 if (c->v.behavior > SECURITY_FS_USE_NONE)
2187 len = le32_to_cpu(buf[1]);
2188 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2192 rc = next_entry(c->u.name, fp, len);
2196 rc = context_read_and_validate(&c->context[0], p, fp);
2203 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2206 for (k = 0; k < 4; k++)
2207 c->u.node6.addr[k] = nodebuf[k];
2208 for (k = 0; k < 4; k++)
2209 c->u.node6.mask[k] = nodebuf[k+4];
2210 rc = context_read_and_validate(&c->context[0], p, fp);
2224 * Read the configuration data from a policy database binary
2225 * representation file into a policy database structure.
2227 int policydb_read(struct policydb *p, void *fp)
2229 struct role_allow *ra, *lra;
2230 struct role_trans *tr, *ltr;
2233 u32 len, nprim, nel;
2236 struct policydb_compat_info *info;
2238 rc = policydb_init(p);
2242 /* Read the magic number and string length. */
2243 rc = next_entry(buf, fp, sizeof(u32) * 2);
2248 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2249 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2250 "not match expected magic number 0x%x\n",
2251 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2256 len = le32_to_cpu(buf[1]);
2257 if (len != strlen(POLICYDB_STRING)) {
2258 printk(KERN_ERR "SELinux: policydb string length %d does not "
2259 "match expected length %Zu\n",
2260 len, strlen(POLICYDB_STRING));
2265 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2266 if (!policydb_str) {
2267 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2268 "string of length %d\n", len);
2272 rc = next_entry(policydb_str, fp, len);
2274 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2275 kfree(policydb_str);
2280 policydb_str[len] = '\0';
2281 if (strcmp(policydb_str, POLICYDB_STRING)) {
2282 printk(KERN_ERR "SELinux: policydb string %s does not match "
2283 "my string %s\n", policydb_str, POLICYDB_STRING);
2284 kfree(policydb_str);
2287 /* Done with policydb_str. */
2288 kfree(policydb_str);
2289 policydb_str = NULL;
2291 /* Read the version and table sizes. */
2292 rc = next_entry(buf, fp, sizeof(u32)*4);
2297 p->policyvers = le32_to_cpu(buf[0]);
2298 if (p->policyvers < POLICYDB_VERSION_MIN ||
2299 p->policyvers > POLICYDB_VERSION_MAX) {
2300 printk(KERN_ERR "SELinux: policydb version %d does not match "
2301 "my version range %d-%d\n",
2302 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2306 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2310 if (p->policyvers < POLICYDB_VERSION_MLS) {
2311 printk(KERN_ERR "SELinux: security policydb version %d "
2312 "(MLS) not backwards compatible\n",
2317 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2318 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2320 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2321 rc = ebitmap_read(&p->policycaps, fp);
2326 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2327 rc = ebitmap_read(&p->permissive_map, fp);
2333 info = policydb_lookup_compat(p->policyvers);
2335 printk(KERN_ERR "SELinux: unable to find policy compat info "
2336 "for version %d\n", p->policyvers);
2341 if (le32_to_cpu(buf[2]) != info->sym_num ||
2342 le32_to_cpu(buf[3]) != info->ocon_num) {
2343 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2344 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2345 le32_to_cpu(buf[3]),
2346 info->sym_num, info->ocon_num);
2350 for (i = 0; i < info->sym_num; i++) {
2351 rc = next_entry(buf, fp, sizeof(u32)*2);
2354 nprim = le32_to_cpu(buf[0]);
2355 nel = le32_to_cpu(buf[1]);
2356 for (j = 0; j < nel; j++) {
2357 rc = read_f[i](p, p->symtab[i].table, fp);
2362 p->symtab[i].nprim = nprim;
2366 p->process_class = string_to_security_class(p, "process");
2367 if (!p->process_class)
2370 rc = avtab_read(&p->te_avtab, fp, p);
2374 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2375 rc = cond_read_list(p, fp);
2380 rc = next_entry(buf, fp, sizeof(u32));
2383 nel = le32_to_cpu(buf[0]);
2385 for (i = 0; i < nel; i++) {
2387 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2394 rc = next_entry(buf, fp, sizeof(u32)*3);
2399 tr->role = le32_to_cpu(buf[0]);
2400 tr->type = le32_to_cpu(buf[1]);
2401 tr->new_role = le32_to_cpu(buf[2]);
2402 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2403 rc = next_entry(buf, fp, sizeof(u32));
2406 tr->tclass = le32_to_cpu(buf[0]);
2408 tr->tclass = p->process_class;
2410 if (!policydb_role_isvalid(p, tr->role) ||
2411 !policydb_type_isvalid(p, tr->type) ||
2412 !policydb_class_isvalid(p, tr->tclass) ||
2413 !policydb_role_isvalid(p, tr->new_role))
2418 rc = next_entry(buf, fp, sizeof(u32));
2421 nel = le32_to_cpu(buf[0]);
2423 for (i = 0; i < nel; i++) {
2425 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2432 rc = next_entry(buf, fp, sizeof(u32)*2);
2437 ra->role = le32_to_cpu(buf[0]);
2438 ra->new_role = le32_to_cpu(buf[1]);
2439 if (!policydb_role_isvalid(p, ra->role) ||
2440 !policydb_role_isvalid(p, ra->new_role))
2445 rc = filename_trans_read(p, fp);
2449 rc = policydb_index(p);
2454 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2455 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2456 if (!p->process_trans_perms)
2459 rc = ocontext_read(p, info, fp);
2463 rc = genfs_read(p, fp);
2467 rc = range_read(p, fp);
2472 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2474 GFP_KERNEL | __GFP_ZERO);
2475 if (!p->type_attr_map_array)
2478 /* preallocate so we don't have to worry about the put ever failing */
2479 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2480 GFP_KERNEL | __GFP_ZERO);
2484 for (i = 0; i < p->p_types.nprim; i++) {
2485 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2489 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2490 rc = ebitmap_read(e, fp);
2494 /* add the type itself as the degenerate case */
2495 rc = ebitmap_set_bit(e, i, 1);
2500 rc = policydb_bounds_sanity_check(p);
2508 policydb_destroy(p);
2513 * Write a MLS level structure to a policydb binary
2514 * representation file.
2516 static int mls_write_level(struct mls_level *l, void *fp)
2521 buf[0] = cpu_to_le32(l->sens);
2522 rc = put_entry(buf, sizeof(u32), 1, fp);
2526 rc = ebitmap_write(&l->cat, fp);
2534 * Write a MLS range structure to a policydb binary
2535 * representation file.
2537 static int mls_write_range_helper(struct mls_range *r, void *fp)
2543 eq = mls_level_eq(&r->level[1], &r->level[0]);
2549 buf[0] = cpu_to_le32(items-1);
2550 buf[1] = cpu_to_le32(r->level[0].sens);
2552 buf[2] = cpu_to_le32(r->level[1].sens);
2554 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2556 rc = put_entry(buf, sizeof(u32), items, fp);
2560 rc = ebitmap_write(&r->level[0].cat, fp);
2564 rc = ebitmap_write(&r->level[1].cat, fp);
2572 static int sens_write(void *vkey, void *datum, void *ptr)
2575 struct level_datum *levdatum = datum;
2576 struct policy_data *pd = ptr;
2583 buf[0] = cpu_to_le32(len);
2584 buf[1] = cpu_to_le32(levdatum->isalias);
2585 rc = put_entry(buf, sizeof(u32), 2, fp);
2589 rc = put_entry(key, 1, len, fp);
2593 rc = mls_write_level(levdatum->level, fp);
2600 static int cat_write(void *vkey, void *datum, void *ptr)
2603 struct cat_datum *catdatum = datum;
2604 struct policy_data *pd = ptr;
2611 buf[0] = cpu_to_le32(len);
2612 buf[1] = cpu_to_le32(catdatum->value);
2613 buf[2] = cpu_to_le32(catdatum->isalias);
2614 rc = put_entry(buf, sizeof(u32), 3, fp);
2618 rc = put_entry(key, 1, len, fp);
2625 static int role_trans_write(struct policydb *p, void *fp)
2627 struct role_trans *r = p->role_tr;
2628 struct role_trans *tr;
2634 for (tr = r; tr; tr = tr->next)
2636 buf[0] = cpu_to_le32(nel);
2637 rc = put_entry(buf, sizeof(u32), 1, fp);
2640 for (tr = r; tr; tr = tr->next) {
2641 buf[0] = cpu_to_le32(tr->role);
2642 buf[1] = cpu_to_le32(tr->type);
2643 buf[2] = cpu_to_le32(tr->new_role);
2644 rc = put_entry(buf, sizeof(u32), 3, fp);
2647 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2648 buf[0] = cpu_to_le32(tr->tclass);
2649 rc = put_entry(buf, sizeof(u32), 1, fp);
2658 static int role_allow_write(struct role_allow *r, void *fp)
2660 struct role_allow *ra;
2666 for (ra = r; ra; ra = ra->next)
2668 buf[0] = cpu_to_le32(nel);
2669 rc = put_entry(buf, sizeof(u32), 1, fp);
2672 for (ra = r; ra; ra = ra->next) {
2673 buf[0] = cpu_to_le32(ra->role);
2674 buf[1] = cpu_to_le32(ra->new_role);
2675 rc = put_entry(buf, sizeof(u32), 2, fp);
2683 * Write a security context structure
2684 * to a policydb binary representation file.
2686 static int context_write(struct policydb *p, struct context *c,
2692 buf[0] = cpu_to_le32(c->user);
2693 buf[1] = cpu_to_le32(c->role);
2694 buf[2] = cpu_to_le32(c->type);
2696 rc = put_entry(buf, sizeof(u32), 3, fp);
2700 rc = mls_write_range_helper(&c->range, fp);
2708 * The following *_write functions are used to
2709 * write the symbol data to a policy database
2710 * binary representation file.
2713 static int perm_write(void *vkey, void *datum, void *fp)
2716 struct perm_datum *perdatum = datum;
2722 buf[0] = cpu_to_le32(len);
2723 buf[1] = cpu_to_le32(perdatum->value);
2724 rc = put_entry(buf, sizeof(u32), 2, fp);
2728 rc = put_entry(key, 1, len, fp);
2735 static int common_write(void *vkey, void *datum, void *ptr)
2738 struct common_datum *comdatum = datum;
2739 struct policy_data *pd = ptr;
2746 buf[0] = cpu_to_le32(len);
2747 buf[1] = cpu_to_le32(comdatum->value);
2748 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2749 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2750 rc = put_entry(buf, sizeof(u32), 4, fp);
2754 rc = put_entry(key, 1, len, fp);
2758 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2765 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2768 struct constraint_node *c;
2769 struct constraint_expr *e;
2774 for (c = node; c; c = c->next) {
2776 for (e = c->expr; e; e = e->next)
2778 buf[0] = cpu_to_le32(c->permissions);
2779 buf[1] = cpu_to_le32(nel);
2780 rc = put_entry(buf, sizeof(u32), 2, fp);
2783 for (e = c->expr; e; e = e->next) {
2784 buf[0] = cpu_to_le32(e->expr_type);
2785 buf[1] = cpu_to_le32(e->attr);
2786 buf[2] = cpu_to_le32(e->op);
2787 rc = put_entry(buf, sizeof(u32), 3, fp);
2791 switch (e->expr_type) {
2793 rc = ebitmap_write(&e->names, fp);
2806 static int class_write(void *vkey, void *datum, void *ptr)
2809 struct class_datum *cladatum = datum;
2810 struct policy_data *pd = ptr;
2812 struct policydb *p = pd->p;
2813 struct constraint_node *c;
2820 if (cladatum->comkey)
2821 len2 = strlen(cladatum->comkey);
2826 for (c = cladatum->constraints; c; c = c->next)
2829 buf[0] = cpu_to_le32(len);
2830 buf[1] = cpu_to_le32(len2);
2831 buf[2] = cpu_to_le32(cladatum->value);
2832 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2833 if (cladatum->permissions.table)
2834 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2837 buf[5] = cpu_to_le32(ncons);
2838 rc = put_entry(buf, sizeof(u32), 6, fp);
2842 rc = put_entry(key, 1, len, fp);
2846 if (cladatum->comkey) {
2847 rc = put_entry(cladatum->comkey, 1, len2, fp);
2852 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2856 rc = write_cons_helper(p, cladatum->constraints, fp);
2860 /* write out the validatetrans rule */
2862 for (c = cladatum->validatetrans; c; c = c->next)
2865 buf[0] = cpu_to_le32(ncons);
2866 rc = put_entry(buf, sizeof(u32), 1, fp);
2870 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2874 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2875 buf[0] = cpu_to_le32(cladatum->default_user);
2876 buf[1] = cpu_to_le32(cladatum->default_role);
2877 buf[2] = cpu_to_le32(cladatum->default_range);
2879 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2884 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2885 buf[0] = cpu_to_le32(cladatum->default_type);
2886 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2894 static int role_write(void *vkey, void *datum, void *ptr)
2897 struct role_datum *role = datum;
2898 struct policy_data *pd = ptr;
2900 struct policydb *p = pd->p;
2907 buf[items++] = cpu_to_le32(len);
2908 buf[items++] = cpu_to_le32(role->value);
2909 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2910 buf[items++] = cpu_to_le32(role->bounds);
2912 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2914 rc = put_entry(buf, sizeof(u32), items, fp);
2918 rc = put_entry(key, 1, len, fp);
2922 rc = ebitmap_write(&role->dominates, fp);
2926 rc = ebitmap_write(&role->types, fp);
2933 static int type_write(void *vkey, void *datum, void *ptr)
2936 struct type_datum *typdatum = datum;
2937 struct policy_data *pd = ptr;
2938 struct policydb *p = pd->p;
2946 buf[items++] = cpu_to_le32(len);
2947 buf[items++] = cpu_to_le32(typdatum->value);
2948 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2951 if (typdatum->primary)
2952 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2954 if (typdatum->attribute)
2955 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2957 buf[items++] = cpu_to_le32(properties);
2958 buf[items++] = cpu_to_le32(typdatum->bounds);
2960 buf[items++] = cpu_to_le32(typdatum->primary);
2962 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2963 rc = put_entry(buf, sizeof(u32), items, fp);
2967 rc = put_entry(key, 1, len, fp);
2974 static int user_write(void *vkey, void *datum, void *ptr)
2977 struct user_datum *usrdatum = datum;
2978 struct policy_data *pd = ptr;
2979 struct policydb *p = pd->p;
2987 buf[items++] = cpu_to_le32(len);
2988 buf[items++] = cpu_to_le32(usrdatum->value);
2989 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2990 buf[items++] = cpu_to_le32(usrdatum->bounds);
2991 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2992 rc = put_entry(buf, sizeof(u32), items, fp);
2996 rc = put_entry(key, 1, len, fp);
3000 rc = ebitmap_write(&usrdatum->roles, fp);
3004 rc = mls_write_range_helper(&usrdatum->range, fp);
3008 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3015 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3028 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3031 unsigned int i, j, rc;
3036 for (i = 0; i < info->ocon_num; i++) {
3038 for (c = p->ocontexts[i]; c; c = c->next)
3040 buf[0] = cpu_to_le32(nel);
3041 rc = put_entry(buf, sizeof(u32), 1, fp);
3044 for (c = p->ocontexts[i]; c; c = c->next) {
3047 buf[0] = cpu_to_le32(c->sid[0]);
3048 rc = put_entry(buf, sizeof(u32), 1, fp);
3051 rc = context_write(p, &c->context[0], fp);
3057 len = strlen(c->u.name);
3058 buf[0] = cpu_to_le32(len);
3059 rc = put_entry(buf, sizeof(u32), 1, fp);
3062 rc = put_entry(c->u.name, 1, len, fp);
3065 rc = context_write(p, &c->context[0], fp);
3068 rc = context_write(p, &c->context[1], fp);
3073 buf[0] = cpu_to_le32(c->u.port.protocol);
3074 buf[1] = cpu_to_le32(c->u.port.low_port);
3075 buf[2] = cpu_to_le32(c->u.port.high_port);
3076 rc = put_entry(buf, sizeof(u32), 3, fp);
3079 rc = context_write(p, &c->context[0], fp);
3084 nodebuf[0] = c->u.node.addr; /* network order */
3085 nodebuf[1] = c->u.node.mask; /* network order */
3086 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3089 rc = context_write(p, &c->context[0], fp);
3094 buf[0] = cpu_to_le32(c->v.behavior);
3095 len = strlen(c->u.name);
3096 buf[1] = cpu_to_le32(len);
3097 rc = put_entry(buf, sizeof(u32), 2, fp);
3100 rc = put_entry(c->u.name, 1, len, fp);
3103 rc = context_write(p, &c->context[0], fp);
3108 for (j = 0; j < 4; j++)
3109 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3110 for (j = 0; j < 4; j++)
3111 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3112 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3115 rc = context_write(p, &c->context[0], fp);
3125 static int genfs_write(struct policydb *p, void *fp)
3127 struct genfs *genfs;
3134 for (genfs = p->genfs; genfs; genfs = genfs->next)
3136 buf[0] = cpu_to_le32(len);
3137 rc = put_entry(buf, sizeof(u32), 1, fp);
3140 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3141 len = strlen(genfs->fstype);
3142 buf[0] = cpu_to_le32(len);
3143 rc = put_entry(buf, sizeof(u32), 1, fp);
3146 rc = put_entry(genfs->fstype, 1, len, fp);
3150 for (c = genfs->head; c; c = c->next)
3152 buf[0] = cpu_to_le32(len);
3153 rc = put_entry(buf, sizeof(u32), 1, fp);
3156 for (c = genfs->head; c; c = c->next) {
3157 len = strlen(c->u.name);
3158 buf[0] = cpu_to_le32(len);
3159 rc = put_entry(buf, sizeof(u32), 1, fp);
3162 rc = put_entry(c->u.name, 1, len, fp);
3165 buf[0] = cpu_to_le32(c->v.sclass);
3166 rc = put_entry(buf, sizeof(u32), 1, fp);
3169 rc = context_write(p, &c->context[0], fp);
3177 static int hashtab_cnt(void *key, void *data, void *ptr)
3185 static int range_write_helper(void *key, void *data, void *ptr)
3188 struct range_trans *rt = key;
3189 struct mls_range *r = data;
3190 struct policy_data *pd = ptr;
3192 struct policydb *p = pd->p;
3195 buf[0] = cpu_to_le32(rt->source_type);
3196 buf[1] = cpu_to_le32(rt->target_type);
3197 rc = put_entry(buf, sizeof(u32), 2, fp);
3200 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3201 buf[0] = cpu_to_le32(rt->target_class);
3202 rc = put_entry(buf, sizeof(u32), 1, fp);
3206 rc = mls_write_range_helper(r, fp);
3213 static int range_write(struct policydb *p, void *fp)
3218 struct policy_data pd;
3223 /* count the number of entries in the hashtab */
3225 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3229 buf[0] = cpu_to_le32(nel);
3230 rc = put_entry(buf, sizeof(u32), 1, fp);
3234 /* actually write all of the entries */
3235 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3242 static int filename_write_helper(void *key, void *data, void *ptr)
3245 struct filename_trans *ft = key;
3246 struct filename_trans_datum *otype = data;
3251 len = strlen(ft->name);
3252 buf[0] = cpu_to_le32(len);
3253 rc = put_entry(buf, sizeof(u32), 1, fp);
3257 rc = put_entry(ft->name, sizeof(char), len, fp);
3261 buf[0] = cpu_to_le32(ft->stype);
3262 buf[1] = cpu_to_le32(ft->ttype);
3263 buf[2] = cpu_to_le32(ft->tclass);
3264 buf[3] = cpu_to_le32(otype->otype);
3266 rc = put_entry(buf, sizeof(u32), 4, fp);
3273 static int filename_trans_write(struct policydb *p, void *fp)
3279 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3283 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3287 buf[0] = cpu_to_le32(nel);
3288 rc = put_entry(buf, sizeof(u32), 1, fp);
3292 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3300 * Write the configuration data in a policy database
3301 * structure to a policy database binary representation
3304 int policydb_write(struct policydb *p, void *fp)
3306 unsigned int i, num_syms;
3311 struct policydb_compat_info *info;
3314 * refuse to write policy older than compressed avtab
3315 * to simplify the writer. There are other tests dropped
3316 * since we assume this throughout the writer code. Be
3317 * careful if you ever try to remove this restriction
3319 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3320 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3321 " Because it is less than version %d\n", p->policyvers,
3322 POLICYDB_VERSION_AVTAB);
3328 config |= POLICYDB_CONFIG_MLS;
3330 if (p->reject_unknown)
3331 config |= REJECT_UNKNOWN;
3332 if (p->allow_unknown)
3333 config |= ALLOW_UNKNOWN;
3335 /* Write the magic number and string identifiers. */
3336 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3337 len = strlen(POLICYDB_STRING);
3338 buf[1] = cpu_to_le32(len);
3339 rc = put_entry(buf, sizeof(u32), 2, fp);
3342 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3346 /* Write the version, config, and table sizes. */
3347 info = policydb_lookup_compat(p->policyvers);
3349 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3350 "version %d", p->policyvers);
3354 buf[0] = cpu_to_le32(p->policyvers);
3355 buf[1] = cpu_to_le32(config);
3356 buf[2] = cpu_to_le32(info->sym_num);
3357 buf[3] = cpu_to_le32(info->ocon_num);
3359 rc = put_entry(buf, sizeof(u32), 4, fp);
3363 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3364 rc = ebitmap_write(&p->policycaps, fp);
3369 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3370 rc = ebitmap_write(&p->permissive_map, fp);
3375 num_syms = info->sym_num;
3376 for (i = 0; i < num_syms; i++) {
3377 struct policy_data pd;
3382 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3383 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3385 rc = put_entry(buf, sizeof(u32), 2, fp);
3388 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3393 rc = avtab_write(p, &p->te_avtab, fp);
3397 rc = cond_write_list(p, p->cond_list, fp);
3401 rc = role_trans_write(p, fp);
3405 rc = role_allow_write(p->role_allow, fp);
3409 rc = filename_trans_write(p, fp);
3413 rc = ocontext_write(p, info, fp);
3417 rc = genfs_write(p, fp);
3421 rc = range_write(p, fp);
3425 for (i = 0; i < p->p_types.nprim; i++) {
3426 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3429 rc = ebitmap_write(e, fp);