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,
76 static struct policydb_compat_info *policydb_lookup_compat(int version)
79 struct policydb_compat_info *info = NULL;
81 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
82 if (policydb_compat[i].version == version) {
83 info = &policydb_compat[i];
91 * Initialize the role table.
93 int roles_init(struct policydb *p)
97 struct role_datum *role;
99 role = kmalloc(sizeof(*role), GFP_KERNEL);
104 memset(role, 0, sizeof(*role));
105 role->value = ++p->p_roles.nprim;
106 if (role->value != OBJECT_R_VAL) {
110 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
115 strcpy(key, OBJECT_R);
116 rc = hashtab_insert(p->p_roles.table, key, role);
130 * Initialize a policy database structure.
132 int policydb_init(struct policydb *p)
136 memset(p, 0, sizeof(*p));
138 for (i = 0; i < SYM_NUM; i++) {
139 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
141 goto out_free_symtab;
144 rc = avtab_init(&p->te_avtab);
146 goto out_free_symtab;
152 rc = cond_policydb_init(p);
160 avtab_destroy(&p->te_avtab);
163 for (i = 0; i < SYM_NUM; i++)
164 hashtab_destroy(p->symtab[i].table);
169 * The following *_index functions are used to
170 * define the val_to_name and val_to_struct arrays
171 * in a policy database structure. The val_to_name
172 * arrays are used when converting security context
173 * structures into string representations. The
174 * val_to_struct arrays are used when the attributes
175 * of a class, role, or user are needed.
178 static int common_index(void *key, void *datum, void *datap)
181 struct common_datum *comdatum;
185 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
187 p->p_common_val_to_name[comdatum->value - 1] = key;
191 static int class_index(void *key, void *datum, void *datap)
194 struct class_datum *cladatum;
198 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
200 p->p_class_val_to_name[cladatum->value - 1] = key;
201 p->class_val_to_struct[cladatum->value - 1] = cladatum;
205 static int role_index(void *key, void *datum, void *datap)
208 struct role_datum *role;
212 if (!role->value || role->value > p->p_roles.nprim)
214 p->p_role_val_to_name[role->value - 1] = key;
215 p->role_val_to_struct[role->value - 1] = role;
219 static int type_index(void *key, void *datum, void *datap)
222 struct type_datum *typdatum;
227 if (typdatum->primary) {
228 if (!typdatum->value || typdatum->value > p->p_types.nprim)
230 p->p_type_val_to_name[typdatum->value - 1] = key;
236 static int user_index(void *key, void *datum, void *datap)
239 struct user_datum *usrdatum;
243 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
245 p->p_user_val_to_name[usrdatum->value - 1] = key;
246 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
250 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
262 * Define the common val_to_name array and the class
263 * val_to_name and val_to_struct arrays in a policy
264 * database structure.
266 * Caller must clean up upon failure.
268 int policydb_index_classes(struct policydb *p)
272 p->p_common_val_to_name =
273 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
274 if (!p->p_common_val_to_name) {
279 rc = hashtab_map(p->p_commons.table, common_index, p);
283 p->class_val_to_struct =
284 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
285 if (!p->class_val_to_struct) {
290 p->p_class_val_to_name =
291 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
292 if (!p->p_class_val_to_name) {
297 rc = hashtab_map(p->p_classes.table, class_index, p);
303 static void symtab_hash_eval(struct symtab *s)
307 for (i = 0; i < SYM_NUM; i++) {
308 struct hashtab *h = s[i].table;
309 struct hashtab_info info;
311 hashtab_stat(h, &info);
312 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
313 "longest chain length %d\n", symtab_name[i], h->nel,
314 info.slots_used, h->size, info.max_chain_len);
320 * Define the other val_to_name and val_to_struct arrays
321 * in a policy database structure.
323 * Caller must clean up on failure.
325 int policydb_index_others(struct policydb *p)
329 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
330 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
331 mls_policydb_index_others(p);
334 printk(KERN_INFO "security: %d classes, %d rules\n",
335 p->p_classes.nprim, p->te_avtab.nel);
338 avtab_hash_eval(&p->te_avtab, "rules");
339 symtab_hash_eval(p->symtab);
342 p->role_val_to_struct =
343 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
345 if (!p->role_val_to_struct) {
350 p->user_val_to_struct =
351 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
353 if (!p->user_val_to_struct) {
358 if (cond_init_bool_indexes(p)) {
363 for (i = SYM_ROLES; i < SYM_NUM; i++) {
364 p->sym_val_to_name[i] =
365 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
366 if (!p->sym_val_to_name[i]) {
370 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
380 * The following *_destroy functions are used to
381 * free any memory allocated for each kind of
382 * symbol data in the policy database.
385 static int perm_destroy(void *key, void *datum, void *p)
392 static int common_destroy(void *key, void *datum, void *p)
394 struct common_datum *comdatum;
398 hashtab_map(comdatum->permissions.table, perm_destroy, 0);
399 hashtab_destroy(comdatum->permissions.table);
404 static int class_destroy(void *key, void *datum, void *p)
406 struct class_datum *cladatum;
407 struct constraint_node *constraint, *ctemp;
408 struct constraint_expr *e, *etmp;
412 hashtab_map(cladatum->permissions.table, perm_destroy, 0);
413 hashtab_destroy(cladatum->permissions.table);
414 constraint = cladatum->constraints;
416 e = constraint->expr;
418 ebitmap_destroy(&e->names);
424 constraint = constraint->next;
427 kfree(cladatum->comkey);
432 static int role_destroy(void *key, void *datum, void *p)
434 struct role_datum *role;
438 ebitmap_destroy(&role->dominates);
439 ebitmap_destroy(&role->types);
444 static int type_destroy(void *key, void *datum, void *p)
451 static int user_destroy(void *key, void *datum, void *p)
453 struct user_datum *usrdatum;
457 ebitmap_destroy(&usrdatum->roles);
458 mls_user_destroy(usrdatum);
463 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
474 void ocontext_destroy(struct ocontext *c, int i)
476 context_destroy(&c->context[0]);
477 context_destroy(&c->context[1]);
478 if (i == OCON_ISID || i == OCON_FS ||
479 i == OCON_NETIF || i == OCON_FSUSE)
485 * Free any memory allocated by a policy database structure.
487 void policydb_destroy(struct policydb *p)
489 struct ocontext *c, *ctmp;
490 struct genfs *g, *gtmp;
493 for (i = 0; i < SYM_NUM; i++) {
494 hashtab_map(p->symtab[i].table, destroy_f[i], 0);
495 hashtab_destroy(p->symtab[i].table);
498 for (i = 0; i < SYM_NUM; i++) {
499 if (p->sym_val_to_name[i])
500 kfree(p->sym_val_to_name[i]);
503 if (p->class_val_to_struct)
504 kfree(p->class_val_to_struct);
505 if (p->role_val_to_struct)
506 kfree(p->role_val_to_struct);
507 if (p->user_val_to_struct)
508 kfree(p->user_val_to_struct);
510 avtab_destroy(&p->te_avtab);
512 for (i = 0; i < OCON_NUM; i++) {
517 ocontext_destroy(ctmp,i);
528 ocontext_destroy(ctmp,OCON_FSUSE);
535 cond_policydb_destroy(p);
541 * Load the initial SIDs specified in a policy database
542 * structure into a SID table.
544 int policydb_load_isids(struct policydb *p, struct sidtab *s)
546 struct ocontext *head, *c;
551 printk(KERN_ERR "security: out of memory on SID table init\n");
555 head = p->ocontexts[OCON_ISID];
556 for (c = head; c; c = c->next) {
557 if (!c->context[0].user) {
558 printk(KERN_ERR "security: SID %s was never "
559 "defined.\n", c->u.name);
563 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
564 printk(KERN_ERR "security: unable to load initial "
565 "SID %s.\n", c->u.name);
575 * Return 1 if the fields in the security context
576 * structure `c' are valid. Return 0 otherwise.
578 int policydb_context_isvalid(struct policydb *p, struct context *c)
580 struct role_datum *role;
581 struct user_datum *usrdatum;
583 if (!c->role || c->role > p->p_roles.nprim)
586 if (!c->user || c->user > p->p_users.nprim)
589 if (!c->type || c->type > p->p_types.nprim)
592 if (c->role != OBJECT_R_VAL) {
594 * Role must be authorized for the type.
596 role = p->role_val_to_struct[c->role - 1];
597 if (!ebitmap_get_bit(&role->types,
599 /* role may not be associated with type */
603 * User must be authorized for the role.
605 usrdatum = p->user_val_to_struct[c->user - 1];
609 if (!ebitmap_get_bit(&usrdatum->roles,
611 /* user may not be associated with role */
615 if (!mls_context_isvalid(p, c))
622 * Read and validate a security context structure
623 * from a policydb binary representation file.
625 static int context_read_and_validate(struct context *c,
632 buf = next_entry(fp, sizeof(u32)*3);
634 printk(KERN_ERR "security: context truncated\n");
638 c->user = le32_to_cpu(buf[0]);
639 c->role = le32_to_cpu(buf[1]);
640 c->type = le32_to_cpu(buf[2]);
641 if (mls_read_range(c, fp)) {
642 printk(KERN_ERR "security: error reading MLS range of "
648 if (!policydb_context_isvalid(p, c)) {
649 printk(KERN_ERR "security: invalid security context\n");
658 * The following *_read functions are used to
659 * read the symbol data from a policy database
660 * binary representation file.
663 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
666 struct perm_datum *perdatum;
670 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
675 memset(perdatum, 0, sizeof(*perdatum));
677 buf = next_entry(fp, sizeof(u32)*2);
683 len = le32_to_cpu(buf[0]);
684 perdatum->value = le32_to_cpu(buf[1]);
685 rc = mls_read_perm(perdatum, fp);
689 buf = next_entry(fp, len);
694 key = kmalloc(len + 1,GFP_KERNEL);
699 memcpy(key, buf, len);
702 rc = hashtab_insert(h, key, perdatum);
708 perm_destroy(key, perdatum, NULL);
712 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
715 struct common_datum *comdatum;
719 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
724 memset(comdatum, 0, sizeof(*comdatum));
726 buf = next_entry(fp, sizeof(u32)*4);
732 len = le32_to_cpu(buf[0]);
733 comdatum->value = le32_to_cpu(buf[1]);
735 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
738 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
739 nel = le32_to_cpu(buf[3]);
741 buf = next_entry(fp, len);
746 key = kmalloc(len + 1,GFP_KERNEL);
751 memcpy(key, buf, len);
754 for (i = 0; i < nel; i++) {
755 rc = perm_read(p, comdatum->permissions.table, fp);
760 rc = hashtab_insert(h, key, comdatum);
766 common_destroy(key, comdatum, NULL);
770 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
773 struct class_datum *cladatum;
774 struct constraint_node *c, *lc;
775 struct constraint_expr *e, *le;
776 u32 *buf, len, len2, ncons, nexpr, nel;
779 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
784 memset(cladatum, 0, sizeof(*cladatum));
786 buf = next_entry(fp, sizeof(u32)*6);
792 len = le32_to_cpu(buf[0]);
793 len2 = le32_to_cpu(buf[1]);
794 cladatum->value = le32_to_cpu(buf[2]);
796 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
799 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
800 nel = le32_to_cpu(buf[4]);
802 ncons = le32_to_cpu(buf[5]);
804 buf = next_entry(fp, len);
809 key = kmalloc(len + 1,GFP_KERNEL);
814 memcpy(key, buf, len);
818 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
819 if (!cladatum->comkey) {
823 buf = next_entry(fp, len2);
828 memcpy(cladatum->comkey, buf, len2);
829 cladatum->comkey[len2] = 0;
831 cladatum->comdatum = hashtab_search(p->p_commons.table,
833 if (!cladatum->comdatum) {
834 printk(KERN_ERR "security: unknown common %s\n",
840 for (i = 0; i < nel; i++) {
841 rc = perm_read(p, cladatum->permissions.table, fp);
848 for (i = 0; i < ncons; i++) {
849 c = kmalloc(sizeof(*c), GFP_KERNEL);
854 memset(c, 0, sizeof(*c));
859 cladatum->constraints = c;
862 buf = next_entry(fp, sizeof(u32)*2);
865 c->permissions = le32_to_cpu(buf[0]);
866 nexpr = le32_to_cpu(buf[1]);
869 for (j = 0; j < nexpr; j++) {
870 e = kmalloc(sizeof(*e), GFP_KERNEL);
875 memset(e, 0, sizeof(*e));
883 buf = next_entry(fp, sizeof(u32)*3);
886 e->expr_type = le32_to_cpu(buf[0]);
887 e->attr = le32_to_cpu(buf[1]);
888 e->op = le32_to_cpu(buf[2]);
890 switch (e->expr_type) {
902 if (depth == (CEXPR_MAXDEPTH-1))
907 if (depth == (CEXPR_MAXDEPTH-1))
910 if (ebitmap_read(&e->names, fp))
923 rc = mls_read_class(cladatum, fp);
927 rc = hashtab_insert(h, key, cladatum);
933 class_destroy(key, cladatum, NULL);
937 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
940 struct role_datum *role;
944 role = kmalloc(sizeof(*role), GFP_KERNEL);
949 memset(role, 0, sizeof(*role));
951 buf = next_entry(fp, sizeof(u32)*2);
957 len = le32_to_cpu(buf[0]);
958 role->value = le32_to_cpu(buf[1]);
960 buf = next_entry(fp, len);
965 key = kmalloc(len + 1,GFP_KERNEL);
970 memcpy(key, buf, len);
973 rc = ebitmap_read(&role->dominates, fp);
977 rc = ebitmap_read(&role->types, fp);
981 if (strcmp(key, OBJECT_R) == 0) {
982 if (role->value != OBJECT_R_VAL) {
983 printk(KERN_ERR "Role %s has wrong value %d\n",
984 OBJECT_R, role->value);
992 rc = hashtab_insert(h, key, role);
998 role_destroy(key, role, NULL);
1002 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1005 struct type_datum *typdatum;
1009 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1014 memset(typdatum, 0, sizeof(*typdatum));
1016 buf = next_entry(fp, sizeof(u32)*3);
1022 len = le32_to_cpu(buf[0]);
1023 typdatum->value = le32_to_cpu(buf[1]);
1024 typdatum->primary = le32_to_cpu(buf[2]);
1026 buf = next_entry(fp, len);
1031 key = kmalloc(len + 1,GFP_KERNEL);
1036 memcpy(key, buf, len);
1039 rc = hashtab_insert(h, key, typdatum);
1045 type_destroy(key, typdatum, NULL);
1049 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1052 struct user_datum *usrdatum;
1057 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1062 memset(usrdatum, 0, sizeof(*usrdatum));
1064 buf = next_entry(fp, sizeof(u32)*2);
1070 len = le32_to_cpu(buf[0]);
1071 usrdatum->value = le32_to_cpu(buf[1]);
1073 buf = next_entry(fp, len);
1078 key = kmalloc(len + 1,GFP_KERNEL);
1083 memcpy(key, buf, len);
1086 rc = ebitmap_read(&usrdatum->roles, fp);
1090 rc = mls_read_user(usrdatum, fp);
1094 rc = hashtab_insert(h, key, usrdatum);
1100 user_destroy(key, usrdatum, NULL);
1104 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1115 #define mls_config(x) \
1116 ((x) & POLICYDB_CONFIG_MLS) ? "mls" : "no_mls"
1119 * Read the configuration data from a policy database binary
1120 * representation file into a policy database structure.
1122 int policydb_read(struct policydb *p, void *fp)
1124 struct role_allow *ra, *lra;
1125 struct role_trans *tr, *ltr;
1126 struct ocontext *l, *c, *newc;
1127 struct genfs *genfs_p, *genfs, *newgenfs;
1128 int i, j, rc, r_policyvers;
1129 u32 *buf, len, len2, config, nprim, nel, nel2;
1131 struct policydb_compat_info *info;
1134 mls_set_config(config);
1136 rc = policydb_init(p);
1141 /* Read the magic number and string length. */
1142 buf = next_entry(fp, sizeof(u32)* 2);
1146 for (i = 0; i < 2; i++)
1147 buf[i] = le32_to_cpu(buf[i]);
1149 if (buf[0] != POLICYDB_MAGIC) {
1150 printk(KERN_ERR "security: policydb magic number 0x%x does "
1151 "not match expected magic number 0x%x\n",
1152 buf[0], POLICYDB_MAGIC);
1157 if (len != strlen(POLICYDB_STRING)) {
1158 printk(KERN_ERR "security: policydb string length %d does not "
1159 "match expected length %Zu\n",
1160 len, strlen(POLICYDB_STRING));
1163 buf = next_entry(fp, len);
1165 printk(KERN_ERR "security: truncated policydb string identifier\n");
1168 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1169 if (!policydb_str) {
1170 printk(KERN_ERR "security: unable to allocate memory for policydb "
1171 "string of length %d\n", len);
1175 memcpy(policydb_str, buf, len);
1176 policydb_str[len] = 0;
1177 if (strcmp(policydb_str, POLICYDB_STRING)) {
1178 printk(KERN_ERR "security: policydb string %s does not match "
1179 "my string %s\n", policydb_str, POLICYDB_STRING);
1180 kfree(policydb_str);
1183 /* Done with policydb_str. */
1184 kfree(policydb_str);
1185 policydb_str = NULL;
1187 /* Read the version, config, and table sizes. */
1188 buf = next_entry(fp, sizeof(u32)*4);
1191 for (i = 0; i < 4; i++)
1192 buf[i] = le32_to_cpu(buf[i]);
1194 r_policyvers = buf[0];
1195 if (r_policyvers < POLICYDB_VERSION_MIN ||
1196 r_policyvers > POLICYDB_VERSION_MAX) {
1197 printk(KERN_ERR "security: policydb version %d does not match "
1198 "my version range %d-%d\n",
1199 buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1203 if (buf[1] != config) {
1204 printk(KERN_ERR "security: policydb configuration (%s) does "
1205 "not match my configuration (%s)\n",
1207 mls_config(config));
1212 info = policydb_lookup_compat(r_policyvers);
1214 printk(KERN_ERR "security: unable to find policy compat info "
1215 "for version %d\n", r_policyvers);
1219 if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1220 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1221 "not match mine (%d,%d)\n", buf[2], buf[3],
1222 info->sym_num, info->ocon_num);
1226 rc = mls_read_nlevels(p, fp);
1230 for (i = 0; i < info->sym_num; i++) {
1231 buf = next_entry(fp, sizeof(u32)*2);
1236 nprim = le32_to_cpu(buf[0]);
1237 nel = le32_to_cpu(buf[1]);
1238 for (j = 0; j < nel; j++) {
1239 rc = read_f[i](p, p->symtab[i].table, fp);
1244 p->symtab[i].nprim = nprim;
1247 rc = avtab_read(&p->te_avtab, fp, config);
1251 if (r_policyvers >= POLICYDB_VERSION_BOOL) {
1252 rc = cond_read_list(p, fp);
1257 buf = next_entry(fp, sizeof(u32));
1262 nel = le32_to_cpu(buf[0]);
1264 for (i = 0; i < nel; i++) {
1265 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1270 memset(tr, 0, sizeof(*tr));
1276 buf = next_entry(fp, sizeof(u32)*3);
1281 tr->role = le32_to_cpu(buf[0]);
1282 tr->type = le32_to_cpu(buf[1]);
1283 tr->new_role = le32_to_cpu(buf[2]);
1287 buf = next_entry(fp, sizeof(u32));
1292 nel = le32_to_cpu(buf[0]);
1294 for (i = 0; i < nel; i++) {
1295 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1300 memset(ra, 0, sizeof(*ra));
1306 buf = next_entry(fp, sizeof(u32)*2);
1311 ra->role = le32_to_cpu(buf[0]);
1312 ra->new_role = le32_to_cpu(buf[1]);
1316 rc = policydb_index_classes(p);
1320 rc = policydb_index_others(p);
1324 for (i = 0; i < info->ocon_num; i++) {
1325 buf = next_entry(fp, sizeof(u32));
1330 nel = le32_to_cpu(buf[0]);
1332 for (j = 0; j < nel; j++) {
1333 c = kmalloc(sizeof(*c), GFP_KERNEL);
1338 memset(c, 0, sizeof(*c));
1342 p->ocontexts[i] = c;
1348 buf = next_entry(fp, sizeof(u32));
1351 c->sid[0] = le32_to_cpu(buf[0]);
1352 rc = context_read_and_validate(&c->context[0], p, fp);
1358 buf = next_entry(fp, sizeof(u32));
1361 len = le32_to_cpu(buf[0]);
1362 buf = next_entry(fp, len);
1365 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1370 memcpy(c->u.name, buf, len);
1372 rc = context_read_and_validate(&c->context[0], p, fp);
1375 rc = context_read_and_validate(&c->context[1], p, fp);
1380 buf = next_entry(fp, sizeof(u32)*3);
1383 c->u.port.protocol = le32_to_cpu(buf[0]);
1384 c->u.port.low_port = le32_to_cpu(buf[1]);
1385 c->u.port.high_port = le32_to_cpu(buf[2]);
1386 rc = context_read_and_validate(&c->context[0], p, fp);
1391 buf = next_entry(fp, sizeof(u32)* 2);
1394 c->u.node.addr = le32_to_cpu(buf[0]);
1395 c->u.node.mask = le32_to_cpu(buf[1]);
1396 rc = context_read_and_validate(&c->context[0], p, fp);
1401 buf = next_entry(fp, sizeof(u32)*2);
1404 c->v.behavior = le32_to_cpu(buf[0]);
1405 if (c->v.behavior > SECURITY_FS_USE_NONE)
1407 len = le32_to_cpu(buf[1]);
1408 buf = next_entry(fp, len);
1411 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1416 memcpy(c->u.name, buf, len);
1418 rc = context_read_and_validate(&c->context[0], p, fp);
1425 buf = next_entry(fp, sizeof(u32) * 8);
1428 for (k = 0; k < 4; k++)
1429 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1430 for (k = 0; k < 4; k++)
1431 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1432 if (context_read_and_validate(&c->context[0], p, fp))
1440 buf = next_entry(fp, sizeof(u32));
1445 nel = le32_to_cpu(buf[0]);
1448 for (i = 0; i < nel; i++) {
1449 buf = next_entry(fp, sizeof(u32));
1452 len = le32_to_cpu(buf[0]);
1453 buf = next_entry(fp, len);
1456 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1461 memset(newgenfs, 0, sizeof(*newgenfs));
1463 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1464 if (!newgenfs->fstype) {
1469 memcpy(newgenfs->fstype, buf, len);
1470 newgenfs->fstype[len] = 0;
1471 for (genfs_p = NULL, genfs = p->genfs; genfs;
1472 genfs_p = genfs, genfs = genfs->next) {
1473 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1474 printk(KERN_ERR "security: dup genfs "
1475 "fstype %s\n", newgenfs->fstype);
1476 kfree(newgenfs->fstype);
1480 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1483 newgenfs->next = genfs;
1485 genfs_p->next = newgenfs;
1487 p->genfs = newgenfs;
1488 buf = next_entry(fp, sizeof(u32));
1491 nel2 = le32_to_cpu(buf[0]);
1492 for (j = 0; j < nel2; j++) {
1493 buf = next_entry(fp, sizeof(u32));
1496 len = le32_to_cpu(buf[0]);
1497 buf = next_entry(fp, len);
1501 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1506 memset(newc, 0, sizeof(*newc));
1508 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1509 if (!newc->u.name) {
1513 memcpy(newc->u.name, buf, len);
1514 newc->u.name[len] = 0;
1515 buf = next_entry(fp, sizeof(u32));
1518 newc->v.sclass = le32_to_cpu(buf[0]);
1519 if (context_read_and_validate(&newc->context[0], p, fp))
1521 for (l = NULL, c = newgenfs->head; c;
1522 l = c, c = c->next) {
1523 if (!strcmp(newc->u.name, c->u.name) &&
1524 (!c->v.sclass || !newc->v.sclass ||
1525 newc->v.sclass == c->v.sclass)) {
1526 printk(KERN_ERR "security: dup genfs "
1528 newgenfs->fstype, c->u.name);
1531 len = strlen(newc->u.name);
1532 len2 = strlen(c->u.name);
1541 newgenfs->head = newc;
1545 rc = mls_read_trusted(p, fp);
1551 ocontext_destroy(newc,OCON_FSUSE);
1553 policydb_destroy(p);