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