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