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,
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
129 .ocon_num = OCON_NUM,
133 static struct policydb_compat_info *policydb_lookup_compat(int version)
136 struct policydb_compat_info *info = NULL;
138 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
139 if (policydb_compat[i].version == version) {
140 info = &policydb_compat[i];
148 * Initialize the role table.
150 static int roles_init(struct policydb *p)
154 struct role_datum *role;
157 role = kzalloc(sizeof(*role), GFP_KERNEL);
162 role->value = ++p->p_roles.nprim;
163 if (role->value != OBJECT_R_VAL)
167 key = kstrdup(OBJECT_R, GFP_KERNEL);
171 rc = hashtab_insert(p->p_roles.table, key, role);
182 static u32 rangetr_hash(struct hashtab *h, const void *k)
184 const struct range_trans *key = k;
185 return (key->source_type + (key->target_type << 3) +
186 (key->target_class << 5)) & (h->size - 1);
189 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
191 const struct range_trans *key1 = k1, *key2 = k2;
194 v = key1->source_type - key2->source_type;
198 v = key1->target_type - key2->target_type;
202 v = key1->target_class - key2->target_class;
208 * Initialize a policy database structure.
210 static int policydb_init(struct policydb *p)
214 memset(p, 0, sizeof(*p));
216 for (i = 0; i < SYM_NUM; i++) {
217 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
222 rc = avtab_init(&p->te_avtab);
230 rc = cond_policydb_init(p);
234 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
238 ebitmap_init(&p->policycaps);
239 ebitmap_init(&p->permissive_map);
243 for (i = 0; i < SYM_NUM; i++)
244 hashtab_destroy(p->symtab[i].table);
249 * The following *_index functions are used to
250 * define the val_to_name and val_to_struct arrays
251 * in a policy database structure. The val_to_name
252 * arrays are used when converting security context
253 * structures into string representations. The
254 * val_to_struct arrays are used when the attributes
255 * of a class, role, or user are needed.
258 static int common_index(void *key, void *datum, void *datap)
261 struct common_datum *comdatum;
262 struct flex_array *fa;
266 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
269 fa = p->sym_val_to_name[SYM_COMMONS];
270 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
271 GFP_KERNEL | __GFP_ZERO))
276 static int class_index(void *key, void *datum, void *datap)
279 struct class_datum *cladatum;
280 struct flex_array *fa;
284 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
286 fa = p->sym_val_to_name[SYM_CLASSES];
287 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
288 GFP_KERNEL | __GFP_ZERO))
290 p->class_val_to_struct[cladatum->value - 1] = cladatum;
294 static int role_index(void *key, void *datum, void *datap)
297 struct role_datum *role;
298 struct flex_array *fa;
303 || role->value > p->p_roles.nprim
304 || role->bounds > p->p_roles.nprim)
307 fa = p->sym_val_to_name[SYM_ROLES];
308 if (flex_array_put_ptr(fa, role->value - 1, key,
309 GFP_KERNEL | __GFP_ZERO))
311 p->role_val_to_struct[role->value - 1] = role;
315 static int type_index(void *key, void *datum, void *datap)
318 struct type_datum *typdatum;
319 struct flex_array *fa;
324 if (typdatum->primary) {
326 || typdatum->value > p->p_types.nprim
327 || typdatum->bounds > p->p_types.nprim)
329 fa = p->sym_val_to_name[SYM_TYPES];
330 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
331 GFP_KERNEL | __GFP_ZERO))
334 fa = p->type_val_to_struct_array;
335 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
336 GFP_KERNEL | __GFP_ZERO))
343 static int user_index(void *key, void *datum, void *datap)
346 struct user_datum *usrdatum;
347 struct flex_array *fa;
352 || usrdatum->value > p->p_users.nprim
353 || usrdatum->bounds > p->p_users.nprim)
356 fa = p->sym_val_to_name[SYM_USERS];
357 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
358 GFP_KERNEL | __GFP_ZERO))
360 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
364 static int sens_index(void *key, void *datum, void *datap)
367 struct level_datum *levdatum;
368 struct flex_array *fa;
373 if (!levdatum->isalias) {
374 if (!levdatum->level->sens ||
375 levdatum->level->sens > p->p_levels.nprim)
377 fa = p->sym_val_to_name[SYM_LEVELS];
378 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
379 GFP_KERNEL | __GFP_ZERO))
386 static int cat_index(void *key, void *datum, void *datap)
389 struct cat_datum *catdatum;
390 struct flex_array *fa;
395 if (!catdatum->isalias) {
396 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
398 fa = p->sym_val_to_name[SYM_CATS];
399 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
400 GFP_KERNEL | __GFP_ZERO))
407 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
420 static void symtab_hash_eval(struct symtab *s)
424 for (i = 0; i < SYM_NUM; i++) {
425 struct hashtab *h = s[i].table;
426 struct hashtab_info info;
428 hashtab_stat(h, &info);
429 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
430 "longest chain length %d\n", symtab_name[i], h->nel,
431 info.slots_used, h->size, info.max_chain_len);
435 static void rangetr_hash_eval(struct hashtab *h)
437 struct hashtab_info info;
439 hashtab_stat(h, &info);
440 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
441 "longest chain length %d\n", h->nel,
442 info.slots_used, h->size, info.max_chain_len);
445 static inline void rangetr_hash_eval(struct hashtab *h)
451 * Define the other val_to_name and val_to_struct arrays
452 * in a policy database structure.
454 * Caller must clean up on failure.
456 static int policydb_index(struct policydb *p)
460 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
461 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
463 printk(", %d sens, %d cats", p->p_levels.nprim,
467 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
468 p->p_classes.nprim, p->te_avtab.nel);
471 avtab_hash_eval(&p->te_avtab, "rules");
472 symtab_hash_eval(p->symtab);
476 p->class_val_to_struct =
477 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
479 if (!p->class_val_to_struct)
483 p->role_val_to_struct =
484 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
486 if (!p->role_val_to_struct)
490 p->user_val_to_struct =
491 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
493 if (!p->user_val_to_struct)
496 /* Yes, I want the sizeof the pointer, not the structure */
498 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
500 GFP_KERNEL | __GFP_ZERO);
501 if (!p->type_val_to_struct_array)
504 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
505 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
509 rc = cond_init_bool_indexes(p);
513 for (i = 0; i < SYM_NUM; i++) {
515 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
517 GFP_KERNEL | __GFP_ZERO);
518 if (!p->sym_val_to_name[i])
521 rc = flex_array_prealloc(p->sym_val_to_name[i],
522 0, p->symtab[i].nprim - 1,
523 GFP_KERNEL | __GFP_ZERO);
527 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
537 * The following *_destroy functions are used to
538 * free any memory allocated for each kind of
539 * symbol data in the policy database.
542 static int perm_destroy(void *key, void *datum, void *p)
549 static int common_destroy(void *key, void *datum, void *p)
551 struct common_datum *comdatum;
556 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
557 hashtab_destroy(comdatum->permissions.table);
563 static int cls_destroy(void *key, void *datum, void *p)
565 struct class_datum *cladatum;
566 struct constraint_node *constraint, *ctemp;
567 struct constraint_expr *e, *etmp;
572 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
573 hashtab_destroy(cladatum->permissions.table);
574 constraint = cladatum->constraints;
576 e = constraint->expr;
578 ebitmap_destroy(&e->names);
584 constraint = constraint->next;
588 constraint = cladatum->validatetrans;
590 e = constraint->expr;
592 ebitmap_destroy(&e->names);
598 constraint = constraint->next;
602 kfree(cladatum->comkey);
608 static int role_destroy(void *key, void *datum, void *p)
610 struct role_datum *role;
615 ebitmap_destroy(&role->dominates);
616 ebitmap_destroy(&role->types);
622 static int type_destroy(void *key, void *datum, void *p)
629 static int user_destroy(void *key, void *datum, void *p)
631 struct user_datum *usrdatum;
636 ebitmap_destroy(&usrdatum->roles);
637 ebitmap_destroy(&usrdatum->range.level[0].cat);
638 ebitmap_destroy(&usrdatum->range.level[1].cat);
639 ebitmap_destroy(&usrdatum->dfltlevel.cat);
645 static int sens_destroy(void *key, void *datum, void *p)
647 struct level_datum *levdatum;
652 ebitmap_destroy(&levdatum->level->cat);
653 kfree(levdatum->level);
659 static int cat_destroy(void *key, void *datum, void *p)
666 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
678 static int range_tr_destroy(void *key, void *datum, void *p)
680 struct mls_range *rt = datum;
682 ebitmap_destroy(&rt->level[0].cat);
683 ebitmap_destroy(&rt->level[1].cat);
689 static void ocontext_destroy(struct ocontext *c, int i)
694 context_destroy(&c->context[0]);
695 context_destroy(&c->context[1]);
696 if (i == OCON_ISID || i == OCON_FS ||
697 i == OCON_NETIF || i == OCON_FSUSE)
703 * Free any memory allocated by a policy database structure.
705 void policydb_destroy(struct policydb *p)
707 struct ocontext *c, *ctmp;
708 struct genfs *g, *gtmp;
710 struct role_allow *ra, *lra = NULL;
711 struct role_trans *tr, *ltr = NULL;
712 struct filename_trans *ft, *nft;
714 for (i = 0; i < SYM_NUM; i++) {
716 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
717 hashtab_destroy(p->symtab[i].table);
720 for (i = 0; i < SYM_NUM; i++) {
721 if (p->sym_val_to_name[i])
722 flex_array_free(p->sym_val_to_name[i]);
725 kfree(p->class_val_to_struct);
726 kfree(p->role_val_to_struct);
727 kfree(p->user_val_to_struct);
728 if (p->type_val_to_struct_array)
729 flex_array_free(p->type_val_to_struct_array);
731 avtab_destroy(&p->te_avtab);
733 for (i = 0; i < OCON_NUM; i++) {
739 ocontext_destroy(ctmp, i);
741 p->ocontexts[i] = NULL;
752 ocontext_destroy(ctmp, OCON_FSUSE);
760 cond_policydb_destroy(p);
762 for (tr = p->role_tr; tr; tr = tr->next) {
769 for (ra = p->role_allow; ra; ra = ra->next) {
776 hashtab_map(p->range_tr, range_tr_destroy, NULL);
777 hashtab_destroy(p->range_tr);
779 if (p->type_attr_map_array) {
780 for (i = 0; i < p->p_types.nprim; i++) {
783 e = flex_array_get(p->type_attr_map_array, i);
788 flex_array_free(p->type_attr_map_array);
791 ft = p->filename_trans;
799 ebitmap_destroy(&p->policycaps);
800 ebitmap_destroy(&p->permissive_map);
806 * Load the initial SIDs specified in a policy database
807 * structure into a SID table.
809 int policydb_load_isids(struct policydb *p, struct sidtab *s)
811 struct ocontext *head, *c;
816 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
820 head = p->ocontexts[OCON_ISID];
821 for (c = head; c; c = c->next) {
823 if (!c->context[0].user) {
824 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
829 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
831 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
841 int policydb_class_isvalid(struct policydb *p, unsigned int class)
843 if (!class || class > p->p_classes.nprim)
848 int policydb_role_isvalid(struct policydb *p, unsigned int role)
850 if (!role || role > p->p_roles.nprim)
855 int policydb_type_isvalid(struct policydb *p, unsigned int type)
857 if (!type || type > p->p_types.nprim)
863 * Return 1 if the fields in the security context
864 * structure `c' are valid. Return 0 otherwise.
866 int policydb_context_isvalid(struct policydb *p, struct context *c)
868 struct role_datum *role;
869 struct user_datum *usrdatum;
871 if (!c->role || c->role > p->p_roles.nprim)
874 if (!c->user || c->user > p->p_users.nprim)
877 if (!c->type || c->type > p->p_types.nprim)
880 if (c->role != OBJECT_R_VAL) {
882 * Role must be authorized for the type.
884 role = p->role_val_to_struct[c->role - 1];
885 if (!ebitmap_get_bit(&role->types, c->type - 1))
886 /* role may not be associated with type */
890 * User must be authorized for the role.
892 usrdatum = p->user_val_to_struct[c->user - 1];
896 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
897 /* user may not be associated with role */
901 if (!mls_context_isvalid(p, c))
908 * Read a MLS range structure from a policydb binary
909 * representation file.
911 static int mls_read_range_helper(struct mls_range *r, void *fp)
917 rc = next_entry(buf, fp, sizeof(u32));
922 items = le32_to_cpu(buf[0]);
923 if (items > ARRAY_SIZE(buf)) {
924 printk(KERN_ERR "SELinux: mls: range overflow\n");
928 rc = next_entry(buf, fp, sizeof(u32) * items);
930 printk(KERN_ERR "SELinux: mls: truncated range\n");
934 r->level[0].sens = le32_to_cpu(buf[0]);
936 r->level[1].sens = le32_to_cpu(buf[1]);
938 r->level[1].sens = r->level[0].sens;
940 rc = ebitmap_read(&r->level[0].cat, fp);
942 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
946 rc = ebitmap_read(&r->level[1].cat, fp);
948 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
952 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
954 printk(KERN_ERR "SELinux: mls: out of memory\n");
961 ebitmap_destroy(&r->level[0].cat);
967 * Read and validate a security context structure
968 * from a policydb binary representation file.
970 static int context_read_and_validate(struct context *c,
977 rc = next_entry(buf, fp, sizeof buf);
979 printk(KERN_ERR "SELinux: context truncated\n");
982 c->user = le32_to_cpu(buf[0]);
983 c->role = le32_to_cpu(buf[1]);
984 c->type = le32_to_cpu(buf[2]);
985 if (p->policyvers >= POLICYDB_VERSION_MLS) {
986 rc = mls_read_range_helper(&c->range, fp);
988 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
994 if (!policydb_context_isvalid(p, c)) {
995 printk(KERN_ERR "SELinux: invalid security context\n");
1005 * The following *_read functions are used to
1006 * read the symbol data from a policy database
1007 * binary representation file.
1010 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1013 struct perm_datum *perdatum;
1019 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1023 rc = next_entry(buf, fp, sizeof buf);
1027 len = le32_to_cpu(buf[0]);
1028 perdatum->value = le32_to_cpu(buf[1]);
1031 key = kmalloc(len + 1, GFP_KERNEL);
1035 rc = next_entry(key, fp, len);
1040 rc = hashtab_insert(h, key, perdatum);
1046 perm_destroy(key, perdatum, NULL);
1050 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1053 struct common_datum *comdatum;
1059 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1063 rc = next_entry(buf, fp, sizeof buf);
1067 len = le32_to_cpu(buf[0]);
1068 comdatum->value = le32_to_cpu(buf[1]);
1070 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1073 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1074 nel = le32_to_cpu(buf[3]);
1077 key = kmalloc(len + 1, GFP_KERNEL);
1081 rc = next_entry(key, fp, len);
1086 for (i = 0; i < nel; i++) {
1087 rc = perm_read(p, comdatum->permissions.table, fp);
1092 rc = hashtab_insert(h, key, comdatum);
1097 common_destroy(key, comdatum, NULL);
1101 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1102 int allowxtarget, void *fp)
1104 struct constraint_node *c, *lc;
1105 struct constraint_expr *e, *le;
1108 int rc, i, j, depth;
1111 for (i = 0; i < ncons; i++) {
1112 c = kzalloc(sizeof(*c), GFP_KERNEL);
1121 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1124 c->permissions = le32_to_cpu(buf[0]);
1125 nexpr = le32_to_cpu(buf[1]);
1128 for (j = 0; j < nexpr; j++) {
1129 e = kzalloc(sizeof(*e), GFP_KERNEL);
1138 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1141 e->expr_type = le32_to_cpu(buf[0]);
1142 e->attr = le32_to_cpu(buf[1]);
1143 e->op = le32_to_cpu(buf[2]);
1145 switch (e->expr_type) {
1157 if (depth == (CEXPR_MAXDEPTH - 1))
1162 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1164 if (depth == (CEXPR_MAXDEPTH - 1))
1167 rc = ebitmap_read(&e->names, fp);
1184 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1187 struct class_datum *cladatum;
1189 u32 len, len2, ncons, nel;
1193 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1197 rc = next_entry(buf, fp, sizeof(u32)*6);
1201 len = le32_to_cpu(buf[0]);
1202 len2 = le32_to_cpu(buf[1]);
1203 cladatum->value = le32_to_cpu(buf[2]);
1205 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1208 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1209 nel = le32_to_cpu(buf[4]);
1211 ncons = le32_to_cpu(buf[5]);
1214 key = kmalloc(len + 1, GFP_KERNEL);
1218 rc = next_entry(key, fp, len);
1225 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1226 if (!cladatum->comkey)
1228 rc = next_entry(cladatum->comkey, fp, len2);
1231 cladatum->comkey[len2] = '\0';
1234 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1235 if (!cladatum->comdatum) {
1236 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1240 for (i = 0; i < nel; i++) {
1241 rc = perm_read(p, cladatum->permissions.table, fp);
1246 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1250 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1251 /* grab the validatetrans rules */
1252 rc = next_entry(buf, fp, sizeof(u32));
1255 ncons = le32_to_cpu(buf[0]);
1256 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1261 rc = hashtab_insert(h, key, cladatum);
1267 cls_destroy(key, cladatum, NULL);
1271 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1274 struct role_datum *role;
1275 int rc, to_read = 2;
1280 role = kzalloc(sizeof(*role), GFP_KERNEL);
1284 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1287 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1291 len = le32_to_cpu(buf[0]);
1292 role->value = le32_to_cpu(buf[1]);
1293 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1294 role->bounds = le32_to_cpu(buf[2]);
1297 key = kmalloc(len + 1, GFP_KERNEL);
1301 rc = next_entry(key, fp, len);
1306 rc = ebitmap_read(&role->dominates, fp);
1310 rc = ebitmap_read(&role->types, fp);
1314 if (strcmp(key, OBJECT_R) == 0) {
1316 if (role->value != OBJECT_R_VAL) {
1317 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1318 OBJECT_R, role->value);
1325 rc = hashtab_insert(h, key, role);
1330 role_destroy(key, role, NULL);
1334 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1337 struct type_datum *typdatum;
1338 int rc, to_read = 3;
1343 typdatum = kzalloc(sizeof(*typdatum), 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 typdatum->value = le32_to_cpu(buf[1]);
1356 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1357 u32 prop = le32_to_cpu(buf[2]);
1359 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1360 typdatum->primary = 1;
1361 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1362 typdatum->attribute = 1;
1364 typdatum->bounds = le32_to_cpu(buf[3]);
1366 typdatum->primary = le32_to_cpu(buf[2]);
1370 key = kmalloc(len + 1, GFP_KERNEL);
1373 rc = next_entry(key, fp, len);
1378 rc = hashtab_insert(h, key, typdatum);
1383 type_destroy(key, typdatum, NULL);
1389 * Read a MLS level structure from a policydb binary
1390 * representation file.
1392 static int mls_read_level(struct mls_level *lp, void *fp)
1397 memset(lp, 0, sizeof(*lp));
1399 rc = next_entry(buf, fp, sizeof buf);
1401 printk(KERN_ERR "SELinux: mls: truncated level\n");
1404 lp->sens = le32_to_cpu(buf[0]);
1406 rc = ebitmap_read(&lp->cat, fp);
1408 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1414 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1417 struct user_datum *usrdatum;
1418 int rc, to_read = 2;
1423 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1430 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1434 len = le32_to_cpu(buf[0]);
1435 usrdatum->value = le32_to_cpu(buf[1]);
1436 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1437 usrdatum->bounds = le32_to_cpu(buf[2]);
1440 key = kmalloc(len + 1, GFP_KERNEL);
1443 rc = next_entry(key, fp, len);
1448 rc = ebitmap_read(&usrdatum->roles, fp);
1452 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1453 rc = mls_read_range_helper(&usrdatum->range, fp);
1456 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1461 rc = hashtab_insert(h, key, usrdatum);
1466 user_destroy(key, usrdatum, NULL);
1470 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1473 struct level_datum *levdatum;
1479 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1483 rc = next_entry(buf, fp, sizeof buf);
1487 len = le32_to_cpu(buf[0]);
1488 levdatum->isalias = le32_to_cpu(buf[1]);
1491 key = kmalloc(len + 1, GFP_ATOMIC);
1494 rc = next_entry(key, fp, len);
1500 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1501 if (!levdatum->level)
1504 rc = mls_read_level(levdatum->level, fp);
1508 rc = hashtab_insert(h, key, levdatum);
1513 sens_destroy(key, levdatum, NULL);
1517 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1520 struct cat_datum *catdatum;
1526 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1530 rc = next_entry(buf, fp, sizeof buf);
1534 len = le32_to_cpu(buf[0]);
1535 catdatum->value = le32_to_cpu(buf[1]);
1536 catdatum->isalias = le32_to_cpu(buf[2]);
1539 key = kmalloc(len + 1, GFP_ATOMIC);
1542 rc = next_entry(key, fp, len);
1547 rc = hashtab_insert(h, key, catdatum);
1552 cat_destroy(key, catdatum, NULL);
1556 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1568 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1570 struct user_datum *upper, *user;
1571 struct policydb *p = datap;
1574 upper = user = datum;
1575 while (upper->bounds) {
1576 struct ebitmap_node *node;
1579 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1580 printk(KERN_ERR "SELinux: user %s: "
1581 "too deep or looped boundary",
1586 upper = p->user_val_to_struct[upper->bounds - 1];
1587 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1588 if (ebitmap_get_bit(&upper->roles, bit))
1592 "SELinux: boundary violated policy: "
1593 "user=%s role=%s bounds=%s\n",
1594 sym_name(p, SYM_USERS, user->value - 1),
1595 sym_name(p, SYM_ROLES, bit),
1596 sym_name(p, SYM_USERS, upper->value - 1));
1605 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1607 struct role_datum *upper, *role;
1608 struct policydb *p = datap;
1611 upper = role = datum;
1612 while (upper->bounds) {
1613 struct ebitmap_node *node;
1616 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1617 printk(KERN_ERR "SELinux: role %s: "
1618 "too deep or looped bounds\n",
1623 upper = p->role_val_to_struct[upper->bounds - 1];
1624 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1625 if (ebitmap_get_bit(&upper->types, bit))
1629 "SELinux: boundary violated policy: "
1630 "role=%s type=%s bounds=%s\n",
1631 sym_name(p, SYM_ROLES, role->value - 1),
1632 sym_name(p, SYM_TYPES, bit),
1633 sym_name(p, SYM_ROLES, upper->value - 1));
1642 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1644 struct type_datum *upper;
1645 struct policydb *p = datap;
1649 while (upper->bounds) {
1650 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1651 printk(KERN_ERR "SELinux: type %s: "
1652 "too deep or looped boundary\n",
1657 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1661 if (upper->attribute) {
1662 printk(KERN_ERR "SELinux: type %s: "
1663 "bounded by attribute %s",
1665 sym_name(p, SYM_TYPES, upper->value - 1));
1673 static int policydb_bounds_sanity_check(struct policydb *p)
1677 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1680 rc = hashtab_map(p->p_users.table,
1681 user_bounds_sanity_check, p);
1685 rc = hashtab_map(p->p_roles.table,
1686 role_bounds_sanity_check, p);
1690 rc = hashtab_map(p->p_types.table,
1691 type_bounds_sanity_check, p);
1698 extern int ss_initialized;
1700 u16 string_to_security_class(struct policydb *p, const char *name)
1702 struct class_datum *cladatum;
1704 cladatum = hashtab_search(p->p_classes.table, name);
1708 return cladatum->value;
1711 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1713 struct class_datum *cladatum;
1714 struct perm_datum *perdatum = NULL;
1715 struct common_datum *comdatum;
1717 if (!tclass || tclass > p->p_classes.nprim)
1720 cladatum = p->class_val_to_struct[tclass-1];
1721 comdatum = cladatum->comdatum;
1723 perdatum = hashtab_search(comdatum->permissions.table,
1726 perdatum = hashtab_search(cladatum->permissions.table,
1731 return 1U << (perdatum->value-1);
1734 static int range_read(struct policydb *p, void *fp)
1736 struct range_trans *rt = NULL;
1737 struct mls_range *r = NULL;
1742 if (p->policyvers < POLICYDB_VERSION_MLS)
1745 rc = next_entry(buf, fp, sizeof(u32));
1749 nel = le32_to_cpu(buf[0]);
1750 for (i = 0; i < nel; i++) {
1752 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1756 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1760 rt->source_type = le32_to_cpu(buf[0]);
1761 rt->target_type = le32_to_cpu(buf[1]);
1762 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1763 rc = next_entry(buf, fp, sizeof(u32));
1766 rt->target_class = le32_to_cpu(buf[0]);
1768 rt->target_class = p->process_class;
1771 if (!policydb_type_isvalid(p, rt->source_type) ||
1772 !policydb_type_isvalid(p, rt->target_type) ||
1773 !policydb_class_isvalid(p, rt->target_class))
1777 r = kzalloc(sizeof(*r), GFP_KERNEL);
1781 rc = mls_read_range_helper(r, fp);
1786 if (!mls_range_isvalid(p, r)) {
1787 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1791 rc = hashtab_insert(p->range_tr, rt, r);
1798 rangetr_hash_eval(p->range_tr);
1806 static int filename_trans_read(struct policydb *p, void *fp)
1808 struct filename_trans *ft, *last;
1814 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1817 rc = next_entry(buf, fp, sizeof(u32));
1820 nel = le32_to_cpu(buf[0]);
1822 printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1824 last = p->filename_trans;
1825 while (last && last->next)
1828 for (i = 0; i < nel; i++) {
1830 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1834 /* add it to the tail of the list */
1836 p->filename_trans = ft;
1841 /* length of the path component string */
1842 rc = next_entry(buf, fp, sizeof(u32));
1845 len = le32_to_cpu(buf[0]);
1848 name = kmalloc(len + 1, GFP_KERNEL);
1854 /* path component string */
1855 rc = next_entry(name, fp, len);
1860 printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1862 rc = next_entry(buf, fp, sizeof(u32) * 4);
1866 ft->stype = le32_to_cpu(buf[0]);
1867 ft->ttype = le32_to_cpu(buf[1]);
1868 ft->tclass = le32_to_cpu(buf[2]);
1869 ft->otype = le32_to_cpu(buf[3]);
1876 static int genfs_read(struct policydb *p, void *fp)
1879 u32 nel, nel2, len, len2;
1881 struct ocontext *l, *c;
1882 struct ocontext *newc = NULL;
1883 struct genfs *genfs_p, *genfs;
1884 struct genfs *newgenfs = NULL;
1886 rc = next_entry(buf, fp, sizeof(u32));
1889 nel = le32_to_cpu(buf[0]);
1891 for (i = 0; i < nel; i++) {
1892 rc = next_entry(buf, fp, sizeof(u32));
1895 len = le32_to_cpu(buf[0]);
1898 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1903 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1904 if (!newgenfs->fstype)
1907 rc = next_entry(newgenfs->fstype, fp, len);
1911 newgenfs->fstype[len] = 0;
1913 for (genfs_p = NULL, genfs = p->genfs; genfs;
1914 genfs_p = genfs, genfs = genfs->next) {
1916 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1917 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1921 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1924 newgenfs->next = genfs;
1926 genfs_p->next = newgenfs;
1928 p->genfs = newgenfs;
1932 rc = next_entry(buf, fp, sizeof(u32));
1936 nel2 = le32_to_cpu(buf[0]);
1937 for (j = 0; j < nel2; j++) {
1938 rc = next_entry(buf, fp, sizeof(u32));
1941 len = le32_to_cpu(buf[0]);
1944 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1949 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1953 rc = next_entry(newc->u.name, fp, len);
1956 newc->u.name[len] = 0;
1958 rc = next_entry(buf, fp, sizeof(u32));
1962 newc->v.sclass = le32_to_cpu(buf[0]);
1963 rc = context_read_and_validate(&newc->context[0], p, fp);
1967 for (l = NULL, c = genfs->head; c;
1968 l = c, c = c->next) {
1970 if (!strcmp(newc->u.name, c->u.name) &&
1971 (!c->v.sclass || !newc->v.sclass ||
1972 newc->v.sclass == c->v.sclass)) {
1973 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1974 genfs->fstype, c->u.name);
1977 len = strlen(newc->u.name);
1978 len2 = strlen(c->u.name);
1994 kfree(newgenfs->fstype);
1996 ocontext_destroy(newc, OCON_FSUSE);
2001 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2007 struct ocontext *l, *c;
2010 for (i = 0; i < info->ocon_num; i++) {
2011 rc = next_entry(buf, fp, sizeof(u32));
2014 nel = le32_to_cpu(buf[0]);
2017 for (j = 0; j < nel; j++) {
2019 c = kzalloc(sizeof(*c), GFP_KERNEL);
2025 p->ocontexts[i] = c;
2030 rc = next_entry(buf, fp, sizeof(u32));
2034 c->sid[0] = le32_to_cpu(buf[0]);
2035 rc = context_read_and_validate(&c->context[0], p, fp);
2041 rc = next_entry(buf, fp, sizeof(u32));
2044 len = le32_to_cpu(buf[0]);
2047 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2051 rc = next_entry(c->u.name, fp, len);
2056 rc = context_read_and_validate(&c->context[0], p, fp);
2059 rc = context_read_and_validate(&c->context[1], p, fp);
2064 rc = next_entry(buf, fp, sizeof(u32)*3);
2067 c->u.port.protocol = le32_to_cpu(buf[0]);
2068 c->u.port.low_port = le32_to_cpu(buf[1]);
2069 c->u.port.high_port = le32_to_cpu(buf[2]);
2070 rc = context_read_and_validate(&c->context[0], p, fp);
2075 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2078 c->u.node.addr = nodebuf[0]; /* network order */
2079 c->u.node.mask = nodebuf[1]; /* network order */
2080 rc = context_read_and_validate(&c->context[0], p, fp);
2085 rc = next_entry(buf, fp, sizeof(u32)*2);
2090 c->v.behavior = le32_to_cpu(buf[0]);
2091 if (c->v.behavior > SECURITY_FS_USE_NONE)
2095 len = le32_to_cpu(buf[1]);
2096 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2100 rc = next_entry(c->u.name, fp, len);
2104 rc = context_read_and_validate(&c->context[0], p, fp);
2111 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2114 for (k = 0; k < 4; k++)
2115 c->u.node6.addr[k] = nodebuf[k];
2116 for (k = 0; k < 4; k++)
2117 c->u.node6.mask[k] = nodebuf[k+4];
2118 rc = context_read_and_validate(&c->context[0], p, fp);
2132 * Read the configuration data from a policy database binary
2133 * representation file into a policy database structure.
2135 int policydb_read(struct policydb *p, void *fp)
2137 struct role_allow *ra, *lra;
2138 struct role_trans *tr, *ltr;
2141 u32 len, nprim, nel;
2144 struct policydb_compat_info *info;
2146 rc = policydb_init(p);
2150 /* Read the magic number and string length. */
2151 rc = next_entry(buf, fp, sizeof(u32) * 2);
2156 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2157 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2158 "not match expected magic number 0x%x\n",
2159 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2164 len = le32_to_cpu(buf[1]);
2165 if (len != strlen(POLICYDB_STRING)) {
2166 printk(KERN_ERR "SELinux: policydb string length %d does not "
2167 "match expected length %Zu\n",
2168 len, strlen(POLICYDB_STRING));
2173 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2174 if (!policydb_str) {
2175 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2176 "string of length %d\n", len);
2180 rc = next_entry(policydb_str, fp, len);
2182 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2183 kfree(policydb_str);
2188 policydb_str[len] = '\0';
2189 if (strcmp(policydb_str, POLICYDB_STRING)) {
2190 printk(KERN_ERR "SELinux: policydb string %s does not match "
2191 "my string %s\n", policydb_str, POLICYDB_STRING);
2192 kfree(policydb_str);
2195 /* Done with policydb_str. */
2196 kfree(policydb_str);
2197 policydb_str = NULL;
2199 /* Read the version and table sizes. */
2200 rc = next_entry(buf, fp, sizeof(u32)*4);
2205 p->policyvers = le32_to_cpu(buf[0]);
2206 if (p->policyvers < POLICYDB_VERSION_MIN ||
2207 p->policyvers > POLICYDB_VERSION_MAX) {
2208 printk(KERN_ERR "SELinux: policydb version %d does not match "
2209 "my version range %d-%d\n",
2210 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2214 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2218 if (p->policyvers < POLICYDB_VERSION_MLS) {
2219 printk(KERN_ERR "SELinux: security policydb version %d "
2220 "(MLS) not backwards compatible\n",
2225 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2226 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2228 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2229 rc = ebitmap_read(&p->policycaps, fp);
2234 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2235 rc = ebitmap_read(&p->permissive_map, fp);
2241 info = policydb_lookup_compat(p->policyvers);
2243 printk(KERN_ERR "SELinux: unable to find policy compat info "
2244 "for version %d\n", p->policyvers);
2249 if (le32_to_cpu(buf[2]) != info->sym_num ||
2250 le32_to_cpu(buf[3]) != info->ocon_num) {
2251 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2252 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2253 le32_to_cpu(buf[3]),
2254 info->sym_num, info->ocon_num);
2258 for (i = 0; i < info->sym_num; i++) {
2259 rc = next_entry(buf, fp, sizeof(u32)*2);
2262 nprim = le32_to_cpu(buf[0]);
2263 nel = le32_to_cpu(buf[1]);
2264 for (j = 0; j < nel; j++) {
2265 rc = read_f[i](p, p->symtab[i].table, fp);
2270 p->symtab[i].nprim = nprim;
2273 rc = avtab_read(&p->te_avtab, fp, p);
2277 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2278 rc = cond_read_list(p, fp);
2283 rc = next_entry(buf, fp, sizeof(u32));
2286 nel = le32_to_cpu(buf[0]);
2288 for (i = 0; i < nel; i++) {
2290 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2297 rc = next_entry(buf, fp, sizeof(u32)*3);
2302 tr->role = le32_to_cpu(buf[0]);
2303 tr->type = le32_to_cpu(buf[1]);
2304 tr->new_role = le32_to_cpu(buf[2]);
2305 if (!policydb_role_isvalid(p, tr->role) ||
2306 !policydb_type_isvalid(p, tr->type) ||
2307 !policydb_role_isvalid(p, tr->new_role))
2312 rc = next_entry(buf, fp, sizeof(u32));
2315 nel = le32_to_cpu(buf[0]);
2317 for (i = 0; i < nel; i++) {
2319 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2326 rc = next_entry(buf, fp, sizeof(u32)*2);
2331 ra->role = le32_to_cpu(buf[0]);
2332 ra->new_role = le32_to_cpu(buf[1]);
2333 if (!policydb_role_isvalid(p, ra->role) ||
2334 !policydb_role_isvalid(p, ra->new_role))
2339 rc = filename_trans_read(p, fp);
2343 rc = policydb_index(p);
2348 p->process_class = string_to_security_class(p, "process");
2349 if (!p->process_class)
2353 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2354 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2355 if (!p->process_trans_perms)
2358 rc = ocontext_read(p, info, fp);
2362 rc = genfs_read(p, fp);
2366 rc = range_read(p, fp);
2371 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2373 GFP_KERNEL | __GFP_ZERO);
2374 if (!p->type_attr_map_array)
2377 /* preallocate so we don't have to worry about the put ever failing */
2378 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2379 GFP_KERNEL | __GFP_ZERO);
2383 for (i = 0; i < p->p_types.nprim; i++) {
2384 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2388 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2389 rc = ebitmap_read(e, fp);
2393 /* add the type itself as the degenerate case */
2394 rc = ebitmap_set_bit(e, i, 1);
2399 rc = policydb_bounds_sanity_check(p);
2407 policydb_destroy(p);
2412 * Write a MLS level structure to a policydb binary
2413 * representation file.
2415 static int mls_write_level(struct mls_level *l, void *fp)
2420 buf[0] = cpu_to_le32(l->sens);
2421 rc = put_entry(buf, sizeof(u32), 1, fp);
2425 rc = ebitmap_write(&l->cat, fp);
2433 * Write a MLS range structure to a policydb binary
2434 * representation file.
2436 static int mls_write_range_helper(struct mls_range *r, void *fp)
2442 eq = mls_level_eq(&r->level[1], &r->level[0]);
2448 buf[0] = cpu_to_le32(items-1);
2449 buf[1] = cpu_to_le32(r->level[0].sens);
2451 buf[2] = cpu_to_le32(r->level[1].sens);
2453 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2455 rc = put_entry(buf, sizeof(u32), items, fp);
2459 rc = ebitmap_write(&r->level[0].cat, fp);
2463 rc = ebitmap_write(&r->level[1].cat, fp);
2471 static int sens_write(void *vkey, void *datum, void *ptr)
2474 struct level_datum *levdatum = datum;
2475 struct policy_data *pd = ptr;
2482 buf[0] = cpu_to_le32(len);
2483 buf[1] = cpu_to_le32(levdatum->isalias);
2484 rc = put_entry(buf, sizeof(u32), 2, fp);
2488 rc = put_entry(key, 1, len, fp);
2492 rc = mls_write_level(levdatum->level, fp);
2499 static int cat_write(void *vkey, void *datum, void *ptr)
2502 struct cat_datum *catdatum = datum;
2503 struct policy_data *pd = ptr;
2510 buf[0] = cpu_to_le32(len);
2511 buf[1] = cpu_to_le32(catdatum->value);
2512 buf[2] = cpu_to_le32(catdatum->isalias);
2513 rc = put_entry(buf, sizeof(u32), 3, fp);
2517 rc = put_entry(key, 1, len, fp);
2524 static int role_trans_write(struct role_trans *r, void *fp)
2526 struct role_trans *tr;
2532 for (tr = r; tr; tr = tr->next)
2534 buf[0] = cpu_to_le32(nel);
2535 rc = put_entry(buf, sizeof(u32), 1, fp);
2538 for (tr = r; tr; tr = tr->next) {
2539 buf[0] = cpu_to_le32(tr->role);
2540 buf[1] = cpu_to_le32(tr->type);
2541 buf[2] = cpu_to_le32(tr->new_role);
2542 rc = put_entry(buf, sizeof(u32), 3, fp);
2550 static int role_allow_write(struct role_allow *r, void *fp)
2552 struct role_allow *ra;
2558 for (ra = r; ra; ra = ra->next)
2560 buf[0] = cpu_to_le32(nel);
2561 rc = put_entry(buf, sizeof(u32), 1, fp);
2564 for (ra = r; ra; ra = ra->next) {
2565 buf[0] = cpu_to_le32(ra->role);
2566 buf[1] = cpu_to_le32(ra->new_role);
2567 rc = put_entry(buf, sizeof(u32), 2, fp);
2575 * Write a security context structure
2576 * to a policydb binary representation file.
2578 static int context_write(struct policydb *p, struct context *c,
2584 buf[0] = cpu_to_le32(c->user);
2585 buf[1] = cpu_to_le32(c->role);
2586 buf[2] = cpu_to_le32(c->type);
2588 rc = put_entry(buf, sizeof(u32), 3, fp);
2592 rc = mls_write_range_helper(&c->range, fp);
2600 * The following *_write functions are used to
2601 * write the symbol data to a policy database
2602 * binary representation file.
2605 static int perm_write(void *vkey, void *datum, void *fp)
2608 struct perm_datum *perdatum = datum;
2614 buf[0] = cpu_to_le32(len);
2615 buf[1] = cpu_to_le32(perdatum->value);
2616 rc = put_entry(buf, sizeof(u32), 2, fp);
2620 rc = put_entry(key, 1, len, fp);
2627 static int common_write(void *vkey, void *datum, void *ptr)
2630 struct common_datum *comdatum = datum;
2631 struct policy_data *pd = ptr;
2638 buf[0] = cpu_to_le32(len);
2639 buf[1] = cpu_to_le32(comdatum->value);
2640 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2641 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2642 rc = put_entry(buf, sizeof(u32), 4, fp);
2646 rc = put_entry(key, 1, len, fp);
2650 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2657 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2660 struct constraint_node *c;
2661 struct constraint_expr *e;
2666 for (c = node; c; c = c->next) {
2668 for (e = c->expr; e; e = e->next)
2670 buf[0] = cpu_to_le32(c->permissions);
2671 buf[1] = cpu_to_le32(nel);
2672 rc = put_entry(buf, sizeof(u32), 2, fp);
2675 for (e = c->expr; e; e = e->next) {
2676 buf[0] = cpu_to_le32(e->expr_type);
2677 buf[1] = cpu_to_le32(e->attr);
2678 buf[2] = cpu_to_le32(e->op);
2679 rc = put_entry(buf, sizeof(u32), 3, fp);
2683 switch (e->expr_type) {
2685 rc = ebitmap_write(&e->names, fp);
2698 static int class_write(void *vkey, void *datum, void *ptr)
2701 struct class_datum *cladatum = datum;
2702 struct policy_data *pd = ptr;
2704 struct policydb *p = pd->p;
2705 struct constraint_node *c;
2712 if (cladatum->comkey)
2713 len2 = strlen(cladatum->comkey);
2718 for (c = cladatum->constraints; c; c = c->next)
2721 buf[0] = cpu_to_le32(len);
2722 buf[1] = cpu_to_le32(len2);
2723 buf[2] = cpu_to_le32(cladatum->value);
2724 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2725 if (cladatum->permissions.table)
2726 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2729 buf[5] = cpu_to_le32(ncons);
2730 rc = put_entry(buf, sizeof(u32), 6, fp);
2734 rc = put_entry(key, 1, len, fp);
2738 if (cladatum->comkey) {
2739 rc = put_entry(cladatum->comkey, 1, len2, fp);
2744 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2748 rc = write_cons_helper(p, cladatum->constraints, fp);
2752 /* write out the validatetrans rule */
2754 for (c = cladatum->validatetrans; c; c = c->next)
2757 buf[0] = cpu_to_le32(ncons);
2758 rc = put_entry(buf, sizeof(u32), 1, fp);
2762 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2769 static int role_write(void *vkey, void *datum, void *ptr)
2772 struct role_datum *role = datum;
2773 struct policy_data *pd = ptr;
2775 struct policydb *p = pd->p;
2782 buf[items++] = cpu_to_le32(len);
2783 buf[items++] = cpu_to_le32(role->value);
2784 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2785 buf[items++] = cpu_to_le32(role->bounds);
2787 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2789 rc = put_entry(buf, sizeof(u32), items, fp);
2793 rc = put_entry(key, 1, len, fp);
2797 rc = ebitmap_write(&role->dominates, fp);
2801 rc = ebitmap_write(&role->types, fp);
2808 static int type_write(void *vkey, void *datum, void *ptr)
2811 struct type_datum *typdatum = datum;
2812 struct policy_data *pd = ptr;
2813 struct policydb *p = pd->p;
2821 buf[items++] = cpu_to_le32(len);
2822 buf[items++] = cpu_to_le32(typdatum->value);
2823 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2826 if (typdatum->primary)
2827 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2829 if (typdatum->attribute)
2830 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2832 buf[items++] = cpu_to_le32(properties);
2833 buf[items++] = cpu_to_le32(typdatum->bounds);
2835 buf[items++] = cpu_to_le32(typdatum->primary);
2837 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2838 rc = put_entry(buf, sizeof(u32), items, fp);
2842 rc = put_entry(key, 1, len, fp);
2849 static int user_write(void *vkey, void *datum, void *ptr)
2852 struct user_datum *usrdatum = datum;
2853 struct policy_data *pd = ptr;
2854 struct policydb *p = pd->p;
2862 buf[items++] = cpu_to_le32(len);
2863 buf[items++] = cpu_to_le32(usrdatum->value);
2864 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2865 buf[items++] = cpu_to_le32(usrdatum->bounds);
2866 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2867 rc = put_entry(buf, sizeof(u32), items, fp);
2871 rc = put_entry(key, 1, len, fp);
2875 rc = ebitmap_write(&usrdatum->roles, fp);
2879 rc = mls_write_range_helper(&usrdatum->range, fp);
2883 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2890 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2903 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2906 unsigned int i, j, rc;
2911 for (i = 0; i < info->ocon_num; i++) {
2913 for (c = p->ocontexts[i]; c; c = c->next)
2915 buf[0] = cpu_to_le32(nel);
2916 rc = put_entry(buf, sizeof(u32), 1, fp);
2919 for (c = p->ocontexts[i]; c; c = c->next) {
2922 buf[0] = cpu_to_le32(c->sid[0]);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2926 rc = context_write(p, &c->context[0], fp);
2932 len = strlen(c->u.name);
2933 buf[0] = cpu_to_le32(len);
2934 rc = put_entry(buf, sizeof(u32), 1, fp);
2937 rc = put_entry(c->u.name, 1, len, fp);
2940 rc = context_write(p, &c->context[0], fp);
2943 rc = context_write(p, &c->context[1], fp);
2948 buf[0] = cpu_to_le32(c->u.port.protocol);
2949 buf[1] = cpu_to_le32(c->u.port.low_port);
2950 buf[2] = cpu_to_le32(c->u.port.high_port);
2951 rc = put_entry(buf, sizeof(u32), 3, fp);
2954 rc = context_write(p, &c->context[0], fp);
2959 nodebuf[0] = c->u.node.addr; /* network order */
2960 nodebuf[1] = c->u.node.mask; /* network order */
2961 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2964 rc = context_write(p, &c->context[0], fp);
2969 buf[0] = cpu_to_le32(c->v.behavior);
2970 len = strlen(c->u.name);
2971 buf[1] = cpu_to_le32(len);
2972 rc = put_entry(buf, sizeof(u32), 2, fp);
2975 rc = put_entry(c->u.name, 1, len, fp);
2978 rc = context_write(p, &c->context[0], fp);
2983 for (j = 0; j < 4; j++)
2984 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2985 for (j = 0; j < 4; j++)
2986 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2987 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2990 rc = context_write(p, &c->context[0], fp);
3000 static int genfs_write(struct policydb *p, void *fp)
3002 struct genfs *genfs;
3009 for (genfs = p->genfs; genfs; genfs = genfs->next)
3011 buf[0] = cpu_to_le32(len);
3012 rc = put_entry(buf, sizeof(u32), 1, fp);
3015 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3016 len = strlen(genfs->fstype);
3017 buf[0] = cpu_to_le32(len);
3018 rc = put_entry(buf, sizeof(u32), 1, fp);
3021 rc = put_entry(genfs->fstype, 1, len, fp);
3025 for (c = genfs->head; c; c = c->next)
3027 buf[0] = cpu_to_le32(len);
3028 rc = put_entry(buf, sizeof(u32), 1, fp);
3031 for (c = genfs->head; c; c = c->next) {
3032 len = strlen(c->u.name);
3033 buf[0] = cpu_to_le32(len);
3034 rc = put_entry(buf, sizeof(u32), 1, fp);
3037 rc = put_entry(c->u.name, 1, len, fp);
3040 buf[0] = cpu_to_le32(c->v.sclass);
3041 rc = put_entry(buf, sizeof(u32), 1, fp);
3044 rc = context_write(p, &c->context[0], fp);
3052 static int range_count(void *key, void *data, void *ptr)
3060 static int range_write_helper(void *key, void *data, void *ptr)
3063 struct range_trans *rt = key;
3064 struct mls_range *r = data;
3065 struct policy_data *pd = ptr;
3067 struct policydb *p = pd->p;
3070 buf[0] = cpu_to_le32(rt->source_type);
3071 buf[1] = cpu_to_le32(rt->target_type);
3072 rc = put_entry(buf, sizeof(u32), 2, fp);
3075 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3076 buf[0] = cpu_to_le32(rt->target_class);
3077 rc = put_entry(buf, sizeof(u32), 1, fp);
3081 rc = mls_write_range_helper(r, fp);
3088 static int range_write(struct policydb *p, void *fp)
3093 struct policy_data pd;
3098 /* count the number of entries in the hashtab */
3100 rc = hashtab_map(p->range_tr, range_count, &nel);
3104 buf[0] = cpu_to_le32(nel);
3105 rc = put_entry(buf, sizeof(u32), 1, fp);
3109 /* actually write all of the entries */
3110 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3117 static int filename_trans_write(struct policydb *p, void *fp)
3119 struct filename_trans *ft;
3124 for (ft = p->filename_trans; ft; ft = ft->next)
3127 buf[0] = cpu_to_le32(nel);
3128 rc = put_entry(buf, sizeof(u32), 1, fp);
3132 for (ft = p->filename_trans; ft; ft = ft->next) {
3133 len = strlen(ft->name);
3134 buf[0] = cpu_to_le32(len);
3135 rc = put_entry(buf, sizeof(u32), 1, fp);
3139 rc = put_entry(ft->name, sizeof(char), len, fp);
3145 buf[2] = ft->tclass;
3148 rc = put_entry(buf, sizeof(u32), 4, fp);
3155 * Write the configuration data in a policy database
3156 * structure to a policy database binary representation
3159 int policydb_write(struct policydb *p, void *fp)
3161 unsigned int i, num_syms;
3166 struct policydb_compat_info *info;
3169 * refuse to write policy older than compressed avtab
3170 * to simplify the writer. There are other tests dropped
3171 * since we assume this throughout the writer code. Be
3172 * careful if you ever try to remove this restriction
3174 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3175 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3176 " Because it is less than version %d\n", p->policyvers,
3177 POLICYDB_VERSION_AVTAB);
3183 config |= POLICYDB_CONFIG_MLS;
3185 if (p->reject_unknown)
3186 config |= REJECT_UNKNOWN;
3187 if (p->allow_unknown)
3188 config |= ALLOW_UNKNOWN;
3190 /* Write the magic number and string identifiers. */
3191 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3192 len = strlen(POLICYDB_STRING);
3193 buf[1] = cpu_to_le32(len);
3194 rc = put_entry(buf, sizeof(u32), 2, fp);
3197 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3201 /* Write the version, config, and table sizes. */
3202 info = policydb_lookup_compat(p->policyvers);
3204 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3205 "version %d", p->policyvers);
3209 buf[0] = cpu_to_le32(p->policyvers);
3210 buf[1] = cpu_to_le32(config);
3211 buf[2] = cpu_to_le32(info->sym_num);
3212 buf[3] = cpu_to_le32(info->ocon_num);
3214 rc = put_entry(buf, sizeof(u32), 4, fp);
3218 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3219 rc = ebitmap_write(&p->policycaps, fp);
3224 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3225 rc = ebitmap_write(&p->permissive_map, fp);
3230 num_syms = info->sym_num;
3231 for (i = 0; i < num_syms; i++) {
3232 struct policy_data pd;
3237 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3238 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3240 rc = put_entry(buf, sizeof(u32), 2, fp);
3243 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3248 rc = avtab_write(p, &p->te_avtab, fp);
3252 rc = cond_write_list(p, p->cond_list, fp);
3256 rc = role_trans_write(p->role_tr, fp);
3260 rc = role_allow_write(p->role_allow, fp);
3264 rc = filename_trans_write(p, fp);
3268 rc = ocontext_write(p, info, fp);
3272 rc = genfs_write(p, fp);
3276 rc = range_write(p, fp);
3280 for (i = 0; i < p->p_types.nprim; i++) {
3281 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3284 rc = ebitmap_write(e, fp);