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, 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,
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 last = p->filename_trans;
1823 while (last && last->next)
1826 for (i = 0; i < nel; i++) {
1828 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1832 /* add it to the tail of the list */
1834 p->filename_trans = ft;
1839 /* length of the path component string */
1840 rc = next_entry(buf, fp, sizeof(u32));
1843 len = le32_to_cpu(buf[0]);
1846 name = kmalloc(len + 1, GFP_KERNEL);
1852 /* path component string */
1853 rc = next_entry(name, fp, len);
1858 rc = next_entry(buf, fp, sizeof(u32) * 4);
1862 ft->stype = le32_to_cpu(buf[0]);
1863 ft->ttype = le32_to_cpu(buf[1]);
1864 ft->tclass = le32_to_cpu(buf[2]);
1865 ft->otype = le32_to_cpu(buf[3]);
1872 static int genfs_read(struct policydb *p, void *fp)
1875 u32 nel, nel2, len, len2;
1877 struct ocontext *l, *c;
1878 struct ocontext *newc = NULL;
1879 struct genfs *genfs_p, *genfs;
1880 struct genfs *newgenfs = NULL;
1882 rc = next_entry(buf, fp, sizeof(u32));
1885 nel = le32_to_cpu(buf[0]);
1887 for (i = 0; i < nel; i++) {
1888 rc = next_entry(buf, fp, sizeof(u32));
1891 len = le32_to_cpu(buf[0]);
1894 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1899 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1900 if (!newgenfs->fstype)
1903 rc = next_entry(newgenfs->fstype, fp, len);
1907 newgenfs->fstype[len] = 0;
1909 for (genfs_p = NULL, genfs = p->genfs; genfs;
1910 genfs_p = genfs, genfs = genfs->next) {
1912 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1913 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1917 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1920 newgenfs->next = genfs;
1922 genfs_p->next = newgenfs;
1924 p->genfs = newgenfs;
1928 rc = next_entry(buf, fp, sizeof(u32));
1932 nel2 = le32_to_cpu(buf[0]);
1933 for (j = 0; j < nel2; j++) {
1934 rc = next_entry(buf, fp, sizeof(u32));
1937 len = le32_to_cpu(buf[0]);
1940 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1945 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1949 rc = next_entry(newc->u.name, fp, len);
1952 newc->u.name[len] = 0;
1954 rc = next_entry(buf, fp, sizeof(u32));
1958 newc->v.sclass = le32_to_cpu(buf[0]);
1959 rc = context_read_and_validate(&newc->context[0], p, fp);
1963 for (l = NULL, c = genfs->head; c;
1964 l = c, c = c->next) {
1966 if (!strcmp(newc->u.name, c->u.name) &&
1967 (!c->v.sclass || !newc->v.sclass ||
1968 newc->v.sclass == c->v.sclass)) {
1969 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1970 genfs->fstype, c->u.name);
1973 len = strlen(newc->u.name);
1974 len2 = strlen(c->u.name);
1990 kfree(newgenfs->fstype);
1992 ocontext_destroy(newc, OCON_FSUSE);
1997 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2003 struct ocontext *l, *c;
2006 for (i = 0; i < info->ocon_num; i++) {
2007 rc = next_entry(buf, fp, sizeof(u32));
2010 nel = le32_to_cpu(buf[0]);
2013 for (j = 0; j < nel; j++) {
2015 c = kzalloc(sizeof(*c), GFP_KERNEL);
2021 p->ocontexts[i] = c;
2026 rc = next_entry(buf, fp, sizeof(u32));
2030 c->sid[0] = le32_to_cpu(buf[0]);
2031 rc = context_read_and_validate(&c->context[0], p, fp);
2037 rc = next_entry(buf, fp, sizeof(u32));
2040 len = le32_to_cpu(buf[0]);
2043 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2047 rc = next_entry(c->u.name, fp, len);
2052 rc = context_read_and_validate(&c->context[0], p, fp);
2055 rc = context_read_and_validate(&c->context[1], p, fp);
2060 rc = next_entry(buf, fp, sizeof(u32)*3);
2063 c->u.port.protocol = le32_to_cpu(buf[0]);
2064 c->u.port.low_port = le32_to_cpu(buf[1]);
2065 c->u.port.high_port = le32_to_cpu(buf[2]);
2066 rc = context_read_and_validate(&c->context[0], p, fp);
2071 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2074 c->u.node.addr = nodebuf[0]; /* network order */
2075 c->u.node.mask = nodebuf[1]; /* network order */
2076 rc = context_read_and_validate(&c->context[0], p, fp);
2081 rc = next_entry(buf, fp, sizeof(u32)*2);
2086 c->v.behavior = le32_to_cpu(buf[0]);
2087 if (c->v.behavior > SECURITY_FS_USE_NONE)
2091 len = le32_to_cpu(buf[1]);
2092 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2096 rc = next_entry(c->u.name, fp, len);
2100 rc = context_read_and_validate(&c->context[0], p, fp);
2107 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2110 for (k = 0; k < 4; k++)
2111 c->u.node6.addr[k] = nodebuf[k];
2112 for (k = 0; k < 4; k++)
2113 c->u.node6.mask[k] = nodebuf[k+4];
2114 rc = context_read_and_validate(&c->context[0], p, fp);
2128 * Read the configuration data from a policy database binary
2129 * representation file into a policy database structure.
2131 int policydb_read(struct policydb *p, void *fp)
2133 struct role_allow *ra, *lra;
2134 struct role_trans *tr, *ltr;
2137 u32 len, nprim, nel;
2140 struct policydb_compat_info *info;
2142 rc = policydb_init(p);
2146 /* Read the magic number and string length. */
2147 rc = next_entry(buf, fp, sizeof(u32) * 2);
2152 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2153 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2154 "not match expected magic number 0x%x\n",
2155 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2160 len = le32_to_cpu(buf[1]);
2161 if (len != strlen(POLICYDB_STRING)) {
2162 printk(KERN_ERR "SELinux: policydb string length %d does not "
2163 "match expected length %Zu\n",
2164 len, strlen(POLICYDB_STRING));
2169 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2170 if (!policydb_str) {
2171 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2172 "string of length %d\n", len);
2176 rc = next_entry(policydb_str, fp, len);
2178 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2179 kfree(policydb_str);
2184 policydb_str[len] = '\0';
2185 if (strcmp(policydb_str, POLICYDB_STRING)) {
2186 printk(KERN_ERR "SELinux: policydb string %s does not match "
2187 "my string %s\n", policydb_str, POLICYDB_STRING);
2188 kfree(policydb_str);
2191 /* Done with policydb_str. */
2192 kfree(policydb_str);
2193 policydb_str = NULL;
2195 /* Read the version and table sizes. */
2196 rc = next_entry(buf, fp, sizeof(u32)*4);
2201 p->policyvers = le32_to_cpu(buf[0]);
2202 if (p->policyvers < POLICYDB_VERSION_MIN ||
2203 p->policyvers > POLICYDB_VERSION_MAX) {
2204 printk(KERN_ERR "SELinux: policydb version %d does not match "
2205 "my version range %d-%d\n",
2206 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2210 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2214 if (p->policyvers < POLICYDB_VERSION_MLS) {
2215 printk(KERN_ERR "SELinux: security policydb version %d "
2216 "(MLS) not backwards compatible\n",
2221 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2222 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2224 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2225 rc = ebitmap_read(&p->policycaps, fp);
2230 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2231 rc = ebitmap_read(&p->permissive_map, fp);
2237 info = policydb_lookup_compat(p->policyvers);
2239 printk(KERN_ERR "SELinux: unable to find policy compat info "
2240 "for version %d\n", p->policyvers);
2245 if (le32_to_cpu(buf[2]) != info->sym_num ||
2246 le32_to_cpu(buf[3]) != info->ocon_num) {
2247 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2248 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2249 le32_to_cpu(buf[3]),
2250 info->sym_num, info->ocon_num);
2254 for (i = 0; i < info->sym_num; i++) {
2255 rc = next_entry(buf, fp, sizeof(u32)*2);
2258 nprim = le32_to_cpu(buf[0]);
2259 nel = le32_to_cpu(buf[1]);
2260 for (j = 0; j < nel; j++) {
2261 rc = read_f[i](p, p->symtab[i].table, fp);
2266 p->symtab[i].nprim = nprim;
2269 rc = avtab_read(&p->te_avtab, fp, p);
2273 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2274 rc = cond_read_list(p, fp);
2279 rc = next_entry(buf, fp, sizeof(u32));
2282 nel = le32_to_cpu(buf[0]);
2284 for (i = 0; i < nel; i++) {
2286 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2293 rc = next_entry(buf, fp, sizeof(u32)*3);
2298 tr->role = le32_to_cpu(buf[0]);
2299 tr->type = le32_to_cpu(buf[1]);
2300 tr->new_role = le32_to_cpu(buf[2]);
2301 if (!policydb_role_isvalid(p, tr->role) ||
2302 !policydb_type_isvalid(p, tr->type) ||
2303 !policydb_role_isvalid(p, tr->new_role))
2308 rc = next_entry(buf, fp, sizeof(u32));
2311 nel = le32_to_cpu(buf[0]);
2313 for (i = 0; i < nel; i++) {
2315 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2322 rc = next_entry(buf, fp, sizeof(u32)*2);
2327 ra->role = le32_to_cpu(buf[0]);
2328 ra->new_role = le32_to_cpu(buf[1]);
2329 if (!policydb_role_isvalid(p, ra->role) ||
2330 !policydb_role_isvalid(p, ra->new_role))
2335 rc = filename_trans_read(p, fp);
2339 rc = policydb_index(p);
2344 p->process_class = string_to_security_class(p, "process");
2345 if (!p->process_class)
2349 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2350 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2351 if (!p->process_trans_perms)
2354 rc = ocontext_read(p, info, fp);
2358 rc = genfs_read(p, fp);
2362 rc = range_read(p, fp);
2367 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2369 GFP_KERNEL | __GFP_ZERO);
2370 if (!p->type_attr_map_array)
2373 /* preallocate so we don't have to worry about the put ever failing */
2374 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2375 GFP_KERNEL | __GFP_ZERO);
2379 for (i = 0; i < p->p_types.nprim; i++) {
2380 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2384 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2385 rc = ebitmap_read(e, fp);
2389 /* add the type itself as the degenerate case */
2390 rc = ebitmap_set_bit(e, i, 1);
2395 rc = policydb_bounds_sanity_check(p);
2403 policydb_destroy(p);
2408 * Write a MLS level structure to a policydb binary
2409 * representation file.
2411 static int mls_write_level(struct mls_level *l, void *fp)
2416 buf[0] = cpu_to_le32(l->sens);
2417 rc = put_entry(buf, sizeof(u32), 1, fp);
2421 rc = ebitmap_write(&l->cat, fp);
2429 * Write a MLS range structure to a policydb binary
2430 * representation file.
2432 static int mls_write_range_helper(struct mls_range *r, void *fp)
2438 eq = mls_level_eq(&r->level[1], &r->level[0]);
2444 buf[0] = cpu_to_le32(items-1);
2445 buf[1] = cpu_to_le32(r->level[0].sens);
2447 buf[2] = cpu_to_le32(r->level[1].sens);
2449 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2451 rc = put_entry(buf, sizeof(u32), items, fp);
2455 rc = ebitmap_write(&r->level[0].cat, fp);
2459 rc = ebitmap_write(&r->level[1].cat, fp);
2467 static int sens_write(void *vkey, void *datum, void *ptr)
2470 struct level_datum *levdatum = datum;
2471 struct policy_data *pd = ptr;
2478 buf[0] = cpu_to_le32(len);
2479 buf[1] = cpu_to_le32(levdatum->isalias);
2480 rc = put_entry(buf, sizeof(u32), 2, fp);
2484 rc = put_entry(key, 1, len, fp);
2488 rc = mls_write_level(levdatum->level, fp);
2495 static int cat_write(void *vkey, void *datum, void *ptr)
2498 struct cat_datum *catdatum = datum;
2499 struct policy_data *pd = ptr;
2506 buf[0] = cpu_to_le32(len);
2507 buf[1] = cpu_to_le32(catdatum->value);
2508 buf[2] = cpu_to_le32(catdatum->isalias);
2509 rc = put_entry(buf, sizeof(u32), 3, fp);
2513 rc = put_entry(key, 1, len, fp);
2520 static int role_trans_write(struct role_trans *r, void *fp)
2522 struct role_trans *tr;
2528 for (tr = r; tr; tr = tr->next)
2530 buf[0] = cpu_to_le32(nel);
2531 rc = put_entry(buf, sizeof(u32), 1, fp);
2534 for (tr = r; tr; tr = tr->next) {
2535 buf[0] = cpu_to_le32(tr->role);
2536 buf[1] = cpu_to_le32(tr->type);
2537 buf[2] = cpu_to_le32(tr->new_role);
2538 rc = put_entry(buf, sizeof(u32), 3, fp);
2546 static int role_allow_write(struct role_allow *r, void *fp)
2548 struct role_allow *ra;
2554 for (ra = r; ra; ra = ra->next)
2556 buf[0] = cpu_to_le32(nel);
2557 rc = put_entry(buf, sizeof(u32), 1, fp);
2560 for (ra = r; ra; ra = ra->next) {
2561 buf[0] = cpu_to_le32(ra->role);
2562 buf[1] = cpu_to_le32(ra->new_role);
2563 rc = put_entry(buf, sizeof(u32), 2, fp);
2571 * Write a security context structure
2572 * to a policydb binary representation file.
2574 static int context_write(struct policydb *p, struct context *c,
2580 buf[0] = cpu_to_le32(c->user);
2581 buf[1] = cpu_to_le32(c->role);
2582 buf[2] = cpu_to_le32(c->type);
2584 rc = put_entry(buf, sizeof(u32), 3, fp);
2588 rc = mls_write_range_helper(&c->range, fp);
2596 * The following *_write functions are used to
2597 * write the symbol data to a policy database
2598 * binary representation file.
2601 static int perm_write(void *vkey, void *datum, void *fp)
2604 struct perm_datum *perdatum = datum;
2610 buf[0] = cpu_to_le32(len);
2611 buf[1] = cpu_to_le32(perdatum->value);
2612 rc = put_entry(buf, sizeof(u32), 2, fp);
2616 rc = put_entry(key, 1, len, fp);
2623 static int common_write(void *vkey, void *datum, void *ptr)
2626 struct common_datum *comdatum = datum;
2627 struct policy_data *pd = ptr;
2634 buf[0] = cpu_to_le32(len);
2635 buf[1] = cpu_to_le32(comdatum->value);
2636 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2637 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2638 rc = put_entry(buf, sizeof(u32), 4, fp);
2642 rc = put_entry(key, 1, len, fp);
2646 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2653 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2656 struct constraint_node *c;
2657 struct constraint_expr *e;
2662 for (c = node; c; c = c->next) {
2664 for (e = c->expr; e; e = e->next)
2666 buf[0] = cpu_to_le32(c->permissions);
2667 buf[1] = cpu_to_le32(nel);
2668 rc = put_entry(buf, sizeof(u32), 2, fp);
2671 for (e = c->expr; e; e = e->next) {
2672 buf[0] = cpu_to_le32(e->expr_type);
2673 buf[1] = cpu_to_le32(e->attr);
2674 buf[2] = cpu_to_le32(e->op);
2675 rc = put_entry(buf, sizeof(u32), 3, fp);
2679 switch (e->expr_type) {
2681 rc = ebitmap_write(&e->names, fp);
2694 static int class_write(void *vkey, void *datum, void *ptr)
2697 struct class_datum *cladatum = datum;
2698 struct policy_data *pd = ptr;
2700 struct policydb *p = pd->p;
2701 struct constraint_node *c;
2708 if (cladatum->comkey)
2709 len2 = strlen(cladatum->comkey);
2714 for (c = cladatum->constraints; c; c = c->next)
2717 buf[0] = cpu_to_le32(len);
2718 buf[1] = cpu_to_le32(len2);
2719 buf[2] = cpu_to_le32(cladatum->value);
2720 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2721 if (cladatum->permissions.table)
2722 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2725 buf[5] = cpu_to_le32(ncons);
2726 rc = put_entry(buf, sizeof(u32), 6, fp);
2730 rc = put_entry(key, 1, len, fp);
2734 if (cladatum->comkey) {
2735 rc = put_entry(cladatum->comkey, 1, len2, fp);
2740 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2744 rc = write_cons_helper(p, cladatum->constraints, fp);
2748 /* write out the validatetrans rule */
2750 for (c = cladatum->validatetrans; c; c = c->next)
2753 buf[0] = cpu_to_le32(ncons);
2754 rc = put_entry(buf, sizeof(u32), 1, fp);
2758 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2765 static int role_write(void *vkey, void *datum, void *ptr)
2768 struct role_datum *role = datum;
2769 struct policy_data *pd = ptr;
2771 struct policydb *p = pd->p;
2778 buf[items++] = cpu_to_le32(len);
2779 buf[items++] = cpu_to_le32(role->value);
2780 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2781 buf[items++] = cpu_to_le32(role->bounds);
2783 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2785 rc = put_entry(buf, sizeof(u32), items, fp);
2789 rc = put_entry(key, 1, len, fp);
2793 rc = ebitmap_write(&role->dominates, fp);
2797 rc = ebitmap_write(&role->types, fp);
2804 static int type_write(void *vkey, void *datum, void *ptr)
2807 struct type_datum *typdatum = datum;
2808 struct policy_data *pd = ptr;
2809 struct policydb *p = pd->p;
2817 buf[items++] = cpu_to_le32(len);
2818 buf[items++] = cpu_to_le32(typdatum->value);
2819 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2822 if (typdatum->primary)
2823 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2825 if (typdatum->attribute)
2826 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2828 buf[items++] = cpu_to_le32(properties);
2829 buf[items++] = cpu_to_le32(typdatum->bounds);
2831 buf[items++] = cpu_to_le32(typdatum->primary);
2833 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2834 rc = put_entry(buf, sizeof(u32), items, fp);
2838 rc = put_entry(key, 1, len, fp);
2845 static int user_write(void *vkey, void *datum, void *ptr)
2848 struct user_datum *usrdatum = datum;
2849 struct policy_data *pd = ptr;
2850 struct policydb *p = pd->p;
2858 buf[items++] = cpu_to_le32(len);
2859 buf[items++] = cpu_to_le32(usrdatum->value);
2860 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2861 buf[items++] = cpu_to_le32(usrdatum->bounds);
2862 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2863 rc = put_entry(buf, sizeof(u32), items, fp);
2867 rc = put_entry(key, 1, len, fp);
2871 rc = ebitmap_write(&usrdatum->roles, fp);
2875 rc = mls_write_range_helper(&usrdatum->range, fp);
2879 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2886 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2899 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2902 unsigned int i, j, rc;
2907 for (i = 0; i < info->ocon_num; i++) {
2909 for (c = p->ocontexts[i]; c; c = c->next)
2911 buf[0] = cpu_to_le32(nel);
2912 rc = put_entry(buf, sizeof(u32), 1, fp);
2915 for (c = p->ocontexts[i]; c; c = c->next) {
2918 buf[0] = cpu_to_le32(c->sid[0]);
2919 rc = put_entry(buf, sizeof(u32), 1, fp);
2922 rc = context_write(p, &c->context[0], fp);
2928 len = strlen(c->u.name);
2929 buf[0] = cpu_to_le32(len);
2930 rc = put_entry(buf, sizeof(u32), 1, fp);
2933 rc = put_entry(c->u.name, 1, len, fp);
2936 rc = context_write(p, &c->context[0], fp);
2939 rc = context_write(p, &c->context[1], fp);
2944 buf[0] = cpu_to_le32(c->u.port.protocol);
2945 buf[1] = cpu_to_le32(c->u.port.low_port);
2946 buf[2] = cpu_to_le32(c->u.port.high_port);
2947 rc = put_entry(buf, sizeof(u32), 3, fp);
2950 rc = context_write(p, &c->context[0], fp);
2955 nodebuf[0] = c->u.node.addr; /* network order */
2956 nodebuf[1] = c->u.node.mask; /* network order */
2957 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2960 rc = context_write(p, &c->context[0], fp);
2965 buf[0] = cpu_to_le32(c->v.behavior);
2966 len = strlen(c->u.name);
2967 buf[1] = cpu_to_le32(len);
2968 rc = put_entry(buf, sizeof(u32), 2, fp);
2971 rc = put_entry(c->u.name, 1, len, fp);
2974 rc = context_write(p, &c->context[0], fp);
2979 for (j = 0; j < 4; j++)
2980 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2981 for (j = 0; j < 4; j++)
2982 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2983 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2986 rc = context_write(p, &c->context[0], fp);
2996 static int genfs_write(struct policydb *p, void *fp)
2998 struct genfs *genfs;
3005 for (genfs = p->genfs; genfs; genfs = genfs->next)
3007 buf[0] = cpu_to_le32(len);
3008 rc = put_entry(buf, sizeof(u32), 1, fp);
3011 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3012 len = strlen(genfs->fstype);
3013 buf[0] = cpu_to_le32(len);
3014 rc = put_entry(buf, sizeof(u32), 1, fp);
3017 rc = put_entry(genfs->fstype, 1, len, fp);
3021 for (c = genfs->head; c; c = c->next)
3023 buf[0] = cpu_to_le32(len);
3024 rc = put_entry(buf, sizeof(u32), 1, fp);
3027 for (c = genfs->head; c; c = c->next) {
3028 len = strlen(c->u.name);
3029 buf[0] = cpu_to_le32(len);
3030 rc = put_entry(buf, sizeof(u32), 1, fp);
3033 rc = put_entry(c->u.name, 1, len, fp);
3036 buf[0] = cpu_to_le32(c->v.sclass);
3037 rc = put_entry(buf, sizeof(u32), 1, fp);
3040 rc = context_write(p, &c->context[0], fp);
3048 static int range_count(void *key, void *data, void *ptr)
3056 static int range_write_helper(void *key, void *data, void *ptr)
3059 struct range_trans *rt = key;
3060 struct mls_range *r = data;
3061 struct policy_data *pd = ptr;
3063 struct policydb *p = pd->p;
3066 buf[0] = cpu_to_le32(rt->source_type);
3067 buf[1] = cpu_to_le32(rt->target_type);
3068 rc = put_entry(buf, sizeof(u32), 2, fp);
3071 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3072 buf[0] = cpu_to_le32(rt->target_class);
3073 rc = put_entry(buf, sizeof(u32), 1, fp);
3077 rc = mls_write_range_helper(r, fp);
3084 static int range_write(struct policydb *p, void *fp)
3089 struct policy_data pd;
3094 /* count the number of entries in the hashtab */
3096 rc = hashtab_map(p->range_tr, range_count, &nel);
3100 buf[0] = cpu_to_le32(nel);
3101 rc = put_entry(buf, sizeof(u32), 1, fp);
3105 /* actually write all of the entries */
3106 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3113 static int filename_trans_write(struct policydb *p, void *fp)
3115 struct filename_trans *ft;
3120 for (ft = p->filename_trans; ft; ft = ft->next)
3123 buf[0] = cpu_to_le32(nel);
3124 rc = put_entry(buf, sizeof(u32), 1, fp);
3128 for (ft = p->filename_trans; ft; ft = ft->next) {
3129 len = strlen(ft->name);
3130 buf[0] = cpu_to_le32(len);
3131 rc = put_entry(buf, sizeof(u32), 1, fp);
3135 rc = put_entry(ft->name, sizeof(char), len, fp);
3141 buf[2] = ft->tclass;
3144 rc = put_entry(buf, sizeof(u32), 4, fp);
3151 * Write the configuration data in a policy database
3152 * structure to a policy database binary representation
3155 int policydb_write(struct policydb *p, void *fp)
3157 unsigned int i, num_syms;
3162 struct policydb_compat_info *info;
3165 * refuse to write policy older than compressed avtab
3166 * to simplify the writer. There are other tests dropped
3167 * since we assume this throughout the writer code. Be
3168 * careful if you ever try to remove this restriction
3170 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3171 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3172 " Because it is less than version %d\n", p->policyvers,
3173 POLICYDB_VERSION_AVTAB);
3179 config |= POLICYDB_CONFIG_MLS;
3181 if (p->reject_unknown)
3182 config |= REJECT_UNKNOWN;
3183 if (p->allow_unknown)
3184 config |= ALLOW_UNKNOWN;
3186 /* Write the magic number and string identifiers. */
3187 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3188 len = strlen(POLICYDB_STRING);
3189 buf[1] = cpu_to_le32(len);
3190 rc = put_entry(buf, sizeof(u32), 2, fp);
3193 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3197 /* Write the version, config, and table sizes. */
3198 info = policydb_lookup_compat(p->policyvers);
3200 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3201 "version %d", p->policyvers);
3205 buf[0] = cpu_to_le32(p->policyvers);
3206 buf[1] = cpu_to_le32(config);
3207 buf[2] = cpu_to_le32(info->sym_num);
3208 buf[3] = cpu_to_le32(info->ocon_num);
3210 rc = put_entry(buf, sizeof(u32), 4, fp);
3214 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3215 rc = ebitmap_write(&p->policycaps, fp);
3220 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3221 rc = ebitmap_write(&p->permissive_map, fp);
3226 num_syms = info->sym_num;
3227 for (i = 0; i < num_syms; i++) {
3228 struct policy_data pd;
3233 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3234 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3236 rc = put_entry(buf, sizeof(u32), 2, fp);
3239 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3244 rc = avtab_write(p, &p->te_avtab, fp);
3248 rc = cond_write_list(p, p->cond_list, fp);
3252 rc = role_trans_write(p->role_tr, fp);
3256 rc = role_allow_write(p->role_allow, fp);
3260 rc = filename_trans_write(p, fp);
3264 rc = ocontext_write(p, info, fp);
3268 rc = genfs_write(p, fp);
3272 rc = range_write(p, fp);
3276 for (i = 0; i < p->p_types.nprim; i++) {
3277 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3280 rc = ebitmap_write(e, fp);