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