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",
908 int policydb_class_isvalid(struct policydb *p, unsigned int class)
910 if (!class || class > p->p_classes.nprim)
915 int policydb_role_isvalid(struct policydb *p, unsigned int role)
917 if (!role || role > p->p_roles.nprim)
922 int policydb_type_isvalid(struct policydb *p, unsigned int type)
924 if (!type || type > p->p_types.nprim)
930 * Return 1 if the fields in the security context
931 * structure `c' are valid. Return 0 otherwise.
933 int policydb_context_isvalid(struct policydb *p, struct context *c)
935 struct role_datum *role;
936 struct user_datum *usrdatum;
938 if (!c->role || c->role > p->p_roles.nprim)
941 if (!c->user || c->user > p->p_users.nprim)
944 if (!c->type || c->type > p->p_types.nprim)
947 if (c->role != OBJECT_R_VAL) {
949 * Role must be authorized for the type.
951 role = p->role_val_to_struct[c->role - 1];
952 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
953 /* role may not be associated with type */
957 * User must be authorized for the role.
959 usrdatum = p->user_val_to_struct[c->user - 1];
963 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
964 /* user may not be associated with role */
968 if (!mls_context_isvalid(p, c))
975 * Read a MLS range structure from a policydb binary
976 * representation file.
978 static int mls_read_range_helper(struct mls_range *r, void *fp)
984 rc = next_entry(buf, fp, sizeof(u32));
989 items = le32_to_cpu(buf[0]);
990 if (items > ARRAY_SIZE(buf)) {
991 pr_err("SELinux: mls: range overflow\n");
995 rc = next_entry(buf, fp, sizeof(u32) * items);
997 pr_err("SELinux: mls: truncated range\n");
1001 r->level[0].sens = le32_to_cpu(buf[0]);
1003 r->level[1].sens = le32_to_cpu(buf[1]);
1005 r->level[1].sens = r->level[0].sens;
1007 rc = ebitmap_read(&r->level[0].cat, fp);
1009 pr_err("SELinux: mls: error reading low categories\n");
1013 rc = ebitmap_read(&r->level[1].cat, fp);
1015 pr_err("SELinux: mls: error reading high categories\n");
1019 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1021 pr_err("SELinux: mls: out of memory\n");
1028 ebitmap_destroy(&r->level[0].cat);
1034 * Read and validate a security context structure
1035 * from a policydb binary representation file.
1037 static int context_read_and_validate(struct context *c,
1044 rc = next_entry(buf, fp, sizeof buf);
1046 pr_err("SELinux: context truncated\n");
1049 c->user = le32_to_cpu(buf[0]);
1050 c->role = le32_to_cpu(buf[1]);
1051 c->type = le32_to_cpu(buf[2]);
1052 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1053 rc = mls_read_range_helper(&c->range, fp);
1055 pr_err("SELinux: error reading MLS range of context\n");
1061 if (!policydb_context_isvalid(p, c)) {
1062 pr_err("SELinux: invalid security context\n");
1072 * The following *_read functions are used to
1073 * read the symbol data from a policy database
1074 * binary representation file.
1077 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1082 if ((len == 0) || (len == (u32)-1))
1085 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1089 rc = next_entry(str, fp, len);
1100 static int perm_read(struct policydb *p, struct symtab *s, void *fp)
1103 struct perm_datum *perdatum;
1108 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1112 rc = next_entry(buf, fp, sizeof buf);
1116 len = le32_to_cpu(buf[0]);
1117 perdatum->value = le32_to_cpu(buf[1]);
1119 rc = str_read(&key, GFP_KERNEL, fp, len);
1123 rc = symtab_insert(s, key, perdatum);
1129 perm_destroy(key, perdatum, NULL);
1133 static int common_read(struct policydb *p, struct symtab *s, void *fp)
1136 struct common_datum *comdatum;
1141 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1145 rc = next_entry(buf, fp, sizeof buf);
1149 len = le32_to_cpu(buf[0]);
1150 comdatum->value = le32_to_cpu(buf[1]);
1151 nel = le32_to_cpu(buf[3]);
1153 rc = symtab_init(&comdatum->permissions, nel);
1156 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1158 rc = str_read(&key, GFP_KERNEL, fp, len);
1162 for (i = 0; i < nel; i++) {
1163 rc = perm_read(p, &comdatum->permissions, fp);
1168 rc = symtab_insert(s, key, comdatum);
1173 common_destroy(key, comdatum, NULL);
1177 static void type_set_init(struct type_set *t)
1179 ebitmap_init(&t->types);
1180 ebitmap_init(&t->negset);
1183 static int type_set_read(struct type_set *t, void *fp)
1188 if (ebitmap_read(&t->types, fp))
1190 if (ebitmap_read(&t->negset, fp))
1193 rc = next_entry(buf, fp, sizeof(u32));
1196 t->flags = le32_to_cpu(buf[0]);
1202 static int read_cons_helper(struct policydb *p,
1203 struct constraint_node **nodep,
1204 int ncons, int allowxtarget, void *fp)
1206 struct constraint_node *c, *lc;
1207 struct constraint_expr *e, *le;
1210 int rc, i, j, depth;
1213 for (i = 0; i < ncons; i++) {
1214 c = kzalloc(sizeof(*c), GFP_KERNEL);
1223 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1226 c->permissions = le32_to_cpu(buf[0]);
1227 nexpr = le32_to_cpu(buf[1]);
1230 for (j = 0; j < nexpr; j++) {
1231 e = kzalloc(sizeof(*e), GFP_KERNEL);
1240 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1243 e->expr_type = le32_to_cpu(buf[0]);
1244 e->attr = le32_to_cpu(buf[1]);
1245 e->op = le32_to_cpu(buf[2]);
1247 switch (e->expr_type) {
1259 if (depth == (CEXPR_MAXDEPTH - 1))
1264 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1266 if (depth == (CEXPR_MAXDEPTH - 1))
1269 rc = ebitmap_read(&e->names, fp);
1272 if (p->policyvers >=
1273 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1274 e->type_names = kzalloc(sizeof
1275 (*e->type_names), GFP_KERNEL);
1278 type_set_init(e->type_names);
1279 rc = type_set_read(e->type_names, fp);
1297 static int class_read(struct policydb *p, struct symtab *s, void *fp)
1300 struct class_datum *cladatum;
1302 u32 len, len2, ncons, nel;
1305 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1309 rc = next_entry(buf, fp, sizeof(u32)*6);
1313 len = le32_to_cpu(buf[0]);
1314 len2 = le32_to_cpu(buf[1]);
1315 cladatum->value = le32_to_cpu(buf[2]);
1316 nel = le32_to_cpu(buf[4]);
1318 rc = symtab_init(&cladatum->permissions, nel);
1321 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1323 ncons = le32_to_cpu(buf[5]);
1325 rc = str_read(&key, GFP_KERNEL, fp, len);
1330 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1335 cladatum->comdatum = symtab_search(&p->p_commons,
1337 if (!cladatum->comdatum) {
1338 pr_err("SELinux: unknown common %s\n",
1343 for (i = 0; i < nel; i++) {
1344 rc = perm_read(p, &cladatum->permissions, fp);
1349 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1353 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1354 /* grab the validatetrans rules */
1355 rc = next_entry(buf, fp, sizeof(u32));
1358 ncons = le32_to_cpu(buf[0]);
1359 rc = read_cons_helper(p, &cladatum->validatetrans,
1365 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1366 rc = next_entry(buf, fp, sizeof(u32) * 3);
1370 cladatum->default_user = le32_to_cpu(buf[0]);
1371 cladatum->default_role = le32_to_cpu(buf[1]);
1372 cladatum->default_range = le32_to_cpu(buf[2]);
1375 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1376 rc = next_entry(buf, fp, sizeof(u32) * 1);
1379 cladatum->default_type = le32_to_cpu(buf[0]);
1382 rc = symtab_insert(s, key, cladatum);
1388 cls_destroy(key, cladatum, NULL);
1392 static int role_read(struct policydb *p, struct symtab *s, void *fp)
1395 struct role_datum *role;
1396 int rc, to_read = 2;
1400 role = kzalloc(sizeof(*role), GFP_KERNEL);
1404 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1407 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1411 len = le32_to_cpu(buf[0]);
1412 role->value = le32_to_cpu(buf[1]);
1413 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1414 role->bounds = le32_to_cpu(buf[2]);
1416 rc = str_read(&key, GFP_KERNEL, fp, len);
1420 rc = ebitmap_read(&role->dominates, fp);
1424 rc = ebitmap_read(&role->types, fp);
1428 if (strcmp(key, OBJECT_R) == 0) {
1430 if (role->value != OBJECT_R_VAL) {
1431 pr_err("SELinux: Role %s has wrong value %d\n",
1432 OBJECT_R, role->value);
1439 rc = symtab_insert(s, key, role);
1444 role_destroy(key, role, NULL);
1448 static int type_read(struct policydb *p, struct symtab *s, void *fp)
1451 struct type_datum *typdatum;
1452 int rc, to_read = 3;
1456 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1460 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1463 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1467 len = le32_to_cpu(buf[0]);
1468 typdatum->value = le32_to_cpu(buf[1]);
1469 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1470 u32 prop = le32_to_cpu(buf[2]);
1472 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1473 typdatum->primary = 1;
1474 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1475 typdatum->attribute = 1;
1477 typdatum->bounds = le32_to_cpu(buf[3]);
1479 typdatum->primary = le32_to_cpu(buf[2]);
1482 rc = str_read(&key, GFP_KERNEL, fp, len);
1486 rc = symtab_insert(s, key, typdatum);
1491 type_destroy(key, typdatum, NULL);
1497 * Read a MLS level structure from a policydb binary
1498 * representation file.
1500 static int mls_read_level(struct mls_level *lp, void *fp)
1505 memset(lp, 0, sizeof(*lp));
1507 rc = next_entry(buf, fp, sizeof buf);
1509 pr_err("SELinux: mls: truncated level\n");
1512 lp->sens = le32_to_cpu(buf[0]);
1514 rc = ebitmap_read(&lp->cat, fp);
1516 pr_err("SELinux: mls: error reading level categories\n");
1522 static int user_read(struct policydb *p, struct symtab *s, void *fp)
1525 struct user_datum *usrdatum;
1526 int rc, to_read = 2;
1530 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1534 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1537 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1541 len = le32_to_cpu(buf[0]);
1542 usrdatum->value = le32_to_cpu(buf[1]);
1543 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1544 usrdatum->bounds = le32_to_cpu(buf[2]);
1546 rc = str_read(&key, GFP_KERNEL, fp, len);
1550 rc = ebitmap_read(&usrdatum->roles, fp);
1554 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1555 rc = mls_read_range_helper(&usrdatum->range, fp);
1558 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1563 rc = symtab_insert(s, key, usrdatum);
1568 user_destroy(key, usrdatum, NULL);
1572 static int sens_read(struct policydb *p, struct symtab *s, void *fp)
1575 struct level_datum *levdatum;
1580 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1584 rc = next_entry(buf, fp, sizeof buf);
1588 len = le32_to_cpu(buf[0]);
1589 levdatum->isalias = le32_to_cpu(buf[1]);
1591 rc = str_read(&key, GFP_ATOMIC, fp, len);
1596 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1597 if (!levdatum->level)
1600 rc = mls_read_level(levdatum->level, fp);
1604 rc = symtab_insert(s, key, levdatum);
1609 sens_destroy(key, levdatum, NULL);
1613 static int cat_read(struct policydb *p, struct symtab *s, void *fp)
1616 struct cat_datum *catdatum;
1621 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1625 rc = next_entry(buf, fp, sizeof buf);
1629 len = le32_to_cpu(buf[0]);
1630 catdatum->value = le32_to_cpu(buf[1]);
1631 catdatum->isalias = le32_to_cpu(buf[2]);
1633 rc = str_read(&key, GFP_ATOMIC, fp, len);
1637 rc = symtab_insert(s, key, catdatum);
1642 cat_destroy(key, catdatum, NULL);
1646 static int (*read_f[SYM_NUM]) (struct policydb *p, struct symtab *s, void *fp) =
1658 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1660 struct user_datum *upper, *user;
1661 struct policydb *p = datap;
1664 upper = user = datum;
1665 while (upper->bounds) {
1666 struct ebitmap_node *node;
1669 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1670 pr_err("SELinux: user %s: "
1671 "too deep or looped boundary",
1676 upper = p->user_val_to_struct[upper->bounds - 1];
1677 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1678 if (ebitmap_get_bit(&upper->roles, bit))
1681 pr_err("SELinux: boundary violated policy: "
1682 "user=%s role=%s bounds=%s\n",
1683 sym_name(p, SYM_USERS, user->value - 1),
1684 sym_name(p, SYM_ROLES, bit),
1685 sym_name(p, SYM_USERS, upper->value - 1));
1694 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1696 struct role_datum *upper, *role;
1697 struct policydb *p = datap;
1700 upper = role = datum;
1701 while (upper->bounds) {
1702 struct ebitmap_node *node;
1705 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1706 pr_err("SELinux: role %s: "
1707 "too deep or looped bounds\n",
1712 upper = p->role_val_to_struct[upper->bounds - 1];
1713 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1714 if (ebitmap_get_bit(&upper->types, bit))
1717 pr_err("SELinux: boundary violated policy: "
1718 "role=%s type=%s bounds=%s\n",
1719 sym_name(p, SYM_ROLES, role->value - 1),
1720 sym_name(p, SYM_TYPES, bit),
1721 sym_name(p, SYM_ROLES, upper->value - 1));
1730 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1732 struct type_datum *upper;
1733 struct policydb *p = datap;
1737 while (upper->bounds) {
1738 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1739 pr_err("SELinux: type %s: "
1740 "too deep or looped boundary\n",
1745 upper = p->type_val_to_struct[upper->bounds - 1];
1748 if (upper->attribute) {
1749 pr_err("SELinux: type %s: "
1750 "bounded by attribute %s",
1752 sym_name(p, SYM_TYPES, upper->value - 1));
1760 static int policydb_bounds_sanity_check(struct policydb *p)
1764 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1767 rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
1771 rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
1775 rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
1782 u16 string_to_security_class(struct policydb *p, const char *name)
1784 struct class_datum *cladatum;
1786 cladatum = symtab_search(&p->p_classes, name);
1790 return cladatum->value;
1793 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1795 struct class_datum *cladatum;
1796 struct perm_datum *perdatum = NULL;
1797 struct common_datum *comdatum;
1799 if (!tclass || tclass > p->p_classes.nprim)
1802 cladatum = p->class_val_to_struct[tclass-1];
1803 comdatum = cladatum->comdatum;
1805 perdatum = symtab_search(&comdatum->permissions, name);
1807 perdatum = symtab_search(&cladatum->permissions, name);
1811 return 1U << (perdatum->value-1);
1814 static int range_read(struct policydb *p, void *fp)
1816 struct range_trans *rt = NULL;
1817 struct mls_range *r = NULL;
1822 if (p->policyvers < POLICYDB_VERSION_MLS)
1825 rc = next_entry(buf, fp, sizeof(u32));
1829 nel = le32_to_cpu(buf[0]);
1831 rc = hashtab_init(&p->range_tr, nel);
1835 for (i = 0; i < nel; i++) {
1837 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1841 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1845 rt->source_type = le32_to_cpu(buf[0]);
1846 rt->target_type = le32_to_cpu(buf[1]);
1847 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1848 rc = next_entry(buf, fp, sizeof(u32));
1851 rt->target_class = le32_to_cpu(buf[0]);
1853 rt->target_class = p->process_class;
1856 if (!policydb_type_isvalid(p, rt->source_type) ||
1857 !policydb_type_isvalid(p, rt->target_type) ||
1858 !policydb_class_isvalid(p, rt->target_class))
1862 r = kzalloc(sizeof(*r), GFP_KERNEL);
1866 rc = mls_read_range_helper(r, fp);
1871 if (!mls_range_isvalid(p, r)) {
1872 pr_warn("SELinux: rangetrans: invalid range\n");
1876 rc = hashtab_insert(&p->range_tr, rt, r, rangetr_key_params);
1883 hash_eval(&p->range_tr, "rangetr");
1891 static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
1893 struct filename_trans_key key, *ft = NULL;
1894 struct filename_trans_datum *last, *datum = NULL;
1896 u32 len, stype, otype;
1900 /* length of the path component string */
1901 rc = next_entry(buf, fp, sizeof(u32));
1904 len = le32_to_cpu(buf[0]);
1906 /* path component string */
1907 rc = str_read(&name, GFP_KERNEL, fp, len);
1911 rc = next_entry(buf, fp, sizeof(u32) * 4);
1915 stype = le32_to_cpu(buf[0]);
1916 key.ttype = le32_to_cpu(buf[1]);
1917 key.tclass = le32_to_cpu(buf[2]);
1920 otype = le32_to_cpu(buf[3]);
1923 datum = policydb_filenametr_search(p, &key);
1925 if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
1926 /* conflicting/duplicate rules are ignored */
1930 if (likely(datum->otype == otype))
1933 datum = datum->next;
1937 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
1941 ebitmap_init(&datum->stypes);
1942 datum->otype = otype;
1945 if (unlikely(last)) {
1949 ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
1953 rc = hashtab_insert(&p->filename_trans, ft, datum,
1954 filenametr_key_params);
1959 rc = ebitmap_set_bit(&p->filename_trans_ttypes,
1966 return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
1975 static int filename_trans_read_helper(struct policydb *p, void *fp)
1977 struct filename_trans_key *ft = NULL;
1978 struct filename_trans_datum **dst, *datum, *first = NULL;
1980 u32 len, ttype, tclass, ndatum, i;
1984 /* length of the path component string */
1985 rc = next_entry(buf, fp, sizeof(u32));
1988 len = le32_to_cpu(buf[0]);
1990 /* path component string */
1991 rc = str_read(&name, GFP_KERNEL, fp, len);
1995 rc = next_entry(buf, fp, sizeof(u32) * 3);
1999 ttype = le32_to_cpu(buf[0]);
2000 tclass = le32_to_cpu(buf[1]);
2002 ndatum = le32_to_cpu(buf[2]);
2004 pr_err("SELinux: Filename transition key with no datum\n");
2010 for (i = 0; i < ndatum; i++) {
2012 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
2018 /* ebitmap_read() will at least init the bitmap */
2019 rc = ebitmap_read(&datum->stypes, fp);
2023 rc = next_entry(buf, fp, sizeof(u32));
2027 datum->otype = le32_to_cpu(buf[0]);
2034 ft = kmalloc(sizeof(*ft), GFP_KERNEL);
2039 ft->tclass = tclass;
2042 rc = hashtab_insert(&p->filename_trans, ft, first,
2043 filenametr_key_params);
2045 pr_err("SELinux: Duplicate filename transition key\n");
2049 return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
2056 first = first->next;
2058 ebitmap_destroy(&datum->stypes);
2064 static int filename_trans_read(struct policydb *p, void *fp)
2070 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
2073 rc = next_entry(buf, fp, sizeof(u32));
2076 nel = le32_to_cpu(buf[0]);
2078 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
2079 p->compat_filename_trans_count = nel;
2081 rc = hashtab_init(&p->filename_trans, (1 << 11));
2085 for (i = 0; i < nel; i++) {
2086 rc = filename_trans_read_helper_compat(p, fp);
2091 rc = hashtab_init(&p->filename_trans, nel);
2095 for (i = 0; i < nel; i++) {
2096 rc = filename_trans_read_helper(p, fp);
2101 hash_eval(&p->filename_trans, "filenametr");
2105 static int genfs_read(struct policydb *p, void *fp)
2108 u32 nel, nel2, len, len2;
2110 struct ocontext *l, *c;
2111 struct ocontext *newc = NULL;
2112 struct genfs *genfs_p, *genfs;
2113 struct genfs *newgenfs = NULL;
2115 rc = next_entry(buf, fp, sizeof(u32));
2118 nel = le32_to_cpu(buf[0]);
2120 for (i = 0; i < nel; i++) {
2121 rc = next_entry(buf, fp, sizeof(u32));
2124 len = le32_to_cpu(buf[0]);
2127 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2131 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2135 for (genfs_p = NULL, genfs = p->genfs; genfs;
2136 genfs_p = genfs, genfs = genfs->next) {
2138 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2139 pr_err("SELinux: dup genfs fstype %s\n",
2143 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2146 newgenfs->next = genfs;
2148 genfs_p->next = newgenfs;
2150 p->genfs = newgenfs;
2154 rc = next_entry(buf, fp, sizeof(u32));
2158 nel2 = le32_to_cpu(buf[0]);
2159 for (j = 0; j < nel2; j++) {
2160 rc = next_entry(buf, fp, sizeof(u32));
2163 len = le32_to_cpu(buf[0]);
2166 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2170 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2174 rc = next_entry(buf, fp, sizeof(u32));
2178 newc->v.sclass = le32_to_cpu(buf[0]);
2179 rc = context_read_and_validate(&newc->context[0], p, fp);
2183 for (l = NULL, c = genfs->head; c;
2184 l = c, c = c->next) {
2186 if (!strcmp(newc->u.name, c->u.name) &&
2187 (!c->v.sclass || !newc->v.sclass ||
2188 newc->v.sclass == c->v.sclass)) {
2189 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2190 genfs->fstype, c->u.name);
2193 len = strlen(newc->u.name);
2194 len2 = strlen(c->u.name);
2210 kfree(newgenfs->fstype);
2213 ocontext_destroy(newc, OCON_FSUSE);
2218 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2223 __be64 prefixbuf[1];
2225 struct ocontext *l, *c;
2228 for (i = 0; i < info->ocon_num; i++) {
2229 rc = next_entry(buf, fp, sizeof(u32));
2232 nel = le32_to_cpu(buf[0]);
2235 for (j = 0; j < nel; j++) {
2237 c = kzalloc(sizeof(*c), GFP_KERNEL);
2243 p->ocontexts[i] = c;
2248 rc = next_entry(buf, fp, sizeof(u32));
2252 c->sid[0] = le32_to_cpu(buf[0]);
2253 rc = context_read_and_validate(&c->context[0], p, fp);
2259 rc = next_entry(buf, fp, sizeof(u32));
2262 len = le32_to_cpu(buf[0]);
2264 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2268 rc = context_read_and_validate(&c->context[0], p, fp);
2271 rc = context_read_and_validate(&c->context[1], p, fp);
2276 rc = next_entry(buf, fp, sizeof(u32)*3);
2279 c->u.port.protocol = le32_to_cpu(buf[0]);
2280 c->u.port.low_port = le32_to_cpu(buf[1]);
2281 c->u.port.high_port = le32_to_cpu(buf[2]);
2282 rc = context_read_and_validate(&c->context[0], p, fp);
2287 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2290 c->u.node.addr = nodebuf[0]; /* network order */
2291 c->u.node.mask = nodebuf[1]; /* network order */
2292 rc = context_read_and_validate(&c->context[0], p, fp);
2297 rc = next_entry(buf, fp, sizeof(u32)*2);
2302 c->v.behavior = le32_to_cpu(buf[0]);
2303 /* Determined at runtime, not in policy DB. */
2304 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2306 if (c->v.behavior > SECURITY_FS_USE_MAX)
2309 len = le32_to_cpu(buf[1]);
2310 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2314 rc = context_read_and_validate(&c->context[0], p, fp);
2321 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2324 for (k = 0; k < 4; k++)
2325 c->u.node6.addr[k] = nodebuf[k];
2326 for (k = 0; k < 4; k++)
2327 c->u.node6.mask[k] = nodebuf[k+4];
2328 rc = context_read_and_validate(&c->context[0], p, fp);
2334 u32 pkey_lo, pkey_hi;
2336 rc = next_entry(prefixbuf, fp, sizeof(u64));
2340 /* we need to have subnet_prefix in CPU order */
2341 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2343 rc = next_entry(buf, fp, sizeof(u32) * 2);
2347 pkey_lo = le32_to_cpu(buf[0]);
2348 pkey_hi = le32_to_cpu(buf[1]);
2350 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2355 c->u.ibpkey.low_pkey = pkey_lo;
2356 c->u.ibpkey.high_pkey = pkey_hi;
2358 rc = context_read_and_validate(&c->context[0],
2365 case OCON_IBENDPORT: {
2368 rc = next_entry(buf, fp, sizeof(u32) * 2);
2371 len = le32_to_cpu(buf[0]);
2373 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2377 port = le32_to_cpu(buf[1]);
2378 if (port > U8_MAX || port == 0) {
2383 c->u.ibendport.port = port;
2385 rc = context_read_and_validate(&c->context[0],
2401 * Read the configuration data from a policy database binary
2402 * representation file into a policy database structure.
2404 int policydb_read(struct policydb *p, void *fp)
2406 struct role_allow *ra, *lra;
2407 struct role_trans_key *rtk = NULL;
2408 struct role_trans_datum *rtd = NULL;
2411 u32 len, nprim, nel, perm;
2414 struct policydb_compat_info *info;
2418 /* Read the magic number and string length. */
2419 rc = next_entry(buf, fp, sizeof(u32) * 2);
2424 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2425 pr_err("SELinux: policydb magic number 0x%x does "
2426 "not match expected magic number 0x%x\n",
2427 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2432 len = le32_to_cpu(buf[1]);
2433 if (len != strlen(POLICYDB_STRING)) {
2434 pr_err("SELinux: policydb string length %d does not "
2435 "match expected length %zu\n",
2436 len, strlen(POLICYDB_STRING));
2441 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2442 if (!policydb_str) {
2443 pr_err("SELinux: unable to allocate memory for policydb "
2444 "string of length %d\n", len);
2448 rc = next_entry(policydb_str, fp, len);
2450 pr_err("SELinux: truncated policydb string identifier\n");
2451 kfree(policydb_str);
2456 policydb_str[len] = '\0';
2457 if (strcmp(policydb_str, POLICYDB_STRING)) {
2458 pr_err("SELinux: policydb string %s does not match "
2459 "my string %s\n", policydb_str, POLICYDB_STRING);
2460 kfree(policydb_str);
2463 /* Done with policydb_str. */
2464 kfree(policydb_str);
2465 policydb_str = NULL;
2467 /* Read the version and table sizes. */
2468 rc = next_entry(buf, fp, sizeof(u32)*4);
2473 p->policyvers = le32_to_cpu(buf[0]);
2474 if (p->policyvers < POLICYDB_VERSION_MIN ||
2475 p->policyvers > POLICYDB_VERSION_MAX) {
2476 pr_err("SELinux: policydb version %d does not match "
2477 "my version range %d-%d\n",
2478 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2482 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2486 if (p->policyvers < POLICYDB_VERSION_MLS) {
2487 pr_err("SELinux: security policydb version %d "
2488 "(MLS) not backwards compatible\n",
2493 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2494 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2496 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2497 rc = ebitmap_read(&p->policycaps, fp);
2502 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2503 rc = ebitmap_read(&p->permissive_map, fp);
2509 info = policydb_lookup_compat(p->policyvers);
2511 pr_err("SELinux: unable to find policy compat info "
2512 "for version %d\n", p->policyvers);
2517 if (le32_to_cpu(buf[2]) != info->sym_num ||
2518 le32_to_cpu(buf[3]) != info->ocon_num) {
2519 pr_err("SELinux: policydb table sizes (%d,%d) do "
2520 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2521 le32_to_cpu(buf[3]),
2522 info->sym_num, info->ocon_num);
2526 for (i = 0; i < info->sym_num; i++) {
2527 rc = next_entry(buf, fp, sizeof(u32)*2);
2530 nprim = le32_to_cpu(buf[0]);
2531 nel = le32_to_cpu(buf[1]);
2533 rc = symtab_init(&p->symtab[i], nel);
2537 if (i == SYM_ROLES) {
2543 for (j = 0; j < nel; j++) {
2544 rc = read_f[i](p, &p->symtab[i], fp);
2549 p->symtab[i].nprim = nprim;
2553 p->process_class = string_to_security_class(p, "process");
2554 if (!p->process_class) {
2555 pr_err("SELinux: process class is required, not defined in policy\n");
2559 rc = avtab_read(&p->te_avtab, fp, p);
2563 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2564 rc = cond_read_list(p, fp);
2569 rc = next_entry(buf, fp, sizeof(u32));
2572 nel = le32_to_cpu(buf[0]);
2574 rc = hashtab_init(&p->role_tr, nel);
2577 for (i = 0; i < nel; i++) {
2579 rtk = kmalloc(sizeof(*rtk), GFP_KERNEL);
2584 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
2588 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 (*write_f[SYM_NUM]) (void *key, void *datum,
3262 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3265 unsigned int i, j, rc;
3267 __be64 prefixbuf[1];
3271 for (i = 0; i < info->ocon_num; i++) {
3273 for (c = p->ocontexts[i]; c; c = c->next)
3275 buf[0] = cpu_to_le32(nel);
3276 rc = put_entry(buf, sizeof(u32), 1, fp);
3279 for (c = p->ocontexts[i]; c; c = c->next) {
3282 buf[0] = cpu_to_le32(c->sid[0]);
3283 rc = put_entry(buf, sizeof(u32), 1, fp);
3286 rc = context_write(p, &c->context[0], fp);
3292 len = strlen(c->u.name);
3293 buf[0] = cpu_to_le32(len);
3294 rc = put_entry(buf, sizeof(u32), 1, fp);
3297 rc = put_entry(c->u.name, 1, len, fp);
3300 rc = context_write(p, &c->context[0], fp);
3303 rc = context_write(p, &c->context[1], fp);
3308 buf[0] = cpu_to_le32(c->u.port.protocol);
3309 buf[1] = cpu_to_le32(c->u.port.low_port);
3310 buf[2] = cpu_to_le32(c->u.port.high_port);
3311 rc = put_entry(buf, sizeof(u32), 3, fp);
3314 rc = context_write(p, &c->context[0], fp);
3319 nodebuf[0] = c->u.node.addr; /* network order */
3320 nodebuf[1] = c->u.node.mask; /* network order */
3321 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3324 rc = context_write(p, &c->context[0], fp);
3329 buf[0] = cpu_to_le32(c->v.behavior);
3330 len = strlen(c->u.name);
3331 buf[1] = cpu_to_le32(len);
3332 rc = put_entry(buf, sizeof(u32), 2, fp);
3335 rc = put_entry(c->u.name, 1, len, fp);
3338 rc = context_write(p, &c->context[0], fp);
3343 for (j = 0; j < 4; j++)
3344 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3345 for (j = 0; j < 4; j++)
3346 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3347 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3350 rc = context_write(p, &c->context[0], fp);
3355 /* subnet_prefix is in CPU order */
3356 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3358 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3362 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3363 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3365 rc = put_entry(buf, sizeof(u32), 2, fp);
3368 rc = context_write(p, &c->context[0], fp);
3372 case OCON_IBENDPORT:
3373 len = strlen(c->u.ibendport.dev_name);
3374 buf[0] = cpu_to_le32(len);
3375 buf[1] = cpu_to_le32(c->u.ibendport.port);
3376 rc = put_entry(buf, sizeof(u32), 2, fp);
3379 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3382 rc = context_write(p, &c->context[0], fp);
3392 static int genfs_write(struct policydb *p, void *fp)
3394 struct genfs *genfs;
3401 for (genfs = p->genfs; genfs; genfs = genfs->next)
3403 buf[0] = cpu_to_le32(len);
3404 rc = put_entry(buf, sizeof(u32), 1, fp);
3407 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3408 len = strlen(genfs->fstype);
3409 buf[0] = cpu_to_le32(len);
3410 rc = put_entry(buf, sizeof(u32), 1, fp);
3413 rc = put_entry(genfs->fstype, 1, len, fp);
3417 for (c = genfs->head; c; c = c->next)
3419 buf[0] = cpu_to_le32(len);
3420 rc = put_entry(buf, sizeof(u32), 1, fp);
3423 for (c = genfs->head; c; c = c->next) {
3424 len = strlen(c->u.name);
3425 buf[0] = cpu_to_le32(len);
3426 rc = put_entry(buf, sizeof(u32), 1, fp);
3429 rc = put_entry(c->u.name, 1, len, fp);
3432 buf[0] = cpu_to_le32(c->v.sclass);
3433 rc = put_entry(buf, sizeof(u32), 1, fp);
3436 rc = context_write(p, &c->context[0], fp);
3444 static int range_write_helper(void *key, void *data, void *ptr)
3447 struct range_trans *rt = key;
3448 struct mls_range *r = data;
3449 struct policy_data *pd = ptr;
3451 struct policydb *p = pd->p;
3454 buf[0] = cpu_to_le32(rt->source_type);
3455 buf[1] = cpu_to_le32(rt->target_type);
3456 rc = put_entry(buf, sizeof(u32), 2, fp);
3459 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3460 buf[0] = cpu_to_le32(rt->target_class);
3461 rc = put_entry(buf, sizeof(u32), 1, fp);
3465 rc = mls_write_range_helper(r, fp);
3472 static int range_write(struct policydb *p, void *fp)
3476 struct policy_data pd;
3481 buf[0] = cpu_to_le32(p->range_tr.nel);
3482 rc = put_entry(buf, sizeof(u32), 1, fp);
3486 /* actually write all of the entries */
3487 rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
3494 static int filename_write_helper_compat(void *key, void *data, void *ptr)
3496 struct filename_trans_key *ft = key;
3497 struct filename_trans_datum *datum = data;
3498 struct ebitmap_node *node;
3502 u32 bit, len = strlen(ft->name);
3505 ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
3506 buf[0] = cpu_to_le32(len);
3507 rc = put_entry(buf, sizeof(u32), 1, fp);
3511 rc = put_entry(ft->name, sizeof(char), len, fp);
3515 buf[0] = cpu_to_le32(bit + 1);
3516 buf[1] = cpu_to_le32(ft->ttype);
3517 buf[2] = cpu_to_le32(ft->tclass);
3518 buf[3] = cpu_to_le32(datum->otype);
3520 rc = put_entry(buf, sizeof(u32), 4, fp);
3525 datum = datum->next;
3526 } while (unlikely(datum));
3531 static int filename_write_helper(void *key, void *data, void *ptr)
3533 struct filename_trans_key *ft = key;
3534 struct filename_trans_datum *datum;
3538 u32 ndatum, len = strlen(ft->name);
3540 buf[0] = cpu_to_le32(len);
3541 rc = put_entry(buf, sizeof(u32), 1, fp);
3545 rc = put_entry(ft->name, sizeof(char), len, fp);
3553 datum = datum->next;
3554 } while (unlikely(datum));
3556 buf[0] = cpu_to_le32(ft->ttype);
3557 buf[1] = cpu_to_le32(ft->tclass);
3558 buf[2] = cpu_to_le32(ndatum);
3559 rc = put_entry(buf, sizeof(u32), 3, fp);
3565 rc = ebitmap_write(&datum->stypes, fp);
3569 buf[0] = cpu_to_le32(datum->otype);
3570 rc = put_entry(buf, sizeof(u32), 1, fp);
3574 datum = datum->next;
3575 } while (unlikely(datum));
3580 static int filename_trans_write(struct policydb *p, void *fp)
3585 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3588 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
3589 buf[0] = cpu_to_le32(p->compat_filename_trans_count);
3590 rc = put_entry(buf, sizeof(u32), 1, fp);
3594 rc = hashtab_map(&p->filename_trans,
3595 filename_write_helper_compat, fp);
3597 buf[0] = cpu_to_le32(p->filename_trans.nel);
3598 rc = put_entry(buf, sizeof(u32), 1, fp);
3602 rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
3608 * Write the configuration data in a policy database
3609 * structure to a policy database binary representation
3612 int policydb_write(struct policydb *p, void *fp)
3614 unsigned int i, num_syms;
3619 struct policydb_compat_info *info;
3622 * refuse to write policy older than compressed avtab
3623 * to simplify the writer. There are other tests dropped
3624 * since we assume this throughout the writer code. Be
3625 * careful if you ever try to remove this restriction
3627 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3628 pr_err("SELinux: refusing to write policy version %d."
3629 " Because it is less than version %d\n", p->policyvers,
3630 POLICYDB_VERSION_AVTAB);
3636 config |= POLICYDB_CONFIG_MLS;
3638 if (p->reject_unknown)
3639 config |= REJECT_UNKNOWN;
3640 if (p->allow_unknown)
3641 config |= ALLOW_UNKNOWN;
3643 /* Write the magic number and string identifiers. */
3644 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3645 len = strlen(POLICYDB_STRING);
3646 buf[1] = cpu_to_le32(len);
3647 rc = put_entry(buf, sizeof(u32), 2, fp);
3650 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3654 /* Write the version, config, and table sizes. */
3655 info = policydb_lookup_compat(p->policyvers);
3657 pr_err("SELinux: compatibility lookup failed for policy "
3658 "version %d", p->policyvers);
3662 buf[0] = cpu_to_le32(p->policyvers);
3663 buf[1] = cpu_to_le32(config);
3664 buf[2] = cpu_to_le32(info->sym_num);
3665 buf[3] = cpu_to_le32(info->ocon_num);
3667 rc = put_entry(buf, sizeof(u32), 4, fp);
3671 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3672 rc = ebitmap_write(&p->policycaps, fp);
3677 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3678 rc = ebitmap_write(&p->permissive_map, fp);
3683 num_syms = info->sym_num;
3684 for (i = 0; i < num_syms; i++) {
3685 struct policy_data pd;
3690 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3691 buf[1] = cpu_to_le32(p->symtab[i].table.nel);
3693 rc = put_entry(buf, sizeof(u32), 2, fp);
3696 rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
3701 rc = avtab_write(p, &p->te_avtab, fp);
3705 rc = cond_write_list(p, fp);
3709 rc = role_trans_write(p, fp);
3713 rc = role_allow_write(p->role_allow, fp);
3717 rc = filename_trans_write(p, fp);
3721 rc = ocontext_write(p, info, fp);
3725 rc = genfs_write(p, fp);
3729 rc = range_write(p, fp);
3733 for (i = 0; i < p->p_types.nprim; i++) {
3734 struct ebitmap *e = &p->type_attr_map_array[i];
3736 rc = ebitmap_write(e, fp);