OSDN Git Service

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