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