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 struct policydb_compat_info {
65 /* These need to be updated if SYM_NUM or OCON_NUM changes */
66 static struct policydb_compat_info policydb_compat[] = {
68 .version = POLICYDB_VERSION_BASE,
69 .sym_num = SYM_NUM - 3,
70 .ocon_num = OCON_NUM - 3,
73 .version = POLICYDB_VERSION_BOOL,
74 .sym_num = SYM_NUM - 2,
75 .ocon_num = OCON_NUM - 3,
78 .version = POLICYDB_VERSION_IPV6,
79 .sym_num = SYM_NUM - 2,
80 .ocon_num = OCON_NUM - 2,
83 .version = POLICYDB_VERSION_NLCLASS,
84 .sym_num = SYM_NUM - 2,
85 .ocon_num = OCON_NUM - 2,
88 .version = POLICYDB_VERSION_MLS,
90 .ocon_num = OCON_NUM - 2,
93 .version = POLICYDB_VERSION_AVTAB,
95 .ocon_num = OCON_NUM - 2,
98 .version = POLICYDB_VERSION_RANGETRANS,
100 .ocon_num = OCON_NUM - 2,
103 .version = POLICYDB_VERSION_POLCAP,
105 .ocon_num = OCON_NUM - 2,
108 .version = POLICYDB_VERSION_PERMISSIVE,
110 .ocon_num = OCON_NUM - 2,
113 .version = POLICYDB_VERSION_BOUNDARY,
115 .ocon_num = OCON_NUM - 2,
118 .version = POLICYDB_VERSION_FILENAME_TRANS,
120 .ocon_num = OCON_NUM - 2,
123 .version = POLICYDB_VERSION_ROLETRANS,
125 .ocon_num = OCON_NUM - 2,
128 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
130 .ocon_num = OCON_NUM - 2,
133 .version = POLICYDB_VERSION_DEFAULT_TYPE,
135 .ocon_num = OCON_NUM - 2,
138 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
140 .ocon_num = OCON_NUM - 2,
143 .version = POLICYDB_VERSION_XPERMS_IOCTL,
145 .ocon_num = OCON_NUM - 2,
148 .version = POLICYDB_VERSION_INFINIBAND,
150 .ocon_num = OCON_NUM,
153 .version = POLICYDB_VERSION_GLBLUB,
155 .ocon_num = OCON_NUM,
159 static struct policydb_compat_info *policydb_lookup_compat(int version)
162 struct policydb_compat_info *info = NULL;
164 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
165 if (policydb_compat[i].version == version) {
166 info = &policydb_compat[i];
174 * The following *_destroy functions are used to
175 * free any memory allocated for each kind of
176 * symbol data in the policy database.
179 static int perm_destroy(void *key, void *datum, void *p)
186 static int common_destroy(void *key, void *datum, void *p)
188 struct common_datum *comdatum;
193 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
194 hashtab_destroy(comdatum->permissions.table);
200 static void constraint_expr_destroy(struct constraint_expr *expr)
203 ebitmap_destroy(&expr->names);
204 if (expr->type_names) {
205 ebitmap_destroy(&expr->type_names->types);
206 ebitmap_destroy(&expr->type_names->negset);
207 kfree(expr->type_names);
213 static int cls_destroy(void *key, void *datum, void *p)
215 struct class_datum *cladatum;
216 struct constraint_node *constraint, *ctemp;
217 struct constraint_expr *e, *etmp;
222 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
223 hashtab_destroy(cladatum->permissions.table);
224 constraint = cladatum->constraints;
226 e = constraint->expr;
230 constraint_expr_destroy(etmp);
233 constraint = constraint->next;
237 constraint = cladatum->validatetrans;
239 e = constraint->expr;
243 constraint_expr_destroy(etmp);
246 constraint = constraint->next;
249 kfree(cladatum->comkey);
255 static int role_destroy(void *key, void *datum, void *p)
257 struct role_datum *role;
262 ebitmap_destroy(&role->dominates);
263 ebitmap_destroy(&role->types);
269 static int type_destroy(void *key, void *datum, void *p)
276 static int user_destroy(void *key, void *datum, void *p)
278 struct user_datum *usrdatum;
283 ebitmap_destroy(&usrdatum->roles);
284 ebitmap_destroy(&usrdatum->range.level[0].cat);
285 ebitmap_destroy(&usrdatum->range.level[1].cat);
286 ebitmap_destroy(&usrdatum->dfltlevel.cat);
292 static int sens_destroy(void *key, void *datum, void *p)
294 struct level_datum *levdatum;
300 ebitmap_destroy(&levdatum->level->cat);
301 kfree(levdatum->level);
307 static int cat_destroy(void *key, void *datum, void *p)
314 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
326 static int filenametr_destroy(void *key, void *datum, void *p)
328 struct filename_trans_key *ft = key;
329 struct filename_trans_datum *next, *d = datum;
334 ebitmap_destroy(&d->stypes);
338 } while (unlikely(d));
343 static int range_tr_destroy(void *key, void *datum, void *p)
345 struct mls_range *rt = datum;
348 ebitmap_destroy(&rt->level[0].cat);
349 ebitmap_destroy(&rt->level[1].cat);
355 static void ocontext_destroy(struct ocontext *c, int i)
360 context_destroy(&c->context[0]);
361 context_destroy(&c->context[1]);
362 if (i == OCON_ISID || i == OCON_FS ||
363 i == OCON_NETIF || i == OCON_FSUSE)
369 * Initialize the role table.
371 static int roles_init(struct policydb *p)
375 struct role_datum *role;
377 role = kzalloc(sizeof(*role), GFP_KERNEL);
382 role->value = ++p->p_roles.nprim;
383 if (role->value != OBJECT_R_VAL)
387 key = kstrdup(OBJECT_R, GFP_KERNEL);
391 rc = hashtab_insert(p->p_roles.table, key, role);
402 static u32 filenametr_hash(struct hashtab *h, const void *k)
404 const struct filename_trans_key *ft = k;
406 unsigned int byte_num;
409 hash = ft->ttype ^ ft->tclass;
412 while ((focus = ft->name[byte_num++]))
413 hash = partial_name_hash(focus, hash);
414 return hash & (h->size - 1);
417 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
419 const struct filename_trans_key *ft1 = k1;
420 const struct filename_trans_key *ft2 = k2;
423 v = ft1->ttype - ft2->ttype;
427 v = ft1->tclass - ft2->tclass;
431 return strcmp(ft1->name, ft2->name);
435 static u32 rangetr_hash(struct hashtab *h, const void *k)
437 const struct range_trans *key = k;
439 return (key->source_type + (key->target_type << 3) +
440 (key->target_class << 5)) & (h->size - 1);
443 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
445 const struct range_trans *key1 = k1, *key2 = k2;
448 v = key1->source_type - key2->source_type;
452 v = key1->target_type - key2->target_type;
456 v = key1->target_class - key2->target_class;
462 * Initialize a policy database structure.
464 static int policydb_init(struct policydb *p)
466 memset(p, 0, sizeof(*p));
468 avtab_init(&p->te_avtab);
469 cond_policydb_init(p);
471 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
473 if (!p->filename_trans)
476 ebitmap_init(&p->filename_trans_ttypes);
477 ebitmap_init(&p->policycaps);
478 ebitmap_init(&p->permissive_map);
484 * The following *_index functions are used to
485 * define the val_to_name and val_to_struct arrays
486 * in a policy database structure. The val_to_name
487 * arrays are used when converting security context
488 * structures into string representations. The
489 * val_to_struct arrays are used when the attributes
490 * of a class, role, or user are needed.
493 static int common_index(void *key, void *datum, void *datap)
496 struct common_datum *comdatum;
500 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
503 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
508 static int class_index(void *key, void *datum, void *datap)
511 struct class_datum *cladatum;
515 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
518 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
519 p->class_val_to_struct[cladatum->value - 1] = cladatum;
523 static int role_index(void *key, void *datum, void *datap)
526 struct role_datum *role;
531 || role->value > p->p_roles.nprim
532 || role->bounds > p->p_roles.nprim)
535 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
536 p->role_val_to_struct[role->value - 1] = role;
540 static int type_index(void *key, void *datum, void *datap)
543 struct type_datum *typdatum;
548 if (typdatum->primary) {
550 || typdatum->value > p->p_types.nprim
551 || typdatum->bounds > p->p_types.nprim)
553 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
554 p->type_val_to_struct[typdatum->value - 1] = typdatum;
560 static int user_index(void *key, void *datum, void *datap)
563 struct user_datum *usrdatum;
568 || usrdatum->value > p->p_users.nprim
569 || usrdatum->bounds > p->p_users.nprim)
572 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
573 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
577 static int sens_index(void *key, void *datum, void *datap)
580 struct level_datum *levdatum;
585 if (!levdatum->isalias) {
586 if (!levdatum->level->sens ||
587 levdatum->level->sens > p->p_levels.nprim)
590 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
596 static int cat_index(void *key, void *datum, void *datap)
599 struct cat_datum *catdatum;
604 if (!catdatum->isalias) {
605 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
608 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
614 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
627 static void hash_eval(struct hashtab *h, const char *hash_name)
629 struct hashtab_info info;
631 hashtab_stat(h, &info);
632 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
633 hash_name, h->nel, info.slots_used, h->size,
637 static void symtab_hash_eval(struct symtab *s)
641 for (i = 0; i < SYM_NUM; i++)
642 hash_eval(s[i].table, symtab_name[i]);
646 static inline void hash_eval(struct hashtab *h, char *hash_name)
652 * Define the other val_to_name and val_to_struct arrays
653 * in a policy database structure.
655 * Caller must clean up on failure.
657 static int policydb_index(struct policydb *p)
662 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
663 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
664 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
666 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
667 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
670 pr_debug("SELinux: %d classes, %d rules\n",
671 p->p_classes.nprim, p->te_avtab.nel);
674 avtab_hash_eval(&p->te_avtab, "rules");
675 symtab_hash_eval(p->symtab);
678 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
679 sizeof(*p->class_val_to_struct),
681 if (!p->class_val_to_struct)
684 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
685 sizeof(*p->role_val_to_struct),
687 if (!p->role_val_to_struct)
690 p->user_val_to_struct = kcalloc(p->p_users.nprim,
691 sizeof(*p->user_val_to_struct),
693 if (!p->user_val_to_struct)
696 p->type_val_to_struct = kvcalloc(p->p_types.nprim,
697 sizeof(*p->type_val_to_struct),
699 if (!p->type_val_to_struct)
702 rc = cond_init_bool_indexes(p);
706 for (i = 0; i < SYM_NUM; i++) {
707 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
710 if (!p->sym_val_to_name[i])
713 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
723 * Free any memory allocated by a policy database structure.
725 void policydb_destroy(struct policydb *p)
727 struct ocontext *c, *ctmp;
728 struct genfs *g, *gtmp;
730 struct role_allow *ra, *lra = NULL;
731 struct role_trans *tr, *ltr = NULL;
733 for (i = 0; i < SYM_NUM; i++) {
735 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
736 hashtab_destroy(p->symtab[i].table);
739 for (i = 0; i < SYM_NUM; i++)
740 kvfree(p->sym_val_to_name[i]);
742 kfree(p->class_val_to_struct);
743 kfree(p->role_val_to_struct);
744 kfree(p->user_val_to_struct);
745 kvfree(p->type_val_to_struct);
747 avtab_destroy(&p->te_avtab);
749 for (i = 0; i < OCON_NUM; i++) {
755 ocontext_destroy(ctmp, i);
757 p->ocontexts[i] = NULL;
768 ocontext_destroy(ctmp, OCON_FSUSE);
776 cond_policydb_destroy(p);
778 for (tr = p->role_tr; tr; tr = tr->next) {
785 for (ra = p->role_allow; ra; ra = ra->next) {
792 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
793 hashtab_destroy(p->filename_trans);
795 hashtab_map(p->range_tr, range_tr_destroy, NULL);
796 hashtab_destroy(p->range_tr);
798 if (p->type_attr_map_array) {
799 for (i = 0; i < p->p_types.nprim; i++)
800 ebitmap_destroy(&p->type_attr_map_array[i]);
801 kvfree(p->type_attr_map_array);
804 ebitmap_destroy(&p->filename_trans_ttypes);
805 ebitmap_destroy(&p->policycaps);
806 ebitmap_destroy(&p->permissive_map);
810 * Load the initial SIDs specified in a policy database
811 * structure into a SID table.
813 int policydb_load_isids(struct policydb *p, struct sidtab *s)
815 struct ocontext *head, *c;
820 pr_err("SELinux: out of memory on SID table init\n");
824 head = p->ocontexts[OCON_ISID];
825 for (c = head; c; c = c->next) {
827 const char *name = security_get_initial_sid_context(sid);
829 if (sid == SECSID_NULL) {
830 pr_err("SELinux: SID 0 was assigned a context.\n");
835 /* Ignore initial SIDs unused by this kernel. */
839 rc = context_add_hash(p, &c->context[0]);
844 rc = sidtab_set_initial(s, sid, &c->context[0]);
846 pr_err("SELinux: unable to load initial SID %s.\n",
857 int policydb_class_isvalid(struct policydb *p, unsigned int class)
859 if (!class || class > p->p_classes.nprim)
864 int policydb_role_isvalid(struct policydb *p, unsigned int role)
866 if (!role || role > p->p_roles.nprim)
871 int policydb_type_isvalid(struct policydb *p, unsigned int type)
873 if (!type || type > p->p_types.nprim)
879 * Return 1 if the fields in the security context
880 * structure `c' are valid. Return 0 otherwise.
882 int policydb_context_isvalid(struct policydb *p, struct context *c)
884 struct role_datum *role;
885 struct user_datum *usrdatum;
887 if (!c->role || c->role > p->p_roles.nprim)
890 if (!c->user || c->user > p->p_users.nprim)
893 if (!c->type || c->type > p->p_types.nprim)
896 if (c->role != OBJECT_R_VAL) {
898 * Role must be authorized for the type.
900 role = p->role_val_to_struct[c->role - 1];
901 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
902 /* role may not be associated with type */
906 * User must be authorized for the role.
908 usrdatum = p->user_val_to_struct[c->user - 1];
912 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
913 /* user may not be associated with role */
917 if (!mls_context_isvalid(p, c))
924 * Read a MLS range structure from a policydb binary
925 * representation file.
927 static int mls_read_range_helper(struct mls_range *r, void *fp)
933 rc = next_entry(buf, fp, sizeof(u32));
938 items = le32_to_cpu(buf[0]);
939 if (items > ARRAY_SIZE(buf)) {
940 pr_err("SELinux: mls: range overflow\n");
944 rc = next_entry(buf, fp, sizeof(u32) * items);
946 pr_err("SELinux: mls: truncated range\n");
950 r->level[0].sens = le32_to_cpu(buf[0]);
952 r->level[1].sens = le32_to_cpu(buf[1]);
954 r->level[1].sens = r->level[0].sens;
956 rc = ebitmap_read(&r->level[0].cat, fp);
958 pr_err("SELinux: mls: error reading low categories\n");
962 rc = ebitmap_read(&r->level[1].cat, fp);
964 pr_err("SELinux: mls: error reading high categories\n");
968 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
970 pr_err("SELinux: mls: out of memory\n");
977 ebitmap_destroy(&r->level[0].cat);
983 * Read and validate a security context structure
984 * from a policydb binary representation file.
986 static int context_read_and_validate(struct context *c,
993 rc = next_entry(buf, fp, sizeof buf);
995 pr_err("SELinux: context truncated\n");
998 c->user = le32_to_cpu(buf[0]);
999 c->role = le32_to_cpu(buf[1]);
1000 c->type = le32_to_cpu(buf[2]);
1001 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1002 rc = mls_read_range_helper(&c->range, fp);
1004 pr_err("SELinux: error reading MLS range of context\n");
1010 if (!policydb_context_isvalid(p, c)) {
1011 pr_err("SELinux: invalid security context\n");
1021 * The following *_read functions are used to
1022 * read the symbol data from a policy database
1023 * binary representation file.
1026 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1031 if ((len == 0) || (len == (u32)-1))
1034 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1038 rc = next_entry(str, fp, len);
1049 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1052 struct perm_datum *perdatum;
1057 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1061 rc = next_entry(buf, fp, sizeof buf);
1065 len = le32_to_cpu(buf[0]);
1066 perdatum->value = le32_to_cpu(buf[1]);
1068 rc = str_read(&key, GFP_KERNEL, fp, len);
1072 rc = hashtab_insert(h, key, perdatum);
1078 perm_destroy(key, perdatum, NULL);
1082 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1085 struct common_datum *comdatum;
1090 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1094 rc = next_entry(buf, fp, sizeof buf);
1098 len = le32_to_cpu(buf[0]);
1099 comdatum->value = le32_to_cpu(buf[1]);
1100 nel = le32_to_cpu(buf[3]);
1102 rc = symtab_init(&comdatum->permissions, nel);
1105 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1107 rc = str_read(&key, GFP_KERNEL, fp, len);
1111 for (i = 0; i < nel; i++) {
1112 rc = perm_read(p, comdatum->permissions.table, fp);
1117 rc = hashtab_insert(h, key, comdatum);
1122 common_destroy(key, comdatum, NULL);
1126 static void type_set_init(struct type_set *t)
1128 ebitmap_init(&t->types);
1129 ebitmap_init(&t->negset);
1132 static int type_set_read(struct type_set *t, void *fp)
1137 if (ebitmap_read(&t->types, fp))
1139 if (ebitmap_read(&t->negset, fp))
1142 rc = next_entry(buf, fp, sizeof(u32));
1145 t->flags = le32_to_cpu(buf[0]);
1151 static int read_cons_helper(struct policydb *p,
1152 struct constraint_node **nodep,
1153 int ncons, int allowxtarget, void *fp)
1155 struct constraint_node *c, *lc;
1156 struct constraint_expr *e, *le;
1159 int rc, i, j, depth;
1162 for (i = 0; i < ncons; i++) {
1163 c = kzalloc(sizeof(*c), GFP_KERNEL);
1172 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1175 c->permissions = le32_to_cpu(buf[0]);
1176 nexpr = le32_to_cpu(buf[1]);
1179 for (j = 0; j < nexpr; j++) {
1180 e = kzalloc(sizeof(*e), GFP_KERNEL);
1189 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1192 e->expr_type = le32_to_cpu(buf[0]);
1193 e->attr = le32_to_cpu(buf[1]);
1194 e->op = le32_to_cpu(buf[2]);
1196 switch (e->expr_type) {
1208 if (depth == (CEXPR_MAXDEPTH - 1))
1213 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1215 if (depth == (CEXPR_MAXDEPTH - 1))
1218 rc = ebitmap_read(&e->names, fp);
1221 if (p->policyvers >=
1222 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1223 e->type_names = kzalloc(sizeof
1224 (*e->type_names), GFP_KERNEL);
1227 type_set_init(e->type_names);
1228 rc = type_set_read(e->type_names, fp);
1246 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1249 struct class_datum *cladatum;
1251 u32 len, len2, ncons, nel;
1254 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1258 rc = next_entry(buf, fp, sizeof(u32)*6);
1262 len = le32_to_cpu(buf[0]);
1263 len2 = le32_to_cpu(buf[1]);
1264 cladatum->value = le32_to_cpu(buf[2]);
1265 nel = le32_to_cpu(buf[4]);
1267 rc = symtab_init(&cladatum->permissions, nel);
1270 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1272 ncons = le32_to_cpu(buf[5]);
1274 rc = str_read(&key, GFP_KERNEL, fp, len);
1279 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1284 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1285 if (!cladatum->comdatum) {
1286 pr_err("SELinux: unknown common %s\n",
1291 for (i = 0; i < nel; i++) {
1292 rc = perm_read(p, cladatum->permissions.table, fp);
1297 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1301 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1302 /* grab the validatetrans rules */
1303 rc = next_entry(buf, fp, sizeof(u32));
1306 ncons = le32_to_cpu(buf[0]);
1307 rc = read_cons_helper(p, &cladatum->validatetrans,
1313 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1314 rc = next_entry(buf, fp, sizeof(u32) * 3);
1318 cladatum->default_user = le32_to_cpu(buf[0]);
1319 cladatum->default_role = le32_to_cpu(buf[1]);
1320 cladatum->default_range = le32_to_cpu(buf[2]);
1323 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1324 rc = next_entry(buf, fp, sizeof(u32) * 1);
1327 cladatum->default_type = le32_to_cpu(buf[0]);
1330 rc = hashtab_insert(h, key, cladatum);
1336 cls_destroy(key, cladatum, NULL);
1340 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1343 struct role_datum *role;
1344 int rc, to_read = 2;
1348 role = kzalloc(sizeof(*role), GFP_KERNEL);
1352 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1355 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1359 len = le32_to_cpu(buf[0]);
1360 role->value = le32_to_cpu(buf[1]);
1361 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1362 role->bounds = le32_to_cpu(buf[2]);
1364 rc = str_read(&key, GFP_KERNEL, fp, len);
1368 rc = ebitmap_read(&role->dominates, fp);
1372 rc = ebitmap_read(&role->types, fp);
1376 if (strcmp(key, OBJECT_R) == 0) {
1378 if (role->value != OBJECT_R_VAL) {
1379 pr_err("SELinux: Role %s has wrong value %d\n",
1380 OBJECT_R, role->value);
1387 rc = hashtab_insert(h, key, role);
1392 role_destroy(key, role, NULL);
1396 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1399 struct type_datum *typdatum;
1400 int rc, to_read = 3;
1404 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1408 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1411 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1415 len = le32_to_cpu(buf[0]);
1416 typdatum->value = le32_to_cpu(buf[1]);
1417 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1418 u32 prop = le32_to_cpu(buf[2]);
1420 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1421 typdatum->primary = 1;
1422 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1423 typdatum->attribute = 1;
1425 typdatum->bounds = le32_to_cpu(buf[3]);
1427 typdatum->primary = le32_to_cpu(buf[2]);
1430 rc = str_read(&key, GFP_KERNEL, fp, len);
1434 rc = hashtab_insert(h, key, typdatum);
1439 type_destroy(key, typdatum, NULL);
1445 * Read a MLS level structure from a policydb binary
1446 * representation file.
1448 static int mls_read_level(struct mls_level *lp, void *fp)
1453 memset(lp, 0, sizeof(*lp));
1455 rc = next_entry(buf, fp, sizeof buf);
1457 pr_err("SELinux: mls: truncated level\n");
1460 lp->sens = le32_to_cpu(buf[0]);
1462 rc = ebitmap_read(&lp->cat, fp);
1464 pr_err("SELinux: mls: error reading level categories\n");
1470 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1473 struct user_datum *usrdatum;
1474 int rc, to_read = 2;
1478 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1482 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1485 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1489 len = le32_to_cpu(buf[0]);
1490 usrdatum->value = le32_to_cpu(buf[1]);
1491 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1492 usrdatum->bounds = le32_to_cpu(buf[2]);
1494 rc = str_read(&key, GFP_KERNEL, fp, len);
1498 rc = ebitmap_read(&usrdatum->roles, fp);
1502 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1503 rc = mls_read_range_helper(&usrdatum->range, fp);
1506 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1511 rc = hashtab_insert(h, key, usrdatum);
1516 user_destroy(key, usrdatum, NULL);
1520 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1523 struct level_datum *levdatum;
1528 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1532 rc = next_entry(buf, fp, sizeof buf);
1536 len = le32_to_cpu(buf[0]);
1537 levdatum->isalias = le32_to_cpu(buf[1]);
1539 rc = str_read(&key, GFP_ATOMIC, fp, len);
1544 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1545 if (!levdatum->level)
1548 rc = mls_read_level(levdatum->level, fp);
1552 rc = hashtab_insert(h, key, levdatum);
1557 sens_destroy(key, levdatum, NULL);
1561 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1564 struct cat_datum *catdatum;
1569 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1573 rc = next_entry(buf, fp, sizeof buf);
1577 len = le32_to_cpu(buf[0]);
1578 catdatum->value = le32_to_cpu(buf[1]);
1579 catdatum->isalias = le32_to_cpu(buf[2]);
1581 rc = str_read(&key, GFP_ATOMIC, fp, len);
1585 rc = hashtab_insert(h, key, catdatum);
1590 cat_destroy(key, catdatum, NULL);
1594 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1606 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1608 struct user_datum *upper, *user;
1609 struct policydb *p = datap;
1612 upper = user = datum;
1613 while (upper->bounds) {
1614 struct ebitmap_node *node;
1617 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1618 pr_err("SELinux: user %s: "
1619 "too deep or looped boundary",
1624 upper = p->user_val_to_struct[upper->bounds - 1];
1625 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1626 if (ebitmap_get_bit(&upper->roles, bit))
1629 pr_err("SELinux: boundary violated policy: "
1630 "user=%s role=%s bounds=%s\n",
1631 sym_name(p, SYM_USERS, user->value - 1),
1632 sym_name(p, SYM_ROLES, bit),
1633 sym_name(p, SYM_USERS, upper->value - 1));
1642 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1644 struct role_datum *upper, *role;
1645 struct policydb *p = datap;
1648 upper = role = datum;
1649 while (upper->bounds) {
1650 struct ebitmap_node *node;
1653 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1654 pr_err("SELinux: role %s: "
1655 "too deep or looped bounds\n",
1660 upper = p->role_val_to_struct[upper->bounds - 1];
1661 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1662 if (ebitmap_get_bit(&upper->types, bit))
1665 pr_err("SELinux: boundary violated policy: "
1666 "role=%s type=%s bounds=%s\n",
1667 sym_name(p, SYM_ROLES, role->value - 1),
1668 sym_name(p, SYM_TYPES, bit),
1669 sym_name(p, SYM_ROLES, upper->value - 1));
1678 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1680 struct type_datum *upper;
1681 struct policydb *p = datap;
1685 while (upper->bounds) {
1686 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1687 pr_err("SELinux: type %s: "
1688 "too deep or looped boundary\n",
1693 upper = p->type_val_to_struct[upper->bounds - 1];
1696 if (upper->attribute) {
1697 pr_err("SELinux: type %s: "
1698 "bounded by attribute %s",
1700 sym_name(p, SYM_TYPES, upper->value - 1));
1708 static int policydb_bounds_sanity_check(struct policydb *p)
1712 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1715 rc = hashtab_map(p->p_users.table,
1716 user_bounds_sanity_check, p);
1720 rc = hashtab_map(p->p_roles.table,
1721 role_bounds_sanity_check, p);
1725 rc = hashtab_map(p->p_types.table,
1726 type_bounds_sanity_check, p);
1733 u16 string_to_security_class(struct policydb *p, const char *name)
1735 struct class_datum *cladatum;
1737 cladatum = hashtab_search(p->p_classes.table, name);
1741 return cladatum->value;
1744 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1746 struct class_datum *cladatum;
1747 struct perm_datum *perdatum = NULL;
1748 struct common_datum *comdatum;
1750 if (!tclass || tclass > p->p_classes.nprim)
1753 cladatum = p->class_val_to_struct[tclass-1];
1754 comdatum = cladatum->comdatum;
1756 perdatum = hashtab_search(comdatum->permissions.table,
1759 perdatum = hashtab_search(cladatum->permissions.table,
1764 return 1U << (perdatum->value-1);
1767 static int range_read(struct policydb *p, void *fp)
1769 struct range_trans *rt = NULL;
1770 struct mls_range *r = NULL;
1775 if (p->policyvers < POLICYDB_VERSION_MLS)
1778 rc = next_entry(buf, fp, sizeof(u32));
1782 nel = le32_to_cpu(buf[0]);
1784 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, nel);
1788 for (i = 0; i < nel; i++) {
1790 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1794 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1798 rt->source_type = le32_to_cpu(buf[0]);
1799 rt->target_type = le32_to_cpu(buf[1]);
1800 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1801 rc = next_entry(buf, fp, sizeof(u32));
1804 rt->target_class = le32_to_cpu(buf[0]);
1806 rt->target_class = p->process_class;
1809 if (!policydb_type_isvalid(p, rt->source_type) ||
1810 !policydb_type_isvalid(p, rt->target_type) ||
1811 !policydb_class_isvalid(p, rt->target_class))
1815 r = kzalloc(sizeof(*r), GFP_KERNEL);
1819 rc = mls_read_range_helper(r, fp);
1824 if (!mls_range_isvalid(p, r)) {
1825 pr_warn("SELinux: rangetrans: invalid range\n");
1829 rc = hashtab_insert(p->range_tr, rt, r);
1836 hash_eval(p->range_tr, "rangetr");
1844 static int filename_trans_read_one(struct policydb *p, void *fp)
1846 struct filename_trans_key key, *ft = NULL;
1847 struct filename_trans_datum *last, *datum = NULL;
1849 u32 len, stype, otype;
1853 /* length of the path component string */
1854 rc = next_entry(buf, fp, sizeof(u32));
1857 len = le32_to_cpu(buf[0]);
1859 /* path component string */
1860 rc = str_read(&name, GFP_KERNEL, fp, len);
1864 rc = next_entry(buf, fp, sizeof(u32) * 4);
1868 stype = le32_to_cpu(buf[0]);
1869 key.ttype = le32_to_cpu(buf[1]);
1870 key.tclass = le32_to_cpu(buf[2]);
1873 otype = le32_to_cpu(buf[3]);
1876 datum = hashtab_search(p->filename_trans, &key);
1878 if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
1879 /* conflicting/duplicate rules are ignored */
1883 if (likely(datum->otype == otype))
1886 datum = datum->next;
1890 datum = kmalloc(sizeof(*datum), GFP_KERNEL);
1894 ebitmap_init(&datum->stypes);
1895 datum->otype = otype;
1898 if (unlikely(last)) {
1902 ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
1906 rc = hashtab_insert(p->filename_trans, ft, datum);
1911 rc = ebitmap_set_bit(&p->filename_trans_ttypes,
1918 return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
1927 static int filename_trans_read(struct policydb *p, void *fp)
1933 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1936 rc = next_entry(buf, fp, sizeof(u32));
1939 nel = le32_to_cpu(buf[0]);
1941 p->filename_trans_count = nel;
1943 for (i = 0; i < nel; i++) {
1944 rc = filename_trans_read_one(p, fp);
1948 hash_eval(p->filename_trans, "filenametr");
1952 static int genfs_read(struct policydb *p, void *fp)
1955 u32 nel, nel2, len, len2;
1957 struct ocontext *l, *c;
1958 struct ocontext *newc = NULL;
1959 struct genfs *genfs_p, *genfs;
1960 struct genfs *newgenfs = NULL;
1962 rc = next_entry(buf, fp, sizeof(u32));
1965 nel = le32_to_cpu(buf[0]);
1967 for (i = 0; i < nel; i++) {
1968 rc = next_entry(buf, fp, sizeof(u32));
1971 len = le32_to_cpu(buf[0]);
1974 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1978 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
1982 for (genfs_p = NULL, genfs = p->genfs; genfs;
1983 genfs_p = genfs, genfs = genfs->next) {
1985 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1986 pr_err("SELinux: dup genfs fstype %s\n",
1990 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1993 newgenfs->next = genfs;
1995 genfs_p->next = newgenfs;
1997 p->genfs = newgenfs;
2001 rc = next_entry(buf, fp, sizeof(u32));
2005 nel2 = le32_to_cpu(buf[0]);
2006 for (j = 0; j < nel2; j++) {
2007 rc = next_entry(buf, fp, sizeof(u32));
2010 len = le32_to_cpu(buf[0]);
2013 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2017 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2021 rc = next_entry(buf, fp, sizeof(u32));
2025 newc->v.sclass = le32_to_cpu(buf[0]);
2026 rc = context_read_and_validate(&newc->context[0], p, fp);
2030 for (l = NULL, c = genfs->head; c;
2031 l = c, c = c->next) {
2033 if (!strcmp(newc->u.name, c->u.name) &&
2034 (!c->v.sclass || !newc->v.sclass ||
2035 newc->v.sclass == c->v.sclass)) {
2036 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2037 genfs->fstype, c->u.name);
2040 len = strlen(newc->u.name);
2041 len2 = strlen(c->u.name);
2057 kfree(newgenfs->fstype);
2060 ocontext_destroy(newc, OCON_FSUSE);
2065 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2070 __be64 prefixbuf[1];
2072 struct ocontext *l, *c;
2075 for (i = 0; i < info->ocon_num; i++) {
2076 rc = next_entry(buf, fp, sizeof(u32));
2079 nel = le32_to_cpu(buf[0]);
2082 for (j = 0; j < nel; j++) {
2084 c = kzalloc(sizeof(*c), GFP_KERNEL);
2090 p->ocontexts[i] = c;
2095 rc = next_entry(buf, fp, sizeof(u32));
2099 c->sid[0] = le32_to_cpu(buf[0]);
2100 rc = context_read_and_validate(&c->context[0], p, fp);
2106 rc = next_entry(buf, fp, sizeof(u32));
2109 len = le32_to_cpu(buf[0]);
2111 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2115 rc = context_read_and_validate(&c->context[0], p, fp);
2118 rc = context_read_and_validate(&c->context[1], p, fp);
2123 rc = next_entry(buf, fp, sizeof(u32)*3);
2126 c->u.port.protocol = le32_to_cpu(buf[0]);
2127 c->u.port.low_port = le32_to_cpu(buf[1]);
2128 c->u.port.high_port = le32_to_cpu(buf[2]);
2129 rc = context_read_and_validate(&c->context[0], p, fp);
2134 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2137 c->u.node.addr = nodebuf[0]; /* network order */
2138 c->u.node.mask = nodebuf[1]; /* network order */
2139 rc = context_read_and_validate(&c->context[0], p, fp);
2144 rc = next_entry(buf, fp, sizeof(u32)*2);
2149 c->v.behavior = le32_to_cpu(buf[0]);
2150 /* Determined at runtime, not in policy DB. */
2151 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2153 if (c->v.behavior > SECURITY_FS_USE_MAX)
2156 len = le32_to_cpu(buf[1]);
2157 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2161 rc = context_read_and_validate(&c->context[0], p, fp);
2168 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2171 for (k = 0; k < 4; k++)
2172 c->u.node6.addr[k] = nodebuf[k];
2173 for (k = 0; k < 4; k++)
2174 c->u.node6.mask[k] = nodebuf[k+4];
2175 rc = context_read_and_validate(&c->context[0], p, fp);
2181 u32 pkey_lo, pkey_hi;
2183 rc = next_entry(prefixbuf, fp, sizeof(u64));
2187 /* we need to have subnet_prefix in CPU order */
2188 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2190 rc = next_entry(buf, fp, sizeof(u32) * 2);
2194 pkey_lo = le32_to_cpu(buf[0]);
2195 pkey_hi = le32_to_cpu(buf[1]);
2197 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2202 c->u.ibpkey.low_pkey = pkey_lo;
2203 c->u.ibpkey.high_pkey = pkey_hi;
2205 rc = context_read_and_validate(&c->context[0],
2212 case OCON_IBENDPORT: {
2215 rc = next_entry(buf, fp, sizeof(u32) * 2);
2218 len = le32_to_cpu(buf[0]);
2220 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2224 port = le32_to_cpu(buf[1]);
2225 if (port > U8_MAX || port == 0) {
2230 c->u.ibendport.port = port;
2232 rc = context_read_and_validate(&c->context[0],
2248 * Read the configuration data from a policy database binary
2249 * representation file into a policy database structure.
2251 int policydb_read(struct policydb *p, void *fp)
2253 struct role_allow *ra, *lra;
2254 struct role_trans *tr, *ltr;
2257 u32 len, nprim, nel;
2260 struct policydb_compat_info *info;
2262 rc = policydb_init(p);
2266 /* Read the magic number and string length. */
2267 rc = next_entry(buf, fp, sizeof(u32) * 2);
2272 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2273 pr_err("SELinux: policydb magic number 0x%x does "
2274 "not match expected magic number 0x%x\n",
2275 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2280 len = le32_to_cpu(buf[1]);
2281 if (len != strlen(POLICYDB_STRING)) {
2282 pr_err("SELinux: policydb string length %d does not "
2283 "match expected length %zu\n",
2284 len, strlen(POLICYDB_STRING));
2289 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2290 if (!policydb_str) {
2291 pr_err("SELinux: unable to allocate memory for policydb "
2292 "string of length %d\n", len);
2296 rc = next_entry(policydb_str, fp, len);
2298 pr_err("SELinux: truncated policydb string identifier\n");
2299 kfree(policydb_str);
2304 policydb_str[len] = '\0';
2305 if (strcmp(policydb_str, POLICYDB_STRING)) {
2306 pr_err("SELinux: policydb string %s does not match "
2307 "my string %s\n", policydb_str, POLICYDB_STRING);
2308 kfree(policydb_str);
2311 /* Done with policydb_str. */
2312 kfree(policydb_str);
2313 policydb_str = NULL;
2315 /* Read the version and table sizes. */
2316 rc = next_entry(buf, fp, sizeof(u32)*4);
2321 p->policyvers = le32_to_cpu(buf[0]);
2322 if (p->policyvers < POLICYDB_VERSION_MIN ||
2323 p->policyvers > POLICYDB_VERSION_MAX) {
2324 pr_err("SELinux: policydb version %d does not match "
2325 "my version range %d-%d\n",
2326 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2330 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2334 if (p->policyvers < POLICYDB_VERSION_MLS) {
2335 pr_err("SELinux: security policydb version %d "
2336 "(MLS) not backwards compatible\n",
2341 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2342 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2344 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2345 rc = ebitmap_read(&p->policycaps, fp);
2350 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2351 rc = ebitmap_read(&p->permissive_map, fp);
2357 info = policydb_lookup_compat(p->policyvers);
2359 pr_err("SELinux: unable to find policy compat info "
2360 "for version %d\n", p->policyvers);
2365 if (le32_to_cpu(buf[2]) != info->sym_num ||
2366 le32_to_cpu(buf[3]) != info->ocon_num) {
2367 pr_err("SELinux: policydb table sizes (%d,%d) do "
2368 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2369 le32_to_cpu(buf[3]),
2370 info->sym_num, info->ocon_num);
2374 for (i = 0; i < info->sym_num; i++) {
2375 rc = next_entry(buf, fp, sizeof(u32)*2);
2378 nprim = le32_to_cpu(buf[0]);
2379 nel = le32_to_cpu(buf[1]);
2381 rc = symtab_init(&p->symtab[i], nel);
2385 if (i == SYM_ROLES) {
2391 for (j = 0; j < nel; j++) {
2392 rc = read_f[i](p, p->symtab[i].table, fp);
2397 p->symtab[i].nprim = nprim;
2401 p->process_class = string_to_security_class(p, "process");
2402 if (!p->process_class)
2405 rc = avtab_read(&p->te_avtab, fp, p);
2409 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2410 rc = cond_read_list(p, fp);
2415 rc = next_entry(buf, fp, sizeof(u32));
2418 nel = le32_to_cpu(buf[0]);
2420 for (i = 0; i < nel; i++) {
2422 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2429 rc = next_entry(buf, fp, sizeof(u32)*3);
2434 tr->role = le32_to_cpu(buf[0]);
2435 tr->type = le32_to_cpu(buf[1]);
2436 tr->new_role = le32_to_cpu(buf[2]);
2437 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2438 rc = next_entry(buf, fp, sizeof(u32));
2441 tr->tclass = le32_to_cpu(buf[0]);
2443 tr->tclass = p->process_class;
2446 if (!policydb_role_isvalid(p, tr->role) ||
2447 !policydb_type_isvalid(p, tr->type) ||
2448 !policydb_class_isvalid(p, tr->tclass) ||
2449 !policydb_role_isvalid(p, tr->new_role))
2454 rc = next_entry(buf, fp, sizeof(u32));
2457 nel = le32_to_cpu(buf[0]);
2459 for (i = 0; i < nel; i++) {
2461 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2468 rc = next_entry(buf, fp, sizeof(u32)*2);
2473 ra->role = le32_to_cpu(buf[0]);
2474 ra->new_role = le32_to_cpu(buf[1]);
2475 if (!policydb_role_isvalid(p, ra->role) ||
2476 !policydb_role_isvalid(p, ra->new_role))
2481 rc = filename_trans_read(p, fp);
2485 rc = policydb_index(p);
2490 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2491 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2492 if (!p->process_trans_perms)
2495 rc = ocontext_read(p, info, fp);
2499 rc = genfs_read(p, fp);
2503 rc = range_read(p, fp);
2507 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2508 sizeof(*p->type_attr_map_array),
2510 if (!p->type_attr_map_array)
2513 /* just in case ebitmap_init() becomes more than just a memset(0): */
2514 for (i = 0; i < p->p_types.nprim; i++)
2515 ebitmap_init(&p->type_attr_map_array[i]);
2517 for (i = 0; i < p->p_types.nprim; i++) {
2518 struct ebitmap *e = &p->type_attr_map_array[i];
2520 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2521 rc = ebitmap_read(e, fp);
2525 /* add the type itself as the degenerate case */
2526 rc = ebitmap_set_bit(e, i, 1);
2531 rc = policydb_bounds_sanity_check(p);
2539 policydb_destroy(p);
2544 * Write a MLS level structure to a policydb binary
2545 * representation file.
2547 static int mls_write_level(struct mls_level *l, void *fp)
2552 buf[0] = cpu_to_le32(l->sens);
2553 rc = put_entry(buf, sizeof(u32), 1, fp);
2557 rc = ebitmap_write(&l->cat, fp);
2565 * Write a MLS range structure to a policydb binary
2566 * representation file.
2568 static int mls_write_range_helper(struct mls_range *r, void *fp)
2574 eq = mls_level_eq(&r->level[1], &r->level[0]);
2580 buf[0] = cpu_to_le32(items-1);
2581 buf[1] = cpu_to_le32(r->level[0].sens);
2583 buf[2] = cpu_to_le32(r->level[1].sens);
2585 BUG_ON(items > ARRAY_SIZE(buf));
2587 rc = put_entry(buf, sizeof(u32), items, fp);
2591 rc = ebitmap_write(&r->level[0].cat, fp);
2595 rc = ebitmap_write(&r->level[1].cat, fp);
2603 static int sens_write(void *vkey, void *datum, void *ptr)
2606 struct level_datum *levdatum = datum;
2607 struct policy_data *pd = ptr;
2614 buf[0] = cpu_to_le32(len);
2615 buf[1] = cpu_to_le32(levdatum->isalias);
2616 rc = put_entry(buf, sizeof(u32), 2, fp);
2620 rc = put_entry(key, 1, len, fp);
2624 rc = mls_write_level(levdatum->level, fp);
2631 static int cat_write(void *vkey, void *datum, void *ptr)
2634 struct cat_datum *catdatum = datum;
2635 struct policy_data *pd = ptr;
2642 buf[0] = cpu_to_le32(len);
2643 buf[1] = cpu_to_le32(catdatum->value);
2644 buf[2] = cpu_to_le32(catdatum->isalias);
2645 rc = put_entry(buf, sizeof(u32), 3, fp);
2649 rc = put_entry(key, 1, len, fp);
2656 static int role_trans_write(struct policydb *p, void *fp)
2658 struct role_trans *r = p->role_tr;
2659 struct role_trans *tr;
2665 for (tr = r; tr; tr = tr->next)
2667 buf[0] = cpu_to_le32(nel);
2668 rc = put_entry(buf, sizeof(u32), 1, fp);
2671 for (tr = r; tr; tr = tr->next) {
2672 buf[0] = cpu_to_le32(tr->role);
2673 buf[1] = cpu_to_le32(tr->type);
2674 buf[2] = cpu_to_le32(tr->new_role);
2675 rc = put_entry(buf, sizeof(u32), 3, fp);
2678 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2679 buf[0] = cpu_to_le32(tr->tclass);
2680 rc = put_entry(buf, sizeof(u32), 1, fp);
2689 static int role_allow_write(struct role_allow *r, void *fp)
2691 struct role_allow *ra;
2697 for (ra = r; ra; ra = ra->next)
2699 buf[0] = cpu_to_le32(nel);
2700 rc = put_entry(buf, sizeof(u32), 1, fp);
2703 for (ra = r; ra; ra = ra->next) {
2704 buf[0] = cpu_to_le32(ra->role);
2705 buf[1] = cpu_to_le32(ra->new_role);
2706 rc = put_entry(buf, sizeof(u32), 2, fp);
2714 * Write a security context structure
2715 * to a policydb binary representation file.
2717 static int context_write(struct policydb *p, struct context *c,
2723 buf[0] = cpu_to_le32(c->user);
2724 buf[1] = cpu_to_le32(c->role);
2725 buf[2] = cpu_to_le32(c->type);
2727 rc = put_entry(buf, sizeof(u32), 3, fp);
2731 rc = mls_write_range_helper(&c->range, fp);
2739 * The following *_write functions are used to
2740 * write the symbol data to a policy database
2741 * binary representation file.
2744 static int perm_write(void *vkey, void *datum, void *fp)
2747 struct perm_datum *perdatum = datum;
2753 buf[0] = cpu_to_le32(len);
2754 buf[1] = cpu_to_le32(perdatum->value);
2755 rc = put_entry(buf, sizeof(u32), 2, fp);
2759 rc = put_entry(key, 1, len, fp);
2766 static int common_write(void *vkey, void *datum, void *ptr)
2769 struct common_datum *comdatum = datum;
2770 struct policy_data *pd = ptr;
2777 buf[0] = cpu_to_le32(len);
2778 buf[1] = cpu_to_le32(comdatum->value);
2779 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2780 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2781 rc = put_entry(buf, sizeof(u32), 4, fp);
2785 rc = put_entry(key, 1, len, fp);
2789 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2796 static int type_set_write(struct type_set *t, void *fp)
2801 if (ebitmap_write(&t->types, fp))
2803 if (ebitmap_write(&t->negset, fp))
2806 buf[0] = cpu_to_le32(t->flags);
2807 rc = put_entry(buf, sizeof(u32), 1, fp);
2814 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2817 struct constraint_node *c;
2818 struct constraint_expr *e;
2823 for (c = node; c; c = c->next) {
2825 for (e = c->expr; e; e = e->next)
2827 buf[0] = cpu_to_le32(c->permissions);
2828 buf[1] = cpu_to_le32(nel);
2829 rc = put_entry(buf, sizeof(u32), 2, fp);
2832 for (e = c->expr; e; e = e->next) {
2833 buf[0] = cpu_to_le32(e->expr_type);
2834 buf[1] = cpu_to_le32(e->attr);
2835 buf[2] = cpu_to_le32(e->op);
2836 rc = put_entry(buf, sizeof(u32), 3, fp);
2840 switch (e->expr_type) {
2842 rc = ebitmap_write(&e->names, fp);
2845 if (p->policyvers >=
2846 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2847 rc = type_set_write(e->type_names, fp);
2861 static int class_write(void *vkey, void *datum, void *ptr)
2864 struct class_datum *cladatum = datum;
2865 struct policy_data *pd = ptr;
2867 struct policydb *p = pd->p;
2868 struct constraint_node *c;
2875 if (cladatum->comkey)
2876 len2 = strlen(cladatum->comkey);
2881 for (c = cladatum->constraints; c; c = c->next)
2884 buf[0] = cpu_to_le32(len);
2885 buf[1] = cpu_to_le32(len2);
2886 buf[2] = cpu_to_le32(cladatum->value);
2887 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2888 if (cladatum->permissions.table)
2889 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2892 buf[5] = cpu_to_le32(ncons);
2893 rc = put_entry(buf, sizeof(u32), 6, fp);
2897 rc = put_entry(key, 1, len, fp);
2901 if (cladatum->comkey) {
2902 rc = put_entry(cladatum->comkey, 1, len2, fp);
2907 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2911 rc = write_cons_helper(p, cladatum->constraints, fp);
2915 /* write out the validatetrans rule */
2917 for (c = cladatum->validatetrans; c; c = c->next)
2920 buf[0] = cpu_to_le32(ncons);
2921 rc = put_entry(buf, sizeof(u32), 1, fp);
2925 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2929 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2930 buf[0] = cpu_to_le32(cladatum->default_user);
2931 buf[1] = cpu_to_le32(cladatum->default_role);
2932 buf[2] = cpu_to_le32(cladatum->default_range);
2934 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2939 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2940 buf[0] = cpu_to_le32(cladatum->default_type);
2941 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2949 static int role_write(void *vkey, void *datum, void *ptr)
2952 struct role_datum *role = datum;
2953 struct policy_data *pd = ptr;
2955 struct policydb *p = pd->p;
2962 buf[items++] = cpu_to_le32(len);
2963 buf[items++] = cpu_to_le32(role->value);
2964 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2965 buf[items++] = cpu_to_le32(role->bounds);
2967 BUG_ON(items > ARRAY_SIZE(buf));
2969 rc = put_entry(buf, sizeof(u32), items, fp);
2973 rc = put_entry(key, 1, len, fp);
2977 rc = ebitmap_write(&role->dominates, fp);
2981 rc = ebitmap_write(&role->types, fp);
2988 static int type_write(void *vkey, void *datum, void *ptr)
2991 struct type_datum *typdatum = datum;
2992 struct policy_data *pd = ptr;
2993 struct policydb *p = pd->p;
3001 buf[items++] = cpu_to_le32(len);
3002 buf[items++] = cpu_to_le32(typdatum->value);
3003 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3006 if (typdatum->primary)
3007 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3009 if (typdatum->attribute)
3010 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3012 buf[items++] = cpu_to_le32(properties);
3013 buf[items++] = cpu_to_le32(typdatum->bounds);
3015 buf[items++] = cpu_to_le32(typdatum->primary);
3017 BUG_ON(items > ARRAY_SIZE(buf));
3018 rc = put_entry(buf, sizeof(u32), items, fp);
3022 rc = put_entry(key, 1, len, fp);
3029 static int user_write(void *vkey, void *datum, void *ptr)
3032 struct user_datum *usrdatum = datum;
3033 struct policy_data *pd = ptr;
3034 struct policydb *p = pd->p;
3042 buf[items++] = cpu_to_le32(len);
3043 buf[items++] = cpu_to_le32(usrdatum->value);
3044 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3045 buf[items++] = cpu_to_le32(usrdatum->bounds);
3046 BUG_ON(items > ARRAY_SIZE(buf));
3047 rc = put_entry(buf, sizeof(u32), items, fp);
3051 rc = put_entry(key, 1, len, fp);
3055 rc = ebitmap_write(&usrdatum->roles, fp);
3059 rc = mls_write_range_helper(&usrdatum->range, fp);
3063 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3070 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3083 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3086 unsigned int i, j, rc;
3088 __be64 prefixbuf[1];
3092 for (i = 0; i < info->ocon_num; i++) {
3094 for (c = p->ocontexts[i]; c; c = c->next)
3096 buf[0] = cpu_to_le32(nel);
3097 rc = put_entry(buf, sizeof(u32), 1, fp);
3100 for (c = p->ocontexts[i]; c; c = c->next) {
3103 buf[0] = cpu_to_le32(c->sid[0]);
3104 rc = put_entry(buf, sizeof(u32), 1, fp);
3107 rc = context_write(p, &c->context[0], fp);
3113 len = strlen(c->u.name);
3114 buf[0] = cpu_to_le32(len);
3115 rc = put_entry(buf, sizeof(u32), 1, fp);
3118 rc = put_entry(c->u.name, 1, len, fp);
3121 rc = context_write(p, &c->context[0], fp);
3124 rc = context_write(p, &c->context[1], fp);
3129 buf[0] = cpu_to_le32(c->u.port.protocol);
3130 buf[1] = cpu_to_le32(c->u.port.low_port);
3131 buf[2] = cpu_to_le32(c->u.port.high_port);
3132 rc = put_entry(buf, sizeof(u32), 3, fp);
3135 rc = context_write(p, &c->context[0], fp);
3140 nodebuf[0] = c->u.node.addr; /* network order */
3141 nodebuf[1] = c->u.node.mask; /* network order */
3142 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3145 rc = context_write(p, &c->context[0], fp);
3150 buf[0] = cpu_to_le32(c->v.behavior);
3151 len = strlen(c->u.name);
3152 buf[1] = cpu_to_le32(len);
3153 rc = put_entry(buf, sizeof(u32), 2, fp);
3156 rc = put_entry(c->u.name, 1, len, fp);
3159 rc = context_write(p, &c->context[0], fp);
3164 for (j = 0; j < 4; j++)
3165 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3166 for (j = 0; j < 4; j++)
3167 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3168 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3171 rc = context_write(p, &c->context[0], fp);
3176 /* subnet_prefix is in CPU order */
3177 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3179 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3183 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3184 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3186 rc = put_entry(buf, sizeof(u32), 2, fp);
3189 rc = context_write(p, &c->context[0], fp);
3193 case OCON_IBENDPORT:
3194 len = strlen(c->u.ibendport.dev_name);
3195 buf[0] = cpu_to_le32(len);
3196 buf[1] = cpu_to_le32(c->u.ibendport.port);
3197 rc = put_entry(buf, sizeof(u32), 2, fp);
3200 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3203 rc = context_write(p, &c->context[0], fp);
3213 static int genfs_write(struct policydb *p, void *fp)
3215 struct genfs *genfs;
3222 for (genfs = p->genfs; genfs; genfs = genfs->next)
3224 buf[0] = cpu_to_le32(len);
3225 rc = put_entry(buf, sizeof(u32), 1, fp);
3228 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3229 len = strlen(genfs->fstype);
3230 buf[0] = cpu_to_le32(len);
3231 rc = put_entry(buf, sizeof(u32), 1, fp);
3234 rc = put_entry(genfs->fstype, 1, len, fp);
3238 for (c = genfs->head; c; c = c->next)
3240 buf[0] = cpu_to_le32(len);
3241 rc = put_entry(buf, sizeof(u32), 1, fp);
3244 for (c = genfs->head; c; c = c->next) {
3245 len = strlen(c->u.name);
3246 buf[0] = cpu_to_le32(len);
3247 rc = put_entry(buf, sizeof(u32), 1, fp);
3250 rc = put_entry(c->u.name, 1, len, fp);
3253 buf[0] = cpu_to_le32(c->v.sclass);
3254 rc = put_entry(buf, sizeof(u32), 1, fp);
3257 rc = context_write(p, &c->context[0], fp);
3265 static int hashtab_cnt(void *key, void *data, void *ptr)
3273 static int range_write_helper(void *key, void *data, void *ptr)
3276 struct range_trans *rt = key;
3277 struct mls_range *r = data;
3278 struct policy_data *pd = ptr;
3280 struct policydb *p = pd->p;
3283 buf[0] = cpu_to_le32(rt->source_type);
3284 buf[1] = cpu_to_le32(rt->target_type);
3285 rc = put_entry(buf, sizeof(u32), 2, fp);
3288 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3289 buf[0] = cpu_to_le32(rt->target_class);
3290 rc = put_entry(buf, sizeof(u32), 1, fp);
3294 rc = mls_write_range_helper(r, fp);
3301 static int range_write(struct policydb *p, void *fp)
3305 struct policy_data pd;
3310 /* count the number of entries in the hashtab */
3312 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3316 buf[0] = cpu_to_le32(nel);
3317 rc = put_entry(buf, sizeof(u32), 1, fp);
3321 /* actually write all of the entries */
3322 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3329 static int filename_write_helper(void *key, void *data, void *ptr)
3331 struct filename_trans_key *ft = key;
3332 struct filename_trans_datum *datum = data;
3333 struct ebitmap_node *node;
3337 u32 bit, len = strlen(ft->name);
3340 ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
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(bit + 1);
3351 buf[1] = cpu_to_le32(ft->ttype);
3352 buf[2] = cpu_to_le32(ft->tclass);
3353 buf[3] = cpu_to_le32(datum->otype);
3355 rc = put_entry(buf, sizeof(u32), 4, fp);
3360 datum = datum->next;
3361 } while (unlikely(datum));
3366 static int filename_trans_write(struct policydb *p, void *fp)
3371 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3374 buf[0] = cpu_to_le32(p->filename_trans_count);
3375 rc = put_entry(buf, sizeof(u32), 1, fp);
3379 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3387 * Write the configuration data in a policy database
3388 * structure to a policy database binary representation
3391 int policydb_write(struct policydb *p, void *fp)
3393 unsigned int i, num_syms;
3398 struct policydb_compat_info *info;
3401 * refuse to write policy older than compressed avtab
3402 * to simplify the writer. There are other tests dropped
3403 * since we assume this throughout the writer code. Be
3404 * careful if you ever try to remove this restriction
3406 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3407 pr_err("SELinux: refusing to write policy version %d."
3408 " Because it is less than version %d\n", p->policyvers,
3409 POLICYDB_VERSION_AVTAB);
3415 config |= POLICYDB_CONFIG_MLS;
3417 if (p->reject_unknown)
3418 config |= REJECT_UNKNOWN;
3419 if (p->allow_unknown)
3420 config |= ALLOW_UNKNOWN;
3422 /* Write the magic number and string identifiers. */
3423 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3424 len = strlen(POLICYDB_STRING);
3425 buf[1] = cpu_to_le32(len);
3426 rc = put_entry(buf, sizeof(u32), 2, fp);
3429 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3433 /* Write the version, config, and table sizes. */
3434 info = policydb_lookup_compat(p->policyvers);
3436 pr_err("SELinux: compatibility lookup failed for policy "
3437 "version %d", p->policyvers);
3441 buf[0] = cpu_to_le32(p->policyvers);
3442 buf[1] = cpu_to_le32(config);
3443 buf[2] = cpu_to_le32(info->sym_num);
3444 buf[3] = cpu_to_le32(info->ocon_num);
3446 rc = put_entry(buf, sizeof(u32), 4, fp);
3450 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3451 rc = ebitmap_write(&p->policycaps, fp);
3456 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3457 rc = ebitmap_write(&p->permissive_map, fp);
3462 num_syms = info->sym_num;
3463 for (i = 0; i < num_syms; i++) {
3464 struct policy_data pd;
3469 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3470 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3472 rc = put_entry(buf, sizeof(u32), 2, fp);
3475 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3480 rc = avtab_write(p, &p->te_avtab, fp);
3484 rc = cond_write_list(p, fp);
3488 rc = role_trans_write(p, fp);
3492 rc = role_allow_write(p->role_allow, fp);
3496 rc = filename_trans_write(p, fp);
3500 rc = ocontext_write(p, info, fp);
3504 rc = genfs_write(p, fp);
3508 rc = range_write(p, fp);
3512 for (i = 0; i < p->p_types.nprim; i++) {
3513 struct ebitmap *e = &p->type_attr_map_array[i];
3515 rc = ebitmap_write(e, fp);