2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
10 * Support for enhanced MLS infrastructure.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul@paul-moore.com>
18 * Added support for the policy capability bitmap
20 * Update: Mellanox Techonologies
22 * Added Infiniband support
24 * Copyright (C) 2016 Mellanox Techonologies
25 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/audit.h>
42 #include "conditional.h"
49 static const char *symtab_name[SYM_NUM] = {
61 static unsigned int symtab_sizes[SYM_NUM] = {
72 struct policydb_compat_info {
78 /* These need to be updated if SYM_NUM or OCON_NUM changes */
79 static struct policydb_compat_info policydb_compat[] = {
81 .version = POLICYDB_VERSION_BASE,
82 .sym_num = SYM_NUM - 3,
83 .ocon_num = OCON_NUM - 3,
86 .version = POLICYDB_VERSION_BOOL,
87 .sym_num = SYM_NUM - 2,
88 .ocon_num = OCON_NUM - 3,
91 .version = POLICYDB_VERSION_IPV6,
92 .sym_num = SYM_NUM - 2,
93 .ocon_num = OCON_NUM - 2,
96 .version = POLICYDB_VERSION_NLCLASS,
97 .sym_num = SYM_NUM - 2,
98 .ocon_num = OCON_NUM - 2,
101 .version = POLICYDB_VERSION_MLS,
103 .ocon_num = OCON_NUM - 2,
106 .version = POLICYDB_VERSION_AVTAB,
108 .ocon_num = OCON_NUM - 2,
111 .version = POLICYDB_VERSION_RANGETRANS,
113 .ocon_num = OCON_NUM - 2,
116 .version = POLICYDB_VERSION_POLCAP,
118 .ocon_num = OCON_NUM - 2,
121 .version = POLICYDB_VERSION_PERMISSIVE,
123 .ocon_num = OCON_NUM - 2,
126 .version = POLICYDB_VERSION_BOUNDARY,
128 .ocon_num = OCON_NUM - 2,
131 .version = POLICYDB_VERSION_FILENAME_TRANS,
133 .ocon_num = OCON_NUM - 2,
136 .version = POLICYDB_VERSION_ROLETRANS,
138 .ocon_num = OCON_NUM - 2,
141 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
143 .ocon_num = OCON_NUM - 2,
146 .version = POLICYDB_VERSION_DEFAULT_TYPE,
148 .ocon_num = OCON_NUM - 2,
151 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
153 .ocon_num = OCON_NUM - 2,
156 .version = POLICYDB_VERSION_XPERMS_IOCTL,
158 .ocon_num = OCON_NUM - 2,
161 .version = POLICYDB_VERSION_INFINIBAND,
163 .ocon_num = OCON_NUM,
167 static struct policydb_compat_info *policydb_lookup_compat(int version)
170 struct policydb_compat_info *info = NULL;
172 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
173 if (policydb_compat[i].version == version) {
174 info = &policydb_compat[i];
182 * Initialize the role table.
184 static int roles_init(struct policydb *p)
188 struct role_datum *role;
190 role = kzalloc(sizeof(*role), GFP_KERNEL);
195 role->value = ++p->p_roles.nprim;
196 if (role->value != OBJECT_R_VAL)
200 key = kstrdup(OBJECT_R, GFP_KERNEL);
204 rc = hashtab_insert(p->p_roles.table, key, role);
215 static u32 filenametr_hash(struct hashtab *h, const void *k)
217 const struct filename_trans *ft = k;
219 unsigned int byte_num;
222 hash = ft->stype ^ ft->ttype ^ ft->tclass;
225 while ((focus = ft->name[byte_num++]))
226 hash = partial_name_hash(focus, hash);
227 return hash & (h->size - 1);
230 static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
232 const struct filename_trans *ft1 = k1;
233 const struct filename_trans *ft2 = k2;
236 v = ft1->stype - ft2->stype;
240 v = ft1->ttype - ft2->ttype;
244 v = ft1->tclass - ft2->tclass;
248 return strcmp(ft1->name, ft2->name);
252 static u32 rangetr_hash(struct hashtab *h, const void *k)
254 const struct range_trans *key = k;
255 return (key->source_type + (key->target_type << 3) +
256 (key->target_class << 5)) & (h->size - 1);
259 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
261 const struct range_trans *key1 = k1, *key2 = k2;
264 v = key1->source_type - key2->source_type;
268 v = key1->target_type - key2->target_type;
272 v = key1->target_class - key2->target_class;
278 * Initialize a policy database structure.
280 static int policydb_init(struct policydb *p)
284 memset(p, 0, sizeof(*p));
286 for (i = 0; i < SYM_NUM; i++) {
287 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
292 rc = avtab_init(&p->te_avtab);
300 rc = cond_policydb_init(p);
304 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
305 if (!p->filename_trans) {
310 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
316 ebitmap_init(&p->filename_trans_ttypes);
317 ebitmap_init(&p->policycaps);
318 ebitmap_init(&p->permissive_map);
322 hashtab_destroy(p->filename_trans);
323 hashtab_destroy(p->range_tr);
324 for (i = 0; i < SYM_NUM; i++)
325 hashtab_destroy(p->symtab[i].table);
330 * The following *_index functions are used to
331 * define the val_to_name and val_to_struct arrays
332 * in a policy database structure. The val_to_name
333 * arrays are used when converting security context
334 * structures into string representations. The
335 * val_to_struct arrays are used when the attributes
336 * of a class, role, or user are needed.
339 static int common_index(void *key, void *datum, void *datap)
342 struct common_datum *comdatum;
346 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
349 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
354 static int class_index(void *key, void *datum, void *datap)
357 struct class_datum *cladatum;
361 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
364 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
365 p->class_val_to_struct[cladatum->value - 1] = cladatum;
369 static int role_index(void *key, void *datum, void *datap)
372 struct role_datum *role;
377 || role->value > p->p_roles.nprim
378 || role->bounds > p->p_roles.nprim)
381 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
382 p->role_val_to_struct[role->value - 1] = role;
386 static int type_index(void *key, void *datum, void *datap)
389 struct type_datum *typdatum;
394 if (typdatum->primary) {
396 || typdatum->value > p->p_types.nprim
397 || typdatum->bounds > p->p_types.nprim)
399 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
400 p->type_val_to_struct_array[typdatum->value - 1] = typdatum;
406 static int user_index(void *key, void *datum, void *datap)
409 struct user_datum *usrdatum;
414 || usrdatum->value > p->p_users.nprim
415 || usrdatum->bounds > p->p_users.nprim)
418 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
419 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
423 static int sens_index(void *key, void *datum, void *datap)
426 struct level_datum *levdatum;
431 if (!levdatum->isalias) {
432 if (!levdatum->level->sens ||
433 levdatum->level->sens > p->p_levels.nprim)
436 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
442 static int cat_index(void *key, void *datum, void *datap)
445 struct cat_datum *catdatum;
450 if (!catdatum->isalias) {
451 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
454 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
460 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
473 static void hash_eval(struct hashtab *h, const char *hash_name)
475 struct hashtab_info info;
477 hashtab_stat(h, &info);
478 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
479 "longest chain length %d\n", hash_name, h->nel,
480 info.slots_used, h->size, info.max_chain_len);
483 static void symtab_hash_eval(struct symtab *s)
487 for (i = 0; i < SYM_NUM; i++)
488 hash_eval(s[i].table, symtab_name[i]);
492 static inline void hash_eval(struct hashtab *h, char *hash_name)
498 * Define the other val_to_name and val_to_struct arrays
499 * in a policy database structure.
501 * Caller must clean up on failure.
503 static int policydb_index(struct policydb *p)
508 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
509 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
510 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
512 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
513 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
516 pr_debug("SELinux: %d classes, %d rules\n",
517 p->p_classes.nprim, p->te_avtab.nel);
520 avtab_hash_eval(&p->te_avtab, "rules");
521 symtab_hash_eval(p->symtab);
524 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
525 sizeof(*p->class_val_to_struct),
527 if (!p->class_val_to_struct)
530 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
531 sizeof(*p->role_val_to_struct),
533 if (!p->role_val_to_struct)
536 p->user_val_to_struct = kcalloc(p->p_users.nprim,
537 sizeof(*p->user_val_to_struct),
539 if (!p->user_val_to_struct)
542 p->type_val_to_struct_array = kvcalloc(p->p_types.nprim,
543 sizeof(*p->type_val_to_struct_array),
545 if (!p->type_val_to_struct_array)
548 rc = cond_init_bool_indexes(p);
552 for (i = 0; i < SYM_NUM; i++) {
553 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
556 if (!p->sym_val_to_name[i])
559 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
569 * The following *_destroy functions are used to
570 * free any memory allocated for each kind of
571 * symbol data in the policy database.
574 static int perm_destroy(void *key, void *datum, void *p)
581 static int common_destroy(void *key, void *datum, void *p)
583 struct common_datum *comdatum;
588 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
589 hashtab_destroy(comdatum->permissions.table);
595 static void constraint_expr_destroy(struct constraint_expr *expr)
598 ebitmap_destroy(&expr->names);
599 if (expr->type_names) {
600 ebitmap_destroy(&expr->type_names->types);
601 ebitmap_destroy(&expr->type_names->negset);
602 kfree(expr->type_names);
608 static int cls_destroy(void *key, void *datum, void *p)
610 struct class_datum *cladatum;
611 struct constraint_node *constraint, *ctemp;
612 struct constraint_expr *e, *etmp;
617 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
618 hashtab_destroy(cladatum->permissions.table);
619 constraint = cladatum->constraints;
621 e = constraint->expr;
625 constraint_expr_destroy(etmp);
628 constraint = constraint->next;
632 constraint = cladatum->validatetrans;
634 e = constraint->expr;
638 constraint_expr_destroy(etmp);
641 constraint = constraint->next;
644 kfree(cladatum->comkey);
650 static int role_destroy(void *key, void *datum, void *p)
652 struct role_datum *role;
657 ebitmap_destroy(&role->dominates);
658 ebitmap_destroy(&role->types);
664 static int type_destroy(void *key, void *datum, void *p)
671 static int user_destroy(void *key, void *datum, void *p)
673 struct user_datum *usrdatum;
678 ebitmap_destroy(&usrdatum->roles);
679 ebitmap_destroy(&usrdatum->range.level[0].cat);
680 ebitmap_destroy(&usrdatum->range.level[1].cat);
681 ebitmap_destroy(&usrdatum->dfltlevel.cat);
687 static int sens_destroy(void *key, void *datum, void *p)
689 struct level_datum *levdatum;
695 ebitmap_destroy(&levdatum->level->cat);
696 kfree(levdatum->level);
702 static int cat_destroy(void *key, void *datum, void *p)
709 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
721 static int filenametr_destroy(void *key, void *datum, void *p)
723 struct filename_trans *ft = key;
731 static int range_tr_destroy(void *key, void *datum, void *p)
733 struct mls_range *rt = datum;
735 ebitmap_destroy(&rt->level[0].cat);
736 ebitmap_destroy(&rt->level[1].cat);
742 static void ocontext_destroy(struct ocontext *c, int i)
747 context_destroy(&c->context[0]);
748 context_destroy(&c->context[1]);
749 if (i == OCON_ISID || i == OCON_FS ||
750 i == OCON_NETIF || i == OCON_FSUSE)
756 * Free any memory allocated by a policy database structure.
758 void policydb_destroy(struct policydb *p)
760 struct ocontext *c, *ctmp;
761 struct genfs *g, *gtmp;
763 struct role_allow *ra, *lra = NULL;
764 struct role_trans *tr, *ltr = NULL;
766 for (i = 0; i < SYM_NUM; i++) {
768 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
769 hashtab_destroy(p->symtab[i].table);
772 for (i = 0; i < SYM_NUM; i++)
773 kvfree(p->sym_val_to_name[i]);
775 kfree(p->class_val_to_struct);
776 kfree(p->role_val_to_struct);
777 kfree(p->user_val_to_struct);
778 kvfree(p->type_val_to_struct_array);
780 avtab_destroy(&p->te_avtab);
782 for (i = 0; i < OCON_NUM; i++) {
788 ocontext_destroy(ctmp, i);
790 p->ocontexts[i] = NULL;
801 ocontext_destroy(ctmp, OCON_FSUSE);
809 cond_policydb_destroy(p);
811 for (tr = p->role_tr; tr; tr = tr->next) {
818 for (ra = p->role_allow; ra; ra = ra->next) {
825 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
826 hashtab_destroy(p->filename_trans);
828 hashtab_map(p->range_tr, range_tr_destroy, NULL);
829 hashtab_destroy(p->range_tr);
831 if (p->type_attr_map_array) {
832 for (i = 0; i < p->p_types.nprim; i++)
833 ebitmap_destroy(&p->type_attr_map_array[i]);
834 kvfree(p->type_attr_map_array);
837 ebitmap_destroy(&p->filename_trans_ttypes);
838 ebitmap_destroy(&p->policycaps);
839 ebitmap_destroy(&p->permissive_map);
843 * Load the initial SIDs specified in a policy database
844 * structure into a SID table.
846 int policydb_load_isids(struct policydb *p, struct sidtab *s)
848 struct ocontext *head, *c;
853 pr_err("SELinux: out of memory on SID table init\n");
857 head = p->ocontexts[OCON_ISID];
858 for (c = head; c; c = c->next) {
860 if (!c->context[0].user) {
861 pr_err("SELinux: SID %s was never defined.\n",
866 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
867 pr_err("SELinux: Initial SID %s out of range.\n",
873 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
875 pr_err("SELinux: unable to load initial SID %s.\n",
886 int policydb_class_isvalid(struct policydb *p, unsigned int class)
888 if (!class || class > p->p_classes.nprim)
893 int policydb_role_isvalid(struct policydb *p, unsigned int role)
895 if (!role || role > p->p_roles.nprim)
900 int policydb_type_isvalid(struct policydb *p, unsigned int type)
902 if (!type || type > p->p_types.nprim)
908 * Return 1 if the fields in the security context
909 * structure `c' are valid. Return 0 otherwise.
911 int policydb_context_isvalid(struct policydb *p, struct context *c)
913 struct role_datum *role;
914 struct user_datum *usrdatum;
916 if (!c->role || c->role > p->p_roles.nprim)
919 if (!c->user || c->user > p->p_users.nprim)
922 if (!c->type || c->type > p->p_types.nprim)
925 if (c->role != OBJECT_R_VAL) {
927 * Role must be authorized for the type.
929 role = p->role_val_to_struct[c->role - 1];
930 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
931 /* role may not be associated with type */
935 * User must be authorized for the role.
937 usrdatum = p->user_val_to_struct[c->user - 1];
941 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
942 /* user may not be associated with role */
946 if (!mls_context_isvalid(p, c))
953 * Read a MLS range structure from a policydb binary
954 * representation file.
956 static int mls_read_range_helper(struct mls_range *r, void *fp)
962 rc = next_entry(buf, fp, sizeof(u32));
967 items = le32_to_cpu(buf[0]);
968 if (items > ARRAY_SIZE(buf)) {
969 pr_err("SELinux: mls: range overflow\n");
973 rc = next_entry(buf, fp, sizeof(u32) * items);
975 pr_err("SELinux: mls: truncated range\n");
979 r->level[0].sens = le32_to_cpu(buf[0]);
981 r->level[1].sens = le32_to_cpu(buf[1]);
983 r->level[1].sens = r->level[0].sens;
985 rc = ebitmap_read(&r->level[0].cat, fp);
987 pr_err("SELinux: mls: error reading low categories\n");
991 rc = ebitmap_read(&r->level[1].cat, fp);
993 pr_err("SELinux: mls: error reading high categories\n");
997 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
999 pr_err("SELinux: mls: out of memory\n");
1006 ebitmap_destroy(&r->level[0].cat);
1012 * Read and validate a security context structure
1013 * from a policydb binary representation file.
1015 static int context_read_and_validate(struct context *c,
1022 rc = next_entry(buf, fp, sizeof buf);
1024 pr_err("SELinux: context truncated\n");
1027 c->user = le32_to_cpu(buf[0]);
1028 c->role = le32_to_cpu(buf[1]);
1029 c->type = le32_to_cpu(buf[2]);
1030 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1031 rc = mls_read_range_helper(&c->range, fp);
1033 pr_err("SELinux: error reading MLS range of context\n");
1039 if (!policydb_context_isvalid(p, c)) {
1040 pr_err("SELinux: invalid security context\n");
1050 * The following *_read functions are used to
1051 * read the symbol data from a policy database
1052 * binary representation file.
1055 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1060 if ((len == 0) || (len == (u32)-1))
1063 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1067 /* it's expected the caller should free the str */
1070 rc = next_entry(str, fp, len);
1078 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1081 struct perm_datum *perdatum;
1086 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1090 rc = next_entry(buf, fp, sizeof buf);
1094 len = le32_to_cpu(buf[0]);
1095 perdatum->value = le32_to_cpu(buf[1]);
1097 rc = str_read(&key, GFP_KERNEL, fp, len);
1101 rc = hashtab_insert(h, key, perdatum);
1107 perm_destroy(key, perdatum, NULL);
1111 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1114 struct common_datum *comdatum;
1119 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1123 rc = next_entry(buf, fp, sizeof buf);
1127 len = le32_to_cpu(buf[0]);
1128 comdatum->value = le32_to_cpu(buf[1]);
1130 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1133 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1134 nel = le32_to_cpu(buf[3]);
1136 rc = str_read(&key, GFP_KERNEL, fp, len);
1140 for (i = 0; i < nel; i++) {
1141 rc = perm_read(p, comdatum->permissions.table, fp);
1146 rc = hashtab_insert(h, key, comdatum);
1151 common_destroy(key, comdatum, NULL);
1155 static void type_set_init(struct type_set *t)
1157 ebitmap_init(&t->types);
1158 ebitmap_init(&t->negset);
1161 static int type_set_read(struct type_set *t, void *fp)
1166 if (ebitmap_read(&t->types, fp))
1168 if (ebitmap_read(&t->negset, fp))
1171 rc = next_entry(buf, fp, sizeof(u32));
1174 t->flags = le32_to_cpu(buf[0]);
1180 static int read_cons_helper(struct policydb *p,
1181 struct constraint_node **nodep,
1182 int ncons, int allowxtarget, void *fp)
1184 struct constraint_node *c, *lc;
1185 struct constraint_expr *e, *le;
1188 int rc, i, j, depth;
1191 for (i = 0; i < ncons; i++) {
1192 c = kzalloc(sizeof(*c), GFP_KERNEL);
1201 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1204 c->permissions = le32_to_cpu(buf[0]);
1205 nexpr = le32_to_cpu(buf[1]);
1208 for (j = 0; j < nexpr; j++) {
1209 e = kzalloc(sizeof(*e), GFP_KERNEL);
1218 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1221 e->expr_type = le32_to_cpu(buf[0]);
1222 e->attr = le32_to_cpu(buf[1]);
1223 e->op = le32_to_cpu(buf[2]);
1225 switch (e->expr_type) {
1237 if (depth == (CEXPR_MAXDEPTH - 1))
1242 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1244 if (depth == (CEXPR_MAXDEPTH - 1))
1247 rc = ebitmap_read(&e->names, fp);
1250 if (p->policyvers >=
1251 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1252 e->type_names = kzalloc(sizeof
1257 type_set_init(e->type_names);
1258 rc = type_set_read(e->type_names, fp);
1276 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1279 struct class_datum *cladatum;
1281 u32 len, len2, ncons, nel;
1284 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1288 rc = next_entry(buf, fp, sizeof(u32)*6);
1292 len = le32_to_cpu(buf[0]);
1293 len2 = le32_to_cpu(buf[1]);
1294 cladatum->value = le32_to_cpu(buf[2]);
1296 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1299 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1300 nel = le32_to_cpu(buf[4]);
1302 ncons = le32_to_cpu(buf[5]);
1304 rc = str_read(&key, GFP_KERNEL, fp, len);
1309 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1314 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1315 if (!cladatum->comdatum) {
1316 pr_err("SELinux: unknown common %s\n",
1321 for (i = 0; i < nel; i++) {
1322 rc = perm_read(p, cladatum->permissions.table, fp);
1327 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1331 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1332 /* grab the validatetrans rules */
1333 rc = next_entry(buf, fp, sizeof(u32));
1336 ncons = le32_to_cpu(buf[0]);
1337 rc = read_cons_helper(p, &cladatum->validatetrans,
1343 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1344 rc = next_entry(buf, fp, sizeof(u32) * 3);
1348 cladatum->default_user = le32_to_cpu(buf[0]);
1349 cladatum->default_role = le32_to_cpu(buf[1]);
1350 cladatum->default_range = le32_to_cpu(buf[2]);
1353 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1354 rc = next_entry(buf, fp, sizeof(u32) * 1);
1357 cladatum->default_type = le32_to_cpu(buf[0]);
1360 rc = hashtab_insert(h, key, cladatum);
1366 cls_destroy(key, cladatum, NULL);
1370 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1373 struct role_datum *role;
1374 int rc, to_read = 2;
1378 role = kzalloc(sizeof(*role), GFP_KERNEL);
1382 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1385 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1389 len = le32_to_cpu(buf[0]);
1390 role->value = le32_to_cpu(buf[1]);
1391 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1392 role->bounds = le32_to_cpu(buf[2]);
1394 rc = str_read(&key, GFP_KERNEL, fp, len);
1398 rc = ebitmap_read(&role->dominates, fp);
1402 rc = ebitmap_read(&role->types, fp);
1406 if (strcmp(key, OBJECT_R) == 0) {
1408 if (role->value != OBJECT_R_VAL) {
1409 pr_err("SELinux: Role %s has wrong value %d\n",
1410 OBJECT_R, role->value);
1417 rc = hashtab_insert(h, key, role);
1422 role_destroy(key, role, NULL);
1426 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1429 struct type_datum *typdatum;
1430 int rc, to_read = 3;
1434 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1438 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1441 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1445 len = le32_to_cpu(buf[0]);
1446 typdatum->value = le32_to_cpu(buf[1]);
1447 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1448 u32 prop = le32_to_cpu(buf[2]);
1450 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1451 typdatum->primary = 1;
1452 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1453 typdatum->attribute = 1;
1455 typdatum->bounds = le32_to_cpu(buf[3]);
1457 typdatum->primary = le32_to_cpu(buf[2]);
1460 rc = str_read(&key, GFP_KERNEL, fp, len);
1464 rc = hashtab_insert(h, key, typdatum);
1469 type_destroy(key, typdatum, NULL);
1475 * Read a MLS level structure from a policydb binary
1476 * representation file.
1478 static int mls_read_level(struct mls_level *lp, void *fp)
1483 memset(lp, 0, sizeof(*lp));
1485 rc = next_entry(buf, fp, sizeof buf);
1487 pr_err("SELinux: mls: truncated level\n");
1490 lp->sens = le32_to_cpu(buf[0]);
1492 rc = ebitmap_read(&lp->cat, fp);
1494 pr_err("SELinux: mls: error reading level categories\n");
1500 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1503 struct user_datum *usrdatum;
1504 int rc, to_read = 2;
1508 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1512 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1515 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1519 len = le32_to_cpu(buf[0]);
1520 usrdatum->value = le32_to_cpu(buf[1]);
1521 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1522 usrdatum->bounds = le32_to_cpu(buf[2]);
1524 rc = str_read(&key, GFP_KERNEL, fp, len);
1528 rc = ebitmap_read(&usrdatum->roles, fp);
1532 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1533 rc = mls_read_range_helper(&usrdatum->range, fp);
1536 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1541 rc = hashtab_insert(h, key, usrdatum);
1546 user_destroy(key, usrdatum, NULL);
1550 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1553 struct level_datum *levdatum;
1558 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1562 rc = next_entry(buf, fp, sizeof buf);
1566 len = le32_to_cpu(buf[0]);
1567 levdatum->isalias = le32_to_cpu(buf[1]);
1569 rc = str_read(&key, GFP_ATOMIC, fp, len);
1574 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1575 if (!levdatum->level)
1578 rc = mls_read_level(levdatum->level, fp);
1582 rc = hashtab_insert(h, key, levdatum);
1587 sens_destroy(key, levdatum, NULL);
1591 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1594 struct cat_datum *catdatum;
1599 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1603 rc = next_entry(buf, fp, sizeof buf);
1607 len = le32_to_cpu(buf[0]);
1608 catdatum->value = le32_to_cpu(buf[1]);
1609 catdatum->isalias = le32_to_cpu(buf[2]);
1611 rc = str_read(&key, GFP_ATOMIC, fp, len);
1615 rc = hashtab_insert(h, key, catdatum);
1620 cat_destroy(key, catdatum, NULL);
1624 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1636 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1638 struct user_datum *upper, *user;
1639 struct policydb *p = datap;
1642 upper = user = datum;
1643 while (upper->bounds) {
1644 struct ebitmap_node *node;
1647 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1648 pr_err("SELinux: user %s: "
1649 "too deep or looped boundary",
1654 upper = p->user_val_to_struct[upper->bounds - 1];
1655 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1656 if (ebitmap_get_bit(&upper->roles, bit))
1659 pr_err("SELinux: boundary violated policy: "
1660 "user=%s role=%s bounds=%s\n",
1661 sym_name(p, SYM_USERS, user->value - 1),
1662 sym_name(p, SYM_ROLES, bit),
1663 sym_name(p, SYM_USERS, upper->value - 1));
1672 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1674 struct role_datum *upper, *role;
1675 struct policydb *p = datap;
1678 upper = role = datum;
1679 while (upper->bounds) {
1680 struct ebitmap_node *node;
1683 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1684 pr_err("SELinux: role %s: "
1685 "too deep or looped bounds\n",
1690 upper = p->role_val_to_struct[upper->bounds - 1];
1691 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1692 if (ebitmap_get_bit(&upper->types, bit))
1695 pr_err("SELinux: boundary violated policy: "
1696 "role=%s type=%s bounds=%s\n",
1697 sym_name(p, SYM_ROLES, role->value - 1),
1698 sym_name(p, SYM_TYPES, bit),
1699 sym_name(p, SYM_ROLES, upper->value - 1));
1708 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1710 struct type_datum *upper;
1711 struct policydb *p = datap;
1715 while (upper->bounds) {
1716 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1717 pr_err("SELinux: type %s: "
1718 "too deep or looped boundary\n",
1723 upper = p->type_val_to_struct_array[upper->bounds - 1];
1726 if (upper->attribute) {
1727 pr_err("SELinux: type %s: "
1728 "bounded by attribute %s",
1730 sym_name(p, SYM_TYPES, upper->value - 1));
1738 static int policydb_bounds_sanity_check(struct policydb *p)
1742 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1745 rc = hashtab_map(p->p_users.table,
1746 user_bounds_sanity_check, p);
1750 rc = hashtab_map(p->p_roles.table,
1751 role_bounds_sanity_check, p);
1755 rc = hashtab_map(p->p_types.table,
1756 type_bounds_sanity_check, p);
1763 u16 string_to_security_class(struct policydb *p, const char *name)
1765 struct class_datum *cladatum;
1767 cladatum = hashtab_search(p->p_classes.table, name);
1771 return cladatum->value;
1774 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1776 struct class_datum *cladatum;
1777 struct perm_datum *perdatum = NULL;
1778 struct common_datum *comdatum;
1780 if (!tclass || tclass > p->p_classes.nprim)
1783 cladatum = p->class_val_to_struct[tclass-1];
1784 comdatum = cladatum->comdatum;
1786 perdatum = hashtab_search(comdatum->permissions.table,
1789 perdatum = hashtab_search(cladatum->permissions.table,
1794 return 1U << (perdatum->value-1);
1797 static int range_read(struct policydb *p, void *fp)
1799 struct range_trans *rt = NULL;
1800 struct mls_range *r = NULL;
1805 if (p->policyvers < POLICYDB_VERSION_MLS)
1808 rc = next_entry(buf, fp, sizeof(u32));
1812 nel = le32_to_cpu(buf[0]);
1813 for (i = 0; i < nel; i++) {
1815 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1819 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1823 rt->source_type = le32_to_cpu(buf[0]);
1824 rt->target_type = le32_to_cpu(buf[1]);
1825 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1826 rc = next_entry(buf, fp, sizeof(u32));
1829 rt->target_class = le32_to_cpu(buf[0]);
1831 rt->target_class = p->process_class;
1834 if (!policydb_type_isvalid(p, rt->source_type) ||
1835 !policydb_type_isvalid(p, rt->target_type) ||
1836 !policydb_class_isvalid(p, rt->target_class))
1840 r = kzalloc(sizeof(*r), GFP_KERNEL);
1844 rc = mls_read_range_helper(r, fp);
1849 if (!mls_range_isvalid(p, r)) {
1850 pr_warn("SELinux: rangetrans: invalid range\n");
1854 rc = hashtab_insert(p->range_tr, rt, r);
1861 hash_eval(p->range_tr, "rangetr");
1869 static int filename_trans_read(struct policydb *p, void *fp)
1871 struct filename_trans *ft;
1872 struct filename_trans_datum *otype;
1878 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1881 rc = next_entry(buf, fp, sizeof(u32));
1884 nel = le32_to_cpu(buf[0]);
1886 for (i = 0; i < nel; i++) {
1891 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1896 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1900 /* length of the path component string */
1901 rc = next_entry(buf, fp, sizeof(u32));
1904 len = le32_to_cpu(buf[0]);
1906 /* path component string */
1907 rc = str_read(&name, GFP_KERNEL, fp, len);
1913 rc = next_entry(buf, fp, sizeof(u32) * 4);
1917 ft->stype = le32_to_cpu(buf[0]);
1918 ft->ttype = le32_to_cpu(buf[1]);
1919 ft->tclass = le32_to_cpu(buf[2]);
1921 otype->otype = le32_to_cpu(buf[3]);
1923 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1927 rc = hashtab_insert(p->filename_trans, ft, otype);
1930 * Do not return -EEXIST to the caller, or the system
1935 /* But free memory to avoid memory leak. */
1941 hash_eval(p->filename_trans, "filenametr");
1951 static int genfs_read(struct policydb *p, void *fp)
1954 u32 nel, nel2, len, len2;
1956 struct ocontext *l, *c;
1957 struct ocontext *newc = NULL;
1958 struct genfs *genfs_p, *genfs;
1959 struct genfs *newgenfs = NULL;
1961 rc = next_entry(buf, fp, sizeof(u32));
1964 nel = le32_to_cpu(buf[0]);
1966 for (i = 0; i < nel; i++) {
1967 rc = next_entry(buf, fp, sizeof(u32));
1970 len = le32_to_cpu(buf[0]);
1973 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1977 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
1981 for (genfs_p = NULL, genfs = p->genfs; genfs;
1982 genfs_p = genfs, genfs = genfs->next) {
1984 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1985 pr_err("SELinux: dup genfs fstype %s\n",
1989 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1992 newgenfs->next = genfs;
1994 genfs_p->next = newgenfs;
1996 p->genfs = newgenfs;
2000 rc = next_entry(buf, fp, sizeof(u32));
2004 nel2 = le32_to_cpu(buf[0]);
2005 for (j = 0; j < nel2; j++) {
2006 rc = next_entry(buf, fp, sizeof(u32));
2009 len = le32_to_cpu(buf[0]);
2012 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2016 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2020 rc = next_entry(buf, fp, sizeof(u32));
2024 newc->v.sclass = le32_to_cpu(buf[0]);
2025 rc = context_read_and_validate(&newc->context[0], p, fp);
2029 for (l = NULL, c = genfs->head; c;
2030 l = c, c = c->next) {
2032 if (!strcmp(newc->u.name, c->u.name) &&
2033 (!c->v.sclass || !newc->v.sclass ||
2034 newc->v.sclass == c->v.sclass)) {
2035 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2036 genfs->fstype, c->u.name);
2039 len = strlen(newc->u.name);
2040 len2 = strlen(c->u.name);
2056 kfree(newgenfs->fstype);
2059 ocontext_destroy(newc, OCON_FSUSE);
2064 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2069 __be64 prefixbuf[1];
2071 struct ocontext *l, *c;
2074 for (i = 0; i < info->ocon_num; i++) {
2075 rc = next_entry(buf, fp, sizeof(u32));
2078 nel = le32_to_cpu(buf[0]);
2081 for (j = 0; j < nel; j++) {
2083 c = kzalloc(sizeof(*c), GFP_KERNEL);
2089 p->ocontexts[i] = c;
2094 rc = next_entry(buf, fp, sizeof(u32));
2098 c->sid[0] = le32_to_cpu(buf[0]);
2099 rc = context_read_and_validate(&c->context[0], p, fp);
2105 rc = next_entry(buf, fp, sizeof(u32));
2108 len = le32_to_cpu(buf[0]);
2110 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2114 rc = context_read_and_validate(&c->context[0], p, fp);
2117 rc = context_read_and_validate(&c->context[1], p, fp);
2122 rc = next_entry(buf, fp, sizeof(u32)*3);
2125 c->u.port.protocol = le32_to_cpu(buf[0]);
2126 c->u.port.low_port = le32_to_cpu(buf[1]);
2127 c->u.port.high_port = le32_to_cpu(buf[2]);
2128 rc = context_read_and_validate(&c->context[0], p, fp);
2133 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2136 c->u.node.addr = nodebuf[0]; /* network order */
2137 c->u.node.mask = nodebuf[1]; /* network order */
2138 rc = context_read_and_validate(&c->context[0], p, fp);
2143 rc = next_entry(buf, fp, sizeof(u32)*2);
2148 c->v.behavior = le32_to_cpu(buf[0]);
2149 /* Determined at runtime, not in policy DB. */
2150 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2152 if (c->v.behavior > SECURITY_FS_USE_MAX)
2155 len = le32_to_cpu(buf[1]);
2156 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2160 rc = context_read_and_validate(&c->context[0], p, fp);
2167 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2170 for (k = 0; k < 4; k++)
2171 c->u.node6.addr[k] = nodebuf[k];
2172 for (k = 0; k < 4; k++)
2173 c->u.node6.mask[k] = nodebuf[k+4];
2174 rc = context_read_and_validate(&c->context[0], p, fp);
2180 u32 pkey_lo, pkey_hi;
2182 rc = next_entry(prefixbuf, fp, sizeof(u64));
2186 /* we need to have subnet_prefix in CPU order */
2187 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2189 rc = next_entry(buf, fp, sizeof(u32) * 2);
2193 pkey_lo = le32_to_cpu(buf[0]);
2194 pkey_hi = le32_to_cpu(buf[1]);
2196 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2201 c->u.ibpkey.low_pkey = pkey_lo;
2202 c->u.ibpkey.high_pkey = pkey_hi;
2204 rc = context_read_and_validate(&c->context[0],
2211 case OCON_IBENDPORT: {
2214 rc = next_entry(buf, fp, sizeof(u32) * 2);
2217 len = le32_to_cpu(buf[0]);
2219 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2223 port = le32_to_cpu(buf[1]);
2224 if (port > U8_MAX || port == 0) {
2229 c->u.ibendport.port = port;
2231 rc = context_read_and_validate(&c->context[0],
2247 * Read the configuration data from a policy database binary
2248 * representation file into a policy database structure.
2250 int policydb_read(struct policydb *p, void *fp)
2252 struct role_allow *ra, *lra;
2253 struct role_trans *tr, *ltr;
2256 u32 len, nprim, nel;
2259 struct policydb_compat_info *info;
2261 rc = policydb_init(p);
2265 /* Read the magic number and string length. */
2266 rc = next_entry(buf, fp, sizeof(u32) * 2);
2271 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2272 pr_err("SELinux: policydb magic number 0x%x does "
2273 "not match expected magic number 0x%x\n",
2274 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2279 len = le32_to_cpu(buf[1]);
2280 if (len != strlen(POLICYDB_STRING)) {
2281 pr_err("SELinux: policydb string length %d does not "
2282 "match expected length %zu\n",
2283 len, strlen(POLICYDB_STRING));
2288 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2289 if (!policydb_str) {
2290 pr_err("SELinux: unable to allocate memory for policydb "
2291 "string of length %d\n", len);
2295 rc = next_entry(policydb_str, fp, len);
2297 pr_err("SELinux: truncated policydb string identifier\n");
2298 kfree(policydb_str);
2303 policydb_str[len] = '\0';
2304 if (strcmp(policydb_str, POLICYDB_STRING)) {
2305 pr_err("SELinux: policydb string %s does not match "
2306 "my string %s\n", policydb_str, POLICYDB_STRING);
2307 kfree(policydb_str);
2310 /* Done with policydb_str. */
2311 kfree(policydb_str);
2312 policydb_str = NULL;
2314 /* Read the version and table sizes. */
2315 rc = next_entry(buf, fp, sizeof(u32)*4);
2320 p->policyvers = le32_to_cpu(buf[0]);
2321 if (p->policyvers < POLICYDB_VERSION_MIN ||
2322 p->policyvers > POLICYDB_VERSION_MAX) {
2323 pr_err("SELinux: policydb version %d does not match "
2324 "my version range %d-%d\n",
2325 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2329 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2333 if (p->policyvers < POLICYDB_VERSION_MLS) {
2334 pr_err("SELinux: security policydb version %d "
2335 "(MLS) not backwards compatible\n",
2340 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2341 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2343 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2344 rc = ebitmap_read(&p->policycaps, fp);
2349 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2350 rc = ebitmap_read(&p->permissive_map, fp);
2356 info = policydb_lookup_compat(p->policyvers);
2358 pr_err("SELinux: unable to find policy compat info "
2359 "for version %d\n", p->policyvers);
2364 if (le32_to_cpu(buf[2]) != info->sym_num ||
2365 le32_to_cpu(buf[3]) != info->ocon_num) {
2366 pr_err("SELinux: policydb table sizes (%d,%d) do "
2367 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2368 le32_to_cpu(buf[3]),
2369 info->sym_num, info->ocon_num);
2373 for (i = 0; i < info->sym_num; i++) {
2374 rc = next_entry(buf, fp, sizeof(u32)*2);
2377 nprim = le32_to_cpu(buf[0]);
2378 nel = le32_to_cpu(buf[1]);
2379 for (j = 0; j < nel; j++) {
2380 rc = read_f[i](p, p->symtab[i].table, fp);
2385 p->symtab[i].nprim = nprim;
2389 p->process_class = string_to_security_class(p, "process");
2390 if (!p->process_class)
2393 rc = avtab_read(&p->te_avtab, fp, p);
2397 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2398 rc = cond_read_list(p, fp);
2403 rc = next_entry(buf, fp, sizeof(u32));
2406 nel = le32_to_cpu(buf[0]);
2408 for (i = 0; i < nel; i++) {
2410 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2417 rc = next_entry(buf, fp, sizeof(u32)*3);
2422 tr->role = le32_to_cpu(buf[0]);
2423 tr->type = le32_to_cpu(buf[1]);
2424 tr->new_role = le32_to_cpu(buf[2]);
2425 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2426 rc = next_entry(buf, fp, sizeof(u32));
2429 tr->tclass = le32_to_cpu(buf[0]);
2431 tr->tclass = p->process_class;
2434 if (!policydb_role_isvalid(p, tr->role) ||
2435 !policydb_type_isvalid(p, tr->type) ||
2436 !policydb_class_isvalid(p, tr->tclass) ||
2437 !policydb_role_isvalid(p, tr->new_role))
2442 rc = next_entry(buf, fp, sizeof(u32));
2445 nel = le32_to_cpu(buf[0]);
2447 for (i = 0; i < nel; i++) {
2449 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2456 rc = next_entry(buf, fp, sizeof(u32)*2);
2461 ra->role = le32_to_cpu(buf[0]);
2462 ra->new_role = le32_to_cpu(buf[1]);
2463 if (!policydb_role_isvalid(p, ra->role) ||
2464 !policydb_role_isvalid(p, ra->new_role))
2469 rc = filename_trans_read(p, fp);
2473 rc = policydb_index(p);
2478 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2479 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2480 if (!p->process_trans_perms)
2483 rc = ocontext_read(p, info, fp);
2487 rc = genfs_read(p, fp);
2491 rc = range_read(p, fp);
2495 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2496 sizeof(*p->type_attr_map_array),
2498 if (!p->type_attr_map_array)
2501 /* just in case ebitmap_init() becomes more than just a memset(0): */
2502 for (i = 0; i < p->p_types.nprim; i++)
2503 ebitmap_init(&p->type_attr_map_array[i]);
2505 for (i = 0; i < p->p_types.nprim; i++) {
2506 struct ebitmap *e = &p->type_attr_map_array[i];
2508 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2509 rc = ebitmap_read(e, fp);
2513 /* add the type itself as the degenerate case */
2514 rc = ebitmap_set_bit(e, i, 1);
2519 rc = policydb_bounds_sanity_check(p);
2527 policydb_destroy(p);
2532 * Write a MLS level structure to a policydb binary
2533 * representation file.
2535 static int mls_write_level(struct mls_level *l, void *fp)
2540 buf[0] = cpu_to_le32(l->sens);
2541 rc = put_entry(buf, sizeof(u32), 1, fp);
2545 rc = ebitmap_write(&l->cat, fp);
2553 * Write a MLS range structure to a policydb binary
2554 * representation file.
2556 static int mls_write_range_helper(struct mls_range *r, void *fp)
2562 eq = mls_level_eq(&r->level[1], &r->level[0]);
2568 buf[0] = cpu_to_le32(items-1);
2569 buf[1] = cpu_to_le32(r->level[0].sens);
2571 buf[2] = cpu_to_le32(r->level[1].sens);
2573 BUG_ON(items > ARRAY_SIZE(buf));
2575 rc = put_entry(buf, sizeof(u32), items, fp);
2579 rc = ebitmap_write(&r->level[0].cat, fp);
2583 rc = ebitmap_write(&r->level[1].cat, fp);
2591 static int sens_write(void *vkey, void *datum, void *ptr)
2594 struct level_datum *levdatum = datum;
2595 struct policy_data *pd = ptr;
2602 buf[0] = cpu_to_le32(len);
2603 buf[1] = cpu_to_le32(levdatum->isalias);
2604 rc = put_entry(buf, sizeof(u32), 2, fp);
2608 rc = put_entry(key, 1, len, fp);
2612 rc = mls_write_level(levdatum->level, fp);
2619 static int cat_write(void *vkey, void *datum, void *ptr)
2622 struct cat_datum *catdatum = datum;
2623 struct policy_data *pd = ptr;
2630 buf[0] = cpu_to_le32(len);
2631 buf[1] = cpu_to_le32(catdatum->value);
2632 buf[2] = cpu_to_le32(catdatum->isalias);
2633 rc = put_entry(buf, sizeof(u32), 3, fp);
2637 rc = put_entry(key, 1, len, fp);
2644 static int role_trans_write(struct policydb *p, void *fp)
2646 struct role_trans *r = p->role_tr;
2647 struct role_trans *tr;
2653 for (tr = r; tr; tr = tr->next)
2655 buf[0] = cpu_to_le32(nel);
2656 rc = put_entry(buf, sizeof(u32), 1, fp);
2659 for (tr = r; tr; tr = tr->next) {
2660 buf[0] = cpu_to_le32(tr->role);
2661 buf[1] = cpu_to_le32(tr->type);
2662 buf[2] = cpu_to_le32(tr->new_role);
2663 rc = put_entry(buf, sizeof(u32), 3, fp);
2666 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2667 buf[0] = cpu_to_le32(tr->tclass);
2668 rc = put_entry(buf, sizeof(u32), 1, fp);
2677 static int role_allow_write(struct role_allow *r, void *fp)
2679 struct role_allow *ra;
2685 for (ra = r; ra; ra = ra->next)
2687 buf[0] = cpu_to_le32(nel);
2688 rc = put_entry(buf, sizeof(u32), 1, fp);
2691 for (ra = r; ra; ra = ra->next) {
2692 buf[0] = cpu_to_le32(ra->role);
2693 buf[1] = cpu_to_le32(ra->new_role);
2694 rc = put_entry(buf, sizeof(u32), 2, fp);
2702 * Write a security context structure
2703 * to a policydb binary representation file.
2705 static int context_write(struct policydb *p, struct context *c,
2711 buf[0] = cpu_to_le32(c->user);
2712 buf[1] = cpu_to_le32(c->role);
2713 buf[2] = cpu_to_le32(c->type);
2715 rc = put_entry(buf, sizeof(u32), 3, fp);
2719 rc = mls_write_range_helper(&c->range, fp);
2727 * The following *_write functions are used to
2728 * write the symbol data to a policy database
2729 * binary representation file.
2732 static int perm_write(void *vkey, void *datum, void *fp)
2735 struct perm_datum *perdatum = datum;
2741 buf[0] = cpu_to_le32(len);
2742 buf[1] = cpu_to_le32(perdatum->value);
2743 rc = put_entry(buf, sizeof(u32), 2, fp);
2747 rc = put_entry(key, 1, len, fp);
2754 static int common_write(void *vkey, void *datum, void *ptr)
2757 struct common_datum *comdatum = datum;
2758 struct policy_data *pd = ptr;
2765 buf[0] = cpu_to_le32(len);
2766 buf[1] = cpu_to_le32(comdatum->value);
2767 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2768 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2769 rc = put_entry(buf, sizeof(u32), 4, fp);
2773 rc = put_entry(key, 1, len, fp);
2777 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2784 static int type_set_write(struct type_set *t, void *fp)
2789 if (ebitmap_write(&t->types, fp))
2791 if (ebitmap_write(&t->negset, fp))
2794 buf[0] = cpu_to_le32(t->flags);
2795 rc = put_entry(buf, sizeof(u32), 1, fp);
2802 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2805 struct constraint_node *c;
2806 struct constraint_expr *e;
2811 for (c = node; c; c = c->next) {
2813 for (e = c->expr; e; e = e->next)
2815 buf[0] = cpu_to_le32(c->permissions);
2816 buf[1] = cpu_to_le32(nel);
2817 rc = put_entry(buf, sizeof(u32), 2, fp);
2820 for (e = c->expr; e; e = e->next) {
2821 buf[0] = cpu_to_le32(e->expr_type);
2822 buf[1] = cpu_to_le32(e->attr);
2823 buf[2] = cpu_to_le32(e->op);
2824 rc = put_entry(buf, sizeof(u32), 3, fp);
2828 switch (e->expr_type) {
2830 rc = ebitmap_write(&e->names, fp);
2833 if (p->policyvers >=
2834 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2835 rc = type_set_write(e->type_names, fp);
2849 static int class_write(void *vkey, void *datum, void *ptr)
2852 struct class_datum *cladatum = datum;
2853 struct policy_data *pd = ptr;
2855 struct policydb *p = pd->p;
2856 struct constraint_node *c;
2863 if (cladatum->comkey)
2864 len2 = strlen(cladatum->comkey);
2869 for (c = cladatum->constraints; c; c = c->next)
2872 buf[0] = cpu_to_le32(len);
2873 buf[1] = cpu_to_le32(len2);
2874 buf[2] = cpu_to_le32(cladatum->value);
2875 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2876 if (cladatum->permissions.table)
2877 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2880 buf[5] = cpu_to_le32(ncons);
2881 rc = put_entry(buf, sizeof(u32), 6, fp);
2885 rc = put_entry(key, 1, len, fp);
2889 if (cladatum->comkey) {
2890 rc = put_entry(cladatum->comkey, 1, len2, fp);
2895 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2899 rc = write_cons_helper(p, cladatum->constraints, fp);
2903 /* write out the validatetrans rule */
2905 for (c = cladatum->validatetrans; c; c = c->next)
2908 buf[0] = cpu_to_le32(ncons);
2909 rc = put_entry(buf, sizeof(u32), 1, fp);
2913 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2917 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2918 buf[0] = cpu_to_le32(cladatum->default_user);
2919 buf[1] = cpu_to_le32(cladatum->default_role);
2920 buf[2] = cpu_to_le32(cladatum->default_range);
2922 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2927 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2928 buf[0] = cpu_to_le32(cladatum->default_type);
2929 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2937 static int role_write(void *vkey, void *datum, void *ptr)
2940 struct role_datum *role = datum;
2941 struct policy_data *pd = ptr;
2943 struct policydb *p = pd->p;
2950 buf[items++] = cpu_to_le32(len);
2951 buf[items++] = cpu_to_le32(role->value);
2952 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2953 buf[items++] = cpu_to_le32(role->bounds);
2955 BUG_ON(items > ARRAY_SIZE(buf));
2957 rc = put_entry(buf, sizeof(u32), items, fp);
2961 rc = put_entry(key, 1, len, fp);
2965 rc = ebitmap_write(&role->dominates, fp);
2969 rc = ebitmap_write(&role->types, fp);
2976 static int type_write(void *vkey, void *datum, void *ptr)
2979 struct type_datum *typdatum = datum;
2980 struct policy_data *pd = ptr;
2981 struct policydb *p = pd->p;
2989 buf[items++] = cpu_to_le32(len);
2990 buf[items++] = cpu_to_le32(typdatum->value);
2991 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2994 if (typdatum->primary)
2995 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2997 if (typdatum->attribute)
2998 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3000 buf[items++] = cpu_to_le32(properties);
3001 buf[items++] = cpu_to_le32(typdatum->bounds);
3003 buf[items++] = cpu_to_le32(typdatum->primary);
3005 BUG_ON(items > ARRAY_SIZE(buf));
3006 rc = put_entry(buf, sizeof(u32), items, fp);
3010 rc = put_entry(key, 1, len, fp);
3017 static int user_write(void *vkey, void *datum, void *ptr)
3020 struct user_datum *usrdatum = datum;
3021 struct policy_data *pd = ptr;
3022 struct policydb *p = pd->p;
3030 buf[items++] = cpu_to_le32(len);
3031 buf[items++] = cpu_to_le32(usrdatum->value);
3032 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3033 buf[items++] = cpu_to_le32(usrdatum->bounds);
3034 BUG_ON(items > ARRAY_SIZE(buf));
3035 rc = put_entry(buf, sizeof(u32), items, fp);
3039 rc = put_entry(key, 1, len, fp);
3043 rc = ebitmap_write(&usrdatum->roles, fp);
3047 rc = mls_write_range_helper(&usrdatum->range, fp);
3051 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3058 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3071 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3074 unsigned int i, j, rc;
3076 __be64 prefixbuf[1];
3080 for (i = 0; i < info->ocon_num; i++) {
3082 for (c = p->ocontexts[i]; c; c = c->next)
3084 buf[0] = cpu_to_le32(nel);
3085 rc = put_entry(buf, sizeof(u32), 1, fp);
3088 for (c = p->ocontexts[i]; c; c = c->next) {
3091 buf[0] = cpu_to_le32(c->sid[0]);
3092 rc = put_entry(buf, sizeof(u32), 1, fp);
3095 rc = context_write(p, &c->context[0], fp);
3101 len = strlen(c->u.name);
3102 buf[0] = cpu_to_le32(len);
3103 rc = put_entry(buf, sizeof(u32), 1, fp);
3106 rc = put_entry(c->u.name, 1, len, fp);
3109 rc = context_write(p, &c->context[0], fp);
3112 rc = context_write(p, &c->context[1], fp);
3117 buf[0] = cpu_to_le32(c->u.port.protocol);
3118 buf[1] = cpu_to_le32(c->u.port.low_port);
3119 buf[2] = cpu_to_le32(c->u.port.high_port);
3120 rc = put_entry(buf, sizeof(u32), 3, fp);
3123 rc = context_write(p, &c->context[0], fp);
3128 nodebuf[0] = c->u.node.addr; /* network order */
3129 nodebuf[1] = c->u.node.mask; /* network order */
3130 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3133 rc = context_write(p, &c->context[0], fp);
3138 buf[0] = cpu_to_le32(c->v.behavior);
3139 len = strlen(c->u.name);
3140 buf[1] = cpu_to_le32(len);
3141 rc = put_entry(buf, sizeof(u32), 2, fp);
3144 rc = put_entry(c->u.name, 1, len, fp);
3147 rc = context_write(p, &c->context[0], fp);
3152 for (j = 0; j < 4; j++)
3153 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3154 for (j = 0; j < 4; j++)
3155 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3156 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3159 rc = context_write(p, &c->context[0], fp);
3164 /* subnet_prefix is in CPU order */
3165 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3167 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3171 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3172 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3174 rc = put_entry(buf, sizeof(u32), 2, fp);
3177 rc = context_write(p, &c->context[0], fp);
3181 case OCON_IBENDPORT:
3182 len = strlen(c->u.ibendport.dev_name);
3183 buf[0] = cpu_to_le32(len);
3184 buf[1] = cpu_to_le32(c->u.ibendport.port);
3185 rc = put_entry(buf, sizeof(u32), 2, fp);
3188 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3191 rc = context_write(p, &c->context[0], fp);
3201 static int genfs_write(struct policydb *p, void *fp)
3203 struct genfs *genfs;
3210 for (genfs = p->genfs; genfs; genfs = genfs->next)
3212 buf[0] = cpu_to_le32(len);
3213 rc = put_entry(buf, sizeof(u32), 1, fp);
3216 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3217 len = strlen(genfs->fstype);
3218 buf[0] = cpu_to_le32(len);
3219 rc = put_entry(buf, sizeof(u32), 1, fp);
3222 rc = put_entry(genfs->fstype, 1, len, fp);
3226 for (c = genfs->head; c; c = c->next)
3228 buf[0] = cpu_to_le32(len);
3229 rc = put_entry(buf, sizeof(u32), 1, fp);
3232 for (c = genfs->head; c; c = c->next) {
3233 len = strlen(c->u.name);
3234 buf[0] = cpu_to_le32(len);
3235 rc = put_entry(buf, sizeof(u32), 1, fp);
3238 rc = put_entry(c->u.name, 1, len, fp);
3241 buf[0] = cpu_to_le32(c->v.sclass);
3242 rc = put_entry(buf, sizeof(u32), 1, fp);
3245 rc = context_write(p, &c->context[0], fp);
3253 static int hashtab_cnt(void *key, void *data, void *ptr)
3261 static int range_write_helper(void *key, void *data, void *ptr)
3264 struct range_trans *rt = key;
3265 struct mls_range *r = data;
3266 struct policy_data *pd = ptr;
3268 struct policydb *p = pd->p;
3271 buf[0] = cpu_to_le32(rt->source_type);
3272 buf[1] = cpu_to_le32(rt->target_type);
3273 rc = put_entry(buf, sizeof(u32), 2, fp);
3276 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3277 buf[0] = cpu_to_le32(rt->target_class);
3278 rc = put_entry(buf, sizeof(u32), 1, fp);
3282 rc = mls_write_range_helper(r, fp);
3289 static int range_write(struct policydb *p, void *fp)
3293 struct policy_data pd;
3298 /* count the number of entries in the hashtab */
3300 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3304 buf[0] = cpu_to_le32(nel);
3305 rc = put_entry(buf, sizeof(u32), 1, fp);
3309 /* actually write all of the entries */
3310 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3317 static int filename_write_helper(void *key, void *data, void *ptr)
3320 struct filename_trans *ft = key;
3321 struct filename_trans_datum *otype = data;
3326 len = strlen(ft->name);
3327 buf[0] = cpu_to_le32(len);
3328 rc = put_entry(buf, sizeof(u32), 1, fp);
3332 rc = put_entry(ft->name, sizeof(char), len, fp);
3336 buf[0] = cpu_to_le32(ft->stype);
3337 buf[1] = cpu_to_le32(ft->ttype);
3338 buf[2] = cpu_to_le32(ft->tclass);
3339 buf[3] = cpu_to_le32(otype->otype);
3341 rc = put_entry(buf, sizeof(u32), 4, fp);
3348 static int filename_trans_write(struct policydb *p, void *fp)
3354 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3358 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3362 buf[0] = cpu_to_le32(nel);
3363 rc = put_entry(buf, sizeof(u32), 1, fp);
3367 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3375 * Write the configuration data in a policy database
3376 * structure to a policy database binary representation
3379 int policydb_write(struct policydb *p, void *fp)
3381 unsigned int i, num_syms;
3386 struct policydb_compat_info *info;
3389 * refuse to write policy older than compressed avtab
3390 * to simplify the writer. There are other tests dropped
3391 * since we assume this throughout the writer code. Be
3392 * careful if you ever try to remove this restriction
3394 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3395 pr_err("SELinux: refusing to write policy version %d."
3396 " Because it is less than version %d\n", p->policyvers,
3397 POLICYDB_VERSION_AVTAB);
3403 config |= POLICYDB_CONFIG_MLS;
3405 if (p->reject_unknown)
3406 config |= REJECT_UNKNOWN;
3407 if (p->allow_unknown)
3408 config |= ALLOW_UNKNOWN;
3410 /* Write the magic number and string identifiers. */
3411 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3412 len = strlen(POLICYDB_STRING);
3413 buf[1] = cpu_to_le32(len);
3414 rc = put_entry(buf, sizeof(u32), 2, fp);
3417 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3421 /* Write the version, config, and table sizes. */
3422 info = policydb_lookup_compat(p->policyvers);
3424 pr_err("SELinux: compatibility lookup failed for policy "
3425 "version %d", p->policyvers);
3429 buf[0] = cpu_to_le32(p->policyvers);
3430 buf[1] = cpu_to_le32(config);
3431 buf[2] = cpu_to_le32(info->sym_num);
3432 buf[3] = cpu_to_le32(info->ocon_num);
3434 rc = put_entry(buf, sizeof(u32), 4, fp);
3438 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3439 rc = ebitmap_write(&p->policycaps, fp);
3444 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3445 rc = ebitmap_write(&p->permissive_map, fp);
3450 num_syms = info->sym_num;
3451 for (i = 0; i < num_syms; i++) {
3452 struct policy_data pd;
3457 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3458 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3460 rc = put_entry(buf, sizeof(u32), 2, fp);
3463 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3468 rc = avtab_write(p, &p->te_avtab, fp);
3472 rc = cond_write_list(p, p->cond_list, fp);
3476 rc = role_trans_write(p, fp);
3480 rc = role_allow_write(p->role_allow, fp);
3484 rc = filename_trans_write(p, fp);
3488 rc = ocontext_write(p, info, fp);
3492 rc = genfs_write(p, fp);
3496 rc = range_write(p, fp);
3500 for (i = 0; i < p->p_types.nprim; i++) {
3501 struct ebitmap *e = &p->type_attr_map_array[i];
3503 rc = ebitmap_write(e, fp);