GNU Linux-libre 4.9.304-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(KERN_CONT ", %d sens, %d cats", p->p_levels.nprim,
535                        p->p_cats.nprim);
536         printk(KERN_CONT "\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                 kzalloc(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                 kzalloc(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                 kzalloc(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 (!role || !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         if ((len == 0) || (len == (u32)-1))
1103                 return -EINVAL;
1104
1105         str = kmalloc(len + 1, flags | __GFP_NOWARN);
1106         if (!str)
1107                 return -ENOMEM;
1108
1109         /* it's expected the caller should free the str */
1110         *strp = str;
1111
1112         rc = next_entry(str, fp, len);
1113         if (rc)
1114                 return rc;
1115
1116         str[len] = '\0';
1117         return 0;
1118 }
1119
1120 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1121 {
1122         char *key = NULL;
1123         struct perm_datum *perdatum;
1124         int rc;
1125         __le32 buf[2];
1126         u32 len;
1127
1128         rc = -ENOMEM;
1129         perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1130         if (!perdatum)
1131                 goto bad;
1132
1133         rc = next_entry(buf, fp, sizeof buf);
1134         if (rc)
1135                 goto bad;
1136
1137         len = le32_to_cpu(buf[0]);
1138         perdatum->value = le32_to_cpu(buf[1]);
1139
1140         rc = str_read(&key, GFP_KERNEL, fp, len);
1141         if (rc)
1142                 goto bad;
1143
1144         rc = hashtab_insert(h, key, perdatum);
1145         if (rc)
1146                 goto bad;
1147
1148         return 0;
1149 bad:
1150         perm_destroy(key, perdatum, NULL);
1151         return rc;
1152 }
1153
1154 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1155 {
1156         char *key = NULL;
1157         struct common_datum *comdatum;
1158         __le32 buf[4];
1159         u32 len, nel;
1160         int i, rc;
1161
1162         rc = -ENOMEM;
1163         comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1164         if (!comdatum)
1165                 goto bad;
1166
1167         rc = next_entry(buf, fp, sizeof buf);
1168         if (rc)
1169                 goto bad;
1170
1171         len = le32_to_cpu(buf[0]);
1172         comdatum->value = le32_to_cpu(buf[1]);
1173
1174         rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1175         if (rc)
1176                 goto bad;
1177         comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1178         nel = le32_to_cpu(buf[3]);
1179
1180         rc = str_read(&key, GFP_KERNEL, fp, len);
1181         if (rc)
1182                 goto bad;
1183
1184         for (i = 0; i < nel; i++) {
1185                 rc = perm_read(p, comdatum->permissions.table, fp);
1186                 if (rc)
1187                         goto bad;
1188         }
1189
1190         rc = hashtab_insert(h, key, comdatum);
1191         if (rc)
1192                 goto bad;
1193         return 0;
1194 bad:
1195         common_destroy(key, comdatum, NULL);
1196         return rc;
1197 }
1198
1199 static void type_set_init(struct type_set *t)
1200 {
1201         ebitmap_init(&t->types);
1202         ebitmap_init(&t->negset);
1203 }
1204
1205 static int type_set_read(struct type_set *t, void *fp)
1206 {
1207         __le32 buf[1];
1208         int rc;
1209
1210         if (ebitmap_read(&t->types, fp))
1211                 return -EINVAL;
1212         if (ebitmap_read(&t->negset, fp))
1213                 return -EINVAL;
1214
1215         rc = next_entry(buf, fp, sizeof(u32));
1216         if (rc < 0)
1217                 return -EINVAL;
1218         t->flags = le32_to_cpu(buf[0]);
1219
1220         return 0;
1221 }
1222
1223
1224 static int read_cons_helper(struct policydb *p,
1225                                 struct constraint_node **nodep,
1226                                 int ncons, int allowxtarget, void *fp)
1227 {
1228         struct constraint_node *c, *lc;
1229         struct constraint_expr *e, *le;
1230         __le32 buf[3];
1231         u32 nexpr;
1232         int rc, i, j, depth;
1233
1234         lc = NULL;
1235         for (i = 0; i < ncons; i++) {
1236                 c = kzalloc(sizeof(*c), GFP_KERNEL);
1237                 if (!c)
1238                         return -ENOMEM;
1239
1240                 if (lc)
1241                         lc->next = c;
1242                 else
1243                         *nodep = c;
1244
1245                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1246                 if (rc)
1247                         return rc;
1248                 c->permissions = le32_to_cpu(buf[0]);
1249                 nexpr = le32_to_cpu(buf[1]);
1250                 le = NULL;
1251                 depth = -1;
1252                 for (j = 0; j < nexpr; j++) {
1253                         e = kzalloc(sizeof(*e), GFP_KERNEL);
1254                         if (!e)
1255                                 return -ENOMEM;
1256
1257                         if (le)
1258                                 le->next = e;
1259                         else
1260                                 c->expr = e;
1261
1262                         rc = next_entry(buf, fp, (sizeof(u32) * 3));
1263                         if (rc)
1264                                 return rc;
1265                         e->expr_type = le32_to_cpu(buf[0]);
1266                         e->attr = le32_to_cpu(buf[1]);
1267                         e->op = le32_to_cpu(buf[2]);
1268
1269                         switch (e->expr_type) {
1270                         case CEXPR_NOT:
1271                                 if (depth < 0)
1272                                         return -EINVAL;
1273                                 break;
1274                         case CEXPR_AND:
1275                         case CEXPR_OR:
1276                                 if (depth < 1)
1277                                         return -EINVAL;
1278                                 depth--;
1279                                 break;
1280                         case CEXPR_ATTR:
1281                                 if (depth == (CEXPR_MAXDEPTH - 1))
1282                                         return -EINVAL;
1283                                 depth++;
1284                                 break;
1285                         case CEXPR_NAMES:
1286                                 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1287                                         return -EINVAL;
1288                                 if (depth == (CEXPR_MAXDEPTH - 1))
1289                                         return -EINVAL;
1290                                 depth++;
1291                                 rc = ebitmap_read(&e->names, fp);
1292                                 if (rc)
1293                                         return rc;
1294                                 if (p->policyvers >=
1295                                         POLICYDB_VERSION_CONSTRAINT_NAMES) {
1296                                                 e->type_names = kzalloc(sizeof
1297                                                 (*e->type_names),
1298                                                 GFP_KERNEL);
1299                                         if (!e->type_names)
1300                                                 return -ENOMEM;
1301                                         type_set_init(e->type_names);
1302                                         rc = type_set_read(e->type_names, fp);
1303                                         if (rc)
1304                                                 return rc;
1305                                 }
1306                                 break;
1307                         default:
1308                                 return -EINVAL;
1309                         }
1310                         le = e;
1311                 }
1312                 if (depth != 0)
1313                         return -EINVAL;
1314                 lc = c;
1315         }
1316
1317         return 0;
1318 }
1319
1320 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1321 {
1322         char *key = NULL;
1323         struct class_datum *cladatum;
1324         __le32 buf[6];
1325         u32 len, len2, ncons, nel;
1326         int i, rc;
1327
1328         rc = -ENOMEM;
1329         cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1330         if (!cladatum)
1331                 goto bad;
1332
1333         rc = next_entry(buf, fp, sizeof(u32)*6);
1334         if (rc)
1335                 goto bad;
1336
1337         len = le32_to_cpu(buf[0]);
1338         len2 = le32_to_cpu(buf[1]);
1339         cladatum->value = le32_to_cpu(buf[2]);
1340
1341         rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1342         if (rc)
1343                 goto bad;
1344         cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1345         nel = le32_to_cpu(buf[4]);
1346
1347         ncons = le32_to_cpu(buf[5]);
1348
1349         rc = str_read(&key, GFP_KERNEL, fp, len);
1350         if (rc)
1351                 goto bad;
1352
1353         if (len2) {
1354                 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1355                 if (rc)
1356                         goto bad;
1357
1358                 rc = -EINVAL;
1359                 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1360                 if (!cladatum->comdatum) {
1361                         printk(KERN_ERR "SELinux:  unknown common %s\n", cladatum->comkey);
1362                         goto bad;
1363                 }
1364         }
1365         for (i = 0; i < nel; i++) {
1366                 rc = perm_read(p, cladatum->permissions.table, fp);
1367                 if (rc)
1368                         goto bad;
1369         }
1370
1371         rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1372         if (rc)
1373                 goto bad;
1374
1375         if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1376                 /* grab the validatetrans rules */
1377                 rc = next_entry(buf, fp, sizeof(u32));
1378                 if (rc)
1379                         goto bad;
1380                 ncons = le32_to_cpu(buf[0]);
1381                 rc = read_cons_helper(p, &cladatum->validatetrans,
1382                                 ncons, 1, fp);
1383                 if (rc)
1384                         goto bad;
1385         }
1386
1387         if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1388                 rc = next_entry(buf, fp, sizeof(u32) * 3);
1389                 if (rc)
1390                         goto bad;
1391
1392                 cladatum->default_user = le32_to_cpu(buf[0]);
1393                 cladatum->default_role = le32_to_cpu(buf[1]);
1394                 cladatum->default_range = le32_to_cpu(buf[2]);
1395         }
1396
1397         if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1398                 rc = next_entry(buf, fp, sizeof(u32) * 1);
1399                 if (rc)
1400                         goto bad;
1401                 cladatum->default_type = le32_to_cpu(buf[0]);
1402         }
1403
1404         rc = hashtab_insert(h, key, cladatum);
1405         if (rc)
1406                 goto bad;
1407
1408         return 0;
1409 bad:
1410         cls_destroy(key, cladatum, NULL);
1411         return rc;
1412 }
1413
1414 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1415 {
1416         char *key = NULL;
1417         struct role_datum *role;
1418         int rc, to_read = 2;
1419         __le32 buf[3];
1420         u32 len;
1421
1422         rc = -ENOMEM;
1423         role = kzalloc(sizeof(*role), GFP_KERNEL);
1424         if (!role)
1425                 goto bad;
1426
1427         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428                 to_read = 3;
1429
1430         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1431         if (rc)
1432                 goto bad;
1433
1434         len = le32_to_cpu(buf[0]);
1435         role->value = le32_to_cpu(buf[1]);
1436         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1437                 role->bounds = le32_to_cpu(buf[2]);
1438
1439         rc = str_read(&key, GFP_KERNEL, fp, len);
1440         if (rc)
1441                 goto bad;
1442
1443         rc = ebitmap_read(&role->dominates, fp);
1444         if (rc)
1445                 goto bad;
1446
1447         rc = ebitmap_read(&role->types, fp);
1448         if (rc)
1449                 goto bad;
1450
1451         if (strcmp(key, OBJECT_R) == 0) {
1452                 rc = -EINVAL;
1453                 if (role->value != OBJECT_R_VAL) {
1454                         printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1455                                OBJECT_R, role->value);
1456                         goto bad;
1457                 }
1458                 rc = 0;
1459                 goto bad;
1460         }
1461
1462         rc = hashtab_insert(h, key, role);
1463         if (rc)
1464                 goto bad;
1465         return 0;
1466 bad:
1467         role_destroy(key, role, NULL);
1468         return rc;
1469 }
1470
1471 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1472 {
1473         char *key = NULL;
1474         struct type_datum *typdatum;
1475         int rc, to_read = 3;
1476         __le32 buf[4];
1477         u32 len;
1478
1479         rc = -ENOMEM;
1480         typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1481         if (!typdatum)
1482                 goto bad;
1483
1484         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1485                 to_read = 4;
1486
1487         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1488         if (rc)
1489                 goto bad;
1490
1491         len = le32_to_cpu(buf[0]);
1492         typdatum->value = le32_to_cpu(buf[1]);
1493         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1494                 u32 prop = le32_to_cpu(buf[2]);
1495
1496                 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1497                         typdatum->primary = 1;
1498                 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1499                         typdatum->attribute = 1;
1500
1501                 typdatum->bounds = le32_to_cpu(buf[3]);
1502         } else {
1503                 typdatum->primary = le32_to_cpu(buf[2]);
1504         }
1505
1506         rc = str_read(&key, GFP_KERNEL, fp, len);
1507         if (rc)
1508                 goto bad;
1509
1510         rc = hashtab_insert(h, key, typdatum);
1511         if (rc)
1512                 goto bad;
1513         return 0;
1514 bad:
1515         type_destroy(key, typdatum, NULL);
1516         return rc;
1517 }
1518
1519
1520 /*
1521  * Read a MLS level structure from a policydb binary
1522  * representation file.
1523  */
1524 static int mls_read_level(struct mls_level *lp, void *fp)
1525 {
1526         __le32 buf[1];
1527         int rc;
1528
1529         memset(lp, 0, sizeof(*lp));
1530
1531         rc = next_entry(buf, fp, sizeof buf);
1532         if (rc) {
1533                 printk(KERN_ERR "SELinux: mls: truncated level\n");
1534                 return rc;
1535         }
1536         lp->sens = le32_to_cpu(buf[0]);
1537
1538         rc = ebitmap_read(&lp->cat, fp);
1539         if (rc) {
1540                 printk(KERN_ERR "SELinux: mls:  error reading level categories\n");
1541                 return rc;
1542         }
1543         return 0;
1544 }
1545
1546 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1547 {
1548         char *key = NULL;
1549         struct user_datum *usrdatum;
1550         int rc, to_read = 2;
1551         __le32 buf[3];
1552         u32 len;
1553
1554         rc = -ENOMEM;
1555         usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1556         if (!usrdatum)
1557                 goto bad;
1558
1559         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1560                 to_read = 3;
1561
1562         rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1563         if (rc)
1564                 goto bad;
1565
1566         len = le32_to_cpu(buf[0]);
1567         usrdatum->value = le32_to_cpu(buf[1]);
1568         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1569                 usrdatum->bounds = le32_to_cpu(buf[2]);
1570
1571         rc = str_read(&key, GFP_KERNEL, fp, len);
1572         if (rc)
1573                 goto bad;
1574
1575         rc = ebitmap_read(&usrdatum->roles, fp);
1576         if (rc)
1577                 goto bad;
1578
1579         if (p->policyvers >= POLICYDB_VERSION_MLS) {
1580                 rc = mls_read_range_helper(&usrdatum->range, fp);
1581                 if (rc)
1582                         goto bad;
1583                 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1584                 if (rc)
1585                         goto bad;
1586         }
1587
1588         rc = hashtab_insert(h, key, usrdatum);
1589         if (rc)
1590                 goto bad;
1591         return 0;
1592 bad:
1593         user_destroy(key, usrdatum, NULL);
1594         return rc;
1595 }
1596
1597 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1598 {
1599         char *key = NULL;
1600         struct level_datum *levdatum;
1601         int rc;
1602         __le32 buf[2];
1603         u32 len;
1604
1605         rc = -ENOMEM;
1606         levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1607         if (!levdatum)
1608                 goto bad;
1609
1610         rc = next_entry(buf, fp, sizeof buf);
1611         if (rc)
1612                 goto bad;
1613
1614         len = le32_to_cpu(buf[0]);
1615         levdatum->isalias = le32_to_cpu(buf[1]);
1616
1617         rc = str_read(&key, GFP_ATOMIC, fp, len);
1618         if (rc)
1619                 goto bad;
1620
1621         rc = -ENOMEM;
1622         levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1623         if (!levdatum->level)
1624                 goto bad;
1625
1626         rc = mls_read_level(levdatum->level, fp);
1627         if (rc)
1628                 goto bad;
1629
1630         rc = hashtab_insert(h, key, levdatum);
1631         if (rc)
1632                 goto bad;
1633         return 0;
1634 bad:
1635         sens_destroy(key, levdatum, NULL);
1636         return rc;
1637 }
1638
1639 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1640 {
1641         char *key = NULL;
1642         struct cat_datum *catdatum;
1643         int rc;
1644         __le32 buf[3];
1645         u32 len;
1646
1647         rc = -ENOMEM;
1648         catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1649         if (!catdatum)
1650                 goto bad;
1651
1652         rc = next_entry(buf, fp, sizeof buf);
1653         if (rc)
1654                 goto bad;
1655
1656         len = le32_to_cpu(buf[0]);
1657         catdatum->value = le32_to_cpu(buf[1]);
1658         catdatum->isalias = le32_to_cpu(buf[2]);
1659
1660         rc = str_read(&key, GFP_ATOMIC, fp, len);
1661         if (rc)
1662                 goto bad;
1663
1664         rc = hashtab_insert(h, key, catdatum);
1665         if (rc)
1666                 goto bad;
1667         return 0;
1668 bad:
1669         cat_destroy(key, catdatum, NULL);
1670         return rc;
1671 }
1672
1673 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1674 {
1675         common_read,
1676         class_read,
1677         role_read,
1678         type_read,
1679         user_read,
1680         cond_read_bool,
1681         sens_read,
1682         cat_read,
1683 };
1684
1685 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1686 {
1687         struct user_datum *upper, *user;
1688         struct policydb *p = datap;
1689         int depth = 0;
1690
1691         upper = user = datum;
1692         while (upper->bounds) {
1693                 struct ebitmap_node *node;
1694                 unsigned long bit;
1695
1696                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1697                         printk(KERN_ERR "SELinux: user %s: "
1698                                "too deep or looped boundary",
1699                                (char *) key);
1700                         return -EINVAL;
1701                 }
1702
1703                 upper = p->user_val_to_struct[upper->bounds - 1];
1704                 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1705                         if (ebitmap_get_bit(&upper->roles, bit))
1706                                 continue;
1707
1708                         printk(KERN_ERR
1709                                "SELinux: boundary violated policy: "
1710                                "user=%s role=%s bounds=%s\n",
1711                                sym_name(p, SYM_USERS, user->value - 1),
1712                                sym_name(p, SYM_ROLES, bit),
1713                                sym_name(p, SYM_USERS, upper->value - 1));
1714
1715                         return -EINVAL;
1716                 }
1717         }
1718
1719         return 0;
1720 }
1721
1722 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1723 {
1724         struct role_datum *upper, *role;
1725         struct policydb *p = datap;
1726         int depth = 0;
1727
1728         upper = role = datum;
1729         while (upper->bounds) {
1730                 struct ebitmap_node *node;
1731                 unsigned long bit;
1732
1733                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1734                         printk(KERN_ERR "SELinux: role %s: "
1735                                "too deep or looped bounds\n",
1736                                (char *) key);
1737                         return -EINVAL;
1738                 }
1739
1740                 upper = p->role_val_to_struct[upper->bounds - 1];
1741                 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1742                         if (ebitmap_get_bit(&upper->types, bit))
1743                                 continue;
1744
1745                         printk(KERN_ERR
1746                                "SELinux: boundary violated policy: "
1747                                "role=%s type=%s bounds=%s\n",
1748                                sym_name(p, SYM_ROLES, role->value - 1),
1749                                sym_name(p, SYM_TYPES, bit),
1750                                sym_name(p, SYM_ROLES, upper->value - 1));
1751
1752                         return -EINVAL;
1753                 }
1754         }
1755
1756         return 0;
1757 }
1758
1759 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1760 {
1761         struct type_datum *upper;
1762         struct policydb *p = datap;
1763         int depth = 0;
1764
1765         upper = datum;
1766         while (upper->bounds) {
1767                 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1768                         printk(KERN_ERR "SELinux: type %s: "
1769                                "too deep or looped boundary\n",
1770                                (char *) key);
1771                         return -EINVAL;
1772                 }
1773
1774                 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1775                                            upper->bounds - 1);
1776                 BUG_ON(!upper);
1777
1778                 if (upper->attribute) {
1779                         printk(KERN_ERR "SELinux: type %s: "
1780                                "bounded by attribute %s",
1781                                (char *) key,
1782                                sym_name(p, SYM_TYPES, upper->value - 1));
1783                         return -EINVAL;
1784                 }
1785         }
1786
1787         return 0;
1788 }
1789
1790 static int policydb_bounds_sanity_check(struct policydb *p)
1791 {
1792         int rc;
1793
1794         if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1795                 return 0;
1796
1797         rc = hashtab_map(p->p_users.table,
1798                          user_bounds_sanity_check, p);
1799         if (rc)
1800                 return rc;
1801
1802         rc = hashtab_map(p->p_roles.table,
1803                          role_bounds_sanity_check, p);
1804         if (rc)
1805                 return rc;
1806
1807         rc = hashtab_map(p->p_types.table,
1808                          type_bounds_sanity_check, p);
1809         if (rc)
1810                 return rc;
1811
1812         return 0;
1813 }
1814
1815 u16 string_to_security_class(struct policydb *p, const char *name)
1816 {
1817         struct class_datum *cladatum;
1818
1819         cladatum = hashtab_search(p->p_classes.table, name);
1820         if (!cladatum)
1821                 return 0;
1822
1823         return cladatum->value;
1824 }
1825
1826 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1827 {
1828         struct class_datum *cladatum;
1829         struct perm_datum *perdatum = NULL;
1830         struct common_datum *comdatum;
1831
1832         if (!tclass || tclass > p->p_classes.nprim)
1833                 return 0;
1834
1835         cladatum = p->class_val_to_struct[tclass-1];
1836         comdatum = cladatum->comdatum;
1837         if (comdatum)
1838                 perdatum = hashtab_search(comdatum->permissions.table,
1839                                           name);
1840         if (!perdatum)
1841                 perdatum = hashtab_search(cladatum->permissions.table,
1842                                           name);
1843         if (!perdatum)
1844                 return 0;
1845
1846         return 1U << (perdatum->value-1);
1847 }
1848
1849 static int range_read(struct policydb *p, void *fp)
1850 {
1851         struct range_trans *rt = NULL;
1852         struct mls_range *r = NULL;
1853         int i, rc;
1854         __le32 buf[2];
1855         u32 nel;
1856
1857         if (p->policyvers < POLICYDB_VERSION_MLS)
1858                 return 0;
1859
1860         rc = next_entry(buf, fp, sizeof(u32));
1861         if (rc)
1862                 goto out;
1863
1864         nel = le32_to_cpu(buf[0]);
1865         for (i = 0; i < nel; i++) {
1866                 rc = -ENOMEM;
1867                 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1868                 if (!rt)
1869                         goto out;
1870
1871                 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1872                 if (rc)
1873                         goto out;
1874
1875                 rt->source_type = le32_to_cpu(buf[0]);
1876                 rt->target_type = le32_to_cpu(buf[1]);
1877                 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1878                         rc = next_entry(buf, fp, sizeof(u32));
1879                         if (rc)
1880                                 goto out;
1881                         rt->target_class = le32_to_cpu(buf[0]);
1882                 } else
1883                         rt->target_class = p->process_class;
1884
1885                 rc = -EINVAL;
1886                 if (!policydb_type_isvalid(p, rt->source_type) ||
1887                     !policydb_type_isvalid(p, rt->target_type) ||
1888                     !policydb_class_isvalid(p, rt->target_class))
1889                         goto out;
1890
1891                 rc = -ENOMEM;
1892                 r = kzalloc(sizeof(*r), GFP_KERNEL);
1893                 if (!r)
1894                         goto out;
1895
1896                 rc = mls_read_range_helper(r, fp);
1897                 if (rc)
1898                         goto out;
1899
1900                 rc = -EINVAL;
1901                 if (!mls_range_isvalid(p, r)) {
1902                         printk(KERN_WARNING "SELinux:  rangetrans:  invalid range\n");
1903                         goto out;
1904                 }
1905
1906                 rc = hashtab_insert(p->range_tr, rt, r);
1907                 if (rc)
1908                         goto out;
1909
1910                 rt = NULL;
1911                 r = NULL;
1912         }
1913         hash_eval(p->range_tr, "rangetr");
1914         rc = 0;
1915 out:
1916         kfree(rt);
1917         kfree(r);
1918         return rc;
1919 }
1920
1921 static int filename_trans_read(struct policydb *p, void *fp)
1922 {
1923         struct filename_trans *ft;
1924         struct filename_trans_datum *otype;
1925         char *name;
1926         u32 nel, len;
1927         __le32 buf[4];
1928         int rc, i;
1929
1930         if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1931                 return 0;
1932
1933         rc = next_entry(buf, fp, sizeof(u32));
1934         if (rc)
1935                 return rc;
1936         nel = le32_to_cpu(buf[0]);
1937
1938         for (i = 0; i < nel; i++) {
1939                 ft = NULL;
1940                 otype = NULL;
1941                 name = NULL;
1942
1943                 rc = -ENOMEM;
1944                 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1945                 if (!ft)
1946                         goto out;
1947
1948                 rc = -ENOMEM;
1949                 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1950                 if (!otype)
1951                         goto out;
1952
1953                 /* length of the path component string */
1954                 rc = next_entry(buf, fp, sizeof(u32));
1955                 if (rc)
1956                         goto out;
1957                 len = le32_to_cpu(buf[0]);
1958
1959                 /* path component string */
1960                 rc = str_read(&name, GFP_KERNEL, fp, len);
1961                 if (rc)
1962                         goto out;
1963
1964                 ft->name = name;
1965
1966                 rc = next_entry(buf, fp, sizeof(u32) * 4);
1967                 if (rc)
1968                         goto out;
1969
1970                 ft->stype = le32_to_cpu(buf[0]);
1971                 ft->ttype = le32_to_cpu(buf[1]);
1972                 ft->tclass = le32_to_cpu(buf[2]);
1973
1974                 otype->otype = le32_to_cpu(buf[3]);
1975
1976                 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1977                 if (rc)
1978                         goto out;
1979
1980                 rc = hashtab_insert(p->filename_trans, ft, otype);
1981                 if (rc) {
1982                         /*
1983                          * Do not return -EEXIST to the caller, or the system
1984                          * will not boot.
1985                          */
1986                         if (rc != -EEXIST)
1987                                 goto out;
1988                         /* But free memory to avoid memory leak. */
1989                         kfree(ft);
1990                         kfree(name);
1991                         kfree(otype);
1992                 }
1993         }
1994         hash_eval(p->filename_trans, "filenametr");
1995         return 0;
1996 out:
1997         kfree(ft);
1998         kfree(name);
1999         kfree(otype);
2000
2001         return rc;
2002 }
2003
2004 static int genfs_read(struct policydb *p, void *fp)
2005 {
2006         int i, j, rc;
2007         u32 nel, nel2, len, len2;
2008         __le32 buf[1];
2009         struct ocontext *l, *c;
2010         struct ocontext *newc = NULL;
2011         struct genfs *genfs_p, *genfs;
2012         struct genfs *newgenfs = NULL;
2013
2014         rc = next_entry(buf, fp, sizeof(u32));
2015         if (rc)
2016                 goto out;
2017         nel = le32_to_cpu(buf[0]);
2018
2019         for (i = 0; i < nel; i++) {
2020                 rc = next_entry(buf, fp, sizeof(u32));
2021                 if (rc)
2022                         goto out;
2023                 len = le32_to_cpu(buf[0]);
2024
2025                 rc = -ENOMEM;
2026                 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2027                 if (!newgenfs)
2028                         goto out;
2029
2030                 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2031                 if (rc)
2032                         goto out;
2033
2034                 for (genfs_p = NULL, genfs = p->genfs; genfs;
2035                      genfs_p = genfs, genfs = genfs->next) {
2036                         rc = -EINVAL;
2037                         if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2038                                 printk(KERN_ERR "SELinux:  dup genfs fstype %s\n",
2039                                        newgenfs->fstype);
2040                                 goto out;
2041                         }
2042                         if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2043                                 break;
2044                 }
2045                 newgenfs->next = genfs;
2046                 if (genfs_p)
2047                         genfs_p->next = newgenfs;
2048                 else
2049                         p->genfs = newgenfs;
2050                 genfs = newgenfs;
2051                 newgenfs = NULL;
2052
2053                 rc = next_entry(buf, fp, sizeof(u32));
2054                 if (rc)
2055                         goto out;
2056
2057                 nel2 = le32_to_cpu(buf[0]);
2058                 for (j = 0; j < nel2; j++) {
2059                         rc = next_entry(buf, fp, sizeof(u32));
2060                         if (rc)
2061                                 goto out;
2062                         len = le32_to_cpu(buf[0]);
2063
2064                         rc = -ENOMEM;
2065                         newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2066                         if (!newc)
2067                                 goto out;
2068
2069                         rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2070                         if (rc)
2071                                 goto out;
2072
2073                         rc = next_entry(buf, fp, sizeof(u32));
2074                         if (rc)
2075                                 goto out;
2076
2077                         newc->v.sclass = le32_to_cpu(buf[0]);
2078                         rc = context_read_and_validate(&newc->context[0], p, fp);
2079                         if (rc)
2080                                 goto out;
2081
2082                         for (l = NULL, c = genfs->head; c;
2083                              l = c, c = c->next) {
2084                                 rc = -EINVAL;
2085                                 if (!strcmp(newc->u.name, c->u.name) &&
2086                                     (!c->v.sclass || !newc->v.sclass ||
2087                                      newc->v.sclass == c->v.sclass)) {
2088                                         printk(KERN_ERR "SELinux:  dup genfs entry (%s,%s)\n",
2089                                                genfs->fstype, c->u.name);
2090                                         goto out;
2091                                 }
2092                                 len = strlen(newc->u.name);
2093                                 len2 = strlen(c->u.name);
2094                                 if (len > len2)
2095                                         break;
2096                         }
2097
2098                         newc->next = c;
2099                         if (l)
2100                                 l->next = newc;
2101                         else
2102                                 genfs->head = newc;
2103                         newc = NULL;
2104                 }
2105         }
2106         rc = 0;
2107 out:
2108         if (newgenfs)
2109                 kfree(newgenfs->fstype);
2110         kfree(newgenfs);
2111         ocontext_destroy(newc, OCON_FSUSE);
2112
2113         return rc;
2114 }
2115
2116 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2117                          void *fp)
2118 {
2119         int i, j, rc;
2120         u32 nel, len;
2121         __le32 buf[3];
2122         struct ocontext *l, *c;
2123         u32 nodebuf[8];
2124
2125         for (i = 0; i < info->ocon_num; i++) {
2126                 rc = next_entry(buf, fp, sizeof(u32));
2127                 if (rc)
2128                         goto out;
2129                 nel = le32_to_cpu(buf[0]);
2130
2131                 l = NULL;
2132                 for (j = 0; j < nel; j++) {
2133                         rc = -ENOMEM;
2134                         c = kzalloc(sizeof(*c), GFP_KERNEL);
2135                         if (!c)
2136                                 goto out;
2137                         if (l)
2138                                 l->next = c;
2139                         else
2140                                 p->ocontexts[i] = c;
2141                         l = c;
2142
2143                         switch (i) {
2144                         case OCON_ISID:
2145                                 rc = next_entry(buf, fp, sizeof(u32));
2146                                 if (rc)
2147                                         goto out;
2148
2149                                 c->sid[0] = le32_to_cpu(buf[0]);
2150                                 rc = context_read_and_validate(&c->context[0], p, fp);
2151                                 if (rc)
2152                                         goto out;
2153                                 break;
2154                         case OCON_FS:
2155                         case OCON_NETIF:
2156                                 rc = next_entry(buf, fp, sizeof(u32));
2157                                 if (rc)
2158                                         goto out;
2159                                 len = le32_to_cpu(buf[0]);
2160
2161                                 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2162                                 if (rc)
2163                                         goto out;
2164
2165                                 rc = context_read_and_validate(&c->context[0], p, fp);
2166                                 if (rc)
2167                                         goto out;
2168                                 rc = context_read_and_validate(&c->context[1], p, fp);
2169                                 if (rc)
2170                                         goto out;
2171                                 break;
2172                         case OCON_PORT:
2173                                 rc = next_entry(buf, fp, sizeof(u32)*3);
2174                                 if (rc)
2175                                         goto out;
2176                                 c->u.port.protocol = le32_to_cpu(buf[0]);
2177                                 c->u.port.low_port = le32_to_cpu(buf[1]);
2178                                 c->u.port.high_port = le32_to_cpu(buf[2]);
2179                                 rc = context_read_and_validate(&c->context[0], p, fp);
2180                                 if (rc)
2181                                         goto out;
2182                                 break;
2183                         case OCON_NODE:
2184                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2185                                 if (rc)
2186                                         goto out;
2187                                 c->u.node.addr = nodebuf[0]; /* network order */
2188                                 c->u.node.mask = nodebuf[1]; /* network order */
2189                                 rc = context_read_and_validate(&c->context[0], p, fp);
2190                                 if (rc)
2191                                         goto out;
2192                                 break;
2193                         case OCON_FSUSE:
2194                                 rc = next_entry(buf, fp, sizeof(u32)*2);
2195                                 if (rc)
2196                                         goto out;
2197
2198                                 rc = -EINVAL;
2199                                 c->v.behavior = le32_to_cpu(buf[0]);
2200                                 /* Determined at runtime, not in policy DB. */
2201                                 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2202                                         goto out;
2203                                 if (c->v.behavior > SECURITY_FS_USE_MAX)
2204                                         goto out;
2205
2206                                 len = le32_to_cpu(buf[1]);
2207                                 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2208                                 if (rc)
2209                                         goto out;
2210
2211                                 rc = context_read_and_validate(&c->context[0], p, fp);
2212                                 if (rc)
2213                                         goto out;
2214                                 break;
2215                         case OCON_NODE6: {
2216                                 int k;
2217
2218                                 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2219                                 if (rc)
2220                                         goto out;
2221                                 for (k = 0; k < 4; k++)
2222                                         c->u.node6.addr[k] = nodebuf[k];
2223                                 for (k = 0; k < 4; k++)
2224                                         c->u.node6.mask[k] = nodebuf[k+4];
2225                                 rc = context_read_and_validate(&c->context[0], p, fp);
2226                                 if (rc)
2227                                         goto out;
2228                                 break;
2229                         }
2230                         }
2231                 }
2232         }
2233         rc = 0;
2234 out:
2235         return rc;
2236 }
2237
2238 /*
2239  * Read the configuration data from a policy database binary
2240  * representation file into a policy database structure.
2241  */
2242 int policydb_read(struct policydb *p, void *fp)
2243 {
2244         struct role_allow *ra, *lra;
2245         struct role_trans *tr, *ltr;
2246         int i, j, rc;
2247         __le32 buf[4];
2248         u32 len, nprim, nel;
2249
2250         char *policydb_str;
2251         struct policydb_compat_info *info;
2252
2253         rc = policydb_init(p);
2254         if (rc)
2255                 return rc;
2256
2257         /* Read the magic number and string length. */
2258         rc = next_entry(buf, fp, sizeof(u32) * 2);
2259         if (rc)
2260                 goto bad;
2261
2262         rc = -EINVAL;
2263         if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2264                 printk(KERN_ERR "SELinux:  policydb magic number 0x%x does "
2265                        "not match expected magic number 0x%x\n",
2266                        le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2267                 goto bad;
2268         }
2269
2270         rc = -EINVAL;
2271         len = le32_to_cpu(buf[1]);
2272         if (len != strlen(POLICYDB_STRING)) {
2273                 printk(KERN_ERR "SELinux:  policydb string length %d does not "
2274                        "match expected length %Zu\n",
2275                        len, strlen(POLICYDB_STRING));
2276                 goto bad;
2277         }
2278
2279         rc = -ENOMEM;
2280         policydb_str = kmalloc(len + 1, GFP_KERNEL);
2281         if (!policydb_str) {
2282                 printk(KERN_ERR "SELinux:  unable to allocate memory for policydb "
2283                        "string of length %d\n", len);
2284                 goto bad;
2285         }
2286
2287         rc = next_entry(policydb_str, fp, len);
2288         if (rc) {
2289                 printk(KERN_ERR "SELinux:  truncated policydb string identifier\n");
2290                 kfree(policydb_str);
2291                 goto bad;
2292         }
2293
2294         rc = -EINVAL;
2295         policydb_str[len] = '\0';
2296         if (strcmp(policydb_str, POLICYDB_STRING)) {
2297                 printk(KERN_ERR "SELinux:  policydb string %s does not match "
2298                        "my string %s\n", policydb_str, POLICYDB_STRING);
2299                 kfree(policydb_str);
2300                 goto bad;
2301         }
2302         /* Done with policydb_str. */
2303         kfree(policydb_str);
2304         policydb_str = NULL;
2305
2306         /* Read the version and table sizes. */
2307         rc = next_entry(buf, fp, sizeof(u32)*4);
2308         if (rc)
2309                 goto bad;
2310
2311         rc = -EINVAL;
2312         p->policyvers = le32_to_cpu(buf[0]);
2313         if (p->policyvers < POLICYDB_VERSION_MIN ||
2314             p->policyvers > POLICYDB_VERSION_MAX) {
2315                 printk(KERN_ERR "SELinux:  policydb version %d does not match "
2316                        "my version range %d-%d\n",
2317                        le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2318                 goto bad;
2319         }
2320
2321         if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2322                 p->mls_enabled = 1;
2323
2324                 rc = -EINVAL;
2325                 if (p->policyvers < POLICYDB_VERSION_MLS) {
2326                         printk(KERN_ERR "SELinux: security policydb version %d "
2327                                 "(MLS) not backwards compatible\n",
2328                                 p->policyvers);
2329                         goto bad;
2330                 }
2331         }
2332         p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2333         p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2334
2335         if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2336                 rc = ebitmap_read(&p->policycaps, fp);
2337                 if (rc)
2338                         goto bad;
2339         }
2340
2341         if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2342                 rc = ebitmap_read(&p->permissive_map, fp);
2343                 if (rc)
2344                         goto bad;
2345         }
2346
2347         rc = -EINVAL;
2348         info = policydb_lookup_compat(p->policyvers);
2349         if (!info) {
2350                 printk(KERN_ERR "SELinux:  unable to find policy compat info "
2351                        "for version %d\n", p->policyvers);
2352                 goto bad;
2353         }
2354
2355         rc = -EINVAL;
2356         if (le32_to_cpu(buf[2]) != info->sym_num ||
2357                 le32_to_cpu(buf[3]) != info->ocon_num) {
2358                 printk(KERN_ERR "SELinux:  policydb table sizes (%d,%d) do "
2359                        "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2360                         le32_to_cpu(buf[3]),
2361                        info->sym_num, info->ocon_num);
2362                 goto bad;
2363         }
2364
2365         for (i = 0; i < info->sym_num; i++) {
2366                 rc = next_entry(buf, fp, sizeof(u32)*2);
2367                 if (rc)
2368                         goto bad;
2369                 nprim = le32_to_cpu(buf[0]);
2370                 nel = le32_to_cpu(buf[1]);
2371                 for (j = 0; j < nel; j++) {
2372                         rc = read_f[i](p, p->symtab[i].table, fp);
2373                         if (rc)
2374                                 goto bad;
2375                 }
2376
2377                 p->symtab[i].nprim = nprim;
2378         }
2379
2380         rc = -EINVAL;
2381         p->process_class = string_to_security_class(p, "process");
2382         if (!p->process_class)
2383                 goto bad;
2384
2385         rc = avtab_read(&p->te_avtab, fp, p);
2386         if (rc)
2387                 goto bad;
2388
2389         if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2390                 rc = cond_read_list(p, fp);
2391                 if (rc)
2392                         goto bad;
2393         }
2394
2395         rc = next_entry(buf, fp, sizeof(u32));
2396         if (rc)
2397                 goto bad;
2398         nel = le32_to_cpu(buf[0]);
2399         ltr = NULL;
2400         for (i = 0; i < nel; i++) {
2401                 rc = -ENOMEM;
2402                 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2403                 if (!tr)
2404                         goto bad;
2405                 if (ltr)
2406                         ltr->next = tr;
2407                 else
2408                         p->role_tr = tr;
2409                 rc = next_entry(buf, fp, sizeof(u32)*3);
2410                 if (rc)
2411                         goto bad;
2412
2413                 rc = -EINVAL;
2414                 tr->role = le32_to_cpu(buf[0]);
2415                 tr->type = le32_to_cpu(buf[1]);
2416                 tr->new_role = le32_to_cpu(buf[2]);
2417                 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2418                         rc = next_entry(buf, fp, sizeof(u32));
2419                         if (rc)
2420                                 goto bad;
2421                         tr->tclass = le32_to_cpu(buf[0]);
2422                 } else
2423                         tr->tclass = p->process_class;
2424
2425                 rc = -EINVAL;
2426                 if (!policydb_role_isvalid(p, tr->role) ||
2427                     !policydb_type_isvalid(p, tr->type) ||
2428                     !policydb_class_isvalid(p, tr->tclass) ||
2429                     !policydb_role_isvalid(p, tr->new_role))
2430                         goto bad;
2431                 ltr = tr;
2432         }
2433
2434         rc = next_entry(buf, fp, sizeof(u32));
2435         if (rc)
2436                 goto bad;
2437         nel = le32_to_cpu(buf[0]);
2438         lra = NULL;
2439         for (i = 0; i < nel; i++) {
2440                 rc = -ENOMEM;
2441                 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2442                 if (!ra)
2443                         goto bad;
2444                 if (lra)
2445                         lra->next = ra;
2446                 else
2447                         p->role_allow = ra;
2448                 rc = next_entry(buf, fp, sizeof(u32)*2);
2449                 if (rc)
2450                         goto bad;
2451
2452                 rc = -EINVAL;
2453                 ra->role = le32_to_cpu(buf[0]);
2454                 ra->new_role = le32_to_cpu(buf[1]);
2455                 if (!policydb_role_isvalid(p, ra->role) ||
2456                     !policydb_role_isvalid(p, ra->new_role))
2457                         goto bad;
2458                 lra = ra;
2459         }
2460
2461         rc = filename_trans_read(p, fp);
2462         if (rc)
2463                 goto bad;
2464
2465         rc = policydb_index(p);
2466         if (rc)
2467                 goto bad;
2468
2469         rc = -EINVAL;
2470         p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2471         p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2472         if (!p->process_trans_perms)
2473                 goto bad;
2474
2475         rc = ocontext_read(p, info, fp);
2476         if (rc)
2477                 goto bad;
2478
2479         rc = genfs_read(p, fp);
2480         if (rc)
2481                 goto bad;
2482
2483         rc = range_read(p, fp);
2484         if (rc)
2485                 goto bad;
2486
2487         rc = -ENOMEM;
2488         p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2489                                                   p->p_types.nprim,
2490                                                   GFP_KERNEL | __GFP_ZERO);
2491         if (!p->type_attr_map_array)
2492                 goto bad;
2493
2494         /* preallocate so we don't have to worry about the put ever failing */
2495         rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2496                                  GFP_KERNEL | __GFP_ZERO);
2497         if (rc)
2498                 goto bad;
2499
2500         for (i = 0; i < p->p_types.nprim; i++) {
2501                 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2502
2503                 BUG_ON(!e);
2504                 ebitmap_init(e);
2505                 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2506                         rc = ebitmap_read(e, fp);
2507                         if (rc)
2508                                 goto bad;
2509                 }
2510                 /* add the type itself as the degenerate case */
2511                 rc = ebitmap_set_bit(e, i, 1);
2512                 if (rc)
2513                         goto bad;
2514         }
2515
2516         rc = policydb_bounds_sanity_check(p);
2517         if (rc)
2518                 goto bad;
2519
2520         rc = 0;
2521 out:
2522         return rc;
2523 bad:
2524         policydb_destroy(p);
2525         goto out;
2526 }
2527
2528 /*
2529  * Write a MLS level structure to a policydb binary
2530  * representation file.
2531  */
2532 static int mls_write_level(struct mls_level *l, void *fp)
2533 {
2534         __le32 buf[1];
2535         int rc;
2536
2537         buf[0] = cpu_to_le32(l->sens);
2538         rc = put_entry(buf, sizeof(u32), 1, fp);
2539         if (rc)
2540                 return rc;
2541
2542         rc = ebitmap_write(&l->cat, fp);
2543         if (rc)
2544                 return rc;
2545
2546         return 0;
2547 }
2548
2549 /*
2550  * Write a MLS range structure to a policydb binary
2551  * representation file.
2552  */
2553 static int mls_write_range_helper(struct mls_range *r, void *fp)
2554 {
2555         __le32 buf[3];
2556         size_t items;
2557         int rc, eq;
2558
2559         eq = mls_level_eq(&r->level[1], &r->level[0]);
2560
2561         if (eq)
2562                 items = 2;
2563         else
2564                 items = 3;
2565         buf[0] = cpu_to_le32(items-1);
2566         buf[1] = cpu_to_le32(r->level[0].sens);
2567         if (!eq)
2568                 buf[2] = cpu_to_le32(r->level[1].sens);
2569
2570         BUG_ON(items > ARRAY_SIZE(buf));
2571
2572         rc = put_entry(buf, sizeof(u32), items, fp);
2573         if (rc)
2574                 return rc;
2575
2576         rc = ebitmap_write(&r->level[0].cat, fp);
2577         if (rc)
2578                 return rc;
2579         if (!eq) {
2580                 rc = ebitmap_write(&r->level[1].cat, fp);
2581                 if (rc)
2582                         return rc;
2583         }
2584
2585         return 0;
2586 }
2587
2588 static int sens_write(void *vkey, void *datum, void *ptr)
2589 {
2590         char *key = vkey;
2591         struct level_datum *levdatum = datum;
2592         struct policy_data *pd = ptr;
2593         void *fp = pd->fp;
2594         __le32 buf[2];
2595         size_t len;
2596         int rc;
2597
2598         len = strlen(key);
2599         buf[0] = cpu_to_le32(len);
2600         buf[1] = cpu_to_le32(levdatum->isalias);
2601         rc = put_entry(buf, sizeof(u32), 2, fp);
2602         if (rc)
2603                 return rc;
2604
2605         rc = put_entry(key, 1, len, fp);
2606         if (rc)
2607                 return rc;
2608
2609         rc = mls_write_level(levdatum->level, fp);
2610         if (rc)
2611                 return rc;
2612
2613         return 0;
2614 }
2615
2616 static int cat_write(void *vkey, void *datum, void *ptr)
2617 {
2618         char *key = vkey;
2619         struct cat_datum *catdatum = datum;
2620         struct policy_data *pd = ptr;
2621         void *fp = pd->fp;
2622         __le32 buf[3];
2623         size_t len;
2624         int rc;
2625
2626         len = strlen(key);
2627         buf[0] = cpu_to_le32(len);
2628         buf[1] = cpu_to_le32(catdatum->value);
2629         buf[2] = cpu_to_le32(catdatum->isalias);
2630         rc = put_entry(buf, sizeof(u32), 3, fp);
2631         if (rc)
2632                 return rc;
2633
2634         rc = put_entry(key, 1, len, fp);
2635         if (rc)
2636                 return rc;
2637
2638         return 0;
2639 }
2640
2641 static int role_trans_write(struct policydb *p, void *fp)
2642 {
2643         struct role_trans *r = p->role_tr;
2644         struct role_trans *tr;
2645         u32 buf[3];
2646         size_t nel;
2647         int rc;
2648
2649         nel = 0;
2650         for (tr = r; tr; tr = tr->next)
2651                 nel++;
2652         buf[0] = cpu_to_le32(nel);
2653         rc = put_entry(buf, sizeof(u32), 1, fp);
2654         if (rc)
2655                 return rc;
2656         for (tr = r; tr; tr = tr->next) {
2657                 buf[0] = cpu_to_le32(tr->role);
2658                 buf[1] = cpu_to_le32(tr->type);
2659                 buf[2] = cpu_to_le32(tr->new_role);
2660                 rc = put_entry(buf, sizeof(u32), 3, fp);
2661                 if (rc)
2662                         return rc;
2663                 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2664                         buf[0] = cpu_to_le32(tr->tclass);
2665                         rc = put_entry(buf, sizeof(u32), 1, fp);
2666                         if (rc)
2667                                 return rc;
2668                 }
2669         }
2670
2671         return 0;
2672 }
2673
2674 static int role_allow_write(struct role_allow *r, void *fp)
2675 {
2676         struct role_allow *ra;
2677         u32 buf[2];
2678         size_t nel;
2679         int rc;
2680
2681         nel = 0;
2682         for (ra = r; ra; ra = ra->next)
2683                 nel++;
2684         buf[0] = cpu_to_le32(nel);
2685         rc = put_entry(buf, sizeof(u32), 1, fp);
2686         if (rc)
2687                 return rc;
2688         for (ra = r; ra; ra = ra->next) {
2689                 buf[0] = cpu_to_le32(ra->role);
2690                 buf[1] = cpu_to_le32(ra->new_role);
2691                 rc = put_entry(buf, sizeof(u32), 2, fp);
2692                 if (rc)
2693                         return rc;
2694         }
2695         return 0;
2696 }
2697
2698 /*
2699  * Write a security context structure
2700  * to a policydb binary representation file.
2701  */
2702 static int context_write(struct policydb *p, struct context *c,
2703                          void *fp)
2704 {
2705         int rc;
2706         __le32 buf[3];
2707
2708         buf[0] = cpu_to_le32(c->user);
2709         buf[1] = cpu_to_le32(c->role);
2710         buf[2] = cpu_to_le32(c->type);
2711
2712         rc = put_entry(buf, sizeof(u32), 3, fp);
2713         if (rc)
2714                 return rc;
2715
2716         rc = mls_write_range_helper(&c->range, fp);
2717         if (rc)
2718                 return rc;
2719
2720         return 0;
2721 }
2722
2723 /*
2724  * The following *_write functions are used to
2725  * write the symbol data to a policy database
2726  * binary representation file.
2727  */
2728
2729 static int perm_write(void *vkey, void *datum, void *fp)
2730 {
2731         char *key = vkey;
2732         struct perm_datum *perdatum = datum;
2733         __le32 buf[2];
2734         size_t len;
2735         int rc;
2736
2737         len = strlen(key);
2738         buf[0] = cpu_to_le32(len);
2739         buf[1] = cpu_to_le32(perdatum->value);
2740         rc = put_entry(buf, sizeof(u32), 2, fp);
2741         if (rc)
2742                 return rc;
2743
2744         rc = put_entry(key, 1, len, fp);
2745         if (rc)
2746                 return rc;
2747
2748         return 0;
2749 }
2750
2751 static int common_write(void *vkey, void *datum, void *ptr)
2752 {
2753         char *key = vkey;
2754         struct common_datum *comdatum = datum;
2755         struct policy_data *pd = ptr;
2756         void *fp = pd->fp;
2757         __le32 buf[4];
2758         size_t len;
2759         int rc;
2760
2761         len = strlen(key);
2762         buf[0] = cpu_to_le32(len);
2763         buf[1] = cpu_to_le32(comdatum->value);
2764         buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2765         buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2766         rc = put_entry(buf, sizeof(u32), 4, fp);
2767         if (rc)
2768                 return rc;
2769
2770         rc = put_entry(key, 1, len, fp);
2771         if (rc)
2772                 return rc;
2773
2774         rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2775         if (rc)
2776                 return rc;
2777
2778         return 0;
2779 }
2780
2781 static int type_set_write(struct type_set *t, void *fp)
2782 {
2783         int rc;
2784         __le32 buf[1];
2785
2786         if (ebitmap_write(&t->types, fp))
2787                 return -EINVAL;
2788         if (ebitmap_write(&t->negset, fp))
2789                 return -EINVAL;
2790
2791         buf[0] = cpu_to_le32(t->flags);
2792         rc = put_entry(buf, sizeof(u32), 1, fp);
2793         if (rc)
2794                 return -EINVAL;
2795
2796         return 0;
2797 }
2798
2799 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2800                              void *fp)
2801 {
2802         struct constraint_node *c;
2803         struct constraint_expr *e;
2804         __le32 buf[3];
2805         u32 nel;
2806         int rc;
2807
2808         for (c = node; c; c = c->next) {
2809                 nel = 0;
2810                 for (e = c->expr; e; e = e->next)
2811                         nel++;
2812                 buf[0] = cpu_to_le32(c->permissions);
2813                 buf[1] = cpu_to_le32(nel);
2814                 rc = put_entry(buf, sizeof(u32), 2, fp);
2815                 if (rc)
2816                         return rc;
2817                 for (e = c->expr; e; e = e->next) {
2818                         buf[0] = cpu_to_le32(e->expr_type);
2819                         buf[1] = cpu_to_le32(e->attr);
2820                         buf[2] = cpu_to_le32(e->op);
2821                         rc = put_entry(buf, sizeof(u32), 3, fp);
2822                         if (rc)
2823                                 return rc;
2824
2825                         switch (e->expr_type) {
2826                         case CEXPR_NAMES:
2827                                 rc = ebitmap_write(&e->names, fp);
2828                                 if (rc)
2829                                         return rc;
2830                                 if (p->policyvers >=
2831                                         POLICYDB_VERSION_CONSTRAINT_NAMES) {
2832                                         rc = type_set_write(e->type_names, fp);
2833                                         if (rc)
2834                                                 return rc;
2835                                 }
2836                                 break;
2837                         default:
2838                                 break;
2839                         }
2840                 }
2841         }
2842
2843         return 0;
2844 }
2845
2846 static int class_write(void *vkey, void *datum, void *ptr)
2847 {
2848         char *key = vkey;
2849         struct class_datum *cladatum = datum;
2850         struct policy_data *pd = ptr;
2851         void *fp = pd->fp;
2852         struct policydb *p = pd->p;
2853         struct constraint_node *c;
2854         __le32 buf[6];
2855         u32 ncons;
2856         size_t len, len2;
2857         int rc;
2858
2859         len = strlen(key);
2860         if (cladatum->comkey)
2861                 len2 = strlen(cladatum->comkey);
2862         else
2863                 len2 = 0;
2864
2865         ncons = 0;
2866         for (c = cladatum->constraints; c; c = c->next)
2867                 ncons++;
2868
2869         buf[0] = cpu_to_le32(len);
2870         buf[1] = cpu_to_le32(len2);
2871         buf[2] = cpu_to_le32(cladatum->value);
2872         buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2873         if (cladatum->permissions.table)
2874                 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2875         else
2876                 buf[4] = 0;
2877         buf[5] = cpu_to_le32(ncons);
2878         rc = put_entry(buf, sizeof(u32), 6, fp);
2879         if (rc)
2880                 return rc;
2881
2882         rc = put_entry(key, 1, len, fp);
2883         if (rc)
2884                 return rc;
2885
2886         if (cladatum->comkey) {
2887                 rc = put_entry(cladatum->comkey, 1, len2, fp);
2888                 if (rc)
2889                         return rc;
2890         }
2891
2892         rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2893         if (rc)
2894                 return rc;
2895
2896         rc = write_cons_helper(p, cladatum->constraints, fp);
2897         if (rc)
2898                 return rc;
2899
2900         /* write out the validatetrans rule */
2901         ncons = 0;
2902         for (c = cladatum->validatetrans; c; c = c->next)
2903                 ncons++;
2904
2905         buf[0] = cpu_to_le32(ncons);
2906         rc = put_entry(buf, sizeof(u32), 1, fp);
2907         if (rc)
2908                 return rc;
2909
2910         rc = write_cons_helper(p, cladatum->validatetrans, fp);
2911         if (rc)
2912                 return rc;
2913
2914         if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2915                 buf[0] = cpu_to_le32(cladatum->default_user);
2916                 buf[1] = cpu_to_le32(cladatum->default_role);
2917                 buf[2] = cpu_to_le32(cladatum->default_range);
2918
2919                 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2920                 if (rc)
2921                         return rc;
2922         }
2923
2924         if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2925                 buf[0] = cpu_to_le32(cladatum->default_type);
2926                 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2927                 if (rc)
2928                         return rc;
2929         }
2930
2931         return 0;
2932 }
2933
2934 static int role_write(void *vkey, void *datum, void *ptr)
2935 {
2936         char *key = vkey;
2937         struct role_datum *role = datum;
2938         struct policy_data *pd = ptr;
2939         void *fp = pd->fp;
2940         struct policydb *p = pd->p;
2941         __le32 buf[3];
2942         size_t items, len;
2943         int rc;
2944
2945         len = strlen(key);
2946         items = 0;
2947         buf[items++] = cpu_to_le32(len);
2948         buf[items++] = cpu_to_le32(role->value);
2949         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2950                 buf[items++] = cpu_to_le32(role->bounds);
2951
2952         BUG_ON(items > ARRAY_SIZE(buf));
2953
2954         rc = put_entry(buf, sizeof(u32), items, fp);
2955         if (rc)
2956                 return rc;
2957
2958         rc = put_entry(key, 1, len, fp);
2959         if (rc)
2960                 return rc;
2961
2962         rc = ebitmap_write(&role->dominates, fp);
2963         if (rc)
2964                 return rc;
2965
2966         rc = ebitmap_write(&role->types, fp);
2967         if (rc)
2968                 return rc;
2969
2970         return 0;
2971 }
2972
2973 static int type_write(void *vkey, void *datum, void *ptr)
2974 {
2975         char *key = vkey;
2976         struct type_datum *typdatum = datum;
2977         struct policy_data *pd = ptr;
2978         struct policydb *p = pd->p;
2979         void *fp = pd->fp;
2980         __le32 buf[4];
2981         int rc;
2982         size_t items, len;
2983
2984         len = strlen(key);
2985         items = 0;
2986         buf[items++] = cpu_to_le32(len);
2987         buf[items++] = cpu_to_le32(typdatum->value);
2988         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2989                 u32 properties = 0;
2990
2991                 if (typdatum->primary)
2992                         properties |= TYPEDATUM_PROPERTY_PRIMARY;
2993
2994                 if (typdatum->attribute)
2995                         properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2996
2997                 buf[items++] = cpu_to_le32(properties);
2998                 buf[items++] = cpu_to_le32(typdatum->bounds);
2999         } else {
3000                 buf[items++] = cpu_to_le32(typdatum->primary);
3001         }
3002         BUG_ON(items > ARRAY_SIZE(buf));
3003         rc = put_entry(buf, sizeof(u32), items, fp);
3004         if (rc)
3005                 return rc;
3006
3007         rc = put_entry(key, 1, len, fp);
3008         if (rc)
3009                 return rc;
3010
3011         return 0;
3012 }
3013
3014 static int user_write(void *vkey, void *datum, void *ptr)
3015 {
3016         char *key = vkey;
3017         struct user_datum *usrdatum = datum;
3018         struct policy_data *pd = ptr;
3019         struct policydb *p = pd->p;
3020         void *fp = pd->fp;
3021         __le32 buf[3];
3022         size_t items, len;
3023         int rc;
3024
3025         len = strlen(key);
3026         items = 0;
3027         buf[items++] = cpu_to_le32(len);
3028         buf[items++] = cpu_to_le32(usrdatum->value);
3029         if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3030                 buf[items++] = cpu_to_le32(usrdatum->bounds);
3031         BUG_ON(items > ARRAY_SIZE(buf));
3032         rc = put_entry(buf, sizeof(u32), items, fp);
3033         if (rc)
3034                 return rc;
3035
3036         rc = put_entry(key, 1, len, fp);
3037         if (rc)
3038                 return rc;
3039
3040         rc = ebitmap_write(&usrdatum->roles, fp);
3041         if (rc)
3042                 return rc;
3043
3044         rc = mls_write_range_helper(&usrdatum->range, fp);
3045         if (rc)
3046                 return rc;
3047
3048         rc = mls_write_level(&usrdatum->dfltlevel, fp);
3049         if (rc)
3050                 return rc;
3051
3052         return 0;
3053 }
3054
3055 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3056                                 void *datap) =
3057 {
3058         common_write,
3059         class_write,
3060         role_write,
3061         type_write,
3062         user_write,
3063         cond_write_bool,
3064         sens_write,
3065         cat_write,
3066 };
3067
3068 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3069                           void *fp)
3070 {
3071         unsigned int i, j, rc;
3072         size_t nel, len;
3073         __le32 buf[3];
3074         u32 nodebuf[8];
3075         struct ocontext *c;
3076         for (i = 0; i < info->ocon_num; i++) {
3077                 nel = 0;
3078                 for (c = p->ocontexts[i]; c; c = c->next)
3079                         nel++;
3080                 buf[0] = cpu_to_le32(nel);
3081                 rc = put_entry(buf, sizeof(u32), 1, fp);
3082                 if (rc)
3083                         return rc;
3084                 for (c = p->ocontexts[i]; c; c = c->next) {
3085                         switch (i) {
3086                         case OCON_ISID:
3087                                 buf[0] = cpu_to_le32(c->sid[0]);
3088                                 rc = put_entry(buf, sizeof(u32), 1, fp);
3089                                 if (rc)
3090                                         return rc;
3091                                 rc = context_write(p, &c->context[0], fp);
3092                                 if (rc)
3093                                         return rc;
3094                                 break;
3095                         case OCON_FS:
3096                         case OCON_NETIF:
3097                                 len = strlen(c->u.name);
3098                                 buf[0] = cpu_to_le32(len);
3099                                 rc = put_entry(buf, sizeof(u32), 1, fp);
3100                                 if (rc)
3101                                         return rc;
3102                                 rc = put_entry(c->u.name, 1, len, fp);
3103                                 if (rc)
3104                                         return rc;
3105                                 rc = context_write(p, &c->context[0], fp);
3106                                 if (rc)
3107                                         return rc;
3108                                 rc = context_write(p, &c->context[1], fp);
3109                                 if (rc)
3110                                         return rc;
3111                                 break;
3112                         case OCON_PORT:
3113                                 buf[0] = cpu_to_le32(c->u.port.protocol);
3114                                 buf[1] = cpu_to_le32(c->u.port.low_port);
3115                                 buf[2] = cpu_to_le32(c->u.port.high_port);
3116                                 rc = put_entry(buf, sizeof(u32), 3, fp);
3117                                 if (rc)
3118                                         return rc;
3119                                 rc = context_write(p, &c->context[0], fp);
3120                                 if (rc)
3121                                         return rc;
3122                                 break;
3123                         case OCON_NODE:
3124                                 nodebuf[0] = c->u.node.addr; /* network order */
3125                                 nodebuf[1] = c->u.node.mask; /* network order */
3126                                 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3127                                 if (rc)
3128                                         return rc;
3129                                 rc = context_write(p, &c->context[0], fp);
3130                                 if (rc)
3131                                         return rc;
3132                                 break;
3133                         case OCON_FSUSE:
3134                                 buf[0] = cpu_to_le32(c->v.behavior);
3135                                 len = strlen(c->u.name);
3136                                 buf[1] = cpu_to_le32(len);
3137                                 rc = put_entry(buf, sizeof(u32), 2, fp);
3138                                 if (rc)
3139                                         return rc;
3140                                 rc = put_entry(c->u.name, 1, len, fp);
3141                                 if (rc)
3142                                         return rc;
3143                                 rc = context_write(p, &c->context[0], fp);
3144                                 if (rc)
3145                                         return rc;
3146                                 break;
3147                         case OCON_NODE6:
3148                                 for (j = 0; j < 4; j++)
3149                                         nodebuf[j] = c->u.node6.addr[j]; /* network order */
3150                                 for (j = 0; j < 4; j++)
3151                                         nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3152                                 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3153                                 if (rc)
3154                                         return rc;
3155                                 rc = context_write(p, &c->context[0], fp);
3156                                 if (rc)
3157                                         return rc;
3158                                 break;
3159                         }
3160                 }
3161         }
3162         return 0;
3163 }
3164
3165 static int genfs_write(struct policydb *p, void *fp)
3166 {
3167         struct genfs *genfs;
3168         struct ocontext *c;
3169         size_t len;
3170         __le32 buf[1];
3171         int rc;
3172
3173         len = 0;
3174         for (genfs = p->genfs; genfs; genfs = genfs->next)
3175                 len++;
3176         buf[0] = cpu_to_le32(len);
3177         rc = put_entry(buf, sizeof(u32), 1, fp);
3178         if (rc)
3179                 return rc;
3180         for (genfs = p->genfs; genfs; genfs = genfs->next) {
3181                 len = strlen(genfs->fstype);
3182                 buf[0] = cpu_to_le32(len);
3183                 rc = put_entry(buf, sizeof(u32), 1, fp);
3184                 if (rc)
3185                         return rc;
3186                 rc = put_entry(genfs->fstype, 1, len, fp);
3187                 if (rc)
3188                         return rc;
3189                 len = 0;
3190                 for (c = genfs->head; c; c = c->next)
3191                         len++;
3192                 buf[0] = cpu_to_le32(len);
3193                 rc = put_entry(buf, sizeof(u32), 1, fp);
3194                 if (rc)
3195                         return rc;
3196                 for (c = genfs->head; c; c = c->next) {
3197                         len = strlen(c->u.name);
3198                         buf[0] = cpu_to_le32(len);
3199                         rc = put_entry(buf, sizeof(u32), 1, fp);
3200                         if (rc)
3201                                 return rc;
3202                         rc = put_entry(c->u.name, 1, len, fp);
3203                         if (rc)
3204                                 return rc;
3205                         buf[0] = cpu_to_le32(c->v.sclass);
3206                         rc = put_entry(buf, sizeof(u32), 1, fp);
3207                         if (rc)
3208                                 return rc;
3209                         rc = context_write(p, &c->context[0], fp);
3210                         if (rc)
3211                                 return rc;
3212                 }
3213         }
3214         return 0;
3215 }
3216
3217 static int hashtab_cnt(void *key, void *data, void *ptr)
3218 {
3219         int *cnt = ptr;
3220         *cnt = *cnt + 1;
3221
3222         return 0;
3223 }
3224
3225 static int range_write_helper(void *key, void *data, void *ptr)
3226 {
3227         __le32 buf[2];
3228         struct range_trans *rt = key;
3229         struct mls_range *r = data;
3230         struct policy_data *pd = ptr;
3231         void *fp = pd->fp;
3232         struct policydb *p = pd->p;
3233         int rc;
3234
3235         buf[0] = cpu_to_le32(rt->source_type);
3236         buf[1] = cpu_to_le32(rt->target_type);
3237         rc = put_entry(buf, sizeof(u32), 2, fp);
3238         if (rc)
3239                 return rc;
3240         if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3241                 buf[0] = cpu_to_le32(rt->target_class);
3242                 rc = put_entry(buf, sizeof(u32), 1, fp);
3243                 if (rc)
3244                         return rc;
3245         }
3246         rc = mls_write_range_helper(r, fp);
3247         if (rc)
3248                 return rc;
3249
3250         return 0;
3251 }
3252
3253 static int range_write(struct policydb *p, void *fp)
3254 {
3255         __le32 buf[1];
3256         int rc, nel;
3257         struct policy_data pd;
3258
3259         pd.p = p;
3260         pd.fp = fp;
3261
3262         /* count the number of entries in the hashtab */
3263         nel = 0;
3264         rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3265         if (rc)
3266                 return rc;
3267
3268         buf[0] = cpu_to_le32(nel);
3269         rc = put_entry(buf, sizeof(u32), 1, fp);
3270         if (rc)
3271                 return rc;
3272
3273         /* actually write all of the entries */
3274         rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3275         if (rc)
3276                 return rc;
3277
3278         return 0;
3279 }
3280
3281 static int filename_write_helper(void *key, void *data, void *ptr)
3282 {
3283         __le32 buf[4];
3284         struct filename_trans *ft = key;
3285         struct filename_trans_datum *otype = data;
3286         void *fp = ptr;
3287         int rc;
3288         u32 len;
3289
3290         len = strlen(ft->name);
3291         buf[0] = cpu_to_le32(len);
3292         rc = put_entry(buf, sizeof(u32), 1, fp);
3293         if (rc)
3294                 return rc;
3295
3296         rc = put_entry(ft->name, sizeof(char), len, fp);
3297         if (rc)
3298                 return rc;
3299
3300         buf[0] = cpu_to_le32(ft->stype);
3301         buf[1] = cpu_to_le32(ft->ttype);
3302         buf[2] = cpu_to_le32(ft->tclass);
3303         buf[3] = cpu_to_le32(otype->otype);
3304
3305         rc = put_entry(buf, sizeof(u32), 4, fp);
3306         if (rc)
3307                 return rc;
3308
3309         return 0;
3310 }
3311
3312 static int filename_trans_write(struct policydb *p, void *fp)
3313 {
3314         u32 nel;
3315         __le32 buf[1];
3316         int rc;
3317
3318         if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3319                 return 0;
3320
3321         nel = 0;
3322         rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3323         if (rc)
3324                 return rc;
3325
3326         buf[0] = cpu_to_le32(nel);
3327         rc = put_entry(buf, sizeof(u32), 1, fp);
3328         if (rc)
3329                 return rc;
3330
3331         rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3332         if (rc)
3333                 return rc;
3334
3335         return 0;
3336 }
3337
3338 /*
3339  * Write the configuration data in a policy database
3340  * structure to a policy database binary representation
3341  * file.
3342  */
3343 int policydb_write(struct policydb *p, void *fp)
3344 {
3345         unsigned int i, num_syms;
3346         int rc;
3347         __le32 buf[4];
3348         u32 config;
3349         size_t len;
3350         struct policydb_compat_info *info;
3351
3352         /*
3353          * refuse to write policy older than compressed avtab
3354          * to simplify the writer.  There are other tests dropped
3355          * since we assume this throughout the writer code.  Be
3356          * careful if you ever try to remove this restriction
3357          */
3358         if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3359                 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3360                        "  Because it is less than version %d\n", p->policyvers,
3361                        POLICYDB_VERSION_AVTAB);
3362                 return -EINVAL;
3363         }
3364
3365         config = 0;
3366         if (p->mls_enabled)
3367                 config |= POLICYDB_CONFIG_MLS;
3368
3369         if (p->reject_unknown)
3370                 config |= REJECT_UNKNOWN;
3371         if (p->allow_unknown)
3372                 config |= ALLOW_UNKNOWN;
3373
3374         /* Write the magic number and string identifiers. */
3375         buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3376         len = strlen(POLICYDB_STRING);
3377         buf[1] = cpu_to_le32(len);
3378         rc = put_entry(buf, sizeof(u32), 2, fp);
3379         if (rc)
3380                 return rc;
3381         rc = put_entry(POLICYDB_STRING, 1, len, fp);
3382         if (rc)
3383                 return rc;
3384
3385         /* Write the version, config, and table sizes. */
3386         info = policydb_lookup_compat(p->policyvers);
3387         if (!info) {
3388                 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3389                     "version %d", p->policyvers);
3390                 return -EINVAL;
3391         }
3392
3393         buf[0] = cpu_to_le32(p->policyvers);
3394         buf[1] = cpu_to_le32(config);
3395         buf[2] = cpu_to_le32(info->sym_num);
3396         buf[3] = cpu_to_le32(info->ocon_num);
3397
3398         rc = put_entry(buf, sizeof(u32), 4, fp);
3399         if (rc)
3400                 return rc;
3401
3402         if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3403                 rc = ebitmap_write(&p->policycaps, fp);
3404                 if (rc)
3405                         return rc;
3406         }
3407
3408         if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3409                 rc = ebitmap_write(&p->permissive_map, fp);
3410                 if (rc)
3411                         return rc;
3412         }
3413
3414         num_syms = info->sym_num;
3415         for (i = 0; i < num_syms; i++) {
3416                 struct policy_data pd;
3417
3418                 pd.fp = fp;
3419                 pd.p = p;
3420
3421                 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3422                 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3423
3424                 rc = put_entry(buf, sizeof(u32), 2, fp);
3425                 if (rc)
3426                         return rc;
3427                 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3428                 if (rc)
3429                         return rc;
3430         }
3431
3432         rc = avtab_write(p, &p->te_avtab, fp);
3433         if (rc)
3434                 return rc;
3435
3436         rc = cond_write_list(p, p->cond_list, fp);
3437         if (rc)
3438                 return rc;
3439
3440         rc = role_trans_write(p, fp);
3441         if (rc)
3442                 return rc;
3443
3444         rc = role_allow_write(p->role_allow, fp);
3445         if (rc)
3446                 return rc;
3447
3448         rc = filename_trans_write(p, fp);
3449         if (rc)
3450                 return rc;
3451
3452         rc = ocontext_write(p, info, fp);
3453         if (rc)
3454                 return rc;
3455
3456         rc = genfs_write(p, fp);
3457         if (rc)
3458                 return rc;
3459
3460         rc = range_write(p, fp);
3461         if (rc)
3462                 return rc;
3463
3464         for (i = 0; i < p->p_types.nprim; i++) {
3465                 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3466
3467                 BUG_ON(!e);
3468                 rc = ebitmap_write(e, fp);
3469                 if (rc)
3470                         return rc;
3471         }
3472
3473         return 0;
3474 }