]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/selinux/ss/policydb.c
selinux: Return directly after a failed kzalloc() in class_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 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1322 if (!cladatum)
1323 return -ENOMEM;
1324
1325 rc = next_entry(buf, fp, sizeof(u32)*6);
1326 if (rc)
1327 goto bad;
1328
1329 len = le32_to_cpu(buf[0]);
1330 len2 = le32_to_cpu(buf[1]);
1331 cladatum->value = le32_to_cpu(buf[2]);
1332
1333 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1334 if (rc)
1335 goto bad;
1336 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1337 nel = le32_to_cpu(buf[4]);
1338
1339 ncons = le32_to_cpu(buf[5]);
1340
1341 rc = str_read(&key, GFP_KERNEL, fp, len);
1342 if (rc)
1343 goto bad;
1344
1345 if (len2) {
1346 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
1347 if (rc)
1348 goto bad;
1349
1350 rc = -EINVAL;
1351 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1352 if (!cladatum->comdatum) {
1353 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1354 goto bad;
1355 }
1356 }
1357 for (i = 0; i < nel; i++) {
1358 rc = perm_read(p, cladatum->permissions.table, fp);
1359 if (rc)
1360 goto bad;
1361 }
1362
1363 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
1364 if (rc)
1365 goto bad;
1366
1367 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1368 /* grab the validatetrans rules */
1369 rc = next_entry(buf, fp, sizeof(u32));
1370 if (rc)
1371 goto bad;
1372 ncons = le32_to_cpu(buf[0]);
1373 rc = read_cons_helper(p, &cladatum->validatetrans,
1374 ncons, 1, fp);
1375 if (rc)
1376 goto bad;
1377 }
1378
1379 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1380 rc = next_entry(buf, fp, sizeof(u32) * 3);
1381 if (rc)
1382 goto bad;
1383
1384 cladatum->default_user = le32_to_cpu(buf[0]);
1385 cladatum->default_role = le32_to_cpu(buf[1]);
1386 cladatum->default_range = le32_to_cpu(buf[2]);
1387 }
1388
1389 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1390 rc = next_entry(buf, fp, sizeof(u32) * 1);
1391 if (rc)
1392 goto bad;
1393 cladatum->default_type = le32_to_cpu(buf[0]);
1394 }
1395
1396 rc = hashtab_insert(h, key, cladatum);
1397 if (rc)
1398 goto bad;
1399
1400 return 0;
1401 bad:
1402 cls_destroy(key, cladatum, NULL);
1403 return rc;
1404 }
1405
1406 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1407 {
1408 char *key = NULL;
1409 struct role_datum *role;
1410 int rc, to_read = 2;
1411 __le32 buf[3];
1412 u32 len;
1413
1414 role = kzalloc(sizeof(*role), GFP_KERNEL);
1415 if (!role)
1416 return -ENOMEM;
1417
1418 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1419 to_read = 3;
1420
1421 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1422 if (rc)
1423 goto bad;
1424
1425 len = le32_to_cpu(buf[0]);
1426 role->value = le32_to_cpu(buf[1]);
1427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428 role->bounds = le32_to_cpu(buf[2]);
1429
1430 rc = str_read(&key, GFP_KERNEL, fp, len);
1431 if (rc)
1432 goto bad;
1433
1434 rc = ebitmap_read(&role->dominates, fp);
1435 if (rc)
1436 goto bad;
1437
1438 rc = ebitmap_read(&role->types, fp);
1439 if (rc)
1440 goto bad;
1441
1442 if (strcmp(key, OBJECT_R) == 0) {
1443 rc = -EINVAL;
1444 if (role->value != OBJECT_R_VAL) {
1445 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1446 OBJECT_R, role->value);
1447 goto bad;
1448 }
1449 rc = 0;
1450 goto bad;
1451 }
1452
1453 rc = hashtab_insert(h, key, role);
1454 if (rc)
1455 goto bad;
1456 return 0;
1457 bad:
1458 role_destroy(key, role, NULL);
1459 return rc;
1460 }
1461
1462 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1463 {
1464 char *key = NULL;
1465 struct type_datum *typdatum;
1466 int rc, to_read = 3;
1467 __le32 buf[4];
1468 u32 len;
1469
1470 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1471 if (!typdatum)
1472 return -ENOMEM;
1473
1474 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1475 to_read = 4;
1476
1477 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1478 if (rc)
1479 goto bad;
1480
1481 len = le32_to_cpu(buf[0]);
1482 typdatum->value = le32_to_cpu(buf[1]);
1483 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1484 u32 prop = le32_to_cpu(buf[2]);
1485
1486 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1487 typdatum->primary = 1;
1488 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1489 typdatum->attribute = 1;
1490
1491 typdatum->bounds = le32_to_cpu(buf[3]);
1492 } else {
1493 typdatum->primary = le32_to_cpu(buf[2]);
1494 }
1495
1496 rc = str_read(&key, GFP_KERNEL, fp, len);
1497 if (rc)
1498 goto bad;
1499
1500 rc = hashtab_insert(h, key, typdatum);
1501 if (rc)
1502 goto bad;
1503 return 0;
1504 bad:
1505 type_destroy(key, typdatum, NULL);
1506 return rc;
1507 }
1508
1509
1510 /*
1511 * Read a MLS level structure from a policydb binary
1512 * representation file.
1513 */
1514 static int mls_read_level(struct mls_level *lp, void *fp)
1515 {
1516 __le32 buf[1];
1517 int rc;
1518
1519 memset(lp, 0, sizeof(*lp));
1520
1521 rc = next_entry(buf, fp, sizeof buf);
1522 if (rc) {
1523 printk(KERN_ERR "SELinux: mls: truncated level\n");
1524 return rc;
1525 }
1526 lp->sens = le32_to_cpu(buf[0]);
1527
1528 rc = ebitmap_read(&lp->cat, fp);
1529 if (rc) {
1530 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1531 return rc;
1532 }
1533 return 0;
1534 }
1535
1536 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1537 {
1538 char *key = NULL;
1539 struct user_datum *usrdatum;
1540 int rc, to_read = 2;
1541 __le32 buf[3];
1542 u32 len;
1543
1544 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1545 if (!usrdatum)
1546 return -ENOMEM;
1547
1548 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1549 to_read = 3;
1550
1551 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1552 if (rc)
1553 goto bad;
1554
1555 len = le32_to_cpu(buf[0]);
1556 usrdatum->value = le32_to_cpu(buf[1]);
1557 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1558 usrdatum->bounds = le32_to_cpu(buf[2]);
1559
1560 rc = str_read(&key, GFP_KERNEL, fp, len);
1561 if (rc)
1562 goto bad;
1563
1564 rc = ebitmap_read(&usrdatum->roles, fp);
1565 if (rc)
1566 goto bad;
1567
1568 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1569 rc = mls_read_range_helper(&usrdatum->range, fp);
1570 if (rc)
1571 goto bad;
1572 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1573 if (rc)
1574 goto bad;
1575 }
1576
1577 rc = hashtab_insert(h, key, usrdatum);
1578 if (rc)
1579 goto bad;
1580 return 0;
1581 bad:
1582 user_destroy(key, usrdatum, NULL);
1583 return rc;
1584 }
1585
1586 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1587 {
1588 char *key = NULL;
1589 struct level_datum *levdatum;
1590 int rc;
1591 __le32 buf[2];
1592 u32 len;
1593
1594 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1595 if (!levdatum)
1596 return -ENOMEM;
1597
1598 rc = next_entry(buf, fp, sizeof buf);
1599 if (rc)
1600 goto bad;
1601
1602 len = le32_to_cpu(buf[0]);
1603 levdatum->isalias = le32_to_cpu(buf[1]);
1604
1605 rc = str_read(&key, GFP_ATOMIC, fp, len);
1606 if (rc)
1607 goto bad;
1608
1609 rc = -ENOMEM;
1610 levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
1611 if (!levdatum->level)
1612 goto bad;
1613
1614 rc = mls_read_level(levdatum->level, fp);
1615 if (rc)
1616 goto bad;
1617
1618 rc = hashtab_insert(h, key, levdatum);
1619 if (rc)
1620 goto bad;
1621 return 0;
1622 bad:
1623 sens_destroy(key, levdatum, NULL);
1624 return rc;
1625 }
1626
1627 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1628 {
1629 char *key = NULL;
1630 struct cat_datum *catdatum;
1631 int rc;
1632 __le32 buf[3];
1633 u32 len;
1634
1635 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1636 if (!catdatum)
1637 return -ENOMEM;
1638
1639 rc = next_entry(buf, fp, sizeof buf);
1640 if (rc)
1641 goto bad;
1642
1643 len = le32_to_cpu(buf[0]);
1644 catdatum->value = le32_to_cpu(buf[1]);
1645 catdatum->isalias = le32_to_cpu(buf[2]);
1646
1647 rc = str_read(&key, GFP_ATOMIC, fp, len);
1648 if (rc)
1649 goto bad;
1650
1651 rc = hashtab_insert(h, key, catdatum);
1652 if (rc)
1653 goto bad;
1654 return 0;
1655 bad:
1656 cat_destroy(key, catdatum, NULL);
1657 return rc;
1658 }
1659
1660 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1661 {
1662 common_read,
1663 class_read,
1664 role_read,
1665 type_read,
1666 user_read,
1667 cond_read_bool,
1668 sens_read,
1669 cat_read,
1670 };
1671
1672 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1673 {
1674 struct user_datum *upper, *user;
1675 struct policydb *p = datap;
1676 int depth = 0;
1677
1678 upper = user = datum;
1679 while (upper->bounds) {
1680 struct ebitmap_node *node;
1681 unsigned long bit;
1682
1683 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1684 printk(KERN_ERR "SELinux: user %s: "
1685 "too deep or looped boundary",
1686 (char *) key);
1687 return -EINVAL;
1688 }
1689
1690 upper = p->user_val_to_struct[upper->bounds - 1];
1691 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1692 if (ebitmap_get_bit(&upper->roles, bit))
1693 continue;
1694
1695 printk(KERN_ERR
1696 "SELinux: boundary violated policy: "
1697 "user=%s role=%s bounds=%s\n",
1698 sym_name(p, SYM_USERS, user->value - 1),
1699 sym_name(p, SYM_ROLES, bit),
1700 sym_name(p, SYM_USERS, upper->value - 1));
1701
1702 return -EINVAL;
1703 }
1704 }
1705
1706 return 0;
1707 }
1708
1709 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1710 {
1711 struct role_datum *upper, *role;
1712 struct policydb *p = datap;
1713 int depth = 0;
1714
1715 upper = role = datum;
1716 while (upper->bounds) {
1717 struct ebitmap_node *node;
1718 unsigned long bit;
1719
1720 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1721 printk(KERN_ERR "SELinux: role %s: "
1722 "too deep or looped bounds\n",
1723 (char *) key);
1724 return -EINVAL;
1725 }
1726
1727 upper = p->role_val_to_struct[upper->bounds - 1];
1728 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1729 if (ebitmap_get_bit(&upper->types, bit))
1730 continue;
1731
1732 printk(KERN_ERR
1733 "SELinux: boundary violated policy: "
1734 "role=%s type=%s bounds=%s\n",
1735 sym_name(p, SYM_ROLES, role->value - 1),
1736 sym_name(p, SYM_TYPES, bit),
1737 sym_name(p, SYM_ROLES, upper->value - 1));
1738
1739 return -EINVAL;
1740 }
1741 }
1742
1743 return 0;
1744 }
1745
1746 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1747 {
1748 struct type_datum *upper;
1749 struct policydb *p = datap;
1750 int depth = 0;
1751
1752 upper = datum;
1753 while (upper->bounds) {
1754 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1755 printk(KERN_ERR "SELinux: type %s: "
1756 "too deep or looped boundary\n",
1757 (char *) key);
1758 return -EINVAL;
1759 }
1760
1761 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1762 upper->bounds - 1);
1763 BUG_ON(!upper);
1764
1765 if (upper->attribute) {
1766 printk(KERN_ERR "SELinux: type %s: "
1767 "bounded by attribute %s",
1768 (char *) key,
1769 sym_name(p, SYM_TYPES, upper->value - 1));
1770 return -EINVAL;
1771 }
1772 }
1773
1774 return 0;
1775 }
1776
1777 static int policydb_bounds_sanity_check(struct policydb *p)
1778 {
1779 int rc;
1780
1781 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1782 return 0;
1783
1784 rc = hashtab_map(p->p_users.table,
1785 user_bounds_sanity_check, p);
1786 if (rc)
1787 return rc;
1788
1789 rc = hashtab_map(p->p_roles.table,
1790 role_bounds_sanity_check, p);
1791 if (rc)
1792 return rc;
1793
1794 rc = hashtab_map(p->p_types.table,
1795 type_bounds_sanity_check, p);
1796 if (rc)
1797 return rc;
1798
1799 return 0;
1800 }
1801
1802 u16 string_to_security_class(struct policydb *p, const char *name)
1803 {
1804 struct class_datum *cladatum;
1805
1806 cladatum = hashtab_search(p->p_classes.table, name);
1807 if (!cladatum)
1808 return 0;
1809
1810 return cladatum->value;
1811 }
1812
1813 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1814 {
1815 struct class_datum *cladatum;
1816 struct perm_datum *perdatum = NULL;
1817 struct common_datum *comdatum;
1818
1819 if (!tclass || tclass > p->p_classes.nprim)
1820 return 0;
1821
1822 cladatum = p->class_val_to_struct[tclass-1];
1823 comdatum = cladatum->comdatum;
1824 if (comdatum)
1825 perdatum = hashtab_search(comdatum->permissions.table,
1826 name);
1827 if (!perdatum)
1828 perdatum = hashtab_search(cladatum->permissions.table,
1829 name);
1830 if (!perdatum)
1831 return 0;
1832
1833 return 1U << (perdatum->value-1);
1834 }
1835
1836 static int range_read(struct policydb *p, void *fp)
1837 {
1838 struct range_trans *rt;
1839 struct mls_range *r = NULL;
1840 int i, rc;
1841 __le32 buf[2];
1842 u32 nel;
1843
1844 if (p->policyvers < POLICYDB_VERSION_MLS)
1845 return 0;
1846
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc)
1849 return rc;
1850
1851 nel = le32_to_cpu(buf[0]);
1852 for (i = 0; i < nel; i++) {
1853 rc = -ENOMEM;
1854 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1855 if (!rt)
1856 goto out;
1857
1858 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1859 if (rc)
1860 goto out;
1861
1862 rt->source_type = le32_to_cpu(buf[0]);
1863 rt->target_type = le32_to_cpu(buf[1]);
1864 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1865 rc = next_entry(buf, fp, sizeof(u32));
1866 if (rc)
1867 goto out;
1868 rt->target_class = le32_to_cpu(buf[0]);
1869 } else
1870 rt->target_class = p->process_class;
1871
1872 rc = -EINVAL;
1873 if (!policydb_type_isvalid(p, rt->source_type) ||
1874 !policydb_type_isvalid(p, rt->target_type) ||
1875 !policydb_class_isvalid(p, rt->target_class))
1876 goto out;
1877
1878 rc = -ENOMEM;
1879 r = kzalloc(sizeof(*r), GFP_KERNEL);
1880 if (!r)
1881 goto out;
1882
1883 rc = mls_read_range_helper(r, fp);
1884 if (rc)
1885 goto out;
1886
1887 rc = -EINVAL;
1888 if (!mls_range_isvalid(p, r)) {
1889 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1890 goto out;
1891 }
1892
1893 rc = hashtab_insert(p->range_tr, rt, r);
1894 if (rc)
1895 goto out;
1896
1897 rt = NULL;
1898 r = NULL;
1899 }
1900 hash_eval(p->range_tr, "rangetr");
1901 rc = 0;
1902 out:
1903 kfree(rt);
1904 kfree(r);
1905 return rc;
1906 }
1907
1908 static int filename_trans_read(struct policydb *p, void *fp)
1909 {
1910 struct filename_trans *ft;
1911 struct filename_trans_datum *otype;
1912 char *name;
1913 u32 nel, len;
1914 __le32 buf[4];
1915 int rc, i;
1916
1917 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1918 return 0;
1919
1920 rc = next_entry(buf, fp, sizeof(u32));
1921 if (rc)
1922 return rc;
1923 nel = le32_to_cpu(buf[0]);
1924
1925 for (i = 0; i < nel; i++) {
1926 otype = NULL;
1927 name = NULL;
1928
1929 rc = -ENOMEM;
1930 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1931 if (!ft)
1932 goto out;
1933
1934 rc = -ENOMEM;
1935 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1936 if (!otype)
1937 goto out;
1938
1939 /* length of the path component string */
1940 rc = next_entry(buf, fp, sizeof(u32));
1941 if (rc)
1942 goto out;
1943 len = le32_to_cpu(buf[0]);
1944
1945 /* path component string */
1946 rc = str_read(&name, GFP_KERNEL, fp, len);
1947 if (rc)
1948 goto out;
1949
1950 ft->name = name;
1951
1952 rc = next_entry(buf, fp, sizeof(u32) * 4);
1953 if (rc)
1954 goto out;
1955
1956 ft->stype = le32_to_cpu(buf[0]);
1957 ft->ttype = le32_to_cpu(buf[1]);
1958 ft->tclass = le32_to_cpu(buf[2]);
1959
1960 otype->otype = le32_to_cpu(buf[3]);
1961
1962 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1963 if (rc)
1964 goto out;
1965
1966 rc = hashtab_insert(p->filename_trans, ft, otype);
1967 if (rc) {
1968 /*
1969 * Do not return -EEXIST to the caller, or the system
1970 * will not boot.
1971 */
1972 if (rc != -EEXIST)
1973 goto out;
1974 /* But free memory to avoid memory leak. */
1975 kfree(ft);
1976 kfree(name);
1977 kfree(otype);
1978 }
1979 }
1980 hash_eval(p->filename_trans, "filenametr");
1981 return 0;
1982 out:
1983 kfree(ft);
1984 kfree(name);
1985 kfree(otype);
1986
1987 return rc;
1988 }
1989
1990 static int genfs_read(struct policydb *p, void *fp)
1991 {
1992 int i, j, rc;
1993 u32 nel, nel2, len, len2;
1994 __le32 buf[1];
1995 struct ocontext *l, *c;
1996 struct ocontext *newc = NULL;
1997 struct genfs *genfs_p, *genfs;
1998 struct genfs *newgenfs = NULL;
1999
2000 rc = next_entry(buf, fp, sizeof(u32));
2001 if (rc)
2002 return rc;
2003 nel = le32_to_cpu(buf[0]);
2004
2005 for (i = 0; i < nel; i++) {
2006 rc = next_entry(buf, fp, sizeof(u32));
2007 if (rc)
2008 goto out;
2009 len = le32_to_cpu(buf[0]);
2010
2011 rc = -ENOMEM;
2012 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2013 if (!newgenfs)
2014 goto out;
2015
2016 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
2017 if (rc)
2018 goto out;
2019
2020 for (genfs_p = NULL, genfs = p->genfs; genfs;
2021 genfs_p = genfs, genfs = genfs->next) {
2022 rc = -EINVAL;
2023 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2024 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2025 newgenfs->fstype);
2026 goto out;
2027 }
2028 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2029 break;
2030 }
2031 newgenfs->next = genfs;
2032 if (genfs_p)
2033 genfs_p->next = newgenfs;
2034 else
2035 p->genfs = newgenfs;
2036 genfs = newgenfs;
2037 newgenfs = NULL;
2038
2039 rc = next_entry(buf, fp, sizeof(u32));
2040 if (rc)
2041 goto out;
2042
2043 nel2 = le32_to_cpu(buf[0]);
2044 for (j = 0; j < nel2; j++) {
2045 rc = next_entry(buf, fp, sizeof(u32));
2046 if (rc)
2047 goto out;
2048 len = le32_to_cpu(buf[0]);
2049
2050 rc = -ENOMEM;
2051 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2052 if (!newc)
2053 goto out;
2054
2055 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
2056 if (rc)
2057 goto out;
2058
2059 rc = next_entry(buf, fp, sizeof(u32));
2060 if (rc)
2061 goto out;
2062
2063 newc->v.sclass = le32_to_cpu(buf[0]);
2064 rc = context_read_and_validate(&newc->context[0], p, fp);
2065 if (rc)
2066 goto out;
2067
2068 for (l = NULL, c = genfs->head; c;
2069 l = c, c = c->next) {
2070 rc = -EINVAL;
2071 if (!strcmp(newc->u.name, c->u.name) &&
2072 (!c->v.sclass || !newc->v.sclass ||
2073 newc->v.sclass == c->v.sclass)) {
2074 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2075 genfs->fstype, c->u.name);
2076 goto out;
2077 }
2078 len = strlen(newc->u.name);
2079 len2 = strlen(c->u.name);
2080 if (len > len2)
2081 break;
2082 }
2083
2084 newc->next = c;
2085 if (l)
2086 l->next = newc;
2087 else
2088 genfs->head = newc;
2089 newc = NULL;
2090 }
2091 }
2092 rc = 0;
2093 out:
2094 if (newgenfs) {
2095 kfree(newgenfs->fstype);
2096 kfree(newgenfs);
2097 }
2098 ocontext_destroy(newc, OCON_FSUSE);
2099
2100 return rc;
2101 }
2102
2103 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2104 void *fp)
2105 {
2106 int i, j, rc;
2107 u32 nel, len;
2108 __le32 buf[3];
2109 struct ocontext *l, *c;
2110 u32 nodebuf[8];
2111
2112 for (i = 0; i < info->ocon_num; i++) {
2113 rc = next_entry(buf, fp, sizeof(u32));
2114 if (rc)
2115 goto out;
2116 nel = le32_to_cpu(buf[0]);
2117
2118 l = NULL;
2119 for (j = 0; j < nel; j++) {
2120 rc = -ENOMEM;
2121 c = kzalloc(sizeof(*c), GFP_KERNEL);
2122 if (!c)
2123 goto out;
2124 if (l)
2125 l->next = c;
2126 else
2127 p->ocontexts[i] = c;
2128 l = c;
2129
2130 switch (i) {
2131 case OCON_ISID:
2132 rc = next_entry(buf, fp, sizeof(u32));
2133 if (rc)
2134 goto out;
2135
2136 c->sid[0] = le32_to_cpu(buf[0]);
2137 rc = context_read_and_validate(&c->context[0], p, fp);
2138 if (rc)
2139 goto out;
2140 break;
2141 case OCON_FS:
2142 case OCON_NETIF:
2143 rc = next_entry(buf, fp, sizeof(u32));
2144 if (rc)
2145 goto out;
2146 len = le32_to_cpu(buf[0]);
2147
2148 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2149 if (rc)
2150 goto out;
2151
2152 rc = context_read_and_validate(&c->context[0], p, fp);
2153 if (rc)
2154 goto out;
2155 rc = context_read_and_validate(&c->context[1], p, fp);
2156 if (rc)
2157 goto out;
2158 break;
2159 case OCON_PORT:
2160 rc = next_entry(buf, fp, sizeof(u32)*3);
2161 if (rc)
2162 goto out;
2163 c->u.port.protocol = le32_to_cpu(buf[0]);
2164 c->u.port.low_port = le32_to_cpu(buf[1]);
2165 c->u.port.high_port = le32_to_cpu(buf[2]);
2166 rc = context_read_and_validate(&c->context[0], p, fp);
2167 if (rc)
2168 goto out;
2169 break;
2170 case OCON_NODE:
2171 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2172 if (rc)
2173 goto out;
2174 c->u.node.addr = nodebuf[0]; /* network order */
2175 c->u.node.mask = nodebuf[1]; /* network order */
2176 rc = context_read_and_validate(&c->context[0], p, fp);
2177 if (rc)
2178 goto out;
2179 break;
2180 case OCON_FSUSE:
2181 rc = next_entry(buf, fp, sizeof(u32)*2);
2182 if (rc)
2183 goto out;
2184
2185 rc = -EINVAL;
2186 c->v.behavior = le32_to_cpu(buf[0]);
2187 /* Determined at runtime, not in policy DB. */
2188 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2189 goto out;
2190 if (c->v.behavior > SECURITY_FS_USE_MAX)
2191 goto out;
2192
2193 len = le32_to_cpu(buf[1]);
2194 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
2195 if (rc)
2196 goto out;
2197
2198 rc = context_read_and_validate(&c->context[0], p, fp);
2199 if (rc)
2200 goto out;
2201 break;
2202 case OCON_NODE6: {
2203 int k;
2204
2205 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2206 if (rc)
2207 goto out;
2208 for (k = 0; k < 4; k++)
2209 c->u.node6.addr[k] = nodebuf[k];
2210 for (k = 0; k < 4; k++)
2211 c->u.node6.mask[k] = nodebuf[k+4];
2212 rc = context_read_and_validate(&c->context[0], p, fp);
2213 if (rc)
2214 goto out;
2215 break;
2216 }
2217 }
2218 }
2219 }
2220 rc = 0;
2221 out:
2222 return rc;
2223 }
2224
2225 /*
2226 * Read the configuration data from a policy database binary
2227 * representation file into a policy database structure.
2228 */
2229 int policydb_read(struct policydb *p, void *fp)
2230 {
2231 struct role_allow *ra, *lra;
2232 struct role_trans *tr, *ltr;
2233 int i, j, rc;
2234 __le32 buf[4];
2235 u32 len, nprim, nel;
2236
2237 char *policydb_str;
2238 struct policydb_compat_info *info;
2239
2240 rc = policydb_init(p);
2241 if (rc)
2242 return rc;
2243
2244 /* Read the magic number and string length. */
2245 rc = next_entry(buf, fp, sizeof(u32) * 2);
2246 if (rc)
2247 goto bad;
2248
2249 rc = -EINVAL;
2250 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2251 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2252 "not match expected magic number 0x%x\n",
2253 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2254 goto bad;
2255 }
2256
2257 rc = -EINVAL;
2258 len = le32_to_cpu(buf[1]);
2259 if (len != strlen(POLICYDB_STRING)) {
2260 printk(KERN_ERR "SELinux: policydb string length %d does not "
2261 "match expected length %zu\n",
2262 len, strlen(POLICYDB_STRING));
2263 goto bad;
2264 }
2265
2266 rc = -ENOMEM;
2267 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2268 if (!policydb_str) {
2269 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2270 "string of length %d\n", len);
2271 goto bad;
2272 }
2273
2274 rc = next_entry(policydb_str, fp, len);
2275 if (rc) {
2276 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2277 kfree(policydb_str);
2278 goto bad;
2279 }
2280
2281 rc = -EINVAL;
2282 policydb_str[len] = '\0';
2283 if (strcmp(policydb_str, POLICYDB_STRING)) {
2284 printk(KERN_ERR "SELinux: policydb string %s does not match "
2285 "my string %s\n", policydb_str, POLICYDB_STRING);
2286 kfree(policydb_str);
2287 goto bad;
2288 }
2289 /* Done with policydb_str. */
2290 kfree(policydb_str);
2291 policydb_str = NULL;
2292
2293 /* Read the version and table sizes. */
2294 rc = next_entry(buf, fp, sizeof(u32)*4);
2295 if (rc)
2296 goto bad;
2297
2298 rc = -EINVAL;
2299 p->policyvers = le32_to_cpu(buf[0]);
2300 if (p->policyvers < POLICYDB_VERSION_MIN ||
2301 p->policyvers > POLICYDB_VERSION_MAX) {
2302 printk(KERN_ERR "SELinux: policydb version %d does not match "
2303 "my version range %d-%d\n",
2304 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2305 goto bad;
2306 }
2307
2308 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2309 p->mls_enabled = 1;
2310
2311 rc = -EINVAL;
2312 if (p->policyvers < POLICYDB_VERSION_MLS) {
2313 printk(KERN_ERR "SELinux: security policydb version %d "
2314 "(MLS) not backwards compatible\n",
2315 p->policyvers);
2316 goto bad;
2317 }
2318 }
2319 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2320 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2321
2322 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2323 rc = ebitmap_read(&p->policycaps, fp);
2324 if (rc)
2325 goto bad;
2326 }
2327
2328 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2329 rc = ebitmap_read(&p->permissive_map, fp);
2330 if (rc)
2331 goto bad;
2332 }
2333
2334 rc = -EINVAL;
2335 info = policydb_lookup_compat(p->policyvers);
2336 if (!info) {
2337 printk(KERN_ERR "SELinux: unable to find policy compat info "
2338 "for version %d\n", p->policyvers);
2339 goto bad;
2340 }
2341
2342 rc = -EINVAL;
2343 if (le32_to_cpu(buf[2]) != info->sym_num ||
2344 le32_to_cpu(buf[3]) != info->ocon_num) {
2345 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2346 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2347 le32_to_cpu(buf[3]),
2348 info->sym_num, info->ocon_num);
2349 goto bad;
2350 }
2351
2352 for (i = 0; i < info->sym_num; i++) {
2353 rc = next_entry(buf, fp, sizeof(u32)*2);
2354 if (rc)
2355 goto bad;
2356 nprim = le32_to_cpu(buf[0]);
2357 nel = le32_to_cpu(buf[1]);
2358 for (j = 0; j < nel; j++) {
2359 rc = read_f[i](p, p->symtab[i].table, fp);
2360 if (rc)
2361 goto bad;
2362 }
2363
2364 p->symtab[i].nprim = nprim;
2365 }
2366
2367 rc = -EINVAL;
2368 p->process_class = string_to_security_class(p, "process");
2369 if (!p->process_class)
2370 goto bad;
2371
2372 rc = avtab_read(&p->te_avtab, fp, p);
2373 if (rc)
2374 goto bad;
2375
2376 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2377 rc = cond_read_list(p, fp);
2378 if (rc)
2379 goto bad;
2380 }
2381
2382 rc = next_entry(buf, fp, sizeof(u32));
2383 if (rc)
2384 goto bad;
2385 nel = le32_to_cpu(buf[0]);
2386 ltr = NULL;
2387 for (i = 0; i < nel; i++) {
2388 rc = -ENOMEM;
2389 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2390 if (!tr)
2391 goto bad;
2392 if (ltr)
2393 ltr->next = tr;
2394 else
2395 p->role_tr = tr;
2396 rc = next_entry(buf, fp, sizeof(u32)*3);
2397 if (rc)
2398 goto bad;
2399
2400 rc = -EINVAL;
2401 tr->role = le32_to_cpu(buf[0]);
2402 tr->type = le32_to_cpu(buf[1]);
2403 tr->new_role = le32_to_cpu(buf[2]);
2404 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2405 rc = next_entry(buf, fp, sizeof(u32));
2406 if (rc)
2407 goto bad;
2408 tr->tclass = le32_to_cpu(buf[0]);
2409 } else
2410 tr->tclass = p->process_class;
2411
2412 rc = -EINVAL;
2413 if (!policydb_role_isvalid(p, tr->role) ||
2414 !policydb_type_isvalid(p, tr->type) ||
2415 !policydb_class_isvalid(p, tr->tclass) ||
2416 !policydb_role_isvalid(p, tr->new_role))
2417 goto bad;
2418 ltr = tr;
2419 }
2420
2421 rc = next_entry(buf, fp, sizeof(u32));
2422 if (rc)
2423 goto bad;
2424 nel = le32_to_cpu(buf[0]);
2425 lra = NULL;
2426 for (i = 0; i < nel; i++) {
2427 rc = -ENOMEM;
2428 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2429 if (!ra)
2430 goto bad;
2431 if (lra)
2432 lra->next = ra;
2433 else
2434 p->role_allow = ra;
2435 rc = next_entry(buf, fp, sizeof(u32)*2);
2436 if (rc)
2437 goto bad;
2438
2439 rc = -EINVAL;
2440 ra->role = le32_to_cpu(buf[0]);
2441 ra->new_role = le32_to_cpu(buf[1]);
2442 if (!policydb_role_isvalid(p, ra->role) ||
2443 !policydb_role_isvalid(p, ra->new_role))
2444 goto bad;
2445 lra = ra;
2446 }
2447
2448 rc = filename_trans_read(p, fp);
2449 if (rc)
2450 goto bad;
2451
2452 rc = policydb_index(p);
2453 if (rc)
2454 goto bad;
2455
2456 rc = -EINVAL;
2457 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2458 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2459 if (!p->process_trans_perms)
2460 goto bad;
2461
2462 rc = ocontext_read(p, info, fp);
2463 if (rc)
2464 goto bad;
2465
2466 rc = genfs_read(p, fp);
2467 if (rc)
2468 goto bad;
2469
2470 rc = range_read(p, fp);
2471 if (rc)
2472 goto bad;
2473
2474 rc = -ENOMEM;
2475 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2476 p->p_types.nprim,
2477 GFP_KERNEL | __GFP_ZERO);
2478 if (!p->type_attr_map_array)
2479 goto bad;
2480
2481 /* preallocate so we don't have to worry about the put ever failing */
2482 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
2483 GFP_KERNEL | __GFP_ZERO);
2484 if (rc)
2485 goto bad;
2486
2487 for (i = 0; i < p->p_types.nprim; i++) {
2488 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2489
2490 BUG_ON(!e);
2491 ebitmap_init(e);
2492 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2493 rc = ebitmap_read(e, fp);
2494 if (rc)
2495 goto bad;
2496 }
2497 /* add the type itself as the degenerate case */
2498 rc = ebitmap_set_bit(e, i, 1);
2499 if (rc)
2500 goto bad;
2501 }
2502
2503 rc = policydb_bounds_sanity_check(p);
2504 if (rc)
2505 goto bad;
2506
2507 rc = 0;
2508 out:
2509 return rc;
2510 bad:
2511 policydb_destroy(p);
2512 goto out;
2513 }
2514
2515 /*
2516 * Write a MLS level structure to a policydb binary
2517 * representation file.
2518 */
2519 static int mls_write_level(struct mls_level *l, void *fp)
2520 {
2521 __le32 buf[1];
2522 int rc;
2523
2524 buf[0] = cpu_to_le32(l->sens);
2525 rc = put_entry(buf, sizeof(u32), 1, fp);
2526 if (rc)
2527 return rc;
2528
2529 rc = ebitmap_write(&l->cat, fp);
2530 if (rc)
2531 return rc;
2532
2533 return 0;
2534 }
2535
2536 /*
2537 * Write a MLS range structure to a policydb binary
2538 * representation file.
2539 */
2540 static int mls_write_range_helper(struct mls_range *r, void *fp)
2541 {
2542 __le32 buf[3];
2543 size_t items;
2544 int rc, eq;
2545
2546 eq = mls_level_eq(&r->level[1], &r->level[0]);
2547
2548 if (eq)
2549 items = 2;
2550 else
2551 items = 3;
2552 buf[0] = cpu_to_le32(items-1);
2553 buf[1] = cpu_to_le32(r->level[0].sens);
2554 if (!eq)
2555 buf[2] = cpu_to_le32(r->level[1].sens);
2556
2557 BUG_ON(items > ARRAY_SIZE(buf));
2558
2559 rc = put_entry(buf, sizeof(u32), items, fp);
2560 if (rc)
2561 return rc;
2562
2563 rc = ebitmap_write(&r->level[0].cat, fp);
2564 if (rc)
2565 return rc;
2566 if (!eq) {
2567 rc = ebitmap_write(&r->level[1].cat, fp);
2568 if (rc)
2569 return rc;
2570 }
2571
2572 return 0;
2573 }
2574
2575 static int sens_write(void *vkey, void *datum, void *ptr)
2576 {
2577 char *key = vkey;
2578 struct level_datum *levdatum = datum;
2579 struct policy_data *pd = ptr;
2580 void *fp = pd->fp;
2581 __le32 buf[2];
2582 size_t len;
2583 int rc;
2584
2585 len = strlen(key);
2586 buf[0] = cpu_to_le32(len);
2587 buf[1] = cpu_to_le32(levdatum->isalias);
2588 rc = put_entry(buf, sizeof(u32), 2, fp);
2589 if (rc)
2590 return rc;
2591
2592 rc = put_entry(key, 1, len, fp);
2593 if (rc)
2594 return rc;
2595
2596 rc = mls_write_level(levdatum->level, fp);
2597 if (rc)
2598 return rc;
2599
2600 return 0;
2601 }
2602
2603 static int cat_write(void *vkey, void *datum, void *ptr)
2604 {
2605 char *key = vkey;
2606 struct cat_datum *catdatum = datum;
2607 struct policy_data *pd = ptr;
2608 void *fp = pd->fp;
2609 __le32 buf[3];
2610 size_t len;
2611 int rc;
2612
2613 len = strlen(key);
2614 buf[0] = cpu_to_le32(len);
2615 buf[1] = cpu_to_le32(catdatum->value);
2616 buf[2] = cpu_to_le32(catdatum->isalias);
2617 rc = put_entry(buf, sizeof(u32), 3, fp);
2618 if (rc)
2619 return rc;
2620
2621 rc = put_entry(key, 1, len, fp);
2622 if (rc)
2623 return rc;
2624
2625 return 0;
2626 }
2627
2628 static int role_trans_write(struct policydb *p, void *fp)
2629 {
2630 struct role_trans *r = p->role_tr;
2631 struct role_trans *tr;
2632 u32 buf[3];
2633 size_t nel;
2634 int rc;
2635
2636 nel = 0;
2637 for (tr = r; tr; tr = tr->next)
2638 nel++;
2639 buf[0] = cpu_to_le32(nel);
2640 rc = put_entry(buf, sizeof(u32), 1, fp);
2641 if (rc)
2642 return rc;
2643 for (tr = r; tr; tr = tr->next) {
2644 buf[0] = cpu_to_le32(tr->role);
2645 buf[1] = cpu_to_le32(tr->type);
2646 buf[2] = cpu_to_le32(tr->new_role);
2647 rc = put_entry(buf, sizeof(u32), 3, fp);
2648 if (rc)
2649 return rc;
2650 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2651 buf[0] = cpu_to_le32(tr->tclass);
2652 rc = put_entry(buf, sizeof(u32), 1, fp);
2653 if (rc)
2654 return rc;
2655 }
2656 }
2657
2658 return 0;
2659 }
2660
2661 static int role_allow_write(struct role_allow *r, void *fp)
2662 {
2663 struct role_allow *ra;
2664 u32 buf[2];
2665 size_t nel;
2666 int rc;
2667
2668 nel = 0;
2669 for (ra = r; ra; ra = ra->next)
2670 nel++;
2671 buf[0] = cpu_to_le32(nel);
2672 rc = put_entry(buf, sizeof(u32), 1, fp);
2673 if (rc)
2674 return rc;
2675 for (ra = r; ra; ra = ra->next) {
2676 buf[0] = cpu_to_le32(ra->role);
2677 buf[1] = cpu_to_le32(ra->new_role);
2678 rc = put_entry(buf, sizeof(u32), 2, fp);
2679 if (rc)
2680 return rc;
2681 }
2682 return 0;
2683 }
2684
2685 /*
2686 * Write a security context structure
2687 * to a policydb binary representation file.
2688 */
2689 static int context_write(struct policydb *p, struct context *c,
2690 void *fp)
2691 {
2692 int rc;
2693 __le32 buf[3];
2694
2695 buf[0] = cpu_to_le32(c->user);
2696 buf[1] = cpu_to_le32(c->role);
2697 buf[2] = cpu_to_le32(c->type);
2698
2699 rc = put_entry(buf, sizeof(u32), 3, fp);
2700 if (rc)
2701 return rc;
2702
2703 rc = mls_write_range_helper(&c->range, fp);
2704 if (rc)
2705 return rc;
2706
2707 return 0;
2708 }
2709
2710 /*
2711 * The following *_write functions are used to
2712 * write the symbol data to a policy database
2713 * binary representation file.
2714 */
2715
2716 static int perm_write(void *vkey, void *datum, void *fp)
2717 {
2718 char *key = vkey;
2719 struct perm_datum *perdatum = datum;
2720 __le32 buf[2];
2721 size_t len;
2722 int rc;
2723
2724 len = strlen(key);
2725 buf[0] = cpu_to_le32(len);
2726 buf[1] = cpu_to_le32(perdatum->value);
2727 rc = put_entry(buf, sizeof(u32), 2, fp);
2728 if (rc)
2729 return rc;
2730
2731 rc = put_entry(key, 1, len, fp);
2732 if (rc)
2733 return rc;
2734
2735 return 0;
2736 }
2737
2738 static int common_write(void *vkey, void *datum, void *ptr)
2739 {
2740 char *key = vkey;
2741 struct common_datum *comdatum = datum;
2742 struct policy_data *pd = ptr;
2743 void *fp = pd->fp;
2744 __le32 buf[4];
2745 size_t len;
2746 int rc;
2747
2748 len = strlen(key);
2749 buf[0] = cpu_to_le32(len);
2750 buf[1] = cpu_to_le32(comdatum->value);
2751 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2752 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2753 rc = put_entry(buf, sizeof(u32), 4, fp);
2754 if (rc)
2755 return rc;
2756
2757 rc = put_entry(key, 1, len, fp);
2758 if (rc)
2759 return rc;
2760
2761 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2762 if (rc)
2763 return rc;
2764
2765 return 0;
2766 }
2767
2768 static int type_set_write(struct type_set *t, void *fp)
2769 {
2770 int rc;
2771 __le32 buf[1];
2772
2773 if (ebitmap_write(&t->types, fp))
2774 return -EINVAL;
2775 if (ebitmap_write(&t->negset, fp))
2776 return -EINVAL;
2777
2778 buf[0] = cpu_to_le32(t->flags);
2779 rc = put_entry(buf, sizeof(u32), 1, fp);
2780 if (rc)
2781 return -EINVAL;
2782
2783 return 0;
2784 }
2785
2786 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2787 void *fp)
2788 {
2789 struct constraint_node *c;
2790 struct constraint_expr *e;
2791 __le32 buf[3];
2792 u32 nel;
2793 int rc;
2794
2795 for (c = node; c; c = c->next) {
2796 nel = 0;
2797 for (e = c->expr; e; e = e->next)
2798 nel++;
2799 buf[0] = cpu_to_le32(c->permissions);
2800 buf[1] = cpu_to_le32(nel);
2801 rc = put_entry(buf, sizeof(u32), 2, fp);
2802 if (rc)
2803 return rc;
2804 for (e = c->expr; e; e = e->next) {
2805 buf[0] = cpu_to_le32(e->expr_type);
2806 buf[1] = cpu_to_le32(e->attr);
2807 buf[2] = cpu_to_le32(e->op);
2808 rc = put_entry(buf, sizeof(u32), 3, fp);
2809 if (rc)
2810 return rc;
2811
2812 switch (e->expr_type) {
2813 case CEXPR_NAMES:
2814 rc = ebitmap_write(&e->names, fp);
2815 if (rc)
2816 return rc;
2817 if (p->policyvers >=
2818 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2819 rc = type_set_write(e->type_names, fp);
2820 if (rc)
2821 return rc;
2822 }
2823 break;
2824 default:
2825 break;
2826 }
2827 }
2828 }
2829
2830 return 0;
2831 }
2832
2833 static int class_write(void *vkey, void *datum, void *ptr)
2834 {
2835 char *key = vkey;
2836 struct class_datum *cladatum = datum;
2837 struct policy_data *pd = ptr;
2838 void *fp = pd->fp;
2839 struct policydb *p = pd->p;
2840 struct constraint_node *c;
2841 __le32 buf[6];
2842 u32 ncons;
2843 size_t len, len2;
2844 int rc;
2845
2846 len = strlen(key);
2847 if (cladatum->comkey)
2848 len2 = strlen(cladatum->comkey);
2849 else
2850 len2 = 0;
2851
2852 ncons = 0;
2853 for (c = cladatum->constraints; c; c = c->next)
2854 ncons++;
2855
2856 buf[0] = cpu_to_le32(len);
2857 buf[1] = cpu_to_le32(len2);
2858 buf[2] = cpu_to_le32(cladatum->value);
2859 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2860 if (cladatum->permissions.table)
2861 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2862 else
2863 buf[4] = 0;
2864 buf[5] = cpu_to_le32(ncons);
2865 rc = put_entry(buf, sizeof(u32), 6, fp);
2866 if (rc)
2867 return rc;
2868
2869 rc = put_entry(key, 1, len, fp);
2870 if (rc)
2871 return rc;
2872
2873 if (cladatum->comkey) {
2874 rc = put_entry(cladatum->comkey, 1, len2, fp);
2875 if (rc)
2876 return rc;
2877 }
2878
2879 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2880 if (rc)
2881 return rc;
2882
2883 rc = write_cons_helper(p, cladatum->constraints, fp);
2884 if (rc)
2885 return rc;
2886
2887 /* write out the validatetrans rule */
2888 ncons = 0;
2889 for (c = cladatum->validatetrans; c; c = c->next)
2890 ncons++;
2891
2892 buf[0] = cpu_to_le32(ncons);
2893 rc = put_entry(buf, sizeof(u32), 1, fp);
2894 if (rc)
2895 return rc;
2896
2897 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2898 if (rc)
2899 return rc;
2900
2901 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2902 buf[0] = cpu_to_le32(cladatum->default_user);
2903 buf[1] = cpu_to_le32(cladatum->default_role);
2904 buf[2] = cpu_to_le32(cladatum->default_range);
2905
2906 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2907 if (rc)
2908 return rc;
2909 }
2910
2911 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2912 buf[0] = cpu_to_le32(cladatum->default_type);
2913 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2914 if (rc)
2915 return rc;
2916 }
2917
2918 return 0;
2919 }
2920
2921 static int role_write(void *vkey, void *datum, void *ptr)
2922 {
2923 char *key = vkey;
2924 struct role_datum *role = datum;
2925 struct policy_data *pd = ptr;
2926 void *fp = pd->fp;
2927 struct policydb *p = pd->p;
2928 __le32 buf[3];
2929 size_t items, len;
2930 int rc;
2931
2932 len = strlen(key);
2933 items = 0;
2934 buf[items++] = cpu_to_le32(len);
2935 buf[items++] = cpu_to_le32(role->value);
2936 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2937 buf[items++] = cpu_to_le32(role->bounds);
2938
2939 BUG_ON(items > ARRAY_SIZE(buf));
2940
2941 rc = put_entry(buf, sizeof(u32), items, fp);
2942 if (rc)
2943 return rc;
2944
2945 rc = put_entry(key, 1, len, fp);
2946 if (rc)
2947 return rc;
2948
2949 rc = ebitmap_write(&role->dominates, fp);
2950 if (rc)
2951 return rc;
2952
2953 rc = ebitmap_write(&role->types, fp);
2954 if (rc)
2955 return rc;
2956
2957 return 0;
2958 }
2959
2960 static int type_write(void *vkey, void *datum, void *ptr)
2961 {
2962 char *key = vkey;
2963 struct type_datum *typdatum = datum;
2964 struct policy_data *pd = ptr;
2965 struct policydb *p = pd->p;
2966 void *fp = pd->fp;
2967 __le32 buf[4];
2968 int rc;
2969 size_t items, len;
2970
2971 len = strlen(key);
2972 items = 0;
2973 buf[items++] = cpu_to_le32(len);
2974 buf[items++] = cpu_to_le32(typdatum->value);
2975 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2976 u32 properties = 0;
2977
2978 if (typdatum->primary)
2979 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2980
2981 if (typdatum->attribute)
2982 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2983
2984 buf[items++] = cpu_to_le32(properties);
2985 buf[items++] = cpu_to_le32(typdatum->bounds);
2986 } else {
2987 buf[items++] = cpu_to_le32(typdatum->primary);
2988 }
2989 BUG_ON(items > ARRAY_SIZE(buf));
2990 rc = put_entry(buf, sizeof(u32), items, fp);
2991 if (rc)
2992 return rc;
2993
2994 rc = put_entry(key, 1, len, fp);
2995 if (rc)
2996 return rc;
2997
2998 return 0;
2999 }
3000
3001 static int user_write(void *vkey, void *datum, void *ptr)
3002 {
3003 char *key = vkey;
3004 struct user_datum *usrdatum = datum;
3005 struct policy_data *pd = ptr;
3006 struct policydb *p = pd->p;
3007 void *fp = pd->fp;
3008 __le32 buf[3];
3009 size_t items, len;
3010 int rc;
3011
3012 len = strlen(key);
3013 items = 0;
3014 buf[items++] = cpu_to_le32(len);
3015 buf[items++] = cpu_to_le32(usrdatum->value);
3016 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3017 buf[items++] = cpu_to_le32(usrdatum->bounds);
3018 BUG_ON(items > ARRAY_SIZE(buf));
3019 rc = put_entry(buf, sizeof(u32), items, fp);
3020 if (rc)
3021 return rc;
3022
3023 rc = put_entry(key, 1, len, fp);
3024 if (rc)
3025 return rc;
3026
3027 rc = ebitmap_write(&usrdatum->roles, fp);
3028 if (rc)
3029 return rc;
3030
3031 rc = mls_write_range_helper(&usrdatum->range, fp);
3032 if (rc)
3033 return rc;
3034
3035 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3036 if (rc)
3037 return rc;
3038
3039 return 0;
3040 }
3041
3042 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3043 void *datap) =
3044 {
3045 common_write,
3046 class_write,
3047 role_write,
3048 type_write,
3049 user_write,
3050 cond_write_bool,
3051 sens_write,
3052 cat_write,
3053 };
3054
3055 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3056 void *fp)
3057 {
3058 unsigned int i, j, rc;
3059 size_t nel, len;
3060 __le32 buf[3];
3061 u32 nodebuf[8];
3062 struct ocontext *c;
3063 for (i = 0; i < info->ocon_num; i++) {
3064 nel = 0;
3065 for (c = p->ocontexts[i]; c; c = c->next)
3066 nel++;
3067 buf[0] = cpu_to_le32(nel);
3068 rc = put_entry(buf, sizeof(u32), 1, fp);
3069 if (rc)
3070 return rc;
3071 for (c = p->ocontexts[i]; c; c = c->next) {
3072 switch (i) {
3073 case OCON_ISID:
3074 buf[0] = cpu_to_le32(c->sid[0]);
3075 rc = put_entry(buf, sizeof(u32), 1, fp);
3076 if (rc)
3077 return rc;
3078 rc = context_write(p, &c->context[0], fp);
3079 if (rc)
3080 return rc;
3081 break;
3082 case OCON_FS:
3083 case OCON_NETIF:
3084 len = strlen(c->u.name);
3085 buf[0] = cpu_to_le32(len);
3086 rc = put_entry(buf, sizeof(u32), 1, fp);
3087 if (rc)
3088 return rc;
3089 rc = put_entry(c->u.name, 1, len, fp);
3090 if (rc)
3091 return rc;
3092 rc = context_write(p, &c->context[0], fp);
3093 if (rc)
3094 return rc;
3095 rc = context_write(p, &c->context[1], fp);
3096 if (rc)
3097 return rc;
3098 break;
3099 case OCON_PORT:
3100 buf[0] = cpu_to_le32(c->u.port.protocol);
3101 buf[1] = cpu_to_le32(c->u.port.low_port);
3102 buf[2] = cpu_to_le32(c->u.port.high_port);
3103 rc = put_entry(buf, sizeof(u32), 3, fp);
3104 if (rc)
3105 return rc;
3106 rc = context_write(p, &c->context[0], fp);
3107 if (rc)
3108 return rc;
3109 break;
3110 case OCON_NODE:
3111 nodebuf[0] = c->u.node.addr; /* network order */
3112 nodebuf[1] = c->u.node.mask; /* network order */
3113 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3114 if (rc)
3115 return rc;
3116 rc = context_write(p, &c->context[0], fp);
3117 if (rc)
3118 return rc;
3119 break;
3120 case OCON_FSUSE:
3121 buf[0] = cpu_to_le32(c->v.behavior);
3122 len = strlen(c->u.name);
3123 buf[1] = cpu_to_le32(len);
3124 rc = put_entry(buf, sizeof(u32), 2, fp);
3125 if (rc)
3126 return rc;
3127 rc = put_entry(c->u.name, 1, len, fp);
3128 if (rc)
3129 return rc;
3130 rc = context_write(p, &c->context[0], fp);
3131 if (rc)
3132 return rc;
3133 break;
3134 case OCON_NODE6:
3135 for (j = 0; j < 4; j++)
3136 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3137 for (j = 0; j < 4; j++)
3138 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3139 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3140 if (rc)
3141 return rc;
3142 rc = context_write(p, &c->context[0], fp);
3143 if (rc)
3144 return rc;
3145 break;
3146 }
3147 }
3148 }
3149 return 0;
3150 }
3151
3152 static int genfs_write(struct policydb *p, void *fp)
3153 {
3154 struct genfs *genfs;
3155 struct ocontext *c;
3156 size_t len;
3157 __le32 buf[1];
3158 int rc;
3159
3160 len = 0;
3161 for (genfs = p->genfs; genfs; genfs = genfs->next)
3162 len++;
3163 buf[0] = cpu_to_le32(len);
3164 rc = put_entry(buf, sizeof(u32), 1, fp);
3165 if (rc)
3166 return rc;
3167 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3168 len = strlen(genfs->fstype);
3169 buf[0] = cpu_to_le32(len);
3170 rc = put_entry(buf, sizeof(u32), 1, fp);
3171 if (rc)
3172 return rc;
3173 rc = put_entry(genfs->fstype, 1, len, fp);
3174 if (rc)
3175 return rc;
3176 len = 0;
3177 for (c = genfs->head; c; c = c->next)
3178 len++;
3179 buf[0] = cpu_to_le32(len);
3180 rc = put_entry(buf, sizeof(u32), 1, fp);
3181 if (rc)
3182 return rc;
3183 for (c = genfs->head; c; c = c->next) {
3184 len = strlen(c->u.name);
3185 buf[0] = cpu_to_le32(len);
3186 rc = put_entry(buf, sizeof(u32), 1, fp);
3187 if (rc)
3188 return rc;
3189 rc = put_entry(c->u.name, 1, len, fp);
3190 if (rc)
3191 return rc;
3192 buf[0] = cpu_to_le32(c->v.sclass);
3193 rc = put_entry(buf, sizeof(u32), 1, fp);
3194 if (rc)
3195 return rc;
3196 rc = context_write(p, &c->context[0], fp);
3197 if (rc)
3198 return rc;
3199 }
3200 }
3201 return 0;
3202 }
3203
3204 static int hashtab_cnt(void *key, void *data, void *ptr)
3205 {
3206 int *cnt = ptr;
3207 *cnt = *cnt + 1;
3208
3209 return 0;
3210 }
3211
3212 static int range_write_helper(void *key, void *data, void *ptr)
3213 {
3214 __le32 buf[2];
3215 struct range_trans *rt = key;
3216 struct mls_range *r = data;
3217 struct policy_data *pd = ptr;
3218 void *fp = pd->fp;
3219 struct policydb *p = pd->p;
3220 int rc;
3221
3222 buf[0] = cpu_to_le32(rt->source_type);
3223 buf[1] = cpu_to_le32(rt->target_type);
3224 rc = put_entry(buf, sizeof(u32), 2, fp);
3225 if (rc)
3226 return rc;
3227 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3228 buf[0] = cpu_to_le32(rt->target_class);
3229 rc = put_entry(buf, sizeof(u32), 1, fp);
3230 if (rc)
3231 return rc;
3232 }
3233 rc = mls_write_range_helper(r, fp);
3234 if (rc)
3235 return rc;
3236
3237 return 0;
3238 }
3239
3240 static int range_write(struct policydb *p, void *fp)
3241 {
3242 __le32 buf[1];
3243 int rc, nel;
3244 struct policy_data pd;
3245
3246 pd.p = p;
3247 pd.fp = fp;
3248
3249 /* count the number of entries in the hashtab */
3250 nel = 0;
3251 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3252 if (rc)
3253 return rc;
3254
3255 buf[0] = cpu_to_le32(nel);
3256 rc = put_entry(buf, sizeof(u32), 1, fp);
3257 if (rc)
3258 return rc;
3259
3260 /* actually write all of the entries */
3261 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3262 if (rc)
3263 return rc;
3264
3265 return 0;
3266 }
3267
3268 static int filename_write_helper(void *key, void *data, void *ptr)
3269 {
3270 __le32 buf[4];
3271 struct filename_trans *ft = key;
3272 struct filename_trans_datum *otype = data;
3273 void *fp = ptr;
3274 int rc;
3275 u32 len;
3276
3277 len = strlen(ft->name);
3278 buf[0] = cpu_to_le32(len);
3279 rc = put_entry(buf, sizeof(u32), 1, fp);
3280 if (rc)
3281 return rc;
3282
3283 rc = put_entry(ft->name, sizeof(char), len, fp);
3284 if (rc)
3285 return rc;
3286
3287 buf[0] = cpu_to_le32(ft->stype);
3288 buf[1] = cpu_to_le32(ft->ttype);
3289 buf[2] = cpu_to_le32(ft->tclass);
3290 buf[3] = cpu_to_le32(otype->otype);
3291
3292 rc = put_entry(buf, sizeof(u32), 4, fp);
3293 if (rc)
3294 return rc;
3295
3296 return 0;
3297 }
3298
3299 static int filename_trans_write(struct policydb *p, void *fp)
3300 {
3301 u32 nel;
3302 __le32 buf[1];
3303 int rc;
3304
3305 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3306 return 0;
3307
3308 nel = 0;
3309 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3310 if (rc)
3311 return rc;
3312
3313 buf[0] = cpu_to_le32(nel);
3314 rc = put_entry(buf, sizeof(u32), 1, fp);
3315 if (rc)
3316 return rc;
3317
3318 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3319 if (rc)
3320 return rc;
3321
3322 return 0;
3323 }
3324
3325 /*
3326 * Write the configuration data in a policy database
3327 * structure to a policy database binary representation
3328 * file.
3329 */
3330 int policydb_write(struct policydb *p, void *fp)
3331 {
3332 unsigned int i, num_syms;
3333 int rc;
3334 __le32 buf[4];
3335 u32 config;
3336 size_t len;
3337 struct policydb_compat_info *info;
3338
3339 /*
3340 * refuse to write policy older than compressed avtab
3341 * to simplify the writer. There are other tests dropped
3342 * since we assume this throughout the writer code. Be
3343 * careful if you ever try to remove this restriction
3344 */
3345 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3346 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3347 " Because it is less than version %d\n", p->policyvers,
3348 POLICYDB_VERSION_AVTAB);
3349 return -EINVAL;
3350 }
3351
3352 config = 0;
3353 if (p->mls_enabled)
3354 config |= POLICYDB_CONFIG_MLS;
3355
3356 if (p->reject_unknown)
3357 config |= REJECT_UNKNOWN;
3358 if (p->allow_unknown)
3359 config |= ALLOW_UNKNOWN;
3360
3361 /* Write the magic number and string identifiers. */
3362 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3363 len = strlen(POLICYDB_STRING);
3364 buf[1] = cpu_to_le32(len);
3365 rc = put_entry(buf, sizeof(u32), 2, fp);
3366 if (rc)
3367 return rc;
3368 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3369 if (rc)
3370 return rc;
3371
3372 /* Write the version, config, and table sizes. */
3373 info = policydb_lookup_compat(p->policyvers);
3374 if (!info) {
3375 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3376 "version %d", p->policyvers);
3377 return -EINVAL;
3378 }
3379
3380 buf[0] = cpu_to_le32(p->policyvers);
3381 buf[1] = cpu_to_le32(config);
3382 buf[2] = cpu_to_le32(info->sym_num);
3383 buf[3] = cpu_to_le32(info->ocon_num);
3384
3385 rc = put_entry(buf, sizeof(u32), 4, fp);
3386 if (rc)
3387 return rc;
3388
3389 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3390 rc = ebitmap_write(&p->policycaps, fp);
3391 if (rc)
3392 return rc;
3393 }
3394
3395 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3396 rc = ebitmap_write(&p->permissive_map, fp);
3397 if (rc)
3398 return rc;
3399 }
3400
3401 num_syms = info->sym_num;
3402 for (i = 0; i < num_syms; i++) {
3403 struct policy_data pd;
3404
3405 pd.fp = fp;
3406 pd.p = p;
3407
3408 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3409 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3410
3411 rc = put_entry(buf, sizeof(u32), 2, fp);
3412 if (rc)
3413 return rc;
3414 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3415 if (rc)
3416 return rc;
3417 }
3418
3419 rc = avtab_write(p, &p->te_avtab, fp);
3420 if (rc)
3421 return rc;
3422
3423 rc = cond_write_list(p, p->cond_list, fp);
3424 if (rc)
3425 return rc;
3426
3427 rc = role_trans_write(p, fp);
3428 if (rc)
3429 return rc;
3430
3431 rc = role_allow_write(p->role_allow, fp);
3432 if (rc)
3433 return rc;
3434
3435 rc = filename_trans_write(p, fp);
3436 if (rc)
3437 return rc;
3438
3439 rc = ocontext_write(p, info, fp);
3440 if (rc)
3441 return rc;
3442
3443 rc = genfs_write(p, fp);
3444 if (rc)
3445 return rc;
3446
3447 rc = range_write(p, fp);
3448 if (rc)
3449 return rc;
3450
3451 for (i = 0; i < p->p_types.nprim; i++) {
3452 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3453
3454 BUG_ON(!e);
3455 rc = ebitmap_write(e, fp);
3456 if (rc)
3457 return rc;
3458 }
3459
3460 return 0;
3461 }