2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
10 * Support for enhanced MLS infrastructure.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul@paul-moore.com>
18 * Added support for the policy capability bitmap
20 * Update: Mellanox Techonologies
22 * Added Infiniband support
24 * Copyright (C) 2016 Mellanox Techonologies
25 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/audit.h>
39 #include <linux/flex_array.h>
43 #include "conditional.h"
50 static const char *symtab_name[SYM_NUM] = {
62 static unsigned int symtab_sizes[SYM_NUM] = {
73 struct policydb_compat_info {
79 /* These need to be updated if SYM_NUM or OCON_NUM changes */
80 static struct policydb_compat_info policydb_compat[] = {
82 .version = POLICYDB_VERSION_BASE,
83 .sym_num = SYM_NUM - 3,
84 .ocon_num = OCON_NUM - 3,
87 .version = POLICYDB_VERSION_BOOL,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM - 3,
92 .version = POLICYDB_VERSION_IPV6,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM - 2,
97 .version = POLICYDB_VERSION_NLCLASS,
98 .sym_num = SYM_NUM - 2,
99 .ocon_num = OCON_NUM - 2,
102 .version = POLICYDB_VERSION_MLS,
104 .ocon_num = OCON_NUM - 2,
107 .version = POLICYDB_VERSION_AVTAB,
109 .ocon_num = OCON_NUM - 2,
112 .version = POLICYDB_VERSION_RANGETRANS,
114 .ocon_num = OCON_NUM - 2,
117 .version = POLICYDB_VERSION_POLCAP,
119 .ocon_num = OCON_NUM - 2,
122 .version = POLICYDB_VERSION_PERMISSIVE,
124 .ocon_num = OCON_NUM - 2,
127 .version = POLICYDB_VERSION_BOUNDARY,
129 .ocon_num = OCON_NUM - 2,
132 .version = POLICYDB_VERSION_FILENAME_TRANS,
134 .ocon_num = OCON_NUM - 2,
137 .version = POLICYDB_VERSION_ROLETRANS,
139 .ocon_num = OCON_NUM - 2,
142 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
144 .ocon_num = OCON_NUM - 2,
147 .version = POLICYDB_VERSION_DEFAULT_TYPE,
149 .ocon_num = OCON_NUM - 2,
152 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
154 .ocon_num = OCON_NUM - 2,
157 .version = POLICYDB_VERSION_XPERMS_IOCTL,
159 .ocon_num = OCON_NUM - 2,
162 .version = POLICYDB_VERSION_INFINIBAND,
164 .ocon_num = OCON_NUM,
168 static struct policydb_compat_info *policydb_lookup_compat(int version)
171 struct policydb_compat_info *info = NULL;
173 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
174 if (policydb_compat[i].version == version) {
175 info = &policydb_compat[i];
183 * Initialize the role table.
185 static int roles_init(struct policydb *p)
189 struct role_datum *role;
191 role = kzalloc(sizeof(*role), GFP_KERNEL);
196 role->value = ++p->p_roles.nprim;
197 if (role->value != OBJECT_R_VAL)
201 key = kstrdup(OBJECT_R, GFP_KERNEL);
205 rc = hashtab_insert(p->p_roles.table, key, role);
216 static u32 filenametr_hash(struct hashtab *h, const void *k)
218 const struct filename_trans *ft = k;
220 unsigned int byte_num;
223 hash = ft->stype ^ ft->ttype ^ ft->tclass;
226 while ((focus = ft->name[byte_num++]))
227 hash = partial_name_hash(focus, hash);
228 return hash & (h->size - 1);
231 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
233 const struct filename_trans *ft1 = k1;
234 const struct filename_trans *ft2 = k2;
237 v = ft1->stype - ft2->stype;
241 v = ft1->ttype - ft2->ttype;
245 v = ft1->tclass - ft2->tclass;
249 return strcmp(ft1->name, ft2->name);
253 static u32 rangetr_hash(struct hashtab *h, const void *k)
255 const struct range_trans *key = k;
256 return (key->source_type + (key->target_type << 3) +
257 (key->target_class << 5)) & (h->size - 1);
260 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
262 const struct range_trans *key1 = k1, *key2 = k2;
265 v = key1->source_type - key2->source_type;
269 v = key1->target_type - key2->target_type;
273 v = key1->target_class - key2->target_class;
279 * Initialize a policy database structure.
281 static int policydb_init(struct policydb *p)
285 memset(p, 0, sizeof(*p));
287 for (i = 0; i < SYM_NUM; i++) {
288 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
293 rc = avtab_init(&p->te_avtab);
301 rc = cond_policydb_init(p);
305 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
306 if (!p->filename_trans) {
311 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
317 ebitmap_init(&p->filename_trans_ttypes);
318 ebitmap_init(&p->policycaps);
319 ebitmap_init(&p->permissive_map);
323 hashtab_destroy(p->filename_trans);
324 hashtab_destroy(p->range_tr);
325 for (i = 0; i < SYM_NUM; i++)
326 hashtab_destroy(p->symtab[i].table);
331 * The following *_index functions are used to
332 * define the val_to_name and val_to_struct arrays
333 * in a policy database structure. The val_to_name
334 * arrays are used when converting security context
335 * structures into string representations. The
336 * val_to_struct arrays are used when the attributes
337 * of a class, role, or user are needed.
340 static int common_index(void *key, void *datum, void *datap)
343 struct common_datum *comdatum;
344 struct flex_array *fa;
348 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
351 fa = p->sym_val_to_name[SYM_COMMONS];
352 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
353 GFP_KERNEL | __GFP_ZERO))
358 static int class_index(void *key, void *datum, void *datap)
361 struct class_datum *cladatum;
362 struct flex_array *fa;
366 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
368 fa = p->sym_val_to_name[SYM_CLASSES];
369 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
370 GFP_KERNEL | __GFP_ZERO))
372 p->class_val_to_struct[cladatum->value - 1] = cladatum;
376 static int role_index(void *key, void *datum, void *datap)
379 struct role_datum *role;
380 struct flex_array *fa;
385 || role->value > p->p_roles.nprim
386 || role->bounds > p->p_roles.nprim)
389 fa = p->sym_val_to_name[SYM_ROLES];
390 if (flex_array_put_ptr(fa, role->value - 1, key,
391 GFP_KERNEL | __GFP_ZERO))
393 p->role_val_to_struct[role->value - 1] = role;
397 static int type_index(void *key, void *datum, void *datap)
400 struct type_datum *typdatum;
401 struct flex_array *fa;
406 if (typdatum->primary) {
408 || typdatum->value > p->p_types.nprim
409 || typdatum->bounds > p->p_types.nprim)
411 fa = p->sym_val_to_name[SYM_TYPES];
412 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
413 GFP_KERNEL | __GFP_ZERO))
416 fa = p->type_val_to_struct_array;
417 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
418 GFP_KERNEL | __GFP_ZERO))
425 static int user_index(void *key, void *datum, void *datap)
428 struct user_datum *usrdatum;
429 struct flex_array *fa;
434 || usrdatum->value > p->p_users.nprim
435 || usrdatum->bounds > p->p_users.nprim)
438 fa = p->sym_val_to_name[SYM_USERS];
439 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
440 GFP_KERNEL | __GFP_ZERO))
442 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
446 static int sens_index(void *key, void *datum, void *datap)
449 struct level_datum *levdatum;
450 struct flex_array *fa;
455 if (!levdatum->isalias) {
456 if (!levdatum->level->sens ||
457 levdatum->level->sens > p->p_levels.nprim)
459 fa = p->sym_val_to_name[SYM_LEVELS];
460 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
461 GFP_KERNEL | __GFP_ZERO))
468 static int cat_index(void *key, void *datum, void *datap)
471 struct cat_datum *catdatum;
472 struct flex_array *fa;
477 if (!catdatum->isalias) {
478 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
480 fa = p->sym_val_to_name[SYM_CATS];
481 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
482 GFP_KERNEL | __GFP_ZERO))
489 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
502 static void hash_eval(struct hashtab *h, const char *hash_name)
504 struct hashtab_info info;
506 hashtab_stat(h, &info);
507 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
508 "longest chain length %d\n", hash_name, h->nel,
509 info.slots_used, h->size, info.max_chain_len);
512 static void symtab_hash_eval(struct symtab *s)
516 for (i = 0; i < SYM_NUM; i++)
517 hash_eval(s[i].table, symtab_name[i]);
521 static inline void hash_eval(struct hashtab *h, char *hash_name)
527 * Define the other val_to_name and val_to_struct arrays
528 * in a policy database structure.
530 * Caller must clean up on failure.
532 static int policydb_index(struct policydb *p)
537 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
538 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
539 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
541 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
542 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
545 pr_debug("SELinux: %d classes, %d rules\n",
546 p->p_classes.nprim, p->te_avtab.nel);
549 avtab_hash_eval(&p->te_avtab, "rules");
550 symtab_hash_eval(p->symtab);
553 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
554 sizeof(*p->class_val_to_struct),
556 if (!p->class_val_to_struct)
559 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
560 sizeof(*p->role_val_to_struct),
562 if (!p->role_val_to_struct)
565 p->user_val_to_struct = kcalloc(p->p_users.nprim,
566 sizeof(*p->user_val_to_struct),
568 if (!p->user_val_to_struct)
571 /* Yes, I want the sizeof the pointer, not the structure */
572 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
574 GFP_KERNEL | __GFP_ZERO);
575 if (!p->type_val_to_struct_array)
578 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
579 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
583 rc = cond_init_bool_indexes(p);
587 for (i = 0; i < SYM_NUM; i++) {
588 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
590 GFP_KERNEL | __GFP_ZERO);
591 if (!p->sym_val_to_name[i])
594 rc = flex_array_prealloc(p->sym_val_to_name[i],
595 0, p->symtab[i].nprim,
596 GFP_KERNEL | __GFP_ZERO);
600 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
610 * The following *_destroy functions are used to
611 * free any memory allocated for each kind of
612 * symbol data in the policy database.
615 static int perm_destroy(void *key, void *datum, void *p)
622 static int common_destroy(void *key, void *datum, void *p)
624 struct common_datum *comdatum;
629 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
630 hashtab_destroy(comdatum->permissions.table);
636 static void constraint_expr_destroy(struct constraint_expr *expr)
639 ebitmap_destroy(&expr->names);
640 if (expr->type_names) {
641 ebitmap_destroy(&expr->type_names->types);
642 ebitmap_destroy(&expr->type_names->negset);
643 kfree(expr->type_names);
649 static int cls_destroy(void *key, void *datum, void *p)
651 struct class_datum *cladatum;
652 struct constraint_node *constraint, *ctemp;
653 struct constraint_expr *e, *etmp;
658 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
659 hashtab_destroy(cladatum->permissions.table);
660 constraint = cladatum->constraints;
662 e = constraint->expr;
666 constraint_expr_destroy(etmp);
669 constraint = constraint->next;
673 constraint = cladatum->validatetrans;
675 e = constraint->expr;
679 constraint_expr_destroy(etmp);
682 constraint = constraint->next;
685 kfree(cladatum->comkey);
691 static int role_destroy(void *key, void *datum, void *p)
693 struct role_datum *role;
698 ebitmap_destroy(&role->dominates);
699 ebitmap_destroy(&role->types);
705 static int type_destroy(void *key, void *datum, void *p)
712 static int user_destroy(void *key, void *datum, void *p)
714 struct user_datum *usrdatum;
719 ebitmap_destroy(&usrdatum->roles);
720 ebitmap_destroy(&usrdatum->range.level[0].cat);
721 ebitmap_destroy(&usrdatum->range.level[1].cat);
722 ebitmap_destroy(&usrdatum->dfltlevel.cat);
728 static int sens_destroy(void *key, void *datum, void *p)
730 struct level_datum *levdatum;
735 ebitmap_destroy(&levdatum->level->cat);
736 kfree(levdatum->level);
742 static int cat_destroy(void *key, void *datum, void *p)
749 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
761 static int filenametr_destroy(void *key, void *datum, void *p)
763 struct filename_trans *ft = key;
771 static int range_tr_destroy(void *key, void *datum, void *p)
773 struct mls_range *rt = datum;
775 ebitmap_destroy(&rt->level[0].cat);
776 ebitmap_destroy(&rt->level[1].cat);
782 static void ocontext_destroy(struct ocontext *c, int i)
787 context_destroy(&c->context[0]);
788 context_destroy(&c->context[1]);
789 if (i == OCON_ISID || i == OCON_FS ||
790 i == OCON_NETIF || i == OCON_FSUSE)
796 * Free any memory allocated by a policy database structure.
798 void policydb_destroy(struct policydb *p)
800 struct ocontext *c, *ctmp;
801 struct genfs *g, *gtmp;
803 struct role_allow *ra, *lra = NULL;
804 struct role_trans *tr, *ltr = NULL;
806 for (i = 0; i < SYM_NUM; i++) {
808 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
809 hashtab_destroy(p->symtab[i].table);
812 for (i = 0; i < SYM_NUM; i++) {
813 if (p->sym_val_to_name[i])
814 flex_array_free(p->sym_val_to_name[i]);
817 kfree(p->class_val_to_struct);
818 kfree(p->role_val_to_struct);
819 kfree(p->user_val_to_struct);
820 if (p->type_val_to_struct_array)
821 flex_array_free(p->type_val_to_struct_array);
823 avtab_destroy(&p->te_avtab);
825 for (i = 0; i < OCON_NUM; i++) {
831 ocontext_destroy(ctmp, i);
833 p->ocontexts[i] = NULL;
844 ocontext_destroy(ctmp, OCON_FSUSE);
852 cond_policydb_destroy(p);
854 for (tr = p->role_tr; tr; tr = tr->next) {
861 for (ra = p->role_allow; ra; ra = ra->next) {
868 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
869 hashtab_destroy(p->filename_trans);
871 hashtab_map(p->range_tr, range_tr_destroy, NULL);
872 hashtab_destroy(p->range_tr);
874 if (p->type_attr_map_array) {
875 for (i = 0; i < p->p_types.nprim; i++) {
878 e = flex_array_get(p->type_attr_map_array, i);
883 flex_array_free(p->type_attr_map_array);
886 ebitmap_destroy(&p->filename_trans_ttypes);
887 ebitmap_destroy(&p->policycaps);
888 ebitmap_destroy(&p->permissive_map);
892 * Load the initial SIDs specified in a policy database
893 * structure into a SID table.
895 int policydb_load_isids(struct policydb *p, struct sidtab *s)
897 struct ocontext *head, *c;
902 pr_err("SELinux: out of memory on SID table init\n");
906 head = p->ocontexts[OCON_ISID];
907 for (c = head; c; c = c->next) {
909 if (!c->context[0].user) {
910 pr_err("SELinux: SID %s was never defined.\n",
915 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
916 pr_err("SELinux: Initial SID %s out of range.\n",
922 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
924 pr_err("SELinux: unable to load initial SID %s.\n",
935 int policydb_class_isvalid(struct policydb *p, unsigned int class)
937 if (!class || class > p->p_classes.nprim)
942 int policydb_role_isvalid(struct policydb *p, unsigned int role)
944 if (!role || role > p->p_roles.nprim)
949 int policydb_type_isvalid(struct policydb *p, unsigned int type)
951 if (!type || type > p->p_types.nprim)
957 * Return 1 if the fields in the security context
958 * structure `c' are valid. Return 0 otherwise.
960 int policydb_context_isvalid(struct policydb *p, struct context *c)
962 struct role_datum *role;
963 struct user_datum *usrdatum;
965 if (!c->role || c->role > p->p_roles.nprim)
968 if (!c->user || c->user > p->p_users.nprim)
971 if (!c->type || c->type > p->p_types.nprim)
974 if (c->role != OBJECT_R_VAL) {
976 * Role must be authorized for the type.
978 role = p->role_val_to_struct[c->role - 1];
979 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
980 /* role may not be associated with type */
984 * User must be authorized for the role.
986 usrdatum = p->user_val_to_struct[c->user - 1];
990 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
991 /* user may not be associated with role */
995 if (!mls_context_isvalid(p, c))
1002 * Read a MLS range structure from a policydb binary
1003 * representation file.
1005 static int mls_read_range_helper(struct mls_range *r, void *fp)
1011 rc = next_entry(buf, fp, sizeof(u32));
1016 items = le32_to_cpu(buf[0]);
1017 if (items > ARRAY_SIZE(buf)) {
1018 pr_err("SELinux: mls: range overflow\n");
1022 rc = next_entry(buf, fp, sizeof(u32) * items);
1024 pr_err("SELinux: mls: truncated range\n");
1028 r->level[0].sens = le32_to_cpu(buf[0]);
1030 r->level[1].sens = le32_to_cpu(buf[1]);
1032 r->level[1].sens = r->level[0].sens;
1034 rc = ebitmap_read(&r->level[0].cat, fp);
1036 pr_err("SELinux: mls: error reading low categories\n");
1040 rc = ebitmap_read(&r->level[1].cat, fp);
1042 pr_err("SELinux: mls: error reading high categories\n");
1046 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1048 pr_err("SELinux: mls: out of memory\n");
1055 ebitmap_destroy(&r->level[0].cat);
1061 * Read and validate a security context structure
1062 * from a policydb binary representation file.
1064 static int context_read_and_validate(struct context *c,
1071 rc = next_entry(buf, fp, sizeof buf);
1073 pr_err("SELinux: context truncated\n");
1076 c->user = le32_to_cpu(buf[0]);
1077 c->role = le32_to_cpu(buf[1]);
1078 c->type = le32_to_cpu(buf[2]);
1079 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1080 rc = mls_read_range_helper(&c->range, fp);
1082 pr_err("SELinux: error reading MLS range of context\n");
1088 if (!policydb_context_isvalid(p, c)) {
1089 pr_err("SELinux: invalid security context\n");
1099 * The following *_read functions are used to
1100 * read the symbol data from a policy database
1101 * binary representation file.
1104 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1109 if ((len == 0) || (len == (u32)-1))
1112 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1116 /* it's expected the caller should free the str */
1119 rc = next_entry(str, fp, len);
1127 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1130 struct perm_datum *perdatum;
1135 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1139 rc = next_entry(buf, fp, sizeof buf);
1143 len = le32_to_cpu(buf[0]);
1144 perdatum->value = le32_to_cpu(buf[1]);
1146 rc = str_read(&key, GFP_KERNEL, fp, len);
1150 rc = hashtab_insert(h, key, perdatum);
1156 perm_destroy(key, perdatum, NULL);
1160 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1163 struct common_datum *comdatum;
1168 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1172 rc = next_entry(buf, fp, sizeof buf);
1176 len = le32_to_cpu(buf[0]);
1177 comdatum->value = le32_to_cpu(buf[1]);
1179 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1182 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1183 nel = le32_to_cpu(buf[3]);
1185 rc = str_read(&key, GFP_KERNEL, fp, len);
1189 for (i = 0; i < nel; i++) {
1190 rc = perm_read(p, comdatum->permissions.table, fp);
1195 rc = hashtab_insert(h, key, comdatum);
1200 common_destroy(key, comdatum, NULL);
1204 static void type_set_init(struct type_set *t)
1206 ebitmap_init(&t->types);
1207 ebitmap_init(&t->negset);
1210 static int type_set_read(struct type_set *t, void *fp)
1215 if (ebitmap_read(&t->types, fp))
1217 if (ebitmap_read(&t->negset, fp))
1220 rc = next_entry(buf, fp, sizeof(u32));
1223 t->flags = le32_to_cpu(buf[0]);
1229 static int read_cons_helper(struct policydb *p,
1230 struct constraint_node **nodep,
1231 int ncons, int allowxtarget, void *fp)
1233 struct constraint_node *c, *lc;
1234 struct constraint_expr *e, *le;
1237 int rc, i, j, depth;
1240 for (i = 0; i < ncons; i++) {
1241 c = kzalloc(sizeof(*c), GFP_KERNEL);
1250 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1253 c->permissions = le32_to_cpu(buf[0]);
1254 nexpr = le32_to_cpu(buf[1]);
1257 for (j = 0; j < nexpr; j++) {
1258 e = kzalloc(sizeof(*e), GFP_KERNEL);
1267 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1270 e->expr_type = le32_to_cpu(buf[0]);
1271 e->attr = le32_to_cpu(buf[1]);
1272 e->op = le32_to_cpu(buf[2]);
1274 switch (e->expr_type) {
1286 if (depth == (CEXPR_MAXDEPTH - 1))
1291 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1293 if (depth == (CEXPR_MAXDEPTH - 1))
1296 rc = ebitmap_read(&e->names, fp);
1299 if (p->policyvers >=
1300 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1301 e->type_names = kzalloc(sizeof
1306 type_set_init(e->type_names);
1307 rc = type_set_read(e->type_names, fp);
1325 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1328 struct class_datum *cladatum;
1330 u32 len, len2, ncons, nel;
1333 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1337 rc = next_entry(buf, fp, sizeof(u32)*6);
1341 len = le32_to_cpu(buf[0]);
1342 len2 = le32_to_cpu(buf[1]);
1343 cladatum->value = le32_to_cpu(buf[2]);
1345 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1348 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1349 nel = le32_to_cpu(buf[4]);
1351 ncons = le32_to_cpu(buf[5]);
1353 rc = str_read(&key, GFP_KERNEL, fp, len);
1358 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1363 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1364 if (!cladatum->comdatum) {
1365 pr_err("SELinux: unknown common %s\n",
1370 for (i = 0; i < nel; i++) {
1371 rc = perm_read(p, cladatum->permissions.table, fp);
1376 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1380 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1381 /* grab the validatetrans rules */
1382 rc = next_entry(buf, fp, sizeof(u32));
1385 ncons = le32_to_cpu(buf[0]);
1386 rc = read_cons_helper(p, &cladatum->validatetrans,
1392 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1393 rc = next_entry(buf, fp, sizeof(u32) * 3);
1397 cladatum->default_user = le32_to_cpu(buf[0]);
1398 cladatum->default_role = le32_to_cpu(buf[1]);
1399 cladatum->default_range = le32_to_cpu(buf[2]);
1402 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1403 rc = next_entry(buf, fp, sizeof(u32) * 1);
1406 cladatum->default_type = le32_to_cpu(buf[0]);
1409 rc = hashtab_insert(h, key, cladatum);
1415 cls_destroy(key, cladatum, NULL);
1419 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1422 struct role_datum *role;
1423 int rc, to_read = 2;
1427 role = kzalloc(sizeof(*role), GFP_KERNEL);
1431 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1434 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1438 len = le32_to_cpu(buf[0]);
1439 role->value = le32_to_cpu(buf[1]);
1440 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1441 role->bounds = le32_to_cpu(buf[2]);
1443 rc = str_read(&key, GFP_KERNEL, fp, len);
1447 rc = ebitmap_read(&role->dominates, fp);
1451 rc = ebitmap_read(&role->types, fp);
1455 if (strcmp(key, OBJECT_R) == 0) {
1457 if (role->value != OBJECT_R_VAL) {
1458 pr_err("SELinux: Role %s has wrong value %d\n",
1459 OBJECT_R, role->value);
1466 rc = hashtab_insert(h, key, role);
1471 role_destroy(key, role, NULL);
1475 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1478 struct type_datum *typdatum;
1479 int rc, to_read = 3;
1483 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1487 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1490 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1494 len = le32_to_cpu(buf[0]);
1495 typdatum->value = le32_to_cpu(buf[1]);
1496 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1497 u32 prop = le32_to_cpu(buf[2]);
1499 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1500 typdatum->primary = 1;
1501 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1502 typdatum->attribute = 1;
1504 typdatum->bounds = le32_to_cpu(buf[3]);
1506 typdatum->primary = le32_to_cpu(buf[2]);
1509 rc = str_read(&key, GFP_KERNEL, fp, len);
1513 rc = hashtab_insert(h, key, typdatum);
1518 type_destroy(key, typdatum, NULL);
1524 * Read a MLS level structure from a policydb binary
1525 * representation file.
1527 static int mls_read_level(struct mls_level *lp, void *fp)
1532 memset(lp, 0, sizeof(*lp));
1534 rc = next_entry(buf, fp, sizeof buf);
1536 pr_err("SELinux: mls: truncated level\n");
1539 lp->sens = le32_to_cpu(buf[0]);
1541 rc = ebitmap_read(&lp->cat, fp);
1543 pr_err("SELinux: mls: error reading level categories\n");
1549 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1552 struct user_datum *usrdatum;
1553 int rc, to_read = 2;
1557 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1561 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1564 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1568 len = le32_to_cpu(buf[0]);
1569 usrdatum->value = le32_to_cpu(buf[1]);
1570 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1571 usrdatum->bounds = le32_to_cpu(buf[2]);
1573 rc = str_read(&key, GFP_KERNEL, fp, len);
1577 rc = ebitmap_read(&usrdatum->roles, fp);
1581 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1582 rc = mls_read_range_helper(&usrdatum->range, fp);
1585 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1590 rc = hashtab_insert(h, key, usrdatum);
1595 user_destroy(key, usrdatum, NULL);
1599 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1602 struct level_datum *levdatum;
1607 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1611 rc = next_entry(buf, fp, sizeof buf);
1615 len = le32_to_cpu(buf[0]);
1616 levdatum->isalias = le32_to_cpu(buf[1]);
1618 rc = str_read(&key, GFP_ATOMIC, fp, len);
1623 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1624 if (!levdatum->level)
1627 rc = mls_read_level(levdatum->level, fp);
1631 rc = hashtab_insert(h, key, levdatum);
1636 sens_destroy(key, levdatum, NULL);
1640 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1643 struct cat_datum *catdatum;
1648 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1652 rc = next_entry(buf, fp, sizeof buf);
1656 len = le32_to_cpu(buf[0]);
1657 catdatum->value = le32_to_cpu(buf[1]);
1658 catdatum->isalias = le32_to_cpu(buf[2]);
1660 rc = str_read(&key, GFP_ATOMIC, fp, len);
1664 rc = hashtab_insert(h, key, catdatum);
1669 cat_destroy(key, catdatum, NULL);
1673 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1685 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1687 struct user_datum *upper, *user;
1688 struct policydb *p = datap;
1691 upper = user = datum;
1692 while (upper->bounds) {
1693 struct ebitmap_node *node;
1696 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1697 pr_err("SELinux: user %s: "
1698 "too deep or looped boundary",
1703 upper = p->user_val_to_struct[upper->bounds - 1];
1704 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1705 if (ebitmap_get_bit(&upper->roles, bit))
1708 pr_err("SELinux: boundary violated policy: "
1709 "user=%s role=%s bounds=%s\n",
1710 sym_name(p, SYM_USERS, user->value - 1),
1711 sym_name(p, SYM_ROLES, bit),
1712 sym_name(p, SYM_USERS, upper->value - 1));
1721 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1723 struct role_datum *upper, *role;
1724 struct policydb *p = datap;
1727 upper = role = datum;
1728 while (upper->bounds) {
1729 struct ebitmap_node *node;
1732 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1733 pr_err("SELinux: role %s: "
1734 "too deep or looped bounds\n",
1739 upper = p->role_val_to_struct[upper->bounds - 1];
1740 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1741 if (ebitmap_get_bit(&upper->types, bit))
1744 pr_err("SELinux: boundary violated policy: "
1745 "role=%s type=%s bounds=%s\n",
1746 sym_name(p, SYM_ROLES, role->value - 1),
1747 sym_name(p, SYM_TYPES, bit),
1748 sym_name(p, SYM_ROLES, upper->value - 1));
1757 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1759 struct type_datum *upper;
1760 struct policydb *p = datap;
1764 while (upper->bounds) {
1765 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1766 pr_err("SELinux: type %s: "
1767 "too deep or looped boundary\n",
1772 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1776 if (upper->attribute) {
1777 pr_err("SELinux: type %s: "
1778 "bounded by attribute %s",
1780 sym_name(p, SYM_TYPES, upper->value - 1));
1788 static int policydb_bounds_sanity_check(struct policydb *p)
1792 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1795 rc = hashtab_map(p->p_users.table,
1796 user_bounds_sanity_check, p);
1800 rc = hashtab_map(p->p_roles.table,
1801 role_bounds_sanity_check, p);
1805 rc = hashtab_map(p->p_types.table,
1806 type_bounds_sanity_check, p);
1813 u16 string_to_security_class(struct policydb *p, const char *name)
1815 struct class_datum *cladatum;
1817 cladatum = hashtab_search(p->p_classes.table, name);
1821 return cladatum->value;
1824 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1826 struct class_datum *cladatum;
1827 struct perm_datum *perdatum = NULL;
1828 struct common_datum *comdatum;
1830 if (!tclass || tclass > p->p_classes.nprim)
1833 cladatum = p->class_val_to_struct[tclass-1];
1834 comdatum = cladatum->comdatum;
1836 perdatum = hashtab_search(comdatum->permissions.table,
1839 perdatum = hashtab_search(cladatum->permissions.table,
1844 return 1U << (perdatum->value-1);
1847 static int range_read(struct policydb *p, void *fp)
1849 struct range_trans *rt = NULL;
1850 struct mls_range *r = NULL;
1855 if (p->policyvers < POLICYDB_VERSION_MLS)
1858 rc = next_entry(buf, fp, sizeof(u32));
1862 nel = le32_to_cpu(buf[0]);
1863 for (i = 0; i < nel; i++) {
1865 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1869 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1873 rt->source_type = le32_to_cpu(buf[0]);
1874 rt->target_type = le32_to_cpu(buf[1]);
1875 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1876 rc = next_entry(buf, fp, sizeof(u32));
1879 rt->target_class = le32_to_cpu(buf[0]);
1881 rt->target_class = p->process_class;
1884 if (!policydb_type_isvalid(p, rt->source_type) ||
1885 !policydb_type_isvalid(p, rt->target_type) ||
1886 !policydb_class_isvalid(p, rt->target_class))
1890 r = kzalloc(sizeof(*r), GFP_KERNEL);
1894 rc = mls_read_range_helper(r, fp);
1899 if (!mls_range_isvalid(p, r)) {
1900 pr_warn("SELinux: rangetrans: invalid range\n");
1904 rc = hashtab_insert(p->range_tr, rt, r);
1911 hash_eval(p->range_tr, "rangetr");
1919 static int filename_trans_read(struct policydb *p, void *fp)
1921 struct filename_trans *ft;
1922 struct filename_trans_datum *otype;
1928 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1931 rc = next_entry(buf, fp, sizeof(u32));
1934 nel = le32_to_cpu(buf[0]);
1936 for (i = 0; i < nel; i++) {
1941 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1946 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1950 /* length of the path component string */
1951 rc = next_entry(buf, fp, sizeof(u32));
1954 len = le32_to_cpu(buf[0]);
1956 /* path component string */
1957 rc = str_read(&name, GFP_KERNEL, fp, len);
1963 rc = next_entry(buf, fp, sizeof(u32) * 4);
1967 ft->stype = le32_to_cpu(buf[0]);
1968 ft->ttype = le32_to_cpu(buf[1]);
1969 ft->tclass = le32_to_cpu(buf[2]);
1971 otype->otype = le32_to_cpu(buf[3]);
1973 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1977 rc = hashtab_insert(p->filename_trans, ft, otype);
1980 * Do not return -EEXIST to the caller, or the system
1985 /* But free memory to avoid memory leak. */
1991 hash_eval(p->filename_trans, "filenametr");
2001 static int genfs_read(struct policydb *p, void *fp)
2004 u32 nel, nel2, len, len2;
2006 struct ocontext *l, *c;
2007 struct ocontext *newc = NULL;
2008 struct genfs *genfs_p, *genfs;
2009 struct genfs *newgenfs = NULL;
2011 rc = next_entry(buf, fp, sizeof(u32));
2014 nel = le32_to_cpu(buf[0]);
2016 for (i = 0; i < nel; i++) {
2017 rc = next_entry(buf, fp, sizeof(u32));
2020 len = le32_to_cpu(buf[0]);
2023 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2027 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2031 for (genfs_p = NULL, genfs = p->genfs; genfs;
2032 genfs_p = genfs, genfs = genfs->next) {
2034 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2035 pr_err("SELinux: dup genfs fstype %s\n",
2039 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2042 newgenfs->next = genfs;
2044 genfs_p->next = newgenfs;
2046 p->genfs = newgenfs;
2050 rc = next_entry(buf, fp, sizeof(u32));
2054 nel2 = le32_to_cpu(buf[0]);
2055 for (j = 0; j < nel2; j++) {
2056 rc = next_entry(buf, fp, sizeof(u32));
2059 len = le32_to_cpu(buf[0]);
2062 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2066 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2070 rc = next_entry(buf, fp, sizeof(u32));
2074 newc->v.sclass = le32_to_cpu(buf[0]);
2075 rc = context_read_and_validate(&newc->context[0], p, fp);
2079 for (l = NULL, c = genfs->head; c;
2080 l = c, c = c->next) {
2082 if (!strcmp(newc->u.name, c->u.name) &&
2083 (!c->v.sclass || !newc->v.sclass ||
2084 newc->v.sclass == c->v.sclass)) {
2085 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2086 genfs->fstype, c->u.name);
2089 len = strlen(newc->u.name);
2090 len2 = strlen(c->u.name);
2106 kfree(newgenfs->fstype);
2109 ocontext_destroy(newc, OCON_FSUSE);
2114 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2119 __be64 prefixbuf[1];
2121 struct ocontext *l, *c;
2124 for (i = 0; i < info->ocon_num; i++) {
2125 rc = next_entry(buf, fp, sizeof(u32));
2128 nel = le32_to_cpu(buf[0]);
2131 for (j = 0; j < nel; j++) {
2133 c = kzalloc(sizeof(*c), GFP_KERNEL);
2139 p->ocontexts[i] = c;
2144 rc = next_entry(buf, fp, sizeof(u32));
2148 c->sid[0] = le32_to_cpu(buf[0]);
2149 rc = context_read_and_validate(&c->context[0], p, fp);
2155 rc = next_entry(buf, fp, sizeof(u32));
2158 len = le32_to_cpu(buf[0]);
2160 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2164 rc = context_read_and_validate(&c->context[0], p, fp);
2167 rc = context_read_and_validate(&c->context[1], p, fp);
2172 rc = next_entry(buf, fp, sizeof(u32)*3);
2175 c->u.port.protocol = le32_to_cpu(buf[0]);
2176 c->u.port.low_port = le32_to_cpu(buf[1]);
2177 c->u.port.high_port = le32_to_cpu(buf[2]);
2178 rc = context_read_and_validate(&c->context[0], p, fp);
2183 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2186 c->u.node.addr = nodebuf[0]; /* network order */
2187 c->u.node.mask = nodebuf[1]; /* network order */
2188 rc = context_read_and_validate(&c->context[0], p, fp);
2193 rc = next_entry(buf, fp, sizeof(u32)*2);
2198 c->v.behavior = le32_to_cpu(buf[0]);
2199 /* Determined at runtime, not in policy DB. */
2200 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2202 if (c->v.behavior > SECURITY_FS_USE_MAX)
2205 len = le32_to_cpu(buf[1]);
2206 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2210 rc = context_read_and_validate(&c->context[0], p, fp);
2217 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2220 for (k = 0; k < 4; k++)
2221 c->u.node6.addr[k] = nodebuf[k];
2222 for (k = 0; k < 4; k++)
2223 c->u.node6.mask[k] = nodebuf[k+4];
2224 rc = context_read_and_validate(&c->context[0], p, fp);
2230 u32 pkey_lo, pkey_hi;
2232 rc = next_entry(prefixbuf, fp, sizeof(u64));
2236 /* we need to have subnet_prefix in CPU order */
2237 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2239 rc = next_entry(buf, fp, sizeof(u32) * 2);
2243 pkey_lo = le32_to_cpu(buf[0]);
2244 pkey_hi = le32_to_cpu(buf[1]);
2246 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2251 c->u.ibpkey.low_pkey = pkey_lo;
2252 c->u.ibpkey.high_pkey = pkey_hi;
2254 rc = context_read_and_validate(&c->context[0],
2261 case OCON_IBENDPORT: {
2264 rc = next_entry(buf, fp, sizeof(u32) * 2);
2267 len = le32_to_cpu(buf[0]);
2269 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2273 port = le32_to_cpu(buf[1]);
2274 if (port > U8_MAX || port == 0) {
2279 c->u.ibendport.port = port;
2281 rc = context_read_and_validate(&c->context[0],
2297 * Read the configuration data from a policy database binary
2298 * representation file into a policy database structure.
2300 int policydb_read(struct policydb *p, void *fp)
2302 struct role_allow *ra, *lra;
2303 struct role_trans *tr, *ltr;
2306 u32 len, nprim, nel;
2309 struct policydb_compat_info *info;
2311 rc = policydb_init(p);
2315 /* Read the magic number and string length. */
2316 rc = next_entry(buf, fp, sizeof(u32) * 2);
2321 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2322 pr_err("SELinux: policydb magic number 0x%x does "
2323 "not match expected magic number 0x%x\n",
2324 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2329 len = le32_to_cpu(buf[1]);
2330 if (len != strlen(POLICYDB_STRING)) {
2331 pr_err("SELinux: policydb string length %d does not "
2332 "match expected length %zu\n",
2333 len, strlen(POLICYDB_STRING));
2338 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2339 if (!policydb_str) {
2340 pr_err("SELinux: unable to allocate memory for policydb "
2341 "string of length %d\n", len);
2345 rc = next_entry(policydb_str, fp, len);
2347 pr_err("SELinux: truncated policydb string identifier\n");
2348 kfree(policydb_str);
2353 policydb_str[len] = '\0';
2354 if (strcmp(policydb_str, POLICYDB_STRING)) {
2355 pr_err("SELinux: policydb string %s does not match "
2356 "my string %s\n", policydb_str, POLICYDB_STRING);
2357 kfree(policydb_str);
2360 /* Done with policydb_str. */
2361 kfree(policydb_str);
2362 policydb_str = NULL;
2364 /* Read the version and table sizes. */
2365 rc = next_entry(buf, fp, sizeof(u32)*4);
2370 p->policyvers = le32_to_cpu(buf[0]);
2371 if (p->policyvers < POLICYDB_VERSION_MIN ||
2372 p->policyvers > POLICYDB_VERSION_MAX) {
2373 pr_err("SELinux: policydb version %d does not match "
2374 "my version range %d-%d\n",
2375 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2379 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2383 if (p->policyvers < POLICYDB_VERSION_MLS) {
2384 pr_err("SELinux: security policydb version %d "
2385 "(MLS) not backwards compatible\n",
2390 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2391 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2393 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2394 rc = ebitmap_read(&p->policycaps, fp);
2399 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2400 rc = ebitmap_read(&p->permissive_map, fp);
2406 info = policydb_lookup_compat(p->policyvers);
2408 pr_err("SELinux: unable to find policy compat info "
2409 "for version %d\n", p->policyvers);
2414 if (le32_to_cpu(buf[2]) != info->sym_num ||
2415 le32_to_cpu(buf[3]) != info->ocon_num) {
2416 pr_err("SELinux: policydb table sizes (%d,%d) do "
2417 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2418 le32_to_cpu(buf[3]),
2419 info->sym_num, info->ocon_num);
2423 for (i = 0; i < info->sym_num; i++) {
2424 rc = next_entry(buf, fp, sizeof(u32)*2);
2427 nprim = le32_to_cpu(buf[0]);
2428 nel = le32_to_cpu(buf[1]);
2429 for (j = 0; j < nel; j++) {
2430 rc = read_f[i](p, p->symtab[i].table, fp);
2435 p->symtab[i].nprim = nprim;
2439 p->process_class = string_to_security_class(p, "process");
2440 if (!p->process_class)
2443 rc = avtab_read(&p->te_avtab, fp, p);
2447 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2448 rc = cond_read_list(p, fp);
2453 rc = next_entry(buf, fp, sizeof(u32));
2456 nel = le32_to_cpu(buf[0]);
2458 for (i = 0; i < nel; i++) {
2460 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2467 rc = next_entry(buf, fp, sizeof(u32)*3);
2472 tr->role = le32_to_cpu(buf[0]);
2473 tr->type = le32_to_cpu(buf[1]);
2474 tr->new_role = le32_to_cpu(buf[2]);
2475 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2476 rc = next_entry(buf, fp, sizeof(u32));
2479 tr->tclass = le32_to_cpu(buf[0]);
2481 tr->tclass = p->process_class;
2484 if (!policydb_role_isvalid(p, tr->role) ||
2485 !policydb_type_isvalid(p, tr->type) ||
2486 !policydb_class_isvalid(p, tr->tclass) ||
2487 !policydb_role_isvalid(p, tr->new_role))
2492 rc = next_entry(buf, fp, sizeof(u32));
2495 nel = le32_to_cpu(buf[0]);
2497 for (i = 0; i < nel; i++) {
2499 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2506 rc = next_entry(buf, fp, sizeof(u32)*2);
2511 ra->role = le32_to_cpu(buf[0]);
2512 ra->new_role = le32_to_cpu(buf[1]);
2513 if (!policydb_role_isvalid(p, ra->role) ||
2514 !policydb_role_isvalid(p, ra->new_role))
2519 rc = filename_trans_read(p, fp);
2523 rc = policydb_index(p);
2528 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2529 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2530 if (!p->process_trans_perms)
2533 rc = ocontext_read(p, info, fp);
2537 rc = genfs_read(p, fp);
2541 rc = range_read(p, fp);
2546 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2548 GFP_KERNEL | __GFP_ZERO);
2549 if (!p->type_attr_map_array)
2552 /* preallocate so we don't have to worry about the put ever failing */
2553 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2554 GFP_KERNEL | __GFP_ZERO);
2558 for (i = 0; i < p->p_types.nprim; i++) {
2559 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2563 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2564 rc = ebitmap_read(e, fp);
2568 /* add the type itself as the degenerate case */
2569 rc = ebitmap_set_bit(e, i, 1);
2574 rc = policydb_bounds_sanity_check(p);
2582 policydb_destroy(p);
2587 * Write a MLS level structure to a policydb binary
2588 * representation file.
2590 static int mls_write_level(struct mls_level *l, void *fp)
2595 buf[0] = cpu_to_le32(l->sens);
2596 rc = put_entry(buf, sizeof(u32), 1, fp);
2600 rc = ebitmap_write(&l->cat, fp);
2608 * Write a MLS range structure to a policydb binary
2609 * representation file.
2611 static int mls_write_range_helper(struct mls_range *r, void *fp)
2617 eq = mls_level_eq(&r->level[1], &r->level[0]);
2623 buf[0] = cpu_to_le32(items-1);
2624 buf[1] = cpu_to_le32(r->level[0].sens);
2626 buf[2] = cpu_to_le32(r->level[1].sens);
2628 BUG_ON(items > ARRAY_SIZE(buf));
2630 rc = put_entry(buf, sizeof(u32), items, fp);
2634 rc = ebitmap_write(&r->level[0].cat, fp);
2638 rc = ebitmap_write(&r->level[1].cat, fp);
2646 static int sens_write(void *vkey, void *datum, void *ptr)
2649 struct level_datum *levdatum = datum;
2650 struct policy_data *pd = ptr;
2657 buf[0] = cpu_to_le32(len);
2658 buf[1] = cpu_to_le32(levdatum->isalias);
2659 rc = put_entry(buf, sizeof(u32), 2, fp);
2663 rc = put_entry(key, 1, len, fp);
2667 rc = mls_write_level(levdatum->level, fp);
2674 static int cat_write(void *vkey, void *datum, void *ptr)
2677 struct cat_datum *catdatum = datum;
2678 struct policy_data *pd = ptr;
2685 buf[0] = cpu_to_le32(len);
2686 buf[1] = cpu_to_le32(catdatum->value);
2687 buf[2] = cpu_to_le32(catdatum->isalias);
2688 rc = put_entry(buf, sizeof(u32), 3, fp);
2692 rc = put_entry(key, 1, len, fp);
2699 static int role_trans_write(struct policydb *p, void *fp)
2701 struct role_trans *r = p->role_tr;
2702 struct role_trans *tr;
2708 for (tr = r; tr; tr = tr->next)
2710 buf[0] = cpu_to_le32(nel);
2711 rc = put_entry(buf, sizeof(u32), 1, fp);
2714 for (tr = r; tr; tr = tr->next) {
2715 buf[0] = cpu_to_le32(tr->role);
2716 buf[1] = cpu_to_le32(tr->type);
2717 buf[2] = cpu_to_le32(tr->new_role);
2718 rc = put_entry(buf, sizeof(u32), 3, fp);
2721 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2722 buf[0] = cpu_to_le32(tr->tclass);
2723 rc = put_entry(buf, sizeof(u32), 1, fp);
2732 static int role_allow_write(struct role_allow *r, void *fp)
2734 struct role_allow *ra;
2740 for (ra = r; ra; ra = ra->next)
2742 buf[0] = cpu_to_le32(nel);
2743 rc = put_entry(buf, sizeof(u32), 1, fp);
2746 for (ra = r; ra; ra = ra->next) {
2747 buf[0] = cpu_to_le32(ra->role);
2748 buf[1] = cpu_to_le32(ra->new_role);
2749 rc = put_entry(buf, sizeof(u32), 2, fp);
2757 * Write a security context structure
2758 * to a policydb binary representation file.
2760 static int context_write(struct policydb *p, struct context *c,
2766 buf[0] = cpu_to_le32(c->user);
2767 buf[1] = cpu_to_le32(c->role);
2768 buf[2] = cpu_to_le32(c->type);
2770 rc = put_entry(buf, sizeof(u32), 3, fp);
2774 rc = mls_write_range_helper(&c->range, fp);
2782 * The following *_write functions are used to
2783 * write the symbol data to a policy database
2784 * binary representation file.
2787 static int perm_write(void *vkey, void *datum, void *fp)
2790 struct perm_datum *perdatum = datum;
2796 buf[0] = cpu_to_le32(len);
2797 buf[1] = cpu_to_le32(perdatum->value);
2798 rc = put_entry(buf, sizeof(u32), 2, fp);
2802 rc = put_entry(key, 1, len, fp);
2809 static int common_write(void *vkey, void *datum, void *ptr)
2812 struct common_datum *comdatum = datum;
2813 struct policy_data *pd = ptr;
2820 buf[0] = cpu_to_le32(len);
2821 buf[1] = cpu_to_le32(comdatum->value);
2822 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2823 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2824 rc = put_entry(buf, sizeof(u32), 4, fp);
2828 rc = put_entry(key, 1, len, fp);
2832 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2839 static int type_set_write(struct type_set *t, void *fp)
2844 if (ebitmap_write(&t->types, fp))
2846 if (ebitmap_write(&t->negset, fp))
2849 buf[0] = cpu_to_le32(t->flags);
2850 rc = put_entry(buf, sizeof(u32), 1, fp);
2857 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2860 struct constraint_node *c;
2861 struct constraint_expr *e;
2866 for (c = node; c; c = c->next) {
2868 for (e = c->expr; e; e = e->next)
2870 buf[0] = cpu_to_le32(c->permissions);
2871 buf[1] = cpu_to_le32(nel);
2872 rc = put_entry(buf, sizeof(u32), 2, fp);
2875 for (e = c->expr; e; e = e->next) {
2876 buf[0] = cpu_to_le32(e->expr_type);
2877 buf[1] = cpu_to_le32(e->attr);
2878 buf[2] = cpu_to_le32(e->op);
2879 rc = put_entry(buf, sizeof(u32), 3, fp);
2883 switch (e->expr_type) {
2885 rc = ebitmap_write(&e->names, fp);
2888 if (p->policyvers >=
2889 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2890 rc = type_set_write(e->type_names, fp);
2904 static int class_write(void *vkey, void *datum, void *ptr)
2907 struct class_datum *cladatum = datum;
2908 struct policy_data *pd = ptr;
2910 struct policydb *p = pd->p;
2911 struct constraint_node *c;
2918 if (cladatum->comkey)
2919 len2 = strlen(cladatum->comkey);
2924 for (c = cladatum->constraints; c; c = c->next)
2927 buf[0] = cpu_to_le32(len);
2928 buf[1] = cpu_to_le32(len2);
2929 buf[2] = cpu_to_le32(cladatum->value);
2930 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2931 if (cladatum->permissions.table)
2932 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2935 buf[5] = cpu_to_le32(ncons);
2936 rc = put_entry(buf, sizeof(u32), 6, fp);
2940 rc = put_entry(key, 1, len, fp);
2944 if (cladatum->comkey) {
2945 rc = put_entry(cladatum->comkey, 1, len2, fp);
2950 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2954 rc = write_cons_helper(p, cladatum->constraints, fp);
2958 /* write out the validatetrans rule */
2960 for (c = cladatum->validatetrans; c; c = c->next)
2963 buf[0] = cpu_to_le32(ncons);
2964 rc = put_entry(buf, sizeof(u32), 1, fp);
2968 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2972 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2973 buf[0] = cpu_to_le32(cladatum->default_user);
2974 buf[1] = cpu_to_le32(cladatum->default_role);
2975 buf[2] = cpu_to_le32(cladatum->default_range);
2977 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2982 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2983 buf[0] = cpu_to_le32(cladatum->default_type);
2984 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2992 static int role_write(void *vkey, void *datum, void *ptr)
2995 struct role_datum *role = datum;
2996 struct policy_data *pd = ptr;
2998 struct policydb *p = pd->p;
3005 buf[items++] = cpu_to_le32(len);
3006 buf[items++] = cpu_to_le32(role->value);
3007 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3008 buf[items++] = cpu_to_le32(role->bounds);
3010 BUG_ON(items > ARRAY_SIZE(buf));
3012 rc = put_entry(buf, sizeof(u32), items, fp);
3016 rc = put_entry(key, 1, len, fp);
3020 rc = ebitmap_write(&role->dominates, fp);
3024 rc = ebitmap_write(&role->types, fp);
3031 static int type_write(void *vkey, void *datum, void *ptr)
3034 struct type_datum *typdatum = datum;
3035 struct policy_data *pd = ptr;
3036 struct policydb *p = pd->p;
3044 buf[items++] = cpu_to_le32(len);
3045 buf[items++] = cpu_to_le32(typdatum->value);
3046 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3049 if (typdatum->primary)
3050 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3052 if (typdatum->attribute)
3053 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3055 buf[items++] = cpu_to_le32(properties);
3056 buf[items++] = cpu_to_le32(typdatum->bounds);
3058 buf[items++] = cpu_to_le32(typdatum->primary);
3060 BUG_ON(items > ARRAY_SIZE(buf));
3061 rc = put_entry(buf, sizeof(u32), items, fp);
3065 rc = put_entry(key, 1, len, fp);
3072 static int user_write(void *vkey, void *datum, void *ptr)
3075 struct user_datum *usrdatum = datum;
3076 struct policy_data *pd = ptr;
3077 struct policydb *p = pd->p;
3085 buf[items++] = cpu_to_le32(len);
3086 buf[items++] = cpu_to_le32(usrdatum->value);
3087 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3088 buf[items++] = cpu_to_le32(usrdatum->bounds);
3089 BUG_ON(items > ARRAY_SIZE(buf));
3090 rc = put_entry(buf, sizeof(u32), items, fp);
3094 rc = put_entry(key, 1, len, fp);
3098 rc = ebitmap_write(&usrdatum->roles, fp);
3102 rc = mls_write_range_helper(&usrdatum->range, fp);
3106 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3113 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3126 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3129 unsigned int i, j, rc;
3131 __be64 prefixbuf[1];
3135 for (i = 0; i < info->ocon_num; i++) {
3137 for (c = p->ocontexts[i]; c; c = c->next)
3139 buf[0] = cpu_to_le32(nel);
3140 rc = put_entry(buf, sizeof(u32), 1, fp);
3143 for (c = p->ocontexts[i]; c; c = c->next) {
3146 buf[0] = cpu_to_le32(c->sid[0]);
3147 rc = put_entry(buf, sizeof(u32), 1, fp);
3150 rc = context_write(p, &c->context[0], fp);
3156 len = strlen(c->u.name);
3157 buf[0] = cpu_to_le32(len);
3158 rc = put_entry(buf, sizeof(u32), 1, fp);
3161 rc = put_entry(c->u.name, 1, len, fp);
3164 rc = context_write(p, &c->context[0], fp);
3167 rc = context_write(p, &c->context[1], fp);
3172 buf[0] = cpu_to_le32(c->u.port.protocol);
3173 buf[1] = cpu_to_le32(c->u.port.low_port);
3174 buf[2] = cpu_to_le32(c->u.port.high_port);
3175 rc = put_entry(buf, sizeof(u32), 3, fp);
3178 rc = context_write(p, &c->context[0], fp);
3183 nodebuf[0] = c->u.node.addr; /* network order */
3184 nodebuf[1] = c->u.node.mask; /* network order */
3185 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3188 rc = context_write(p, &c->context[0], fp);
3193 buf[0] = cpu_to_le32(c->v.behavior);
3194 len = strlen(c->u.name);
3195 buf[1] = cpu_to_le32(len);
3196 rc = put_entry(buf, sizeof(u32), 2, fp);
3199 rc = put_entry(c->u.name, 1, len, fp);
3202 rc = context_write(p, &c->context[0], fp);
3207 for (j = 0; j < 4; j++)
3208 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3209 for (j = 0; j < 4; j++)
3210 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3211 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3214 rc = context_write(p, &c->context[0], fp);
3219 /* subnet_prefix is in CPU order */
3220 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3222 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3226 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3227 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3229 rc = put_entry(buf, sizeof(u32), 2, fp);
3232 rc = context_write(p, &c->context[0], fp);
3236 case OCON_IBENDPORT:
3237 len = strlen(c->u.ibendport.dev_name);
3238 buf[0] = cpu_to_le32(len);
3239 buf[1] = cpu_to_le32(c->u.ibendport.port);
3240 rc = put_entry(buf, sizeof(u32), 2, fp);
3243 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3246 rc = context_write(p, &c->context[0], fp);
3256 static int genfs_write(struct policydb *p, void *fp)
3258 struct genfs *genfs;
3265 for (genfs = p->genfs; genfs; genfs = genfs->next)
3267 buf[0] = cpu_to_le32(len);
3268 rc = put_entry(buf, sizeof(u32), 1, fp);
3271 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3272 len = strlen(genfs->fstype);
3273 buf[0] = cpu_to_le32(len);
3274 rc = put_entry(buf, sizeof(u32), 1, fp);
3277 rc = put_entry(genfs->fstype, 1, len, fp);
3281 for (c = genfs->head; c; c = c->next)
3283 buf[0] = cpu_to_le32(len);
3284 rc = put_entry(buf, sizeof(u32), 1, fp);
3287 for (c = genfs->head; c; c = c->next) {
3288 len = strlen(c->u.name);
3289 buf[0] = cpu_to_le32(len);
3290 rc = put_entry(buf, sizeof(u32), 1, fp);
3293 rc = put_entry(c->u.name, 1, len, fp);
3296 buf[0] = cpu_to_le32(c->v.sclass);
3297 rc = put_entry(buf, sizeof(u32), 1, fp);
3300 rc = context_write(p, &c->context[0], fp);
3308 static int hashtab_cnt(void *key, void *data, void *ptr)
3316 static int range_write_helper(void *key, void *data, void *ptr)
3319 struct range_trans *rt = key;
3320 struct mls_range *r = data;
3321 struct policy_data *pd = ptr;
3323 struct policydb *p = pd->p;
3326 buf[0] = cpu_to_le32(rt->source_type);
3327 buf[1] = cpu_to_le32(rt->target_type);
3328 rc = put_entry(buf, sizeof(u32), 2, fp);
3331 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3332 buf[0] = cpu_to_le32(rt->target_class);
3333 rc = put_entry(buf, sizeof(u32), 1, fp);
3337 rc = mls_write_range_helper(r, fp);
3344 static int range_write(struct policydb *p, void *fp)
3348 struct policy_data pd;
3353 /* count the number of entries in the hashtab */
3355 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3359 buf[0] = cpu_to_le32(nel);
3360 rc = put_entry(buf, sizeof(u32), 1, fp);
3364 /* actually write all of the entries */
3365 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3372 static int filename_write_helper(void *key, void *data, void *ptr)
3375 struct filename_trans *ft = key;
3376 struct filename_trans_datum *otype = data;
3381 len = strlen(ft->name);
3382 buf[0] = cpu_to_le32(len);
3383 rc = put_entry(buf, sizeof(u32), 1, fp);
3387 rc = put_entry(ft->name, sizeof(char), len, fp);
3391 buf[0] = cpu_to_le32(ft->stype);
3392 buf[1] = cpu_to_le32(ft->ttype);
3393 buf[2] = cpu_to_le32(ft->tclass);
3394 buf[3] = cpu_to_le32(otype->otype);
3396 rc = put_entry(buf, sizeof(u32), 4, fp);
3403 static int filename_trans_write(struct policydb *p, void *fp)
3409 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3413 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3417 buf[0] = cpu_to_le32(nel);
3418 rc = put_entry(buf, sizeof(u32), 1, fp);
3422 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3430 * Write the configuration data in a policy database
3431 * structure to a policy database binary representation
3434 int policydb_write(struct policydb *p, void *fp)
3436 unsigned int i, num_syms;
3441 struct policydb_compat_info *info;
3444 * refuse to write policy older than compressed avtab
3445 * to simplify the writer. There are other tests dropped
3446 * since we assume this throughout the writer code. Be
3447 * careful if you ever try to remove this restriction
3449 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3450 pr_err("SELinux: refusing to write policy version %d."
3451 " Because it is less than version %d\n", p->policyvers,
3452 POLICYDB_VERSION_AVTAB);
3458 config |= POLICYDB_CONFIG_MLS;
3460 if (p->reject_unknown)
3461 config |= REJECT_UNKNOWN;
3462 if (p->allow_unknown)
3463 config |= ALLOW_UNKNOWN;
3465 /* Write the magic number and string identifiers. */
3466 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3467 len = strlen(POLICYDB_STRING);
3468 buf[1] = cpu_to_le32(len);
3469 rc = put_entry(buf, sizeof(u32), 2, fp);
3472 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3476 /* Write the version, config, and table sizes. */
3477 info = policydb_lookup_compat(p->policyvers);
3479 pr_err("SELinux: compatibility lookup failed for policy "
3480 "version %d", p->policyvers);
3484 buf[0] = cpu_to_le32(p->policyvers);
3485 buf[1] = cpu_to_le32(config);
3486 buf[2] = cpu_to_le32(info->sym_num);
3487 buf[3] = cpu_to_le32(info->ocon_num);
3489 rc = put_entry(buf, sizeof(u32), 4, fp);
3493 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3494 rc = ebitmap_write(&p->policycaps, fp);
3499 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3500 rc = ebitmap_write(&p->permissive_map, fp);
3505 num_syms = info->sym_num;
3506 for (i = 0; i < num_syms; i++) {
3507 struct policy_data pd;
3512 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3513 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3515 rc = put_entry(buf, sizeof(u32), 2, fp);
3518 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3523 rc = avtab_write(p, &p->te_avtab, fp);
3527 rc = cond_write_list(p, p->cond_list, fp);
3531 rc = role_trans_write(p, fp);
3535 rc = role_allow_write(p->role_allow, fp);
3539 rc = filename_trans_write(p, fp);
3543 rc = ocontext_write(p, info, fp);
3547 rc = genfs_write(p, fp);
3551 rc = range_write(p, fp);
3555 for (i = 0; i < p->p_types.nprim; i++) {
3556 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3559 rc = ebitmap_write(e, fp);