blob: 8e6262d12aa9bfaa97d3641946d6ae32fa132e54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the policy database.
3 *
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
6
7/*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 *
10 * Support for enhanced MLS infrastructure.
11 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
14 * Added conditional policy language extensions
15 *
16 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
21 */
22
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/string.h>
26#include <linux/errno.h>
27#include "security.h"
28
29#include "policydb.h"
30#include "conditional.h"
31#include "mls.h"
32
33#define _DEBUG_HASHES
34
35#ifdef DEBUG_HASHES
36static char *symtab_name[SYM_NUM] = {
37 "common prefixes",
38 "classes",
39 "roles",
40 "types",
41 "users",
42 "bools",
43 "levels",
44 "categories",
45};
46#endif
47
48int selinux_mls_enabled = 0;
49
50static unsigned int symtab_sizes[SYM_NUM] = {
51 2,
52 32,
53 16,
54 512,
55 128,
56 16,
57 16,
58 16,
59};
60
61struct policydb_compat_info {
62 int version;
63 int sym_num;
64 int ocon_num;
65};
66
67/* These need to be updated if SYM_NUM or OCON_NUM changes */
68static struct policydb_compat_info policydb_compat[] = {
69 {
70 .version = POLICYDB_VERSION_BASE,
71 .sym_num = SYM_NUM - 3,
72 .ocon_num = OCON_NUM - 1,
73 },
74 {
75 .version = POLICYDB_VERSION_BOOL,
76 .sym_num = SYM_NUM - 2,
77 .ocon_num = OCON_NUM - 1,
78 },
79 {
80 .version = POLICYDB_VERSION_IPV6,
81 .sym_num = SYM_NUM - 2,
82 .ocon_num = OCON_NUM,
83 },
84 {
85 .version = POLICYDB_VERSION_NLCLASS,
86 .sym_num = SYM_NUM - 2,
87 .ocon_num = OCON_NUM,
88 },
89 {
90 .version = POLICYDB_VERSION_MLS,
91 .sym_num = SYM_NUM,
92 .ocon_num = OCON_NUM,
93 },
Stephen Smalley782ebb92005-09-03 15:55:16 -070094 {
95 .version = POLICYDB_VERSION_AVTAB,
96 .sym_num = SYM_NUM,
97 .ocon_num = OCON_NUM,
98 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101static struct policydb_compat_info *policydb_lookup_compat(int version)
102{
103 int i;
104 struct policydb_compat_info *info = NULL;
105
106 for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
107 if (policydb_compat[i].version == version) {
108 info = &policydb_compat[i];
109 break;
110 }
111 }
112 return info;
113}
114
115/*
116 * Initialize the role table.
117 */
118static int roles_init(struct policydb *p)
119{
120 char *key = NULL;
121 int rc;
122 struct role_datum *role;
123
124 role = kmalloc(sizeof(*role), GFP_KERNEL);
125 if (!role) {
126 rc = -ENOMEM;
127 goto out;
128 }
129 memset(role, 0, sizeof(*role));
130 role->value = ++p->p_roles.nprim;
131 if (role->value != OBJECT_R_VAL) {
132 rc = -EINVAL;
133 goto out_free_role;
134 }
135 key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
136 if (!key) {
137 rc = -ENOMEM;
138 goto out_free_role;
139 }
140 strcpy(key, OBJECT_R);
141 rc = hashtab_insert(p->p_roles.table, key, role);
142 if (rc)
143 goto out_free_key;
144out:
145 return rc;
146
147out_free_key:
148 kfree(key);
149out_free_role:
150 kfree(role);
151 goto out;
152}
153
154/*
155 * Initialize a policy database structure.
156 */
157static int policydb_init(struct policydb *p)
158{
159 int i, rc;
160
161 memset(p, 0, sizeof(*p));
162
163 for (i = 0; i < SYM_NUM; i++) {
164 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
165 if (rc)
166 goto out_free_symtab;
167 }
168
169 rc = avtab_init(&p->te_avtab);
170 if (rc)
171 goto out_free_symtab;
172
173 rc = roles_init(p);
174 if (rc)
175 goto out_free_avtab;
176
177 rc = cond_policydb_init(p);
178 if (rc)
179 goto out_free_avtab;
180
181out:
182 return rc;
183
184out_free_avtab:
185 avtab_destroy(&p->te_avtab);
186
187out_free_symtab:
188 for (i = 0; i < SYM_NUM; i++)
189 hashtab_destroy(p->symtab[i].table);
190 goto out;
191}
192
193/*
194 * The following *_index functions are used to
195 * define the val_to_name and val_to_struct arrays
196 * in a policy database structure. The val_to_name
197 * arrays are used when converting security context
198 * structures into string representations. The
199 * val_to_struct arrays are used when the attributes
200 * of a class, role, or user are needed.
201 */
202
203static int common_index(void *key, void *datum, void *datap)
204{
205 struct policydb *p;
206 struct common_datum *comdatum;
207
208 comdatum = datum;
209 p = datap;
210 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
211 return -EINVAL;
212 p->p_common_val_to_name[comdatum->value - 1] = key;
213 return 0;
214}
215
216static int class_index(void *key, void *datum, void *datap)
217{
218 struct policydb *p;
219 struct class_datum *cladatum;
220
221 cladatum = datum;
222 p = datap;
223 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
224 return -EINVAL;
225 p->p_class_val_to_name[cladatum->value - 1] = key;
226 p->class_val_to_struct[cladatum->value - 1] = cladatum;
227 return 0;
228}
229
230static int role_index(void *key, void *datum, void *datap)
231{
232 struct policydb *p;
233 struct role_datum *role;
234
235 role = datum;
236 p = datap;
237 if (!role->value || role->value > p->p_roles.nprim)
238 return -EINVAL;
239 p->p_role_val_to_name[role->value - 1] = key;
240 p->role_val_to_struct[role->value - 1] = role;
241 return 0;
242}
243
244static int type_index(void *key, void *datum, void *datap)
245{
246 struct policydb *p;
247 struct type_datum *typdatum;
248
249 typdatum = datum;
250 p = datap;
251
252 if (typdatum->primary) {
253 if (!typdatum->value || typdatum->value > p->p_types.nprim)
254 return -EINVAL;
255 p->p_type_val_to_name[typdatum->value - 1] = key;
256 }
257
258 return 0;
259}
260
261static int user_index(void *key, void *datum, void *datap)
262{
263 struct policydb *p;
264 struct user_datum *usrdatum;
265
266 usrdatum = datum;
267 p = datap;
268 if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
269 return -EINVAL;
270 p->p_user_val_to_name[usrdatum->value - 1] = key;
271 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
272 return 0;
273}
274
275static int sens_index(void *key, void *datum, void *datap)
276{
277 struct policydb *p;
278 struct level_datum *levdatum;
279
280 levdatum = datum;
281 p = datap;
282
283 if (!levdatum->isalias) {
284 if (!levdatum->level->sens ||
285 levdatum->level->sens > p->p_levels.nprim)
286 return -EINVAL;
287 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
288 }
289
290 return 0;
291}
292
293static int cat_index(void *key, void *datum, void *datap)
294{
295 struct policydb *p;
296 struct cat_datum *catdatum;
297
298 catdatum = datum;
299 p = datap;
300
301 if (!catdatum->isalias) {
302 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
303 return -EINVAL;
304 p->p_cat_val_to_name[catdatum->value - 1] = key;
305 }
306
307 return 0;
308}
309
310static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
311{
312 common_index,
313 class_index,
314 role_index,
315 type_index,
316 user_index,
317 cond_index_bool,
318 sens_index,
319 cat_index,
320};
321
322/*
323 * Define the common val_to_name array and the class
324 * val_to_name and val_to_struct arrays in a policy
325 * database structure.
326 *
327 * Caller must clean up upon failure.
328 */
329static int policydb_index_classes(struct policydb *p)
330{
331 int rc;
332
333 p->p_common_val_to_name =
334 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
335 if (!p->p_common_val_to_name) {
336 rc = -ENOMEM;
337 goto out;
338 }
339
340 rc = hashtab_map(p->p_commons.table, common_index, p);
341 if (rc)
342 goto out;
343
344 p->class_val_to_struct =
345 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
346 if (!p->class_val_to_struct) {
347 rc = -ENOMEM;
348 goto out;
349 }
350
351 p->p_class_val_to_name =
352 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
353 if (!p->p_class_val_to_name) {
354 rc = -ENOMEM;
355 goto out;
356 }
357
358 rc = hashtab_map(p->p_classes.table, class_index, p);
359out:
360 return rc;
361}
362
363#ifdef DEBUG_HASHES
364static void symtab_hash_eval(struct symtab *s)
365{
366 int i;
367
368 for (i = 0; i < SYM_NUM; i++) {
369 struct hashtab *h = s[i].table;
370 struct hashtab_info info;
371
372 hashtab_stat(h, &info);
373 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
374 "longest chain length %d\n", symtab_name[i], h->nel,
375 info.slots_used, h->size, info.max_chain_len);
376 }
377}
378#endif
379
380/*
381 * Define the other val_to_name and val_to_struct arrays
382 * in a policy database structure.
383 *
384 * Caller must clean up on failure.
385 */
386static int policydb_index_others(struct policydb *p)
387{
388 int i, rc = 0;
389
390 printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
391 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
392 if (selinux_mls_enabled)
393 printk(", %d sens, %d cats", p->p_levels.nprim,
394 p->p_cats.nprim);
395 printk("\n");
396
397 printk(KERN_INFO "security: %d classes, %d rules\n",
398 p->p_classes.nprim, p->te_avtab.nel);
399
400#ifdef DEBUG_HASHES
401 avtab_hash_eval(&p->te_avtab, "rules");
402 symtab_hash_eval(p->symtab);
403#endif
404
405 p->role_val_to_struct =
406 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
407 GFP_KERNEL);
408 if (!p->role_val_to_struct) {
409 rc = -ENOMEM;
410 goto out;
411 }
412
413 p->user_val_to_struct =
414 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
415 GFP_KERNEL);
416 if (!p->user_val_to_struct) {
417 rc = -ENOMEM;
418 goto out;
419 }
420
421 if (cond_init_bool_indexes(p)) {
422 rc = -ENOMEM;
423 goto out;
424 }
425
426 for (i = SYM_ROLES; i < SYM_NUM; i++) {
427 p->sym_val_to_name[i] =
428 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
429 if (!p->sym_val_to_name[i]) {
430 rc = -ENOMEM;
431 goto out;
432 }
433 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
434 if (rc)
435 goto out;
436 }
437
438out:
439 return rc;
440}
441
442/*
443 * The following *_destroy functions are used to
444 * free any memory allocated for each kind of
445 * symbol data in the policy database.
446 */
447
448static int perm_destroy(void *key, void *datum, void *p)
449{
450 kfree(key);
451 kfree(datum);
452 return 0;
453}
454
455static int common_destroy(void *key, void *datum, void *p)
456{
457 struct common_datum *comdatum;
458
459 kfree(key);
460 comdatum = datum;
461 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
462 hashtab_destroy(comdatum->permissions.table);
463 kfree(datum);
464 return 0;
465}
466
467static int class_destroy(void *key, void *datum, void *p)
468{
469 struct class_datum *cladatum;
470 struct constraint_node *constraint, *ctemp;
471 struct constraint_expr *e, *etmp;
472
473 kfree(key);
474 cladatum = datum;
475 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
476 hashtab_destroy(cladatum->permissions.table);
477 constraint = cladatum->constraints;
478 while (constraint) {
479 e = constraint->expr;
480 while (e) {
481 ebitmap_destroy(&e->names);
482 etmp = e;
483 e = e->next;
484 kfree(etmp);
485 }
486 ctemp = constraint;
487 constraint = constraint->next;
488 kfree(ctemp);
489 }
490
491 constraint = cladatum->validatetrans;
492 while (constraint) {
493 e = constraint->expr;
494 while (e) {
495 ebitmap_destroy(&e->names);
496 etmp = e;
497 e = e->next;
498 kfree(etmp);
499 }
500 ctemp = constraint;
501 constraint = constraint->next;
502 kfree(ctemp);
503 }
504
505 kfree(cladatum->comkey);
506 kfree(datum);
507 return 0;
508}
509
510static int role_destroy(void *key, void *datum, void *p)
511{
512 struct role_datum *role;
513
514 kfree(key);
515 role = datum;
516 ebitmap_destroy(&role->dominates);
517 ebitmap_destroy(&role->types);
518 kfree(datum);
519 return 0;
520}
521
522static int type_destroy(void *key, void *datum, void *p)
523{
524 kfree(key);
525 kfree(datum);
526 return 0;
527}
528
529static int user_destroy(void *key, void *datum, void *p)
530{
531 struct user_datum *usrdatum;
532
533 kfree(key);
534 usrdatum = datum;
535 ebitmap_destroy(&usrdatum->roles);
536 ebitmap_destroy(&usrdatum->range.level[0].cat);
537 ebitmap_destroy(&usrdatum->range.level[1].cat);
538 ebitmap_destroy(&usrdatum->dfltlevel.cat);
539 kfree(datum);
540 return 0;
541}
542
543static int sens_destroy(void *key, void *datum, void *p)
544{
545 struct level_datum *levdatum;
546
547 kfree(key);
548 levdatum = datum;
549 ebitmap_destroy(&levdatum->level->cat);
550 kfree(levdatum->level);
551 kfree(datum);
552 return 0;
553}
554
555static int cat_destroy(void *key, void *datum, void *p)
556{
557 kfree(key);
558 kfree(datum);
559 return 0;
560}
561
562static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
563{
564 common_destroy,
565 class_destroy,
566 role_destroy,
567 type_destroy,
568 user_destroy,
569 cond_destroy_bool,
570 sens_destroy,
571 cat_destroy,
572};
573
574static void ocontext_destroy(struct ocontext *c, int i)
575{
576 context_destroy(&c->context[0]);
577 context_destroy(&c->context[1]);
578 if (i == OCON_ISID || i == OCON_FS ||
579 i == OCON_NETIF || i == OCON_FSUSE)
580 kfree(c->u.name);
581 kfree(c);
582}
583
584/*
585 * Free any memory allocated by a policy database structure.
586 */
587void policydb_destroy(struct policydb *p)
588{
589 struct ocontext *c, *ctmp;
590 struct genfs *g, *gtmp;
591 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700592 struct role_allow *ra, *lra = NULL;
593 struct role_trans *tr, *ltr = NULL;
594 struct range_trans *rt, *lrt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 for (i = 0; i < SYM_NUM; i++) {
597 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
598 hashtab_destroy(p->symtab[i].table);
599 }
600
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700601 for (i = 0; i < SYM_NUM; i++)
602 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700604 kfree(p->class_val_to_struct);
605 kfree(p->role_val_to_struct);
606 kfree(p->user_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 avtab_destroy(&p->te_avtab);
609
610 for (i = 0; i < OCON_NUM; i++) {
611 c = p->ocontexts[i];
612 while (c) {
613 ctmp = c;
614 c = c->next;
615 ocontext_destroy(ctmp,i);
616 }
617 }
618
619 g = p->genfs;
620 while (g) {
621 kfree(g->fstype);
622 c = g->head;
623 while (c) {
624 ctmp = c;
625 c = c->next;
626 ocontext_destroy(ctmp,OCON_FSUSE);
627 }
628 gtmp = g;
629 g = g->next;
630 kfree(gtmp);
631 }
632
633 cond_policydb_destroy(p);
634
Stephen Smalley782ebb92005-09-03 15:55:16 -0700635 for (tr = p->role_tr; tr; tr = tr->next) {
636 if (ltr) kfree(ltr);
637 ltr = tr;
638 }
639 if (ltr) kfree(ltr);
640
641 for (ra = p->role_allow; ra; ra = ra -> next) {
642 if (lra) kfree(lra);
643 lra = ra;
644 }
645 if (lra) kfree(lra);
646
647 for (rt = p->range_tr; rt; rt = rt -> next) {
648 if (lrt) kfree(lrt);
649 lrt = rt;
650 }
651 if (lrt) kfree(lrt);
652
Stephen Smalley282c1f52005-10-23 12:57:15 -0700653 if (p->type_attr_map) {
654 for (i = 0; i < p->p_types.nprim; i++)
655 ebitmap_destroy(&p->type_attr_map[i]);
656 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700657 kfree(p->type_attr_map);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660}
661
662/*
663 * Load the initial SIDs specified in a policy database
664 * structure into a SID table.
665 */
666int policydb_load_isids(struct policydb *p, struct sidtab *s)
667{
668 struct ocontext *head, *c;
669 int rc;
670
671 rc = sidtab_init(s);
672 if (rc) {
673 printk(KERN_ERR "security: out of memory on SID table init\n");
674 goto out;
675 }
676
677 head = p->ocontexts[OCON_ISID];
678 for (c = head; c; c = c->next) {
679 if (!c->context[0].user) {
680 printk(KERN_ERR "security: SID %s was never "
681 "defined.\n", c->u.name);
682 rc = -EINVAL;
683 goto out;
684 }
685 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
686 printk(KERN_ERR "security: unable to load initial "
687 "SID %s.\n", c->u.name);
688 rc = -EINVAL;
689 goto out;
690 }
691 }
692out:
693 return rc;
694}
695
696/*
697 * Return 1 if the fields in the security context
698 * structure `c' are valid. Return 0 otherwise.
699 */
700int policydb_context_isvalid(struct policydb *p, struct context *c)
701{
702 struct role_datum *role;
703 struct user_datum *usrdatum;
704
705 if (!c->role || c->role > p->p_roles.nprim)
706 return 0;
707
708 if (!c->user || c->user > p->p_users.nprim)
709 return 0;
710
711 if (!c->type || c->type > p->p_types.nprim)
712 return 0;
713
714 if (c->role != OBJECT_R_VAL) {
715 /*
716 * Role must be authorized for the type.
717 */
718 role = p->role_val_to_struct[c->role - 1];
719 if (!ebitmap_get_bit(&role->types,
720 c->type - 1))
721 /* role may not be associated with type */
722 return 0;
723
724 /*
725 * User must be authorized for the role.
726 */
727 usrdatum = p->user_val_to_struct[c->user - 1];
728 if (!usrdatum)
729 return 0;
730
731 if (!ebitmap_get_bit(&usrdatum->roles,
732 c->role - 1))
733 /* user may not be associated with role */
734 return 0;
735 }
736
737 if (!mls_context_isvalid(p, c))
738 return 0;
739
740 return 1;
741}
742
743/*
744 * Read a MLS range structure from a policydb binary
745 * representation file.
746 */
747static int mls_read_range_helper(struct mls_range *r, void *fp)
748{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700749 __le32 buf[2];
750 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int rc;
752
753 rc = next_entry(buf, fp, sizeof(u32));
754 if (rc < 0)
755 goto out;
756
757 items = le32_to_cpu(buf[0]);
758 if (items > ARRAY_SIZE(buf)) {
759 printk(KERN_ERR "security: mls: range overflow\n");
760 rc = -EINVAL;
761 goto out;
762 }
763 rc = next_entry(buf, fp, sizeof(u32) * items);
764 if (rc < 0) {
765 printk(KERN_ERR "security: mls: truncated range\n");
766 goto out;
767 }
768 r->level[0].sens = le32_to_cpu(buf[0]);
769 if (items > 1)
770 r->level[1].sens = le32_to_cpu(buf[1]);
771 else
772 r->level[1].sens = r->level[0].sens;
773
774 rc = ebitmap_read(&r->level[0].cat, fp);
775 if (rc) {
776 printk(KERN_ERR "security: mls: error reading low "
777 "categories\n");
778 goto out;
779 }
780 if (items > 1) {
781 rc = ebitmap_read(&r->level[1].cat, fp);
782 if (rc) {
783 printk(KERN_ERR "security: mls: error reading high "
784 "categories\n");
785 goto bad_high;
786 }
787 } else {
788 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
789 if (rc) {
790 printk(KERN_ERR "security: mls: out of memory\n");
791 goto bad_high;
792 }
793 }
794
795 rc = 0;
796out:
797 return rc;
798bad_high:
799 ebitmap_destroy(&r->level[0].cat);
800 goto out;
801}
802
803/*
804 * Read and validate a security context structure
805 * from a policydb binary representation file.
806 */
807static int context_read_and_validate(struct context *c,
808 struct policydb *p,
809 void *fp)
810{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700811 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int rc;
813
814 rc = next_entry(buf, fp, sizeof buf);
815 if (rc < 0) {
816 printk(KERN_ERR "security: context truncated\n");
817 goto out;
818 }
819 c->user = le32_to_cpu(buf[0]);
820 c->role = le32_to_cpu(buf[1]);
821 c->type = le32_to_cpu(buf[2]);
822 if (p->policyvers >= POLICYDB_VERSION_MLS) {
823 if (mls_read_range_helper(&c->range, fp)) {
824 printk(KERN_ERR "security: error reading MLS range of "
825 "context\n");
826 rc = -EINVAL;
827 goto out;
828 }
829 }
830
831 if (!policydb_context_isvalid(p, c)) {
832 printk(KERN_ERR "security: invalid security context\n");
833 context_destroy(c);
834 rc = -EINVAL;
835 }
836out:
837 return rc;
838}
839
840/*
841 * The following *_read functions are used to
842 * read the symbol data from a policy database
843 * binary representation file.
844 */
845
846static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
847{
848 char *key = NULL;
849 struct perm_datum *perdatum;
850 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700851 __le32 buf[2];
852 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
855 if (!perdatum) {
856 rc = -ENOMEM;
857 goto out;
858 }
859 memset(perdatum, 0, sizeof(*perdatum));
860
861 rc = next_entry(buf, fp, sizeof buf);
862 if (rc < 0)
863 goto bad;
864
865 len = le32_to_cpu(buf[0]);
866 perdatum->value = le32_to_cpu(buf[1]);
867
868 key = kmalloc(len + 1,GFP_KERNEL);
869 if (!key) {
870 rc = -ENOMEM;
871 goto bad;
872 }
873 rc = next_entry(key, fp, len);
874 if (rc < 0)
875 goto bad;
876 key[len] = 0;
877
878 rc = hashtab_insert(h, key, perdatum);
879 if (rc)
880 goto bad;
881out:
882 return rc;
883bad:
884 perm_destroy(key, perdatum, NULL);
885 goto out;
886}
887
888static int common_read(struct policydb *p, struct hashtab *h, void *fp)
889{
890 char *key = NULL;
891 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700892 __le32 buf[4];
893 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int i, rc;
895
896 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
897 if (!comdatum) {
898 rc = -ENOMEM;
899 goto out;
900 }
901 memset(comdatum, 0, sizeof(*comdatum));
902
903 rc = next_entry(buf, fp, sizeof buf);
904 if (rc < 0)
905 goto bad;
906
907 len = le32_to_cpu(buf[0]);
908 comdatum->value = le32_to_cpu(buf[1]);
909
910 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
911 if (rc)
912 goto bad;
913 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
914 nel = le32_to_cpu(buf[3]);
915
916 key = kmalloc(len + 1,GFP_KERNEL);
917 if (!key) {
918 rc = -ENOMEM;
919 goto bad;
920 }
921 rc = next_entry(key, fp, len);
922 if (rc < 0)
923 goto bad;
924 key[len] = 0;
925
926 for (i = 0; i < nel; i++) {
927 rc = perm_read(p, comdatum->permissions.table, fp);
928 if (rc)
929 goto bad;
930 }
931
932 rc = hashtab_insert(h, key, comdatum);
933 if (rc)
934 goto bad;
935out:
936 return rc;
937bad:
938 common_destroy(key, comdatum, NULL);
939 goto out;
940}
941
942static int read_cons_helper(struct constraint_node **nodep, int ncons,
943 int allowxtarget, void *fp)
944{
945 struct constraint_node *c, *lc;
946 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700947 __le32 buf[3];
948 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int rc, i, j, depth;
950
951 lc = NULL;
952 for (i = 0; i < ncons; i++) {
953 c = kmalloc(sizeof(*c), GFP_KERNEL);
954 if (!c)
955 return -ENOMEM;
956 memset(c, 0, sizeof(*c));
957
958 if (lc) {
959 lc->next = c;
960 } else {
961 *nodep = c;
962 }
963
964 rc = next_entry(buf, fp, (sizeof(u32) * 2));
965 if (rc < 0)
966 return rc;
967 c->permissions = le32_to_cpu(buf[0]);
968 nexpr = le32_to_cpu(buf[1]);
969 le = NULL;
970 depth = -1;
971 for (j = 0; j < nexpr; j++) {
972 e = kmalloc(sizeof(*e), GFP_KERNEL);
973 if (!e)
974 return -ENOMEM;
975 memset(e, 0, sizeof(*e));
976
977 if (le) {
978 le->next = e;
979 } else {
980 c->expr = e;
981 }
982
983 rc = next_entry(buf, fp, (sizeof(u32) * 3));
984 if (rc < 0)
985 return rc;
986 e->expr_type = le32_to_cpu(buf[0]);
987 e->attr = le32_to_cpu(buf[1]);
988 e->op = le32_to_cpu(buf[2]);
989
990 switch (e->expr_type) {
991 case CEXPR_NOT:
992 if (depth < 0)
993 return -EINVAL;
994 break;
995 case CEXPR_AND:
996 case CEXPR_OR:
997 if (depth < 1)
998 return -EINVAL;
999 depth--;
1000 break;
1001 case CEXPR_ATTR:
1002 if (depth == (CEXPR_MAXDEPTH - 1))
1003 return -EINVAL;
1004 depth++;
1005 break;
1006 case CEXPR_NAMES:
1007 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1008 return -EINVAL;
1009 if (depth == (CEXPR_MAXDEPTH - 1))
1010 return -EINVAL;
1011 depth++;
1012 if (ebitmap_read(&e->names, fp))
1013 return -EINVAL;
1014 break;
1015 default:
1016 return -EINVAL;
1017 }
1018 le = e;
1019 }
1020 if (depth != 0)
1021 return -EINVAL;
1022 lc = c;
1023 }
1024
1025 return 0;
1026}
1027
1028static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1029{
1030 char *key = NULL;
1031 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001032 __le32 buf[6];
1033 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int i, rc;
1035
1036 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1037 if (!cladatum) {
1038 rc = -ENOMEM;
1039 goto out;
1040 }
1041 memset(cladatum, 0, sizeof(*cladatum));
1042
1043 rc = next_entry(buf, fp, sizeof(u32)*6);
1044 if (rc < 0)
1045 goto bad;
1046
1047 len = le32_to_cpu(buf[0]);
1048 len2 = le32_to_cpu(buf[1]);
1049 cladatum->value = le32_to_cpu(buf[2]);
1050
1051 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1052 if (rc)
1053 goto bad;
1054 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1055 nel = le32_to_cpu(buf[4]);
1056
1057 ncons = le32_to_cpu(buf[5]);
1058
1059 key = kmalloc(len + 1,GFP_KERNEL);
1060 if (!key) {
1061 rc = -ENOMEM;
1062 goto bad;
1063 }
1064 rc = next_entry(key, fp, len);
1065 if (rc < 0)
1066 goto bad;
1067 key[len] = 0;
1068
1069 if (len2) {
1070 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1071 if (!cladatum->comkey) {
1072 rc = -ENOMEM;
1073 goto bad;
1074 }
1075 rc = next_entry(cladatum->comkey, fp, len2);
1076 if (rc < 0)
1077 goto bad;
1078 cladatum->comkey[len2] = 0;
1079
1080 cladatum->comdatum = hashtab_search(p->p_commons.table,
1081 cladatum->comkey);
1082 if (!cladatum->comdatum) {
1083 printk(KERN_ERR "security: unknown common %s\n",
1084 cladatum->comkey);
1085 rc = -EINVAL;
1086 goto bad;
1087 }
1088 }
1089 for (i = 0; i < nel; i++) {
1090 rc = perm_read(p, cladatum->permissions.table, fp);
1091 if (rc)
1092 goto bad;
1093 }
1094
1095 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1096 if (rc)
1097 goto bad;
1098
1099 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1100 /* grab the validatetrans rules */
1101 rc = next_entry(buf, fp, sizeof(u32));
1102 if (rc < 0)
1103 goto bad;
1104 ncons = le32_to_cpu(buf[0]);
1105 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1106 if (rc)
1107 goto bad;
1108 }
1109
1110 rc = hashtab_insert(h, key, cladatum);
1111 if (rc)
1112 goto bad;
1113
1114 rc = 0;
1115out:
1116 return rc;
1117bad:
1118 class_destroy(key, cladatum, NULL);
1119 goto out;
1120}
1121
1122static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1123{
1124 char *key = NULL;
1125 struct role_datum *role;
1126 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001127 __le32 buf[2];
1128 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 role = kmalloc(sizeof(*role), GFP_KERNEL);
1131 if (!role) {
1132 rc = -ENOMEM;
1133 goto out;
1134 }
1135 memset(role, 0, sizeof(*role));
1136
1137 rc = next_entry(buf, fp, sizeof buf);
1138 if (rc < 0)
1139 goto bad;
1140
1141 len = le32_to_cpu(buf[0]);
1142 role->value = le32_to_cpu(buf[1]);
1143
1144 key = kmalloc(len + 1,GFP_KERNEL);
1145 if (!key) {
1146 rc = -ENOMEM;
1147 goto bad;
1148 }
1149 rc = next_entry(key, fp, len);
1150 if (rc < 0)
1151 goto bad;
1152 key[len] = 0;
1153
1154 rc = ebitmap_read(&role->dominates, fp);
1155 if (rc)
1156 goto bad;
1157
1158 rc = ebitmap_read(&role->types, fp);
1159 if (rc)
1160 goto bad;
1161
1162 if (strcmp(key, OBJECT_R) == 0) {
1163 if (role->value != OBJECT_R_VAL) {
1164 printk(KERN_ERR "Role %s has wrong value %d\n",
1165 OBJECT_R, role->value);
1166 rc = -EINVAL;
1167 goto bad;
1168 }
1169 rc = 0;
1170 goto bad;
1171 }
1172
1173 rc = hashtab_insert(h, key, role);
1174 if (rc)
1175 goto bad;
1176out:
1177 return rc;
1178bad:
1179 role_destroy(key, role, NULL);
1180 goto out;
1181}
1182
1183static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1184{
1185 char *key = NULL;
1186 struct type_datum *typdatum;
1187 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001188 __le32 buf[3];
1189 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1192 if (!typdatum) {
1193 rc = -ENOMEM;
1194 return rc;
1195 }
1196 memset(typdatum, 0, sizeof(*typdatum));
1197
1198 rc = next_entry(buf, fp, sizeof buf);
1199 if (rc < 0)
1200 goto bad;
1201
1202 len = le32_to_cpu(buf[0]);
1203 typdatum->value = le32_to_cpu(buf[1]);
1204 typdatum->primary = le32_to_cpu(buf[2]);
1205
1206 key = kmalloc(len + 1,GFP_KERNEL);
1207 if (!key) {
1208 rc = -ENOMEM;
1209 goto bad;
1210 }
1211 rc = next_entry(key, fp, len);
1212 if (rc < 0)
1213 goto bad;
1214 key[len] = 0;
1215
1216 rc = hashtab_insert(h, key, typdatum);
1217 if (rc)
1218 goto bad;
1219out:
1220 return rc;
1221bad:
1222 type_destroy(key, typdatum, NULL);
1223 goto out;
1224}
1225
1226
1227/*
1228 * Read a MLS level structure from a policydb binary
1229 * representation file.
1230 */
1231static int mls_read_level(struct mls_level *lp, void *fp)
1232{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001233 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int rc;
1235
1236 memset(lp, 0, sizeof(*lp));
1237
1238 rc = next_entry(buf, fp, sizeof buf);
1239 if (rc < 0) {
1240 printk(KERN_ERR "security: mls: truncated level\n");
1241 goto bad;
1242 }
1243 lp->sens = le32_to_cpu(buf[0]);
1244
1245 if (ebitmap_read(&lp->cat, fp)) {
1246 printk(KERN_ERR "security: mls: error reading level "
1247 "categories\n");
1248 goto bad;
1249 }
1250 return 0;
1251
1252bad:
1253 return -EINVAL;
1254}
1255
1256static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1257{
1258 char *key = NULL;
1259 struct user_datum *usrdatum;
1260 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001261 __le32 buf[2];
1262 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1265 if (!usrdatum) {
1266 rc = -ENOMEM;
1267 goto out;
1268 }
1269 memset(usrdatum, 0, sizeof(*usrdatum));
1270
1271 rc = next_entry(buf, fp, sizeof buf);
1272 if (rc < 0)
1273 goto bad;
1274
1275 len = le32_to_cpu(buf[0]);
1276 usrdatum->value = le32_to_cpu(buf[1]);
1277
1278 key = kmalloc(len + 1,GFP_KERNEL);
1279 if (!key) {
1280 rc = -ENOMEM;
1281 goto bad;
1282 }
1283 rc = next_entry(key, fp, len);
1284 if (rc < 0)
1285 goto bad;
1286 key[len] = 0;
1287
1288 rc = ebitmap_read(&usrdatum->roles, fp);
1289 if (rc)
1290 goto bad;
1291
1292 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1293 rc = mls_read_range_helper(&usrdatum->range, fp);
1294 if (rc)
1295 goto bad;
1296 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1297 if (rc)
1298 goto bad;
1299 }
1300
1301 rc = hashtab_insert(h, key, usrdatum);
1302 if (rc)
1303 goto bad;
1304out:
1305 return rc;
1306bad:
1307 user_destroy(key, usrdatum, NULL);
1308 goto out;
1309}
1310
1311static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1312{
1313 char *key = NULL;
1314 struct level_datum *levdatum;
1315 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001316 __le32 buf[2];
1317 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1320 if (!levdatum) {
1321 rc = -ENOMEM;
1322 goto out;
1323 }
1324 memset(levdatum, 0, sizeof(*levdatum));
1325
1326 rc = next_entry(buf, fp, sizeof buf);
1327 if (rc < 0)
1328 goto bad;
1329
1330 len = le32_to_cpu(buf[0]);
1331 levdatum->isalias = le32_to_cpu(buf[1]);
1332
1333 key = kmalloc(len + 1,GFP_ATOMIC);
1334 if (!key) {
1335 rc = -ENOMEM;
1336 goto bad;
1337 }
1338 rc = next_entry(key, fp, len);
1339 if (rc < 0)
1340 goto bad;
1341 key[len] = 0;
1342
1343 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1344 if (!levdatum->level) {
1345 rc = -ENOMEM;
1346 goto bad;
1347 }
1348 if (mls_read_level(levdatum->level, fp)) {
1349 rc = -EINVAL;
1350 goto bad;
1351 }
1352
1353 rc = hashtab_insert(h, key, levdatum);
1354 if (rc)
1355 goto bad;
1356out:
1357 return rc;
1358bad:
1359 sens_destroy(key, levdatum, NULL);
1360 goto out;
1361}
1362
1363static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1364{
1365 char *key = NULL;
1366 struct cat_datum *catdatum;
1367 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001368 __le32 buf[3];
1369 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1372 if (!catdatum) {
1373 rc = -ENOMEM;
1374 goto out;
1375 }
1376 memset(catdatum, 0, sizeof(*catdatum));
1377
1378 rc = next_entry(buf, fp, sizeof buf);
1379 if (rc < 0)
1380 goto bad;
1381
1382 len = le32_to_cpu(buf[0]);
1383 catdatum->value = le32_to_cpu(buf[1]);
1384 catdatum->isalias = le32_to_cpu(buf[2]);
1385
1386 key = kmalloc(len + 1,GFP_ATOMIC);
1387 if (!key) {
1388 rc = -ENOMEM;
1389 goto bad;
1390 }
1391 rc = next_entry(key, fp, len);
1392 if (rc < 0)
1393 goto bad;
1394 key[len] = 0;
1395
1396 rc = hashtab_insert(h, key, catdatum);
1397 if (rc)
1398 goto bad;
1399out:
1400 return rc;
1401
1402bad:
1403 cat_destroy(key, catdatum, NULL);
1404 goto out;
1405}
1406
1407static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1408{
1409 common_read,
1410 class_read,
1411 role_read,
1412 type_read,
1413 user_read,
1414 cond_read_bool,
1415 sens_read,
1416 cat_read,
1417};
1418
1419extern int ss_initialized;
1420
1421/*
1422 * Read the configuration data from a policy database binary
1423 * representation file into a policy database structure.
1424 */
1425int policydb_read(struct policydb *p, void *fp)
1426{
1427 struct role_allow *ra, *lra;
1428 struct role_trans *tr, *ltr;
1429 struct ocontext *l, *c, *newc;
1430 struct genfs *genfs_p, *genfs, *newgenfs;
1431 int i, j, rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001432 __le32 buf[8];
1433 u32 len, len2, config, nprim, nel, nel2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 char *policydb_str;
1435 struct policydb_compat_info *info;
1436 struct range_trans *rt, *lrt;
1437
1438 config = 0;
1439
1440 rc = policydb_init(p);
1441 if (rc)
1442 goto out;
1443
1444 /* Read the magic number and string length. */
1445 rc = next_entry(buf, fp, sizeof(u32)* 2);
1446 if (rc < 0)
1447 goto bad;
1448
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001449 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 printk(KERN_ERR "security: policydb magic number 0x%x does "
1451 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001452 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto bad;
1454 }
1455
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001456 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (len != strlen(POLICYDB_STRING)) {
1458 printk(KERN_ERR "security: policydb string length %d does not "
1459 "match expected length %Zu\n",
1460 len, strlen(POLICYDB_STRING));
1461 goto bad;
1462 }
1463 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1464 if (!policydb_str) {
1465 printk(KERN_ERR "security: unable to allocate memory for policydb "
1466 "string of length %d\n", len);
1467 rc = -ENOMEM;
1468 goto bad;
1469 }
1470 rc = next_entry(policydb_str, fp, len);
1471 if (rc < 0) {
1472 printk(KERN_ERR "security: truncated policydb string identifier\n");
1473 kfree(policydb_str);
1474 goto bad;
1475 }
1476 policydb_str[len] = 0;
1477 if (strcmp(policydb_str, POLICYDB_STRING)) {
1478 printk(KERN_ERR "security: policydb string %s does not match "
1479 "my string %s\n", policydb_str, POLICYDB_STRING);
1480 kfree(policydb_str);
1481 goto bad;
1482 }
1483 /* Done with policydb_str. */
1484 kfree(policydb_str);
1485 policydb_str = NULL;
1486
1487 /* Read the version, config, and table sizes. */
1488 rc = next_entry(buf, fp, sizeof(u32)*4);
1489 if (rc < 0)
1490 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001492 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (p->policyvers < POLICYDB_VERSION_MIN ||
1494 p->policyvers > POLICYDB_VERSION_MAX) {
1495 printk(KERN_ERR "security: policydb version %d does not match "
1496 "my version range %d-%d\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001497 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto bad;
1499 }
1500
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001501 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (ss_initialized && !selinux_mls_enabled) {
1503 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1504 "policies\n");
1505 goto bad;
1506 }
1507 selinux_mls_enabled = 1;
1508 config |= POLICYDB_CONFIG_MLS;
1509
1510 if (p->policyvers < POLICYDB_VERSION_MLS) {
1511 printk(KERN_ERR "security policydb version %d (MLS) "
1512 "not backwards compatible\n", p->policyvers);
1513 goto bad;
1514 }
1515 } else {
1516 if (ss_initialized && selinux_mls_enabled) {
1517 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1518 "policies\n");
1519 goto bad;
1520 }
1521 }
1522
1523 info = policydb_lookup_compat(p->policyvers);
1524 if (!info) {
1525 printk(KERN_ERR "security: unable to find policy compat info "
1526 "for version %d\n", p->policyvers);
1527 goto bad;
1528 }
1529
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001530 if (le32_to_cpu(buf[2]) != info->sym_num ||
1531 le32_to_cpu(buf[3]) != info->ocon_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001533 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
1534 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 info->sym_num, info->ocon_num);
1536 goto bad;
1537 }
1538
1539 for (i = 0; i < info->sym_num; i++) {
1540 rc = next_entry(buf, fp, sizeof(u32)*2);
1541 if (rc < 0)
1542 goto bad;
1543 nprim = le32_to_cpu(buf[0]);
1544 nel = le32_to_cpu(buf[1]);
1545 for (j = 0; j < nel; j++) {
1546 rc = read_f[i](p, p->symtab[i].table, fp);
1547 if (rc)
1548 goto bad;
1549 }
1550
1551 p->symtab[i].nprim = nprim;
1552 }
1553
Stephen Smalley782ebb92005-09-03 15:55:16 -07001554 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (rc)
1556 goto bad;
1557
1558 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1559 rc = cond_read_list(p, fp);
1560 if (rc)
1561 goto bad;
1562 }
1563
1564 rc = next_entry(buf, fp, sizeof(u32));
1565 if (rc < 0)
1566 goto bad;
1567 nel = le32_to_cpu(buf[0]);
1568 ltr = NULL;
1569 for (i = 0; i < nel; i++) {
1570 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1571 if (!tr) {
1572 rc = -ENOMEM;
1573 goto bad;
1574 }
1575 memset(tr, 0, sizeof(*tr));
1576 if (ltr) {
1577 ltr->next = tr;
1578 } else {
1579 p->role_tr = tr;
1580 }
1581 rc = next_entry(buf, fp, sizeof(u32)*3);
1582 if (rc < 0)
1583 goto bad;
1584 tr->role = le32_to_cpu(buf[0]);
1585 tr->type = le32_to_cpu(buf[1]);
1586 tr->new_role = le32_to_cpu(buf[2]);
1587 ltr = tr;
1588 }
1589
1590 rc = next_entry(buf, fp, sizeof(u32));
1591 if (rc < 0)
1592 goto bad;
1593 nel = le32_to_cpu(buf[0]);
1594 lra = NULL;
1595 for (i = 0; i < nel; i++) {
1596 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1597 if (!ra) {
1598 rc = -ENOMEM;
1599 goto bad;
1600 }
1601 memset(ra, 0, sizeof(*ra));
1602 if (lra) {
1603 lra->next = ra;
1604 } else {
1605 p->role_allow = ra;
1606 }
1607 rc = next_entry(buf, fp, sizeof(u32)*2);
1608 if (rc < 0)
1609 goto bad;
1610 ra->role = le32_to_cpu(buf[0]);
1611 ra->new_role = le32_to_cpu(buf[1]);
1612 lra = ra;
1613 }
1614
1615 rc = policydb_index_classes(p);
1616 if (rc)
1617 goto bad;
1618
1619 rc = policydb_index_others(p);
1620 if (rc)
1621 goto bad;
1622
1623 for (i = 0; i < info->ocon_num; i++) {
1624 rc = next_entry(buf, fp, sizeof(u32));
1625 if (rc < 0)
1626 goto bad;
1627 nel = le32_to_cpu(buf[0]);
1628 l = NULL;
1629 for (j = 0; j < nel; j++) {
1630 c = kmalloc(sizeof(*c), GFP_KERNEL);
1631 if (!c) {
1632 rc = -ENOMEM;
1633 goto bad;
1634 }
1635 memset(c, 0, sizeof(*c));
1636 if (l) {
1637 l->next = c;
1638 } else {
1639 p->ocontexts[i] = c;
1640 }
1641 l = c;
1642 rc = -EINVAL;
1643 switch (i) {
1644 case OCON_ISID:
1645 rc = next_entry(buf, fp, sizeof(u32));
1646 if (rc < 0)
1647 goto bad;
1648 c->sid[0] = le32_to_cpu(buf[0]);
1649 rc = context_read_and_validate(&c->context[0], p, fp);
1650 if (rc)
1651 goto bad;
1652 break;
1653 case OCON_FS:
1654 case OCON_NETIF:
1655 rc = next_entry(buf, fp, sizeof(u32));
1656 if (rc < 0)
1657 goto bad;
1658 len = le32_to_cpu(buf[0]);
1659 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1660 if (!c->u.name) {
1661 rc = -ENOMEM;
1662 goto bad;
1663 }
1664 rc = next_entry(c->u.name, fp, len);
1665 if (rc < 0)
1666 goto bad;
1667 c->u.name[len] = 0;
1668 rc = context_read_and_validate(&c->context[0], p, fp);
1669 if (rc)
1670 goto bad;
1671 rc = context_read_and_validate(&c->context[1], p, fp);
1672 if (rc)
1673 goto bad;
1674 break;
1675 case OCON_PORT:
1676 rc = next_entry(buf, fp, sizeof(u32)*3);
1677 if (rc < 0)
1678 goto bad;
1679 c->u.port.protocol = le32_to_cpu(buf[0]);
1680 c->u.port.low_port = le32_to_cpu(buf[1]);
1681 c->u.port.high_port = le32_to_cpu(buf[2]);
1682 rc = context_read_and_validate(&c->context[0], p, fp);
1683 if (rc)
1684 goto bad;
1685 break;
1686 case OCON_NODE:
1687 rc = next_entry(buf, fp, sizeof(u32)* 2);
1688 if (rc < 0)
1689 goto bad;
1690 c->u.node.addr = le32_to_cpu(buf[0]);
1691 c->u.node.mask = le32_to_cpu(buf[1]);
1692 rc = context_read_and_validate(&c->context[0], p, fp);
1693 if (rc)
1694 goto bad;
1695 break;
1696 case OCON_FSUSE:
1697 rc = next_entry(buf, fp, sizeof(u32)*2);
1698 if (rc < 0)
1699 goto bad;
1700 c->v.behavior = le32_to_cpu(buf[0]);
1701 if (c->v.behavior > SECURITY_FS_USE_NONE)
1702 goto bad;
1703 len = le32_to_cpu(buf[1]);
1704 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1705 if (!c->u.name) {
1706 rc = -ENOMEM;
1707 goto bad;
1708 }
1709 rc = next_entry(c->u.name, fp, len);
1710 if (rc < 0)
1711 goto bad;
1712 c->u.name[len] = 0;
1713 rc = context_read_and_validate(&c->context[0], p, fp);
1714 if (rc)
1715 goto bad;
1716 break;
1717 case OCON_NODE6: {
1718 int k;
1719
1720 rc = next_entry(buf, fp, sizeof(u32) * 8);
1721 if (rc < 0)
1722 goto bad;
1723 for (k = 0; k < 4; k++)
1724 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1725 for (k = 0; k < 4; k++)
1726 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1727 if (context_read_and_validate(&c->context[0], p, fp))
1728 goto bad;
1729 break;
1730 }
1731 }
1732 }
1733 }
1734
1735 rc = next_entry(buf, fp, sizeof(u32));
1736 if (rc < 0)
1737 goto bad;
1738 nel = le32_to_cpu(buf[0]);
1739 genfs_p = NULL;
1740 rc = -EINVAL;
1741 for (i = 0; i < nel; i++) {
1742 rc = next_entry(buf, fp, sizeof(u32));
1743 if (rc < 0)
1744 goto bad;
1745 len = le32_to_cpu(buf[0]);
1746 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1747 if (!newgenfs) {
1748 rc = -ENOMEM;
1749 goto bad;
1750 }
1751 memset(newgenfs, 0, sizeof(*newgenfs));
1752
1753 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1754 if (!newgenfs->fstype) {
1755 rc = -ENOMEM;
1756 kfree(newgenfs);
1757 goto bad;
1758 }
1759 rc = next_entry(newgenfs->fstype, fp, len);
1760 if (rc < 0) {
1761 kfree(newgenfs->fstype);
1762 kfree(newgenfs);
1763 goto bad;
1764 }
1765 newgenfs->fstype[len] = 0;
1766 for (genfs_p = NULL, genfs = p->genfs; genfs;
1767 genfs_p = genfs, genfs = genfs->next) {
1768 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1769 printk(KERN_ERR "security: dup genfs "
1770 "fstype %s\n", newgenfs->fstype);
1771 kfree(newgenfs->fstype);
1772 kfree(newgenfs);
1773 goto bad;
1774 }
1775 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1776 break;
1777 }
1778 newgenfs->next = genfs;
1779 if (genfs_p)
1780 genfs_p->next = newgenfs;
1781 else
1782 p->genfs = newgenfs;
1783 rc = next_entry(buf, fp, sizeof(u32));
1784 if (rc < 0)
1785 goto bad;
1786 nel2 = le32_to_cpu(buf[0]);
1787 for (j = 0; j < nel2; j++) {
1788 rc = next_entry(buf, fp, sizeof(u32));
1789 if (rc < 0)
1790 goto bad;
1791 len = le32_to_cpu(buf[0]);
1792
1793 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1794 if (!newc) {
1795 rc = -ENOMEM;
1796 goto bad;
1797 }
1798 memset(newc, 0, sizeof(*newc));
1799
1800 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1801 if (!newc->u.name) {
1802 rc = -ENOMEM;
1803 goto bad_newc;
1804 }
1805 rc = next_entry(newc->u.name, fp, len);
1806 if (rc < 0)
1807 goto bad_newc;
1808 newc->u.name[len] = 0;
1809 rc = next_entry(buf, fp, sizeof(u32));
1810 if (rc < 0)
1811 goto bad_newc;
1812 newc->v.sclass = le32_to_cpu(buf[0]);
1813 if (context_read_and_validate(&newc->context[0], p, fp))
1814 goto bad_newc;
1815 for (l = NULL, c = newgenfs->head; c;
1816 l = c, c = c->next) {
1817 if (!strcmp(newc->u.name, c->u.name) &&
1818 (!c->v.sclass || !newc->v.sclass ||
1819 newc->v.sclass == c->v.sclass)) {
1820 printk(KERN_ERR "security: dup genfs "
1821 "entry (%s,%s)\n",
1822 newgenfs->fstype, c->u.name);
1823 goto bad_newc;
1824 }
1825 len = strlen(newc->u.name);
1826 len2 = strlen(c->u.name);
1827 if (len > len2)
1828 break;
1829 }
1830
1831 newc->next = c;
1832 if (l)
1833 l->next = newc;
1834 else
1835 newgenfs->head = newc;
1836 }
1837 }
1838
1839 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1840 rc = next_entry(buf, fp, sizeof(u32));
1841 if (rc < 0)
1842 goto bad;
1843 nel = le32_to_cpu(buf[0]);
1844 lrt = NULL;
1845 for (i = 0; i < nel; i++) {
1846 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1847 if (!rt) {
1848 rc = -ENOMEM;
1849 goto bad;
1850 }
1851 memset(rt, 0, sizeof(*rt));
1852 if (lrt)
1853 lrt->next = rt;
1854 else
1855 p->range_tr = rt;
1856 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1857 if (rc < 0)
1858 goto bad;
1859 rt->dom = le32_to_cpu(buf[0]);
1860 rt->type = le32_to_cpu(buf[1]);
1861 rc = mls_read_range_helper(&rt->range, fp);
1862 if (rc)
1863 goto bad;
1864 lrt = rt;
1865 }
1866 }
1867
Stephen Smalley782ebb92005-09-03 15:55:16 -07001868 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1869 if (!p->type_attr_map)
1870 goto bad;
1871
1872 for (i = 0; i < p->p_types.nprim; i++) {
1873 ebitmap_init(&p->type_attr_map[i]);
1874 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1875 if (ebitmap_read(&p->type_attr_map[i], fp))
1876 goto bad;
1877 }
1878 /* add the type itself as the degenerate case */
1879 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1880 goto bad;
1881 }
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rc = 0;
1884out:
1885 return rc;
1886bad_newc:
1887 ocontext_destroy(newc,OCON_FSUSE);
1888bad:
1889 if (!rc)
1890 rc = -EINVAL;
1891 policydb_destroy(p);
1892 goto out;
1893}