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