ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
8  *
9  *      Added conditional policy language extensions
10  *
11  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12  *      This program is free software; you can redistribute it and/or modify
13  *      it under the terms of the GNU General Public License as published by
14  *      the Free Software Foundation, version 2.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include "security.h"
22
23 #include "policydb.h"
24 #include "conditional.h"
25 #include "mls.h"
26
27 #define _DEBUG_HASHES
28
29 #ifdef DEBUG_HASHES
30 static char *symtab_name[SYM_NUM] = {
31         "common prefixes",
32         "classes",
33         "roles",
34         "types",
35         "users",
36         mls_symtab_names
37         "bools"
38 };
39 #endif
40
41 static unsigned int symtab_sizes[SYM_NUM] = {
42         2,
43         32,
44         16,
45         512,
46         128,
47         mls_symtab_sizes
48         16
49 };
50
51 struct policydb_compat_info {
52         int version;
53         int sym_num;
54         int ocon_num;
55 };
56
57 /* These need to be updated if SYM_NUM or OCON_NUM changes */
58 static struct policydb_compat_info policydb_compat[] = {
59         {
60                 .version        = POLICYDB_VERSION_BASE,
61                 .sym_num        = SYM_NUM - 1,
62                 .ocon_num       = OCON_NUM - 1,
63         },
64         {
65                 .version        = POLICYDB_VERSION_BOOL,
66                 .sym_num        = SYM_NUM,
67                 .ocon_num       = OCON_NUM - 1,
68         },
69         {
70                 .version        = POLICYDB_VERSION_IPV6,
71                 .sym_num        = SYM_NUM,
72                 .ocon_num       = OCON_NUM,
73         },
74 };
75
76 static struct policydb_compat_info *policydb_lookup_compat(int version)
77 {
78         int i;
79         struct policydb_compat_info *info = NULL;
80
81         for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
82                 if (policydb_compat[i].version == version) {
83                         info = &policydb_compat[i];
84                         break;
85                 }
86         }
87         return info;
88 }
89
90 /*
91  * Initialize the role table.
92  */
93 int roles_init(struct policydb *p)
94 {
95         char *key = 0;
96         int rc;
97         struct role_datum *role;
98
99         role = kmalloc(sizeof(*role), GFP_KERNEL);
100         if (!role) {
101                 rc = -ENOMEM;
102                 goto out;
103         }
104         memset(role, 0, sizeof(*role));
105         role->value = ++p->p_roles.nprim;
106         if (role->value != OBJECT_R_VAL) {
107                 rc = -EINVAL;
108                 goto out_free_role;
109         }
110         key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
111         if (!key) {
112                 rc = -ENOMEM;
113                 goto out_free_role;
114         }
115         strcpy(key, OBJECT_R);
116         rc = hashtab_insert(p->p_roles.table, key, role);
117         if (rc)
118                 goto out_free_key;
119 out:
120         return rc;
121
122 out_free_key:
123         kfree(key);
124 out_free_role:
125         kfree(role);
126         goto out;
127 }
128
129 /*
130  * Initialize a policy database structure.
131  */
132 int policydb_init(struct policydb *p)
133 {
134         int i, rc;
135
136         memset(p, 0, sizeof(*p));
137
138         for (i = 0; i < SYM_NUM; i++) {
139                 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
140                 if (rc)
141                         goto out_free_symtab;
142         }
143
144         rc = avtab_init(&p->te_avtab);
145         if (rc)
146                 goto out_free_symtab;
147
148         rc = roles_init(p);
149         if (rc)
150                 goto out_free_avtab;
151
152         rc = cond_policydb_init(p);
153         if (rc)
154                 goto out_free_avtab;
155
156 out:
157         return rc;
158
159 out_free_avtab:
160         avtab_destroy(&p->te_avtab);
161
162 out_free_symtab:
163         for (i = 0; i < SYM_NUM; i++)
164                 hashtab_destroy(p->symtab[i].table);
165         goto out;
166 }
167
168 /*
169  * The following *_index functions are used to
170  * define the val_to_name and val_to_struct arrays
171  * in a policy database structure.  The val_to_name
172  * arrays are used when converting security context
173  * structures into string representations.  The
174  * val_to_struct arrays are used when the attributes
175  * of a class, role, or user are needed.
176  */
177
178 static int common_index(void *key, void *datum, void *datap)
179 {
180         struct policydb *p;
181         struct common_datum *comdatum;
182
183         comdatum = datum;
184         p = datap;
185         if (!comdatum->value || comdatum->value > p->p_commons.nprim)
186                 return -EINVAL;
187         p->p_common_val_to_name[comdatum->value - 1] = key;
188         return 0;
189 }
190
191 static int class_index(void *key, void *datum, void *datap)
192 {
193         struct policydb *p;
194         struct class_datum *cladatum;
195
196         cladatum = datum;
197         p = datap;
198         if (!cladatum->value || cladatum->value > p->p_classes.nprim)
199                 return -EINVAL;
200         p->p_class_val_to_name[cladatum->value - 1] = key;
201         p->class_val_to_struct[cladatum->value - 1] = cladatum;
202         return 0;
203 }
204
205 static int role_index(void *key, void *datum, void *datap)
206 {
207         struct policydb *p;
208         struct role_datum *role;
209
210         role = datum;
211         p = datap;
212         if (!role->value || role->value > p->p_roles.nprim)
213                 return -EINVAL;
214         p->p_role_val_to_name[role->value - 1] = key;
215         p->role_val_to_struct[role->value - 1] = role;
216         return 0;
217 }
218
219 static int type_index(void *key, void *datum, void *datap)
220 {
221         struct policydb *p;
222         struct type_datum *typdatum;
223
224         typdatum = datum;
225         p = datap;
226
227         if (typdatum->primary) {
228                 if (!typdatum->value || typdatum->value > p->p_types.nprim)
229                         return -EINVAL;
230                 p->p_type_val_to_name[typdatum->value - 1] = key;
231         }
232
233         return 0;
234 }
235
236 static int user_index(void *key, void *datum, void *datap)
237 {
238         struct policydb *p;
239         struct user_datum *usrdatum;
240
241         usrdatum = datum;
242         p = datap;
243         if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
244                 return -EINVAL;
245         p->p_user_val_to_name[usrdatum->value - 1] = key;
246         p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
247         return 0;
248 }
249
250 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
251 {
252         common_index,
253         class_index,
254         role_index,
255         type_index,
256         user_index,
257         mls_index_f
258         cond_index_bool
259 };
260
261 /*
262  * Define the common val_to_name array and the class
263  * val_to_name and val_to_struct arrays in a policy
264  * database structure.
265  *
266  * Caller must clean up upon failure.
267  */
268 int policydb_index_classes(struct policydb *p)
269 {
270         int rc;
271
272         p->p_common_val_to_name =
273                 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
274         if (!p->p_common_val_to_name) {
275                 rc = -ENOMEM;
276                 goto out;
277         }
278
279         rc = hashtab_map(p->p_commons.table, common_index, p);
280         if (rc)
281                 goto out;
282
283         p->class_val_to_struct =
284                 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
285         if (!p->class_val_to_struct) {
286                 rc = -ENOMEM;
287                 goto out;
288         }
289
290         p->p_class_val_to_name =
291                 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
292         if (!p->p_class_val_to_name) {
293                 rc = -ENOMEM;
294                 goto out;
295         }
296
297         rc = hashtab_map(p->p_classes.table, class_index, p);
298 out:
299         return rc;
300 }
301
302 #ifdef DEBUG_HASHES
303 static void symtab_hash_eval(struct symtab *s)
304 {
305         int i;
306
307         for (i = 0; i < SYM_NUM; i++) {
308                 struct hashtab *h = s[i].table;
309                 struct hashtab_info info;
310
311                 hashtab_stat(h, &info);
312                 printk(KERN_INFO "%s:  %d entries and %d/%d buckets used, "
313                        "longest chain length %d\n", symtab_name[i], h->nel,
314                        info.slots_used, h->size, info.max_chain_len);
315         }
316 }
317 #endif
318
319 /*
320  * Define the other val_to_name and val_to_struct arrays
321  * in a policy database structure.
322  *
323  * Caller must clean up on failure.
324  */
325 int policydb_index_others(struct policydb *p)
326 {
327         int i, rc = 0;
328
329         printk(KERN_INFO "security:  %d users, %d roles, %d types, %d bools",
330                p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
331         mls_policydb_index_others(p);
332         printk("\n");
333
334         printk(KERN_INFO "security:  %d classes, %d rules\n",
335                p->p_classes.nprim, p->te_avtab.nel);
336
337 #ifdef DEBUG_HASHES
338         avtab_hash_eval(&p->te_avtab, "rules");
339         symtab_hash_eval(p->symtab);
340 #endif
341
342         p->role_val_to_struct =
343                 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
344                         GFP_KERNEL);
345         if (!p->role_val_to_struct) {
346                 rc = -ENOMEM;
347                 goto out;
348         }
349
350         p->user_val_to_struct =
351                 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
352                         GFP_KERNEL);
353         if (!p->user_val_to_struct) {
354                 rc = -ENOMEM;
355                 goto out;
356         }
357
358         if (cond_init_bool_indexes(p)) {
359                 rc = -ENOMEM;
360                 goto out;
361         }
362
363         for (i = SYM_ROLES; i < SYM_NUM; i++) {
364                 p->sym_val_to_name[i] =
365                         kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
366                 if (!p->sym_val_to_name[i]) {
367                         rc = -ENOMEM;
368                         goto out;
369                 }
370                 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
371                 if (rc)
372                         goto out;
373         }
374
375 out:
376         return rc;
377 }
378
379 /*
380  * The following *_destroy functions are used to
381  * free any memory allocated for each kind of
382  * symbol data in the policy database.
383  */
384
385 static int perm_destroy(void *key, void *datum, void *p)
386 {
387         kfree(key);
388         kfree(datum);
389         return 0;
390 }
391
392 static int common_destroy(void *key, void *datum, void *p)
393 {
394         struct common_datum *comdatum;
395
396         kfree(key);
397         comdatum = datum;
398         hashtab_map(comdatum->permissions.table, perm_destroy, 0);
399         hashtab_destroy(comdatum->permissions.table);
400         kfree(datum);
401         return 0;
402 }
403
404 static int class_destroy(void *key, void *datum, void *p)
405 {
406         struct class_datum *cladatum;
407         struct constraint_node *constraint, *ctemp;
408         struct constraint_expr *e, *etmp;
409
410         kfree(key);
411         cladatum = datum;
412         hashtab_map(cladatum->permissions.table, perm_destroy, 0);
413         hashtab_destroy(cladatum->permissions.table);
414         constraint = cladatum->constraints;
415         while (constraint) {
416                 e = constraint->expr;
417                 while (e) {
418                         ebitmap_destroy(&e->names);
419                         etmp = e;
420                         e = e->next;
421                         kfree(etmp);
422                 }
423                 ctemp = constraint;
424                 constraint = constraint->next;
425                 kfree(ctemp);
426         }
427         kfree(cladatum->comkey);
428         kfree(datum);
429         return 0;
430 }
431
432 static int role_destroy(void *key, void *datum, void *p)
433 {
434         struct role_datum *role;
435
436         kfree(key);
437         role = datum;
438         ebitmap_destroy(&role->dominates);
439         ebitmap_destroy(&role->types);
440         kfree(datum);
441         return 0;
442 }
443
444 static int type_destroy(void *key, void *datum, void *p)
445 {
446         kfree(key);
447         kfree(datum);
448         return 0;
449 }
450
451 static int user_destroy(void *key, void *datum, void *p)
452 {
453         struct user_datum *usrdatum;
454
455         kfree(key);
456         usrdatum = datum;
457         ebitmap_destroy(&usrdatum->roles);
458         mls_user_destroy(usrdatum);
459         kfree(datum);
460         return 0;
461 }
462
463 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
464 {
465         common_destroy,
466         class_destroy,
467         role_destroy,
468         type_destroy,
469         user_destroy,
470         mls_destroy_f
471         cond_destroy_bool
472 };
473
474 void ocontext_destroy(struct ocontext *c, int i)
475 {
476         context_destroy(&c->context[0]);
477         context_destroy(&c->context[1]);
478         if (i == OCON_ISID || i == OCON_FS ||
479             i == OCON_NETIF || i == OCON_FSUSE)
480                 kfree(c->u.name);
481         kfree(c);
482 }
483
484 /*
485  * Free any memory allocated by a policy database structure.
486  */
487 void policydb_destroy(struct policydb *p)
488 {
489         struct ocontext *c, *ctmp;
490         struct genfs *g, *gtmp;
491         int i;
492
493         for (i = 0; i < SYM_NUM; i++) {
494                 hashtab_map(p->symtab[i].table, destroy_f[i], 0);
495                 hashtab_destroy(p->symtab[i].table);
496         }
497
498         for (i = 0; i < SYM_NUM; i++) {
499                 if (p->sym_val_to_name[i])
500                         kfree(p->sym_val_to_name[i]);
501         }
502
503         if (p->class_val_to_struct)
504                 kfree(p->class_val_to_struct);
505         if (p->role_val_to_struct)
506                 kfree(p->role_val_to_struct);
507         if (p->user_val_to_struct)
508                 kfree(p->user_val_to_struct);
509
510         avtab_destroy(&p->te_avtab);
511
512         for (i = 0; i < OCON_NUM; i++) {
513                 c = p->ocontexts[i];
514                 while (c) {
515                         ctmp = c;
516                         c = c->next;
517                         ocontext_destroy(ctmp,i);
518                 }
519         }
520
521         g = p->genfs;
522         while (g) {
523                 kfree(g->fstype);
524                 c = g->head;
525                 while (c) {
526                         ctmp = c;
527                         c = c->next;
528                         ocontext_destroy(ctmp,OCON_FSUSE);
529                 }
530                 gtmp = g;
531                 g = g->next;
532                 kfree(gtmp);
533         }
534
535         cond_policydb_destroy(p);
536
537         return;
538 }
539
540 /*
541  * Load the initial SIDs specified in a policy database
542  * structure into a SID table.
543  */
544 int policydb_load_isids(struct policydb *p, struct sidtab *s)
545 {
546         struct ocontext *head, *c;
547         int rc;
548
549         rc = sidtab_init(s);
550         if (rc) {
551                 printk(KERN_ERR "security:  out of memory on SID table init\n");
552                 goto out;
553         }
554
555         head = p->ocontexts[OCON_ISID];
556         for (c = head; c; c = c->next) {
557                 if (!c->context[0].user) {
558                         printk(KERN_ERR "security:  SID %s was never "
559                                "defined.\n", c->u.name);
560                         rc = -EINVAL;
561                         goto out;
562                 }
563                 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
564                         printk(KERN_ERR "security:  unable to load initial "
565                                "SID %s.\n", c->u.name);
566                         rc = -EINVAL;
567                         goto out;
568                 }
569         }
570 out:
571         return rc;
572 }
573
574 /*
575  * Return 1 if the fields in the security context
576  * structure `c' are valid.  Return 0 otherwise.
577  */
578 int policydb_context_isvalid(struct policydb *p, struct context *c)
579 {
580         struct role_datum *role;
581         struct user_datum *usrdatum;
582
583         if (!c->role || c->role > p->p_roles.nprim)
584                 return 0;
585
586         if (!c->user || c->user > p->p_users.nprim)
587                 return 0;
588
589         if (!c->type || c->type > p->p_types.nprim)
590                 return 0;
591
592         if (c->role != OBJECT_R_VAL) {
593                 /*
594                  * Role must be authorized for the type.
595                  */
596                 role = p->role_val_to_struct[c->role - 1];
597                 if (!ebitmap_get_bit(&role->types,
598                                      c->type - 1))
599                         /* role may not be associated with type */
600                         return 0;
601
602                 /*
603                  * User must be authorized for the role.
604                  */
605                 usrdatum = p->user_val_to_struct[c->user - 1];
606                 if (!usrdatum)
607                         return 0;
608
609                 if (!ebitmap_get_bit(&usrdatum->roles,
610                                      c->role - 1))
611                         /* user may not be associated with role */
612                         return 0;
613         }
614
615         if (!mls_context_isvalid(p, c))
616                 return 0;
617
618         return 1;
619 }
620
621 /*
622  * Read and validate a security context structure
623  * from a policydb binary representation file.
624  */
625 static int context_read_and_validate(struct context *c,
626                                      struct policydb *p,
627                                      void *fp)
628 {
629         u32 *buf;
630         int rc = 0;
631
632         buf = next_entry(fp, sizeof(u32)*3);
633         if (!buf) {
634                 printk(KERN_ERR "security: context truncated\n");
635                 rc = -EINVAL;
636                 goto out;
637         }
638         c->user = le32_to_cpu(buf[0]);
639         c->role = le32_to_cpu(buf[1]);
640         c->type = le32_to_cpu(buf[2]);
641         if (mls_read_range(c, fp)) {
642                 printk(KERN_ERR "security: error reading MLS range of "
643                        "context\n");
644                 rc = -EINVAL;
645                 goto out;
646         }
647
648         if (!policydb_context_isvalid(p, c)) {
649                 printk(KERN_ERR "security:  invalid security context\n");
650                 context_destroy(c);
651                 rc = -EINVAL;
652         }
653 out:
654         return rc;
655 }
656
657 /*
658  * The following *_read functions are used to
659  * read the symbol data from a policy database
660  * binary representation file.
661  */
662
663 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
664 {
665         char *key = 0;
666         struct perm_datum *perdatum;
667         int rc;
668         u32 *buf, len;
669
670         perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
671         if (!perdatum) {
672                 rc = -ENOMEM;
673                 goto out;
674         }
675         memset(perdatum, 0, sizeof(*perdatum));
676
677         buf = next_entry(fp, sizeof(u32)*2);
678         if (!buf) {
679                 rc = -EINVAL;
680                 goto bad;
681         }
682
683         len = le32_to_cpu(buf[0]);
684         perdatum->value = le32_to_cpu(buf[1]);
685         rc = mls_read_perm(perdatum, fp);
686         if (rc)
687                 goto bad;
688
689         buf = next_entry(fp, len);
690         if (!buf) {
691                 rc = -EINVAL;
692                 goto bad;
693         }
694         key = kmalloc(len + 1,GFP_KERNEL);
695         if (!key) {
696                 rc = -ENOMEM;
697                 goto bad;
698         }
699         memcpy(key, buf, len);
700         key[len] = 0;
701
702         rc = hashtab_insert(h, key, perdatum);
703         if (rc)
704                 goto bad;
705 out:
706         return rc;
707 bad:
708         perm_destroy(key, perdatum, NULL);
709         goto out;
710 }
711
712 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
713 {
714         char *key = 0;
715         struct common_datum *comdatum;
716         u32 *buf, len, nel;
717         int i, rc;
718
719         comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
720         if (!comdatum) {
721                 rc = -ENOMEM;
722                 goto out;
723         }
724         memset(comdatum, 0, sizeof(*comdatum));
725
726         buf = next_entry(fp, sizeof(u32)*4);
727         if (!buf) {
728                 rc = -EINVAL;
729                 goto bad;
730         }
731
732         len = le32_to_cpu(buf[0]);
733         comdatum->value = le32_to_cpu(buf[1]);
734
735         rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
736         if (rc)
737                 goto bad;
738         comdatum->permissions.nprim = le32_to_cpu(buf[2]);
739         nel = le32_to_cpu(buf[3]);
740
741         buf = next_entry(fp, len);
742         if (!buf) {
743                 rc = -EINVAL;
744                 goto bad;
745         }
746         key = kmalloc(len + 1,GFP_KERNEL);
747         if (!key) {
748                 rc = -ENOMEM;
749                 goto bad;
750         }
751         memcpy(key, buf, len);
752         key[len] = 0;
753
754         for (i = 0; i < nel; i++) {
755                 rc = perm_read(p, comdatum->permissions.table, fp);
756                 if (rc)
757                         goto bad;
758         }
759
760         rc = hashtab_insert(h, key, comdatum);
761         if (rc)
762                 goto bad;
763 out:
764         return rc;
765 bad:
766         common_destroy(key, comdatum, NULL);
767         goto out;
768 }
769
770 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
771 {
772         char *key = 0;
773         struct class_datum *cladatum;
774         struct constraint_node *c, *lc;
775         struct constraint_expr *e, *le;
776         u32 *buf, len, len2, ncons, nexpr, nel;
777         int i, j, depth, rc;
778
779         cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
780         if (!cladatum) {
781                 rc = -ENOMEM;
782                 goto bad;
783         }
784         memset(cladatum, 0, sizeof(*cladatum));
785
786         buf = next_entry(fp, sizeof(u32)*6);
787         if (!buf) {
788                 rc = -EINVAL;
789                 goto bad;
790         }
791
792         len = le32_to_cpu(buf[0]);
793         len2 = le32_to_cpu(buf[1]);
794         cladatum->value = le32_to_cpu(buf[2]);
795
796         rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
797         if (rc)
798                 goto bad;
799         cladatum->permissions.nprim = le32_to_cpu(buf[3]);
800         nel = le32_to_cpu(buf[4]);
801
802         ncons = le32_to_cpu(buf[5]);
803
804         buf = next_entry(fp, len);
805         if (!buf) {
806                 rc = -EINVAL;
807                 goto bad;
808         }
809         key = kmalloc(len + 1,GFP_KERNEL);
810         if (!key) {
811                 rc = -ENOMEM;
812                 goto bad;
813         }
814         memcpy(key, buf, len);
815         key[len] = 0;
816
817         if (len2) {
818                 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
819                 if (!cladatum->comkey) {
820                         rc = -ENOMEM;
821                         goto bad;
822                 }
823                 buf = next_entry(fp, len2);
824                 if (!buf) {
825                         rc = -EINVAL;
826                         goto bad;
827                 }
828                 memcpy(cladatum->comkey, buf, len2);
829                 cladatum->comkey[len2] = 0;
830
831                 cladatum->comdatum = hashtab_search(p->p_commons.table,
832                                                     cladatum->comkey);
833                 if (!cladatum->comdatum) {
834                         printk(KERN_ERR "security:  unknown common %s\n",
835                                cladatum->comkey);
836                         rc = -EINVAL;
837                         goto bad;
838                 }
839         }
840         for (i = 0; i < nel; i++) {
841                 rc = perm_read(p, cladatum->permissions.table, fp);
842                 if (rc)
843                         goto bad;
844         }
845
846         lc = NULL;
847         rc = -EINVAL;
848         for (i = 0; i < ncons; i++) {
849                 c = kmalloc(sizeof(*c), GFP_KERNEL);
850                 if (!c) {
851                         rc = -ENOMEM;
852                         goto bad;
853                 }
854                 memset(c, 0, sizeof(*c));
855
856                 if (lc) {
857                         lc->next = c;
858                 } else {
859                         cladatum->constraints = c;
860                 }
861
862                 buf = next_entry(fp, sizeof(u32)*2);
863                 if (!buf)
864                         goto bad;
865                 c->permissions = le32_to_cpu(buf[0]);
866                 nexpr = le32_to_cpu(buf[1]);
867                 le = NULL;
868                 depth = -1;
869                 for (j = 0; j < nexpr; j++) {
870                         e = kmalloc(sizeof(*e), GFP_KERNEL);
871                         if (!e) {
872                                 rc = -ENOMEM;
873                                 goto bad;
874                         }
875                         memset(e, 0, sizeof(*e));
876
877                         if (le) {
878                                 le->next = e;
879                         } else {
880                                 c->expr = e;
881                         }
882
883                         buf = next_entry(fp, sizeof(u32)*3);
884                         if (!buf)
885                                 goto bad;
886                         e->expr_type = le32_to_cpu(buf[0]);
887                         e->attr = le32_to_cpu(buf[1]);
888                         e->op = le32_to_cpu(buf[2]);
889
890                         switch (e->expr_type) {
891                         case CEXPR_NOT:
892                                 if (depth < 0)
893                                         goto bad;
894                                 break;
895                         case CEXPR_AND:
896                         case CEXPR_OR:
897                                 if (depth < 1)
898                                         goto bad;
899                                 depth--;
900                                 break;
901                         case CEXPR_ATTR:
902                                 if (depth == (CEXPR_MAXDEPTH-1))
903                                         goto bad;
904                                 depth++;
905                                 break;
906                         case CEXPR_NAMES:
907                                 if (depth == (CEXPR_MAXDEPTH-1))
908                                         goto bad;
909                                 depth++;
910                                 if (ebitmap_read(&e->names, fp))
911                                         goto bad;
912                                 break;
913                         default:
914                                 goto bad;
915                         }
916                         le = e;
917                 }
918                 if (depth != 0)
919                         goto bad;
920                 lc = c;
921         }
922
923         rc = mls_read_class(cladatum, fp);
924         if (rc)
925                 goto bad;
926
927         rc = hashtab_insert(h, key, cladatum);
928         if (rc)
929                 goto bad;
930 out:
931         return rc;
932 bad:
933         class_destroy(key, cladatum, NULL);
934         goto out;
935 }
936
937 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
938 {
939         char *key = 0;
940         struct role_datum *role;
941         int rc;
942         u32 *buf, len;
943
944         role = kmalloc(sizeof(*role), GFP_KERNEL);
945         if (!role) {
946                 rc = -ENOMEM;
947                 goto out;
948         }
949         memset(role, 0, sizeof(*role));
950
951         buf = next_entry(fp, sizeof(u32)*2);
952         if (!buf) {
953                 rc = -EINVAL;
954                 goto bad;
955         }
956
957         len = le32_to_cpu(buf[0]);
958         role->value = le32_to_cpu(buf[1]);
959
960         buf = next_entry(fp, len);
961         if (!buf) {
962                 rc = -EINVAL;
963                 goto bad;
964         }
965         key = kmalloc(len + 1,GFP_KERNEL);
966         if (!key) {
967                 rc = -ENOMEM;
968                 goto bad;
969         }
970         memcpy(key, buf, len);
971         key[len] = 0;
972
973         rc = ebitmap_read(&role->dominates, fp);
974         if (rc)
975                 goto bad;
976
977         rc = ebitmap_read(&role->types, fp);
978         if (rc)
979                 goto bad;
980
981         if (strcmp(key, OBJECT_R) == 0) {
982                 if (role->value != OBJECT_R_VAL) {
983                         printk(KERN_ERR "Role %s has wrong value %d\n",
984                                OBJECT_R, role->value);
985                         rc = -EINVAL;
986                         goto bad;
987                 }
988                 rc = 0;
989                 goto bad;
990         }
991
992         rc = hashtab_insert(h, key, role);
993         if (rc)
994                 goto bad;
995 out:
996         return rc;
997 bad:
998         role_destroy(key, role, NULL);
999         goto out;
1000 }
1001
1002 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1003 {
1004         char *key = 0;
1005         struct type_datum *typdatum;
1006         int rc;
1007         u32 *buf, len;
1008
1009         typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1010         if (!typdatum) {
1011                 rc = -ENOMEM;
1012                 return rc;
1013         }
1014         memset(typdatum, 0, sizeof(*typdatum));
1015
1016         buf = next_entry(fp, sizeof(u32)*3);
1017         if (!buf) {
1018                 rc = -EINVAL;
1019                 goto bad;
1020         }
1021
1022         len = le32_to_cpu(buf[0]);
1023         typdatum->value = le32_to_cpu(buf[1]);
1024         typdatum->primary = le32_to_cpu(buf[2]);
1025
1026         buf = next_entry(fp, len);
1027         if (!buf) {
1028                 rc = -EINVAL;
1029                 goto bad;
1030         }
1031         key = kmalloc(len + 1,GFP_KERNEL);
1032         if (!key) {
1033                 rc = -ENOMEM;
1034                 goto bad;
1035         }
1036         memcpy(key, buf, len);
1037         key[len] = 0;
1038
1039         rc = hashtab_insert(h, key, typdatum);
1040         if (rc)
1041                 goto bad;
1042 out:
1043         return rc;
1044 bad:
1045         type_destroy(key, typdatum, NULL);
1046         goto out;
1047 }
1048
1049 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1050 {
1051         char *key = 0;
1052         struct user_datum *usrdatum;
1053         int rc;
1054         u32 *buf, len;
1055
1056
1057         usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1058         if (!usrdatum) {
1059                 rc = -ENOMEM;
1060                 goto out;
1061         }
1062         memset(usrdatum, 0, sizeof(*usrdatum));
1063
1064         buf = next_entry(fp, sizeof(u32)*2);
1065         if (!buf) {
1066                 rc = -EINVAL;
1067                 goto bad;
1068         }
1069
1070         len = le32_to_cpu(buf[0]);
1071         usrdatum->value = le32_to_cpu(buf[1]);
1072
1073         buf = next_entry(fp, len);
1074         if (!buf) {
1075                 rc = -EINVAL;
1076                 goto bad;
1077         }
1078         key = kmalloc(len + 1,GFP_KERNEL);
1079         if (!key) {
1080                 rc = -ENOMEM;
1081                 goto bad;
1082         }
1083         memcpy(key, buf, len);
1084         key[len] = 0;
1085
1086         rc = ebitmap_read(&usrdatum->roles, fp);
1087         if (rc)
1088                 goto bad;
1089
1090         rc = mls_read_user(usrdatum, fp);
1091         if (rc)
1092                 goto bad;
1093
1094         rc = hashtab_insert(h, key, usrdatum);
1095         if (rc)
1096                 goto bad;
1097 out:
1098         return rc;
1099 bad:
1100         user_destroy(key, usrdatum, NULL);
1101         goto out;
1102 }
1103
1104 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1105 {
1106         common_read,
1107         class_read,
1108         role_read,
1109         type_read,
1110         user_read,
1111         mls_read_f
1112         cond_read_bool
1113 };
1114
1115 #define mls_config(x) \
1116        ((x) & POLICYDB_CONFIG_MLS) ? "mls" : "no_mls"
1117
1118 /*
1119  * Read the configuration data from a policy database binary
1120  * representation file into a policy database structure.
1121  */
1122 int policydb_read(struct policydb *p, void *fp)
1123 {
1124         struct role_allow *ra, *lra;
1125         struct role_trans *tr, *ltr;
1126         struct ocontext *l, *c, *newc;
1127         struct genfs *genfs_p, *genfs, *newgenfs;
1128         int i, j, rc, r_policyvers;
1129         u32 *buf, len, len2, config, nprim, nel, nel2;
1130         char *policydb_str;
1131         struct policydb_compat_info *info;
1132
1133         config = 0;
1134         mls_set_config(config);
1135
1136         rc = policydb_init(p);
1137         if (rc)
1138                 goto out;
1139
1140         rc = -EINVAL;
1141         /* Read the magic number and string length. */
1142         buf = next_entry(fp, sizeof(u32)* 2);
1143         if (!buf)
1144                 goto bad;
1145
1146         for (i = 0; i < 2; i++)
1147                 buf[i] = le32_to_cpu(buf[i]);
1148
1149         if (buf[0] != POLICYDB_MAGIC) {
1150                 printk(KERN_ERR "security:  policydb magic number 0x%x does "
1151                        "not match expected magic number 0x%x\n",
1152                        buf[0], POLICYDB_MAGIC);
1153                 goto bad;
1154         }
1155
1156         len = buf[1];
1157         if (len != strlen(POLICYDB_STRING)) {
1158                 printk(KERN_ERR "security:  policydb string length %d does not "
1159                        "match expected length %Zu\n",
1160                        len, strlen(POLICYDB_STRING));
1161                 goto bad;
1162         }
1163         buf = next_entry(fp, len);
1164         if (!buf) {
1165                 printk(KERN_ERR "security:  truncated policydb string identifier\n");
1166                 goto bad;
1167         }
1168         policydb_str = kmalloc(len + 1,GFP_KERNEL);
1169         if (!policydb_str) {
1170                 printk(KERN_ERR "security:  unable to allocate memory for policydb "
1171                        "string of length %d\n", len);
1172                 rc = -ENOMEM;
1173                 goto bad;
1174         }
1175         memcpy(policydb_str, buf, len);
1176         policydb_str[len] = 0;
1177         if (strcmp(policydb_str, POLICYDB_STRING)) {
1178                 printk(KERN_ERR "security:  policydb string %s does not match "
1179                        "my string %s\n", policydb_str, POLICYDB_STRING);
1180                 kfree(policydb_str);
1181                 goto bad;
1182         }
1183         /* Done with policydb_str. */
1184         kfree(policydb_str);
1185         policydb_str = NULL;
1186
1187         /* Read the version, config, and table sizes. */
1188         buf = next_entry(fp, sizeof(u32)*4);
1189         if (!buf)
1190                 goto bad;
1191         for (i = 0; i < 4; i++)
1192                 buf[i] = le32_to_cpu(buf[i]);
1193
1194         r_policyvers = buf[0];
1195         if (r_policyvers < POLICYDB_VERSION_MIN ||
1196             r_policyvers > POLICYDB_VERSION_MAX) {
1197                 printk(KERN_ERR "security:  policydb version %d does not match "
1198                        "my version range %d-%d\n",
1199                        buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1200                 goto bad;
1201         }
1202
1203         if (buf[1] != config) {
1204                 printk(KERN_ERR "security:  policydb configuration (%s) does "
1205                        "not match my configuration (%s)\n",
1206                        mls_config(buf[1]),
1207                        mls_config(config));
1208                 goto bad;
1209         }
1210
1211
1212         info = policydb_lookup_compat(r_policyvers);
1213         if (!info) {
1214                 printk(KERN_ERR "security:  unable to find policy compat info "
1215                        "for version %d\n", r_policyvers);
1216                 goto bad;
1217         }
1218
1219         if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1220                 printk(KERN_ERR "security:  policydb table sizes (%d,%d) do "
1221                        "not match mine (%d,%d)\n", buf[2], buf[3],
1222                        info->sym_num, info->ocon_num);
1223                 goto bad;
1224         }
1225
1226         rc = mls_read_nlevels(p, fp);
1227         if (rc)
1228                 goto bad;
1229
1230         for (i = 0; i < info->sym_num; i++) {
1231                 buf = next_entry(fp, sizeof(u32)*2);
1232                 if (!buf) {
1233                         rc = -EINVAL;
1234                         goto bad;
1235                 }
1236                 nprim = le32_to_cpu(buf[0]);
1237                 nel = le32_to_cpu(buf[1]);
1238                 for (j = 0; j < nel; j++) {
1239                         rc = read_f[i](p, p->symtab[i].table, fp);
1240                         if (rc)
1241                                 goto bad;
1242                 }
1243
1244                 p->symtab[i].nprim = nprim;
1245         }
1246
1247         rc = avtab_read(&p->te_avtab, fp, config);
1248         if (rc)
1249                 goto bad;
1250
1251         if (r_policyvers >= POLICYDB_VERSION_BOOL) {
1252                 rc = cond_read_list(p, fp);
1253                 if (rc)
1254                         goto bad;
1255         }
1256
1257         buf = next_entry(fp, sizeof(u32));
1258         if (!buf) {
1259                 rc = -EINVAL;
1260                 goto bad;
1261         }
1262         nel = le32_to_cpu(buf[0]);
1263         ltr = NULL;
1264         for (i = 0; i < nel; i++) {
1265                 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1266                 if (!tr) {
1267                         rc = -ENOMEM;
1268                         goto bad;
1269                 }
1270                 memset(tr, 0, sizeof(*tr));
1271                 if (ltr) {
1272                         ltr->next = tr;
1273                 } else {
1274                         p->role_tr = tr;
1275                 }
1276                 buf = next_entry(fp, sizeof(u32)*3);
1277                 if (!buf) {
1278                         rc = -EINVAL;
1279                         goto bad;
1280                 }
1281                 tr->role = le32_to_cpu(buf[0]);
1282                 tr->type = le32_to_cpu(buf[1]);
1283                 tr->new_role = le32_to_cpu(buf[2]);
1284                 ltr = tr;
1285         }
1286
1287         buf = next_entry(fp, sizeof(u32));
1288         if (!buf) {
1289                 rc = -EINVAL;
1290                 goto bad;
1291         }
1292         nel = le32_to_cpu(buf[0]);
1293         lra = NULL;
1294         for (i = 0; i < nel; i++) {
1295                 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1296                 if (!ra) {
1297                         rc = -ENOMEM;
1298                         goto bad;
1299                 }
1300                 memset(ra, 0, sizeof(*ra));
1301                 if (lra) {
1302                         lra->next = ra;
1303                 } else {
1304                         p->role_allow = ra;
1305                 }
1306                 buf = next_entry(fp, sizeof(u32)*2);
1307                 if (!buf) {
1308                         rc = -EINVAL;
1309                         goto bad;
1310                 }
1311                 ra->role = le32_to_cpu(buf[0]);
1312                 ra->new_role = le32_to_cpu(buf[1]);
1313                 lra = ra;
1314         }
1315
1316         rc = policydb_index_classes(p);
1317         if (rc)
1318                 goto bad;
1319
1320         rc = policydb_index_others(p);
1321         if (rc)
1322                 goto bad;
1323
1324         for (i = 0; i < info->ocon_num; i++) {
1325                 buf = next_entry(fp, sizeof(u32));
1326                 if (!buf) {
1327                         rc = -EINVAL;
1328                         goto bad;
1329                 }
1330                 nel = le32_to_cpu(buf[0]);
1331                 l = NULL;
1332                 for (j = 0; j < nel; j++) {
1333                         c = kmalloc(sizeof(*c), GFP_KERNEL);
1334                         if (!c) {
1335                                 rc = -ENOMEM;
1336                                 goto bad;
1337                         }
1338                         memset(c, 0, sizeof(*c));
1339                         if (l) {
1340                                 l->next = c;
1341                         } else {
1342                                 p->ocontexts[i] = c;
1343                         }
1344                         l = c;
1345                         rc = -EINVAL;
1346                         switch (i) {
1347                         case OCON_ISID:
1348                                 buf = next_entry(fp, sizeof(u32));
1349                                 if (!buf)
1350                                         goto bad;
1351                                 c->sid[0] = le32_to_cpu(buf[0]);
1352                                 rc = context_read_and_validate(&c->context[0], p, fp);
1353                                 if (rc)
1354                                         goto bad;
1355                                 break;
1356                         case OCON_FS:
1357                         case OCON_NETIF:
1358                                 buf = next_entry(fp, sizeof(u32));
1359                                 if (!buf)
1360                                         goto bad;
1361                                 len = le32_to_cpu(buf[0]);
1362                                 buf = next_entry(fp, len);
1363                                 if (!buf)
1364                                         goto bad;
1365                                 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1366                                 if (!c->u.name) {
1367                                         rc = -ENOMEM;
1368                                         goto bad;
1369                                 }
1370                                 memcpy(c->u.name, buf, len);
1371                                 c->u.name[len] = 0;
1372                                 rc = context_read_and_validate(&c->context[0], p, fp);
1373                                 if (rc)
1374                                         goto bad;
1375                                 rc = context_read_and_validate(&c->context[1], p, fp);
1376                                 if (rc)
1377                                         goto bad;
1378                                 break;
1379                         case OCON_PORT:
1380                                 buf = next_entry(fp, sizeof(u32)*3);
1381                                 if (!buf)
1382                                         goto bad;
1383                                 c->u.port.protocol = le32_to_cpu(buf[0]);
1384                                 c->u.port.low_port = le32_to_cpu(buf[1]);
1385                                 c->u.port.high_port = le32_to_cpu(buf[2]);
1386                                 rc = context_read_and_validate(&c->context[0], p, fp);
1387                                 if (rc)
1388                                         goto bad;
1389                                 break;
1390                         case OCON_NODE:
1391                                 buf = next_entry(fp, sizeof(u32)* 2);
1392                                 if (!buf)
1393                                         goto bad;
1394                                 c->u.node.addr = le32_to_cpu(buf[0]);
1395                                 c->u.node.mask = le32_to_cpu(buf[1]);
1396                                 rc = context_read_and_validate(&c->context[0], p, fp);
1397                                 if (rc)
1398                                         goto bad;
1399                                 break;
1400                         case OCON_FSUSE:
1401                                 buf = next_entry(fp, sizeof(u32)*2);
1402                                 if (!buf)
1403                                         goto bad;
1404                                 c->v.behavior = le32_to_cpu(buf[0]);
1405                                 if (c->v.behavior > SECURITY_FS_USE_NONE)
1406                                         goto bad;
1407                                 len = le32_to_cpu(buf[1]);
1408                                 buf = next_entry(fp, len);
1409                                 if (!buf)
1410                                         goto bad;
1411                                 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1412                                 if (!c->u.name) {
1413                                         rc = -ENOMEM;
1414                                         goto bad;
1415                                 }
1416                                 memcpy(c->u.name, buf, len);
1417                                 c->u.name[len] = 0;
1418                                 rc = context_read_and_validate(&c->context[0], p, fp);
1419                                 if (rc)
1420                                         goto bad;
1421                                 break;
1422                         case OCON_NODE6: {
1423                                 int k;
1424
1425                                 buf = next_entry(fp, sizeof(u32) * 8);
1426                                 if (!buf)
1427                                         goto bad;
1428                                 for (k = 0; k < 4; k++)
1429                                         c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1430                                 for (k = 0; k < 4; k++)
1431                                         c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1432                                 if (context_read_and_validate(&c->context[0], p, fp))
1433                                         goto bad;
1434                                 break;
1435                         }
1436                         }
1437                 }
1438         }
1439
1440         buf = next_entry(fp, sizeof(u32));
1441         if (!buf) {
1442                 rc = -EINVAL;
1443                 goto bad;
1444         }
1445         nel = le32_to_cpu(buf[0]);
1446         genfs_p = NULL;
1447         rc = -EINVAL;
1448         for (i = 0; i < nel; i++) {
1449                 buf = next_entry(fp, sizeof(u32));
1450                 if (!buf)
1451                         goto bad;
1452                 len = le32_to_cpu(buf[0]);
1453                 buf = next_entry(fp, len);
1454                 if (!buf)
1455                         goto bad;
1456                 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1457                 if (!newgenfs) {
1458                         rc = -ENOMEM;
1459                         goto bad;
1460                 }
1461                 memset(newgenfs, 0, sizeof(*newgenfs));
1462
1463                 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1464                 if (!newgenfs->fstype) {
1465                         rc = -ENOMEM;
1466                         kfree(newgenfs);
1467                         goto bad;
1468                 }
1469                 memcpy(newgenfs->fstype, buf, len);
1470                 newgenfs->fstype[len] = 0;
1471                 for (genfs_p = NULL, genfs = p->genfs; genfs;
1472                      genfs_p = genfs, genfs = genfs->next) {
1473                         if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1474                                 printk(KERN_ERR "security:  dup genfs "
1475                                        "fstype %s\n", newgenfs->fstype);
1476                                 kfree(newgenfs->fstype);
1477                                 kfree(newgenfs);
1478                                 goto bad;
1479                         }
1480                         if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1481                                 break;
1482                 }
1483                 newgenfs->next = genfs;
1484                 if (genfs_p)
1485                         genfs_p->next = newgenfs;
1486                 else
1487                         p->genfs = newgenfs;
1488                 buf = next_entry(fp, sizeof(u32));
1489                 if (!buf)
1490                         goto bad;
1491                 nel2 = le32_to_cpu(buf[0]);
1492                 for (j = 0; j < nel2; j++) {
1493                         buf = next_entry(fp, sizeof(u32));
1494                         if (!buf)
1495                                 goto bad;
1496                         len = le32_to_cpu(buf[0]);
1497                         buf = next_entry(fp, len);
1498                         if (!buf)
1499                                 goto bad;
1500
1501                         newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1502                         if (!newc) {
1503                                 rc = -ENOMEM;
1504                                 goto bad;
1505                         }
1506                         memset(newc, 0, sizeof(*newc));
1507
1508                         newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1509                         if (!newc->u.name) {
1510                                 rc = -ENOMEM;
1511                                 goto bad_newc;
1512                         }
1513                         memcpy(newc->u.name, buf, len);
1514                         newc->u.name[len] = 0;
1515                         buf = next_entry(fp, sizeof(u32));
1516                         if (!buf)
1517                                 goto bad_newc;
1518                         newc->v.sclass = le32_to_cpu(buf[0]);
1519                         if (context_read_and_validate(&newc->context[0], p, fp))
1520                                 goto bad_newc;
1521                         for (l = NULL, c = newgenfs->head; c;
1522                              l = c, c = c->next) {
1523                                 if (!strcmp(newc->u.name, c->u.name) &&
1524                                     (!c->v.sclass || !newc->v.sclass ||
1525                                      newc->v.sclass == c->v.sclass)) {
1526                                         printk(KERN_ERR "security:  dup genfs "
1527                                                "entry (%s,%s)\n",
1528                                                newgenfs->fstype, c->u.name);
1529                                         goto bad_newc;
1530                                 }
1531                                 len = strlen(newc->u.name);
1532                                 len2 = strlen(c->u.name);
1533                                 if (len > len2)
1534                                         break;
1535                         }
1536
1537                         newc->next = c;
1538                         if (l)
1539                                 l->next = newc;
1540                         else
1541                                 newgenfs->head = newc;
1542                 }
1543         }
1544
1545         rc = mls_read_trusted(p, fp);
1546         if (rc)
1547                 goto bad;
1548 out:
1549         return rc;
1550 bad_newc:
1551         ocontext_destroy(newc,OCON_FSUSE);
1552 bad:
1553         policydb_destroy(p);
1554         goto out;
1555 }