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->policycaps);
244 ebitmap_init(&p->permissive_map);
248 for (i = 0; i < SYM_NUM; i++)
249 hashtab_destroy(p->symtab[i].table);
254 * The following *_index functions are used to
255 * define the val_to_name and val_to_struct arrays
256 * in a policy database structure. The val_to_name
257 * arrays are used when converting security context
258 * structures into string representations. The
259 * val_to_struct arrays are used when the attributes
260 * of a class, role, or user are needed.
263 static int common_index(void *key, void *datum, void *datap)
266 struct common_datum *comdatum;
267 struct flex_array *fa;
271 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
274 fa = p->sym_val_to_name[SYM_COMMONS];
275 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
276 GFP_KERNEL | __GFP_ZERO))
281 static int class_index(void *key, void *datum, void *datap)
284 struct class_datum *cladatum;
285 struct flex_array *fa;
289 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
291 fa = p->sym_val_to_name[SYM_CLASSES];
292 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
293 GFP_KERNEL | __GFP_ZERO))
295 p->class_val_to_struct[cladatum->value - 1] = cladatum;
299 static int role_index(void *key, void *datum, void *datap)
302 struct role_datum *role;
303 struct flex_array *fa;
308 || role->value > p->p_roles.nprim
309 || role->bounds > p->p_roles.nprim)
312 fa = p->sym_val_to_name[SYM_ROLES];
313 if (flex_array_put_ptr(fa, role->value - 1, key,
314 GFP_KERNEL | __GFP_ZERO))
316 p->role_val_to_struct[role->value - 1] = role;
320 static int type_index(void *key, void *datum, void *datap)
323 struct type_datum *typdatum;
324 struct flex_array *fa;
329 if (typdatum->primary) {
331 || typdatum->value > p->p_types.nprim
332 || typdatum->bounds > p->p_types.nprim)
334 fa = p->sym_val_to_name[SYM_TYPES];
335 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
336 GFP_KERNEL | __GFP_ZERO))
339 fa = p->type_val_to_struct_array;
340 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
341 GFP_KERNEL | __GFP_ZERO))
348 static int user_index(void *key, void *datum, void *datap)
351 struct user_datum *usrdatum;
352 struct flex_array *fa;
357 || usrdatum->value > p->p_users.nprim
358 || usrdatum->bounds > p->p_users.nprim)
361 fa = p->sym_val_to_name[SYM_USERS];
362 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
363 GFP_KERNEL | __GFP_ZERO))
365 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
369 static int sens_index(void *key, void *datum, void *datap)
372 struct level_datum *levdatum;
373 struct flex_array *fa;
378 if (!levdatum->isalias) {
379 if (!levdatum->level->sens ||
380 levdatum->level->sens > p->p_levels.nprim)
382 fa = p->sym_val_to_name[SYM_LEVELS];
383 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
384 GFP_KERNEL | __GFP_ZERO))
391 static int cat_index(void *key, void *datum, void *datap)
394 struct cat_datum *catdatum;
395 struct flex_array *fa;
400 if (!catdatum->isalias) {
401 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
403 fa = p->sym_val_to_name[SYM_CATS];
404 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
405 GFP_KERNEL | __GFP_ZERO))
412 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
425 static void symtab_hash_eval(struct symtab *s)
429 for (i = 0; i < SYM_NUM; i++) {
430 struct hashtab *h = s[i].table;
431 struct hashtab_info info;
433 hashtab_stat(h, &info);
434 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
435 "longest chain length %d\n", symtab_name[i], h->nel,
436 info.slots_used, h->size, info.max_chain_len);
440 static void rangetr_hash_eval(struct hashtab *h)
442 struct hashtab_info info;
444 hashtab_stat(h, &info);
445 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
446 "longest chain length %d\n", h->nel,
447 info.slots_used, h->size, info.max_chain_len);
450 static inline void rangetr_hash_eval(struct hashtab *h)
456 * Define the other val_to_name and val_to_struct arrays
457 * in a policy database structure.
459 * Caller must clean up on failure.
461 static int policydb_index(struct policydb *p)
465 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
466 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
468 printk(", %d sens, %d cats", p->p_levels.nprim,
472 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
473 p->p_classes.nprim, p->te_avtab.nel);
476 avtab_hash_eval(&p->te_avtab, "rules");
477 symtab_hash_eval(p->symtab);
481 p->class_val_to_struct =
482 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
484 if (!p->class_val_to_struct)
488 p->role_val_to_struct =
489 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
491 if (!p->role_val_to_struct)
495 p->user_val_to_struct =
496 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
498 if (!p->user_val_to_struct)
501 /* Yes, I want the sizeof the pointer, not the structure */
503 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
505 GFP_KERNEL | __GFP_ZERO);
506 if (!p->type_val_to_struct_array)
509 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
510 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
514 rc = cond_init_bool_indexes(p);
518 for (i = 0; i < SYM_NUM; i++) {
520 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
522 GFP_KERNEL | __GFP_ZERO);
523 if (!p->sym_val_to_name[i])
526 rc = flex_array_prealloc(p->sym_val_to_name[i],
527 0, p->symtab[i].nprim - 1,
528 GFP_KERNEL | __GFP_ZERO);
532 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
542 * The following *_destroy functions are used to
543 * free any memory allocated for each kind of
544 * symbol data in the policy database.
547 static int perm_destroy(void *key, void *datum, void *p)
554 static int common_destroy(void *key, void *datum, void *p)
556 struct common_datum *comdatum;
561 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
562 hashtab_destroy(comdatum->permissions.table);
568 static int cls_destroy(void *key, void *datum, void *p)
570 struct class_datum *cladatum;
571 struct constraint_node *constraint, *ctemp;
572 struct constraint_expr *e, *etmp;
577 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
578 hashtab_destroy(cladatum->permissions.table);
579 constraint = cladatum->constraints;
581 e = constraint->expr;
583 ebitmap_destroy(&e->names);
589 constraint = constraint->next;
593 constraint = cladatum->validatetrans;
595 e = constraint->expr;
597 ebitmap_destroy(&e->names);
603 constraint = constraint->next;
607 kfree(cladatum->comkey);
613 static int role_destroy(void *key, void *datum, void *p)
615 struct role_datum *role;
620 ebitmap_destroy(&role->dominates);
621 ebitmap_destroy(&role->types);
627 static int type_destroy(void *key, void *datum, void *p)
634 static int user_destroy(void *key, void *datum, void *p)
636 struct user_datum *usrdatum;
641 ebitmap_destroy(&usrdatum->roles);
642 ebitmap_destroy(&usrdatum->range.level[0].cat);
643 ebitmap_destroy(&usrdatum->range.level[1].cat);
644 ebitmap_destroy(&usrdatum->dfltlevel.cat);
650 static int sens_destroy(void *key, void *datum, void *p)
652 struct level_datum *levdatum;
657 ebitmap_destroy(&levdatum->level->cat);
658 kfree(levdatum->level);
664 static int cat_destroy(void *key, void *datum, void *p)
671 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
683 static int range_tr_destroy(void *key, void *datum, void *p)
685 struct mls_range *rt = datum;
687 ebitmap_destroy(&rt->level[0].cat);
688 ebitmap_destroy(&rt->level[1].cat);
694 static void ocontext_destroy(struct ocontext *c, int i)
699 context_destroy(&c->context[0]);
700 context_destroy(&c->context[1]);
701 if (i == OCON_ISID || i == OCON_FS ||
702 i == OCON_NETIF || i == OCON_FSUSE)
708 * Free any memory allocated by a policy database structure.
710 void policydb_destroy(struct policydb *p)
712 struct ocontext *c, *ctmp;
713 struct genfs *g, *gtmp;
715 struct role_allow *ra, *lra = NULL;
716 struct role_trans *tr, *ltr = NULL;
717 struct filename_trans *ft, *nft;
719 for (i = 0; i < SYM_NUM; i++) {
721 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
722 hashtab_destroy(p->symtab[i].table);
725 for (i = 0; i < SYM_NUM; i++) {
726 if (p->sym_val_to_name[i])
727 flex_array_free(p->sym_val_to_name[i]);
730 kfree(p->class_val_to_struct);
731 kfree(p->role_val_to_struct);
732 kfree(p->user_val_to_struct);
733 if (p->type_val_to_struct_array)
734 flex_array_free(p->type_val_to_struct_array);
736 avtab_destroy(&p->te_avtab);
738 for (i = 0; i < OCON_NUM; i++) {
744 ocontext_destroy(ctmp, i);
746 p->ocontexts[i] = NULL;
757 ocontext_destroy(ctmp, OCON_FSUSE);
765 cond_policydb_destroy(p);
767 for (tr = p->role_tr; tr; tr = tr->next) {
774 for (ra = p->role_allow; ra; ra = ra->next) {
781 hashtab_map(p->range_tr, range_tr_destroy, NULL);
782 hashtab_destroy(p->range_tr);
784 if (p->type_attr_map_array) {
785 for (i = 0; i < p->p_types.nprim; i++) {
788 e = flex_array_get(p->type_attr_map_array, i);
793 flex_array_free(p->type_attr_map_array);
796 ft = p->filename_trans;
804 ebitmap_destroy(&p->policycaps);
805 ebitmap_destroy(&p->permissive_map);
811 * Load the initial SIDs specified in a policy database
812 * structure into a SID table.
814 int policydb_load_isids(struct policydb *p, struct sidtab *s)
816 struct ocontext *head, *c;
821 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
825 head = p->ocontexts[OCON_ISID];
826 for (c = head; c; c = c->next) {
828 if (!c->context[0].user) {
829 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
834 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
836 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
846 int policydb_class_isvalid(struct policydb *p, unsigned int class)
848 if (!class || class > p->p_classes.nprim)
853 int policydb_role_isvalid(struct policydb *p, unsigned int role)
855 if (!role || role > p->p_roles.nprim)
860 int policydb_type_isvalid(struct policydb *p, unsigned int type)
862 if (!type || type > p->p_types.nprim)
868 * Return 1 if the fields in the security context
869 * structure `c' are valid. Return 0 otherwise.
871 int policydb_context_isvalid(struct policydb *p, struct context *c)
873 struct role_datum *role;
874 struct user_datum *usrdatum;
876 if (!c->role || c->role > p->p_roles.nprim)
879 if (!c->user || c->user > p->p_users.nprim)
882 if (!c->type || c->type > p->p_types.nprim)
885 if (c->role != OBJECT_R_VAL) {
887 * Role must be authorized for the type.
889 role = p->role_val_to_struct[c->role - 1];
890 if (!ebitmap_get_bit(&role->types, c->type - 1))
891 /* role may not be associated with type */
895 * User must be authorized for the role.
897 usrdatum = p->user_val_to_struct[c->user - 1];
901 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
902 /* user may not be associated with role */
906 if (!mls_context_isvalid(p, c))
913 * Read a MLS range structure from a policydb binary
914 * representation file.
916 static int mls_read_range_helper(struct mls_range *r, void *fp)
922 rc = next_entry(buf, fp, sizeof(u32));
927 items = le32_to_cpu(buf[0]);
928 if (items > ARRAY_SIZE(buf)) {
929 printk(KERN_ERR "SELinux: mls: range overflow\n");
933 rc = next_entry(buf, fp, sizeof(u32) * items);
935 printk(KERN_ERR "SELinux: mls: truncated range\n");
939 r->level[0].sens = le32_to_cpu(buf[0]);
941 r->level[1].sens = le32_to_cpu(buf[1]);
943 r->level[1].sens = r->level[0].sens;
945 rc = ebitmap_read(&r->level[0].cat, fp);
947 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
951 rc = ebitmap_read(&r->level[1].cat, fp);
953 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
957 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
959 printk(KERN_ERR "SELinux: mls: out of memory\n");
966 ebitmap_destroy(&r->level[0].cat);
972 * Read and validate a security context structure
973 * from a policydb binary representation file.
975 static int context_read_and_validate(struct context *c,
982 rc = next_entry(buf, fp, sizeof buf);
984 printk(KERN_ERR "SELinux: context truncated\n");
987 c->user = le32_to_cpu(buf[0]);
988 c->role = le32_to_cpu(buf[1]);
989 c->type = le32_to_cpu(buf[2]);
990 if (p->policyvers >= POLICYDB_VERSION_MLS) {
991 rc = mls_read_range_helper(&c->range, fp);
993 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
999 if (!policydb_context_isvalid(p, c)) {
1000 printk(KERN_ERR "SELinux: invalid security context\n");
1010 * The following *_read functions are used to
1011 * read the symbol data from a policy database
1012 * binary representation file.
1015 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1018 struct perm_datum *perdatum;
1024 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1028 rc = next_entry(buf, fp, sizeof buf);
1032 len = le32_to_cpu(buf[0]);
1033 perdatum->value = le32_to_cpu(buf[1]);
1036 key = kmalloc(len + 1, GFP_KERNEL);
1040 rc = next_entry(key, fp, len);
1045 rc = hashtab_insert(h, key, perdatum);
1051 perm_destroy(key, perdatum, NULL);
1055 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1058 struct common_datum *comdatum;
1064 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1068 rc = next_entry(buf, fp, sizeof buf);
1072 len = le32_to_cpu(buf[0]);
1073 comdatum->value = le32_to_cpu(buf[1]);
1075 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1078 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1079 nel = le32_to_cpu(buf[3]);
1082 key = kmalloc(len + 1, GFP_KERNEL);
1086 rc = next_entry(key, fp, len);
1091 for (i = 0; i < nel; i++) {
1092 rc = perm_read(p, comdatum->permissions.table, fp);
1097 rc = hashtab_insert(h, key, comdatum);
1102 common_destroy(key, comdatum, NULL);
1106 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1107 int allowxtarget, void *fp)
1109 struct constraint_node *c, *lc;
1110 struct constraint_expr *e, *le;
1113 int rc, i, j, depth;
1116 for (i = 0; i < ncons; i++) {
1117 c = kzalloc(sizeof(*c), GFP_KERNEL);
1126 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1129 c->permissions = le32_to_cpu(buf[0]);
1130 nexpr = le32_to_cpu(buf[1]);
1133 for (j = 0; j < nexpr; j++) {
1134 e = kzalloc(sizeof(*e), GFP_KERNEL);
1143 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1146 e->expr_type = le32_to_cpu(buf[0]);
1147 e->attr = le32_to_cpu(buf[1]);
1148 e->op = le32_to_cpu(buf[2]);
1150 switch (e->expr_type) {
1162 if (depth == (CEXPR_MAXDEPTH - 1))
1167 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1169 if (depth == (CEXPR_MAXDEPTH - 1))
1172 rc = ebitmap_read(&e->names, fp);
1189 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1192 struct class_datum *cladatum;
1194 u32 len, len2, ncons, nel;
1198 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1202 rc = next_entry(buf, fp, sizeof(u32)*6);
1206 len = le32_to_cpu(buf[0]);
1207 len2 = le32_to_cpu(buf[1]);
1208 cladatum->value = le32_to_cpu(buf[2]);
1210 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1213 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1214 nel = le32_to_cpu(buf[4]);
1216 ncons = le32_to_cpu(buf[5]);
1219 key = kmalloc(len + 1, GFP_KERNEL);
1223 rc = next_entry(key, fp, len);
1230 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1231 if (!cladatum->comkey)
1233 rc = next_entry(cladatum->comkey, fp, len2);
1236 cladatum->comkey[len2] = '\0';
1239 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1240 if (!cladatum->comdatum) {
1241 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1245 for (i = 0; i < nel; i++) {
1246 rc = perm_read(p, cladatum->permissions.table, fp);
1251 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1255 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1256 /* grab the validatetrans rules */
1257 rc = next_entry(buf, fp, sizeof(u32));
1260 ncons = le32_to_cpu(buf[0]);
1261 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1266 rc = hashtab_insert(h, key, cladatum);
1272 cls_destroy(key, cladatum, NULL);
1276 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1279 struct role_datum *role;
1280 int rc, to_read = 2;
1285 role = kzalloc(sizeof(*role), GFP_KERNEL);
1289 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1292 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1296 len = le32_to_cpu(buf[0]);
1297 role->value = le32_to_cpu(buf[1]);
1298 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1299 role->bounds = le32_to_cpu(buf[2]);
1302 key = kmalloc(len + 1, GFP_KERNEL);
1306 rc = next_entry(key, fp, len);
1311 rc = ebitmap_read(&role->dominates, fp);
1315 rc = ebitmap_read(&role->types, fp);
1319 if (strcmp(key, OBJECT_R) == 0) {
1321 if (role->value != OBJECT_R_VAL) {
1322 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1323 OBJECT_R, role->value);
1330 rc = hashtab_insert(h, key, role);
1335 role_destroy(key, role, NULL);
1339 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1342 struct type_datum *typdatum;
1343 int rc, to_read = 3;
1348 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1352 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1355 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1359 len = le32_to_cpu(buf[0]);
1360 typdatum->value = le32_to_cpu(buf[1]);
1361 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1362 u32 prop = le32_to_cpu(buf[2]);
1364 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1365 typdatum->primary = 1;
1366 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1367 typdatum->attribute = 1;
1369 typdatum->bounds = le32_to_cpu(buf[3]);
1371 typdatum->primary = le32_to_cpu(buf[2]);
1375 key = kmalloc(len + 1, GFP_KERNEL);
1378 rc = next_entry(key, fp, len);
1383 rc = hashtab_insert(h, key, typdatum);
1388 type_destroy(key, typdatum, NULL);
1394 * Read a MLS level structure from a policydb binary
1395 * representation file.
1397 static int mls_read_level(struct mls_level *lp, void *fp)
1402 memset(lp, 0, sizeof(*lp));
1404 rc = next_entry(buf, fp, sizeof buf);
1406 printk(KERN_ERR "SELinux: mls: truncated level\n");
1409 lp->sens = le32_to_cpu(buf[0]);
1411 rc = ebitmap_read(&lp->cat, fp);
1413 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1419 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1422 struct user_datum *usrdatum;
1423 int rc, to_read = 2;
1428 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1432 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1435 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1439 len = le32_to_cpu(buf[0]);
1440 usrdatum->value = le32_to_cpu(buf[1]);
1441 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1442 usrdatum->bounds = le32_to_cpu(buf[2]);
1445 key = kmalloc(len + 1, GFP_KERNEL);
1448 rc = next_entry(key, fp, len);
1453 rc = ebitmap_read(&usrdatum->roles, fp);
1457 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1458 rc = mls_read_range_helper(&usrdatum->range, fp);
1461 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1466 rc = hashtab_insert(h, key, usrdatum);
1471 user_destroy(key, usrdatum, NULL);
1475 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1478 struct level_datum *levdatum;
1484 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1488 rc = next_entry(buf, fp, sizeof buf);
1492 len = le32_to_cpu(buf[0]);
1493 levdatum->isalias = le32_to_cpu(buf[1]);
1496 key = kmalloc(len + 1, GFP_ATOMIC);
1499 rc = next_entry(key, fp, len);
1505 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1506 if (!levdatum->level)
1509 rc = mls_read_level(levdatum->level, fp);
1513 rc = hashtab_insert(h, key, levdatum);
1518 sens_destroy(key, levdatum, NULL);
1522 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1525 struct cat_datum *catdatum;
1531 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1535 rc = next_entry(buf, fp, sizeof buf);
1539 len = le32_to_cpu(buf[0]);
1540 catdatum->value = le32_to_cpu(buf[1]);
1541 catdatum->isalias = le32_to_cpu(buf[2]);
1544 key = kmalloc(len + 1, GFP_ATOMIC);
1547 rc = next_entry(key, fp, len);
1552 rc = hashtab_insert(h, key, catdatum);
1557 cat_destroy(key, catdatum, NULL);
1561 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1573 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1575 struct user_datum *upper, *user;
1576 struct policydb *p = datap;
1579 upper = user = datum;
1580 while (upper->bounds) {
1581 struct ebitmap_node *node;
1584 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1585 printk(KERN_ERR "SELinux: user %s: "
1586 "too deep or looped boundary",
1591 upper = p->user_val_to_struct[upper->bounds - 1];
1592 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1593 if (ebitmap_get_bit(&upper->roles, bit))
1597 "SELinux: boundary violated policy: "
1598 "user=%s role=%s bounds=%s\n",
1599 sym_name(p, SYM_USERS, user->value - 1),
1600 sym_name(p, SYM_ROLES, bit),
1601 sym_name(p, SYM_USERS, upper->value - 1));
1610 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1612 struct role_datum *upper, *role;
1613 struct policydb *p = datap;
1616 upper = role = datum;
1617 while (upper->bounds) {
1618 struct ebitmap_node *node;
1621 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1622 printk(KERN_ERR "SELinux: role %s: "
1623 "too deep or looped bounds\n",
1628 upper = p->role_val_to_struct[upper->bounds - 1];
1629 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1630 if (ebitmap_get_bit(&upper->types, bit))
1634 "SELinux: boundary violated policy: "
1635 "role=%s type=%s bounds=%s\n",
1636 sym_name(p, SYM_ROLES, role->value - 1),
1637 sym_name(p, SYM_TYPES, bit),
1638 sym_name(p, SYM_ROLES, upper->value - 1));
1647 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1649 struct type_datum *upper;
1650 struct policydb *p = datap;
1654 while (upper->bounds) {
1655 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1656 printk(KERN_ERR "SELinux: type %s: "
1657 "too deep or looped boundary\n",
1662 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1666 if (upper->attribute) {
1667 printk(KERN_ERR "SELinux: type %s: "
1668 "bounded by attribute %s",
1670 sym_name(p, SYM_TYPES, upper->value - 1));
1678 static int policydb_bounds_sanity_check(struct policydb *p)
1682 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1685 rc = hashtab_map(p->p_users.table,
1686 user_bounds_sanity_check, p);
1690 rc = hashtab_map(p->p_roles.table,
1691 role_bounds_sanity_check, p);
1695 rc = hashtab_map(p->p_types.table,
1696 type_bounds_sanity_check, p);
1703 extern int ss_initialized;
1705 u16 string_to_security_class(struct policydb *p, const char *name)
1707 struct class_datum *cladatum;
1709 cladatum = hashtab_search(p->p_classes.table, name);
1713 return cladatum->value;
1716 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1718 struct class_datum *cladatum;
1719 struct perm_datum *perdatum = NULL;
1720 struct common_datum *comdatum;
1722 if (!tclass || tclass > p->p_classes.nprim)
1725 cladatum = p->class_val_to_struct[tclass-1];
1726 comdatum = cladatum->comdatum;
1728 perdatum = hashtab_search(comdatum->permissions.table,
1731 perdatum = hashtab_search(cladatum->permissions.table,
1736 return 1U << (perdatum->value-1);
1739 static int range_read(struct policydb *p, void *fp)
1741 struct range_trans *rt = NULL;
1742 struct mls_range *r = NULL;
1747 if (p->policyvers < POLICYDB_VERSION_MLS)
1750 rc = next_entry(buf, fp, sizeof(u32));
1754 nel = le32_to_cpu(buf[0]);
1755 for (i = 0; i < nel; i++) {
1757 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1761 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1765 rt->source_type = le32_to_cpu(buf[0]);
1766 rt->target_type = le32_to_cpu(buf[1]);
1767 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1768 rc = next_entry(buf, fp, sizeof(u32));
1771 rt->target_class = le32_to_cpu(buf[0]);
1773 rt->target_class = p->process_class;
1776 if (!policydb_type_isvalid(p, rt->source_type) ||
1777 !policydb_type_isvalid(p, rt->target_type) ||
1778 !policydb_class_isvalid(p, rt->target_class))
1782 r = kzalloc(sizeof(*r), GFP_KERNEL);
1786 rc = mls_read_range_helper(r, fp);
1791 if (!mls_range_isvalid(p, r)) {
1792 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1796 rc = hashtab_insert(p->range_tr, rt, r);
1803 rangetr_hash_eval(p->range_tr);
1811 static int filename_trans_read(struct policydb *p, void *fp)
1813 struct filename_trans *ft, *last;
1819 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1822 rc = next_entry(buf, fp, sizeof(u32));
1825 nel = le32_to_cpu(buf[0]);
1827 printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1829 last = p->filename_trans;
1830 while (last && last->next)
1833 for (i = 0; i < nel; i++) {
1835 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1839 /* add it to the tail of the list */
1841 p->filename_trans = ft;
1846 /* length of the path component string */
1847 rc = next_entry(buf, fp, sizeof(u32));
1850 len = le32_to_cpu(buf[0]);
1853 name = kmalloc(len + 1, GFP_KERNEL);
1859 /* path component string */
1860 rc = next_entry(name, fp, len);
1865 printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1867 rc = next_entry(buf, fp, sizeof(u32) * 4);
1871 ft->stype = le32_to_cpu(buf[0]);
1872 ft->ttype = le32_to_cpu(buf[1]);
1873 ft->tclass = le32_to_cpu(buf[2]);
1874 ft->otype = le32_to_cpu(buf[3]);
1881 static int genfs_read(struct policydb *p, void *fp)
1884 u32 nel, nel2, len, len2;
1886 struct ocontext *l, *c;
1887 struct ocontext *newc = NULL;
1888 struct genfs *genfs_p, *genfs;
1889 struct genfs *newgenfs = NULL;
1891 rc = next_entry(buf, fp, sizeof(u32));
1894 nel = le32_to_cpu(buf[0]);
1896 for (i = 0; i < nel; i++) {
1897 rc = next_entry(buf, fp, sizeof(u32));
1900 len = le32_to_cpu(buf[0]);
1903 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1908 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1909 if (!newgenfs->fstype)
1912 rc = next_entry(newgenfs->fstype, fp, len);
1916 newgenfs->fstype[len] = 0;
1918 for (genfs_p = NULL, genfs = p->genfs; genfs;
1919 genfs_p = genfs, genfs = genfs->next) {
1921 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1922 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1926 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1929 newgenfs->next = genfs;
1931 genfs_p->next = newgenfs;
1933 p->genfs = newgenfs;
1937 rc = next_entry(buf, fp, sizeof(u32));
1941 nel2 = le32_to_cpu(buf[0]);
1942 for (j = 0; j < nel2; j++) {
1943 rc = next_entry(buf, fp, sizeof(u32));
1946 len = le32_to_cpu(buf[0]);
1949 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1954 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1958 rc = next_entry(newc->u.name, fp, len);
1961 newc->u.name[len] = 0;
1963 rc = next_entry(buf, fp, sizeof(u32));
1967 newc->v.sclass = le32_to_cpu(buf[0]);
1968 rc = context_read_and_validate(&newc->context[0], p, fp);
1972 for (l = NULL, c = genfs->head; c;
1973 l = c, c = c->next) {
1975 if (!strcmp(newc->u.name, c->u.name) &&
1976 (!c->v.sclass || !newc->v.sclass ||
1977 newc->v.sclass == c->v.sclass)) {
1978 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1979 genfs->fstype, c->u.name);
1982 len = strlen(newc->u.name);
1983 len2 = strlen(c->u.name);
1999 kfree(newgenfs->fstype);
2001 ocontext_destroy(newc, OCON_FSUSE);
2006 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2012 struct ocontext *l, *c;
2015 for (i = 0; i < info->ocon_num; i++) {
2016 rc = next_entry(buf, fp, sizeof(u32));
2019 nel = le32_to_cpu(buf[0]);
2022 for (j = 0; j < nel; j++) {
2024 c = kzalloc(sizeof(*c), GFP_KERNEL);
2030 p->ocontexts[i] = c;
2035 rc = next_entry(buf, fp, sizeof(u32));
2039 c->sid[0] = le32_to_cpu(buf[0]);
2040 rc = context_read_and_validate(&c->context[0], p, fp);
2046 rc = next_entry(buf, fp, sizeof(u32));
2049 len = le32_to_cpu(buf[0]);
2052 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2056 rc = next_entry(c->u.name, fp, len);
2061 rc = context_read_and_validate(&c->context[0], p, fp);
2064 rc = context_read_and_validate(&c->context[1], p, fp);
2069 rc = next_entry(buf, fp, sizeof(u32)*3);
2072 c->u.port.protocol = le32_to_cpu(buf[0]);
2073 c->u.port.low_port = le32_to_cpu(buf[1]);
2074 c->u.port.high_port = le32_to_cpu(buf[2]);
2075 rc = context_read_and_validate(&c->context[0], p, fp);
2080 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2083 c->u.node.addr = nodebuf[0]; /* network order */
2084 c->u.node.mask = nodebuf[1]; /* network order */
2085 rc = context_read_and_validate(&c->context[0], p, fp);
2090 rc = next_entry(buf, fp, sizeof(u32)*2);
2095 c->v.behavior = le32_to_cpu(buf[0]);
2096 if (c->v.behavior > SECURITY_FS_USE_NONE)
2100 len = le32_to_cpu(buf[1]);
2101 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2105 rc = next_entry(c->u.name, fp, len);
2109 rc = context_read_and_validate(&c->context[0], p, fp);
2116 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2119 for (k = 0; k < 4; k++)
2120 c->u.node6.addr[k] = nodebuf[k];
2121 for (k = 0; k < 4; k++)
2122 c->u.node6.mask[k] = nodebuf[k+4];
2123 rc = context_read_and_validate(&c->context[0], p, fp);
2137 * Read the configuration data from a policy database binary
2138 * representation file into a policy database structure.
2140 int policydb_read(struct policydb *p, void *fp)
2142 struct role_allow *ra, *lra;
2143 struct role_trans *tr, *ltr;
2146 u32 len, nprim, nel;
2149 struct policydb_compat_info *info;
2151 rc = policydb_init(p);
2155 /* Read the magic number and string length. */
2156 rc = next_entry(buf, fp, sizeof(u32) * 2);
2161 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2162 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2163 "not match expected magic number 0x%x\n",
2164 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2169 len = le32_to_cpu(buf[1]);
2170 if (len != strlen(POLICYDB_STRING)) {
2171 printk(KERN_ERR "SELinux: policydb string length %d does not "
2172 "match expected length %Zu\n",
2173 len, strlen(POLICYDB_STRING));
2178 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2179 if (!policydb_str) {
2180 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2181 "string of length %d\n", len);
2185 rc = next_entry(policydb_str, fp, len);
2187 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2188 kfree(policydb_str);
2193 policydb_str[len] = '\0';
2194 if (strcmp(policydb_str, POLICYDB_STRING)) {
2195 printk(KERN_ERR "SELinux: policydb string %s does not match "
2196 "my string %s\n", policydb_str, POLICYDB_STRING);
2197 kfree(policydb_str);
2200 /* Done with policydb_str. */
2201 kfree(policydb_str);
2202 policydb_str = NULL;
2204 /* Read the version and table sizes. */
2205 rc = next_entry(buf, fp, sizeof(u32)*4);
2210 p->policyvers = le32_to_cpu(buf[0]);
2211 if (p->policyvers < POLICYDB_VERSION_MIN ||
2212 p->policyvers > POLICYDB_VERSION_MAX) {
2213 printk(KERN_ERR "SELinux: policydb version %d does not match "
2214 "my version range %d-%d\n",
2215 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2219 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2223 if (p->policyvers < POLICYDB_VERSION_MLS) {
2224 printk(KERN_ERR "SELinux: security policydb version %d "
2225 "(MLS) not backwards compatible\n",
2230 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2231 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2233 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2234 rc = ebitmap_read(&p->policycaps, fp);
2239 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2240 rc = ebitmap_read(&p->permissive_map, fp);
2246 info = policydb_lookup_compat(p->policyvers);
2248 printk(KERN_ERR "SELinux: unable to find policy compat info "
2249 "for version %d\n", p->policyvers);
2254 if (le32_to_cpu(buf[2]) != info->sym_num ||
2255 le32_to_cpu(buf[3]) != info->ocon_num) {
2256 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2257 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2258 le32_to_cpu(buf[3]),
2259 info->sym_num, info->ocon_num);
2263 for (i = 0; i < info->sym_num; i++) {
2264 rc = next_entry(buf, fp, sizeof(u32)*2);
2267 nprim = le32_to_cpu(buf[0]);
2268 nel = le32_to_cpu(buf[1]);
2269 for (j = 0; j < nel; j++) {
2270 rc = read_f[i](p, p->symtab[i].table, fp);
2275 p->symtab[i].nprim = nprim;
2279 p->process_class = string_to_security_class(p, "process");
2280 if (!p->process_class)
2283 rc = avtab_read(&p->te_avtab, fp, p);
2287 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2288 rc = cond_read_list(p, fp);
2293 rc = next_entry(buf, fp, sizeof(u32));
2296 nel = le32_to_cpu(buf[0]);
2298 for (i = 0; i < nel; i++) {
2300 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2307 rc = next_entry(buf, fp, sizeof(u32)*3);
2312 tr->role = le32_to_cpu(buf[0]);
2313 tr->type = le32_to_cpu(buf[1]);
2314 tr->new_role = le32_to_cpu(buf[2]);
2315 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2316 rc = next_entry(buf, fp, sizeof(u32));
2319 tr->tclass = le32_to_cpu(buf[0]);
2321 tr->tclass = p->process_class;
2323 if (!policydb_role_isvalid(p, tr->role) ||
2324 !policydb_type_isvalid(p, tr->type) ||
2325 !policydb_class_isvalid(p, tr->tclass) ||
2326 !policydb_role_isvalid(p, tr->new_role))
2331 rc = next_entry(buf, fp, sizeof(u32));
2334 nel = le32_to_cpu(buf[0]);
2336 for (i = 0; i < nel; i++) {
2338 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2345 rc = next_entry(buf, fp, sizeof(u32)*2);
2350 ra->role = le32_to_cpu(buf[0]);
2351 ra->new_role = le32_to_cpu(buf[1]);
2352 if (!policydb_role_isvalid(p, ra->role) ||
2353 !policydb_role_isvalid(p, ra->new_role))
2358 rc = filename_trans_read(p, fp);
2362 rc = policydb_index(p);
2367 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2368 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2369 if (!p->process_trans_perms)
2372 rc = ocontext_read(p, info, fp);
2376 rc = genfs_read(p, fp);
2380 rc = range_read(p, fp);
2385 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2387 GFP_KERNEL | __GFP_ZERO);
2388 if (!p->type_attr_map_array)
2391 /* preallocate so we don't have to worry about the put ever failing */
2392 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2393 GFP_KERNEL | __GFP_ZERO);
2397 for (i = 0; i < p->p_types.nprim; i++) {
2398 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2402 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2403 rc = ebitmap_read(e, fp);
2407 /* add the type itself as the degenerate case */
2408 rc = ebitmap_set_bit(e, i, 1);
2413 rc = policydb_bounds_sanity_check(p);
2421 policydb_destroy(p);
2426 * Write a MLS level structure to a policydb binary
2427 * representation file.
2429 static int mls_write_level(struct mls_level *l, void *fp)
2434 buf[0] = cpu_to_le32(l->sens);
2435 rc = put_entry(buf, sizeof(u32), 1, fp);
2439 rc = ebitmap_write(&l->cat, fp);
2447 * Write a MLS range structure to a policydb binary
2448 * representation file.
2450 static int mls_write_range_helper(struct mls_range *r, void *fp)
2456 eq = mls_level_eq(&r->level[1], &r->level[0]);
2462 buf[0] = cpu_to_le32(items-1);
2463 buf[1] = cpu_to_le32(r->level[0].sens);
2465 buf[2] = cpu_to_le32(r->level[1].sens);
2467 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2469 rc = put_entry(buf, sizeof(u32), items, fp);
2473 rc = ebitmap_write(&r->level[0].cat, fp);
2477 rc = ebitmap_write(&r->level[1].cat, fp);
2485 static int sens_write(void *vkey, void *datum, void *ptr)
2488 struct level_datum *levdatum = datum;
2489 struct policy_data *pd = ptr;
2496 buf[0] = cpu_to_le32(len);
2497 buf[1] = cpu_to_le32(levdatum->isalias);
2498 rc = put_entry(buf, sizeof(u32), 2, fp);
2502 rc = put_entry(key, 1, len, fp);
2506 rc = mls_write_level(levdatum->level, fp);
2513 static int cat_write(void *vkey, void *datum, void *ptr)
2516 struct cat_datum *catdatum = datum;
2517 struct policy_data *pd = ptr;
2524 buf[0] = cpu_to_le32(len);
2525 buf[1] = cpu_to_le32(catdatum->value);
2526 buf[2] = cpu_to_le32(catdatum->isalias);
2527 rc = put_entry(buf, sizeof(u32), 3, fp);
2531 rc = put_entry(key, 1, len, fp);
2538 static int role_trans_write(struct policydb *p, void *fp)
2540 struct role_trans *r = p->role_tr;
2541 struct role_trans *tr;
2547 for (tr = r; tr; tr = tr->next)
2549 buf[0] = cpu_to_le32(nel);
2550 rc = put_entry(buf, sizeof(u32), 1, fp);
2553 for (tr = r; tr; tr = tr->next) {
2554 buf[0] = cpu_to_le32(tr->role);
2555 buf[1] = cpu_to_le32(tr->type);
2556 buf[2] = cpu_to_le32(tr->new_role);
2557 rc = put_entry(buf, sizeof(u32), 3, fp);
2560 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2561 buf[0] = cpu_to_le32(tr->tclass);
2562 rc = put_entry(buf, sizeof(u32), 1, fp);
2571 static int role_allow_write(struct role_allow *r, void *fp)
2573 struct role_allow *ra;
2579 for (ra = r; ra; ra = ra->next)
2581 buf[0] = cpu_to_le32(nel);
2582 rc = put_entry(buf, sizeof(u32), 1, fp);
2585 for (ra = r; ra; ra = ra->next) {
2586 buf[0] = cpu_to_le32(ra->role);
2587 buf[1] = cpu_to_le32(ra->new_role);
2588 rc = put_entry(buf, sizeof(u32), 2, fp);
2596 * Write a security context structure
2597 * to a policydb binary representation file.
2599 static int context_write(struct policydb *p, struct context *c,
2605 buf[0] = cpu_to_le32(c->user);
2606 buf[1] = cpu_to_le32(c->role);
2607 buf[2] = cpu_to_le32(c->type);
2609 rc = put_entry(buf, sizeof(u32), 3, fp);
2613 rc = mls_write_range_helper(&c->range, fp);
2621 * The following *_write functions are used to
2622 * write the symbol data to a policy database
2623 * binary representation file.
2626 static int perm_write(void *vkey, void *datum, void *fp)
2629 struct perm_datum *perdatum = datum;
2635 buf[0] = cpu_to_le32(len);
2636 buf[1] = cpu_to_le32(perdatum->value);
2637 rc = put_entry(buf, sizeof(u32), 2, fp);
2641 rc = put_entry(key, 1, len, fp);
2648 static int common_write(void *vkey, void *datum, void *ptr)
2651 struct common_datum *comdatum = datum;
2652 struct policy_data *pd = ptr;
2659 buf[0] = cpu_to_le32(len);
2660 buf[1] = cpu_to_le32(comdatum->value);
2661 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2662 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2663 rc = put_entry(buf, sizeof(u32), 4, fp);
2667 rc = put_entry(key, 1, len, fp);
2671 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2678 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2681 struct constraint_node *c;
2682 struct constraint_expr *e;
2687 for (c = node; c; c = c->next) {
2689 for (e = c->expr; e; e = e->next)
2691 buf[0] = cpu_to_le32(c->permissions);
2692 buf[1] = cpu_to_le32(nel);
2693 rc = put_entry(buf, sizeof(u32), 2, fp);
2696 for (e = c->expr; e; e = e->next) {
2697 buf[0] = cpu_to_le32(e->expr_type);
2698 buf[1] = cpu_to_le32(e->attr);
2699 buf[2] = cpu_to_le32(e->op);
2700 rc = put_entry(buf, sizeof(u32), 3, fp);
2704 switch (e->expr_type) {
2706 rc = ebitmap_write(&e->names, fp);
2719 static int class_write(void *vkey, void *datum, void *ptr)
2722 struct class_datum *cladatum = datum;
2723 struct policy_data *pd = ptr;
2725 struct policydb *p = pd->p;
2726 struct constraint_node *c;
2733 if (cladatum->comkey)
2734 len2 = strlen(cladatum->comkey);
2739 for (c = cladatum->constraints; c; c = c->next)
2742 buf[0] = cpu_to_le32(len);
2743 buf[1] = cpu_to_le32(len2);
2744 buf[2] = cpu_to_le32(cladatum->value);
2745 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2746 if (cladatum->permissions.table)
2747 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2750 buf[5] = cpu_to_le32(ncons);
2751 rc = put_entry(buf, sizeof(u32), 6, fp);
2755 rc = put_entry(key, 1, len, fp);
2759 if (cladatum->comkey) {
2760 rc = put_entry(cladatum->comkey, 1, len2, fp);
2765 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2769 rc = write_cons_helper(p, cladatum->constraints, fp);
2773 /* write out the validatetrans rule */
2775 for (c = cladatum->validatetrans; c; c = c->next)
2778 buf[0] = cpu_to_le32(ncons);
2779 rc = put_entry(buf, sizeof(u32), 1, fp);
2783 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2790 static int role_write(void *vkey, void *datum, void *ptr)
2793 struct role_datum *role = datum;
2794 struct policy_data *pd = ptr;
2796 struct policydb *p = pd->p;
2803 buf[items++] = cpu_to_le32(len);
2804 buf[items++] = cpu_to_le32(role->value);
2805 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2806 buf[items++] = cpu_to_le32(role->bounds);
2808 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2810 rc = put_entry(buf, sizeof(u32), items, fp);
2814 rc = put_entry(key, 1, len, fp);
2818 rc = ebitmap_write(&role->dominates, fp);
2822 rc = ebitmap_write(&role->types, fp);
2829 static int type_write(void *vkey, void *datum, void *ptr)
2832 struct type_datum *typdatum = datum;
2833 struct policy_data *pd = ptr;
2834 struct policydb *p = pd->p;
2842 buf[items++] = cpu_to_le32(len);
2843 buf[items++] = cpu_to_le32(typdatum->value);
2844 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2847 if (typdatum->primary)
2848 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2850 if (typdatum->attribute)
2851 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2853 buf[items++] = cpu_to_le32(properties);
2854 buf[items++] = cpu_to_le32(typdatum->bounds);
2856 buf[items++] = cpu_to_le32(typdatum->primary);
2858 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2859 rc = put_entry(buf, sizeof(u32), items, fp);
2863 rc = put_entry(key, 1, len, fp);
2870 static int user_write(void *vkey, void *datum, void *ptr)
2873 struct user_datum *usrdatum = datum;
2874 struct policy_data *pd = ptr;
2875 struct policydb *p = pd->p;
2883 buf[items++] = cpu_to_le32(len);
2884 buf[items++] = cpu_to_le32(usrdatum->value);
2885 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2886 buf[items++] = cpu_to_le32(usrdatum->bounds);
2887 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2888 rc = put_entry(buf, sizeof(u32), items, fp);
2892 rc = put_entry(key, 1, len, fp);
2896 rc = ebitmap_write(&usrdatum->roles, fp);
2900 rc = mls_write_range_helper(&usrdatum->range, fp);
2904 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2911 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2924 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2927 unsigned int i, j, rc;
2932 for (i = 0; i < info->ocon_num; i++) {
2934 for (c = p->ocontexts[i]; c; c = c->next)
2936 buf[0] = cpu_to_le32(nel);
2937 rc = put_entry(buf, sizeof(u32), 1, fp);
2940 for (c = p->ocontexts[i]; c; c = c->next) {
2943 buf[0] = cpu_to_le32(c->sid[0]);
2944 rc = put_entry(buf, sizeof(u32), 1, fp);
2947 rc = context_write(p, &c->context[0], fp);
2953 len = strlen(c->u.name);
2954 buf[0] = cpu_to_le32(len);
2955 rc = put_entry(buf, sizeof(u32), 1, fp);
2958 rc = put_entry(c->u.name, 1, len, fp);
2961 rc = context_write(p, &c->context[0], fp);
2964 rc = context_write(p, &c->context[1], fp);
2969 buf[0] = cpu_to_le32(c->u.port.protocol);
2970 buf[1] = cpu_to_le32(c->u.port.low_port);
2971 buf[2] = cpu_to_le32(c->u.port.high_port);
2972 rc = put_entry(buf, sizeof(u32), 3, fp);
2975 rc = context_write(p, &c->context[0], fp);
2980 nodebuf[0] = c->u.node.addr; /* network order */
2981 nodebuf[1] = c->u.node.mask; /* network order */
2982 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2985 rc = context_write(p, &c->context[0], fp);
2990 buf[0] = cpu_to_le32(c->v.behavior);
2991 len = strlen(c->u.name);
2992 buf[1] = cpu_to_le32(len);
2993 rc = put_entry(buf, sizeof(u32), 2, fp);
2996 rc = put_entry(c->u.name, 1, len, fp);
2999 rc = context_write(p, &c->context[0], fp);
3004 for (j = 0; j < 4; j++)
3005 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3006 for (j = 0; j < 4; j++)
3007 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3008 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3011 rc = context_write(p, &c->context[0], fp);
3021 static int genfs_write(struct policydb *p, void *fp)
3023 struct genfs *genfs;
3030 for (genfs = p->genfs; genfs; genfs = genfs->next)
3032 buf[0] = cpu_to_le32(len);
3033 rc = put_entry(buf, sizeof(u32), 1, fp);
3036 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3037 len = strlen(genfs->fstype);
3038 buf[0] = cpu_to_le32(len);
3039 rc = put_entry(buf, sizeof(u32), 1, fp);
3042 rc = put_entry(genfs->fstype, 1, len, fp);
3046 for (c = genfs->head; c; c = c->next)
3048 buf[0] = cpu_to_le32(len);
3049 rc = put_entry(buf, sizeof(u32), 1, fp);
3052 for (c = genfs->head; c; c = c->next) {
3053 len = strlen(c->u.name);
3054 buf[0] = cpu_to_le32(len);
3055 rc = put_entry(buf, sizeof(u32), 1, fp);
3058 rc = put_entry(c->u.name, 1, len, fp);
3061 buf[0] = cpu_to_le32(c->v.sclass);
3062 rc = put_entry(buf, sizeof(u32), 1, fp);
3065 rc = context_write(p, &c->context[0], fp);
3073 static int range_count(void *key, void *data, void *ptr)
3081 static int range_write_helper(void *key, void *data, void *ptr)
3084 struct range_trans *rt = key;
3085 struct mls_range *r = data;
3086 struct policy_data *pd = ptr;
3088 struct policydb *p = pd->p;
3091 buf[0] = cpu_to_le32(rt->source_type);
3092 buf[1] = cpu_to_le32(rt->target_type);
3093 rc = put_entry(buf, sizeof(u32), 2, fp);
3096 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3097 buf[0] = cpu_to_le32(rt->target_class);
3098 rc = put_entry(buf, sizeof(u32), 1, fp);
3102 rc = mls_write_range_helper(r, fp);
3109 static int range_write(struct policydb *p, void *fp)
3114 struct policy_data pd;
3119 /* count the number of entries in the hashtab */
3121 rc = hashtab_map(p->range_tr, range_count, &nel);
3125 buf[0] = cpu_to_le32(nel);
3126 rc = put_entry(buf, sizeof(u32), 1, fp);
3130 /* actually write all of the entries */
3131 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3138 static int filename_trans_write(struct policydb *p, void *fp)
3140 struct filename_trans *ft;
3145 for (ft = p->filename_trans; ft; ft = ft->next)
3148 buf[0] = cpu_to_le32(nel);
3149 rc = put_entry(buf, sizeof(u32), 1, fp);
3153 for (ft = p->filename_trans; ft; ft = ft->next) {
3154 len = strlen(ft->name);
3155 buf[0] = cpu_to_le32(len);
3156 rc = put_entry(buf, sizeof(u32), 1, fp);
3160 rc = put_entry(ft->name, sizeof(char), len, fp);
3166 buf[2] = ft->tclass;
3169 rc = put_entry(buf, sizeof(u32), 4, fp);
3176 * Write the configuration data in a policy database
3177 * structure to a policy database binary representation
3180 int policydb_write(struct policydb *p, void *fp)
3182 unsigned int i, num_syms;
3187 struct policydb_compat_info *info;
3190 * refuse to write policy older than compressed avtab
3191 * to simplify the writer. There are other tests dropped
3192 * since we assume this throughout the writer code. Be
3193 * careful if you ever try to remove this restriction
3195 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3196 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3197 " Because it is less than version %d\n", p->policyvers,
3198 POLICYDB_VERSION_AVTAB);
3204 config |= POLICYDB_CONFIG_MLS;
3206 if (p->reject_unknown)
3207 config |= REJECT_UNKNOWN;
3208 if (p->allow_unknown)
3209 config |= ALLOW_UNKNOWN;
3211 /* Write the magic number and string identifiers. */
3212 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3213 len = strlen(POLICYDB_STRING);
3214 buf[1] = cpu_to_le32(len);
3215 rc = put_entry(buf, sizeof(u32), 2, fp);
3218 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3222 /* Write the version, config, and table sizes. */
3223 info = policydb_lookup_compat(p->policyvers);
3225 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3226 "version %d", p->policyvers);
3230 buf[0] = cpu_to_le32(p->policyvers);
3231 buf[1] = cpu_to_le32(config);
3232 buf[2] = cpu_to_le32(info->sym_num);
3233 buf[3] = cpu_to_le32(info->ocon_num);
3235 rc = put_entry(buf, sizeof(u32), 4, fp);
3239 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3240 rc = ebitmap_write(&p->policycaps, fp);
3245 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3246 rc = ebitmap_write(&p->permissive_map, fp);
3251 num_syms = info->sym_num;
3252 for (i = 0; i < num_syms; i++) {
3253 struct policy_data pd;
3258 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3259 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3261 rc = put_entry(buf, sizeof(u32), 2, fp);
3264 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3269 rc = avtab_write(p, &p->te_avtab, fp);
3273 rc = cond_write_list(p, p->cond_list, fp);
3277 rc = role_trans_write(p, fp);
3281 rc = role_allow_write(p->role_allow, fp);
3285 rc = filename_trans_write(p, fp);
3289 rc = ocontext_write(p, info, fp);
3293 rc = genfs_write(p, fp);
3297 rc = range_write(p, fp);
3301 for (i = 0; i < p->p_types.nprim; i++) {
3302 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3305 rc = ebitmap_write(e, fp);