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;
736 ebitmap_destroy(&levdatum->level->cat);
737 kfree(levdatum->level);
743 static int cat_destroy(void *key, void *datum, void *p)
750 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
762 static int filenametr_destroy(void *key, void *datum, void *p)
764 struct filename_trans *ft = key;
772 static int range_tr_destroy(void *key, void *datum, void *p)
774 struct mls_range *rt = datum;
776 ebitmap_destroy(&rt->level[0].cat);
777 ebitmap_destroy(&rt->level[1].cat);
783 static void ocontext_destroy(struct ocontext *c, int i)
788 context_destroy(&c->context[0]);
789 context_destroy(&c->context[1]);
790 if (i == OCON_ISID || i == OCON_FS ||
791 i == OCON_NETIF || i == OCON_FSUSE)
797 * Free any memory allocated by a policy database structure.
799 void policydb_destroy(struct policydb *p)
801 struct ocontext *c, *ctmp;
802 struct genfs *g, *gtmp;
804 struct role_allow *ra, *lra = NULL;
805 struct role_trans *tr, *ltr = NULL;
807 for (i = 0; i < SYM_NUM; i++) {
809 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
810 hashtab_destroy(p->symtab[i].table);
813 for (i = 0; i < SYM_NUM; i++) {
814 if (p->sym_val_to_name[i])
815 flex_array_free(p->sym_val_to_name[i]);
818 kfree(p->class_val_to_struct);
819 kfree(p->role_val_to_struct);
820 kfree(p->user_val_to_struct);
821 if (p->type_val_to_struct_array)
822 flex_array_free(p->type_val_to_struct_array);
824 avtab_destroy(&p->te_avtab);
826 for (i = 0; i < OCON_NUM; i++) {
832 ocontext_destroy(ctmp, i);
834 p->ocontexts[i] = NULL;
845 ocontext_destroy(ctmp, OCON_FSUSE);
853 cond_policydb_destroy(p);
855 for (tr = p->role_tr; tr; tr = tr->next) {
862 for (ra = p->role_allow; ra; ra = ra->next) {
869 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
870 hashtab_destroy(p->filename_trans);
872 hashtab_map(p->range_tr, range_tr_destroy, NULL);
873 hashtab_destroy(p->range_tr);
875 if (p->type_attr_map_array) {
876 for (i = 0; i < p->p_types.nprim; i++) {
879 e = flex_array_get(p->type_attr_map_array, i);
884 flex_array_free(p->type_attr_map_array);
887 ebitmap_destroy(&p->filename_trans_ttypes);
888 ebitmap_destroy(&p->policycaps);
889 ebitmap_destroy(&p->permissive_map);
893 * Load the initial SIDs specified in a policy database
894 * structure into a SID table.
896 int policydb_load_isids(struct policydb *p, struct sidtab *s)
898 struct ocontext *head, *c;
903 pr_err("SELinux: out of memory on SID table init\n");
907 head = p->ocontexts[OCON_ISID];
908 for (c = head; c; c = c->next) {
910 if (!c->context[0].user) {
911 pr_err("SELinux: SID %s was never defined.\n",
916 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
917 pr_err("SELinux: Initial SID %s out of range.\n",
923 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
925 pr_err("SELinux: unable to load initial SID %s.\n",
936 int policydb_class_isvalid(struct policydb *p, unsigned int class)
938 if (!class || class > p->p_classes.nprim)
943 int policydb_role_isvalid(struct policydb *p, unsigned int role)
945 if (!role || role > p->p_roles.nprim)
950 int policydb_type_isvalid(struct policydb *p, unsigned int type)
952 if (!type || type > p->p_types.nprim)
958 * Return 1 if the fields in the security context
959 * structure `c' are valid. Return 0 otherwise.
961 int policydb_context_isvalid(struct policydb *p, struct context *c)
963 struct role_datum *role;
964 struct user_datum *usrdatum;
966 if (!c->role || c->role > p->p_roles.nprim)
969 if (!c->user || c->user > p->p_users.nprim)
972 if (!c->type || c->type > p->p_types.nprim)
975 if (c->role != OBJECT_R_VAL) {
977 * Role must be authorized for the type.
979 role = p->role_val_to_struct[c->role - 1];
980 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
981 /* role may not be associated with type */
985 * User must be authorized for the role.
987 usrdatum = p->user_val_to_struct[c->user - 1];
991 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
992 /* user may not be associated with role */
996 if (!mls_context_isvalid(p, c))
1003 * Read a MLS range structure from a policydb binary
1004 * representation file.
1006 static int mls_read_range_helper(struct mls_range *r, void *fp)
1012 rc = next_entry(buf, fp, sizeof(u32));
1017 items = le32_to_cpu(buf[0]);
1018 if (items > ARRAY_SIZE(buf)) {
1019 pr_err("SELinux: mls: range overflow\n");
1023 rc = next_entry(buf, fp, sizeof(u32) * items);
1025 pr_err("SELinux: mls: truncated range\n");
1029 r->level[0].sens = le32_to_cpu(buf[0]);
1031 r->level[1].sens = le32_to_cpu(buf[1]);
1033 r->level[1].sens = r->level[0].sens;
1035 rc = ebitmap_read(&r->level[0].cat, fp);
1037 pr_err("SELinux: mls: error reading low categories\n");
1041 rc = ebitmap_read(&r->level[1].cat, fp);
1043 pr_err("SELinux: mls: error reading high categories\n");
1047 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1049 pr_err("SELinux: mls: out of memory\n");
1056 ebitmap_destroy(&r->level[0].cat);
1062 * Read and validate a security context structure
1063 * from a policydb binary representation file.
1065 static int context_read_and_validate(struct context *c,
1072 rc = next_entry(buf, fp, sizeof buf);
1074 pr_err("SELinux: context truncated\n");
1077 c->user = le32_to_cpu(buf[0]);
1078 c->role = le32_to_cpu(buf[1]);
1079 c->type = le32_to_cpu(buf[2]);
1080 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1081 rc = mls_read_range_helper(&c->range, fp);
1083 pr_err("SELinux: error reading MLS range of context\n");
1089 if (!policydb_context_isvalid(p, c)) {
1090 pr_err("SELinux: invalid security context\n");
1100 * The following *_read functions are used to
1101 * read the symbol data from a policy database
1102 * binary representation file.
1105 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1110 if ((len == 0) || (len == (u32)-1))
1113 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1117 /* it's expected the caller should free the str */
1120 rc = next_entry(str, fp, len);
1128 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1131 struct perm_datum *perdatum;
1136 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1140 rc = next_entry(buf, fp, sizeof buf);
1144 len = le32_to_cpu(buf[0]);
1145 perdatum->value = le32_to_cpu(buf[1]);
1147 rc = str_read(&key, GFP_KERNEL, fp, len);
1151 rc = hashtab_insert(h, key, perdatum);
1157 perm_destroy(key, perdatum, NULL);
1161 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1164 struct common_datum *comdatum;
1169 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1173 rc = next_entry(buf, fp, sizeof buf);
1177 len = le32_to_cpu(buf[0]);
1178 comdatum->value = le32_to_cpu(buf[1]);
1180 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1183 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1184 nel = le32_to_cpu(buf[3]);
1186 rc = str_read(&key, GFP_KERNEL, fp, len);
1190 for (i = 0; i < nel; i++) {
1191 rc = perm_read(p, comdatum->permissions.table, fp);
1196 rc = hashtab_insert(h, key, comdatum);
1201 common_destroy(key, comdatum, NULL);
1205 static void type_set_init(struct type_set *t)
1207 ebitmap_init(&t->types);
1208 ebitmap_init(&t->negset);
1211 static int type_set_read(struct type_set *t, void *fp)
1216 if (ebitmap_read(&t->types, fp))
1218 if (ebitmap_read(&t->negset, fp))
1221 rc = next_entry(buf, fp, sizeof(u32));
1224 t->flags = le32_to_cpu(buf[0]);
1230 static int read_cons_helper(struct policydb *p,
1231 struct constraint_node **nodep,
1232 int ncons, int allowxtarget, void *fp)
1234 struct constraint_node *c, *lc;
1235 struct constraint_expr *e, *le;
1238 int rc, i, j, depth;
1241 for (i = 0; i < ncons; i++) {
1242 c = kzalloc(sizeof(*c), GFP_KERNEL);
1251 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1254 c->permissions = le32_to_cpu(buf[0]);
1255 nexpr = le32_to_cpu(buf[1]);
1258 for (j = 0; j < nexpr; j++) {
1259 e = kzalloc(sizeof(*e), GFP_KERNEL);
1268 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1271 e->expr_type = le32_to_cpu(buf[0]);
1272 e->attr = le32_to_cpu(buf[1]);
1273 e->op = le32_to_cpu(buf[2]);
1275 switch (e->expr_type) {
1287 if (depth == (CEXPR_MAXDEPTH - 1))
1292 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1294 if (depth == (CEXPR_MAXDEPTH - 1))
1297 rc = ebitmap_read(&e->names, fp);
1300 if (p->policyvers >=
1301 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1302 e->type_names = kzalloc(sizeof
1307 type_set_init(e->type_names);
1308 rc = type_set_read(e->type_names, fp);
1326 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1329 struct class_datum *cladatum;
1331 u32 len, len2, ncons, nel;
1334 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1338 rc = next_entry(buf, fp, sizeof(u32)*6);
1342 len = le32_to_cpu(buf[0]);
1343 len2 = le32_to_cpu(buf[1]);
1344 cladatum->value = le32_to_cpu(buf[2]);
1346 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1349 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1350 nel = le32_to_cpu(buf[4]);
1352 ncons = le32_to_cpu(buf[5]);
1354 rc = str_read(&key, GFP_KERNEL, fp, len);
1359 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1364 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1365 if (!cladatum->comdatum) {
1366 pr_err("SELinux: unknown common %s\n",
1371 for (i = 0; i < nel; i++) {
1372 rc = perm_read(p, cladatum->permissions.table, fp);
1377 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1381 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1382 /* grab the validatetrans rules */
1383 rc = next_entry(buf, fp, sizeof(u32));
1386 ncons = le32_to_cpu(buf[0]);
1387 rc = read_cons_helper(p, &cladatum->validatetrans,
1393 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1394 rc = next_entry(buf, fp, sizeof(u32) * 3);
1398 cladatum->default_user = le32_to_cpu(buf[0]);
1399 cladatum->default_role = le32_to_cpu(buf[1]);
1400 cladatum->default_range = le32_to_cpu(buf[2]);
1403 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1404 rc = next_entry(buf, fp, sizeof(u32) * 1);
1407 cladatum->default_type = le32_to_cpu(buf[0]);
1410 rc = hashtab_insert(h, key, cladatum);
1416 cls_destroy(key, cladatum, NULL);
1420 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1423 struct role_datum *role;
1424 int rc, to_read = 2;
1428 role = kzalloc(sizeof(*role), GFP_KERNEL);
1432 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1435 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1439 len = le32_to_cpu(buf[0]);
1440 role->value = le32_to_cpu(buf[1]);
1441 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1442 role->bounds = le32_to_cpu(buf[2]);
1444 rc = str_read(&key, GFP_KERNEL, fp, len);
1448 rc = ebitmap_read(&role->dominates, fp);
1452 rc = ebitmap_read(&role->types, fp);
1456 if (strcmp(key, OBJECT_R) == 0) {
1458 if (role->value != OBJECT_R_VAL) {
1459 pr_err("SELinux: Role %s has wrong value %d\n",
1460 OBJECT_R, role->value);
1467 rc = hashtab_insert(h, key, role);
1472 role_destroy(key, role, NULL);
1476 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1479 struct type_datum *typdatum;
1480 int rc, to_read = 3;
1484 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1488 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1491 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1495 len = le32_to_cpu(buf[0]);
1496 typdatum->value = le32_to_cpu(buf[1]);
1497 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1498 u32 prop = le32_to_cpu(buf[2]);
1500 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1501 typdatum->primary = 1;
1502 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1503 typdatum->attribute = 1;
1505 typdatum->bounds = le32_to_cpu(buf[3]);
1507 typdatum->primary = le32_to_cpu(buf[2]);
1510 rc = str_read(&key, GFP_KERNEL, fp, len);
1514 rc = hashtab_insert(h, key, typdatum);
1519 type_destroy(key, typdatum, NULL);
1525 * Read a MLS level structure from a policydb binary
1526 * representation file.
1528 static int mls_read_level(struct mls_level *lp, void *fp)
1533 memset(lp, 0, sizeof(*lp));
1535 rc = next_entry(buf, fp, sizeof buf);
1537 pr_err("SELinux: mls: truncated level\n");
1540 lp->sens = le32_to_cpu(buf[0]);
1542 rc = ebitmap_read(&lp->cat, fp);
1544 pr_err("SELinux: mls: error reading level categories\n");
1550 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1553 struct user_datum *usrdatum;
1554 int rc, to_read = 2;
1558 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1562 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1565 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1569 len = le32_to_cpu(buf[0]);
1570 usrdatum->value = le32_to_cpu(buf[1]);
1571 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1572 usrdatum->bounds = le32_to_cpu(buf[2]);
1574 rc = str_read(&key, GFP_KERNEL, fp, len);
1578 rc = ebitmap_read(&usrdatum->roles, fp);
1582 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1583 rc = mls_read_range_helper(&usrdatum->range, fp);
1586 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1591 rc = hashtab_insert(h, key, usrdatum);
1596 user_destroy(key, usrdatum, NULL);
1600 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1603 struct level_datum *levdatum;
1608 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1612 rc = next_entry(buf, fp, sizeof buf);
1616 len = le32_to_cpu(buf[0]);
1617 levdatum->isalias = le32_to_cpu(buf[1]);
1619 rc = str_read(&key, GFP_ATOMIC, fp, len);
1624 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1625 if (!levdatum->level)
1628 rc = mls_read_level(levdatum->level, fp);
1632 rc = hashtab_insert(h, key, levdatum);
1637 sens_destroy(key, levdatum, NULL);
1641 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1644 struct cat_datum *catdatum;
1649 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1653 rc = next_entry(buf, fp, sizeof buf);
1657 len = le32_to_cpu(buf[0]);
1658 catdatum->value = le32_to_cpu(buf[1]);
1659 catdatum->isalias = le32_to_cpu(buf[2]);
1661 rc = str_read(&key, GFP_ATOMIC, fp, len);
1665 rc = hashtab_insert(h, key, catdatum);
1670 cat_destroy(key, catdatum, NULL);
1674 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1686 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1688 struct user_datum *upper, *user;
1689 struct policydb *p = datap;
1692 upper = user = datum;
1693 while (upper->bounds) {
1694 struct ebitmap_node *node;
1697 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1698 pr_err("SELinux: user %s: "
1699 "too deep or looped boundary",
1704 upper = p->user_val_to_struct[upper->bounds - 1];
1705 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1706 if (ebitmap_get_bit(&upper->roles, bit))
1709 pr_err("SELinux: boundary violated policy: "
1710 "user=%s role=%s bounds=%s\n",
1711 sym_name(p, SYM_USERS, user->value - 1),
1712 sym_name(p, SYM_ROLES, bit),
1713 sym_name(p, SYM_USERS, upper->value - 1));
1722 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1724 struct role_datum *upper, *role;
1725 struct policydb *p = datap;
1728 upper = role = datum;
1729 while (upper->bounds) {
1730 struct ebitmap_node *node;
1733 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1734 pr_err("SELinux: role %s: "
1735 "too deep or looped bounds\n",
1740 upper = p->role_val_to_struct[upper->bounds - 1];
1741 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1742 if (ebitmap_get_bit(&upper->types, bit))
1745 pr_err("SELinux: boundary violated policy: "
1746 "role=%s type=%s bounds=%s\n",
1747 sym_name(p, SYM_ROLES, role->value - 1),
1748 sym_name(p, SYM_TYPES, bit),
1749 sym_name(p, SYM_ROLES, upper->value - 1));
1758 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1760 struct type_datum *upper;
1761 struct policydb *p = datap;
1765 while (upper->bounds) {
1766 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1767 pr_err("SELinux: type %s: "
1768 "too deep or looped boundary\n",
1773 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1777 if (upper->attribute) {
1778 pr_err("SELinux: type %s: "
1779 "bounded by attribute %s",
1781 sym_name(p, SYM_TYPES, upper->value - 1));
1789 static int policydb_bounds_sanity_check(struct policydb *p)
1793 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1796 rc = hashtab_map(p->p_users.table,
1797 user_bounds_sanity_check, p);
1801 rc = hashtab_map(p->p_roles.table,
1802 role_bounds_sanity_check, p);
1806 rc = hashtab_map(p->p_types.table,
1807 type_bounds_sanity_check, p);
1814 u16 string_to_security_class(struct policydb *p, const char *name)
1816 struct class_datum *cladatum;
1818 cladatum = hashtab_search(p->p_classes.table, name);
1822 return cladatum->value;
1825 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1827 struct class_datum *cladatum;
1828 struct perm_datum *perdatum = NULL;
1829 struct common_datum *comdatum;
1831 if (!tclass || tclass > p->p_classes.nprim)
1834 cladatum = p->class_val_to_struct[tclass-1];
1835 comdatum = cladatum->comdatum;
1837 perdatum = hashtab_search(comdatum->permissions.table,
1840 perdatum = hashtab_search(cladatum->permissions.table,
1845 return 1U << (perdatum->value-1);
1848 static int range_read(struct policydb *p, void *fp)
1850 struct range_trans *rt = NULL;
1851 struct mls_range *r = NULL;
1856 if (p->policyvers < POLICYDB_VERSION_MLS)
1859 rc = next_entry(buf, fp, sizeof(u32));
1863 nel = le32_to_cpu(buf[0]);
1864 for (i = 0; i < nel; i++) {
1866 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1870 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1874 rt->source_type = le32_to_cpu(buf[0]);
1875 rt->target_type = le32_to_cpu(buf[1]);
1876 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1877 rc = next_entry(buf, fp, sizeof(u32));
1880 rt->target_class = le32_to_cpu(buf[0]);
1882 rt->target_class = p->process_class;
1885 if (!policydb_type_isvalid(p, rt->source_type) ||
1886 !policydb_type_isvalid(p, rt->target_type) ||
1887 !policydb_class_isvalid(p, rt->target_class))
1891 r = kzalloc(sizeof(*r), GFP_KERNEL);
1895 rc = mls_read_range_helper(r, fp);
1900 if (!mls_range_isvalid(p, r)) {
1901 pr_warn("SELinux: rangetrans: invalid range\n");
1905 rc = hashtab_insert(p->range_tr, rt, r);
1912 hash_eval(p->range_tr, "rangetr");
1920 static int filename_trans_read(struct policydb *p, void *fp)
1922 struct filename_trans *ft;
1923 struct filename_trans_datum *otype;
1929 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1932 rc = next_entry(buf, fp, sizeof(u32));
1935 nel = le32_to_cpu(buf[0]);
1937 for (i = 0; i < nel; i++) {
1942 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1947 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1951 /* length of the path component string */
1952 rc = next_entry(buf, fp, sizeof(u32));
1955 len = le32_to_cpu(buf[0]);
1957 /* path component string */
1958 rc = str_read(&name, GFP_KERNEL, fp, len);
1964 rc = next_entry(buf, fp, sizeof(u32) * 4);
1968 ft->stype = le32_to_cpu(buf[0]);
1969 ft->ttype = le32_to_cpu(buf[1]);
1970 ft->tclass = le32_to_cpu(buf[2]);
1972 otype->otype = le32_to_cpu(buf[3]);
1974 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1978 rc = hashtab_insert(p->filename_trans, ft, otype);
1981 * Do not return -EEXIST to the caller, or the system
1986 /* But free memory to avoid memory leak. */
1992 hash_eval(p->filename_trans, "filenametr");
2002 static int genfs_read(struct policydb *p, void *fp)
2005 u32 nel, nel2, len, len2;
2007 struct ocontext *l, *c;
2008 struct ocontext *newc = NULL;
2009 struct genfs *genfs_p, *genfs;
2010 struct genfs *newgenfs = NULL;
2012 rc = next_entry(buf, fp, sizeof(u32));
2015 nel = le32_to_cpu(buf[0]);
2017 for (i = 0; i < nel; i++) {
2018 rc = next_entry(buf, fp, sizeof(u32));
2021 len = le32_to_cpu(buf[0]);
2024 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2028 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2032 for (genfs_p = NULL, genfs = p->genfs; genfs;
2033 genfs_p = genfs, genfs = genfs->next) {
2035 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2036 pr_err("SELinux: dup genfs fstype %s\n",
2040 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2043 newgenfs->next = genfs;
2045 genfs_p->next = newgenfs;
2047 p->genfs = newgenfs;
2051 rc = next_entry(buf, fp, sizeof(u32));
2055 nel2 = le32_to_cpu(buf[0]);
2056 for (j = 0; j < nel2; j++) {
2057 rc = next_entry(buf, fp, sizeof(u32));
2060 len = le32_to_cpu(buf[0]);
2063 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2067 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2071 rc = next_entry(buf, fp, sizeof(u32));
2075 newc->v.sclass = le32_to_cpu(buf[0]);
2076 rc = context_read_and_validate(&newc->context[0], p, fp);
2080 for (l = NULL, c = genfs->head; c;
2081 l = c, c = c->next) {
2083 if (!strcmp(newc->u.name, c->u.name) &&
2084 (!c->v.sclass || !newc->v.sclass ||
2085 newc->v.sclass == c->v.sclass)) {
2086 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2087 genfs->fstype, c->u.name);
2090 len = strlen(newc->u.name);
2091 len2 = strlen(c->u.name);
2107 kfree(newgenfs->fstype);
2110 ocontext_destroy(newc, OCON_FSUSE);
2115 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2120 __be64 prefixbuf[1];
2122 struct ocontext *l, *c;
2125 for (i = 0; i < info->ocon_num; i++) {
2126 rc = next_entry(buf, fp, sizeof(u32));
2129 nel = le32_to_cpu(buf[0]);
2132 for (j = 0; j < nel; j++) {
2134 c = kzalloc(sizeof(*c), GFP_KERNEL);
2140 p->ocontexts[i] = c;
2145 rc = next_entry(buf, fp, sizeof(u32));
2149 c->sid[0] = le32_to_cpu(buf[0]);
2150 rc = context_read_and_validate(&c->context[0], p, fp);
2156 rc = next_entry(buf, fp, sizeof(u32));
2159 len = le32_to_cpu(buf[0]);
2161 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2165 rc = context_read_and_validate(&c->context[0], p, fp);
2168 rc = context_read_and_validate(&c->context[1], p, fp);
2173 rc = next_entry(buf, fp, sizeof(u32)*3);
2176 c->u.port.protocol = le32_to_cpu(buf[0]);
2177 c->u.port.low_port = le32_to_cpu(buf[1]);
2178 c->u.port.high_port = le32_to_cpu(buf[2]);
2179 rc = context_read_and_validate(&c->context[0], p, fp);
2184 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2187 c->u.node.addr = nodebuf[0]; /* network order */
2188 c->u.node.mask = nodebuf[1]; /* network order */
2189 rc = context_read_and_validate(&c->context[0], p, fp);
2194 rc = next_entry(buf, fp, sizeof(u32)*2);
2199 c->v.behavior = le32_to_cpu(buf[0]);
2200 /* Determined at runtime, not in policy DB. */
2201 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2203 if (c->v.behavior > SECURITY_FS_USE_MAX)
2206 len = le32_to_cpu(buf[1]);
2207 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2211 rc = context_read_and_validate(&c->context[0], p, fp);
2218 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2221 for (k = 0; k < 4; k++)
2222 c->u.node6.addr[k] = nodebuf[k];
2223 for (k = 0; k < 4; k++)
2224 c->u.node6.mask[k] = nodebuf[k+4];
2225 rc = context_read_and_validate(&c->context[0], p, fp);
2231 u32 pkey_lo, pkey_hi;
2233 rc = next_entry(prefixbuf, fp, sizeof(u64));
2237 /* we need to have subnet_prefix in CPU order */
2238 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2240 rc = next_entry(buf, fp, sizeof(u32) * 2);
2244 pkey_lo = le32_to_cpu(buf[0]);
2245 pkey_hi = le32_to_cpu(buf[1]);
2247 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2252 c->u.ibpkey.low_pkey = pkey_lo;
2253 c->u.ibpkey.high_pkey = pkey_hi;
2255 rc = context_read_and_validate(&c->context[0],
2262 case OCON_IBENDPORT: {
2265 rc = next_entry(buf, fp, sizeof(u32) * 2);
2268 len = le32_to_cpu(buf[0]);
2270 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2274 port = le32_to_cpu(buf[1]);
2275 if (port > U8_MAX || port == 0) {
2280 c->u.ibendport.port = port;
2282 rc = context_read_and_validate(&c->context[0],
2298 * Read the configuration data from a policy database binary
2299 * representation file into a policy database structure.
2301 int policydb_read(struct policydb *p, void *fp)
2303 struct role_allow *ra, *lra;
2304 struct role_trans *tr, *ltr;
2307 u32 len, nprim, nel;
2310 struct policydb_compat_info *info;
2312 rc = policydb_init(p);
2316 /* Read the magic number and string length. */
2317 rc = next_entry(buf, fp, sizeof(u32) * 2);
2322 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2323 pr_err("SELinux: policydb magic number 0x%x does "
2324 "not match expected magic number 0x%x\n",
2325 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2330 len = le32_to_cpu(buf[1]);
2331 if (len != strlen(POLICYDB_STRING)) {
2332 pr_err("SELinux: policydb string length %d does not "
2333 "match expected length %zu\n",
2334 len, strlen(POLICYDB_STRING));
2339 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2340 if (!policydb_str) {
2341 pr_err("SELinux: unable to allocate memory for policydb "
2342 "string of length %d\n", len);
2346 rc = next_entry(policydb_str, fp, len);
2348 pr_err("SELinux: truncated policydb string identifier\n");
2349 kfree(policydb_str);
2354 policydb_str[len] = '\0';
2355 if (strcmp(policydb_str, POLICYDB_STRING)) {
2356 pr_err("SELinux: policydb string %s does not match "
2357 "my string %s\n", policydb_str, POLICYDB_STRING);
2358 kfree(policydb_str);
2361 /* Done with policydb_str. */
2362 kfree(policydb_str);
2363 policydb_str = NULL;
2365 /* Read the version and table sizes. */
2366 rc = next_entry(buf, fp, sizeof(u32)*4);
2371 p->policyvers = le32_to_cpu(buf[0]);
2372 if (p->policyvers < POLICYDB_VERSION_MIN ||
2373 p->policyvers > POLICYDB_VERSION_MAX) {
2374 pr_err("SELinux: policydb version %d does not match "
2375 "my version range %d-%d\n",
2376 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2380 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2384 if (p->policyvers < POLICYDB_VERSION_MLS) {
2385 pr_err("SELinux: security policydb version %d "
2386 "(MLS) not backwards compatible\n",
2391 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2392 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2394 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2395 rc = ebitmap_read(&p->policycaps, fp);
2400 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2401 rc = ebitmap_read(&p->permissive_map, fp);
2407 info = policydb_lookup_compat(p->policyvers);
2409 pr_err("SELinux: unable to find policy compat info "
2410 "for version %d\n", p->policyvers);
2415 if (le32_to_cpu(buf[2]) != info->sym_num ||
2416 le32_to_cpu(buf[3]) != info->ocon_num) {
2417 pr_err("SELinux: policydb table sizes (%d,%d) do "
2418 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2419 le32_to_cpu(buf[3]),
2420 info->sym_num, info->ocon_num);
2424 for (i = 0; i < info->sym_num; i++) {
2425 rc = next_entry(buf, fp, sizeof(u32)*2);
2428 nprim = le32_to_cpu(buf[0]);
2429 nel = le32_to_cpu(buf[1]);
2430 for (j = 0; j < nel; j++) {
2431 rc = read_f[i](p, p->symtab[i].table, fp);
2436 p->symtab[i].nprim = nprim;
2440 p->process_class = string_to_security_class(p, "process");
2441 if (!p->process_class)
2444 rc = avtab_read(&p->te_avtab, fp, p);
2448 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2449 rc = cond_read_list(p, fp);
2454 rc = next_entry(buf, fp, sizeof(u32));
2457 nel = le32_to_cpu(buf[0]);
2459 for (i = 0; i < nel; i++) {
2461 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2468 rc = next_entry(buf, fp, sizeof(u32)*3);
2473 tr->role = le32_to_cpu(buf[0]);
2474 tr->type = le32_to_cpu(buf[1]);
2475 tr->new_role = le32_to_cpu(buf[2]);
2476 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2477 rc = next_entry(buf, fp, sizeof(u32));
2480 tr->tclass = le32_to_cpu(buf[0]);
2482 tr->tclass = p->process_class;
2485 if (!policydb_role_isvalid(p, tr->role) ||
2486 !policydb_type_isvalid(p, tr->type) ||
2487 !policydb_class_isvalid(p, tr->tclass) ||
2488 !policydb_role_isvalid(p, tr->new_role))
2493 rc = next_entry(buf, fp, sizeof(u32));
2496 nel = le32_to_cpu(buf[0]);
2498 for (i = 0; i < nel; i++) {
2500 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2507 rc = next_entry(buf, fp, sizeof(u32)*2);
2512 ra->role = le32_to_cpu(buf[0]);
2513 ra->new_role = le32_to_cpu(buf[1]);
2514 if (!policydb_role_isvalid(p, ra->role) ||
2515 !policydb_role_isvalid(p, ra->new_role))
2520 rc = filename_trans_read(p, fp);
2524 rc = policydb_index(p);
2529 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2530 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2531 if (!p->process_trans_perms)
2534 rc = ocontext_read(p, info, fp);
2538 rc = genfs_read(p, fp);
2542 rc = range_read(p, fp);
2547 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2549 GFP_KERNEL | __GFP_ZERO);
2550 if (!p->type_attr_map_array)
2553 /* preallocate so we don't have to worry about the put ever failing */
2554 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2555 GFP_KERNEL | __GFP_ZERO);
2559 for (i = 0; i < p->p_types.nprim; i++) {
2560 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2564 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2565 rc = ebitmap_read(e, fp);
2569 /* add the type itself as the degenerate case */
2570 rc = ebitmap_set_bit(e, i, 1);
2575 rc = policydb_bounds_sanity_check(p);
2583 policydb_destroy(p);
2588 * Write a MLS level structure to a policydb binary
2589 * representation file.
2591 static int mls_write_level(struct mls_level *l, void *fp)
2596 buf[0] = cpu_to_le32(l->sens);
2597 rc = put_entry(buf, sizeof(u32), 1, fp);
2601 rc = ebitmap_write(&l->cat, fp);
2609 * Write a MLS range structure to a policydb binary
2610 * representation file.
2612 static int mls_write_range_helper(struct mls_range *r, void *fp)
2618 eq = mls_level_eq(&r->level[1], &r->level[0]);
2624 buf[0] = cpu_to_le32(items-1);
2625 buf[1] = cpu_to_le32(r->level[0].sens);
2627 buf[2] = cpu_to_le32(r->level[1].sens);
2629 BUG_ON(items > ARRAY_SIZE(buf));
2631 rc = put_entry(buf, sizeof(u32), items, fp);
2635 rc = ebitmap_write(&r->level[0].cat, fp);
2639 rc = ebitmap_write(&r->level[1].cat, fp);
2647 static int sens_write(void *vkey, void *datum, void *ptr)
2650 struct level_datum *levdatum = datum;
2651 struct policy_data *pd = ptr;
2658 buf[0] = cpu_to_le32(len);
2659 buf[1] = cpu_to_le32(levdatum->isalias);
2660 rc = put_entry(buf, sizeof(u32), 2, fp);
2664 rc = put_entry(key, 1, len, fp);
2668 rc = mls_write_level(levdatum->level, fp);
2675 static int cat_write(void *vkey, void *datum, void *ptr)
2678 struct cat_datum *catdatum = datum;
2679 struct policy_data *pd = ptr;
2686 buf[0] = cpu_to_le32(len);
2687 buf[1] = cpu_to_le32(catdatum->value);
2688 buf[2] = cpu_to_le32(catdatum->isalias);
2689 rc = put_entry(buf, sizeof(u32), 3, fp);
2693 rc = put_entry(key, 1, len, fp);
2700 static int role_trans_write(struct policydb *p, void *fp)
2702 struct role_trans *r = p->role_tr;
2703 struct role_trans *tr;
2709 for (tr = r; tr; tr = tr->next)
2711 buf[0] = cpu_to_le32(nel);
2712 rc = put_entry(buf, sizeof(u32), 1, fp);
2715 for (tr = r; tr; tr = tr->next) {
2716 buf[0] = cpu_to_le32(tr->role);
2717 buf[1] = cpu_to_le32(tr->type);
2718 buf[2] = cpu_to_le32(tr->new_role);
2719 rc = put_entry(buf, sizeof(u32), 3, fp);
2722 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2723 buf[0] = cpu_to_le32(tr->tclass);
2724 rc = put_entry(buf, sizeof(u32), 1, fp);
2733 static int role_allow_write(struct role_allow *r, void *fp)
2735 struct role_allow *ra;
2741 for (ra = r; ra; ra = ra->next)
2743 buf[0] = cpu_to_le32(nel);
2744 rc = put_entry(buf, sizeof(u32), 1, fp);
2747 for (ra = r; ra; ra = ra->next) {
2748 buf[0] = cpu_to_le32(ra->role);
2749 buf[1] = cpu_to_le32(ra->new_role);
2750 rc = put_entry(buf, sizeof(u32), 2, fp);
2758 * Write a security context structure
2759 * to a policydb binary representation file.
2761 static int context_write(struct policydb *p, struct context *c,
2767 buf[0] = cpu_to_le32(c->user);
2768 buf[1] = cpu_to_le32(c->role);
2769 buf[2] = cpu_to_le32(c->type);
2771 rc = put_entry(buf, sizeof(u32), 3, fp);
2775 rc = mls_write_range_helper(&c->range, fp);
2783 * The following *_write functions are used to
2784 * write the symbol data to a policy database
2785 * binary representation file.
2788 static int perm_write(void *vkey, void *datum, void *fp)
2791 struct perm_datum *perdatum = datum;
2797 buf[0] = cpu_to_le32(len);
2798 buf[1] = cpu_to_le32(perdatum->value);
2799 rc = put_entry(buf, sizeof(u32), 2, fp);
2803 rc = put_entry(key, 1, len, fp);
2810 static int common_write(void *vkey, void *datum, void *ptr)
2813 struct common_datum *comdatum = datum;
2814 struct policy_data *pd = ptr;
2821 buf[0] = cpu_to_le32(len);
2822 buf[1] = cpu_to_le32(comdatum->value);
2823 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2824 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2825 rc = put_entry(buf, sizeof(u32), 4, fp);
2829 rc = put_entry(key, 1, len, fp);
2833 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2840 static int type_set_write(struct type_set *t, void *fp)
2845 if (ebitmap_write(&t->types, fp))
2847 if (ebitmap_write(&t->negset, fp))
2850 buf[0] = cpu_to_le32(t->flags);
2851 rc = put_entry(buf, sizeof(u32), 1, fp);
2858 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2861 struct constraint_node *c;
2862 struct constraint_expr *e;
2867 for (c = node; c; c = c->next) {
2869 for (e = c->expr; e; e = e->next)
2871 buf[0] = cpu_to_le32(c->permissions);
2872 buf[1] = cpu_to_le32(nel);
2873 rc = put_entry(buf, sizeof(u32), 2, fp);
2876 for (e = c->expr; e; e = e->next) {
2877 buf[0] = cpu_to_le32(e->expr_type);
2878 buf[1] = cpu_to_le32(e->attr);
2879 buf[2] = cpu_to_le32(e->op);
2880 rc = put_entry(buf, sizeof(u32), 3, fp);
2884 switch (e->expr_type) {
2886 rc = ebitmap_write(&e->names, fp);
2889 if (p->policyvers >=
2890 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2891 rc = type_set_write(e->type_names, fp);
2905 static int class_write(void *vkey, void *datum, void *ptr)
2908 struct class_datum *cladatum = datum;
2909 struct policy_data *pd = ptr;
2911 struct policydb *p = pd->p;
2912 struct constraint_node *c;
2919 if (cladatum->comkey)
2920 len2 = strlen(cladatum->comkey);
2925 for (c = cladatum->constraints; c; c = c->next)
2928 buf[0] = cpu_to_le32(len);
2929 buf[1] = cpu_to_le32(len2);
2930 buf[2] = cpu_to_le32(cladatum->value);
2931 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2932 if (cladatum->permissions.table)
2933 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2936 buf[5] = cpu_to_le32(ncons);
2937 rc = put_entry(buf, sizeof(u32), 6, fp);
2941 rc = put_entry(key, 1, len, fp);
2945 if (cladatum->comkey) {
2946 rc = put_entry(cladatum->comkey, 1, len2, fp);
2951 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2955 rc = write_cons_helper(p, cladatum->constraints, fp);
2959 /* write out the validatetrans rule */
2961 for (c = cladatum->validatetrans; c; c = c->next)
2964 buf[0] = cpu_to_le32(ncons);
2965 rc = put_entry(buf, sizeof(u32), 1, fp);
2969 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2973 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2974 buf[0] = cpu_to_le32(cladatum->default_user);
2975 buf[1] = cpu_to_le32(cladatum->default_role);
2976 buf[2] = cpu_to_le32(cladatum->default_range);
2978 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2983 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2984 buf[0] = cpu_to_le32(cladatum->default_type);
2985 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2993 static int role_write(void *vkey, void *datum, void *ptr)
2996 struct role_datum *role = datum;
2997 struct policy_data *pd = ptr;
2999 struct policydb *p = pd->p;
3006 buf[items++] = cpu_to_le32(len);
3007 buf[items++] = cpu_to_le32(role->value);
3008 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3009 buf[items++] = cpu_to_le32(role->bounds);
3011 BUG_ON(items > ARRAY_SIZE(buf));
3013 rc = put_entry(buf, sizeof(u32), items, fp);
3017 rc = put_entry(key, 1, len, fp);
3021 rc = ebitmap_write(&role->dominates, fp);
3025 rc = ebitmap_write(&role->types, fp);
3032 static int type_write(void *vkey, void *datum, void *ptr)
3035 struct type_datum *typdatum = datum;
3036 struct policy_data *pd = ptr;
3037 struct policydb *p = pd->p;
3045 buf[items++] = cpu_to_le32(len);
3046 buf[items++] = cpu_to_le32(typdatum->value);
3047 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3050 if (typdatum->primary)
3051 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3053 if (typdatum->attribute)
3054 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3056 buf[items++] = cpu_to_le32(properties);
3057 buf[items++] = cpu_to_le32(typdatum->bounds);
3059 buf[items++] = cpu_to_le32(typdatum->primary);
3061 BUG_ON(items > ARRAY_SIZE(buf));
3062 rc = put_entry(buf, sizeof(u32), items, fp);
3066 rc = put_entry(key, 1, len, fp);
3073 static int user_write(void *vkey, void *datum, void *ptr)
3076 struct user_datum *usrdatum = datum;
3077 struct policy_data *pd = ptr;
3078 struct policydb *p = pd->p;
3086 buf[items++] = cpu_to_le32(len);
3087 buf[items++] = cpu_to_le32(usrdatum->value);
3088 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3089 buf[items++] = cpu_to_le32(usrdatum->bounds);
3090 BUG_ON(items > ARRAY_SIZE(buf));
3091 rc = put_entry(buf, sizeof(u32), items, fp);
3095 rc = put_entry(key, 1, len, fp);
3099 rc = ebitmap_write(&usrdatum->roles, fp);
3103 rc = mls_write_range_helper(&usrdatum->range, fp);
3107 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3114 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3127 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3130 unsigned int i, j, rc;
3132 __be64 prefixbuf[1];
3136 for (i = 0; i < info->ocon_num; i++) {
3138 for (c = p->ocontexts[i]; c; c = c->next)
3140 buf[0] = cpu_to_le32(nel);
3141 rc = put_entry(buf, sizeof(u32), 1, fp);
3144 for (c = p->ocontexts[i]; c; c = c->next) {
3147 buf[0] = cpu_to_le32(c->sid[0]);
3148 rc = put_entry(buf, sizeof(u32), 1, fp);
3151 rc = context_write(p, &c->context[0], fp);
3157 len = strlen(c->u.name);
3158 buf[0] = cpu_to_le32(len);
3159 rc = put_entry(buf, sizeof(u32), 1, fp);
3162 rc = put_entry(c->u.name, 1, len, fp);
3165 rc = context_write(p, &c->context[0], fp);
3168 rc = context_write(p, &c->context[1], fp);
3173 buf[0] = cpu_to_le32(c->u.port.protocol);
3174 buf[1] = cpu_to_le32(c->u.port.low_port);
3175 buf[2] = cpu_to_le32(c->u.port.high_port);
3176 rc = put_entry(buf, sizeof(u32), 3, fp);
3179 rc = context_write(p, &c->context[0], fp);
3184 nodebuf[0] = c->u.node.addr; /* network order */
3185 nodebuf[1] = c->u.node.mask; /* network order */
3186 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3189 rc = context_write(p, &c->context[0], fp);
3194 buf[0] = cpu_to_le32(c->v.behavior);
3195 len = strlen(c->u.name);
3196 buf[1] = cpu_to_le32(len);
3197 rc = put_entry(buf, sizeof(u32), 2, fp);
3200 rc = put_entry(c->u.name, 1, len, fp);
3203 rc = context_write(p, &c->context[0], fp);
3208 for (j = 0; j < 4; j++)
3209 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3210 for (j = 0; j < 4; j++)
3211 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3212 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3215 rc = context_write(p, &c->context[0], fp);
3220 /* subnet_prefix is in CPU order */
3221 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3223 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3227 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3228 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3230 rc = put_entry(buf, sizeof(u32), 2, fp);
3233 rc = context_write(p, &c->context[0], fp);
3237 case OCON_IBENDPORT:
3238 len = strlen(c->u.ibendport.dev_name);
3239 buf[0] = cpu_to_le32(len);
3240 buf[1] = cpu_to_le32(c->u.ibendport.port);
3241 rc = put_entry(buf, sizeof(u32), 2, fp);
3244 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3247 rc = context_write(p, &c->context[0], fp);
3257 static int genfs_write(struct policydb *p, void *fp)
3259 struct genfs *genfs;
3266 for (genfs = p->genfs; genfs; genfs = genfs->next)
3268 buf[0] = cpu_to_le32(len);
3269 rc = put_entry(buf, sizeof(u32), 1, fp);
3272 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3273 len = strlen(genfs->fstype);
3274 buf[0] = cpu_to_le32(len);
3275 rc = put_entry(buf, sizeof(u32), 1, fp);
3278 rc = put_entry(genfs->fstype, 1, len, fp);
3282 for (c = genfs->head; c; c = c->next)
3284 buf[0] = cpu_to_le32(len);
3285 rc = put_entry(buf, sizeof(u32), 1, fp);
3288 for (c = genfs->head; c; c = c->next) {
3289 len = strlen(c->u.name);
3290 buf[0] = cpu_to_le32(len);
3291 rc = put_entry(buf, sizeof(u32), 1, fp);
3294 rc = put_entry(c->u.name, 1, len, fp);
3297 buf[0] = cpu_to_le32(c->v.sclass);
3298 rc = put_entry(buf, sizeof(u32), 1, fp);
3301 rc = context_write(p, &c->context[0], fp);
3309 static int hashtab_cnt(void *key, void *data, void *ptr)
3317 static int range_write_helper(void *key, void *data, void *ptr)
3320 struct range_trans *rt = key;
3321 struct mls_range *r = data;
3322 struct policy_data *pd = ptr;
3324 struct policydb *p = pd->p;
3327 buf[0] = cpu_to_le32(rt->source_type);
3328 buf[1] = cpu_to_le32(rt->target_type);
3329 rc = put_entry(buf, sizeof(u32), 2, fp);
3332 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3333 buf[0] = cpu_to_le32(rt->target_class);
3334 rc = put_entry(buf, sizeof(u32), 1, fp);
3338 rc = mls_write_range_helper(r, fp);
3345 static int range_write(struct policydb *p, void *fp)
3349 struct policy_data pd;
3354 /* count the number of entries in the hashtab */
3356 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3360 buf[0] = cpu_to_le32(nel);
3361 rc = put_entry(buf, sizeof(u32), 1, fp);
3365 /* actually write all of the entries */
3366 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3373 static int filename_write_helper(void *key, void *data, void *ptr)
3376 struct filename_trans *ft = key;
3377 struct filename_trans_datum *otype = data;
3382 len = strlen(ft->name);
3383 buf[0] = cpu_to_le32(len);
3384 rc = put_entry(buf, sizeof(u32), 1, fp);
3388 rc = put_entry(ft->name, sizeof(char), len, fp);
3392 buf[0] = cpu_to_le32(ft->stype);
3393 buf[1] = cpu_to_le32(ft->ttype);
3394 buf[2] = cpu_to_le32(ft->tclass);
3395 buf[3] = cpu_to_le32(otype->otype);
3397 rc = put_entry(buf, sizeof(u32), 4, fp);
3404 static int filename_trans_write(struct policydb *p, void *fp)
3410 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3414 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3418 buf[0] = cpu_to_le32(nel);
3419 rc = put_entry(buf, sizeof(u32), 1, fp);
3423 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3431 * Write the configuration data in a policy database
3432 * structure to a policy database binary representation
3435 int policydb_write(struct policydb *p, void *fp)
3437 unsigned int i, num_syms;
3442 struct policydb_compat_info *info;
3445 * refuse to write policy older than compressed avtab
3446 * to simplify the writer. There are other tests dropped
3447 * since we assume this throughout the writer code. Be
3448 * careful if you ever try to remove this restriction
3450 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3451 pr_err("SELinux: refusing to write policy version %d."
3452 " Because it is less than version %d\n", p->policyvers,
3453 POLICYDB_VERSION_AVTAB);
3459 config |= POLICYDB_CONFIG_MLS;
3461 if (p->reject_unknown)
3462 config |= REJECT_UNKNOWN;
3463 if (p->allow_unknown)
3464 config |= ALLOW_UNKNOWN;
3466 /* Write the magic number and string identifiers. */
3467 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3468 len = strlen(POLICYDB_STRING);
3469 buf[1] = cpu_to_le32(len);
3470 rc = put_entry(buf, sizeof(u32), 2, fp);
3473 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3477 /* Write the version, config, and table sizes. */
3478 info = policydb_lookup_compat(p->policyvers);
3480 pr_err("SELinux: compatibility lookup failed for policy "
3481 "version %d", p->policyvers);
3485 buf[0] = cpu_to_le32(p->policyvers);
3486 buf[1] = cpu_to_le32(config);
3487 buf[2] = cpu_to_le32(info->sym_num);
3488 buf[3] = cpu_to_le32(info->ocon_num);
3490 rc = put_entry(buf, sizeof(u32), 4, fp);
3494 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3495 rc = ebitmap_write(&p->policycaps, fp);
3500 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3501 rc = ebitmap_write(&p->permissive_map, fp);
3506 num_syms = info->sym_num;
3507 for (i = 0; i < num_syms; i++) {
3508 struct policy_data pd;
3513 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3514 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3516 rc = put_entry(buf, sizeof(u32), 2, fp);
3519 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3524 rc = avtab_write(p, &p->te_avtab, fp);
3528 rc = cond_write_list(p, p->cond_list, fp);
3532 rc = role_trans_write(p, fp);
3536 rc = role_allow_write(p->role_allow, fp);
3540 rc = filename_trans_write(p, fp);
3544 rc = ocontext_write(p, info, fp);
3548 rc = genfs_write(p, fp);
3552 rc = range_write(p, fp);
3556 for (i = 0; i < p->p_types.nprim; i++) {
3557 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3560 rc = ebitmap_write(e, fp);