1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
9 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
11 * Support for enhanced MLS infrastructure.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Updated: Hewlett-Packard <paul@paul-moore.com>
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"
47 static const char *symtab_name[SYM_NUM] = {
59 struct policydb_compat_info {
65 /* These need to be updated if SYM_NUM or OCON_NUM changes */
66 static struct policydb_compat_info policydb_compat[] = {
68 .version = POLICYDB_VERSION_BASE,
69 .sym_num = SYM_NUM - 3,
70 .ocon_num = OCON_NUM - 3,
73 .version = POLICYDB_VERSION_BOOL,
74 .sym_num = SYM_NUM - 2,
75 .ocon_num = OCON_NUM - 3,
78 .version = POLICYDB_VERSION_IPV6,
79 .sym_num = SYM_NUM - 2,
80 .ocon_num = OCON_NUM - 2,
83 .version = POLICYDB_VERSION_NLCLASS,
84 .sym_num = SYM_NUM - 2,
85 .ocon_num = OCON_NUM - 2,
88 .version = POLICYDB_VERSION_MLS,
90 .ocon_num = OCON_NUM - 2,
93 .version = POLICYDB_VERSION_AVTAB,
95 .ocon_num = OCON_NUM - 2,
98 .version = POLICYDB_VERSION_RANGETRANS,
100 .ocon_num = OCON_NUM - 2,
103 .version = POLICYDB_VERSION_POLCAP,
105 .ocon_num = OCON_NUM - 2,
108 .version = POLICYDB_VERSION_PERMISSIVE,
110 .ocon_num = OCON_NUM - 2,
113 .version = POLICYDB_VERSION_BOUNDARY,
115 .ocon_num = OCON_NUM - 2,
118 .version = POLICYDB_VERSION_FILENAME_TRANS,
120 .ocon_num = OCON_NUM - 2,
123 .version = POLICYDB_VERSION_ROLETRANS,
125 .ocon_num = OCON_NUM - 2,
128 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
130 .ocon_num = OCON_NUM - 2,
133 .version = POLICYDB_VERSION_DEFAULT_TYPE,
135 .ocon_num = OCON_NUM - 2,
138 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
140 .ocon_num = OCON_NUM - 2,
143 .version = POLICYDB_VERSION_XPERMS_IOCTL,
145 .ocon_num = OCON_NUM - 2,
148 .version = POLICYDB_VERSION_INFINIBAND,
150 .ocon_num = OCON_NUM,
153 .version = POLICYDB_VERSION_GLBLUB,
155 .ocon_num = OCON_NUM,
158 .version = POLICYDB_VERSION_COMP_FTRANS,
160 .ocon_num = OCON_NUM,
164 static struct policydb_compat_info *policydb_lookup_compat(int version)
167 struct policydb_compat_info *info = NULL;
169 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
170 if (policydb_compat[i].version == version) {
171 info = &policydb_compat[i];
179 * The following *_destroy functions are used to
180 * free any memory allocated for each kind of
181 * symbol data in the policy database.
184 static int perm_destroy(void *key, void *datum, void *p)
191 static int common_destroy(void *key, void *datum, void *p)
193 struct common_datum *comdatum;
198 hashtab_map(&comdatum->permissions.table, perm_destroy, NULL);
199 hashtab_destroy(&comdatum->permissions.table);
205 static void constraint_expr_destroy(struct constraint_expr *expr)
208 ebitmap_destroy(&expr->names);
209 if (expr->type_names) {
210 ebitmap_destroy(&expr->type_names->types);
211 ebitmap_destroy(&expr->type_names->negset);
212 kfree(expr->type_names);
218 static int cls_destroy(void *key, void *datum, void *p)
220 struct class_datum *cladatum;
221 struct constraint_node *constraint, *ctemp;
222 struct constraint_expr *e, *etmp;
227 hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
228 hashtab_destroy(&cladatum->permissions.table);
229 constraint = cladatum->constraints;
231 e = constraint->expr;
235 constraint_expr_destroy(etmp);
238 constraint = constraint->next;
242 constraint = cladatum->validatetrans;
244 e = constraint->expr;
248 constraint_expr_destroy(etmp);
251 constraint = constraint->next;
254 kfree(cladatum->comkey);
260 static int role_destroy(void *key, void *datum, void *p)
262 struct role_datum *role;
267 ebitmap_destroy(&role->dominates);
268 ebitmap_destroy(&role->types);
274 static int type_destroy(void *key, void *datum, void *p)
281 static int user_destroy(void *key, void *datum, void *p)
283 struct user_datum *usrdatum;
288 ebitmap_destroy(&usrdatum->roles);
289 ebitmap_destroy(&usrdatum->range.level[0].cat);
290 ebitmap_destroy(&usrdatum->range.level[1].cat);
291 ebitmap_destroy(&usrdatum->dfltlevel.cat);
297 static int sens_destroy(void *key, void *datum, void *p)
299 struct level_datum *levdatum;
305 ebitmap_destroy(&levdatum->level->cat);
306 kfree(levdatum->level);
312 static int cat_destroy(void *key, void *datum, void *p)
319 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
331 static int filenametr_destroy(void *key, void *datum, void *p)
333 struct filename_trans_key *ft = key;
334 struct filename_trans_datum *next, *d = datum;
339 ebitmap_destroy(&d->stypes);
343 } while (unlikely(d));
348 static int range_tr_destroy(void *key, void *datum, void *p)
350 struct mls_range *rt = datum;
353 ebitmap_destroy(&rt->level[0].cat);
354 ebitmap_destroy(&rt->level[1].cat);
360 static int role_tr_destroy(void *key, void *datum, void *p)
367 static void ocontext_destroy(struct ocontext *c, int i)
372 context_destroy(&c->context[0]);
373 context_destroy(&c->context[1]);
374 if (i == OCON_ISID || i == OCON_FS ||
375 i == OCON_NETIF || i == OCON_FSUSE)
381 * Initialize the role table.
383 static int roles_init(struct policydb *p)
387 struct role_datum *role;
389 role = kzalloc(sizeof(*role), GFP_KERNEL);
394 role->value = ++p->p_roles.nprim;
395 if (role->value != OBJECT_R_VAL)
399 key = kstrdup(OBJECT_R, GFP_KERNEL);
403 rc = symtab_insert(&p->p_roles, key, role);
414 static u32 filenametr_hash(const void *k)
416 const struct filename_trans_key *ft = k;
418 unsigned int byte_num;
421 hash = ft->ttype ^ ft->tclass;
424 while ((focus = ft->name[byte_num++]))
425 hash = partial_name_hash(focus, hash);
429 static int filenametr_cmp(const void *k1, const void *k2)
431 const struct filename_trans_key *ft1 = k1;
432 const struct filename_trans_key *ft2 = k2;
435 v = ft1->ttype - ft2->ttype;
439 v = ft1->tclass - ft2->tclass;
443 return strcmp(ft1->name, ft2->name);
447 static const struct hashtab_key_params filenametr_key_params = {
448 .hash = filenametr_hash,
449 .cmp = filenametr_cmp,
452 struct filename_trans_datum *policydb_filenametr_search(
453 struct policydb *p, struct filename_trans_key *key)
455 return hashtab_search(&p->filename_trans, key, filenametr_key_params);
458 static u32 rangetr_hash(const void *k)
460 const struct range_trans *key = k;
462 return key->source_type + (key->target_type << 3) +
463 (key->target_class << 5);
466 static int rangetr_cmp(const void *k1, const void *k2)
468 const struct range_trans *key1 = k1, *key2 = k2;
471 v = key1->source_type - key2->source_type;
475 v = key1->target_type - key2->target_type;
479 v = key1->target_class - key2->target_class;
484 static const struct hashtab_key_params rangetr_key_params = {
485 .hash = rangetr_hash,
489 struct mls_range *policydb_rangetr_search(struct policydb *p,
490 struct range_trans *key)
492 return hashtab_search(&p->range_tr, key, rangetr_key_params);
495 static u32 role_trans_hash(const void *k)
497 const struct role_trans_key *key = k;
499 return key->role + (key->type << 3) + (key->tclass << 5);
502 static int role_trans_cmp(const void *k1, const void *k2)
504 const struct role_trans_key *key1 = k1, *key2 = k2;
507 v = key1->role - key2->role;
511 v = key1->type - key2->type;
515 return key1->tclass - key2->tclass;
518 static const struct hashtab_key_params roletr_key_params = {
519 .hash = role_trans_hash,
520 .cmp = role_trans_cmp,
523 struct role_trans_datum *policydb_roletr_search(struct policydb *p,
524 struct role_trans_key *key)
526 return hashtab_search(&p->role_tr, key, roletr_key_params);
530 * Initialize a policy database structure.
532 static void policydb_init(struct policydb *p)
534 memset(p, 0, sizeof(*p));
536 avtab_init(&p->te_avtab);
537 cond_policydb_init(p);
539 ebitmap_init(&p->filename_trans_ttypes);
540 ebitmap_init(&p->policycaps);
541 ebitmap_init(&p->permissive_map);
545 * The following *_index functions are used to
546 * define the val_to_name and val_to_struct arrays
547 * in a policy database structure. The val_to_name
548 * arrays are used when converting security context
549 * structures into string representations. The
550 * val_to_struct arrays are used when the attributes
551 * of a class, role, or user are needed.
554 static int common_index(void *key, void *datum, void *datap)
557 struct common_datum *comdatum;
561 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
564 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
569 static int class_index(void *key, void *datum, void *datap)
572 struct class_datum *cladatum;
576 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
579 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
580 p->class_val_to_struct[cladatum->value - 1] = cladatum;
584 static int role_index(void *key, void *datum, void *datap)
587 struct role_datum *role;
592 || role->value > p->p_roles.nprim
593 || role->bounds > p->p_roles.nprim)
596 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
597 p->role_val_to_struct[role->value - 1] = role;
601 static int type_index(void *key, void *datum, void *datap)
604 struct type_datum *typdatum;
609 if (typdatum->primary) {
611 || typdatum->value > p->p_types.nprim
612 || typdatum->bounds > p->p_types.nprim)
614 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
615 p->type_val_to_struct[typdatum->value - 1] = typdatum;
621 static int user_index(void *key, void *datum, void *datap)
624 struct user_datum *usrdatum;
629 || usrdatum->value > p->p_users.nprim
630 || usrdatum->bounds > p->p_users.nprim)
633 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
634 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
638 static int sens_index(void *key, void *datum, void *datap)
641 struct level_datum *levdatum;
646 if (!levdatum->isalias) {
647 if (!levdatum->level->sens ||
648 levdatum->level->sens > p->p_levels.nprim)
651 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
657 static int cat_index(void *key, void *datum, void *datap)
660 struct cat_datum *catdatum;
665 if (!catdatum->isalias) {
666 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
669 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
675 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
688 static void hash_eval(struct hashtab *h, const char *hash_name)
690 struct hashtab_info info;
692 hashtab_stat(h, &info);
693 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
694 hash_name, h->nel, info.slots_used, h->size,
698 static void symtab_hash_eval(struct symtab *s)
702 for (i = 0; i < SYM_NUM; i++)
703 hash_eval(&s[i].table, symtab_name[i]);
707 static inline void hash_eval(struct hashtab *h, char *hash_name)
713 * Define the other val_to_name and val_to_struct arrays
714 * in a policy database structure.
716 * Caller must clean up on failure.
718 static int policydb_index(struct policydb *p)
723 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
724 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
725 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
727 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
728 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
731 pr_debug("SELinux: %d classes, %d rules\n",
732 p->p_classes.nprim, p->te_avtab.nel);
735 avtab_hash_eval(&p->te_avtab, "rules");
736 symtab_hash_eval(p->symtab);
739 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
740 sizeof(*p->class_val_to_struct),
742 if (!p->class_val_to_struct)
745 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
746 sizeof(*p->role_val_to_struct),
748 if (!p->role_val_to_struct)
751 p->user_val_to_struct = kcalloc(p->p_users.nprim,
752 sizeof(*p->user_val_to_struct),
754 if (!p->user_val_to_struct)
757 p->type_val_to_struct = kvcalloc(p->p_types.nprim,
758 sizeof(*p->type_val_to_struct),
760 if (!p->type_val_to_struct)
763 rc = cond_init_bool_indexes(p);
767 for (i = 0; i < SYM_NUM; i++) {
768 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
771 if (!p->sym_val_to_name[i])
774 rc = hashtab_map(&p->symtab[i].table, index_f[i], p);
784 * Free any memory allocated by a policy database structure.
786 void policydb_destroy(struct policydb *p)
788 struct ocontext *c, *ctmp;
789 struct genfs *g, *gtmp;
791 struct role_allow *ra, *lra = NULL;
793 for (i = 0; i < SYM_NUM; i++) {
795 hashtab_map(&p->symtab[i].table, destroy_f[i], NULL);
796 hashtab_destroy(&p->symtab[i].table);
799 for (i = 0; i < SYM_NUM; i++)
800 kvfree(p->sym_val_to_name[i]);
802 kfree(p->class_val_to_struct);
803 kfree(p->role_val_to_struct);
804 kfree(p->user_val_to_struct);
805 kvfree(p->type_val_to_struct);
807 avtab_destroy(&p->te_avtab);
809 for (i = 0; i < OCON_NUM; i++) {
815 ocontext_destroy(ctmp, i);
817 p->ocontexts[i] = NULL;
828 ocontext_destroy(ctmp, OCON_FSUSE);
836 cond_policydb_destroy(p);
838 hashtab_map(&p->role_tr, role_tr_destroy, NULL);
839 hashtab_destroy(&p->role_tr);
841 for (ra = p->role_allow; ra; ra = ra->next) {
848 hashtab_map(&p->filename_trans, filenametr_destroy, NULL);
849 hashtab_destroy(&p->filename_trans);
851 hashtab_map(&p->range_tr, range_tr_destroy, NULL);
852 hashtab_destroy(&p->range_tr);
854 if (p->type_attr_map_array) {
855 for (i = 0; i < p->p_types.nprim; i++)
856 ebitmap_destroy(&p->type_attr_map_array[i]);
857 kvfree(p->type_attr_map_array);
860 ebitmap_destroy(&p->filename_trans_ttypes);
861 ebitmap_destroy(&p->policycaps);
862 ebitmap_destroy(&p->permissive_map);
866 * Load the initial SIDs specified in a policy database
867 * structure into a SID table.
869 int policydb_load_isids(struct policydb *p, struct sidtab *s)
871 struct ocontext *head, *c;
876 pr_err("SELinux: out of memory on SID table init\n");
880 head = p->ocontexts[OCON_ISID];
881 for (c = head; c; c = c->next) {
883 const char *name = security_get_initial_sid_context(sid);
885 if (sid == SECSID_NULL) {
886 pr_err("SELinux: SID 0 was assigned a context.\n");
891 /* Ignore initial SIDs unused by this kernel. */
895 rc = sidtab_set_initial(s, sid, &c->context[0]);
897 pr_err("SELinux: unable to load initial SID %s.\n",
906 int policydb_class_isvalid(struct policydb *p, unsigned int class)
908 if (!class || class > p->p_classes.nprim)
913 int policydb_role_isvalid(struct policydb *p, unsigned int role)
915 if (!role || role > p->p_roles.nprim)
920 int policydb_type_isvalid(struct policydb *p, unsigned int type)
922 if (!type || type > p->p_types.nprim)
928 * Return 1 if the fields in the security context
929 * structure `c' are valid. Return 0 otherwise.
931 int policydb_context_isvalid(struct policydb *p, struct context *c)
933 struct role_datum *role;
934 struct user_datum *usrdatum;
936 if (!c->role || c->role > p->p_roles.nprim)
939 if (!c->user || c->user > p->p_users.nprim)
942 if (!c->type || c->type > p->p_types.nprim)
945 if (c->role != OBJECT_R_VAL) {
947 * Role must be authorized for the type.
949 role = p->role_val_to_struct[c->role - 1];
950 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
951 /* role may not be associated with type */
955 * User must be authorized for the role.
957 usrdatum = p->user_val_to_struct[c->user - 1];
961 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
962 /* user may not be associated with role */
966 if (!mls_context_isvalid(p, c))
973 * Read a MLS range structure from a policydb binary
974 * representation file.
976 static int mls_read_range_helper(struct mls_range *r, void *fp)
982 rc = next_entry(buf, fp, sizeof(u32));
987 items = le32_to_cpu(buf[0]);
988 if (items > ARRAY_SIZE(buf)) {
989 pr_err("SELinux: mls: range overflow\n");
993 rc = next_entry(buf, fp, sizeof(u32) * items);
995 pr_err("SELinux: mls: truncated range\n");
999 r->level[0].sens = le32_to_cpu(buf[0]);
1001 r->level[1].sens = le32_to_cpu(buf[1]);
1003 r->level[1].sens = r->level[0].sens;
1005 rc = ebitmap_read(&r->level[0].cat, fp);
1007 pr_err("SELinux: mls: error reading low categories\n");
1011 rc = ebitmap_read(&r->level[1].cat, fp);
1013 pr_err("SELinux: mls: error reading high categories\n");
1017 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1019 pr_err("SELinux: mls: out of memory\n");
1026 ebitmap_destroy(&r->level[0].cat);
1032 * Read and validate a security context structure
1033 * from a policydb binary representation file.
1035 static int context_read_and_validate(struct context *c,
1042 rc = next_entry(buf, fp, sizeof buf);
1044 pr_err("SELinux: context truncated\n");
1047 c->user = le32_to_cpu(buf[0]);
1048 c->role = le32_to_cpu(buf[1]);
1049 c->type = le32_to_cpu(buf[2]);
1050 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1051 rc = mls_read_range_helper(&c->range, fp);
1053 pr_err("SELinux: error reading MLS range of context\n");
1059 if (!policydb_context_isvalid(p, c)) {
1060 pr_err("SELinux: invalid security context\n");
1070 * The following *_read functions are used to
1071 * read the symbol data from a policy database
1072 * binary representation file.
1075 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1080 if ((len == 0) || (len == (u32)-1))
1083 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1087 rc = next_entry(str, fp, len);
1098 static int perm_read(struct policydb *p, struct symtab *s, void *fp)
1101 struct perm_datum *perdatum;
1106 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1110 rc = next_entry(buf, fp, sizeof buf);
1114 len = le32_to_cpu(buf[0]);
1115 perdatum->value = le32_to_cpu(buf[1]);
1117 rc = str_read(&key, GFP_KERNEL, fp, len);
1121 rc = symtab_insert(s, key, perdatum);
1127 perm_destroy(key, perdatum, NULL);
1131 static int common_read(struct policydb *p, struct symtab *s, void *fp)
1134 struct common_datum *comdatum;
1139 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1143 rc = next_entry(buf, fp, sizeof buf);
1147 len = le32_to_cpu(buf[0]);
1148 comdatum->value = le32_to_cpu(buf[1]);
1149 nel = le32_to_cpu(buf[3]);
1151 rc = symtab_init(&comdatum->permissions, nel);
1154 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1156 rc = str_read(&key, GFP_KERNEL, fp, len);
1160 for (i = 0; i < nel; i++) {
1161 rc = perm_read(p, &comdatum->permissions, fp);
1166 rc = symtab_insert(s, key, comdatum);
1171 common_destroy(key, comdatum, NULL);
1175 static void type_set_init(struct type_set *t)
1177 ebitmap_init(&t->types);
1178 ebitmap_init(&t->negset);
1181 static int type_set_read(struct type_set *t, void *fp)
1186 if (ebitmap_read(&t->types, fp))
1188 if (ebitmap_read(&t->negset, fp))
1191 rc = next_entry(buf, fp, sizeof(u32));
1194 t->flags = le32_to_cpu(buf[0]);
1200 static int read_cons_helper(struct policydb *p,
1201 struct constraint_node **nodep,
1202 int ncons, int allowxtarget, void *fp)
1204 struct constraint_node *c, *lc;
1205 struct constraint_expr *e, *le;
1208 int rc, i, j, depth;
1211 for (i = 0; i < ncons; i++) {
1212 c = kzalloc(sizeof(*c), GFP_KERNEL);
1221 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1224 c->permissions = le32_to_cpu(buf[0]);
1225 nexpr = le32_to_cpu(buf[1]);
1228 for (j = 0; j < nexpr; j++) {
1229 e = kzalloc(sizeof(*e), GFP_KERNEL);
1238 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1241 e->expr_type = le32_to_cpu(buf[0]);
1242 e->attr = le32_to_cpu(buf[1]);
1243 e->op = le32_to_cpu(buf[2]);
1245 switch (e->expr_type) {
1257 if (depth == (CEXPR_MAXDEPTH - 1))
1262 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1264 if (depth == (CEXPR_MAXDEPTH - 1))
1267 rc = ebitmap_read(&e->names, fp);
1270 if (p->policyvers >=
1271 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1272 e->type_names = kzalloc(sizeof
1273 (*e->type_names), GFP_KERNEL);
1276 type_set_init(e->type_names);
1277 rc = type_set_read(e->type_names, fp);
1295 static int class_read(struct policydb *p, struct symtab *s, void *fp)
1298 struct class_datum *cladatum;
1300 u32 len, len2, ncons, nel;
1303 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1307 rc = next_entry(buf, fp, sizeof(u32)*6);
1311 len = le32_to_cpu(buf[0]);
1312 len2 = le32_to_cpu(buf[1]);
1313 cladatum->value = le32_to_cpu(buf[2]);
1314 nel = le32_to_cpu(buf[4]);
1316 rc = symtab_init(&cladatum->permissions, nel);
1319 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1321 ncons = le32_to_cpu(buf[5]);
1323 rc = str_read(&key, GFP_KERNEL, fp, len);
1328 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1333 cladatum->comdatum = symtab_search(&p->p_commons,
1335 if (!cladatum->comdatum) {
1336 pr_err("SELinux: unknown common %s\n",
1341 for (i = 0; i < nel; i++) {
1342 rc = perm_read(p, &cladatum->permissions, fp);
1347 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1351 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1352 /* grab the validatetrans rules */
1353 rc = next_entry(buf, fp, sizeof(u32));
1356 ncons = le32_to_cpu(buf[0]);
1357 rc = read_cons_helper(p, &cladatum->validatetrans,
1363 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1364 rc = next_entry(buf, fp, sizeof(u32) * 3);
1368 cladatum->default_user = le32_to_cpu(buf[0]);
1369 cladatum->default_role = le32_to_cpu(buf[1]);
1370 cladatum->default_range = le32_to_cpu(buf[2]);
1373 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1374 rc = next_entry(buf, fp, sizeof(u32) * 1);
1377 cladatum->default_type = le32_to_cpu(buf[0]);
1380 rc = symtab_insert(s, key, cladatum);
1386 cls_destroy(key, cladatum, NULL);
1390 static int role_read(struct policydb *p, struct symtab *s, void *fp)
1393 struct role_datum *role;
1394 int rc, to_read = 2;
1398 role = kzalloc(sizeof(*role), GFP_KERNEL);
1402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1405 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1409 len = le32_to_cpu(buf[0]);
1410 role->value = le32_to_cpu(buf[1]);
1411 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1412 role->bounds = le32_to_cpu(buf[2]);
1414 rc = str_read(&key, GFP_KERNEL, fp, len);
1418 rc = ebitmap_read(&role->dominates, fp);
1422 rc = ebitmap_read(&role->types, fp);
1426 if (strcmp(key, OBJECT_R) == 0) {
1428 if (role->value != OBJECT_R_VAL) {
1429 pr_err("SELinux: Role %s has wrong value %d\n",
1430 OBJECT_R, role->value);
1437 rc = symtab_insert(s, key, role);
1442 role_destroy(key, role, NULL);
1446 static int type_read(struct policydb *p, struct symtab *s, void *fp)
1449 struct type_datum *typdatum;
1450 int rc, to_read = 3;
1454 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1458 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1461 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1465 len = le32_to_cpu(buf[0]);
1466 typdatum->value = le32_to_cpu(buf[1]);
1467 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1468 u32 prop = le32_to_cpu(buf[2]);
1470 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1471 typdatum->primary = 1;
1472 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1473 typdatum->attribute = 1;
1475 typdatum->bounds = le32_to_cpu(buf[3]);
1477 typdatum->primary = le32_to_cpu(buf[2]);
1480 rc = str_read(&key, GFP_KERNEL, fp, len);
1484 rc = symtab_insert(s, key, typdatum);
1489 type_destroy(key, typdatum, NULL);
1495 * Read a MLS level structure from a policydb binary
1496 * representation file.
1498 static int mls_read_level(struct mls_level *lp, void *fp)
1503 memset(lp, 0, sizeof(*lp));
1505 rc = next_entry(buf, fp, sizeof buf);
1507 pr_err("SELinux: mls: truncated level\n");
1510 lp->sens = le32_to_cpu(buf[0]);
1512 rc = ebitmap_read(&lp->cat, fp);
1514 pr_err("SELinux: mls: error reading level categories\n");
1520 static int user_read(struct policydb *p, struct symtab *s, void *fp)
1523 struct user_datum *usrdatum;
1524 int rc, to_read = 2;
1528 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1532 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1535 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1539 len = le32_to_cpu(buf[0]);
1540 usrdatum->value = le32_to_cpu(buf[1]);
1541 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1542 usrdatum->bounds = le32_to_cpu(buf[2]);
1544 rc = str_read(&key, GFP_KERNEL, fp, len);
1548 rc = ebitmap_read(&usrdatum->roles, fp);
1552 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1553 rc = mls_read_range_helper(&usrdatum->range, fp);
1556 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1561 rc = symtab_insert(s, key, usrdatum);
1566 user_destroy(key, usrdatum, NULL);
1570 static int sens_read(struct policydb *p, struct symtab *s, void *fp)
1573 struct level_datum *levdatum;
1578 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1582 rc = next_entry(buf, fp, sizeof buf);
1586 len = le32_to_cpu(buf[0]);
1587 levdatum->isalias = le32_to_cpu(buf[1]);
1589 rc = str_read(&key, GFP_ATOMIC, fp, len);
1594 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1595 if (!levdatum->level)
1598 rc = mls_read_level(levdatum->level, fp);
1602 rc = symtab_insert(s, key, levdatum);
1607 sens_destroy(key, levdatum, NULL);
1611 static int cat_read(struct policydb *p, struct symtab *s, void *fp)
1614 struct cat_datum *catdatum;
1619 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1623 rc = next_entry(buf, fp, sizeof buf);
1627 len = le32_to_cpu(buf[0]);
1628 catdatum->value = le32_to_cpu(buf[1]);
1629 catdatum->isalias = le32_to_cpu(buf[2]);
1631 rc = str_read(&key, GFP_ATOMIC, fp, len);
1635 rc = symtab_insert(s, key, catdatum);
1640 cat_destroy(key, catdatum, NULL);
1644 static int (*read_f[SYM_NUM]) (struct policydb *p, struct symtab *s, void *fp) =
1656 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1658 struct user_datum *upper, *user;
1659 struct policydb *p = datap;
1662 upper = user = datum;
1663 while (upper->bounds) {
1664 struct ebitmap_node *node;
1667 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1668 pr_err("SELinux: user %s: "
1669 "too deep or looped boundary",
1674 upper = p->user_val_to_struct[upper->bounds - 1];
1675 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1676 if (ebitmap_get_bit(&upper->roles, bit))
1679 pr_err("SELinux: boundary violated policy: "
1680 "user=%s role=%s bounds=%s\n",
1681 sym_name(p, SYM_USERS, user->value - 1),
1682 sym_name(p, SYM_ROLES, bit),
1683 sym_name(p, SYM_USERS, upper->value - 1));
1692 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1694 struct role_datum *upper, *role;
1695 struct policydb *p = datap;
1698 upper = role = datum;
1699 while (upper->bounds) {
1700 struct ebitmap_node *node;
1703 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1704 pr_err("SELinux: role %s: "
1705 "too deep or looped bounds\n",
1710 upper = p->role_val_to_struct[upper->bounds - 1];
1711 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1712 if (ebitmap_get_bit(&upper->types, bit))
1715 pr_err("SELinux: boundary violated policy: "
1716 "role=%s type=%s bounds=%s\n",
1717 sym_name(p, SYM_ROLES, role->value - 1),
1718 sym_name(p, SYM_TYPES, bit),
1719 sym_name(p, SYM_ROLES, upper->value - 1));
1728 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1730 struct type_datum *upper;
1731 struct policydb *p = datap;
1735 while (upper->bounds) {
1736 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1737 pr_err("SELinux: type %s: "
1738 "too deep or looped boundary\n",
1743 upper = p->type_val_to_struct[upper->bounds - 1];
1746 if (upper->attribute) {
1747 pr_err("SELinux: type %s: "
1748 "bounded by attribute %s",
1750 sym_name(p, SYM_TYPES, upper->value - 1));
1758 static int policydb_bounds_sanity_check(struct policydb *p)
1762 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1765 rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
1769 rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
1773 rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
1780 u16 string_to_security_class(struct policydb *p, const char *name)
1782 struct class_datum *cladatum;
1784 cladatum = symtab_search(&p->p_classes, name);
1788 return cladatum->value;
1791 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1793 struct class_datum *cladatum;
1794 struct perm_datum *perdatum = NULL;
1795 struct common_datum *comdatum;
1797 if (!tclass || tclass > p->p_classes.nprim)
1800 cladatum = p->class_val_to_struct[tclass-1];
1801 comdatum = cladatum->comdatum;
1803 perdatum = symtab_search(&comdatum->permissions, name);
1805 perdatum = symtab_search(&cladatum->permissions, name);
1809 return 1U << (perdatum->value-1);
1812 static int range_read(struct policydb *p, void *fp)
1814 struct range_trans *rt = NULL;
1815 struct mls_range *r = NULL;
1820 if (p->policyvers < POLICYDB_VERSION_MLS)
1823 rc = next_entry(buf, fp, sizeof(u32));
1827 nel = le32_to_cpu(buf[0]);
1829 rc = hashtab_init(&p->range_tr, nel);
1833 for (i = 0; i < nel; i++) {
1835 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1839 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1843 rt->source_type = le32_to_cpu(buf[0]);
1844 rt->target_type = le32_to_cpu(buf[1]);
1845 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1846 rc = next_entry(buf, fp, sizeof(u32));
1849 rt->target_class = le32_to_cpu(buf[0]);
1851 rt->target_class = p->process_class;
1854 if (!policydb_type_isvalid(p, rt->source_type) ||
1855 !policydb_type_isvalid(p, rt->target_type) ||
1856 !policydb_class_isvalid(p, rt->target_class))
1860 r = kzalloc(sizeof(*r), GFP_KERNEL);
1864 rc = mls_read_range_helper(r, fp);
1869 if (!mls_range_isvalid(p, r)) {
1870 pr_warn("SELinux: rangetrans: invalid range\n");
1874 rc = hashtab_insert(&p->range_tr, rt, r, rangetr_key_params);
1881 hash_eval(&p->range_tr, "rangetr");
1889 static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
1891 struct filename_trans_key key, *ft = NULL;
1892 struct filename_trans_datum *last, *datum = NULL;
1894 u32 len, stype, otype;
1898 /* length of the path component string */
1899 rc = next_entry(buf, fp, sizeof(u32));
1902 len = le32_to_cpu(buf[0]);
1904 /* path component string */
1905 rc = str_read(&name, GFP_KERNEL, fp, len);
1909 rc = next_entry(buf, fp, sizeof(u32) * 4);
1913 stype = le32_to_cpu(buf[0]);
1914 key.ttype = le32_to_cpu(buf[1]);
1915 key.tclass = le32_to_cpu(buf[2]);
1918 otype = le32_to_cpu(buf[3]);
1921 datum = policydb_filenametr_search(p, &key);
1923 if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
1924 /* conflicting/duplicate rules are ignored */
1928 if (likely(datum->otype == otype))
1931 datum = datum->next;
1935 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
1939 ebitmap_init(&datum->stypes);
1940 datum->otype = otype;
1943 if (unlikely(last)) {
1947 ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
1951 rc = hashtab_insert(&p->filename_trans, ft, datum,
1952 filenametr_key_params);
1957 rc = ebitmap_set_bit(&p->filename_trans_ttypes,
1964 return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
1973 static int filename_trans_read_helper(struct policydb *p, void *fp)
1975 struct filename_trans_key *ft = NULL;
1976 struct filename_trans_datum **dst, *datum, *first = NULL;
1978 u32 len, ttype, tclass, ndatum, i;
1982 /* length of the path component string */
1983 rc = next_entry(buf, fp, sizeof(u32));
1986 len = le32_to_cpu(buf[0]);
1988 /* path component string */
1989 rc = str_read(&name, GFP_KERNEL, fp, len);
1993 rc = next_entry(buf, fp, sizeof(u32) * 3);
1997 ttype = le32_to_cpu(buf[0]);
1998 tclass = le32_to_cpu(buf[1]);
2000 ndatum = le32_to_cpu(buf[2]);
2002 pr_err("SELinux: Filename transition key with no datum\n");
2008 for (i = 0; i < ndatum; i++) {
2010 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
2016 /* ebitmap_read() will at least init the bitmap */
2017 rc = ebitmap_read(&datum->stypes, fp);
2021 rc = next_entry(buf, fp, sizeof(u32));
2025 datum->otype = le32_to_cpu(buf[0]);
2032 ft = kmalloc(sizeof(*ft), GFP_KERNEL);
2037 ft->tclass = tclass;
2040 rc = hashtab_insert(&p->filename_trans, ft, first,
2041 filenametr_key_params);
2043 pr_err("SELinux: Duplicate filename transition key\n");
2047 return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
2054 first = first->next;
2056 ebitmap_destroy(&datum->stypes);
2062 static int filename_trans_read(struct policydb *p, void *fp)
2068 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
2071 rc = next_entry(buf, fp, sizeof(u32));
2074 nel = le32_to_cpu(buf[0]);
2076 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
2077 p->compat_filename_trans_count = nel;
2079 rc = hashtab_init(&p->filename_trans, (1 << 11));
2083 for (i = 0; i < nel; i++) {
2084 rc = filename_trans_read_helper_compat(p, fp);
2089 rc = hashtab_init(&p->filename_trans, nel);
2093 for (i = 0; i < nel; i++) {
2094 rc = filename_trans_read_helper(p, fp);
2099 hash_eval(&p->filename_trans, "filenametr");
2103 static int genfs_read(struct policydb *p, void *fp)
2106 u32 nel, nel2, len, len2;
2108 struct ocontext *l, *c;
2109 struct ocontext *newc = NULL;
2110 struct genfs *genfs_p, *genfs;
2111 struct genfs *newgenfs = NULL;
2113 rc = next_entry(buf, fp, sizeof(u32));
2116 nel = le32_to_cpu(buf[0]);
2118 for (i = 0; i < nel; i++) {
2119 rc = next_entry(buf, fp, sizeof(u32));
2122 len = le32_to_cpu(buf[0]);
2125 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2129 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2133 for (genfs_p = NULL, genfs = p->genfs; genfs;
2134 genfs_p = genfs, genfs = genfs->next) {
2136 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2137 pr_err("SELinux: dup genfs fstype %s\n",
2141 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2144 newgenfs->next = genfs;
2146 genfs_p->next = newgenfs;
2148 p->genfs = newgenfs;
2152 rc = next_entry(buf, fp, sizeof(u32));
2156 nel2 = le32_to_cpu(buf[0]);
2157 for (j = 0; j < nel2; j++) {
2158 rc = next_entry(buf, fp, sizeof(u32));
2161 len = le32_to_cpu(buf[0]);
2164 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2168 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2172 rc = next_entry(buf, fp, sizeof(u32));
2176 newc->v.sclass = le32_to_cpu(buf[0]);
2177 rc = context_read_and_validate(&newc->context[0], p, fp);
2181 for (l = NULL, c = genfs->head; c;
2182 l = c, c = c->next) {
2184 if (!strcmp(newc->u.name, c->u.name) &&
2185 (!c->v.sclass || !newc->v.sclass ||
2186 newc->v.sclass == c->v.sclass)) {
2187 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2188 genfs->fstype, c->u.name);
2191 len = strlen(newc->u.name);
2192 len2 = strlen(c->u.name);
2208 kfree(newgenfs->fstype);
2211 ocontext_destroy(newc, OCON_FSUSE);
2216 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2221 __be64 prefixbuf[1];
2223 struct ocontext *l, *c;
2226 for (i = 0; i < info->ocon_num; i++) {
2227 rc = next_entry(buf, fp, sizeof(u32));
2230 nel = le32_to_cpu(buf[0]);
2233 for (j = 0; j < nel; j++) {
2235 c = kzalloc(sizeof(*c), GFP_KERNEL);
2241 p->ocontexts[i] = c;
2246 rc = next_entry(buf, fp, sizeof(u32));
2250 c->sid[0] = le32_to_cpu(buf[0]);
2251 rc = context_read_and_validate(&c->context[0], p, fp);
2257 rc = next_entry(buf, fp, sizeof(u32));
2260 len = le32_to_cpu(buf[0]);
2262 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2266 rc = context_read_and_validate(&c->context[0], p, fp);
2269 rc = context_read_and_validate(&c->context[1], p, fp);
2274 rc = next_entry(buf, fp, sizeof(u32)*3);
2277 c->u.port.protocol = le32_to_cpu(buf[0]);
2278 c->u.port.low_port = le32_to_cpu(buf[1]);
2279 c->u.port.high_port = le32_to_cpu(buf[2]);
2280 rc = context_read_and_validate(&c->context[0], p, fp);
2285 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2288 c->u.node.addr = nodebuf[0]; /* network order */
2289 c->u.node.mask = nodebuf[1]; /* network order */
2290 rc = context_read_and_validate(&c->context[0], p, fp);
2295 rc = next_entry(buf, fp, sizeof(u32)*2);
2300 c->v.behavior = le32_to_cpu(buf[0]);
2301 /* Determined at runtime, not in policy DB. */
2302 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2304 if (c->v.behavior > SECURITY_FS_USE_MAX)
2307 len = le32_to_cpu(buf[1]);
2308 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2312 rc = context_read_and_validate(&c->context[0], p, fp);
2319 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2322 for (k = 0; k < 4; k++)
2323 c->u.node6.addr[k] = nodebuf[k];
2324 for (k = 0; k < 4; k++)
2325 c->u.node6.mask[k] = nodebuf[k+4];
2326 rc = context_read_and_validate(&c->context[0], p, fp);
2332 u32 pkey_lo, pkey_hi;
2334 rc = next_entry(prefixbuf, fp, sizeof(u64));
2338 /* we need to have subnet_prefix in CPU order */
2339 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2341 rc = next_entry(buf, fp, sizeof(u32) * 2);
2345 pkey_lo = le32_to_cpu(buf[0]);
2346 pkey_hi = le32_to_cpu(buf[1]);
2348 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2353 c->u.ibpkey.low_pkey = pkey_lo;
2354 c->u.ibpkey.high_pkey = pkey_hi;
2356 rc = context_read_and_validate(&c->context[0],
2363 case OCON_IBENDPORT: {
2366 rc = next_entry(buf, fp, sizeof(u32) * 2);
2369 len = le32_to_cpu(buf[0]);
2371 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2375 port = le32_to_cpu(buf[1]);
2376 if (port > U8_MAX || port == 0) {
2381 c->u.ibendport.port = port;
2383 rc = context_read_and_validate(&c->context[0],
2399 * Read the configuration data from a policy database binary
2400 * representation file into a policy database structure.
2402 int policydb_read(struct policydb *p, void *fp)
2404 struct role_allow *ra, *lra;
2405 struct role_trans_key *rtk = NULL;
2406 struct role_trans_datum *rtd = NULL;
2409 u32 len, nprim, nel, perm;
2412 struct policydb_compat_info *info;
2416 /* Read the magic number and string length. */
2417 rc = next_entry(buf, fp, sizeof(u32) * 2);
2422 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2423 pr_err("SELinux: policydb magic number 0x%x does "
2424 "not match expected magic number 0x%x\n",
2425 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2430 len = le32_to_cpu(buf[1]);
2431 if (len != strlen(POLICYDB_STRING)) {
2432 pr_err("SELinux: policydb string length %d does not "
2433 "match expected length %zu\n",
2434 len, strlen(POLICYDB_STRING));
2439 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2440 if (!policydb_str) {
2441 pr_err("SELinux: unable to allocate memory for policydb "
2442 "string of length %d\n", len);
2446 rc = next_entry(policydb_str, fp, len);
2448 pr_err("SELinux: truncated policydb string identifier\n");
2449 kfree(policydb_str);
2454 policydb_str[len] = '\0';
2455 if (strcmp(policydb_str, POLICYDB_STRING)) {
2456 pr_err("SELinux: policydb string %s does not match "
2457 "my string %s\n", policydb_str, POLICYDB_STRING);
2458 kfree(policydb_str);
2461 /* Done with policydb_str. */
2462 kfree(policydb_str);
2463 policydb_str = NULL;
2465 /* Read the version and table sizes. */
2466 rc = next_entry(buf, fp, sizeof(u32)*4);
2471 p->policyvers = le32_to_cpu(buf[0]);
2472 if (p->policyvers < POLICYDB_VERSION_MIN ||
2473 p->policyvers > POLICYDB_VERSION_MAX) {
2474 pr_err("SELinux: policydb version %d does not match "
2475 "my version range %d-%d\n",
2476 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2480 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2484 if (p->policyvers < POLICYDB_VERSION_MLS) {
2485 pr_err("SELinux: security policydb version %d "
2486 "(MLS) not backwards compatible\n",
2491 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2492 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2494 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2495 rc = ebitmap_read(&p->policycaps, fp);
2500 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2501 rc = ebitmap_read(&p->permissive_map, fp);
2507 info = policydb_lookup_compat(p->policyvers);
2509 pr_err("SELinux: unable to find policy compat info "
2510 "for version %d\n", p->policyvers);
2515 if (le32_to_cpu(buf[2]) != info->sym_num ||
2516 le32_to_cpu(buf[3]) != info->ocon_num) {
2517 pr_err("SELinux: policydb table sizes (%d,%d) do "
2518 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2519 le32_to_cpu(buf[3]),
2520 info->sym_num, info->ocon_num);
2524 for (i = 0; i < info->sym_num; i++) {
2525 rc = next_entry(buf, fp, sizeof(u32)*2);
2528 nprim = le32_to_cpu(buf[0]);
2529 nel = le32_to_cpu(buf[1]);
2531 rc = symtab_init(&p->symtab[i], nel);
2535 if (i == SYM_ROLES) {
2541 for (j = 0; j < nel; j++) {
2542 rc = read_f[i](p, &p->symtab[i], fp);
2547 p->symtab[i].nprim = nprim;
2551 p->process_class = string_to_security_class(p, "process");
2552 if (!p->process_class) {
2553 pr_err("SELinux: process class is required, not defined in policy\n");
2557 rc = avtab_read(&p->te_avtab, fp, p);
2561 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2562 rc = cond_read_list(p, fp);
2567 rc = next_entry(buf, fp, sizeof(u32));
2570 nel = le32_to_cpu(buf[0]);
2572 rc = hashtab_init(&p->role_tr, nel);
2575 for (i = 0; i < nel; i++) {
2577 rtk = kmalloc(sizeof(*rtk), GFP_KERNEL);
2582 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
2586 rc = next_entry(buf, fp, sizeof(u32)*3);
2591 rtk->role = le32_to_cpu(buf[0]);
2592 rtk->type = le32_to_cpu(buf[1]);
2593 rtd->new_role = le32_to_cpu(buf[2]);
2594 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2595 rc = next_entry(buf, fp, sizeof(u32));
2598 rtk->tclass = le32_to_cpu(buf[0]);
2600 rtk->tclass = p->process_class;
2603 if (!policydb_role_isvalid(p, rtk->role) ||
2604 !policydb_type_isvalid(p, rtk->type) ||
2605 !policydb_class_isvalid(p, rtk->tclass) ||
2606 !policydb_role_isvalid(p, rtd->new_role))
2609 rc = hashtab_insert(&p->role_tr, rtk, rtd, roletr_key_params);
2617 rc = next_entry(buf, fp, sizeof(u32));
2620 nel = le32_to_cpu(buf[0]);
2622 for (i = 0; i < nel; i++) {
2624 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2631 rc = next_entry(buf, fp, sizeof(u32)*2);
2636 ra->role = le32_to_cpu(buf[0]);
2637 ra->new_role = le32_to_cpu(buf[1]);
2638 if (!policydb_role_isvalid(p, ra->role) ||
2639 !policydb_role_isvalid(p, ra->new_role))
2644 rc = filename_trans_read(p, fp);
2648 rc = policydb_index(p);
2653 perm = string_to_av_perm(p, p->process_class, "transition");
2655 pr_err("SELinux: process transition permission is required, not defined in policy\n");
2658 p->process_trans_perms = perm;
2659 perm = string_to_av_perm(p, p->process_class, "dyntransition");
2661 pr_err("SELinux: process dyntransition permission is required, not defined in policy\n");
2664 p->process_trans_perms |= perm;
2666 rc = ocontext_read(p, info, fp);
2670 rc = genfs_read(p, fp);
2674 rc = range_read(p, fp);
2679 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2680 sizeof(*p->type_attr_map_array),
2682 if (!p->type_attr_map_array)
2685 /* just in case ebitmap_init() becomes more than just a memset(0): */
2686 for (i = 0; i < p->p_types.nprim; i++)
2687 ebitmap_init(&p->type_attr_map_array[i]);
2689 for (i = 0; i < p->p_types.nprim; i++) {
2690 struct ebitmap *e = &p->type_attr_map_array[i];
2692 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2693 rc = ebitmap_read(e, fp);
2697 /* add the type itself as the degenerate case */
2698 rc = ebitmap_set_bit(e, i, 1);
2703 rc = policydb_bounds_sanity_check(p);
2713 policydb_destroy(p);
2718 * Write a MLS level structure to a policydb binary
2719 * representation file.
2721 static int mls_write_level(struct mls_level *l, void *fp)
2726 buf[0] = cpu_to_le32(l->sens);
2727 rc = put_entry(buf, sizeof(u32), 1, fp);
2731 rc = ebitmap_write(&l->cat, fp);
2739 * Write a MLS range structure to a policydb binary
2740 * representation file.
2742 static int mls_write_range_helper(struct mls_range *r, void *fp)
2748 eq = mls_level_eq(&r->level[1], &r->level[0]);
2754 buf[0] = cpu_to_le32(items-1);
2755 buf[1] = cpu_to_le32(r->level[0].sens);
2757 buf[2] = cpu_to_le32(r->level[1].sens);
2759 BUG_ON(items > ARRAY_SIZE(buf));
2761 rc = put_entry(buf, sizeof(u32), items, fp);
2765 rc = ebitmap_write(&r->level[0].cat, fp);
2769 rc = ebitmap_write(&r->level[1].cat, fp);
2777 static int sens_write(void *vkey, void *datum, void *ptr)
2780 struct level_datum *levdatum = datum;
2781 struct policy_data *pd = ptr;
2788 buf[0] = cpu_to_le32(len);
2789 buf[1] = cpu_to_le32(levdatum->isalias);
2790 rc = put_entry(buf, sizeof(u32), 2, fp);
2794 rc = put_entry(key, 1, len, fp);
2798 rc = mls_write_level(levdatum->level, fp);
2805 static int cat_write(void *vkey, void *datum, void *ptr)
2808 struct cat_datum *catdatum = datum;
2809 struct policy_data *pd = ptr;
2816 buf[0] = cpu_to_le32(len);
2817 buf[1] = cpu_to_le32(catdatum->value);
2818 buf[2] = cpu_to_le32(catdatum->isalias);
2819 rc = put_entry(buf, sizeof(u32), 3, fp);
2823 rc = put_entry(key, 1, len, fp);
2830 static int role_trans_write_one(void *key, void *datum, void *ptr)
2832 struct role_trans_key *rtk = key;
2833 struct role_trans_datum *rtd = datum;
2834 struct policy_data *pd = ptr;
2836 struct policydb *p = pd->p;
2840 buf[0] = cpu_to_le32(rtk->role);
2841 buf[1] = cpu_to_le32(rtk->type);
2842 buf[2] = cpu_to_le32(rtd->new_role);
2843 rc = put_entry(buf, sizeof(u32), 3, fp);
2846 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2847 buf[0] = cpu_to_le32(rtk->tclass);
2848 rc = put_entry(buf, sizeof(u32), 1, fp);
2855 static int role_trans_write(struct policydb *p, void *fp)
2857 struct policy_data pd = { .p = p, .fp = fp };
2861 buf[0] = cpu_to_le32(p->role_tr.nel);
2862 rc = put_entry(buf, sizeof(u32), 1, fp);
2866 return hashtab_map(&p->role_tr, role_trans_write_one, &pd);
2869 static int role_allow_write(struct role_allow *r, void *fp)
2871 struct role_allow *ra;
2877 for (ra = r; ra; ra = ra->next)
2879 buf[0] = cpu_to_le32(nel);
2880 rc = put_entry(buf, sizeof(u32), 1, fp);
2883 for (ra = r; ra; ra = ra->next) {
2884 buf[0] = cpu_to_le32(ra->role);
2885 buf[1] = cpu_to_le32(ra->new_role);
2886 rc = put_entry(buf, sizeof(u32), 2, fp);
2894 * Write a security context structure
2895 * to a policydb binary representation file.
2897 static int context_write(struct policydb *p, struct context *c,
2903 buf[0] = cpu_to_le32(c->user);
2904 buf[1] = cpu_to_le32(c->role);
2905 buf[2] = cpu_to_le32(c->type);
2907 rc = put_entry(buf, sizeof(u32), 3, fp);
2911 rc = mls_write_range_helper(&c->range, fp);
2919 * The following *_write functions are used to
2920 * write the symbol data to a policy database
2921 * binary representation file.
2924 static int perm_write(void *vkey, void *datum, void *fp)
2927 struct perm_datum *perdatum = datum;
2933 buf[0] = cpu_to_le32(len);
2934 buf[1] = cpu_to_le32(perdatum->value);
2935 rc = put_entry(buf, sizeof(u32), 2, fp);
2939 rc = put_entry(key, 1, len, fp);
2946 static int common_write(void *vkey, void *datum, void *ptr)
2949 struct common_datum *comdatum = datum;
2950 struct policy_data *pd = ptr;
2957 buf[0] = cpu_to_le32(len);
2958 buf[1] = cpu_to_le32(comdatum->value);
2959 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2960 buf[3] = cpu_to_le32(comdatum->permissions.table.nel);
2961 rc = put_entry(buf, sizeof(u32), 4, fp);
2965 rc = put_entry(key, 1, len, fp);
2969 rc = hashtab_map(&comdatum->permissions.table, perm_write, fp);
2976 static int type_set_write(struct type_set *t, void *fp)
2981 if (ebitmap_write(&t->types, fp))
2983 if (ebitmap_write(&t->negset, fp))
2986 buf[0] = cpu_to_le32(t->flags);
2987 rc = put_entry(buf, sizeof(u32), 1, fp);
2994 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2997 struct constraint_node *c;
2998 struct constraint_expr *e;
3003 for (c = node; c; c = c->next) {
3005 for (e = c->expr; e; e = e->next)
3007 buf[0] = cpu_to_le32(c->permissions);
3008 buf[1] = cpu_to_le32(nel);
3009 rc = put_entry(buf, sizeof(u32), 2, fp);
3012 for (e = c->expr; e; e = e->next) {
3013 buf[0] = cpu_to_le32(e->expr_type);
3014 buf[1] = cpu_to_le32(e->attr);
3015 buf[2] = cpu_to_le32(e->op);
3016 rc = put_entry(buf, sizeof(u32), 3, fp);
3020 switch (e->expr_type) {
3022 rc = ebitmap_write(&e->names, fp);
3025 if (p->policyvers >=
3026 POLICYDB_VERSION_CONSTRAINT_NAMES) {
3027 rc = type_set_write(e->type_names, fp);
3041 static int class_write(void *vkey, void *datum, void *ptr)
3044 struct class_datum *cladatum = datum;
3045 struct policy_data *pd = ptr;
3047 struct policydb *p = pd->p;
3048 struct constraint_node *c;
3055 if (cladatum->comkey)
3056 len2 = strlen(cladatum->comkey);
3061 for (c = cladatum->constraints; c; c = c->next)
3064 buf[0] = cpu_to_le32(len);
3065 buf[1] = cpu_to_le32(len2);
3066 buf[2] = cpu_to_le32(cladatum->value);
3067 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
3068 buf[4] = cpu_to_le32(cladatum->permissions.table.nel);
3069 buf[5] = cpu_to_le32(ncons);
3070 rc = put_entry(buf, sizeof(u32), 6, fp);
3074 rc = put_entry(key, 1, len, fp);
3078 if (cladatum->comkey) {
3079 rc = put_entry(cladatum->comkey, 1, len2, fp);
3084 rc = hashtab_map(&cladatum->permissions.table, perm_write, fp);
3088 rc = write_cons_helper(p, cladatum->constraints, fp);
3092 /* write out the validatetrans rule */
3094 for (c = cladatum->validatetrans; c; c = c->next)
3097 buf[0] = cpu_to_le32(ncons);
3098 rc = put_entry(buf, sizeof(u32), 1, fp);
3102 rc = write_cons_helper(p, cladatum->validatetrans, fp);
3106 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
3107 buf[0] = cpu_to_le32(cladatum->default_user);
3108 buf[1] = cpu_to_le32(cladatum->default_role);
3109 buf[2] = cpu_to_le32(cladatum->default_range);
3111 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
3116 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
3117 buf[0] = cpu_to_le32(cladatum->default_type);
3118 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
3126 static int role_write(void *vkey, void *datum, void *ptr)
3129 struct role_datum *role = datum;
3130 struct policy_data *pd = ptr;
3132 struct policydb *p = pd->p;
3139 buf[items++] = cpu_to_le32(len);
3140 buf[items++] = cpu_to_le32(role->value);
3141 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3142 buf[items++] = cpu_to_le32(role->bounds);
3144 BUG_ON(items > ARRAY_SIZE(buf));
3146 rc = put_entry(buf, sizeof(u32), items, fp);
3150 rc = put_entry(key, 1, len, fp);
3154 rc = ebitmap_write(&role->dominates, fp);
3158 rc = ebitmap_write(&role->types, fp);
3165 static int type_write(void *vkey, void *datum, void *ptr)
3168 struct type_datum *typdatum = datum;
3169 struct policy_data *pd = ptr;
3170 struct policydb *p = pd->p;
3178 buf[items++] = cpu_to_le32(len);
3179 buf[items++] = cpu_to_le32(typdatum->value);
3180 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3183 if (typdatum->primary)
3184 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3186 if (typdatum->attribute)
3187 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3189 buf[items++] = cpu_to_le32(properties);
3190 buf[items++] = cpu_to_le32(typdatum->bounds);
3192 buf[items++] = cpu_to_le32(typdatum->primary);
3194 BUG_ON(items > ARRAY_SIZE(buf));
3195 rc = put_entry(buf, sizeof(u32), items, fp);
3199 rc = put_entry(key, 1, len, fp);
3206 static int user_write(void *vkey, void *datum, void *ptr)
3209 struct user_datum *usrdatum = datum;
3210 struct policy_data *pd = ptr;
3211 struct policydb *p = pd->p;
3219 buf[items++] = cpu_to_le32(len);
3220 buf[items++] = cpu_to_le32(usrdatum->value);
3221 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3222 buf[items++] = cpu_to_le32(usrdatum->bounds);
3223 BUG_ON(items > ARRAY_SIZE(buf));
3224 rc = put_entry(buf, sizeof(u32), items, fp);
3228 rc = put_entry(key, 1, len, fp);
3232 rc = ebitmap_write(&usrdatum->roles, fp);
3236 rc = mls_write_range_helper(&usrdatum->range, fp);
3240 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3247 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3260 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3263 unsigned int i, j, rc;
3265 __be64 prefixbuf[1];
3269 for (i = 0; i < info->ocon_num; i++) {
3271 for (c = p->ocontexts[i]; c; c = c->next)
3273 buf[0] = cpu_to_le32(nel);
3274 rc = put_entry(buf, sizeof(u32), 1, fp);
3277 for (c = p->ocontexts[i]; c; c = c->next) {
3280 buf[0] = cpu_to_le32(c->sid[0]);
3281 rc = put_entry(buf, sizeof(u32), 1, fp);
3284 rc = context_write(p, &c->context[0], fp);
3290 len = strlen(c->u.name);
3291 buf[0] = cpu_to_le32(len);
3292 rc = put_entry(buf, sizeof(u32), 1, fp);
3295 rc = put_entry(c->u.name, 1, len, fp);
3298 rc = context_write(p, &c->context[0], fp);
3301 rc = context_write(p, &c->context[1], fp);
3306 buf[0] = cpu_to_le32(c->u.port.protocol);
3307 buf[1] = cpu_to_le32(c->u.port.low_port);
3308 buf[2] = cpu_to_le32(c->u.port.high_port);
3309 rc = put_entry(buf, sizeof(u32), 3, fp);
3312 rc = context_write(p, &c->context[0], fp);
3317 nodebuf[0] = c->u.node.addr; /* network order */
3318 nodebuf[1] = c->u.node.mask; /* network order */
3319 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3322 rc = context_write(p, &c->context[0], fp);
3327 buf[0] = cpu_to_le32(c->v.behavior);
3328 len = strlen(c->u.name);
3329 buf[1] = cpu_to_le32(len);
3330 rc = put_entry(buf, sizeof(u32), 2, fp);
3333 rc = put_entry(c->u.name, 1, len, fp);
3336 rc = context_write(p, &c->context[0], fp);
3341 for (j = 0; j < 4; j++)
3342 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3343 for (j = 0; j < 4; j++)
3344 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3345 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3348 rc = context_write(p, &c->context[0], fp);
3353 /* subnet_prefix is in CPU order */
3354 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3356 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3360 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3361 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3363 rc = put_entry(buf, sizeof(u32), 2, fp);
3366 rc = context_write(p, &c->context[0], fp);
3370 case OCON_IBENDPORT:
3371 len = strlen(c->u.ibendport.dev_name);
3372 buf[0] = cpu_to_le32(len);
3373 buf[1] = cpu_to_le32(c->u.ibendport.port);
3374 rc = put_entry(buf, sizeof(u32), 2, fp);
3377 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3380 rc = context_write(p, &c->context[0], fp);
3390 static int genfs_write(struct policydb *p, void *fp)
3392 struct genfs *genfs;
3399 for (genfs = p->genfs; genfs; genfs = genfs->next)
3401 buf[0] = cpu_to_le32(len);
3402 rc = put_entry(buf, sizeof(u32), 1, fp);
3405 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3406 len = strlen(genfs->fstype);
3407 buf[0] = cpu_to_le32(len);
3408 rc = put_entry(buf, sizeof(u32), 1, fp);
3411 rc = put_entry(genfs->fstype, 1, len, fp);
3415 for (c = genfs->head; c; c = c->next)
3417 buf[0] = cpu_to_le32(len);
3418 rc = put_entry(buf, sizeof(u32), 1, fp);
3421 for (c = genfs->head; c; c = c->next) {
3422 len = strlen(c->u.name);
3423 buf[0] = cpu_to_le32(len);
3424 rc = put_entry(buf, sizeof(u32), 1, fp);
3427 rc = put_entry(c->u.name, 1, len, fp);
3430 buf[0] = cpu_to_le32(c->v.sclass);
3431 rc = put_entry(buf, sizeof(u32), 1, fp);
3434 rc = context_write(p, &c->context[0], fp);
3442 static int range_write_helper(void *key, void *data, void *ptr)
3445 struct range_trans *rt = key;
3446 struct mls_range *r = data;
3447 struct policy_data *pd = ptr;
3449 struct policydb *p = pd->p;
3452 buf[0] = cpu_to_le32(rt->source_type);
3453 buf[1] = cpu_to_le32(rt->target_type);
3454 rc = put_entry(buf, sizeof(u32), 2, fp);
3457 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3458 buf[0] = cpu_to_le32(rt->target_class);
3459 rc = put_entry(buf, sizeof(u32), 1, fp);
3463 rc = mls_write_range_helper(r, fp);
3470 static int range_write(struct policydb *p, void *fp)
3474 struct policy_data pd;
3479 buf[0] = cpu_to_le32(p->range_tr.nel);
3480 rc = put_entry(buf, sizeof(u32), 1, fp);
3484 /* actually write all of the entries */
3485 rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
3492 static int filename_write_helper_compat(void *key, void *data, void *ptr)
3494 struct filename_trans_key *ft = key;
3495 struct filename_trans_datum *datum = data;
3496 struct ebitmap_node *node;
3500 u32 bit, len = strlen(ft->name);
3503 ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
3504 buf[0] = cpu_to_le32(len);
3505 rc = put_entry(buf, sizeof(u32), 1, fp);
3509 rc = put_entry(ft->name, sizeof(char), len, fp);
3513 buf[0] = cpu_to_le32(bit + 1);
3514 buf[1] = cpu_to_le32(ft->ttype);
3515 buf[2] = cpu_to_le32(ft->tclass);
3516 buf[3] = cpu_to_le32(datum->otype);
3518 rc = put_entry(buf, sizeof(u32), 4, fp);
3523 datum = datum->next;
3524 } while (unlikely(datum));
3529 static int filename_write_helper(void *key, void *data, void *ptr)
3531 struct filename_trans_key *ft = key;
3532 struct filename_trans_datum *datum;
3536 u32 ndatum, len = strlen(ft->name);
3538 buf[0] = cpu_to_le32(len);
3539 rc = put_entry(buf, sizeof(u32), 1, fp);
3543 rc = put_entry(ft->name, sizeof(char), len, fp);
3551 datum = datum->next;
3552 } while (unlikely(datum));
3554 buf[0] = cpu_to_le32(ft->ttype);
3555 buf[1] = cpu_to_le32(ft->tclass);
3556 buf[2] = cpu_to_le32(ndatum);
3557 rc = put_entry(buf, sizeof(u32), 3, fp);
3563 rc = ebitmap_write(&datum->stypes, fp);
3567 buf[0] = cpu_to_le32(datum->otype);
3568 rc = put_entry(buf, sizeof(u32), 1, fp);
3572 datum = datum->next;
3573 } while (unlikely(datum));
3578 static int filename_trans_write(struct policydb *p, void *fp)
3583 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3586 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
3587 buf[0] = cpu_to_le32(p->compat_filename_trans_count);
3588 rc = put_entry(buf, sizeof(u32), 1, fp);
3592 rc = hashtab_map(&p->filename_trans,
3593 filename_write_helper_compat, fp);
3595 buf[0] = cpu_to_le32(p->filename_trans.nel);
3596 rc = put_entry(buf, sizeof(u32), 1, fp);
3600 rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
3606 * Write the configuration data in a policy database
3607 * structure to a policy database binary representation
3610 int policydb_write(struct policydb *p, void *fp)
3612 unsigned int i, num_syms;
3617 struct policydb_compat_info *info;
3620 * refuse to write policy older than compressed avtab
3621 * to simplify the writer. There are other tests dropped
3622 * since we assume this throughout the writer code. Be
3623 * careful if you ever try to remove this restriction
3625 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3626 pr_err("SELinux: refusing to write policy version %d."
3627 " Because it is less than version %d\n", p->policyvers,
3628 POLICYDB_VERSION_AVTAB);
3634 config |= POLICYDB_CONFIG_MLS;
3636 if (p->reject_unknown)
3637 config |= REJECT_UNKNOWN;
3638 if (p->allow_unknown)
3639 config |= ALLOW_UNKNOWN;
3641 /* Write the magic number and string identifiers. */
3642 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3643 len = strlen(POLICYDB_STRING);
3644 buf[1] = cpu_to_le32(len);
3645 rc = put_entry(buf, sizeof(u32), 2, fp);
3648 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3652 /* Write the version, config, and table sizes. */
3653 info = policydb_lookup_compat(p->policyvers);
3655 pr_err("SELinux: compatibility lookup failed for policy "
3656 "version %d", p->policyvers);
3660 buf[0] = cpu_to_le32(p->policyvers);
3661 buf[1] = cpu_to_le32(config);
3662 buf[2] = cpu_to_le32(info->sym_num);
3663 buf[3] = cpu_to_le32(info->ocon_num);
3665 rc = put_entry(buf, sizeof(u32), 4, fp);
3669 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3670 rc = ebitmap_write(&p->policycaps, fp);
3675 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3676 rc = ebitmap_write(&p->permissive_map, fp);
3681 num_syms = info->sym_num;
3682 for (i = 0; i < num_syms; i++) {
3683 struct policy_data pd;
3688 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3689 buf[1] = cpu_to_le32(p->symtab[i].table.nel);
3691 rc = put_entry(buf, sizeof(u32), 2, fp);
3694 rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
3699 rc = avtab_write(p, &p->te_avtab, fp);
3703 rc = cond_write_list(p, fp);
3707 rc = role_trans_write(p, fp);
3711 rc = role_allow_write(p->role_allow, fp);
3715 rc = filename_trans_write(p, fp);
3719 rc = ocontext_write(p, info, fp);
3723 rc = genfs_write(p, fp);
3727 rc = range_write(p, fp);
3731 for (i = 0; i < p->p_types.nprim; i++) {
3732 struct ebitmap *e = &p->type_attr_map_array[i];
3734 rc = ebitmap_write(e, fp);