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;
277 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
280 * Initialize a policy database structure.
282 static int policydb_init(struct policydb *p)
286 memset(p, 0, sizeof(*p));
288 for (i = 0; i < SYM_NUM; i++) {
289 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
294 rc = avtab_init(&p->te_avtab);
302 rc = cond_policydb_init(p);
306 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
307 if (!p->filename_trans) {
312 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
318 ebitmap_init(&p->filename_trans_ttypes);
319 ebitmap_init(&p->policycaps);
320 ebitmap_init(&p->permissive_map);
324 hashtab_destroy(p->filename_trans);
325 hashtab_destroy(p->range_tr);
326 for (i = 0; i < SYM_NUM; i++) {
327 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
328 hashtab_destroy(p->symtab[i].table);
334 * The following *_index functions are used to
335 * define the val_to_name and val_to_struct arrays
336 * in a policy database structure. The val_to_name
337 * arrays are used when converting security context
338 * structures into string representations. The
339 * val_to_struct arrays are used when the attributes
340 * of a class, role, or user are needed.
343 static int common_index(void *key, void *datum, void *datap)
346 struct common_datum *comdatum;
350 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
353 p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
358 static int class_index(void *key, void *datum, void *datap)
361 struct class_datum *cladatum;
365 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
368 p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
369 p->class_val_to_struct[cladatum->value - 1] = cladatum;
373 static int role_index(void *key, void *datum, void *datap)
376 struct role_datum *role;
381 || role->value > p->p_roles.nprim
382 || role->bounds > p->p_roles.nprim)
385 p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
386 p->role_val_to_struct[role->value - 1] = role;
390 static int type_index(void *key, void *datum, void *datap)
393 struct type_datum *typdatum;
398 if (typdatum->primary) {
400 || typdatum->value > p->p_types.nprim
401 || typdatum->bounds > p->p_types.nprim)
403 p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
404 p->type_val_to_struct_array[typdatum->value - 1] = typdatum;
410 static int user_index(void *key, void *datum, void *datap)
413 struct user_datum *usrdatum;
418 || usrdatum->value > p->p_users.nprim
419 || usrdatum->bounds > p->p_users.nprim)
422 p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
423 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
427 static int sens_index(void *key, void *datum, void *datap)
430 struct level_datum *levdatum;
435 if (!levdatum->isalias) {
436 if (!levdatum->level->sens ||
437 levdatum->level->sens > p->p_levels.nprim)
440 p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
446 static int cat_index(void *key, void *datum, void *datap)
449 struct cat_datum *catdatum;
454 if (!catdatum->isalias) {
455 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
458 p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
464 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
477 static void hash_eval(struct hashtab *h, const char *hash_name)
479 struct hashtab_info info;
481 hashtab_stat(h, &info);
482 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
483 "longest chain length %d\n", hash_name, h->nel,
484 info.slots_used, h->size, info.max_chain_len);
487 static void symtab_hash_eval(struct symtab *s)
491 for (i = 0; i < SYM_NUM; i++)
492 hash_eval(s[i].table, symtab_name[i]);
496 static inline void hash_eval(struct hashtab *h, char *hash_name)
502 * Define the other val_to_name and val_to_struct arrays
503 * in a policy database structure.
505 * Caller must clean up on failure.
507 static int policydb_index(struct policydb *p)
512 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
513 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
514 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
516 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
517 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
520 pr_debug("SELinux: %d classes, %d rules\n",
521 p->p_classes.nprim, p->te_avtab.nel);
524 avtab_hash_eval(&p->te_avtab, "rules");
525 symtab_hash_eval(p->symtab);
528 p->class_val_to_struct = kcalloc(p->p_classes.nprim,
529 sizeof(*p->class_val_to_struct),
531 if (!p->class_val_to_struct)
534 p->role_val_to_struct = kcalloc(p->p_roles.nprim,
535 sizeof(*p->role_val_to_struct),
537 if (!p->role_val_to_struct)
540 p->user_val_to_struct = kcalloc(p->p_users.nprim,
541 sizeof(*p->user_val_to_struct),
543 if (!p->user_val_to_struct)
546 p->type_val_to_struct_array = kvcalloc(p->p_types.nprim,
547 sizeof(*p->type_val_to_struct_array),
549 if (!p->type_val_to_struct_array)
552 rc = cond_init_bool_indexes(p);
556 for (i = 0; i < SYM_NUM; i++) {
557 p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
560 if (!p->sym_val_to_name[i])
563 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
573 * The following *_destroy functions are used to
574 * free any memory allocated for each kind of
575 * symbol data in the policy database.
578 static int perm_destroy(void *key, void *datum, void *p)
585 static int common_destroy(void *key, void *datum, void *p)
587 struct common_datum *comdatum;
592 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
593 hashtab_destroy(comdatum->permissions.table);
599 static void constraint_expr_destroy(struct constraint_expr *expr)
602 ebitmap_destroy(&expr->names);
603 if (expr->type_names) {
604 ebitmap_destroy(&expr->type_names->types);
605 ebitmap_destroy(&expr->type_names->negset);
606 kfree(expr->type_names);
612 static int cls_destroy(void *key, void *datum, void *p)
614 struct class_datum *cladatum;
615 struct constraint_node *constraint, *ctemp;
616 struct constraint_expr *e, *etmp;
621 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
622 hashtab_destroy(cladatum->permissions.table);
623 constraint = cladatum->constraints;
625 e = constraint->expr;
629 constraint_expr_destroy(etmp);
632 constraint = constraint->next;
636 constraint = cladatum->validatetrans;
638 e = constraint->expr;
642 constraint_expr_destroy(etmp);
645 constraint = constraint->next;
648 kfree(cladatum->comkey);
654 static int role_destroy(void *key, void *datum, void *p)
656 struct role_datum *role;
661 ebitmap_destroy(&role->dominates);
662 ebitmap_destroy(&role->types);
668 static int type_destroy(void *key, void *datum, void *p)
675 static int user_destroy(void *key, void *datum, void *p)
677 struct user_datum *usrdatum;
682 ebitmap_destroy(&usrdatum->roles);
683 ebitmap_destroy(&usrdatum->range.level[0].cat);
684 ebitmap_destroy(&usrdatum->range.level[1].cat);
685 ebitmap_destroy(&usrdatum->dfltlevel.cat);
691 static int sens_destroy(void *key, void *datum, void *p)
693 struct level_datum *levdatum;
699 ebitmap_destroy(&levdatum->level->cat);
700 kfree(levdatum->level);
706 static int cat_destroy(void *key, void *datum, void *p)
713 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
725 static int filenametr_destroy(void *key, void *datum, void *p)
727 struct filename_trans *ft = key;
735 static int range_tr_destroy(void *key, void *datum, void *p)
737 struct mls_range *rt = datum;
739 ebitmap_destroy(&rt->level[0].cat);
740 ebitmap_destroy(&rt->level[1].cat);
746 static void ocontext_destroy(struct ocontext *c, int i)
751 context_destroy(&c->context[0]);
752 context_destroy(&c->context[1]);
753 if (i == OCON_ISID || i == OCON_FS ||
754 i == OCON_NETIF || i == OCON_FSUSE)
760 * Free any memory allocated by a policy database structure.
762 void policydb_destroy(struct policydb *p)
764 struct ocontext *c, *ctmp;
765 struct genfs *g, *gtmp;
767 struct role_allow *ra, *lra = NULL;
768 struct role_trans *tr, *ltr = NULL;
770 for (i = 0; i < SYM_NUM; i++) {
772 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
773 hashtab_destroy(p->symtab[i].table);
776 for (i = 0; i < SYM_NUM; i++)
777 kvfree(p->sym_val_to_name[i]);
779 kfree(p->class_val_to_struct);
780 kfree(p->role_val_to_struct);
781 kfree(p->user_val_to_struct);
782 kvfree(p->type_val_to_struct_array);
784 avtab_destroy(&p->te_avtab);
786 for (i = 0; i < OCON_NUM; i++) {
792 ocontext_destroy(ctmp, i);
794 p->ocontexts[i] = NULL;
805 ocontext_destroy(ctmp, OCON_FSUSE);
813 cond_policydb_destroy(p);
815 for (tr = p->role_tr; tr; tr = tr->next) {
822 for (ra = p->role_allow; ra; ra = ra->next) {
829 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
830 hashtab_destroy(p->filename_trans);
832 hashtab_map(p->range_tr, range_tr_destroy, NULL);
833 hashtab_destroy(p->range_tr);
835 if (p->type_attr_map_array) {
836 for (i = 0; i < p->p_types.nprim; i++)
837 ebitmap_destroy(&p->type_attr_map_array[i]);
838 kvfree(p->type_attr_map_array);
841 ebitmap_destroy(&p->filename_trans_ttypes);
842 ebitmap_destroy(&p->policycaps);
843 ebitmap_destroy(&p->permissive_map);
847 * Load the initial SIDs specified in a policy database
848 * structure into a SID table.
850 int policydb_load_isids(struct policydb *p, struct sidtab *s)
852 struct ocontext *head, *c;
857 pr_err("SELinux: out of memory on SID table init\n");
861 head = p->ocontexts[OCON_ISID];
862 for (c = head; c; c = c->next) {
864 if (!c->context[0].user) {
865 pr_err("SELinux: SID %s was never defined.\n",
870 if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
871 pr_err("SELinux: Initial SID %s out of range.\n",
877 rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
879 pr_err("SELinux: unable to load initial SID %s.\n",
890 int policydb_class_isvalid(struct policydb *p, unsigned int class)
892 if (!class || class > p->p_classes.nprim)
897 int policydb_role_isvalid(struct policydb *p, unsigned int role)
899 if (!role || role > p->p_roles.nprim)
904 int policydb_type_isvalid(struct policydb *p, unsigned int type)
906 if (!type || type > p->p_types.nprim)
912 * Return 1 if the fields in the security context
913 * structure `c' are valid. Return 0 otherwise.
915 int policydb_context_isvalid(struct policydb *p, struct context *c)
917 struct role_datum *role;
918 struct user_datum *usrdatum;
920 if (!c->role || c->role > p->p_roles.nprim)
923 if (!c->user || c->user > p->p_users.nprim)
926 if (!c->type || c->type > p->p_types.nprim)
929 if (c->role != OBJECT_R_VAL) {
931 * Role must be authorized for the type.
933 role = p->role_val_to_struct[c->role - 1];
934 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
935 /* role may not be associated with type */
939 * User must be authorized for the role.
941 usrdatum = p->user_val_to_struct[c->user - 1];
945 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
946 /* user may not be associated with role */
950 if (!mls_context_isvalid(p, c))
957 * Read a MLS range structure from a policydb binary
958 * representation file.
960 static int mls_read_range_helper(struct mls_range *r, void *fp)
966 rc = next_entry(buf, fp, sizeof(u32));
971 items = le32_to_cpu(buf[0]);
972 if (items > ARRAY_SIZE(buf)) {
973 pr_err("SELinux: mls: range overflow\n");
977 rc = next_entry(buf, fp, sizeof(u32) * items);
979 pr_err("SELinux: mls: truncated range\n");
983 r->level[0].sens = le32_to_cpu(buf[0]);
985 r->level[1].sens = le32_to_cpu(buf[1]);
987 r->level[1].sens = r->level[0].sens;
989 rc = ebitmap_read(&r->level[0].cat, fp);
991 pr_err("SELinux: mls: error reading low categories\n");
995 rc = ebitmap_read(&r->level[1].cat, fp);
997 pr_err("SELinux: mls: error reading high categories\n");
1001 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1003 pr_err("SELinux: mls: out of memory\n");
1010 ebitmap_destroy(&r->level[0].cat);
1016 * Read and validate a security context structure
1017 * from a policydb binary representation file.
1019 static int context_read_and_validate(struct context *c,
1026 rc = next_entry(buf, fp, sizeof buf);
1028 pr_err("SELinux: context truncated\n");
1031 c->user = le32_to_cpu(buf[0]);
1032 c->role = le32_to_cpu(buf[1]);
1033 c->type = le32_to_cpu(buf[2]);
1034 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1035 rc = mls_read_range_helper(&c->range, fp);
1037 pr_err("SELinux: error reading MLS range of context\n");
1043 if (!policydb_context_isvalid(p, c)) {
1044 pr_err("SELinux: invalid security context\n");
1054 * The following *_read functions are used to
1055 * read the symbol data from a policy database
1056 * binary representation file.
1059 static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1064 if ((len == 0) || (len == (u32)-1))
1067 str = kmalloc(len + 1, flags | __GFP_NOWARN);
1071 /* it's expected the caller should free the str */
1074 rc = next_entry(str, fp, len);
1082 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1085 struct perm_datum *perdatum;
1090 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1094 rc = next_entry(buf, fp, sizeof buf);
1098 len = le32_to_cpu(buf[0]);
1099 perdatum->value = le32_to_cpu(buf[1]);
1101 rc = str_read(&key, GFP_KERNEL, fp, len);
1105 rc = hashtab_insert(h, key, perdatum);
1111 perm_destroy(key, perdatum, NULL);
1115 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1118 struct common_datum *comdatum;
1123 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1127 rc = next_entry(buf, fp, sizeof buf);
1131 len = le32_to_cpu(buf[0]);
1132 comdatum->value = le32_to_cpu(buf[1]);
1134 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1137 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1138 nel = le32_to_cpu(buf[3]);
1140 rc = str_read(&key, GFP_KERNEL, fp, len);
1144 for (i = 0; i < nel; i++) {
1145 rc = perm_read(p, comdatum->permissions.table, fp);
1150 rc = hashtab_insert(h, key, comdatum);
1155 common_destroy(key, comdatum, NULL);
1159 static void type_set_init(struct type_set *t)
1161 ebitmap_init(&t->types);
1162 ebitmap_init(&t->negset);
1165 static int type_set_read(struct type_set *t, void *fp)
1170 if (ebitmap_read(&t->types, fp))
1172 if (ebitmap_read(&t->negset, fp))
1175 rc = next_entry(buf, fp, sizeof(u32));
1178 t->flags = le32_to_cpu(buf[0]);
1184 static int read_cons_helper(struct policydb *p,
1185 struct constraint_node **nodep,
1186 int ncons, int allowxtarget, void *fp)
1188 struct constraint_node *c, *lc;
1189 struct constraint_expr *e, *le;
1192 int rc, i, j, depth;
1195 for (i = 0; i < ncons; i++) {
1196 c = kzalloc(sizeof(*c), GFP_KERNEL);
1205 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1208 c->permissions = le32_to_cpu(buf[0]);
1209 nexpr = le32_to_cpu(buf[1]);
1212 for (j = 0; j < nexpr; j++) {
1213 e = kzalloc(sizeof(*e), GFP_KERNEL);
1222 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1225 e->expr_type = le32_to_cpu(buf[0]);
1226 e->attr = le32_to_cpu(buf[1]);
1227 e->op = le32_to_cpu(buf[2]);
1229 switch (e->expr_type) {
1241 if (depth == (CEXPR_MAXDEPTH - 1))
1246 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1248 if (depth == (CEXPR_MAXDEPTH - 1))
1251 rc = ebitmap_read(&e->names, fp);
1254 if (p->policyvers >=
1255 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1256 e->type_names = kzalloc(sizeof
1261 type_set_init(e->type_names);
1262 rc = type_set_read(e->type_names, fp);
1280 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1283 struct class_datum *cladatum;
1285 u32 len, len2, ncons, nel;
1288 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1292 rc = next_entry(buf, fp, sizeof(u32)*6);
1296 len = le32_to_cpu(buf[0]);
1297 len2 = le32_to_cpu(buf[1]);
1298 cladatum->value = le32_to_cpu(buf[2]);
1300 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1303 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1304 nel = le32_to_cpu(buf[4]);
1306 ncons = le32_to_cpu(buf[5]);
1308 rc = str_read(&key, GFP_KERNEL, fp, len);
1313 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1318 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1319 if (!cladatum->comdatum) {
1320 pr_err("SELinux: unknown common %s\n",
1325 for (i = 0; i < nel; i++) {
1326 rc = perm_read(p, cladatum->permissions.table, fp);
1331 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1335 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1336 /* grab the validatetrans rules */
1337 rc = next_entry(buf, fp, sizeof(u32));
1340 ncons = le32_to_cpu(buf[0]);
1341 rc = read_cons_helper(p, &cladatum->validatetrans,
1347 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1348 rc = next_entry(buf, fp, sizeof(u32) * 3);
1352 cladatum->default_user = le32_to_cpu(buf[0]);
1353 cladatum->default_role = le32_to_cpu(buf[1]);
1354 cladatum->default_range = le32_to_cpu(buf[2]);
1357 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1358 rc = next_entry(buf, fp, sizeof(u32) * 1);
1361 cladatum->default_type = le32_to_cpu(buf[0]);
1364 rc = hashtab_insert(h, key, cladatum);
1370 cls_destroy(key, cladatum, NULL);
1374 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1377 struct role_datum *role;
1378 int rc, to_read = 2;
1382 role = kzalloc(sizeof(*role), GFP_KERNEL);
1386 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1389 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1393 len = le32_to_cpu(buf[0]);
1394 role->value = le32_to_cpu(buf[1]);
1395 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1396 role->bounds = le32_to_cpu(buf[2]);
1398 rc = str_read(&key, GFP_KERNEL, fp, len);
1402 rc = ebitmap_read(&role->dominates, fp);
1406 rc = ebitmap_read(&role->types, fp);
1410 if (strcmp(key, OBJECT_R) == 0) {
1412 if (role->value != OBJECT_R_VAL) {
1413 pr_err("SELinux: Role %s has wrong value %d\n",
1414 OBJECT_R, role->value);
1421 rc = hashtab_insert(h, key, role);
1426 role_destroy(key, role, NULL);
1430 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1433 struct type_datum *typdatum;
1434 int rc, to_read = 3;
1438 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1442 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1445 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1449 len = le32_to_cpu(buf[0]);
1450 typdatum->value = le32_to_cpu(buf[1]);
1451 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1452 u32 prop = le32_to_cpu(buf[2]);
1454 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1455 typdatum->primary = 1;
1456 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1457 typdatum->attribute = 1;
1459 typdatum->bounds = le32_to_cpu(buf[3]);
1461 typdatum->primary = le32_to_cpu(buf[2]);
1464 rc = str_read(&key, GFP_KERNEL, fp, len);
1468 rc = hashtab_insert(h, key, typdatum);
1473 type_destroy(key, typdatum, NULL);
1479 * Read a MLS level structure from a policydb binary
1480 * representation file.
1482 static int mls_read_level(struct mls_level *lp, void *fp)
1487 memset(lp, 0, sizeof(*lp));
1489 rc = next_entry(buf, fp, sizeof buf);
1491 pr_err("SELinux: mls: truncated level\n");
1494 lp->sens = le32_to_cpu(buf[0]);
1496 rc = ebitmap_read(&lp->cat, fp);
1498 pr_err("SELinux: mls: error reading level categories\n");
1504 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1507 struct user_datum *usrdatum;
1508 int rc, to_read = 2;
1512 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1516 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1519 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1523 len = le32_to_cpu(buf[0]);
1524 usrdatum->value = le32_to_cpu(buf[1]);
1525 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1526 usrdatum->bounds = le32_to_cpu(buf[2]);
1528 rc = str_read(&key, GFP_KERNEL, fp, len);
1532 rc = ebitmap_read(&usrdatum->roles, fp);
1536 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1537 rc = mls_read_range_helper(&usrdatum->range, fp);
1540 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1545 rc = hashtab_insert(h, key, usrdatum);
1550 user_destroy(key, usrdatum, NULL);
1554 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1557 struct level_datum *levdatum;
1562 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1566 rc = next_entry(buf, fp, sizeof buf);
1570 len = le32_to_cpu(buf[0]);
1571 levdatum->isalias = le32_to_cpu(buf[1]);
1573 rc = str_read(&key, GFP_ATOMIC, fp, len);
1578 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1579 if (!levdatum->level)
1582 rc = mls_read_level(levdatum->level, fp);
1586 rc = hashtab_insert(h, key, levdatum);
1591 sens_destroy(key, levdatum, NULL);
1595 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1598 struct cat_datum *catdatum;
1603 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1607 rc = next_entry(buf, fp, sizeof buf);
1611 len = le32_to_cpu(buf[0]);
1612 catdatum->value = le32_to_cpu(buf[1]);
1613 catdatum->isalias = le32_to_cpu(buf[2]);
1615 rc = str_read(&key, GFP_ATOMIC, fp, len);
1619 rc = hashtab_insert(h, key, catdatum);
1624 cat_destroy(key, catdatum, NULL);
1628 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1640 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1642 struct user_datum *upper, *user;
1643 struct policydb *p = datap;
1646 upper = user = datum;
1647 while (upper->bounds) {
1648 struct ebitmap_node *node;
1651 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1652 pr_err("SELinux: user %s: "
1653 "too deep or looped boundary",
1658 upper = p->user_val_to_struct[upper->bounds - 1];
1659 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1660 if (ebitmap_get_bit(&upper->roles, bit))
1663 pr_err("SELinux: boundary violated policy: "
1664 "user=%s role=%s bounds=%s\n",
1665 sym_name(p, SYM_USERS, user->value - 1),
1666 sym_name(p, SYM_ROLES, bit),
1667 sym_name(p, SYM_USERS, upper->value - 1));
1676 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1678 struct role_datum *upper, *role;
1679 struct policydb *p = datap;
1682 upper = role = datum;
1683 while (upper->bounds) {
1684 struct ebitmap_node *node;
1687 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1688 pr_err("SELinux: role %s: "
1689 "too deep or looped bounds\n",
1694 upper = p->role_val_to_struct[upper->bounds - 1];
1695 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1696 if (ebitmap_get_bit(&upper->types, bit))
1699 pr_err("SELinux: boundary violated policy: "
1700 "role=%s type=%s bounds=%s\n",
1701 sym_name(p, SYM_ROLES, role->value - 1),
1702 sym_name(p, SYM_TYPES, bit),
1703 sym_name(p, SYM_ROLES, upper->value - 1));
1712 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1714 struct type_datum *upper;
1715 struct policydb *p = datap;
1719 while (upper->bounds) {
1720 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1721 pr_err("SELinux: type %s: "
1722 "too deep or looped boundary\n",
1727 upper = p->type_val_to_struct_array[upper->bounds - 1];
1730 if (upper->attribute) {
1731 pr_err("SELinux: type %s: "
1732 "bounded by attribute %s",
1734 sym_name(p, SYM_TYPES, upper->value - 1));
1742 static int policydb_bounds_sanity_check(struct policydb *p)
1746 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1749 rc = hashtab_map(p->p_users.table,
1750 user_bounds_sanity_check, p);
1754 rc = hashtab_map(p->p_roles.table,
1755 role_bounds_sanity_check, p);
1759 rc = hashtab_map(p->p_types.table,
1760 type_bounds_sanity_check, p);
1767 u16 string_to_security_class(struct policydb *p, const char *name)
1769 struct class_datum *cladatum;
1771 cladatum = hashtab_search(p->p_classes.table, name);
1775 return cladatum->value;
1778 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1780 struct class_datum *cladatum;
1781 struct perm_datum *perdatum = NULL;
1782 struct common_datum *comdatum;
1784 if (!tclass || tclass > p->p_classes.nprim)
1787 cladatum = p->class_val_to_struct[tclass-1];
1788 comdatum = cladatum->comdatum;
1790 perdatum = hashtab_search(comdatum->permissions.table,
1793 perdatum = hashtab_search(cladatum->permissions.table,
1798 return 1U << (perdatum->value-1);
1801 static int range_read(struct policydb *p, void *fp)
1803 struct range_trans *rt = NULL;
1804 struct mls_range *r = NULL;
1809 if (p->policyvers < POLICYDB_VERSION_MLS)
1812 rc = next_entry(buf, fp, sizeof(u32));
1816 nel = le32_to_cpu(buf[0]);
1817 for (i = 0; i < nel; i++) {
1819 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1823 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1827 rt->source_type = le32_to_cpu(buf[0]);
1828 rt->target_type = le32_to_cpu(buf[1]);
1829 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1830 rc = next_entry(buf, fp, sizeof(u32));
1833 rt->target_class = le32_to_cpu(buf[0]);
1835 rt->target_class = p->process_class;
1838 if (!policydb_type_isvalid(p, rt->source_type) ||
1839 !policydb_type_isvalid(p, rt->target_type) ||
1840 !policydb_class_isvalid(p, rt->target_class))
1844 r = kzalloc(sizeof(*r), GFP_KERNEL);
1848 rc = mls_read_range_helper(r, fp);
1853 if (!mls_range_isvalid(p, r)) {
1854 pr_warn("SELinux: rangetrans: invalid range\n");
1858 rc = hashtab_insert(p->range_tr, rt, r);
1865 hash_eval(p->range_tr, "rangetr");
1873 static int filename_trans_read(struct policydb *p, void *fp)
1875 struct filename_trans *ft;
1876 struct filename_trans_datum *otype;
1882 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1885 rc = next_entry(buf, fp, sizeof(u32));
1888 nel = le32_to_cpu(buf[0]);
1890 for (i = 0; i < nel; i++) {
1895 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1900 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1904 /* length of the path component string */
1905 rc = next_entry(buf, fp, sizeof(u32));
1908 len = le32_to_cpu(buf[0]);
1910 /* path component string */
1911 rc = str_read(&name, GFP_KERNEL, fp, len);
1917 rc = next_entry(buf, fp, sizeof(u32) * 4);
1921 ft->stype = le32_to_cpu(buf[0]);
1922 ft->ttype = le32_to_cpu(buf[1]);
1923 ft->tclass = le32_to_cpu(buf[2]);
1925 otype->otype = le32_to_cpu(buf[3]);
1927 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1931 rc = hashtab_insert(p->filename_trans, ft, otype);
1934 * Do not return -EEXIST to the caller, or the system
1939 /* But free memory to avoid memory leak. */
1945 hash_eval(p->filename_trans, "filenametr");
1955 static int genfs_read(struct policydb *p, void *fp)
1958 u32 nel, nel2, len, len2;
1960 struct ocontext *l, *c;
1961 struct ocontext *newc = NULL;
1962 struct genfs *genfs_p, *genfs;
1963 struct genfs *newgenfs = NULL;
1965 rc = next_entry(buf, fp, sizeof(u32));
1968 nel = le32_to_cpu(buf[0]);
1970 for (i = 0; i < nel; i++) {
1971 rc = next_entry(buf, fp, sizeof(u32));
1974 len = le32_to_cpu(buf[0]);
1977 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1981 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
1985 for (genfs_p = NULL, genfs = p->genfs; genfs;
1986 genfs_p = genfs, genfs = genfs->next) {
1988 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1989 pr_err("SELinux: dup genfs fstype %s\n",
1993 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1996 newgenfs->next = genfs;
1998 genfs_p->next = newgenfs;
2000 p->genfs = newgenfs;
2004 rc = next_entry(buf, fp, sizeof(u32));
2008 nel2 = le32_to_cpu(buf[0]);
2009 for (j = 0; j < nel2; j++) {
2010 rc = next_entry(buf, fp, sizeof(u32));
2013 len = le32_to_cpu(buf[0]);
2016 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2020 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2024 rc = next_entry(buf, fp, sizeof(u32));
2028 newc->v.sclass = le32_to_cpu(buf[0]);
2029 rc = context_read_and_validate(&newc->context[0], p, fp);
2033 for (l = NULL, c = genfs->head; c;
2034 l = c, c = c->next) {
2036 if (!strcmp(newc->u.name, c->u.name) &&
2037 (!c->v.sclass || !newc->v.sclass ||
2038 newc->v.sclass == c->v.sclass)) {
2039 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2040 genfs->fstype, c->u.name);
2043 len = strlen(newc->u.name);
2044 len2 = strlen(c->u.name);
2060 kfree(newgenfs->fstype);
2063 ocontext_destroy(newc, OCON_FSUSE);
2068 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2073 __be64 prefixbuf[1];
2075 struct ocontext *l, *c;
2078 for (i = 0; i < info->ocon_num; i++) {
2079 rc = next_entry(buf, fp, sizeof(u32));
2082 nel = le32_to_cpu(buf[0]);
2085 for (j = 0; j < nel; j++) {
2087 c = kzalloc(sizeof(*c), GFP_KERNEL);
2093 p->ocontexts[i] = c;
2098 rc = next_entry(buf, fp, sizeof(u32));
2102 c->sid[0] = le32_to_cpu(buf[0]);
2103 rc = context_read_and_validate(&c->context[0], p, fp);
2109 rc = next_entry(buf, fp, sizeof(u32));
2112 len = le32_to_cpu(buf[0]);
2114 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2118 rc = context_read_and_validate(&c->context[0], p, fp);
2121 rc = context_read_and_validate(&c->context[1], p, fp);
2126 rc = next_entry(buf, fp, sizeof(u32)*3);
2129 c->u.port.protocol = le32_to_cpu(buf[0]);
2130 c->u.port.low_port = le32_to_cpu(buf[1]);
2131 c->u.port.high_port = le32_to_cpu(buf[2]);
2132 rc = context_read_and_validate(&c->context[0], p, fp);
2137 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2140 c->u.node.addr = nodebuf[0]; /* network order */
2141 c->u.node.mask = nodebuf[1]; /* network order */
2142 rc = context_read_and_validate(&c->context[0], p, fp);
2147 rc = next_entry(buf, fp, sizeof(u32)*2);
2152 c->v.behavior = le32_to_cpu(buf[0]);
2153 /* Determined at runtime, not in policy DB. */
2154 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2156 if (c->v.behavior > SECURITY_FS_USE_MAX)
2159 len = le32_to_cpu(buf[1]);
2160 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2164 rc = context_read_and_validate(&c->context[0], p, fp);
2171 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2174 for (k = 0; k < 4; k++)
2175 c->u.node6.addr[k] = nodebuf[k];
2176 for (k = 0; k < 4; k++)
2177 c->u.node6.mask[k] = nodebuf[k+4];
2178 rc = context_read_and_validate(&c->context[0], p, fp);
2184 u32 pkey_lo, pkey_hi;
2186 rc = next_entry(prefixbuf, fp, sizeof(u64));
2190 /* we need to have subnet_prefix in CPU order */
2191 c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
2193 rc = next_entry(buf, fp, sizeof(u32) * 2);
2197 pkey_lo = le32_to_cpu(buf[0]);
2198 pkey_hi = le32_to_cpu(buf[1]);
2200 if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
2205 c->u.ibpkey.low_pkey = pkey_lo;
2206 c->u.ibpkey.high_pkey = pkey_hi;
2208 rc = context_read_and_validate(&c->context[0],
2215 case OCON_IBENDPORT: {
2218 rc = next_entry(buf, fp, sizeof(u32) * 2);
2221 len = le32_to_cpu(buf[0]);
2223 rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
2227 port = le32_to_cpu(buf[1]);
2228 if (port > U8_MAX || port == 0) {
2233 c->u.ibendport.port = port;
2235 rc = context_read_and_validate(&c->context[0],
2251 * Read the configuration data from a policy database binary
2252 * representation file into a policy database structure.
2254 int policydb_read(struct policydb *p, void *fp)
2256 struct role_allow *ra, *lra;
2257 struct role_trans *tr, *ltr;
2260 u32 len, nprim, nel;
2263 struct policydb_compat_info *info;
2265 rc = policydb_init(p);
2269 /* Read the magic number and string length. */
2270 rc = next_entry(buf, fp, sizeof(u32) * 2);
2275 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2276 pr_err("SELinux: policydb magic number 0x%x does "
2277 "not match expected magic number 0x%x\n",
2278 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2283 len = le32_to_cpu(buf[1]);
2284 if (len != strlen(POLICYDB_STRING)) {
2285 pr_err("SELinux: policydb string length %d does not "
2286 "match expected length %zu\n",
2287 len, strlen(POLICYDB_STRING));
2292 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2293 if (!policydb_str) {
2294 pr_err("SELinux: unable to allocate memory for policydb "
2295 "string of length %d\n", len);
2299 rc = next_entry(policydb_str, fp, len);
2301 pr_err("SELinux: truncated policydb string identifier\n");
2302 kfree(policydb_str);
2307 policydb_str[len] = '\0';
2308 if (strcmp(policydb_str, POLICYDB_STRING)) {
2309 pr_err("SELinux: policydb string %s does not match "
2310 "my string %s\n", policydb_str, POLICYDB_STRING);
2311 kfree(policydb_str);
2314 /* Done with policydb_str. */
2315 kfree(policydb_str);
2316 policydb_str = NULL;
2318 /* Read the version and table sizes. */
2319 rc = next_entry(buf, fp, sizeof(u32)*4);
2324 p->policyvers = le32_to_cpu(buf[0]);
2325 if (p->policyvers < POLICYDB_VERSION_MIN ||
2326 p->policyvers > POLICYDB_VERSION_MAX) {
2327 pr_err("SELinux: policydb version %d does not match "
2328 "my version range %d-%d\n",
2329 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2333 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2337 if (p->policyvers < POLICYDB_VERSION_MLS) {
2338 pr_err("SELinux: security policydb version %d "
2339 "(MLS) not backwards compatible\n",
2344 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2345 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2347 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2348 rc = ebitmap_read(&p->policycaps, fp);
2353 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2354 rc = ebitmap_read(&p->permissive_map, fp);
2360 info = policydb_lookup_compat(p->policyvers);
2362 pr_err("SELinux: unable to find policy compat info "
2363 "for version %d\n", p->policyvers);
2368 if (le32_to_cpu(buf[2]) != info->sym_num ||
2369 le32_to_cpu(buf[3]) != info->ocon_num) {
2370 pr_err("SELinux: policydb table sizes (%d,%d) do "
2371 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2372 le32_to_cpu(buf[3]),
2373 info->sym_num, info->ocon_num);
2377 for (i = 0; i < info->sym_num; i++) {
2378 rc = next_entry(buf, fp, sizeof(u32)*2);
2381 nprim = le32_to_cpu(buf[0]);
2382 nel = le32_to_cpu(buf[1]);
2383 for (j = 0; j < nel; j++) {
2384 rc = read_f[i](p, p->symtab[i].table, fp);
2389 p->symtab[i].nprim = nprim;
2393 p->process_class = string_to_security_class(p, "process");
2394 if (!p->process_class)
2397 rc = avtab_read(&p->te_avtab, fp, p);
2401 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2402 rc = cond_read_list(p, fp);
2407 rc = next_entry(buf, fp, sizeof(u32));
2410 nel = le32_to_cpu(buf[0]);
2412 for (i = 0; i < nel; i++) {
2414 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2421 rc = next_entry(buf, fp, sizeof(u32)*3);
2426 tr->role = le32_to_cpu(buf[0]);
2427 tr->type = le32_to_cpu(buf[1]);
2428 tr->new_role = le32_to_cpu(buf[2]);
2429 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2430 rc = next_entry(buf, fp, sizeof(u32));
2433 tr->tclass = le32_to_cpu(buf[0]);
2435 tr->tclass = p->process_class;
2438 if (!policydb_role_isvalid(p, tr->role) ||
2439 !policydb_type_isvalid(p, tr->type) ||
2440 !policydb_class_isvalid(p, tr->tclass) ||
2441 !policydb_role_isvalid(p, tr->new_role))
2446 rc = next_entry(buf, fp, sizeof(u32));
2449 nel = le32_to_cpu(buf[0]);
2451 for (i = 0; i < nel; i++) {
2453 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2460 rc = next_entry(buf, fp, sizeof(u32)*2);
2465 ra->role = le32_to_cpu(buf[0]);
2466 ra->new_role = le32_to_cpu(buf[1]);
2467 if (!policydb_role_isvalid(p, ra->role) ||
2468 !policydb_role_isvalid(p, ra->new_role))
2473 rc = filename_trans_read(p, fp);
2477 rc = policydb_index(p);
2482 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2483 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2484 if (!p->process_trans_perms)
2487 rc = ocontext_read(p, info, fp);
2491 rc = genfs_read(p, fp);
2495 rc = range_read(p, fp);
2499 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2500 sizeof(*p->type_attr_map_array),
2502 if (!p->type_attr_map_array)
2505 /* just in case ebitmap_init() becomes more than just a memset(0): */
2506 for (i = 0; i < p->p_types.nprim; i++)
2507 ebitmap_init(&p->type_attr_map_array[i]);
2509 for (i = 0; i < p->p_types.nprim; i++) {
2510 struct ebitmap *e = &p->type_attr_map_array[i];
2512 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2513 rc = ebitmap_read(e, fp);
2517 /* add the type itself as the degenerate case */
2518 rc = ebitmap_set_bit(e, i, 1);
2523 rc = policydb_bounds_sanity_check(p);
2531 policydb_destroy(p);
2536 * Write a MLS level structure to a policydb binary
2537 * representation file.
2539 static int mls_write_level(struct mls_level *l, void *fp)
2544 buf[0] = cpu_to_le32(l->sens);
2545 rc = put_entry(buf, sizeof(u32), 1, fp);
2549 rc = ebitmap_write(&l->cat, fp);
2557 * Write a MLS range structure to a policydb binary
2558 * representation file.
2560 static int mls_write_range_helper(struct mls_range *r, void *fp)
2566 eq = mls_level_eq(&r->level[1], &r->level[0]);
2572 buf[0] = cpu_to_le32(items-1);
2573 buf[1] = cpu_to_le32(r->level[0].sens);
2575 buf[2] = cpu_to_le32(r->level[1].sens);
2577 BUG_ON(items > ARRAY_SIZE(buf));
2579 rc = put_entry(buf, sizeof(u32), items, fp);
2583 rc = ebitmap_write(&r->level[0].cat, fp);
2587 rc = ebitmap_write(&r->level[1].cat, fp);
2595 static int sens_write(void *vkey, void *datum, void *ptr)
2598 struct level_datum *levdatum = datum;
2599 struct policy_data *pd = ptr;
2606 buf[0] = cpu_to_le32(len);
2607 buf[1] = cpu_to_le32(levdatum->isalias);
2608 rc = put_entry(buf, sizeof(u32), 2, fp);
2612 rc = put_entry(key, 1, len, fp);
2616 rc = mls_write_level(levdatum->level, fp);
2623 static int cat_write(void *vkey, void *datum, void *ptr)
2626 struct cat_datum *catdatum = datum;
2627 struct policy_data *pd = ptr;
2634 buf[0] = cpu_to_le32(len);
2635 buf[1] = cpu_to_le32(catdatum->value);
2636 buf[2] = cpu_to_le32(catdatum->isalias);
2637 rc = put_entry(buf, sizeof(u32), 3, fp);
2641 rc = put_entry(key, 1, len, fp);
2648 static int role_trans_write(struct policydb *p, void *fp)
2650 struct role_trans *r = p->role_tr;
2651 struct role_trans *tr;
2657 for (tr = r; tr; tr = tr->next)
2659 buf[0] = cpu_to_le32(nel);
2660 rc = put_entry(buf, sizeof(u32), 1, fp);
2663 for (tr = r; tr; tr = tr->next) {
2664 buf[0] = cpu_to_le32(tr->role);
2665 buf[1] = cpu_to_le32(tr->type);
2666 buf[2] = cpu_to_le32(tr->new_role);
2667 rc = put_entry(buf, sizeof(u32), 3, fp);
2670 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2671 buf[0] = cpu_to_le32(tr->tclass);
2672 rc = put_entry(buf, sizeof(u32), 1, fp);
2681 static int role_allow_write(struct role_allow *r, void *fp)
2683 struct role_allow *ra;
2689 for (ra = r; ra; ra = ra->next)
2691 buf[0] = cpu_to_le32(nel);
2692 rc = put_entry(buf, sizeof(u32), 1, fp);
2695 for (ra = r; ra; ra = ra->next) {
2696 buf[0] = cpu_to_le32(ra->role);
2697 buf[1] = cpu_to_le32(ra->new_role);
2698 rc = put_entry(buf, sizeof(u32), 2, fp);
2706 * Write a security context structure
2707 * to a policydb binary representation file.
2709 static int context_write(struct policydb *p, struct context *c,
2715 buf[0] = cpu_to_le32(c->user);
2716 buf[1] = cpu_to_le32(c->role);
2717 buf[2] = cpu_to_le32(c->type);
2719 rc = put_entry(buf, sizeof(u32), 3, fp);
2723 rc = mls_write_range_helper(&c->range, fp);
2731 * The following *_write functions are used to
2732 * write the symbol data to a policy database
2733 * binary representation file.
2736 static int perm_write(void *vkey, void *datum, void *fp)
2739 struct perm_datum *perdatum = datum;
2745 buf[0] = cpu_to_le32(len);
2746 buf[1] = cpu_to_le32(perdatum->value);
2747 rc = put_entry(buf, sizeof(u32), 2, fp);
2751 rc = put_entry(key, 1, len, fp);
2758 static int common_write(void *vkey, void *datum, void *ptr)
2761 struct common_datum *comdatum = datum;
2762 struct policy_data *pd = ptr;
2769 buf[0] = cpu_to_le32(len);
2770 buf[1] = cpu_to_le32(comdatum->value);
2771 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2772 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2773 rc = put_entry(buf, sizeof(u32), 4, fp);
2777 rc = put_entry(key, 1, len, fp);
2781 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2788 static int type_set_write(struct type_set *t, void *fp)
2793 if (ebitmap_write(&t->types, fp))
2795 if (ebitmap_write(&t->negset, fp))
2798 buf[0] = cpu_to_le32(t->flags);
2799 rc = put_entry(buf, sizeof(u32), 1, fp);
2806 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2809 struct constraint_node *c;
2810 struct constraint_expr *e;
2815 for (c = node; c; c = c->next) {
2817 for (e = c->expr; e; e = e->next)
2819 buf[0] = cpu_to_le32(c->permissions);
2820 buf[1] = cpu_to_le32(nel);
2821 rc = put_entry(buf, sizeof(u32), 2, fp);
2824 for (e = c->expr; e; e = e->next) {
2825 buf[0] = cpu_to_le32(e->expr_type);
2826 buf[1] = cpu_to_le32(e->attr);
2827 buf[2] = cpu_to_le32(e->op);
2828 rc = put_entry(buf, sizeof(u32), 3, fp);
2832 switch (e->expr_type) {
2834 rc = ebitmap_write(&e->names, fp);
2837 if (p->policyvers >=
2838 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2839 rc = type_set_write(e->type_names, fp);
2853 static int class_write(void *vkey, void *datum, void *ptr)
2856 struct class_datum *cladatum = datum;
2857 struct policy_data *pd = ptr;
2859 struct policydb *p = pd->p;
2860 struct constraint_node *c;
2867 if (cladatum->comkey)
2868 len2 = strlen(cladatum->comkey);
2873 for (c = cladatum->constraints; c; c = c->next)
2876 buf[0] = cpu_to_le32(len);
2877 buf[1] = cpu_to_le32(len2);
2878 buf[2] = cpu_to_le32(cladatum->value);
2879 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2880 if (cladatum->permissions.table)
2881 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2884 buf[5] = cpu_to_le32(ncons);
2885 rc = put_entry(buf, sizeof(u32), 6, fp);
2889 rc = put_entry(key, 1, len, fp);
2893 if (cladatum->comkey) {
2894 rc = put_entry(cladatum->comkey, 1, len2, fp);
2899 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2903 rc = write_cons_helper(p, cladatum->constraints, fp);
2907 /* write out the validatetrans rule */
2909 for (c = cladatum->validatetrans; c; c = c->next)
2912 buf[0] = cpu_to_le32(ncons);
2913 rc = put_entry(buf, sizeof(u32), 1, fp);
2917 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2921 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2922 buf[0] = cpu_to_le32(cladatum->default_user);
2923 buf[1] = cpu_to_le32(cladatum->default_role);
2924 buf[2] = cpu_to_le32(cladatum->default_range);
2926 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2931 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2932 buf[0] = cpu_to_le32(cladatum->default_type);
2933 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2941 static int role_write(void *vkey, void *datum, void *ptr)
2944 struct role_datum *role = datum;
2945 struct policy_data *pd = ptr;
2947 struct policydb *p = pd->p;
2954 buf[items++] = cpu_to_le32(len);
2955 buf[items++] = cpu_to_le32(role->value);
2956 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2957 buf[items++] = cpu_to_le32(role->bounds);
2959 BUG_ON(items > ARRAY_SIZE(buf));
2961 rc = put_entry(buf, sizeof(u32), items, fp);
2965 rc = put_entry(key, 1, len, fp);
2969 rc = ebitmap_write(&role->dominates, fp);
2973 rc = ebitmap_write(&role->types, fp);
2980 static int type_write(void *vkey, void *datum, void *ptr)
2983 struct type_datum *typdatum = datum;
2984 struct policy_data *pd = ptr;
2985 struct policydb *p = pd->p;
2993 buf[items++] = cpu_to_le32(len);
2994 buf[items++] = cpu_to_le32(typdatum->value);
2995 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2998 if (typdatum->primary)
2999 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3001 if (typdatum->attribute)
3002 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3004 buf[items++] = cpu_to_le32(properties);
3005 buf[items++] = cpu_to_le32(typdatum->bounds);
3007 buf[items++] = cpu_to_le32(typdatum->primary);
3009 BUG_ON(items > ARRAY_SIZE(buf));
3010 rc = put_entry(buf, sizeof(u32), items, fp);
3014 rc = put_entry(key, 1, len, fp);
3021 static int user_write(void *vkey, void *datum, void *ptr)
3024 struct user_datum *usrdatum = datum;
3025 struct policy_data *pd = ptr;
3026 struct policydb *p = pd->p;
3034 buf[items++] = cpu_to_le32(len);
3035 buf[items++] = cpu_to_le32(usrdatum->value);
3036 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3037 buf[items++] = cpu_to_le32(usrdatum->bounds);
3038 BUG_ON(items > ARRAY_SIZE(buf));
3039 rc = put_entry(buf, sizeof(u32), items, fp);
3043 rc = put_entry(key, 1, len, fp);
3047 rc = ebitmap_write(&usrdatum->roles, fp);
3051 rc = mls_write_range_helper(&usrdatum->range, fp);
3055 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3062 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3075 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3078 unsigned int i, j, rc;
3080 __be64 prefixbuf[1];
3084 for (i = 0; i < info->ocon_num; i++) {
3086 for (c = p->ocontexts[i]; c; c = c->next)
3088 buf[0] = cpu_to_le32(nel);
3089 rc = put_entry(buf, sizeof(u32), 1, fp);
3092 for (c = p->ocontexts[i]; c; c = c->next) {
3095 buf[0] = cpu_to_le32(c->sid[0]);
3096 rc = put_entry(buf, sizeof(u32), 1, fp);
3099 rc = context_write(p, &c->context[0], fp);
3105 len = strlen(c->u.name);
3106 buf[0] = cpu_to_le32(len);
3107 rc = put_entry(buf, sizeof(u32), 1, fp);
3110 rc = put_entry(c->u.name, 1, len, fp);
3113 rc = context_write(p, &c->context[0], fp);
3116 rc = context_write(p, &c->context[1], fp);
3121 buf[0] = cpu_to_le32(c->u.port.protocol);
3122 buf[1] = cpu_to_le32(c->u.port.low_port);
3123 buf[2] = cpu_to_le32(c->u.port.high_port);
3124 rc = put_entry(buf, sizeof(u32), 3, fp);
3127 rc = context_write(p, &c->context[0], fp);
3132 nodebuf[0] = c->u.node.addr; /* network order */
3133 nodebuf[1] = c->u.node.mask; /* network order */
3134 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3137 rc = context_write(p, &c->context[0], fp);
3142 buf[0] = cpu_to_le32(c->v.behavior);
3143 len = strlen(c->u.name);
3144 buf[1] = cpu_to_le32(len);
3145 rc = put_entry(buf, sizeof(u32), 2, fp);
3148 rc = put_entry(c->u.name, 1, len, fp);
3151 rc = context_write(p, &c->context[0], fp);
3156 for (j = 0; j < 4; j++)
3157 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3158 for (j = 0; j < 4; j++)
3159 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3160 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3163 rc = context_write(p, &c->context[0], fp);
3168 /* subnet_prefix is in CPU order */
3169 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3171 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3175 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3176 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3178 rc = put_entry(buf, sizeof(u32), 2, fp);
3181 rc = context_write(p, &c->context[0], fp);
3185 case OCON_IBENDPORT:
3186 len = strlen(c->u.ibendport.dev_name);
3187 buf[0] = cpu_to_le32(len);
3188 buf[1] = cpu_to_le32(c->u.ibendport.port);
3189 rc = put_entry(buf, sizeof(u32), 2, fp);
3192 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3195 rc = context_write(p, &c->context[0], fp);
3205 static int genfs_write(struct policydb *p, void *fp)
3207 struct genfs *genfs;
3214 for (genfs = p->genfs; genfs; genfs = genfs->next)
3216 buf[0] = cpu_to_le32(len);
3217 rc = put_entry(buf, sizeof(u32), 1, fp);
3220 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3221 len = strlen(genfs->fstype);
3222 buf[0] = cpu_to_le32(len);
3223 rc = put_entry(buf, sizeof(u32), 1, fp);
3226 rc = put_entry(genfs->fstype, 1, len, fp);
3230 for (c = genfs->head; c; c = c->next)
3232 buf[0] = cpu_to_le32(len);
3233 rc = put_entry(buf, sizeof(u32), 1, fp);
3236 for (c = genfs->head; c; c = c->next) {
3237 len = strlen(c->u.name);
3238 buf[0] = cpu_to_le32(len);
3239 rc = put_entry(buf, sizeof(u32), 1, fp);
3242 rc = put_entry(c->u.name, 1, len, fp);
3245 buf[0] = cpu_to_le32(c->v.sclass);
3246 rc = put_entry(buf, sizeof(u32), 1, fp);
3249 rc = context_write(p, &c->context[0], fp);
3257 static int hashtab_cnt(void *key, void *data, void *ptr)
3265 static int range_write_helper(void *key, void *data, void *ptr)
3268 struct range_trans *rt = key;
3269 struct mls_range *r = data;
3270 struct policy_data *pd = ptr;
3272 struct policydb *p = pd->p;
3275 buf[0] = cpu_to_le32(rt->source_type);
3276 buf[1] = cpu_to_le32(rt->target_type);
3277 rc = put_entry(buf, sizeof(u32), 2, fp);
3280 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3281 buf[0] = cpu_to_le32(rt->target_class);
3282 rc = put_entry(buf, sizeof(u32), 1, fp);
3286 rc = mls_write_range_helper(r, fp);
3293 static int range_write(struct policydb *p, void *fp)
3297 struct policy_data pd;
3302 /* count the number of entries in the hashtab */
3304 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3308 buf[0] = cpu_to_le32(nel);
3309 rc = put_entry(buf, sizeof(u32), 1, fp);
3313 /* actually write all of the entries */
3314 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3321 static int filename_write_helper(void *key, void *data, void *ptr)
3324 struct filename_trans *ft = key;
3325 struct filename_trans_datum *otype = data;
3330 len = strlen(ft->name);
3331 buf[0] = cpu_to_le32(len);
3332 rc = put_entry(buf, sizeof(u32), 1, fp);
3336 rc = put_entry(ft->name, sizeof(char), len, fp);
3340 buf[0] = cpu_to_le32(ft->stype);
3341 buf[1] = cpu_to_le32(ft->ttype);
3342 buf[2] = cpu_to_le32(ft->tclass);
3343 buf[3] = cpu_to_le32(otype->otype);
3345 rc = put_entry(buf, sizeof(u32), 4, fp);
3352 static int filename_trans_write(struct policydb *p, void *fp)
3358 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3362 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3366 buf[0] = cpu_to_le32(nel);
3367 rc = put_entry(buf, sizeof(u32), 1, fp);
3371 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3379 * Write the configuration data in a policy database
3380 * structure to a policy database binary representation
3383 int policydb_write(struct policydb *p, void *fp)
3385 unsigned int i, num_syms;
3390 struct policydb_compat_info *info;
3393 * refuse to write policy older than compressed avtab
3394 * to simplify the writer. There are other tests dropped
3395 * since we assume this throughout the writer code. Be
3396 * careful if you ever try to remove this restriction
3398 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3399 pr_err("SELinux: refusing to write policy version %d."
3400 " Because it is less than version %d\n", p->policyvers,
3401 POLICYDB_VERSION_AVTAB);
3407 config |= POLICYDB_CONFIG_MLS;
3409 if (p->reject_unknown)
3410 config |= REJECT_UNKNOWN;
3411 if (p->allow_unknown)
3412 config |= ALLOW_UNKNOWN;
3414 /* Write the magic number and string identifiers. */
3415 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3416 len = strlen(POLICYDB_STRING);
3417 buf[1] = cpu_to_le32(len);
3418 rc = put_entry(buf, sizeof(u32), 2, fp);
3421 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3425 /* Write the version, config, and table sizes. */
3426 info = policydb_lookup_compat(p->policyvers);
3428 pr_err("SELinux: compatibility lookup failed for policy "
3429 "version %d", p->policyvers);
3433 buf[0] = cpu_to_le32(p->policyvers);
3434 buf[1] = cpu_to_le32(config);
3435 buf[2] = cpu_to_le32(info->sym_num);
3436 buf[3] = cpu_to_le32(info->ocon_num);
3438 rc = put_entry(buf, sizeof(u32), 4, fp);
3442 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3443 rc = ebitmap_write(&p->policycaps, fp);
3448 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3449 rc = ebitmap_write(&p->permissive_map, fp);
3454 num_syms = info->sym_num;
3455 for (i = 0; i < num_syms; i++) {
3456 struct policy_data pd;
3461 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3462 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3464 rc = put_entry(buf, sizeof(u32), 2, fp);
3467 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3472 rc = avtab_write(p, &p->te_avtab, fp);
3476 rc = cond_write_list(p, p->cond_list, fp);
3480 rc = role_trans_write(p, fp);
3484 rc = role_allow_write(p->role_allow, fp);
3488 rc = filename_trans_write(p, fp);
3492 rc = ocontext_write(p, info, fp);
3496 rc = genfs_write(p, fp);
3500 rc = range_write(p, fp);
3504 for (i = 0; i < p->p_types.nprim; i++) {
3505 struct ebitmap *e = &p->type_attr_map_array[i];
3507 rc = ebitmap_write(e, fp);