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 static unsigned int symtab_sizes[SYM_NUM] = {
51 struct policydb_compat_info {
57 /* These need to be updated if SYM_NUM or OCON_NUM changes */
58 static struct policydb_compat_info policydb_compat[] = {
60 .version = POLICYDB_VERSION_BASE,
61 .sym_num = SYM_NUM - 1,
62 .ocon_num = OCON_NUM - 1,
65 .version = POLICYDB_VERSION_BOOL,
67 .ocon_num = OCON_NUM - 1,
70 .version = POLICYDB_VERSION_IPV6,
75 .version = POLICYDB_VERSION_NLCLASS,
81 static struct policydb_compat_info *policydb_lookup_compat(int version)
84 struct policydb_compat_info *info = NULL;
86 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
87 if (policydb_compat[i].version == version) {
88 info = &policydb_compat[i];
96 * Initialize the role table.
98 int roles_init(struct policydb *p)
102 struct role_datum *role;
104 role = kmalloc(sizeof(*role), GFP_KERNEL);
109 memset(role, 0, sizeof(*role));
110 role->value = ++p->p_roles.nprim;
111 if (role->value != OBJECT_R_VAL) {
115 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
120 strcpy(key, OBJECT_R);
121 rc = hashtab_insert(p->p_roles.table, key, role);
135 * Initialize a policy database structure.
137 int policydb_init(struct policydb *p)
141 memset(p, 0, sizeof(*p));
143 for (i = 0; i < SYM_NUM; i++) {
144 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
146 goto out_free_symtab;
149 rc = avtab_init(&p->te_avtab);
151 goto out_free_symtab;
157 rc = cond_policydb_init(p);
165 avtab_destroy(&p->te_avtab);
168 for (i = 0; i < SYM_NUM; i++)
169 hashtab_destroy(p->symtab[i].table);
174 * The following *_index functions are used to
175 * define the val_to_name and val_to_struct arrays
176 * in a policy database structure. The val_to_name
177 * arrays are used when converting security context
178 * structures into string representations. The
179 * val_to_struct arrays are used when the attributes
180 * of a class, role, or user are needed.
183 static int common_index(void *key, void *datum, void *datap)
186 struct common_datum *comdatum;
190 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
192 p->p_common_val_to_name[comdatum->value - 1] = key;
196 static int class_index(void *key, void *datum, void *datap)
199 struct class_datum *cladatum;
203 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
205 p->p_class_val_to_name[cladatum->value - 1] = key;
206 p->class_val_to_struct[cladatum->value - 1] = cladatum;
210 static int role_index(void *key, void *datum, void *datap)
213 struct role_datum *role;
217 if (!role->value || role->value > p->p_roles.nprim)
219 p->p_role_val_to_name[role->value - 1] = key;
220 p->role_val_to_struct[role->value - 1] = role;
224 static int type_index(void *key, void *datum, void *datap)
227 struct type_datum *typdatum;
232 if (typdatum->primary) {
233 if (!typdatum->value || typdatum->value > p->p_types.nprim)
235 p->p_type_val_to_name[typdatum->value - 1] = key;
241 static int user_index(void *key, void *datum, void *datap)
244 struct user_datum *usrdatum;
248 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
250 p->p_user_val_to_name[usrdatum->value - 1] = key;
251 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
255 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
267 * Define the common val_to_name array and the class
268 * val_to_name and val_to_struct arrays in a policy
269 * database structure.
271 * Caller must clean up upon failure.
273 int policydb_index_classes(struct policydb *p)
277 p->p_common_val_to_name =
278 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
279 if (!p->p_common_val_to_name) {
284 rc = hashtab_map(p->p_commons.table, common_index, p);
288 p->class_val_to_struct =
289 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
290 if (!p->class_val_to_struct) {
295 p->p_class_val_to_name =
296 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
297 if (!p->p_class_val_to_name) {
302 rc = hashtab_map(p->p_classes.table, class_index, p);
308 static void symtab_hash_eval(struct symtab *s)
312 for (i = 0; i < SYM_NUM; i++) {
313 struct hashtab *h = s[i].table;
314 struct hashtab_info info;
316 hashtab_stat(h, &info);
317 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
318 "longest chain length %d\n", symtab_name[i], h->nel,
319 info.slots_used, h->size, info.max_chain_len);
325 * Define the other val_to_name and val_to_struct arrays
326 * in a policy database structure.
328 * Caller must clean up on failure.
330 int policydb_index_others(struct policydb *p)
334 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
335 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
336 mls_policydb_index_others(p);
339 printk(KERN_INFO "security: %d classes, %d rules\n",
340 p->p_classes.nprim, p->te_avtab.nel);
343 avtab_hash_eval(&p->te_avtab, "rules");
344 symtab_hash_eval(p->symtab);
347 p->role_val_to_struct =
348 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
350 if (!p->role_val_to_struct) {
355 p->user_val_to_struct =
356 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
358 if (!p->user_val_to_struct) {
363 if (cond_init_bool_indexes(p)) {
368 for (i = SYM_ROLES; i < SYM_NUM; i++) {
369 p->sym_val_to_name[i] =
370 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
371 if (!p->sym_val_to_name[i]) {
375 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
385 * The following *_destroy functions are used to
386 * free any memory allocated for each kind of
387 * symbol data in the policy database.
390 static int perm_destroy(void *key, void *datum, void *p)
397 static int common_destroy(void *key, void *datum, void *p)
399 struct common_datum *comdatum;
403 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
404 hashtab_destroy(comdatum->permissions.table);
409 static int class_destroy(void *key, void *datum, void *p)
411 struct class_datum *cladatum;
412 struct constraint_node *constraint, *ctemp;
413 struct constraint_expr *e, *etmp;
417 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
418 hashtab_destroy(cladatum->permissions.table);
419 constraint = cladatum->constraints;
421 e = constraint->expr;
423 ebitmap_destroy(&e->names);
429 constraint = constraint->next;
432 kfree(cladatum->comkey);
437 static int role_destroy(void *key, void *datum, void *p)
439 struct role_datum *role;
443 ebitmap_destroy(&role->dominates);
444 ebitmap_destroy(&role->types);
449 static int type_destroy(void *key, void *datum, void *p)
456 static int user_destroy(void *key, void *datum, void *p)
458 struct user_datum *usrdatum;
462 ebitmap_destroy(&usrdatum->roles);
463 mls_user_destroy(usrdatum);
468 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
479 void ocontext_destroy(struct ocontext *c, int i)
481 context_destroy(&c->context[0]);
482 context_destroy(&c->context[1]);
483 if (i == OCON_ISID || i == OCON_FS ||
484 i == OCON_NETIF || i == OCON_FSUSE)
490 * Free any memory allocated by a policy database structure.
492 void policydb_destroy(struct policydb *p)
494 struct ocontext *c, *ctmp;
495 struct genfs *g, *gtmp;
498 for (i = 0; i < SYM_NUM; i++) {
499 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
500 hashtab_destroy(p->symtab[i].table);
503 for (i = 0; i < SYM_NUM; i++) {
504 if (p->sym_val_to_name[i])
505 kfree(p->sym_val_to_name[i]);
508 if (p->class_val_to_struct)
509 kfree(p->class_val_to_struct);
510 if (p->role_val_to_struct)
511 kfree(p->role_val_to_struct);
512 if (p->user_val_to_struct)
513 kfree(p->user_val_to_struct);
515 avtab_destroy(&p->te_avtab);
517 for (i = 0; i < OCON_NUM; i++) {
522 ocontext_destroy(ctmp,i);
533 ocontext_destroy(ctmp,OCON_FSUSE);
540 cond_policydb_destroy(p);
546 * Load the initial SIDs specified in a policy database
547 * structure into a SID table.
549 int policydb_load_isids(struct policydb *p, struct sidtab *s)
551 struct ocontext *head, *c;
556 printk(KERN_ERR "security: out of memory on SID table init\n");
560 head = p->ocontexts[OCON_ISID];
561 for (c = head; c; c = c->next) {
562 if (!c->context[0].user) {
563 printk(KERN_ERR "security: SID %s was never "
564 "defined.\n", c->u.name);
568 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
569 printk(KERN_ERR "security: unable to load initial "
570 "SID %s.\n", c->u.name);
580 * Return 1 if the fields in the security context
581 * structure `c' are valid. Return 0 otherwise.
583 int policydb_context_isvalid(struct policydb *p, struct context *c)
585 struct role_datum *role;
586 struct user_datum *usrdatum;
588 if (!c->role || c->role > p->p_roles.nprim)
591 if (!c->user || c->user > p->p_users.nprim)
594 if (!c->type || c->type > p->p_types.nprim)
597 if (c->role != OBJECT_R_VAL) {
599 * Role must be authorized for the type.
601 role = p->role_val_to_struct[c->role - 1];
602 if (!ebitmap_get_bit(&role->types,
604 /* role may not be associated with type */
608 * User must be authorized for the role.
610 usrdatum = p->user_val_to_struct[c->user - 1];
614 if (!ebitmap_get_bit(&usrdatum->roles,
616 /* user may not be associated with role */
620 if (!mls_context_isvalid(p, c))
627 * Read and validate a security context structure
628 * from a policydb binary representation file.
630 static int context_read_and_validate(struct context *c,
637 rc = next_entry(buf, fp, sizeof buf);
639 printk(KERN_ERR "security: context truncated\n");
642 c->user = le32_to_cpu(buf[0]);
643 c->role = le32_to_cpu(buf[1]);
644 c->type = le32_to_cpu(buf[2]);
645 if (mls_read_range(c, fp)) {
646 printk(KERN_ERR "security: error reading MLS range of "
652 if (!policydb_context_isvalid(p, c)) {
653 printk(KERN_ERR "security: invalid security context\n");
662 * The following *_read functions are used to
663 * read the symbol data from a policy database
664 * binary representation file.
667 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
670 struct perm_datum *perdatum;
674 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
679 memset(perdatum, 0, sizeof(*perdatum));
681 rc = next_entry(buf, fp, sizeof buf);
685 len = le32_to_cpu(buf[0]);
686 perdatum->value = le32_to_cpu(buf[1]);
687 rc = mls_read_perm(perdatum, fp);
691 key = kmalloc(len + 1,GFP_KERNEL);
696 rc = next_entry(key, fp, len);
701 rc = hashtab_insert(h, key, perdatum);
707 perm_destroy(key, perdatum, NULL);
711 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
714 struct common_datum *comdatum;
715 u32 buf[4], len, nel;
718 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
723 memset(comdatum, 0, sizeof(*comdatum));
725 rc = next_entry(buf, fp, sizeof buf);
729 len = le32_to_cpu(buf[0]);
730 comdatum->value = le32_to_cpu(buf[1]);
732 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
735 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
736 nel = le32_to_cpu(buf[3]);
738 key = kmalloc(len + 1,GFP_KERNEL);
743 rc = next_entry(key, fp, len);
748 for (i = 0; i < nel; i++) {
749 rc = perm_read(p, comdatum->permissions.table, fp);
754 rc = hashtab_insert(h, key, comdatum);
760 common_destroy(key, comdatum, NULL);
764 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
767 struct class_datum *cladatum;
768 struct constraint_node *c, *lc;
769 struct constraint_expr *e, *le;
770 u32 buf[6], len, len2, ncons, nexpr, nel;
773 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
778 memset(cladatum, 0, sizeof(*cladatum));
780 rc = next_entry(buf, fp, sizeof(u32)*6);
784 len = le32_to_cpu(buf[0]);
785 len2 = le32_to_cpu(buf[1]);
786 cladatum->value = le32_to_cpu(buf[2]);
788 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
791 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
792 nel = le32_to_cpu(buf[4]);
794 ncons = le32_to_cpu(buf[5]);
796 key = kmalloc(len + 1,GFP_KERNEL);
801 rc = next_entry(key, fp, len);
807 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
808 if (!cladatum->comkey) {
812 rc = next_entry(cladatum->comkey, fp, len2);
815 cladatum->comkey[len2] = 0;
817 cladatum->comdatum = hashtab_search(p->p_commons.table,
819 if (!cladatum->comdatum) {
820 printk(KERN_ERR "security: unknown common %s\n",
826 for (i = 0; i < nel; i++) {
827 rc = perm_read(p, cladatum->permissions.table, fp);
833 for (i = 0; i < ncons; i++) {
834 c = kmalloc(sizeof(*c), GFP_KERNEL);
839 memset(c, 0, sizeof(*c));
844 cladatum->constraints = c;
847 rc = next_entry(buf, fp, sizeof(u32)*2);
850 c->permissions = le32_to_cpu(buf[0]);
851 nexpr = le32_to_cpu(buf[1]);
854 for (j = 0; j < nexpr; j++) {
855 e = kmalloc(sizeof(*e), GFP_KERNEL);
860 memset(e, 0, sizeof(*e));
868 rc = next_entry(buf, fp, sizeof(u32)*3);
871 e->expr_type = le32_to_cpu(buf[0]);
872 e->attr = le32_to_cpu(buf[1]);
873 e->op = le32_to_cpu(buf[2]);
876 switch (e->expr_type) {
888 if (depth == (CEXPR_MAXDEPTH-1))
893 if (depth == (CEXPR_MAXDEPTH-1))
896 if (ebitmap_read(&e->names, fp))
909 rc = mls_read_class(cladatum, fp);
913 rc = hashtab_insert(h, key, cladatum);
921 class_destroy(key, cladatum, NULL);
925 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
928 struct role_datum *role;
932 role = kmalloc(sizeof(*role), GFP_KERNEL);
937 memset(role, 0, sizeof(*role));
939 rc = next_entry(buf, fp, sizeof buf);
943 len = le32_to_cpu(buf[0]);
944 role->value = le32_to_cpu(buf[1]);
946 key = kmalloc(len + 1,GFP_KERNEL);
951 rc = next_entry(key, fp, len);
956 rc = ebitmap_read(&role->dominates, fp);
960 rc = ebitmap_read(&role->types, fp);
964 if (strcmp(key, OBJECT_R) == 0) {
965 if (role->value != OBJECT_R_VAL) {
966 printk(KERN_ERR "Role %s has wrong value %d\n",
967 OBJECT_R, role->value);
975 rc = hashtab_insert(h, key, role);
981 role_destroy(key, role, NULL);
985 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
988 struct type_datum *typdatum;
992 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
997 memset(typdatum, 0, sizeof(*typdatum));
999 rc = next_entry(buf, fp, sizeof buf);
1003 len = le32_to_cpu(buf[0]);
1004 typdatum->value = le32_to_cpu(buf[1]);
1005 typdatum->primary = le32_to_cpu(buf[2]);
1007 key = kmalloc(len + 1,GFP_KERNEL);
1012 rc = next_entry(key, fp, len);
1017 rc = hashtab_insert(h, key, typdatum);
1023 type_destroy(key, typdatum, NULL);
1027 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1030 struct user_datum *usrdatum;
1035 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1040 memset(usrdatum, 0, sizeof(*usrdatum));
1042 rc = next_entry(buf, fp, sizeof buf);
1046 len = le32_to_cpu(buf[0]);
1047 usrdatum->value = le32_to_cpu(buf[1]);
1049 key = kmalloc(len + 1,GFP_KERNEL);
1054 rc = next_entry(key, fp, len);
1059 rc = ebitmap_read(&usrdatum->roles, fp);
1063 rc = mls_read_user(usrdatum, fp);
1067 rc = hashtab_insert(h, key, usrdatum);
1073 user_destroy(key, usrdatum, NULL);
1077 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1088 #define mls_config(x) \
1089 ((x) & POLICYDB_CONFIG_MLS) ? "mls" : "no_mls"
1092 * Read the configuration data from a policy database binary
1093 * representation file into a policy database structure.
1095 int policydb_read(struct policydb *p, void *fp)
1097 struct role_allow *ra, *lra;
1098 struct role_trans *tr, *ltr;
1099 struct ocontext *l, *c, *newc;
1100 struct genfs *genfs_p, *genfs, *newgenfs;
1102 u32 buf[8], len, len2, config, nprim, nel, nel2;
1104 struct policydb_compat_info *info;
1107 mls_set_config(config);
1109 rc = policydb_init(p);
1113 /* Read the magic number and string length. */
1114 rc = next_entry(buf, fp, sizeof(u32)* 2);
1118 for (i = 0; i < 2; i++)
1119 buf[i] = le32_to_cpu(buf[i]);
1121 if (buf[0] != POLICYDB_MAGIC) {
1122 printk(KERN_ERR "security: policydb magic number 0x%x does "
1123 "not match expected magic number 0x%x\n",
1124 buf[0], POLICYDB_MAGIC);
1129 if (len != strlen(POLICYDB_STRING)) {
1130 printk(KERN_ERR "security: policydb string length %d does not "
1131 "match expected length %Zu\n",
1132 len, strlen(POLICYDB_STRING));
1135 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1136 if (!policydb_str) {
1137 printk(KERN_ERR "security: unable to allocate memory for policydb "
1138 "string of length %d\n", len);
1142 rc = next_entry(policydb_str, fp, len);
1144 printk(KERN_ERR "security: truncated policydb string identifier\n");
1145 kfree(policydb_str);
1148 policydb_str[len] = 0;
1149 if (strcmp(policydb_str, POLICYDB_STRING)) {
1150 printk(KERN_ERR "security: policydb string %s does not match "
1151 "my string %s\n", policydb_str, POLICYDB_STRING);
1152 kfree(policydb_str);
1155 /* Done with policydb_str. */
1156 kfree(policydb_str);
1157 policydb_str = NULL;
1159 /* Read the version, config, and table sizes. */
1160 rc = next_entry(buf, fp, sizeof(u32)*4);
1163 for (i = 0; i < 4; i++)
1164 buf[i] = le32_to_cpu(buf[i]);
1166 p->policyvers = buf[0];
1167 if (p->policyvers < POLICYDB_VERSION_MIN ||
1168 p->policyvers > POLICYDB_VERSION_MAX) {
1169 printk(KERN_ERR "security: policydb version %d does not match "
1170 "my version range %d-%d\n",
1171 buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1175 if (buf[1] != config) {
1176 printk(KERN_ERR "security: policydb configuration (%s) does "
1177 "not match my configuration (%s)\n",
1179 mls_config(config));
1184 info = policydb_lookup_compat(p->policyvers);
1186 printk(KERN_ERR "security: unable to find policy compat info "
1187 "for version %d\n", p->policyvers);
1191 if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1192 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1193 "not match mine (%d,%d)\n", buf[2], buf[3],
1194 info->sym_num, info->ocon_num);
1198 rc = mls_read_nlevels(p, fp);
1202 for (i = 0; i < info->sym_num; i++) {
1203 rc = next_entry(buf, fp, sizeof(u32)*2);
1206 nprim = le32_to_cpu(buf[0]);
1207 nel = le32_to_cpu(buf[1]);
1208 for (j = 0; j < nel; j++) {
1209 rc = read_f[i](p, p->symtab[i].table, fp);
1214 p->symtab[i].nprim = nprim;
1217 rc = avtab_read(&p->te_avtab, fp, config);
1221 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1222 rc = cond_read_list(p, fp);
1227 rc = next_entry(buf, fp, sizeof(u32));
1230 nel = le32_to_cpu(buf[0]);
1232 for (i = 0; i < nel; i++) {
1233 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1238 memset(tr, 0, sizeof(*tr));
1244 rc = next_entry(buf, fp, sizeof(u32)*3);
1247 tr->role = le32_to_cpu(buf[0]);
1248 tr->type = le32_to_cpu(buf[1]);
1249 tr->new_role = le32_to_cpu(buf[2]);
1253 rc = next_entry(buf, fp, sizeof(u32));
1256 nel = le32_to_cpu(buf[0]);
1258 for (i = 0; i < nel; i++) {
1259 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1264 memset(ra, 0, sizeof(*ra));
1270 rc = next_entry(buf, fp, sizeof(u32)*2);
1273 ra->role = le32_to_cpu(buf[0]);
1274 ra->new_role = le32_to_cpu(buf[1]);
1278 rc = policydb_index_classes(p);
1282 rc = policydb_index_others(p);
1286 for (i = 0; i < info->ocon_num; i++) {
1287 rc = next_entry(buf, fp, sizeof(u32));
1290 nel = le32_to_cpu(buf[0]);
1292 for (j = 0; j < nel; j++) {
1293 c = kmalloc(sizeof(*c), GFP_KERNEL);
1298 memset(c, 0, sizeof(*c));
1302 p->ocontexts[i] = c;
1308 rc = next_entry(buf, fp, sizeof(u32));
1311 c->sid[0] = le32_to_cpu(buf[0]);
1312 rc = context_read_and_validate(&c->context[0], p, fp);
1318 rc = next_entry(buf, fp, sizeof(u32));
1321 len = le32_to_cpu(buf[0]);
1322 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1327 rc = next_entry(c->u.name, fp, len);
1331 rc = context_read_and_validate(&c->context[0], p, fp);
1334 rc = context_read_and_validate(&c->context[1], p, fp);
1339 rc = next_entry(buf, fp, sizeof(u32)*3);
1342 c->u.port.protocol = le32_to_cpu(buf[0]);
1343 c->u.port.low_port = le32_to_cpu(buf[1]);
1344 c->u.port.high_port = le32_to_cpu(buf[2]);
1345 rc = context_read_and_validate(&c->context[0], p, fp);
1350 rc = next_entry(buf, fp, sizeof(u32)* 2);
1353 c->u.node.addr = le32_to_cpu(buf[0]);
1354 c->u.node.mask = le32_to_cpu(buf[1]);
1355 rc = context_read_and_validate(&c->context[0], p, fp);
1360 rc = next_entry(buf, fp, sizeof(u32)*2);
1363 c->v.behavior = le32_to_cpu(buf[0]);
1364 if (c->v.behavior > SECURITY_FS_USE_NONE)
1366 len = le32_to_cpu(buf[1]);
1367 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1372 rc = next_entry(c->u.name, fp, len);
1376 rc = context_read_and_validate(&c->context[0], p, fp);
1383 rc = next_entry(buf, fp, sizeof(u32) * 8);
1386 for (k = 0; k < 4; k++)
1387 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1388 for (k = 0; k < 4; k++)
1389 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1390 if (context_read_and_validate(&c->context[0], p, fp))
1398 rc = next_entry(buf, fp, sizeof(u32));
1401 nel = le32_to_cpu(buf[0]);
1404 for (i = 0; i < nel; i++) {
1405 rc = next_entry(buf, fp, sizeof(u32));
1408 len = le32_to_cpu(buf[0]);
1409 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1414 memset(newgenfs, 0, sizeof(*newgenfs));
1416 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1417 if (!newgenfs->fstype) {
1422 rc = next_entry(newgenfs->fstype, fp, len);
1424 kfree(newgenfs->fstype);
1428 newgenfs->fstype[len] = 0;
1429 for (genfs_p = NULL, genfs = p->genfs; genfs;
1430 genfs_p = genfs, genfs = genfs->next) {
1431 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1432 printk(KERN_ERR "security: dup genfs "
1433 "fstype %s\n", newgenfs->fstype);
1434 kfree(newgenfs->fstype);
1438 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1441 newgenfs->next = genfs;
1443 genfs_p->next = newgenfs;
1445 p->genfs = newgenfs;
1446 rc = next_entry(buf, fp, sizeof(u32));
1449 nel2 = le32_to_cpu(buf[0]);
1450 for (j = 0; j < nel2; j++) {
1451 rc = next_entry(buf, fp, sizeof(u32));
1454 len = le32_to_cpu(buf[0]);
1456 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1461 memset(newc, 0, sizeof(*newc));
1463 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1464 if (!newc->u.name) {
1468 rc = next_entry(newc->u.name, fp, len);
1471 newc->u.name[len] = 0;
1472 rc = next_entry(buf, fp, sizeof(u32));
1475 newc->v.sclass = le32_to_cpu(buf[0]);
1476 if (context_read_and_validate(&newc->context[0], p, fp))
1478 for (l = NULL, c = newgenfs->head; c;
1479 l = c, c = c->next) {
1480 if (!strcmp(newc->u.name, c->u.name) &&
1481 (!c->v.sclass || !newc->v.sclass ||
1482 newc->v.sclass == c->v.sclass)) {
1483 printk(KERN_ERR "security: dup genfs "
1485 newgenfs->fstype, c->u.name);
1488 len = strlen(newc->u.name);
1489 len2 = strlen(c->u.name);
1498 newgenfs->head = newc;
1502 rc = mls_read_trusted(p, fp);
1510 ocontext_destroy(newc,OCON_FSUSE);
1514 policydb_destroy(p);