blob: 7b03fa0f92b0be8f1beed2d718a7d1ef48ad2b1d [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
653 for (i = 0; i < p->p_types.nprim; i++)
654 ebitmap_destroy(&p->type_attr_map[i]);
655 kfree(p->type_attr_map);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return;
658}
659
660/*
661 * Load the initial SIDs specified in a policy database
662 * structure into a SID table.
663 */
664int policydb_load_isids(struct policydb *p, struct sidtab *s)
665{
666 struct ocontext *head, *c;
667 int rc;
668
669 rc = sidtab_init(s);
670 if (rc) {
671 printk(KERN_ERR "security: out of memory on SID table init\n");
672 goto out;
673 }
674
675 head = p->ocontexts[OCON_ISID];
676 for (c = head; c; c = c->next) {
677 if (!c->context[0].user) {
678 printk(KERN_ERR "security: SID %s was never "
679 "defined.\n", c->u.name);
680 rc = -EINVAL;
681 goto out;
682 }
683 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
684 printk(KERN_ERR "security: unable to load initial "
685 "SID %s.\n", c->u.name);
686 rc = -EINVAL;
687 goto out;
688 }
689 }
690out:
691 return rc;
692}
693
694/*
695 * Return 1 if the fields in the security context
696 * structure `c' are valid. Return 0 otherwise.
697 */
698int policydb_context_isvalid(struct policydb *p, struct context *c)
699{
700 struct role_datum *role;
701 struct user_datum *usrdatum;
702
703 if (!c->role || c->role > p->p_roles.nprim)
704 return 0;
705
706 if (!c->user || c->user > p->p_users.nprim)
707 return 0;
708
709 if (!c->type || c->type > p->p_types.nprim)
710 return 0;
711
712 if (c->role != OBJECT_R_VAL) {
713 /*
714 * Role must be authorized for the type.
715 */
716 role = p->role_val_to_struct[c->role - 1];
717 if (!ebitmap_get_bit(&role->types,
718 c->type - 1))
719 /* role may not be associated with type */
720 return 0;
721
722 /*
723 * User must be authorized for the role.
724 */
725 usrdatum = p->user_val_to_struct[c->user - 1];
726 if (!usrdatum)
727 return 0;
728
729 if (!ebitmap_get_bit(&usrdatum->roles,
730 c->role - 1))
731 /* user may not be associated with role */
732 return 0;
733 }
734
735 if (!mls_context_isvalid(p, c))
736 return 0;
737
738 return 1;
739}
740
741/*
742 * Read a MLS range structure from a policydb binary
743 * representation file.
744 */
745static int mls_read_range_helper(struct mls_range *r, void *fp)
746{
747 u32 buf[2], items;
748 int rc;
749
750 rc = next_entry(buf, fp, sizeof(u32));
751 if (rc < 0)
752 goto out;
753
754 items = le32_to_cpu(buf[0]);
755 if (items > ARRAY_SIZE(buf)) {
756 printk(KERN_ERR "security: mls: range overflow\n");
757 rc = -EINVAL;
758 goto out;
759 }
760 rc = next_entry(buf, fp, sizeof(u32) * items);
761 if (rc < 0) {
762 printk(KERN_ERR "security: mls: truncated range\n");
763 goto out;
764 }
765 r->level[0].sens = le32_to_cpu(buf[0]);
766 if (items > 1)
767 r->level[1].sens = le32_to_cpu(buf[1]);
768 else
769 r->level[1].sens = r->level[0].sens;
770
771 rc = ebitmap_read(&r->level[0].cat, fp);
772 if (rc) {
773 printk(KERN_ERR "security: mls: error reading low "
774 "categories\n");
775 goto out;
776 }
777 if (items > 1) {
778 rc = ebitmap_read(&r->level[1].cat, fp);
779 if (rc) {
780 printk(KERN_ERR "security: mls: error reading high "
781 "categories\n");
782 goto bad_high;
783 }
784 } else {
785 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
786 if (rc) {
787 printk(KERN_ERR "security: mls: out of memory\n");
788 goto bad_high;
789 }
790 }
791
792 rc = 0;
793out:
794 return rc;
795bad_high:
796 ebitmap_destroy(&r->level[0].cat);
797 goto out;
798}
799
800/*
801 * Read and validate a security context structure
802 * from a policydb binary representation file.
803 */
804static int context_read_and_validate(struct context *c,
805 struct policydb *p,
806 void *fp)
807{
808 u32 buf[3];
809 int rc;
810
811 rc = next_entry(buf, fp, sizeof buf);
812 if (rc < 0) {
813 printk(KERN_ERR "security: context truncated\n");
814 goto out;
815 }
816 c->user = le32_to_cpu(buf[0]);
817 c->role = le32_to_cpu(buf[1]);
818 c->type = le32_to_cpu(buf[2]);
819 if (p->policyvers >= POLICYDB_VERSION_MLS) {
820 if (mls_read_range_helper(&c->range, fp)) {
821 printk(KERN_ERR "security: error reading MLS range of "
822 "context\n");
823 rc = -EINVAL;
824 goto out;
825 }
826 }
827
828 if (!policydb_context_isvalid(p, c)) {
829 printk(KERN_ERR "security: invalid security context\n");
830 context_destroy(c);
831 rc = -EINVAL;
832 }
833out:
834 return rc;
835}
836
837/*
838 * The following *_read functions are used to
839 * read the symbol data from a policy database
840 * binary representation file.
841 */
842
843static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
844{
845 char *key = NULL;
846 struct perm_datum *perdatum;
847 int rc;
848 u32 buf[2], len;
849
850 perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
851 if (!perdatum) {
852 rc = -ENOMEM;
853 goto out;
854 }
855 memset(perdatum, 0, sizeof(*perdatum));
856
857 rc = next_entry(buf, fp, sizeof buf);
858 if (rc < 0)
859 goto bad;
860
861 len = le32_to_cpu(buf[0]);
862 perdatum->value = le32_to_cpu(buf[1]);
863
864 key = kmalloc(len + 1,GFP_KERNEL);
865 if (!key) {
866 rc = -ENOMEM;
867 goto bad;
868 }
869 rc = next_entry(key, fp, len);
870 if (rc < 0)
871 goto bad;
872 key[len] = 0;
873
874 rc = hashtab_insert(h, key, perdatum);
875 if (rc)
876 goto bad;
877out:
878 return rc;
879bad:
880 perm_destroy(key, perdatum, NULL);
881 goto out;
882}
883
884static int common_read(struct policydb *p, struct hashtab *h, void *fp)
885{
886 char *key = NULL;
887 struct common_datum *comdatum;
888 u32 buf[4], len, nel;
889 int i, rc;
890
891 comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
892 if (!comdatum) {
893 rc = -ENOMEM;
894 goto out;
895 }
896 memset(comdatum, 0, sizeof(*comdatum));
897
898 rc = next_entry(buf, fp, sizeof buf);
899 if (rc < 0)
900 goto bad;
901
902 len = le32_to_cpu(buf[0]);
903 comdatum->value = le32_to_cpu(buf[1]);
904
905 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
906 if (rc)
907 goto bad;
908 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
909 nel = le32_to_cpu(buf[3]);
910
911 key = kmalloc(len + 1,GFP_KERNEL);
912 if (!key) {
913 rc = -ENOMEM;
914 goto bad;
915 }
916 rc = next_entry(key, fp, len);
917 if (rc < 0)
918 goto bad;
919 key[len] = 0;
920
921 for (i = 0; i < nel; i++) {
922 rc = perm_read(p, comdatum->permissions.table, fp);
923 if (rc)
924 goto bad;
925 }
926
927 rc = hashtab_insert(h, key, comdatum);
928 if (rc)
929 goto bad;
930out:
931 return rc;
932bad:
933 common_destroy(key, comdatum, NULL);
934 goto out;
935}
936
937static int read_cons_helper(struct constraint_node **nodep, int ncons,
938 int allowxtarget, void *fp)
939{
940 struct constraint_node *c, *lc;
941 struct constraint_expr *e, *le;
942 u32 buf[3], nexpr;
943 int rc, i, j, depth;
944
945 lc = NULL;
946 for (i = 0; i < ncons; i++) {
947 c = kmalloc(sizeof(*c), GFP_KERNEL);
948 if (!c)
949 return -ENOMEM;
950 memset(c, 0, sizeof(*c));
951
952 if (lc) {
953 lc->next = c;
954 } else {
955 *nodep = c;
956 }
957
958 rc = next_entry(buf, fp, (sizeof(u32) * 2));
959 if (rc < 0)
960 return rc;
961 c->permissions = le32_to_cpu(buf[0]);
962 nexpr = le32_to_cpu(buf[1]);
963 le = NULL;
964 depth = -1;
965 for (j = 0; j < nexpr; j++) {
966 e = kmalloc(sizeof(*e), GFP_KERNEL);
967 if (!e)
968 return -ENOMEM;
969 memset(e, 0, sizeof(*e));
970
971 if (le) {
972 le->next = e;
973 } else {
974 c->expr = e;
975 }
976
977 rc = next_entry(buf, fp, (sizeof(u32) * 3));
978 if (rc < 0)
979 return rc;
980 e->expr_type = le32_to_cpu(buf[0]);
981 e->attr = le32_to_cpu(buf[1]);
982 e->op = le32_to_cpu(buf[2]);
983
984 switch (e->expr_type) {
985 case CEXPR_NOT:
986 if (depth < 0)
987 return -EINVAL;
988 break;
989 case CEXPR_AND:
990 case CEXPR_OR:
991 if (depth < 1)
992 return -EINVAL;
993 depth--;
994 break;
995 case CEXPR_ATTR:
996 if (depth == (CEXPR_MAXDEPTH - 1))
997 return -EINVAL;
998 depth++;
999 break;
1000 case CEXPR_NAMES:
1001 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1002 return -EINVAL;
1003 if (depth == (CEXPR_MAXDEPTH - 1))
1004 return -EINVAL;
1005 depth++;
1006 if (ebitmap_read(&e->names, fp))
1007 return -EINVAL;
1008 break;
1009 default:
1010 return -EINVAL;
1011 }
1012 le = e;
1013 }
1014 if (depth != 0)
1015 return -EINVAL;
1016 lc = c;
1017 }
1018
1019 return 0;
1020}
1021
1022static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1023{
1024 char *key = NULL;
1025 struct class_datum *cladatum;
1026 u32 buf[6], len, len2, ncons, nel;
1027 int i, rc;
1028
1029 cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
1030 if (!cladatum) {
1031 rc = -ENOMEM;
1032 goto out;
1033 }
1034 memset(cladatum, 0, sizeof(*cladatum));
1035
1036 rc = next_entry(buf, fp, sizeof(u32)*6);
1037 if (rc < 0)
1038 goto bad;
1039
1040 len = le32_to_cpu(buf[0]);
1041 len2 = le32_to_cpu(buf[1]);
1042 cladatum->value = le32_to_cpu(buf[2]);
1043
1044 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1045 if (rc)
1046 goto bad;
1047 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1048 nel = le32_to_cpu(buf[4]);
1049
1050 ncons = le32_to_cpu(buf[5]);
1051
1052 key = kmalloc(len + 1,GFP_KERNEL);
1053 if (!key) {
1054 rc = -ENOMEM;
1055 goto bad;
1056 }
1057 rc = next_entry(key, fp, len);
1058 if (rc < 0)
1059 goto bad;
1060 key[len] = 0;
1061
1062 if (len2) {
1063 cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
1064 if (!cladatum->comkey) {
1065 rc = -ENOMEM;
1066 goto bad;
1067 }
1068 rc = next_entry(cladatum->comkey, fp, len2);
1069 if (rc < 0)
1070 goto bad;
1071 cladatum->comkey[len2] = 0;
1072
1073 cladatum->comdatum = hashtab_search(p->p_commons.table,
1074 cladatum->comkey);
1075 if (!cladatum->comdatum) {
1076 printk(KERN_ERR "security: unknown common %s\n",
1077 cladatum->comkey);
1078 rc = -EINVAL;
1079 goto bad;
1080 }
1081 }
1082 for (i = 0; i < nel; i++) {
1083 rc = perm_read(p, cladatum->permissions.table, fp);
1084 if (rc)
1085 goto bad;
1086 }
1087
1088 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1089 if (rc)
1090 goto bad;
1091
1092 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1093 /* grab the validatetrans rules */
1094 rc = next_entry(buf, fp, sizeof(u32));
1095 if (rc < 0)
1096 goto bad;
1097 ncons = le32_to_cpu(buf[0]);
1098 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1099 if (rc)
1100 goto bad;
1101 }
1102
1103 rc = hashtab_insert(h, key, cladatum);
1104 if (rc)
1105 goto bad;
1106
1107 rc = 0;
1108out:
1109 return rc;
1110bad:
1111 class_destroy(key, cladatum, NULL);
1112 goto out;
1113}
1114
1115static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1116{
1117 char *key = NULL;
1118 struct role_datum *role;
1119 int rc;
1120 u32 buf[2], len;
1121
1122 role = kmalloc(sizeof(*role), GFP_KERNEL);
1123 if (!role) {
1124 rc = -ENOMEM;
1125 goto out;
1126 }
1127 memset(role, 0, sizeof(*role));
1128
1129 rc = next_entry(buf, fp, sizeof buf);
1130 if (rc < 0)
1131 goto bad;
1132
1133 len = le32_to_cpu(buf[0]);
1134 role->value = le32_to_cpu(buf[1]);
1135
1136 key = kmalloc(len + 1,GFP_KERNEL);
1137 if (!key) {
1138 rc = -ENOMEM;
1139 goto bad;
1140 }
1141 rc = next_entry(key, fp, len);
1142 if (rc < 0)
1143 goto bad;
1144 key[len] = 0;
1145
1146 rc = ebitmap_read(&role->dominates, fp);
1147 if (rc)
1148 goto bad;
1149
1150 rc = ebitmap_read(&role->types, fp);
1151 if (rc)
1152 goto bad;
1153
1154 if (strcmp(key, OBJECT_R) == 0) {
1155 if (role->value != OBJECT_R_VAL) {
1156 printk(KERN_ERR "Role %s has wrong value %d\n",
1157 OBJECT_R, role->value);
1158 rc = -EINVAL;
1159 goto bad;
1160 }
1161 rc = 0;
1162 goto bad;
1163 }
1164
1165 rc = hashtab_insert(h, key, role);
1166 if (rc)
1167 goto bad;
1168out:
1169 return rc;
1170bad:
1171 role_destroy(key, role, NULL);
1172 goto out;
1173}
1174
1175static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1176{
1177 char *key = NULL;
1178 struct type_datum *typdatum;
1179 int rc;
1180 u32 buf[3], len;
1181
1182 typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
1183 if (!typdatum) {
1184 rc = -ENOMEM;
1185 return rc;
1186 }
1187 memset(typdatum, 0, sizeof(*typdatum));
1188
1189 rc = next_entry(buf, fp, sizeof buf);
1190 if (rc < 0)
1191 goto bad;
1192
1193 len = le32_to_cpu(buf[0]);
1194 typdatum->value = le32_to_cpu(buf[1]);
1195 typdatum->primary = le32_to_cpu(buf[2]);
1196
1197 key = kmalloc(len + 1,GFP_KERNEL);
1198 if (!key) {
1199 rc = -ENOMEM;
1200 goto bad;
1201 }
1202 rc = next_entry(key, fp, len);
1203 if (rc < 0)
1204 goto bad;
1205 key[len] = 0;
1206
1207 rc = hashtab_insert(h, key, typdatum);
1208 if (rc)
1209 goto bad;
1210out:
1211 return rc;
1212bad:
1213 type_destroy(key, typdatum, NULL);
1214 goto out;
1215}
1216
1217
1218/*
1219 * Read a MLS level structure from a policydb binary
1220 * representation file.
1221 */
1222static int mls_read_level(struct mls_level *lp, void *fp)
1223{
1224 u32 buf[1];
1225 int rc;
1226
1227 memset(lp, 0, sizeof(*lp));
1228
1229 rc = next_entry(buf, fp, sizeof buf);
1230 if (rc < 0) {
1231 printk(KERN_ERR "security: mls: truncated level\n");
1232 goto bad;
1233 }
1234 lp->sens = le32_to_cpu(buf[0]);
1235
1236 if (ebitmap_read(&lp->cat, fp)) {
1237 printk(KERN_ERR "security: mls: error reading level "
1238 "categories\n");
1239 goto bad;
1240 }
1241 return 0;
1242
1243bad:
1244 return -EINVAL;
1245}
1246
1247static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1248{
1249 char *key = NULL;
1250 struct user_datum *usrdatum;
1251 int rc;
1252 u32 buf[2], len;
1253
1254 usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
1255 if (!usrdatum) {
1256 rc = -ENOMEM;
1257 goto out;
1258 }
1259 memset(usrdatum, 0, sizeof(*usrdatum));
1260
1261 rc = next_entry(buf, fp, sizeof buf);
1262 if (rc < 0)
1263 goto bad;
1264
1265 len = le32_to_cpu(buf[0]);
1266 usrdatum->value = le32_to_cpu(buf[1]);
1267
1268 key = kmalloc(len + 1,GFP_KERNEL);
1269 if (!key) {
1270 rc = -ENOMEM;
1271 goto bad;
1272 }
1273 rc = next_entry(key, fp, len);
1274 if (rc < 0)
1275 goto bad;
1276 key[len] = 0;
1277
1278 rc = ebitmap_read(&usrdatum->roles, fp);
1279 if (rc)
1280 goto bad;
1281
1282 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1283 rc = mls_read_range_helper(&usrdatum->range, fp);
1284 if (rc)
1285 goto bad;
1286 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1287 if (rc)
1288 goto bad;
1289 }
1290
1291 rc = hashtab_insert(h, key, usrdatum);
1292 if (rc)
1293 goto bad;
1294out:
1295 return rc;
1296bad:
1297 user_destroy(key, usrdatum, NULL);
1298 goto out;
1299}
1300
1301static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1302{
1303 char *key = NULL;
1304 struct level_datum *levdatum;
1305 int rc;
1306 u32 buf[2], len;
1307
1308 levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
1309 if (!levdatum) {
1310 rc = -ENOMEM;
1311 goto out;
1312 }
1313 memset(levdatum, 0, sizeof(*levdatum));
1314
1315 rc = next_entry(buf, fp, sizeof buf);
1316 if (rc < 0)
1317 goto bad;
1318
1319 len = le32_to_cpu(buf[0]);
1320 levdatum->isalias = le32_to_cpu(buf[1]);
1321
1322 key = kmalloc(len + 1,GFP_ATOMIC);
1323 if (!key) {
1324 rc = -ENOMEM;
1325 goto bad;
1326 }
1327 rc = next_entry(key, fp, len);
1328 if (rc < 0)
1329 goto bad;
1330 key[len] = 0;
1331
1332 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1333 if (!levdatum->level) {
1334 rc = -ENOMEM;
1335 goto bad;
1336 }
1337 if (mls_read_level(levdatum->level, fp)) {
1338 rc = -EINVAL;
1339 goto bad;
1340 }
1341
1342 rc = hashtab_insert(h, key, levdatum);
1343 if (rc)
1344 goto bad;
1345out:
1346 return rc;
1347bad:
1348 sens_destroy(key, levdatum, NULL);
1349 goto out;
1350}
1351
1352static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1353{
1354 char *key = NULL;
1355 struct cat_datum *catdatum;
1356 int rc;
1357 u32 buf[3], len;
1358
1359 catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
1360 if (!catdatum) {
1361 rc = -ENOMEM;
1362 goto out;
1363 }
1364 memset(catdatum, 0, sizeof(*catdatum));
1365
1366 rc = next_entry(buf, fp, sizeof buf);
1367 if (rc < 0)
1368 goto bad;
1369
1370 len = le32_to_cpu(buf[0]);
1371 catdatum->value = le32_to_cpu(buf[1]);
1372 catdatum->isalias = le32_to_cpu(buf[2]);
1373
1374 key = kmalloc(len + 1,GFP_ATOMIC);
1375 if (!key) {
1376 rc = -ENOMEM;
1377 goto bad;
1378 }
1379 rc = next_entry(key, fp, len);
1380 if (rc < 0)
1381 goto bad;
1382 key[len] = 0;
1383
1384 rc = hashtab_insert(h, key, catdatum);
1385 if (rc)
1386 goto bad;
1387out:
1388 return rc;
1389
1390bad:
1391 cat_destroy(key, catdatum, NULL);
1392 goto out;
1393}
1394
1395static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1396{
1397 common_read,
1398 class_read,
1399 role_read,
1400 type_read,
1401 user_read,
1402 cond_read_bool,
1403 sens_read,
1404 cat_read,
1405};
1406
1407extern int ss_initialized;
1408
1409/*
1410 * Read the configuration data from a policy database binary
1411 * representation file into a policy database structure.
1412 */
1413int policydb_read(struct policydb *p, void *fp)
1414{
1415 struct role_allow *ra, *lra;
1416 struct role_trans *tr, *ltr;
1417 struct ocontext *l, *c, *newc;
1418 struct genfs *genfs_p, *genfs, *newgenfs;
1419 int i, j, rc;
1420 u32 buf[8], len, len2, config, nprim, nel, nel2;
1421 char *policydb_str;
1422 struct policydb_compat_info *info;
1423 struct range_trans *rt, *lrt;
1424
1425 config = 0;
1426
1427 rc = policydb_init(p);
1428 if (rc)
1429 goto out;
1430
1431 /* Read the magic number and string length. */
1432 rc = next_entry(buf, fp, sizeof(u32)* 2);
1433 if (rc < 0)
1434 goto bad;
1435
1436 for (i = 0; i < 2; i++)
1437 buf[i] = le32_to_cpu(buf[i]);
1438
1439 if (buf[0] != POLICYDB_MAGIC) {
1440 printk(KERN_ERR "security: policydb magic number 0x%x does "
1441 "not match expected magic number 0x%x\n",
1442 buf[0], POLICYDB_MAGIC);
1443 goto bad;
1444 }
1445
1446 len = buf[1];
1447 if (len != strlen(POLICYDB_STRING)) {
1448 printk(KERN_ERR "security: policydb string length %d does not "
1449 "match expected length %Zu\n",
1450 len, strlen(POLICYDB_STRING));
1451 goto bad;
1452 }
1453 policydb_str = kmalloc(len + 1,GFP_KERNEL);
1454 if (!policydb_str) {
1455 printk(KERN_ERR "security: unable to allocate memory for policydb "
1456 "string of length %d\n", len);
1457 rc = -ENOMEM;
1458 goto bad;
1459 }
1460 rc = next_entry(policydb_str, fp, len);
1461 if (rc < 0) {
1462 printk(KERN_ERR "security: truncated policydb string identifier\n");
1463 kfree(policydb_str);
1464 goto bad;
1465 }
1466 policydb_str[len] = 0;
1467 if (strcmp(policydb_str, POLICYDB_STRING)) {
1468 printk(KERN_ERR "security: policydb string %s does not match "
1469 "my string %s\n", policydb_str, POLICYDB_STRING);
1470 kfree(policydb_str);
1471 goto bad;
1472 }
1473 /* Done with policydb_str. */
1474 kfree(policydb_str);
1475 policydb_str = NULL;
1476
1477 /* Read the version, config, and table sizes. */
1478 rc = next_entry(buf, fp, sizeof(u32)*4);
1479 if (rc < 0)
1480 goto bad;
1481 for (i = 0; i < 4; i++)
1482 buf[i] = le32_to_cpu(buf[i]);
1483
1484 p->policyvers = buf[0];
1485 if (p->policyvers < POLICYDB_VERSION_MIN ||
1486 p->policyvers > POLICYDB_VERSION_MAX) {
1487 printk(KERN_ERR "security: policydb version %d does not match "
1488 "my version range %d-%d\n",
1489 buf[0], POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
1490 goto bad;
1491 }
1492
1493 if ((buf[1] & POLICYDB_CONFIG_MLS)) {
1494 if (ss_initialized && !selinux_mls_enabled) {
1495 printk(KERN_ERR "Cannot switch between non-MLS and MLS "
1496 "policies\n");
1497 goto bad;
1498 }
1499 selinux_mls_enabled = 1;
1500 config |= POLICYDB_CONFIG_MLS;
1501
1502 if (p->policyvers < POLICYDB_VERSION_MLS) {
1503 printk(KERN_ERR "security policydb version %d (MLS) "
1504 "not backwards compatible\n", p->policyvers);
1505 goto bad;
1506 }
1507 } else {
1508 if (ss_initialized && selinux_mls_enabled) {
1509 printk(KERN_ERR "Cannot switch between MLS and non-MLS "
1510 "policies\n");
1511 goto bad;
1512 }
1513 }
1514
1515 info = policydb_lookup_compat(p->policyvers);
1516 if (!info) {
1517 printk(KERN_ERR "security: unable to find policy compat info "
1518 "for version %d\n", p->policyvers);
1519 goto bad;
1520 }
1521
1522 if (buf[2] != info->sym_num || buf[3] != info->ocon_num) {
1523 printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
1524 "not match mine (%d,%d)\n", buf[2], buf[3],
1525 info->sym_num, info->ocon_num);
1526 goto bad;
1527 }
1528
1529 for (i = 0; i < info->sym_num; i++) {
1530 rc = next_entry(buf, fp, sizeof(u32)*2);
1531 if (rc < 0)
1532 goto bad;
1533 nprim = le32_to_cpu(buf[0]);
1534 nel = le32_to_cpu(buf[1]);
1535 for (j = 0; j < nel; j++) {
1536 rc = read_f[i](p, p->symtab[i].table, fp);
1537 if (rc)
1538 goto bad;
1539 }
1540
1541 p->symtab[i].nprim = nprim;
1542 }
1543
Stephen Smalley782ebb92005-09-03 15:55:16 -07001544 rc = avtab_read(&p->te_avtab, fp, p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (rc)
1546 goto bad;
1547
1548 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
1549 rc = cond_read_list(p, fp);
1550 if (rc)
1551 goto bad;
1552 }
1553
1554 rc = next_entry(buf, fp, sizeof(u32));
1555 if (rc < 0)
1556 goto bad;
1557 nel = le32_to_cpu(buf[0]);
1558 ltr = NULL;
1559 for (i = 0; i < nel; i++) {
1560 tr = kmalloc(sizeof(*tr), GFP_KERNEL);
1561 if (!tr) {
1562 rc = -ENOMEM;
1563 goto bad;
1564 }
1565 memset(tr, 0, sizeof(*tr));
1566 if (ltr) {
1567 ltr->next = tr;
1568 } else {
1569 p->role_tr = tr;
1570 }
1571 rc = next_entry(buf, fp, sizeof(u32)*3);
1572 if (rc < 0)
1573 goto bad;
1574 tr->role = le32_to_cpu(buf[0]);
1575 tr->type = le32_to_cpu(buf[1]);
1576 tr->new_role = le32_to_cpu(buf[2]);
1577 ltr = tr;
1578 }
1579
1580 rc = next_entry(buf, fp, sizeof(u32));
1581 if (rc < 0)
1582 goto bad;
1583 nel = le32_to_cpu(buf[0]);
1584 lra = NULL;
1585 for (i = 0; i < nel; i++) {
1586 ra = kmalloc(sizeof(*ra), GFP_KERNEL);
1587 if (!ra) {
1588 rc = -ENOMEM;
1589 goto bad;
1590 }
1591 memset(ra, 0, sizeof(*ra));
1592 if (lra) {
1593 lra->next = ra;
1594 } else {
1595 p->role_allow = ra;
1596 }
1597 rc = next_entry(buf, fp, sizeof(u32)*2);
1598 if (rc < 0)
1599 goto bad;
1600 ra->role = le32_to_cpu(buf[0]);
1601 ra->new_role = le32_to_cpu(buf[1]);
1602 lra = ra;
1603 }
1604
1605 rc = policydb_index_classes(p);
1606 if (rc)
1607 goto bad;
1608
1609 rc = policydb_index_others(p);
1610 if (rc)
1611 goto bad;
1612
1613 for (i = 0; i < info->ocon_num; i++) {
1614 rc = next_entry(buf, fp, sizeof(u32));
1615 if (rc < 0)
1616 goto bad;
1617 nel = le32_to_cpu(buf[0]);
1618 l = NULL;
1619 for (j = 0; j < nel; j++) {
1620 c = kmalloc(sizeof(*c), GFP_KERNEL);
1621 if (!c) {
1622 rc = -ENOMEM;
1623 goto bad;
1624 }
1625 memset(c, 0, sizeof(*c));
1626 if (l) {
1627 l->next = c;
1628 } else {
1629 p->ocontexts[i] = c;
1630 }
1631 l = c;
1632 rc = -EINVAL;
1633 switch (i) {
1634 case OCON_ISID:
1635 rc = next_entry(buf, fp, sizeof(u32));
1636 if (rc < 0)
1637 goto bad;
1638 c->sid[0] = le32_to_cpu(buf[0]);
1639 rc = context_read_and_validate(&c->context[0], p, fp);
1640 if (rc)
1641 goto bad;
1642 break;
1643 case OCON_FS:
1644 case OCON_NETIF:
1645 rc = next_entry(buf, fp, sizeof(u32));
1646 if (rc < 0)
1647 goto bad;
1648 len = le32_to_cpu(buf[0]);
1649 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1650 if (!c->u.name) {
1651 rc = -ENOMEM;
1652 goto bad;
1653 }
1654 rc = next_entry(c->u.name, fp, len);
1655 if (rc < 0)
1656 goto bad;
1657 c->u.name[len] = 0;
1658 rc = context_read_and_validate(&c->context[0], p, fp);
1659 if (rc)
1660 goto bad;
1661 rc = context_read_and_validate(&c->context[1], p, fp);
1662 if (rc)
1663 goto bad;
1664 break;
1665 case OCON_PORT:
1666 rc = next_entry(buf, fp, sizeof(u32)*3);
1667 if (rc < 0)
1668 goto bad;
1669 c->u.port.protocol = le32_to_cpu(buf[0]);
1670 c->u.port.low_port = le32_to_cpu(buf[1]);
1671 c->u.port.high_port = le32_to_cpu(buf[2]);
1672 rc = context_read_and_validate(&c->context[0], p, fp);
1673 if (rc)
1674 goto bad;
1675 break;
1676 case OCON_NODE:
1677 rc = next_entry(buf, fp, sizeof(u32)* 2);
1678 if (rc < 0)
1679 goto bad;
1680 c->u.node.addr = le32_to_cpu(buf[0]);
1681 c->u.node.mask = le32_to_cpu(buf[1]);
1682 rc = context_read_and_validate(&c->context[0], p, fp);
1683 if (rc)
1684 goto bad;
1685 break;
1686 case OCON_FSUSE:
1687 rc = next_entry(buf, fp, sizeof(u32)*2);
1688 if (rc < 0)
1689 goto bad;
1690 c->v.behavior = le32_to_cpu(buf[0]);
1691 if (c->v.behavior > SECURITY_FS_USE_NONE)
1692 goto bad;
1693 len = le32_to_cpu(buf[1]);
1694 c->u.name = kmalloc(len + 1,GFP_KERNEL);
1695 if (!c->u.name) {
1696 rc = -ENOMEM;
1697 goto bad;
1698 }
1699 rc = next_entry(c->u.name, fp, len);
1700 if (rc < 0)
1701 goto bad;
1702 c->u.name[len] = 0;
1703 rc = context_read_and_validate(&c->context[0], p, fp);
1704 if (rc)
1705 goto bad;
1706 break;
1707 case OCON_NODE6: {
1708 int k;
1709
1710 rc = next_entry(buf, fp, sizeof(u32) * 8);
1711 if (rc < 0)
1712 goto bad;
1713 for (k = 0; k < 4; k++)
1714 c->u.node6.addr[k] = le32_to_cpu(buf[k]);
1715 for (k = 0; k < 4; k++)
1716 c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
1717 if (context_read_and_validate(&c->context[0], p, fp))
1718 goto bad;
1719 break;
1720 }
1721 }
1722 }
1723 }
1724
1725 rc = next_entry(buf, fp, sizeof(u32));
1726 if (rc < 0)
1727 goto bad;
1728 nel = le32_to_cpu(buf[0]);
1729 genfs_p = NULL;
1730 rc = -EINVAL;
1731 for (i = 0; i < nel; i++) {
1732 rc = next_entry(buf, fp, sizeof(u32));
1733 if (rc < 0)
1734 goto bad;
1735 len = le32_to_cpu(buf[0]);
1736 newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
1737 if (!newgenfs) {
1738 rc = -ENOMEM;
1739 goto bad;
1740 }
1741 memset(newgenfs, 0, sizeof(*newgenfs));
1742
1743 newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
1744 if (!newgenfs->fstype) {
1745 rc = -ENOMEM;
1746 kfree(newgenfs);
1747 goto bad;
1748 }
1749 rc = next_entry(newgenfs->fstype, fp, len);
1750 if (rc < 0) {
1751 kfree(newgenfs->fstype);
1752 kfree(newgenfs);
1753 goto bad;
1754 }
1755 newgenfs->fstype[len] = 0;
1756 for (genfs_p = NULL, genfs = p->genfs; genfs;
1757 genfs_p = genfs, genfs = genfs->next) {
1758 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1759 printk(KERN_ERR "security: dup genfs "
1760 "fstype %s\n", newgenfs->fstype);
1761 kfree(newgenfs->fstype);
1762 kfree(newgenfs);
1763 goto bad;
1764 }
1765 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1766 break;
1767 }
1768 newgenfs->next = genfs;
1769 if (genfs_p)
1770 genfs_p->next = newgenfs;
1771 else
1772 p->genfs = newgenfs;
1773 rc = next_entry(buf, fp, sizeof(u32));
1774 if (rc < 0)
1775 goto bad;
1776 nel2 = le32_to_cpu(buf[0]);
1777 for (j = 0; j < nel2; j++) {
1778 rc = next_entry(buf, fp, sizeof(u32));
1779 if (rc < 0)
1780 goto bad;
1781 len = le32_to_cpu(buf[0]);
1782
1783 newc = kmalloc(sizeof(*newc), GFP_KERNEL);
1784 if (!newc) {
1785 rc = -ENOMEM;
1786 goto bad;
1787 }
1788 memset(newc, 0, sizeof(*newc));
1789
1790 newc->u.name = kmalloc(len + 1,GFP_KERNEL);
1791 if (!newc->u.name) {
1792 rc = -ENOMEM;
1793 goto bad_newc;
1794 }
1795 rc = next_entry(newc->u.name, fp, len);
1796 if (rc < 0)
1797 goto bad_newc;
1798 newc->u.name[len] = 0;
1799 rc = next_entry(buf, fp, sizeof(u32));
1800 if (rc < 0)
1801 goto bad_newc;
1802 newc->v.sclass = le32_to_cpu(buf[0]);
1803 if (context_read_and_validate(&newc->context[0], p, fp))
1804 goto bad_newc;
1805 for (l = NULL, c = newgenfs->head; c;
1806 l = c, c = c->next) {
1807 if (!strcmp(newc->u.name, c->u.name) &&
1808 (!c->v.sclass || !newc->v.sclass ||
1809 newc->v.sclass == c->v.sclass)) {
1810 printk(KERN_ERR "security: dup genfs "
1811 "entry (%s,%s)\n",
1812 newgenfs->fstype, c->u.name);
1813 goto bad_newc;
1814 }
1815 len = strlen(newc->u.name);
1816 len2 = strlen(c->u.name);
1817 if (len > len2)
1818 break;
1819 }
1820
1821 newc->next = c;
1822 if (l)
1823 l->next = newc;
1824 else
1825 newgenfs->head = newc;
1826 }
1827 }
1828
1829 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1830 rc = next_entry(buf, fp, sizeof(u32));
1831 if (rc < 0)
1832 goto bad;
1833 nel = le32_to_cpu(buf[0]);
1834 lrt = NULL;
1835 for (i = 0; i < nel; i++) {
1836 rt = kmalloc(sizeof(*rt), GFP_KERNEL);
1837 if (!rt) {
1838 rc = -ENOMEM;
1839 goto bad;
1840 }
1841 memset(rt, 0, sizeof(*rt));
1842 if (lrt)
1843 lrt->next = rt;
1844 else
1845 p->range_tr = rt;
1846 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1847 if (rc < 0)
1848 goto bad;
1849 rt->dom = le32_to_cpu(buf[0]);
1850 rt->type = le32_to_cpu(buf[1]);
1851 rc = mls_read_range_helper(&rt->range, fp);
1852 if (rc)
1853 goto bad;
1854 lrt = rt;
1855 }
1856 }
1857
Stephen Smalley782ebb92005-09-03 15:55:16 -07001858 p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
1859 if (!p->type_attr_map)
1860 goto bad;
1861
1862 for (i = 0; i < p->p_types.nprim; i++) {
1863 ebitmap_init(&p->type_attr_map[i]);
1864 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
1865 if (ebitmap_read(&p->type_attr_map[i], fp))
1866 goto bad;
1867 }
1868 /* add the type itself as the degenerate case */
1869 if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
1870 goto bad;
1871 }
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rc = 0;
1874out:
1875 return rc;
1876bad_newc:
1877 ocontext_destroy(newc,OCON_FSUSE);
1878bad:
1879 if (!rc)
1880 rc = -EINVAL;
1881 policydb_destroy(p);
1882 goto out;
1883}