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.moore@hp.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,
128 static struct policydb_compat_info *policydb_lookup_compat(int version)
131 struct policydb_compat_info *info = NULL;
133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
134 if (policydb_compat[i].version == version) {
135 info = &policydb_compat[i];
143 * Initialize the role table.
145 static int roles_init(struct policydb *p)
149 struct role_datum *role;
152 role = kzalloc(sizeof(*role), GFP_KERNEL);
157 role->value = ++p->p_roles.nprim;
158 if (role->value != OBJECT_R_VAL)
162 key = kstrdup(OBJECT_R, GFP_KERNEL);
166 rc = hashtab_insert(p->p_roles.table, key, role);
177 static u32 rangetr_hash(struct hashtab *h, const void *k)
179 const struct range_trans *key = k;
180 return (key->source_type + (key->target_type << 3) +
181 (key->target_class << 5)) & (h->size - 1);
184 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
186 const struct range_trans *key1 = k1, *key2 = k2;
189 v = key1->source_type - key2->source_type;
193 v = key1->target_type - key2->target_type;
197 v = key1->target_class - key2->target_class;
203 * Initialize a policy database structure.
205 static int policydb_init(struct policydb *p)
209 memset(p, 0, sizeof(*p));
211 for (i = 0; i < SYM_NUM; i++) {
212 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
217 rc = avtab_init(&p->te_avtab);
225 rc = cond_policydb_init(p);
229 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
233 ebitmap_init(&p->policycaps);
234 ebitmap_init(&p->permissive_map);
238 for (i = 0; i < SYM_NUM; i++)
239 hashtab_destroy(p->symtab[i].table);
244 * The following *_index functions are used to
245 * define the val_to_name and val_to_struct arrays
246 * in a policy database structure. The val_to_name
247 * arrays are used when converting security context
248 * structures into string representations. The
249 * val_to_struct arrays are used when the attributes
250 * of a class, role, or user are needed.
253 static int common_index(void *key, void *datum, void *datap)
256 struct common_datum *comdatum;
257 struct flex_array *fa;
261 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
264 fa = p->sym_val_to_name[SYM_COMMONS];
265 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
266 GFP_KERNEL | __GFP_ZERO))
271 static int class_index(void *key, void *datum, void *datap)
274 struct class_datum *cladatum;
275 struct flex_array *fa;
279 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
281 fa = p->sym_val_to_name[SYM_CLASSES];
282 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
283 GFP_KERNEL | __GFP_ZERO))
285 p->class_val_to_struct[cladatum->value - 1] = cladatum;
289 static int role_index(void *key, void *datum, void *datap)
292 struct role_datum *role;
293 struct flex_array *fa;
298 || role->value > p->p_roles.nprim
299 || role->bounds > p->p_roles.nprim)
302 fa = p->sym_val_to_name[SYM_ROLES];
303 if (flex_array_put_ptr(fa, role->value - 1, key,
304 GFP_KERNEL | __GFP_ZERO))
306 p->role_val_to_struct[role->value - 1] = role;
310 static int type_index(void *key, void *datum, void *datap)
313 struct type_datum *typdatum;
314 struct flex_array *fa;
319 if (typdatum->primary) {
321 || typdatum->value > p->p_types.nprim
322 || typdatum->bounds > p->p_types.nprim)
324 fa = p->sym_val_to_name[SYM_TYPES];
325 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
326 GFP_KERNEL | __GFP_ZERO))
329 fa = p->type_val_to_struct_array;
330 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
331 GFP_KERNEL | __GFP_ZERO))
338 static int user_index(void *key, void *datum, void *datap)
341 struct user_datum *usrdatum;
342 struct flex_array *fa;
347 || usrdatum->value > p->p_users.nprim
348 || usrdatum->bounds > p->p_users.nprim)
351 fa = p->sym_val_to_name[SYM_USERS];
352 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
353 GFP_KERNEL | __GFP_ZERO))
355 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
359 static int sens_index(void *key, void *datum, void *datap)
362 struct level_datum *levdatum;
363 struct flex_array *fa;
368 if (!levdatum->isalias) {
369 if (!levdatum->level->sens ||
370 levdatum->level->sens > p->p_levels.nprim)
372 fa = p->sym_val_to_name[SYM_LEVELS];
373 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
374 GFP_KERNEL | __GFP_ZERO))
381 static int cat_index(void *key, void *datum, void *datap)
384 struct cat_datum *catdatum;
385 struct flex_array *fa;
390 if (!catdatum->isalias) {
391 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
393 fa = p->sym_val_to_name[SYM_CATS];
394 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
402 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
415 * Define the common val_to_name array and the class
416 * val_to_name and val_to_struct arrays in a policy
417 * database structure.
419 * Caller must clean up upon failure.
421 static int policydb_index_classes(struct policydb *p)
426 p->sym_val_to_name[SYM_COMMONS] = flex_array_alloc(sizeof(char *),
428 GFP_KERNEL | __GFP_ZERO);
429 if (!p->sym_val_to_name[SYM_COMMONS])
432 rc = flex_array_prealloc(p->sym_val_to_name[SYM_COMMONS],
433 0, p->p_commons.nprim - 1,
434 GFP_KERNEL | __GFP_ZERO);
438 rc = hashtab_map(p->p_commons.table, common_index, p);
443 p->class_val_to_struct =
444 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
445 if (!p->class_val_to_struct)
449 p->sym_val_to_name[SYM_CLASSES] = flex_array_alloc(sizeof(char *),
451 GFP_KERNEL | __GFP_ZERO);
452 if (!p->sym_val_to_name[SYM_CLASSES])
455 rc = flex_array_prealloc(p->sym_val_to_name[SYM_CLASSES],
456 0, p->p_classes.nprim - 1,
457 GFP_KERNEL | __GFP_ZERO);
461 rc = hashtab_map(p->p_classes.table, class_index, p);
467 static void symtab_hash_eval(struct symtab *s)
471 for (i = 0; i < SYM_NUM; i++) {
472 struct hashtab *h = s[i].table;
473 struct hashtab_info info;
475 hashtab_stat(h, &info);
476 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
477 "longest chain length %d\n", symtab_name[i], h->nel,
478 info.slots_used, h->size, info.max_chain_len);
482 static void rangetr_hash_eval(struct hashtab *h)
484 struct hashtab_info info;
486 hashtab_stat(h, &info);
487 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
488 "longest chain length %d\n", h->nel,
489 info.slots_used, h->size, info.max_chain_len);
492 static inline void rangetr_hash_eval(struct hashtab *h)
498 * Define the other val_to_name and val_to_struct arrays
499 * in a policy database structure.
501 * Caller must clean up on failure.
503 static int policydb_index_others(struct policydb *p)
507 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
508 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
510 printk(", %d sens, %d cats", p->p_levels.nprim,
514 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
515 p->p_classes.nprim, p->te_avtab.nel);
518 avtab_hash_eval(&p->te_avtab, "rules");
519 symtab_hash_eval(p->symtab);
523 p->role_val_to_struct =
524 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
526 if (!p->role_val_to_struct)
530 p->user_val_to_struct =
531 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
533 if (!p->user_val_to_struct)
536 /* Yes, I want the sizeof the pointer, not the structure */
538 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
540 GFP_KERNEL | __GFP_ZERO);
541 if (!p->type_val_to_struct_array)
544 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
545 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
550 if (cond_init_bool_indexes(p))
553 for (i = SYM_ROLES; i < SYM_NUM; i++) {
555 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
557 GFP_KERNEL | __GFP_ZERO);
558 if (!p->sym_val_to_name[i])
561 rc = flex_array_prealloc(p->sym_val_to_name[i],
562 0, p->symtab[i].nprim - 1,
563 GFP_KERNEL | __GFP_ZERO);
567 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
577 * The following *_destroy functions are used to
578 * free any memory allocated for each kind of
579 * symbol data in the policy database.
582 static int perm_destroy(void *key, void *datum, void *p)
589 static int common_destroy(void *key, void *datum, void *p)
591 struct common_datum *comdatum;
596 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
597 hashtab_destroy(comdatum->permissions.table);
603 static int cls_destroy(void *key, void *datum, void *p)
605 struct class_datum *cladatum;
606 struct constraint_node *constraint, *ctemp;
607 struct constraint_expr *e, *etmp;
612 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
613 hashtab_destroy(cladatum->permissions.table);
614 constraint = cladatum->constraints;
616 e = constraint->expr;
618 ebitmap_destroy(&e->names);
624 constraint = constraint->next;
628 constraint = cladatum->validatetrans;
630 e = constraint->expr;
632 ebitmap_destroy(&e->names);
638 constraint = constraint->next;
642 kfree(cladatum->comkey);
648 static int role_destroy(void *key, void *datum, void *p)
650 struct role_datum *role;
655 ebitmap_destroy(&role->dominates);
656 ebitmap_destroy(&role->types);
662 static int type_destroy(void *key, void *datum, void *p)
669 static int user_destroy(void *key, void *datum, void *p)
671 struct user_datum *usrdatum;
676 ebitmap_destroy(&usrdatum->roles);
677 ebitmap_destroy(&usrdatum->range.level[0].cat);
678 ebitmap_destroy(&usrdatum->range.level[1].cat);
679 ebitmap_destroy(&usrdatum->dfltlevel.cat);
685 static int sens_destroy(void *key, void *datum, void *p)
687 struct level_datum *levdatum;
692 ebitmap_destroy(&levdatum->level->cat);
693 kfree(levdatum->level);
699 static int cat_destroy(void *key, void *datum, void *p)
706 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
718 static int range_tr_destroy(void *key, void *datum, void *p)
720 struct mls_range *rt = datum;
722 ebitmap_destroy(&rt->level[0].cat);
723 ebitmap_destroy(&rt->level[1].cat);
729 static void ocontext_destroy(struct ocontext *c, int i)
734 context_destroy(&c->context[0]);
735 context_destroy(&c->context[1]);
736 if (i == OCON_ISID || i == OCON_FS ||
737 i == OCON_NETIF || i == OCON_FSUSE)
743 * Free any memory allocated by a policy database structure.
745 void policydb_destroy(struct policydb *p)
747 struct ocontext *c, *ctmp;
748 struct genfs *g, *gtmp;
750 struct role_allow *ra, *lra = NULL;
751 struct role_trans *tr, *ltr = NULL;
753 for (i = 0; i < SYM_NUM; i++) {
755 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
756 hashtab_destroy(p->symtab[i].table);
759 for (i = 0; i < SYM_NUM; i++) {
760 if (p->sym_val_to_name[i])
761 flex_array_free(p->sym_val_to_name[i]);
764 kfree(p->class_val_to_struct);
765 kfree(p->role_val_to_struct);
766 kfree(p->user_val_to_struct);
767 if (p->type_val_to_struct_array)
768 flex_array_free(p->type_val_to_struct_array);
770 avtab_destroy(&p->te_avtab);
772 for (i = 0; i < OCON_NUM; i++) {
778 ocontext_destroy(ctmp, i);
780 p->ocontexts[i] = NULL;
791 ocontext_destroy(ctmp, OCON_FSUSE);
799 cond_policydb_destroy(p);
801 for (tr = p->role_tr; tr; tr = tr->next) {
808 for (ra = p->role_allow; ra; ra = ra->next) {
815 hashtab_map(p->range_tr, range_tr_destroy, NULL);
816 hashtab_destroy(p->range_tr);
818 if (p->type_attr_map_array) {
819 for (i = 0; i < p->p_types.nprim; i++) {
822 e = flex_array_get(p->type_attr_map_array, i);
827 flex_array_free(p->type_attr_map_array);
829 ebitmap_destroy(&p->policycaps);
830 ebitmap_destroy(&p->permissive_map);
836 * Load the initial SIDs specified in a policy database
837 * structure into a SID table.
839 int policydb_load_isids(struct policydb *p, struct sidtab *s)
841 struct ocontext *head, *c;
846 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
850 head = p->ocontexts[OCON_ISID];
851 for (c = head; c; c = c->next) {
853 if (!c->context[0].user) {
854 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
859 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
861 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
871 int policydb_class_isvalid(struct policydb *p, unsigned int class)
873 if (!class || class > p->p_classes.nprim)
878 int policydb_role_isvalid(struct policydb *p, unsigned int role)
880 if (!role || role > p->p_roles.nprim)
885 int policydb_type_isvalid(struct policydb *p, unsigned int type)
887 if (!type || type > p->p_types.nprim)
893 * Return 1 if the fields in the security context
894 * structure `c' are valid. Return 0 otherwise.
896 int policydb_context_isvalid(struct policydb *p, struct context *c)
898 struct role_datum *role;
899 struct user_datum *usrdatum;
901 if (!c->role || c->role > p->p_roles.nprim)
904 if (!c->user || c->user > p->p_users.nprim)
907 if (!c->type || c->type > p->p_types.nprim)
910 if (c->role != OBJECT_R_VAL) {
912 * Role must be authorized for the type.
914 role = p->role_val_to_struct[c->role - 1];
915 if (!ebitmap_get_bit(&role->types, c->type - 1))
916 /* role may not be associated with type */
920 * User must be authorized for the role.
922 usrdatum = p->user_val_to_struct[c->user - 1];
926 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
927 /* user may not be associated with role */
931 if (!mls_context_isvalid(p, c))
938 * Read a MLS range structure from a policydb binary
939 * representation file.
941 static int mls_read_range_helper(struct mls_range *r, void *fp)
947 rc = next_entry(buf, fp, sizeof(u32));
952 items = le32_to_cpu(buf[0]);
953 if (items > ARRAY_SIZE(buf)) {
954 printk(KERN_ERR "SELinux: mls: range overflow\n");
958 rc = next_entry(buf, fp, sizeof(u32) * items);
960 printk(KERN_ERR "SELinux: mls: truncated range\n");
964 r->level[0].sens = le32_to_cpu(buf[0]);
966 r->level[1].sens = le32_to_cpu(buf[1]);
968 r->level[1].sens = r->level[0].sens;
970 rc = ebitmap_read(&r->level[0].cat, fp);
972 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
976 rc = ebitmap_read(&r->level[1].cat, fp);
978 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
982 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
984 printk(KERN_ERR "SELinux: mls: out of memory\n");
991 ebitmap_destroy(&r->level[0].cat);
997 * Read and validate a security context structure
998 * from a policydb binary representation file.
1000 static int context_read_and_validate(struct context *c,
1007 rc = next_entry(buf, fp, sizeof buf);
1009 printk(KERN_ERR "SELinux: context truncated\n");
1012 c->user = le32_to_cpu(buf[0]);
1013 c->role = le32_to_cpu(buf[1]);
1014 c->type = le32_to_cpu(buf[2]);
1015 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1016 rc = mls_read_range_helper(&c->range, fp);
1018 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1024 if (!policydb_context_isvalid(p, c)) {
1025 printk(KERN_ERR "SELinux: invalid security context\n");
1035 * The following *_read functions are used to
1036 * read the symbol data from a policy database
1037 * binary representation file.
1040 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1043 struct perm_datum *perdatum;
1049 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1053 rc = next_entry(buf, fp, sizeof buf);
1057 len = le32_to_cpu(buf[0]);
1058 perdatum->value = le32_to_cpu(buf[1]);
1061 key = kmalloc(len + 1, GFP_KERNEL);
1065 rc = next_entry(key, fp, len);
1070 rc = hashtab_insert(h, key, perdatum);
1076 perm_destroy(key, perdatum, NULL);
1080 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1083 struct common_datum *comdatum;
1089 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1093 rc = next_entry(buf, fp, sizeof buf);
1097 len = le32_to_cpu(buf[0]);
1098 comdatum->value = le32_to_cpu(buf[1]);
1100 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1103 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1104 nel = le32_to_cpu(buf[3]);
1107 key = kmalloc(len + 1, GFP_KERNEL);
1111 rc = next_entry(key, fp, len);
1116 for (i = 0; i < nel; i++) {
1117 rc = perm_read(p, comdatum->permissions.table, fp);
1122 rc = hashtab_insert(h, key, comdatum);
1127 common_destroy(key, comdatum, NULL);
1131 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1132 int allowxtarget, void *fp)
1134 struct constraint_node *c, *lc;
1135 struct constraint_expr *e, *le;
1138 int rc, i, j, depth;
1141 for (i = 0; i < ncons; i++) {
1142 c = kzalloc(sizeof(*c), GFP_KERNEL);
1151 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1154 c->permissions = le32_to_cpu(buf[0]);
1155 nexpr = le32_to_cpu(buf[1]);
1158 for (j = 0; j < nexpr; j++) {
1159 e = kzalloc(sizeof(*e), GFP_KERNEL);
1168 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1171 e->expr_type = le32_to_cpu(buf[0]);
1172 e->attr = le32_to_cpu(buf[1]);
1173 e->op = le32_to_cpu(buf[2]);
1175 switch (e->expr_type) {
1187 if (depth == (CEXPR_MAXDEPTH - 1))
1192 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1194 if (depth == (CEXPR_MAXDEPTH - 1))
1197 rc = ebitmap_read(&e->names, fp);
1214 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1217 struct class_datum *cladatum;
1219 u32 len, len2, ncons, nel;
1223 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1227 rc = next_entry(buf, fp, sizeof(u32)*6);
1231 len = le32_to_cpu(buf[0]);
1232 len2 = le32_to_cpu(buf[1]);
1233 cladatum->value = le32_to_cpu(buf[2]);
1235 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1238 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1239 nel = le32_to_cpu(buf[4]);
1241 ncons = le32_to_cpu(buf[5]);
1244 key = kmalloc(len + 1, GFP_KERNEL);
1248 rc = next_entry(key, fp, len);
1255 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1256 if (!cladatum->comkey)
1258 rc = next_entry(cladatum->comkey, fp, len2);
1261 cladatum->comkey[len2] = '\0';
1264 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1265 if (!cladatum->comdatum) {
1266 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1270 for (i = 0; i < nel; i++) {
1271 rc = perm_read(p, cladatum->permissions.table, fp);
1276 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1280 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1281 /* grab the validatetrans rules */
1282 rc = next_entry(buf, fp, sizeof(u32));
1285 ncons = le32_to_cpu(buf[0]);
1286 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1291 rc = hashtab_insert(h, key, cladatum);
1297 cls_destroy(key, cladatum, NULL);
1301 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1304 struct role_datum *role;
1305 int rc, to_read = 2;
1310 role = kzalloc(sizeof(*role), GFP_KERNEL);
1314 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1317 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1321 len = le32_to_cpu(buf[0]);
1322 role->value = le32_to_cpu(buf[1]);
1323 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1324 role->bounds = le32_to_cpu(buf[2]);
1327 key = kmalloc(len + 1, GFP_KERNEL);
1331 rc = next_entry(key, fp, len);
1336 rc = ebitmap_read(&role->dominates, fp);
1340 rc = ebitmap_read(&role->types, fp);
1344 if (strcmp(key, OBJECT_R) == 0) {
1346 if (role->value != OBJECT_R_VAL) {
1347 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1348 OBJECT_R, role->value);
1355 rc = hashtab_insert(h, key, role);
1360 role_destroy(key, role, NULL);
1364 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1367 struct type_datum *typdatum;
1368 int rc, to_read = 3;
1373 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1377 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1380 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1384 len = le32_to_cpu(buf[0]);
1385 typdatum->value = le32_to_cpu(buf[1]);
1386 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1387 u32 prop = le32_to_cpu(buf[2]);
1389 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1390 typdatum->primary = 1;
1391 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1392 typdatum->attribute = 1;
1394 typdatum->bounds = le32_to_cpu(buf[3]);
1396 typdatum->primary = le32_to_cpu(buf[2]);
1400 key = kmalloc(len + 1, GFP_KERNEL);
1403 rc = next_entry(key, fp, len);
1408 rc = hashtab_insert(h, key, typdatum);
1413 type_destroy(key, typdatum, NULL);
1419 * Read a MLS level structure from a policydb binary
1420 * representation file.
1422 static int mls_read_level(struct mls_level *lp, void *fp)
1427 memset(lp, 0, sizeof(*lp));
1429 rc = next_entry(buf, fp, sizeof buf);
1431 printk(KERN_ERR "SELinux: mls: truncated level\n");
1434 lp->sens = le32_to_cpu(buf[0]);
1436 rc = ebitmap_read(&lp->cat, fp);
1438 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1444 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1447 struct user_datum *usrdatum;
1448 int rc, to_read = 2;
1453 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1457 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1460 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1464 len = le32_to_cpu(buf[0]);
1465 usrdatum->value = le32_to_cpu(buf[1]);
1466 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1467 usrdatum->bounds = le32_to_cpu(buf[2]);
1470 key = kmalloc(len + 1, GFP_KERNEL);
1473 rc = next_entry(key, fp, len);
1478 rc = ebitmap_read(&usrdatum->roles, fp);
1482 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1483 rc = mls_read_range_helper(&usrdatum->range, fp);
1486 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1491 rc = hashtab_insert(h, key, usrdatum);
1496 user_destroy(key, usrdatum, NULL);
1500 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1503 struct level_datum *levdatum;
1509 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1513 rc = next_entry(buf, fp, sizeof buf);
1517 len = le32_to_cpu(buf[0]);
1518 levdatum->isalias = le32_to_cpu(buf[1]);
1521 key = kmalloc(len + 1, GFP_ATOMIC);
1524 rc = next_entry(key, fp, len);
1530 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1531 if (!levdatum->level)
1534 rc = mls_read_level(levdatum->level, fp);
1538 rc = hashtab_insert(h, key, levdatum);
1543 sens_destroy(key, levdatum, NULL);
1547 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1550 struct cat_datum *catdatum;
1556 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1560 rc = next_entry(buf, fp, sizeof buf);
1564 len = le32_to_cpu(buf[0]);
1565 catdatum->value = le32_to_cpu(buf[1]);
1566 catdatum->isalias = le32_to_cpu(buf[2]);
1569 key = kmalloc(len + 1, GFP_ATOMIC);
1572 rc = next_entry(key, fp, len);
1577 rc = hashtab_insert(h, key, catdatum);
1582 cat_destroy(key, catdatum, NULL);
1586 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1598 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1600 struct user_datum *upper, *user;
1601 struct policydb *p = datap;
1604 upper = user = datum;
1605 while (upper->bounds) {
1606 struct ebitmap_node *node;
1609 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1610 printk(KERN_ERR "SELinux: user %s: "
1611 "too deep or looped boundary",
1616 upper = p->user_val_to_struct[upper->bounds - 1];
1617 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1618 if (ebitmap_get_bit(&upper->roles, bit))
1622 "SELinux: boundary violated policy: "
1623 "user=%s role=%s bounds=%s\n",
1624 sym_name(p, SYM_USERS, user->value - 1),
1625 sym_name(p, SYM_ROLES, bit),
1626 sym_name(p, SYM_USERS, upper->value - 1));
1635 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1637 struct role_datum *upper, *role;
1638 struct policydb *p = datap;
1641 upper = role = datum;
1642 while (upper->bounds) {
1643 struct ebitmap_node *node;
1646 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1647 printk(KERN_ERR "SELinux: role %s: "
1648 "too deep or looped bounds\n",
1653 upper = p->role_val_to_struct[upper->bounds - 1];
1654 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1655 if (ebitmap_get_bit(&upper->types, bit))
1659 "SELinux: boundary violated policy: "
1660 "role=%s type=%s bounds=%s\n",
1661 sym_name(p, SYM_ROLES, role->value - 1),
1662 sym_name(p, SYM_TYPES, bit),
1663 sym_name(p, SYM_ROLES, upper->value - 1));
1672 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1674 struct type_datum *upper;
1675 struct policydb *p = datap;
1679 while (upper->bounds) {
1680 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1681 printk(KERN_ERR "SELinux: type %s: "
1682 "too deep or looped boundary\n",
1687 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1691 if (upper->attribute) {
1692 printk(KERN_ERR "SELinux: type %s: "
1693 "bounded by attribute %s",
1695 sym_name(p, SYM_TYPES, upper->value - 1));
1703 static int policydb_bounds_sanity_check(struct policydb *p)
1707 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1710 rc = hashtab_map(p->p_users.table,
1711 user_bounds_sanity_check, p);
1715 rc = hashtab_map(p->p_roles.table,
1716 role_bounds_sanity_check, p);
1720 rc = hashtab_map(p->p_types.table,
1721 type_bounds_sanity_check, p);
1728 extern int ss_initialized;
1730 u16 string_to_security_class(struct policydb *p, const char *name)
1732 struct class_datum *cladatum;
1734 cladatum = hashtab_search(p->p_classes.table, name);
1738 return cladatum->value;
1741 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1743 struct class_datum *cladatum;
1744 struct perm_datum *perdatum = NULL;
1745 struct common_datum *comdatum;
1747 if (!tclass || tclass > p->p_classes.nprim)
1750 cladatum = p->class_val_to_struct[tclass-1];
1751 comdatum = cladatum->comdatum;
1753 perdatum = hashtab_search(comdatum->permissions.table,
1756 perdatum = hashtab_search(cladatum->permissions.table,
1761 return 1U << (perdatum->value-1);
1764 static int range_read(struct policydb *p, void *fp)
1766 struct range_trans *rt = NULL;
1767 struct mls_range *r = NULL;
1772 if (p->policyvers < POLICYDB_VERSION_MLS)
1775 rc = next_entry(buf, fp, sizeof(u32));
1779 nel = le32_to_cpu(buf[0]);
1780 for (i = 0; i < nel; i++) {
1782 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1786 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1790 rt->source_type = le32_to_cpu(buf[0]);
1791 rt->target_type = le32_to_cpu(buf[1]);
1792 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1793 rc = next_entry(buf, fp, sizeof(u32));
1796 rt->target_class = le32_to_cpu(buf[0]);
1798 rt->target_class = p->process_class;
1801 if (!policydb_type_isvalid(p, rt->source_type) ||
1802 !policydb_type_isvalid(p, rt->target_type) ||
1803 !policydb_class_isvalid(p, rt->target_class))
1807 r = kzalloc(sizeof(*r), GFP_KERNEL);
1811 rc = mls_read_range_helper(r, fp);
1816 if (!mls_range_isvalid(p, r)) {
1817 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1821 rc = hashtab_insert(p->range_tr, rt, r);
1828 rangetr_hash_eval(p->range_tr);
1836 static int genfs_read(struct policydb *p, void *fp)
1839 u32 nel, nel2, len, len2;
1841 struct ocontext *l, *c;
1842 struct ocontext *newc = NULL;
1843 struct genfs *genfs_p, *genfs;
1844 struct genfs *newgenfs = NULL;
1846 rc = next_entry(buf, fp, sizeof(u32));
1849 nel = le32_to_cpu(buf[0]);
1851 for (i = 0; i < nel; i++) {
1852 rc = next_entry(buf, fp, sizeof(u32));
1855 len = le32_to_cpu(buf[0]);
1858 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1863 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1864 if (!newgenfs->fstype)
1867 rc = next_entry(newgenfs->fstype, fp, len);
1871 newgenfs->fstype[len] = 0;
1873 for (genfs_p = NULL, genfs = p->genfs; genfs;
1874 genfs_p = genfs, genfs = genfs->next) {
1876 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1877 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1881 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1884 newgenfs->next = genfs;
1886 genfs_p->next = newgenfs;
1888 p->genfs = newgenfs;
1892 rc = next_entry(buf, fp, sizeof(u32));
1896 nel2 = le32_to_cpu(buf[0]);
1897 for (j = 0; j < nel2; j++) {
1898 rc = next_entry(buf, fp, sizeof(u32));
1901 len = le32_to_cpu(buf[0]);
1904 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1909 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1913 rc = next_entry(newc->u.name, fp, len);
1916 newc->u.name[len] = 0;
1918 rc = next_entry(buf, fp, sizeof(u32));
1922 newc->v.sclass = le32_to_cpu(buf[0]);
1923 rc = context_read_and_validate(&newc->context[0], p, fp);
1927 for (l = NULL, c = genfs->head; c;
1928 l = c, c = c->next) {
1930 if (!strcmp(newc->u.name, c->u.name) &&
1931 (!c->v.sclass || !newc->v.sclass ||
1932 newc->v.sclass == c->v.sclass)) {
1933 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1934 genfs->fstype, c->u.name);
1937 len = strlen(newc->u.name);
1938 len2 = strlen(c->u.name);
1954 kfree(newgenfs->fstype);
1956 ocontext_destroy(newc, OCON_FSUSE);
1961 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1967 struct ocontext *l, *c;
1970 for (i = 0; i < info->ocon_num; i++) {
1971 rc = next_entry(buf, fp, sizeof(u32));
1974 nel = le32_to_cpu(buf[0]);
1977 for (j = 0; j < nel; j++) {
1979 c = kzalloc(sizeof(*c), GFP_KERNEL);
1985 p->ocontexts[i] = c;
1990 rc = next_entry(buf, fp, sizeof(u32));
1994 c->sid[0] = le32_to_cpu(buf[0]);
1995 rc = context_read_and_validate(&c->context[0], p, fp);
2001 rc = next_entry(buf, fp, sizeof(u32));
2004 len = le32_to_cpu(buf[0]);
2007 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2011 rc = next_entry(c->u.name, fp, len);
2016 rc = context_read_and_validate(&c->context[0], p, fp);
2019 rc = context_read_and_validate(&c->context[1], p, fp);
2024 rc = next_entry(buf, fp, sizeof(u32)*3);
2027 c->u.port.protocol = le32_to_cpu(buf[0]);
2028 c->u.port.low_port = le32_to_cpu(buf[1]);
2029 c->u.port.high_port = le32_to_cpu(buf[2]);
2030 rc = context_read_and_validate(&c->context[0], p, fp);
2035 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2038 c->u.node.addr = nodebuf[0]; /* network order */
2039 c->u.node.mask = nodebuf[1]; /* network order */
2040 rc = context_read_and_validate(&c->context[0], p, fp);
2045 rc = next_entry(buf, fp, sizeof(u32)*2);
2050 c->v.behavior = le32_to_cpu(buf[0]);
2051 if (c->v.behavior > SECURITY_FS_USE_NONE)
2055 len = le32_to_cpu(buf[1]);
2056 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2060 rc = next_entry(c->u.name, fp, len);
2064 rc = context_read_and_validate(&c->context[0], p, fp);
2071 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2074 for (k = 0; k < 4; k++)
2075 c->u.node6.addr[k] = nodebuf[k];
2076 for (k = 0; k < 4; k++)
2077 c->u.node6.mask[k] = nodebuf[k+4];
2078 rc = context_read_and_validate(&c->context[0], p, fp);
2092 * Read the configuration data from a policy database binary
2093 * representation file into a policy database structure.
2095 int policydb_read(struct policydb *p, void *fp)
2097 struct role_allow *ra, *lra;
2098 struct role_trans *tr, *ltr;
2101 u32 len, nprim, nel;
2104 struct policydb_compat_info *info;
2106 rc = policydb_init(p);
2110 /* Read the magic number and string length. */
2111 rc = next_entry(buf, fp, sizeof(u32) * 2);
2116 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2117 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2118 "not match expected magic number 0x%x\n",
2119 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2124 len = le32_to_cpu(buf[1]);
2125 if (len != strlen(POLICYDB_STRING)) {
2126 printk(KERN_ERR "SELinux: policydb string length %d does not "
2127 "match expected length %Zu\n",
2128 len, strlen(POLICYDB_STRING));
2133 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2134 if (!policydb_str) {
2135 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2136 "string of length %d\n", len);
2140 rc = next_entry(policydb_str, fp, len);
2142 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2143 kfree(policydb_str);
2148 policydb_str[len] = '\0';
2149 if (strcmp(policydb_str, POLICYDB_STRING)) {
2150 printk(KERN_ERR "SELinux: policydb string %s does not match "
2151 "my string %s\n", policydb_str, POLICYDB_STRING);
2152 kfree(policydb_str);
2155 /* Done with policydb_str. */
2156 kfree(policydb_str);
2157 policydb_str = NULL;
2159 /* Read the version and table sizes. */
2160 rc = next_entry(buf, fp, sizeof(u32)*4);
2165 p->policyvers = le32_to_cpu(buf[0]);
2166 if (p->policyvers < POLICYDB_VERSION_MIN ||
2167 p->policyvers > POLICYDB_VERSION_MAX) {
2168 printk(KERN_ERR "SELinux: policydb version %d does not match "
2169 "my version range %d-%d\n",
2170 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2174 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2178 if (p->policyvers < POLICYDB_VERSION_MLS) {
2179 printk(KERN_ERR "SELinux: security policydb version %d "
2180 "(MLS) not backwards compatible\n",
2185 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2186 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2188 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2189 rc = ebitmap_read(&p->policycaps, fp);
2194 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2195 rc = ebitmap_read(&p->permissive_map, fp);
2201 info = policydb_lookup_compat(p->policyvers);
2203 printk(KERN_ERR "SELinux: unable to find policy compat info "
2204 "for version %d\n", p->policyvers);
2209 if (le32_to_cpu(buf[2]) != info->sym_num ||
2210 le32_to_cpu(buf[3]) != info->ocon_num) {
2211 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2212 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2213 le32_to_cpu(buf[3]),
2214 info->sym_num, info->ocon_num);
2218 for (i = 0; i < info->sym_num; i++) {
2219 rc = next_entry(buf, fp, sizeof(u32)*2);
2222 nprim = le32_to_cpu(buf[0]);
2223 nel = le32_to_cpu(buf[1]);
2224 for (j = 0; j < nel; j++) {
2225 rc = read_f[i](p, p->symtab[i].table, fp);
2230 p->symtab[i].nprim = nprim;
2233 rc = avtab_read(&p->te_avtab, fp, p);
2237 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2238 rc = cond_read_list(p, fp);
2243 rc = next_entry(buf, fp, sizeof(u32));
2246 nel = le32_to_cpu(buf[0]);
2248 for (i = 0; i < nel; i++) {
2250 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2257 rc = next_entry(buf, fp, sizeof(u32)*3);
2262 tr->role = le32_to_cpu(buf[0]);
2263 tr->type = le32_to_cpu(buf[1]);
2264 tr->new_role = le32_to_cpu(buf[2]);
2265 if (!policydb_role_isvalid(p, tr->role) ||
2266 !policydb_type_isvalid(p, tr->type) ||
2267 !policydb_role_isvalid(p, tr->new_role))
2272 rc = next_entry(buf, fp, sizeof(u32));
2275 nel = le32_to_cpu(buf[0]);
2277 for (i = 0; i < nel; i++) {
2279 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2286 rc = next_entry(buf, fp, sizeof(u32)*2);
2291 ra->role = le32_to_cpu(buf[0]);
2292 ra->new_role = le32_to_cpu(buf[1]);
2293 if (!policydb_role_isvalid(p, ra->role) ||
2294 !policydb_role_isvalid(p, ra->new_role))
2299 rc = policydb_index_classes(p);
2303 rc = policydb_index_others(p);
2308 p->process_class = string_to_security_class(p, "process");
2309 if (!p->process_class)
2313 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2314 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2315 if (!p->process_trans_perms)
2318 rc = ocontext_read(p, info, fp);
2322 rc = genfs_read(p, fp);
2326 rc = range_read(p, fp);
2331 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2333 GFP_KERNEL | __GFP_ZERO);
2334 if (!p->type_attr_map_array)
2337 /* preallocate so we don't have to worry about the put ever failing */
2338 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2339 GFP_KERNEL | __GFP_ZERO);
2343 for (i = 0; i < p->p_types.nprim; i++) {
2344 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2348 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2349 rc = ebitmap_read(e, fp);
2353 /* add the type itself as the degenerate case */
2354 rc = ebitmap_set_bit(e, i, 1);
2359 rc = policydb_bounds_sanity_check(p);
2367 policydb_destroy(p);
2372 * Write a MLS level structure to a policydb binary
2373 * representation file.
2375 static int mls_write_level(struct mls_level *l, void *fp)
2380 buf[0] = cpu_to_le32(l->sens);
2381 rc = put_entry(buf, sizeof(u32), 1, fp);
2385 rc = ebitmap_write(&l->cat, fp);
2393 * Write a MLS range structure to a policydb binary
2394 * representation file.
2396 static int mls_write_range_helper(struct mls_range *r, void *fp)
2402 eq = mls_level_eq(&r->level[1], &r->level[0]);
2408 buf[0] = cpu_to_le32(items-1);
2409 buf[1] = cpu_to_le32(r->level[0].sens);
2411 buf[2] = cpu_to_le32(r->level[1].sens);
2413 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2415 rc = put_entry(buf, sizeof(u32), items, fp);
2419 rc = ebitmap_write(&r->level[0].cat, fp);
2423 rc = ebitmap_write(&r->level[1].cat, fp);
2431 static int sens_write(void *vkey, void *datum, void *ptr)
2434 struct level_datum *levdatum = datum;
2435 struct policy_data *pd = ptr;
2442 buf[0] = cpu_to_le32(len);
2443 buf[1] = cpu_to_le32(levdatum->isalias);
2444 rc = put_entry(buf, sizeof(u32), 2, fp);
2448 rc = put_entry(key, 1, len, fp);
2452 rc = mls_write_level(levdatum->level, fp);
2459 static int cat_write(void *vkey, void *datum, void *ptr)
2462 struct cat_datum *catdatum = datum;
2463 struct policy_data *pd = ptr;
2470 buf[0] = cpu_to_le32(len);
2471 buf[1] = cpu_to_le32(catdatum->value);
2472 buf[2] = cpu_to_le32(catdatum->isalias);
2473 rc = put_entry(buf, sizeof(u32), 3, fp);
2477 rc = put_entry(key, 1, len, fp);
2484 static int role_trans_write(struct role_trans *r, void *fp)
2486 struct role_trans *tr;
2492 for (tr = r; tr; tr = tr->next)
2494 buf[0] = cpu_to_le32(nel);
2495 rc = put_entry(buf, sizeof(u32), 1, fp);
2498 for (tr = r; tr; tr = tr->next) {
2499 buf[0] = cpu_to_le32(tr->role);
2500 buf[1] = cpu_to_le32(tr->type);
2501 buf[2] = cpu_to_le32(tr->new_role);
2502 rc = put_entry(buf, sizeof(u32), 3, fp);
2510 static int role_allow_write(struct role_allow *r, void *fp)
2512 struct role_allow *ra;
2518 for (ra = r; ra; ra = ra->next)
2520 buf[0] = cpu_to_le32(nel);
2521 rc = put_entry(buf, sizeof(u32), 1, fp);
2524 for (ra = r; ra; ra = ra->next) {
2525 buf[0] = cpu_to_le32(ra->role);
2526 buf[1] = cpu_to_le32(ra->new_role);
2527 rc = put_entry(buf, sizeof(u32), 2, fp);
2535 * Write a security context structure
2536 * to a policydb binary representation file.
2538 static int context_write(struct policydb *p, struct context *c,
2544 buf[0] = cpu_to_le32(c->user);
2545 buf[1] = cpu_to_le32(c->role);
2546 buf[2] = cpu_to_le32(c->type);
2548 rc = put_entry(buf, sizeof(u32), 3, fp);
2552 rc = mls_write_range_helper(&c->range, fp);
2560 * The following *_write functions are used to
2561 * write the symbol data to a policy database
2562 * binary representation file.
2565 static int perm_write(void *vkey, void *datum, void *fp)
2568 struct perm_datum *perdatum = datum;
2574 buf[0] = cpu_to_le32(len);
2575 buf[1] = cpu_to_le32(perdatum->value);
2576 rc = put_entry(buf, sizeof(u32), 2, fp);
2580 rc = put_entry(key, 1, len, fp);
2587 static int common_write(void *vkey, void *datum, void *ptr)
2590 struct common_datum *comdatum = datum;
2591 struct policy_data *pd = ptr;
2598 buf[0] = cpu_to_le32(len);
2599 buf[1] = cpu_to_le32(comdatum->value);
2600 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2601 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2602 rc = put_entry(buf, sizeof(u32), 4, fp);
2606 rc = put_entry(key, 1, len, fp);
2610 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2617 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2620 struct constraint_node *c;
2621 struct constraint_expr *e;
2626 for (c = node; c; c = c->next) {
2628 for (e = c->expr; e; e = e->next)
2630 buf[0] = cpu_to_le32(c->permissions);
2631 buf[1] = cpu_to_le32(nel);
2632 rc = put_entry(buf, sizeof(u32), 2, fp);
2635 for (e = c->expr; e; e = e->next) {
2636 buf[0] = cpu_to_le32(e->expr_type);
2637 buf[1] = cpu_to_le32(e->attr);
2638 buf[2] = cpu_to_le32(e->op);
2639 rc = put_entry(buf, sizeof(u32), 3, fp);
2643 switch (e->expr_type) {
2645 rc = ebitmap_write(&e->names, fp);
2658 static int class_write(void *vkey, void *datum, void *ptr)
2661 struct class_datum *cladatum = datum;
2662 struct policy_data *pd = ptr;
2664 struct policydb *p = pd->p;
2665 struct constraint_node *c;
2672 if (cladatum->comkey)
2673 len2 = strlen(cladatum->comkey);
2678 for (c = cladatum->constraints; c; c = c->next)
2681 buf[0] = cpu_to_le32(len);
2682 buf[1] = cpu_to_le32(len2);
2683 buf[2] = cpu_to_le32(cladatum->value);
2684 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2685 if (cladatum->permissions.table)
2686 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2689 buf[5] = cpu_to_le32(ncons);
2690 rc = put_entry(buf, sizeof(u32), 6, fp);
2694 rc = put_entry(key, 1, len, fp);
2698 if (cladatum->comkey) {
2699 rc = put_entry(cladatum->comkey, 1, len2, fp);
2704 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2708 rc = write_cons_helper(p, cladatum->constraints, fp);
2712 /* write out the validatetrans rule */
2714 for (c = cladatum->validatetrans; c; c = c->next)
2717 buf[0] = cpu_to_le32(ncons);
2718 rc = put_entry(buf, sizeof(u32), 1, fp);
2722 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2729 static int role_write(void *vkey, void *datum, void *ptr)
2732 struct role_datum *role = datum;
2733 struct policy_data *pd = ptr;
2735 struct policydb *p = pd->p;
2742 buf[items++] = cpu_to_le32(len);
2743 buf[items++] = cpu_to_le32(role->value);
2744 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2745 buf[items++] = cpu_to_le32(role->bounds);
2747 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2749 rc = put_entry(buf, sizeof(u32), items, fp);
2753 rc = put_entry(key, 1, len, fp);
2757 rc = ebitmap_write(&role->dominates, fp);
2761 rc = ebitmap_write(&role->types, fp);
2768 static int type_write(void *vkey, void *datum, void *ptr)
2771 struct type_datum *typdatum = datum;
2772 struct policy_data *pd = ptr;
2773 struct policydb *p = pd->p;
2781 buf[items++] = cpu_to_le32(len);
2782 buf[items++] = cpu_to_le32(typdatum->value);
2783 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2786 if (typdatum->primary)
2787 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2789 if (typdatum->attribute)
2790 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2792 buf[items++] = cpu_to_le32(properties);
2793 buf[items++] = cpu_to_le32(typdatum->bounds);
2795 buf[items++] = cpu_to_le32(typdatum->primary);
2797 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2798 rc = put_entry(buf, sizeof(u32), items, fp);
2802 rc = put_entry(key, 1, len, fp);
2809 static int user_write(void *vkey, void *datum, void *ptr)
2812 struct user_datum *usrdatum = datum;
2813 struct policy_data *pd = ptr;
2814 struct policydb *p = pd->p;
2822 buf[items++] = cpu_to_le32(len);
2823 buf[items++] = cpu_to_le32(usrdatum->value);
2824 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2825 buf[items++] = cpu_to_le32(usrdatum->bounds);
2826 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2827 rc = put_entry(buf, sizeof(u32), items, fp);
2831 rc = put_entry(key, 1, len, fp);
2835 rc = ebitmap_write(&usrdatum->roles, fp);
2839 rc = mls_write_range_helper(&usrdatum->range, fp);
2843 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2850 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2863 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2866 unsigned int i, j, rc;
2871 for (i = 0; i < info->ocon_num; i++) {
2873 for (c = p->ocontexts[i]; c; c = c->next)
2875 buf[0] = cpu_to_le32(nel);
2876 rc = put_entry(buf, sizeof(u32), 1, fp);
2879 for (c = p->ocontexts[i]; c; c = c->next) {
2882 buf[0] = cpu_to_le32(c->sid[0]);
2883 rc = put_entry(buf, sizeof(u32), 1, fp);
2886 rc = context_write(p, &c->context[0], fp);
2892 len = strlen(c->u.name);
2893 buf[0] = cpu_to_le32(len);
2894 rc = put_entry(buf, sizeof(u32), 1, fp);
2897 rc = put_entry(c->u.name, 1, len, fp);
2900 rc = context_write(p, &c->context[0], fp);
2903 rc = context_write(p, &c->context[1], fp);
2908 buf[0] = cpu_to_le32(c->u.port.protocol);
2909 buf[1] = cpu_to_le32(c->u.port.low_port);
2910 buf[2] = cpu_to_le32(c->u.port.high_port);
2911 rc = put_entry(buf, sizeof(u32), 3, fp);
2914 rc = context_write(p, &c->context[0], fp);
2919 nodebuf[0] = c->u.node.addr; /* network order */
2920 nodebuf[1] = c->u.node.mask; /* network order */
2921 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2924 rc = context_write(p, &c->context[0], fp);
2929 buf[0] = cpu_to_le32(c->v.behavior);
2930 len = strlen(c->u.name);
2931 buf[1] = cpu_to_le32(len);
2932 rc = put_entry(buf, sizeof(u32), 2, fp);
2935 rc = put_entry(c->u.name, 1, len, fp);
2938 rc = context_write(p, &c->context[0], fp);
2943 for (j = 0; j < 4; j++)
2944 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2945 for (j = 0; j < 4; j++)
2946 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2947 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2950 rc = context_write(p, &c->context[0], fp);
2960 static int genfs_write(struct policydb *p, void *fp)
2962 struct genfs *genfs;
2969 for (genfs = p->genfs; genfs; genfs = genfs->next)
2971 buf[0] = cpu_to_le32(len);
2972 rc = put_entry(buf, sizeof(u32), 1, fp);
2975 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2976 len = strlen(genfs->fstype);
2977 buf[0] = cpu_to_le32(len);
2978 rc = put_entry(buf, sizeof(u32), 1, fp);
2981 rc = put_entry(genfs->fstype, 1, len, fp);
2985 for (c = genfs->head; c; c = c->next)
2987 buf[0] = cpu_to_le32(len);
2988 rc = put_entry(buf, sizeof(u32), 1, fp);
2991 for (c = genfs->head; c; c = c->next) {
2992 len = strlen(c->u.name);
2993 buf[0] = cpu_to_le32(len);
2994 rc = put_entry(buf, sizeof(u32), 1, fp);
2997 rc = put_entry(c->u.name, 1, len, fp);
3000 buf[0] = cpu_to_le32(c->v.sclass);
3001 rc = put_entry(buf, sizeof(u32), 1, fp);
3004 rc = context_write(p, &c->context[0], fp);
3012 static int range_count(void *key, void *data, void *ptr)
3020 static int range_write_helper(void *key, void *data, void *ptr)
3023 struct range_trans *rt = key;
3024 struct mls_range *r = data;
3025 struct policy_data *pd = ptr;
3027 struct policydb *p = pd->p;
3030 buf[0] = cpu_to_le32(rt->source_type);
3031 buf[1] = cpu_to_le32(rt->target_type);
3032 rc = put_entry(buf, sizeof(u32), 2, fp);
3035 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3036 buf[0] = cpu_to_le32(rt->target_class);
3037 rc = put_entry(buf, sizeof(u32), 1, fp);
3041 rc = mls_write_range_helper(r, fp);
3048 static int range_write(struct policydb *p, void *fp)
3053 struct policy_data pd;
3058 /* count the number of entries in the hashtab */
3060 rc = hashtab_map(p->range_tr, range_count, &nel);
3064 buf[0] = cpu_to_le32(nel);
3065 rc = put_entry(buf, sizeof(u32), 1, fp);
3069 /* actually write all of the entries */
3070 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3078 * Write the configuration data in a policy database
3079 * structure to a policy database binary representation
3082 int policydb_write(struct policydb *p, void *fp)
3084 unsigned int i, num_syms;
3089 struct policydb_compat_info *info;
3092 * refuse to write policy older than compressed avtab
3093 * to simplify the writer. There are other tests dropped
3094 * since we assume this throughout the writer code. Be
3095 * careful if you ever try to remove this restriction
3097 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3098 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3099 " Because it is less than version %d\n", p->policyvers,
3100 POLICYDB_VERSION_AVTAB);
3106 config |= POLICYDB_CONFIG_MLS;
3108 if (p->reject_unknown)
3109 config |= REJECT_UNKNOWN;
3110 if (p->allow_unknown)
3111 config |= ALLOW_UNKNOWN;
3113 /* Write the magic number and string identifiers. */
3114 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3115 len = strlen(POLICYDB_STRING);
3116 buf[1] = cpu_to_le32(len);
3117 rc = put_entry(buf, sizeof(u32), 2, fp);
3120 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3124 /* Write the version, config, and table sizes. */
3125 info = policydb_lookup_compat(p->policyvers);
3127 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3128 "version %d", p->policyvers);
3132 buf[0] = cpu_to_le32(p->policyvers);
3133 buf[1] = cpu_to_le32(config);
3134 buf[2] = cpu_to_le32(info->sym_num);
3135 buf[3] = cpu_to_le32(info->ocon_num);
3137 rc = put_entry(buf, sizeof(u32), 4, fp);
3141 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3142 rc = ebitmap_write(&p->policycaps, fp);
3147 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3148 rc = ebitmap_write(&p->permissive_map, fp);
3153 num_syms = info->sym_num;
3154 for (i = 0; i < num_syms; i++) {
3155 struct policy_data pd;
3160 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3161 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3163 rc = put_entry(buf, sizeof(u32), 2, fp);
3166 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3171 rc = avtab_write(p, &p->te_avtab, fp);
3175 rc = cond_write_list(p, p->cond_list, fp);
3179 rc = role_trans_write(p->role_tr, fp);
3183 rc = role_allow_write(p->role_allow, fp);
3187 rc = ocontext_write(p, info, fp);
3191 rc = genfs_write(p, fp);
3195 rc = range_write(p, fp);
3199 for (i = 0; i < p->p_types.nprim; i++) {
3200 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3203 rc = ebitmap_write(e, fp);