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