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