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