2 * Implementation of the policy database.
10 * Support for enhanced MLS infrastructure.
14 * Added conditional policy language extensions
18 * Added support for the policy capability bitmap
20 * Update: Mellanox Techonologies
22 * Added Infiniband support
24 * Copyright (C) 2016 Mellanox Techonologies
25 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/audit.h>
39 #include <linux/flex_array.h>
43 #include "conditional.h"
50 static const char *symtab_name[SYM_NUM] = {
62 static unsigned int symtab_sizes[SYM_NUM] = {
73 struct policydb_compat_info {
79 /* These need to be updated if SYM_NUM or OCON_NUM changes */
80 static struct policydb_compat_info policydb_compat[] = {
82 .version = POLICYDB_VERSION_BASE,
83 .sym_num = SYM_NUM - 3,
84 .ocon_num = OCON_NUM - 3,
87 .version = POLICYDB_VERSION_BOOL,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM - 3,
92 .version = POLICYDB_VERSION_IPV6,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM - 2,
97 .version = POLICYDB_VERSION_NLCLASS,
98 .sym_num = SYM_NUM - 2,
99 .ocon_num = OCON_NUM - 2,
102 .version = POLICYDB_VERSION_MLS,
104 .ocon_num = OCON_NUM - 2,
107 .version = POLICYDB_VERSION_AVTAB,
109 .ocon_num = OCON_NUM - 2,
112 .version = POLICYDB_VERSION_RANGETRANS,
114 .ocon_num = OCON_NUM - 2,
117 .version = POLICYDB_VERSION_POLCAP,
119 .ocon_num = OCON_NUM - 2,
122 .version = POLICYDB_VERSION_PERMISSIVE,
124 .ocon_num = OCON_NUM - 2,
127 .version = POLICYDB_VERSION_BOUNDARY,
129 .ocon_num = OCON_NUM - 2,
132 .version = POLICYDB_VERSION_FILENAME_TRANS,
134 .ocon_num = OCON_NUM - 2,
137 .version = POLICYDB_VERSION_ROLETRANS,
139 .ocon_num = OCON_NUM - 2,
142 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
144 .ocon_num = OCON_NUM - 2,
147 .version = POLICYDB_VERSION_DEFAULT_TYPE,
149 .ocon_num = OCON_NUM - 2,
152 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
154 .ocon_num = OCON_NUM - 2,
157 .version = POLICYDB_VERSION_XPERMS_IOCTL,
159 .ocon_num = OCON_NUM - 2,
162 .version = POLICYDB_VERSION_INFINIBAND,
164 .ocon_num = OCON_NUM,
168 static struct policydb_compat_info *policydb_lookup_compat(int version)
171 struct policydb_compat_info *info = NULL;
173 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
174 if (policydb_compat[i].version == version) {
175 info = &policydb_compat[i];
183 * Initialize the role table.
185 static int roles_init(struct policydb *p)
189 struct role_datum *role;
191 role = kzalloc(sizeof(*role), GFP_KERNEL);
196 role->value = ++p->p_roles.nprim;
197 if (role->value != OBJECT_R_VAL)
201 key = kstrdup(OBJECT_R, GFP_KERNEL);
205 rc = hashtab_insert(p->p_roles.table, key, role);
216 static u32 filenametr_hash(struct hashtab *h, const void *k)
218 const struct filename_trans *ft = k;
220 unsigned int byte_num;
223 hash = ft->stype ^ ft->ttype ^ ft->tclass;
226 while ((focus = ft->name[byte_num++]))
227 hash = partial_name_hash(focus, hash);
228 return hash & (h->size - 1);
231 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
233 const struct filename_trans *ft1 = k1;
234 const struct filename_trans *ft2 = k2;
237 v = ft1->stype - ft2->stype;
241 v = ft1->ttype - ft2->ttype;
245 v = ft1->tclass - ft2->tclass;
249 return strcmp(ft1->name, ft2->name);
253 static u32 rangetr_hash(struct hashtab *h, const void *k)
255 const struct range_trans *key = k;
256 return (key->source_type + (key->target_type << 3) +
257 (key->target_class << 5)) & (h->size - 1);
260 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
262 const struct range_trans *key1 = k1, *key2 = k2;
265 v = key1->source_type - key2->source_type;
269 v = key1->target_type - key2->target_type;
273 v = key1->target_class - key2->target_class;
279 * Initialize a policy database structure.
281 static int policydb_init(struct policydb *p)
285 memset(p, 0, sizeof(*p));
287 for (i = 0; i < SYM_NUM; i++) {
288 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
293 rc = avtab_init(&p->te_avtab);
301 rc = cond_policydb_init(p);
305 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
306 if (!p->filename_trans) {
311 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
317 ebitmap_init(&p->filename_trans_ttypes);
318 ebitmap_init(&p->policycaps);
319 ebitmap_init(&p->permissive_map);
323 hashtab_destroy(p->filename_trans);
324 hashtab_destroy(p->range_tr);
325 for (i = 0; i < SYM_NUM; i++)
326 hashtab_destroy(p->symtab[i].table);
331 * The following *_index functions are used to
332 * define the val_to_name and val_to_struct arrays
333 * in a policy database structure. The val_to_name
334 * arrays are used when converting security context
335 * structures into string representations. The
336 * val_to_struct arrays are used when the attributes
337 * of a class, role, or user are needed.
340 static int common_index(void *key, void *datum, void *datap)
343 struct common_datum *comdatum;
344 struct flex_array *fa;
348 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
351 fa = p->sym_val_to_name[SYM_COMMONS];
352 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
353 GFP_KERNEL | __GFP_ZERO))
358 static int class_index(void *key, void *datum, void *datap)
361 struct class_datum *cladatum;
362 struct flex_array *fa;
366 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
368 fa = p->sym_val_to_name[SYM_CLASSES];
369 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
370 GFP_KERNEL | __GFP_ZERO))
372 p->class_val_to_struct[cladatum->value - 1] = cladatum;
376 static int role_index(void *key, void *datum, void *datap)
379 struct role_datum *role;
380 struct flex_array *fa;
385 || role->value > p->p_roles.nprim
386 || role->bounds > p->p_roles.nprim)
389 fa = p->sym_val_to_name[SYM_ROLES];
390 if (flex_array_put_ptr(fa, role->value - 1, key,
391 GFP_KERNEL | __GFP_ZERO))
393 p->role_val_to_struct[role->value - 1] = role;
397 static int type_index(void *key, void *datum, void *datap)
400 struct type_datum *typdatum;
401 struct flex_array *fa;
406 if (typdatum->primary) {
408 || typdatum->value > p->p_types.nprim
409 || typdatum->bounds > p->p_types.nprim)
411 fa = p->sym_val_to_name[SYM_TYPES];
412 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
413 GFP_KERNEL | __GFP_ZERO))
416 fa = p->type_val_to_struct_array;
417 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
418 GFP_KERNEL | __GFP_ZERO))
425 static int user_index(void *key, void *datum, void *datap)
428 struct user_datum *usrdatum;
429 struct flex_array *fa;
434 || usrdatum->value > p->p_users.nprim
435 || usrdatum->bounds > p->p_users.nprim)
438 fa = p->sym_val_to_name[SYM_USERS];
439 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
440 GFP_KERNEL | __GFP_ZERO))
442 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
446 static int sens_index(void *key, void *datum, void *datap)
449 struct level_datum *levdatum;
450 struct flex_array *fa;
455 if (!levdatum->isalias) {
456 if (!levdatum->level->sens ||
457 levdatum->level->sens > p->p_levels.nprim)
459 fa = p->sym_val_to_name[SYM_LEVELS];
460 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
461 GFP_KERNEL | __GFP_ZERO))
468 static int cat_index(void *key, void *datum, void *datap)
471 struct cat_datum *catdatum;
472 struct flex_array *fa;
477 if (!catdatum->isalias) {
478 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
480 fa = p->sym_val_to_name[SYM_CATS];
481 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
482 GFP_KERNEL | __GFP_ZERO))
489 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
502 static void hash_eval(struct hashtab *h, const char *hash_name)
504 struct hashtab_info info;
506 hashtab_stat(h, &info);
507 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
508 "longest chain length %d\n", hash_name, h->nel,
509 info.slots_used, h->size, info.max_chain_len);
512 static void symtab_hash_eval(struct symtab *s)
516 for (i = 0; i < SYM_NUM; i++)
517 hash_eval(s[i].table, symtab_name[i]);
521 static inline void hash_eval(struct hashtab *h, char *hash_name)
527 * Define the other val_to_name and val_to_struct arrays
528 * in a policy database structure.
530 * Caller must clean up on failure.
532 static int policydb_index(struct policydb *p)
537 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
538 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
539 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
541 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
542 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
545 pr_debug("SELinux: %d classes, %d rules\n",
546 p->p_classes.nprim, p->te_avtab.nel);
549 avtab_hash_eval(&p->te_avtab, "rules");
550 symtab_hash_eval(p->symtab);
553 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
554 sizeof(*p->class_val_to_struct),
556 if (!p->class_val_to_struct)
559 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
560 sizeof(*p->role_val_to_struct),
562 if (!p->role_val_to_struct)
565 p->user_val_to_struct = kcalloc(p->p_users.nprim,
566 sizeof(*p->user_val_to_struct),
568 if (!p->user_val_to_struct)
571 /* Yes, I want the sizeof the pointer, not the structure */
572 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
574 GFP_KERNEL | __GFP_ZERO);
575 if (!p->type_val_to_struct_array)
578 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
579 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
583 rc = cond_init_bool_indexes(p);
587 for (i = 0; i < SYM_NUM; i++) {
588 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
590 GFP_KERNEL | __GFP_ZERO);
591 if (!p->sym_val_to_name[i])
594 rc = flex_array_prealloc(p->sym_val_to_name[i],
595 0, p->symtab[i].nprim,
596 GFP_KERNEL | __GFP_ZERO);
600 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
610 * The following *_destroy functions are used to
611 * free any memory allocated for each kind of
612 * symbol data in the policy database.
615 static int perm_destroy(void *key, void *datum, void *p)
622 static int common_destroy(void *key, void *datum, void *p)
624 struct common_datum *comdatum;
629 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
630 hashtab_destroy(comdatum->permissions.table);
636 static void constraint_expr_destroy(struct constraint_expr *expr)
639 ebitmap_destroy(&expr->names);
640 if (expr->type_names) {
641 ebitmap_destroy(&expr->type_names->types);
642 ebitmap_destroy(&expr->type_names->negset);
643 kfree(expr->type_names);
649 static int cls_destroy(void *key, void *datum, void *p)
651 struct class_datum *cladatum;
652 struct constraint_node *constraint, *ctemp;
653 struct constraint_expr *e, *etmp;
658 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
659 hashtab_destroy(cladatum->permissions.table);
660 constraint = cladatum->constraints;
662 e = constraint->expr;
666 constraint_expr_destroy(etmp);
669 constraint = constraint->next;
673 constraint = cladatum->validatetrans;
675 e = constraint->expr;
679 constraint_expr_destroy(etmp);
682 constraint = constraint->next;
685 kfree(cladatum->comkey);
691 static int role_destroy(void *key, void *datum, void *p)
693 struct role_datum *role;
698 ebitmap_destroy(&role->dominates);
699 ebitmap_destroy(&role->types);
705 static int type_destroy(void *key, void *datum, void *p)
712 static int user_destroy(void *key, void *datum, void *p)
714 struct user_datum *usrdatum;
719 ebitmap_destroy(&usrdatum->roles);
720 ebitmap_destroy(&usrdatum->range.level[0].cat);
721 ebitmap_destroy(&usrdatum->range.level[1].cat);
722 ebitmap_destroy(&usrdatum->dfltlevel.cat);
728 static int sens_destroy(void *key, void *datum, void *p)
730 struct level_datum *levdatum;
735 ebitmap_destroy(&levdatum->level->cat);
736 kfree(levdatum->level);
742 static int cat_destroy(void *key, void *datum, void *p)
749 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
761 static int filenametr_destroy(void *key, void *datum, void *p)
763 struct filename_trans *ft = key;
771 static int range_tr_destroy(void *key, void *datum, void *p)
773 struct mls_range *rt = datum;
775 ebitmap_destroy(&rt->level[0].cat);
776 ebitmap_destroy(&rt->level[1].cat);
782 static void ocontext_destroy(struct ocontext *c, int i)
787 context_destroy(&c->context[0]);
788 context_destroy(&c->context[1]);
789 if (i == OCON_ISID || i == OCON_FS ||
790 i == OCON_NETIF || i == OCON_FSUSE)
796 * Free any memory allocated by a policy database structure.
798 void policydb_destroy(struct policydb *p)
800 struct ocontext *c, *ctmp;
801 struct genfs *g, *gtmp;
803 struct role_allow *ra, *lra = NULL;
804 struct role_trans *tr, *ltr = NULL;
806 for (i = 0; i < SYM_NUM; i++) {
808 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
809 hashtab_destroy(p->symtab[i].table);
812 for (i = 0; i < SYM_NUM; i++) {
813 if (p->sym_val_to_name[i])
814 flex_array_free(p->sym_val_to_name[i]);
817 kfree(p->class_val_to_struct);
818 kfree(p->role_val_to_struct);
819 kfree(p->user_val_to_struct);
820 if (p->type_val_to_struct_array)
821 flex_array_free(p->type_val_to_struct_array);
823 avtab_destroy(&p->te_avtab);
825 for (i = 0; i < OCON_NUM; i++) {
831 ocontext_destroy(ctmp, i);
833 p->ocontexts[i] = NULL;
844 ocontext_destroy(ctmp, OCON_FSUSE);
852 cond_policydb_destroy(p);
854 for (tr = p->role_tr; tr; tr = tr->next) {
861 for (ra = p->role_allow; ra; ra = ra->next) {
868 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
869 hashtab_destroy(p->filename_trans);
871 hashtab_map(p->range_tr, range_tr_destroy, NULL);
872 hashtab_destroy(p->range_tr);
874 if (p->type_attr_map_array) {
875 for (i = 0; i < p->p_types.nprim; i++) {
878 e = flex_array_get(p->type_attr_map_array, i);
883 flex_array_free(p->type_attr_map_array);
886 ebitmap_destroy(&p->filename_trans_ttypes);
887 ebitmap_destroy(&p->policycaps);
888 ebitmap_destroy(&p->permissive_map);
892 * Load the initial SIDs specified in a policy database
893 * structure into a SID table.
895 int policydb_load_isids(struct policydb *p, struct sidtab *s)
897 struct ocontext *head, *c;
902 pr_err("SELinux: out of memory on SID table init\n");
906 head = p->ocontexts[OCON_ISID];
907 for (c = head; c; c = c->next) {
909 if (!c->context[0].user) {
910 pr_err("SELinux: SID %s was never defined.\n",
915 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
917 pr_err("SELinux: unable to load initial SID %s.\n",
927 int policydb_class_isvalid(struct policydb *p, unsigned int class)
929 if (!class || class > p->p_classes.nprim)
934 int policydb_role_isvalid(struct policydb *p, unsigned int role)
936 if (!role || role > p->p_roles.nprim)
941 int policydb_type_isvalid(struct policydb *p, unsigned int type)
943 if (!type || type > p->p_types.nprim)
949 * Return 1 if the fields in the security context
950 * structure `c' are valid. Return 0 otherwise.
952 int policydb_context_isvalid(struct policydb *p, struct context *c)
954 struct role_datum *role;
955 struct user_datum *usrdatum;
957 if (!c->role || c->role > p->p_roles.nprim)
960 if (!c->user || c->user > p->p_users.nprim)
963 if (!c->type || c->type > p->p_types.nprim)
966 if (c->role != OBJECT_R_VAL) {
968 * Role must be authorized for the type.
970 role = p->role_val_to_struct[c->role - 1];
971 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
972 /* role may not be associated with type */
976 * User must be authorized for the role.
978 usrdatum = p->user_val_to_struct[c->user - 1];
982 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
983 /* user may not be associated with role */
987 if (!mls_context_isvalid(p, c))
994 * Read a MLS range structure from a policydb binary
995 * representation file.
997 static int mls_read_range_helper(struct mls_range *r, void *fp)
1003 rc = next_entry(buf, fp, sizeof(u32));
1008 items = le32_to_cpu(buf[0]);
1009 if (items > ARRAY_SIZE(buf)) {
1010 pr_err("SELinux: mls: range overflow\n");
1014 rc = next_entry(buf, fp, sizeof(u32) * items);
1016 pr_err("SELinux: mls: truncated range\n");
1020 r->level[0].sens = le32_to_cpu(buf[0]);
1022 r->level[1].sens = le32_to_cpu(buf[1]);
1024 r->level[1].sens = r->level[0].sens;
1026 rc = ebitmap_read(&r->level[0].cat, fp);
1028 pr_err("SELinux: mls: error reading low categories\n");
1032 rc = ebitmap_read(&r->level[1].cat, fp);
1034 pr_err("SELinux: mls: error reading high categories\n");
1038 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1040 pr_err("SELinux: mls: out of memory\n");
1047 ebitmap_destroy(&r->level[0].cat);
1053 * Read and validate a security context structure
1054 * from a policydb binary representation file.
1056 static int context_read_and_validate(struct context *c,
1063 rc = next_entry(buf, fp, sizeof buf);
1065 pr_err("SELinux: context truncated\n");
1068 c->user = le32_to_cpu(buf[0]);
1069 c->role = le32_to_cpu(buf[1]);
1070 c->type = le32_to_cpu(buf[2]);
1071 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1072 rc = mls_read_range_helper(&c->range, fp);
1074 pr_err("SELinux: error reading MLS range of context\n");
1080 if (!policydb_context_isvalid(p, c)) {
1081 pr_err("SELinux: invalid security context\n");
1091 * The following *_read functions are used to
1092 * read the symbol data from a policy database
1093 * binary representation file.
1096 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1101 if ((len == 0) || (len == (u32)-1))
1104 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1108 /* it's expected the caller should free the str */
1111 rc = next_entry(str, fp, len);
1119 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1122 struct perm_datum *perdatum;
1127 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1131 rc = next_entry(buf, fp, sizeof buf);
1135 len = le32_to_cpu(buf[0]);
1136 perdatum->value = le32_to_cpu(buf[1]);
1138 rc = str_read(&key, GFP_KERNEL, fp, len);
1142 rc = hashtab_insert(h, key, perdatum);
1148 perm_destroy(key, perdatum, NULL);
1152 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1155 struct common_datum *comdatum;
1160 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1164 rc = next_entry(buf, fp, sizeof buf);
1168 len = le32_to_cpu(buf[0]);
1169 comdatum->value = le32_to_cpu(buf[1]);
1171 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1174 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1175 nel = le32_to_cpu(buf[3]);
1177 rc = str_read(&key, GFP_KERNEL, fp, len);
1181 for (i = 0; i < nel; i++) {
1182 rc = perm_read(p, comdatum->permissions.table, fp);
1187 rc = hashtab_insert(h, key, comdatum);
1192 common_destroy(key, comdatum, NULL);
1196 static void type_set_init(struct type_set *t)
1198 ebitmap_init(&t->types);
1199 ebitmap_init(&t->negset);
1202 static int type_set_read(struct type_set *t, void *fp)
1207 if (ebitmap_read(&t->types, fp))
1209 if (ebitmap_read(&t->negset, fp))
1212 rc = next_entry(buf, fp, sizeof(u32));
1215 t->flags = le32_to_cpu(buf[0]);
1221 static int read_cons_helper(struct policydb *p,
1222 struct constraint_node **nodep,
1223 int ncons, int allowxtarget, void *fp)
1225 struct constraint_node *c, *lc;
1226 struct constraint_expr *e, *le;
1229 int rc, i, j, depth;
1232 for (i = 0; i < ncons; i++) {
1233 c = kzalloc(sizeof(*c), GFP_KERNEL);
1242 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1245 c->permissions = le32_to_cpu(buf[0]);
1246 nexpr = le32_to_cpu(buf[1]);
1249 for (j = 0; j < nexpr; j++) {
1250 e = kzalloc(sizeof(*e), GFP_KERNEL);
1259 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1262 e->expr_type = le32_to_cpu(buf[0]);
1263 e->attr = le32_to_cpu(buf[1]);
1264 e->op = le32_to_cpu(buf[2]);
1266 switch (e->expr_type) {
1278 if (depth == (CEXPR_MAXDEPTH - 1))
1283 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1285 if (depth == (CEXPR_MAXDEPTH - 1))
1288 rc = ebitmap_read(&e->names, fp);
1291 if (p->policyvers >=
1292 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1293 e->type_names = kzalloc(sizeof
1298 type_set_init(e->type_names);
1299 rc = type_set_read(e->type_names, fp);
1317 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1320 struct class_datum *cladatum;
1322 u32 len, len2, ncons, nel;
1325 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1329 rc = next_entry(buf, fp, sizeof(u32)*6);
1333 len = le32_to_cpu(buf[0]);
1334 len2 = le32_to_cpu(buf[1]);
1335 cladatum->value = le32_to_cpu(buf[2]);
1337 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1340 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1341 nel = le32_to_cpu(buf[4]);
1343 ncons = le32_to_cpu(buf[5]);
1345 rc = str_read(&key, GFP_KERNEL, fp, len);
1350 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1355 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1356 if (!cladatum->comdatum) {
1357 pr_err("SELinux: unknown common %s\n",
1362 for (i = 0; i < nel; i++) {
1363 rc = perm_read(p, cladatum->permissions.table, fp);
1368 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1372 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1373 /* grab the validatetrans rules */
1374 rc = next_entry(buf, fp, sizeof(u32));
1377 ncons = le32_to_cpu(buf[0]);
1378 rc = read_cons_helper(p, &cladatum->validatetrans,
1384 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1385 rc = next_entry(buf, fp, sizeof(u32) * 3);
1389 cladatum->default_user = le32_to_cpu(buf[0]);
1390 cladatum->default_role = le32_to_cpu(buf[1]);
1391 cladatum->default_range = le32_to_cpu(buf[2]);
1394 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1395 rc = next_entry(buf, fp, sizeof(u32) * 1);
1398 cladatum->default_type = le32_to_cpu(buf[0]);
1401 rc = hashtab_insert(h, key, cladatum);
1407 cls_destroy(key, cladatum, NULL);
1411 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1414 struct role_datum *role;
1415 int rc, to_read = 2;
1419 role = kzalloc(sizeof(*role), GFP_KERNEL);
1423 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1426 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1430 len = le32_to_cpu(buf[0]);
1431 role->value = le32_to_cpu(buf[1]);
1432 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1433 role->bounds = le32_to_cpu(buf[2]);
1435 rc = str_read(&key, GFP_KERNEL, fp, len);
1439 rc = ebitmap_read(&role->dominates, fp);
1443 rc = ebitmap_read(&role->types, fp);
1447 if (strcmp(key, OBJECT_R) == 0) {
1449 if (role->value != OBJECT_R_VAL) {
1450 pr_err("SELinux: Role %s has wrong value %d\n",
1451 OBJECT_R, role->value);
1458 rc = hashtab_insert(h, key, role);
1463 role_destroy(key, role, NULL);
1467 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1470 struct type_datum *typdatum;
1471 int rc, to_read = 3;
1475 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1479 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1482 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1486 len = le32_to_cpu(buf[0]);
1487 typdatum->value = le32_to_cpu(buf[1]);
1488 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1489 u32 prop = le32_to_cpu(buf[2]);
1491 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1492 typdatum->primary = 1;
1493 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1494 typdatum->attribute = 1;
1496 typdatum->bounds = le32_to_cpu(buf[3]);
1498 typdatum->primary = le32_to_cpu(buf[2]);
1501 rc = str_read(&key, GFP_KERNEL, fp, len);
1505 rc = hashtab_insert(h, key, typdatum);
1510 type_destroy(key, typdatum, NULL);
1516 * Read a MLS level structure from a policydb binary
1517 * representation file.
1519 static int mls_read_level(struct mls_level *lp, void *fp)
1524 memset(lp, 0, sizeof(*lp));
1526 rc = next_entry(buf, fp, sizeof buf);
1528 pr_err("SELinux: mls: truncated level\n");
1531 lp->sens = le32_to_cpu(buf[0]);
1533 rc = ebitmap_read(&lp->cat, fp);
1535 pr_err("SELinux: mls: error reading level categories\n");
1541 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1544 struct user_datum *usrdatum;
1545 int rc, to_read = 2;
1549 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1553 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1556 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1560 len = le32_to_cpu(buf[0]);
1561 usrdatum->value = le32_to_cpu(buf[1]);
1562 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1563 usrdatum->bounds = le32_to_cpu(buf[2]);
1565 rc = str_read(&key, GFP_KERNEL, fp, len);
1569 rc = ebitmap_read(&usrdatum->roles, fp);
1573 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1574 rc = mls_read_range_helper(&usrdatum->range, fp);
1577 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1582 rc = hashtab_insert(h, key, usrdatum);
1587 user_destroy(key, usrdatum, NULL);
1591 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1594 struct level_datum *levdatum;
1599 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1603 rc = next_entry(buf, fp, sizeof buf);
1607 len = le32_to_cpu(buf[0]);
1608 levdatum->isalias = le32_to_cpu(buf[1]);
1610 rc = str_read(&key, GFP_ATOMIC, fp, len);
1615 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1616 if (!levdatum->level)
1619 rc = mls_read_level(levdatum->level, fp);
1623 rc = hashtab_insert(h, key, levdatum);
1628 sens_destroy(key, levdatum, NULL);
1632 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1635 struct cat_datum *catdatum;
1640 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1644 rc = next_entry(buf, fp, sizeof buf);
1648 len = le32_to_cpu(buf[0]);
1649 catdatum->value = le32_to_cpu(buf[1]);
1650 catdatum->isalias = le32_to_cpu(buf[2]);
1652 rc = str_read(&key, GFP_ATOMIC, fp, len);
1656 rc = hashtab_insert(h, key, catdatum);
1661 cat_destroy(key, catdatum, NULL);
1665 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1677 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1679 struct user_datum *upper, *user;
1680 struct policydb *p = datap;
1683 upper = user = datum;
1684 while (upper->bounds) {
1685 struct ebitmap_node *node;
1688 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1689 pr_err("SELinux: user %s: "
1690 "too deep or looped boundary",
1695 upper = p->user_val_to_struct[upper->bounds - 1];
1696 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1697 if (ebitmap_get_bit(&upper->roles, bit))
1700 pr_err("SELinux: boundary violated policy: "
1701 "user=%s role=%s bounds=%s\n",
1702 sym_name(p, SYM_USERS, user->value - 1),
1703 sym_name(p, SYM_ROLES, bit),
1704 sym_name(p, SYM_USERS, upper->value - 1));
1713 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1715 struct role_datum *upper, *role;
1716 struct policydb *p = datap;
1719 upper = role = datum;
1720 while (upper->bounds) {
1721 struct ebitmap_node *node;
1724 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1725 pr_err("SELinux: role %s: "
1726 "too deep or looped bounds\n",
1731 upper = p->role_val_to_struct[upper->bounds - 1];
1732 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1733 if (ebitmap_get_bit(&upper->types, bit))
1736 pr_err("SELinux: boundary violated policy: "
1737 "role=%s type=%s bounds=%s\n",
1738 sym_name(p, SYM_ROLES, role->value - 1),
1739 sym_name(p, SYM_TYPES, bit),
1740 sym_name(p, SYM_ROLES, upper->value - 1));
1749 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1751 struct type_datum *upper;
1752 struct policydb *p = datap;
1756 while (upper->bounds) {
1757 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1758 pr_err("SELinux: type %s: "
1759 "too deep or looped boundary\n",
1764 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1768 if (upper->attribute) {
1769 pr_err("SELinux: type %s: "
1770 "bounded by attribute %s",
1772 sym_name(p, SYM_TYPES, upper->value - 1));
1780 static int policydb_bounds_sanity_check(struct policydb *p)
1784 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1787 rc = hashtab_map(p->p_users.table,
1788 user_bounds_sanity_check, p);
1792 rc = hashtab_map(p->p_roles.table,
1793 role_bounds_sanity_check, p);
1797 rc = hashtab_map(p->p_types.table,
1798 type_bounds_sanity_check, p);
1805 u16 string_to_security_class(struct policydb *p, const char *name)
1807 struct class_datum *cladatum;
1809 cladatum = hashtab_search(p->p_classes.table, name);
1813 return cladatum->value;
1816 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1818 struct class_datum *cladatum;
1819 struct perm_datum *perdatum = NULL;
1820 struct common_datum *comdatum;
1822 if (!tclass || tclass > p->p_classes.nprim)
1825 cladatum = p->class_val_to_struct[tclass-1];
1826 comdatum = cladatum->comdatum;
1828 perdatum = hashtab_search(comdatum->permissions.table,
1831 perdatum = hashtab_search(cladatum->permissions.table,
1836 return 1U << (perdatum->value-1);
1839 static int range_read(struct policydb *p, void *fp)
1841 struct range_trans *rt = NULL;
1842 struct mls_range *r = NULL;
1847 if (p->policyvers < POLICYDB_VERSION_MLS)
1850 rc = next_entry(buf, fp, sizeof(u32));
1854 nel = le32_to_cpu(buf[0]);
1855 for (i = 0; i < nel; i++) {
1857 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1861 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1865 rt->source_type = le32_to_cpu(buf[0]);
1866 rt->target_type = le32_to_cpu(buf[1]);
1867 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1868 rc = next_entry(buf, fp, sizeof(u32));
1871 rt->target_class = le32_to_cpu(buf[0]);
1873 rt->target_class = p->process_class;
1876 if (!policydb_type_isvalid(p, rt->source_type) ||
1877 !policydb_type_isvalid(p, rt->target_type) ||
1878 !policydb_class_isvalid(p, rt->target_class))
1882 r = kzalloc(sizeof(*r), GFP_KERNEL);
1886 rc = mls_read_range_helper(r, fp);
1891 if (!mls_range_isvalid(p, r)) {
1892 pr_warn("SELinux: rangetrans: invalid range\n");
1896 rc = hashtab_insert(p->range_tr, rt, r);
1903 hash_eval(p->range_tr, "rangetr");
1911 static int filename_trans_read(struct policydb *p, void *fp)
1913 struct filename_trans *ft;
1914 struct filename_trans_datum *otype;
1920 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1923 rc = next_entry(buf, fp, sizeof(u32));
1926 nel = le32_to_cpu(buf[0]);
1928 for (i = 0; i < nel; i++) {
1933 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1938 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1942 /* length of the path component string */
1943 rc = next_entry(buf, fp, sizeof(u32));
1946 len = le32_to_cpu(buf[0]);
1948 /* path component string */
1949 rc = str_read(&name, GFP_KERNEL, fp, len);
1955 rc = next_entry(buf, fp, sizeof(u32) * 4);
1959 ft->stype = le32_to_cpu(buf[0]);
1960 ft->ttype = le32_to_cpu(buf[1]);
1961 ft->tclass = le32_to_cpu(buf[2]);
1963 otype->otype = le32_to_cpu(buf[3]);
1965 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1969 rc = hashtab_insert(p->filename_trans, ft, otype);
1972 * Do not return -EEXIST to the caller, or the system
1977 /* But free memory to avoid memory leak. */
1983 hash_eval(p->filename_trans, "filenametr");
1993 static int genfs_read(struct policydb *p, void *fp)
1996 u32 nel, nel2, len, len2;
1998 struct ocontext *l, *c;
1999 struct ocontext *newc = NULL;
2000 struct genfs *genfs_p, *genfs;
2001 struct genfs *newgenfs = NULL;
2003 rc = next_entry(buf, fp, sizeof(u32));
2006 nel = le32_to_cpu(buf[0]);
2008 for (i = 0; i < nel; i++) {
2009 rc = next_entry(buf, fp, sizeof(u32));
2012 len = le32_to_cpu(buf[0]);
2015 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2019 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2023 for (genfs_p = NULL, genfs = p->genfs; genfs;
2024 genfs_p = genfs, genfs = genfs->next) {
2026 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2027 pr_err("SELinux: dup genfs fstype %s\n",
2031 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2034 newgenfs->next = genfs;
2036 genfs_p->next = newgenfs;
2038 p->genfs = newgenfs;
2042 rc = next_entry(buf, fp, sizeof(u32));
2046 nel2 = le32_to_cpu(buf[0]);
2047 for (j = 0; j < nel2; j++) {
2048 rc = next_entry(buf, fp, sizeof(u32));
2051 len = le32_to_cpu(buf[0]);
2054 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2058 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2062 rc = next_entry(buf, fp, sizeof(u32));
2066 newc->v.sclass = le32_to_cpu(buf[0]);
2067 rc = context_read_and_validate(&newc->context[0], p, fp);
2071 for (l = NULL, c = genfs->head; c;
2072 l = c, c = c->next) {
2074 if (!strcmp(newc->u.name, c->u.name) &&
2075 (!c->v.sclass || !newc->v.sclass ||
2076 newc->v.sclass == c->v.sclass)) {
2077 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2078 genfs->fstype, c->u.name);
2081 len = strlen(newc->u.name);
2082 len2 = strlen(c->u.name);
2098 kfree(newgenfs->fstype);
2101 ocontext_destroy(newc, OCON_FSUSE);
2106 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2112 struct ocontext *l, *c;
2115 for (i = 0; i < info->ocon_num; i++) {
2116 rc = next_entry(buf, fp, sizeof(u32));
2119 nel = le32_to_cpu(buf[0]);
2122 for (j = 0; j < nel; j++) {
2124 c = kzalloc(sizeof(*c), GFP_KERNEL);
2130 p->ocontexts[i] = c;
2135 rc = next_entry(buf, fp, sizeof(u32));
2139 c->sid[0] = le32_to_cpu(buf[0]);
2140 rc = context_read_and_validate(&c->context[0], p, fp);
2146 rc = next_entry(buf, fp, sizeof(u32));
2149 len = le32_to_cpu(buf[0]);
2151 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2155 rc = context_read_and_validate(&c->context[0], p, fp);
2158 rc = context_read_and_validate(&c->context[1], p, fp);
2163 rc = next_entry(buf, fp, sizeof(u32)*3);
2166 c->u.port.protocol = le32_to_cpu(buf[0]);
2167 c->u.port.low_port = le32_to_cpu(buf[1]);
2168 c->u.port.high_port = le32_to_cpu(buf[2]);
2169 rc = context_read_and_validate(&c->context[0], p, fp);
2174 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2177 c->u.node.addr = nodebuf[0]; /* network order */
2178 c->u.node.mask = nodebuf[1]; /* network order */
2179 rc = context_read_and_validate(&c->context[0], p, fp);
2184 rc = next_entry(buf, fp, sizeof(u32)*2);
2189 c->v.behavior = le32_to_cpu(buf[0]);
2190 /* Determined at runtime, not in policy DB. */
2191 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2193 if (c->v.behavior > SECURITY_FS_USE_MAX)
2196 len = le32_to_cpu(buf[1]);
2197 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2201 rc = context_read_and_validate(&c->context[0], p, fp);
2208 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2211 for (k = 0; k < 4; k++)
2212 c->u.node6.addr[k] = nodebuf[k];
2213 for (k = 0; k < 4; k++)
2214 c->u.node6.mask[k] = nodebuf[k+4];
2215 rc = context_read_and_validate(&c->context[0], p, fp);
2221 rc = next_entry(nodebuf, fp, sizeof(u32) * 4);
2225 c->u.ibpkey.subnet_prefix = be64_to_cpu(*((__be64 *)nodebuf));
2227 if (nodebuf[2] > 0xffff ||
2228 nodebuf[3] > 0xffff) {
2233 c->u.ibpkey.low_pkey = le32_to_cpu(nodebuf[2]);
2234 c->u.ibpkey.high_pkey = le32_to_cpu(nodebuf[3]);
2236 rc = context_read_and_validate(&c->context[0],
2242 case OCON_IBENDPORT:
2243 rc = next_entry(buf, fp, sizeof(u32) * 2);
2246 len = le32_to_cpu(buf[0]);
2248 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2252 if (buf[1] > 0xff || buf[1] == 0) {
2257 c->u.ibendport.port = le32_to_cpu(buf[1]);
2259 rc = context_read_and_validate(&c->context[0],
2274 * Read the configuration data from a policy database binary
2275 * representation file into a policy database structure.
2277 int policydb_read(struct policydb *p, void *fp)
2279 struct role_allow *ra, *lra;
2280 struct role_trans *tr, *ltr;
2283 u32 len, nprim, nel;
2286 struct policydb_compat_info *info;
2288 rc = policydb_init(p);
2292 /* Read the magic number and string length. */
2293 rc = next_entry(buf, fp, sizeof(u32) * 2);
2298 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2299 pr_err("SELinux: policydb magic number 0x%x does "
2300 "not match expected magic number 0x%x\n",
2301 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2306 len = le32_to_cpu(buf[1]);
2307 if (len != strlen(POLICYDB_STRING)) {
2308 pr_err("SELinux: policydb string length %d does not "
2309 "match expected length %zu\n",
2310 len, strlen(POLICYDB_STRING));
2315 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2316 if (!policydb_str) {
2317 pr_err("SELinux: unable to allocate memory for policydb "
2318 "string of length %d\n", len);
2322 rc = next_entry(policydb_str, fp, len);
2324 pr_err("SELinux: truncated policydb string identifier\n");
2325 kfree(policydb_str);
2330 policydb_str[len] = '\0';
2331 if (strcmp(policydb_str, POLICYDB_STRING)) {
2332 pr_err("SELinux: policydb string %s does not match "
2333 "my string %s\n", policydb_str, POLICYDB_STRING);
2334 kfree(policydb_str);
2337 /* Done with policydb_str. */
2338 kfree(policydb_str);
2339 policydb_str = NULL;
2341 /* Read the version and table sizes. */
2342 rc = next_entry(buf, fp, sizeof(u32)*4);
2347 p->policyvers = le32_to_cpu(buf[0]);
2348 if (p->policyvers < POLICYDB_VERSION_MIN ||
2349 p->policyvers > POLICYDB_VERSION_MAX) {
2350 pr_err("SELinux: policydb version %d does not match "
2351 "my version range %d-%d\n",
2352 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2356 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2360 if (p->policyvers < POLICYDB_VERSION_MLS) {
2361 pr_err("SELinux: security policydb version %d "
2362 "(MLS) not backwards compatible\n",
2367 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2368 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2370 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2371 rc = ebitmap_read(&p->policycaps, fp);
2376 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2377 rc = ebitmap_read(&p->permissive_map, fp);
2383 info = policydb_lookup_compat(p->policyvers);
2385 pr_err("SELinux: unable to find policy compat info "
2386 "for version %d\n", p->policyvers);
2391 if (le32_to_cpu(buf[2]) != info->sym_num ||
2392 le32_to_cpu(buf[3]) != info->ocon_num) {
2393 pr_err("SELinux: policydb table sizes (%d,%d) do "
2394 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2395 le32_to_cpu(buf[3]),
2396 info->sym_num, info->ocon_num);
2400 for (i = 0; i < info->sym_num; i++) {
2401 rc = next_entry(buf, fp, sizeof(u32)*2);
2404 nprim = le32_to_cpu(buf[0]);
2405 nel = le32_to_cpu(buf[1]);
2406 for (j = 0; j < nel; j++) {
2407 rc = read_f[i](p, p->symtab[i].table, fp);
2412 p->symtab[i].nprim = nprim;
2416 p->process_class = string_to_security_class(p, "process");
2417 if (!p->process_class)
2420 rc = avtab_read(&p->te_avtab, fp, p);
2424 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2425 rc = cond_read_list(p, fp);
2430 rc = next_entry(buf, fp, sizeof(u32));
2433 nel = le32_to_cpu(buf[0]);
2435 for (i = 0; i < nel; i++) {
2437 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2444 rc = next_entry(buf, fp, sizeof(u32)*3);
2449 tr->role = le32_to_cpu(buf[0]);
2450 tr->type = le32_to_cpu(buf[1]);
2451 tr->new_role = le32_to_cpu(buf[2]);
2452 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2453 rc = next_entry(buf, fp, sizeof(u32));
2456 tr->tclass = le32_to_cpu(buf[0]);
2458 tr->tclass = p->process_class;
2461 if (!policydb_role_isvalid(p, tr->role) ||
2462 !policydb_type_isvalid(p, tr->type) ||
2463 !policydb_class_isvalid(p, tr->tclass) ||
2464 !policydb_role_isvalid(p, tr->new_role))
2469 rc = next_entry(buf, fp, sizeof(u32));
2472 nel = le32_to_cpu(buf[0]);
2474 for (i = 0; i < nel; i++) {
2476 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2483 rc = next_entry(buf, fp, sizeof(u32)*2);
2488 ra->role = le32_to_cpu(buf[0]);
2489 ra->new_role = le32_to_cpu(buf[1]);
2490 if (!policydb_role_isvalid(p, ra->role) ||
2491 !policydb_role_isvalid(p, ra->new_role))
2496 rc = filename_trans_read(p, fp);
2500 rc = policydb_index(p);
2505 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2506 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2507 if (!p->process_trans_perms)
2510 rc = ocontext_read(p, info, fp);
2514 rc = genfs_read(p, fp);
2518 rc = range_read(p, fp);
2523 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2525 GFP_KERNEL | __GFP_ZERO);
2526 if (!p->type_attr_map_array)
2529 /* preallocate so we don't have to worry about the put ever failing */
2530 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2531 GFP_KERNEL | __GFP_ZERO);
2535 for (i = 0; i < p->p_types.nprim; i++) {
2536 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2540 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2541 rc = ebitmap_read(e, fp);
2545 /* add the type itself as the degenerate case */
2546 rc = ebitmap_set_bit(e, i, 1);
2551 rc = policydb_bounds_sanity_check(p);
2559 policydb_destroy(p);
2564 * Write a MLS level structure to a policydb binary
2565 * representation file.
2567 static int mls_write_level(struct mls_level *l, void *fp)
2572 buf[0] = cpu_to_le32(l->sens);
2573 rc = put_entry(buf, sizeof(u32), 1, fp);
2577 rc = ebitmap_write(&l->cat, fp);
2585 * Write a MLS range structure to a policydb binary
2586 * representation file.
2588 static int mls_write_range_helper(struct mls_range *r, void *fp)
2594 eq = mls_level_eq(&r->level[1], &r->level[0]);
2600 buf[0] = cpu_to_le32(items-1);
2601 buf[1] = cpu_to_le32(r->level[0].sens);
2603 buf[2] = cpu_to_le32(r->level[1].sens);
2605 BUG_ON(items > ARRAY_SIZE(buf));
2607 rc = put_entry(buf, sizeof(u32), items, fp);
2611 rc = ebitmap_write(&r->level[0].cat, fp);
2615 rc = ebitmap_write(&r->level[1].cat, fp);
2623 static int sens_write(void *vkey, void *datum, void *ptr)
2626 struct level_datum *levdatum = datum;
2627 struct policy_data *pd = ptr;
2634 buf[0] = cpu_to_le32(len);
2635 buf[1] = cpu_to_le32(levdatum->isalias);
2636 rc = put_entry(buf, sizeof(u32), 2, fp);
2640 rc = put_entry(key, 1, len, fp);
2644 rc = mls_write_level(levdatum->level, fp);
2651 static int cat_write(void *vkey, void *datum, void *ptr)
2654 struct cat_datum *catdatum = datum;
2655 struct policy_data *pd = ptr;
2662 buf[0] = cpu_to_le32(len);
2663 buf[1] = cpu_to_le32(catdatum->value);
2664 buf[2] = cpu_to_le32(catdatum->isalias);
2665 rc = put_entry(buf, sizeof(u32), 3, fp);
2669 rc = put_entry(key, 1, len, fp);
2676 static int role_trans_write(struct policydb *p, void *fp)
2678 struct role_trans *r = p->role_tr;
2679 struct role_trans *tr;
2685 for (tr = r; tr; tr = tr->next)
2687 buf[0] = cpu_to_le32(nel);
2688 rc = put_entry(buf, sizeof(u32), 1, fp);
2691 for (tr = r; tr; tr = tr->next) {
2692 buf[0] = cpu_to_le32(tr->role);
2693 buf[1] = cpu_to_le32(tr->type);
2694 buf[2] = cpu_to_le32(tr->new_role);
2695 rc = put_entry(buf, sizeof(u32), 3, fp);
2698 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2699 buf[0] = cpu_to_le32(tr->tclass);
2700 rc = put_entry(buf, sizeof(u32), 1, fp);
2709 static int role_allow_write(struct role_allow *r, void *fp)
2711 struct role_allow *ra;
2717 for (ra = r; ra; ra = ra->next)
2719 buf[0] = cpu_to_le32(nel);
2720 rc = put_entry(buf, sizeof(u32), 1, fp);
2723 for (ra = r; ra; ra = ra->next) {
2724 buf[0] = cpu_to_le32(ra->role);
2725 buf[1] = cpu_to_le32(ra->new_role);
2726 rc = put_entry(buf, sizeof(u32), 2, fp);
2734 * Write a security context structure
2735 * to a policydb binary representation file.
2737 static int context_write(struct policydb *p, struct context *c,
2743 buf[0] = cpu_to_le32(c->user);
2744 buf[1] = cpu_to_le32(c->role);
2745 buf[2] = cpu_to_le32(c->type);
2747 rc = put_entry(buf, sizeof(u32), 3, fp);
2751 rc = mls_write_range_helper(&c->range, fp);
2759 * The following *_write functions are used to
2760 * write the symbol data to a policy database
2761 * binary representation file.
2764 static int perm_write(void *vkey, void *datum, void *fp)
2767 struct perm_datum *perdatum = datum;
2773 buf[0] = cpu_to_le32(len);
2774 buf[1] = cpu_to_le32(perdatum->value);
2775 rc = put_entry(buf, sizeof(u32), 2, fp);
2779 rc = put_entry(key, 1, len, fp);
2786 static int common_write(void *vkey, void *datum, void *ptr)
2789 struct common_datum *comdatum = datum;
2790 struct policy_data *pd = ptr;
2797 buf[0] = cpu_to_le32(len);
2798 buf[1] = cpu_to_le32(comdatum->value);
2799 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2800 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2801 rc = put_entry(buf, sizeof(u32), 4, fp);
2805 rc = put_entry(key, 1, len, fp);
2809 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2816 static int type_set_write(struct type_set *t, void *fp)
2821 if (ebitmap_write(&t->types, fp))
2823 if (ebitmap_write(&t->negset, fp))
2826 buf[0] = cpu_to_le32(t->flags);
2827 rc = put_entry(buf, sizeof(u32), 1, fp);
2834 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2837 struct constraint_node *c;
2838 struct constraint_expr *e;
2843 for (c = node; c; c = c->next) {
2845 for (e = c->expr; e; e = e->next)
2847 buf[0] = cpu_to_le32(c->permissions);
2848 buf[1] = cpu_to_le32(nel);
2849 rc = put_entry(buf, sizeof(u32), 2, fp);
2852 for (e = c->expr; e; e = e->next) {
2853 buf[0] = cpu_to_le32(e->expr_type);
2854 buf[1] = cpu_to_le32(e->attr);
2855 buf[2] = cpu_to_le32(e->op);
2856 rc = put_entry(buf, sizeof(u32), 3, fp);
2860 switch (e->expr_type) {
2862 rc = ebitmap_write(&e->names, fp);
2865 if (p->policyvers >=
2866 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2867 rc = type_set_write(e->type_names, fp);
2881 static int class_write(void *vkey, void *datum, void *ptr)
2884 struct class_datum *cladatum = datum;
2885 struct policy_data *pd = ptr;
2887 struct policydb *p = pd->p;
2888 struct constraint_node *c;
2895 if (cladatum->comkey)
2896 len2 = strlen(cladatum->comkey);
2901 for (c = cladatum->constraints; c; c = c->next)
2904 buf[0] = cpu_to_le32(len);
2905 buf[1] = cpu_to_le32(len2);
2906 buf[2] = cpu_to_le32(cladatum->value);
2907 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2908 if (cladatum->permissions.table)
2909 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2912 buf[5] = cpu_to_le32(ncons);
2913 rc = put_entry(buf, sizeof(u32), 6, fp);
2917 rc = put_entry(key, 1, len, fp);
2921 if (cladatum->comkey) {
2922 rc = put_entry(cladatum->comkey, 1, len2, fp);
2927 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2931 rc = write_cons_helper(p, cladatum->constraints, fp);
2935 /* write out the validatetrans rule */
2937 for (c = cladatum->validatetrans; c; c = c->next)
2940 buf[0] = cpu_to_le32(ncons);
2941 rc = put_entry(buf, sizeof(u32), 1, fp);
2945 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2949 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2950 buf[0] = cpu_to_le32(cladatum->default_user);
2951 buf[1] = cpu_to_le32(cladatum->default_role);
2952 buf[2] = cpu_to_le32(cladatum->default_range);
2954 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2959 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2960 buf[0] = cpu_to_le32(cladatum->default_type);
2961 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2969 static int role_write(void *vkey, void *datum, void *ptr)
2972 struct role_datum *role = datum;
2973 struct policy_data *pd = ptr;
2975 struct policydb *p = pd->p;
2982 buf[items++] = cpu_to_le32(len);
2983 buf[items++] = cpu_to_le32(role->value);
2984 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2985 buf[items++] = cpu_to_le32(role->bounds);
2987 BUG_ON(items > ARRAY_SIZE(buf));
2989 rc = put_entry(buf, sizeof(u32), items, fp);
2993 rc = put_entry(key, 1, len, fp);
2997 rc = ebitmap_write(&role->dominates, fp);
3001 rc = ebitmap_write(&role->types, fp);
3008 static int type_write(void *vkey, void *datum, void *ptr)
3011 struct type_datum *typdatum = datum;
3012 struct policy_data *pd = ptr;
3013 struct policydb *p = pd->p;
3021 buf[items++] = cpu_to_le32(len);
3022 buf[items++] = cpu_to_le32(typdatum->value);
3023 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3026 if (typdatum->primary)
3027 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3029 if (typdatum->attribute)
3030 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3032 buf[items++] = cpu_to_le32(properties);
3033 buf[items++] = cpu_to_le32(typdatum->bounds);
3035 buf[items++] = cpu_to_le32(typdatum->primary);
3037 BUG_ON(items > ARRAY_SIZE(buf));
3038 rc = put_entry(buf, sizeof(u32), items, fp);
3042 rc = put_entry(key, 1, len, fp);
3049 static int user_write(void *vkey, void *datum, void *ptr)
3052 struct user_datum *usrdatum = datum;
3053 struct policy_data *pd = ptr;
3054 struct policydb *p = pd->p;
3062 buf[items++] = cpu_to_le32(len);
3063 buf[items++] = cpu_to_le32(usrdatum->value);
3064 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3065 buf[items++] = cpu_to_le32(usrdatum->bounds);
3066 BUG_ON(items > ARRAY_SIZE(buf));
3067 rc = put_entry(buf, sizeof(u32), items, fp);
3071 rc = put_entry(key, 1, len, fp);
3075 rc = ebitmap_write(&usrdatum->roles, fp);
3079 rc = mls_write_range_helper(&usrdatum->range, fp);
3083 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3090 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3103 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3106 unsigned int i, j, rc;
3111 for (i = 0; i < info->ocon_num; i++) {
3113 for (c = p->ocontexts[i]; c; c = c->next)
3115 buf[0] = cpu_to_le32(nel);
3116 rc = put_entry(buf, sizeof(u32), 1, fp);
3119 for (c = p->ocontexts[i]; c; c = c->next) {
3122 buf[0] = cpu_to_le32(c->sid[0]);
3123 rc = put_entry(buf, sizeof(u32), 1, fp);
3126 rc = context_write(p, &c->context[0], fp);
3132 len = strlen(c->u.name);
3133 buf[0] = cpu_to_le32(len);
3134 rc = put_entry(buf, sizeof(u32), 1, fp);
3137 rc = put_entry(c->u.name, 1, len, fp);
3140 rc = context_write(p, &c->context[0], fp);
3143 rc = context_write(p, &c->context[1], fp);
3148 buf[0] = cpu_to_le32(c->u.port.protocol);
3149 buf[1] = cpu_to_le32(c->u.port.low_port);
3150 buf[2] = cpu_to_le32(c->u.port.high_port);
3151 rc = put_entry(buf, sizeof(u32), 3, fp);
3154 rc = context_write(p, &c->context[0], fp);
3159 nodebuf[0] = c->u.node.addr; /* network order */
3160 nodebuf[1] = c->u.node.mask; /* network order */
3161 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3164 rc = context_write(p, &c->context[0], fp);
3169 buf[0] = cpu_to_le32(c->v.behavior);
3170 len = strlen(c->u.name);
3171 buf[1] = cpu_to_le32(len);
3172 rc = put_entry(buf, sizeof(u32), 2, fp);
3175 rc = put_entry(c->u.name, 1, len, fp);
3178 rc = context_write(p, &c->context[0], fp);
3183 for (j = 0; j < 4; j++)
3184 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3185 for (j = 0; j < 4; j++)
3186 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3187 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3190 rc = context_write(p, &c->context[0], fp);
3195 *((__be64 *)nodebuf) = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3197 nodebuf[2] = cpu_to_le32(c->u.ibpkey.low_pkey);
3198 nodebuf[3] = cpu_to_le32(c->u.ibpkey.high_pkey);
3200 rc = put_entry(nodebuf, sizeof(u32), 4, fp);
3203 rc = context_write(p, &c->context[0], fp);
3207 case OCON_IBENDPORT:
3208 len = strlen(c->u.ibendport.dev_name);
3209 buf[0] = cpu_to_le32(len);
3210 buf[1] = cpu_to_le32(c->u.ibendport.port);
3211 rc = put_entry(buf, sizeof(u32), 2, fp);
3214 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3217 rc = context_write(p, &c->context[0], fp);
3227 static int genfs_write(struct policydb *p, void *fp)
3229 struct genfs *genfs;
3236 for (genfs = p->genfs; genfs; genfs = genfs->next)
3238 buf[0] = cpu_to_le32(len);
3239 rc = put_entry(buf, sizeof(u32), 1, fp);
3242 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3243 len = strlen(genfs->fstype);
3244 buf[0] = cpu_to_le32(len);
3245 rc = put_entry(buf, sizeof(u32), 1, fp);
3248 rc = put_entry(genfs->fstype, 1, len, fp);
3252 for (c = genfs->head; c; c = c->next)
3254 buf[0] = cpu_to_le32(len);
3255 rc = put_entry(buf, sizeof(u32), 1, fp);
3258 for (c = genfs->head; c; c = c->next) {
3259 len = strlen(c->u.name);
3260 buf[0] = cpu_to_le32(len);
3261 rc = put_entry(buf, sizeof(u32), 1, fp);
3264 rc = put_entry(c->u.name, 1, len, fp);
3267 buf[0] = cpu_to_le32(c->v.sclass);
3268 rc = put_entry(buf, sizeof(u32), 1, fp);
3271 rc = context_write(p, &c->context[0], fp);
3279 static int hashtab_cnt(void *key, void *data, void *ptr)
3287 static int range_write_helper(void *key, void *data, void *ptr)
3290 struct range_trans *rt = key;
3291 struct mls_range *r = data;
3292 struct policy_data *pd = ptr;
3294 struct policydb *p = pd->p;
3297 buf[0] = cpu_to_le32(rt->source_type);
3298 buf[1] = cpu_to_le32(rt->target_type);
3299 rc = put_entry(buf, sizeof(u32), 2, fp);
3302 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3303 buf[0] = cpu_to_le32(rt->target_class);
3304 rc = put_entry(buf, sizeof(u32), 1, fp);
3308 rc = mls_write_range_helper(r, fp);
3315 static int range_write(struct policydb *p, void *fp)
3319 struct policy_data pd;
3324 /* count the number of entries in the hashtab */
3326 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3330 buf[0] = cpu_to_le32(nel);
3331 rc = put_entry(buf, sizeof(u32), 1, fp);
3335 /* actually write all of the entries */
3336 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3343 static int filename_write_helper(void *key, void *data, void *ptr)
3346 struct filename_trans *ft = key;
3347 struct filename_trans_datum *otype = data;
3352 len = strlen(ft->name);
3353 buf[0] = cpu_to_le32(len);
3354 rc = put_entry(buf, sizeof(u32), 1, fp);
3358 rc = put_entry(ft->name, sizeof(char), len, fp);
3362 buf[0] = cpu_to_le32(ft->stype);
3363 buf[1] = cpu_to_le32(ft->ttype);
3364 buf[2] = cpu_to_le32(ft->tclass);
3365 buf[3] = cpu_to_le32(otype->otype);
3367 rc = put_entry(buf, sizeof(u32), 4, fp);
3374 static int filename_trans_write(struct policydb *p, void *fp)
3380 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3384 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3388 buf[0] = cpu_to_le32(nel);
3389 rc = put_entry(buf, sizeof(u32), 1, fp);
3393 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3401 * Write the configuration data in a policy database
3402 * structure to a policy database binary representation
3405 int policydb_write(struct policydb *p, void *fp)
3407 unsigned int i, num_syms;
3412 struct policydb_compat_info *info;
3415 * refuse to write policy older than compressed avtab
3416 * to simplify the writer. There are other tests dropped
3417 * since we assume this throughout the writer code. Be
3418 * careful if you ever try to remove this restriction
3420 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3421 pr_err("SELinux: refusing to write policy version %d."
3422 " Because it is less than version %d\n", p->policyvers,
3423 POLICYDB_VERSION_AVTAB);
3429 config |= POLICYDB_CONFIG_MLS;
3431 if (p->reject_unknown)
3432 config |= REJECT_UNKNOWN;
3433 if (p->allow_unknown)
3434 config |= ALLOW_UNKNOWN;
3436 /* Write the magic number and string identifiers. */
3437 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3438 len = strlen(POLICYDB_STRING);
3439 buf[1] = cpu_to_le32(len);
3440 rc = put_entry(buf, sizeof(u32), 2, fp);
3443 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3447 /* Write the version, config, and table sizes. */
3448 info = policydb_lookup_compat(p->policyvers);
3450 pr_err("SELinux: compatibility lookup failed for policy "
3451 "version %d", p->policyvers);
3455 buf[0] = cpu_to_le32(p->policyvers);
3456 buf[1] = cpu_to_le32(config);
3457 buf[2] = cpu_to_le32(info->sym_num);
3458 buf[3] = cpu_to_le32(info->ocon_num);
3460 rc = put_entry(buf, sizeof(u32), 4, fp);
3464 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3465 rc = ebitmap_write(&p->policycaps, fp);
3470 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3471 rc = ebitmap_write(&p->permissive_map, fp);
3476 num_syms = info->sym_num;
3477 for (i = 0; i < num_syms; i++) {
3478 struct policy_data pd;
3483 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3484 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3486 rc = put_entry(buf, sizeof(u32), 2, fp);
3489 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3494 rc = avtab_write(p, &p->te_avtab, fp);
3498 rc = cond_write_list(p, p->cond_list, fp);
3502 rc = role_trans_write(p, fp);
3506 rc = role_allow_write(p->role_allow, fp);
3510 rc = filename_trans_write(p, fp);
3514 rc = ocontext_write(p, info, fp);
3518 rc = genfs_write(p, fp);
3522 rc = range_write(p, fp);
3526 for (i = 0; i < p->p_types.nprim; i++) {
3527 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3530 rc = ebitmap_write(e, fp);