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