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,
143 static struct policydb_compat_info *policydb_lookup_compat(int version)
146 struct policydb_compat_info *info = NULL;
148 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
149 if (policydb_compat[i].version == version) {
150 info = &policydb_compat[i];
158 * Initialize the role table.
160 static int roles_init(struct policydb *p)
164 struct role_datum *role;
167 role = kzalloc(sizeof(*role), GFP_KERNEL);
172 role->value = ++p->p_roles.nprim;
173 if (role->value != OBJECT_R_VAL)
177 key = kstrdup(OBJECT_R, GFP_KERNEL);
181 rc = hashtab_insert(p->p_roles.table, key, role);
192 static u32 filenametr_hash(struct hashtab *h, const void *k)
194 const struct filename_trans *ft = k;
196 unsigned int byte_num;
199 hash = ft->stype ^ ft->ttype ^ ft->tclass;
202 while ((focus = ft->name[byte_num++]))
203 hash = partial_name_hash(focus, hash);
204 return hash & (h->size - 1);
207 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
209 const struct filename_trans *ft1 = k1;
210 const struct filename_trans *ft2 = k2;
213 v = ft1->stype - ft2->stype;
217 v = ft1->ttype - ft2->ttype;
221 v = ft1->tclass - ft2->tclass;
225 return strcmp(ft1->name, ft2->name);
229 static u32 rangetr_hash(struct hashtab *h, const void *k)
231 const struct range_trans *key = k;
232 return (key->source_type + (key->target_type << 3) +
233 (key->target_class << 5)) & (h->size - 1);
236 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
238 const struct range_trans *key1 = k1, *key2 = k2;
241 v = key1->source_type - key2->source_type;
245 v = key1->target_type - key2->target_type;
249 v = key1->target_class - key2->target_class;
255 * Initialize a policy database structure.
257 static int policydb_init(struct policydb *p)
261 memset(p, 0, sizeof(*p));
263 for (i = 0; i < SYM_NUM; i++) {
264 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
269 rc = avtab_init(&p->te_avtab);
277 rc = cond_policydb_init(p);
281 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
282 if (!p->filename_trans)
285 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
289 ebitmap_init(&p->filename_trans_ttypes);
290 ebitmap_init(&p->policycaps);
291 ebitmap_init(&p->permissive_map);
295 hashtab_destroy(p->filename_trans);
296 hashtab_destroy(p->range_tr);
297 for (i = 0; i < SYM_NUM; i++)
298 hashtab_destroy(p->symtab[i].table);
303 * The following *_index functions are used to
304 * define the val_to_name and val_to_struct arrays
305 * in a policy database structure. The val_to_name
306 * arrays are used when converting security context
307 * structures into string representations. The
308 * val_to_struct arrays are used when the attributes
309 * of a class, role, or user are needed.
312 static int common_index(void *key, void *datum, void *datap)
315 struct common_datum *comdatum;
316 struct flex_array *fa;
320 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
323 fa = p->sym_val_to_name[SYM_COMMONS];
324 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
325 GFP_KERNEL | __GFP_ZERO))
330 static int class_index(void *key, void *datum, void *datap)
333 struct class_datum *cladatum;
334 struct flex_array *fa;
338 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
340 fa = p->sym_val_to_name[SYM_CLASSES];
341 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
342 GFP_KERNEL | __GFP_ZERO))
344 p->class_val_to_struct[cladatum->value - 1] = cladatum;
348 static int role_index(void *key, void *datum, void *datap)
351 struct role_datum *role;
352 struct flex_array *fa;
357 || role->value > p->p_roles.nprim
358 || role->bounds > p->p_roles.nprim)
361 fa = p->sym_val_to_name[SYM_ROLES];
362 if (flex_array_put_ptr(fa, role->value - 1, key,
363 GFP_KERNEL | __GFP_ZERO))
365 p->role_val_to_struct[role->value - 1] = role;
369 static int type_index(void *key, void *datum, void *datap)
372 struct type_datum *typdatum;
373 struct flex_array *fa;
378 if (typdatum->primary) {
380 || typdatum->value > p->p_types.nprim
381 || typdatum->bounds > p->p_types.nprim)
383 fa = p->sym_val_to_name[SYM_TYPES];
384 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
385 GFP_KERNEL | __GFP_ZERO))
388 fa = p->type_val_to_struct_array;
389 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
390 GFP_KERNEL | __GFP_ZERO))
397 static int user_index(void *key, void *datum, void *datap)
400 struct user_datum *usrdatum;
401 struct flex_array *fa;
406 || usrdatum->value > p->p_users.nprim
407 || usrdatum->bounds > p->p_users.nprim)
410 fa = p->sym_val_to_name[SYM_USERS];
411 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
412 GFP_KERNEL | __GFP_ZERO))
414 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
418 static int sens_index(void *key, void *datum, void *datap)
421 struct level_datum *levdatum;
422 struct flex_array *fa;
427 if (!levdatum->isalias) {
428 if (!levdatum->level->sens ||
429 levdatum->level->sens > p->p_levels.nprim)
431 fa = p->sym_val_to_name[SYM_LEVELS];
432 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
433 GFP_KERNEL | __GFP_ZERO))
440 static int cat_index(void *key, void *datum, void *datap)
443 struct cat_datum *catdatum;
444 struct flex_array *fa;
449 if (!catdatum->isalias) {
450 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
452 fa = p->sym_val_to_name[SYM_CATS];
453 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
454 GFP_KERNEL | __GFP_ZERO))
461 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
474 static void hash_eval(struct hashtab *h, const char *hash_name)
476 struct hashtab_info info;
478 hashtab_stat(h, &info);
479 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
480 "longest chain length %d\n", hash_name, h->nel,
481 info.slots_used, h->size, info.max_chain_len);
484 static void symtab_hash_eval(struct symtab *s)
488 for (i = 0; i < SYM_NUM; i++)
489 hash_eval(s[i].table, symtab_name[i]);
493 static inline void hash_eval(struct hashtab *h, char *hash_name)
499 * Define the other val_to_name and val_to_struct arrays
500 * in a policy database structure.
502 * Caller must clean up on failure.
504 static int policydb_index(struct policydb *p)
508 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
509 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
511 printk(", %d sens, %d cats", p->p_levels.nprim,
515 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
516 p->p_classes.nprim, p->te_avtab.nel);
519 avtab_hash_eval(&p->te_avtab, "rules");
520 symtab_hash_eval(p->symtab);
524 p->class_val_to_struct =
525 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
527 if (!p->class_val_to_struct)
531 p->role_val_to_struct =
532 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
534 if (!p->role_val_to_struct)
538 p->user_val_to_struct =
539 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
541 if (!p->user_val_to_struct)
544 /* Yes, I want the sizeof the pointer, not the structure */
546 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
548 GFP_KERNEL | __GFP_ZERO);
549 if (!p->type_val_to_struct_array)
552 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
553 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
557 rc = cond_init_bool_indexes(p);
561 for (i = 0; i < SYM_NUM; i++) {
563 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
565 GFP_KERNEL | __GFP_ZERO);
566 if (!p->sym_val_to_name[i])
569 rc = flex_array_prealloc(p->sym_val_to_name[i],
570 0, p->symtab[i].nprim,
571 GFP_KERNEL | __GFP_ZERO);
575 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
585 * The following *_destroy functions are used to
586 * free any memory allocated for each kind of
587 * symbol data in the policy database.
590 static int perm_destroy(void *key, void *datum, void *p)
597 static int common_destroy(void *key, void *datum, void *p)
599 struct common_datum *comdatum;
604 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
605 hashtab_destroy(comdatum->permissions.table);
611 static int cls_destroy(void *key, void *datum, void *p)
613 struct class_datum *cladatum;
614 struct constraint_node *constraint, *ctemp;
615 struct constraint_expr *e, *etmp;
620 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
621 hashtab_destroy(cladatum->permissions.table);
622 constraint = cladatum->constraints;
624 e = constraint->expr;
626 ebitmap_destroy(&e->names);
632 constraint = constraint->next;
636 constraint = cladatum->validatetrans;
638 e = constraint->expr;
640 ebitmap_destroy(&e->names);
646 constraint = constraint->next;
650 kfree(cladatum->comkey);
656 static int role_destroy(void *key, void *datum, void *p)
658 struct role_datum *role;
663 ebitmap_destroy(&role->dominates);
664 ebitmap_destroy(&role->types);
670 static int type_destroy(void *key, void *datum, void *p)
677 static int user_destroy(void *key, void *datum, void *p)
679 struct user_datum *usrdatum;
684 ebitmap_destroy(&usrdatum->roles);
685 ebitmap_destroy(&usrdatum->range.level[0].cat);
686 ebitmap_destroy(&usrdatum->range.level[1].cat);
687 ebitmap_destroy(&usrdatum->dfltlevel.cat);
693 static int sens_destroy(void *key, void *datum, void *p)
695 struct level_datum *levdatum;
700 ebitmap_destroy(&levdatum->level->cat);
701 kfree(levdatum->level);
707 static int cat_destroy(void *key, void *datum, void *p)
714 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
726 static int filenametr_destroy(void *key, void *datum, void *p)
728 struct filename_trans *ft = key;
736 static int range_tr_destroy(void *key, void *datum, void *p)
738 struct mls_range *rt = datum;
740 ebitmap_destroy(&rt->level[0].cat);
741 ebitmap_destroy(&rt->level[1].cat);
747 static void ocontext_destroy(struct ocontext *c, int i)
752 context_destroy(&c->context[0]);
753 context_destroy(&c->context[1]);
754 if (i == OCON_ISID || i == OCON_FS ||
755 i == OCON_NETIF || i == OCON_FSUSE)
761 * Free any memory allocated by a policy database structure.
763 void policydb_destroy(struct policydb *p)
765 struct ocontext *c, *ctmp;
766 struct genfs *g, *gtmp;
768 struct role_allow *ra, *lra = NULL;
769 struct role_trans *tr, *ltr = NULL;
771 for (i = 0; i < SYM_NUM; i++) {
773 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
774 hashtab_destroy(p->symtab[i].table);
777 for (i = 0; i < SYM_NUM; i++) {
778 if (p->sym_val_to_name[i])
779 flex_array_free(p->sym_val_to_name[i]);
782 kfree(p->class_val_to_struct);
783 kfree(p->role_val_to_struct);
784 kfree(p->user_val_to_struct);
785 if (p->type_val_to_struct_array)
786 flex_array_free(p->type_val_to_struct_array);
788 avtab_destroy(&p->te_avtab);
790 for (i = 0; i < OCON_NUM; i++) {
796 ocontext_destroy(ctmp, i);
798 p->ocontexts[i] = NULL;
809 ocontext_destroy(ctmp, OCON_FSUSE);
817 cond_policydb_destroy(p);
819 for (tr = p->role_tr; tr; tr = tr->next) {
826 for (ra = p->role_allow; ra; ra = ra->next) {
833 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
834 hashtab_destroy(p->filename_trans);
836 hashtab_map(p->range_tr, range_tr_destroy, NULL);
837 hashtab_destroy(p->range_tr);
839 if (p->type_attr_map_array) {
840 for (i = 0; i < p->p_types.nprim; i++) {
843 e = flex_array_get(p->type_attr_map_array, i);
848 flex_array_free(p->type_attr_map_array);
851 ebitmap_destroy(&p->filename_trans_ttypes);
852 ebitmap_destroy(&p->policycaps);
853 ebitmap_destroy(&p->permissive_map);
859 * Load the initial SIDs specified in a policy database
860 * structure into a SID table.
862 int policydb_load_isids(struct policydb *p, struct sidtab *s)
864 struct ocontext *head, *c;
869 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
873 head = p->ocontexts[OCON_ISID];
874 for (c = head; c; c = c->next) {
876 if (!c->context[0].user) {
877 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
882 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
884 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
894 int policydb_class_isvalid(struct policydb *p, unsigned int class)
896 if (!class || class > p->p_classes.nprim)
901 int policydb_role_isvalid(struct policydb *p, unsigned int role)
903 if (!role || role > p->p_roles.nprim)
908 int policydb_type_isvalid(struct policydb *p, unsigned int type)
910 if (!type || type > p->p_types.nprim)
916 * Return 1 if the fields in the security context
917 * structure `c' are valid. Return 0 otherwise.
919 int policydb_context_isvalid(struct policydb *p, struct context *c)
921 struct role_datum *role;
922 struct user_datum *usrdatum;
924 if (!c->role || c->role > p->p_roles.nprim)
927 if (!c->user || c->user > p->p_users.nprim)
930 if (!c->type || c->type > p->p_types.nprim)
933 if (c->role != OBJECT_R_VAL) {
935 * Role must be authorized for the type.
937 role = p->role_val_to_struct[c->role - 1];
938 if (!ebitmap_get_bit(&role->types, c->type - 1))
939 /* role may not be associated with type */
943 * User must be authorized for the role.
945 usrdatum = p->user_val_to_struct[c->user - 1];
949 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
950 /* user may not be associated with role */
954 if (!mls_context_isvalid(p, c))
961 * Read a MLS range structure from a policydb binary
962 * representation file.
964 static int mls_read_range_helper(struct mls_range *r, void *fp)
970 rc = next_entry(buf, fp, sizeof(u32));
975 items = le32_to_cpu(buf[0]);
976 if (items > ARRAY_SIZE(buf)) {
977 printk(KERN_ERR "SELinux: mls: range overflow\n");
981 rc = next_entry(buf, fp, sizeof(u32) * items);
983 printk(KERN_ERR "SELinux: mls: truncated range\n");
987 r->level[0].sens = le32_to_cpu(buf[0]);
989 r->level[1].sens = le32_to_cpu(buf[1]);
991 r->level[1].sens = r->level[0].sens;
993 rc = ebitmap_read(&r->level[0].cat, fp);
995 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
999 rc = ebitmap_read(&r->level[1].cat, fp);
1001 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
1005 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1007 printk(KERN_ERR "SELinux: mls: out of memory\n");
1014 ebitmap_destroy(&r->level[0].cat);
1020 * Read and validate a security context structure
1021 * from a policydb binary representation file.
1023 static int context_read_and_validate(struct context *c,
1030 rc = next_entry(buf, fp, sizeof buf);
1032 printk(KERN_ERR "SELinux: context truncated\n");
1035 c->user = le32_to_cpu(buf[0]);
1036 c->role = le32_to_cpu(buf[1]);
1037 c->type = le32_to_cpu(buf[2]);
1038 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1039 rc = mls_read_range_helper(&c->range, fp);
1041 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1047 if (!policydb_context_isvalid(p, c)) {
1048 printk(KERN_ERR "SELinux: invalid security context\n");
1058 * The following *_read functions are used to
1059 * read the symbol data from a policy database
1060 * binary representation file.
1063 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1066 struct perm_datum *perdatum;
1072 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1076 rc = next_entry(buf, fp, sizeof buf);
1080 len = le32_to_cpu(buf[0]);
1081 perdatum->value = le32_to_cpu(buf[1]);
1084 key = kmalloc(len + 1, GFP_KERNEL);
1088 rc = next_entry(key, fp, len);
1093 rc = hashtab_insert(h, key, perdatum);
1099 perm_destroy(key, perdatum, NULL);
1103 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1106 struct common_datum *comdatum;
1112 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1116 rc = next_entry(buf, fp, sizeof buf);
1120 len = le32_to_cpu(buf[0]);
1121 comdatum->value = le32_to_cpu(buf[1]);
1123 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1126 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1127 nel = le32_to_cpu(buf[3]);
1130 key = kmalloc(len + 1, GFP_KERNEL);
1134 rc = next_entry(key, fp, len);
1139 for (i = 0; i < nel; i++) {
1140 rc = perm_read(p, comdatum->permissions.table, fp);
1145 rc = hashtab_insert(h, key, comdatum);
1150 common_destroy(key, comdatum, NULL);
1154 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1155 int allowxtarget, void *fp)
1157 struct constraint_node *c, *lc;
1158 struct constraint_expr *e, *le;
1161 int rc, i, j, depth;
1164 for (i = 0; i < ncons; i++) {
1165 c = kzalloc(sizeof(*c), GFP_KERNEL);
1174 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1177 c->permissions = le32_to_cpu(buf[0]);
1178 nexpr = le32_to_cpu(buf[1]);
1181 for (j = 0; j < nexpr; j++) {
1182 e = kzalloc(sizeof(*e), GFP_KERNEL);
1191 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1194 e->expr_type = le32_to_cpu(buf[0]);
1195 e->attr = le32_to_cpu(buf[1]);
1196 e->op = le32_to_cpu(buf[2]);
1198 switch (e->expr_type) {
1210 if (depth == (CEXPR_MAXDEPTH - 1))
1215 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1217 if (depth == (CEXPR_MAXDEPTH - 1))
1220 rc = ebitmap_read(&e->names, fp);
1237 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1240 struct class_datum *cladatum;
1242 u32 len, len2, ncons, nel;
1246 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1250 rc = next_entry(buf, fp, sizeof(u32)*6);
1254 len = le32_to_cpu(buf[0]);
1255 len2 = le32_to_cpu(buf[1]);
1256 cladatum->value = le32_to_cpu(buf[2]);
1258 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1261 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1262 nel = le32_to_cpu(buf[4]);
1264 ncons = le32_to_cpu(buf[5]);
1267 key = kmalloc(len + 1, GFP_KERNEL);
1271 rc = next_entry(key, fp, len);
1278 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1279 if (!cladatum->comkey)
1281 rc = next_entry(cladatum->comkey, fp, len2);
1284 cladatum->comkey[len2] = '\0';
1287 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1288 if (!cladatum->comdatum) {
1289 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1293 for (i = 0; i < nel; i++) {
1294 rc = perm_read(p, cladatum->permissions.table, fp);
1299 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1303 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1304 /* grab the validatetrans rules */
1305 rc = next_entry(buf, fp, sizeof(u32));
1308 ncons = le32_to_cpu(buf[0]);
1309 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1314 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1315 rc = next_entry(buf, fp, sizeof(u32) * 3);
1319 cladatum->default_user = le32_to_cpu(buf[0]);
1320 cladatum->default_role = le32_to_cpu(buf[1]);
1321 cladatum->default_range = le32_to_cpu(buf[2]);
1324 rc = hashtab_insert(h, key, cladatum);
1330 cls_destroy(key, cladatum, NULL);
1334 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1337 struct role_datum *role;
1338 int rc, to_read = 2;
1343 role = kzalloc(sizeof(*role), GFP_KERNEL);
1347 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1350 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1354 len = le32_to_cpu(buf[0]);
1355 role->value = le32_to_cpu(buf[1]);
1356 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1357 role->bounds = le32_to_cpu(buf[2]);
1360 key = kmalloc(len + 1, GFP_KERNEL);
1364 rc = next_entry(key, fp, len);
1369 rc = ebitmap_read(&role->dominates, fp);
1373 rc = ebitmap_read(&role->types, fp);
1377 if (strcmp(key, OBJECT_R) == 0) {
1379 if (role->value != OBJECT_R_VAL) {
1380 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1381 OBJECT_R, role->value);
1388 rc = hashtab_insert(h, key, role);
1393 role_destroy(key, role, NULL);
1397 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1400 struct type_datum *typdatum;
1401 int rc, to_read = 3;
1406 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1410 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1413 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1417 len = le32_to_cpu(buf[0]);
1418 typdatum->value = le32_to_cpu(buf[1]);
1419 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1420 u32 prop = le32_to_cpu(buf[2]);
1422 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1423 typdatum->primary = 1;
1424 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1425 typdatum->attribute = 1;
1427 typdatum->bounds = le32_to_cpu(buf[3]);
1429 typdatum->primary = le32_to_cpu(buf[2]);
1433 key = kmalloc(len + 1, GFP_KERNEL);
1436 rc = next_entry(key, fp, len);
1441 rc = hashtab_insert(h, key, typdatum);
1446 type_destroy(key, typdatum, NULL);
1452 * Read a MLS level structure from a policydb binary
1453 * representation file.
1455 static int mls_read_level(struct mls_level *lp, void *fp)
1460 memset(lp, 0, sizeof(*lp));
1462 rc = next_entry(buf, fp, sizeof buf);
1464 printk(KERN_ERR "SELinux: mls: truncated level\n");
1467 lp->sens = le32_to_cpu(buf[0]);
1469 rc = ebitmap_read(&lp->cat, fp);
1471 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1477 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1480 struct user_datum *usrdatum;
1481 int rc, to_read = 2;
1486 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1490 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1493 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1497 len = le32_to_cpu(buf[0]);
1498 usrdatum->value = le32_to_cpu(buf[1]);
1499 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1500 usrdatum->bounds = le32_to_cpu(buf[2]);
1503 key = kmalloc(len + 1, GFP_KERNEL);
1506 rc = next_entry(key, fp, len);
1511 rc = ebitmap_read(&usrdatum->roles, fp);
1515 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1516 rc = mls_read_range_helper(&usrdatum->range, fp);
1519 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1524 rc = hashtab_insert(h, key, usrdatum);
1529 user_destroy(key, usrdatum, NULL);
1533 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1536 struct level_datum *levdatum;
1542 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1546 rc = next_entry(buf, fp, sizeof buf);
1550 len = le32_to_cpu(buf[0]);
1551 levdatum->isalias = le32_to_cpu(buf[1]);
1554 key = kmalloc(len + 1, GFP_ATOMIC);
1557 rc = next_entry(key, fp, len);
1563 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1564 if (!levdatum->level)
1567 rc = mls_read_level(levdatum->level, fp);
1571 rc = hashtab_insert(h, key, levdatum);
1576 sens_destroy(key, levdatum, NULL);
1580 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1583 struct cat_datum *catdatum;
1589 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1593 rc = next_entry(buf, fp, sizeof buf);
1597 len = le32_to_cpu(buf[0]);
1598 catdatum->value = le32_to_cpu(buf[1]);
1599 catdatum->isalias = le32_to_cpu(buf[2]);
1602 key = kmalloc(len + 1, GFP_ATOMIC);
1605 rc = next_entry(key, fp, len);
1610 rc = hashtab_insert(h, key, catdatum);
1615 cat_destroy(key, catdatum, NULL);
1619 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1631 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1633 struct user_datum *upper, *user;
1634 struct policydb *p = datap;
1637 upper = user = datum;
1638 while (upper->bounds) {
1639 struct ebitmap_node *node;
1642 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1643 printk(KERN_ERR "SELinux: user %s: "
1644 "too deep or looped boundary",
1649 upper = p->user_val_to_struct[upper->bounds - 1];
1650 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1651 if (ebitmap_get_bit(&upper->roles, bit))
1655 "SELinux: boundary violated policy: "
1656 "user=%s role=%s bounds=%s\n",
1657 sym_name(p, SYM_USERS, user->value - 1),
1658 sym_name(p, SYM_ROLES, bit),
1659 sym_name(p, SYM_USERS, upper->value - 1));
1668 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1670 struct role_datum *upper, *role;
1671 struct policydb *p = datap;
1674 upper = role = datum;
1675 while (upper->bounds) {
1676 struct ebitmap_node *node;
1679 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1680 printk(KERN_ERR "SELinux: role %s: "
1681 "too deep or looped bounds\n",
1686 upper = p->role_val_to_struct[upper->bounds - 1];
1687 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1688 if (ebitmap_get_bit(&upper->types, bit))
1692 "SELinux: boundary violated policy: "
1693 "role=%s type=%s bounds=%s\n",
1694 sym_name(p, SYM_ROLES, role->value - 1),
1695 sym_name(p, SYM_TYPES, bit),
1696 sym_name(p, SYM_ROLES, upper->value - 1));
1705 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1707 struct type_datum *upper;
1708 struct policydb *p = datap;
1712 while (upper->bounds) {
1713 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1714 printk(KERN_ERR "SELinux: type %s: "
1715 "too deep or looped boundary\n",
1720 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1724 if (upper->attribute) {
1725 printk(KERN_ERR "SELinux: type %s: "
1726 "bounded by attribute %s",
1728 sym_name(p, SYM_TYPES, upper->value - 1));
1736 static int policydb_bounds_sanity_check(struct policydb *p)
1740 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1743 rc = hashtab_map(p->p_users.table,
1744 user_bounds_sanity_check, p);
1748 rc = hashtab_map(p->p_roles.table,
1749 role_bounds_sanity_check, p);
1753 rc = hashtab_map(p->p_types.table,
1754 type_bounds_sanity_check, p);
1761 u16 string_to_security_class(struct policydb *p, const char *name)
1763 struct class_datum *cladatum;
1765 cladatum = hashtab_search(p->p_classes.table, name);
1769 return cladatum->value;
1772 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1774 struct class_datum *cladatum;
1775 struct perm_datum *perdatum = NULL;
1776 struct common_datum *comdatum;
1778 if (!tclass || tclass > p->p_classes.nprim)
1781 cladatum = p->class_val_to_struct[tclass-1];
1782 comdatum = cladatum->comdatum;
1784 perdatum = hashtab_search(comdatum->permissions.table,
1787 perdatum = hashtab_search(cladatum->permissions.table,
1792 return 1U << (perdatum->value-1);
1795 static int range_read(struct policydb *p, void *fp)
1797 struct range_trans *rt = NULL;
1798 struct mls_range *r = NULL;
1803 if (p->policyvers < POLICYDB_VERSION_MLS)
1806 rc = next_entry(buf, fp, sizeof(u32));
1810 nel = le32_to_cpu(buf[0]);
1811 for (i = 0; i < nel; i++) {
1813 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1817 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1821 rt->source_type = le32_to_cpu(buf[0]);
1822 rt->target_type = le32_to_cpu(buf[1]);
1823 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1824 rc = next_entry(buf, fp, sizeof(u32));
1827 rt->target_class = le32_to_cpu(buf[0]);
1829 rt->target_class = p->process_class;
1832 if (!policydb_type_isvalid(p, rt->source_type) ||
1833 !policydb_type_isvalid(p, rt->target_type) ||
1834 !policydb_class_isvalid(p, rt->target_class))
1838 r = kzalloc(sizeof(*r), GFP_KERNEL);
1842 rc = mls_read_range_helper(r, fp);
1847 if (!mls_range_isvalid(p, r)) {
1848 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1852 rc = hashtab_insert(p->range_tr, rt, r);
1859 hash_eval(p->range_tr, "rangetr");
1867 static int filename_trans_read(struct policydb *p, void *fp)
1869 struct filename_trans *ft;
1870 struct filename_trans_datum *otype;
1876 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1879 rc = next_entry(buf, fp, sizeof(u32));
1882 nel = le32_to_cpu(buf[0]);
1884 for (i = 0; i < nel; i++) {
1890 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1895 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1899 /* length of the path component string */
1900 rc = next_entry(buf, fp, sizeof(u32));
1903 len = le32_to_cpu(buf[0]);
1906 name = kmalloc(len + 1, GFP_KERNEL);
1912 /* path component string */
1913 rc = next_entry(name, fp, len);
1918 rc = next_entry(buf, fp, sizeof(u32) * 4);
1922 ft->stype = le32_to_cpu(buf[0]);
1923 ft->ttype = le32_to_cpu(buf[1]);
1924 ft->tclass = le32_to_cpu(buf[2]);
1926 otype->otype = le32_to_cpu(buf[3]);
1928 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1932 hashtab_insert(p->filename_trans, ft, otype);
1934 hash_eval(p->filename_trans, "filenametr");
1944 static int genfs_read(struct policydb *p, void *fp)
1947 u32 nel, nel2, len, len2;
1949 struct ocontext *l, *c;
1950 struct ocontext *newc = NULL;
1951 struct genfs *genfs_p, *genfs;
1952 struct genfs *newgenfs = NULL;
1954 rc = next_entry(buf, fp, sizeof(u32));
1957 nel = le32_to_cpu(buf[0]);
1959 for (i = 0; i < nel; i++) {
1960 rc = next_entry(buf, fp, sizeof(u32));
1963 len = le32_to_cpu(buf[0]);
1966 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1971 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1972 if (!newgenfs->fstype)
1975 rc = next_entry(newgenfs->fstype, fp, len);
1979 newgenfs->fstype[len] = 0;
1981 for (genfs_p = NULL, genfs = p->genfs; genfs;
1982 genfs_p = genfs, genfs = genfs->next) {
1984 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1985 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1989 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1992 newgenfs->next = genfs;
1994 genfs_p->next = newgenfs;
1996 p->genfs = newgenfs;
2000 rc = next_entry(buf, fp, sizeof(u32));
2004 nel2 = le32_to_cpu(buf[0]);
2005 for (j = 0; j < nel2; j++) {
2006 rc = next_entry(buf, fp, sizeof(u32));
2009 len = le32_to_cpu(buf[0]);
2012 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2017 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2021 rc = next_entry(newc->u.name, fp, len);
2024 newc->u.name[len] = 0;
2026 rc = next_entry(buf, fp, sizeof(u32));
2030 newc->v.sclass = le32_to_cpu(buf[0]);
2031 rc = context_read_and_validate(&newc->context[0], p, fp);
2035 for (l = NULL, c = genfs->head; c;
2036 l = c, c = c->next) {
2038 if (!strcmp(newc->u.name, c->u.name) &&
2039 (!c->v.sclass || !newc->v.sclass ||
2040 newc->v.sclass == c->v.sclass)) {
2041 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2042 genfs->fstype, c->u.name);
2045 len = strlen(newc->u.name);
2046 len2 = strlen(c->u.name);
2062 kfree(newgenfs->fstype);
2064 ocontext_destroy(newc, OCON_FSUSE);
2069 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2075 struct ocontext *l, *c;
2078 for (i = 0; i < info->ocon_num; i++) {
2079 rc = next_entry(buf, fp, sizeof(u32));
2082 nel = le32_to_cpu(buf[0]);
2085 for (j = 0; j < nel; j++) {
2087 c = kzalloc(sizeof(*c), GFP_KERNEL);
2093 p->ocontexts[i] = c;
2098 rc = next_entry(buf, fp, sizeof(u32));
2102 c->sid[0] = le32_to_cpu(buf[0]);
2103 rc = context_read_and_validate(&c->context[0], p, fp);
2109 rc = next_entry(buf, fp, sizeof(u32));
2112 len = le32_to_cpu(buf[0]);
2115 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2119 rc = next_entry(c->u.name, fp, len);
2124 rc = context_read_and_validate(&c->context[0], p, fp);
2127 rc = context_read_and_validate(&c->context[1], p, fp);
2132 rc = next_entry(buf, fp, sizeof(u32)*3);
2135 c->u.port.protocol = le32_to_cpu(buf[0]);
2136 c->u.port.low_port = le32_to_cpu(buf[1]);
2137 c->u.port.high_port = le32_to_cpu(buf[2]);
2138 rc = context_read_and_validate(&c->context[0], p, fp);
2143 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2146 c->u.node.addr = nodebuf[0]; /* network order */
2147 c->u.node.mask = nodebuf[1]; /* network order */
2148 rc = context_read_and_validate(&c->context[0], p, fp);
2153 rc = next_entry(buf, fp, sizeof(u32)*2);
2158 c->v.behavior = le32_to_cpu(buf[0]);
2159 if (c->v.behavior > SECURITY_FS_USE_NONE)
2163 len = le32_to_cpu(buf[1]);
2164 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2168 rc = next_entry(c->u.name, fp, len);
2172 rc = context_read_and_validate(&c->context[0], p, fp);
2179 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2182 for (k = 0; k < 4; k++)
2183 c->u.node6.addr[k] = nodebuf[k];
2184 for (k = 0; k < 4; k++)
2185 c->u.node6.mask[k] = nodebuf[k+4];
2186 rc = context_read_and_validate(&c->context[0], p, fp);
2200 * Read the configuration data from a policy database binary
2201 * representation file into a policy database structure.
2203 int policydb_read(struct policydb *p, void *fp)
2205 struct role_allow *ra, *lra;
2206 struct role_trans *tr, *ltr;
2209 u32 len, nprim, nel;
2212 struct policydb_compat_info *info;
2214 rc = policydb_init(p);
2218 /* Read the magic number and string length. */
2219 rc = next_entry(buf, fp, sizeof(u32) * 2);
2224 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2225 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2226 "not match expected magic number 0x%x\n",
2227 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2232 len = le32_to_cpu(buf[1]);
2233 if (len != strlen(POLICYDB_STRING)) {
2234 printk(KERN_ERR "SELinux: policydb string length %d does not "
2235 "match expected length %Zu\n",
2236 len, strlen(POLICYDB_STRING));
2241 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2242 if (!policydb_str) {
2243 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2244 "string of length %d\n", len);
2248 rc = next_entry(policydb_str, fp, len);
2250 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2251 kfree(policydb_str);
2256 policydb_str[len] = '\0';
2257 if (strcmp(policydb_str, POLICYDB_STRING)) {
2258 printk(KERN_ERR "SELinux: policydb string %s does not match "
2259 "my string %s\n", policydb_str, POLICYDB_STRING);
2260 kfree(policydb_str);
2263 /* Done with policydb_str. */
2264 kfree(policydb_str);
2265 policydb_str = NULL;
2267 /* Read the version and table sizes. */
2268 rc = next_entry(buf, fp, sizeof(u32)*4);
2273 p->policyvers = le32_to_cpu(buf[0]);
2274 if (p->policyvers < POLICYDB_VERSION_MIN ||
2275 p->policyvers > POLICYDB_VERSION_MAX) {
2276 printk(KERN_ERR "SELinux: policydb version %d does not match "
2277 "my version range %d-%d\n",
2278 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2282 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2286 if (p->policyvers < POLICYDB_VERSION_MLS) {
2287 printk(KERN_ERR "SELinux: security policydb version %d "
2288 "(MLS) not backwards compatible\n",
2293 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2294 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2296 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2297 rc = ebitmap_read(&p->policycaps, fp);
2302 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2303 rc = ebitmap_read(&p->permissive_map, fp);
2309 info = policydb_lookup_compat(p->policyvers);
2311 printk(KERN_ERR "SELinux: unable to find policy compat info "
2312 "for version %d\n", p->policyvers);
2317 if (le32_to_cpu(buf[2]) != info->sym_num ||
2318 le32_to_cpu(buf[3]) != info->ocon_num) {
2319 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2320 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2321 le32_to_cpu(buf[3]),
2322 info->sym_num, info->ocon_num);
2326 for (i = 0; i < info->sym_num; i++) {
2327 rc = next_entry(buf, fp, sizeof(u32)*2);
2330 nprim = le32_to_cpu(buf[0]);
2331 nel = le32_to_cpu(buf[1]);
2332 for (j = 0; j < nel; j++) {
2333 rc = read_f[i](p, p->symtab[i].table, fp);
2338 p->symtab[i].nprim = nprim;
2342 p->process_class = string_to_security_class(p, "process");
2343 if (!p->process_class)
2346 rc = avtab_read(&p->te_avtab, fp, p);
2350 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2351 rc = cond_read_list(p, fp);
2356 rc = next_entry(buf, fp, sizeof(u32));
2359 nel = le32_to_cpu(buf[0]);
2361 for (i = 0; i < nel; i++) {
2363 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2370 rc = next_entry(buf, fp, sizeof(u32)*3);
2375 tr->role = le32_to_cpu(buf[0]);
2376 tr->type = le32_to_cpu(buf[1]);
2377 tr->new_role = le32_to_cpu(buf[2]);
2378 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2379 rc = next_entry(buf, fp, sizeof(u32));
2382 tr->tclass = le32_to_cpu(buf[0]);
2384 tr->tclass = p->process_class;
2386 if (!policydb_role_isvalid(p, tr->role) ||
2387 !policydb_type_isvalid(p, tr->type) ||
2388 !policydb_class_isvalid(p, tr->tclass) ||
2389 !policydb_role_isvalid(p, tr->new_role))
2394 rc = next_entry(buf, fp, sizeof(u32));
2397 nel = le32_to_cpu(buf[0]);
2399 for (i = 0; i < nel; i++) {
2401 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2408 rc = next_entry(buf, fp, sizeof(u32)*2);
2413 ra->role = le32_to_cpu(buf[0]);
2414 ra->new_role = le32_to_cpu(buf[1]);
2415 if (!policydb_role_isvalid(p, ra->role) ||
2416 !policydb_role_isvalid(p, ra->new_role))
2421 rc = filename_trans_read(p, fp);
2425 rc = policydb_index(p);
2430 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2431 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2432 if (!p->process_trans_perms)
2435 rc = ocontext_read(p, info, fp);
2439 rc = genfs_read(p, fp);
2443 rc = range_read(p, fp);
2448 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2450 GFP_KERNEL | __GFP_ZERO);
2451 if (!p->type_attr_map_array)
2454 /* preallocate so we don't have to worry about the put ever failing */
2455 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2456 GFP_KERNEL | __GFP_ZERO);
2460 for (i = 0; i < p->p_types.nprim; i++) {
2461 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2465 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2466 rc = ebitmap_read(e, fp);
2470 /* add the type itself as the degenerate case */
2471 rc = ebitmap_set_bit(e, i, 1);
2476 rc = policydb_bounds_sanity_check(p);
2484 policydb_destroy(p);
2489 * Write a MLS level structure to a policydb binary
2490 * representation file.
2492 static int mls_write_level(struct mls_level *l, void *fp)
2497 buf[0] = cpu_to_le32(l->sens);
2498 rc = put_entry(buf, sizeof(u32), 1, fp);
2502 rc = ebitmap_write(&l->cat, fp);
2510 * Write a MLS range structure to a policydb binary
2511 * representation file.
2513 static int mls_write_range_helper(struct mls_range *r, void *fp)
2519 eq = mls_level_eq(&r->level[1], &r->level[0]);
2525 buf[0] = cpu_to_le32(items-1);
2526 buf[1] = cpu_to_le32(r->level[0].sens);
2528 buf[2] = cpu_to_le32(r->level[1].sens);
2530 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2532 rc = put_entry(buf, sizeof(u32), items, fp);
2536 rc = ebitmap_write(&r->level[0].cat, fp);
2540 rc = ebitmap_write(&r->level[1].cat, fp);
2548 static int sens_write(void *vkey, void *datum, void *ptr)
2551 struct level_datum *levdatum = datum;
2552 struct policy_data *pd = ptr;
2559 buf[0] = cpu_to_le32(len);
2560 buf[1] = cpu_to_le32(levdatum->isalias);
2561 rc = put_entry(buf, sizeof(u32), 2, fp);
2565 rc = put_entry(key, 1, len, fp);
2569 rc = mls_write_level(levdatum->level, fp);
2576 static int cat_write(void *vkey, void *datum, void *ptr)
2579 struct cat_datum *catdatum = datum;
2580 struct policy_data *pd = ptr;
2587 buf[0] = cpu_to_le32(len);
2588 buf[1] = cpu_to_le32(catdatum->value);
2589 buf[2] = cpu_to_le32(catdatum->isalias);
2590 rc = put_entry(buf, sizeof(u32), 3, fp);
2594 rc = put_entry(key, 1, len, fp);
2601 static int role_trans_write(struct policydb *p, void *fp)
2603 struct role_trans *r = p->role_tr;
2604 struct role_trans *tr;
2610 for (tr = r; tr; tr = tr->next)
2612 buf[0] = cpu_to_le32(nel);
2613 rc = put_entry(buf, sizeof(u32), 1, fp);
2616 for (tr = r; tr; tr = tr->next) {
2617 buf[0] = cpu_to_le32(tr->role);
2618 buf[1] = cpu_to_le32(tr->type);
2619 buf[2] = cpu_to_le32(tr->new_role);
2620 rc = put_entry(buf, sizeof(u32), 3, fp);
2623 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2624 buf[0] = cpu_to_le32(tr->tclass);
2625 rc = put_entry(buf, sizeof(u32), 1, fp);
2634 static int role_allow_write(struct role_allow *r, void *fp)
2636 struct role_allow *ra;
2642 for (ra = r; ra; ra = ra->next)
2644 buf[0] = cpu_to_le32(nel);
2645 rc = put_entry(buf, sizeof(u32), 1, fp);
2648 for (ra = r; ra; ra = ra->next) {
2649 buf[0] = cpu_to_le32(ra->role);
2650 buf[1] = cpu_to_le32(ra->new_role);
2651 rc = put_entry(buf, sizeof(u32), 2, fp);
2659 * Write a security context structure
2660 * to a policydb binary representation file.
2662 static int context_write(struct policydb *p, struct context *c,
2668 buf[0] = cpu_to_le32(c->user);
2669 buf[1] = cpu_to_le32(c->role);
2670 buf[2] = cpu_to_le32(c->type);
2672 rc = put_entry(buf, sizeof(u32), 3, fp);
2676 rc = mls_write_range_helper(&c->range, fp);
2684 * The following *_write functions are used to
2685 * write the symbol data to a policy database
2686 * binary representation file.
2689 static int perm_write(void *vkey, void *datum, void *fp)
2692 struct perm_datum *perdatum = datum;
2698 buf[0] = cpu_to_le32(len);
2699 buf[1] = cpu_to_le32(perdatum->value);
2700 rc = put_entry(buf, sizeof(u32), 2, fp);
2704 rc = put_entry(key, 1, len, fp);
2711 static int common_write(void *vkey, void *datum, void *ptr)
2714 struct common_datum *comdatum = datum;
2715 struct policy_data *pd = ptr;
2722 buf[0] = cpu_to_le32(len);
2723 buf[1] = cpu_to_le32(comdatum->value);
2724 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2725 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2726 rc = put_entry(buf, sizeof(u32), 4, fp);
2730 rc = put_entry(key, 1, len, fp);
2734 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2741 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2744 struct constraint_node *c;
2745 struct constraint_expr *e;
2750 for (c = node; c; c = c->next) {
2752 for (e = c->expr; e; e = e->next)
2754 buf[0] = cpu_to_le32(c->permissions);
2755 buf[1] = cpu_to_le32(nel);
2756 rc = put_entry(buf, sizeof(u32), 2, fp);
2759 for (e = c->expr; e; e = e->next) {
2760 buf[0] = cpu_to_le32(e->expr_type);
2761 buf[1] = cpu_to_le32(e->attr);
2762 buf[2] = cpu_to_le32(e->op);
2763 rc = put_entry(buf, sizeof(u32), 3, fp);
2767 switch (e->expr_type) {
2769 rc = ebitmap_write(&e->names, fp);
2782 static int class_write(void *vkey, void *datum, void *ptr)
2785 struct class_datum *cladatum = datum;
2786 struct policy_data *pd = ptr;
2788 struct policydb *p = pd->p;
2789 struct constraint_node *c;
2796 if (cladatum->comkey)
2797 len2 = strlen(cladatum->comkey);
2802 for (c = cladatum->constraints; c; c = c->next)
2805 buf[0] = cpu_to_le32(len);
2806 buf[1] = cpu_to_le32(len2);
2807 buf[2] = cpu_to_le32(cladatum->value);
2808 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2809 if (cladatum->permissions.table)
2810 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2813 buf[5] = cpu_to_le32(ncons);
2814 rc = put_entry(buf, sizeof(u32), 6, fp);
2818 rc = put_entry(key, 1, len, fp);
2822 if (cladatum->comkey) {
2823 rc = put_entry(cladatum->comkey, 1, len2, fp);
2828 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2832 rc = write_cons_helper(p, cladatum->constraints, fp);
2836 /* write out the validatetrans rule */
2838 for (c = cladatum->validatetrans; c; c = c->next)
2841 buf[0] = cpu_to_le32(ncons);
2842 rc = put_entry(buf, sizeof(u32), 1, fp);
2846 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2850 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2851 buf[0] = cpu_to_le32(cladatum->default_user);
2852 buf[1] = cpu_to_le32(cladatum->default_role);
2853 buf[2] = cpu_to_le32(cladatum->default_range);
2855 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2863 static int role_write(void *vkey, void *datum, void *ptr)
2866 struct role_datum *role = datum;
2867 struct policy_data *pd = ptr;
2869 struct policydb *p = pd->p;
2876 buf[items++] = cpu_to_le32(len);
2877 buf[items++] = cpu_to_le32(role->value);
2878 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2879 buf[items++] = cpu_to_le32(role->bounds);
2881 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2883 rc = put_entry(buf, sizeof(u32), items, fp);
2887 rc = put_entry(key, 1, len, fp);
2891 rc = ebitmap_write(&role->dominates, fp);
2895 rc = ebitmap_write(&role->types, fp);
2902 static int type_write(void *vkey, void *datum, void *ptr)
2905 struct type_datum *typdatum = datum;
2906 struct policy_data *pd = ptr;
2907 struct policydb *p = pd->p;
2915 buf[items++] = cpu_to_le32(len);
2916 buf[items++] = cpu_to_le32(typdatum->value);
2917 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2920 if (typdatum->primary)
2921 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2923 if (typdatum->attribute)
2924 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2926 buf[items++] = cpu_to_le32(properties);
2927 buf[items++] = cpu_to_le32(typdatum->bounds);
2929 buf[items++] = cpu_to_le32(typdatum->primary);
2931 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2932 rc = put_entry(buf, sizeof(u32), items, fp);
2936 rc = put_entry(key, 1, len, fp);
2943 static int user_write(void *vkey, void *datum, void *ptr)
2946 struct user_datum *usrdatum = datum;
2947 struct policy_data *pd = ptr;
2948 struct policydb *p = pd->p;
2956 buf[items++] = cpu_to_le32(len);
2957 buf[items++] = cpu_to_le32(usrdatum->value);
2958 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2959 buf[items++] = cpu_to_le32(usrdatum->bounds);
2960 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2961 rc = put_entry(buf, sizeof(u32), items, fp);
2965 rc = put_entry(key, 1, len, fp);
2969 rc = ebitmap_write(&usrdatum->roles, fp);
2973 rc = mls_write_range_helper(&usrdatum->range, fp);
2977 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2984 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2997 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3000 unsigned int i, j, rc;
3005 for (i = 0; i < info->ocon_num; i++) {
3007 for (c = p->ocontexts[i]; c; c = c->next)
3009 buf[0] = cpu_to_le32(nel);
3010 rc = put_entry(buf, sizeof(u32), 1, fp);
3013 for (c = p->ocontexts[i]; c; c = c->next) {
3016 buf[0] = cpu_to_le32(c->sid[0]);
3017 rc = put_entry(buf, sizeof(u32), 1, fp);
3020 rc = context_write(p, &c->context[0], fp);
3026 len = strlen(c->u.name);
3027 buf[0] = cpu_to_le32(len);
3028 rc = put_entry(buf, sizeof(u32), 1, fp);
3031 rc = put_entry(c->u.name, 1, len, fp);
3034 rc = context_write(p, &c->context[0], fp);
3037 rc = context_write(p, &c->context[1], fp);
3042 buf[0] = cpu_to_le32(c->u.port.protocol);
3043 buf[1] = cpu_to_le32(c->u.port.low_port);
3044 buf[2] = cpu_to_le32(c->u.port.high_port);
3045 rc = put_entry(buf, sizeof(u32), 3, fp);
3048 rc = context_write(p, &c->context[0], fp);
3053 nodebuf[0] = c->u.node.addr; /* network order */
3054 nodebuf[1] = c->u.node.mask; /* network order */
3055 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3058 rc = context_write(p, &c->context[0], fp);
3063 buf[0] = cpu_to_le32(c->v.behavior);
3064 len = strlen(c->u.name);
3065 buf[1] = cpu_to_le32(len);
3066 rc = put_entry(buf, sizeof(u32), 2, fp);
3069 rc = put_entry(c->u.name, 1, len, fp);
3072 rc = context_write(p, &c->context[0], fp);
3077 for (j = 0; j < 4; j++)
3078 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3079 for (j = 0; j < 4; j++)
3080 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3081 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3084 rc = context_write(p, &c->context[0], fp);
3094 static int genfs_write(struct policydb *p, void *fp)
3096 struct genfs *genfs;
3103 for (genfs = p->genfs; genfs; genfs = genfs->next)
3105 buf[0] = cpu_to_le32(len);
3106 rc = put_entry(buf, sizeof(u32), 1, fp);
3109 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3110 len = strlen(genfs->fstype);
3111 buf[0] = cpu_to_le32(len);
3112 rc = put_entry(buf, sizeof(u32), 1, fp);
3115 rc = put_entry(genfs->fstype, 1, len, fp);
3119 for (c = genfs->head; c; c = c->next)
3121 buf[0] = cpu_to_le32(len);
3122 rc = put_entry(buf, sizeof(u32), 1, fp);
3125 for (c = genfs->head; c; c = c->next) {
3126 len = strlen(c->u.name);
3127 buf[0] = cpu_to_le32(len);
3128 rc = put_entry(buf, sizeof(u32), 1, fp);
3131 rc = put_entry(c->u.name, 1, len, fp);
3134 buf[0] = cpu_to_le32(c->v.sclass);
3135 rc = put_entry(buf, sizeof(u32), 1, fp);
3138 rc = context_write(p, &c->context[0], fp);
3146 static int hashtab_cnt(void *key, void *data, void *ptr)
3154 static int range_write_helper(void *key, void *data, void *ptr)
3157 struct range_trans *rt = key;
3158 struct mls_range *r = data;
3159 struct policy_data *pd = ptr;
3161 struct policydb *p = pd->p;
3164 buf[0] = cpu_to_le32(rt->source_type);
3165 buf[1] = cpu_to_le32(rt->target_type);
3166 rc = put_entry(buf, sizeof(u32), 2, fp);
3169 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3170 buf[0] = cpu_to_le32(rt->target_class);
3171 rc = put_entry(buf, sizeof(u32), 1, fp);
3175 rc = mls_write_range_helper(r, fp);
3182 static int range_write(struct policydb *p, void *fp)
3187 struct policy_data pd;
3192 /* count the number of entries in the hashtab */
3194 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3198 buf[0] = cpu_to_le32(nel);
3199 rc = put_entry(buf, sizeof(u32), 1, fp);
3203 /* actually write all of the entries */
3204 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3211 static int filename_write_helper(void *key, void *data, void *ptr)
3214 struct filename_trans *ft = key;
3215 struct filename_trans_datum *otype = data;
3220 len = strlen(ft->name);
3221 buf[0] = cpu_to_le32(len);
3222 rc = put_entry(buf, sizeof(u32), 1, fp);
3226 rc = put_entry(ft->name, sizeof(char), len, fp);
3232 buf[2] = ft->tclass;
3233 buf[3] = otype->otype;
3235 rc = put_entry(buf, sizeof(u32), 4, fp);
3242 static int filename_trans_write(struct policydb *p, void *fp)
3248 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3252 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3256 buf[0] = cpu_to_le32(nel);
3257 rc = put_entry(buf, sizeof(u32), 1, fp);
3261 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3269 * Write the configuration data in a policy database
3270 * structure to a policy database binary representation
3273 int policydb_write(struct policydb *p, void *fp)
3275 unsigned int i, num_syms;
3280 struct policydb_compat_info *info;
3283 * refuse to write policy older than compressed avtab
3284 * to simplify the writer. There are other tests dropped
3285 * since we assume this throughout the writer code. Be
3286 * careful if you ever try to remove this restriction
3288 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3289 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3290 " Because it is less than version %d\n", p->policyvers,
3291 POLICYDB_VERSION_AVTAB);
3297 config |= POLICYDB_CONFIG_MLS;
3299 if (p->reject_unknown)
3300 config |= REJECT_UNKNOWN;
3301 if (p->allow_unknown)
3302 config |= ALLOW_UNKNOWN;
3304 /* Write the magic number and string identifiers. */
3305 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3306 len = strlen(POLICYDB_STRING);
3307 buf[1] = cpu_to_le32(len);
3308 rc = put_entry(buf, sizeof(u32), 2, fp);
3311 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3315 /* Write the version, config, and table sizes. */
3316 info = policydb_lookup_compat(p->policyvers);
3318 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3319 "version %d", p->policyvers);
3323 buf[0] = cpu_to_le32(p->policyvers);
3324 buf[1] = cpu_to_le32(config);
3325 buf[2] = cpu_to_le32(info->sym_num);
3326 buf[3] = cpu_to_le32(info->ocon_num);
3328 rc = put_entry(buf, sizeof(u32), 4, fp);
3332 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3333 rc = ebitmap_write(&p->policycaps, fp);
3338 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3339 rc = ebitmap_write(&p->permissive_map, fp);
3344 num_syms = info->sym_num;
3345 for (i = 0; i < num_syms; i++) {
3346 struct policy_data pd;
3351 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3352 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3354 rc = put_entry(buf, sizeof(u32), 2, fp);
3357 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3362 rc = avtab_write(p, &p->te_avtab, fp);
3366 rc = cond_write_list(p, p->cond_list, fp);
3370 rc = role_trans_write(p, fp);
3374 rc = role_allow_write(p->role_allow, fp);
3378 rc = filename_trans_write(p, fp);
3382 rc = ocontext_write(p, info, fp);
3386 rc = genfs_write(p, fp);
3390 rc = range_write(p, fp);
3394 for (i = 0; i < p->p_types.nprim; i++) {
3395 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3398 rc = ebitmap_write(e, fp);