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