Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / security / selinux / ss / policydb.c
1 /*
2  * Implementation of the policy database.
3  *
4  * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5  */
6
7 /*
8  * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9  *
10  *      Support for enhanced MLS infrastructure.
11  *
12  * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13  *
14  *      Added conditional policy language extensions
15  *
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.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include "security.h"
28
29 #include "policydb.h"
30 #include "conditional.h"
31 #include "mls.h"
32
33 #define _DEBUG_HASHES
34
35 #ifdef DEBUG_HASHES
36 static char *symtab_name[SYM_NUM] = {
37         "common prefixes",
38         "classes",
39         "roles",
40         "types",
41         "users",
42         "bools",
43         "levels",
44         "categories",
45 };
46 #endif
47
48 int selinux_mls_enabled = 0;
49
50 static unsigned int symtab_sizes[SYM_NUM] = {
51         2,
52         32,
53         16,
54         512,
55         128,
56         16,
57         16,
58         16,
59 };
60
61 struct policydb_compat_info {
62         int version;
63         int sym_num;
64         int ocon_num;
65 };
66
67 /* These need to be updated if SYM_NUM or OCON_NUM changes */
68 static struct policydb_compat_info policydb_compat[] = {
69         {
70                 .version        = POLICYDB_VERSION_BASE,
71                 .sym_num        = SYM_NUM - 3,
72                 .ocon_num       = OCON_NUM - 1,
73         },
74         {
75                 .version        = POLICYDB_VERSION_BOOL,
76                 .sym_num        = SYM_NUM - 2,
77                 .ocon_num       = OCON_NUM - 1,
78         },
79         {
80                 .version        = POLICYDB_VERSION_IPV6,
81                 .sym_num        = SYM_NUM - 2,
82                 .ocon_num       = OCON_NUM,
83         },
84         {
85                 .version        = POLICYDB_VERSION_NLCLASS,
86                 .sym_num        = SYM_NUM - 2,
87                 .ocon_num       = OCON_NUM,
88         },
89         {
90                 .version        = POLICYDB_VERSION_MLS,
91                 .sym_num        = SYM_NUM,
92                 .ocon_num       = OCON_NUM,
93         },
94         {
95                 .version        = POLICYDB_VERSION_AVTAB,
96                 .sym_num        = SYM_NUM,
97                 .ocon_num       = OCON_NUM,
98         },
99 };
100
101 static struct policydb_compat_info *policydb_lookup_compat(int version)
102 {
103         int i;
104         struct policydb_compat_info *info = NULL;
105
106         for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
107                 if (policydb_compat[i].version == version) {
108                         info = &policydb_compat[i];
109                         break;
110                 }
111         }
112         return info;
113 }
114
115 /*
116  * Initialize the role table.
117  */
118 static int roles_init(struct policydb *p)
119 {
120         char *key = NULL;
121         int rc;
122         struct role_datum *role;
123
124         role = kmalloc(sizeof(*role), GFP_KERNEL);
125         if (!role) {
126                 rc = -ENOMEM;
127                 goto out;
128         }
129         memset(role, 0, sizeof(*role));
130         role->value = ++p->p_roles.nprim;
131         if (role->value != OBJECT_R_VAL) {
132                 rc = -EINVAL;
133                 goto out_free_role;
134         }
135         key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
136         if (!key) {
137                 rc = -ENOMEM;
138                 goto out_free_role;
139         }
140         strcpy(key, OBJECT_R);
141         rc = hashtab_insert(p->p_roles.table, key, role);
142         if (rc)
143                 goto out_free_key;
144 out:
145         return rc;
146
147 out_free_key:
148         kfree(key);
149 out_free_role:
150         kfree(role);
151         goto out;
152 }
153
154 /*
155  * Initialize a policy database structure.
156  */
157 static int policydb_init(struct policydb *p)
158 {
159         int i, rc;
160
161         memset(p, 0, sizeof(*p));
162
163         for (i = 0; i < SYM_NUM; i++) {
164                 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
165                 if (rc)
166                         goto out_free_symtab;
167         }
168
169         rc = avtab_init(&p->te_avtab);
170         if (rc)
171                 goto out_free_symtab;
172
173         rc = roles_init(p);
174         if (rc)
175                 goto out_free_avtab;
176
177         rc = cond_policydb_init(p);
178         if (rc)
179                 goto out_free_avtab;
180
181 out:
182         return rc;
183
184 out_free_avtab:
185         avtab_destroy(&p->te_avtab);
186
187 out_free_symtab:
188         for (i = 0; i < SYM_NUM; i++)
189                 hashtab_destroy(p->symtab[i].table);
190         goto out;
191 }
192
193 /*
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.
201  */
202
203 static int common_index(void *key, void *datum, void *datap)
204 {
205         struct policydb *p;
206         struct common_datum *comdatum;
207
208         comdatum = datum;
209         p = datap;
210         if (!comdatum->value || comdatum->value > p->p_commons.nprim)
211                 return -EINVAL;
212         p->p_common_val_to_name[comdatum->value - 1] = key;
213         return 0;
214 }
215
216 static int class_index(void *key, void *datum, void *datap)
217 {
218         struct policydb *p;
219         struct class_datum *cladatum;
220
221         cladatum = datum;
222         p = datap;
223         if (!cladatum->value || cladatum->value > p->p_classes.nprim)
224                 return -EINVAL;
225         p->p_class_val_to_name[cladatum->value - 1] = key;
226         p->class_val_to_struct[cladatum->value - 1] = cladatum;
227         return 0;
228 }
229
230 static int role_index(void *key, void *datum, void *datap)
231 {
232         struct policydb *p;
233         struct role_datum *role;
234
235         role = datum;
236         p = datap;
237         if (!role->value || role->value > p->p_roles.nprim)
238                 return -EINVAL;
239         p->p_role_val_to_name[role->value - 1] = key;
240         p->role_val_to_struct[role->value - 1] = role;
241         return 0;
242 }
243
244 static int type_index(void *key, void *datum, void *datap)
245 {
246         struct policydb *p;
247         struct type_datum *typdatum;
248
249         typdatum = datum;
250         p = datap;
251
252         if (typdatum->primary) {
253                 if (!typdatum->value || typdatum->value > p->p_types.nprim)
254                         return -EINVAL;
255                 p->p_type_val_to_name[typdatum->value - 1] = key;
256         }
257
258         return 0;
259 }
260
261 static int user_index(void *key, void *datum, void *datap)
262 {
263         struct policydb *p;
264         struct user_datum *usrdatum;
265
266         usrdatum = datum;
267         p = datap;
268         if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
269                 return -EINVAL;
270         p->p_user_val_to_name[usrdatum->value - 1] = key;
271         p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
272         return 0;
273 }
274
275 static int sens_index(void *key, void *datum, void *datap)
276 {
277         struct policydb *p;
278         struct level_datum *levdatum;
279
280         levdatum = datum;
281         p = datap;
282
283         if (!levdatum->isalias) {
284                 if (!levdatum->level->sens ||
285                     levdatum->level->sens > p->p_levels.nprim)
286                         return -EINVAL;
287                 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
288         }
289
290         return 0;
291 }
292
293 static int cat_index(void *key, void *datum, void *datap)
294 {
295         struct policydb *p;
296         struct cat_datum *catdatum;
297
298         catdatum = datum;
299         p = datap;
300
301         if (!catdatum->isalias) {
302                 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
303                         return -EINVAL;
304                 p->p_cat_val_to_name[catdatum->value - 1] = key;
305         }
306
307         return 0;
308 }
309
310 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
311 {
312         common_index,
313         class_index,
314         role_index,
315         type_index,
316         user_index,
317         cond_index_bool,
318         sens_index,
319         cat_index,
320 };
321
322 /*
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.
326  *
327  * Caller must clean up upon failure.
328  */
329 static int policydb_index_classes(struct policydb *p)
330 {
331         int rc;
332
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) {
336                 rc = -ENOMEM;
337                 goto out;
338         }
339
340         rc = hashtab_map(p->p_commons.table, common_index, p);
341         if (rc)
342                 goto out;
343
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) {
347                 rc = -ENOMEM;
348                 goto out;
349         }
350
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) {
354                 rc = -ENOMEM;
355                 goto out;
356         }
357
358         rc = hashtab_map(p->p_classes.table, class_index, p);
359 out:
360         return rc;
361 }
362
363 #ifdef DEBUG_HASHES
364 static void symtab_hash_eval(struct symtab *s)
365 {
366         int i;
367
368         for (i = 0; i < SYM_NUM; i++) {
369                 struct hashtab *h = s[i].table;
370                 struct hashtab_info info;
371
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);
376         }
377 }
378 #endif
379
380 /*
381  * Define the other val_to_name and val_to_struct arrays
382  * in a policy database structure.
383  *
384  * Caller must clean up on failure.
385  */
386 static int policydb_index_others(struct policydb *p)
387 {
388         int i, rc = 0;
389
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,
394                        p->p_cats.nprim);
395         printk("\n");
396
397         printk(KERN_INFO "security:  %d classes, %d rules\n",
398                p->p_classes.nprim, p->te_avtab.nel);
399
400 #ifdef DEBUG_HASHES
401         avtab_hash_eval(&p->te_avtab, "rules");
402         symtab_hash_eval(p->symtab);
403 #endif
404
405         p->role_val_to_struct =
406                 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
407                         GFP_KERNEL);
408         if (!p->role_val_to_struct) {
409                 rc = -ENOMEM;
410                 goto out;
411         }
412
413         p->user_val_to_struct =
414                 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
415                         GFP_KERNEL);
416         if (!p->user_val_to_struct) {
417                 rc = -ENOMEM;
418                 goto out;
419         }
420
421         if (cond_init_bool_indexes(p)) {
422                 rc = -ENOMEM;
423                 goto out;
424         }
425
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]) {
430                         rc = -ENOMEM;
431                         goto out;
432                 }
433                 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
434                 if (rc)
435                         goto out;
436         }
437
438 out:
439         return rc;
440 }
441
442 /*
443  * The following *_destroy functions are used to
444  * free any memory allocated for each kind of
445  * symbol data in the policy database.
446  */
447
448 static int perm_destroy(void *key, void *datum, void *p)
449 {
450         kfree(key);
451         kfree(datum);
452         return 0;
453 }
454
455 static int common_destroy(void *key, void *datum, void *p)
456 {
457         struct common_datum *comdatum;
458
459         kfree(key);
460         comdatum = datum;
461         hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
462         hashtab_destroy(comdatum->permissions.table);
463         kfree(datum);
464         return 0;
465 }
466
467 static int class_destroy(void *key, void *datum, void *p)
468 {
469         struct class_datum *cladatum;
470         struct constraint_node *constraint, *ctemp;
471         struct constraint_expr *e, *etmp;
472
473         kfree(key);
474         cladatum = datum;
475         hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
476         hashtab_destroy(cladatum->permissions.table);
477         constraint = cladatum->constraints;
478         while (constraint) {
479                 e = constraint->expr;
480                 while (e) {
481                         ebitmap_destroy(&e->names);
482                         etmp = e;
483                         e = e->next;
484                         kfree(etmp);
485                 }
486                 ctemp = constraint;
487                 constraint = constraint->next;
488                 kfree(ctemp);
489         }
490
491         constraint = cladatum->validatetrans;
492         while (constraint) {
493                 e = constraint->expr;
494                 while (e) {
495                         ebitmap_destroy(&e->names);
496                         etmp = e;
497                         e = e->next;
498                         kfree(etmp);
499                 }
500                 ctemp = constraint;
501                 constraint = constraint->next;
502                 kfree(ctemp);
503         }
504
505         kfree(cladatum->comkey);
506         kfree(datum);
507         return 0;
508 }
509
510 static int role_destroy(void *key, void *datum, void *p)
511 {
512         struct role_datum *role;
513
514         kfree(key);
515         role = datum;
516         ebitmap_destroy(&role->dominates);
517         ebitmap_destroy(&role->types);
518         kfree(datum);
519         return 0;
520 }
521
522 static int type_destroy(void *key, void *datum, void *p)
523 {
524         kfree(key);
525         kfree(datum);
526         return 0;
527 }
528
529 static int user_destroy(void *key, void *datum, void *p)
530 {
531         struct user_datum *usrdatum;
532
533         kfree(key);
534         usrdatum = datum;
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);
539         kfree(datum);
540         return 0;
541 }
542
543 static int sens_destroy(void *key, void *datum, void *p)
544 {
545         struct level_datum *levdatum;
546
547         kfree(key);
548         levdatum = datum;
549         ebitmap_destroy(&levdatum->level->cat);
550         kfree(levdatum->level);
551         kfree(datum);
552         return 0;
553 }
554
555 static int cat_destroy(void *key, void *datum, void *p)
556 {
557         kfree(key);
558         kfree(datum);
559         return 0;
560 }
561
562 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
563 {
564         common_destroy,
565         class_destroy,
566         role_destroy,
567         type_destroy,
568         user_destroy,
569         cond_destroy_bool,
570         sens_destroy,
571         cat_destroy,
572 };
573
574 static void ocontext_destroy(struct ocontext *c, int i)
575 {
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)
580                 kfree(c->u.name);
581         kfree(c);
582 }
583
584 /*
585  * Free any memory allocated by a policy database structure.
586  */
587 void policydb_destroy(struct policydb *p)
588 {
589         struct ocontext *c, *ctmp;
590         struct genfs *g, *gtmp;
591         int i;
592         struct role_allow *ra, *lra = NULL;
593         struct role_trans *tr, *ltr = NULL;
594         struct range_trans *rt, *lrt = NULL;
595
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);
599         }
600
601         for (i = 0; i < SYM_NUM; i++)
602                 kfree(p->sym_val_to_name[i]);
603
604         kfree(p->class_val_to_struct);
605         kfree(p->role_val_to_struct);
606         kfree(p->user_val_to_struct);
607
608         avtab_destroy(&p->te_avtab);
609
610         for (i = 0; i < OCON_NUM; i++) {
611                 c = p->ocontexts[i];
612                 while (c) {
613                         ctmp = c;
614                         c = c->next;
615                         ocontext_destroy(ctmp,i);
616                 }
617         }
618
619         g = p->genfs;
620         while (g) {
621                 kfree(g->fstype);
622                 c = g->head;
623                 while (c) {
624                         ctmp = c;
625                         c = c->next;
626                         ocontext_destroy(ctmp,OCON_FSUSE);
627                 }
628                 gtmp = g;
629                 g = g->next;
630                 kfree(gtmp);
631         }
632
633         cond_policydb_destroy(p);
634
635         for (tr = p->role_tr; tr; tr = tr->next) {
636                 if (ltr) kfree(ltr);
637                 ltr = tr;
638         }
639         if (ltr) kfree(ltr);
640
641         for (ra = p->role_allow; ra; ra = ra -> next) {
642                 if (lra) kfree(lra);
643                 lra = ra;
644         }
645         if (lra) kfree(lra);
646
647         for (rt = p->range_tr; rt; rt = rt -> next) {
648                 if (lrt) kfree(lrt);
649                 lrt = rt;
650         }
651         if (lrt) kfree(lrt);
652
653         for (i = 0; i < p->p_types.nprim; i++)
654                 ebitmap_destroy(&p->type_attr_map[i]);
655         kfree(p->type_attr_map);
656
657         return;
658 }
659
660 /*
661  * Load the initial SIDs specified in a policy database
662  * structure into a SID table.
663  */
664 int policydb_load_isids(struct policydb *p, struct sidtab *s)
665 {
666         struct ocontext *head, *c;
667         int rc;
668
669         rc = sidtab_init(s);
670         if (rc) {
671                 printk(KERN_ERR "security:  out of memory on SID table init\n");
672                 goto out;
673         }
674
675         head = p->ocontexts[OCON_ISID];
676         for (c = head; c; c = c->next) {
677                 if (!c->context[0].user) {
678                         printk(KERN_ERR "security:  SID %s was never "
679                                "defined.\n", c->u.name);
680                         rc = -EINVAL;
681                         goto out;
682                 }
683                 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
684                         printk(KERN_ERR "security:  unable to load initial "
685                                "SID %s.\n", c->u.name);
686                         rc = -EINVAL;
687                         goto out;
688                 }
689         }
690 out:
691         return rc;
692 }
693
694 /*
695  * Return 1 if the fields in the security context
696  * structure `c' are valid.  Return 0 otherwise.
697  */
698 int policydb_context_isvalid(struct policydb *p, struct context *c)
699 {
700         struct role_datum *role;
701         struct user_datum *usrdatum;
702
703         if (!c->role || c->role > p->p_roles.nprim)
704                 return 0;
705
706         if (!c->user || c->user > p->p_users.nprim)
707                 return 0;
708
709         if (!c->type || c->type > p->p_types.nprim)
710                 return 0;
711
712         if (c->role != OBJECT_R_VAL) {
713                 /*
714                  * Role must be authorized for the type.
715                  */
716                 role = p->role_val_to_struct[c->role - 1];
717                 if (!ebitmap_get_bit(&role->types,
718                                      c->type - 1))
719                         /* role may not be associated with type */
720                         return 0;
721
722                 /*
723                  * User must be authorized for the role.
724                  */
725                 usrdatum = p->user_val_to_struct[c->user - 1];
726                 if (!usrdatum)
727                         return 0;
728
729                 if (!ebitmap_get_bit(&usrdatum->roles,
730                                      c->role - 1))
731                         /* user may not be associated with role */
732                         return 0;
733         }
734
735         if (!mls_context_isvalid(p, c))
736                 return 0;
737
738         return 1;
739 }
740
741 /*
742  * Read a MLS range structure from a policydb binary
743  * representation file.
744  */
745 static int mls_read_range_helper(struct mls_range *r, void *fp)
746 {
747         __le32 buf[2];
748         u32 items;
749         int rc;
750
751         rc = next_entry(buf, fp, sizeof(u32));
752         if (rc < 0)
753                 goto out;
754
755         items = le32_to_cpu(buf[0]);
756         if (items > ARRAY_SIZE(buf)) {
757                 printk(KERN_ERR "security: mls:  range overflow\n");
758                 rc = -EINVAL;
759                 goto out;
760         }
761         rc = next_entry(buf, fp, sizeof(u32) * items);
762         if (rc < 0) {
763                 printk(KERN_ERR "security: mls:  truncated range\n");
764                 goto out;
765         }
766         r->level[0].sens = le32_to_cpu(buf[0]);
767         if (items > 1)
768                 r->level[1].sens = le32_to_cpu(buf[1]);
769         else
770                 r->level[1].sens = r->level[0].sens;
771
772         rc = ebitmap_read(&r->level[0].cat, fp);
773         if (rc) {
774                 printk(KERN_ERR "security: mls:  error reading low "
775                        "categories\n");
776                 goto out;
777         }
778         if (items > 1) {
779                 rc = ebitmap_read(&r->level[1].cat, fp);
780                 if (rc) {
781                         printk(KERN_ERR "security: mls:  error reading high "
782                                "categories\n");
783                         goto bad_high;
784                 }
785         } else {
786                 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
787                 if (rc) {
788                         printk(KERN_ERR "security: mls:  out of memory\n");
789                         goto bad_high;
790                 }
791         }
792
793         rc = 0;
794 out:
795         return rc;
796 bad_high:
797         ebitmap_destroy(&r->level[0].cat);
798         goto out;
799 }
800
801 /*
802  * Read and validate a security context structure
803  * from a policydb binary representation file.
804  */
805 static int context_read_and_validate(struct context *c,
806                                      struct policydb *p,
807                                      void *fp)
808 {
809         __le32 buf[3];
810         int rc;
811
812         rc = next_entry(buf, fp, sizeof buf);
813         if (rc < 0) {
814                 printk(KERN_ERR "security: context truncated\n");
815                 goto out;
816         }
817         c->user = le32_to_cpu(buf[0]);
818         c->role = le32_to_cpu(buf[1]);
819         c->type = le32_to_cpu(buf[2]);
820         if (p->policyvers >= POLICYDB_VERSION_MLS) {
821                 if (mls_read_range_helper(&c->range, fp)) {
822                         printk(KERN_ERR "security: error reading MLS range of "
823                                "context\n");
824                         rc = -EINVAL;
825                         goto out;
826                 }
827         }
828
829         if (!policydb_context_isvalid(p, c)) {
830                 printk(KERN_ERR "security:  invalid security context\n");
831                 context_destroy(c);
832                 rc = -EINVAL;
833         }
834 out:
835         return rc;
836 }
837
838 /*
839  * The following *_read functions are used to
840  * read the symbol data from a policy database
841  * binary representation file.
842  */
843
844 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
845 {
846         char *key = NULL;
847         struct perm_datum *perdatum;
848         int rc;
849         __le32 buf[2];
850         u32 len;
851
852         perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
853         if (!perdatum) {
854                 rc = -ENOMEM;
855                 goto out;
856         }
857         memset(perdatum, 0, sizeof(*perdatum));
858
859         rc = next_entry(buf, fp, sizeof buf);
860         if (rc < 0)
861                 goto bad;
862
863         len = le32_to_cpu(buf[0]);
864         perdatum->value = le32_to_cpu(buf[1]);
865
866         key = kmalloc(len + 1,GFP_KERNEL);
867         if (!key) {
868                 rc = -ENOMEM;
869                 goto bad;
870         }
871         rc = next_entry(key, fp, len);
872         if (rc < 0)
873                 goto bad;
874         key[len] = 0;
875
876         rc = hashtab_insert(h, key, perdatum);
877         if (rc)
878                 goto bad;
879 out:
880         return rc;
881 bad:
882         perm_destroy(key, perdatum, NULL);
883         goto out;
884 }
885
886 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
887 {
888         char *key = NULL;
889         struct common_datum *comdatum;
890         __le32 buf[4];
891         u32 len, nel;
892         int i, rc;
893
894         comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
895         if (!comdatum) {
896                 rc = -ENOMEM;
897                 goto out;
898         }
899         memset(comdatum, 0, sizeof(*comdatum));
900
901         rc = next_entry(buf, fp, sizeof buf);
902         if (rc < 0)
903                 goto bad;
904
905         len = le32_to_cpu(buf[0]);
906         comdatum->value = le32_to_cpu(buf[1]);
907
908         rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
909         if (rc)
910                 goto bad;
911         comdatum->permissions.nprim = le32_to_cpu(buf[2]);
912         nel = le32_to_cpu(buf[3]);
913
914         key = kmalloc(len + 1,GFP_KERNEL);
915         if (!key) {
916                 rc = -ENOMEM;
917                 goto bad;
918         }
919         rc = next_entry(key, fp, len);
920         if (rc < 0)
921                 goto bad;
922         key[len] = 0;
923
924         for (i = 0; i < nel; i++) {
925                 rc = perm_read(p, comdatum->permissions.table, fp);
926                 if (rc)
927                         goto bad;
928         }
929
930         rc = hashtab_insert(h, key, comdatum);
931         if (rc)
932                 goto bad;
933 out:
934         return rc;
935 bad:
936         common_destroy(key, comdatum, NULL);
937         goto out;
938 }
939
940 static int read_cons_helper(struct constraint_node **nodep, int ncons,
941                             int allowxtarget, void *fp)
942 {
943         struct constraint_node *c, *lc;
944         struct constraint_expr *e, *le;
945         __le32 buf[3];
946         u32 nexpr;
947         int rc, i, j, depth;
948
949         lc = NULL;
950         for (i = 0; i < ncons; i++) {
951                 c = kmalloc(sizeof(*c), GFP_KERNEL);
952                 if (!c)
953                         return -ENOMEM;
954                 memset(c, 0, sizeof(*c));
955
956                 if (lc) {
957                         lc->next = c;
958                 } else {
959                         *nodep = c;
960                 }
961
962                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
963                 if (rc < 0)
964                         return rc;
965                 c->permissions = le32_to_cpu(buf[0]);
966                 nexpr = le32_to_cpu(buf[1]);
967                 le = NULL;
968                 depth = -1;
969                 for (j = 0; j < nexpr; j++) {
970                         e = kmalloc(sizeof(*e), GFP_KERNEL);
971                         if (!e)
972                                 return -ENOMEM;
973                         memset(e, 0, sizeof(*e));
974
975                         if (le) {
976                                 le->next = e;
977                         } else {
978                                 c->expr = e;
979                         }
980
981                         rc = next_entry(buf, fp, (sizeof(u32) * 3));
982                         if (rc < 0)
983                                 return rc;
984                         e->expr_type = le32_to_cpu(buf[0]);
985                         e->attr = le32_to_cpu(buf[1]);
986                         e->op = le32_to_cpu(buf[2]);
987
988                         switch (e->expr_type) {
989                         case CEXPR_NOT:
990                                 if (depth < 0)
991                                         return -EINVAL;
992                                 break;
993                         case CEXPR_AND:
994                         case CEXPR_OR:
995                                 if (depth < 1)
996                                         return -EINVAL;
997                                 depth--;
998                                 break;
999                         case CEXPR_ATTR:
1000                                 if (depth == (CEXPR_MAXDEPTH - 1))
1001                                         return -EINVAL;
1002                                 depth++;
1003                                 break;
1004                         case CEXPR_NAMES:
1005                                 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1006                                         return -EINVAL;
1007                                 if (depth == (CEXPR_MAXDEPTH - 1))
1008                                         return -EINVAL;
1009                                 depth++;
1010                                 if (ebitmap_read(&e->names, fp))
1011                                         return -EINVAL;
1012                                 break;
1013                         default:
1014                                 return -EINVAL;
1015                         }
1016                         le = e;
1017                 }
1018                 if (depth != 0)
1019                         return -EINVAL;
1020                 lc = c;
1021         }
1022
1023         return 0;
1024 }
1025
1026 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1027 {
1028         char *key = NULL;
1029         struct class_datum *cladatum;
1030         __le32 buf[6];
1031         u32 len, len2, ncons, nel;
1032         int i, rc;
1033
1034         cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1035         if (!cladatum) {
1036                 rc = -ENOMEM;
1037                 goto out;
1038         }
1039         memset(cladatum, 0, sizeof(*cladatum));
1040
1041         rc = next_entry(buf, fp, sizeof(u32)*6);
1042         if (rc < 0)
1043                 goto bad;
1044
1045         len = le32_to_cpu(buf[0]);
1046         len2 = le32_to_cpu(buf[1]);
1047         cladatum->value = le32_to_cpu(buf[2]);
1048
1049         rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1050         if (rc)
1051                 goto bad;
1052         cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1053         nel = le32_to_cpu(buf[4]);
1054
1055         ncons = le32_to_cpu(buf[5]);
1056
1057         key = kmalloc(len + 1,GFP_KERNEL);
1058         if (!key) {
1059                 rc = -ENOMEM;
1060                 goto bad;
1061         }
1062         rc = next_entry(key, fp, len);
1063         if (rc < 0)
1064                 goto bad;
1065         key[len] = 0;
1066
1067         if (len2) {
1068                 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1069                 if (!cladatum->comkey) {
1070                         rc = -ENOMEM;
1071                         goto bad;
1072                 }
1073                 rc = next_entry(cladatum->comkey, fp, len2);
1074                 if (rc < 0)
1075                         goto bad;
1076                 cladatum->comkey[len2] = 0;
1077
1078                 cladatum->comdatum = hashtab_search(p->p_commons.table,
1079                                                     cladatum->comkey);
1080                 if (!cladatum->comdatum) {
1081                         printk(KERN_ERR "security:  unknown common %s\n",
1082                                cladatum->comkey);
1083                         rc = -EINVAL;
1084                         goto bad;
1085                 }
1086         }
1087         for (i = 0; i < nel; i++) {
1088                 rc = perm_read(p, cladatum->permissions.table, fp);
1089                 if (rc)
1090                         goto bad;
1091         }
1092
1093         rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1094         if (rc)
1095                 goto bad;
1096
1097         if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1098                 /* grab the validatetrans rules */
1099                 rc = next_entry(buf, fp, sizeof(u32));
1100                 if (rc < 0)
1101                         goto bad;
1102                 ncons = le32_to_cpu(buf[0]);
1103                 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1104                 if (rc)
1105                         goto bad;
1106         }
1107
1108         rc = hashtab_insert(h, key, cladatum);
1109         if (rc)
1110                 goto bad;
1111
1112         rc = 0;
1113 out:
1114         return rc;
1115 bad:
1116         class_destroy(key, cladatum, NULL);
1117         goto out;
1118 }
1119
1120 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1121 {
1122         char *key = NULL;
1123         struct role_datum *role;
1124         int rc;
1125         __le32 buf[2];
1126         u32 len;
1127
1128         role = kmalloc(sizeof(*role), GFP_KERNEL);
1129         if (!role) {
1130                 rc = -ENOMEM;
1131                 goto out;
1132         }
1133         memset(role, 0, sizeof(*role));
1134
1135         rc = next_entry(buf, fp, sizeof buf);
1136         if (rc < 0)
1137                 goto bad;
1138
1139         len = le32_to_cpu(buf[0]);
1140         role->value = le32_to_cpu(buf[1]);
1141
1142         key = kmalloc(len + 1,GFP_KERNEL);
1143         if (!key) {
1144                 rc = -ENOMEM;
1145                 goto bad;
1146         }
1147         rc = next_entry(key, fp, len);
1148         if (rc < 0)
1149                 goto bad;
1150         key[len] = 0;
1151
1152         rc = ebitmap_read(&role->dominates, fp);
1153         if (rc)
1154                 goto bad;
1155
1156         rc = ebitmap_read(&role->types, fp);
1157         if (rc)
1158                 goto bad;
1159
1160         if (strcmp(key, OBJECT_R) == 0) {
1161                 if (role->value != OBJECT_R_VAL) {
1162                         printk(KERN_ERR "Role %s has wrong value %d\n",
1163                                OBJECT_R, role->value);
1164                         rc = -EINVAL;
1165                         goto bad;
1166                 }
1167                 rc = 0;
1168                 goto bad;
1169         }
1170
1171         rc = hashtab_insert(h, key, role);
1172         if (rc)
1173                 goto bad;
1174 out:
1175         return rc;
1176 bad:
1177         role_destroy(key, role, NULL);
1178         goto out;
1179 }
1180
1181 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1182 {
1183         char *key = NULL;
1184         struct type_datum *typdatum;
1185         int rc;
1186         __le32 buf[3];
1187         u32 len;
1188
1189         typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1190         if (!typdatum) {
1191                 rc = -ENOMEM;
1192                 return rc;
1193         }
1194         memset(typdatum, 0, sizeof(*typdatum));
1195
1196         rc = next_entry(buf, fp, sizeof buf);
1197         if (rc < 0)
1198                 goto bad;
1199
1200         len = le32_to_cpu(buf[0]);
1201         typdatum->value = le32_to_cpu(buf[1]);
1202         typdatum->primary = le32_to_cpu(buf[2]);
1203
1204         key = kmalloc(len + 1,GFP_KERNEL);
1205         if (!key) {
1206                 rc = -ENOMEM;
1207                 goto bad;
1208         }
1209         rc = next_entry(key, fp, len);
1210         if (rc < 0)
1211                 goto bad;
1212         key[len] = 0;
1213
1214         rc = hashtab_insert(h, key, typdatum);
1215         if (rc)
1216                 goto bad;
1217 out:
1218         return rc;
1219 bad:
1220         type_destroy(key, typdatum, NULL);
1221         goto out;
1222 }
1223
1224
1225 /*
1226  * Read a MLS level structure from a policydb binary
1227  * representation file.
1228  */
1229 static int mls_read_level(struct mls_level *lp, void *fp)
1230 {
1231         __le32 buf[1];
1232         int rc;
1233
1234         memset(lp, 0, sizeof(*lp));
1235
1236         rc = next_entry(buf, fp, sizeof buf);
1237         if (rc < 0) {
1238                 printk(KERN_ERR "security: mls: truncated level\n");
1239                 goto bad;
1240         }
1241         lp->sens = le32_to_cpu(buf[0]);
1242
1243         if (ebitmap_read(&lp->cat, fp)) {
1244                 printk(KERN_ERR "security: mls:  error reading level "
1245                        "categories\n");
1246                 goto bad;
1247         }
1248         return 0;
1249
1250 bad:
1251         return -EINVAL;
1252 }
1253
1254 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1255 {
1256         char *key = NULL;
1257         struct user_datum *usrdatum;
1258         int rc;
1259         __le32 buf[2];
1260         u32 len;
1261
1262         usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1263         if (!usrdatum) {
1264                 rc = -ENOMEM;
1265                 goto out;
1266         }
1267         memset(usrdatum, 0, sizeof(*usrdatum));
1268
1269         rc = next_entry(buf, fp, sizeof buf);
1270         if (rc < 0)
1271                 goto bad;
1272
1273         len = le32_to_cpu(buf[0]);
1274         usrdatum->value = le32_to_cpu(buf[1]);
1275
1276         key = kmalloc(len + 1,GFP_KERNEL);
1277         if (!key) {
1278                 rc = -ENOMEM;
1279                 goto bad;
1280         }
1281         rc = next_entry(key, fp, len);
1282         if (rc < 0)
1283                 goto bad;
1284         key[len] = 0;
1285
1286         rc = ebitmap_read(&usrdatum->roles, fp);
1287         if (rc)
1288                 goto bad;
1289
1290         if (p->policyvers >= POLICYDB_VERSION_MLS) {
1291                 rc = mls_read_range_helper(&usrdatum->range, fp);
1292                 if (rc)
1293                         goto bad;
1294                 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1295                 if (rc)
1296                         goto bad;
1297         }
1298
1299         rc = hashtab_insert(h, key, usrdatum);
1300         if (rc)
1301                 goto bad;
1302 out:
1303         return rc;
1304 bad:
1305         user_destroy(key, usrdatum, NULL);
1306         goto out;
1307 }
1308
1309 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1310 {
1311         char *key = NULL;
1312         struct level_datum *levdatum;
1313         int rc;
1314         __le32 buf[2];
1315         u32 len;
1316
1317         levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1318         if (!levdatum) {
1319                 rc = -ENOMEM;
1320                 goto out;
1321         }
1322         memset(levdatum, 0, sizeof(*levdatum));
1323
1324         rc = next_entry(buf, fp, sizeof buf);
1325         if (rc < 0)
1326                 goto bad;
1327
1328         len = le32_to_cpu(buf[0]);
1329         levdatum->isalias = le32_to_cpu(buf[1]);
1330
1331         key = kmalloc(len + 1,GFP_ATOMIC);
1332         if (!key) {
1333                 rc = -ENOMEM;
1334                 goto bad;
1335         }
1336         rc = next_entry(key, fp, len);
1337         if (rc < 0)
1338                 goto bad;
1339         key[len] = 0;
1340
1341         levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1342         if (!levdatum->level) {
1343                 rc = -ENOMEM;
1344                 goto bad;
1345         }
1346         if (mls_read_level(levdatum->level, fp)) {
1347                 rc = -EINVAL;
1348                 goto bad;
1349         }
1350
1351         rc = hashtab_insert(h, key, levdatum);
1352         if (rc)
1353                 goto bad;
1354 out:
1355         return rc;
1356 bad:
1357         sens_destroy(key, levdatum, NULL);
1358         goto out;
1359 }
1360
1361 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1362 {
1363         char *key = NULL;
1364         struct cat_datum *catdatum;
1365         int rc;
1366         __le32 buf[3];
1367         u32 len;
1368
1369         catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1370         if (!catdatum) {
1371                 rc = -ENOMEM;
1372                 goto out;
1373         }
1374         memset(catdatum, 0, sizeof(*catdatum));
1375
1376         rc = next_entry(buf, fp, sizeof buf);
1377         if (rc < 0)
1378                 goto bad;
1379
1380         len = le32_to_cpu(buf[0]);
1381         catdatum->value = le32_to_cpu(buf[1]);
1382         catdatum->isalias = le32_to_cpu(buf[2]);
1383
1384         key = kmalloc(len + 1,GFP_ATOMIC);
1385         if (!key) {
1386                 rc = -ENOMEM;
1387                 goto bad;
1388         }
1389         rc = next_entry(key, fp, len);
1390         if (rc < 0)
1391                 goto bad;
1392         key[len] = 0;
1393
1394         rc = hashtab_insert(h, key, catdatum);
1395         if (rc)
1396                 goto bad;
1397 out:
1398         return rc;
1399
1400 bad:
1401         cat_destroy(key, catdatum, NULL);
1402         goto out;
1403 }
1404
1405 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1406 {
1407         common_read,
1408         class_read,
1409         role_read,
1410         type_read,
1411         user_read,
1412         cond_read_bool,
1413         sens_read,
1414         cat_read,
1415 };
1416
1417 extern int ss_initialized;
1418
1419 /*
1420  * Read the configuration data from a policy database binary
1421  * representation file into a policy database structure.
1422  */
1423 int policydb_read(struct policydb *p, void *fp)
1424 {
1425         struct role_allow *ra, *lra;
1426         struct role_trans *tr, *ltr;
1427         struct ocontext *l, *c, *newc;
1428         struct genfs *genfs_p, *genfs, *newgenfs;
1429         int i, j, rc;
1430         __le32 buf[8];
1431         u32 len, len2, config, nprim, nel, nel2;
1432         char *policydb_str;
1433         struct policydb_compat_info *info;
1434         struct range_trans *rt, *lrt;
1435
1436         config = 0;
1437
1438         rc = policydb_init(p);
1439         if (rc)
1440                 goto out;
1441
1442         /* Read the magic number and string length. */
1443         rc = next_entry(buf, fp, sizeof(u32)* 2);
1444         if (rc < 0)
1445                 goto bad;
1446
1447         if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
1448                 printk(KERN_ERR "security:  policydb magic number 0x%x does "
1449                        "not match expected magic number 0x%x\n",
1450                        le32_to_cpu(buf[0]), POLICYDB_MAGIC);
1451                 goto bad;
1452         }
1453
1454         len = le32_to_cpu(buf[1]);
1455         if (len != strlen(POLICYDB_STRING)) {
1456                 printk(KERN_ERR "security:  policydb string length %d does not "
1457                        "match expected length %Zu\n",
1458                        len, strlen(POLICYDB_STRING));
1459                 goto bad;
1460         }
1461         policydb_str = kmalloc(len + 1,GFP_KERNEL);
1462         if (!policydb_str) {
1463                 printk(KERN_ERR "security:  unable to allocate memory for policydb "
1464                        "string of length %d\n", len);
1465                 rc = -ENOMEM;
1466                 goto bad;
1467         }
1468         rc = next_entry(policydb_str, fp, len);
1469         if (rc < 0) {
1470                 printk(KERN_ERR "security:  truncated policydb string identifier\n");
1471                 kfree(policydb_str);
1472                 goto bad;
1473         }
1474         policydb_str[len] = 0;
1475         if (strcmp(policydb_str, POLICYDB_STRING)) {
1476                 printk(KERN_ERR "security:  policydb string %s does not match "
1477                        "my string %s\n", policydb_str, POLICYDB_STRING);
1478                 kfree(policydb_str);
1479                 goto bad;
1480         }
1481         /* Done with policydb_str. */
1482         kfree(policydb_str);
1483         policydb_str = NULL;
1484
1485         /* Read the version, config, and table sizes. */
1486         rc = next_entry(buf, fp, sizeof(u32)*4);
1487         if (rc < 0)
1488                 goto bad;
1489
1490         p->policyvers = le32_to_cpu(buf[0]);
1491         if (p->policyvers < POLICYDB_VERSION_MIN ||
1492             p->policyvers > POLICYDB_VERSION_MAX) {
1493                 printk(KERN_ERR "security:  policydb version %d does not match "
1494                        "my version range %d-%d\n",
1495                        le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1496                 goto bad;
1497         }
1498
1499         if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
1500                 if (ss_initialized && !selinux_mls_enabled) {
1501                         printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1502                                "policies\n");
1503                         goto bad;
1504                 }
1505                 selinux_mls_enabled = 1;
1506                 config |= POLICYDB_CONFIG_MLS;
1507
1508                 if (p->policyvers < POLICYDB_VERSION_MLS) {
1509                         printk(KERN_ERR "security policydb version %d (MLS) "
1510                                "not backwards compatible\n", p->policyvers);
1511                         goto bad;
1512                 }
1513         } else {
1514                 if (ss_initialized && selinux_mls_enabled) {
1515                         printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1516                                "policies\n");
1517                         goto bad;
1518                 }
1519         }
1520
1521         info = policydb_lookup_compat(p->policyvers);
1522         if (!info) {
1523                 printk(KERN_ERR "security:  unable to find policy compat info "
1524                        "for version %d\n", p->policyvers);
1525                 goto bad;
1526         }
1527
1528         if (le32_to_cpu(buf[2]) != info->sym_num ||
1529                 le32_to_cpu(buf[3]) != info->ocon_num) {
1530                 printk(KERN_ERR "security:  policydb table sizes (%d,%d) do "
1531                        "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1532                         le32_to_cpu(buf[3]),
1533                        info->sym_num, info->ocon_num);
1534                 goto bad;
1535         }
1536
1537         for (i = 0; i < info->sym_num; i++) {
1538                 rc = next_entry(buf, fp, sizeof(u32)*2);
1539                 if (rc < 0)
1540                         goto bad;
1541                 nprim = le32_to_cpu(buf[0]);
1542                 nel = le32_to_cpu(buf[1]);
1543                 for (j = 0; j < nel; j++) {
1544                         rc = read_f[i](p, p->symtab[i].table, fp);
1545                         if (rc)
1546                                 goto bad;
1547                 }
1548
1549                 p->symtab[i].nprim = nprim;
1550         }
1551
1552         rc = avtab_read(&p->te_avtab, fp, p->policyvers);
1553         if (rc)
1554                 goto bad;
1555
1556         if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1557                 rc = cond_read_list(p, fp);
1558                 if (rc)
1559                         goto bad;
1560         }
1561
1562         rc = next_entry(buf, fp, sizeof(u32));
1563         if (rc < 0)
1564                 goto bad;
1565         nel = le32_to_cpu(buf[0]);
1566         ltr = NULL;
1567         for (i = 0; i < nel; i++) {
1568                 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1569                 if (!tr) {
1570                         rc = -ENOMEM;
1571                         goto bad;
1572                 }
1573                 memset(tr, 0, sizeof(*tr));
1574                 if (ltr) {
1575                         ltr->next = tr;
1576                 } else {
1577                         p->role_tr = tr;
1578                 }
1579                 rc = next_entry(buf, fp, sizeof(u32)*3);
1580                 if (rc < 0)
1581                         goto bad;
1582                 tr->role = le32_to_cpu(buf[0]);
1583                 tr->type = le32_to_cpu(buf[1]);
1584                 tr->new_role = le32_to_cpu(buf[2]);
1585                 ltr = tr;
1586         }
1587
1588         rc = next_entry(buf, fp, sizeof(u32));
1589         if (rc < 0)
1590                 goto bad;
1591         nel = le32_to_cpu(buf[0]);
1592         lra = NULL;
1593         for (i = 0; i < nel; i++) {
1594                 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1595                 if (!ra) {
1596                         rc = -ENOMEM;
1597                         goto bad;
1598                 }
1599                 memset(ra, 0, sizeof(*ra));
1600                 if (lra) {
1601                         lra->next = ra;
1602                 } else {
1603                         p->role_allow = ra;
1604                 }
1605                 rc = next_entry(buf, fp, sizeof(u32)*2);
1606                 if (rc < 0)
1607                         goto bad;
1608                 ra->role = le32_to_cpu(buf[0]);
1609                 ra->new_role = le32_to_cpu(buf[1]);
1610                 lra = ra;
1611         }
1612
1613         rc = policydb_index_classes(p);
1614         if (rc)
1615                 goto bad;
1616
1617         rc = policydb_index_others(p);
1618         if (rc)
1619                 goto bad;
1620
1621         for (i = 0; i < info->ocon_num; i++) {
1622                 rc = next_entry(buf, fp, sizeof(u32));
1623                 if (rc < 0)
1624                         goto bad;
1625                 nel = le32_to_cpu(buf[0]);
1626                 l = NULL;
1627                 for (j = 0; j < nel; j++) {
1628                         c = kmalloc(sizeof(*c), GFP_KERNEL);
1629                         if (!c) {
1630                                 rc = -ENOMEM;
1631                                 goto bad;
1632                         }
1633                         memset(c, 0, sizeof(*c));
1634                         if (l) {
1635                                 l->next = c;
1636                         } else {
1637                                 p->ocontexts[i] = c;
1638                         }
1639                         l = c;
1640                         rc = -EINVAL;
1641                         switch (i) {
1642                         case OCON_ISID:
1643                                 rc = next_entry(buf, fp, sizeof(u32));
1644                                 if (rc < 0)
1645                                         goto bad;
1646                                 c->sid[0] = le32_to_cpu(buf[0]);
1647                                 rc = context_read_and_validate(&c->context[0], p, fp);
1648                                 if (rc)
1649                                         goto bad;
1650                                 break;
1651                         case OCON_FS:
1652                         case OCON_NETIF:
1653                                 rc = next_entry(buf, fp, sizeof(u32));
1654                                 if (rc < 0)
1655                                         goto bad;
1656                                 len = le32_to_cpu(buf[0]);
1657                                 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1658                                 if (!c->u.name) {
1659                                         rc = -ENOMEM;
1660                                         goto bad;
1661                                 }
1662                                 rc = next_entry(c->u.name, fp, len);
1663                                 if (rc < 0)
1664                                         goto bad;
1665                                 c->u.name[len] = 0;
1666                                 rc = context_read_and_validate(&c->context[0], p, fp);
1667                                 if (rc)
1668                                         goto bad;
1669                                 rc = context_read_and_validate(&c->context[1], p, fp);
1670                                 if (rc)
1671                                         goto bad;
1672                                 break;
1673                         case OCON_PORT:
1674                                 rc = next_entry(buf, fp, sizeof(u32)*3);
1675                                 if (rc < 0)
1676                                         goto bad;
1677                                 c->u.port.protocol = le32_to_cpu(buf[0]);
1678                                 c->u.port.low_port = le32_to_cpu(buf[1]);
1679                                 c->u.port.high_port = le32_to_cpu(buf[2]);
1680                                 rc = context_read_and_validate(&c->context[0], p, fp);
1681                                 if (rc)
1682                                         goto bad;
1683                                 break;
1684                         case OCON_NODE:
1685                                 rc = next_entry(buf, fp, sizeof(u32)* 2);
1686                                 if (rc < 0)
1687                                         goto bad;
1688                                 c->u.node.addr = le32_to_cpu(buf[0]);
1689                                 c->u.node.mask = le32_to_cpu(buf[1]);
1690                                 rc = context_read_and_validate(&c->context[0], p, fp);
1691                                 if (rc)
1692                                         goto bad;
1693                                 break;
1694                         case OCON_FSUSE:
1695                                 rc = next_entry(buf, fp, sizeof(u32)*2);
1696                                 if (rc < 0)
1697                                         goto bad;
1698                                 c->v.behavior = le32_to_cpu(buf[0]);
1699                                 if (c->v.behavior > SECURITY_FS_USE_NONE)
1700                                         goto bad;
1701                                 len = le32_to_cpu(buf[1]);
1702                                 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1703                                 if (!c->u.name) {
1704                                         rc = -ENOMEM;
1705                                         goto bad;
1706                                 }
1707                                 rc = next_entry(c->u.name, fp, len);
1708                                 if (rc < 0)
1709                                         goto bad;
1710                                 c->u.name[len] = 0;
1711                                 rc = context_read_and_validate(&c->context[0], p, fp);
1712                                 if (rc)
1713                                         goto bad;
1714                                 break;
1715                         case OCON_NODE6: {
1716                                 int k;
1717
1718                                 rc = next_entry(buf, fp, sizeof(u32) * 8);
1719                                 if (rc < 0)
1720                                         goto bad;
1721                                 for (k = 0; k < 4; k++)
1722                                         c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1723                                 for (k = 0; k < 4; k++)
1724                                         c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1725                                 if (context_read_and_validate(&c->context[0], p, fp))
1726                                         goto bad;
1727                                 break;
1728                         }
1729                         }
1730                 }
1731         }
1732
1733         rc = next_entry(buf, fp, sizeof(u32));
1734         if (rc < 0)
1735                 goto bad;
1736         nel = le32_to_cpu(buf[0]);
1737         genfs_p = NULL;
1738         rc = -EINVAL;
1739         for (i = 0; i < nel; i++) {
1740                 rc = next_entry(buf, fp, sizeof(u32));
1741                 if (rc < 0)
1742                         goto bad;
1743                 len = le32_to_cpu(buf[0]);
1744                 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1745                 if (!newgenfs) {
1746                         rc = -ENOMEM;
1747                         goto bad;
1748                 }
1749                 memset(newgenfs, 0, sizeof(*newgenfs));
1750
1751                 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1752                 if (!newgenfs->fstype) {
1753                         rc = -ENOMEM;
1754                         kfree(newgenfs);
1755                         goto bad;
1756                 }
1757                 rc = next_entry(newgenfs->fstype, fp, len);
1758                 if (rc < 0) {
1759                         kfree(newgenfs->fstype);
1760                         kfree(newgenfs);
1761                         goto bad;
1762                 }
1763                 newgenfs->fstype[len] = 0;
1764                 for (genfs_p = NULL, genfs = p->genfs; genfs;
1765                      genfs_p = genfs, genfs = genfs->next) {
1766                         if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1767                                 printk(KERN_ERR "security:  dup genfs "
1768                                        "fstype %s\n", newgenfs->fstype);
1769                                 kfree(newgenfs->fstype);
1770                                 kfree(newgenfs);
1771                                 goto bad;
1772                         }
1773                         if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1774                                 break;
1775                 }
1776                 newgenfs->next = genfs;
1777                 if (genfs_p)
1778                         genfs_p->next = newgenfs;
1779                 else
1780                         p->genfs = newgenfs;
1781                 rc = next_entry(buf, fp, sizeof(u32));
1782                 if (rc < 0)
1783                         goto bad;
1784                 nel2 = le32_to_cpu(buf[0]);
1785                 for (j = 0; j < nel2; j++) {
1786                         rc = next_entry(buf, fp, sizeof(u32));
1787                         if (rc < 0)
1788                                 goto bad;
1789                         len = le32_to_cpu(buf[0]);
1790
1791                         newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1792                         if (!newc) {
1793                                 rc = -ENOMEM;
1794                                 goto bad;
1795                         }
1796                         memset(newc, 0, sizeof(*newc));
1797
1798                         newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1799                         if (!newc->u.name) {
1800                                 rc = -ENOMEM;
1801                                 goto bad_newc;
1802                         }
1803                         rc = next_entry(newc->u.name, fp, len);
1804                         if (rc < 0)
1805                                 goto bad_newc;
1806                         newc->u.name[len] = 0;
1807                         rc = next_entry(buf, fp, sizeof(u32));
1808                         if (rc < 0)
1809                                 goto bad_newc;
1810                         newc->v.sclass = le32_to_cpu(buf[0]);
1811                         if (context_read_and_validate(&newc->context[0], p, fp))
1812                                 goto bad_newc;
1813                         for (l = NULL, c = newgenfs->head; c;
1814                              l = c, c = c->next) {
1815                                 if (!strcmp(newc->u.name, c->u.name) &&
1816                                     (!c->v.sclass || !newc->v.sclass ||
1817                                      newc->v.sclass == c->v.sclass)) {
1818                                         printk(KERN_ERR "security:  dup genfs "
1819                                                "entry (%s,%s)\n",
1820                                                newgenfs->fstype, c->u.name);
1821                                         goto bad_newc;
1822                                 }
1823                                 len = strlen(newc->u.name);
1824                                 len2 = strlen(c->u.name);
1825                                 if (len > len2)
1826                                         break;
1827                         }
1828
1829                         newc->next = c;
1830                         if (l)
1831                                 l->next = newc;
1832                         else
1833                                 newgenfs->head = newc;
1834                 }
1835         }
1836
1837         if (p->policyvers >= POLICYDB_VERSION_MLS) {
1838                 rc = next_entry(buf, fp, sizeof(u32));
1839                 if (rc < 0)
1840                         goto bad;
1841                 nel = le32_to_cpu(buf[0]);
1842                 lrt = NULL;
1843                 for (i = 0; i < nel; i++) {
1844                         rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1845                         if (!rt) {
1846                                 rc = -ENOMEM;
1847                                 goto bad;
1848                         }
1849                         memset(rt, 0, sizeof(*rt));
1850                         if (lrt)
1851                                 lrt->next = rt;
1852                         else
1853                                 p->range_tr = rt;
1854                         rc = next_entry(buf, fp, (sizeof(u32) * 2));
1855                         if (rc < 0)
1856                                 goto bad;
1857                         rt->dom = le32_to_cpu(buf[0]);
1858                         rt->type = le32_to_cpu(buf[1]);
1859                         rc = mls_read_range_helper(&rt->range, fp);
1860                         if (rc)
1861                                 goto bad;
1862                         lrt = rt;
1863                 }
1864         }
1865
1866         p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1867         if (!p->type_attr_map)
1868                 goto bad;
1869
1870         for (i = 0; i < p->p_types.nprim; i++) {
1871                 ebitmap_init(&p->type_attr_map[i]);
1872                 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1873                         if (ebitmap_read(&p->type_attr_map[i], fp))
1874                                 goto bad;
1875                 }
1876                 /* add the type itself as the degenerate case */
1877                 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1878                                 goto bad;
1879         }
1880
1881         rc = 0;
1882 out:
1883         return rc;
1884 bad_newc:
1885         ocontext_destroy(newc,OCON_FSUSE);
1886 bad:
1887         if (!rc)
1888                 rc = -EINVAL;
1889         policydb_destroy(p);
1890         goto out;
1891 }