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