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