1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
9 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
11 * Support for enhanced MLS infrastructure.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Updated: Hewlett-Packard <paul@paul-moore.com>
19 * Added support for the policy capability bitmap
21 * Update: Mellanox Techonologies
23 * Added Infiniband support
25 * Copyright (C) 2016 Mellanox Techonologies
26 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
27 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
28 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/audit.h>
40 #include "conditional.h"
47 static const char *symtab_name[SYM_NUM] = {
59 static unsigned int symtab_sizes[SYM_NUM] = {
70 struct policydb_compat_info {
76 /* These need to be updated if SYM_NUM or OCON_NUM changes */
77 static struct policydb_compat_info policydb_compat[] = {
79 .version = POLICYDB_VERSION_BASE,
80 .sym_num = SYM_NUM - 3,
81 .ocon_num = OCON_NUM - 3,
84 .version = POLICYDB_VERSION_BOOL,
85 .sym_num = SYM_NUM - 2,
86 .ocon_num = OCON_NUM - 3,
89 .version = POLICYDB_VERSION_IPV6,
90 .sym_num = SYM_NUM - 2,
91 .ocon_num = OCON_NUM - 2,
94 .version = POLICYDB_VERSION_NLCLASS,
95 .sym_num = SYM_NUM - 2,
96 .ocon_num = OCON_NUM - 2,
99 .version = POLICYDB_VERSION_MLS,
101 .ocon_num = OCON_NUM - 2,
104 .version = POLICYDB_VERSION_AVTAB,
106 .ocon_num = OCON_NUM - 2,
109 .version = POLICYDB_VERSION_RANGETRANS,
111 .ocon_num = OCON_NUM - 2,
114 .version = POLICYDB_VERSION_POLCAP,
116 .ocon_num = OCON_NUM - 2,
119 .version = POLICYDB_VERSION_PERMISSIVE,
121 .ocon_num = OCON_NUM - 2,
124 .version = POLICYDB_VERSION_BOUNDARY,
126 .ocon_num = OCON_NUM - 2,
129 .version = POLICYDB_VERSION_FILENAME_TRANS,
131 .ocon_num = OCON_NUM - 2,
134 .version = POLICYDB_VERSION_ROLETRANS,
136 .ocon_num = OCON_NUM - 2,
139 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
141 .ocon_num = OCON_NUM - 2,
144 .version = POLICYDB_VERSION_DEFAULT_TYPE,
146 .ocon_num = OCON_NUM - 2,
149 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
151 .ocon_num = OCON_NUM - 2,
154 .version = POLICYDB_VERSION_XPERMS_IOCTL,
156 .ocon_num = OCON_NUM - 2,
159 .version = POLICYDB_VERSION_INFINIBAND,
161 .ocon_num = OCON_NUM,
165 static struct policydb_compat_info *policydb_lookup_compat(int version)
168 struct policydb_compat_info *info = NULL;
170 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
171 if (policydb_compat[i].version == version) {
172 info = &policydb_compat[i];
180 * Initialize the role table.
182 static int roles_init(struct policydb *p)
186 struct role_datum *role;
188 role = kzalloc(sizeof(*role), GFP_KERNEL);
193 role->value = ++p->p_roles.nprim;
194 if (role->value != OBJECT_R_VAL)
198 key = kstrdup(OBJECT_R, GFP_KERNEL);
202 rc = hashtab_insert(p->p_roles.table, key, role);
213 static u32 filenametr_hash(struct hashtab *h, const void *k)
215 const struct filename_trans *ft = k;
217 unsigned int byte_num;
220 hash = ft->stype ^ ft->ttype ^ ft->tclass;
223 while ((focus = ft->name[byte_num++]))
224 hash = partial_name_hash(focus, hash);
225 return hash & (h->size - 1);
228 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
230 const struct filename_trans *ft1 = k1;
231 const struct filename_trans *ft2 = k2;
234 v = ft1->stype - ft2->stype;
238 v = ft1->ttype - ft2->ttype;
242 v = ft1->tclass - ft2->tclass;
246 return strcmp(ft1->name, ft2->name);
250 static u32 rangetr_hash(struct hashtab *h, const void *k)
252 const struct range_trans *key = k;
253 return (key->source_type + (key->target_type << 3) +
254 (key->target_class << 5)) & (h->size - 1);
257 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
259 const struct range_trans *key1 = k1, *key2 = k2;
262 v = key1->source_type - key2->source_type;
266 v = key1->target_type - key2->target_type;
270 v = key1->target_class - key2->target_class;
276 * Initialize a policy database structure.
278 static int policydb_init(struct policydb *p)
282 memset(p, 0, sizeof(*p));
284 for (i = 0; i < SYM_NUM; i++) {
285 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
290 rc = avtab_init(&p->te_avtab);
298 rc = cond_policydb_init(p);
302 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
303 if (!p->filename_trans) {
308 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
314 ebitmap_init(&p->filename_trans_ttypes);
315 ebitmap_init(&p->policycaps);
316 ebitmap_init(&p->permissive_map);
320 hashtab_destroy(p->filename_trans);
321 hashtab_destroy(p->range_tr);
322 for (i = 0; i < SYM_NUM; i++)
323 hashtab_destroy(p->symtab[i].table);
328 * The following *_index functions are used to
329 * define the val_to_name and val_to_struct arrays
330 * in a policy database structure. The val_to_name
331 * arrays are used when converting security context
332 * structures into string representations. The
333 * val_to_struct arrays are used when the attributes
334 * of a class, role, or user are needed.
337 static int common_index(void *key, void *datum, void *datap)
340 struct common_datum *comdatum;
344 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
347 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
352 static int class_index(void *key, void *datum, void *datap)
355 struct class_datum *cladatum;
359 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
362 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
363 p->class_val_to_struct[cladatum->value - 1] = cladatum;
367 static int role_index(void *key, void *datum, void *datap)
370 struct role_datum *role;
375 || role->value > p->p_roles.nprim
376 || role->bounds > p->p_roles.nprim)
379 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
380 p->role_val_to_struct[role->value - 1] = role;
384 static int type_index(void *key, void *datum, void *datap)
387 struct type_datum *typdatum;
392 if (typdatum->primary) {
394 || typdatum->value > p->p_types.nprim
395 || typdatum->bounds > p->p_types.nprim)
397 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
398 p->type_val_to_struct_array[typdatum->value - 1] = typdatum;
404 static int user_index(void *key, void *datum, void *datap)
407 struct user_datum *usrdatum;
412 || usrdatum->value > p->p_users.nprim
413 || usrdatum->bounds > p->p_users.nprim)
416 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
417 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
421 static int sens_index(void *key, void *datum, void *datap)
424 struct level_datum *levdatum;
429 if (!levdatum->isalias) {
430 if (!levdatum->level->sens ||
431 levdatum->level->sens > p->p_levels.nprim)
434 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
440 static int cat_index(void *key, void *datum, void *datap)
443 struct cat_datum *catdatum;
448 if (!catdatum->isalias) {
449 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
452 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
458 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
471 static void hash_eval(struct hashtab *h, const char *hash_name)
473 struct hashtab_info info;
475 hashtab_stat(h, &info);
476 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
477 "longest chain length %d\n", hash_name, h->nel,
478 info.slots_used, h->size, info.max_chain_len);
481 static void symtab_hash_eval(struct symtab *s)
485 for (i = 0; i < SYM_NUM; i++)
486 hash_eval(s[i].table, symtab_name[i]);
490 static inline void hash_eval(struct hashtab *h, char *hash_name)
496 * Define the other val_to_name and val_to_struct arrays
497 * in a policy database structure.
499 * Caller must clean up on failure.
501 static int policydb_index(struct policydb *p)
506 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
507 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
508 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
510 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
511 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
514 pr_debug("SELinux: %d classes, %d rules\n",
515 p->p_classes.nprim, p->te_avtab.nel);
518 avtab_hash_eval(&p->te_avtab, "rules");
519 symtab_hash_eval(p->symtab);
522 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
523 sizeof(*p->class_val_to_struct),
525 if (!p->class_val_to_struct)
528 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
529 sizeof(*p->role_val_to_struct),
531 if (!p->role_val_to_struct)
534 p->user_val_to_struct = kcalloc(p->p_users.nprim,
535 sizeof(*p->user_val_to_struct),
537 if (!p->user_val_to_struct)
540 p->type_val_to_struct_array = kvcalloc(p->p_types.nprim,
541 sizeof(*p->type_val_to_struct_array),
543 if (!p->type_val_to_struct_array)
546 rc = cond_init_bool_indexes(p);
550 for (i = 0; i < SYM_NUM; i++) {
551 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
554 if (!p->sym_val_to_name[i])
557 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
567 * The following *_destroy functions are used to
568 * free any memory allocated for each kind of
569 * symbol data in the policy database.
572 static int perm_destroy(void *key, void *datum, void *p)
579 static int common_destroy(void *key, void *datum, void *p)
581 struct common_datum *comdatum;
586 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
587 hashtab_destroy(comdatum->permissions.table);
593 static void constraint_expr_destroy(struct constraint_expr *expr)
596 ebitmap_destroy(&expr->names);
597 if (expr->type_names) {
598 ebitmap_destroy(&expr->type_names->types);
599 ebitmap_destroy(&expr->type_names->negset);
600 kfree(expr->type_names);
606 static int cls_destroy(void *key, void *datum, void *p)
608 struct class_datum *cladatum;
609 struct constraint_node *constraint, *ctemp;
610 struct constraint_expr *e, *etmp;
615 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
616 hashtab_destroy(cladatum->permissions.table);
617 constraint = cladatum->constraints;
619 e = constraint->expr;
623 constraint_expr_destroy(etmp);
626 constraint = constraint->next;
630 constraint = cladatum->validatetrans;
632 e = constraint->expr;
636 constraint_expr_destroy(etmp);
639 constraint = constraint->next;
642 kfree(cladatum->comkey);
648 static int role_destroy(void *key, void *datum, void *p)
650 struct role_datum *role;
655 ebitmap_destroy(&role->dominates);
656 ebitmap_destroy(&role->types);
662 static int type_destroy(void *key, void *datum, void *p)
669 static int user_destroy(void *key, void *datum, void *p)
671 struct user_datum *usrdatum;
676 ebitmap_destroy(&usrdatum->roles);
677 ebitmap_destroy(&usrdatum->range.level[0].cat);
678 ebitmap_destroy(&usrdatum->range.level[1].cat);
679 ebitmap_destroy(&usrdatum->dfltlevel.cat);
685 static int sens_destroy(void *key, void *datum, void *p)
687 struct level_datum *levdatum;
693 ebitmap_destroy(&levdatum->level->cat);
694 kfree(levdatum->level);
700 static int cat_destroy(void *key, void *datum, void *p)
707 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
719 static int filenametr_destroy(void *key, void *datum, void *p)
721 struct filename_trans *ft = key;
729 static int range_tr_destroy(void *key, void *datum, void *p)
731 struct mls_range *rt = datum;
733 ebitmap_destroy(&rt->level[0].cat);
734 ebitmap_destroy(&rt->level[1].cat);
740 static void ocontext_destroy(struct ocontext *c, int i)
745 context_destroy(&c->context[0]);
746 context_destroy(&c->context[1]);
747 if (i == OCON_ISID || i == OCON_FS ||
748 i == OCON_NETIF || i == OCON_FSUSE)
754 * Free any memory allocated by a policy database structure.
756 void policydb_destroy(struct policydb *p)
758 struct ocontext *c, *ctmp;
759 struct genfs *g, *gtmp;
761 struct role_allow *ra, *lra = NULL;
762 struct role_trans *tr, *ltr = NULL;
764 for (i = 0; i < SYM_NUM; i++) {
766 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
767 hashtab_destroy(p->symtab[i].table);
770 for (i = 0; i < SYM_NUM; i++)
771 kvfree(p->sym_val_to_name[i]);
773 kfree(p->class_val_to_struct);
774 kfree(p->role_val_to_struct);
775 kfree(p->user_val_to_struct);
776 kvfree(p->type_val_to_struct_array);
778 avtab_destroy(&p->te_avtab);
780 for (i = 0; i < OCON_NUM; i++) {
786 ocontext_destroy(ctmp, i);
788 p->ocontexts[i] = NULL;
799 ocontext_destroy(ctmp, OCON_FSUSE);
807 cond_policydb_destroy(p);
809 for (tr = p->role_tr; tr; tr = tr->next) {
816 for (ra = p->role_allow; ra; ra = ra->next) {
823 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
824 hashtab_destroy(p->filename_trans);
826 hashtab_map(p->range_tr, range_tr_destroy, NULL);
827 hashtab_destroy(p->range_tr);
829 if (p->type_attr_map_array) {
830 for (i = 0; i < p->p_types.nprim; i++)
831 ebitmap_destroy(&p->type_attr_map_array[i]);
832 kvfree(p->type_attr_map_array);
835 ebitmap_destroy(&p->filename_trans_ttypes);
836 ebitmap_destroy(&p->policycaps);
837 ebitmap_destroy(&p->permissive_map);
841 * Load the initial SIDs specified in a policy database
842 * structure into a SID table.
844 int policydb_load_isids(struct policydb *p, struct sidtab *s)
846 struct ocontext *head, *c;
851 pr_err("SELinux: out of memory on SID table init\n");
855 head = p->ocontexts[OCON_ISID];
856 for (c = head; c; c = c->next) {
858 if (!c->context[0].user) {
859 pr_err("SELinux: SID %s was never defined.\n",
864 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
865 pr_err("SELinux: Initial SID %s out of range.\n",
871 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
873 pr_err("SELinux: unable to load initial SID %s.\n",
884 int policydb_class_isvalid(struct policydb *p, unsigned int class)
886 if (!class || class > p->p_classes.nprim)
891 int policydb_role_isvalid(struct policydb *p, unsigned int role)
893 if (!role || role > p->p_roles.nprim)
898 int policydb_type_isvalid(struct policydb *p, unsigned int type)
900 if (!type || type > p->p_types.nprim)
906 * Return 1 if the fields in the security context
907 * structure `c' are valid. Return 0 otherwise.
909 int policydb_context_isvalid(struct policydb *p, struct context *c)
911 struct role_datum *role;
912 struct user_datum *usrdatum;
914 if (!c->role || c->role > p->p_roles.nprim)
917 if (!c->user || c->user > p->p_users.nprim)
920 if (!c->type || c->type > p->p_types.nprim)
923 if (c->role != OBJECT_R_VAL) {
925 * Role must be authorized for the type.
927 role = p->role_val_to_struct[c->role - 1];
928 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
929 /* role may not be associated with type */
933 * User must be authorized for the role.
935 usrdatum = p->user_val_to_struct[c->user - 1];
939 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
940 /* user may not be associated with role */
944 if (!mls_context_isvalid(p, c))
951 * Read a MLS range structure from a policydb binary
952 * representation file.
954 static int mls_read_range_helper(struct mls_range *r, void *fp)
960 rc = next_entry(buf, fp, sizeof(u32));
965 items = le32_to_cpu(buf[0]);
966 if (items > ARRAY_SIZE(buf)) {
967 pr_err("SELinux: mls: range overflow\n");
971 rc = next_entry(buf, fp, sizeof(u32) * items);
973 pr_err("SELinux: mls: truncated range\n");
977 r->level[0].sens = le32_to_cpu(buf[0]);
979 r->level[1].sens = le32_to_cpu(buf[1]);
981 r->level[1].sens = r->level[0].sens;
983 rc = ebitmap_read(&r->level[0].cat, fp);
985 pr_err("SELinux: mls: error reading low categories\n");
989 rc = ebitmap_read(&r->level[1].cat, fp);
991 pr_err("SELinux: mls: error reading high categories\n");
995 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
997 pr_err("SELinux: mls: out of memory\n");
1004 ebitmap_destroy(&r->level[0].cat);
1010 * Read and validate a security context structure
1011 * from a policydb binary representation file.
1013 static int context_read_and_validate(struct context *c,
1020 rc = next_entry(buf, fp, sizeof buf);
1022 pr_err("SELinux: context truncated\n");
1025 c->user = le32_to_cpu(buf[0]);
1026 c->role = le32_to_cpu(buf[1]);
1027 c->type = le32_to_cpu(buf[2]);
1028 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1029 rc = mls_read_range_helper(&c->range, fp);
1031 pr_err("SELinux: error reading MLS range of context\n");
1037 if (!policydb_context_isvalid(p, c)) {
1038 pr_err("SELinux: invalid security context\n");
1048 * The following *_read functions are used to
1049 * read the symbol data from a policy database
1050 * binary representation file.
1053 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1058 if ((len == 0) || (len == (u32)-1))
1061 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1065 /* it's expected the caller should free the str */
1068 rc = next_entry(str, fp, len);
1076 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1079 struct perm_datum *perdatum;
1084 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1088 rc = next_entry(buf, fp, sizeof buf);
1092 len = le32_to_cpu(buf[0]);
1093 perdatum->value = le32_to_cpu(buf[1]);
1095 rc = str_read(&key, GFP_KERNEL, fp, len);
1099 rc = hashtab_insert(h, key, perdatum);
1105 perm_destroy(key, perdatum, NULL);
1109 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1112 struct common_datum *comdatum;
1117 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1121 rc = next_entry(buf, fp, sizeof buf);
1125 len = le32_to_cpu(buf[0]);
1126 comdatum->value = le32_to_cpu(buf[1]);
1128 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1131 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1132 nel = le32_to_cpu(buf[3]);
1134 rc = str_read(&key, GFP_KERNEL, fp, len);
1138 for (i = 0; i < nel; i++) {
1139 rc = perm_read(p, comdatum->permissions.table, fp);
1144 rc = hashtab_insert(h, key, comdatum);
1149 common_destroy(key, comdatum, NULL);
1153 static void type_set_init(struct type_set *t)
1155 ebitmap_init(&t->types);
1156 ebitmap_init(&t->negset);
1159 static int type_set_read(struct type_set *t, void *fp)
1164 if (ebitmap_read(&t->types, fp))
1166 if (ebitmap_read(&t->negset, fp))
1169 rc = next_entry(buf, fp, sizeof(u32));
1172 t->flags = le32_to_cpu(buf[0]);
1178 static int read_cons_helper(struct policydb *p,
1179 struct constraint_node **nodep,
1180 int ncons, int allowxtarget, void *fp)
1182 struct constraint_node *c, *lc;
1183 struct constraint_expr *e, *le;
1186 int rc, i, j, depth;
1189 for (i = 0; i < ncons; i++) {
1190 c = kzalloc(sizeof(*c), GFP_KERNEL);
1199 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1202 c->permissions = le32_to_cpu(buf[0]);
1203 nexpr = le32_to_cpu(buf[1]);
1206 for (j = 0; j < nexpr; j++) {
1207 e = kzalloc(sizeof(*e), GFP_KERNEL);
1216 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1219 e->expr_type = le32_to_cpu(buf[0]);
1220 e->attr = le32_to_cpu(buf[1]);
1221 e->op = le32_to_cpu(buf[2]);
1223 switch (e->expr_type) {
1235 if (depth == (CEXPR_MAXDEPTH - 1))
1240 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1242 if (depth == (CEXPR_MAXDEPTH - 1))
1245 rc = ebitmap_read(&e->names, fp);
1248 if (p->policyvers >=
1249 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1250 e->type_names = kzalloc(sizeof
1255 type_set_init(e->type_names);
1256 rc = type_set_read(e->type_names, fp);
1274 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1277 struct class_datum *cladatum;
1279 u32 len, len2, ncons, nel;
1282 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1286 rc = next_entry(buf, fp, sizeof(u32)*6);
1290 len = le32_to_cpu(buf[0]);
1291 len2 = le32_to_cpu(buf[1]);
1292 cladatum->value = le32_to_cpu(buf[2]);
1294 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1297 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1298 nel = le32_to_cpu(buf[4]);
1300 ncons = le32_to_cpu(buf[5]);
1302 rc = str_read(&key, GFP_KERNEL, fp, len);
1307 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1312 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1313 if (!cladatum->comdatum) {
1314 pr_err("SELinux: unknown common %s\n",
1319 for (i = 0; i < nel; i++) {
1320 rc = perm_read(p, cladatum->permissions.table, fp);
1325 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1329 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1330 /* grab the validatetrans rules */
1331 rc = next_entry(buf, fp, sizeof(u32));
1334 ncons = le32_to_cpu(buf[0]);
1335 rc = read_cons_helper(p, &cladatum->validatetrans,
1341 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1342 rc = next_entry(buf, fp, sizeof(u32) * 3);
1346 cladatum->default_user = le32_to_cpu(buf[0]);
1347 cladatum->default_role = le32_to_cpu(buf[1]);
1348 cladatum->default_range = le32_to_cpu(buf[2]);
1351 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1352 rc = next_entry(buf, fp, sizeof(u32) * 1);
1355 cladatum->default_type = le32_to_cpu(buf[0]);
1358 rc = hashtab_insert(h, key, cladatum);
1364 cls_destroy(key, cladatum, NULL);
1368 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1371 struct role_datum *role;
1372 int rc, to_read = 2;
1376 role = kzalloc(sizeof(*role), GFP_KERNEL);
1380 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1383 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1387 len = le32_to_cpu(buf[0]);
1388 role->value = le32_to_cpu(buf[1]);
1389 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1390 role->bounds = le32_to_cpu(buf[2]);
1392 rc = str_read(&key, GFP_KERNEL, fp, len);
1396 rc = ebitmap_read(&role->dominates, fp);
1400 rc = ebitmap_read(&role->types, fp);
1404 if (strcmp(key, OBJECT_R) == 0) {
1406 if (role->value != OBJECT_R_VAL) {
1407 pr_err("SELinux: Role %s has wrong value %d\n",
1408 OBJECT_R, role->value);
1415 rc = hashtab_insert(h, key, role);
1420 role_destroy(key, role, NULL);
1424 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1427 struct type_datum *typdatum;
1428 int rc, to_read = 3;
1432 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1436 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1439 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1443 len = le32_to_cpu(buf[0]);
1444 typdatum->value = le32_to_cpu(buf[1]);
1445 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1446 u32 prop = le32_to_cpu(buf[2]);
1448 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1449 typdatum->primary = 1;
1450 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1451 typdatum->attribute = 1;
1453 typdatum->bounds = le32_to_cpu(buf[3]);
1455 typdatum->primary = le32_to_cpu(buf[2]);
1458 rc = str_read(&key, GFP_KERNEL, fp, len);
1462 rc = hashtab_insert(h, key, typdatum);
1467 type_destroy(key, typdatum, NULL);
1473 * Read a MLS level structure from a policydb binary
1474 * representation file.
1476 static int mls_read_level(struct mls_level *lp, void *fp)
1481 memset(lp, 0, sizeof(*lp));
1483 rc = next_entry(buf, fp, sizeof buf);
1485 pr_err("SELinux: mls: truncated level\n");
1488 lp->sens = le32_to_cpu(buf[0]);
1490 rc = ebitmap_read(&lp->cat, fp);
1492 pr_err("SELinux: mls: error reading level categories\n");
1498 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1501 struct user_datum *usrdatum;
1502 int rc, to_read = 2;
1506 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1510 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1513 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1517 len = le32_to_cpu(buf[0]);
1518 usrdatum->value = le32_to_cpu(buf[1]);
1519 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1520 usrdatum->bounds = le32_to_cpu(buf[2]);
1522 rc = str_read(&key, GFP_KERNEL, fp, len);
1526 rc = ebitmap_read(&usrdatum->roles, fp);
1530 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1531 rc = mls_read_range_helper(&usrdatum->range, fp);
1534 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1539 rc = hashtab_insert(h, key, usrdatum);
1544 user_destroy(key, usrdatum, NULL);
1548 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1551 struct level_datum *levdatum;
1556 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1560 rc = next_entry(buf, fp, sizeof buf);
1564 len = le32_to_cpu(buf[0]);
1565 levdatum->isalias = le32_to_cpu(buf[1]);
1567 rc = str_read(&key, GFP_ATOMIC, fp, len);
1572 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1573 if (!levdatum->level)
1576 rc = mls_read_level(levdatum->level, fp);
1580 rc = hashtab_insert(h, key, levdatum);
1585 sens_destroy(key, levdatum, NULL);
1589 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1592 struct cat_datum *catdatum;
1597 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1601 rc = next_entry(buf, fp, sizeof buf);
1605 len = le32_to_cpu(buf[0]);
1606 catdatum->value = le32_to_cpu(buf[1]);
1607 catdatum->isalias = le32_to_cpu(buf[2]);
1609 rc = str_read(&key, GFP_ATOMIC, fp, len);
1613 rc = hashtab_insert(h, key, catdatum);
1618 cat_destroy(key, catdatum, NULL);
1622 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1634 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1636 struct user_datum *upper, *user;
1637 struct policydb *p = datap;
1640 upper = user = datum;
1641 while (upper->bounds) {
1642 struct ebitmap_node *node;
1645 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1646 pr_err("SELinux: user %s: "
1647 "too deep or looped boundary",
1652 upper = p->user_val_to_struct[upper->bounds - 1];
1653 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1654 if (ebitmap_get_bit(&upper->roles, bit))
1657 pr_err("SELinux: boundary violated policy: "
1658 "user=%s role=%s bounds=%s\n",
1659 sym_name(p, SYM_USERS, user->value - 1),
1660 sym_name(p, SYM_ROLES, bit),
1661 sym_name(p, SYM_USERS, upper->value - 1));
1670 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1672 struct role_datum *upper, *role;
1673 struct policydb *p = datap;
1676 upper = role = datum;
1677 while (upper->bounds) {
1678 struct ebitmap_node *node;
1681 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1682 pr_err("SELinux: role %s: "
1683 "too deep or looped bounds\n",
1688 upper = p->role_val_to_struct[upper->bounds - 1];
1689 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1690 if (ebitmap_get_bit(&upper->types, bit))
1693 pr_err("SELinux: boundary violated policy: "
1694 "role=%s type=%s bounds=%s\n",
1695 sym_name(p, SYM_ROLES, role->value - 1),
1696 sym_name(p, SYM_TYPES, bit),
1697 sym_name(p, SYM_ROLES, upper->value - 1));
1706 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1708 struct type_datum *upper;
1709 struct policydb *p = datap;
1713 while (upper->bounds) {
1714 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1715 pr_err("SELinux: type %s: "
1716 "too deep or looped boundary\n",
1721 upper = p->type_val_to_struct_array[upper->bounds - 1];
1724 if (upper->attribute) {
1725 pr_err("SELinux: type %s: "
1726 "bounded by attribute %s",
1728 sym_name(p, SYM_TYPES, upper->value - 1));
1736 static int policydb_bounds_sanity_check(struct policydb *p)
1740 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1743 rc = hashtab_map(p->p_users.table,
1744 user_bounds_sanity_check, p);
1748 rc = hashtab_map(p->p_roles.table,
1749 role_bounds_sanity_check, p);
1753 rc = hashtab_map(p->p_types.table,
1754 type_bounds_sanity_check, p);
1761 u16 string_to_security_class(struct policydb *p, const char *name)
1763 struct class_datum *cladatum;
1765 cladatum = hashtab_search(p->p_classes.table, name);
1769 return cladatum->value;
1772 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1774 struct class_datum *cladatum;
1775 struct perm_datum *perdatum = NULL;
1776 struct common_datum *comdatum;
1778 if (!tclass || tclass > p->p_classes.nprim)
1781 cladatum = p->class_val_to_struct[tclass-1];
1782 comdatum = cladatum->comdatum;
1784 perdatum = hashtab_search(comdatum->permissions.table,
1787 perdatum = hashtab_search(cladatum->permissions.table,
1792 return 1U << (perdatum->value-1);
1795 static int range_read(struct policydb *p, void *fp)
1797 struct range_trans *rt = NULL;
1798 struct mls_range *r = NULL;
1803 if (p->policyvers < POLICYDB_VERSION_MLS)
1806 rc = next_entry(buf, fp, sizeof(u32));
1810 nel = le32_to_cpu(buf[0]);
1811 for (i = 0; i < nel; i++) {
1813 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1817 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1821 rt->source_type = le32_to_cpu(buf[0]);
1822 rt->target_type = le32_to_cpu(buf[1]);
1823 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1824 rc = next_entry(buf, fp, sizeof(u32));
1827 rt->target_class = le32_to_cpu(buf[0]);
1829 rt->target_class = p->process_class;
1832 if (!policydb_type_isvalid(p, rt->source_type) ||
1833 !policydb_type_isvalid(p, rt->target_type) ||
1834 !policydb_class_isvalid(p, rt->target_class))
1838 r = kzalloc(sizeof(*r), GFP_KERNEL);
1842 rc = mls_read_range_helper(r, fp);
1847 if (!mls_range_isvalid(p, r)) {
1848 pr_warn("SELinux: rangetrans: invalid range\n");
1852 rc = hashtab_insert(p->range_tr, rt, r);
1859 hash_eval(p->range_tr, "rangetr");
1867 static int filename_trans_read(struct policydb *p, void *fp)
1869 struct filename_trans *ft;
1870 struct filename_trans_datum *otype;
1876 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1879 rc = next_entry(buf, fp, sizeof(u32));
1882 nel = le32_to_cpu(buf[0]);
1884 for (i = 0; i < nel; i++) {
1889 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1894 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1898 /* length of the path component string */
1899 rc = next_entry(buf, fp, sizeof(u32));
1902 len = le32_to_cpu(buf[0]);
1904 /* path component string */
1905 rc = str_read(&name, GFP_KERNEL, fp, len);
1911 rc = next_entry(buf, fp, sizeof(u32) * 4);
1915 ft->stype = le32_to_cpu(buf[0]);
1916 ft->ttype = le32_to_cpu(buf[1]);
1917 ft->tclass = le32_to_cpu(buf[2]);
1919 otype->otype = le32_to_cpu(buf[3]);
1921 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1925 rc = hashtab_insert(p->filename_trans, ft, otype);
1928 * Do not return -EEXIST to the caller, or the system
1933 /* But free memory to avoid memory leak. */
1939 hash_eval(p->filename_trans, "filenametr");
1949 static int genfs_read(struct policydb *p, void *fp)
1952 u32 nel, nel2, len, len2;
1954 struct ocontext *l, *c;
1955 struct ocontext *newc = NULL;
1956 struct genfs *genfs_p, *genfs;
1957 struct genfs *newgenfs = NULL;
1959 rc = next_entry(buf, fp, sizeof(u32));
1962 nel = le32_to_cpu(buf[0]);
1964 for (i = 0; i < nel; i++) {
1965 rc = next_entry(buf, fp, sizeof(u32));
1968 len = le32_to_cpu(buf[0]);
1971 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1975 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
1979 for (genfs_p = NULL, genfs = p->genfs; genfs;
1980 genfs_p = genfs, genfs = genfs->next) {
1982 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1983 pr_err("SELinux: dup genfs fstype %s\n",
1987 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1990 newgenfs->next = genfs;
1992 genfs_p->next = newgenfs;
1994 p->genfs = newgenfs;
1998 rc = next_entry(buf, fp, sizeof(u32));
2002 nel2 = le32_to_cpu(buf[0]);
2003 for (j = 0; j < nel2; j++) {
2004 rc = next_entry(buf, fp, sizeof(u32));
2007 len = le32_to_cpu(buf[0]);
2010 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2014 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2018 rc = next_entry(buf, fp, sizeof(u32));
2022 newc->v.sclass = le32_to_cpu(buf[0]);
2023 rc = context_read_and_validate(&newc->context[0], p, fp);
2027 for (l = NULL, c = genfs->head; c;
2028 l = c, c = c->next) {
2030 if (!strcmp(newc->u.name, c->u.name) &&
2031 (!c->v.sclass || !newc->v.sclass ||
2032 newc->v.sclass == c->v.sclass)) {
2033 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2034 genfs->fstype, c->u.name);
2037 len = strlen(newc->u.name);
2038 len2 = strlen(c->u.name);
2054 kfree(newgenfs->fstype);
2057 ocontext_destroy(newc, OCON_FSUSE);
2062 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2067 __be64 prefixbuf[1];
2069 struct ocontext *l, *c;
2072 for (i = 0; i < info->ocon_num; i++) {
2073 rc = next_entry(buf, fp, sizeof(u32));
2076 nel = le32_to_cpu(buf[0]);
2079 for (j = 0; j < nel; j++) {
2081 c = kzalloc(sizeof(*c), GFP_KERNEL);
2087 p->ocontexts[i] = c;
2092 rc = next_entry(buf, fp, sizeof(u32));
2096 c->sid[0] = le32_to_cpu(buf[0]);
2097 rc = context_read_and_validate(&c->context[0], p, fp);
2103 rc = next_entry(buf, fp, sizeof(u32));
2106 len = le32_to_cpu(buf[0]);
2108 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2112 rc = context_read_and_validate(&c->context[0], p, fp);
2115 rc = context_read_and_validate(&c->context[1], p, fp);
2120 rc = next_entry(buf, fp, sizeof(u32)*3);
2123 c->u.port.protocol = le32_to_cpu(buf[0]);
2124 c->u.port.low_port = le32_to_cpu(buf[1]);
2125 c->u.port.high_port = le32_to_cpu(buf[2]);
2126 rc = context_read_and_validate(&c->context[0], p, fp);
2131 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2134 c->u.node.addr = nodebuf[0]; /* network order */
2135 c->u.node.mask = nodebuf[1]; /* network order */
2136 rc = context_read_and_validate(&c->context[0], p, fp);
2141 rc = next_entry(buf, fp, sizeof(u32)*2);
2146 c->v.behavior = le32_to_cpu(buf[0]);
2147 /* Determined at runtime, not in policy DB. */
2148 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2150 if (c->v.behavior > SECURITY_FS_USE_MAX)
2153 len = le32_to_cpu(buf[1]);
2154 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2158 rc = context_read_and_validate(&c->context[0], p, fp);
2165 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2168 for (k = 0; k < 4; k++)
2169 c->u.node6.addr[k] = nodebuf[k];
2170 for (k = 0; k < 4; k++)
2171 c->u.node6.mask[k] = nodebuf[k+4];
2172 rc = context_read_and_validate(&c->context[0], p, fp);
2178 u32 pkey_lo, pkey_hi;
2180 rc = next_entry(prefixbuf, fp, sizeof(u64));
2184 /* we need to have subnet_prefix in CPU order */
2185 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2187 rc = next_entry(buf, fp, sizeof(u32) * 2);
2191 pkey_lo = le32_to_cpu(buf[0]);
2192 pkey_hi = le32_to_cpu(buf[1]);
2194 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2199 c->u.ibpkey.low_pkey = pkey_lo;
2200 c->u.ibpkey.high_pkey = pkey_hi;
2202 rc = context_read_and_validate(&c->context[0],
2209 case OCON_IBENDPORT: {
2212 rc = next_entry(buf, fp, sizeof(u32) * 2);
2215 len = le32_to_cpu(buf[0]);
2217 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2221 port = le32_to_cpu(buf[1]);
2222 if (port > U8_MAX || port == 0) {
2227 c->u.ibendport.port = port;
2229 rc = context_read_and_validate(&c->context[0],
2245 * Read the configuration data from a policy database binary
2246 * representation file into a policy database structure.
2248 int policydb_read(struct policydb *p, void *fp)
2250 struct role_allow *ra, *lra;
2251 struct role_trans *tr, *ltr;
2254 u32 len, nprim, nel;
2257 struct policydb_compat_info *info;
2259 rc = policydb_init(p);
2263 /* Read the magic number and string length. */
2264 rc = next_entry(buf, fp, sizeof(u32) * 2);
2269 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2270 pr_err("SELinux: policydb magic number 0x%x does "
2271 "not match expected magic number 0x%x\n",
2272 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2277 len = le32_to_cpu(buf[1]);
2278 if (len != strlen(POLICYDB_STRING)) {
2279 pr_err("SELinux: policydb string length %d does not "
2280 "match expected length %zu\n",
2281 len, strlen(POLICYDB_STRING));
2286 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2287 if (!policydb_str) {
2288 pr_err("SELinux: unable to allocate memory for policydb "
2289 "string of length %d\n", len);
2293 rc = next_entry(policydb_str, fp, len);
2295 pr_err("SELinux: truncated policydb string identifier\n");
2296 kfree(policydb_str);
2301 policydb_str[len] = '\0';
2302 if (strcmp(policydb_str, POLICYDB_STRING)) {
2303 pr_err("SELinux: policydb string %s does not match "
2304 "my string %s\n", policydb_str, POLICYDB_STRING);
2305 kfree(policydb_str);
2308 /* Done with policydb_str. */
2309 kfree(policydb_str);
2310 policydb_str = NULL;
2312 /* Read the version and table sizes. */
2313 rc = next_entry(buf, fp, sizeof(u32)*4);
2318 p->policyvers = le32_to_cpu(buf[0]);
2319 if (p->policyvers < POLICYDB_VERSION_MIN ||
2320 p->policyvers > POLICYDB_VERSION_MAX) {
2321 pr_err("SELinux: policydb version %d does not match "
2322 "my version range %d-%d\n",
2323 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2327 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2331 if (p->policyvers < POLICYDB_VERSION_MLS) {
2332 pr_err("SELinux: security policydb version %d "
2333 "(MLS) not backwards compatible\n",
2338 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2339 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2341 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2342 rc = ebitmap_read(&p->policycaps, fp);
2347 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2348 rc = ebitmap_read(&p->permissive_map, fp);
2354 info = policydb_lookup_compat(p->policyvers);
2356 pr_err("SELinux: unable to find policy compat info "
2357 "for version %d\n", p->policyvers);
2362 if (le32_to_cpu(buf[2]) != info->sym_num ||
2363 le32_to_cpu(buf[3]) != info->ocon_num) {
2364 pr_err("SELinux: policydb table sizes (%d,%d) do "
2365 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2366 le32_to_cpu(buf[3]),
2367 info->sym_num, info->ocon_num);
2371 for (i = 0; i < info->sym_num; i++) {
2372 rc = next_entry(buf, fp, sizeof(u32)*2);
2375 nprim = le32_to_cpu(buf[0]);
2376 nel = le32_to_cpu(buf[1]);
2377 for (j = 0; j < nel; j++) {
2378 rc = read_f[i](p, p->symtab[i].table, fp);
2383 p->symtab[i].nprim = nprim;
2387 p->process_class = string_to_security_class(p, "process");
2388 if (!p->process_class)
2391 rc = avtab_read(&p->te_avtab, fp, p);
2395 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2396 rc = cond_read_list(p, fp);
2401 rc = next_entry(buf, fp, sizeof(u32));
2404 nel = le32_to_cpu(buf[0]);
2406 for (i = 0; i < nel; i++) {
2408 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2415 rc = next_entry(buf, fp, sizeof(u32)*3);
2420 tr->role = le32_to_cpu(buf[0]);
2421 tr->type = le32_to_cpu(buf[1]);
2422 tr->new_role = le32_to_cpu(buf[2]);
2423 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2424 rc = next_entry(buf, fp, sizeof(u32));
2427 tr->tclass = le32_to_cpu(buf[0]);
2429 tr->tclass = p->process_class;
2432 if (!policydb_role_isvalid(p, tr->role) ||
2433 !policydb_type_isvalid(p, tr->type) ||
2434 !policydb_class_isvalid(p, tr->tclass) ||
2435 !policydb_role_isvalid(p, tr->new_role))
2440 rc = next_entry(buf, fp, sizeof(u32));
2443 nel = le32_to_cpu(buf[0]);
2445 for (i = 0; i < nel; i++) {
2447 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2454 rc = next_entry(buf, fp, sizeof(u32)*2);
2459 ra->role = le32_to_cpu(buf[0]);
2460 ra->new_role = le32_to_cpu(buf[1]);
2461 if (!policydb_role_isvalid(p, ra->role) ||
2462 !policydb_role_isvalid(p, ra->new_role))
2467 rc = filename_trans_read(p, fp);
2471 rc = policydb_index(p);
2476 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2477 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2478 if (!p->process_trans_perms)
2481 rc = ocontext_read(p, info, fp);
2485 rc = genfs_read(p, fp);
2489 rc = range_read(p, fp);
2493 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2494 sizeof(*p->type_attr_map_array),
2496 if (!p->type_attr_map_array)
2499 /* just in case ebitmap_init() becomes more than just a memset(0): */
2500 for (i = 0; i < p->p_types.nprim; i++)
2501 ebitmap_init(&p->type_attr_map_array[i]);
2503 for (i = 0; i < p->p_types.nprim; i++) {
2504 struct ebitmap *e = &p->type_attr_map_array[i];
2506 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2507 rc = ebitmap_read(e, fp);
2511 /* add the type itself as the degenerate case */
2512 rc = ebitmap_set_bit(e, i, 1);
2517 rc = policydb_bounds_sanity_check(p);
2525 policydb_destroy(p);
2530 * Write a MLS level structure to a policydb binary
2531 * representation file.
2533 static int mls_write_level(struct mls_level *l, void *fp)
2538 buf[0] = cpu_to_le32(l->sens);
2539 rc = put_entry(buf, sizeof(u32), 1, fp);
2543 rc = ebitmap_write(&l->cat, fp);
2551 * Write a MLS range structure to a policydb binary
2552 * representation file.
2554 static int mls_write_range_helper(struct mls_range *r, void *fp)
2560 eq = mls_level_eq(&r->level[1], &r->level[0]);
2566 buf[0] = cpu_to_le32(items-1);
2567 buf[1] = cpu_to_le32(r->level[0].sens);
2569 buf[2] = cpu_to_le32(r->level[1].sens);
2571 BUG_ON(items > ARRAY_SIZE(buf));
2573 rc = put_entry(buf, sizeof(u32), items, fp);
2577 rc = ebitmap_write(&r->level[0].cat, fp);
2581 rc = ebitmap_write(&r->level[1].cat, fp);
2589 static int sens_write(void *vkey, void *datum, void *ptr)
2592 struct level_datum *levdatum = datum;
2593 struct policy_data *pd = ptr;
2600 buf[0] = cpu_to_le32(len);
2601 buf[1] = cpu_to_le32(levdatum->isalias);
2602 rc = put_entry(buf, sizeof(u32), 2, fp);
2606 rc = put_entry(key, 1, len, fp);
2610 rc = mls_write_level(levdatum->level, fp);
2617 static int cat_write(void *vkey, void *datum, void *ptr)
2620 struct cat_datum *catdatum = datum;
2621 struct policy_data *pd = ptr;
2628 buf[0] = cpu_to_le32(len);
2629 buf[1] = cpu_to_le32(catdatum->value);
2630 buf[2] = cpu_to_le32(catdatum->isalias);
2631 rc = put_entry(buf, sizeof(u32), 3, fp);
2635 rc = put_entry(key, 1, len, fp);
2642 static int role_trans_write(struct policydb *p, void *fp)
2644 struct role_trans *r = p->role_tr;
2645 struct role_trans *tr;
2651 for (tr = r; tr; tr = tr->next)
2653 buf[0] = cpu_to_le32(nel);
2654 rc = put_entry(buf, sizeof(u32), 1, fp);
2657 for (tr = r; tr; tr = tr->next) {
2658 buf[0] = cpu_to_le32(tr->role);
2659 buf[1] = cpu_to_le32(tr->type);
2660 buf[2] = cpu_to_le32(tr->new_role);
2661 rc = put_entry(buf, sizeof(u32), 3, fp);
2664 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2665 buf[0] = cpu_to_le32(tr->tclass);
2666 rc = put_entry(buf, sizeof(u32), 1, fp);
2675 static int role_allow_write(struct role_allow *r, void *fp)
2677 struct role_allow *ra;
2683 for (ra = r; ra; ra = ra->next)
2685 buf[0] = cpu_to_le32(nel);
2686 rc = put_entry(buf, sizeof(u32), 1, fp);
2689 for (ra = r; ra; ra = ra->next) {
2690 buf[0] = cpu_to_le32(ra->role);
2691 buf[1] = cpu_to_le32(ra->new_role);
2692 rc = put_entry(buf, sizeof(u32), 2, fp);
2700 * Write a security context structure
2701 * to a policydb binary representation file.
2703 static int context_write(struct policydb *p, struct context *c,
2709 buf[0] = cpu_to_le32(c->user);
2710 buf[1] = cpu_to_le32(c->role);
2711 buf[2] = cpu_to_le32(c->type);
2713 rc = put_entry(buf, sizeof(u32), 3, fp);
2717 rc = mls_write_range_helper(&c->range, fp);
2725 * The following *_write functions are used to
2726 * write the symbol data to a policy database
2727 * binary representation file.
2730 static int perm_write(void *vkey, void *datum, void *fp)
2733 struct perm_datum *perdatum = datum;
2739 buf[0] = cpu_to_le32(len);
2740 buf[1] = cpu_to_le32(perdatum->value);
2741 rc = put_entry(buf, sizeof(u32), 2, fp);
2745 rc = put_entry(key, 1, len, fp);
2752 static int common_write(void *vkey, void *datum, void *ptr)
2755 struct common_datum *comdatum = datum;
2756 struct policy_data *pd = ptr;
2763 buf[0] = cpu_to_le32(len);
2764 buf[1] = cpu_to_le32(comdatum->value);
2765 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2766 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2767 rc = put_entry(buf, sizeof(u32), 4, fp);
2771 rc = put_entry(key, 1, len, fp);
2775 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2782 static int type_set_write(struct type_set *t, void *fp)
2787 if (ebitmap_write(&t->types, fp))
2789 if (ebitmap_write(&t->negset, fp))
2792 buf[0] = cpu_to_le32(t->flags);
2793 rc = put_entry(buf, sizeof(u32), 1, fp);
2800 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2803 struct constraint_node *c;
2804 struct constraint_expr *e;
2809 for (c = node; c; c = c->next) {
2811 for (e = c->expr; e; e = e->next)
2813 buf[0] = cpu_to_le32(c->permissions);
2814 buf[1] = cpu_to_le32(nel);
2815 rc = put_entry(buf, sizeof(u32), 2, fp);
2818 for (e = c->expr; e; e = e->next) {
2819 buf[0] = cpu_to_le32(e->expr_type);
2820 buf[1] = cpu_to_le32(e->attr);
2821 buf[2] = cpu_to_le32(e->op);
2822 rc = put_entry(buf, sizeof(u32), 3, fp);
2826 switch (e->expr_type) {
2828 rc = ebitmap_write(&e->names, fp);
2831 if (p->policyvers >=
2832 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2833 rc = type_set_write(e->type_names, fp);
2847 static int class_write(void *vkey, void *datum, void *ptr)
2850 struct class_datum *cladatum = datum;
2851 struct policy_data *pd = ptr;
2853 struct policydb *p = pd->p;
2854 struct constraint_node *c;
2861 if (cladatum->comkey)
2862 len2 = strlen(cladatum->comkey);
2867 for (c = cladatum->constraints; c; c = c->next)
2870 buf[0] = cpu_to_le32(len);
2871 buf[1] = cpu_to_le32(len2);
2872 buf[2] = cpu_to_le32(cladatum->value);
2873 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2874 if (cladatum->permissions.table)
2875 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2878 buf[5] = cpu_to_le32(ncons);
2879 rc = put_entry(buf, sizeof(u32), 6, fp);
2883 rc = put_entry(key, 1, len, fp);
2887 if (cladatum->comkey) {
2888 rc = put_entry(cladatum->comkey, 1, len2, fp);
2893 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2897 rc = write_cons_helper(p, cladatum->constraints, fp);
2901 /* write out the validatetrans rule */
2903 for (c = cladatum->validatetrans; c; c = c->next)
2906 buf[0] = cpu_to_le32(ncons);
2907 rc = put_entry(buf, sizeof(u32), 1, fp);
2911 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2915 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2916 buf[0] = cpu_to_le32(cladatum->default_user);
2917 buf[1] = cpu_to_le32(cladatum->default_role);
2918 buf[2] = cpu_to_le32(cladatum->default_range);
2920 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2925 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2926 buf[0] = cpu_to_le32(cladatum->default_type);
2927 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2935 static int role_write(void *vkey, void *datum, void *ptr)
2938 struct role_datum *role = datum;
2939 struct policy_data *pd = ptr;
2941 struct policydb *p = pd->p;
2948 buf[items++] = cpu_to_le32(len);
2949 buf[items++] = cpu_to_le32(role->value);
2950 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2951 buf[items++] = cpu_to_le32(role->bounds);
2953 BUG_ON(items > ARRAY_SIZE(buf));
2955 rc = put_entry(buf, sizeof(u32), items, fp);
2959 rc = put_entry(key, 1, len, fp);
2963 rc = ebitmap_write(&role->dominates, fp);
2967 rc = ebitmap_write(&role->types, fp);
2974 static int type_write(void *vkey, void *datum, void *ptr)
2977 struct type_datum *typdatum = datum;
2978 struct policy_data *pd = ptr;
2979 struct policydb *p = pd->p;
2987 buf[items++] = cpu_to_le32(len);
2988 buf[items++] = cpu_to_le32(typdatum->value);
2989 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2992 if (typdatum->primary)
2993 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2995 if (typdatum->attribute)
2996 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2998 buf[items++] = cpu_to_le32(properties);
2999 buf[items++] = cpu_to_le32(typdatum->bounds);
3001 buf[items++] = cpu_to_le32(typdatum->primary);
3003 BUG_ON(items > ARRAY_SIZE(buf));
3004 rc = put_entry(buf, sizeof(u32), items, fp);
3008 rc = put_entry(key, 1, len, fp);
3015 static int user_write(void *vkey, void *datum, void *ptr)
3018 struct user_datum *usrdatum = datum;
3019 struct policy_data *pd = ptr;
3020 struct policydb *p = pd->p;
3028 buf[items++] = cpu_to_le32(len);
3029 buf[items++] = cpu_to_le32(usrdatum->value);
3030 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3031 buf[items++] = cpu_to_le32(usrdatum->bounds);
3032 BUG_ON(items > ARRAY_SIZE(buf));
3033 rc = put_entry(buf, sizeof(u32), items, fp);
3037 rc = put_entry(key, 1, len, fp);
3041 rc = ebitmap_write(&usrdatum->roles, fp);
3045 rc = mls_write_range_helper(&usrdatum->range, fp);
3049 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3056 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3069 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3072 unsigned int i, j, rc;
3074 __be64 prefixbuf[1];
3078 for (i = 0; i < info->ocon_num; i++) {
3080 for (c = p->ocontexts[i]; c; c = c->next)
3082 buf[0] = cpu_to_le32(nel);
3083 rc = put_entry(buf, sizeof(u32), 1, fp);
3086 for (c = p->ocontexts[i]; c; c = c->next) {
3089 buf[0] = cpu_to_le32(c->sid[0]);
3090 rc = put_entry(buf, sizeof(u32), 1, fp);
3093 rc = context_write(p, &c->context[0], fp);
3099 len = strlen(c->u.name);
3100 buf[0] = cpu_to_le32(len);
3101 rc = put_entry(buf, sizeof(u32), 1, fp);
3104 rc = put_entry(c->u.name, 1, len, fp);
3107 rc = context_write(p, &c->context[0], fp);
3110 rc = context_write(p, &c->context[1], fp);
3115 buf[0] = cpu_to_le32(c->u.port.protocol);
3116 buf[1] = cpu_to_le32(c->u.port.low_port);
3117 buf[2] = cpu_to_le32(c->u.port.high_port);
3118 rc = put_entry(buf, sizeof(u32), 3, fp);
3121 rc = context_write(p, &c->context[0], fp);
3126 nodebuf[0] = c->u.node.addr; /* network order */
3127 nodebuf[1] = c->u.node.mask; /* network order */
3128 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3131 rc = context_write(p, &c->context[0], fp);
3136 buf[0] = cpu_to_le32(c->v.behavior);
3137 len = strlen(c->u.name);
3138 buf[1] = cpu_to_le32(len);
3139 rc = put_entry(buf, sizeof(u32), 2, fp);
3142 rc = put_entry(c->u.name, 1, len, fp);
3145 rc = context_write(p, &c->context[0], fp);
3150 for (j = 0; j < 4; j++)
3151 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3152 for (j = 0; j < 4; j++)
3153 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3154 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3157 rc = context_write(p, &c->context[0], fp);
3162 /* subnet_prefix is in CPU order */
3163 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3165 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3169 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3170 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3172 rc = put_entry(buf, sizeof(u32), 2, fp);
3175 rc = context_write(p, &c->context[0], fp);
3179 case OCON_IBENDPORT:
3180 len = strlen(c->u.ibendport.dev_name);
3181 buf[0] = cpu_to_le32(len);
3182 buf[1] = cpu_to_le32(c->u.ibendport.port);
3183 rc = put_entry(buf, sizeof(u32), 2, fp);
3186 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3189 rc = context_write(p, &c->context[0], fp);
3199 static int genfs_write(struct policydb *p, void *fp)
3201 struct genfs *genfs;
3208 for (genfs = p->genfs; genfs; genfs = genfs->next)
3210 buf[0] = cpu_to_le32(len);
3211 rc = put_entry(buf, sizeof(u32), 1, fp);
3214 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3215 len = strlen(genfs->fstype);
3216 buf[0] = cpu_to_le32(len);
3217 rc = put_entry(buf, sizeof(u32), 1, fp);
3220 rc = put_entry(genfs->fstype, 1, len, fp);
3224 for (c = genfs->head; c; c = c->next)
3226 buf[0] = cpu_to_le32(len);
3227 rc = put_entry(buf, sizeof(u32), 1, fp);
3230 for (c = genfs->head; c; c = c->next) {
3231 len = strlen(c->u.name);
3232 buf[0] = cpu_to_le32(len);
3233 rc = put_entry(buf, sizeof(u32), 1, fp);
3236 rc = put_entry(c->u.name, 1, len, fp);
3239 buf[0] = cpu_to_le32(c->v.sclass);
3240 rc = put_entry(buf, sizeof(u32), 1, fp);
3243 rc = context_write(p, &c->context[0], fp);
3251 static int hashtab_cnt(void *key, void *data, void *ptr)
3259 static int range_write_helper(void *key, void *data, void *ptr)
3262 struct range_trans *rt = key;
3263 struct mls_range *r = data;
3264 struct policy_data *pd = ptr;
3266 struct policydb *p = pd->p;
3269 buf[0] = cpu_to_le32(rt->source_type);
3270 buf[1] = cpu_to_le32(rt->target_type);
3271 rc = put_entry(buf, sizeof(u32), 2, fp);
3274 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3275 buf[0] = cpu_to_le32(rt->target_class);
3276 rc = put_entry(buf, sizeof(u32), 1, fp);
3280 rc = mls_write_range_helper(r, fp);
3287 static int range_write(struct policydb *p, void *fp)
3291 struct policy_data pd;
3296 /* count the number of entries in the hashtab */
3298 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3302 buf[0] = cpu_to_le32(nel);
3303 rc = put_entry(buf, sizeof(u32), 1, fp);
3307 /* actually write all of the entries */
3308 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3315 static int filename_write_helper(void *key, void *data, void *ptr)
3318 struct filename_trans *ft = key;
3319 struct filename_trans_datum *otype = data;
3324 len = strlen(ft->name);
3325 buf[0] = cpu_to_le32(len);
3326 rc = put_entry(buf, sizeof(u32), 1, fp);
3330 rc = put_entry(ft->name, sizeof(char), len, fp);
3334 buf[0] = cpu_to_le32(ft->stype);
3335 buf[1] = cpu_to_le32(ft->ttype);
3336 buf[2] = cpu_to_le32(ft->tclass);
3337 buf[3] = cpu_to_le32(otype->otype);
3339 rc = put_entry(buf, sizeof(u32), 4, fp);
3346 static int filename_trans_write(struct policydb *p, void *fp)
3352 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3356 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3360 buf[0] = cpu_to_le32(nel);
3361 rc = put_entry(buf, sizeof(u32), 1, fp);
3365 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3373 * Write the configuration data in a policy database
3374 * structure to a policy database binary representation
3377 int policydb_write(struct policydb *p, void *fp)
3379 unsigned int i, num_syms;
3384 struct policydb_compat_info *info;
3387 * refuse to write policy older than compressed avtab
3388 * to simplify the writer. There are other tests dropped
3389 * since we assume this throughout the writer code. Be
3390 * careful if you ever try to remove this restriction
3392 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3393 pr_err("SELinux: refusing to write policy version %d."
3394 " Because it is less than version %d\n", p->policyvers,
3395 POLICYDB_VERSION_AVTAB);
3401 config |= POLICYDB_CONFIG_MLS;
3403 if (p->reject_unknown)
3404 config |= REJECT_UNKNOWN;
3405 if (p->allow_unknown)
3406 config |= ALLOW_UNKNOWN;
3408 /* Write the magic number and string identifiers. */
3409 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3410 len = strlen(POLICYDB_STRING);
3411 buf[1] = cpu_to_le32(len);
3412 rc = put_entry(buf, sizeof(u32), 2, fp);
3415 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3419 /* Write the version, config, and table sizes. */
3420 info = policydb_lookup_compat(p->policyvers);
3422 pr_err("SELinux: compatibility lookup failed for policy "
3423 "version %d", p->policyvers);
3427 buf[0] = cpu_to_le32(p->policyvers);
3428 buf[1] = cpu_to_le32(config);
3429 buf[2] = cpu_to_le32(info->sym_num);
3430 buf[3] = cpu_to_le32(info->ocon_num);
3432 rc = put_entry(buf, sizeof(u32), 4, fp);
3436 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3437 rc = ebitmap_write(&p->policycaps, fp);
3442 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3443 rc = ebitmap_write(&p->permissive_map, fp);
3448 num_syms = info->sym_num;
3449 for (i = 0; i < num_syms; i++) {
3450 struct policy_data pd;
3455 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3456 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3458 rc = put_entry(buf, sizeof(u32), 2, fp);
3461 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3466 rc = avtab_write(p, &p->te_avtab, fp);
3470 rc = cond_write_list(p, p->cond_list, fp);
3474 rc = role_trans_write(p, fp);
3478 rc = role_allow_write(p->role_allow, fp);
3482 rc = filename_trans_write(p, fp);
3486 rc = ocontext_write(p, info, fp);
3490 rc = genfs_write(p, fp);
3494 rc = range_write(p, fp);
3498 for (i = 0; i < p->p_types.nprim; i++) {
3499 struct ebitmap *e = &p->type_attr_map_array[i];
3501 rc = ebitmap_write(e, fp);