5adca670e5af6b35d205d997d87219652a751d0b
[linux-flexiantxendom0-natty.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 <linux/flex_array.h>
35 #include "security.h"
36
37 #include "policydb.h"
38 #include "conditional.h"
39 #include "mls.h"
40 #include "services.h"
41
42 #define _DEBUG_HASHES
43
44 #ifdef DEBUG_HASHES
45 static const char *symtab_name[SYM_NUM] = {
46         "common prefixes",
47         "classes",
48         "roles",
49         "types",
50         "users",
51         "bools",
52         "levels",
53         "categories",
54 };
55 #endif
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         rc = -ENOMEM;
152         role = kzalloc(sizeof(*role), GFP_KERNEL);
153         if (!role)
154                 goto out;
155
156         rc = -EINVAL;
157         role->value = ++p->p_roles.nprim;
158         if (role->value != OBJECT_R_VAL)
159                 goto out;
160
161         rc = -ENOMEM;
162         key = kstrdup(OBJECT_R, GFP_KERNEL);
163         if (!key)
164                 goto out;
165
166         rc = hashtab_insert(p->p_roles.table, key, role);
167         if (rc)
168                 goto out;
169
170         return 0;
171 out:
172         kfree(key);
173         kfree(role);
174         return rc;
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         int v;
188
189         v = key1->source_type - key2->source_type;
190         if (v)
191                 return v;
192
193         v = key1->target_type - key2->target_type;
194         if (v)
195                 return v;
196
197         v = key1->target_class - key2->target_class;
198
199         return v;
200 }
201
202 /*
203  * Initialize a policy database structure.
204  */
205 static int policydb_init(struct policydb *p)
206 {
207         int i, rc;
208
209         memset(p, 0, sizeof(*p));
210
211         for (i = 0; i < SYM_NUM; i++) {
212                 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
213                 if (rc)
214                         goto out;
215         }
216
217         rc = avtab_init(&p->te_avtab);
218         if (rc)
219                 goto out;
220
221         rc = roles_init(p);
222         if (rc)
223                 goto out;
224
225         rc = cond_policydb_init(p);
226         if (rc)
227                 goto out;
228
229         p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
230         if (!p->range_tr)
231                 goto out;
232
233         ebitmap_init(&p->policycaps);
234         ebitmap_init(&p->permissive_map);
235
236         return 0;
237 out:
238         for (i = 0; i < SYM_NUM; i++)
239                 hashtab_destroy(p->symtab[i].table);
240         return rc;
241 }
242
243 /*
244  * The following *_index functions are used to
245  * define the val_to_name and val_to_struct arrays
246  * in a policy database structure.  The val_to_name
247  * arrays are used when converting security context
248  * structures into string representations.  The
249  * val_to_struct arrays are used when the attributes
250  * of a class, role, or user are needed.
251  */
252
253 static int common_index(void *key, void *datum, void *datap)
254 {
255         struct policydb *p;
256         struct common_datum *comdatum;
257         struct flex_array *fa;
258
259         comdatum = datum;
260         p = datap;
261         if (!comdatum->value || comdatum->value > p->p_commons.nprim)
262                 return -EINVAL;
263
264         fa = p->sym_val_to_name[SYM_COMMONS];
265         if (flex_array_put_ptr(fa, comdatum->value - 1, key,
266                                GFP_KERNEL | __GFP_ZERO))
267                 BUG();
268         return 0;
269 }
270
271 static int class_index(void *key, void *datum, void *datap)
272 {
273         struct policydb *p;
274         struct class_datum *cladatum;
275         struct flex_array *fa;
276
277         cladatum = datum;
278         p = datap;
279         if (!cladatum->value || cladatum->value > p->p_classes.nprim)
280                 return -EINVAL;
281         fa = p->sym_val_to_name[SYM_CLASSES];
282         if (flex_array_put_ptr(fa, cladatum->value - 1, key,
283                                GFP_KERNEL | __GFP_ZERO))
284                 BUG();
285         p->class_val_to_struct[cladatum->value - 1] = cladatum;
286         return 0;
287 }
288
289 static int role_index(void *key, void *datum, void *datap)
290 {
291         struct policydb *p;
292         struct role_datum *role;
293         struct flex_array *fa;
294
295         role = datum;
296         p = datap;
297         if (!role->value
298             || role->value > p->p_roles.nprim
299             || role->bounds > p->p_roles.nprim)
300                 return -EINVAL;
301
302         fa = p->sym_val_to_name[SYM_ROLES];
303         if (flex_array_put_ptr(fa, role->value - 1, key,
304                                GFP_KERNEL | __GFP_ZERO))
305                 BUG();
306         p->role_val_to_struct[role->value - 1] = role;
307         return 0;
308 }
309
310 static int type_index(void *key, void *datum, void *datap)
311 {
312         struct policydb *p;
313         struct type_datum *typdatum;
314         struct flex_array *fa;
315
316         typdatum = datum;
317         p = datap;
318
319         if (typdatum->primary) {
320                 if (!typdatum->value
321                     || typdatum->value > p->p_types.nprim
322                     || typdatum->bounds > p->p_types.nprim)
323                         return -EINVAL;
324                 fa = p->sym_val_to_name[SYM_TYPES];
325                 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
326                                        GFP_KERNEL | __GFP_ZERO))
327                         BUG();
328
329                 fa = p->type_val_to_struct_array;
330                 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
331                                        GFP_KERNEL | __GFP_ZERO))
332                         BUG();
333         }
334
335         return 0;
336 }
337
338 static int user_index(void *key, void *datum, void *datap)
339 {
340         struct policydb *p;
341         struct user_datum *usrdatum;
342         struct flex_array *fa;
343
344         usrdatum = datum;
345         p = datap;
346         if (!usrdatum->value
347             || usrdatum->value > p->p_users.nprim
348             || usrdatum->bounds > p->p_users.nprim)
349                 return -EINVAL;
350
351         fa = p->sym_val_to_name[SYM_USERS];
352         if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
353                                GFP_KERNEL | __GFP_ZERO))
354                 BUG();
355         p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
356         return 0;
357 }
358
359 static int sens_index(void *key, void *datum, void *datap)
360 {
361         struct policydb *p;
362         struct level_datum *levdatum;
363         struct flex_array *fa;
364
365         levdatum = datum;
366         p = datap;
367
368         if (!levdatum->isalias) {
369                 if (!levdatum->level->sens ||
370                     levdatum->level->sens > p->p_levels.nprim)
371                         return -EINVAL;
372                 fa = p->sym_val_to_name[SYM_LEVELS];
373                 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
374                                        GFP_KERNEL | __GFP_ZERO))
375                         BUG();
376         }
377
378         return 0;
379 }
380
381 static int cat_index(void *key, void *datum, void *datap)
382 {
383         struct policydb *p;
384         struct cat_datum *catdatum;
385         struct flex_array *fa;
386
387         catdatum = datum;
388         p = datap;
389
390         if (!catdatum->isalias) {
391                 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
392                         return -EINVAL;
393                 fa = p->sym_val_to_name[SYM_CATS];
394                 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
395                                        GFP_KERNEL | __GFP_ZERO))
396                         BUG();
397         }
398
399         return 0;
400 }
401
402 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
403 {
404         common_index,
405         class_index,
406         role_index,
407         type_index,
408         user_index,
409         cond_index_bool,
410         sens_index,
411         cat_index,
412 };
413
414 /*
415  * Define the common val_to_name array and the class
416  * val_to_name and val_to_struct arrays in a policy
417  * database structure.
418  *
419  * Caller must clean up upon failure.
420  */
421 static int policydb_index_classes(struct policydb *p)
422 {
423         int rc;
424
425         rc = -ENOMEM;
426         p->sym_val_to_name[SYM_COMMONS] = flex_array_alloc(sizeof(char *),
427                                                            p->p_commons.nprim,
428                                                            GFP_KERNEL | __GFP_ZERO);
429         if (!p->sym_val_to_name[SYM_COMMONS])
430                 goto out;
431
432         rc = flex_array_prealloc(p->sym_val_to_name[SYM_COMMONS],
433                                  0, p->p_commons.nprim - 1,
434                                  GFP_KERNEL | __GFP_ZERO);
435         if (rc)
436                 goto out;
437
438         rc = hashtab_map(p->p_commons.table, common_index, p);
439         if (rc)
440                 goto out;
441
442         rc = -ENOMEM;
443         p->class_val_to_struct =
444                 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
445         if (!p->class_val_to_struct)
446                 goto out;
447
448         rc = -ENOMEM;
449         p->sym_val_to_name[SYM_CLASSES] = flex_array_alloc(sizeof(char *),
450                                                            p->p_classes.nprim,
451                                                            GFP_KERNEL | __GFP_ZERO);
452         if (!p->sym_val_to_name[SYM_CLASSES])
453                 goto out;
454
455         rc = flex_array_prealloc(p->sym_val_to_name[SYM_CLASSES],
456                                  0, p->p_classes.nprim - 1,
457                                  GFP_KERNEL | __GFP_ZERO);
458         if (rc)
459                 goto out;
460
461         rc = hashtab_map(p->p_classes.table, class_index, p);
462 out:
463         return rc;
464 }
465
466 #ifdef DEBUG_HASHES
467 static void symtab_hash_eval(struct symtab *s)
468 {
469         int i;
470
471         for (i = 0; i < SYM_NUM; i++) {
472                 struct hashtab *h = s[i].table;
473                 struct hashtab_info info;
474
475                 hashtab_stat(h, &info);
476                 printk(KERN_DEBUG "SELinux: %s:  %d entries and %d/%d buckets used, "
477                        "longest chain length %d\n", symtab_name[i], h->nel,
478                        info.slots_used, h->size, info.max_chain_len);
479         }
480 }
481
482 static void rangetr_hash_eval(struct hashtab *h)
483 {
484         struct hashtab_info info;
485
486         hashtab_stat(h, &info);
487         printk(KERN_DEBUG "SELinux: rangetr:  %d entries and %d/%d buckets used, "
488                "longest chain length %d\n", h->nel,
489                info.slots_used, h->size, info.max_chain_len);
490 }
491 #else
492 static inline void rangetr_hash_eval(struct hashtab *h)
493 {
494 }
495 #endif
496
497 /*
498  * Define the other val_to_name and val_to_struct arrays
499  * in a policy database structure.
500  *
501  * Caller must clean up on failure.
502  */
503 static int policydb_index_others(struct policydb *p)
504 {
505         int i, rc;
506
507         printk(KERN_DEBUG "SELinux:  %d users, %d roles, %d types, %d bools",
508                p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
509         if (p->mls_enabled)
510                 printk(", %d sens, %d cats", p->p_levels.nprim,
511                        p->p_cats.nprim);
512         printk("\n");
513
514         printk(KERN_DEBUG "SELinux:  %d classes, %d rules\n",
515                p->p_classes.nprim, p->te_avtab.nel);
516
517 #ifdef DEBUG_HASHES
518         avtab_hash_eval(&p->te_avtab, "rules");
519         symtab_hash_eval(p->symtab);
520 #endif
521
522         rc = -ENOMEM;
523         p->role_val_to_struct =
524                 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
525                         GFP_KERNEL);
526         if (!p->role_val_to_struct)
527                 goto out;
528
529         rc = -ENOMEM;
530         p->user_val_to_struct =
531                 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
532                         GFP_KERNEL);
533         if (!p->user_val_to_struct)
534                 goto out;
535
536         /* Yes, I want the sizeof the pointer, not the structure */
537         rc = -ENOMEM;
538         p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
539                                                        p->p_types.nprim,
540                                                        GFP_KERNEL | __GFP_ZERO);
541         if (!p->type_val_to_struct_array)
542                 goto out;
543
544         rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
545                                  p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
546         if (rc)
547                 goto out;
548
549         rc = -ENOMEM;
550         if (cond_init_bool_indexes(p))
551                 goto out;
552
553         for (i = SYM_ROLES; i < SYM_NUM; i++) {
554                 rc = -ENOMEM;
555                 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
556                                                          p->symtab[i].nprim,
557                                                          GFP_KERNEL | __GFP_ZERO);
558                 if (!p->sym_val_to_name[i])
559                         goto out;
560
561                 rc = flex_array_prealloc(p->sym_val_to_name[i],
562                                          0, p->symtab[i].nprim - 1,
563                                          GFP_KERNEL | __GFP_ZERO);
564                 if (rc)
565                         goto out;
566
567                 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
568                 if (rc)
569                         goto out;
570         }
571         rc = 0;
572 out:
573         return rc;
574 }
575
576 /*
577  * The following *_destroy functions are used to
578  * free any memory allocated for each kind of
579  * symbol data in the policy database.
580  */
581
582 static int perm_destroy(void *key, void *datum, void *p)
583 {
584         kfree(key);
585         kfree(datum);
586         return 0;
587 }
588
589 static int common_destroy(void *key, void *datum, void *p)
590 {
591         struct common_datum *comdatum;
592
593         kfree(key);
594         if (datum) {
595                 comdatum = datum;
596                 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
597                 hashtab_destroy(comdatum->permissions.table);
598         }
599         kfree(datum);
600         return 0;
601 }
602
603 static int cls_destroy(void *key, void *datum, void *p)
604 {
605         struct class_datum *cladatum;
606         struct constraint_node *constraint, *ctemp;
607         struct constraint_expr *e, *etmp;
608
609         kfree(key);
610         if (datum) {
611                 cladatum = datum;
612                 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
613                 hashtab_destroy(cladatum->permissions.table);
614                 constraint = cladatum->constraints;
615                 while (constraint) {
616                         e = constraint->expr;
617                         while (e) {
618                                 ebitmap_destroy(&e->names);
619                                 etmp = e;
620                                 e = e->next;
621                                 kfree(etmp);
622                         }
623                         ctemp = constraint;
624                         constraint = constraint->next;
625                         kfree(ctemp);
626                 }
627
628                 constraint = cladatum->validatetrans;
629                 while (constraint) {
630                         e = constraint->expr;
631                         while (e) {
632                                 ebitmap_destroy(&e->names);
633                                 etmp = e;
634                                 e = e->next;
635                                 kfree(etmp);
636                         }
637                         ctemp = constraint;
638                         constraint = constraint->next;
639                         kfree(ctemp);
640                 }
641
642                 kfree(cladatum->comkey);
643         }
644         kfree(datum);
645         return 0;
646 }
647
648 static int role_destroy(void *key, void *datum, void *p)
649 {
650         struct role_datum *role;
651
652         kfree(key);
653         if (datum) {
654                 role = datum;
655                 ebitmap_destroy(&role->dominates);
656                 ebitmap_destroy(&role->types);
657         }
658         kfree(datum);
659         return 0;
660 }
661
662 static int type_destroy(void *key, void *datum, void *p)
663 {
664         kfree(key);
665         kfree(datum);
666         return 0;
667 }
668
669 static int user_destroy(void *key, void *datum, void *p)
670 {
671         struct user_datum *usrdatum;
672
673         kfree(key);
674         if (datum) {
675                 usrdatum = datum;
676                 ebitmap_destroy(&usrdatum->roles);
677                 ebitmap_destroy(&usrdatum->range.level[0].cat);
678                 ebitmap_destroy(&usrdatum->range.level[1].cat);
679                 ebitmap_destroy(&usrdatum->dfltlevel.cat);
680         }
681         kfree(datum);
682         return 0;
683 }
684
685 static int sens_destroy(void *key, void *datum, void *p)
686 {
687         struct level_datum *levdatum;
688
689         kfree(key);
690         if (datum) {
691                 levdatum = datum;
692                 ebitmap_destroy(&levdatum->level->cat);
693                 kfree(levdatum->level);
694         }
695         kfree(datum);
696         return 0;
697 }
698
699 static int cat_destroy(void *key, void *datum, void *p)
700 {
701         kfree(key);
702         kfree(datum);
703         return 0;
704 }
705
706 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
707 {
708         common_destroy,
709         cls_destroy,
710         role_destroy,
711         type_destroy,
712         user_destroy,
713         cond_destroy_bool,
714         sens_destroy,
715         cat_destroy,
716 };
717
718 static int range_tr_destroy(void *key, void *datum, void *p)
719 {
720         struct mls_range *rt = datum;
721         kfree(key);
722         ebitmap_destroy(&rt->level[0].cat);
723         ebitmap_destroy(&rt->level[1].cat);
724         kfree(datum);
725         cond_resched();
726         return 0;
727 }
728
729 static void ocontext_destroy(struct ocontext *c, int i)
730 {
731         if (!c)
732                 return;
733
734         context_destroy(&c->context[0]);
735         context_destroy(&c->context[1]);
736         if (i == OCON_ISID || i == OCON_FS ||
737             i == OCON_NETIF || i == OCON_FSUSE)
738                 kfree(c->u.name);
739         kfree(c);
740 }
741
742 /*
743  * Free any memory allocated by a policy database structure.
744  */
745 void policydb_destroy(struct policydb *p)
746 {
747         struct ocontext *c, *ctmp;
748         struct genfs *g, *gtmp;
749         int i;
750         struct role_allow *ra, *lra = NULL;
751         struct role_trans *tr, *ltr = NULL;
752
753         for (i = 0; i < SYM_NUM; i++) {
754                 cond_resched();
755                 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
756                 hashtab_destroy(p->symtab[i].table);
757         }
758
759         for (i = 0; i < SYM_NUM; i++) {
760                 if (p->sym_val_to_name[i])
761                         flex_array_free(p->sym_val_to_name[i]);
762         }
763
764         kfree(p->class_val_to_struct);
765         kfree(p->role_val_to_struct);
766         kfree(p->user_val_to_struct);
767         if (p->type_val_to_struct_array)
768                 flex_array_free(p->type_val_to_struct_array);
769
770         avtab_destroy(&p->te_avtab);
771
772         for (i = 0; i < OCON_NUM; i++) {
773                 cond_resched();
774                 c = p->ocontexts[i];
775                 while (c) {
776                         ctmp = c;
777                         c = c->next;
778                         ocontext_destroy(ctmp, i);
779                 }
780                 p->ocontexts[i] = NULL;
781         }
782
783         g = p->genfs;
784         while (g) {
785                 cond_resched();
786                 kfree(g->fstype);
787                 c = g->head;
788                 while (c) {
789                         ctmp = c;
790                         c = c->next;
791                         ocontext_destroy(ctmp, OCON_FSUSE);
792                 }
793                 gtmp = g;
794                 g = g->next;
795                 kfree(gtmp);
796         }
797         p->genfs = NULL;
798
799         cond_policydb_destroy(p);
800
801         for (tr = p->role_tr; tr; tr = tr->next) {
802                 cond_resched();
803                 kfree(ltr);
804                 ltr = tr;
805         }
806         kfree(ltr);
807
808         for (ra = p->role_allow; ra; ra = ra->next) {
809                 cond_resched();
810                 kfree(lra);
811                 lra = ra;
812         }
813         kfree(lra);
814
815         hashtab_map(p->range_tr, range_tr_destroy, NULL);
816         hashtab_destroy(p->range_tr);
817
818         if (p->type_attr_map_array) {
819                 for (i = 0; i < p->p_types.nprim; i++) {
820                         struct ebitmap *e;
821
822                         e = flex_array_get(p->type_attr_map_array, i);
823                         if (!e)
824                                 continue;
825                         ebitmap_destroy(e);
826                 }
827                 flex_array_free(p->type_attr_map_array);
828         }
829         ebitmap_destroy(&p->policycaps);
830         ebitmap_destroy(&p->permissive_map);
831
832         return;
833 }
834
835 /*
836  * Load the initial SIDs specified in a policy database
837  * structure into a SID table.
838  */
839 int policydb_load_isids(struct policydb *p, struct sidtab *s)
840 {
841         struct ocontext *head, *c;
842         int rc;
843
844         rc = sidtab_init(s);
845         if (rc) {
846                 printk(KERN_ERR "SELinux:  out of memory on SID table init\n");
847                 goto out;
848         }
849
850         head = p->ocontexts[OCON_ISID];
851         for (c = head; c; c = c->next) {
852                 rc = -EINVAL;
853                 if (!c->context[0].user) {
854                         printk(KERN_ERR "SELinux:  SID %s was never defined.\n",
855                                 c->u.name);
856                         goto out;
857                 }
858
859                 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
860                 if (rc) {
861                         printk(KERN_ERR "SELinux:  unable to load initial SID %s.\n",
862                                 c->u.name);
863                         goto out;
864                 }
865         }
866         rc = 0;
867 out:
868         return rc;
869 }
870
871 int policydb_class_isvalid(struct policydb *p, unsigned int class)
872 {
873         if (!class || class > p->p_classes.nprim)
874                 return 0;
875         return 1;
876 }
877
878 int policydb_role_isvalid(struct policydb *p, unsigned int role)
879 {
880         if (!role || role > p->p_roles.nprim)
881                 return 0;
882         return 1;
883 }
884
885 int policydb_type_isvalid(struct policydb *p, unsigned int type)
886 {
887         if (!type || type > p->p_types.nprim)
888                 return 0;
889         return 1;
890 }
891
892 /*
893  * Return 1 if the fields in the security context
894  * structure `c' are valid.  Return 0 otherwise.
895  */
896 int policydb_context_isvalid(struct policydb *p, struct context *c)
897 {
898         struct role_datum *role;
899         struct user_datum *usrdatum;
900
901         if (!c->role || c->role > p->p_roles.nprim)
902                 return 0;
903
904         if (!c->user || c->user > p->p_users.nprim)
905                 return 0;
906
907         if (!c->type || c->type > p->p_types.nprim)
908                 return 0;
909
910         if (c->role != OBJECT_R_VAL) {
911                 /*
912                  * Role must be authorized for the type.
913                  */
914                 role = p->role_val_to_struct[c->role - 1];
915                 if (!ebitmap_get_bit(&role->types, c->type - 1))
916                         /* role may not be associated with type */
917                         return 0;
918
919                 /*
920                  * User must be authorized for the role.
921                  */
922                 usrdatum = p->user_val_to_struct[c->user - 1];
923                 if (!usrdatum)
924                         return 0;
925
926                 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
927                         /* user may not be associated with role */
928                         return 0;
929         }
930
931         if (!mls_context_isvalid(p, c))
932                 return 0;
933
934         return 1;
935 }
936
937 /*
938  * Read a MLS range structure from a policydb binary
939  * representation file.
940  */
941 static int mls_read_range_helper(struct mls_range *r, void *fp)
942 {
943         __le32 buf[2];
944         u32 items;
945         int rc;
946
947         rc = next_entry(buf, fp, sizeof(u32));
948         if (rc)
949                 goto out;
950
951         rc = -EINVAL;
952         items = le32_to_cpu(buf[0]);
953         if (items > ARRAY_SIZE(buf)) {
954                 printk(KERN_ERR "SELinux: mls:  range overflow\n");
955                 goto out;
956         }
957
958         rc = next_entry(buf, fp, sizeof(u32) * items);
959         if (rc) {
960                 printk(KERN_ERR "SELinux: mls:  truncated range\n");
961                 goto out;
962         }
963
964         r->level[0].sens = le32_to_cpu(buf[0]);
965         if (items > 1)
966                 r->level[1].sens = le32_to_cpu(buf[1]);
967         else
968                 r->level[1].sens = r->level[0].sens;
969
970         rc = ebitmap_read(&r->level[0].cat, fp);
971         if (rc) {
972                 printk(KERN_ERR "SELinux: mls:  error reading low categories\n");
973                 goto out;
974         }
975         if (items > 1) {
976                 rc = ebitmap_read(&r->level[1].cat, fp);
977                 if (rc) {
978                         printk(KERN_ERR "SELinux: mls:  error reading high categories\n");
979                         goto bad_high;
980                 }
981         } else {
982                 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
983                 if (rc) {
984                         printk(KERN_ERR "SELinux: mls:  out of memory\n");
985                         goto bad_high;
986                 }
987         }
988
989         return 0;
990 bad_high:
991         ebitmap_destroy(&r->level[0].cat);
992 out:
993         return rc;
994 }
995
996 /*
997  * Read and validate a security context structure
998  * from a policydb binary representation file.
999  */
1000 static int context_read_and_validate(struct context *c,
1001                                      struct policydb *p,
1002                                      void *fp)
1003 {
1004         __le32 buf[3];
1005         int rc;
1006
1007         rc = next_entry(buf, fp, sizeof buf);
1008         if (rc) {
1009                 printk(KERN_ERR "SELinux: context truncated\n");
1010                 goto out;
1011         }
1012         c->user = le32_to_cpu(buf[0]);
1013         c->role = le32_to_cpu(buf[1]);
1014         c->type = le32_to_cpu(buf[2]);
1015         if (p->policyvers >= POLICYDB_VERSION_MLS) {
1016                 rc = mls_read_range_helper(&c->range, fp);
1017                 if (rc) {
1018                         printk(KERN_ERR "SELinux: error reading MLS range of context\n");
1019                         goto out;
1020                 }
1021         }
1022
1023         rc = -EINVAL;
1024         if (!policydb_context_isvalid(p, c)) {
1025                 printk(KERN_ERR "SELinux:  invalid security context\n");
1026                 context_destroy(c);
1027                 goto out;
1028         }
1029         rc = 0;
1030 out:
1031         return rc;
1032 }
1033
1034 /*
1035  * The following *_read functions are used to
1036  * read the symbol data from a policy database
1037  * binary representation file.
1038  */
1039
1040 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1041 {
1042         char *key = NULL;
1043         struct perm_datum *perdatum;
1044         int rc;
1045         __le32 buf[2];
1046         u32 len;
1047
1048         rc = -ENOMEM;
1049         perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1050         if (!perdatum)
1051                 goto bad;
1052
1053         rc = next_entry(buf, fp, sizeof buf);
1054         if (rc)
1055                 goto bad;
1056
1057         len = le32_to_cpu(buf[0]);
1058         perdatum->value = le32_to_cpu(buf[1]);
1059
1060         rc = -ENOMEM;
1061         key = kmalloc(len + 1, GFP_KERNEL);
1062         if (!key)
1063                 goto bad;
1064
1065         rc = next_entry(key, fp, len);
1066         if (rc)
1067                 goto bad;
1068         key[len] = '\0';
1069
1070         rc = hashtab_insert(h, key, perdatum);
1071         if (rc)
1072                 goto bad;
1073
1074         return 0;
1075 bad:
1076         perm_destroy(key, perdatum, NULL);
1077         return rc;
1078 }
1079
1080 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1081 {
1082         char *key = NULL;
1083         struct common_datum *comdatum;
1084         __le32 buf[4];
1085         u32 len, nel;
1086         int i, rc;
1087
1088         rc = -ENOMEM;
1089         comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1090         if (!comdatum)
1091                 goto bad;
1092
1093         rc = next_entry(buf, fp, sizeof buf);
1094         if (rc)
1095                 goto bad;
1096
1097         len = le32_to_cpu(buf[0]);
1098         comdatum->value = le32_to_cpu(buf[1]);
1099
1100         rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1101         if (rc)
1102                 goto bad;
1103         comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1104         nel = le32_to_cpu(buf[3]);
1105
1106         rc = -ENOMEM;
1107         key = kmalloc(len + 1, GFP_KERNEL);
1108         if (!key)
1109                 goto bad;
1110
1111         rc = next_entry(key, fp, len);
1112         if (rc)
1113                 goto bad;
1114         key[len] = '\0';
1115
1116         for (i = 0; i < nel; i++) {
1117                 rc = perm_read(p, comdatum->permissions.table, fp);
1118                 if (rc)
1119                         goto bad;
1120         }
1121
1122         rc = hashtab_insert(h, key, comdatum);
1123         if (rc)
1124                 goto bad;
1125         return 0;
1126 bad:
1127         common_destroy(key, comdatum, NULL);
1128         return rc;
1129 }
1130
1131 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1132                             int allowxtarget, void *fp)
1133 {
1134         struct constraint_node *c, *lc;
1135         struct constraint_expr *e, *le;
1136         __le32 buf[3];
1137         u32 nexpr;
1138         int rc, i, j, depth;
1139
1140         lc = NULL;
1141         for (i = 0; i < ncons; i++) {
1142                 c = kzalloc(sizeof(*c), GFP_KERNEL);
1143                 if (!c)
1144                         return -ENOMEM;
1145
1146                 if (lc)
1147                         lc->next = c;
1148                 else
1149                         *nodep = c;
1150
1151                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1152                 if (rc)
1153                         return rc;
1154                 c->permissions = le32_to_cpu(buf[0]);
1155                 nexpr = le32_to_cpu(buf[1]);
1156                 le = NULL;
1157                 depth = -1;
1158                 for (j = 0; j < nexpr; j++) {
1159                         e = kzalloc(sizeof(*e), GFP_KERNEL);
1160                         if (!e)
1161                                 return -ENOMEM;
1162
1163                         if (le)
1164                                 le->next = e;
1165                         else
1166                                 c->expr = e;
1167
1168                         rc = next_entry(buf, fp, (sizeof(u32) * 3));
1169                         if (rc)
1170                                 return rc;
1171                         e->expr_type = le32_to_cpu(buf[0]);
1172                         e->attr = le32_to_cpu(buf[1]);
1173                         e->op = le32_to_cpu(buf[2]);
1174
1175                         switch (e->expr_type) {
1176                         case CEXPR_NOT:
1177                                 if (depth < 0)
1178                                         return -EINVAL;
1179                                 break;
1180                         case CEXPR_AND:
1181                         case CEXPR_OR:
1182                                 if (depth < 1)
1183                                         return -EINVAL;
1184                                 depth--;
1185                                 break;
1186                         case CEXPR_ATTR:
1187                                 if (depth == (CEXPR_MAXDEPTH - 1))
1188                                         return -EINVAL;
1189                                 depth++;
1190                                 break;
1191                         case CEXPR_NAMES:
1192                                 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1193                                         return -EINVAL;
1194                                 if (depth == (CEXPR_MAXDEPTH - 1))
1195                                         return -EINVAL;
1196                                 depth++;
1197                                 rc = ebitmap_read(&e->names, fp);
1198                                 if (rc)
1199                                         return rc;
1200                                 break;
1201                         default:
1202                                 return -EINVAL;
1203                         }
1204                         le = e;
1205                 }
1206                 if (depth != 0)
1207                         return -EINVAL;
1208                 lc = c;
1209         }
1210
1211         return 0;
1212 }
1213
1214 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1215 {
1216         char *key = NULL;
1217         struct class_datum *cladatum;
1218         __le32 buf[6];
1219         u32 len, len2, ncons, nel;
1220         int i, rc;
1221
1222         rc = -ENOMEM;
1223         cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1224         if (!cladatum)
1225                 goto bad;
1226
1227         rc = next_entry(buf, fp, sizeof(u32)*6);
1228         if (rc)
1229                 goto bad;
1230
1231         len = le32_to_cpu(buf[0]);
1232         len2 = le32_to_cpu(buf[1]);
1233         cladatum->value = le32_to_cpu(buf[2]);
1234
1235         rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1236         if (rc)
1237                 goto bad;
1238         cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1239         nel = le32_to_cpu(buf[4]);
1240
1241         ncons = le32_to_cpu(buf[5]);
1242
1243         rc = -ENOMEM;
1244         key = kmalloc(len + 1, GFP_KERNEL);
1245         if (!key)
1246                 goto bad;
1247
1248         rc = next_entry(key, fp, len);
1249         if (rc)
1250                 goto bad;
1251         key[len] = '\0';
1252
1253         if (len2) {
1254                 rc = -ENOMEM;
1255                 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1256                 if (!cladatum->comkey)
1257                         goto bad;
1258                 rc = next_entry(cladatum->comkey, fp, len2);
1259                 if (rc)
1260                         goto bad;
1261                 cladatum->comkey[len2] = '\0';
1262
1263                 rc = -EINVAL;
1264                 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1265                 if (!cladatum->comdatum) {
1266                         printk(KERN_ERR "SELinux:  unknown common %s\n", cladatum->comkey);
1267                         goto bad;
1268                 }
1269         }
1270         for (i = 0; i < nel; i++) {
1271                 rc = perm_read(p, cladatum->permissions.table, fp);
1272                 if (rc)
1273                         goto bad;
1274         }
1275
1276         rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1277         if (rc)
1278                 goto bad;
1279
1280         if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1281                 /* grab the validatetrans rules */
1282                 rc = next_entry(buf, fp, sizeof(u32));
1283                 if (rc)
1284                         goto bad;
1285                 ncons = le32_to_cpu(buf[0]);
1286                 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1287                 if (rc)
1288                         goto bad;
1289         }
1290
1291         rc = hashtab_insert(h, key, cladatum);
1292         if (rc)
1293                 goto bad;
1294
1295         return 0;
1296 bad:
1297         cls_destroy(key, cladatum, NULL);
1298         return rc;
1299 }
1300
1301 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1302 {
1303         char *key = NULL;
1304         struct role_datum *role;
1305         int rc, to_read = 2;
1306         __le32 buf[3];
1307         u32 len;
1308
1309         rc = -ENOMEM;
1310         role = kzalloc(sizeof(*role), GFP_KERNEL);
1311         if (!role)
1312                 goto bad;
1313
1314         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1315                 to_read = 3;
1316
1317         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1318         if (rc)
1319                 goto bad;
1320
1321         len = le32_to_cpu(buf[0]);
1322         role->value = le32_to_cpu(buf[1]);
1323         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1324                 role->bounds = le32_to_cpu(buf[2]);
1325
1326         rc = -ENOMEM;
1327         key = kmalloc(len + 1, GFP_KERNEL);
1328         if (!key)
1329                 goto bad;
1330
1331         rc = next_entry(key, fp, len);
1332         if (rc)
1333                 goto bad;
1334         key[len] = '\0';
1335
1336         rc = ebitmap_read(&role->dominates, fp);
1337         if (rc)
1338                 goto bad;
1339
1340         rc = ebitmap_read(&role->types, fp);
1341         if (rc)
1342                 goto bad;
1343
1344         if (strcmp(key, OBJECT_R) == 0) {
1345                 rc = -EINVAL;
1346                 if (role->value != OBJECT_R_VAL) {
1347                         printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1348                                OBJECT_R, role->value);
1349                         goto bad;
1350                 }
1351                 rc = 0;
1352                 goto bad;
1353         }
1354
1355         rc = hashtab_insert(h, key, role);
1356         if (rc)
1357                 goto bad;
1358         return 0;
1359 bad:
1360         role_destroy(key, role, NULL);
1361         return rc;
1362 }
1363
1364 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1365 {
1366         char *key = NULL;
1367         struct type_datum *typdatum;
1368         int rc, to_read = 3;
1369         __le32 buf[4];
1370         u32 len;
1371
1372         rc = -ENOMEM;
1373         typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1374         if (!typdatum)
1375                 goto bad;
1376
1377         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1378                 to_read = 4;
1379
1380         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1381         if (rc)
1382                 goto bad;
1383
1384         len = le32_to_cpu(buf[0]);
1385         typdatum->value = le32_to_cpu(buf[1]);
1386         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1387                 u32 prop = le32_to_cpu(buf[2]);
1388
1389                 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1390                         typdatum->primary = 1;
1391                 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1392                         typdatum->attribute = 1;
1393
1394                 typdatum->bounds = le32_to_cpu(buf[3]);
1395         } else {
1396                 typdatum->primary = le32_to_cpu(buf[2]);
1397         }
1398
1399         rc = -ENOMEM;
1400         key = kmalloc(len + 1, GFP_KERNEL);
1401         if (!key)
1402                 goto bad;
1403         rc = next_entry(key, fp, len);
1404         if (rc)
1405                 goto bad;
1406         key[len] = '\0';
1407
1408         rc = hashtab_insert(h, key, typdatum);
1409         if (rc)
1410                 goto bad;
1411         return 0;
1412 bad:
1413         type_destroy(key, typdatum, NULL);
1414         return rc;
1415 }
1416
1417
1418 /*
1419  * Read a MLS level structure from a policydb binary
1420  * representation file.
1421  */
1422 static int mls_read_level(struct mls_level *lp, void *fp)
1423 {
1424         __le32 buf[1];
1425         int rc;
1426
1427         memset(lp, 0, sizeof(*lp));
1428
1429         rc = next_entry(buf, fp, sizeof buf);
1430         if (rc) {
1431                 printk(KERN_ERR "SELinux: mls: truncated level\n");
1432                 return rc;
1433         }
1434         lp->sens = le32_to_cpu(buf[0]);
1435
1436         rc = ebitmap_read(&lp->cat, fp);
1437         if (rc) {
1438                 printk(KERN_ERR "SELinux: mls:  error reading level categories\n");
1439                 return rc;
1440         }
1441         return 0;
1442 }
1443
1444 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1445 {
1446         char *key = NULL;
1447         struct user_datum *usrdatum;
1448         int rc, to_read = 2;
1449         __le32 buf[3];
1450         u32 len;
1451
1452         rc = -ENOMEM;
1453         usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1454         if (!usrdatum)
1455                 goto bad;
1456
1457         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1458                 to_read = 3;
1459
1460         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1461         if (rc)
1462                 goto bad;
1463
1464         len = le32_to_cpu(buf[0]);
1465         usrdatum->value = le32_to_cpu(buf[1]);
1466         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1467                 usrdatum->bounds = le32_to_cpu(buf[2]);
1468
1469         rc = -ENOMEM;
1470         key = kmalloc(len + 1, GFP_KERNEL);
1471         if (!key)
1472                 goto bad;
1473         rc = next_entry(key, fp, len);
1474         if (rc)
1475                 goto bad;
1476         key[len] = '\0';
1477
1478         rc = ebitmap_read(&usrdatum->roles, fp);
1479         if (rc)
1480                 goto bad;
1481
1482         if (p->policyvers >= POLICYDB_VERSION_MLS) {
1483                 rc = mls_read_range_helper(&usrdatum->range, fp);
1484                 if (rc)
1485                         goto bad;
1486                 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1487                 if (rc)
1488                         goto bad;
1489         }
1490
1491         rc = hashtab_insert(h, key, usrdatum);
1492         if (rc)
1493                 goto bad;
1494         return 0;
1495 bad:
1496         user_destroy(key, usrdatum, NULL);
1497         return rc;
1498 }
1499
1500 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1501 {
1502         char *key = NULL;
1503         struct level_datum *levdatum;
1504         int rc;
1505         __le32 buf[2];
1506         u32 len;
1507
1508         rc = -ENOMEM;
1509         levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1510         if (!levdatum)
1511                 goto bad;
1512
1513         rc = next_entry(buf, fp, sizeof buf);
1514         if (rc)
1515                 goto bad;
1516
1517         len = le32_to_cpu(buf[0]);
1518         levdatum->isalias = le32_to_cpu(buf[1]);
1519
1520         rc = -ENOMEM;
1521         key = kmalloc(len + 1, GFP_ATOMIC);
1522         if (!key)
1523                 goto bad;
1524         rc = next_entry(key, fp, len);
1525         if (rc)
1526                 goto bad;
1527         key[len] = '\0';
1528
1529         rc = -ENOMEM;
1530         levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1531         if (!levdatum->level)
1532                 goto bad;
1533
1534         rc = mls_read_level(levdatum->level, fp);
1535         if (rc)
1536                 goto bad;
1537
1538         rc = hashtab_insert(h, key, levdatum);
1539         if (rc)
1540                 goto bad;
1541         return 0;
1542 bad:
1543         sens_destroy(key, levdatum, NULL);
1544         return rc;
1545 }
1546
1547 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1548 {
1549         char *key = NULL;
1550         struct cat_datum *catdatum;
1551         int rc;
1552         __le32 buf[3];
1553         u32 len;
1554
1555         rc = -ENOMEM;
1556         catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1557         if (!catdatum)
1558                 goto bad;
1559
1560         rc = next_entry(buf, fp, sizeof buf);
1561         if (rc)
1562                 goto bad;
1563
1564         len = le32_to_cpu(buf[0]);
1565         catdatum->value = le32_to_cpu(buf[1]);
1566         catdatum->isalias = le32_to_cpu(buf[2]);
1567
1568         rc = -ENOMEM;
1569         key = kmalloc(len + 1, GFP_ATOMIC);
1570         if (!key)
1571                 goto bad;
1572         rc = next_entry(key, fp, len);
1573         if (rc)
1574                 goto bad;
1575         key[len] = '\0';
1576
1577         rc = hashtab_insert(h, key, catdatum);
1578         if (rc)
1579                 goto bad;
1580         return 0;
1581 bad:
1582         cat_destroy(key, catdatum, NULL);
1583         return rc;
1584 }
1585
1586 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1587 {
1588         common_read,
1589         class_read,
1590         role_read,
1591         type_read,
1592         user_read,
1593         cond_read_bool,
1594         sens_read,
1595         cat_read,
1596 };
1597
1598 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1599 {
1600         struct user_datum *upper, *user;
1601         struct policydb *p = datap;
1602         int depth = 0;
1603
1604         upper = user = datum;
1605         while (upper->bounds) {
1606                 struct ebitmap_node *node;
1607                 unsigned long bit;
1608
1609                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1610                         printk(KERN_ERR "SELinux: user %s: "
1611                                "too deep or looped boundary",
1612                                (char *) key);
1613                         return -EINVAL;
1614                 }
1615
1616                 upper = p->user_val_to_struct[upper->bounds - 1];
1617                 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1618                         if (ebitmap_get_bit(&upper->roles, bit))
1619                                 continue;
1620
1621                         printk(KERN_ERR
1622                                "SELinux: boundary violated policy: "
1623                                "user=%s role=%s bounds=%s\n",
1624                                sym_name(p, SYM_USERS, user->value - 1),
1625                                sym_name(p, SYM_ROLES, bit),
1626                                sym_name(p, SYM_USERS, upper->value - 1));
1627
1628                         return -EINVAL;
1629                 }
1630         }
1631
1632         return 0;
1633 }
1634
1635 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1636 {
1637         struct role_datum *upper, *role;
1638         struct policydb *p = datap;
1639         int depth = 0;
1640
1641         upper = role = datum;
1642         while (upper->bounds) {
1643                 struct ebitmap_node *node;
1644                 unsigned long bit;
1645
1646                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1647                         printk(KERN_ERR "SELinux: role %s: "
1648                                "too deep or looped bounds\n",
1649                                (char *) key);
1650                         return -EINVAL;
1651                 }
1652
1653                 upper = p->role_val_to_struct[upper->bounds - 1];
1654                 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1655                         if (ebitmap_get_bit(&upper->types, bit))
1656                                 continue;
1657
1658                         printk(KERN_ERR
1659                                "SELinux: boundary violated policy: "
1660                                "role=%s type=%s bounds=%s\n",
1661                                sym_name(p, SYM_ROLES, role->value - 1),
1662                                sym_name(p, SYM_TYPES, bit),
1663                                sym_name(p, SYM_ROLES, upper->value - 1));
1664
1665                         return -EINVAL;
1666                 }
1667         }
1668
1669         return 0;
1670 }
1671
1672 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1673 {
1674         struct type_datum *upper;
1675         struct policydb *p = datap;
1676         int depth = 0;
1677
1678         upper = datum;
1679         while (upper->bounds) {
1680                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1681                         printk(KERN_ERR "SELinux: type %s: "
1682                                "too deep or looped boundary\n",
1683                                (char *) key);
1684                         return -EINVAL;
1685                 }
1686
1687                 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1688                                            upper->bounds - 1);
1689                 BUG_ON(!upper);
1690
1691                 if (upper->attribute) {
1692                         printk(KERN_ERR "SELinux: type %s: "
1693                                "bounded by attribute %s",
1694                                (char *) key,
1695                                sym_name(p, SYM_TYPES, upper->value - 1));
1696                         return -EINVAL;
1697                 }
1698         }
1699
1700         return 0;
1701 }
1702
1703 static int policydb_bounds_sanity_check(struct policydb *p)
1704 {
1705         int rc;
1706
1707         if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1708                 return 0;
1709
1710         rc = hashtab_map(p->p_users.table,
1711                          user_bounds_sanity_check, p);
1712         if (rc)
1713                 return rc;
1714
1715         rc = hashtab_map(p->p_roles.table,
1716                          role_bounds_sanity_check, p);
1717         if (rc)
1718                 return rc;
1719
1720         rc = hashtab_map(p->p_types.table,
1721                          type_bounds_sanity_check, p);
1722         if (rc)
1723                 return rc;
1724
1725         return 0;
1726 }
1727
1728 extern int ss_initialized;
1729
1730 u16 string_to_security_class(struct policydb *p, const char *name)
1731 {
1732         struct class_datum *cladatum;
1733
1734         cladatum = hashtab_search(p->p_classes.table, name);
1735         if (!cladatum)
1736                 return 0;
1737
1738         return cladatum->value;
1739 }
1740
1741 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1742 {
1743         struct class_datum *cladatum;
1744         struct perm_datum *perdatum = NULL;
1745         struct common_datum *comdatum;
1746
1747         if (!tclass || tclass > p->p_classes.nprim)
1748                 return 0;
1749
1750         cladatum = p->class_val_to_struct[tclass-1];
1751         comdatum = cladatum->comdatum;
1752         if (comdatum)
1753                 perdatum = hashtab_search(comdatum->permissions.table,
1754                                           name);
1755         if (!perdatum)
1756                 perdatum = hashtab_search(cladatum->permissions.table,
1757                                           name);
1758         if (!perdatum)
1759                 return 0;
1760
1761         return 1U << (perdatum->value-1);
1762 }
1763
1764 static int range_read(struct policydb *p, void *fp)
1765 {
1766         struct range_trans *rt = NULL;
1767         struct mls_range *r = NULL;
1768         int i, rc;
1769         __le32 buf[2];
1770         u32 nel;
1771
1772         if (p->policyvers < POLICYDB_VERSION_MLS)
1773                 return 0;
1774
1775         rc = next_entry(buf, fp, sizeof(u32));
1776         if (rc)
1777                 goto out;
1778
1779         nel = le32_to_cpu(buf[0]);
1780         for (i = 0; i < nel; i++) {
1781                 rc = -ENOMEM;
1782                 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1783                 if (!rt)
1784                         goto out;
1785
1786                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1787                 if (rc)
1788                         goto out;
1789
1790                 rt->source_type = le32_to_cpu(buf[0]);
1791                 rt->target_type = le32_to_cpu(buf[1]);
1792                 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1793                         rc = next_entry(buf, fp, sizeof(u32));
1794                         if (rc)
1795                                 goto out;
1796                         rt->target_class = le32_to_cpu(buf[0]);
1797                 } else
1798                         rt->target_class = p->process_class;
1799
1800                 rc = -EINVAL;
1801                 if (!policydb_type_isvalid(p, rt->source_type) ||
1802                     !policydb_type_isvalid(p, rt->target_type) ||
1803                     !policydb_class_isvalid(p, rt->target_class))
1804                         goto out;
1805
1806                 rc = -ENOMEM;
1807                 r = kzalloc(sizeof(*r), GFP_KERNEL);
1808                 if (!r)
1809                         goto out;
1810
1811                 rc = mls_read_range_helper(r, fp);
1812                 if (rc)
1813                         goto out;
1814
1815                 rc = -EINVAL;
1816                 if (!mls_range_isvalid(p, r)) {
1817                         printk(KERN_WARNING "SELinux:  rangetrans:  invalid range\n");
1818                         goto out;
1819                 }
1820
1821                 rc = hashtab_insert(p->range_tr, rt, r);
1822                 if (rc)
1823                         goto out;
1824
1825                 rt = NULL;
1826                 r = NULL;
1827         }
1828         rangetr_hash_eval(p->range_tr);
1829         rc = 0;
1830 out:
1831         kfree(rt);
1832         kfree(r);
1833         return rc;
1834 }
1835
1836 static int genfs_read(struct policydb *p, void *fp)
1837 {
1838         int i, j, rc;
1839         u32 nel, nel2, len, len2;
1840         __le32 buf[1];
1841         struct ocontext *l, *c;
1842         struct ocontext *newc = NULL;
1843         struct genfs *genfs_p, *genfs;
1844         struct genfs *newgenfs = NULL;
1845
1846         rc = next_entry(buf, fp, sizeof(u32));
1847         if (rc)
1848                 goto out;
1849         nel = le32_to_cpu(buf[0]);
1850
1851         for (i = 0; i < nel; i++) {
1852                 rc = next_entry(buf, fp, sizeof(u32));
1853                 if (rc)
1854                         goto out;
1855                 len = le32_to_cpu(buf[0]);
1856
1857                 rc = -ENOMEM;
1858                 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1859                 if (!newgenfs)
1860                         goto out;
1861
1862                 rc = -ENOMEM;
1863                 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1864                 if (!newgenfs->fstype)
1865                         goto out;
1866
1867                 rc = next_entry(newgenfs->fstype, fp, len);
1868                 if (rc)
1869                         goto out;
1870
1871                 newgenfs->fstype[len] = 0;
1872
1873                 for (genfs_p = NULL, genfs = p->genfs; genfs;
1874                      genfs_p = genfs, genfs = genfs->next) {
1875                         rc = -EINVAL;
1876                         if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1877                                 printk(KERN_ERR "SELinux:  dup genfs fstype %s\n",
1878                                        newgenfs->fstype);
1879                                 goto out;
1880                         }
1881                         if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1882                                 break;
1883                 }
1884                 newgenfs->next = genfs;
1885                 if (genfs_p)
1886                         genfs_p->next = newgenfs;
1887                 else
1888                         p->genfs = newgenfs;
1889                 genfs = newgenfs;
1890                 newgenfs = NULL;
1891
1892                 rc = next_entry(buf, fp, sizeof(u32));
1893                 if (rc)
1894                         goto out;
1895
1896                 nel2 = le32_to_cpu(buf[0]);
1897                 for (j = 0; j < nel2; j++) {
1898                         rc = next_entry(buf, fp, sizeof(u32));
1899                         if (rc)
1900                                 goto out;
1901                         len = le32_to_cpu(buf[0]);
1902
1903                         rc = -ENOMEM;
1904                         newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1905                         if (!newc)
1906                                 goto out;
1907
1908                         rc = -ENOMEM;
1909                         newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1910                         if (!newc->u.name)
1911                                 goto out;
1912
1913                         rc = next_entry(newc->u.name, fp, len);
1914                         if (rc)
1915                                 goto out;
1916                         newc->u.name[len] = 0;
1917
1918                         rc = next_entry(buf, fp, sizeof(u32));
1919                         if (rc)
1920                                 goto out;
1921
1922                         newc->v.sclass = le32_to_cpu(buf[0]);
1923                         rc = context_read_and_validate(&newc->context[0], p, fp);
1924                         if (rc)
1925                                 goto out;
1926
1927                         for (l = NULL, c = genfs->head; c;
1928                              l = c, c = c->next) {
1929                                 rc = -EINVAL;
1930                                 if (!strcmp(newc->u.name, c->u.name) &&
1931                                     (!c->v.sclass || !newc->v.sclass ||
1932                                      newc->v.sclass == c->v.sclass)) {
1933                                         printk(KERN_ERR "SELinux:  dup genfs entry (%s,%s)\n",
1934                                                genfs->fstype, c->u.name);
1935                                         goto out;
1936                                 }
1937                                 len = strlen(newc->u.name);
1938                                 len2 = strlen(c->u.name);
1939                                 if (len > len2)
1940                                         break;
1941                         }
1942
1943                         newc->next = c;
1944                         if (l)
1945                                 l->next = newc;
1946                         else
1947                                 genfs->head = newc;
1948                         newc = NULL;
1949                 }
1950         }
1951         rc = 0;
1952 out:
1953         if (newgenfs)
1954                 kfree(newgenfs->fstype);
1955         kfree(newgenfs);
1956         ocontext_destroy(newc, OCON_FSUSE);
1957
1958         return rc;
1959 }
1960
1961 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1962                          void *fp)
1963 {
1964         int i, j, rc;
1965         u32 nel, len;
1966         __le32 buf[3];
1967         struct ocontext *l, *c;
1968         u32 nodebuf[8];
1969
1970         for (i = 0; i < info->ocon_num; i++) {
1971                 rc = next_entry(buf, fp, sizeof(u32));
1972                 if (rc)
1973                         goto out;
1974                 nel = le32_to_cpu(buf[0]);
1975
1976                 l = NULL;
1977                 for (j = 0; j < nel; j++) {
1978                         rc = -ENOMEM;
1979                         c = kzalloc(sizeof(*c), GFP_KERNEL);
1980                         if (!c)
1981                                 goto out;
1982                         if (l)
1983                                 l->next = c;
1984                         else
1985                                 p->ocontexts[i] = c;
1986                         l = c;
1987
1988                         switch (i) {
1989                         case OCON_ISID:
1990                                 rc = next_entry(buf, fp, sizeof(u32));
1991                                 if (rc)
1992                                         goto out;
1993
1994                                 c->sid[0] = le32_to_cpu(buf[0]);
1995                                 rc = context_read_and_validate(&c->context[0], p, fp);
1996                                 if (rc)
1997                                         goto out;
1998                                 break;
1999                         case OCON_FS:
2000                         case OCON_NETIF:
2001                                 rc = next_entry(buf, fp, sizeof(u32));
2002                                 if (rc)
2003                                         goto out;
2004                                 len = le32_to_cpu(buf[0]);
2005
2006                                 rc = -ENOMEM;
2007                                 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2008                                 if (!c->u.name)
2009                                         goto out;
2010
2011                                 rc = next_entry(c->u.name, fp, len);
2012                                 if (rc)
2013                                         goto out;
2014
2015                                 c->u.name[len] = 0;
2016                                 rc = context_read_and_validate(&c->context[0], p, fp);
2017                                 if (rc)
2018                                         goto out;
2019                                 rc = context_read_and_validate(&c->context[1], p, fp);
2020                                 if (rc)
2021                                         goto out;
2022                                 break;
2023                         case OCON_PORT:
2024                                 rc = next_entry(buf, fp, sizeof(u32)*3);
2025                                 if (rc)
2026                                         goto out;
2027                                 c->u.port.protocol = le32_to_cpu(buf[0]);
2028                                 c->u.port.low_port = le32_to_cpu(buf[1]);
2029                                 c->u.port.high_port = le32_to_cpu(buf[2]);
2030                                 rc = context_read_and_validate(&c->context[0], p, fp);
2031                                 if (rc)
2032                                         goto out;
2033                                 break;
2034                         case OCON_NODE:
2035                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2036                                 if (rc)
2037                                         goto out;
2038                                 c->u.node.addr = nodebuf[0]; /* network order */
2039                                 c->u.node.mask = nodebuf[1]; /* network order */
2040                                 rc = context_read_and_validate(&c->context[0], p, fp);
2041                                 if (rc)
2042                                         goto out;
2043                                 break;
2044                         case OCON_FSUSE:
2045                                 rc = next_entry(buf, fp, sizeof(u32)*2);
2046                                 if (rc)
2047                                         goto out;
2048
2049                                 rc = -EINVAL;
2050                                 c->v.behavior = le32_to_cpu(buf[0]);
2051                                 if (c->v.behavior > SECURITY_FS_USE_NONE)
2052                                         goto out;
2053
2054                                 rc = -ENOMEM;
2055                                 len = le32_to_cpu(buf[1]);
2056                                 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2057                                 if (!c->u.name)
2058                                         goto out;
2059
2060                                 rc = next_entry(c->u.name, fp, len);
2061                                 if (rc)
2062                                         goto out;
2063                                 c->u.name[len] = 0;
2064                                 rc = context_read_and_validate(&c->context[0], p, fp);
2065                                 if (rc)
2066                                         goto out;
2067                                 break;
2068                         case OCON_NODE6: {
2069                                 int k;
2070
2071                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2072                                 if (rc)
2073                                         goto out;
2074                                 for (k = 0; k < 4; k++)
2075                                         c->u.node6.addr[k] = nodebuf[k];
2076                                 for (k = 0; k < 4; k++)
2077                                         c->u.node6.mask[k] = nodebuf[k+4];
2078                                 rc = context_read_and_validate(&c->context[0], p, fp);
2079                                 if (rc)
2080                                         goto out;
2081                                 break;
2082                         }
2083                         }
2084                 }
2085         }
2086         rc = 0;
2087 out:
2088         return rc;
2089 }
2090
2091 /*
2092  * Read the configuration data from a policy database binary
2093  * representation file into a policy database structure.
2094  */
2095 int policydb_read(struct policydb *p, void *fp)
2096 {
2097         struct role_allow *ra, *lra;
2098         struct role_trans *tr, *ltr;
2099         int i, j, rc;
2100         __le32 buf[4];
2101         u32 len, nprim, nel;
2102
2103         char *policydb_str;
2104         struct policydb_compat_info *info;
2105
2106         rc = policydb_init(p);
2107         if (rc)
2108                 return rc;
2109
2110         /* Read the magic number and string length. */
2111         rc = next_entry(buf, fp, sizeof(u32) * 2);
2112         if (rc)
2113                 goto bad;
2114
2115         rc = -EINVAL;
2116         if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2117                 printk(KERN_ERR "SELinux:  policydb magic number 0x%x does "
2118                        "not match expected magic number 0x%x\n",
2119                        le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2120                 goto bad;
2121         }
2122
2123         rc = -EINVAL;
2124         len = le32_to_cpu(buf[1]);
2125         if (len != strlen(POLICYDB_STRING)) {
2126                 printk(KERN_ERR "SELinux:  policydb string length %d does not "
2127                        "match expected length %Zu\n",
2128                        len, strlen(POLICYDB_STRING));
2129                 goto bad;
2130         }
2131
2132         rc = -ENOMEM;
2133         policydb_str = kmalloc(len + 1, GFP_KERNEL);
2134         if (!policydb_str) {
2135                 printk(KERN_ERR "SELinux:  unable to allocate memory for policydb "
2136                        "string of length %d\n", len);
2137                 goto bad;
2138         }
2139
2140         rc = next_entry(policydb_str, fp, len);
2141         if (rc) {
2142                 printk(KERN_ERR "SELinux:  truncated policydb string identifier\n");
2143                 kfree(policydb_str);
2144                 goto bad;
2145         }
2146
2147         rc = -EINVAL;
2148         policydb_str[len] = '\0';
2149         if (strcmp(policydb_str, POLICYDB_STRING)) {
2150                 printk(KERN_ERR "SELinux:  policydb string %s does not match "
2151                        "my string %s\n", policydb_str, POLICYDB_STRING);
2152                 kfree(policydb_str);
2153                 goto bad;
2154         }
2155         /* Done with policydb_str. */
2156         kfree(policydb_str);
2157         policydb_str = NULL;
2158
2159         /* Read the version and table sizes. */
2160         rc = next_entry(buf, fp, sizeof(u32)*4);
2161         if (rc)
2162                 goto bad;
2163
2164         rc = -EINVAL;
2165         p->policyvers = le32_to_cpu(buf[0]);
2166         if (p->policyvers < POLICYDB_VERSION_MIN ||
2167             p->policyvers > POLICYDB_VERSION_MAX) {
2168                 printk(KERN_ERR "SELinux:  policydb version %d does not match "
2169                        "my version range %d-%d\n",
2170                        le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2171                 goto bad;
2172         }
2173
2174         if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2175                 p->mls_enabled = 1;
2176
2177                 rc = -EINVAL;
2178                 if (p->policyvers < POLICYDB_VERSION_MLS) {
2179                         printk(KERN_ERR "SELinux: security policydb version %d "
2180                                 "(MLS) not backwards compatible\n",
2181                                 p->policyvers);
2182                         goto bad;
2183                 }
2184         }
2185         p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2186         p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2187
2188         if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2189                 rc = ebitmap_read(&p->policycaps, fp);
2190                 if (rc)
2191                         goto bad;
2192         }
2193
2194         if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2195                 rc = ebitmap_read(&p->permissive_map, fp);
2196                 if (rc)
2197                         goto bad;
2198         }
2199
2200         rc = -EINVAL;
2201         info = policydb_lookup_compat(p->policyvers);
2202         if (!info) {
2203                 printk(KERN_ERR "SELinux:  unable to find policy compat info "
2204                        "for version %d\n", p->policyvers);
2205                 goto bad;
2206         }
2207
2208         rc = -EINVAL;
2209         if (le32_to_cpu(buf[2]) != info->sym_num ||
2210                 le32_to_cpu(buf[3]) != info->ocon_num) {
2211                 printk(KERN_ERR "SELinux:  policydb table sizes (%d,%d) do "
2212                        "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2213                         le32_to_cpu(buf[3]),
2214                        info->sym_num, info->ocon_num);
2215                 goto bad;
2216         }
2217
2218         for (i = 0; i < info->sym_num; i++) {
2219                 rc = next_entry(buf, fp, sizeof(u32)*2);
2220                 if (rc)
2221                         goto bad;
2222                 nprim = le32_to_cpu(buf[0]);
2223                 nel = le32_to_cpu(buf[1]);
2224                 for (j = 0; j < nel; j++) {
2225                         rc = read_f[i](p, p->symtab[i].table, fp);
2226                         if (rc)
2227                                 goto bad;
2228                 }
2229
2230                 p->symtab[i].nprim = nprim;
2231         }
2232
2233         rc = avtab_read(&p->te_avtab, fp, p);
2234         if (rc)
2235                 goto bad;
2236
2237         if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2238                 rc = cond_read_list(p, fp);
2239                 if (rc)
2240                         goto bad;
2241         }
2242
2243         rc = next_entry(buf, fp, sizeof(u32));
2244         if (rc)
2245                 goto bad;
2246         nel = le32_to_cpu(buf[0]);
2247         ltr = NULL;
2248         for (i = 0; i < nel; i++) {
2249                 rc = -ENOMEM;
2250                 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2251                 if (!tr)
2252                         goto bad;
2253                 if (ltr)
2254                         ltr->next = tr;
2255                 else
2256                         p->role_tr = tr;
2257                 rc = next_entry(buf, fp, sizeof(u32)*3);
2258                 if (rc)
2259                         goto bad;
2260
2261                 rc = -EINVAL;
2262                 tr->role = le32_to_cpu(buf[0]);
2263                 tr->type = le32_to_cpu(buf[1]);
2264                 tr->new_role = le32_to_cpu(buf[2]);
2265                 if (!policydb_role_isvalid(p, tr->role) ||
2266                     !policydb_type_isvalid(p, tr->type) ||
2267                     !policydb_role_isvalid(p, tr->new_role))
2268                         goto bad;
2269                 ltr = tr;
2270         }
2271
2272         rc = next_entry(buf, fp, sizeof(u32));
2273         if (rc)
2274                 goto bad;
2275         nel = le32_to_cpu(buf[0]);
2276         lra = NULL;
2277         for (i = 0; i < nel; i++) {
2278                 rc = -ENOMEM;
2279                 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2280                 if (!ra)
2281                         goto bad;
2282                 if (lra)
2283                         lra->next = ra;
2284                 else
2285                         p->role_allow = ra;
2286                 rc = next_entry(buf, fp, sizeof(u32)*2);
2287                 if (rc)
2288                         goto bad;
2289
2290                 rc = -EINVAL;
2291                 ra->role = le32_to_cpu(buf[0]);
2292                 ra->new_role = le32_to_cpu(buf[1]);
2293                 if (!policydb_role_isvalid(p, ra->role) ||
2294                     !policydb_role_isvalid(p, ra->new_role))
2295                         goto bad;
2296                 lra = ra;
2297         }
2298
2299         rc = policydb_index_classes(p);
2300         if (rc)
2301                 goto bad;
2302
2303         rc = policydb_index_others(p);
2304         if (rc)
2305                 goto bad;
2306
2307         rc = -EINVAL;
2308         p->process_class = string_to_security_class(p, "process");
2309         if (!p->process_class)
2310                 goto bad;
2311
2312         rc = -EINVAL;
2313         p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2314         p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2315         if (!p->process_trans_perms)
2316                 goto bad;
2317
2318         rc = ocontext_read(p, info, fp);
2319         if (rc)
2320                 goto bad;
2321
2322         rc = genfs_read(p, fp);
2323         if (rc)
2324                 goto bad;
2325
2326         rc = range_read(p, fp);
2327         if (rc)
2328                 goto bad;
2329
2330         rc = -ENOMEM;
2331         p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2332                                                   p->p_types.nprim,
2333                                                   GFP_KERNEL | __GFP_ZERO);
2334         if (!p->type_attr_map_array)
2335                 goto bad;
2336
2337         /* preallocate so we don't have to worry about the put ever failing */
2338         rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2339                                  GFP_KERNEL | __GFP_ZERO);
2340         if (rc)
2341                 goto bad;
2342
2343         for (i = 0; i < p->p_types.nprim; i++) {
2344                 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2345
2346                 BUG_ON(!e);
2347                 ebitmap_init(e);
2348                 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2349                         rc = ebitmap_read(e, fp);
2350                         if (rc)
2351                                 goto bad;
2352                 }
2353                 /* add the type itself as the degenerate case */
2354                 rc = ebitmap_set_bit(e, i, 1);
2355                 if (rc)
2356                         goto bad;
2357         }
2358
2359         rc = policydb_bounds_sanity_check(p);
2360         if (rc)
2361                 goto bad;
2362
2363         rc = 0;
2364 out:
2365         return rc;
2366 bad:
2367         policydb_destroy(p);
2368         goto out;
2369 }
2370
2371 /*
2372  * Write a MLS level structure to a policydb binary
2373  * representation file.
2374  */
2375 static int mls_write_level(struct mls_level *l, void *fp)
2376 {
2377         __le32 buf[1];
2378         int rc;
2379
2380         buf[0] = cpu_to_le32(l->sens);
2381         rc = put_entry(buf, sizeof(u32), 1, fp);
2382         if (rc)
2383                 return rc;
2384
2385         rc = ebitmap_write(&l->cat, fp);
2386         if (rc)
2387                 return rc;
2388
2389         return 0;
2390 }
2391
2392 /*
2393  * Write a MLS range structure to a policydb binary
2394  * representation file.
2395  */
2396 static int mls_write_range_helper(struct mls_range *r, void *fp)
2397 {
2398         __le32 buf[3];
2399         size_t items;
2400         int rc, eq;
2401
2402         eq = mls_level_eq(&r->level[1], &r->level[0]);
2403
2404         if (eq)
2405                 items = 2;
2406         else
2407                 items = 3;
2408         buf[0] = cpu_to_le32(items-1);
2409         buf[1] = cpu_to_le32(r->level[0].sens);
2410         if (!eq)
2411                 buf[2] = cpu_to_le32(r->level[1].sens);
2412
2413         BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2414
2415         rc = put_entry(buf, sizeof(u32), items, fp);
2416         if (rc)
2417                 return rc;
2418
2419         rc = ebitmap_write(&r->level[0].cat, fp);
2420         if (rc)
2421                 return rc;
2422         if (!eq) {
2423                 rc = ebitmap_write(&r->level[1].cat, fp);
2424                 if (rc)
2425                         return rc;
2426         }
2427
2428         return 0;
2429 }
2430
2431 static int sens_write(void *vkey, void *datum, void *ptr)
2432 {
2433         char *key = vkey;
2434         struct level_datum *levdatum = datum;
2435         struct policy_data *pd = ptr;
2436         void *fp = pd->fp;
2437         __le32 buf[2];
2438         size_t len;
2439         int rc;
2440
2441         len = strlen(key);
2442         buf[0] = cpu_to_le32(len);
2443         buf[1] = cpu_to_le32(levdatum->isalias);
2444         rc = put_entry(buf, sizeof(u32), 2, fp);
2445         if (rc)
2446                 return rc;
2447
2448         rc = put_entry(key, 1, len, fp);
2449         if (rc)
2450                 return rc;
2451
2452         rc = mls_write_level(levdatum->level, fp);
2453         if (rc)
2454                 return rc;
2455
2456         return 0;
2457 }
2458
2459 static int cat_write(void *vkey, void *datum, void *ptr)
2460 {
2461         char *key = vkey;
2462         struct cat_datum *catdatum = datum;
2463         struct policy_data *pd = ptr;
2464         void *fp = pd->fp;
2465         __le32 buf[3];
2466         size_t len;
2467         int rc;
2468
2469         len = strlen(key);
2470         buf[0] = cpu_to_le32(len);
2471         buf[1] = cpu_to_le32(catdatum->value);
2472         buf[2] = cpu_to_le32(catdatum->isalias);
2473         rc = put_entry(buf, sizeof(u32), 3, fp);
2474         if (rc)
2475                 return rc;
2476
2477         rc = put_entry(key, 1, len, fp);
2478         if (rc)
2479                 return rc;
2480
2481         return 0;
2482 }
2483
2484 static int role_trans_write(struct role_trans *r, void *fp)
2485 {
2486         struct role_trans *tr;
2487         u32 buf[3];
2488         size_t nel;
2489         int rc;
2490
2491         nel = 0;
2492         for (tr = r; tr; tr = tr->next)
2493                 nel++;
2494         buf[0] = cpu_to_le32(nel);
2495         rc = put_entry(buf, sizeof(u32), 1, fp);
2496         if (rc)
2497                 return rc;
2498         for (tr = r; tr; tr = tr->next) {
2499                 buf[0] = cpu_to_le32(tr->role);
2500                 buf[1] = cpu_to_le32(tr->type);
2501                 buf[2] = cpu_to_le32(tr->new_role);
2502                 rc = put_entry(buf, sizeof(u32), 3, fp);
2503                 if (rc)
2504                         return rc;
2505         }
2506
2507         return 0;
2508 }
2509
2510 static int role_allow_write(struct role_allow *r, void *fp)
2511 {
2512         struct role_allow *ra;
2513         u32 buf[2];
2514         size_t nel;
2515         int rc;
2516
2517         nel = 0;
2518         for (ra = r; ra; ra = ra->next)
2519                 nel++;
2520         buf[0] = cpu_to_le32(nel);
2521         rc = put_entry(buf, sizeof(u32), 1, fp);
2522         if (rc)
2523                 return rc;
2524         for (ra = r; ra; ra = ra->next) {
2525                 buf[0] = cpu_to_le32(ra->role);
2526                 buf[1] = cpu_to_le32(ra->new_role);
2527                 rc = put_entry(buf, sizeof(u32), 2, fp);
2528                 if (rc)
2529                         return rc;
2530         }
2531         return 0;
2532 }
2533
2534 /*
2535  * Write a security context structure
2536  * to a policydb binary representation file.
2537  */
2538 static int context_write(struct policydb *p, struct context *c,
2539                          void *fp)
2540 {
2541         int rc;
2542         __le32 buf[3];
2543
2544         buf[0] = cpu_to_le32(c->user);
2545         buf[1] = cpu_to_le32(c->role);
2546         buf[2] = cpu_to_le32(c->type);
2547
2548         rc = put_entry(buf, sizeof(u32), 3, fp);
2549         if (rc)
2550                 return rc;
2551
2552         rc = mls_write_range_helper(&c->range, fp);
2553         if (rc)
2554                 return rc;
2555
2556         return 0;
2557 }
2558
2559 /*
2560  * The following *_write functions are used to
2561  * write the symbol data to a policy database
2562  * binary representation file.
2563  */
2564
2565 static int perm_write(void *vkey, void *datum, void *fp)
2566 {
2567         char *key = vkey;
2568         struct perm_datum *perdatum = datum;
2569         __le32 buf[2];
2570         size_t len;
2571         int rc;
2572
2573         len = strlen(key);
2574         buf[0] = cpu_to_le32(len);
2575         buf[1] = cpu_to_le32(perdatum->value);
2576         rc = put_entry(buf, sizeof(u32), 2, fp);
2577         if (rc)
2578                 return rc;
2579
2580         rc = put_entry(key, 1, len, fp);
2581         if (rc)
2582                 return rc;
2583
2584         return 0;
2585 }
2586
2587 static int common_write(void *vkey, void *datum, void *ptr)
2588 {
2589         char *key = vkey;
2590         struct common_datum *comdatum = datum;
2591         struct policy_data *pd = ptr;
2592         void *fp = pd->fp;
2593         __le32 buf[4];
2594         size_t len;
2595         int rc;
2596
2597         len = strlen(key);
2598         buf[0] = cpu_to_le32(len);
2599         buf[1] = cpu_to_le32(comdatum->value);
2600         buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2601         buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2602         rc = put_entry(buf, sizeof(u32), 4, fp);
2603         if (rc)
2604                 return rc;
2605
2606         rc = put_entry(key, 1, len, fp);
2607         if (rc)
2608                 return rc;
2609
2610         rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2611         if (rc)
2612                 return rc;
2613
2614         return 0;
2615 }
2616
2617 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2618                              void *fp)
2619 {
2620         struct constraint_node *c;
2621         struct constraint_expr *e;
2622         __le32 buf[3];
2623         u32 nel;
2624         int rc;
2625
2626         for (c = node; c; c = c->next) {
2627                 nel = 0;
2628                 for (e = c->expr; e; e = e->next)
2629                         nel++;
2630                 buf[0] = cpu_to_le32(c->permissions);
2631                 buf[1] = cpu_to_le32(nel);
2632                 rc = put_entry(buf, sizeof(u32), 2, fp);
2633                 if (rc)
2634                         return rc;
2635                 for (e = c->expr; e; e = e->next) {
2636                         buf[0] = cpu_to_le32(e->expr_type);
2637                         buf[1] = cpu_to_le32(e->attr);
2638                         buf[2] = cpu_to_le32(e->op);
2639                         rc = put_entry(buf, sizeof(u32), 3, fp);
2640                         if (rc)
2641                                 return rc;
2642
2643                         switch (e->expr_type) {
2644                         case CEXPR_NAMES:
2645                                 rc = ebitmap_write(&e->names, fp);
2646                                 if (rc)
2647                                         return rc;
2648                                 break;
2649                         default:
2650                                 break;
2651                         }
2652                 }
2653         }
2654
2655         return 0;
2656 }
2657
2658 static int class_write(void *vkey, void *datum, void *ptr)
2659 {
2660         char *key = vkey;
2661         struct class_datum *cladatum = datum;
2662         struct policy_data *pd = ptr;
2663         void *fp = pd->fp;
2664         struct policydb *p = pd->p;
2665         struct constraint_node *c;
2666         __le32 buf[6];
2667         u32 ncons;
2668         size_t len, len2;
2669         int rc;
2670
2671         len = strlen(key);
2672         if (cladatum->comkey)
2673                 len2 = strlen(cladatum->comkey);
2674         else
2675                 len2 = 0;
2676
2677         ncons = 0;
2678         for (c = cladatum->constraints; c; c = c->next)
2679                 ncons++;
2680
2681         buf[0] = cpu_to_le32(len);
2682         buf[1] = cpu_to_le32(len2);
2683         buf[2] = cpu_to_le32(cladatum->value);
2684         buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2685         if (cladatum->permissions.table)
2686                 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2687         else
2688                 buf[4] = 0;
2689         buf[5] = cpu_to_le32(ncons);
2690         rc = put_entry(buf, sizeof(u32), 6, fp);
2691         if (rc)
2692                 return rc;
2693
2694         rc = put_entry(key, 1, len, fp);
2695         if (rc)
2696                 return rc;
2697
2698         if (cladatum->comkey) {
2699                 rc = put_entry(cladatum->comkey, 1, len2, fp);
2700                 if (rc)
2701                         return rc;
2702         }
2703
2704         rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2705         if (rc)
2706                 return rc;
2707
2708         rc = write_cons_helper(p, cladatum->constraints, fp);
2709         if (rc)
2710                 return rc;
2711
2712         /* write out the validatetrans rule */
2713         ncons = 0;
2714         for (c = cladatum->validatetrans; c; c = c->next)
2715                 ncons++;
2716
2717         buf[0] = cpu_to_le32(ncons);
2718         rc = put_entry(buf, sizeof(u32), 1, fp);
2719         if (rc)
2720                 return rc;
2721
2722         rc = write_cons_helper(p, cladatum->validatetrans, fp);
2723         if (rc)
2724                 return rc;
2725
2726         return 0;
2727 }
2728
2729 static int role_write(void *vkey, void *datum, void *ptr)
2730 {
2731         char *key = vkey;
2732         struct role_datum *role = datum;
2733         struct policy_data *pd = ptr;
2734         void *fp = pd->fp;
2735         struct policydb *p = pd->p;
2736         __le32 buf[3];
2737         size_t items, len;
2738         int rc;
2739
2740         len = strlen(key);
2741         items = 0;
2742         buf[items++] = cpu_to_le32(len);
2743         buf[items++] = cpu_to_le32(role->value);
2744         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2745                 buf[items++] = cpu_to_le32(role->bounds);
2746
2747         BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2748
2749         rc = put_entry(buf, sizeof(u32), items, fp);
2750         if (rc)
2751                 return rc;
2752
2753         rc = put_entry(key, 1, len, fp);
2754         if (rc)
2755                 return rc;
2756
2757         rc = ebitmap_write(&role->dominates, fp);
2758         if (rc)
2759                 return rc;
2760
2761         rc = ebitmap_write(&role->types, fp);
2762         if (rc)
2763                 return rc;
2764
2765         return 0;
2766 }
2767
2768 static int type_write(void *vkey, void *datum, void *ptr)
2769 {
2770         char *key = vkey;
2771         struct type_datum *typdatum = datum;
2772         struct policy_data *pd = ptr;
2773         struct policydb *p = pd->p;
2774         void *fp = pd->fp;
2775         __le32 buf[4];
2776         int rc;
2777         size_t items, len;
2778
2779         len = strlen(key);
2780         items = 0;
2781         buf[items++] = cpu_to_le32(len);
2782         buf[items++] = cpu_to_le32(typdatum->value);
2783         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2784                 u32 properties = 0;
2785
2786                 if (typdatum->primary)
2787                         properties |= TYPEDATUM_PROPERTY_PRIMARY;
2788
2789                 if (typdatum->attribute)
2790                         properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2791
2792                 buf[items++] = cpu_to_le32(properties);
2793                 buf[items++] = cpu_to_le32(typdatum->bounds);
2794         } else {
2795                 buf[items++] = cpu_to_le32(typdatum->primary);
2796         }
2797         BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2798         rc = put_entry(buf, sizeof(u32), items, fp);
2799         if (rc)
2800                 return rc;
2801
2802         rc = put_entry(key, 1, len, fp);
2803         if (rc)
2804                 return rc;
2805
2806         return 0;
2807 }
2808
2809 static int user_write(void *vkey, void *datum, void *ptr)
2810 {
2811         char *key = vkey;
2812         struct user_datum *usrdatum = datum;
2813         struct policy_data *pd = ptr;
2814         struct policydb *p = pd->p;
2815         void *fp = pd->fp;
2816         __le32 buf[3];
2817         size_t items, len;
2818         int rc;
2819
2820         len = strlen(key);
2821         items = 0;
2822         buf[items++] = cpu_to_le32(len);
2823         buf[items++] = cpu_to_le32(usrdatum->value);
2824         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2825                 buf[items++] = cpu_to_le32(usrdatum->bounds);
2826         BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2827         rc = put_entry(buf, sizeof(u32), items, fp);
2828         if (rc)
2829                 return rc;
2830
2831         rc = put_entry(key, 1, len, fp);
2832         if (rc)
2833                 return rc;
2834
2835         rc = ebitmap_write(&usrdatum->roles, fp);
2836         if (rc)
2837                 return rc;
2838
2839         rc = mls_write_range_helper(&usrdatum->range, fp);
2840         if (rc)
2841                 return rc;
2842
2843         rc = mls_write_level(&usrdatum->dfltlevel, fp);
2844         if (rc)
2845                 return rc;
2846
2847         return 0;
2848 }
2849
2850 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2851                                 void *datap) =
2852 {
2853         common_write,
2854         class_write,
2855         role_write,
2856         type_write,
2857         user_write,
2858         cond_write_bool,
2859         sens_write,
2860         cat_write,
2861 };
2862
2863 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2864                           void *fp)
2865 {
2866         unsigned int i, j, rc;
2867         size_t nel, len;
2868         __le32 buf[3];
2869         u32 nodebuf[8];
2870         struct ocontext *c;
2871         for (i = 0; i < info->ocon_num; i++) {
2872                 nel = 0;
2873                 for (c = p->ocontexts[i]; c; c = c->next)
2874                         nel++;
2875                 buf[0] = cpu_to_le32(nel);
2876                 rc = put_entry(buf, sizeof(u32), 1, fp);
2877                 if (rc)
2878                         return rc;
2879                 for (c = p->ocontexts[i]; c; c = c->next) {
2880                         switch (i) {
2881                         case OCON_ISID:
2882                                 buf[0] = cpu_to_le32(c->sid[0]);
2883                                 rc = put_entry(buf, sizeof(u32), 1, fp);
2884                                 if (rc)
2885                                         return rc;
2886                                 rc = context_write(p, &c->context[0], fp);
2887                                 if (rc)
2888                                         return rc;
2889                                 break;
2890                         case OCON_FS:
2891                         case OCON_NETIF:
2892                                 len = strlen(c->u.name);
2893                                 buf[0] = cpu_to_le32(len);
2894                                 rc = put_entry(buf, sizeof(u32), 1, fp);
2895                                 if (rc)
2896                                         return rc;
2897                                 rc = put_entry(c->u.name, 1, len, fp);
2898                                 if (rc)
2899                                         return rc;
2900                                 rc = context_write(p, &c->context[0], fp);
2901                                 if (rc)
2902                                         return rc;
2903                                 rc = context_write(p, &c->context[1], fp);
2904                                 if (rc)
2905                                         return rc;
2906                                 break;
2907                         case OCON_PORT:
2908                                 buf[0] = cpu_to_le32(c->u.port.protocol);
2909                                 buf[1] = cpu_to_le32(c->u.port.low_port);
2910                                 buf[2] = cpu_to_le32(c->u.port.high_port);
2911                                 rc = put_entry(buf, sizeof(u32), 3, fp);
2912                                 if (rc)
2913                                         return rc;
2914                                 rc = context_write(p, &c->context[0], fp);
2915                                 if (rc)
2916                                         return rc;
2917                                 break;
2918                         case OCON_NODE:
2919                                 nodebuf[0] = c->u.node.addr; /* network order */
2920                                 nodebuf[1] = c->u.node.mask; /* network order */
2921                                 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2922                                 if (rc)
2923                                         return rc;
2924                                 rc = context_write(p, &c->context[0], fp);
2925                                 if (rc)
2926                                         return rc;
2927                                 break;
2928                         case OCON_FSUSE:
2929                                 buf[0] = cpu_to_le32(c->v.behavior);
2930                                 len = strlen(c->u.name);
2931                                 buf[1] = cpu_to_le32(len);
2932                                 rc = put_entry(buf, sizeof(u32), 2, fp);
2933                                 if (rc)
2934                                         return rc;
2935                                 rc = put_entry(c->u.name, 1, len, fp);
2936                                 if (rc)
2937                                         return rc;
2938                                 rc = context_write(p, &c->context[0], fp);
2939                                 if (rc)
2940                                         return rc;
2941                                 break;
2942                         case OCON_NODE6:
2943                                 for (j = 0; j < 4; j++)
2944                                         nodebuf[j] = c->u.node6.addr[j]; /* network order */
2945                                 for (j = 0; j < 4; j++)
2946                                         nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2947                                 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2948                                 if (rc)
2949                                         return rc;
2950                                 rc = context_write(p, &c->context[0], fp);
2951                                 if (rc)
2952                                         return rc;
2953                                 break;
2954                         }
2955                 }
2956         }
2957         return 0;
2958 }
2959
2960 static int genfs_write(struct policydb *p, void *fp)
2961 {
2962         struct genfs *genfs;
2963         struct ocontext *c;
2964         size_t len;
2965         __le32 buf[1];
2966         int rc;
2967
2968         len = 0;
2969         for (genfs = p->genfs; genfs; genfs = genfs->next)
2970                 len++;
2971         buf[0] = cpu_to_le32(len);
2972         rc = put_entry(buf, sizeof(u32), 1, fp);
2973         if (rc)
2974                 return rc;
2975         for (genfs = p->genfs; genfs; genfs = genfs->next) {
2976                 len = strlen(genfs->fstype);
2977                 buf[0] = cpu_to_le32(len);
2978                 rc = put_entry(buf, sizeof(u32), 1, fp);
2979                 if (rc)
2980                         return rc;
2981                 rc = put_entry(genfs->fstype, 1, len, fp);
2982                 if (rc)
2983                         return rc;
2984                 len = 0;
2985                 for (c = genfs->head; c; c = c->next)
2986                         len++;
2987                 buf[0] = cpu_to_le32(len);
2988                 rc = put_entry(buf, sizeof(u32), 1, fp);
2989                 if (rc)
2990                         return rc;
2991                 for (c = genfs->head; c; c = c->next) {
2992                         len = strlen(c->u.name);
2993                         buf[0] = cpu_to_le32(len);
2994                         rc = put_entry(buf, sizeof(u32), 1, fp);
2995                         if (rc)
2996                                 return rc;
2997                         rc = put_entry(c->u.name, 1, len, fp);
2998                         if (rc)
2999                                 return rc;
3000                         buf[0] = cpu_to_le32(c->v.sclass);
3001                         rc = put_entry(buf, sizeof(u32), 1, fp);
3002                         if (rc)
3003                                 return rc;
3004                         rc = context_write(p, &c->context[0], fp);
3005                         if (rc)
3006                                 return rc;
3007                 }
3008         }
3009         return 0;
3010 }
3011
3012 static int range_count(void *key, void *data, void *ptr)
3013 {
3014         int *cnt = ptr;
3015         *cnt = *cnt + 1;
3016
3017         return 0;
3018 }
3019
3020 static int range_write_helper(void *key, void *data, void *ptr)
3021 {
3022         __le32 buf[2];
3023         struct range_trans *rt = key;
3024         struct mls_range *r = data;
3025         struct policy_data *pd = ptr;
3026         void *fp = pd->fp;
3027         struct policydb *p = pd->p;
3028         int rc;
3029
3030         buf[0] = cpu_to_le32(rt->source_type);
3031         buf[1] = cpu_to_le32(rt->target_type);
3032         rc = put_entry(buf, sizeof(u32), 2, fp);
3033         if (rc)
3034                 return rc;
3035         if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3036                 buf[0] = cpu_to_le32(rt->target_class);
3037                 rc = put_entry(buf, sizeof(u32), 1, fp);
3038                 if (rc)
3039                         return rc;
3040         }
3041         rc = mls_write_range_helper(r, fp);
3042         if (rc)
3043                 return rc;
3044
3045         return 0;
3046 }
3047
3048 static int range_write(struct policydb *p, void *fp)
3049 {
3050         size_t nel;
3051         __le32 buf[1];
3052         int rc;
3053         struct policy_data pd;
3054
3055         pd.p = p;
3056         pd.fp = fp;
3057
3058         /* count the number of entries in the hashtab */
3059         nel = 0;
3060         rc = hashtab_map(p->range_tr, range_count, &nel);
3061         if (rc)
3062                 return rc;
3063
3064         buf[0] = cpu_to_le32(nel);
3065         rc = put_entry(buf, sizeof(u32), 1, fp);
3066         if (rc)
3067                 return rc;
3068
3069         /* actually write all of the entries */
3070         rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3071         if (rc)
3072                 return rc;
3073
3074         return 0;
3075 }
3076
3077 /*
3078  * Write the configuration data in a policy database
3079  * structure to a policy database binary representation
3080  * file.
3081  */
3082 int policydb_write(struct policydb *p, void *fp)
3083 {
3084         unsigned int i, num_syms;
3085         int rc;
3086         __le32 buf[4];
3087         u32 config;
3088         size_t len;
3089         struct policydb_compat_info *info;
3090
3091         /*
3092          * refuse to write policy older than compressed avtab
3093          * to simplify the writer.  There are other tests dropped
3094          * since we assume this throughout the writer code.  Be
3095          * careful if you ever try to remove this restriction
3096          */
3097         if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3098                 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3099                        "  Because it is less than version %d\n", p->policyvers,
3100                        POLICYDB_VERSION_AVTAB);
3101                 return -EINVAL;
3102         }
3103
3104         config = 0;
3105         if (p->mls_enabled)
3106                 config |= POLICYDB_CONFIG_MLS;
3107
3108         if (p->reject_unknown)
3109                 config |= REJECT_UNKNOWN;
3110         if (p->allow_unknown)
3111                 config |= ALLOW_UNKNOWN;
3112
3113         /* Write the magic number and string identifiers. */
3114         buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3115         len = strlen(POLICYDB_STRING);
3116         buf[1] = cpu_to_le32(len);
3117         rc = put_entry(buf, sizeof(u32), 2, fp);
3118         if (rc)
3119                 return rc;
3120         rc = put_entry(POLICYDB_STRING, 1, len, fp);
3121         if (rc)
3122                 return rc;
3123
3124         /* Write the version, config, and table sizes. */
3125         info = policydb_lookup_compat(p->policyvers);
3126         if (!info) {
3127                 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3128                     "version %d", p->policyvers);
3129                 return -EINVAL;
3130         }
3131
3132         buf[0] = cpu_to_le32(p->policyvers);
3133         buf[1] = cpu_to_le32(config);
3134         buf[2] = cpu_to_le32(info->sym_num);
3135         buf[3] = cpu_to_le32(info->ocon_num);
3136
3137         rc = put_entry(buf, sizeof(u32), 4, fp);
3138         if (rc)
3139                 return rc;
3140
3141         if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3142                 rc = ebitmap_write(&p->policycaps, fp);
3143                 if (rc)
3144                         return rc;
3145         }
3146
3147         if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3148                 rc = ebitmap_write(&p->permissive_map, fp);
3149                 if (rc)
3150                         return rc;
3151         }
3152
3153         num_syms = info->sym_num;
3154         for (i = 0; i < num_syms; i++) {
3155                 struct policy_data pd;
3156
3157                 pd.fp = fp;
3158                 pd.p = p;
3159
3160                 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3161                 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3162
3163                 rc = put_entry(buf, sizeof(u32), 2, fp);
3164                 if (rc)
3165                         return rc;
3166                 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3167                 if (rc)
3168                         return rc;
3169         }
3170
3171         rc = avtab_write(p, &p->te_avtab, fp);
3172         if (rc)
3173                 return rc;
3174
3175         rc = cond_write_list(p, p->cond_list, fp);
3176         if (rc)
3177                 return rc;
3178
3179         rc = role_trans_write(p->role_tr, fp);
3180         if (rc)
3181                 return rc;
3182
3183         rc = role_allow_write(p->role_allow, fp);
3184         if (rc)
3185                 return rc;
3186
3187         rc = ocontext_write(p, info, fp);
3188         if (rc)
3189                 return rc;
3190
3191         rc = genfs_write(p, fp);
3192         if (rc)
3193                 return rc;
3194
3195         rc = range_write(p, fp);
3196         if (rc)
3197                 return rc;
3198
3199         for (i = 0; i < p->p_types.nprim; i++) {
3200                 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3201
3202                 BUG_ON(!e);
3203                 rc = ebitmap_write(e, fp);
3204                 if (rc)
3205                         return rc;
3206         }
3207
3208         return 0;
3209 }