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