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