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