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 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
30 #include "conditional.h"
36 static char *symtab_name[SYM_NUM] = {
48 int selinux_mls_enabled = 0;
50 static unsigned int symtab_sizes[SYM_NUM] = {
61 struct policydb_compat_info {
67 /* These need to be updated if SYM_NUM or OCON_NUM changes */
68 static struct policydb_compat_info policydb_compat[] = {
70 .version = POLICYDB_VERSION_BASE,
71 .sym_num = SYM_NUM - 3,
72 .ocon_num = OCON_NUM - 1,
75 .version = POLICYDB_VERSION_BOOL,
76 .sym_num = SYM_NUM - 2,
77 .ocon_num = OCON_NUM - 1,
80 .version = POLICYDB_VERSION_IPV6,
81 .sym_num = SYM_NUM - 2,
85 .version = POLICYDB_VERSION_NLCLASS,
86 .sym_num = SYM_NUM - 2,
90 .version = POLICYDB_VERSION_MLS,
95 .version = POLICYDB_VERSION_AVTAB,
101 static struct policydb_compat_info *policydb_lookup_compat(int version)
104 struct policydb_compat_info *info = NULL;
106 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
107 if (policydb_compat[i].version == version) {
108 info = &policydb_compat[i];
116 * Initialize the role table.
118 static int roles_init(struct policydb *p)
122 struct role_datum *role;
124 role = kmalloc(sizeof(*role), GFP_KERNEL);
129 memset(role, 0, sizeof(*role));
130 role->value = ++p->p_roles.nprim;
131 if (role->value != OBJECT_R_VAL) {
135 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
140 strcpy(key, OBJECT_R);
141 rc = hashtab_insert(p->p_roles.table, key, role);
155 * Initialize a policy database structure.
157 static int policydb_init(struct policydb *p)
161 memset(p, 0, sizeof(*p));
163 for (i = 0; i < SYM_NUM; i++) {
164 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
166 goto out_free_symtab;
169 rc = avtab_init(&p->te_avtab);
171 goto out_free_symtab;
177 rc = cond_policydb_init(p);
185 avtab_destroy(&p->te_avtab);
188 for (i = 0; i < SYM_NUM; i++)
189 hashtab_destroy(p->symtab[i].table);
194 * The following *_index functions are used to
195 * define the val_to_name and val_to_struct arrays
196 * in a policy database structure. The val_to_name
197 * arrays are used when converting security context
198 * structures into string representations. The
199 * val_to_struct arrays are used when the attributes
200 * of a class, role, or user are needed.
203 static int common_index(void *key, void *datum, void *datap)
206 struct common_datum *comdatum;
210 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
212 p->p_common_val_to_name[comdatum->value - 1] = key;
216 static int class_index(void *key, void *datum, void *datap)
219 struct class_datum *cladatum;
223 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
225 p->p_class_val_to_name[cladatum->value - 1] = key;
226 p->class_val_to_struct[cladatum->value - 1] = cladatum;
230 static int role_index(void *key, void *datum, void *datap)
233 struct role_datum *role;
237 if (!role->value || role->value > p->p_roles.nprim)
239 p->p_role_val_to_name[role->value - 1] = key;
240 p->role_val_to_struct[role->value - 1] = role;
244 static int type_index(void *key, void *datum, void *datap)
247 struct type_datum *typdatum;
252 if (typdatum->primary) {
253 if (!typdatum->value || typdatum->value > p->p_types.nprim)
255 p->p_type_val_to_name[typdatum->value - 1] = key;
261 static int user_index(void *key, void *datum, void *datap)
264 struct user_datum *usrdatum;
268 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
270 p->p_user_val_to_name[usrdatum->value - 1] = key;
271 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
275 static int sens_index(void *key, void *datum, void *datap)
278 struct level_datum *levdatum;
283 if (!levdatum->isalias) {
284 if (!levdatum->level->sens ||
285 levdatum->level->sens > p->p_levels.nprim)
287 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
293 static int cat_index(void *key, void *datum, void *datap)
296 struct cat_datum *catdatum;
301 if (!catdatum->isalias) {
302 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
304 p->p_cat_val_to_name[catdatum->value - 1] = key;
310 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
323 * Define the common val_to_name array and the class
324 * val_to_name and val_to_struct arrays in a policy
325 * database structure.
327 * Caller must clean up upon failure.
329 static int policydb_index_classes(struct policydb *p)
333 p->p_common_val_to_name =
334 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
335 if (!p->p_common_val_to_name) {
340 rc = hashtab_map(p->p_commons.table, common_index, p);
344 p->class_val_to_struct =
345 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
346 if (!p->class_val_to_struct) {
351 p->p_class_val_to_name =
352 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
353 if (!p->p_class_val_to_name) {
358 rc = hashtab_map(p->p_classes.table, class_index, p);
364 static void symtab_hash_eval(struct symtab *s)
368 for (i = 0; i < SYM_NUM; i++) {
369 struct hashtab *h = s[i].table;
370 struct hashtab_info info;
372 hashtab_stat(h, &info);
373 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
374 "longest chain length %d\n", symtab_name[i], h->nel,
375 info.slots_used, h->size, info.max_chain_len);
381 * Define the other val_to_name and val_to_struct arrays
382 * in a policy database structure.
384 * Caller must clean up on failure.
386 static int policydb_index_others(struct policydb *p)
390 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
391 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
392 if (selinux_mls_enabled)
393 printk(", %d sens, %d cats", p->p_levels.nprim,
397 printk(KERN_INFO "security: %d classes, %d rules\n",
398 p->p_classes.nprim, p->te_avtab.nel);
401 avtab_hash_eval(&p->te_avtab, "rules");
402 symtab_hash_eval(p->symtab);
405 p->role_val_to_struct =
406 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
408 if (!p->role_val_to_struct) {
413 p->user_val_to_struct =
414 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
416 if (!p->user_val_to_struct) {
421 if (cond_init_bool_indexes(p)) {
426 for (i = SYM_ROLES; i < SYM_NUM; i++) {
427 p->sym_val_to_name[i] =
428 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
429 if (!p->sym_val_to_name[i]) {
433 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
443 * The following *_destroy functions are used to
444 * free any memory allocated for each kind of
445 * symbol data in the policy database.
448 static int perm_destroy(void *key, void *datum, void *p)
455 static int common_destroy(void *key, void *datum, void *p)
457 struct common_datum *comdatum;
461 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
462 hashtab_destroy(comdatum->permissions.table);
467 static int class_destroy(void *key, void *datum, void *p)
469 struct class_datum *cladatum;
470 struct constraint_node *constraint, *ctemp;
471 struct constraint_expr *e, *etmp;
475 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
476 hashtab_destroy(cladatum->permissions.table);
477 constraint = cladatum->constraints;
479 e = constraint->expr;
481 ebitmap_destroy(&e->names);
487 constraint = constraint->next;
491 constraint = cladatum->validatetrans;
493 e = constraint->expr;
495 ebitmap_destroy(&e->names);
501 constraint = constraint->next;
505 kfree(cladatum->comkey);
510 static int role_destroy(void *key, void *datum, void *p)
512 struct role_datum *role;
516 ebitmap_destroy(&role->dominates);
517 ebitmap_destroy(&role->types);
522 static int type_destroy(void *key, void *datum, void *p)
529 static int user_destroy(void *key, void *datum, void *p)
531 struct user_datum *usrdatum;
535 ebitmap_destroy(&usrdatum->roles);
536 ebitmap_destroy(&usrdatum->range.level[0].cat);
537 ebitmap_destroy(&usrdatum->range.level[1].cat);
538 ebitmap_destroy(&usrdatum->dfltlevel.cat);
543 static int sens_destroy(void *key, void *datum, void *p)
545 struct level_datum *levdatum;
549 ebitmap_destroy(&levdatum->level->cat);
550 kfree(levdatum->level);
555 static int cat_destroy(void *key, void *datum, void *p)
562 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
574 static void ocontext_destroy(struct ocontext *c, int i)
576 context_destroy(&c->context[0]);
577 context_destroy(&c->context[1]);
578 if (i == OCON_ISID || i == OCON_FS ||
579 i == OCON_NETIF || i == OCON_FSUSE)
585 * Free any memory allocated by a policy database structure.
587 void policydb_destroy(struct policydb *p)
589 struct ocontext *c, *ctmp;
590 struct genfs *g, *gtmp;
592 struct role_allow *ra, *lra = NULL;
593 struct role_trans *tr, *ltr = NULL;
594 struct range_trans *rt, *lrt = NULL;
596 for (i = 0; i < SYM_NUM; i++) {
597 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
598 hashtab_destroy(p->symtab[i].table);
601 for (i = 0; i < SYM_NUM; i++)
602 kfree(p->sym_val_to_name[i]);
604 kfree(p->class_val_to_struct);
605 kfree(p->role_val_to_struct);
606 kfree(p->user_val_to_struct);
608 avtab_destroy(&p->te_avtab);
610 for (i = 0; i < OCON_NUM; i++) {
615 ocontext_destroy(ctmp,i);
626 ocontext_destroy(ctmp,OCON_FSUSE);
633 cond_policydb_destroy(p);
635 for (tr = p->role_tr; tr; tr = tr->next) {
641 for (ra = p->role_allow; ra; ra = ra -> next) {
647 for (rt = p->range_tr; rt; rt = rt -> next) {
653 if (p->type_attr_map) {
654 for (i = 0; i < p->p_types.nprim; i++)
655 ebitmap_destroy(&p->type_attr_map[i]);
657 kfree(p->type_attr_map);
663 * Load the initial SIDs specified in a policy database
664 * structure into a SID table.
666 int policydb_load_isids(struct policydb *p, struct sidtab *s)
668 struct ocontext *head, *c;
673 printk(KERN_ERR "security: out of memory on SID table init\n");
677 head = p->ocontexts[OCON_ISID];
678 for (c = head; c; c = c->next) {
679 if (!c->context[0].user) {
680 printk(KERN_ERR "security: SID %s was never "
681 "defined.\n", c->u.name);
685 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
686 printk(KERN_ERR "security: unable to load initial "
687 "SID %s.\n", c->u.name);
697 * Return 1 if the fields in the security context
698 * structure `c' are valid. Return 0 otherwise.
700 int policydb_context_isvalid(struct policydb *p, struct context *c)
702 struct role_datum *role;
703 struct user_datum *usrdatum;
705 if (!c->role || c->role > p->p_roles.nprim)
708 if (!c->user || c->user > p->p_users.nprim)
711 if (!c->type || c->type > p->p_types.nprim)
714 if (c->role != OBJECT_R_VAL) {
716 * Role must be authorized for the type.
718 role = p->role_val_to_struct[c->role - 1];
719 if (!ebitmap_get_bit(&role->types,
721 /* role may not be associated with type */
725 * User must be authorized for the role.
727 usrdatum = p->user_val_to_struct[c->user - 1];
731 if (!ebitmap_get_bit(&usrdatum->roles,
733 /* user may not be associated with role */
737 if (!mls_context_isvalid(p, c))
744 * Read a MLS range structure from a policydb binary
745 * representation file.
747 static int mls_read_range_helper(struct mls_range *r, void *fp)
753 rc = next_entry(buf, fp, sizeof(u32));
757 items = le32_to_cpu(buf[0]);
758 if (items > ARRAY_SIZE(buf)) {
759 printk(KERN_ERR "security: mls: range overflow\n");
763 rc = next_entry(buf, fp, sizeof(u32) * items);
765 printk(KERN_ERR "security: mls: truncated range\n");
768 r->level[0].sens = le32_to_cpu(buf[0]);
770 r->level[1].sens = le32_to_cpu(buf[1]);
772 r->level[1].sens = r->level[0].sens;
774 rc = ebitmap_read(&r->level[0].cat, fp);
776 printk(KERN_ERR "security: mls: error reading low "
781 rc = ebitmap_read(&r->level[1].cat, fp);
783 printk(KERN_ERR "security: mls: error reading high "
788 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
790 printk(KERN_ERR "security: mls: out of memory\n");
799 ebitmap_destroy(&r->level[0].cat);
804 * Read and validate a security context structure
805 * from a policydb binary representation file.
807 static int context_read_and_validate(struct context *c,
814 rc = next_entry(buf, fp, sizeof buf);
816 printk(KERN_ERR "security: context truncated\n");
819 c->user = le32_to_cpu(buf[0]);
820 c->role = le32_to_cpu(buf[1]);
821 c->type = le32_to_cpu(buf[2]);
822 if (p->policyvers >= POLICYDB_VERSION_MLS) {
823 if (mls_read_range_helper(&c->range, fp)) {
824 printk(KERN_ERR "security: error reading MLS range of "
831 if (!policydb_context_isvalid(p, c)) {
832 printk(KERN_ERR "security: invalid security context\n");
841 * The following *_read functions are used to
842 * read the symbol data from a policy database
843 * binary representation file.
846 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
849 struct perm_datum *perdatum;
854 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
859 memset(perdatum, 0, sizeof(*perdatum));
861 rc = next_entry(buf, fp, sizeof buf);
865 len = le32_to_cpu(buf[0]);
866 perdatum->value = le32_to_cpu(buf[1]);
868 key = kmalloc(len + 1,GFP_KERNEL);
873 rc = next_entry(key, fp, len);
878 rc = hashtab_insert(h, key, perdatum);
884 perm_destroy(key, perdatum, NULL);
888 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
891 struct common_datum *comdatum;
896 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
901 memset(comdatum, 0, sizeof(*comdatum));
903 rc = next_entry(buf, fp, sizeof buf);
907 len = le32_to_cpu(buf[0]);
908 comdatum->value = le32_to_cpu(buf[1]);
910 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
913 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
914 nel = le32_to_cpu(buf[3]);
916 key = kmalloc(len + 1,GFP_KERNEL);
921 rc = next_entry(key, fp, len);
926 for (i = 0; i < nel; i++) {
927 rc = perm_read(p, comdatum->permissions.table, fp);
932 rc = hashtab_insert(h, key, comdatum);
938 common_destroy(key, comdatum, NULL);
942 static int read_cons_helper(struct constraint_node **nodep, int ncons,
943 int allowxtarget, void *fp)
945 struct constraint_node *c, *lc;
946 struct constraint_expr *e, *le;
952 for (i = 0; i < ncons; i++) {
953 c = kmalloc(sizeof(*c), GFP_KERNEL);
956 memset(c, 0, sizeof(*c));
964 rc = next_entry(buf, fp, (sizeof(u32) * 2));
967 c->permissions = le32_to_cpu(buf[0]);
968 nexpr = le32_to_cpu(buf[1]);
971 for (j = 0; j < nexpr; j++) {
972 e = kmalloc(sizeof(*e), GFP_KERNEL);
975 memset(e, 0, sizeof(*e));
983 rc = next_entry(buf, fp, (sizeof(u32) * 3));
986 e->expr_type = le32_to_cpu(buf[0]);
987 e->attr = le32_to_cpu(buf[1]);
988 e->op = le32_to_cpu(buf[2]);
990 switch (e->expr_type) {
1002 if (depth == (CEXPR_MAXDEPTH - 1))
1007 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1009 if (depth == (CEXPR_MAXDEPTH - 1))
1012 if (ebitmap_read(&e->names, fp))
1028 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1031 struct class_datum *cladatum;
1033 u32 len, len2, ncons, nel;
1036 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1041 memset(cladatum, 0, sizeof(*cladatum));
1043 rc = next_entry(buf, fp, sizeof(u32)*6);
1047 len = le32_to_cpu(buf[0]);
1048 len2 = le32_to_cpu(buf[1]);
1049 cladatum->value = le32_to_cpu(buf[2]);
1051 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1054 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1055 nel = le32_to_cpu(buf[4]);
1057 ncons = le32_to_cpu(buf[5]);
1059 key = kmalloc(len + 1,GFP_KERNEL);
1064 rc = next_entry(key, fp, len);
1070 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1071 if (!cladatum->comkey) {
1075 rc = next_entry(cladatum->comkey, fp, len2);
1078 cladatum->comkey[len2] = 0;
1080 cladatum->comdatum = hashtab_search(p->p_commons.table,
1082 if (!cladatum->comdatum) {
1083 printk(KERN_ERR "security: unknown common %s\n",
1089 for (i = 0; i < nel; i++) {
1090 rc = perm_read(p, cladatum->permissions.table, fp);
1095 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1099 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1100 /* grab the validatetrans rules */
1101 rc = next_entry(buf, fp, sizeof(u32));
1104 ncons = le32_to_cpu(buf[0]);
1105 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1110 rc = hashtab_insert(h, key, cladatum);
1118 class_destroy(key, cladatum, NULL);
1122 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1125 struct role_datum *role;
1130 role = kmalloc(sizeof(*role), GFP_KERNEL);
1135 memset(role, 0, sizeof(*role));
1137 rc = next_entry(buf, fp, sizeof buf);
1141 len = le32_to_cpu(buf[0]);
1142 role->value = le32_to_cpu(buf[1]);
1144 key = kmalloc(len + 1,GFP_KERNEL);
1149 rc = next_entry(key, fp, len);
1154 rc = ebitmap_read(&role->dominates, fp);
1158 rc = ebitmap_read(&role->types, fp);
1162 if (strcmp(key, OBJECT_R) == 0) {
1163 if (role->value != OBJECT_R_VAL) {
1164 printk(KERN_ERR "Role %s has wrong value %d\n",
1165 OBJECT_R, role->value);
1173 rc = hashtab_insert(h, key, role);
1179 role_destroy(key, role, NULL);
1183 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1186 struct type_datum *typdatum;
1191 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1196 memset(typdatum, 0, sizeof(*typdatum));
1198 rc = next_entry(buf, fp, sizeof buf);
1202 len = le32_to_cpu(buf[0]);
1203 typdatum->value = le32_to_cpu(buf[1]);
1204 typdatum->primary = le32_to_cpu(buf[2]);
1206 key = kmalloc(len + 1,GFP_KERNEL);
1211 rc = next_entry(key, fp, len);
1216 rc = hashtab_insert(h, key, typdatum);
1222 type_destroy(key, typdatum, NULL);
1228 * Read a MLS level structure from a policydb binary
1229 * representation file.
1231 static int mls_read_level(struct mls_level *lp, void *fp)
1236 memset(lp, 0, sizeof(*lp));
1238 rc = next_entry(buf, fp, sizeof buf);
1240 printk(KERN_ERR "security: mls: truncated level\n");
1243 lp->sens = le32_to_cpu(buf[0]);
1245 if (ebitmap_read(&lp->cat, fp)) {
1246 printk(KERN_ERR "security: mls: error reading level "
1256 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1259 struct user_datum *usrdatum;
1264 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1269 memset(usrdatum, 0, sizeof(*usrdatum));
1271 rc = next_entry(buf, fp, sizeof buf);
1275 len = le32_to_cpu(buf[0]);
1276 usrdatum->value = le32_to_cpu(buf[1]);
1278 key = kmalloc(len + 1,GFP_KERNEL);
1283 rc = next_entry(key, fp, len);
1288 rc = ebitmap_read(&usrdatum->roles, fp);
1292 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1293 rc = mls_read_range_helper(&usrdatum->range, fp);
1296 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1301 rc = hashtab_insert(h, key, usrdatum);
1307 user_destroy(key, usrdatum, NULL);
1311 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1314 struct level_datum *levdatum;
1319 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1324 memset(levdatum, 0, sizeof(*levdatum));
1326 rc = next_entry(buf, fp, sizeof buf);
1330 len = le32_to_cpu(buf[0]);
1331 levdatum->isalias = le32_to_cpu(buf[1]);
1333 key = kmalloc(len + 1,GFP_ATOMIC);
1338 rc = next_entry(key, fp, len);
1343 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1344 if (!levdatum->level) {
1348 if (mls_read_level(levdatum->level, fp)) {
1353 rc = hashtab_insert(h, key, levdatum);
1359 sens_destroy(key, levdatum, NULL);
1363 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1366 struct cat_datum *catdatum;
1371 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1376 memset(catdatum, 0, sizeof(*catdatum));
1378 rc = next_entry(buf, fp, sizeof buf);
1382 len = le32_to_cpu(buf[0]);
1383 catdatum->value = le32_to_cpu(buf[1]);
1384 catdatum->isalias = le32_to_cpu(buf[2]);
1386 key = kmalloc(len + 1,GFP_ATOMIC);
1391 rc = next_entry(key, fp, len);
1396 rc = hashtab_insert(h, key, catdatum);
1403 cat_destroy(key, catdatum, NULL);
1407 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1419 extern int ss_initialized;
1422 * Read the configuration data from a policy database binary
1423 * representation file into a policy database structure.
1425 int policydb_read(struct policydb *p, void *fp)
1427 struct role_allow *ra, *lra;
1428 struct role_trans *tr, *ltr;
1429 struct ocontext *l, *c, *newc;
1430 struct genfs *genfs_p, *genfs, *newgenfs;
1433 u32 len, len2, config, nprim, nel, nel2;
1435 struct policydb_compat_info *info;
1436 struct range_trans *rt, *lrt;
1440 rc = policydb_init(p);
1444 /* Read the magic number and string length. */
1445 rc = next_entry(buf, fp, sizeof(u32)* 2);
1449 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1450 printk(KERN_ERR "security: policydb magic number 0x%x does "
1451 "not match expected magic number 0x%x\n",
1452 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1456 len = le32_to_cpu(buf[1]);
1457 if (len != strlen(POLICYDB_STRING)) {
1458 printk(KERN_ERR "security: policydb string length %d does not "
1459 "match expected length %Zu\n",
1460 len, strlen(POLICYDB_STRING));
1463 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1464 if (!policydb_str) {
1465 printk(KERN_ERR "security: unable to allocate memory for policydb "
1466 "string of length %d\n", len);
1470 rc = next_entry(policydb_str, fp, len);
1472 printk(KERN_ERR "security: truncated policydb string identifier\n");
1473 kfree(policydb_str);
1476 policydb_str[len] = 0;
1477 if (strcmp(policydb_str, POLICYDB_STRING)) {
1478 printk(KERN_ERR "security: policydb string %s does not match "
1479 "my string %s\n", policydb_str, POLICYDB_STRING);
1480 kfree(policydb_str);
1483 /* Done with policydb_str. */
1484 kfree(policydb_str);
1485 policydb_str = NULL;
1487 /* Read the version, config, and table sizes. */
1488 rc = next_entry(buf, fp, sizeof(u32)*4);
1492 p->policyvers = le32_to_cpu(buf[0]);
1493 if (p->policyvers < POLICYDB_VERSION_MIN ||
1494 p->policyvers > POLICYDB_VERSION_MAX) {
1495 printk(KERN_ERR "security: policydb version %d does not match "
1496 "my version range %d-%d\n",
1497 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1501 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1502 if (ss_initialized && !selinux_mls_enabled) {
1503 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1507 selinux_mls_enabled = 1;
1508 config |= POLICYDB_CONFIG_MLS;
1510 if (p->policyvers < POLICYDB_VERSION_MLS) {
1511 printk(KERN_ERR "security policydb version %d (MLS) "
1512 "not backwards compatible\n", p->policyvers);
1516 if (ss_initialized && selinux_mls_enabled) {
1517 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1523 info = policydb_lookup_compat(p->policyvers);
1525 printk(KERN_ERR "security: unable to find policy compat info "
1526 "for version %d\n", p->policyvers);
1530 if (le32_to_cpu(buf[2]) != info->sym_num ||
1531 le32_to_cpu(buf[3]) != info->ocon_num) {
1532 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1533 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1534 le32_to_cpu(buf[3]),
1535 info->sym_num, info->ocon_num);
1539 for (i = 0; i < info->sym_num; i++) {
1540 rc = next_entry(buf, fp, sizeof(u32)*2);
1543 nprim = le32_to_cpu(buf[0]);
1544 nel = le32_to_cpu(buf[1]);
1545 for (j = 0; j < nel; j++) {
1546 rc = read_f[i](p, p->symtab[i].table, fp);
1551 p->symtab[i].nprim = nprim;
1554 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1558 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1559 rc = cond_read_list(p, fp);
1564 rc = next_entry(buf, fp, sizeof(u32));
1567 nel = le32_to_cpu(buf[0]);
1569 for (i = 0; i < nel; i++) {
1570 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1575 memset(tr, 0, sizeof(*tr));
1581 rc = next_entry(buf, fp, sizeof(u32)*3);
1584 tr->role = le32_to_cpu(buf[0]);
1585 tr->type = le32_to_cpu(buf[1]);
1586 tr->new_role = le32_to_cpu(buf[2]);
1590 rc = next_entry(buf, fp, sizeof(u32));
1593 nel = le32_to_cpu(buf[0]);
1595 for (i = 0; i < nel; i++) {
1596 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1601 memset(ra, 0, sizeof(*ra));
1607 rc = next_entry(buf, fp, sizeof(u32)*2);
1610 ra->role = le32_to_cpu(buf[0]);
1611 ra->new_role = le32_to_cpu(buf[1]);
1615 rc = policydb_index_classes(p);
1619 rc = policydb_index_others(p);
1623 for (i = 0; i < info->ocon_num; i++) {
1624 rc = next_entry(buf, fp, sizeof(u32));
1627 nel = le32_to_cpu(buf[0]);
1629 for (j = 0; j < nel; j++) {
1630 c = kmalloc(sizeof(*c), GFP_KERNEL);
1635 memset(c, 0, sizeof(*c));
1639 p->ocontexts[i] = c;
1645 rc = next_entry(buf, fp, sizeof(u32));
1648 c->sid[0] = le32_to_cpu(buf[0]);
1649 rc = context_read_and_validate(&c->context[0], p, fp);
1655 rc = next_entry(buf, fp, sizeof(u32));
1658 len = le32_to_cpu(buf[0]);
1659 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1664 rc = next_entry(c->u.name, fp, len);
1668 rc = context_read_and_validate(&c->context[0], p, fp);
1671 rc = context_read_and_validate(&c->context[1], p, fp);
1676 rc = next_entry(buf, fp, sizeof(u32)*3);
1679 c->u.port.protocol = le32_to_cpu(buf[0]);
1680 c->u.port.low_port = le32_to_cpu(buf[1]);
1681 c->u.port.high_port = le32_to_cpu(buf[2]);
1682 rc = context_read_and_validate(&c->context[0], p, fp);
1687 rc = next_entry(buf, fp, sizeof(u32)* 2);
1690 c->u.node.addr = le32_to_cpu(buf[0]);
1691 c->u.node.mask = le32_to_cpu(buf[1]);
1692 rc = context_read_and_validate(&c->context[0], p, fp);
1697 rc = next_entry(buf, fp, sizeof(u32)*2);
1700 c->v.behavior = le32_to_cpu(buf[0]);
1701 if (c->v.behavior > SECURITY_FS_USE_NONE)
1703 len = le32_to_cpu(buf[1]);
1704 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1709 rc = next_entry(c->u.name, fp, len);
1713 rc = context_read_and_validate(&c->context[0], p, fp);
1720 rc = next_entry(buf, fp, sizeof(u32) * 8);
1723 for (k = 0; k < 4; k++)
1724 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1725 for (k = 0; k < 4; k++)
1726 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1727 if (context_read_and_validate(&c->context[0], p, fp))
1735 rc = next_entry(buf, fp, sizeof(u32));
1738 nel = le32_to_cpu(buf[0]);
1741 for (i = 0; i < nel; i++) {
1742 rc = next_entry(buf, fp, sizeof(u32));
1745 len = le32_to_cpu(buf[0]);
1746 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1751 memset(newgenfs, 0, sizeof(*newgenfs));
1753 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1754 if (!newgenfs->fstype) {
1759 rc = next_entry(newgenfs->fstype, fp, len);
1761 kfree(newgenfs->fstype);
1765 newgenfs->fstype[len] = 0;
1766 for (genfs_p = NULL, genfs = p->genfs; genfs;
1767 genfs_p = genfs, genfs = genfs->next) {
1768 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1769 printk(KERN_ERR "security: dup genfs "
1770 "fstype %s\n", newgenfs->fstype);
1771 kfree(newgenfs->fstype);
1775 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1778 newgenfs->next = genfs;
1780 genfs_p->next = newgenfs;
1782 p->genfs = newgenfs;
1783 rc = next_entry(buf, fp, sizeof(u32));
1786 nel2 = le32_to_cpu(buf[0]);
1787 for (j = 0; j < nel2; j++) {
1788 rc = next_entry(buf, fp, sizeof(u32));
1791 len = le32_to_cpu(buf[0]);
1793 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1798 memset(newc, 0, sizeof(*newc));
1800 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1801 if (!newc->u.name) {
1805 rc = next_entry(newc->u.name, fp, len);
1808 newc->u.name[len] = 0;
1809 rc = next_entry(buf, fp, sizeof(u32));
1812 newc->v.sclass = le32_to_cpu(buf[0]);
1813 if (context_read_and_validate(&newc->context[0], p, fp))
1815 for (l = NULL, c = newgenfs->head; c;
1816 l = c, c = c->next) {
1817 if (!strcmp(newc->u.name, c->u.name) &&
1818 (!c->v.sclass || !newc->v.sclass ||
1819 newc->v.sclass == c->v.sclass)) {
1820 printk(KERN_ERR "security: dup genfs "
1822 newgenfs->fstype, c->u.name);
1825 len = strlen(newc->u.name);
1826 len2 = strlen(c->u.name);
1835 newgenfs->head = newc;
1839 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1840 rc = next_entry(buf, fp, sizeof(u32));
1843 nel = le32_to_cpu(buf[0]);
1845 for (i = 0; i < nel; i++) {
1846 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1851 memset(rt, 0, sizeof(*rt));
1856 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1859 rt->dom = le32_to_cpu(buf[0]);
1860 rt->type = le32_to_cpu(buf[1]);
1861 rc = mls_read_range_helper(&rt->range, fp);
1868 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1869 if (!p->type_attr_map)
1872 for (i = 0; i < p->p_types.nprim; i++) {
1873 ebitmap_init(&p->type_attr_map[i]);
1874 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1875 if (ebitmap_read(&p->type_attr_map[i], fp))
1878 /* add the type itself as the degenerate case */
1879 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1887 ocontext_destroy(newc,OCON_FSUSE);
1891 policydb_destroy(p);