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