2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
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.moore@hp.com>
18 * Added support for the policy capability bitmap
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
21 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, version 2.
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/audit.h>
37 #include "conditional.h"
43 static const char *symtab_name[SYM_NUM] = {
55 static unsigned int symtab_sizes[SYM_NUM] = {
66 struct policydb_compat_info {
72 /* These need to be updated if SYM_NUM or OCON_NUM changes */
73 static struct policydb_compat_info policydb_compat[] = {
75 .version = POLICYDB_VERSION_BASE,
76 .sym_num = SYM_NUM - 3,
77 .ocon_num = OCON_NUM - 1,
80 .version = POLICYDB_VERSION_BOOL,
81 .sym_num = SYM_NUM - 2,
82 .ocon_num = OCON_NUM - 1,
85 .version = POLICYDB_VERSION_IPV6,
86 .sym_num = SYM_NUM - 2,
90 .version = POLICYDB_VERSION_NLCLASS,
91 .sym_num = SYM_NUM - 2,
95 .version = POLICYDB_VERSION_MLS,
100 .version = POLICYDB_VERSION_AVTAB,
102 .ocon_num = OCON_NUM,
105 .version = POLICYDB_VERSION_RANGETRANS,
107 .ocon_num = OCON_NUM,
110 .version = POLICYDB_VERSION_POLCAP,
112 .ocon_num = OCON_NUM,
115 .version = POLICYDB_VERSION_PERMISSIVE,
117 .ocon_num = OCON_NUM,
120 .version = POLICYDB_VERSION_BOUNDARY,
122 .ocon_num = OCON_NUM,
126 static struct policydb_compat_info *policydb_lookup_compat(int version)
129 struct policydb_compat_info *info = NULL;
131 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
132 if (policydb_compat[i].version == version) {
133 info = &policydb_compat[i];
141 * Initialize the role table.
143 static int roles_init(struct policydb *p)
147 struct role_datum *role;
149 role = kzalloc(sizeof(*role), GFP_KERNEL);
154 role->value = ++p->p_roles.nprim;
155 if (role->value != OBJECT_R_VAL) {
159 key = kstrdup(OBJECT_R, GFP_KERNEL);
164 rc = hashtab_insert(p->p_roles.table, key, role);
177 static u32 rangetr_hash(struct hashtab *h, const void *k)
179 const struct range_trans *key = k;
180 return (key->source_type + (key->target_type << 3) +
181 (key->target_class << 5)) & (h->size - 1);
184 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
186 const struct range_trans *key1 = k1, *key2 = k2;
187 return (key1->source_type != key2->source_type ||
188 key1->target_type != key2->target_type ||
189 key1->target_class != key2->target_class);
193 * Initialize a policy database structure.
195 static int policydb_init(struct policydb *p)
199 memset(p, 0, sizeof(*p));
201 for (i = 0; i < SYM_NUM; i++) {
202 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
204 goto out_free_symtab;
207 rc = avtab_init(&p->te_avtab);
209 goto out_free_symtab;
213 goto out_free_symtab;
215 rc = cond_policydb_init(p);
217 goto out_free_symtab;
219 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
221 goto out_free_symtab;
223 ebitmap_init(&p->policycaps);
224 ebitmap_init(&p->permissive_map);
230 for (i = 0; i < SYM_NUM; i++)
231 hashtab_destroy(p->symtab[i].table);
236 * The following *_index functions are used to
237 * define the val_to_name and val_to_struct arrays
238 * in a policy database structure. The val_to_name
239 * arrays are used when converting security context
240 * structures into string representations. The
241 * val_to_struct arrays are used when the attributes
242 * of a class, role, or user are needed.
245 static int common_index(void *key, void *datum, void *datap)
248 struct common_datum *comdatum;
252 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
254 p->p_common_val_to_name[comdatum->value - 1] = key;
258 static int class_index(void *key, void *datum, void *datap)
261 struct class_datum *cladatum;
265 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
267 p->p_class_val_to_name[cladatum->value - 1] = key;
268 p->class_val_to_struct[cladatum->value - 1] = cladatum;
272 static int role_index(void *key, void *datum, void *datap)
275 struct role_datum *role;
280 || role->value > p->p_roles.nprim
281 || role->bounds > p->p_roles.nprim)
283 p->p_role_val_to_name[role->value - 1] = key;
284 p->role_val_to_struct[role->value - 1] = role;
288 static int type_index(void *key, void *datum, void *datap)
291 struct type_datum *typdatum;
296 if (typdatum->primary) {
298 || typdatum->value > p->p_types.nprim
299 || typdatum->bounds > p->p_types.nprim)
301 p->p_type_val_to_name[typdatum->value - 1] = key;
302 p->type_val_to_struct[typdatum->value - 1] = typdatum;
308 static int user_index(void *key, void *datum, void *datap)
311 struct user_datum *usrdatum;
316 || usrdatum->value > p->p_users.nprim
317 || usrdatum->bounds > p->p_users.nprim)
319 p->p_user_val_to_name[usrdatum->value - 1] = key;
320 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
324 static int sens_index(void *key, void *datum, void *datap)
327 struct level_datum *levdatum;
332 if (!levdatum->isalias) {
333 if (!levdatum->level->sens ||
334 levdatum->level->sens > p->p_levels.nprim)
336 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
342 static int cat_index(void *key, void *datum, void *datap)
345 struct cat_datum *catdatum;
350 if (!catdatum->isalias) {
351 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
353 p->p_cat_val_to_name[catdatum->value - 1] = key;
359 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
372 * Define the common val_to_name array and the class
373 * val_to_name and val_to_struct arrays in a policy
374 * database structure.
376 * Caller must clean up upon failure.
378 static int policydb_index_classes(struct policydb *p)
382 p->p_common_val_to_name =
383 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
384 if (!p->p_common_val_to_name) {
389 rc = hashtab_map(p->p_commons.table, common_index, p);
393 p->class_val_to_struct =
394 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
395 if (!p->class_val_to_struct) {
400 p->p_class_val_to_name =
401 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
402 if (!p->p_class_val_to_name) {
407 rc = hashtab_map(p->p_classes.table, class_index, p);
413 static void symtab_hash_eval(struct symtab *s)
417 for (i = 0; i < SYM_NUM; i++) {
418 struct hashtab *h = s[i].table;
419 struct hashtab_info info;
421 hashtab_stat(h, &info);
422 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
423 "longest chain length %d\n", symtab_name[i], h->nel,
424 info.slots_used, h->size, info.max_chain_len);
428 static void rangetr_hash_eval(struct hashtab *h)
430 struct hashtab_info info;
432 hashtab_stat(h, &info);
433 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
434 "longest chain length %d\n", h->nel,
435 info.slots_used, h->size, info.max_chain_len);
438 static inline void rangetr_hash_eval(struct hashtab *h)
444 * Define the other val_to_name and val_to_struct arrays
445 * in a policy database structure.
447 * Caller must clean up on failure.
449 static int policydb_index_others(struct policydb *p)
453 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
454 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
456 printk(", %d sens, %d cats", p->p_levels.nprim,
460 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
461 p->p_classes.nprim, p->te_avtab.nel);
464 avtab_hash_eval(&p->te_avtab, "rules");
465 symtab_hash_eval(p->symtab);
468 p->role_val_to_struct =
469 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
471 if (!p->role_val_to_struct) {
476 p->user_val_to_struct =
477 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
479 if (!p->user_val_to_struct) {
484 p->type_val_to_struct =
485 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
487 if (!p->type_val_to_struct) {
492 if (cond_init_bool_indexes(p)) {
497 for (i = SYM_ROLES; i < SYM_NUM; i++) {
498 p->sym_val_to_name[i] =
499 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
500 if (!p->sym_val_to_name[i]) {
504 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
514 * The following *_destroy functions are used to
515 * free any memory allocated for each kind of
516 * symbol data in the policy database.
519 static int perm_destroy(void *key, void *datum, void *p)
526 static int common_destroy(void *key, void *datum, void *p)
528 struct common_datum *comdatum;
532 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
533 hashtab_destroy(comdatum->permissions.table);
538 static int cls_destroy(void *key, void *datum, void *p)
540 struct class_datum *cladatum;
541 struct constraint_node *constraint, *ctemp;
542 struct constraint_expr *e, *etmp;
546 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
547 hashtab_destroy(cladatum->permissions.table);
548 constraint = cladatum->constraints;
550 e = constraint->expr;
552 ebitmap_destroy(&e->names);
558 constraint = constraint->next;
562 constraint = cladatum->validatetrans;
564 e = constraint->expr;
566 ebitmap_destroy(&e->names);
572 constraint = constraint->next;
576 kfree(cladatum->comkey);
581 static int role_destroy(void *key, void *datum, void *p)
583 struct role_datum *role;
587 ebitmap_destroy(&role->dominates);
588 ebitmap_destroy(&role->types);
593 static int type_destroy(void *key, void *datum, void *p)
600 static int user_destroy(void *key, void *datum, void *p)
602 struct user_datum *usrdatum;
606 ebitmap_destroy(&usrdatum->roles);
607 ebitmap_destroy(&usrdatum->range.level[0].cat);
608 ebitmap_destroy(&usrdatum->range.level[1].cat);
609 ebitmap_destroy(&usrdatum->dfltlevel.cat);
614 static int sens_destroy(void *key, void *datum, void *p)
616 struct level_datum *levdatum;
620 ebitmap_destroy(&levdatum->level->cat);
621 kfree(levdatum->level);
626 static int cat_destroy(void *key, void *datum, void *p)
633 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
645 static int range_tr_destroy(void *key, void *datum, void *p)
647 struct mls_range *rt = datum;
649 ebitmap_destroy(&rt->level[0].cat);
650 ebitmap_destroy(&rt->level[1].cat);
656 static void ocontext_destroy(struct ocontext *c, int i)
661 context_destroy(&c->context[0]);
662 context_destroy(&c->context[1]);
663 if (i == OCON_ISID || i == OCON_FS ||
664 i == OCON_NETIF || i == OCON_FSUSE)
670 * Free any memory allocated by a policy database structure.
672 void policydb_destroy(struct policydb *p)
674 struct ocontext *c, *ctmp;
675 struct genfs *g, *gtmp;
677 struct role_allow *ra, *lra = NULL;
678 struct role_trans *tr, *ltr = NULL;
680 for (i = 0; i < SYM_NUM; i++) {
682 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
683 hashtab_destroy(p->symtab[i].table);
686 for (i = 0; i < SYM_NUM; i++)
687 kfree(p->sym_val_to_name[i]);
689 kfree(p->class_val_to_struct);
690 kfree(p->role_val_to_struct);
691 kfree(p->user_val_to_struct);
692 kfree(p->type_val_to_struct);
694 avtab_destroy(&p->te_avtab);
696 for (i = 0; i < OCON_NUM; i++) {
702 ocontext_destroy(ctmp, i);
704 p->ocontexts[i] = NULL;
715 ocontext_destroy(ctmp, OCON_FSUSE);
723 cond_policydb_destroy(p);
725 for (tr = p->role_tr; tr; tr = tr->next) {
732 for (ra = p->role_allow; ra; ra = ra->next) {
739 hashtab_map(p->range_tr, range_tr_destroy, NULL);
740 hashtab_destroy(p->range_tr);
742 if (p->type_attr_map) {
743 for (i = 0; i < p->p_types.nprim; i++)
744 ebitmap_destroy(&p->type_attr_map[i]);
746 kfree(p->type_attr_map);
747 ebitmap_destroy(&p->policycaps);
748 ebitmap_destroy(&p->permissive_map);
754 * Load the initial SIDs specified in a policy database
755 * structure into a SID table.
757 int policydb_load_isids(struct policydb *p, struct sidtab *s)
759 struct ocontext *head, *c;
764 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
768 head = p->ocontexts[OCON_ISID];
769 for (c = head; c; c = c->next) {
770 if (!c->context[0].user) {
771 printk(KERN_ERR "SELinux: SID %s was never "
772 "defined.\n", c->u.name);
776 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
777 printk(KERN_ERR "SELinux: unable to load initial "
778 "SID %s.\n", c->u.name);
787 int policydb_class_isvalid(struct policydb *p, unsigned int class)
789 if (!class || class > p->p_classes.nprim)
794 int policydb_role_isvalid(struct policydb *p, unsigned int role)
796 if (!role || role > p->p_roles.nprim)
801 int policydb_type_isvalid(struct policydb *p, unsigned int type)
803 if (!type || type > p->p_types.nprim)
809 * Return 1 if the fields in the security context
810 * structure `c' are valid. Return 0 otherwise.
812 int policydb_context_isvalid(struct policydb *p, struct context *c)
814 struct role_datum *role;
815 struct user_datum *usrdatum;
817 if (!c->role || c->role > p->p_roles.nprim)
820 if (!c->user || c->user > p->p_users.nprim)
823 if (!c->type || c->type > p->p_types.nprim)
826 if (c->role != OBJECT_R_VAL) {
828 * Role must be authorized for the type.
830 role = p->role_val_to_struct[c->role - 1];
831 if (!ebitmap_get_bit(&role->types,
833 /* role may not be associated with type */
837 * User must be authorized for the role.
839 usrdatum = p->user_val_to_struct[c->user - 1];
843 if (!ebitmap_get_bit(&usrdatum->roles,
845 /* user may not be associated with role */
849 if (!mls_context_isvalid(p, c))
856 * Read a MLS range structure from a policydb binary
857 * representation file.
859 static int mls_read_range_helper(struct mls_range *r, void *fp)
865 rc = next_entry(buf, fp, sizeof(u32));
869 items = le32_to_cpu(buf[0]);
870 if (items > ARRAY_SIZE(buf)) {
871 printk(KERN_ERR "SELinux: mls: range overflow\n");
875 rc = next_entry(buf, fp, sizeof(u32) * items);
877 printk(KERN_ERR "SELinux: mls: truncated range\n");
880 r->level[0].sens = le32_to_cpu(buf[0]);
882 r->level[1].sens = le32_to_cpu(buf[1]);
884 r->level[1].sens = r->level[0].sens;
886 rc = ebitmap_read(&r->level[0].cat, fp);
888 printk(KERN_ERR "SELinux: mls: error reading low "
893 rc = ebitmap_read(&r->level[1].cat, fp);
895 printk(KERN_ERR "SELinux: mls: error reading high "
900 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
902 printk(KERN_ERR "SELinux: mls: out of memory\n");
911 ebitmap_destroy(&r->level[0].cat);
916 * Read and validate a security context structure
917 * from a policydb binary representation file.
919 static int context_read_and_validate(struct context *c,
926 rc = next_entry(buf, fp, sizeof buf);
928 printk(KERN_ERR "SELinux: context truncated\n");
931 c->user = le32_to_cpu(buf[0]);
932 c->role = le32_to_cpu(buf[1]);
933 c->type = le32_to_cpu(buf[2]);
934 if (p->policyvers >= POLICYDB_VERSION_MLS) {
935 if (mls_read_range_helper(&c->range, fp)) {
936 printk(KERN_ERR "SELinux: error reading MLS range of "
943 if (!policydb_context_isvalid(p, c)) {
944 printk(KERN_ERR "SELinux: invalid security context\n");
953 * The following *_read functions are used to
954 * read the symbol data from a policy database
955 * binary representation file.
958 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
961 struct perm_datum *perdatum;
966 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
972 rc = next_entry(buf, fp, sizeof buf);
976 len = le32_to_cpu(buf[0]);
977 perdatum->value = le32_to_cpu(buf[1]);
979 key = kmalloc(len + 1, GFP_KERNEL);
984 rc = next_entry(key, fp, len);
989 rc = hashtab_insert(h, key, perdatum);
995 perm_destroy(key, perdatum, NULL);
999 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1002 struct common_datum *comdatum;
1007 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1013 rc = next_entry(buf, fp, sizeof buf);
1017 len = le32_to_cpu(buf[0]);
1018 comdatum->value = le32_to_cpu(buf[1]);
1020 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1023 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1024 nel = le32_to_cpu(buf[3]);
1026 key = kmalloc(len + 1, GFP_KERNEL);
1031 rc = next_entry(key, fp, len);
1036 for (i = 0; i < nel; i++) {
1037 rc = perm_read(p, comdatum->permissions.table, fp);
1042 rc = hashtab_insert(h, key, comdatum);
1048 common_destroy(key, comdatum, NULL);
1052 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1053 int allowxtarget, void *fp)
1055 struct constraint_node *c, *lc;
1056 struct constraint_expr *e, *le;
1059 int rc, i, j, depth;
1062 for (i = 0; i < ncons; i++) {
1063 c = kzalloc(sizeof(*c), GFP_KERNEL);
1072 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1075 c->permissions = le32_to_cpu(buf[0]);
1076 nexpr = le32_to_cpu(buf[1]);
1079 for (j = 0; j < nexpr; j++) {
1080 e = kzalloc(sizeof(*e), GFP_KERNEL);
1089 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1092 e->expr_type = le32_to_cpu(buf[0]);
1093 e->attr = le32_to_cpu(buf[1]);
1094 e->op = le32_to_cpu(buf[2]);
1096 switch (e->expr_type) {
1108 if (depth == (CEXPR_MAXDEPTH - 1))
1113 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1115 if (depth == (CEXPR_MAXDEPTH - 1))
1118 if (ebitmap_read(&e->names, fp))
1134 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1137 struct class_datum *cladatum;
1139 u32 len, len2, ncons, nel;
1142 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1148 rc = next_entry(buf, fp, sizeof(u32)*6);
1152 len = le32_to_cpu(buf[0]);
1153 len2 = le32_to_cpu(buf[1]);
1154 cladatum->value = le32_to_cpu(buf[2]);
1156 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1159 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1160 nel = le32_to_cpu(buf[4]);
1162 ncons = le32_to_cpu(buf[5]);
1164 key = kmalloc(len + 1, GFP_KERNEL);
1169 rc = next_entry(key, fp, len);
1175 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1176 if (!cladatum->comkey) {
1180 rc = next_entry(cladatum->comkey, fp, len2);
1183 cladatum->comkey[len2] = '\0';
1185 cladatum->comdatum = hashtab_search(p->p_commons.table,
1187 if (!cladatum->comdatum) {
1188 printk(KERN_ERR "SELinux: unknown common %s\n",
1194 for (i = 0; i < nel; i++) {
1195 rc = perm_read(p, cladatum->permissions.table, fp);
1200 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1204 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1205 /* grab the validatetrans rules */
1206 rc = next_entry(buf, fp, sizeof(u32));
1209 ncons = le32_to_cpu(buf[0]);
1210 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1215 rc = hashtab_insert(h, key, cladatum);
1223 cls_destroy(key, cladatum, NULL);
1227 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1230 struct role_datum *role;
1231 int rc, to_read = 2;
1235 role = kzalloc(sizeof(*role), GFP_KERNEL);
1241 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1244 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1248 len = le32_to_cpu(buf[0]);
1249 role->value = le32_to_cpu(buf[1]);
1250 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1251 role->bounds = le32_to_cpu(buf[2]);
1253 key = kmalloc(len + 1, GFP_KERNEL);
1258 rc = next_entry(key, fp, len);
1263 rc = ebitmap_read(&role->dominates, fp);
1267 rc = ebitmap_read(&role->types, fp);
1271 if (strcmp(key, OBJECT_R) == 0) {
1272 if (role->value != OBJECT_R_VAL) {
1273 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1274 OBJECT_R, role->value);
1282 rc = hashtab_insert(h, key, role);
1288 role_destroy(key, role, NULL);
1292 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1295 struct type_datum *typdatum;
1296 int rc, to_read = 3;
1300 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1306 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1309 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1313 len = le32_to_cpu(buf[0]);
1314 typdatum->value = le32_to_cpu(buf[1]);
1315 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1316 u32 prop = le32_to_cpu(buf[2]);
1318 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1319 typdatum->primary = 1;
1320 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1321 typdatum->attribute = 1;
1323 typdatum->bounds = le32_to_cpu(buf[3]);
1325 typdatum->primary = le32_to_cpu(buf[2]);
1328 key = kmalloc(len + 1, GFP_KERNEL);
1333 rc = next_entry(key, fp, len);
1338 rc = hashtab_insert(h, key, typdatum);
1344 type_destroy(key, typdatum, NULL);
1350 * Read a MLS level structure from a policydb binary
1351 * representation file.
1353 static int mls_read_level(struct mls_level *lp, void *fp)
1358 memset(lp, 0, sizeof(*lp));
1360 rc = next_entry(buf, fp, sizeof buf);
1362 printk(KERN_ERR "SELinux: mls: truncated level\n");
1365 lp->sens = le32_to_cpu(buf[0]);
1367 if (ebitmap_read(&lp->cat, fp)) {
1368 printk(KERN_ERR "SELinux: mls: error reading level "
1379 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1382 struct user_datum *usrdatum;
1383 int rc, to_read = 2;
1387 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1393 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1396 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1400 len = le32_to_cpu(buf[0]);
1401 usrdatum->value = le32_to_cpu(buf[1]);
1402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1403 usrdatum->bounds = le32_to_cpu(buf[2]);
1405 key = kmalloc(len + 1, GFP_KERNEL);
1410 rc = next_entry(key, fp, len);
1415 rc = ebitmap_read(&usrdatum->roles, fp);
1419 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1420 rc = mls_read_range_helper(&usrdatum->range, fp);
1423 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1428 rc = hashtab_insert(h, key, usrdatum);
1434 user_destroy(key, usrdatum, NULL);
1438 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1441 struct level_datum *levdatum;
1446 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1452 rc = next_entry(buf, fp, sizeof buf);
1456 len = le32_to_cpu(buf[0]);
1457 levdatum->isalias = le32_to_cpu(buf[1]);
1459 key = kmalloc(len + 1, GFP_ATOMIC);
1464 rc = next_entry(key, fp, len);
1469 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1470 if (!levdatum->level) {
1474 if (mls_read_level(levdatum->level, fp)) {
1479 rc = hashtab_insert(h, key, levdatum);
1485 sens_destroy(key, levdatum, NULL);
1489 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1492 struct cat_datum *catdatum;
1497 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1503 rc = next_entry(buf, fp, sizeof buf);
1507 len = le32_to_cpu(buf[0]);
1508 catdatum->value = le32_to_cpu(buf[1]);
1509 catdatum->isalias = le32_to_cpu(buf[2]);
1511 key = kmalloc(len + 1, GFP_ATOMIC);
1516 rc = next_entry(key, fp, len);
1521 rc = hashtab_insert(h, key, catdatum);
1528 cat_destroy(key, catdatum, NULL);
1532 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1544 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1546 struct user_datum *upper, *user;
1547 struct policydb *p = datap;
1550 upper = user = datum;
1551 while (upper->bounds) {
1552 struct ebitmap_node *node;
1555 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1556 printk(KERN_ERR "SELinux: user %s: "
1557 "too deep or looped boundary",
1562 upper = p->user_val_to_struct[upper->bounds - 1];
1563 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1564 if (ebitmap_get_bit(&upper->roles, bit))
1568 "SELinux: boundary violated policy: "
1569 "user=%s role=%s bounds=%s\n",
1570 p->p_user_val_to_name[user->value - 1],
1571 p->p_role_val_to_name[bit],
1572 p->p_user_val_to_name[upper->value - 1]);
1581 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1583 struct role_datum *upper, *role;
1584 struct policydb *p = datap;
1587 upper = role = datum;
1588 while (upper->bounds) {
1589 struct ebitmap_node *node;
1592 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1593 printk(KERN_ERR "SELinux: role %s: "
1594 "too deep or looped bounds\n",
1599 upper = p->role_val_to_struct[upper->bounds - 1];
1600 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1601 if (ebitmap_get_bit(&upper->types, bit))
1605 "SELinux: boundary violated policy: "
1606 "role=%s type=%s bounds=%s\n",
1607 p->p_role_val_to_name[role->value - 1],
1608 p->p_type_val_to_name[bit],
1609 p->p_role_val_to_name[upper->value - 1]);
1618 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1620 struct type_datum *upper, *type;
1621 struct policydb *p = datap;
1624 upper = type = datum;
1625 while (upper->bounds) {
1626 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1627 printk(KERN_ERR "SELinux: type %s: "
1628 "too deep or looped boundary\n",
1633 upper = p->type_val_to_struct[upper->bounds - 1];
1634 if (upper->attribute) {
1635 printk(KERN_ERR "SELinux: type %s: "
1636 "bounded by attribute %s",
1638 p->p_type_val_to_name[upper->value - 1]);
1646 static int policydb_bounds_sanity_check(struct policydb *p)
1650 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1653 rc = hashtab_map(p->p_users.table,
1654 user_bounds_sanity_check, p);
1658 rc = hashtab_map(p->p_roles.table,
1659 role_bounds_sanity_check, p);
1663 rc = hashtab_map(p->p_types.table,
1664 type_bounds_sanity_check, p);
1671 extern int ss_initialized;
1673 u16 string_to_security_class(struct policydb *p, const char *name)
1675 struct class_datum *cladatum;
1677 cladatum = hashtab_search(p->p_classes.table, name);
1681 return cladatum->value;
1684 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1686 struct class_datum *cladatum;
1687 struct perm_datum *perdatum = NULL;
1688 struct common_datum *comdatum;
1690 if (!tclass || tclass > p->p_classes.nprim)
1693 cladatum = p->class_val_to_struct[tclass-1];
1694 comdatum = cladatum->comdatum;
1696 perdatum = hashtab_search(comdatum->permissions.table,
1699 perdatum = hashtab_search(cladatum->permissions.table,
1704 return 1U << (perdatum->value-1);
1707 static int range_read(struct policydb *p, void *fp)
1709 struct range_trans *rt = NULL;
1710 struct mls_range *r = NULL;
1715 if (p->policyvers < POLICYDB_VERSION_MLS)
1718 rc = next_entry(buf, fp, sizeof(u32));
1722 nel = le32_to_cpu(buf[0]);
1723 for (i = 0; i < nel; i++) {
1725 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1729 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1733 rt->source_type = le32_to_cpu(buf[0]);
1734 rt->target_type = le32_to_cpu(buf[1]);
1735 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1736 rc = next_entry(buf, fp, sizeof(u32));
1739 rt->target_class = le32_to_cpu(buf[0]);
1741 rt->target_class = p->process_class;
1744 if (!policydb_type_isvalid(p, rt->source_type) ||
1745 !policydb_type_isvalid(p, rt->target_type) ||
1746 !policydb_class_isvalid(p, rt->target_class))
1750 r = kzalloc(sizeof(*r), GFP_KERNEL);
1754 rc = mls_read_range_helper(r, fp);
1759 if (!mls_range_isvalid(p, r)) {
1760 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1764 rc = hashtab_insert(p->range_tr, rt, r);
1771 rangetr_hash_eval(p->range_tr);
1779 static int genfs_read(struct policydb *p, void *fp)
1782 u32 nel, nel2, len, len2;
1784 struct ocontext *l, *c;
1785 struct ocontext *newc = NULL;
1786 struct genfs *genfs_p, *genfs;
1787 struct genfs *newgenfs = NULL;
1789 rc = next_entry(buf, fp, sizeof(u32));
1792 nel = le32_to_cpu(buf[0]);
1794 for (i = 0; i < nel; i++) {
1795 rc = next_entry(buf, fp, sizeof(u32));
1798 len = le32_to_cpu(buf[0]);
1801 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1806 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1807 if (!newgenfs->fstype)
1810 rc = next_entry(newgenfs->fstype, fp, len);
1814 newgenfs->fstype[len] = 0;
1816 for (genfs_p = NULL, genfs = p->genfs; genfs;
1817 genfs_p = genfs, genfs = genfs->next) {
1819 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1820 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1824 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1827 newgenfs->next = genfs;
1829 genfs_p->next = newgenfs;
1831 p->genfs = newgenfs;
1835 rc = next_entry(buf, fp, sizeof(u32));
1839 nel2 = le32_to_cpu(buf[0]);
1840 for (j = 0; j < nel2; j++) {
1841 rc = next_entry(buf, fp, sizeof(u32));
1844 len = le32_to_cpu(buf[0]);
1847 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1852 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1856 rc = next_entry(newc->u.name, fp, len);
1859 newc->u.name[len] = 0;
1861 rc = next_entry(buf, fp, sizeof(u32));
1865 newc->v.sclass = le32_to_cpu(buf[0]);
1866 rc = context_read_and_validate(&newc->context[0], p, fp);
1870 for (l = NULL, c = genfs->head; c;
1871 l = c, c = c->next) {
1873 if (!strcmp(newc->u.name, c->u.name) &&
1874 (!c->v.sclass || !newc->v.sclass ||
1875 newc->v.sclass == c->v.sclass)) {
1876 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1877 genfs->fstype, c->u.name);
1880 len = strlen(newc->u.name);
1881 len2 = strlen(c->u.name);
1897 kfree(newgenfs->fstype);
1899 ocontext_destroy(newc, OCON_FSUSE);
1904 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1910 struct ocontext *l, *c;
1913 for (i = 0; i < info->ocon_num; i++) {
1914 rc = next_entry(buf, fp, sizeof(u32));
1917 nel = le32_to_cpu(buf[0]);
1920 for (j = 0; j < nel; j++) {
1922 c = kzalloc(sizeof(*c), GFP_KERNEL);
1928 p->ocontexts[i] = c;
1933 rc = next_entry(buf, fp, sizeof(u32));
1937 c->sid[0] = le32_to_cpu(buf[0]);
1938 rc = context_read_and_validate(&c->context[0], p, fp);
1944 rc = next_entry(buf, fp, sizeof(u32));
1947 len = le32_to_cpu(buf[0]);
1950 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1954 rc = next_entry(c->u.name, fp, len);
1959 rc = context_read_and_validate(&c->context[0], p, fp);
1962 rc = context_read_and_validate(&c->context[1], p, fp);
1967 rc = next_entry(buf, fp, sizeof(u32)*3);
1970 c->u.port.protocol = le32_to_cpu(buf[0]);
1971 c->u.port.low_port = le32_to_cpu(buf[1]);
1972 c->u.port.high_port = le32_to_cpu(buf[2]);
1973 rc = context_read_and_validate(&c->context[0], p, fp);
1978 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1981 c->u.node.addr = nodebuf[0]; /* network order */
1982 c->u.node.mask = nodebuf[1]; /* network order */
1983 rc = context_read_and_validate(&c->context[0], p, fp);
1988 rc = next_entry(buf, fp, sizeof(u32)*2);
1993 c->v.behavior = le32_to_cpu(buf[0]);
1994 if (c->v.behavior > SECURITY_FS_USE_NONE)
1998 len = le32_to_cpu(buf[1]);
1999 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2003 rc = next_entry(c->u.name, fp, len);
2007 rc = context_read_and_validate(&c->context[0], p, fp);
2014 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2017 for (k = 0; k < 4; k++)
2018 c->u.node6.addr[k] = nodebuf[k];
2019 for (k = 0; k < 4; k++)
2020 c->u.node6.mask[k] = nodebuf[k+4];
2021 rc = context_read_and_validate(&c->context[0], p, fp);
2035 * Read the configuration data from a policy database binary
2036 * representation file into a policy database structure.
2038 int policydb_read(struct policydb *p, void *fp)
2040 struct role_allow *ra, *lra;
2041 struct role_trans *tr, *ltr;
2044 u32 len, nprim, nel;
2047 struct policydb_compat_info *info;
2049 rc = policydb_init(p);
2053 /* Read the magic number and string length. */
2054 rc = next_entry(buf, fp, sizeof(u32) * 2);
2058 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2059 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2060 "not match expected magic number 0x%x\n",
2061 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2065 len = le32_to_cpu(buf[1]);
2066 if (len != strlen(POLICYDB_STRING)) {
2067 printk(KERN_ERR "SELinux: policydb string length %d does not "
2068 "match expected length %Zu\n",
2069 len, strlen(POLICYDB_STRING));
2072 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2073 if (!policydb_str) {
2074 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2075 "string of length %d\n", len);
2079 rc = next_entry(policydb_str, fp, len);
2081 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2082 kfree(policydb_str);
2085 policydb_str[len] = '\0';
2086 if (strcmp(policydb_str, POLICYDB_STRING)) {
2087 printk(KERN_ERR "SELinux: policydb string %s does not match "
2088 "my string %s\n", policydb_str, POLICYDB_STRING);
2089 kfree(policydb_str);
2092 /* Done with policydb_str. */
2093 kfree(policydb_str);
2094 policydb_str = NULL;
2096 /* Read the version and table sizes. */
2097 rc = next_entry(buf, fp, sizeof(u32)*4);
2101 p->policyvers = le32_to_cpu(buf[0]);
2102 if (p->policyvers < POLICYDB_VERSION_MIN ||
2103 p->policyvers > POLICYDB_VERSION_MAX) {
2104 printk(KERN_ERR "SELinux: policydb version %d does not match "
2105 "my version range %d-%d\n",
2106 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2110 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2113 if (p->policyvers < POLICYDB_VERSION_MLS) {
2114 printk(KERN_ERR "SELinux: security policydb version %d "
2115 "(MLS) not backwards compatible\n",
2120 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2121 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2123 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
2124 ebitmap_read(&p->policycaps, fp) != 0)
2127 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
2128 ebitmap_read(&p->permissive_map, fp) != 0)
2131 info = policydb_lookup_compat(p->policyvers);
2133 printk(KERN_ERR "SELinux: unable to find policy compat info "
2134 "for version %d\n", p->policyvers);
2138 if (le32_to_cpu(buf[2]) != info->sym_num ||
2139 le32_to_cpu(buf[3]) != info->ocon_num) {
2140 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2141 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2142 le32_to_cpu(buf[3]),
2143 info->sym_num, info->ocon_num);
2147 for (i = 0; i < info->sym_num; i++) {
2148 rc = next_entry(buf, fp, sizeof(u32)*2);
2151 nprim = le32_to_cpu(buf[0]);
2152 nel = le32_to_cpu(buf[1]);
2153 for (j = 0; j < nel; j++) {
2154 rc = read_f[i](p, p->symtab[i].table, fp);
2159 p->symtab[i].nprim = nprim;
2162 rc = avtab_read(&p->te_avtab, fp, p);
2166 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2167 rc = cond_read_list(p, fp);
2172 rc = next_entry(buf, fp, sizeof(u32));
2175 nel = le32_to_cpu(buf[0]);
2177 for (i = 0; i < nel; i++) {
2178 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2187 rc = next_entry(buf, fp, sizeof(u32)*3);
2190 tr->role = le32_to_cpu(buf[0]);
2191 tr->type = le32_to_cpu(buf[1]);
2192 tr->new_role = le32_to_cpu(buf[2]);
2193 if (!policydb_role_isvalid(p, tr->role) ||
2194 !policydb_type_isvalid(p, tr->type) ||
2195 !policydb_role_isvalid(p, tr->new_role)) {
2202 rc = next_entry(buf, fp, sizeof(u32));
2205 nel = le32_to_cpu(buf[0]);
2207 for (i = 0; i < nel; i++) {
2208 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2217 rc = next_entry(buf, fp, sizeof(u32)*2);
2220 ra->role = le32_to_cpu(buf[0]);
2221 ra->new_role = le32_to_cpu(buf[1]);
2222 if (!policydb_role_isvalid(p, ra->role) ||
2223 !policydb_role_isvalid(p, ra->new_role)) {
2230 rc = policydb_index_classes(p);
2234 rc = policydb_index_others(p);
2238 p->process_class = string_to_security_class(p, "process");
2239 if (!p->process_class)
2241 p->process_trans_perms = string_to_av_perm(p, p->process_class,
2243 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
2245 if (!p->process_trans_perms)
2248 rc = ocontext_read(p, info, fp);
2252 rc = genfs_read(p, fp);
2256 rc = range_read(p, fp);
2260 p->type_attr_map = kmalloc(p->p_types.nprim * sizeof(struct ebitmap), GFP_KERNEL);
2261 if (!p->type_attr_map)
2264 for (i = 0; i < p->p_types.nprim; i++) {
2265 ebitmap_init(&p->type_attr_map[i]);
2266 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2267 if (ebitmap_read(&p->type_attr_map[i], fp))
2270 /* add the type itself as the degenerate case */
2271 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2275 rc = policydb_bounds_sanity_check(p);
2285 policydb_destroy(p);