1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
9 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
11 * Support for enhanced MLS infrastructure.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Updated: Hewlett-Packard <paul@paul-moore.com>
19 * Added support for the policy capability bitmap
21 * Update: Mellanox Techonologies
23 * Added Infiniband support
25 * Copyright (C) 2016 Mellanox Techonologies
26 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
27 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
28 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/audit.h>
40 #include "conditional.h"
47 static const char *symtab_name[SYM_NUM] = {
59 static unsigned int symtab_sizes[SYM_NUM] = {
70 struct policydb_compat_info {
76 /* These need to be updated if SYM_NUM or OCON_NUM changes */
77 static struct policydb_compat_info policydb_compat[] = {
79 .version = POLICYDB_VERSION_BASE,
80 .sym_num = SYM_NUM - 3,
81 .ocon_num = OCON_NUM - 3,
84 .version = POLICYDB_VERSION_BOOL,
85 .sym_num = SYM_NUM - 2,
86 .ocon_num = OCON_NUM - 3,
89 .version = POLICYDB_VERSION_IPV6,
90 .sym_num = SYM_NUM - 2,
91 .ocon_num = OCON_NUM - 2,
94 .version = POLICYDB_VERSION_NLCLASS,
95 .sym_num = SYM_NUM - 2,
96 .ocon_num = OCON_NUM - 2,
99 .version = POLICYDB_VERSION_MLS,
101 .ocon_num = OCON_NUM - 2,
104 .version = POLICYDB_VERSION_AVTAB,
106 .ocon_num = OCON_NUM - 2,
109 .version = POLICYDB_VERSION_RANGETRANS,
111 .ocon_num = OCON_NUM - 2,
114 .version = POLICYDB_VERSION_POLCAP,
116 .ocon_num = OCON_NUM - 2,
119 .version = POLICYDB_VERSION_PERMISSIVE,
121 .ocon_num = OCON_NUM - 2,
124 .version = POLICYDB_VERSION_BOUNDARY,
126 .ocon_num = OCON_NUM - 2,
129 .version = POLICYDB_VERSION_FILENAME_TRANS,
131 .ocon_num = OCON_NUM - 2,
134 .version = POLICYDB_VERSION_ROLETRANS,
136 .ocon_num = OCON_NUM - 2,
139 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
141 .ocon_num = OCON_NUM - 2,
144 .version = POLICYDB_VERSION_DEFAULT_TYPE,
146 .ocon_num = OCON_NUM - 2,
149 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
151 .ocon_num = OCON_NUM - 2,
154 .version = POLICYDB_VERSION_XPERMS_IOCTL,
156 .ocon_num = OCON_NUM - 2,
159 .version = POLICYDB_VERSION_INFINIBAND,
161 .ocon_num = OCON_NUM,
164 .version = POLICYDB_VERSION_GLBLUB,
166 .ocon_num = OCON_NUM,
170 static struct policydb_compat_info *policydb_lookup_compat(int version)
173 struct policydb_compat_info *info = NULL;
175 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
176 if (policydb_compat[i].version == version) {
177 info = &policydb_compat[i];
185 * The following *_destroy functions are used to
186 * free any memory allocated for each kind of
187 * symbol data in the policy database.
190 static int perm_destroy(void *key, void *datum, void *p)
197 static int common_destroy(void *key, void *datum, void *p)
199 struct common_datum *comdatum;
204 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
205 hashtab_destroy(comdatum->permissions.table);
211 static void constraint_expr_destroy(struct constraint_expr *expr)
214 ebitmap_destroy(&expr->names);
215 if (expr->type_names) {
216 ebitmap_destroy(&expr->type_names->types);
217 ebitmap_destroy(&expr->type_names->negset);
218 kfree(expr->type_names);
224 static int cls_destroy(void *key, void *datum, void *p)
226 struct class_datum *cladatum;
227 struct constraint_node *constraint, *ctemp;
228 struct constraint_expr *e, *etmp;
233 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
234 hashtab_destroy(cladatum->permissions.table);
235 constraint = cladatum->constraints;
237 e = constraint->expr;
241 constraint_expr_destroy(etmp);
244 constraint = constraint->next;
248 constraint = cladatum->validatetrans;
250 e = constraint->expr;
254 constraint_expr_destroy(etmp);
257 constraint = constraint->next;
260 kfree(cladatum->comkey);
266 static int role_destroy(void *key, void *datum, void *p)
268 struct role_datum *role;
273 ebitmap_destroy(&role->dominates);
274 ebitmap_destroy(&role->types);
280 static int type_destroy(void *key, void *datum, void *p)
287 static int user_destroy(void *key, void *datum, void *p)
289 struct user_datum *usrdatum;
294 ebitmap_destroy(&usrdatum->roles);
295 ebitmap_destroy(&usrdatum->range.level[0].cat);
296 ebitmap_destroy(&usrdatum->range.level[1].cat);
297 ebitmap_destroy(&usrdatum->dfltlevel.cat);
303 static int sens_destroy(void *key, void *datum, void *p)
305 struct level_datum *levdatum;
311 ebitmap_destroy(&levdatum->level->cat);
312 kfree(levdatum->level);
318 static int cat_destroy(void *key, void *datum, void *p)
325 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
337 static int filenametr_destroy(void *key, void *datum, void *p)
339 struct filename_trans *ft = key;
348 static int range_tr_destroy(void *key, void *datum, void *p)
350 struct mls_range *rt = datum;
353 ebitmap_destroy(&rt->level[0].cat);
354 ebitmap_destroy(&rt->level[1].cat);
360 static void ocontext_destroy(struct ocontext *c, int i)
365 context_destroy(&c->context[0]);
366 context_destroy(&c->context[1]);
367 if (i == OCON_ISID || i == OCON_FS ||
368 i == OCON_NETIF || i == OCON_FSUSE)
374 * Initialize the role table.
376 static int roles_init(struct policydb *p)
380 struct role_datum *role;
382 role = kzalloc(sizeof(*role), GFP_KERNEL);
387 role->value = ++p->p_roles.nprim;
388 if (role->value != OBJECT_R_VAL)
392 key = kstrdup(OBJECT_R, GFP_KERNEL);
396 rc = hashtab_insert(p->p_roles.table, key, role);
407 static u32 filenametr_hash(struct hashtab *h, const void *k)
409 const struct filename_trans *ft = k;
411 unsigned int byte_num;
414 hash = ft->stype ^ ft->ttype ^ ft->tclass;
417 while ((focus = ft->name[byte_num++]))
418 hash = partial_name_hash(focus, hash);
419 return hash & (h->size - 1);
422 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
424 const struct filename_trans *ft1 = k1;
425 const struct filename_trans *ft2 = k2;
428 v = ft1->stype - ft2->stype;
432 v = ft1->ttype - ft2->ttype;
436 v = ft1->tclass - ft2->tclass;
440 return strcmp(ft1->name, ft2->name);
444 static u32 rangetr_hash(struct hashtab *h, const void *k)
446 const struct range_trans *key = k;
448 return (key->source_type + (key->target_type << 3) +
449 (key->target_class << 5)) & (h->size - 1);
452 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
454 const struct range_trans *key1 = k1, *key2 = k2;
457 v = key1->source_type - key2->source_type;
461 v = key1->target_type - key2->target_type;
465 v = key1->target_class - key2->target_class;
471 * Initialize a policy database structure.
473 static int policydb_init(struct policydb *p)
477 memset(p, 0, sizeof(*p));
479 for (i = 0; i < SYM_NUM; i++) {
480 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
485 rc = avtab_init(&p->te_avtab);
493 rc = cond_policydb_init(p);
497 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
499 if (!p->filename_trans) {
504 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
510 ebitmap_init(&p->filename_trans_ttypes);
511 ebitmap_init(&p->policycaps);
512 ebitmap_init(&p->permissive_map);
516 hashtab_destroy(p->filename_trans);
517 hashtab_destroy(p->range_tr);
518 for (i = 0; i < SYM_NUM; i++) {
519 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
520 hashtab_destroy(p->symtab[i].table);
526 * The following *_index functions are used to
527 * define the val_to_name and val_to_struct arrays
528 * in a policy database structure. The val_to_name
529 * arrays are used when converting security context
530 * structures into string representations. The
531 * val_to_struct arrays are used when the attributes
532 * of a class, role, or user are needed.
535 static int common_index(void *key, void *datum, void *datap)
538 struct common_datum *comdatum;
542 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
545 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
550 static int class_index(void *key, void *datum, void *datap)
553 struct class_datum *cladatum;
557 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
560 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
561 p->class_val_to_struct[cladatum->value - 1] = cladatum;
565 static int role_index(void *key, void *datum, void *datap)
568 struct role_datum *role;
573 || role->value > p->p_roles.nprim
574 || role->bounds > p->p_roles.nprim)
577 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
578 p->role_val_to_struct[role->value - 1] = role;
582 static int type_index(void *key, void *datum, void *datap)
585 struct type_datum *typdatum;
590 if (typdatum->primary) {
592 || typdatum->value > p->p_types.nprim
593 || typdatum->bounds > p->p_types.nprim)
595 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
596 p->type_val_to_struct[typdatum->value - 1] = typdatum;
602 static int user_index(void *key, void *datum, void *datap)
605 struct user_datum *usrdatum;
610 || usrdatum->value > p->p_users.nprim
611 || usrdatum->bounds > p->p_users.nprim)
614 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
615 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
619 static int sens_index(void *key, void *datum, void *datap)
622 struct level_datum *levdatum;
627 if (!levdatum->isalias) {
628 if (!levdatum->level->sens ||
629 levdatum->level->sens > p->p_levels.nprim)
632 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
638 static int cat_index(void *key, void *datum, void *datap)
641 struct cat_datum *catdatum;
646 if (!catdatum->isalias) {
647 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
650 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
656 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
669 static void hash_eval(struct hashtab *h, const char *hash_name)
671 struct hashtab_info info;
673 hashtab_stat(h, &info);
674 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
675 hash_name, h->nel, info.slots_used, h->size,
679 static void symtab_hash_eval(struct symtab *s)
683 for (i = 0; i < SYM_NUM; i++)
684 hash_eval(s[i].table, symtab_name[i]);
688 static inline void hash_eval(struct hashtab *h, char *hash_name)
694 * Define the other val_to_name and val_to_struct arrays
695 * in a policy database structure.
697 * Caller must clean up on failure.
699 static int policydb_index(struct policydb *p)
704 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
705 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
706 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
708 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
709 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
712 pr_debug("SELinux: %d classes, %d rules\n",
713 p->p_classes.nprim, p->te_avtab.nel);
716 avtab_hash_eval(&p->te_avtab, "rules");
717 symtab_hash_eval(p->symtab);
720 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
721 sizeof(*p->class_val_to_struct),
723 if (!p->class_val_to_struct)
726 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
727 sizeof(*p->role_val_to_struct),
729 if (!p->role_val_to_struct)
732 p->user_val_to_struct = kcalloc(p->p_users.nprim,
733 sizeof(*p->user_val_to_struct),
735 if (!p->user_val_to_struct)
738 p->type_val_to_struct = kvcalloc(p->p_types.nprim,
739 sizeof(*p->type_val_to_struct),
741 if (!p->type_val_to_struct)
744 rc = cond_init_bool_indexes(p);
748 for (i = 0; i < SYM_NUM; i++) {
749 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
752 if (!p->sym_val_to_name[i])
755 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
765 * Free any memory allocated by a policy database structure.
767 void policydb_destroy(struct policydb *p)
769 struct ocontext *c, *ctmp;
770 struct genfs *g, *gtmp;
772 struct role_allow *ra, *lra = NULL;
773 struct role_trans *tr, *ltr = NULL;
775 for (i = 0; i < SYM_NUM; i++) {
777 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
778 hashtab_destroy(p->symtab[i].table);
781 for (i = 0; i < SYM_NUM; i++)
782 kvfree(p->sym_val_to_name[i]);
784 kfree(p->class_val_to_struct);
785 kfree(p->role_val_to_struct);
786 kfree(p->user_val_to_struct);
787 kvfree(p->type_val_to_struct);
789 avtab_destroy(&p->te_avtab);
791 for (i = 0; i < OCON_NUM; i++) {
797 ocontext_destroy(ctmp, i);
799 p->ocontexts[i] = NULL;
810 ocontext_destroy(ctmp, OCON_FSUSE);
818 cond_policydb_destroy(p);
820 for (tr = p->role_tr; tr; tr = tr->next) {
827 for (ra = p->role_allow; ra; ra = ra->next) {
834 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
835 hashtab_destroy(p->filename_trans);
837 hashtab_map(p->range_tr, range_tr_destroy, NULL);
838 hashtab_destroy(p->range_tr);
840 if (p->type_attr_map_array) {
841 for (i = 0; i < p->p_types.nprim; i++)
842 ebitmap_destroy(&p->type_attr_map_array[i]);
843 kvfree(p->type_attr_map_array);
846 ebitmap_destroy(&p->filename_trans_ttypes);
847 ebitmap_destroy(&p->policycaps);
848 ebitmap_destroy(&p->permissive_map);
852 * Load the initial SIDs specified in a policy database
853 * structure into a SID table.
855 int policydb_load_isids(struct policydb *p, struct sidtab *s)
857 struct ocontext *head, *c;
862 pr_err("SELinux: out of memory on SID table init\n");
866 head = p->ocontexts[OCON_ISID];
867 for (c = head; c; c = c->next) {
869 if (!c->context[0].user) {
870 pr_err("SELinux: SID %s was never defined.\n",
875 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
876 pr_err("SELinux: Initial SID %s out of range.\n",
881 rc = context_add_hash(p, &c->context[0]);
887 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
889 pr_err("SELinux: unable to load initial SID %s.\n",
900 int policydb_class_isvalid(struct policydb *p, unsigned int class)
902 if (!class || class > p->p_classes.nprim)
907 int policydb_role_isvalid(struct policydb *p, unsigned int role)
909 if (!role || role > p->p_roles.nprim)
914 int policydb_type_isvalid(struct policydb *p, unsigned int type)
916 if (!type || type > p->p_types.nprim)
922 * Return 1 if the fields in the security context
923 * structure `c' are valid. Return 0 otherwise.
925 int policydb_context_isvalid(struct policydb *p, struct context *c)
927 struct role_datum *role;
928 struct user_datum *usrdatum;
930 if (!c->role || c->role > p->p_roles.nprim)
933 if (!c->user || c->user > p->p_users.nprim)
936 if (!c->type || c->type > p->p_types.nprim)
939 if (c->role != OBJECT_R_VAL) {
941 * Role must be authorized for the type.
943 role = p->role_val_to_struct[c->role - 1];
944 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
945 /* role may not be associated with type */
949 * User must be authorized for the role.
951 usrdatum = p->user_val_to_struct[c->user - 1];
955 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
956 /* user may not be associated with role */
960 if (!mls_context_isvalid(p, c))
967 * Read a MLS range structure from a policydb binary
968 * representation file.
970 static int mls_read_range_helper(struct mls_range *r, void *fp)
976 rc = next_entry(buf, fp, sizeof(u32));
981 items = le32_to_cpu(buf[0]);
982 if (items > ARRAY_SIZE(buf)) {
983 pr_err("SELinux: mls: range overflow\n");
987 rc = next_entry(buf, fp, sizeof(u32) * items);
989 pr_err("SELinux: mls: truncated range\n");
993 r->level[0].sens = le32_to_cpu(buf[0]);
995 r->level[1].sens = le32_to_cpu(buf[1]);
997 r->level[1].sens = r->level[0].sens;
999 rc = ebitmap_read(&r->level[0].cat, fp);
1001 pr_err("SELinux: mls: error reading low categories\n");
1005 rc = ebitmap_read(&r->level[1].cat, fp);
1007 pr_err("SELinux: mls: error reading high categories\n");
1011 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1013 pr_err("SELinux: mls: out of memory\n");
1020 ebitmap_destroy(&r->level[0].cat);
1026 * Read and validate a security context structure
1027 * from a policydb binary representation file.
1029 static int context_read_and_validate(struct context *c,
1036 rc = next_entry(buf, fp, sizeof buf);
1038 pr_err("SELinux: context truncated\n");
1041 c->user = le32_to_cpu(buf[0]);
1042 c->role = le32_to_cpu(buf[1]);
1043 c->type = le32_to_cpu(buf[2]);
1044 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1045 rc = mls_read_range_helper(&c->range, fp);
1047 pr_err("SELinux: error reading MLS range of context\n");
1053 if (!policydb_context_isvalid(p, c)) {
1054 pr_err("SELinux: invalid security context\n");
1064 * The following *_read functions are used to
1065 * read the symbol data from a policy database
1066 * binary representation file.
1069 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1074 if ((len == 0) || (len == (u32)-1))
1077 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1081 /* it's expected the caller should free the str */
1084 rc = next_entry(str, fp, len);
1092 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1095 struct perm_datum *perdatum;
1100 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1104 rc = next_entry(buf, fp, sizeof buf);
1108 len = le32_to_cpu(buf[0]);
1109 perdatum->value = le32_to_cpu(buf[1]);
1111 rc = str_read(&key, GFP_KERNEL, fp, len);
1115 rc = hashtab_insert(h, key, perdatum);
1121 perm_destroy(key, perdatum, NULL);
1125 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1128 struct common_datum *comdatum;
1133 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1137 rc = next_entry(buf, fp, sizeof buf);
1141 len = le32_to_cpu(buf[0]);
1142 comdatum->value = le32_to_cpu(buf[1]);
1144 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1147 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1148 nel = le32_to_cpu(buf[3]);
1150 rc = str_read(&key, GFP_KERNEL, fp, len);
1154 for (i = 0; i < nel; i++) {
1155 rc = perm_read(p, comdatum->permissions.table, fp);
1160 rc = hashtab_insert(h, key, comdatum);
1165 common_destroy(key, comdatum, NULL);
1169 static void type_set_init(struct type_set *t)
1171 ebitmap_init(&t->types);
1172 ebitmap_init(&t->negset);
1175 static int type_set_read(struct type_set *t, void *fp)
1180 if (ebitmap_read(&t->types, fp))
1182 if (ebitmap_read(&t->negset, fp))
1185 rc = next_entry(buf, fp, sizeof(u32));
1188 t->flags = le32_to_cpu(buf[0]);
1194 static int read_cons_helper(struct policydb *p,
1195 struct constraint_node **nodep,
1196 int ncons, int allowxtarget, void *fp)
1198 struct constraint_node *c, *lc;
1199 struct constraint_expr *e, *le;
1202 int rc, i, j, depth;
1205 for (i = 0; i < ncons; i++) {
1206 c = kzalloc(sizeof(*c), GFP_KERNEL);
1215 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1218 c->permissions = le32_to_cpu(buf[0]);
1219 nexpr = le32_to_cpu(buf[1]);
1222 for (j = 0; j < nexpr; j++) {
1223 e = kzalloc(sizeof(*e), GFP_KERNEL);
1232 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1235 e->expr_type = le32_to_cpu(buf[0]);
1236 e->attr = le32_to_cpu(buf[1]);
1237 e->op = le32_to_cpu(buf[2]);
1239 switch (e->expr_type) {
1251 if (depth == (CEXPR_MAXDEPTH - 1))
1256 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1258 if (depth == (CEXPR_MAXDEPTH - 1))
1261 rc = ebitmap_read(&e->names, fp);
1264 if (p->policyvers >=
1265 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1266 e->type_names = kzalloc(sizeof
1271 type_set_init(e->type_names);
1272 rc = type_set_read(e->type_names, fp);
1290 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1293 struct class_datum *cladatum;
1295 u32 len, len2, ncons, nel;
1298 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1302 rc = next_entry(buf, fp, sizeof(u32)*6);
1306 len = le32_to_cpu(buf[0]);
1307 len2 = le32_to_cpu(buf[1]);
1308 cladatum->value = le32_to_cpu(buf[2]);
1310 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1313 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1314 nel = le32_to_cpu(buf[4]);
1316 ncons = le32_to_cpu(buf[5]);
1318 rc = str_read(&key, GFP_KERNEL, fp, len);
1323 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1328 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1329 if (!cladatum->comdatum) {
1330 pr_err("SELinux: unknown common %s\n",
1335 for (i = 0; i < nel; i++) {
1336 rc = perm_read(p, cladatum->permissions.table, fp);
1341 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1345 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1346 /* grab the validatetrans rules */
1347 rc = next_entry(buf, fp, sizeof(u32));
1350 ncons = le32_to_cpu(buf[0]);
1351 rc = read_cons_helper(p, &cladatum->validatetrans,
1357 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1358 rc = next_entry(buf, fp, sizeof(u32) * 3);
1362 cladatum->default_user = le32_to_cpu(buf[0]);
1363 cladatum->default_role = le32_to_cpu(buf[1]);
1364 cladatum->default_range = le32_to_cpu(buf[2]);
1367 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1368 rc = next_entry(buf, fp, sizeof(u32) * 1);
1371 cladatum->default_type = le32_to_cpu(buf[0]);
1374 rc = hashtab_insert(h, key, cladatum);
1380 cls_destroy(key, cladatum, NULL);
1384 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1387 struct role_datum *role;
1388 int rc, to_read = 2;
1392 role = kzalloc(sizeof(*role), GFP_KERNEL);
1396 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1399 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1403 len = le32_to_cpu(buf[0]);
1404 role->value = le32_to_cpu(buf[1]);
1405 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1406 role->bounds = le32_to_cpu(buf[2]);
1408 rc = str_read(&key, GFP_KERNEL, fp, len);
1412 rc = ebitmap_read(&role->dominates, fp);
1416 rc = ebitmap_read(&role->types, fp);
1420 if (strcmp(key, OBJECT_R) == 0) {
1422 if (role->value != OBJECT_R_VAL) {
1423 pr_err("SELinux: Role %s has wrong value %d\n",
1424 OBJECT_R, role->value);
1431 rc = hashtab_insert(h, key, role);
1436 role_destroy(key, role, NULL);
1440 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1443 struct type_datum *typdatum;
1444 int rc, to_read = 3;
1448 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1452 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1455 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1459 len = le32_to_cpu(buf[0]);
1460 typdatum->value = le32_to_cpu(buf[1]);
1461 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1462 u32 prop = le32_to_cpu(buf[2]);
1464 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1465 typdatum->primary = 1;
1466 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1467 typdatum->attribute = 1;
1469 typdatum->bounds = le32_to_cpu(buf[3]);
1471 typdatum->primary = le32_to_cpu(buf[2]);
1474 rc = str_read(&key, GFP_KERNEL, fp, len);
1478 rc = hashtab_insert(h, key, typdatum);
1483 type_destroy(key, typdatum, NULL);
1489 * Read a MLS level structure from a policydb binary
1490 * representation file.
1492 static int mls_read_level(struct mls_level *lp, void *fp)
1497 memset(lp, 0, sizeof(*lp));
1499 rc = next_entry(buf, fp, sizeof buf);
1501 pr_err("SELinux: mls: truncated level\n");
1504 lp->sens = le32_to_cpu(buf[0]);
1506 rc = ebitmap_read(&lp->cat, fp);
1508 pr_err("SELinux: mls: error reading level categories\n");
1514 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1517 struct user_datum *usrdatum;
1518 int rc, to_read = 2;
1522 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1526 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1529 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1533 len = le32_to_cpu(buf[0]);
1534 usrdatum->value = le32_to_cpu(buf[1]);
1535 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1536 usrdatum->bounds = le32_to_cpu(buf[2]);
1538 rc = str_read(&key, GFP_KERNEL, fp, len);
1542 rc = ebitmap_read(&usrdatum->roles, fp);
1546 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1547 rc = mls_read_range_helper(&usrdatum->range, fp);
1550 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1555 rc = hashtab_insert(h, key, usrdatum);
1560 user_destroy(key, usrdatum, NULL);
1564 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1567 struct level_datum *levdatum;
1572 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1576 rc = next_entry(buf, fp, sizeof buf);
1580 len = le32_to_cpu(buf[0]);
1581 levdatum->isalias = le32_to_cpu(buf[1]);
1583 rc = str_read(&key, GFP_ATOMIC, fp, len);
1588 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1589 if (!levdatum->level)
1592 rc = mls_read_level(levdatum->level, fp);
1596 rc = hashtab_insert(h, key, levdatum);
1601 sens_destroy(key, levdatum, NULL);
1605 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1608 struct cat_datum *catdatum;
1613 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1617 rc = next_entry(buf, fp, sizeof buf);
1621 len = le32_to_cpu(buf[0]);
1622 catdatum->value = le32_to_cpu(buf[1]);
1623 catdatum->isalias = le32_to_cpu(buf[2]);
1625 rc = str_read(&key, GFP_ATOMIC, fp, len);
1629 rc = hashtab_insert(h, key, catdatum);
1634 cat_destroy(key, catdatum, NULL);
1638 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1650 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1652 struct user_datum *upper, *user;
1653 struct policydb *p = datap;
1656 upper = user = datum;
1657 while (upper->bounds) {
1658 struct ebitmap_node *node;
1661 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1662 pr_err("SELinux: user %s: "
1663 "too deep or looped boundary",
1668 upper = p->user_val_to_struct[upper->bounds - 1];
1669 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1670 if (ebitmap_get_bit(&upper->roles, bit))
1673 pr_err("SELinux: boundary violated policy: "
1674 "user=%s role=%s bounds=%s\n",
1675 sym_name(p, SYM_USERS, user->value - 1),
1676 sym_name(p, SYM_ROLES, bit),
1677 sym_name(p, SYM_USERS, upper->value - 1));
1686 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1688 struct role_datum *upper, *role;
1689 struct policydb *p = datap;
1692 upper = role = datum;
1693 while (upper->bounds) {
1694 struct ebitmap_node *node;
1697 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1698 pr_err("SELinux: role %s: "
1699 "too deep or looped bounds\n",
1704 upper = p->role_val_to_struct[upper->bounds - 1];
1705 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1706 if (ebitmap_get_bit(&upper->types, bit))
1709 pr_err("SELinux: boundary violated policy: "
1710 "role=%s type=%s bounds=%s\n",
1711 sym_name(p, SYM_ROLES, role->value - 1),
1712 sym_name(p, SYM_TYPES, bit),
1713 sym_name(p, SYM_ROLES, upper->value - 1));
1722 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1724 struct type_datum *upper;
1725 struct policydb *p = datap;
1729 while (upper->bounds) {
1730 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1731 pr_err("SELinux: type %s: "
1732 "too deep or looped boundary\n",
1737 upper = p->type_val_to_struct[upper->bounds - 1];
1740 if (upper->attribute) {
1741 pr_err("SELinux: type %s: "
1742 "bounded by attribute %s",
1744 sym_name(p, SYM_TYPES, upper->value - 1));
1752 static int policydb_bounds_sanity_check(struct policydb *p)
1756 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1759 rc = hashtab_map(p->p_users.table,
1760 user_bounds_sanity_check, p);
1764 rc = hashtab_map(p->p_roles.table,
1765 role_bounds_sanity_check, p);
1769 rc = hashtab_map(p->p_types.table,
1770 type_bounds_sanity_check, p);
1777 u16 string_to_security_class(struct policydb *p, const char *name)
1779 struct class_datum *cladatum;
1781 cladatum = hashtab_search(p->p_classes.table, name);
1785 return cladatum->value;
1788 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1790 struct class_datum *cladatum;
1791 struct perm_datum *perdatum = NULL;
1792 struct common_datum *comdatum;
1794 if (!tclass || tclass > p->p_classes.nprim)
1797 cladatum = p->class_val_to_struct[tclass-1];
1798 comdatum = cladatum->comdatum;
1800 perdatum = hashtab_search(comdatum->permissions.table,
1803 perdatum = hashtab_search(cladatum->permissions.table,
1808 return 1U << (perdatum->value-1);
1811 static int range_read(struct policydb *p, void *fp)
1813 struct range_trans *rt = NULL;
1814 struct mls_range *r = NULL;
1819 if (p->policyvers < POLICYDB_VERSION_MLS)
1822 rc = next_entry(buf, fp, sizeof(u32));
1826 nel = le32_to_cpu(buf[0]);
1827 for (i = 0; i < nel; i++) {
1829 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1833 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1837 rt->source_type = le32_to_cpu(buf[0]);
1838 rt->target_type = le32_to_cpu(buf[1]);
1839 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1840 rc = next_entry(buf, fp, sizeof(u32));
1843 rt->target_class = le32_to_cpu(buf[0]);
1845 rt->target_class = p->process_class;
1848 if (!policydb_type_isvalid(p, rt->source_type) ||
1849 !policydb_type_isvalid(p, rt->target_type) ||
1850 !policydb_class_isvalid(p, rt->target_class))
1854 r = kzalloc(sizeof(*r), GFP_KERNEL);
1858 rc = mls_read_range_helper(r, fp);
1863 if (!mls_range_isvalid(p, r)) {
1864 pr_warn("SELinux: rangetrans: invalid range\n");
1868 rc = hashtab_insert(p->range_tr, rt, r);
1875 hash_eval(p->range_tr, "rangetr");
1883 static int filename_trans_read(struct policydb *p, void *fp)
1885 struct filename_trans *ft;
1886 struct filename_trans_datum *otype;
1892 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1895 rc = next_entry(buf, fp, sizeof(u32));
1898 nel = le32_to_cpu(buf[0]);
1900 for (i = 0; i < nel; i++) {
1905 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1910 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1914 /* length of the path component string */
1915 rc = next_entry(buf, fp, sizeof(u32));
1918 len = le32_to_cpu(buf[0]);
1920 /* path component string */
1921 rc = str_read(&name, GFP_KERNEL, fp, len);
1927 rc = next_entry(buf, fp, sizeof(u32) * 4);
1931 ft->stype = le32_to_cpu(buf[0]);
1932 ft->ttype = le32_to_cpu(buf[1]);
1933 ft->tclass = le32_to_cpu(buf[2]);
1935 otype->otype = le32_to_cpu(buf[3]);
1937 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1941 rc = hashtab_insert(p->filename_trans, ft, otype);
1944 * Do not return -EEXIST to the caller, or the system
1949 /* But free memory to avoid memory leak. */
1955 hash_eval(p->filename_trans, "filenametr");
1965 static int genfs_read(struct policydb *p, void *fp)
1968 u32 nel, nel2, len, len2;
1970 struct ocontext *l, *c;
1971 struct ocontext *newc = NULL;
1972 struct genfs *genfs_p, *genfs;
1973 struct genfs *newgenfs = NULL;
1975 rc = next_entry(buf, fp, sizeof(u32));
1978 nel = le32_to_cpu(buf[0]);
1980 for (i = 0; i < nel; i++) {
1981 rc = next_entry(buf, fp, sizeof(u32));
1984 len = le32_to_cpu(buf[0]);
1987 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1991 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
1995 for (genfs_p = NULL, genfs = p->genfs; genfs;
1996 genfs_p = genfs, genfs = genfs->next) {
1998 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1999 pr_err("SELinux: dup genfs fstype %s\n",
2003 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2006 newgenfs->next = genfs;
2008 genfs_p->next = newgenfs;
2010 p->genfs = newgenfs;
2014 rc = next_entry(buf, fp, sizeof(u32));
2018 nel2 = le32_to_cpu(buf[0]);
2019 for (j = 0; j < nel2; j++) {
2020 rc = next_entry(buf, fp, sizeof(u32));
2023 len = le32_to_cpu(buf[0]);
2026 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2030 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2034 rc = next_entry(buf, fp, sizeof(u32));
2038 newc->v.sclass = le32_to_cpu(buf[0]);
2039 rc = context_read_and_validate(&newc->context[0], p, fp);
2043 for (l = NULL, c = genfs->head; c;
2044 l = c, c = c->next) {
2046 if (!strcmp(newc->u.name, c->u.name) &&
2047 (!c->v.sclass || !newc->v.sclass ||
2048 newc->v.sclass == c->v.sclass)) {
2049 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2050 genfs->fstype, c->u.name);
2053 len = strlen(newc->u.name);
2054 len2 = strlen(c->u.name);
2070 kfree(newgenfs->fstype);
2073 ocontext_destroy(newc, OCON_FSUSE);
2078 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2083 __be64 prefixbuf[1];
2085 struct ocontext *l, *c;
2088 for (i = 0; i < info->ocon_num; i++) {
2089 rc = next_entry(buf, fp, sizeof(u32));
2092 nel = le32_to_cpu(buf[0]);
2095 for (j = 0; j < nel; j++) {
2097 c = kzalloc(sizeof(*c), GFP_KERNEL);
2103 p->ocontexts[i] = c;
2108 rc = next_entry(buf, fp, sizeof(u32));
2112 c->sid[0] = le32_to_cpu(buf[0]);
2113 rc = context_read_and_validate(&c->context[0], p, fp);
2119 rc = next_entry(buf, fp, sizeof(u32));
2122 len = le32_to_cpu(buf[0]);
2124 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2128 rc = context_read_and_validate(&c->context[0], p, fp);
2131 rc = context_read_and_validate(&c->context[1], p, fp);
2136 rc = next_entry(buf, fp, sizeof(u32)*3);
2139 c->u.port.protocol = le32_to_cpu(buf[0]);
2140 c->u.port.low_port = le32_to_cpu(buf[1]);
2141 c->u.port.high_port = le32_to_cpu(buf[2]);
2142 rc = context_read_and_validate(&c->context[0], p, fp);
2147 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2150 c->u.node.addr = nodebuf[0]; /* network order */
2151 c->u.node.mask = nodebuf[1]; /* network order */
2152 rc = context_read_and_validate(&c->context[0], p, fp);
2157 rc = next_entry(buf, fp, sizeof(u32)*2);
2162 c->v.behavior = le32_to_cpu(buf[0]);
2163 /* Determined at runtime, not in policy DB. */
2164 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2166 if (c->v.behavior > SECURITY_FS_USE_MAX)
2169 len = le32_to_cpu(buf[1]);
2170 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2174 rc = context_read_and_validate(&c->context[0], p, fp);
2181 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2184 for (k = 0; k < 4; k++)
2185 c->u.node6.addr[k] = nodebuf[k];
2186 for (k = 0; k < 4; k++)
2187 c->u.node6.mask[k] = nodebuf[k+4];
2188 rc = context_read_and_validate(&c->context[0], p, fp);
2194 u32 pkey_lo, pkey_hi;
2196 rc = next_entry(prefixbuf, fp, sizeof(u64));
2200 /* we need to have subnet_prefix in CPU order */
2201 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2203 rc = next_entry(buf, fp, sizeof(u32) * 2);
2207 pkey_lo = le32_to_cpu(buf[0]);
2208 pkey_hi = le32_to_cpu(buf[1]);
2210 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2215 c->u.ibpkey.low_pkey = pkey_lo;
2216 c->u.ibpkey.high_pkey = pkey_hi;
2218 rc = context_read_and_validate(&c->context[0],
2225 case OCON_IBENDPORT: {
2228 rc = next_entry(buf, fp, sizeof(u32) * 2);
2231 len = le32_to_cpu(buf[0]);
2233 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2237 port = le32_to_cpu(buf[1]);
2238 if (port > U8_MAX || port == 0) {
2243 c->u.ibendport.port = port;
2245 rc = context_read_and_validate(&c->context[0],
2261 * Read the configuration data from a policy database binary
2262 * representation file into a policy database structure.
2264 int policydb_read(struct policydb *p, void *fp)
2266 struct role_allow *ra, *lra;
2267 struct role_trans *tr, *ltr;
2270 u32 len, nprim, nel;
2273 struct policydb_compat_info *info;
2275 rc = policydb_init(p);
2279 /* Read the magic number and string length. */
2280 rc = next_entry(buf, fp, sizeof(u32) * 2);
2285 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2286 pr_err("SELinux: policydb magic number 0x%x does "
2287 "not match expected magic number 0x%x\n",
2288 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2293 len = le32_to_cpu(buf[1]);
2294 if (len != strlen(POLICYDB_STRING)) {
2295 pr_err("SELinux: policydb string length %d does not "
2296 "match expected length %zu\n",
2297 len, strlen(POLICYDB_STRING));
2302 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2303 if (!policydb_str) {
2304 pr_err("SELinux: unable to allocate memory for policydb "
2305 "string of length %d\n", len);
2309 rc = next_entry(policydb_str, fp, len);
2311 pr_err("SELinux: truncated policydb string identifier\n");
2312 kfree(policydb_str);
2317 policydb_str[len] = '\0';
2318 if (strcmp(policydb_str, POLICYDB_STRING)) {
2319 pr_err("SELinux: policydb string %s does not match "
2320 "my string %s\n", policydb_str, POLICYDB_STRING);
2321 kfree(policydb_str);
2324 /* Done with policydb_str. */
2325 kfree(policydb_str);
2326 policydb_str = NULL;
2328 /* Read the version and table sizes. */
2329 rc = next_entry(buf, fp, sizeof(u32)*4);
2334 p->policyvers = le32_to_cpu(buf[0]);
2335 if (p->policyvers < POLICYDB_VERSION_MIN ||
2336 p->policyvers > POLICYDB_VERSION_MAX) {
2337 pr_err("SELinux: policydb version %d does not match "
2338 "my version range %d-%d\n",
2339 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2343 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2347 if (p->policyvers < POLICYDB_VERSION_MLS) {
2348 pr_err("SELinux: security policydb version %d "
2349 "(MLS) not backwards compatible\n",
2354 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2355 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2357 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2358 rc = ebitmap_read(&p->policycaps, fp);
2363 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2364 rc = ebitmap_read(&p->permissive_map, fp);
2370 info = policydb_lookup_compat(p->policyvers);
2372 pr_err("SELinux: unable to find policy compat info "
2373 "for version %d\n", p->policyvers);
2378 if (le32_to_cpu(buf[2]) != info->sym_num ||
2379 le32_to_cpu(buf[3]) != info->ocon_num) {
2380 pr_err("SELinux: policydb table sizes (%d,%d) do "
2381 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2382 le32_to_cpu(buf[3]),
2383 info->sym_num, info->ocon_num);
2387 for (i = 0; i < info->sym_num; i++) {
2388 rc = next_entry(buf, fp, sizeof(u32)*2);
2391 nprim = le32_to_cpu(buf[0]);
2392 nel = le32_to_cpu(buf[1]);
2393 for (j = 0; j < nel; j++) {
2394 rc = read_f[i](p, p->symtab[i].table, fp);
2399 p->symtab[i].nprim = nprim;
2403 p->process_class = string_to_security_class(p, "process");
2404 if (!p->process_class)
2407 rc = avtab_read(&p->te_avtab, fp, p);
2411 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2412 rc = cond_read_list(p, fp);
2417 rc = next_entry(buf, fp, sizeof(u32));
2420 nel = le32_to_cpu(buf[0]);
2422 for (i = 0; i < nel; i++) {
2424 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2431 rc = next_entry(buf, fp, sizeof(u32)*3);
2436 tr->role = le32_to_cpu(buf[0]);
2437 tr->type = le32_to_cpu(buf[1]);
2438 tr->new_role = le32_to_cpu(buf[2]);
2439 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2440 rc = next_entry(buf, fp, sizeof(u32));
2443 tr->tclass = le32_to_cpu(buf[0]);
2445 tr->tclass = p->process_class;
2448 if (!policydb_role_isvalid(p, tr->role) ||
2449 !policydb_type_isvalid(p, tr->type) ||
2450 !policydb_class_isvalid(p, tr->tclass) ||
2451 !policydb_role_isvalid(p, tr->new_role))
2456 rc = next_entry(buf, fp, sizeof(u32));
2459 nel = le32_to_cpu(buf[0]);
2461 for (i = 0; i < nel; i++) {
2463 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2470 rc = next_entry(buf, fp, sizeof(u32)*2);
2475 ra->role = le32_to_cpu(buf[0]);
2476 ra->new_role = le32_to_cpu(buf[1]);
2477 if (!policydb_role_isvalid(p, ra->role) ||
2478 !policydb_role_isvalid(p, ra->new_role))
2483 rc = filename_trans_read(p, fp);
2487 rc = policydb_index(p);
2492 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2493 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2494 if (!p->process_trans_perms)
2497 rc = ocontext_read(p, info, fp);
2501 rc = genfs_read(p, fp);
2505 rc = range_read(p, fp);
2509 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2510 sizeof(*p->type_attr_map_array),
2512 if (!p->type_attr_map_array)
2515 /* just in case ebitmap_init() becomes more than just a memset(0): */
2516 for (i = 0; i < p->p_types.nprim; i++)
2517 ebitmap_init(&p->type_attr_map_array[i]);
2519 for (i = 0; i < p->p_types.nprim; i++) {
2520 struct ebitmap *e = &p->type_attr_map_array[i];
2522 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2523 rc = ebitmap_read(e, fp);
2527 /* add the type itself as the degenerate case */
2528 rc = ebitmap_set_bit(e, i, 1);
2533 rc = policydb_bounds_sanity_check(p);
2541 policydb_destroy(p);
2546 * Write a MLS level structure to a policydb binary
2547 * representation file.
2549 static int mls_write_level(struct mls_level *l, void *fp)
2554 buf[0] = cpu_to_le32(l->sens);
2555 rc = put_entry(buf, sizeof(u32), 1, fp);
2559 rc = ebitmap_write(&l->cat, fp);
2567 * Write a MLS range structure to a policydb binary
2568 * representation file.
2570 static int mls_write_range_helper(struct mls_range *r, void *fp)
2576 eq = mls_level_eq(&r->level[1], &r->level[0]);
2582 buf[0] = cpu_to_le32(items-1);
2583 buf[1] = cpu_to_le32(r->level[0].sens);
2585 buf[2] = cpu_to_le32(r->level[1].sens);
2587 BUG_ON(items > ARRAY_SIZE(buf));
2589 rc = put_entry(buf, sizeof(u32), items, fp);
2593 rc = ebitmap_write(&r->level[0].cat, fp);
2597 rc = ebitmap_write(&r->level[1].cat, fp);
2605 static int sens_write(void *vkey, void *datum, void *ptr)
2608 struct level_datum *levdatum = datum;
2609 struct policy_data *pd = ptr;
2616 buf[0] = cpu_to_le32(len);
2617 buf[1] = cpu_to_le32(levdatum->isalias);
2618 rc = put_entry(buf, sizeof(u32), 2, fp);
2622 rc = put_entry(key, 1, len, fp);
2626 rc = mls_write_level(levdatum->level, fp);
2633 static int cat_write(void *vkey, void *datum, void *ptr)
2636 struct cat_datum *catdatum = datum;
2637 struct policy_data *pd = ptr;
2644 buf[0] = cpu_to_le32(len);
2645 buf[1] = cpu_to_le32(catdatum->value);
2646 buf[2] = cpu_to_le32(catdatum->isalias);
2647 rc = put_entry(buf, sizeof(u32), 3, fp);
2651 rc = put_entry(key, 1, len, fp);
2658 static int role_trans_write(struct policydb *p, void *fp)
2660 struct role_trans *r = p->role_tr;
2661 struct role_trans *tr;
2667 for (tr = r; tr; tr = tr->next)
2669 buf[0] = cpu_to_le32(nel);
2670 rc = put_entry(buf, sizeof(u32), 1, fp);
2673 for (tr = r; tr; tr = tr->next) {
2674 buf[0] = cpu_to_le32(tr->role);
2675 buf[1] = cpu_to_le32(tr->type);
2676 buf[2] = cpu_to_le32(tr->new_role);
2677 rc = put_entry(buf, sizeof(u32), 3, fp);
2680 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2681 buf[0] = cpu_to_le32(tr->tclass);
2682 rc = put_entry(buf, sizeof(u32), 1, fp);
2691 static int role_allow_write(struct role_allow *r, void *fp)
2693 struct role_allow *ra;
2699 for (ra = r; ra; ra = ra->next)
2701 buf[0] = cpu_to_le32(nel);
2702 rc = put_entry(buf, sizeof(u32), 1, fp);
2705 for (ra = r; ra; ra = ra->next) {
2706 buf[0] = cpu_to_le32(ra->role);
2707 buf[1] = cpu_to_le32(ra->new_role);
2708 rc = put_entry(buf, sizeof(u32), 2, fp);
2716 * Write a security context structure
2717 * to a policydb binary representation file.
2719 static int context_write(struct policydb *p, struct context *c,
2725 buf[0] = cpu_to_le32(c->user);
2726 buf[1] = cpu_to_le32(c->role);
2727 buf[2] = cpu_to_le32(c->type);
2729 rc = put_entry(buf, sizeof(u32), 3, fp);
2733 rc = mls_write_range_helper(&c->range, fp);
2741 * The following *_write functions are used to
2742 * write the symbol data to a policy database
2743 * binary representation file.
2746 static int perm_write(void *vkey, void *datum, void *fp)
2749 struct perm_datum *perdatum = datum;
2755 buf[0] = cpu_to_le32(len);
2756 buf[1] = cpu_to_le32(perdatum->value);
2757 rc = put_entry(buf, sizeof(u32), 2, fp);
2761 rc = put_entry(key, 1, len, fp);
2768 static int common_write(void *vkey, void *datum, void *ptr)
2771 struct common_datum *comdatum = datum;
2772 struct policy_data *pd = ptr;
2779 buf[0] = cpu_to_le32(len);
2780 buf[1] = cpu_to_le32(comdatum->value);
2781 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2782 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2783 rc = put_entry(buf, sizeof(u32), 4, fp);
2787 rc = put_entry(key, 1, len, fp);
2791 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2798 static int type_set_write(struct type_set *t, void *fp)
2803 if (ebitmap_write(&t->types, fp))
2805 if (ebitmap_write(&t->negset, fp))
2808 buf[0] = cpu_to_le32(t->flags);
2809 rc = put_entry(buf, sizeof(u32), 1, fp);
2816 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2819 struct constraint_node *c;
2820 struct constraint_expr *e;
2825 for (c = node; c; c = c->next) {
2827 for (e = c->expr; e; e = e->next)
2829 buf[0] = cpu_to_le32(c->permissions);
2830 buf[1] = cpu_to_le32(nel);
2831 rc = put_entry(buf, sizeof(u32), 2, fp);
2834 for (e = c->expr; e; e = e->next) {
2835 buf[0] = cpu_to_le32(e->expr_type);
2836 buf[1] = cpu_to_le32(e->attr);
2837 buf[2] = cpu_to_le32(e->op);
2838 rc = put_entry(buf, sizeof(u32), 3, fp);
2842 switch (e->expr_type) {
2844 rc = ebitmap_write(&e->names, fp);
2847 if (p->policyvers >=
2848 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2849 rc = type_set_write(e->type_names, fp);
2863 static int class_write(void *vkey, void *datum, void *ptr)
2866 struct class_datum *cladatum = datum;
2867 struct policy_data *pd = ptr;
2869 struct policydb *p = pd->p;
2870 struct constraint_node *c;
2877 if (cladatum->comkey)
2878 len2 = strlen(cladatum->comkey);
2883 for (c = cladatum->constraints; c; c = c->next)
2886 buf[0] = cpu_to_le32(len);
2887 buf[1] = cpu_to_le32(len2);
2888 buf[2] = cpu_to_le32(cladatum->value);
2889 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2890 if (cladatum->permissions.table)
2891 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2894 buf[5] = cpu_to_le32(ncons);
2895 rc = put_entry(buf, sizeof(u32), 6, fp);
2899 rc = put_entry(key, 1, len, fp);
2903 if (cladatum->comkey) {
2904 rc = put_entry(cladatum->comkey, 1, len2, fp);
2909 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2913 rc = write_cons_helper(p, cladatum->constraints, fp);
2917 /* write out the validatetrans rule */
2919 for (c = cladatum->validatetrans; c; c = c->next)
2922 buf[0] = cpu_to_le32(ncons);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2927 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2931 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2932 buf[0] = cpu_to_le32(cladatum->default_user);
2933 buf[1] = cpu_to_le32(cladatum->default_role);
2934 buf[2] = cpu_to_le32(cladatum->default_range);
2936 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2941 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2942 buf[0] = cpu_to_le32(cladatum->default_type);
2943 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2951 static int role_write(void *vkey, void *datum, void *ptr)
2954 struct role_datum *role = datum;
2955 struct policy_data *pd = ptr;
2957 struct policydb *p = pd->p;
2964 buf[items++] = cpu_to_le32(len);
2965 buf[items++] = cpu_to_le32(role->value);
2966 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2967 buf[items++] = cpu_to_le32(role->bounds);
2969 BUG_ON(items > ARRAY_SIZE(buf));
2971 rc = put_entry(buf, sizeof(u32), items, fp);
2975 rc = put_entry(key, 1, len, fp);
2979 rc = ebitmap_write(&role->dominates, fp);
2983 rc = ebitmap_write(&role->types, fp);
2990 static int type_write(void *vkey, void *datum, void *ptr)
2993 struct type_datum *typdatum = datum;
2994 struct policy_data *pd = ptr;
2995 struct policydb *p = pd->p;
3003 buf[items++] = cpu_to_le32(len);
3004 buf[items++] = cpu_to_le32(typdatum->value);
3005 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3008 if (typdatum->primary)
3009 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3011 if (typdatum->attribute)
3012 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3014 buf[items++] = cpu_to_le32(properties);
3015 buf[items++] = cpu_to_le32(typdatum->bounds);
3017 buf[items++] = cpu_to_le32(typdatum->primary);
3019 BUG_ON(items > ARRAY_SIZE(buf));
3020 rc = put_entry(buf, sizeof(u32), items, fp);
3024 rc = put_entry(key, 1, len, fp);
3031 static int user_write(void *vkey, void *datum, void *ptr)
3034 struct user_datum *usrdatum = datum;
3035 struct policy_data *pd = ptr;
3036 struct policydb *p = pd->p;
3044 buf[items++] = cpu_to_le32(len);
3045 buf[items++] = cpu_to_le32(usrdatum->value);
3046 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3047 buf[items++] = cpu_to_le32(usrdatum->bounds);
3048 BUG_ON(items > ARRAY_SIZE(buf));
3049 rc = put_entry(buf, sizeof(u32), items, fp);
3053 rc = put_entry(key, 1, len, fp);
3057 rc = ebitmap_write(&usrdatum->roles, fp);
3061 rc = mls_write_range_helper(&usrdatum->range, fp);
3065 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3072 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3085 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3088 unsigned int i, j, rc;
3090 __be64 prefixbuf[1];
3094 for (i = 0; i < info->ocon_num; i++) {
3096 for (c = p->ocontexts[i]; c; c = c->next)
3098 buf[0] = cpu_to_le32(nel);
3099 rc = put_entry(buf, sizeof(u32), 1, fp);
3102 for (c = p->ocontexts[i]; c; c = c->next) {
3105 buf[0] = cpu_to_le32(c->sid[0]);
3106 rc = put_entry(buf, sizeof(u32), 1, fp);
3109 rc = context_write(p, &c->context[0], fp);
3115 len = strlen(c->u.name);
3116 buf[0] = cpu_to_le32(len);
3117 rc = put_entry(buf, sizeof(u32), 1, fp);
3120 rc = put_entry(c->u.name, 1, len, fp);
3123 rc = context_write(p, &c->context[0], fp);
3126 rc = context_write(p, &c->context[1], fp);
3131 buf[0] = cpu_to_le32(c->u.port.protocol);
3132 buf[1] = cpu_to_le32(c->u.port.low_port);
3133 buf[2] = cpu_to_le32(c->u.port.high_port);
3134 rc = put_entry(buf, sizeof(u32), 3, fp);
3137 rc = context_write(p, &c->context[0], fp);
3142 nodebuf[0] = c->u.node.addr; /* network order */
3143 nodebuf[1] = c->u.node.mask; /* network order */
3144 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3147 rc = context_write(p, &c->context[0], fp);
3152 buf[0] = cpu_to_le32(c->v.behavior);
3153 len = strlen(c->u.name);
3154 buf[1] = cpu_to_le32(len);
3155 rc = put_entry(buf, sizeof(u32), 2, fp);
3158 rc = put_entry(c->u.name, 1, len, fp);
3161 rc = context_write(p, &c->context[0], fp);
3166 for (j = 0; j < 4; j++)
3167 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3168 for (j = 0; j < 4; j++)
3169 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3170 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3173 rc = context_write(p, &c->context[0], fp);
3178 /* subnet_prefix is in CPU order */
3179 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3181 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3185 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3186 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3188 rc = put_entry(buf, sizeof(u32), 2, fp);
3191 rc = context_write(p, &c->context[0], fp);
3195 case OCON_IBENDPORT:
3196 len = strlen(c->u.ibendport.dev_name);
3197 buf[0] = cpu_to_le32(len);
3198 buf[1] = cpu_to_le32(c->u.ibendport.port);
3199 rc = put_entry(buf, sizeof(u32), 2, fp);
3202 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3205 rc = context_write(p, &c->context[0], fp);
3215 static int genfs_write(struct policydb *p, void *fp)
3217 struct genfs *genfs;
3224 for (genfs = p->genfs; genfs; genfs = genfs->next)
3226 buf[0] = cpu_to_le32(len);
3227 rc = put_entry(buf, sizeof(u32), 1, fp);
3230 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3231 len = strlen(genfs->fstype);
3232 buf[0] = cpu_to_le32(len);
3233 rc = put_entry(buf, sizeof(u32), 1, fp);
3236 rc = put_entry(genfs->fstype, 1, len, fp);
3240 for (c = genfs->head; c; c = c->next)
3242 buf[0] = cpu_to_le32(len);
3243 rc = put_entry(buf, sizeof(u32), 1, fp);
3246 for (c = genfs->head; c; c = c->next) {
3247 len = strlen(c->u.name);
3248 buf[0] = cpu_to_le32(len);
3249 rc = put_entry(buf, sizeof(u32), 1, fp);
3252 rc = put_entry(c->u.name, 1, len, fp);
3255 buf[0] = cpu_to_le32(c->v.sclass);
3256 rc = put_entry(buf, sizeof(u32), 1, fp);
3259 rc = context_write(p, &c->context[0], fp);
3267 static int hashtab_cnt(void *key, void *data, void *ptr)
3275 static int range_write_helper(void *key, void *data, void *ptr)
3278 struct range_trans *rt = key;
3279 struct mls_range *r = data;
3280 struct policy_data *pd = ptr;
3282 struct policydb *p = pd->p;
3285 buf[0] = cpu_to_le32(rt->source_type);
3286 buf[1] = cpu_to_le32(rt->target_type);
3287 rc = put_entry(buf, sizeof(u32), 2, fp);
3290 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3291 buf[0] = cpu_to_le32(rt->target_class);
3292 rc = put_entry(buf, sizeof(u32), 1, fp);
3296 rc = mls_write_range_helper(r, fp);
3303 static int range_write(struct policydb *p, void *fp)
3307 struct policy_data pd;
3312 /* count the number of entries in the hashtab */
3314 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3318 buf[0] = cpu_to_le32(nel);
3319 rc = put_entry(buf, sizeof(u32), 1, fp);
3323 /* actually write all of the entries */
3324 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3331 static int filename_write_helper(void *key, void *data, void *ptr)
3334 struct filename_trans *ft = key;
3335 struct filename_trans_datum *otype = data;
3340 len = strlen(ft->name);
3341 buf[0] = cpu_to_le32(len);
3342 rc = put_entry(buf, sizeof(u32), 1, fp);
3346 rc = put_entry(ft->name, sizeof(char), len, fp);
3350 buf[0] = cpu_to_le32(ft->stype);
3351 buf[1] = cpu_to_le32(ft->ttype);
3352 buf[2] = cpu_to_le32(ft->tclass);
3353 buf[3] = cpu_to_le32(otype->otype);
3355 rc = put_entry(buf, sizeof(u32), 4, fp);
3362 static int filename_trans_write(struct policydb *p, void *fp)
3368 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3372 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3376 buf[0] = cpu_to_le32(nel);
3377 rc = put_entry(buf, sizeof(u32), 1, fp);
3381 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3389 * Write the configuration data in a policy database
3390 * structure to a policy database binary representation
3393 int policydb_write(struct policydb *p, void *fp)
3395 unsigned int i, num_syms;
3400 struct policydb_compat_info *info;
3403 * refuse to write policy older than compressed avtab
3404 * to simplify the writer. There are other tests dropped
3405 * since we assume this throughout the writer code. Be
3406 * careful if you ever try to remove this restriction
3408 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3409 pr_err("SELinux: refusing to write policy version %d."
3410 " Because it is less than version %d\n", p->policyvers,
3411 POLICYDB_VERSION_AVTAB);
3417 config |= POLICYDB_CONFIG_MLS;
3419 if (p->reject_unknown)
3420 config |= REJECT_UNKNOWN;
3421 if (p->allow_unknown)
3422 config |= ALLOW_UNKNOWN;
3424 /* Write the magic number and string identifiers. */
3425 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3426 len = strlen(POLICYDB_STRING);
3427 buf[1] = cpu_to_le32(len);
3428 rc = put_entry(buf, sizeof(u32), 2, fp);
3431 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3435 /* Write the version, config, and table sizes. */
3436 info = policydb_lookup_compat(p->policyvers);
3438 pr_err("SELinux: compatibility lookup failed for policy "
3439 "version %d", p->policyvers);
3443 buf[0] = cpu_to_le32(p->policyvers);
3444 buf[1] = cpu_to_le32(config);
3445 buf[2] = cpu_to_le32(info->sym_num);
3446 buf[3] = cpu_to_le32(info->ocon_num);
3448 rc = put_entry(buf, sizeof(u32), 4, fp);
3452 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3453 rc = ebitmap_write(&p->policycaps, fp);
3458 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3459 rc = ebitmap_write(&p->permissive_map, fp);
3464 num_syms = info->sym_num;
3465 for (i = 0; i < num_syms; i++) {
3466 struct policy_data pd;
3471 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3472 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3474 rc = put_entry(buf, sizeof(u32), 2, fp);
3477 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3482 rc = avtab_write(p, &p->te_avtab, fp);
3486 rc = cond_write_list(p, p->cond_list, fp);
3490 rc = role_trans_write(p, fp);
3494 rc = role_allow_write(p->role_allow, fp);
3498 rc = filename_trans_write(p, fp);
3502 rc = ocontext_write(p, info, fp);
3506 rc = genfs_write(p, fp);
3510 rc = range_write(p, fp);
3514 for (i = 0; i < p->p_types.nprim; i++) {
3515 struct ebitmap *e = &p->type_attr_map_array[i];
3517 rc = ebitmap_write(e, fp);