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