2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
7 /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
9 * Added conditional policy language extensions
11 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 2.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
24 #include "conditional.h"
30 static char *symtab_name[SYM_NUM] = {
41 int policydb_loaded_version;
43 static unsigned int symtab_sizes[SYM_NUM] = {
53 struct policydb_compat_info {
59 /* These need to be updated if SYM_NUM or OCON_NUM changes */
60 static struct policydb_compat_info policydb_compat[] = {
62 .version = POLICYDB_VERSION_BASE,
63 .sym_num = SYM_NUM - 1,
64 .ocon_num = OCON_NUM - 1,
67 .version = POLICYDB_VERSION_BOOL,
69 .ocon_num = OCON_NUM - 1,
72 .version = POLICYDB_VERSION_IPV6,
77 .version = POLICYDB_VERSION_NLCLASS,
83 static struct policydb_compat_info *policydb_lookup_compat(int version)
86 struct policydb_compat_info *info = NULL;
88 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
89 if (policydb_compat[i].version == version) {
90 info = &policydb_compat[i];
98 * Initialize the role table.
100 int roles_init(struct policydb *p)
104 struct role_datum *role;
106 role = kmalloc(sizeof(*role), GFP_KERNEL);
111 memset(role, 0, sizeof(*role));
112 role->value = ++p->p_roles.nprim;
113 if (role->value != OBJECT_R_VAL) {
117 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
122 strcpy(key, OBJECT_R);
123 rc = hashtab_insert(p->p_roles.table, key, role);
137 * Initialize a policy database structure.
139 int policydb_init(struct policydb *p)
143 memset(p, 0, sizeof(*p));
145 for (i = 0; i < SYM_NUM; i++) {
146 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
148 goto out_free_symtab;
151 rc = avtab_init(&p->te_avtab);
153 goto out_free_symtab;
159 rc = cond_policydb_init(p);
167 avtab_destroy(&p->te_avtab);
170 for (i = 0; i < SYM_NUM; i++)
171 hashtab_destroy(p->symtab[i].table);
176 * The following *_index functions are used to
177 * define the val_to_name and val_to_struct arrays
178 * in a policy database structure. The val_to_name
179 * arrays are used when converting security context
180 * structures into string representations. The
181 * val_to_struct arrays are used when the attributes
182 * of a class, role, or user are needed.
185 static int common_index(void *key, void *datum, void *datap)
188 struct common_datum *comdatum;
192 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
194 p->p_common_val_to_name[comdatum->value - 1] = key;
198 static int class_index(void *key, void *datum, void *datap)
201 struct class_datum *cladatum;
205 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
207 p->p_class_val_to_name[cladatum->value - 1] = key;
208 p->class_val_to_struct[cladatum->value - 1] = cladatum;
212 static int role_index(void *key, void *datum, void *datap)
215 struct role_datum *role;
219 if (!role->value || role->value > p->p_roles.nprim)
221 p->p_role_val_to_name[role->value - 1] = key;
222 p->role_val_to_struct[role->value - 1] = role;
226 static int type_index(void *key, void *datum, void *datap)
229 struct type_datum *typdatum;
234 if (typdatum->primary) {
235 if (!typdatum->value || typdatum->value > p->p_types.nprim)
237 p->p_type_val_to_name[typdatum->value - 1] = key;
243 static int user_index(void *key, void *datum, void *datap)
246 struct user_datum *usrdatum;
250 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
252 p->p_user_val_to_name[usrdatum->value - 1] = key;
253 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
257 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
269 * Define the common val_to_name array and the class
270 * val_to_name and val_to_struct arrays in a policy
271 * database structure.
273 * Caller must clean up upon failure.
275 int policydb_index_classes(struct policydb *p)
279 p->p_common_val_to_name =
280 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
281 if (!p->p_common_val_to_name) {
286 rc = hashtab_map(p->p_commons.table, common_index, p);
290 p->class_val_to_struct =
291 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
292 if (!p->class_val_to_struct) {
297 p->p_class_val_to_name =
298 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
299 if (!p->p_class_val_to_name) {
304 rc = hashtab_map(p->p_classes.table, class_index, p);
310 static void symtab_hash_eval(struct symtab *s)
314 for (i = 0; i < SYM_NUM; i++) {
315 struct hashtab *h = s[i].table;
316 struct hashtab_info info;
318 hashtab_stat(h, &info);
319 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
320 "longest chain length %d\n", symtab_name[i], h->nel,
321 info.slots_used, h->size, info.max_chain_len);
327 * Define the other val_to_name and val_to_struct arrays
328 * in a policy database structure.
330 * Caller must clean up on failure.
332 int policydb_index_others(struct policydb *p)
336 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
337 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
338 mls_policydb_index_others(p);
341 printk(KERN_INFO "security: %d classes, %d rules\n",
342 p->p_classes.nprim, p->te_avtab.nel);
345 avtab_hash_eval(&p->te_avtab, "rules");
346 symtab_hash_eval(p->symtab);
349 p->role_val_to_struct =
350 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
352 if (!p->role_val_to_struct) {
357 p->user_val_to_struct =
358 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
360 if (!p->user_val_to_struct) {
365 if (cond_init_bool_indexes(p)) {
370 for (i = SYM_ROLES; i < SYM_NUM; i++) {
371 p->sym_val_to_name[i] =
372 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
373 if (!p->sym_val_to_name[i]) {
377 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
387 * The following *_destroy functions are used to
388 * free any memory allocated for each kind of
389 * symbol data in the policy database.
392 static int perm_destroy(void *key, void *datum, void *p)
399 static int common_destroy(void *key, void *datum, void *p)
401 struct common_datum *comdatum;
405 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
406 hashtab_destroy(comdatum->permissions.table);
411 static int class_destroy(void *key, void *datum, void *p)
413 struct class_datum *cladatum;
414 struct constraint_node *constraint, *ctemp;
415 struct constraint_expr *e, *etmp;
419 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
420 hashtab_destroy(cladatum->permissions.table);
421 constraint = cladatum->constraints;
423 e = constraint->expr;
425 ebitmap_destroy(&e->names);
431 constraint = constraint->next;
434 kfree(cladatum->comkey);
439 static int role_destroy(void *key, void *datum, void *p)
441 struct role_datum *role;
445 ebitmap_destroy(&role->dominates);
446 ebitmap_destroy(&role->types);
451 static int type_destroy(void *key, void *datum, void *p)
458 static int user_destroy(void *key, void *datum, void *p)
460 struct user_datum *usrdatum;
464 ebitmap_destroy(&usrdatum->roles);
465 mls_user_destroy(usrdatum);
470 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
481 void ocontext_destroy(struct ocontext *c, int i)
483 context_destroy(&c->context[0]);
484 context_destroy(&c->context[1]);
485 if (i == OCON_ISID || i == OCON_FS ||
486 i == OCON_NETIF || i == OCON_FSUSE)
492 * Free any memory allocated by a policy database structure.
494 void policydb_destroy(struct policydb *p)
496 struct ocontext *c, *ctmp;
497 struct genfs *g, *gtmp;
500 for (i = 0; i < SYM_NUM; i++) {
501 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
502 hashtab_destroy(p->symtab[i].table);
505 for (i = 0; i < SYM_NUM; i++) {
506 if (p->sym_val_to_name[i])
507 kfree(p->sym_val_to_name[i]);
510 if (p->class_val_to_struct)
511 kfree(p->class_val_to_struct);
512 if (p->role_val_to_struct)
513 kfree(p->role_val_to_struct);
514 if (p->user_val_to_struct)
515 kfree(p->user_val_to_struct);
517 avtab_destroy(&p->te_avtab);
519 for (i = 0; i < OCON_NUM; i++) {
524 ocontext_destroy(ctmp,i);
535 ocontext_destroy(ctmp,OCON_FSUSE);
542 cond_policydb_destroy(p);
548 * Load the initial SIDs specified in a policy database
549 * structure into a SID table.
551 int policydb_load_isids(struct policydb *p, struct sidtab *s)
553 struct ocontext *head, *c;
558 printk(KERN_ERR "security: out of memory on SID table init\n");
562 head = p->ocontexts[OCON_ISID];
563 for (c = head; c; c = c->next) {
564 if (!c->context[0].user) {
565 printk(KERN_ERR "security: SID %s was never "
566 "defined.\n", c->u.name);
570 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
571 printk(KERN_ERR "security: unable to load initial "
572 "SID %s.\n", c->u.name);
582 * Return 1 if the fields in the security context
583 * structure `c' are valid. Return 0 otherwise.
585 int policydb_context_isvalid(struct policydb *p, struct context *c)
587 struct role_datum *role;
588 struct user_datum *usrdatum;
590 if (!c->role || c->role > p->p_roles.nprim)
593 if (!c->user || c->user > p->p_users.nprim)
596 if (!c->type || c->type > p->p_types.nprim)
599 if (c->role != OBJECT_R_VAL) {
601 * Role must be authorized for the type.
603 role = p->role_val_to_struct[c->role - 1];
604 if (!ebitmap_get_bit(&role->types,
606 /* role may not be associated with type */
610 * User must be authorized for the role.
612 usrdatum = p->user_val_to_struct[c->user - 1];
616 if (!ebitmap_get_bit(&usrdatum->roles,
618 /* user may not be associated with role */
622 if (!mls_context_isvalid(p, c))
629 * Read and validate a security context structure
630 * from a policydb binary representation file.
632 static int context_read_and_validate(struct context *c,
639 buf = next_entry(fp, sizeof(u32)*3);
641 printk(KERN_ERR "security: context truncated\n");
645 c->user = le32_to_cpu(buf[0]);
646 c->role = le32_to_cpu(buf[1]);
647 c->type = le32_to_cpu(buf[2]);
648 if (mls_read_range(c, fp)) {
649 printk(KERN_ERR "security: error reading MLS range of "
655 if (!policydb_context_isvalid(p, c)) {
656 printk(KERN_ERR "security: invalid security context\n");
665 * The following *_read functions are used to
666 * read the symbol data from a policy database
667 * binary representation file.
670 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
673 struct perm_datum *perdatum;
677 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
682 memset(perdatum, 0, sizeof(*perdatum));
684 buf = next_entry(fp, sizeof(u32)*2);
690 len = le32_to_cpu(buf[0]);
691 perdatum->value = le32_to_cpu(buf[1]);
692 rc = mls_read_perm(perdatum, fp);
696 buf = next_entry(fp, len);
701 key = kmalloc(len + 1,GFP_KERNEL);
706 memcpy(key, buf, len);
709 rc = hashtab_insert(h, key, perdatum);
715 perm_destroy(key, perdatum, NULL);
719 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
722 struct common_datum *comdatum;
726 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
731 memset(comdatum, 0, sizeof(*comdatum));
733 buf = next_entry(fp, sizeof(u32)*4);
739 len = le32_to_cpu(buf[0]);
740 comdatum->value = le32_to_cpu(buf[1]);
742 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
745 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
746 nel = le32_to_cpu(buf[3]);
748 buf = next_entry(fp, len);
753 key = kmalloc(len + 1,GFP_KERNEL);
758 memcpy(key, buf, len);
761 for (i = 0; i < nel; i++) {
762 rc = perm_read(p, comdatum->permissions.table, fp);
767 rc = hashtab_insert(h, key, comdatum);
773 common_destroy(key, comdatum, NULL);
777 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
780 struct class_datum *cladatum;
781 struct constraint_node *c, *lc;
782 struct constraint_expr *e, *le;
783 u32 *buf, len, len2, ncons, nexpr, nel;
786 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
791 memset(cladatum, 0, sizeof(*cladatum));
793 buf = next_entry(fp, sizeof(u32)*6);
799 len = le32_to_cpu(buf[0]);
800 len2 = le32_to_cpu(buf[1]);
801 cladatum->value = le32_to_cpu(buf[2]);
803 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
806 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
807 nel = le32_to_cpu(buf[4]);
809 ncons = le32_to_cpu(buf[5]);
811 buf = next_entry(fp, len);
816 key = kmalloc(len + 1,GFP_KERNEL);
821 memcpy(key, buf, len);
825 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
826 if (!cladatum->comkey) {
830 buf = next_entry(fp, len2);
835 memcpy(cladatum->comkey, buf, len2);
836 cladatum->comkey[len2] = 0;
838 cladatum->comdatum = hashtab_search(p->p_commons.table,
840 if (!cladatum->comdatum) {
841 printk(KERN_ERR "security: unknown common %s\n",
847 for (i = 0; i < nel; i++) {
848 rc = perm_read(p, cladatum->permissions.table, fp);
855 for (i = 0; i < ncons; i++) {
856 c = kmalloc(sizeof(*c), GFP_KERNEL);
861 memset(c, 0, sizeof(*c));
866 cladatum->constraints = c;
869 buf = next_entry(fp, sizeof(u32)*2);
872 c->permissions = le32_to_cpu(buf[0]);
873 nexpr = le32_to_cpu(buf[1]);
876 for (j = 0; j < nexpr; j++) {
877 e = kmalloc(sizeof(*e), GFP_KERNEL);
882 memset(e, 0, sizeof(*e));
890 buf = next_entry(fp, sizeof(u32)*3);
893 e->expr_type = le32_to_cpu(buf[0]);
894 e->attr = le32_to_cpu(buf[1]);
895 e->op = le32_to_cpu(buf[2]);
897 switch (e->expr_type) {
909 if (depth == (CEXPR_MAXDEPTH-1))
914 if (depth == (CEXPR_MAXDEPTH-1))
917 if (ebitmap_read(&e->names, fp))
930 rc = mls_read_class(cladatum, fp);
934 rc = hashtab_insert(h, key, cladatum);
940 class_destroy(key, cladatum, NULL);
944 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
947 struct role_datum *role;
951 role = kmalloc(sizeof(*role), GFP_KERNEL);
956 memset(role, 0, sizeof(*role));
958 buf = next_entry(fp, sizeof(u32)*2);
964 len = le32_to_cpu(buf[0]);
965 role->value = le32_to_cpu(buf[1]);
967 buf = next_entry(fp, len);
972 key = kmalloc(len + 1,GFP_KERNEL);
977 memcpy(key, buf, len);
980 rc = ebitmap_read(&role->dominates, fp);
984 rc = ebitmap_read(&role->types, fp);
988 if (strcmp(key, OBJECT_R) == 0) {
989 if (role->value != OBJECT_R_VAL) {
990 printk(KERN_ERR "Role %s has wrong value %d\n",
991 OBJECT_R, role->value);
999 rc = hashtab_insert(h, key, role);
1005 role_destroy(key, role, NULL);
1009 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1012 struct type_datum *typdatum;
1016 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1021 memset(typdatum, 0, sizeof(*typdatum));
1023 buf = next_entry(fp, sizeof(u32)*3);
1029 len = le32_to_cpu(buf[0]);
1030 typdatum->value = le32_to_cpu(buf[1]);
1031 typdatum->primary = le32_to_cpu(buf[2]);
1033 buf = next_entry(fp, len);
1038 key = kmalloc(len + 1,GFP_KERNEL);
1043 memcpy(key, buf, len);
1046 rc = hashtab_insert(h, key, typdatum);
1052 type_destroy(key, typdatum, NULL);
1056 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1059 struct user_datum *usrdatum;
1064 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1069 memset(usrdatum, 0, sizeof(*usrdatum));
1071 buf = next_entry(fp, sizeof(u32)*2);
1077 len = le32_to_cpu(buf[0]);
1078 usrdatum->value = le32_to_cpu(buf[1]);
1080 buf = next_entry(fp, len);
1085 key = kmalloc(len + 1,GFP_KERNEL);
1090 memcpy(key, buf, len);
1093 rc = ebitmap_read(&usrdatum->roles, fp);
1097 rc = mls_read_user(usrdatum, fp);
1101 rc = hashtab_insert(h, key, usrdatum);
1107 user_destroy(key, usrdatum, NULL);
1111 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1122 #define mls_config(x) \
1123 ((x) & POLICYDB_CONFIG_MLS) ? "mls" : "no_mls"
1126 * Read the configuration data from a policy database binary
1127 * representation file into a policy database structure.
1129 int policydb_read(struct policydb *p, void *fp)
1131 struct role_allow *ra, *lra;
1132 struct role_trans *tr, *ltr;
1133 struct ocontext *l, *c, *newc;
1134 struct genfs *genfs_p, *genfs, *newgenfs;
1135 int i, j, rc, r_policyvers = 0;
1136 u32 *buf, len, len2, config, nprim, nel, nel2;
1138 struct policydb_compat_info *info;
1141 mls_set_config(config);
1143 rc = policydb_init(p);
1148 /* Read the magic number and string length. */
1149 buf = next_entry(fp, sizeof(u32)* 2);
1153 for (i = 0; i < 2; i++)
1154 buf[i] = le32_to_cpu(buf[i]);
1156 if (buf[0] != POLICYDB_MAGIC) {
1157 printk(KERN_ERR "security: policydb magic number 0x%x does "
1158 "not match expected magic number 0x%x\n",
1159 buf[0], POLICYDB_MAGIC);
1164 if (len != strlen(POLICYDB_STRING)) {
1165 printk(KERN_ERR "security: policydb string length %d does not "
1166 "match expected length %Zu\n",
1167 len, strlen(POLICYDB_STRING));
1170 buf = next_entry(fp, len);
1172 printk(KERN_ERR "security: truncated policydb string identifier\n");
1175 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1176 if (!policydb_str) {
1177 printk(KERN_ERR "security: unable to allocate memory for policydb "
1178 "string of length %d\n", len);
1182 memcpy(policydb_str, buf, len);
1183 policydb_str[len] = 0;
1184 if (strcmp(policydb_str, POLICYDB_STRING)) {
1185 printk(KERN_ERR "security: policydb string %s does not match "
1186 "my string %s\n", policydb_str, POLICYDB_STRING);
1187 kfree(policydb_str);
1190 /* Done with policydb_str. */
1191 kfree(policydb_str);
1192 policydb_str = NULL;
1194 /* Read the version, config, and table sizes. */
1195 buf = next_entry(fp, sizeof(u32)*4);
1198 for (i = 0; i < 4; i++)
1199 buf[i] = le32_to_cpu(buf[i]);
1201 r_policyvers = buf[0];
1202 if (r_policyvers < POLICYDB_VERSION_MIN ||
1203 r_policyvers > POLICYDB_VERSION_MAX) {
1204 printk(KERN_ERR "security: policydb version %d does not match "
1205 "my version range %d-%d\n",
1206 buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1210 if (buf[1] != config) {
1211 printk(KERN_ERR "security: policydb configuration (%s) does "
1212 "not match my configuration (%s)\n",
1214 mls_config(config));
1219 info = policydb_lookup_compat(r_policyvers);
1221 printk(KERN_ERR "security: unable to find policy compat info "
1222 "for version %d\n", r_policyvers);
1226 if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1227 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1228 "not match mine (%d,%d)\n", buf[2], buf[3],
1229 info->sym_num, info->ocon_num);
1233 rc = mls_read_nlevels(p, fp);
1237 for (i = 0; i < info->sym_num; i++) {
1238 buf = next_entry(fp, sizeof(u32)*2);
1243 nprim = le32_to_cpu(buf[0]);
1244 nel = le32_to_cpu(buf[1]);
1245 for (j = 0; j < nel; j++) {
1246 rc = read_f[i](p, p->symtab[i].table, fp);
1251 p->symtab[i].nprim = nprim;
1254 rc = avtab_read(&p->te_avtab, fp, config);
1258 if (r_policyvers >= POLICYDB_VERSION_BOOL) {
1259 rc = cond_read_list(p, fp);
1264 buf = next_entry(fp, sizeof(u32));
1269 nel = le32_to_cpu(buf[0]);
1271 for (i = 0; i < nel; i++) {
1272 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1277 memset(tr, 0, sizeof(*tr));
1283 buf = next_entry(fp, sizeof(u32)*3);
1288 tr->role = le32_to_cpu(buf[0]);
1289 tr->type = le32_to_cpu(buf[1]);
1290 tr->new_role = le32_to_cpu(buf[2]);
1294 buf = next_entry(fp, sizeof(u32));
1299 nel = le32_to_cpu(buf[0]);
1301 for (i = 0; i < nel; i++) {
1302 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1307 memset(ra, 0, sizeof(*ra));
1313 buf = next_entry(fp, sizeof(u32)*2);
1318 ra->role = le32_to_cpu(buf[0]);
1319 ra->new_role = le32_to_cpu(buf[1]);
1323 rc = policydb_index_classes(p);
1327 rc = policydb_index_others(p);
1331 for (i = 0; i < info->ocon_num; i++) {
1332 buf = next_entry(fp, sizeof(u32));
1337 nel = le32_to_cpu(buf[0]);
1339 for (j = 0; j < nel; j++) {
1340 c = kmalloc(sizeof(*c), GFP_KERNEL);
1345 memset(c, 0, sizeof(*c));
1349 p->ocontexts[i] = c;
1355 buf = next_entry(fp, sizeof(u32));
1358 c->sid[0] = le32_to_cpu(buf[0]);
1359 rc = context_read_and_validate(&c->context[0], p, fp);
1365 buf = next_entry(fp, sizeof(u32));
1368 len = le32_to_cpu(buf[0]);
1369 buf = next_entry(fp, len);
1372 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1377 memcpy(c->u.name, buf, len);
1379 rc = context_read_and_validate(&c->context[0], p, fp);
1382 rc = context_read_and_validate(&c->context[1], p, fp);
1387 buf = next_entry(fp, sizeof(u32)*3);
1390 c->u.port.protocol = le32_to_cpu(buf[0]);
1391 c->u.port.low_port = le32_to_cpu(buf[1]);
1392 c->u.port.high_port = le32_to_cpu(buf[2]);
1393 rc = context_read_and_validate(&c->context[0], p, fp);
1398 buf = next_entry(fp, sizeof(u32)* 2);
1401 c->u.node.addr = le32_to_cpu(buf[0]);
1402 c->u.node.mask = le32_to_cpu(buf[1]);
1403 rc = context_read_and_validate(&c->context[0], p, fp);
1408 buf = next_entry(fp, sizeof(u32)*2);
1411 c->v.behavior = le32_to_cpu(buf[0]);
1412 if (c->v.behavior > SECURITY_FS_USE_NONE)
1414 len = le32_to_cpu(buf[1]);
1415 buf = next_entry(fp, len);
1418 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1423 memcpy(c->u.name, buf, len);
1425 rc = context_read_and_validate(&c->context[0], p, fp);
1432 buf = next_entry(fp, sizeof(u32) * 8);
1435 for (k = 0; k < 4; k++)
1436 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1437 for (k = 0; k < 4; k++)
1438 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1439 if (context_read_and_validate(&c->context[0], p, fp))
1447 buf = next_entry(fp, sizeof(u32));
1452 nel = le32_to_cpu(buf[0]);
1455 for (i = 0; i < nel; i++) {
1456 buf = next_entry(fp, sizeof(u32));
1459 len = le32_to_cpu(buf[0]);
1460 buf = next_entry(fp, len);
1463 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1468 memset(newgenfs, 0, sizeof(*newgenfs));
1470 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1471 if (!newgenfs->fstype) {
1476 memcpy(newgenfs->fstype, buf, len);
1477 newgenfs->fstype[len] = 0;
1478 for (genfs_p = NULL, genfs = p->genfs; genfs;
1479 genfs_p = genfs, genfs = genfs->next) {
1480 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1481 printk(KERN_ERR "security: dup genfs "
1482 "fstype %s\n", newgenfs->fstype);
1483 kfree(newgenfs->fstype);
1487 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1490 newgenfs->next = genfs;
1492 genfs_p->next = newgenfs;
1494 p->genfs = newgenfs;
1495 buf = next_entry(fp, sizeof(u32));
1498 nel2 = le32_to_cpu(buf[0]);
1499 for (j = 0; j < nel2; j++) {
1500 buf = next_entry(fp, sizeof(u32));
1503 len = le32_to_cpu(buf[0]);
1504 buf = next_entry(fp, len);
1508 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1513 memset(newc, 0, sizeof(*newc));
1515 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1516 if (!newc->u.name) {
1520 memcpy(newc->u.name, buf, len);
1521 newc->u.name[len] = 0;
1522 buf = next_entry(fp, sizeof(u32));
1525 newc->v.sclass = le32_to_cpu(buf[0]);
1526 if (context_read_and_validate(&newc->context[0], p, fp))
1528 for (l = NULL, c = newgenfs->head; c;
1529 l = c, c = c->next) {
1530 if (!strcmp(newc->u.name, c->u.name) &&
1531 (!c->v.sclass || !newc->v.sclass ||
1532 newc->v.sclass == c->v.sclass)) {
1533 printk(KERN_ERR "security: dup genfs "
1535 newgenfs->fstype, c->u.name);
1538 len = strlen(newc->u.name);
1539 len2 = strlen(c->u.name);
1548 newgenfs->head = newc;
1552 rc = mls_read_trusted(p, fp);
1556 policydb_loaded_version = r_policyvers;
1559 ocontext_destroy(newc,OCON_FSUSE);
1561 policydb_destroy(p);