1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
5 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
9 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
11 * Support for enhanced MLS infrastructure.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Updated: Hewlett-Packard <paul@paul-moore.com>
19 * Added support for the policy capability bitmap
21 * Update: Mellanox Techonologies
23 * Added Infiniband support
25 * Copyright (C) 2016 Mellanox Techonologies
26 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
27 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
28 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/audit.h>
40 #include "conditional.h"
44 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
45 static const char *const symtab_name[SYM_NUM] = {
57 struct policydb_compat_info {
60 unsigned int ocon_num;
63 /* These need to be updated if SYM_NUM or OCON_NUM changes */
64 static const struct policydb_compat_info policydb_compat[] = {
66 .version = POLICYDB_VERSION_BASE,
67 .sym_num = SYM_NUM - 3,
68 .ocon_num = OCON_NUM - 3,
71 .version = POLICYDB_VERSION_BOOL,
72 .sym_num = SYM_NUM - 2,
73 .ocon_num = OCON_NUM - 3,
76 .version = POLICYDB_VERSION_IPV6,
77 .sym_num = SYM_NUM - 2,
78 .ocon_num = OCON_NUM - 2,
81 .version = POLICYDB_VERSION_NLCLASS,
82 .sym_num = SYM_NUM - 2,
83 .ocon_num = OCON_NUM - 2,
86 .version = POLICYDB_VERSION_MLS,
88 .ocon_num = OCON_NUM - 2,
91 .version = POLICYDB_VERSION_AVTAB,
93 .ocon_num = OCON_NUM - 2,
96 .version = POLICYDB_VERSION_RANGETRANS,
98 .ocon_num = OCON_NUM - 2,
101 .version = POLICYDB_VERSION_POLCAP,
103 .ocon_num = OCON_NUM - 2,
106 .version = POLICYDB_VERSION_PERMISSIVE,
108 .ocon_num = OCON_NUM - 2,
111 .version = POLICYDB_VERSION_BOUNDARY,
113 .ocon_num = OCON_NUM - 2,
116 .version = POLICYDB_VERSION_FILENAME_TRANS,
118 .ocon_num = OCON_NUM - 2,
121 .version = POLICYDB_VERSION_ROLETRANS,
123 .ocon_num = OCON_NUM - 2,
126 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
128 .ocon_num = OCON_NUM - 2,
131 .version = POLICYDB_VERSION_DEFAULT_TYPE,
133 .ocon_num = OCON_NUM - 2,
136 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
138 .ocon_num = OCON_NUM - 2,
141 .version = POLICYDB_VERSION_XPERMS_IOCTL,
143 .ocon_num = OCON_NUM - 2,
146 .version = POLICYDB_VERSION_INFINIBAND,
148 .ocon_num = OCON_NUM,
151 .version = POLICYDB_VERSION_GLBLUB,
153 .ocon_num = OCON_NUM,
156 .version = POLICYDB_VERSION_COMP_FTRANS,
158 .ocon_num = OCON_NUM,
162 static const struct policydb_compat_info *policydb_lookup_compat(unsigned int version)
166 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
167 if (policydb_compat[i].version == version)
168 return &policydb_compat[i];
175 * The following *_destroy functions are used to
176 * free any memory allocated for each kind of
177 * symbol data in the policy database.
180 static int perm_destroy(void *key, void *datum, void *p)
187 static int common_destroy(void *key, void *datum, void *p)
189 struct common_datum *comdatum;
194 hashtab_map(&comdatum->permissions.table, perm_destroy, NULL);
195 hashtab_destroy(&comdatum->permissions.table);
201 static void constraint_expr_destroy(struct constraint_expr *expr)
204 ebitmap_destroy(&expr->names);
205 if (expr->type_names) {
206 ebitmap_destroy(&expr->type_names->types);
207 ebitmap_destroy(&expr->type_names->negset);
208 kfree(expr->type_names);
214 static int cls_destroy(void *key, void *datum, void *p)
216 struct class_datum *cladatum;
217 struct constraint_node *constraint, *ctemp;
218 struct constraint_expr *e, *etmp;
223 hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
224 hashtab_destroy(&cladatum->permissions.table);
225 constraint = cladatum->constraints;
227 e = constraint->expr;
231 constraint_expr_destroy(etmp);
234 constraint = constraint->next;
238 constraint = cladatum->validatetrans;
240 e = constraint->expr;
244 constraint_expr_destroy(etmp);
247 constraint = constraint->next;
250 kfree(cladatum->comkey);
256 static int role_destroy(void *key, void *datum, void *p)
258 struct role_datum *role;
263 ebitmap_destroy(&role->dominates);
264 ebitmap_destroy(&role->types);
270 static int type_destroy(void *key, void *datum, void *p)
277 static int user_destroy(void *key, void *datum, void *p)
279 struct user_datum *usrdatum;
284 ebitmap_destroy(&usrdatum->roles);
285 ebitmap_destroy(&usrdatum->range.level[0].cat);
286 ebitmap_destroy(&usrdatum->range.level[1].cat);
287 ebitmap_destroy(&usrdatum->dfltlevel.cat);
293 static int sens_destroy(void *key, void *datum, void *p)
295 struct level_datum *levdatum;
301 ebitmap_destroy(&levdatum->level->cat);
302 kfree(levdatum->level);
308 static int cat_destroy(void *key, void *datum, void *p)
315 static int (*const destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
326 static int filenametr_destroy(void *key, void *datum, void *p)
328 struct filename_trans_key *ft = key;
329 struct filename_trans_datum *next, *d = datum;
334 ebitmap_destroy(&d->stypes);
338 } while (unlikely(d));
343 static int range_tr_destroy(void *key, void *datum, void *p)
345 struct mls_range *rt = datum;
348 ebitmap_destroy(&rt->level[0].cat);
349 ebitmap_destroy(&rt->level[1].cat);
355 static int role_tr_destroy(void *key, void *datum, void *p)
362 static void ocontext_destroy(struct ocontext *c, unsigned int i)
367 context_destroy(&c->context[0]);
368 context_destroy(&c->context[1]);
369 if (i == OCON_ISID || i == OCON_FS ||
370 i == OCON_NETIF || i == OCON_FSUSE)
376 * Initialize the role table.
378 static int roles_init(struct policydb *p)
382 struct role_datum *role;
384 role = kzalloc(sizeof(*role), GFP_KERNEL);
389 role->value = ++p->p_roles.nprim;
390 if (role->value != OBJECT_R_VAL)
394 key = kstrdup(OBJECT_R, GFP_KERNEL);
398 rc = symtab_insert(&p->p_roles, key, role);
409 static u32 filenametr_hash(const void *k)
411 const struct filename_trans_key *ft = k;
412 unsigned long salt = ft->ttype ^ ft->tclass;
414 return full_name_hash((void *)salt, ft->name, strlen(ft->name));
417 static int filenametr_cmp(const void *k1, const void *k2)
419 const struct filename_trans_key *ft1 = k1;
420 const struct filename_trans_key *ft2 = k2;
423 v = ft1->ttype - ft2->ttype;
427 v = ft1->tclass - ft2->tclass;
431 return strcmp(ft1->name, ft2->name);
435 static const struct hashtab_key_params filenametr_key_params = {
436 .hash = filenametr_hash,
437 .cmp = filenametr_cmp,
440 struct filename_trans_datum *policydb_filenametr_search(
441 struct policydb *p, struct filename_trans_key *key)
443 return hashtab_search(&p->filename_trans, key, filenametr_key_params);
446 static u32 rangetr_hash(const void *k)
448 const struct range_trans *key = k;
450 return key->source_type + (key->target_type << 3) +
451 (key->target_class << 5);
454 static int rangetr_cmp(const void *k1, const void *k2)
456 const struct range_trans *key1 = k1, *key2 = k2;
459 v = key1->source_type - key2->source_type;
463 v = key1->target_type - key2->target_type;
467 v = key1->target_class - key2->target_class;
472 static const struct hashtab_key_params rangetr_key_params = {
473 .hash = rangetr_hash,
477 struct mls_range *policydb_rangetr_search(struct policydb *p,
478 struct range_trans *key)
480 return hashtab_search(&p->range_tr, key, rangetr_key_params);
483 static u32 role_trans_hash(const void *k)
485 const struct role_trans_key *key = k;
487 return jhash_3words(key->role, key->type, (u32)key->tclass << 16 | key->tclass, 0);
490 static int role_trans_cmp(const void *k1, const void *k2)
492 const struct role_trans_key *key1 = k1, *key2 = k2;
495 v = key1->role - key2->role;
499 v = key1->type - key2->type;
503 return key1->tclass - key2->tclass;
506 static const struct hashtab_key_params roletr_key_params = {
507 .hash = role_trans_hash,
508 .cmp = role_trans_cmp,
511 struct role_trans_datum *policydb_roletr_search(struct policydb *p,
512 struct role_trans_key *key)
514 return hashtab_search(&p->role_tr, key, roletr_key_params);
518 * Initialize a policy database structure.
520 static void policydb_init(struct policydb *p)
522 memset(p, 0, sizeof(*p));
524 avtab_init(&p->te_avtab);
525 cond_policydb_init(p);
527 ebitmap_init(&p->filename_trans_ttypes);
528 ebitmap_init(&p->policycaps);
529 ebitmap_init(&p->permissive_map);
533 * The following *_index functions are used to
534 * define the val_to_name and val_to_struct arrays
535 * in a policy database structure. The val_to_name
536 * arrays are used when converting security context
537 * structures into string representations. The
538 * val_to_struct arrays are used when the attributes
539 * of a class, role, or user are needed.
542 static int common_index(void *key, void *datum, void *datap)
545 struct common_datum *comdatum;
549 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
552 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
557 static int class_index(void *key, void *datum, void *datap)
560 struct class_datum *cladatum;
564 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
567 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
568 p->class_val_to_struct[cladatum->value - 1] = cladatum;
572 static int role_index(void *key, void *datum, void *datap)
575 struct role_datum *role;
580 || role->value > p->p_roles.nprim
581 || role->bounds > p->p_roles.nprim)
584 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
585 p->role_val_to_struct[role->value - 1] = role;
589 static int type_index(void *key, void *datum, void *datap)
592 struct type_datum *typdatum;
597 if (typdatum->primary) {
599 || typdatum->value > p->p_types.nprim
600 || typdatum->bounds > p->p_types.nprim)
602 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
603 p->type_val_to_struct[typdatum->value - 1] = typdatum;
609 static int user_index(void *key, void *datum, void *datap)
612 struct user_datum *usrdatum;
617 || usrdatum->value > p->p_users.nprim
618 || usrdatum->bounds > p->p_users.nprim)
621 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
622 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
626 static int sens_index(void *key, void *datum, void *datap)
629 struct level_datum *levdatum;
634 if (!levdatum->isalias) {
635 if (!levdatum->level->sens ||
636 levdatum->level->sens > p->p_levels.nprim)
639 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
645 static int cat_index(void *key, void *datum, void *datap)
648 struct cat_datum *catdatum;
653 if (!catdatum->isalias) {
654 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
657 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
663 static int (*const index_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
674 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
675 static void hash_eval(struct hashtab *h, const char *hash_name)
677 struct hashtab_info info;
679 hashtab_stat(h, &info);
680 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d, sum of chain length^2 %llu\n",
681 hash_name, h->nel, info.slots_used, h->size,
682 info.max_chain_len, info.chain2_len_sum);
685 static void symtab_hash_eval(struct symtab *s)
689 for (i = 0; i < SYM_NUM; i++)
690 hash_eval(&s[i].table, symtab_name[i]);
694 static inline void hash_eval(struct hashtab *h, const char *hash_name)
697 static inline void symtab_hash_eval(struct symtab *s)
700 #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
703 * Define the other val_to_name and val_to_struct arrays
704 * in a policy database structure.
706 * Caller must clean up on failure.
708 static int policydb_index(struct policydb *p)
713 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
714 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
715 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
717 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
718 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
721 pr_debug("SELinux: %d classes, %d rules\n",
722 p->p_classes.nprim, p->te_avtab.nel);
724 avtab_hash_eval(&p->te_avtab, "rules");
725 symtab_hash_eval(p->symtab);
727 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
728 sizeof(*p->class_val_to_struct),
730 if (!p->class_val_to_struct)
733 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
734 sizeof(*p->role_val_to_struct),
736 if (!p->role_val_to_struct)
739 p->user_val_to_struct = kcalloc(p->p_users.nprim,
740 sizeof(*p->user_val_to_struct),
742 if (!p->user_val_to_struct)
745 p->type_val_to_struct = kvcalloc(p->p_types.nprim,
746 sizeof(*p->type_val_to_struct),
748 if (!p->type_val_to_struct)
751 rc = cond_init_bool_indexes(p);
755 for (i = 0; i < SYM_NUM; i++) {
756 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
759 if (!p->sym_val_to_name[i])
762 rc = hashtab_map(&p->symtab[i].table, index_f[i], p);
772 * Free any memory allocated by a policy database structure.
774 void policydb_destroy(struct policydb *p)
776 struct ocontext *c, *ctmp;
777 struct genfs *g, *gtmp;
779 struct role_allow *ra, *lra = NULL;
781 for (i = 0; i < SYM_NUM; i++) {
783 hashtab_map(&p->symtab[i].table, destroy_f[i], NULL);
784 hashtab_destroy(&p->symtab[i].table);
787 for (i = 0; i < SYM_NUM; i++)
788 kvfree(p->sym_val_to_name[i]);
790 kfree(p->class_val_to_struct);
791 kfree(p->role_val_to_struct);
792 kfree(p->user_val_to_struct);
793 kvfree(p->type_val_to_struct);
795 avtab_destroy(&p->te_avtab);
797 for (i = 0; i < OCON_NUM; i++) {
803 ocontext_destroy(ctmp, i);
805 p->ocontexts[i] = NULL;
816 ocontext_destroy(ctmp, OCON_FSUSE);
824 cond_policydb_destroy(p);
826 hashtab_map(&p->role_tr, role_tr_destroy, NULL);
827 hashtab_destroy(&p->role_tr);
829 for (ra = p->role_allow; ra; ra = ra->next) {
836 hashtab_map(&p->filename_trans, filenametr_destroy, NULL);
837 hashtab_destroy(&p->filename_trans);
839 hashtab_map(&p->range_tr, range_tr_destroy, NULL);
840 hashtab_destroy(&p->range_tr);
842 if (p->type_attr_map_array) {
843 for (i = 0; i < p->p_types.nprim; i++)
844 ebitmap_destroy(&p->type_attr_map_array[i]);
845 kvfree(p->type_attr_map_array);
848 ebitmap_destroy(&p->filename_trans_ttypes);
849 ebitmap_destroy(&p->policycaps);
850 ebitmap_destroy(&p->permissive_map);
854 * Load the initial SIDs specified in a policy database
855 * structure into a SID table.
857 int policydb_load_isids(struct policydb *p, struct sidtab *s)
859 struct ocontext *head, *c;
860 bool isid_init_supported = ebitmap_get_bit(&p->policycaps,
861 POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT);
866 pr_err("SELinux: out of memory on SID table init\n");
870 head = p->ocontexts[OCON_ISID];
871 for (c = head; c; c = c->next) {
873 const char *name = security_get_initial_sid_context(sid);
875 if (sid == SECSID_NULL) {
876 pr_err("SELinux: SID 0 was assigned a context.\n");
881 /* Ignore initial SIDs unused by this kernel. */
886 * Also ignore SECINITSID_INIT if the policy doesn't declare
889 if (sid == SECINITSID_INIT && !isid_init_supported)
892 rc = sidtab_set_initial(s, sid, &c->context[0]);
894 pr_err("SELinux: unable to load initial SID %s.\n",
901 * If the policy doesn't support the "userspace_initial_context"
902 * capability, set SECINITSID_INIT to the same context as
903 * SECINITSID_KERNEL. This ensures the same behavior as before
904 * the reintroduction of SECINITSID_INIT, where all tasks
905 * started before policy load would initially get the context
906 * corresponding to SECINITSID_KERNEL.
908 if (sid == SECINITSID_KERNEL && !isid_init_supported) {
909 rc = sidtab_set_initial(s, SECINITSID_INIT, &c->context[0]);
911 pr_err("SELinux: unable to load initial SID %s.\n",
921 int policydb_class_isvalid(struct policydb *p, unsigned int class)
923 if (!class || class > p->p_classes.nprim)
928 int policydb_role_isvalid(struct policydb *p, unsigned int role)
930 if (!role || role > p->p_roles.nprim)
935 int policydb_type_isvalid(struct policydb *p, unsigned int type)
937 if (!type || type > p->p_types.nprim)
943 * Return 1 if the fields in the security context
944 * structure `c' are valid. Return 0 otherwise.
946 int policydb_context_isvalid(struct policydb *p, struct context *c)
948 struct role_datum *role;
949 struct user_datum *usrdatum;
951 if (!c->role || c->role > p->p_roles.nprim)
954 if (!c->user || c->user > p->p_users.nprim)
957 if (!c->type || c->type > p->p_types.nprim)
960 if (c->role != OBJECT_R_VAL) {
962 * Role must be authorized for the type.
964 role = p->role_val_to_struct[c->role - 1];
965 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
966 /* role may not be associated with type */
970 * User must be authorized for the role.
972 usrdatum = p->user_val_to_struct[c->user - 1];
976 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
977 /* user may not be associated with role */
981 if (!mls_context_isvalid(p, c))
988 * Read a MLS range structure from a policydb binary
989 * representation file.
991 static int mls_read_range_helper(struct mls_range *r, void *fp)
997 rc = next_entry(buf, fp, sizeof(u32));
1002 items = le32_to_cpu(buf[0]);
1003 if (items > ARRAY_SIZE(buf)) {
1004 pr_err("SELinux: mls: range overflow\n");
1008 rc = next_entry(buf, fp, sizeof(u32) * items);
1010 pr_err("SELinux: mls: truncated range\n");
1014 r->level[0].sens = le32_to_cpu(buf[0]);
1016 r->level[1].sens = le32_to_cpu(buf[1]);
1018 r->level[1].sens = r->level[0].sens;
1020 rc = ebitmap_read(&r->level[0].cat, fp);
1022 pr_err("SELinux: mls: error reading low categories\n");
1026 rc = ebitmap_read(&r->level[1].cat, fp);
1028 pr_err("SELinux: mls: error reading high categories\n");
1032 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1034 pr_err("SELinux: mls: out of memory\n");
1041 ebitmap_destroy(&r->level[0].cat);
1047 * Read and validate a security context structure
1048 * from a policydb binary representation file.
1050 static int context_read_and_validate(struct context *c,
1057 rc = next_entry(buf, fp, sizeof buf);
1059 pr_err("SELinux: context truncated\n");
1062 c->user = le32_to_cpu(buf[0]);
1063 c->role = le32_to_cpu(buf[1]);
1064 c->type = le32_to_cpu(buf[2]);
1065 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1066 rc = mls_read_range_helper(&c->range, fp);
1068 pr_err("SELinux: error reading MLS range of context\n");
1074 if (!policydb_context_isvalid(p, c)) {
1075 pr_err("SELinux: invalid security context\n");
1085 * The following *_read functions are used to
1086 * read the symbol data from a policy database
1087 * binary representation file.
1090 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1095 if ((len == 0) || (len == (u32)-1))
1098 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1102 rc = next_entry(str, fp, len);
1113 static int perm_read(struct policydb *p, struct symtab *s, void *fp)
1116 struct perm_datum *perdatum;
1121 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1125 rc = next_entry(buf, fp, sizeof buf);
1129 len = le32_to_cpu(buf[0]);
1130 perdatum->value = le32_to_cpu(buf[1]);
1132 rc = str_read(&key, GFP_KERNEL, fp, len);
1136 rc = symtab_insert(s, key, perdatum);
1142 perm_destroy(key, perdatum, NULL);
1146 static int common_read(struct policydb *p, struct symtab *s, void *fp)
1149 struct common_datum *comdatum;
1154 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1158 rc = next_entry(buf, fp, sizeof buf);
1162 len = le32_to_cpu(buf[0]);
1163 comdatum->value = le32_to_cpu(buf[1]);
1164 nel = le32_to_cpu(buf[3]);
1166 rc = symtab_init(&comdatum->permissions, nel);
1169 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1171 rc = str_read(&key, GFP_KERNEL, fp, len);
1175 for (i = 0; i < nel; i++) {
1176 rc = perm_read(p, &comdatum->permissions, fp);
1181 rc = symtab_insert(s, key, comdatum);
1186 common_destroy(key, comdatum, NULL);
1190 static void type_set_init(struct type_set *t)
1192 ebitmap_init(&t->types);
1193 ebitmap_init(&t->negset);
1196 static int type_set_read(struct type_set *t, void *fp)
1201 if (ebitmap_read(&t->types, fp))
1203 if (ebitmap_read(&t->negset, fp))
1206 rc = next_entry(buf, fp, sizeof(u32));
1209 t->flags = le32_to_cpu(buf[0]);
1215 static int read_cons_helper(struct policydb *p,
1216 struct constraint_node **nodep,
1217 u32 ncons, int allowxtarget, void *fp)
1219 struct constraint_node *c, *lc;
1220 struct constraint_expr *e, *le;
1226 for (i = 0; i < ncons; i++) {
1227 c = kzalloc(sizeof(*c), GFP_KERNEL);
1236 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1239 c->permissions = le32_to_cpu(buf[0]);
1240 nexpr = le32_to_cpu(buf[1]);
1243 for (j = 0; j < nexpr; j++) {
1244 e = kzalloc(sizeof(*e), GFP_KERNEL);
1253 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1256 e->expr_type = le32_to_cpu(buf[0]);
1257 e->attr = le32_to_cpu(buf[1]);
1258 e->op = le32_to_cpu(buf[2]);
1260 switch (e->expr_type) {
1272 if (depth == (CEXPR_MAXDEPTH - 1))
1277 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1279 if (depth == (CEXPR_MAXDEPTH - 1))
1282 rc = ebitmap_read(&e->names, fp);
1285 if (p->policyvers >=
1286 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1287 e->type_names = kzalloc(sizeof
1288 (*e->type_names), GFP_KERNEL);
1291 type_set_init(e->type_names);
1292 rc = type_set_read(e->type_names, fp);
1310 static int class_read(struct policydb *p, struct symtab *s, void *fp)
1313 struct class_datum *cladatum;
1315 u32 i, len, len2, ncons, nel;
1318 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1322 rc = next_entry(buf, fp, sizeof(u32)*6);
1326 len = le32_to_cpu(buf[0]);
1327 len2 = le32_to_cpu(buf[1]);
1328 cladatum->value = le32_to_cpu(buf[2]);
1329 nel = le32_to_cpu(buf[4]);
1331 rc = symtab_init(&cladatum->permissions, nel);
1334 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1336 ncons = le32_to_cpu(buf[5]);
1338 rc = str_read(&key, GFP_KERNEL, fp, len);
1343 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1348 cladatum->comdatum = symtab_search(&p->p_commons,
1350 if (!cladatum->comdatum) {
1351 pr_err("SELinux: unknown common %s\n",
1356 for (i = 0; i < nel; i++) {
1357 rc = perm_read(p, &cladatum->permissions, fp);
1362 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1366 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1367 /* grab the validatetrans rules */
1368 rc = next_entry(buf, fp, sizeof(u32));
1371 ncons = le32_to_cpu(buf[0]);
1372 rc = read_cons_helper(p, &cladatum->validatetrans,
1378 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1379 rc = next_entry(buf, fp, sizeof(u32) * 3);
1383 cladatum->default_user = le32_to_cpu(buf[0]);
1384 cladatum->default_role = le32_to_cpu(buf[1]);
1385 cladatum->default_range = le32_to_cpu(buf[2]);
1388 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1389 rc = next_entry(buf, fp, sizeof(u32) * 1);
1392 cladatum->default_type = le32_to_cpu(buf[0]);
1395 rc = symtab_insert(s, key, cladatum);
1401 cls_destroy(key, cladatum, NULL);
1405 static int role_read(struct policydb *p, struct symtab *s, void *fp)
1408 struct role_datum *role;
1410 unsigned int to_read = 2;
1414 role = kzalloc(sizeof(*role), GFP_KERNEL);
1418 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1421 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1425 len = le32_to_cpu(buf[0]);
1426 role->value = le32_to_cpu(buf[1]);
1427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428 role->bounds = le32_to_cpu(buf[2]);
1430 rc = str_read(&key, GFP_KERNEL, fp, len);
1434 rc = ebitmap_read(&role->dominates, fp);
1438 rc = ebitmap_read(&role->types, fp);
1442 if (strcmp(key, OBJECT_R) == 0) {
1444 if (role->value != OBJECT_R_VAL) {
1445 pr_err("SELinux: Role %s has wrong value %d\n",
1446 OBJECT_R, role->value);
1453 rc = symtab_insert(s, key, role);
1458 role_destroy(key, role, NULL);
1462 static int type_read(struct policydb *p, struct symtab *s, void *fp)
1465 struct type_datum *typdatum;
1467 unsigned int to_read = 3;
1471 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1475 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1478 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1482 len = le32_to_cpu(buf[0]);
1483 typdatum->value = le32_to_cpu(buf[1]);
1484 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1485 u32 prop = le32_to_cpu(buf[2]);
1487 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1488 typdatum->primary = 1;
1489 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1490 typdatum->attribute = 1;
1492 typdatum->bounds = le32_to_cpu(buf[3]);
1494 typdatum->primary = le32_to_cpu(buf[2]);
1497 rc = str_read(&key, GFP_KERNEL, fp, len);
1501 rc = symtab_insert(s, key, typdatum);
1506 type_destroy(key, typdatum, NULL);
1512 * Read a MLS level structure from a policydb binary
1513 * representation file.
1515 static int mls_read_level(struct mls_level *lp, void *fp)
1520 memset(lp, 0, sizeof(*lp));
1522 rc = next_entry(buf, fp, sizeof buf);
1524 pr_err("SELinux: mls: truncated level\n");
1527 lp->sens = le32_to_cpu(buf[0]);
1529 rc = ebitmap_read(&lp->cat, fp);
1531 pr_err("SELinux: mls: error reading level categories\n");
1537 static int user_read(struct policydb *p, struct symtab *s, void *fp)
1540 struct user_datum *usrdatum;
1542 unsigned int to_read = 2;
1546 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1550 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1553 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1557 len = le32_to_cpu(buf[0]);
1558 usrdatum->value = le32_to_cpu(buf[1]);
1559 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1560 usrdatum->bounds = le32_to_cpu(buf[2]);
1562 rc = str_read(&key, GFP_KERNEL, fp, len);
1566 rc = ebitmap_read(&usrdatum->roles, fp);
1570 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1571 rc = mls_read_range_helper(&usrdatum->range, fp);
1574 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1579 rc = symtab_insert(s, key, usrdatum);
1584 user_destroy(key, usrdatum, NULL);
1588 static int sens_read(struct policydb *p, struct symtab *s, void *fp)
1591 struct level_datum *levdatum;
1596 levdatum = kzalloc(sizeof(*levdatum), GFP_KERNEL);
1600 rc = next_entry(buf, fp, sizeof buf);
1604 len = le32_to_cpu(buf[0]);
1605 levdatum->isalias = le32_to_cpu(buf[1]);
1607 rc = str_read(&key, GFP_KERNEL, fp, len);
1612 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_KERNEL);
1613 if (!levdatum->level)
1616 rc = mls_read_level(levdatum->level, fp);
1620 rc = symtab_insert(s, key, levdatum);
1625 sens_destroy(key, levdatum, NULL);
1629 static int cat_read(struct policydb *p, struct symtab *s, void *fp)
1632 struct cat_datum *catdatum;
1637 catdatum = kzalloc(sizeof(*catdatum), GFP_KERNEL);
1641 rc = next_entry(buf, fp, sizeof buf);
1645 len = le32_to_cpu(buf[0]);
1646 catdatum->value = le32_to_cpu(buf[1]);
1647 catdatum->isalias = le32_to_cpu(buf[2]);
1649 rc = str_read(&key, GFP_KERNEL, fp, len);
1653 rc = symtab_insert(s, key, catdatum);
1658 cat_destroy(key, catdatum, NULL);
1662 static int (*const read_f[SYM_NUM]) (struct policydb *p,
1663 struct symtab *s, void *fp) = {
1674 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1676 struct user_datum *upper, *user;
1677 struct policydb *p = datap;
1680 upper = user = datum;
1681 while (upper->bounds) {
1682 struct ebitmap_node *node;
1685 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1686 pr_err("SELinux: user %s: "
1687 "too deep or looped boundary\n",
1692 upper = p->user_val_to_struct[upper->bounds - 1];
1693 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1694 if (ebitmap_get_bit(&upper->roles, bit))
1697 pr_err("SELinux: boundary violated policy: "
1698 "user=%s role=%s bounds=%s\n",
1699 sym_name(p, SYM_USERS, user->value - 1),
1700 sym_name(p, SYM_ROLES, bit),
1701 sym_name(p, SYM_USERS, upper->value - 1));
1710 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1712 struct role_datum *upper, *role;
1713 struct policydb *p = datap;
1716 upper = role = datum;
1717 while (upper->bounds) {
1718 struct ebitmap_node *node;
1721 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1722 pr_err("SELinux: role %s: "
1723 "too deep or looped bounds\n",
1728 upper = p->role_val_to_struct[upper->bounds - 1];
1729 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1730 if (ebitmap_get_bit(&upper->types, bit))
1733 pr_err("SELinux: boundary violated policy: "
1734 "role=%s type=%s bounds=%s\n",
1735 sym_name(p, SYM_ROLES, role->value - 1),
1736 sym_name(p, SYM_TYPES, bit),
1737 sym_name(p, SYM_ROLES, upper->value - 1));
1746 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1748 struct type_datum *upper;
1749 struct policydb *p = datap;
1753 while (upper->bounds) {
1754 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1755 pr_err("SELinux: type %s: "
1756 "too deep or looped boundary\n",
1761 upper = p->type_val_to_struct[upper->bounds - 1];
1764 if (upper->attribute) {
1765 pr_err("SELinux: type %s: "
1766 "bounded by attribute %s\n",
1768 sym_name(p, SYM_TYPES, upper->value - 1));
1776 static int policydb_bounds_sanity_check(struct policydb *p)
1780 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1783 rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
1787 rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
1791 rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
1798 u16 string_to_security_class(struct policydb *p, const char *name)
1800 struct class_datum *cladatum;
1802 cladatum = symtab_search(&p->p_classes, name);
1806 return cladatum->value;
1809 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1811 struct class_datum *cladatum;
1812 struct perm_datum *perdatum = NULL;
1813 struct common_datum *comdatum;
1815 if (!tclass || tclass > p->p_classes.nprim)
1818 cladatum = p->class_val_to_struct[tclass-1];
1819 comdatum = cladatum->comdatum;
1821 perdatum = symtab_search(&comdatum->permissions, name);
1823 perdatum = symtab_search(&cladatum->permissions, name);
1827 return 1U << (perdatum->value-1);
1830 static int range_read(struct policydb *p, void *fp)
1832 struct range_trans *rt = NULL;
1833 struct mls_range *r = NULL;
1838 if (p->policyvers < POLICYDB_VERSION_MLS)
1841 rc = next_entry(buf, fp, sizeof(u32));
1845 nel = le32_to_cpu(buf[0]);
1847 rc = hashtab_init(&p->range_tr, nel);
1851 for (i = 0; i < nel; i++) {
1853 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1857 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1861 rt->source_type = le32_to_cpu(buf[0]);
1862 rt->target_type = le32_to_cpu(buf[1]);
1863 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1864 rc = next_entry(buf, fp, sizeof(u32));
1867 rt->target_class = le32_to_cpu(buf[0]);
1869 rt->target_class = p->process_class;
1872 if (!policydb_type_isvalid(p, rt->source_type) ||
1873 !policydb_type_isvalid(p, rt->target_type) ||
1874 !policydb_class_isvalid(p, rt->target_class))
1878 r = kzalloc(sizeof(*r), GFP_KERNEL);
1882 rc = mls_read_range_helper(r, fp);
1887 if (!mls_range_isvalid(p, r)) {
1888 pr_warn("SELinux: rangetrans: invalid range\n");
1892 rc = hashtab_insert(&p->range_tr, rt, r, rangetr_key_params);
1899 hash_eval(&p->range_tr, "rangetr");
1907 static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
1909 struct filename_trans_key key, *ft = NULL;
1910 struct filename_trans_datum *last, *datum = NULL;
1912 u32 len, stype, otype;
1916 /* length of the path component string */
1917 rc = next_entry(buf, fp, sizeof(u32));
1920 len = le32_to_cpu(buf[0]);
1922 /* path component string */
1923 rc = str_read(&name, GFP_KERNEL, fp, len);
1927 rc = next_entry(buf, fp, sizeof(u32) * 4);
1931 stype = le32_to_cpu(buf[0]);
1932 key.ttype = le32_to_cpu(buf[1]);
1933 key.tclass = le32_to_cpu(buf[2]);
1936 otype = le32_to_cpu(buf[3]);
1939 datum = policydb_filenametr_search(p, &key);
1941 if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
1942 /* conflicting/duplicate rules are ignored */
1946 if (likely(datum->otype == otype))
1949 datum = datum->next;
1953 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
1957 ebitmap_init(&datum->stypes);
1958 datum->otype = otype;
1961 if (unlikely(last)) {
1965 ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
1969 rc = hashtab_insert(&p->filename_trans, ft, datum,
1970 filenametr_key_params);
1975 rc = ebitmap_set_bit(&p->filename_trans_ttypes,
1982 return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
1991 static int filename_trans_read_helper(struct policydb *p, void *fp)
1993 struct filename_trans_key *ft = NULL;
1994 struct filename_trans_datum **dst, *datum, *first = NULL;
1996 u32 len, ttype, tclass, ndatum, i;
2000 /* length of the path component string */
2001 rc = next_entry(buf, fp, sizeof(u32));
2004 len = le32_to_cpu(buf[0]);
2006 /* path component string */
2007 rc = str_read(&name, GFP_KERNEL, fp, len);
2011 rc = next_entry(buf, fp, sizeof(u32) * 3);
2015 ttype = le32_to_cpu(buf[0]);
2016 tclass = le32_to_cpu(buf[1]);
2018 ndatum = le32_to_cpu(buf[2]);
2020 pr_err("SELinux: Filename transition key with no datum\n");
2026 for (i = 0; i < ndatum; i++) {
2028 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
2035 /* ebitmap_read() will at least init the bitmap */
2036 rc = ebitmap_read(&datum->stypes, fp);
2040 rc = next_entry(buf, fp, sizeof(u32));
2044 datum->otype = le32_to_cpu(buf[0]);
2050 ft = kmalloc(sizeof(*ft), GFP_KERNEL);
2055 ft->tclass = tclass;
2058 rc = hashtab_insert(&p->filename_trans, ft, first,
2059 filenametr_key_params);
2061 pr_err("SELinux: Duplicate filename transition key\n");
2065 return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
2072 first = first->next;
2074 ebitmap_destroy(&datum->stypes);
2080 static int filename_trans_read(struct policydb *p, void *fp)
2086 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
2089 rc = next_entry(buf, fp, sizeof(u32));
2092 nel = le32_to_cpu(buf[0]);
2094 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
2095 p->compat_filename_trans_count = nel;
2097 rc = hashtab_init(&p->filename_trans, (1 << 11));
2101 for (i = 0; i < nel; i++) {
2102 rc = filename_trans_read_helper_compat(p, fp);
2107 rc = hashtab_init(&p->filename_trans, nel);
2111 for (i = 0; i < nel; i++) {
2112 rc = filename_trans_read_helper(p, fp);
2117 hash_eval(&p->filename_trans, "filenametr");
2121 static int genfs_read(struct policydb *p, void *fp)
2124 u32 i, j, nel, nel2, len, len2;
2126 struct ocontext *l, *c;
2127 struct ocontext *newc = NULL;
2128 struct genfs *genfs_p, *genfs;
2129 struct genfs *newgenfs = NULL;
2131 rc = next_entry(buf, fp, sizeof(u32));
2134 nel = le32_to_cpu(buf[0]);
2136 for (i = 0; i < nel; i++) {
2137 rc = next_entry(buf, fp, sizeof(u32));
2140 len = le32_to_cpu(buf[0]);
2143 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2147 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2151 for (genfs_p = NULL, genfs = p->genfs; genfs;
2152 genfs_p = genfs, genfs = genfs->next) {
2154 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2155 pr_err("SELinux: dup genfs fstype %s\n",
2159 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2162 newgenfs->next = genfs;
2164 genfs_p->next = newgenfs;
2166 p->genfs = newgenfs;
2170 rc = next_entry(buf, fp, sizeof(u32));
2174 nel2 = le32_to_cpu(buf[0]);
2175 for (j = 0; j < nel2; j++) {
2176 rc = next_entry(buf, fp, sizeof(u32));
2179 len = le32_to_cpu(buf[0]);
2182 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2186 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2190 rc = next_entry(buf, fp, sizeof(u32));
2194 newc->v.sclass = le32_to_cpu(buf[0]);
2195 rc = context_read_and_validate(&newc->context[0], p, fp);
2199 for (l = NULL, c = genfs->head; c;
2200 l = c, c = c->next) {
2202 if (!strcmp(newc->u.name, c->u.name) &&
2203 (!c->v.sclass || !newc->v.sclass ||
2204 newc->v.sclass == c->v.sclass)) {
2205 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2206 genfs->fstype, c->u.name);
2209 len = strlen(newc->u.name);
2210 len2 = strlen(c->u.name);
2226 kfree(newgenfs->fstype);
2229 ocontext_destroy(newc, OCON_FSUSE);
2234 static int ocontext_read(struct policydb *p, const struct policydb_compat_info *info,
2240 __be64 prefixbuf[1];
2242 struct ocontext *l, *c;
2245 for (i = 0; i < info->ocon_num; i++) {
2246 rc = next_entry(buf, fp, sizeof(u32));
2249 nel = le32_to_cpu(buf[0]);
2252 for (j = 0; j < nel; j++) {
2254 c = kzalloc(sizeof(*c), GFP_KERNEL);
2260 p->ocontexts[i] = c;
2265 rc = next_entry(buf, fp, sizeof(u32));
2269 c->sid[0] = le32_to_cpu(buf[0]);
2270 rc = context_read_and_validate(&c->context[0], p, fp);
2276 rc = next_entry(buf, fp, sizeof(u32));
2279 len = le32_to_cpu(buf[0]);
2281 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2286 pr_warn("SELinux: void and deprecated fs ocon %s\n",
2289 rc = context_read_and_validate(&c->context[0], p, fp);
2292 rc = context_read_and_validate(&c->context[1], p, fp);
2297 rc = next_entry(buf, fp, sizeof(u32)*3);
2300 c->u.port.protocol = le32_to_cpu(buf[0]);
2301 c->u.port.low_port = le32_to_cpu(buf[1]);
2302 c->u.port.high_port = le32_to_cpu(buf[2]);
2303 rc = context_read_and_validate(&c->context[0], p, fp);
2308 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2311 c->u.node.addr = nodebuf[0]; /* network order */
2312 c->u.node.mask = nodebuf[1]; /* network order */
2313 rc = context_read_and_validate(&c->context[0], p, fp);
2318 rc = next_entry(buf, fp, sizeof(u32)*2);
2323 c->v.behavior = le32_to_cpu(buf[0]);
2324 /* Determined at runtime, not in policy DB. */
2325 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2327 if (c->v.behavior > SECURITY_FS_USE_MAX)
2330 len = le32_to_cpu(buf[1]);
2331 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2335 rc = context_read_and_validate(&c->context[0], p, fp);
2342 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2345 for (k = 0; k < 4; k++)
2346 c->u.node6.addr[k] = nodebuf[k];
2347 for (k = 0; k < 4; k++)
2348 c->u.node6.mask[k] = nodebuf[k+4];
2349 rc = context_read_and_validate(&c->context[0], p, fp);
2355 u32 pkey_lo, pkey_hi;
2357 rc = next_entry(prefixbuf, fp, sizeof(u64));
2361 /* we need to have subnet_prefix in CPU order */
2362 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2364 rc = next_entry(buf, fp, sizeof(u32) * 2);
2368 pkey_lo = le32_to_cpu(buf[0]);
2369 pkey_hi = le32_to_cpu(buf[1]);
2371 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2376 c->u.ibpkey.low_pkey = pkey_lo;
2377 c->u.ibpkey.high_pkey = pkey_hi;
2379 rc = context_read_and_validate(&c->context[0],
2386 case OCON_IBENDPORT: {
2389 rc = next_entry(buf, fp, sizeof(u32) * 2);
2392 len = le32_to_cpu(buf[0]);
2394 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2398 port = le32_to_cpu(buf[1]);
2399 if (port > U8_MAX || port == 0) {
2404 c->u.ibendport.port = port;
2406 rc = context_read_and_validate(&c->context[0],
2422 * Read the configuration data from a policy database binary
2423 * representation file into a policy database structure.
2425 int policydb_read(struct policydb *p, void *fp)
2427 struct role_allow *ra, *lra;
2428 struct role_trans_key *rtk = NULL;
2429 struct role_trans_datum *rtd = NULL;
2432 u32 i, j, len, nprim, nel, perm;
2435 const struct policydb_compat_info *info;
2439 /* Read the magic number and string length. */
2440 rc = next_entry(buf, fp, sizeof(u32) * 2);
2445 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2446 pr_err("SELinux: policydb magic number 0x%x does "
2447 "not match expected magic number 0x%x\n",
2448 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2453 len = le32_to_cpu(buf[1]);
2454 if (len != strlen(POLICYDB_STRING)) {
2455 pr_err("SELinux: policydb string length %d does not "
2456 "match expected length %zu\n",
2457 len, strlen(POLICYDB_STRING));
2462 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2463 if (!policydb_str) {
2464 pr_err("SELinux: unable to allocate memory for policydb "
2465 "string of length %d\n", len);
2469 rc = next_entry(policydb_str, fp, len);
2471 pr_err("SELinux: truncated policydb string identifier\n");
2472 kfree(policydb_str);
2477 policydb_str[len] = '\0';
2478 if (strcmp(policydb_str, POLICYDB_STRING)) {
2479 pr_err("SELinux: policydb string %s does not match "
2480 "my string %s\n", policydb_str, POLICYDB_STRING);
2481 kfree(policydb_str);
2484 /* Done with policydb_str. */
2485 kfree(policydb_str);
2486 policydb_str = NULL;
2488 /* Read the version and table sizes. */
2489 rc = next_entry(buf, fp, sizeof(u32)*4);
2494 p->policyvers = le32_to_cpu(buf[0]);
2495 if (p->policyvers < POLICYDB_VERSION_MIN ||
2496 p->policyvers > POLICYDB_VERSION_MAX) {
2497 pr_err("SELinux: policydb version %d does not match "
2498 "my version range %d-%d\n",
2499 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2503 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2507 if (p->policyvers < POLICYDB_VERSION_MLS) {
2508 pr_err("SELinux: security policydb version %d "
2509 "(MLS) not backwards compatible\n",
2514 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2515 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2517 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2518 rc = ebitmap_read(&p->policycaps, fp);
2523 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2524 rc = ebitmap_read(&p->permissive_map, fp);
2530 info = policydb_lookup_compat(p->policyvers);
2532 pr_err("SELinux: unable to find policy compat info "
2533 "for version %d\n", p->policyvers);
2538 if (le32_to_cpu(buf[2]) != info->sym_num ||
2539 le32_to_cpu(buf[3]) != info->ocon_num) {
2540 pr_err("SELinux: policydb table sizes (%d,%d) do "
2541 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2542 le32_to_cpu(buf[3]),
2543 info->sym_num, info->ocon_num);
2547 for (i = 0; i < info->sym_num; i++) {
2548 rc = next_entry(buf, fp, sizeof(u32)*2);
2551 nprim = le32_to_cpu(buf[0]);
2552 nel = le32_to_cpu(buf[1]);
2554 rc = symtab_init(&p->symtab[i], nel);
2558 if (i == SYM_ROLES) {
2564 for (j = 0; j < nel; j++) {
2565 rc = read_f[i](p, &p->symtab[i], fp);
2570 p->symtab[i].nprim = nprim;
2574 p->process_class = string_to_security_class(p, "process");
2575 if (!p->process_class) {
2576 pr_err("SELinux: process class is required, not defined in policy\n");
2580 rc = avtab_read(&p->te_avtab, fp, p);
2584 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2585 rc = cond_read_list(p, fp);
2590 rc = next_entry(buf, fp, sizeof(u32));
2593 nel = le32_to_cpu(buf[0]);
2595 rc = hashtab_init(&p->role_tr, nel);
2598 for (i = 0; i < nel; i++) {
2600 rtk = kmalloc(sizeof(*rtk), GFP_KERNEL);
2605 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
2609 rc = next_entry(buf, fp, sizeof(u32)*3);
2613 rtk->role = le32_to_cpu(buf[0]);
2614 rtk->type = le32_to_cpu(buf[1]);
2615 rtd->new_role = le32_to_cpu(buf[2]);
2616 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2617 rc = next_entry(buf, fp, sizeof(u32));
2620 rtk->tclass = le32_to_cpu(buf[0]);
2622 rtk->tclass = p->process_class;
2625 if (!policydb_role_isvalid(p, rtk->role) ||
2626 !policydb_type_isvalid(p, rtk->type) ||
2627 !policydb_class_isvalid(p, rtk->tclass) ||
2628 !policydb_role_isvalid(p, rtd->new_role))
2631 rc = hashtab_insert(&p->role_tr, rtk, rtd, roletr_key_params);
2639 rc = next_entry(buf, fp, sizeof(u32));
2642 nel = le32_to_cpu(buf[0]);
2644 for (i = 0; i < nel; i++) {
2646 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2653 rc = next_entry(buf, fp, sizeof(u32)*2);
2658 ra->role = le32_to_cpu(buf[0]);
2659 ra->new_role = le32_to_cpu(buf[1]);
2660 if (!policydb_role_isvalid(p, ra->role) ||
2661 !policydb_role_isvalid(p, ra->new_role))
2666 rc = filename_trans_read(p, fp);
2670 rc = policydb_index(p);
2675 perm = string_to_av_perm(p, p->process_class, "transition");
2677 pr_err("SELinux: process transition permission is required, not defined in policy\n");
2680 p->process_trans_perms = perm;
2681 perm = string_to_av_perm(p, p->process_class, "dyntransition");
2683 pr_err("SELinux: process dyntransition permission is required, not defined in policy\n");
2686 p->process_trans_perms |= perm;
2688 rc = ocontext_read(p, info, fp);
2692 rc = genfs_read(p, fp);
2696 rc = range_read(p, fp);
2701 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2702 sizeof(*p->type_attr_map_array),
2704 if (!p->type_attr_map_array)
2707 /* just in case ebitmap_init() becomes more than just a memset(0): */
2708 for (i = 0; i < p->p_types.nprim; i++)
2709 ebitmap_init(&p->type_attr_map_array[i]);
2711 for (i = 0; i < p->p_types.nprim; i++) {
2712 struct ebitmap *e = &p->type_attr_map_array[i];
2714 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2715 rc = ebitmap_read(e, fp);
2719 /* add the type itself as the degenerate case */
2720 rc = ebitmap_set_bit(e, i, 1);
2725 rc = policydb_bounds_sanity_check(p);
2735 policydb_destroy(p);
2740 * Write a MLS level structure to a policydb binary
2741 * representation file.
2743 static int mls_write_level(struct mls_level *l, void *fp)
2748 buf[0] = cpu_to_le32(l->sens);
2749 rc = put_entry(buf, sizeof(u32), 1, fp);
2753 rc = ebitmap_write(&l->cat, fp);
2761 * Write a MLS range structure to a policydb binary
2762 * representation file.
2764 static int mls_write_range_helper(struct mls_range *r, void *fp)
2770 eq = mls_level_eq(&r->level[1], &r->level[0]);
2776 buf[0] = cpu_to_le32(items-1);
2777 buf[1] = cpu_to_le32(r->level[0].sens);
2779 buf[2] = cpu_to_le32(r->level[1].sens);
2781 BUG_ON(items > ARRAY_SIZE(buf));
2783 rc = put_entry(buf, sizeof(u32), items, fp);
2787 rc = ebitmap_write(&r->level[0].cat, fp);
2791 rc = ebitmap_write(&r->level[1].cat, fp);
2799 static int sens_write(void *vkey, void *datum, void *ptr)
2802 struct level_datum *levdatum = datum;
2803 struct policy_data *pd = ptr;
2810 buf[0] = cpu_to_le32(len);
2811 buf[1] = cpu_to_le32(levdatum->isalias);
2812 rc = put_entry(buf, sizeof(u32), 2, fp);
2816 rc = put_entry(key, 1, len, fp);
2820 rc = mls_write_level(levdatum->level, fp);
2827 static int cat_write(void *vkey, void *datum, void *ptr)
2830 struct cat_datum *catdatum = datum;
2831 struct policy_data *pd = ptr;
2838 buf[0] = cpu_to_le32(len);
2839 buf[1] = cpu_to_le32(catdatum->value);
2840 buf[2] = cpu_to_le32(catdatum->isalias);
2841 rc = put_entry(buf, sizeof(u32), 3, fp);
2845 rc = put_entry(key, 1, len, fp);
2852 static int role_trans_write_one(void *key, void *datum, void *ptr)
2854 struct role_trans_key *rtk = key;
2855 struct role_trans_datum *rtd = datum;
2856 struct policy_data *pd = ptr;
2858 struct policydb *p = pd->p;
2862 buf[0] = cpu_to_le32(rtk->role);
2863 buf[1] = cpu_to_le32(rtk->type);
2864 buf[2] = cpu_to_le32(rtd->new_role);
2865 rc = put_entry(buf, sizeof(u32), 3, fp);
2868 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2869 buf[0] = cpu_to_le32(rtk->tclass);
2870 rc = put_entry(buf, sizeof(u32), 1, fp);
2877 static int role_trans_write(struct policydb *p, void *fp)
2879 struct policy_data pd = { .p = p, .fp = fp };
2883 buf[0] = cpu_to_le32(p->role_tr.nel);
2884 rc = put_entry(buf, sizeof(u32), 1, fp);
2888 return hashtab_map(&p->role_tr, role_trans_write_one, &pd);
2891 static int role_allow_write(struct role_allow *r, void *fp)
2893 struct role_allow *ra;
2899 for (ra = r; ra; ra = ra->next)
2901 buf[0] = cpu_to_le32(nel);
2902 rc = put_entry(buf, sizeof(u32), 1, fp);
2905 for (ra = r; ra; ra = ra->next) {
2906 buf[0] = cpu_to_le32(ra->role);
2907 buf[1] = cpu_to_le32(ra->new_role);
2908 rc = put_entry(buf, sizeof(u32), 2, fp);
2916 * Write a security context structure
2917 * to a policydb binary representation file.
2919 static int context_write(struct policydb *p, struct context *c,
2925 buf[0] = cpu_to_le32(c->user);
2926 buf[1] = cpu_to_le32(c->role);
2927 buf[2] = cpu_to_le32(c->type);
2929 rc = put_entry(buf, sizeof(u32), 3, fp);
2933 rc = mls_write_range_helper(&c->range, fp);
2941 * The following *_write functions are used to
2942 * write the symbol data to a policy database
2943 * binary representation file.
2946 static int perm_write(void *vkey, void *datum, void *fp)
2949 struct perm_datum *perdatum = datum;
2955 buf[0] = cpu_to_le32(len);
2956 buf[1] = cpu_to_le32(perdatum->value);
2957 rc = put_entry(buf, sizeof(u32), 2, fp);
2961 rc = put_entry(key, 1, len, fp);
2968 static int common_write(void *vkey, void *datum, void *ptr)
2971 struct common_datum *comdatum = datum;
2972 struct policy_data *pd = ptr;
2979 buf[0] = cpu_to_le32(len);
2980 buf[1] = cpu_to_le32(comdatum->value);
2981 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2982 buf[3] = cpu_to_le32(comdatum->permissions.table.nel);
2983 rc = put_entry(buf, sizeof(u32), 4, fp);
2987 rc = put_entry(key, 1, len, fp);
2991 rc = hashtab_map(&comdatum->permissions.table, perm_write, fp);
2998 static int type_set_write(struct type_set *t, void *fp)
3003 if (ebitmap_write(&t->types, fp))
3005 if (ebitmap_write(&t->negset, fp))
3008 buf[0] = cpu_to_le32(t->flags);
3009 rc = put_entry(buf, sizeof(u32), 1, fp);
3016 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
3019 struct constraint_node *c;
3020 struct constraint_expr *e;
3025 for (c = node; c; c = c->next) {
3027 for (e = c->expr; e; e = e->next)
3029 buf[0] = cpu_to_le32(c->permissions);
3030 buf[1] = cpu_to_le32(nel);
3031 rc = put_entry(buf, sizeof(u32), 2, fp);
3034 for (e = c->expr; e; e = e->next) {
3035 buf[0] = cpu_to_le32(e->expr_type);
3036 buf[1] = cpu_to_le32(e->attr);
3037 buf[2] = cpu_to_le32(e->op);
3038 rc = put_entry(buf, sizeof(u32), 3, fp);
3042 switch (e->expr_type) {
3044 rc = ebitmap_write(&e->names, fp);
3047 if (p->policyvers >=
3048 POLICYDB_VERSION_CONSTRAINT_NAMES) {
3049 rc = type_set_write(e->type_names, fp);
3063 static int class_write(void *vkey, void *datum, void *ptr)
3066 struct class_datum *cladatum = datum;
3067 struct policy_data *pd = ptr;
3069 struct policydb *p = pd->p;
3070 struct constraint_node *c;
3077 if (cladatum->comkey)
3078 len2 = strlen(cladatum->comkey);
3083 for (c = cladatum->constraints; c; c = c->next)
3086 buf[0] = cpu_to_le32(len);
3087 buf[1] = cpu_to_le32(len2);
3088 buf[2] = cpu_to_le32(cladatum->value);
3089 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
3090 buf[4] = cpu_to_le32(cladatum->permissions.table.nel);
3091 buf[5] = cpu_to_le32(ncons);
3092 rc = put_entry(buf, sizeof(u32), 6, fp);
3096 rc = put_entry(key, 1, len, fp);
3100 if (cladatum->comkey) {
3101 rc = put_entry(cladatum->comkey, 1, len2, fp);
3106 rc = hashtab_map(&cladatum->permissions.table, perm_write, fp);
3110 rc = write_cons_helper(p, cladatum->constraints, fp);
3114 /* write out the validatetrans rule */
3116 for (c = cladatum->validatetrans; c; c = c->next)
3119 buf[0] = cpu_to_le32(ncons);
3120 rc = put_entry(buf, sizeof(u32), 1, fp);
3124 rc = write_cons_helper(p, cladatum->validatetrans, fp);
3128 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
3129 buf[0] = cpu_to_le32(cladatum->default_user);
3130 buf[1] = cpu_to_le32(cladatum->default_role);
3131 buf[2] = cpu_to_le32(cladatum->default_range);
3133 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
3138 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
3139 buf[0] = cpu_to_le32(cladatum->default_type);
3140 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
3148 static int role_write(void *vkey, void *datum, void *ptr)
3151 struct role_datum *role = datum;
3152 struct policy_data *pd = ptr;
3154 struct policydb *p = pd->p;
3161 buf[items++] = cpu_to_le32(len);
3162 buf[items++] = cpu_to_le32(role->value);
3163 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3164 buf[items++] = cpu_to_le32(role->bounds);
3166 BUG_ON(items > ARRAY_SIZE(buf));
3168 rc = put_entry(buf, sizeof(u32), items, fp);
3172 rc = put_entry(key, 1, len, fp);
3176 rc = ebitmap_write(&role->dominates, fp);
3180 rc = ebitmap_write(&role->types, fp);
3187 static int type_write(void *vkey, void *datum, void *ptr)
3190 struct type_datum *typdatum = datum;
3191 struct policy_data *pd = ptr;
3192 struct policydb *p = pd->p;
3200 buf[items++] = cpu_to_le32(len);
3201 buf[items++] = cpu_to_le32(typdatum->value);
3202 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3205 if (typdatum->primary)
3206 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3208 if (typdatum->attribute)
3209 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3211 buf[items++] = cpu_to_le32(properties);
3212 buf[items++] = cpu_to_le32(typdatum->bounds);
3214 buf[items++] = cpu_to_le32(typdatum->primary);
3216 BUG_ON(items > ARRAY_SIZE(buf));
3217 rc = put_entry(buf, sizeof(u32), items, fp);
3221 rc = put_entry(key, 1, len, fp);
3228 static int user_write(void *vkey, void *datum, void *ptr)
3231 struct user_datum *usrdatum = datum;
3232 struct policy_data *pd = ptr;
3233 struct policydb *p = pd->p;
3241 buf[items++] = cpu_to_le32(len);
3242 buf[items++] = cpu_to_le32(usrdatum->value);
3243 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3244 buf[items++] = cpu_to_le32(usrdatum->bounds);
3245 BUG_ON(items > ARRAY_SIZE(buf));
3246 rc = put_entry(buf, sizeof(u32), items, fp);
3250 rc = put_entry(key, 1, len, fp);
3254 rc = ebitmap_write(&usrdatum->roles, fp);
3258 rc = mls_write_range_helper(&usrdatum->range, fp);
3262 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3269 static int (*const write_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
3280 static int ocontext_write(struct policydb *p, const struct policydb_compat_info *info,
3286 __be64 prefixbuf[1];
3290 for (i = 0; i < info->ocon_num; i++) {
3292 for (c = p->ocontexts[i]; c; c = c->next)
3294 buf[0] = cpu_to_le32(nel);
3295 rc = put_entry(buf, sizeof(u32), 1, fp);
3298 for (c = p->ocontexts[i]; c; c = c->next) {
3301 buf[0] = cpu_to_le32(c->sid[0]);
3302 rc = put_entry(buf, sizeof(u32), 1, fp);
3305 rc = context_write(p, &c->context[0], fp);
3311 len = strlen(c->u.name);
3312 buf[0] = cpu_to_le32(len);
3313 rc = put_entry(buf, sizeof(u32), 1, fp);
3316 rc = put_entry(c->u.name, 1, len, fp);
3319 rc = context_write(p, &c->context[0], fp);
3322 rc = context_write(p, &c->context[1], fp);
3327 buf[0] = cpu_to_le32(c->u.port.protocol);
3328 buf[1] = cpu_to_le32(c->u.port.low_port);
3329 buf[2] = cpu_to_le32(c->u.port.high_port);
3330 rc = put_entry(buf, sizeof(u32), 3, fp);
3333 rc = context_write(p, &c->context[0], fp);
3338 nodebuf[0] = c->u.node.addr; /* network order */
3339 nodebuf[1] = c->u.node.mask; /* network order */
3340 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3343 rc = context_write(p, &c->context[0], fp);
3348 buf[0] = cpu_to_le32(c->v.behavior);
3349 len = strlen(c->u.name);
3350 buf[1] = cpu_to_le32(len);
3351 rc = put_entry(buf, sizeof(u32), 2, fp);
3354 rc = put_entry(c->u.name, 1, len, fp);
3357 rc = context_write(p, &c->context[0], fp);
3362 for (j = 0; j < 4; j++)
3363 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3364 for (j = 0; j < 4; j++)
3365 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3366 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3369 rc = context_write(p, &c->context[0], fp);
3374 /* subnet_prefix is in CPU order */
3375 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3377 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3381 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3382 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3384 rc = put_entry(buf, sizeof(u32), 2, fp);
3387 rc = context_write(p, &c->context[0], fp);
3391 case OCON_IBENDPORT:
3392 len = strlen(c->u.ibendport.dev_name);
3393 buf[0] = cpu_to_le32(len);
3394 buf[1] = cpu_to_le32(c->u.ibendport.port);
3395 rc = put_entry(buf, sizeof(u32), 2, fp);
3398 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3401 rc = context_write(p, &c->context[0], fp);
3411 static int genfs_write(struct policydb *p, void *fp)
3413 struct genfs *genfs;
3420 for (genfs = p->genfs; genfs; genfs = genfs->next)
3422 buf[0] = cpu_to_le32(len);
3423 rc = put_entry(buf, sizeof(u32), 1, fp);
3426 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3427 len = strlen(genfs->fstype);
3428 buf[0] = cpu_to_le32(len);
3429 rc = put_entry(buf, sizeof(u32), 1, fp);
3432 rc = put_entry(genfs->fstype, 1, len, fp);
3436 for (c = genfs->head; c; c = c->next)
3438 buf[0] = cpu_to_le32(len);
3439 rc = put_entry(buf, sizeof(u32), 1, fp);
3442 for (c = genfs->head; c; c = c->next) {
3443 len = strlen(c->u.name);
3444 buf[0] = cpu_to_le32(len);
3445 rc = put_entry(buf, sizeof(u32), 1, fp);
3448 rc = put_entry(c->u.name, 1, len, fp);
3451 buf[0] = cpu_to_le32(c->v.sclass);
3452 rc = put_entry(buf, sizeof(u32), 1, fp);
3455 rc = context_write(p, &c->context[0], fp);
3463 static int range_write_helper(void *key, void *data, void *ptr)
3466 struct range_trans *rt = key;
3467 struct mls_range *r = data;
3468 struct policy_data *pd = ptr;
3470 struct policydb *p = pd->p;
3473 buf[0] = cpu_to_le32(rt->source_type);
3474 buf[1] = cpu_to_le32(rt->target_type);
3475 rc = put_entry(buf, sizeof(u32), 2, fp);
3478 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3479 buf[0] = cpu_to_le32(rt->target_class);
3480 rc = put_entry(buf, sizeof(u32), 1, fp);
3484 rc = mls_write_range_helper(r, fp);
3491 static int range_write(struct policydb *p, void *fp)
3495 struct policy_data pd;
3500 buf[0] = cpu_to_le32(p->range_tr.nel);
3501 rc = put_entry(buf, sizeof(u32), 1, fp);
3505 /* actually write all of the entries */
3506 rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
3513 static int filename_write_helper_compat(void *key, void *data, void *ptr)
3515 struct filename_trans_key *ft = key;
3516 struct filename_trans_datum *datum = data;
3517 struct ebitmap_node *node;
3521 u32 bit, len = strlen(ft->name);
3524 ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
3525 buf[0] = cpu_to_le32(len);
3526 rc = put_entry(buf, sizeof(u32), 1, fp);
3530 rc = put_entry(ft->name, sizeof(char), len, fp);
3534 buf[0] = cpu_to_le32(bit + 1);
3535 buf[1] = cpu_to_le32(ft->ttype);
3536 buf[2] = cpu_to_le32(ft->tclass);
3537 buf[3] = cpu_to_le32(datum->otype);
3539 rc = put_entry(buf, sizeof(u32), 4, fp);
3544 datum = datum->next;
3545 } while (unlikely(datum));
3550 static int filename_write_helper(void *key, void *data, void *ptr)
3552 struct filename_trans_key *ft = key;
3553 struct filename_trans_datum *datum;
3557 u32 ndatum, len = strlen(ft->name);
3559 buf[0] = cpu_to_le32(len);
3560 rc = put_entry(buf, sizeof(u32), 1, fp);
3564 rc = put_entry(ft->name, sizeof(char), len, fp);
3572 datum = datum->next;
3573 } while (unlikely(datum));
3575 buf[0] = cpu_to_le32(ft->ttype);
3576 buf[1] = cpu_to_le32(ft->tclass);
3577 buf[2] = cpu_to_le32(ndatum);
3578 rc = put_entry(buf, sizeof(u32), 3, fp);
3584 rc = ebitmap_write(&datum->stypes, fp);
3588 buf[0] = cpu_to_le32(datum->otype);
3589 rc = put_entry(buf, sizeof(u32), 1, fp);
3593 datum = datum->next;
3594 } while (unlikely(datum));
3599 static int filename_trans_write(struct policydb *p, void *fp)
3604 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3607 if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
3608 buf[0] = cpu_to_le32(p->compat_filename_trans_count);
3609 rc = put_entry(buf, sizeof(u32), 1, fp);
3613 rc = hashtab_map(&p->filename_trans,
3614 filename_write_helper_compat, fp);
3616 buf[0] = cpu_to_le32(p->filename_trans.nel);
3617 rc = put_entry(buf, sizeof(u32), 1, fp);
3621 rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
3627 * Write the configuration data in a policy database
3628 * structure to a policy database binary representation
3631 int policydb_write(struct policydb *p, void *fp)
3633 unsigned int num_syms;
3638 const struct policydb_compat_info *info;
3641 * refuse to write policy older than compressed avtab
3642 * to simplify the writer. There are other tests dropped
3643 * since we assume this throughout the writer code. Be
3644 * careful if you ever try to remove this restriction
3646 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3647 pr_err("SELinux: refusing to write policy version %d."
3648 " Because it is less than version %d\n", p->policyvers,
3649 POLICYDB_VERSION_AVTAB);
3655 config |= POLICYDB_CONFIG_MLS;
3657 if (p->reject_unknown)
3658 config |= REJECT_UNKNOWN;
3659 if (p->allow_unknown)
3660 config |= ALLOW_UNKNOWN;
3662 /* Write the magic number and string identifiers. */
3663 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3664 len = strlen(POLICYDB_STRING);
3665 buf[1] = cpu_to_le32(len);
3666 rc = put_entry(buf, sizeof(u32), 2, fp);
3669 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3673 /* Write the version, config, and table sizes. */
3674 info = policydb_lookup_compat(p->policyvers);
3676 pr_err("SELinux: compatibility lookup failed for policy "
3677 "version %d\n", p->policyvers);
3681 buf[0] = cpu_to_le32(p->policyvers);
3682 buf[1] = cpu_to_le32(config);
3683 buf[2] = cpu_to_le32(info->sym_num);
3684 buf[3] = cpu_to_le32(info->ocon_num);
3686 rc = put_entry(buf, sizeof(u32), 4, fp);
3690 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3691 rc = ebitmap_write(&p->policycaps, fp);
3696 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3697 rc = ebitmap_write(&p->permissive_map, fp);
3702 num_syms = info->sym_num;
3703 for (i = 0; i < num_syms; i++) {
3704 struct policy_data pd;
3709 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3710 buf[1] = cpu_to_le32(p->symtab[i].table.nel);
3712 rc = put_entry(buf, sizeof(u32), 2, fp);
3715 rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
3720 rc = avtab_write(p, &p->te_avtab, fp);
3724 rc = cond_write_list(p, fp);
3728 rc = role_trans_write(p, fp);
3732 rc = role_allow_write(p->role_allow, fp);
3736 rc = filename_trans_write(p, fp);
3740 rc = ocontext_write(p, info, fp);
3744 rc = genfs_write(p, fp);
3748 rc = range_write(p, fp);
3752 for (i = 0; i < p->p_types.nprim; i++) {
3753 struct ebitmap *e = &p->type_attr_map_array[i];
3755 rc = ebitmap_write(e, fp);