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,
132 .version = POLICYDB_VERSION_ROLETRANS,
134 .ocon_num = OCON_NUM,
138 static struct policydb_compat_info *policydb_lookup_compat(int version)
141 struct policydb_compat_info *info = NULL;
143 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
144 if (policydb_compat[i].version == version) {
145 info = &policydb_compat[i];
153 * Initialize the role table.
155 static int roles_init(struct policydb *p)
159 struct role_datum *role;
162 role = kzalloc(sizeof(*role), GFP_KERNEL);
167 role->value = ++p->p_roles.nprim;
168 if (role->value != OBJECT_R_VAL)
172 key = kstrdup(OBJECT_R, GFP_KERNEL);
176 rc = hashtab_insert(p->p_roles.table, key, role);
187 static u32 rangetr_hash(struct hashtab *h, const void *k)
189 const struct range_trans *key = k;
190 return (key->source_type + (key->target_type << 3) +
191 (key->target_class << 5)) & (h->size - 1);
194 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
196 const struct range_trans *key1 = k1, *key2 = k2;
199 v = key1->source_type - key2->source_type;
203 v = key1->target_type - key2->target_type;
207 v = key1->target_class - key2->target_class;
213 * Initialize a policy database structure.
215 static int policydb_init(struct policydb *p)
219 memset(p, 0, sizeof(*p));
221 for (i = 0; i < SYM_NUM; i++) {
222 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
227 rc = avtab_init(&p->te_avtab);
235 rc = cond_policydb_init(p);
239 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
243 ebitmap_init(&p->filename_trans_ttypes);
244 ebitmap_init(&p->policycaps);
245 ebitmap_init(&p->permissive_map);
249 for (i = 0; i < SYM_NUM; i++)
250 hashtab_destroy(p->symtab[i].table);
255 * The following *_index functions are used to
256 * define the val_to_name and val_to_struct arrays
257 * in a policy database structure. The val_to_name
258 * arrays are used when converting security context
259 * structures into string representations. The
260 * val_to_struct arrays are used when the attributes
261 * of a class, role, or user are needed.
264 static int common_index(void *key, void *datum, void *datap)
267 struct common_datum *comdatum;
268 struct flex_array *fa;
272 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
275 fa = p->sym_val_to_name[SYM_COMMONS];
276 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
277 GFP_KERNEL | __GFP_ZERO))
282 static int class_index(void *key, void *datum, void *datap)
285 struct class_datum *cladatum;
286 struct flex_array *fa;
290 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
292 fa = p->sym_val_to_name[SYM_CLASSES];
293 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
294 GFP_KERNEL | __GFP_ZERO))
296 p->class_val_to_struct[cladatum->value - 1] = cladatum;
300 static int role_index(void *key, void *datum, void *datap)
303 struct role_datum *role;
304 struct flex_array *fa;
309 || role->value > p->p_roles.nprim
310 || role->bounds > p->p_roles.nprim)
313 fa = p->sym_val_to_name[SYM_ROLES];
314 if (flex_array_put_ptr(fa, role->value - 1, key,
315 GFP_KERNEL | __GFP_ZERO))
317 p->role_val_to_struct[role->value - 1] = role;
321 static int type_index(void *key, void *datum, void *datap)
324 struct type_datum *typdatum;
325 struct flex_array *fa;
330 if (typdatum->primary) {
332 || typdatum->value > p->p_types.nprim
333 || typdatum->bounds > p->p_types.nprim)
335 fa = p->sym_val_to_name[SYM_TYPES];
336 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
337 GFP_KERNEL | __GFP_ZERO))
340 fa = p->type_val_to_struct_array;
341 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
342 GFP_KERNEL | __GFP_ZERO))
349 static int user_index(void *key, void *datum, void *datap)
352 struct user_datum *usrdatum;
353 struct flex_array *fa;
358 || usrdatum->value > p->p_users.nprim
359 || usrdatum->bounds > p->p_users.nprim)
362 fa = p->sym_val_to_name[SYM_USERS];
363 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
364 GFP_KERNEL | __GFP_ZERO))
366 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
370 static int sens_index(void *key, void *datum, void *datap)
373 struct level_datum *levdatum;
374 struct flex_array *fa;
379 if (!levdatum->isalias) {
380 if (!levdatum->level->sens ||
381 levdatum->level->sens > p->p_levels.nprim)
383 fa = p->sym_val_to_name[SYM_LEVELS];
384 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
385 GFP_KERNEL | __GFP_ZERO))
392 static int cat_index(void *key, void *datum, void *datap)
395 struct cat_datum *catdatum;
396 struct flex_array *fa;
401 if (!catdatum->isalias) {
402 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
404 fa = p->sym_val_to_name[SYM_CATS];
405 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
406 GFP_KERNEL | __GFP_ZERO))
413 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
426 static void hash_eval(struct hashtab *h, const char *hash_name)
428 struct hashtab_info info;
430 hashtab_stat(h, &info);
431 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
432 "longest chain length %d\n", hash_name, h->nel,
433 info.slots_used, h->size, info.max_chain_len);
436 static void symtab_hash_eval(struct symtab *s)
440 for (i = 0; i < SYM_NUM; i++)
441 hash_eval(s[i].table, symtab_name[i]);
445 static inline void hash_eval(struct hashtab *h, char *hash_name)
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->filename_trans_ttypes);
800 ebitmap_destroy(&p->policycaps);
801 ebitmap_destroy(&p->permissive_map);
807 * Load the initial SIDs specified in a policy database
808 * structure into a SID table.
810 int policydb_load_isids(struct policydb *p, struct sidtab *s)
812 struct ocontext *head, *c;
817 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
821 head = p->ocontexts[OCON_ISID];
822 for (c = head; c; c = c->next) {
824 if (!c->context[0].user) {
825 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
830 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
832 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
842 int policydb_class_isvalid(struct policydb *p, unsigned int class)
844 if (!class || class > p->p_classes.nprim)
849 int policydb_role_isvalid(struct policydb *p, unsigned int role)
851 if (!role || role > p->p_roles.nprim)
856 int policydb_type_isvalid(struct policydb *p, unsigned int type)
858 if (!type || type > p->p_types.nprim)
864 * Return 1 if the fields in the security context
865 * structure `c' are valid. Return 0 otherwise.
867 int policydb_context_isvalid(struct policydb *p, struct context *c)
869 struct role_datum *role;
870 struct user_datum *usrdatum;
872 if (!c->role || c->role > p->p_roles.nprim)
875 if (!c->user || c->user > p->p_users.nprim)
878 if (!c->type || c->type > p->p_types.nprim)
881 if (c->role != OBJECT_R_VAL) {
883 * Role must be authorized for the type.
885 role = p->role_val_to_struct[c->role - 1];
886 if (!ebitmap_get_bit(&role->types, c->type - 1))
887 /* role may not be associated with type */
891 * User must be authorized for the role.
893 usrdatum = p->user_val_to_struct[c->user - 1];
897 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
898 /* user may not be associated with role */
902 if (!mls_context_isvalid(p, c))
909 * Read a MLS range structure from a policydb binary
910 * representation file.
912 static int mls_read_range_helper(struct mls_range *r, void *fp)
918 rc = next_entry(buf, fp, sizeof(u32));
923 items = le32_to_cpu(buf[0]);
924 if (items > ARRAY_SIZE(buf)) {
925 printk(KERN_ERR "SELinux: mls: range overflow\n");
929 rc = next_entry(buf, fp, sizeof(u32) * items);
931 printk(KERN_ERR "SELinux: mls: truncated range\n");
935 r->level[0].sens = le32_to_cpu(buf[0]);
937 r->level[1].sens = le32_to_cpu(buf[1]);
939 r->level[1].sens = r->level[0].sens;
941 rc = ebitmap_read(&r->level[0].cat, fp);
943 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
947 rc = ebitmap_read(&r->level[1].cat, fp);
949 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
953 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
955 printk(KERN_ERR "SELinux: mls: out of memory\n");
962 ebitmap_destroy(&r->level[0].cat);
968 * Read and validate a security context structure
969 * from a policydb binary representation file.
971 static int context_read_and_validate(struct context *c,
978 rc = next_entry(buf, fp, sizeof buf);
980 printk(KERN_ERR "SELinux: context truncated\n");
983 c->user = le32_to_cpu(buf[0]);
984 c->role = le32_to_cpu(buf[1]);
985 c->type = le32_to_cpu(buf[2]);
986 if (p->policyvers >= POLICYDB_VERSION_MLS) {
987 rc = mls_read_range_helper(&c->range, fp);
989 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
995 if (!policydb_context_isvalid(p, c)) {
996 printk(KERN_ERR "SELinux: invalid security context\n");
1006 * The following *_read functions are used to
1007 * read the symbol data from a policy database
1008 * binary representation file.
1011 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1014 struct perm_datum *perdatum;
1020 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1024 rc = next_entry(buf, fp, sizeof buf);
1028 len = le32_to_cpu(buf[0]);
1029 perdatum->value = le32_to_cpu(buf[1]);
1032 key = kmalloc(len + 1, GFP_KERNEL);
1036 rc = next_entry(key, fp, len);
1041 rc = hashtab_insert(h, key, perdatum);
1047 perm_destroy(key, perdatum, NULL);
1051 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1054 struct common_datum *comdatum;
1060 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1064 rc = next_entry(buf, fp, sizeof buf);
1068 len = le32_to_cpu(buf[0]);
1069 comdatum->value = le32_to_cpu(buf[1]);
1071 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1074 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1075 nel = le32_to_cpu(buf[3]);
1078 key = kmalloc(len + 1, GFP_KERNEL);
1082 rc = next_entry(key, fp, len);
1087 for (i = 0; i < nel; i++) {
1088 rc = perm_read(p, comdatum->permissions.table, fp);
1093 rc = hashtab_insert(h, key, comdatum);
1098 common_destroy(key, comdatum, NULL);
1102 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1103 int allowxtarget, void *fp)
1105 struct constraint_node *c, *lc;
1106 struct constraint_expr *e, *le;
1109 int rc, i, j, depth;
1112 for (i = 0; i < ncons; i++) {
1113 c = kzalloc(sizeof(*c), GFP_KERNEL);
1122 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1125 c->permissions = le32_to_cpu(buf[0]);
1126 nexpr = le32_to_cpu(buf[1]);
1129 for (j = 0; j < nexpr; j++) {
1130 e = kzalloc(sizeof(*e), GFP_KERNEL);
1139 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1142 e->expr_type = le32_to_cpu(buf[0]);
1143 e->attr = le32_to_cpu(buf[1]);
1144 e->op = le32_to_cpu(buf[2]);
1146 switch (e->expr_type) {
1158 if (depth == (CEXPR_MAXDEPTH - 1))
1163 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1165 if (depth == (CEXPR_MAXDEPTH - 1))
1168 rc = ebitmap_read(&e->names, fp);
1185 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1188 struct class_datum *cladatum;
1190 u32 len, len2, ncons, nel;
1194 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1198 rc = next_entry(buf, fp, sizeof(u32)*6);
1202 len = le32_to_cpu(buf[0]);
1203 len2 = le32_to_cpu(buf[1]);
1204 cladatum->value = le32_to_cpu(buf[2]);
1206 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1209 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1210 nel = le32_to_cpu(buf[4]);
1212 ncons = le32_to_cpu(buf[5]);
1215 key = kmalloc(len + 1, GFP_KERNEL);
1219 rc = next_entry(key, fp, len);
1226 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1227 if (!cladatum->comkey)
1229 rc = next_entry(cladatum->comkey, fp, len2);
1232 cladatum->comkey[len2] = '\0';
1235 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1236 if (!cladatum->comdatum) {
1237 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1241 for (i = 0; i < nel; i++) {
1242 rc = perm_read(p, cladatum->permissions.table, fp);
1247 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1251 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1252 /* grab the validatetrans rules */
1253 rc = next_entry(buf, fp, sizeof(u32));
1256 ncons = le32_to_cpu(buf[0]);
1257 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1262 rc = hashtab_insert(h, key, cladatum);
1268 cls_destroy(key, cladatum, NULL);
1272 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1275 struct role_datum *role;
1276 int rc, to_read = 2;
1281 role = kzalloc(sizeof(*role), GFP_KERNEL);
1285 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1288 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1292 len = le32_to_cpu(buf[0]);
1293 role->value = le32_to_cpu(buf[1]);
1294 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1295 role->bounds = le32_to_cpu(buf[2]);
1298 key = kmalloc(len + 1, GFP_KERNEL);
1302 rc = next_entry(key, fp, len);
1307 rc = ebitmap_read(&role->dominates, fp);
1311 rc = ebitmap_read(&role->types, fp);
1315 if (strcmp(key, OBJECT_R) == 0) {
1317 if (role->value != OBJECT_R_VAL) {
1318 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1319 OBJECT_R, role->value);
1326 rc = hashtab_insert(h, key, role);
1331 role_destroy(key, role, NULL);
1335 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1338 struct type_datum *typdatum;
1339 int rc, to_read = 3;
1344 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1348 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1351 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1355 len = le32_to_cpu(buf[0]);
1356 typdatum->value = le32_to_cpu(buf[1]);
1357 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1358 u32 prop = le32_to_cpu(buf[2]);
1360 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1361 typdatum->primary = 1;
1362 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1363 typdatum->attribute = 1;
1365 typdatum->bounds = le32_to_cpu(buf[3]);
1367 typdatum->primary = le32_to_cpu(buf[2]);
1371 key = kmalloc(len + 1, GFP_KERNEL);
1374 rc = next_entry(key, fp, len);
1379 rc = hashtab_insert(h, key, typdatum);
1384 type_destroy(key, typdatum, NULL);
1390 * Read a MLS level structure from a policydb binary
1391 * representation file.
1393 static int mls_read_level(struct mls_level *lp, void *fp)
1398 memset(lp, 0, sizeof(*lp));
1400 rc = next_entry(buf, fp, sizeof buf);
1402 printk(KERN_ERR "SELinux: mls: truncated level\n");
1405 lp->sens = le32_to_cpu(buf[0]);
1407 rc = ebitmap_read(&lp->cat, fp);
1409 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1415 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1418 struct user_datum *usrdatum;
1419 int rc, to_read = 2;
1424 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1428 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1431 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1435 len = le32_to_cpu(buf[0]);
1436 usrdatum->value = le32_to_cpu(buf[1]);
1437 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1438 usrdatum->bounds = le32_to_cpu(buf[2]);
1441 key = kmalloc(len + 1, GFP_KERNEL);
1444 rc = next_entry(key, fp, len);
1449 rc = ebitmap_read(&usrdatum->roles, fp);
1453 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1454 rc = mls_read_range_helper(&usrdatum->range, fp);
1457 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1462 rc = hashtab_insert(h, key, usrdatum);
1467 user_destroy(key, usrdatum, NULL);
1471 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1474 struct level_datum *levdatum;
1480 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1484 rc = next_entry(buf, fp, sizeof buf);
1488 len = le32_to_cpu(buf[0]);
1489 levdatum->isalias = le32_to_cpu(buf[1]);
1492 key = kmalloc(len + 1, GFP_ATOMIC);
1495 rc = next_entry(key, fp, len);
1501 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1502 if (!levdatum->level)
1505 rc = mls_read_level(levdatum->level, fp);
1509 rc = hashtab_insert(h, key, levdatum);
1514 sens_destroy(key, levdatum, NULL);
1518 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1521 struct cat_datum *catdatum;
1527 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1531 rc = next_entry(buf, fp, sizeof buf);
1535 len = le32_to_cpu(buf[0]);
1536 catdatum->value = le32_to_cpu(buf[1]);
1537 catdatum->isalias = le32_to_cpu(buf[2]);
1540 key = kmalloc(len + 1, GFP_ATOMIC);
1543 rc = next_entry(key, fp, len);
1548 rc = hashtab_insert(h, key, catdatum);
1553 cat_destroy(key, catdatum, NULL);
1557 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1569 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1571 struct user_datum *upper, *user;
1572 struct policydb *p = datap;
1575 upper = user = datum;
1576 while (upper->bounds) {
1577 struct ebitmap_node *node;
1580 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1581 printk(KERN_ERR "SELinux: user %s: "
1582 "too deep or looped boundary",
1587 upper = p->user_val_to_struct[upper->bounds - 1];
1588 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1589 if (ebitmap_get_bit(&upper->roles, bit))
1593 "SELinux: boundary violated policy: "
1594 "user=%s role=%s bounds=%s\n",
1595 sym_name(p, SYM_USERS, user->value - 1),
1596 sym_name(p, SYM_ROLES, bit),
1597 sym_name(p, SYM_USERS, upper->value - 1));
1606 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1608 struct role_datum *upper, *role;
1609 struct policydb *p = datap;
1612 upper = role = datum;
1613 while (upper->bounds) {
1614 struct ebitmap_node *node;
1617 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1618 printk(KERN_ERR "SELinux: role %s: "
1619 "too deep or looped bounds\n",
1624 upper = p->role_val_to_struct[upper->bounds - 1];
1625 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1626 if (ebitmap_get_bit(&upper->types, bit))
1630 "SELinux: boundary violated policy: "
1631 "role=%s type=%s bounds=%s\n",
1632 sym_name(p, SYM_ROLES, role->value - 1),
1633 sym_name(p, SYM_TYPES, bit),
1634 sym_name(p, SYM_ROLES, upper->value - 1));
1643 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1645 struct type_datum *upper;
1646 struct policydb *p = datap;
1650 while (upper->bounds) {
1651 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1652 printk(KERN_ERR "SELinux: type %s: "
1653 "too deep or looped boundary\n",
1658 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1662 if (upper->attribute) {
1663 printk(KERN_ERR "SELinux: type %s: "
1664 "bounded by attribute %s",
1666 sym_name(p, SYM_TYPES, upper->value - 1));
1674 static int policydb_bounds_sanity_check(struct policydb *p)
1678 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1681 rc = hashtab_map(p->p_users.table,
1682 user_bounds_sanity_check, p);
1686 rc = hashtab_map(p->p_roles.table,
1687 role_bounds_sanity_check, p);
1691 rc = hashtab_map(p->p_types.table,
1692 type_bounds_sanity_check, p);
1699 extern int ss_initialized;
1701 u16 string_to_security_class(struct policydb *p, const char *name)
1703 struct class_datum *cladatum;
1705 cladatum = hashtab_search(p->p_classes.table, name);
1709 return cladatum->value;
1712 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1714 struct class_datum *cladatum;
1715 struct perm_datum *perdatum = NULL;
1716 struct common_datum *comdatum;
1718 if (!tclass || tclass > p->p_classes.nprim)
1721 cladatum = p->class_val_to_struct[tclass-1];
1722 comdatum = cladatum->comdatum;
1724 perdatum = hashtab_search(comdatum->permissions.table,
1727 perdatum = hashtab_search(cladatum->permissions.table,
1732 return 1U << (perdatum->value-1);
1735 static int range_read(struct policydb *p, void *fp)
1737 struct range_trans *rt = NULL;
1738 struct mls_range *r = NULL;
1743 if (p->policyvers < POLICYDB_VERSION_MLS)
1746 rc = next_entry(buf, fp, sizeof(u32));
1750 nel = le32_to_cpu(buf[0]);
1751 for (i = 0; i < nel; i++) {
1753 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1757 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1761 rt->source_type = le32_to_cpu(buf[0]);
1762 rt->target_type = le32_to_cpu(buf[1]);
1763 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1764 rc = next_entry(buf, fp, sizeof(u32));
1767 rt->target_class = le32_to_cpu(buf[0]);
1769 rt->target_class = p->process_class;
1772 if (!policydb_type_isvalid(p, rt->source_type) ||
1773 !policydb_type_isvalid(p, rt->target_type) ||
1774 !policydb_class_isvalid(p, rt->target_class))
1778 r = kzalloc(sizeof(*r), GFP_KERNEL);
1782 rc = mls_read_range_helper(r, fp);
1787 if (!mls_range_isvalid(p, r)) {
1788 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1792 rc = hashtab_insert(p->range_tr, rt, r);
1799 hash_eval(p->range_tr, "rangetr");
1807 static int filename_trans_read(struct policydb *p, void *fp)
1809 struct filename_trans *ft, *last;
1815 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1818 rc = next_entry(buf, fp, sizeof(u32));
1821 nel = le32_to_cpu(buf[0]);
1823 last = p->filename_trans;
1824 while (last && last->next)
1827 for (i = 0; i < nel; i++) {
1829 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1833 /* add it to the tail of the list */
1835 p->filename_trans = ft;
1840 /* length of the path component string */
1841 rc = next_entry(buf, fp, sizeof(u32));
1844 len = le32_to_cpu(buf[0]);
1847 name = kmalloc(len + 1, GFP_KERNEL);
1853 /* path component string */
1854 rc = next_entry(name, fp, len);
1859 rc = next_entry(buf, fp, sizeof(u32) * 4);
1863 ft->stype = le32_to_cpu(buf[0]);
1864 ft->ttype = le32_to_cpu(buf[1]);
1865 ft->tclass = le32_to_cpu(buf[2]);
1866 ft->otype = le32_to_cpu(buf[3]);
1868 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1877 static int genfs_read(struct policydb *p, void *fp)
1880 u32 nel, nel2, len, len2;
1882 struct ocontext *l, *c;
1883 struct ocontext *newc = NULL;
1884 struct genfs *genfs_p, *genfs;
1885 struct genfs *newgenfs = NULL;
1887 rc = next_entry(buf, fp, sizeof(u32));
1890 nel = le32_to_cpu(buf[0]);
1892 for (i = 0; i < nel; i++) {
1893 rc = next_entry(buf, fp, sizeof(u32));
1896 len = le32_to_cpu(buf[0]);
1899 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1904 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1905 if (!newgenfs->fstype)
1908 rc = next_entry(newgenfs->fstype, fp, len);
1912 newgenfs->fstype[len] = 0;
1914 for (genfs_p = NULL, genfs = p->genfs; genfs;
1915 genfs_p = genfs, genfs = genfs->next) {
1917 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1918 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1922 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1925 newgenfs->next = genfs;
1927 genfs_p->next = newgenfs;
1929 p->genfs = newgenfs;
1933 rc = next_entry(buf, fp, sizeof(u32));
1937 nel2 = le32_to_cpu(buf[0]);
1938 for (j = 0; j < nel2; j++) {
1939 rc = next_entry(buf, fp, sizeof(u32));
1942 len = le32_to_cpu(buf[0]);
1945 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1950 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1954 rc = next_entry(newc->u.name, fp, len);
1957 newc->u.name[len] = 0;
1959 rc = next_entry(buf, fp, sizeof(u32));
1963 newc->v.sclass = le32_to_cpu(buf[0]);
1964 rc = context_read_and_validate(&newc->context[0], p, fp);
1968 for (l = NULL, c = genfs->head; c;
1969 l = c, c = c->next) {
1971 if (!strcmp(newc->u.name, c->u.name) &&
1972 (!c->v.sclass || !newc->v.sclass ||
1973 newc->v.sclass == c->v.sclass)) {
1974 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1975 genfs->fstype, c->u.name);
1978 len = strlen(newc->u.name);
1979 len2 = strlen(c->u.name);
1995 kfree(newgenfs->fstype);
1997 ocontext_destroy(newc, OCON_FSUSE);
2002 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2008 struct ocontext *l, *c;
2011 for (i = 0; i < info->ocon_num; i++) {
2012 rc = next_entry(buf, fp, sizeof(u32));
2015 nel = le32_to_cpu(buf[0]);
2018 for (j = 0; j < nel; j++) {
2020 c = kzalloc(sizeof(*c), GFP_KERNEL);
2026 p->ocontexts[i] = c;
2031 rc = next_entry(buf, fp, sizeof(u32));
2035 c->sid[0] = le32_to_cpu(buf[0]);
2036 rc = context_read_and_validate(&c->context[0], p, fp);
2042 rc = next_entry(buf, fp, sizeof(u32));
2045 len = le32_to_cpu(buf[0]);
2048 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2052 rc = next_entry(c->u.name, fp, len);
2057 rc = context_read_and_validate(&c->context[0], p, fp);
2060 rc = context_read_and_validate(&c->context[1], p, fp);
2065 rc = next_entry(buf, fp, sizeof(u32)*3);
2068 c->u.port.protocol = le32_to_cpu(buf[0]);
2069 c->u.port.low_port = le32_to_cpu(buf[1]);
2070 c->u.port.high_port = le32_to_cpu(buf[2]);
2071 rc = context_read_and_validate(&c->context[0], p, fp);
2076 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2079 c->u.node.addr = nodebuf[0]; /* network order */
2080 c->u.node.mask = nodebuf[1]; /* network order */
2081 rc = context_read_and_validate(&c->context[0], p, fp);
2086 rc = next_entry(buf, fp, sizeof(u32)*2);
2091 c->v.behavior = le32_to_cpu(buf[0]);
2092 if (c->v.behavior > SECURITY_FS_USE_NONE)
2096 len = le32_to_cpu(buf[1]);
2097 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2101 rc = next_entry(c->u.name, fp, len);
2105 rc = context_read_and_validate(&c->context[0], p, fp);
2112 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2115 for (k = 0; k < 4; k++)
2116 c->u.node6.addr[k] = nodebuf[k];
2117 for (k = 0; k < 4; k++)
2118 c->u.node6.mask[k] = nodebuf[k+4];
2119 rc = context_read_and_validate(&c->context[0], p, fp);
2133 * Read the configuration data from a policy database binary
2134 * representation file into a policy database structure.
2136 int policydb_read(struct policydb *p, void *fp)
2138 struct role_allow *ra, *lra;
2139 struct role_trans *tr, *ltr;
2142 u32 len, nprim, nel;
2145 struct policydb_compat_info *info;
2147 rc = policydb_init(p);
2151 /* Read the magic number and string length. */
2152 rc = next_entry(buf, fp, sizeof(u32) * 2);
2157 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2158 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2159 "not match expected magic number 0x%x\n",
2160 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2165 len = le32_to_cpu(buf[1]);
2166 if (len != strlen(POLICYDB_STRING)) {
2167 printk(KERN_ERR "SELinux: policydb string length %d does not "
2168 "match expected length %Zu\n",
2169 len, strlen(POLICYDB_STRING));
2174 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2175 if (!policydb_str) {
2176 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2177 "string of length %d\n", len);
2181 rc = next_entry(policydb_str, fp, len);
2183 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2184 kfree(policydb_str);
2189 policydb_str[len] = '\0';
2190 if (strcmp(policydb_str, POLICYDB_STRING)) {
2191 printk(KERN_ERR "SELinux: policydb string %s does not match "
2192 "my string %s\n", policydb_str, POLICYDB_STRING);
2193 kfree(policydb_str);
2196 /* Done with policydb_str. */
2197 kfree(policydb_str);
2198 policydb_str = NULL;
2200 /* Read the version and table sizes. */
2201 rc = next_entry(buf, fp, sizeof(u32)*4);
2206 p->policyvers = le32_to_cpu(buf[0]);
2207 if (p->policyvers < POLICYDB_VERSION_MIN ||
2208 p->policyvers > POLICYDB_VERSION_MAX) {
2209 printk(KERN_ERR "SELinux: policydb version %d does not match "
2210 "my version range %d-%d\n",
2211 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2215 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2219 if (p->policyvers < POLICYDB_VERSION_MLS) {
2220 printk(KERN_ERR "SELinux: security policydb version %d "
2221 "(MLS) not backwards compatible\n",
2226 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2227 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2229 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2230 rc = ebitmap_read(&p->policycaps, fp);
2235 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2236 rc = ebitmap_read(&p->permissive_map, fp);
2242 info = policydb_lookup_compat(p->policyvers);
2244 printk(KERN_ERR "SELinux: unable to find policy compat info "
2245 "for version %d\n", p->policyvers);
2250 if (le32_to_cpu(buf[2]) != info->sym_num ||
2251 le32_to_cpu(buf[3]) != info->ocon_num) {
2252 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2253 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2254 le32_to_cpu(buf[3]),
2255 info->sym_num, info->ocon_num);
2259 for (i = 0; i < info->sym_num; i++) {
2260 rc = next_entry(buf, fp, sizeof(u32)*2);
2263 nprim = le32_to_cpu(buf[0]);
2264 nel = le32_to_cpu(buf[1]);
2265 for (j = 0; j < nel; j++) {
2266 rc = read_f[i](p, p->symtab[i].table, fp);
2271 p->symtab[i].nprim = nprim;
2275 p->process_class = string_to_security_class(p, "process");
2276 if (!p->process_class)
2279 rc = avtab_read(&p->te_avtab, fp, p);
2283 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2284 rc = cond_read_list(p, fp);
2289 rc = next_entry(buf, fp, sizeof(u32));
2292 nel = le32_to_cpu(buf[0]);
2294 for (i = 0; i < nel; i++) {
2296 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2303 rc = next_entry(buf, fp, sizeof(u32)*3);
2308 tr->role = le32_to_cpu(buf[0]);
2309 tr->type = le32_to_cpu(buf[1]);
2310 tr->new_role = le32_to_cpu(buf[2]);
2311 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2312 rc = next_entry(buf, fp, sizeof(u32));
2315 tr->tclass = le32_to_cpu(buf[0]);
2317 tr->tclass = p->process_class;
2319 if (!policydb_role_isvalid(p, tr->role) ||
2320 !policydb_type_isvalid(p, tr->type) ||
2321 !policydb_class_isvalid(p, tr->tclass) ||
2322 !policydb_role_isvalid(p, tr->new_role))
2327 rc = next_entry(buf, fp, sizeof(u32));
2330 nel = le32_to_cpu(buf[0]);
2332 for (i = 0; i < nel; i++) {
2334 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2341 rc = next_entry(buf, fp, sizeof(u32)*2);
2346 ra->role = le32_to_cpu(buf[0]);
2347 ra->new_role = le32_to_cpu(buf[1]);
2348 if (!policydb_role_isvalid(p, ra->role) ||
2349 !policydb_role_isvalid(p, ra->new_role))
2354 rc = filename_trans_read(p, fp);
2358 rc = policydb_index(p);
2363 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2364 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2365 if (!p->process_trans_perms)
2368 rc = ocontext_read(p, info, fp);
2372 rc = genfs_read(p, fp);
2376 rc = range_read(p, fp);
2381 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2383 GFP_KERNEL | __GFP_ZERO);
2384 if (!p->type_attr_map_array)
2387 /* preallocate so we don't have to worry about the put ever failing */
2388 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2389 GFP_KERNEL | __GFP_ZERO);
2393 for (i = 0; i < p->p_types.nprim; i++) {
2394 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2398 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2399 rc = ebitmap_read(e, fp);
2403 /* add the type itself as the degenerate case */
2404 rc = ebitmap_set_bit(e, i, 1);
2409 rc = policydb_bounds_sanity_check(p);
2417 policydb_destroy(p);
2422 * Write a MLS level structure to a policydb binary
2423 * representation file.
2425 static int mls_write_level(struct mls_level *l, void *fp)
2430 buf[0] = cpu_to_le32(l->sens);
2431 rc = put_entry(buf, sizeof(u32), 1, fp);
2435 rc = ebitmap_write(&l->cat, fp);
2443 * Write a MLS range structure to a policydb binary
2444 * representation file.
2446 static int mls_write_range_helper(struct mls_range *r, void *fp)
2452 eq = mls_level_eq(&r->level[1], &r->level[0]);
2458 buf[0] = cpu_to_le32(items-1);
2459 buf[1] = cpu_to_le32(r->level[0].sens);
2461 buf[2] = cpu_to_le32(r->level[1].sens);
2463 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2465 rc = put_entry(buf, sizeof(u32), items, fp);
2469 rc = ebitmap_write(&r->level[0].cat, fp);
2473 rc = ebitmap_write(&r->level[1].cat, fp);
2481 static int sens_write(void *vkey, void *datum, void *ptr)
2484 struct level_datum *levdatum = datum;
2485 struct policy_data *pd = ptr;
2492 buf[0] = cpu_to_le32(len);
2493 buf[1] = cpu_to_le32(levdatum->isalias);
2494 rc = put_entry(buf, sizeof(u32), 2, fp);
2498 rc = put_entry(key, 1, len, fp);
2502 rc = mls_write_level(levdatum->level, fp);
2509 static int cat_write(void *vkey, void *datum, void *ptr)
2512 struct cat_datum *catdatum = datum;
2513 struct policy_data *pd = ptr;
2520 buf[0] = cpu_to_le32(len);
2521 buf[1] = cpu_to_le32(catdatum->value);
2522 buf[2] = cpu_to_le32(catdatum->isalias);
2523 rc = put_entry(buf, sizeof(u32), 3, fp);
2527 rc = put_entry(key, 1, len, fp);
2534 static int role_trans_write(struct policydb *p, void *fp)
2536 struct role_trans *r = p->role_tr;
2537 struct role_trans *tr;
2543 for (tr = r; tr; tr = tr->next)
2545 buf[0] = cpu_to_le32(nel);
2546 rc = put_entry(buf, sizeof(u32), 1, fp);
2549 for (tr = r; tr; tr = tr->next) {
2550 buf[0] = cpu_to_le32(tr->role);
2551 buf[1] = cpu_to_le32(tr->type);
2552 buf[2] = cpu_to_le32(tr->new_role);
2553 rc = put_entry(buf, sizeof(u32), 3, fp);
2556 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2557 buf[0] = cpu_to_le32(tr->tclass);
2558 rc = put_entry(buf, sizeof(u32), 1, fp);
2567 static int role_allow_write(struct role_allow *r, void *fp)
2569 struct role_allow *ra;
2575 for (ra = r; ra; ra = ra->next)
2577 buf[0] = cpu_to_le32(nel);
2578 rc = put_entry(buf, sizeof(u32), 1, fp);
2581 for (ra = r; ra; ra = ra->next) {
2582 buf[0] = cpu_to_le32(ra->role);
2583 buf[1] = cpu_to_le32(ra->new_role);
2584 rc = put_entry(buf, sizeof(u32), 2, fp);
2592 * Write a security context structure
2593 * to a policydb binary representation file.
2595 static int context_write(struct policydb *p, struct context *c,
2601 buf[0] = cpu_to_le32(c->user);
2602 buf[1] = cpu_to_le32(c->role);
2603 buf[2] = cpu_to_le32(c->type);
2605 rc = put_entry(buf, sizeof(u32), 3, fp);
2609 rc = mls_write_range_helper(&c->range, fp);
2617 * The following *_write functions are used to
2618 * write the symbol data to a policy database
2619 * binary representation file.
2622 static int perm_write(void *vkey, void *datum, void *fp)
2625 struct perm_datum *perdatum = datum;
2631 buf[0] = cpu_to_le32(len);
2632 buf[1] = cpu_to_le32(perdatum->value);
2633 rc = put_entry(buf, sizeof(u32), 2, fp);
2637 rc = put_entry(key, 1, len, fp);
2644 static int common_write(void *vkey, void *datum, void *ptr)
2647 struct common_datum *comdatum = datum;
2648 struct policy_data *pd = ptr;
2655 buf[0] = cpu_to_le32(len);
2656 buf[1] = cpu_to_le32(comdatum->value);
2657 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2658 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2659 rc = put_entry(buf, sizeof(u32), 4, fp);
2663 rc = put_entry(key, 1, len, fp);
2667 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2674 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2677 struct constraint_node *c;
2678 struct constraint_expr *e;
2683 for (c = node; c; c = c->next) {
2685 for (e = c->expr; e; e = e->next)
2687 buf[0] = cpu_to_le32(c->permissions);
2688 buf[1] = cpu_to_le32(nel);
2689 rc = put_entry(buf, sizeof(u32), 2, fp);
2692 for (e = c->expr; e; e = e->next) {
2693 buf[0] = cpu_to_le32(e->expr_type);
2694 buf[1] = cpu_to_le32(e->attr);
2695 buf[2] = cpu_to_le32(e->op);
2696 rc = put_entry(buf, sizeof(u32), 3, fp);
2700 switch (e->expr_type) {
2702 rc = ebitmap_write(&e->names, fp);
2715 static int class_write(void *vkey, void *datum, void *ptr)
2718 struct class_datum *cladatum = datum;
2719 struct policy_data *pd = ptr;
2721 struct policydb *p = pd->p;
2722 struct constraint_node *c;
2729 if (cladatum->comkey)
2730 len2 = strlen(cladatum->comkey);
2735 for (c = cladatum->constraints; c; c = c->next)
2738 buf[0] = cpu_to_le32(len);
2739 buf[1] = cpu_to_le32(len2);
2740 buf[2] = cpu_to_le32(cladatum->value);
2741 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2742 if (cladatum->permissions.table)
2743 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2746 buf[5] = cpu_to_le32(ncons);
2747 rc = put_entry(buf, sizeof(u32), 6, fp);
2751 rc = put_entry(key, 1, len, fp);
2755 if (cladatum->comkey) {
2756 rc = put_entry(cladatum->comkey, 1, len2, fp);
2761 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2765 rc = write_cons_helper(p, cladatum->constraints, fp);
2769 /* write out the validatetrans rule */
2771 for (c = cladatum->validatetrans; c; c = c->next)
2774 buf[0] = cpu_to_le32(ncons);
2775 rc = put_entry(buf, sizeof(u32), 1, fp);
2779 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2786 static int role_write(void *vkey, void *datum, void *ptr)
2789 struct role_datum *role = datum;
2790 struct policy_data *pd = ptr;
2792 struct policydb *p = pd->p;
2799 buf[items++] = cpu_to_le32(len);
2800 buf[items++] = cpu_to_le32(role->value);
2801 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2802 buf[items++] = cpu_to_le32(role->bounds);
2804 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2806 rc = put_entry(buf, sizeof(u32), items, fp);
2810 rc = put_entry(key, 1, len, fp);
2814 rc = ebitmap_write(&role->dominates, fp);
2818 rc = ebitmap_write(&role->types, fp);
2825 static int type_write(void *vkey, void *datum, void *ptr)
2828 struct type_datum *typdatum = datum;
2829 struct policy_data *pd = ptr;
2830 struct policydb *p = pd->p;
2838 buf[items++] = cpu_to_le32(len);
2839 buf[items++] = cpu_to_le32(typdatum->value);
2840 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2843 if (typdatum->primary)
2844 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2846 if (typdatum->attribute)
2847 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2849 buf[items++] = cpu_to_le32(properties);
2850 buf[items++] = cpu_to_le32(typdatum->bounds);
2852 buf[items++] = cpu_to_le32(typdatum->primary);
2854 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2855 rc = put_entry(buf, sizeof(u32), items, fp);
2859 rc = put_entry(key, 1, len, fp);
2866 static int user_write(void *vkey, void *datum, void *ptr)
2869 struct user_datum *usrdatum = datum;
2870 struct policy_data *pd = ptr;
2871 struct policydb *p = pd->p;
2879 buf[items++] = cpu_to_le32(len);
2880 buf[items++] = cpu_to_le32(usrdatum->value);
2881 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2882 buf[items++] = cpu_to_le32(usrdatum->bounds);
2883 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2884 rc = put_entry(buf, sizeof(u32), items, fp);
2888 rc = put_entry(key, 1, len, fp);
2892 rc = ebitmap_write(&usrdatum->roles, fp);
2896 rc = mls_write_range_helper(&usrdatum->range, fp);
2900 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2907 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2920 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2923 unsigned int i, j, rc;
2928 for (i = 0; i < info->ocon_num; i++) {
2930 for (c = p->ocontexts[i]; c; c = c->next)
2932 buf[0] = cpu_to_le32(nel);
2933 rc = put_entry(buf, sizeof(u32), 1, fp);
2936 for (c = p->ocontexts[i]; c; c = c->next) {
2939 buf[0] = cpu_to_le32(c->sid[0]);
2940 rc = put_entry(buf, sizeof(u32), 1, fp);
2943 rc = context_write(p, &c->context[0], fp);
2949 len = strlen(c->u.name);
2950 buf[0] = cpu_to_le32(len);
2951 rc = put_entry(buf, sizeof(u32), 1, fp);
2954 rc = put_entry(c->u.name, 1, len, fp);
2957 rc = context_write(p, &c->context[0], fp);
2960 rc = context_write(p, &c->context[1], fp);
2965 buf[0] = cpu_to_le32(c->u.port.protocol);
2966 buf[1] = cpu_to_le32(c->u.port.low_port);
2967 buf[2] = cpu_to_le32(c->u.port.high_port);
2968 rc = put_entry(buf, sizeof(u32), 3, fp);
2971 rc = context_write(p, &c->context[0], fp);
2976 nodebuf[0] = c->u.node.addr; /* network order */
2977 nodebuf[1] = c->u.node.mask; /* network order */
2978 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2981 rc = context_write(p, &c->context[0], fp);
2986 buf[0] = cpu_to_le32(c->v.behavior);
2987 len = strlen(c->u.name);
2988 buf[1] = cpu_to_le32(len);
2989 rc = put_entry(buf, sizeof(u32), 2, fp);
2992 rc = put_entry(c->u.name, 1, len, fp);
2995 rc = context_write(p, &c->context[0], fp);
3000 for (j = 0; j < 4; j++)
3001 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3002 for (j = 0; j < 4; j++)
3003 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3004 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3007 rc = context_write(p, &c->context[0], fp);
3017 static int genfs_write(struct policydb *p, void *fp)
3019 struct genfs *genfs;
3026 for (genfs = p->genfs; genfs; genfs = genfs->next)
3028 buf[0] = cpu_to_le32(len);
3029 rc = put_entry(buf, sizeof(u32), 1, fp);
3032 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3033 len = strlen(genfs->fstype);
3034 buf[0] = cpu_to_le32(len);
3035 rc = put_entry(buf, sizeof(u32), 1, fp);
3038 rc = put_entry(genfs->fstype, 1, len, fp);
3042 for (c = genfs->head; c; c = c->next)
3044 buf[0] = cpu_to_le32(len);
3045 rc = put_entry(buf, sizeof(u32), 1, fp);
3048 for (c = genfs->head; c; c = c->next) {
3049 len = strlen(c->u.name);
3050 buf[0] = cpu_to_le32(len);
3051 rc = put_entry(buf, sizeof(u32), 1, fp);
3054 rc = put_entry(c->u.name, 1, len, fp);
3057 buf[0] = cpu_to_le32(c->v.sclass);
3058 rc = put_entry(buf, sizeof(u32), 1, fp);
3061 rc = context_write(p, &c->context[0], fp);
3069 static int hashtab_cnt(void *key, void *data, void *ptr)
3077 static int range_write_helper(void *key, void *data, void *ptr)
3080 struct range_trans *rt = key;
3081 struct mls_range *r = data;
3082 struct policy_data *pd = ptr;
3084 struct policydb *p = pd->p;
3087 buf[0] = cpu_to_le32(rt->source_type);
3088 buf[1] = cpu_to_le32(rt->target_type);
3089 rc = put_entry(buf, sizeof(u32), 2, fp);
3092 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3093 buf[0] = cpu_to_le32(rt->target_class);
3094 rc = put_entry(buf, sizeof(u32), 1, fp);
3098 rc = mls_write_range_helper(r, fp);
3105 static int range_write(struct policydb *p, void *fp)
3110 struct policy_data pd;
3115 /* count the number of entries in the hashtab */
3117 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3121 buf[0] = cpu_to_le32(nel);
3122 rc = put_entry(buf, sizeof(u32), 1, fp);
3126 /* actually write all of the entries */
3127 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3134 static int filename_trans_write(struct policydb *p, void *fp)
3136 struct filename_trans *ft;
3141 for (ft = p->filename_trans; ft; ft = ft->next)
3144 buf[0] = cpu_to_le32(nel);
3145 rc = put_entry(buf, sizeof(u32), 1, fp);
3149 for (ft = p->filename_trans; ft; ft = ft->next) {
3150 len = strlen(ft->name);
3151 buf[0] = cpu_to_le32(len);
3152 rc = put_entry(buf, sizeof(u32), 1, fp);
3156 rc = put_entry(ft->name, sizeof(char), len, fp);
3162 buf[2] = ft->tclass;
3165 rc = put_entry(buf, sizeof(u32), 4, fp);
3172 * Write the configuration data in a policy database
3173 * structure to a policy database binary representation
3176 int policydb_write(struct policydb *p, void *fp)
3178 unsigned int i, num_syms;
3183 struct policydb_compat_info *info;
3186 * refuse to write policy older than compressed avtab
3187 * to simplify the writer. There are other tests dropped
3188 * since we assume this throughout the writer code. Be
3189 * careful if you ever try to remove this restriction
3191 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3192 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3193 " Because it is less than version %d\n", p->policyvers,
3194 POLICYDB_VERSION_AVTAB);
3200 config |= POLICYDB_CONFIG_MLS;
3202 if (p->reject_unknown)
3203 config |= REJECT_UNKNOWN;
3204 if (p->allow_unknown)
3205 config |= ALLOW_UNKNOWN;
3207 /* Write the magic number and string identifiers. */
3208 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3209 len = strlen(POLICYDB_STRING);
3210 buf[1] = cpu_to_le32(len);
3211 rc = put_entry(buf, sizeof(u32), 2, fp);
3214 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3218 /* Write the version, config, and table sizes. */
3219 info = policydb_lookup_compat(p->policyvers);
3221 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3222 "version %d", p->policyvers);
3226 buf[0] = cpu_to_le32(p->policyvers);
3227 buf[1] = cpu_to_le32(config);
3228 buf[2] = cpu_to_le32(info->sym_num);
3229 buf[3] = cpu_to_le32(info->ocon_num);
3231 rc = put_entry(buf, sizeof(u32), 4, fp);
3235 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3236 rc = ebitmap_write(&p->policycaps, fp);
3241 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3242 rc = ebitmap_write(&p->permissive_map, fp);
3247 num_syms = info->sym_num;
3248 for (i = 0; i < num_syms; i++) {
3249 struct policy_data pd;
3254 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3255 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3257 rc = put_entry(buf, sizeof(u32), 2, fp);
3260 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3265 rc = avtab_write(p, &p->te_avtab, fp);
3269 rc = cond_write_list(p, p->cond_list, fp);
3273 rc = role_trans_write(p, fp);
3277 rc = role_allow_write(p->role_allow, fp);
3281 rc = filename_trans_write(p, fp);
3285 rc = ocontext_write(p, info, fp);
3289 rc = genfs_write(p, fp);
3293 rc = range_write(p, fp);
3297 for (i = 0; i < p->p_types.nprim; i++) {
3298 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3301 rc = ebitmap_write(e, fp);