]> bbs.cooldavid.org Git - net-next-2.6.git/blob - security/selinux/ss/policydb.c
SELinux: break ocontext reading into a separate function
[net-next-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 const 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 static unsigned int symtab_sizes[SYM_NUM] = {
56         2,
57         32,
58         16,
59         512,
60         128,
61         16,
62         16,
63         16,
64 };
65
66 struct policydb_compat_info {
67         int version;
68         int sym_num;
69         int ocon_num;
70 };
71
72 /* These need to be updated if SYM_NUM or OCON_NUM changes */
73 static struct policydb_compat_info policydb_compat[] = {
74         {
75                 .version        = POLICYDB_VERSION_BASE,
76                 .sym_num        = SYM_NUM - 3,
77                 .ocon_num       = OCON_NUM - 1,
78         },
79         {
80                 .version        = POLICYDB_VERSION_BOOL,
81                 .sym_num        = SYM_NUM - 2,
82                 .ocon_num       = OCON_NUM - 1,
83         },
84         {
85                 .version        = POLICYDB_VERSION_IPV6,
86                 .sym_num        = SYM_NUM - 2,
87                 .ocon_num       = OCON_NUM,
88         },
89         {
90                 .version        = POLICYDB_VERSION_NLCLASS,
91                 .sym_num        = SYM_NUM - 2,
92                 .ocon_num       = OCON_NUM,
93         },
94         {
95                 .version        = POLICYDB_VERSION_MLS,
96                 .sym_num        = SYM_NUM,
97                 .ocon_num       = OCON_NUM,
98         },
99         {
100                 .version        = POLICYDB_VERSION_AVTAB,
101                 .sym_num        = SYM_NUM,
102                 .ocon_num       = OCON_NUM,
103         },
104         {
105                 .version        = POLICYDB_VERSION_RANGETRANS,
106                 .sym_num        = SYM_NUM,
107                 .ocon_num       = OCON_NUM,
108         },
109         {
110                 .version        = POLICYDB_VERSION_POLCAP,
111                 .sym_num        = SYM_NUM,
112                 .ocon_num       = OCON_NUM,
113         },
114         {
115                 .version        = POLICYDB_VERSION_PERMISSIVE,
116                 .sym_num        = SYM_NUM,
117                 .ocon_num       = OCON_NUM,
118         },
119         {
120                 .version        = POLICYDB_VERSION_BOUNDARY,
121                 .sym_num        = SYM_NUM,
122                 .ocon_num       = OCON_NUM,
123         },
124 };
125
126 static struct policydb_compat_info *policydb_lookup_compat(int version)
127 {
128         int i;
129         struct policydb_compat_info *info = NULL;
130
131         for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
132                 if (policydb_compat[i].version == version) {
133                         info = &policydb_compat[i];
134                         break;
135                 }
136         }
137         return info;
138 }
139
140 /*
141  * Initialize the role table.
142  */
143 static int roles_init(struct policydb *p)
144 {
145         char *key = NULL;
146         int rc;
147         struct role_datum *role;
148
149         role = kzalloc(sizeof(*role), GFP_KERNEL);
150         if (!role) {
151                 rc = -ENOMEM;
152                 goto out;
153         }
154         role->value = ++p->p_roles.nprim;
155         if (role->value != OBJECT_R_VAL) {
156                 rc = -EINVAL;
157                 goto out_free_role;
158         }
159         key = kstrdup(OBJECT_R, GFP_KERNEL);
160         if (!key) {
161                 rc = -ENOMEM;
162                 goto out_free_role;
163         }
164         rc = hashtab_insert(p->p_roles.table, key, role);
165         if (rc)
166                 goto out_free_key;
167 out:
168         return rc;
169
170 out_free_key:
171         kfree(key);
172 out_free_role:
173         kfree(role);
174         goto out;
175 }
176
177 static u32 rangetr_hash(struct hashtab *h, const void *k)
178 {
179         const struct range_trans *key = k;
180         return (key->source_type + (key->target_type << 3) +
181                 (key->target_class << 5)) & (h->size - 1);
182 }
183
184 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
185 {
186         const struct range_trans *key1 = k1, *key2 = k2;
187         return (key1->source_type != key2->source_type ||
188                 key1->target_type != key2->target_type ||
189                 key1->target_class != key2->target_class);
190 }
191
192 /*
193  * Initialize a policy database structure.
194  */
195 static int policydb_init(struct policydb *p)
196 {
197         int i, rc;
198
199         memset(p, 0, sizeof(*p));
200
201         for (i = 0; i < SYM_NUM; i++) {
202                 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
203                 if (rc)
204                         goto out_free_symtab;
205         }
206
207         rc = avtab_init(&p->te_avtab);
208         if (rc)
209                 goto out_free_symtab;
210
211         rc = roles_init(p);
212         if (rc)
213                 goto out_free_symtab;
214
215         rc = cond_policydb_init(p);
216         if (rc)
217                 goto out_free_symtab;
218
219         p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
220         if (!p->range_tr)
221                 goto out_free_symtab;
222
223         ebitmap_init(&p->policycaps);
224         ebitmap_init(&p->permissive_map);
225
226 out:
227         return rc;
228
229 out_free_symtab:
230         for (i = 0; i < SYM_NUM; i++)
231                 hashtab_destroy(p->symtab[i].table);
232         goto out;
233 }
234
235 /*
236  * The following *_index functions are used to
237  * define the val_to_name and val_to_struct arrays
238  * in a policy database structure.  The val_to_name
239  * arrays are used when converting security context
240  * structures into string representations.  The
241  * val_to_struct arrays are used when the attributes
242  * of a class, role, or user are needed.
243  */
244
245 static int common_index(void *key, void *datum, void *datap)
246 {
247         struct policydb *p;
248         struct common_datum *comdatum;
249
250         comdatum = datum;
251         p = datap;
252         if (!comdatum->value || comdatum->value > p->p_commons.nprim)
253                 return -EINVAL;
254         p->p_common_val_to_name[comdatum->value - 1] = key;
255         return 0;
256 }
257
258 static int class_index(void *key, void *datum, void *datap)
259 {
260         struct policydb *p;
261         struct class_datum *cladatum;
262
263         cladatum = datum;
264         p = datap;
265         if (!cladatum->value || cladatum->value > p->p_classes.nprim)
266                 return -EINVAL;
267         p->p_class_val_to_name[cladatum->value - 1] = key;
268         p->class_val_to_struct[cladatum->value - 1] = cladatum;
269         return 0;
270 }
271
272 static int role_index(void *key, void *datum, void *datap)
273 {
274         struct policydb *p;
275         struct role_datum *role;
276
277         role = datum;
278         p = datap;
279         if (!role->value
280             || role->value > p->p_roles.nprim
281             || role->bounds > p->p_roles.nprim)
282                 return -EINVAL;
283         p->p_role_val_to_name[role->value - 1] = key;
284         p->role_val_to_struct[role->value - 1] = role;
285         return 0;
286 }
287
288 static int type_index(void *key, void *datum, void *datap)
289 {
290         struct policydb *p;
291         struct type_datum *typdatum;
292
293         typdatum = datum;
294         p = datap;
295
296         if (typdatum->primary) {
297                 if (!typdatum->value
298                     || typdatum->value > p->p_types.nprim
299                     || typdatum->bounds > p->p_types.nprim)
300                         return -EINVAL;
301                 p->p_type_val_to_name[typdatum->value - 1] = key;
302                 p->type_val_to_struct[typdatum->value - 1] = typdatum;
303         }
304
305         return 0;
306 }
307
308 static int user_index(void *key, void *datum, void *datap)
309 {
310         struct policydb *p;
311         struct user_datum *usrdatum;
312
313         usrdatum = datum;
314         p = datap;
315         if (!usrdatum->value
316             || usrdatum->value > p->p_users.nprim
317             || usrdatum->bounds > p->p_users.nprim)
318                 return -EINVAL;
319         p->p_user_val_to_name[usrdatum->value - 1] = key;
320         p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
321         return 0;
322 }
323
324 static int sens_index(void *key, void *datum, void *datap)
325 {
326         struct policydb *p;
327         struct level_datum *levdatum;
328
329         levdatum = datum;
330         p = datap;
331
332         if (!levdatum->isalias) {
333                 if (!levdatum->level->sens ||
334                     levdatum->level->sens > p->p_levels.nprim)
335                         return -EINVAL;
336                 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
337         }
338
339         return 0;
340 }
341
342 static int cat_index(void *key, void *datum, void *datap)
343 {
344         struct policydb *p;
345         struct cat_datum *catdatum;
346
347         catdatum = datum;
348         p = datap;
349
350         if (!catdatum->isalias) {
351                 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
352                         return -EINVAL;
353                 p->p_cat_val_to_name[catdatum->value - 1] = key;
354         }
355
356         return 0;
357 }
358
359 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
360 {
361         common_index,
362         class_index,
363         role_index,
364         type_index,
365         user_index,
366         cond_index_bool,
367         sens_index,
368         cat_index,
369 };
370
371 /*
372  * Define the common val_to_name array and the class
373  * val_to_name and val_to_struct arrays in a policy
374  * database structure.
375  *
376  * Caller must clean up upon failure.
377  */
378 static int policydb_index_classes(struct policydb *p)
379 {
380         int rc;
381
382         p->p_common_val_to_name =
383                 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
384         if (!p->p_common_val_to_name) {
385                 rc = -ENOMEM;
386                 goto out;
387         }
388
389         rc = hashtab_map(p->p_commons.table, common_index, p);
390         if (rc)
391                 goto out;
392
393         p->class_val_to_struct =
394                 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
395         if (!p->class_val_to_struct) {
396                 rc = -ENOMEM;
397                 goto out;
398         }
399
400         p->p_class_val_to_name =
401                 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
402         if (!p->p_class_val_to_name) {
403                 rc = -ENOMEM;
404                 goto out;
405         }
406
407         rc = hashtab_map(p->p_classes.table, class_index, p);
408 out:
409         return rc;
410 }
411
412 #ifdef DEBUG_HASHES
413 static void symtab_hash_eval(struct symtab *s)
414 {
415         int i;
416
417         for (i = 0; i < SYM_NUM; i++) {
418                 struct hashtab *h = s[i].table;
419                 struct hashtab_info info;
420
421                 hashtab_stat(h, &info);
422                 printk(KERN_DEBUG "SELinux: %s:  %d entries and %d/%d buckets used, "
423                        "longest chain length %d\n", symtab_name[i], h->nel,
424                        info.slots_used, h->size, info.max_chain_len);
425         }
426 }
427
428 static void rangetr_hash_eval(struct hashtab *h)
429 {
430         struct hashtab_info info;
431
432         hashtab_stat(h, &info);
433         printk(KERN_DEBUG "SELinux: rangetr:  %d entries and %d/%d buckets used, "
434                "longest chain length %d\n", h->nel,
435                info.slots_used, h->size, info.max_chain_len);
436 }
437 #else
438 static inline void rangetr_hash_eval(struct hashtab *h)
439 {
440 }
441 #endif
442
443 /*
444  * Define the other val_to_name and val_to_struct arrays
445  * in a policy database structure.
446  *
447  * Caller must clean up on failure.
448  */
449 static int policydb_index_others(struct policydb *p)
450 {
451         int i, rc = 0;
452
453         printk(KERN_DEBUG "SELinux:  %d users, %d roles, %d types, %d bools",
454                p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
455         if (p->mls_enabled)
456                 printk(", %d sens, %d cats", p->p_levels.nprim,
457                        p->p_cats.nprim);
458         printk("\n");
459
460         printk(KERN_DEBUG "SELinux:  %d classes, %d rules\n",
461                p->p_classes.nprim, p->te_avtab.nel);
462
463 #ifdef DEBUG_HASHES
464         avtab_hash_eval(&p->te_avtab, "rules");
465         symtab_hash_eval(p->symtab);
466 #endif
467
468         p->role_val_to_struct =
469                 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
470                         GFP_KERNEL);
471         if (!p->role_val_to_struct) {
472                 rc = -ENOMEM;
473                 goto out;
474         }
475
476         p->user_val_to_struct =
477                 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
478                         GFP_KERNEL);
479         if (!p->user_val_to_struct) {
480                 rc = -ENOMEM;
481                 goto out;
482         }
483
484         p->type_val_to_struct =
485                 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
486                         GFP_KERNEL);
487         if (!p->type_val_to_struct) {
488                 rc = -ENOMEM;
489                 goto out;
490         }
491
492         if (cond_init_bool_indexes(p)) {
493                 rc = -ENOMEM;
494                 goto out;
495         }
496
497         for (i = SYM_ROLES; i < SYM_NUM; i++) {
498                 p->sym_val_to_name[i] =
499                         kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
500                 if (!p->sym_val_to_name[i]) {
501                         rc = -ENOMEM;
502                         goto out;
503                 }
504                 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
505                 if (rc)
506                         goto out;
507         }
508
509 out:
510         return rc;
511 }
512
513 /*
514  * The following *_destroy functions are used to
515  * free any memory allocated for each kind of
516  * symbol data in the policy database.
517  */
518
519 static int perm_destroy(void *key, void *datum, void *p)
520 {
521         kfree(key);
522         kfree(datum);
523         return 0;
524 }
525
526 static int common_destroy(void *key, void *datum, void *p)
527 {
528         struct common_datum *comdatum;
529
530         kfree(key);
531         comdatum = datum;
532         hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
533         hashtab_destroy(comdatum->permissions.table);
534         kfree(datum);
535         return 0;
536 }
537
538 static int cls_destroy(void *key, void *datum, void *p)
539 {
540         struct class_datum *cladatum;
541         struct constraint_node *constraint, *ctemp;
542         struct constraint_expr *e, *etmp;
543
544         kfree(key);
545         cladatum = datum;
546         hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
547         hashtab_destroy(cladatum->permissions.table);
548         constraint = cladatum->constraints;
549         while (constraint) {
550                 e = constraint->expr;
551                 while (e) {
552                         ebitmap_destroy(&e->names);
553                         etmp = e;
554                         e = e->next;
555                         kfree(etmp);
556                 }
557                 ctemp = constraint;
558                 constraint = constraint->next;
559                 kfree(ctemp);
560         }
561
562         constraint = cladatum->validatetrans;
563         while (constraint) {
564                 e = constraint->expr;
565                 while (e) {
566                         ebitmap_destroy(&e->names);
567                         etmp = e;
568                         e = e->next;
569                         kfree(etmp);
570                 }
571                 ctemp = constraint;
572                 constraint = constraint->next;
573                 kfree(ctemp);
574         }
575
576         kfree(cladatum->comkey);
577         kfree(datum);
578         return 0;
579 }
580
581 static int role_destroy(void *key, void *datum, void *p)
582 {
583         struct role_datum *role;
584
585         kfree(key);
586         role = datum;
587         ebitmap_destroy(&role->dominates);
588         ebitmap_destroy(&role->types);
589         kfree(datum);
590         return 0;
591 }
592
593 static int type_destroy(void *key, void *datum, void *p)
594 {
595         kfree(key);
596         kfree(datum);
597         return 0;
598 }
599
600 static int user_destroy(void *key, void *datum, void *p)
601 {
602         struct user_datum *usrdatum;
603
604         kfree(key);
605         usrdatum = datum;
606         ebitmap_destroy(&usrdatum->roles);
607         ebitmap_destroy(&usrdatum->range.level[0].cat);
608         ebitmap_destroy(&usrdatum->range.level[1].cat);
609         ebitmap_destroy(&usrdatum->dfltlevel.cat);
610         kfree(datum);
611         return 0;
612 }
613
614 static int sens_destroy(void *key, void *datum, void *p)
615 {
616         struct level_datum *levdatum;
617
618         kfree(key);
619         levdatum = datum;
620         ebitmap_destroy(&levdatum->level->cat);
621         kfree(levdatum->level);
622         kfree(datum);
623         return 0;
624 }
625
626 static int cat_destroy(void *key, void *datum, void *p)
627 {
628         kfree(key);
629         kfree(datum);
630         return 0;
631 }
632
633 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
634 {
635         common_destroy,
636         cls_destroy,
637         role_destroy,
638         type_destroy,
639         user_destroy,
640         cond_destroy_bool,
641         sens_destroy,
642         cat_destroy,
643 };
644
645 static int range_tr_destroy(void *key, void *datum, void *p)
646 {
647         struct mls_range *rt = datum;
648         kfree(key);
649         ebitmap_destroy(&rt->level[0].cat);
650         ebitmap_destroy(&rt->level[1].cat);
651         kfree(datum);
652         cond_resched();
653         return 0;
654 }
655
656 static void ocontext_destroy(struct ocontext *c, int i)
657 {
658         if (!c)
659                 return;
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 static int range_read(struct policydb *p, void *fp)
1708 {
1709         struct range_trans *rt = NULL;
1710         struct mls_range *r = NULL;
1711         int i, rc;
1712         __le32 buf[2];
1713         u32 nel;
1714
1715         if (p->policyvers < POLICYDB_VERSION_MLS)
1716                 return 0;
1717
1718         rc = next_entry(buf, fp, sizeof(u32));
1719         if (rc)
1720                 goto out;
1721
1722         nel = le32_to_cpu(buf[0]);
1723         for (i = 0; i < nel; i++) {
1724                 rc = -ENOMEM;
1725                 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1726                 if (!rt)
1727                         goto out;
1728
1729                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1730                 if (rc)
1731                         goto out;
1732
1733                 rt->source_type = le32_to_cpu(buf[0]);
1734                 rt->target_type = le32_to_cpu(buf[1]);
1735                 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1736                         rc = next_entry(buf, fp, sizeof(u32));
1737                         if (rc)
1738                                 goto out;
1739                         rt->target_class = le32_to_cpu(buf[0]);
1740                 } else
1741                         rt->target_class = p->process_class;
1742
1743                 rc = -EINVAL;
1744                 if (!policydb_type_isvalid(p, rt->source_type) ||
1745                     !policydb_type_isvalid(p, rt->target_type) ||
1746                     !policydb_class_isvalid(p, rt->target_class))
1747                         goto out;
1748
1749                 rc = -ENOMEM;
1750                 r = kzalloc(sizeof(*r), GFP_KERNEL);
1751                 if (!r)
1752                         goto out;
1753
1754                 rc = mls_read_range_helper(r, fp);
1755                 if (rc)
1756                         goto out;
1757
1758                 rc = -EINVAL;
1759                 if (!mls_range_isvalid(p, r)) {
1760                         printk(KERN_WARNING "SELinux:  rangetrans:  invalid range\n");
1761                         goto out;
1762                 }
1763
1764                 rc = hashtab_insert(p->range_tr, rt, r);
1765                 if (rc)
1766                         goto out;
1767
1768                 rt = NULL;
1769                 r = NULL;
1770         }
1771         rangetr_hash_eval(p->range_tr);
1772         rc = 0;
1773 out:
1774         kfree(rt);
1775         kfree(r);
1776         return rc;
1777 }
1778
1779 static int genfs_read(struct policydb *p, void *fp)
1780 {
1781         int i, j, rc;
1782         u32 nel, nel2, len, len2;
1783         __le32 buf[1];
1784         struct ocontext *l, *c;
1785         struct ocontext *newc = NULL;
1786         struct genfs *genfs_p, *genfs;
1787         struct genfs *newgenfs = NULL;
1788
1789         rc = next_entry(buf, fp, sizeof(u32));
1790         if (rc)
1791                 goto out;
1792         nel = le32_to_cpu(buf[0]);
1793
1794         for (i = 0; i < nel; i++) {
1795                 rc = next_entry(buf, fp, sizeof(u32));
1796                 if (rc)
1797                         goto out;
1798                 len = le32_to_cpu(buf[0]);
1799
1800                 rc = -ENOMEM;
1801                 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1802                 if (!newgenfs)
1803                         goto out;
1804
1805                 rc = -ENOMEM;
1806                 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1807                 if (!newgenfs->fstype)
1808                         goto out;
1809
1810                 rc = next_entry(newgenfs->fstype, fp, len);
1811                 if (rc)
1812                         goto out;
1813
1814                 newgenfs->fstype[len] = 0;
1815
1816                 for (genfs_p = NULL, genfs = p->genfs; genfs;
1817                      genfs_p = genfs, genfs = genfs->next) {
1818                         rc = -EINVAL;
1819                         if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1820                                 printk(KERN_ERR "SELinux:  dup genfs fstype %s\n",
1821                                        newgenfs->fstype);
1822                                 goto out;
1823                         }
1824                         if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1825                                 break;
1826                 }
1827                 newgenfs->next = genfs;
1828                 if (genfs_p)
1829                         genfs_p->next = newgenfs;
1830                 else
1831                         p->genfs = newgenfs;
1832                 genfs = newgenfs;
1833                 newgenfs = NULL;
1834
1835                 rc = next_entry(buf, fp, sizeof(u32));
1836                 if (rc)
1837                         goto out;
1838
1839                 nel2 = le32_to_cpu(buf[0]);
1840                 for (j = 0; j < nel2; j++) {
1841                         rc = next_entry(buf, fp, sizeof(u32));
1842                         if (rc)
1843                                 goto out;
1844                         len = le32_to_cpu(buf[0]);
1845
1846                         rc = -ENOMEM;
1847                         newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1848                         if (!newc)
1849                                 goto out;
1850
1851                         rc = -ENOMEM;
1852                         newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1853                         if (!newc->u.name)
1854                                 goto out;
1855
1856                         rc = next_entry(newc->u.name, fp, len);
1857                         if (rc)
1858                                 goto out;
1859                         newc->u.name[len] = 0;
1860
1861                         rc = next_entry(buf, fp, sizeof(u32));
1862                         if (rc)
1863                                 goto out;
1864
1865                         newc->v.sclass = le32_to_cpu(buf[0]);
1866                         rc = context_read_and_validate(&newc->context[0], p, fp);
1867                         if (rc)
1868                                 goto out;
1869
1870                         for (l = NULL, c = genfs->head; c;
1871                              l = c, c = c->next) {
1872                                 rc = -EINVAL;
1873                                 if (!strcmp(newc->u.name, c->u.name) &&
1874                                     (!c->v.sclass || !newc->v.sclass ||
1875                                      newc->v.sclass == c->v.sclass)) {
1876                                         printk(KERN_ERR "SELinux:  dup genfs entry (%s,%s)\n",
1877                                                genfs->fstype, c->u.name);
1878                                         goto out;
1879                                 }
1880                                 len = strlen(newc->u.name);
1881                                 len2 = strlen(c->u.name);
1882                                 if (len > len2)
1883                                         break;
1884                         }
1885
1886                         newc->next = c;
1887                         if (l)
1888                                 l->next = newc;
1889                         else
1890                                 genfs->head = newc;
1891                         newc = NULL;
1892                 }
1893         }
1894         rc = 0;
1895 out:
1896         if (newgenfs)
1897                 kfree(newgenfs->fstype);
1898         kfree(newgenfs);
1899         ocontext_destroy(newc, OCON_FSUSE);
1900
1901         return rc;
1902 }
1903
1904 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1905                          void *fp)
1906 {
1907         int i, j, rc;
1908         u32 nel, len;
1909         __le32 buf[3];
1910         struct ocontext *l, *c;
1911         u32 nodebuf[8];
1912
1913         for (i = 0; i < info->ocon_num; i++) {
1914                 rc = next_entry(buf, fp, sizeof(u32));
1915                 if (rc)
1916                         goto out;
1917                 nel = le32_to_cpu(buf[0]);
1918
1919                 l = NULL;
1920                 for (j = 0; j < nel; j++) {
1921                         rc = -ENOMEM;
1922                         c = kzalloc(sizeof(*c), GFP_KERNEL);
1923                         if (!c)
1924                                 goto out;
1925                         if (l)
1926                                 l->next = c;
1927                         else
1928                                 p->ocontexts[i] = c;
1929                         l = c;
1930
1931                         switch (i) {
1932                         case OCON_ISID:
1933                                 rc = next_entry(buf, fp, sizeof(u32));
1934                                 if (rc)
1935                                         goto out;
1936
1937                                 c->sid[0] = le32_to_cpu(buf[0]);
1938                                 rc = context_read_and_validate(&c->context[0], p, fp);
1939                                 if (rc)
1940                                         goto out;
1941                                 break;
1942                         case OCON_FS:
1943                         case OCON_NETIF:
1944                                 rc = next_entry(buf, fp, sizeof(u32));
1945                                 if (rc)
1946                                         goto out;
1947                                 len = le32_to_cpu(buf[0]);
1948
1949                                 rc = -ENOMEM;
1950                                 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1951                                 if (!c->u.name)
1952                                         goto out;
1953
1954                                 rc = next_entry(c->u.name, fp, len);
1955                                 if (rc)
1956                                         goto out;
1957
1958                                 c->u.name[len] = 0;
1959                                 rc = context_read_and_validate(&c->context[0], p, fp);
1960                                 if (rc)
1961                                         goto out;
1962                                 rc = context_read_and_validate(&c->context[1], p, fp);
1963                                 if (rc)
1964                                         goto out;
1965                                 break;
1966                         case OCON_PORT:
1967                                 rc = next_entry(buf, fp, sizeof(u32)*3);
1968                                 if (rc)
1969                                         goto out;
1970                                 c->u.port.protocol = le32_to_cpu(buf[0]);
1971                                 c->u.port.low_port = le32_to_cpu(buf[1]);
1972                                 c->u.port.high_port = le32_to_cpu(buf[2]);
1973                                 rc = context_read_and_validate(&c->context[0], p, fp);
1974                                 if (rc)
1975                                         goto out;
1976                                 break;
1977                         case OCON_NODE:
1978                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1979                                 if (rc)
1980                                         goto out;
1981                                 c->u.node.addr = nodebuf[0]; /* network order */
1982                                 c->u.node.mask = nodebuf[1]; /* network order */
1983                                 rc = context_read_and_validate(&c->context[0], p, fp);
1984                                 if (rc)
1985                                         goto out;
1986                                 break;
1987                         case OCON_FSUSE:
1988                                 rc = next_entry(buf, fp, sizeof(u32)*2);
1989                                 if (rc)
1990                                         goto out;
1991
1992                                 rc = -EINVAL;
1993                                 c->v.behavior = le32_to_cpu(buf[0]);
1994                                 if (c->v.behavior > SECURITY_FS_USE_NONE)
1995                                         goto out;
1996
1997                                 rc = -ENOMEM;
1998                                 len = le32_to_cpu(buf[1]);
1999                                 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2000                                 if (!c->u.name)
2001                                         goto out;
2002
2003                                 rc = next_entry(c->u.name, fp, len);
2004                                 if (rc)
2005                                         goto out;
2006                                 c->u.name[len] = 0;
2007                                 rc = context_read_and_validate(&c->context[0], p, fp);
2008                                 if (rc)
2009                                         goto out;
2010                                 break;
2011                         case OCON_NODE6: {
2012                                 int k;
2013
2014                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2015                                 if (rc)
2016                                         goto out;
2017                                 for (k = 0; k < 4; k++)
2018                                         c->u.node6.addr[k] = nodebuf[k];
2019                                 for (k = 0; k < 4; k++)
2020                                         c->u.node6.mask[k] = nodebuf[k+4];
2021                                 rc = context_read_and_validate(&c->context[0], p, fp);
2022                                 if (rc)
2023                                         goto out;
2024                                 break;
2025                         }
2026                         }
2027                 }
2028         }
2029         rc = 0;
2030 out:
2031         return rc;
2032 }
2033
2034 /*
2035  * Read the configuration data from a policy database binary
2036  * representation file into a policy database structure.
2037  */
2038 int policydb_read(struct policydb *p, void *fp)
2039 {
2040         struct role_allow *ra, *lra;
2041         struct role_trans *tr, *ltr;
2042         int i, j, rc;
2043         __le32 buf[4];
2044         u32 len, nprim, nel;
2045
2046         char *policydb_str;
2047         struct policydb_compat_info *info;
2048
2049         rc = policydb_init(p);
2050         if (rc)
2051                 goto out;
2052
2053         /* Read the magic number and string length. */
2054         rc = next_entry(buf, fp, sizeof(u32) * 2);
2055         if (rc < 0)
2056                 goto bad;
2057
2058         if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2059                 printk(KERN_ERR "SELinux:  policydb magic number 0x%x does "
2060                        "not match expected magic number 0x%x\n",
2061                        le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2062                 goto bad;
2063         }
2064
2065         len = le32_to_cpu(buf[1]);
2066         if (len != strlen(POLICYDB_STRING)) {
2067                 printk(KERN_ERR "SELinux:  policydb string length %d does not "
2068                        "match expected length %Zu\n",
2069                        len, strlen(POLICYDB_STRING));
2070                 goto bad;
2071         }
2072         policydb_str = kmalloc(len + 1, GFP_KERNEL);
2073         if (!policydb_str) {
2074                 printk(KERN_ERR "SELinux:  unable to allocate memory for policydb "
2075                        "string of length %d\n", len);
2076                 rc = -ENOMEM;
2077                 goto bad;
2078         }
2079         rc = next_entry(policydb_str, fp, len);
2080         if (rc < 0) {
2081                 printk(KERN_ERR "SELinux:  truncated policydb string identifier\n");
2082                 kfree(policydb_str);
2083                 goto bad;
2084         }
2085         policydb_str[len] = '\0';
2086         if (strcmp(policydb_str, POLICYDB_STRING)) {
2087                 printk(KERN_ERR "SELinux:  policydb string %s does not match "
2088                        "my string %s\n", policydb_str, POLICYDB_STRING);
2089                 kfree(policydb_str);
2090                 goto bad;
2091         }
2092         /* Done with policydb_str. */
2093         kfree(policydb_str);
2094         policydb_str = NULL;
2095
2096         /* Read the version and table sizes. */
2097         rc = next_entry(buf, fp, sizeof(u32)*4);
2098         if (rc < 0)
2099                 goto bad;
2100
2101         p->policyvers = le32_to_cpu(buf[0]);
2102         if (p->policyvers < POLICYDB_VERSION_MIN ||
2103             p->policyvers > POLICYDB_VERSION_MAX) {
2104                 printk(KERN_ERR "SELinux:  policydb version %d does not match "
2105                        "my version range %d-%d\n",
2106                        le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2107                 goto bad;
2108         }
2109
2110         if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2111                 p->mls_enabled = 1;
2112
2113                 if (p->policyvers < POLICYDB_VERSION_MLS) {
2114                         printk(KERN_ERR "SELinux: security policydb version %d "
2115                                 "(MLS) not backwards compatible\n",
2116                                 p->policyvers);
2117                         goto bad;
2118                 }
2119         }
2120         p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2121         p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2122
2123         if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
2124             ebitmap_read(&p->policycaps, fp) != 0)
2125                 goto bad;
2126
2127         if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
2128             ebitmap_read(&p->permissive_map, fp) != 0)
2129                 goto bad;
2130
2131         info = policydb_lookup_compat(p->policyvers);
2132         if (!info) {
2133                 printk(KERN_ERR "SELinux:  unable to find policy compat info "
2134                        "for version %d\n", p->policyvers);
2135                 goto bad;
2136         }
2137
2138         if (le32_to_cpu(buf[2]) != info->sym_num ||
2139                 le32_to_cpu(buf[3]) != info->ocon_num) {
2140                 printk(KERN_ERR "SELinux:  policydb table sizes (%d,%d) do "
2141                        "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2142                         le32_to_cpu(buf[3]),
2143                        info->sym_num, info->ocon_num);
2144                 goto bad;
2145         }
2146
2147         for (i = 0; i < info->sym_num; i++) {
2148                 rc = next_entry(buf, fp, sizeof(u32)*2);
2149                 if (rc < 0)
2150                         goto bad;
2151                 nprim = le32_to_cpu(buf[0]);
2152                 nel = le32_to_cpu(buf[1]);
2153                 for (j = 0; j < nel; j++) {
2154                         rc = read_f[i](p, p->symtab[i].table, fp);
2155                         if (rc)
2156                                 goto bad;
2157                 }
2158
2159                 p->symtab[i].nprim = nprim;
2160         }
2161
2162         rc = avtab_read(&p->te_avtab, fp, p);
2163         if (rc)
2164                 goto bad;
2165
2166         if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2167                 rc = cond_read_list(p, fp);
2168                 if (rc)
2169                         goto bad;
2170         }
2171
2172         rc = next_entry(buf, fp, sizeof(u32));
2173         if (rc < 0)
2174                 goto bad;
2175         nel = le32_to_cpu(buf[0]);
2176         ltr = NULL;
2177         for (i = 0; i < nel; i++) {
2178                 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2179                 if (!tr) {
2180                         rc = -ENOMEM;
2181                         goto bad;
2182                 }
2183                 if (ltr)
2184                         ltr->next = tr;
2185                 else
2186                         p->role_tr = tr;
2187                 rc = next_entry(buf, fp, sizeof(u32)*3);
2188                 if (rc < 0)
2189                         goto bad;
2190                 tr->role = le32_to_cpu(buf[0]);
2191                 tr->type = le32_to_cpu(buf[1]);
2192                 tr->new_role = le32_to_cpu(buf[2]);
2193                 if (!policydb_role_isvalid(p, tr->role) ||
2194                     !policydb_type_isvalid(p, tr->type) ||
2195                     !policydb_role_isvalid(p, tr->new_role)) {
2196                         rc = -EINVAL;
2197                         goto bad;
2198                 }
2199                 ltr = tr;
2200         }
2201
2202         rc = next_entry(buf, fp, sizeof(u32));
2203         if (rc < 0)
2204                 goto bad;
2205         nel = le32_to_cpu(buf[0]);
2206         lra = NULL;
2207         for (i = 0; i < nel; i++) {
2208                 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2209                 if (!ra) {
2210                         rc = -ENOMEM;
2211                         goto bad;
2212                 }
2213                 if (lra)
2214                         lra->next = ra;
2215                 else
2216                         p->role_allow = ra;
2217                 rc = next_entry(buf, fp, sizeof(u32)*2);
2218                 if (rc < 0)
2219                         goto bad;
2220                 ra->role = le32_to_cpu(buf[0]);
2221                 ra->new_role = le32_to_cpu(buf[1]);
2222                 if (!policydb_role_isvalid(p, ra->role) ||
2223                     !policydb_role_isvalid(p, ra->new_role)) {
2224                         rc = -EINVAL;
2225                         goto bad;
2226                 }
2227                 lra = ra;
2228         }
2229
2230         rc = policydb_index_classes(p);
2231         if (rc)
2232                 goto bad;
2233
2234         rc = policydb_index_others(p);
2235         if (rc)
2236                 goto bad;
2237
2238         p->process_class = string_to_security_class(p, "process");
2239         if (!p->process_class)
2240                 goto bad;
2241         p->process_trans_perms = string_to_av_perm(p, p->process_class,
2242                                                    "transition");
2243         p->process_trans_perms |= string_to_av_perm(p, p->process_class,
2244                                                     "dyntransition");
2245         if (!p->process_trans_perms)
2246                 goto bad;
2247
2248         rc = ocontext_read(p, info, fp);
2249         if (rc)
2250                 goto bad;
2251
2252         rc = genfs_read(p, fp);
2253         if (rc)
2254                 goto bad;
2255
2256         rc = range_read(p, fp);
2257         if (rc)
2258                 goto bad;
2259
2260         p->type_attr_map = kmalloc(p->p_types.nprim * sizeof(struct ebitmap), GFP_KERNEL);
2261         if (!p->type_attr_map)
2262                 goto bad;
2263
2264         for (i = 0; i < p->p_types.nprim; i++) {
2265                 ebitmap_init(&p->type_attr_map[i]);
2266                 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2267                         if (ebitmap_read(&p->type_attr_map[i], fp))
2268                                 goto bad;
2269                 }
2270                 /* add the type itself as the degenerate case */
2271                 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
2272                                 goto bad;
2273         }
2274
2275         rc = policydb_bounds_sanity_check(p);
2276         if (rc)
2277                 goto bad;
2278
2279         rc = 0;
2280 out:
2281         return rc;
2282 bad:
2283         if (!rc)
2284                 rc = -EINVAL;
2285         policydb_destroy(p);
2286         goto out;
2287 }