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