1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
11 * Support for enhanced MLS infrastructure.
15 * Added conditional policy language extensions
19 * Added support for the policy capability bitmap
21 * Update: Mellanox Techonologies
23 * Added Infiniband support
25 * Copyright (C) 2016 Mellanox Techonologies
26 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
27 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
28 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/audit.h>
40 #include "conditional.h"
44 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
45 static const char *const symtab_name[SYM_NUM] = {
57 struct policydb_compat_info {
60 unsigned int ocon_num;
63 /* These need to be updated if SYM_NUM or OCON_NUM changes */
64 static const struct policydb_compat_info policydb_compat[] = {
66 .version = POLICYDB_VERSION_BASE,
67 .sym_num = SYM_NUM - 3,
68 .ocon_num = OCON_NUM - 3,
71 .version = POLICYDB_VERSION_BOOL,
72 .sym_num = SYM_NUM - 2,
73 .ocon_num = OCON_NUM - 3,
76 .version = POLICYDB_VERSION_IPV6,
77 .sym_num = SYM_NUM - 2,
78 .ocon_num = OCON_NUM - 2,
81 .version = POLICYDB_VERSION_NLCLASS,
82 .sym_num = SYM_NUM - 2,
83 .ocon_num = OCON_NUM - 2,
86 .version = POLICYDB_VERSION_MLS,
88 .ocon_num = OCON_NUM - 2,
91 .version = POLICYDB_VERSION_AVTAB,
93 .ocon_num = OCON_NUM - 2,
96 .version = POLICYDB_VERSION_RANGETRANS,
98 .ocon_num = OCON_NUM - 2,
101 .version = POLICYDB_VERSION_POLCAP,
103 .ocon_num = OCON_NUM - 2,
106 .version = POLICYDB_VERSION_PERMISSIVE,
108 .ocon_num = OCON_NUM - 2,
111 .version = POLICYDB_VERSION_BOUNDARY,
113 .ocon_num = OCON_NUM - 2,
116 .version = POLICYDB_VERSION_FILENAME_TRANS,
118 .ocon_num = OCON_NUM - 2,
121 .version = POLICYDB_VERSION_ROLETRANS,
123 .ocon_num = OCON_NUM - 2,
126 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
128 .ocon_num = OCON_NUM - 2,
131 .version = POLICYDB_VERSION_DEFAULT_TYPE,
133 .ocon_num = OCON_NUM - 2,
136 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
138 .ocon_num = OCON_NUM - 2,
141 .version = POLICYDB_VERSION_XPERMS_IOCTL,
143 .ocon_num = OCON_NUM - 2,
146 .version = POLICYDB_VERSION_INFINIBAND,
148 .ocon_num = OCON_NUM,
151 .version = POLICYDB_VERSION_GLBLUB,
153 .ocon_num = OCON_NUM,
156 .version = POLICYDB_VERSION_COMP_FTRANS,
158 .ocon_num = OCON_NUM,
162 static const struct policydb_compat_info *policydb_lookup_compat(unsigned int version)
166 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
167 if (policydb_compat[i].version == version)
168 return &policydb_compat[i];
175 * The following *_destroy functions are used to
176 * free any memory allocated for each kind of
177 * symbol data in the policy database.
180 static int perm_destroy(void *key, void *datum, void *p)
187 static int common_destroy(void *key, void *datum, void *p)
189 struct common_datum *comdatum;
194 hashtab_map(&comdatum->permissions.table, perm_destroy, NULL);
195 hashtab_destroy(&comdatum->permissions.table);
201 static void constraint_expr_destroy(struct constraint_expr *expr)
204 ebitmap_destroy(&expr->names);
205 if (expr->type_names) {
206 ebitmap_destroy(&expr->type_names->types);
207 ebitmap_destroy(&expr->type_names->negset);
208 kfree(expr->type_names);
214 static int cls_destroy(void *key, void *datum, void *p)
216 struct class_datum *cladatum;
217 struct constraint_node *constraint, *ctemp;
218 struct constraint_expr *e, *etmp;
223 hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
224 hashtab_destroy(&cladatum->permissions.table);
225 constraint = cladatum->constraints;
227 e = constraint->expr;
231 constraint_expr_destroy(etmp);
234 constraint = constraint->next;
238 constraint = cladatum->validatetrans;
240 e = constraint->expr;
244 constraint_expr_destroy(etmp);
247 constraint = constraint->next;
250 kfree(cladatum->comkey);
256 static int role_destroy(void *key, void *datum, void *p)
258 struct role_datum *role;
263 ebitmap_destroy(&role->dominates);
264 ebitmap_destroy(&role->types);
270 static int type_destroy(void *key, void *datum, void *p)
277 static int user_destroy(void *key, void *datum, void *p)
279 struct user_datum *usrdatum;
284 ebitmap_destroy(&usrdatum->roles);
285 ebitmap_destroy(&usrdatum->range.level[0].cat);
286 ebitmap_destroy(&usrdatum->range.level[1].cat);
287 ebitmap_destroy(&usrdatum->dfltlevel.cat);
293 static int sens_destroy(void *key, void *datum, void *p)
295 struct level_datum *levdatum;
301 ebitmap_destroy(&levdatum->level->cat);
302 kfree(levdatum->level);
308 static int cat_destroy(void *key, void *datum, void *p)
315 static int (*const destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
326 static int filenametr_destroy(void *key, void *datum, void *p)
328 struct filename_trans_key *ft = key;
329 struct filename_trans_datum *next, *d = datum;
334 ebitmap_destroy(&d->stypes);
338 } while (unlikely(d));
343 static int range_tr_destroy(void *key, void *datum, void *p)
345 struct mls_range *rt = datum;
348 ebitmap_destroy(&rt->level[0].cat);
349 ebitmap_destroy(&rt->level[1].cat);
355 static int role_tr_destroy(void *key, void *datum, void *p)
362 static void ocontext_destroy(struct ocontext *c, unsigned int i)
367 context_destroy(&c->context[0]);
368 context_destroy(&c->context[1]);
369 if (i == OCON_ISID || i == OCON_FS ||
370 i == OCON_NETIF || i == OCON_FSUSE)
376 * Initialize the role table.
378 static int roles_init(struct policydb *p)
382 struct role_datum *role;
384 role = kzalloc(sizeof(*role), GFP_KERNEL);
389 role->value = ++p->p_roles.nprim;
390 if (role->value != OBJECT_R_VAL)
394 key = kstrdup(OBJECT_R, GFP_KERNEL);
398 rc = symtab_insert(&p->p_roles, key, role);
409 static u32 filenametr_hash(const void *k)
411 const struct filename_trans_key *ft = k;
413 unsigned int byte_num;
416 hash = ft->ttype ^ ft->tclass;
419 while ((focus = ft->name[byte_num++]))
420 hash = partial_name_hash(focus, hash);
424 static int filenametr_cmp(const void *k1, const void *k2)
426 const struct filename_trans_key *ft1 = k1;
427 const struct filename_trans_key *ft2 = k2;
430 v = ft1->ttype - ft2->ttype;
434 v = ft1->tclass - ft2->tclass;
438 return strcmp(ft1->name, ft2->name);
442 static const struct hashtab_key_params filenametr_key_params = {
443 .hash = filenametr_hash,
444 .cmp = filenametr_cmp,
447 struct filename_trans_datum *policydb_filenametr_search(
448 struct policydb *p, struct filename_trans_key *key)
450 return hashtab_search(&p->filename_trans, key, filenametr_key_params);
453 static u32 rangetr_hash(const void *k)
455 const struct range_trans *key = k;
457 return key->source_type + (key->target_type << 3) +
458 (key->target_class << 5);
461 static int rangetr_cmp(const void *k1, const void *k2)
463 const struct range_trans *key1 = k1, *key2 = k2;
466 v = key1->source_type - key2->source_type;
470 v = key1->target_type - key2->target_type;
474 v = key1->target_class - key2->target_class;
479 static const struct hashtab_key_params rangetr_key_params = {
480 .hash = rangetr_hash,
484 struct mls_range *policydb_rangetr_search(struct policydb *p,
485 struct range_trans *key)
487 return hashtab_search(&p->range_tr, key, rangetr_key_params);
490 static u32 role_trans_hash(const void *k)
492 const struct role_trans_key *key = k;
494 return jhash_3words(key->role, key->type, (u32)key->tclass << 16 | key->tclass, 0);
497 static int role_trans_cmp(const void *k1, const void *k2)
499 const struct role_trans_key *key1 = k1, *key2 = k2;
502 v = key1->role - key2->role;
506 v = key1->type - key2->type;
510 return key1->tclass - key2->tclass;
513 static const struct hashtab_key_params roletr_key_params = {
514 .hash = role_trans_hash,
515 .cmp = role_trans_cmp,
518 struct role_trans_datum *policydb_roletr_search(struct policydb *p,
519 struct role_trans_key *key)
521 return hashtab_search(&p->role_tr, key, roletr_key_params);
525 * Initialize a policy database structure.
527 static void policydb_init(struct policydb *p)
529 memset(p, 0, sizeof(*p));
531 avtab_init(&p->te_avtab);
532 cond_policydb_init(p);
534 ebitmap_init(&p->filename_trans_ttypes);
535 ebitmap_init(&p->policycaps);
536 ebitmap_init(&p->permissive_map);
540 * The following *_index functions are used to
541 * define the val_to_name and val_to_struct arrays
542 * in a policy database structure. The val_to_name
543 * arrays are used when converting security context
544 * structures into string representations. The
545 * val_to_struct arrays are used when the attributes
546 * of a class, role, or user are needed.
549 static int common_index(void *key, void *datum, void *datap)
552 struct common_datum *comdatum;
556 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
559 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
564 static int class_index(void *key, void *datum, void *datap)
567 struct class_datum *cladatum;
571 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
574 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
575 p->class_val_to_struct[cladatum->value - 1] = cladatum;
579 static int role_index(void *key, void *datum, void *datap)
582 struct role_datum *role;
587 || role->value > p->p_roles.nprim
588 || role->bounds > p->p_roles.nprim)
591 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
592 p->role_val_to_struct[role->value - 1] = role;
596 static int type_index(void *key, void *datum, void *datap)
599 struct type_datum *typdatum;
604 if (typdatum->primary) {
606 || typdatum->value > p->p_types.nprim
607 || typdatum->bounds > p->p_types.nprim)
609 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
610 p->type_val_to_struct[typdatum->value - 1] = typdatum;
616 static int user_index(void *key, void *datum, void *datap)
619 struct user_datum *usrdatum;
624 || usrdatum->value > p->p_users.nprim
625 || usrdatum->bounds > p->p_users.nprim)
628 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
629 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
633 static int sens_index(void *key, void *datum, void *datap)
636 struct level_datum *levdatum;
641 if (!levdatum->isalias) {
642 if (!levdatum->level->sens ||
643 levdatum->level->sens > p->p_levels.nprim)
646 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
652 static int cat_index(void *key, void *datum, void *datap)
655 struct cat_datum *catdatum;
660 if (!catdatum->isalias) {
661 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
664 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
670 static int (*const index_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
681 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
682 static void hash_eval(struct hashtab *h, const char *hash_name)
684 struct hashtab_info info;
686 hashtab_stat(h, &info);
687 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d, sum of chain length^2 %llu\n",
688 hash_name, h->nel, info.slots_used, h->size,
689 info.max_chain_len, info.chain2_len_sum);
692 static void symtab_hash_eval(struct symtab *s)
696 for (i = 0; i < SYM_NUM; i++)
697 hash_eval(&s[i].table, symtab_name[i]);
701 static inline void hash_eval(struct hashtab *h, const char *hash_name)
704 static inline void symtab_hash_eval(struct symtab *s)
707 #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
710 * Define the other val_to_name and val_to_struct arrays
711 * in a policy database structure.
713 * Caller must clean up on failure.
715 static int policydb_index(struct policydb *p)
720 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
721 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
722 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
724 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
725 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
728 pr_debug("SELinux: %d classes, %d rules\n",
729 p->p_classes.nprim, p->te_avtab.nel);
731 avtab_hash_eval(&p->te_avtab, "rules");
732 symtab_hash_eval(p->symtab);
734 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
735 sizeof(*p->class_val_to_struct),
737 if (!p->class_val_to_struct)
740 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
741 sizeof(*p->role_val_to_struct),
743 if (!p->role_val_to_struct)
746 p->user_val_to_struct = kcalloc(p->p_users.nprim,
747 sizeof(*p->user_val_to_struct),
749 if (!p->user_val_to_struct)
752 p->type_val_to_struct = kvcalloc(p->p_types.nprim,
753 sizeof(*p->type_val_to_struct),
755 if (!p->type_val_to_struct)
758 rc = cond_init_bool_indexes(p);
762 for (i = 0; i < SYM_NUM; i++) {
763 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
766 if (!p->sym_val_to_name[i])
769 rc = hashtab_map(&p->symtab[i].table, index_f[i], p);
779 * Free any memory allocated by a policy database structure.
781 void policydb_destroy(struct policydb *p)
783 struct ocontext *c, *ctmp;
784 struct genfs *g, *gtmp;
786 struct role_allow *ra, *lra = NULL;
788 for (i = 0; i < SYM_NUM; i++) {
790 hashtab_map(&p->symtab[i].table, destroy_f[i], NULL);
791 hashtab_destroy(&p->symtab[i].table);
794 for (i = 0; i < SYM_NUM; i++)
795 kvfree(p->sym_val_to_name[i]);
797 kfree(p->class_val_to_struct);
798 kfree(p->role_val_to_struct);
799 kfree(p->user_val_to_struct);
800 kvfree(p->type_val_to_struct);
802 avtab_destroy(&p->te_avtab);
804 for (i = 0; i < OCON_NUM; i++) {
810 ocontext_destroy(ctmp, i);
812 p->ocontexts[i] = NULL;
823 ocontext_destroy(ctmp, OCON_FSUSE);
831 cond_policydb_destroy(p);
833 hashtab_map(&p->role_tr, role_tr_destroy, NULL);
834 hashtab_destroy(&p->role_tr);
836 for (ra = p->role_allow; ra; ra = ra->next) {
843 hashtab_map(&p->filename_trans, filenametr_destroy, NULL);
844 hashtab_destroy(&p->filename_trans);
846 hashtab_map(&p->range_tr, range_tr_destroy, NULL);
847 hashtab_destroy(&p->range_tr);
849 if (p->type_attr_map_array) {
850 for (i = 0; i < p->p_types.nprim; i++)
851 ebitmap_destroy(&p->type_attr_map_array[i]);
852 kvfree(p->type_attr_map_array);
855 ebitmap_destroy(&p->filename_trans_ttypes);
856 ebitmap_destroy(&p->policycaps);
857 ebitmap_destroy(&p->permissive_map);
861 * Load the initial SIDs specified in a policy database
862 * structure into a SID table.
864 int policydb_load_isids(struct policydb *p, struct sidtab *s)
866 struct ocontext *head, *c;
871 pr_err("SELinux: out of memory on SID table init\n");
875 head = p->ocontexts[OCON_ISID];
876 for (c = head; c; c = c->next) {
878 const char *name = security_get_initial_sid_context(sid);
880 if (sid == SECSID_NULL) {
881 pr_err("SELinux: SID 0 was assigned a context.\n");
886 /* Ignore initial SIDs unused by this kernel. */
890 rc = sidtab_set_initial(s, sid, &c->context[0]);
892 pr_err("SELinux: unable to load initial SID %s.\n",
901 int policydb_class_isvalid(struct policydb *p, unsigned int class)
903 if (!class || class > p->p_classes.nprim)
908 int policydb_role_isvalid(struct policydb *p, unsigned int role)
910 if (!role || role > p->p_roles.nprim)
915 int policydb_type_isvalid(struct policydb *p, unsigned int type)
917 if (!type || type > p->p_types.nprim)
923 * Return 1 if the fields in the security context
924 * structure `c' are valid. Return 0 otherwise.
926 int policydb_context_isvalid(struct policydb *p, struct context *c)
928 struct role_datum *role;
929 struct user_datum *usrdatum;
931 if (!c->role || c->role > p->p_roles.nprim)
934 if (!c->user || c->user > p->p_users.nprim)
937 if (!c->type || c->type > p->p_types.nprim)
940 if (c->role != OBJECT_R_VAL) {
942 * Role must be authorized for the type.
944 role = p->role_val_to_struct[c->role - 1];
945 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
946 /* role may not be associated with type */
950 * User must be authorized for the role.
952 usrdatum = p->user_val_to_struct[c->user - 1];
956 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
957 /* user may not be associated with role */
961 if (!mls_context_isvalid(p, c))
968 * Read a MLS range structure from a policydb binary
969 * representation file.
971 static int mls_read_range_helper(struct mls_range *r, void *fp)
977 rc = next_entry(buf, fp, sizeof(u32));
982 items = le32_to_cpu(buf[0]);
983 if (items > ARRAY_SIZE(buf)) {
984 pr_err("SELinux: mls: range overflow\n");
988 rc = next_entry(buf, fp, sizeof(u32) * items);
990 pr_err("SELinux: mls: truncated range\n");
994 r->level[0].sens = le32_to_cpu(buf[0]);
996 r->level[1].sens = le32_to_cpu(buf[1]);
998 r->level[1].sens = r->level[0].sens;
1000 rc = ebitmap_read(&r->level[0].cat, fp);
1002 pr_err("SELinux: mls: error reading low categories\n");
1006 rc = ebitmap_read(&r->level[1].cat, fp);
1008 pr_err("SELinux: mls: error reading high categories\n");
1012 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1014 pr_err("SELinux: mls: out of memory\n");
1021 ebitmap_destroy(&r->level[0].cat);
1027 * Read and validate a security context structure
1028 * from a policydb binary representation file.
1030 static int context_read_and_validate(struct context *c,
1037 rc = next_entry(buf, fp, sizeof buf);
1039 pr_err("SELinux: context truncated\n");
1042 c->user = le32_to_cpu(buf[0]);
1043 c->role = le32_to_cpu(buf[1]);
1044 c->type = le32_to_cpu(buf[2]);
1045 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1046 rc = mls_read_range_helper(&c->range, fp);
1048 pr_err("SELinux: error reading MLS range of context\n");
1054 if (!policydb_context_isvalid(p, c)) {
1055 pr_err("SELinux: invalid security context\n");
1065 * The following *_read functions are used to
1066 * read the symbol data from a policy database
1067 * binary representation file.
1070 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1075 if ((len == 0) || (len == (u32)-1))
1078 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1082 rc = next_entry(str, fp, len);
1093 static int perm_read(struct policydb *p, struct symtab *s, void *fp)
1096 struct perm_datum *perdatum;
1101 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1105 rc = next_entry(buf, fp, sizeof buf);
1109 len = le32_to_cpu(buf[0]);
1110 perdatum->value = le32_to_cpu(buf[1]);
1112 rc = str_read(&key, GFP_KERNEL, fp, len);
1116 rc = symtab_insert(s, key, perdatum);
1122 perm_destroy(key, perdatum, NULL);
1126 static int common_read(struct policydb *p, struct symtab *s, void *fp)
1129 struct common_datum *comdatum;
1134 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1138 rc = next_entry(buf, fp, sizeof buf);
1142 len = le32_to_cpu(buf[0]);
1143 comdatum->value = le32_to_cpu(buf[1]);
1144 nel = le32_to_cpu(buf[3]);
1146 rc = symtab_init(&comdatum->permissions, nel);
1149 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1151 rc = str_read(&key, GFP_KERNEL, fp, len);
1155 for (i = 0; i < nel; i++) {
1156 rc = perm_read(p, &comdatum->permissions, fp);
1161 rc = symtab_insert(s, key, comdatum);
1166 common_destroy(key, comdatum, NULL);
1170 static void type_set_init(struct type_set *t)
1172 ebitmap_init(&t->types);
1173 ebitmap_init(&t->negset);
1176 static int type_set_read(struct type_set *t, void *fp)
1181 if (ebitmap_read(&t->types, fp))
1183 if (ebitmap_read(&t->negset, fp))
1186 rc = next_entry(buf, fp, sizeof(u32));
1189 t->flags = le32_to_cpu(buf[0]);
1195 static int read_cons_helper(struct policydb *p,
1196 struct constraint_node **nodep,
1197 u32 ncons, int allowxtarget, void *fp)
1199 struct constraint_node *c, *lc;
1200 struct constraint_expr *e, *le;
1206 for (i = 0; i < ncons; i++) {
1207 c = kzalloc(sizeof(*c), GFP_KERNEL);
1216 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1219 c->permissions = le32_to_cpu(buf[0]);
1220 nexpr = le32_to_cpu(buf[1]);
1223 for (j = 0; j < nexpr; j++) {
1224 e = kzalloc(sizeof(*e), GFP_KERNEL);
1233 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1236 e->expr_type = le32_to_cpu(buf[0]);
1237 e->attr = le32_to_cpu(buf[1]);
1238 e->op = le32_to_cpu(buf[2]);
1240 switch (e->expr_type) {
1252 if (depth == (CEXPR_MAXDEPTH - 1))
1257 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1259 if (depth == (CEXPR_MAXDEPTH - 1))
1262 rc = ebitmap_read(&e->names, fp);
1265 if (p->policyvers >=
1266 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1267 e->type_names = kzalloc(sizeof
1268 (*e->type_names), GFP_KERNEL);
1271 type_set_init(e->type_names);
1272 rc = type_set_read(e->type_names, fp);
1290 static int class_read(struct policydb *p, struct symtab *s, void *fp)
1293 struct class_datum *cladatum;
1295 u32 i, len, len2, ncons, nel;
1298 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1302 rc = next_entry(buf, fp, sizeof(u32)*6);
1306 len = le32_to_cpu(buf[0]);
1307 len2 = le32_to_cpu(buf[1]);
1308 cladatum->value = le32_to_cpu(buf[2]);
1309 nel = le32_to_cpu(buf[4]);
1311 rc = symtab_init(&cladatum->permissions, nel);
1314 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1316 ncons = le32_to_cpu(buf[5]);
1318 rc = str_read(&key, GFP_KERNEL, fp, len);
1323 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1328 cladatum->comdatum = symtab_search(&p->p_commons,
1330 if (!cladatum->comdatum) {
1331 pr_err("SELinux: unknown common %s\n",
1336 for (i = 0; i < nel; i++) {
1337 rc = perm_read(p, &cladatum->permissions, fp);
1342 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1346 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1347 /* grab the validatetrans rules */
1348 rc = next_entry(buf, fp, sizeof(u32));
1351 ncons = le32_to_cpu(buf[0]);
1352 rc = read_cons_helper(p, &cladatum->validatetrans,
1358 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1359 rc = next_entry(buf, fp, sizeof(u32) * 3);
1363 cladatum->default_user = le32_to_cpu(buf[0]);
1364 cladatum->default_role = le32_to_cpu(buf[1]);
1365 cladatum->default_range = le32_to_cpu(buf[2]);
1368 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1369 rc = next_entry(buf, fp, sizeof(u32) * 1);
1372 cladatum->default_type = le32_to_cpu(buf[0]);
1375 rc = symtab_insert(s, key, cladatum);
1381 cls_destroy(key, cladatum, NULL);
1385 static int role_read(struct policydb *p, struct symtab *s, void *fp)
1388 struct role_datum *role;
1390 unsigned int to_read = 2;
1394 role = kzalloc(sizeof(*role), GFP_KERNEL);
1398 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1401 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1405 len = le32_to_cpu(buf[0]);
1406 role->value = le32_to_cpu(buf[1]);
1407 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1408 role->bounds = le32_to_cpu(buf[2]);
1410 rc = str_read(&key, GFP_KERNEL, fp, len);
1414 rc = ebitmap_read(&role->dominates, fp);
1418 rc = ebitmap_read(&role->types, fp);
1422 if (strcmp(key, OBJECT_R) == 0) {
1424 if (role->value != OBJECT_R_VAL) {
1425 pr_err("SELinux: Role %s has wrong value %d\n",
1426 OBJECT_R, role->value);
1433 rc = symtab_insert(s, key, role);
1438 role_destroy(key, role, NULL);
1442 static int type_read(struct policydb *p, struct symtab *s, void *fp)
1445 struct type_datum *typdatum;
1447 unsigned int to_read = 3;
1451 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1455 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1458 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1462 len = le32_to_cpu(buf[0]);
1463 typdatum->value = le32_to_cpu(buf[1]);
1464 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1465 u32 prop = le32_to_cpu(buf[2]);
1467 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1468 typdatum->primary = 1;
1469 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1470 typdatum->attribute = 1;
1472 typdatum->bounds = le32_to_cpu(buf[3]);
1474 typdatum->primary = le32_to_cpu(buf[2]);
1477 rc = str_read(&key, GFP_KERNEL, fp, len);
1481 rc = symtab_insert(s, key, typdatum);
1486 type_destroy(key, typdatum, NULL);
1492 * Read a MLS level structure from a policydb binary
1493 * representation file.
1495 static int mls_read_level(struct mls_level *lp, void *fp)
1500 memset(lp, 0, sizeof(*lp));
1502 rc = next_entry(buf, fp, sizeof buf);
1504 pr_err("SELinux: mls: truncated level\n");
1507 lp->sens = le32_to_cpu(buf[0]);
1509 rc = ebitmap_read(&lp->cat, fp);
1511 pr_err("SELinux: mls: error reading level categories\n");
1517 static int user_read(struct policydb *p, struct symtab *s, void *fp)
1520 struct user_datum *usrdatum;
1522 unsigned int to_read = 2;
1526 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1530 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1533 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1537 len = le32_to_cpu(buf[0]);
1538 usrdatum->value = le32_to_cpu(buf[1]);
1539 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1540 usrdatum->bounds = le32_to_cpu(buf[2]);
1542 rc = str_read(&key, GFP_KERNEL, fp, len);
1546 rc = ebitmap_read(&usrdatum->roles, fp);
1550 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1551 rc = mls_read_range_helper(&usrdatum->range, fp);
1554 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1559 rc = symtab_insert(s, key, usrdatum);
1564 user_destroy(key, usrdatum, NULL);
1568 static int sens_read(struct policydb *p, struct symtab *s, void *fp)
1571 struct level_datum *levdatum;
1576 levdatum = kzalloc(sizeof(*levdatum), GFP_KERNEL);
1580 rc = next_entry(buf, fp, sizeof buf);
1584 len = le32_to_cpu(buf[0]);
1585 levdatum->isalias = le32_to_cpu(buf[1]);
1587 rc = str_read(&key, GFP_KERNEL, fp, len);
1592 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_KERNEL);
1593 if (!levdatum->level)
1596 rc = mls_read_level(levdatum->level, fp);
1600 rc = symtab_insert(s, key, levdatum);
1605 sens_destroy(key, levdatum, NULL);
1609 static int cat_read(struct policydb *p, struct symtab *s, void *fp)
1612 struct cat_datum *catdatum;
1617 catdatum = kzalloc(sizeof(*catdatum), GFP_KERNEL);
1621 rc = next_entry(buf, fp, sizeof buf);
1625 len = le32_to_cpu(buf[0]);
1626 catdatum->value = le32_to_cpu(buf[1]);
1627 catdatum->isalias = le32_to_cpu(buf[2]);
1629 rc = str_read(&key, GFP_KERNEL, fp, len);
1633 rc = symtab_insert(s, key, catdatum);
1638 cat_destroy(key, catdatum, NULL);
1642 static int (*const read_f[SYM_NUM]) (struct policydb *p,
1643 struct symtab *s, void *fp) = {
1654 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1656 struct user_datum *upper, *user;
1657 struct policydb *p = datap;
1660 upper = user = datum;
1661 while (upper->bounds) {
1662 struct ebitmap_node *node;
1665 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1666 pr_err("SELinux: user %s: "
1667 "too deep or looped boundary\n",
1672 upper = p->user_val_to_struct[upper->bounds - 1];
1673 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1674 if (ebitmap_get_bit(&upper->roles, bit))
1677 pr_err("SELinux: boundary violated policy: "
1678 "user=%s role=%s bounds=%s\n",
1679 sym_name(p, SYM_USERS, user->value - 1),
1680 sym_name(p, SYM_ROLES, bit),
1681 sym_name(p, SYM_USERS, upper->value - 1));
1690 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1692 struct role_datum *upper, *role;
1693 struct policydb *p = datap;
1696 upper = role = datum;
1697 while (upper->bounds) {
1698 struct ebitmap_node *node;
1701 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1702 pr_err("SELinux: role %s: "
1703 "too deep or looped bounds\n",
1708 upper = p->role_val_to_struct[upper->bounds - 1];
1709 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1710 if (ebitmap_get_bit(&upper->types, bit))
1713 pr_err("SELinux: boundary violated policy: "
1714 "role=%s type=%s bounds=%s\n",
1715 sym_name(p, SYM_ROLES, role->value - 1),
1716 sym_name(p, SYM_TYPES, bit),
1717 sym_name(p, SYM_ROLES, upper->value - 1));
1726 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1728 struct type_datum *upper;
1729 struct policydb *p = datap;
1733 while (upper->bounds) {
1734 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1735 pr_err("SELinux: type %s: "
1736 "too deep or looped boundary\n",
1741 upper = p->type_val_to_struct[upper->bounds - 1];
1744 if (upper->attribute) {
1745 pr_err("SELinux: type %s: "
1746 "bounded by attribute %s\n",
1748 sym_name(p, SYM_TYPES, upper->value - 1));
1756 static int policydb_bounds_sanity_check(struct policydb *p)
1760 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1763 rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
1767 rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
1771 rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
1778 u16 string_to_security_class(struct policydb *p, const char *name)
1780 struct class_datum *cladatum;
1782 cladatum = symtab_search(&p->p_classes, name);
1786 return cladatum->value;
1789 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1791 struct class_datum *cladatum;
1792 struct perm_datum *perdatum = NULL;
1793 struct common_datum *comdatum;
1795 if (!tclass || tclass > p->p_classes.nprim)
1798 cladatum = p->class_val_to_struct[tclass-1];
1799 comdatum = cladatum->comdatum;
1801 perdatum = symtab_search(&comdatum->permissions, name);
1803 perdatum = symtab_search(&cladatum->permissions, name);
1807 return 1U << (perdatum->value-1);
1810 static int range_read(struct policydb *p, void *fp)
1812 struct range_trans *rt = NULL;
1813 struct mls_range *r = NULL;
1818 if (p->policyvers < POLICYDB_VERSION_MLS)
1821 rc = next_entry(buf, fp, sizeof(u32));
1825 nel = le32_to_cpu(buf[0]);
1827 rc = hashtab_init(&p->range_tr, nel);
1831 for (i = 0; i < nel; i++) {
1833 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1837 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1841 rt->source_type = le32_to_cpu(buf[0]);
1842 rt->target_type = le32_to_cpu(buf[1]);
1843 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1844 rc = next_entry(buf, fp, sizeof(u32));
1847 rt->target_class = le32_to_cpu(buf[0]);
1849 rt->target_class = p->process_class;
1852 if (!policydb_type_isvalid(p, rt->source_type) ||
1853 !policydb_type_isvalid(p, rt->target_type) ||
1854 !policydb_class_isvalid(p, rt->target_class))
1858 r = kzalloc(sizeof(*r), GFP_KERNEL);
1862 rc = mls_read_range_helper(r, fp);
1867 if (!mls_range_isvalid(p, r)) {
1868 pr_warn("SELinux: rangetrans: invalid range\n");
1872 rc = hashtab_insert(&p->range_tr, rt, r, rangetr_key_params);
1879 hash_eval(&p->range_tr, "rangetr");
1887 static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
1889 struct filename_trans_key key, *ft = NULL;
1890 struct filename_trans_datum *last, *datum = NULL;
1892 u32 len, stype, otype;
1896 /* length of the path component string */
1897 rc = next_entry(buf, fp, sizeof(u32));
1900 len = le32_to_cpu(buf[0]);
1902 /* path component string */
1903 rc = str_read(&name, GFP_KERNEL, fp, len);
1907 rc = next_entry(buf, fp, sizeof(u32) * 4);
1911 stype = le32_to_cpu(buf[0]);
1912 key.ttype = le32_to_cpu(buf[1]);
1913 key.tclass = le32_to_cpu(buf[2]);
1916 otype = le32_to_cpu(buf[3]);
1919 datum = policydb_filenametr_search(p, &key);
1921 if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
1922 /* conflicting/duplicate rules are ignored */
1926 if (likely(datum->otype == otype))
1929 datum = datum->next;
1933 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
1937 ebitmap_init(&datum->stypes);
1938 datum->otype = otype;
1941 if (unlikely(last)) {
1945 ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
1949 rc = hashtab_insert(&p->filename_trans, ft, datum,
1950 filenametr_key_params);
1955 rc = ebitmap_set_bit(&p->filename_trans_ttypes,
1962 return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
1971 static int filename_trans_read_helper(struct policydb *p, void *fp)
1973 struct filename_trans_key *ft = NULL;
1974 struct filename_trans_datum **dst, *datum, *first = NULL;
1976 u32 len, ttype, tclass, ndatum, i;
1980 /* length of the path component string */
1981 rc = next_entry(buf, fp, sizeof(u32));
1984 len = le32_to_cpu(buf[0]);
1986 /* path component string */
1987 rc = str_read(&name, GFP_KERNEL, fp, len);
1991 rc = next_entry(buf, fp, sizeof(u32) * 3);
1995 ttype = le32_to_cpu(buf[0]);
1996 tclass = le32_to_cpu(buf[1]);
1998 ndatum = le32_to_cpu(buf[2]);
2000 pr_err("SELinux: Filename transition key with no datum\n");
2006 for (i = 0; i < ndatum; i++) {
2008 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
2015 /* ebitmap_read() will at least init the bitmap */
2016 rc = ebitmap_read(&datum->stypes, fp);
2020 rc = next_entry(buf, fp, sizeof(u32));
2024 datum->otype = le32_to_cpu(buf[0]);
2030 ft = kmalloc(sizeof(*ft), GFP_KERNEL);
2035 ft->tclass = tclass;
2038 rc = hashtab_insert(&p->filename_trans, ft, first,
2039 filenametr_key_params);
2041 pr_err("SELinux: Duplicate filename transition key\n");
2045 return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
2052 first = first->next;
2054 ebitmap_destroy(&datum->stypes);
2060 static int filename_trans_read(struct policydb *p, void *fp)
2066 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
2069 rc = next_entry(buf, fp, sizeof(u32));
2072 nel = le32_to_cpu(buf[0]);
2074 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
2075 p->compat_filename_trans_count = nel;
2077 rc = hashtab_init(&p->filename_trans, (1 << 11));
2081 for (i = 0; i < nel; i++) {
2082 rc = filename_trans_read_helper_compat(p, fp);
2087 rc = hashtab_init(&p->filename_trans, nel);
2091 for (i = 0; i < nel; i++) {
2092 rc = filename_trans_read_helper(p, fp);
2097 hash_eval(&p->filename_trans, "filenametr");
2101 static int genfs_read(struct policydb *p, void *fp)
2104 u32 i, j, nel, nel2, len, len2;
2106 struct ocontext *l, *c;
2107 struct ocontext *newc = NULL;
2108 struct genfs *genfs_p, *genfs;
2109 struct genfs *newgenfs = NULL;
2111 rc = next_entry(buf, fp, sizeof(u32));
2114 nel = le32_to_cpu(buf[0]);
2116 for (i = 0; i < nel; i++) {
2117 rc = next_entry(buf, fp, sizeof(u32));
2120 len = le32_to_cpu(buf[0]);
2123 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2127 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2131 for (genfs_p = NULL, genfs = p->genfs; genfs;
2132 genfs_p = genfs, genfs = genfs->next) {
2134 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2135 pr_err("SELinux: dup genfs fstype %s\n",
2139 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2142 newgenfs->next = genfs;
2144 genfs_p->next = newgenfs;
2146 p->genfs = newgenfs;
2150 rc = next_entry(buf, fp, sizeof(u32));
2154 nel2 = le32_to_cpu(buf[0]);
2155 for (j = 0; j < nel2; j++) {
2156 rc = next_entry(buf, fp, sizeof(u32));
2159 len = le32_to_cpu(buf[0]);
2162 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2166 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2170 rc = next_entry(buf, fp, sizeof(u32));
2174 newc->v.sclass = le32_to_cpu(buf[0]);
2175 rc = context_read_and_validate(&newc->context[0], p, fp);
2179 for (l = NULL, c = genfs->head; c;
2180 l = c, c = c->next) {
2182 if (!strcmp(newc->u.name, c->u.name) &&
2183 (!c->v.sclass || !newc->v.sclass ||
2184 newc->v.sclass == c->v.sclass)) {
2185 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2186 genfs->fstype, c->u.name);
2189 len = strlen(newc->u.name);
2190 len2 = strlen(c->u.name);
2206 kfree(newgenfs->fstype);
2209 ocontext_destroy(newc, OCON_FSUSE);
2214 static int ocontext_read(struct policydb *p, const struct policydb_compat_info *info,
2220 __be64 prefixbuf[1];
2222 struct ocontext *l, *c;
2225 for (i = 0; i < info->ocon_num; i++) {
2226 rc = next_entry(buf, fp, sizeof(u32));
2229 nel = le32_to_cpu(buf[0]);
2232 for (j = 0; j < nel; j++) {
2234 c = kzalloc(sizeof(*c), GFP_KERNEL);
2240 p->ocontexts[i] = c;
2245 rc = next_entry(buf, fp, sizeof(u32));
2249 c->sid[0] = le32_to_cpu(buf[0]);
2250 rc = context_read_and_validate(&c->context[0], p, fp);
2256 rc = next_entry(buf, fp, sizeof(u32));
2259 len = le32_to_cpu(buf[0]);
2261 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2266 pr_warn("SELinux: void and deprecated fs ocon %s\n",
2269 rc = context_read_and_validate(&c->context[0], p, fp);
2272 rc = context_read_and_validate(&c->context[1], p, fp);
2277 rc = next_entry(buf, fp, sizeof(u32)*3);
2280 c->u.port.protocol = le32_to_cpu(buf[0]);
2281 c->u.port.low_port = le32_to_cpu(buf[1]);
2282 c->u.port.high_port = le32_to_cpu(buf[2]);
2283 rc = context_read_and_validate(&c->context[0], p, fp);
2288 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2291 c->u.node.addr = nodebuf[0]; /* network order */
2292 c->u.node.mask = nodebuf[1]; /* network order */
2293 rc = context_read_and_validate(&c->context[0], p, fp);
2298 rc = next_entry(buf, fp, sizeof(u32)*2);
2303 c->v.behavior = le32_to_cpu(buf[0]);
2304 /* Determined at runtime, not in policy DB. */
2305 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2307 if (c->v.behavior > SECURITY_FS_USE_MAX)
2310 len = le32_to_cpu(buf[1]);
2311 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2315 rc = context_read_and_validate(&c->context[0], p, fp);
2322 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2325 for (k = 0; k < 4; k++)
2326 c->u.node6.addr[k] = nodebuf[k];
2327 for (k = 0; k < 4; k++)
2328 c->u.node6.mask[k] = nodebuf[k+4];
2329 rc = context_read_and_validate(&c->context[0], p, fp);
2335 u32 pkey_lo, pkey_hi;
2337 rc = next_entry(prefixbuf, fp, sizeof(u64));
2341 /* we need to have subnet_prefix in CPU order */
2342 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2344 rc = next_entry(buf, fp, sizeof(u32) * 2);
2348 pkey_lo = le32_to_cpu(buf[0]);
2349 pkey_hi = le32_to_cpu(buf[1]);
2351 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2356 c->u.ibpkey.low_pkey = pkey_lo;
2357 c->u.ibpkey.high_pkey = pkey_hi;
2359 rc = context_read_and_validate(&c->context[0],
2366 case OCON_IBENDPORT: {
2369 rc = next_entry(buf, fp, sizeof(u32) * 2);
2372 len = le32_to_cpu(buf[0]);
2374 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2378 port = le32_to_cpu(buf[1]);
2379 if (port > U8_MAX || port == 0) {
2384 c->u.ibendport.port = port;
2386 rc = context_read_and_validate(&c->context[0],
2402 * Read the configuration data from a policy database binary
2403 * representation file into a policy database structure.
2405 int policydb_read(struct policydb *p, void *fp)
2407 struct role_allow *ra, *lra;
2408 struct role_trans_key *rtk = NULL;
2409 struct role_trans_datum *rtd = NULL;
2412 u32 i, j, len, nprim, nel, perm;
2415 const struct policydb_compat_info *info;
2419 /* Read the magic number and string length. */
2420 rc = next_entry(buf, fp, sizeof(u32) * 2);
2425 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2426 pr_err("SELinux: policydb magic number 0x%x does "
2427 "not match expected magic number 0x%x\n",
2428 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2433 len = le32_to_cpu(buf[1]);
2434 if (len != strlen(POLICYDB_STRING)) {
2435 pr_err("SELinux: policydb string length %d does not "
2436 "match expected length %zu\n",
2437 len, strlen(POLICYDB_STRING));
2442 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2443 if (!policydb_str) {
2444 pr_err("SELinux: unable to allocate memory for policydb "
2445 "string of length %d\n", len);
2449 rc = next_entry(policydb_str, fp, len);
2451 pr_err("SELinux: truncated policydb string identifier\n");
2452 kfree(policydb_str);
2457 policydb_str[len] = '\0';
2458 if (strcmp(policydb_str, POLICYDB_STRING)) {
2459 pr_err("SELinux: policydb string %s does not match "
2460 "my string %s\n", policydb_str, POLICYDB_STRING);
2461 kfree(policydb_str);
2464 /* Done with policydb_str. */
2465 kfree(policydb_str);
2466 policydb_str = NULL;
2468 /* Read the version and table sizes. */
2469 rc = next_entry(buf, fp, sizeof(u32)*4);
2474 p->policyvers = le32_to_cpu(buf[0]);
2475 if (p->policyvers < POLICYDB_VERSION_MIN ||
2476 p->policyvers > POLICYDB_VERSION_MAX) {
2477 pr_err("SELinux: policydb version %d does not match "
2478 "my version range %d-%d\n",
2479 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2483 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2487 if (p->policyvers < POLICYDB_VERSION_MLS) {
2488 pr_err("SELinux: security policydb version %d "
2489 "(MLS) not backwards compatible\n",
2494 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2495 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2497 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2498 rc = ebitmap_read(&p->policycaps, fp);
2503 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2504 rc = ebitmap_read(&p->permissive_map, fp);
2510 info = policydb_lookup_compat(p->policyvers);
2512 pr_err("SELinux: unable to find policy compat info "
2513 "for version %d\n", p->policyvers);
2518 if (le32_to_cpu(buf[2]) != info->sym_num ||
2519 le32_to_cpu(buf[3]) != info->ocon_num) {
2520 pr_err("SELinux: policydb table sizes (%d,%d) do "
2521 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2522 le32_to_cpu(buf[3]),
2523 info->sym_num, info->ocon_num);
2527 for (i = 0; i < info->sym_num; i++) {
2528 rc = next_entry(buf, fp, sizeof(u32)*2);
2531 nprim = le32_to_cpu(buf[0]);
2532 nel = le32_to_cpu(buf[1]);
2534 rc = symtab_init(&p->symtab[i], nel);
2538 if (i == SYM_ROLES) {
2544 for (j = 0; j < nel; j++) {
2545 rc = read_f[i](p, &p->symtab[i], fp);
2550 p->symtab[i].nprim = nprim;
2554 p->process_class = string_to_security_class(p, "process");
2555 if (!p->process_class) {
2556 pr_err("SELinux: process class is required, not defined in policy\n");
2560 rc = avtab_read(&p->te_avtab, fp, p);
2564 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2565 rc = cond_read_list(p, fp);
2570 rc = next_entry(buf, fp, sizeof(u32));
2573 nel = le32_to_cpu(buf[0]);
2575 rc = hashtab_init(&p->role_tr, nel);
2578 for (i = 0; i < nel; i++) {
2580 rtk = kmalloc(sizeof(*rtk), GFP_KERNEL);
2585 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
2589 rc = next_entry(buf, fp, sizeof(u32)*3);
2593 rtk->role = le32_to_cpu(buf[0]);
2594 rtk->type = le32_to_cpu(buf[1]);
2595 rtd->new_role = le32_to_cpu(buf[2]);
2596 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2597 rc = next_entry(buf, fp, sizeof(u32));
2600 rtk->tclass = le32_to_cpu(buf[0]);
2602 rtk->tclass = p->process_class;
2605 if (!policydb_role_isvalid(p, rtk->role) ||
2606 !policydb_type_isvalid(p, rtk->type) ||
2607 !policydb_class_isvalid(p, rtk->tclass) ||
2608 !policydb_role_isvalid(p, rtd->new_role))
2611 rc = hashtab_insert(&p->role_tr, rtk, rtd, roletr_key_params);
2619 rc = next_entry(buf, fp, sizeof(u32));
2622 nel = le32_to_cpu(buf[0]);
2624 for (i = 0; i < nel; i++) {
2626 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2633 rc = next_entry(buf, fp, sizeof(u32)*2);
2638 ra->role = le32_to_cpu(buf[0]);
2639 ra->new_role = le32_to_cpu(buf[1]);
2640 if (!policydb_role_isvalid(p, ra->role) ||
2641 !policydb_role_isvalid(p, ra->new_role))
2646 rc = filename_trans_read(p, fp);
2650 rc = policydb_index(p);
2655 perm = string_to_av_perm(p, p->process_class, "transition");
2657 pr_err("SELinux: process transition permission is required, not defined in policy\n");
2660 p->process_trans_perms = perm;
2661 perm = string_to_av_perm(p, p->process_class, "dyntransition");
2663 pr_err("SELinux: process dyntransition permission is required, not defined in policy\n");
2666 p->process_trans_perms |= perm;
2668 rc = ocontext_read(p, info, fp);
2672 rc = genfs_read(p, fp);
2676 rc = range_read(p, fp);
2681 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2682 sizeof(*p->type_attr_map_array),
2684 if (!p->type_attr_map_array)
2687 /* just in case ebitmap_init() becomes more than just a memset(0): */
2688 for (i = 0; i < p->p_types.nprim; i++)
2689 ebitmap_init(&p->type_attr_map_array[i]);
2691 for (i = 0; i < p->p_types.nprim; i++) {
2692 struct ebitmap *e = &p->type_attr_map_array[i];
2694 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2695 rc = ebitmap_read(e, fp);
2699 /* add the type itself as the degenerate case */
2700 rc = ebitmap_set_bit(e, i, 1);
2705 rc = policydb_bounds_sanity_check(p);
2715 policydb_destroy(p);
2720 * Write a MLS level structure to a policydb binary
2721 * representation file.
2723 static int mls_write_level(struct mls_level *l, void *fp)
2728 buf[0] = cpu_to_le32(l->sens);
2729 rc = put_entry(buf, sizeof(u32), 1, fp);
2733 rc = ebitmap_write(&l->cat, fp);
2741 * Write a MLS range structure to a policydb binary
2742 * representation file.
2744 static int mls_write_range_helper(struct mls_range *r, void *fp)
2750 eq = mls_level_eq(&r->level[1], &r->level[0]);
2756 buf[0] = cpu_to_le32(items-1);
2757 buf[1] = cpu_to_le32(r->level[0].sens);
2759 buf[2] = cpu_to_le32(r->level[1].sens);
2761 BUG_ON(items > ARRAY_SIZE(buf));
2763 rc = put_entry(buf, sizeof(u32), items, fp);
2767 rc = ebitmap_write(&r->level[0].cat, fp);
2771 rc = ebitmap_write(&r->level[1].cat, fp);
2779 static int sens_write(void *vkey, void *datum, void *ptr)
2782 struct level_datum *levdatum = datum;
2783 struct policy_data *pd = ptr;
2790 buf[0] = cpu_to_le32(len);
2791 buf[1] = cpu_to_le32(levdatum->isalias);
2792 rc = put_entry(buf, sizeof(u32), 2, fp);
2796 rc = put_entry(key, 1, len, fp);
2800 rc = mls_write_level(levdatum->level, fp);
2807 static int cat_write(void *vkey, void *datum, void *ptr)
2810 struct cat_datum *catdatum = datum;
2811 struct policy_data *pd = ptr;
2818 buf[0] = cpu_to_le32(len);
2819 buf[1] = cpu_to_le32(catdatum->value);
2820 buf[2] = cpu_to_le32(catdatum->isalias);
2821 rc = put_entry(buf, sizeof(u32), 3, fp);
2825 rc = put_entry(key, 1, len, fp);
2832 static int role_trans_write_one(void *key, void *datum, void *ptr)
2834 struct role_trans_key *rtk = key;
2835 struct role_trans_datum *rtd = datum;
2836 struct policy_data *pd = ptr;
2838 struct policydb *p = pd->p;
2842 buf[0] = cpu_to_le32(rtk->role);
2843 buf[1] = cpu_to_le32(rtk->type);
2844 buf[2] = cpu_to_le32(rtd->new_role);
2845 rc = put_entry(buf, sizeof(u32), 3, fp);
2848 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2849 buf[0] = cpu_to_le32(rtk->tclass);
2850 rc = put_entry(buf, sizeof(u32), 1, fp);
2857 static int role_trans_write(struct policydb *p, void *fp)
2859 struct policy_data pd = { .p = p, .fp = fp };
2863 buf[0] = cpu_to_le32(p->role_tr.nel);
2864 rc = put_entry(buf, sizeof(u32), 1, fp);
2868 return hashtab_map(&p->role_tr, role_trans_write_one, &pd);
2871 static int role_allow_write(struct role_allow *r, void *fp)
2873 struct role_allow *ra;
2879 for (ra = r; ra; ra = ra->next)
2881 buf[0] = cpu_to_le32(nel);
2882 rc = put_entry(buf, sizeof(u32), 1, fp);
2885 for (ra = r; ra; ra = ra->next) {
2886 buf[0] = cpu_to_le32(ra->role);
2887 buf[1] = cpu_to_le32(ra->new_role);
2888 rc = put_entry(buf, sizeof(u32), 2, fp);
2896 * Write a security context structure
2897 * to a policydb binary representation file.
2899 static int context_write(struct policydb *p, struct context *c,
2905 buf[0] = cpu_to_le32(c->user);
2906 buf[1] = cpu_to_le32(c->role);
2907 buf[2] = cpu_to_le32(c->type);
2909 rc = put_entry(buf, sizeof(u32), 3, fp);
2913 rc = mls_write_range_helper(&c->range, fp);
2921 * The following *_write functions are used to
2922 * write the symbol data to a policy database
2923 * binary representation file.
2926 static int perm_write(void *vkey, void *datum, void *fp)
2929 struct perm_datum *perdatum = datum;
2935 buf[0] = cpu_to_le32(len);
2936 buf[1] = cpu_to_le32(perdatum->value);
2937 rc = put_entry(buf, sizeof(u32), 2, fp);
2941 rc = put_entry(key, 1, len, fp);
2948 static int common_write(void *vkey, void *datum, void *ptr)
2951 struct common_datum *comdatum = datum;
2952 struct policy_data *pd = ptr;
2959 buf[0] = cpu_to_le32(len);
2960 buf[1] = cpu_to_le32(comdatum->value);
2961 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2962 buf[3] = cpu_to_le32(comdatum->permissions.table.nel);
2963 rc = put_entry(buf, sizeof(u32), 4, fp);
2967 rc = put_entry(key, 1, len, fp);
2971 rc = hashtab_map(&comdatum->permissions.table, perm_write, fp);
2978 static int type_set_write(struct type_set *t, void *fp)
2983 if (ebitmap_write(&t->types, fp))
2985 if (ebitmap_write(&t->negset, fp))
2988 buf[0] = cpu_to_le32(t->flags);
2989 rc = put_entry(buf, sizeof(u32), 1, fp);
2996 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2999 struct constraint_node *c;
3000 struct constraint_expr *e;
3005 for (c = node; c; c = c->next) {
3007 for (e = c->expr; e; e = e->next)
3009 buf[0] = cpu_to_le32(c->permissions);
3010 buf[1] = cpu_to_le32(nel);
3011 rc = put_entry(buf, sizeof(u32), 2, fp);
3014 for (e = c->expr; e; e = e->next) {
3015 buf[0] = cpu_to_le32(e->expr_type);
3016 buf[1] = cpu_to_le32(e->attr);
3017 buf[2] = cpu_to_le32(e->op);
3018 rc = put_entry(buf, sizeof(u32), 3, fp);
3022 switch (e->expr_type) {
3024 rc = ebitmap_write(&e->names, fp);
3027 if (p->policyvers >=
3028 POLICYDB_VERSION_CONSTRAINT_NAMES) {
3029 rc = type_set_write(e->type_names, fp);
3043 static int class_write(void *vkey, void *datum, void *ptr)
3046 struct class_datum *cladatum = datum;
3047 struct policy_data *pd = ptr;
3049 struct policydb *p = pd->p;
3050 struct constraint_node *c;
3057 if (cladatum->comkey)
3058 len2 = strlen(cladatum->comkey);
3063 for (c = cladatum->constraints; c; c = c->next)
3066 buf[0] = cpu_to_le32(len);
3067 buf[1] = cpu_to_le32(len2);
3068 buf[2] = cpu_to_le32(cladatum->value);
3069 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
3070 buf[4] = cpu_to_le32(cladatum->permissions.table.nel);
3071 buf[5] = cpu_to_le32(ncons);
3072 rc = put_entry(buf, sizeof(u32), 6, fp);
3076 rc = put_entry(key, 1, len, fp);
3080 if (cladatum->comkey) {
3081 rc = put_entry(cladatum->comkey, 1, len2, fp);
3086 rc = hashtab_map(&cladatum->permissions.table, perm_write, fp);
3090 rc = write_cons_helper(p, cladatum->constraints, fp);
3094 /* write out the validatetrans rule */
3096 for (c = cladatum->validatetrans; c; c = c->next)
3099 buf[0] = cpu_to_le32(ncons);
3100 rc = put_entry(buf, sizeof(u32), 1, fp);
3104 rc = write_cons_helper(p, cladatum->validatetrans, fp);
3108 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
3109 buf[0] = cpu_to_le32(cladatum->default_user);
3110 buf[1] = cpu_to_le32(cladatum->default_role);
3111 buf[2] = cpu_to_le32(cladatum->default_range);
3113 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
3118 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
3119 buf[0] = cpu_to_le32(cladatum->default_type);
3120 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
3128 static int role_write(void *vkey, void *datum, void *ptr)
3131 struct role_datum *role = datum;
3132 struct policy_data *pd = ptr;
3134 struct policydb *p = pd->p;
3141 buf[items++] = cpu_to_le32(len);
3142 buf[items++] = cpu_to_le32(role->value);
3143 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3144 buf[items++] = cpu_to_le32(role->bounds);
3146 BUG_ON(items > ARRAY_SIZE(buf));
3148 rc = put_entry(buf, sizeof(u32), items, fp);
3152 rc = put_entry(key, 1, len, fp);
3156 rc = ebitmap_write(&role->dominates, fp);
3160 rc = ebitmap_write(&role->types, fp);
3167 static int type_write(void *vkey, void *datum, void *ptr)
3170 struct type_datum *typdatum = datum;
3171 struct policy_data *pd = ptr;
3172 struct policydb *p = pd->p;
3180 buf[items++] = cpu_to_le32(len);
3181 buf[items++] = cpu_to_le32(typdatum->value);
3182 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3185 if (typdatum->primary)
3186 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3188 if (typdatum->attribute)
3189 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3191 buf[items++] = cpu_to_le32(properties);
3192 buf[items++] = cpu_to_le32(typdatum->bounds);
3194 buf[items++] = cpu_to_le32(typdatum->primary);
3196 BUG_ON(items > ARRAY_SIZE(buf));
3197 rc = put_entry(buf, sizeof(u32), items, fp);
3201 rc = put_entry(key, 1, len, fp);
3208 static int user_write(void *vkey, void *datum, void *ptr)
3211 struct user_datum *usrdatum = datum;
3212 struct policy_data *pd = ptr;
3213 struct policydb *p = pd->p;
3221 buf[items++] = cpu_to_le32(len);
3222 buf[items++] = cpu_to_le32(usrdatum->value);
3223 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3224 buf[items++] = cpu_to_le32(usrdatum->bounds);
3225 BUG_ON(items > ARRAY_SIZE(buf));
3226 rc = put_entry(buf, sizeof(u32), items, fp);
3230 rc = put_entry(key, 1, len, fp);
3234 rc = ebitmap_write(&usrdatum->roles, fp);
3238 rc = mls_write_range_helper(&usrdatum->range, fp);
3242 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3249 static int (*const write_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
3260 static int ocontext_write(struct policydb *p, const struct policydb_compat_info *info,
3266 __be64 prefixbuf[1];
3270 for (i = 0; i < info->ocon_num; i++) {
3272 for (c = p->ocontexts[i]; c; c = c->next)
3274 buf[0] = cpu_to_le32(nel);
3275 rc = put_entry(buf, sizeof(u32), 1, fp);
3278 for (c = p->ocontexts[i]; c; c = c->next) {
3281 buf[0] = cpu_to_le32(c->sid[0]);
3282 rc = put_entry(buf, sizeof(u32), 1, fp);
3285 rc = context_write(p, &c->context[0], fp);
3291 len = strlen(c->u.name);
3292 buf[0] = cpu_to_le32(len);
3293 rc = put_entry(buf, sizeof(u32), 1, fp);
3296 rc = put_entry(c->u.name, 1, len, fp);
3299 rc = context_write(p, &c->context[0], fp);
3302 rc = context_write(p, &c->context[1], fp);
3307 buf[0] = cpu_to_le32(c->u.port.protocol);
3308 buf[1] = cpu_to_le32(c->u.port.low_port);
3309 buf[2] = cpu_to_le32(c->u.port.high_port);
3310 rc = put_entry(buf, sizeof(u32), 3, fp);
3313 rc = context_write(p, &c->context[0], fp);
3318 nodebuf[0] = c->u.node.addr; /* network order */
3319 nodebuf[1] = c->u.node.mask; /* network order */
3320 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3323 rc = context_write(p, &c->context[0], fp);
3328 buf[0] = cpu_to_le32(c->v.behavior);
3329 len = strlen(c->u.name);
3330 buf[1] = cpu_to_le32(len);
3331 rc = put_entry(buf, sizeof(u32), 2, fp);
3334 rc = put_entry(c->u.name, 1, len, fp);
3337 rc = context_write(p, &c->context[0], fp);
3342 for (j = 0; j < 4; j++)
3343 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3344 for (j = 0; j < 4; j++)
3345 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3346 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3349 rc = context_write(p, &c->context[0], fp);
3354 /* subnet_prefix is in CPU order */
3355 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3357 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3361 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3362 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3364 rc = put_entry(buf, sizeof(u32), 2, fp);
3367 rc = context_write(p, &c->context[0], fp);
3371 case OCON_IBENDPORT:
3372 len = strlen(c->u.ibendport.dev_name);
3373 buf[0] = cpu_to_le32(len);
3374 buf[1] = cpu_to_le32(c->u.ibendport.port);
3375 rc = put_entry(buf, sizeof(u32), 2, fp);
3378 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3381 rc = context_write(p, &c->context[0], fp);
3391 static int genfs_write(struct policydb *p, void *fp)
3393 struct genfs *genfs;
3400 for (genfs = p->genfs; genfs; genfs = genfs->next)
3402 buf[0] = cpu_to_le32(len);
3403 rc = put_entry(buf, sizeof(u32), 1, fp);
3406 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3407 len = strlen(genfs->fstype);
3408 buf[0] = cpu_to_le32(len);
3409 rc = put_entry(buf, sizeof(u32), 1, fp);
3412 rc = put_entry(genfs->fstype, 1, len, fp);
3416 for (c = genfs->head; c; c = c->next)
3418 buf[0] = cpu_to_le32(len);
3419 rc = put_entry(buf, sizeof(u32), 1, fp);
3422 for (c = genfs->head; c; c = c->next) {
3423 len = strlen(c->u.name);
3424 buf[0] = cpu_to_le32(len);
3425 rc = put_entry(buf, sizeof(u32), 1, fp);
3428 rc = put_entry(c->u.name, 1, len, fp);
3431 buf[0] = cpu_to_le32(c->v.sclass);
3432 rc = put_entry(buf, sizeof(u32), 1, fp);
3435 rc = context_write(p, &c->context[0], fp);
3443 static int range_write_helper(void *key, void *data, void *ptr)
3446 struct range_trans *rt = key;
3447 struct mls_range *r = data;
3448 struct policy_data *pd = ptr;
3450 struct policydb *p = pd->p;
3453 buf[0] = cpu_to_le32(rt->source_type);
3454 buf[1] = cpu_to_le32(rt->target_type);
3455 rc = put_entry(buf, sizeof(u32), 2, fp);
3458 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3459 buf[0] = cpu_to_le32(rt->target_class);
3460 rc = put_entry(buf, sizeof(u32), 1, fp);
3464 rc = mls_write_range_helper(r, fp);
3471 static int range_write(struct policydb *p, void *fp)
3475 struct policy_data pd;
3480 buf[0] = cpu_to_le32(p->range_tr.nel);
3481 rc = put_entry(buf, sizeof(u32), 1, fp);
3485 /* actually write all of the entries */
3486 rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
3493 static int filename_write_helper_compat(void *key, void *data, void *ptr)
3495 struct filename_trans_key *ft = key;
3496 struct filename_trans_datum *datum = data;
3497 struct ebitmap_node *node;
3501 u32 bit, len = strlen(ft->name);
3504 ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
3505 buf[0] = cpu_to_le32(len);
3506 rc = put_entry(buf, sizeof(u32), 1, fp);
3510 rc = put_entry(ft->name, sizeof(char), len, fp);
3514 buf[0] = cpu_to_le32(bit + 1);
3515 buf[1] = cpu_to_le32(ft->ttype);
3516 buf[2] = cpu_to_le32(ft->tclass);
3517 buf[3] = cpu_to_le32(datum->otype);
3519 rc = put_entry(buf, sizeof(u32), 4, fp);
3524 datum = datum->next;
3525 } while (unlikely(datum));
3530 static int filename_write_helper(void *key, void *data, void *ptr)
3532 struct filename_trans_key *ft = key;
3533 struct filename_trans_datum *datum;
3537 u32 ndatum, len = strlen(ft->name);
3539 buf[0] = cpu_to_le32(len);
3540 rc = put_entry(buf, sizeof(u32), 1, fp);
3544 rc = put_entry(ft->name, sizeof(char), len, fp);
3552 datum = datum->next;
3553 } while (unlikely(datum));
3555 buf[0] = cpu_to_le32(ft->ttype);
3556 buf[1] = cpu_to_le32(ft->tclass);
3557 buf[2] = cpu_to_le32(ndatum);
3558 rc = put_entry(buf, sizeof(u32), 3, fp);
3564 rc = ebitmap_write(&datum->stypes, fp);
3568 buf[0] = cpu_to_le32(datum->otype);
3569 rc = put_entry(buf, sizeof(u32), 1, fp);
3573 datum = datum->next;
3574 } while (unlikely(datum));
3579 static int filename_trans_write(struct policydb *p, void *fp)
3584 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3587 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
3588 buf[0] = cpu_to_le32(p->compat_filename_trans_count);
3589 rc = put_entry(buf, sizeof(u32), 1, fp);
3593 rc = hashtab_map(&p->filename_trans,
3594 filename_write_helper_compat, fp);
3596 buf[0] = cpu_to_le32(p->filename_trans.nel);
3597 rc = put_entry(buf, sizeof(u32), 1, fp);
3601 rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
3607 * Write the configuration data in a policy database
3608 * structure to a policy database binary representation
3611 int policydb_write(struct policydb *p, void *fp)
3613 unsigned int num_syms;
3618 const struct policydb_compat_info *info;
3621 * refuse to write policy older than compressed avtab
3622 * to simplify the writer. There are other tests dropped
3623 * since we assume this throughout the writer code. Be
3624 * careful if you ever try to remove this restriction
3626 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3627 pr_err("SELinux: refusing to write policy version %d."
3628 " Because it is less than version %d\n", p->policyvers,
3629 POLICYDB_VERSION_AVTAB);
3635 config |= POLICYDB_CONFIG_MLS;
3637 if (p->reject_unknown)
3638 config |= REJECT_UNKNOWN;
3639 if (p->allow_unknown)
3640 config |= ALLOW_UNKNOWN;
3642 /* Write the magic number and string identifiers. */
3643 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3644 len = strlen(POLICYDB_STRING);
3645 buf[1] = cpu_to_le32(len);
3646 rc = put_entry(buf, sizeof(u32), 2, fp);
3649 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3653 /* Write the version, config, and table sizes. */
3654 info = policydb_lookup_compat(p->policyvers);
3656 pr_err("SELinux: compatibility lookup failed for policy "
3657 "version %d\n", p->policyvers);
3661 buf[0] = cpu_to_le32(p->policyvers);
3662 buf[1] = cpu_to_le32(config);
3663 buf[2] = cpu_to_le32(info->sym_num);
3664 buf[3] = cpu_to_le32(info->ocon_num);
3666 rc = put_entry(buf, sizeof(u32), 4, fp);
3670 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3671 rc = ebitmap_write(&p->policycaps, fp);
3676 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3677 rc = ebitmap_write(&p->permissive_map, fp);
3682 num_syms = info->sym_num;
3683 for (i = 0; i < num_syms; i++) {
3684 struct policy_data pd;
3689 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3690 buf[1] = cpu_to_le32(p->symtab[i].table.nel);
3692 rc = put_entry(buf, sizeof(u32), 2, fp);
3695 rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
3700 rc = avtab_write(p, &p->te_avtab, fp);
3704 rc = cond_write_list(p, fp);
3708 rc = role_trans_write(p, fp);
3712 rc = role_allow_write(p->role_allow, fp);
3716 rc = filename_trans_write(p, fp);
3720 rc = ocontext_write(p, info, fp);
3724 rc = genfs_write(p, fp);
3728 rc = range_write(p, fp);
3732 for (i = 0; i < p->p_types.nprim; i++) {
3733 struct ebitmap *e = &p->type_attr_map_array[i];
3735 rc = ebitmap_write(e, fp);