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 (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2357 rc = ebitmap_read(&p->policycaps, fp);
2358 if (rc)
2359 goto bad;
2360 }
2361
2362 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2363 rc = ebitmap_read(&p->permissive_map, fp);
2364 if (rc)
2365 goto bad;
2366 }
2367
2368 rc = -EINVAL;
2369 info = policydb_lookup_compat(p->policyvers);
2370 if (!info) {
2371 pr_err("SELinux: unable to find policy compat info "
2372 "for version %d\n", p->policyvers);
2373 goto bad;
2374 }
2375
2376 rc = -EINVAL;
2377 if (le32_to_cpu(buf[2]) != info->sym_num ||
2378 le32_to_cpu(buf[3]) != info->ocon_num) {
2379 pr_err("SELinux: policydb table sizes (%d,%d) do "
2380 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2381 le32_to_cpu(buf[3]),
2382 info->sym_num, info->ocon_num);
2383 goto bad;
2384 }
2385
2386 for (i = 0; i < info->sym_num; i++) {
2387 rc = next_entry(buf, fp, sizeof(u32)*2);
2388 if (rc)
2389 goto bad;
2390 nprim = le32_to_cpu(buf[0]);
2391 nel = le32_to_cpu(buf[1]);
2392 for (j = 0; j < nel; j++) {
2393 rc = read_f[i](p, p->symtab[i].table, fp);
2394 if (rc)
2395 goto bad;
2396 }
2397
2398 p->symtab[i].nprim = nprim;
2399 }
2400
2401 rc = -EINVAL;
2402 p->process_class = string_to_security_class(p, "process");
2403 if (!p->process_class)
2404 goto bad;
2405
2406 rc = avtab_read(&p->te_avtab, fp, p);
2407 if (rc)
2408 goto bad;
2409
2410 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2411 rc = cond_read_list(p, fp);
2412 if (rc)
2413 goto bad;
2414 }
2415
2416 rc = next_entry(buf, fp, sizeof(u32));
2417 if (rc)
2418 goto bad;
2419 nel = le32_to_cpu(buf[0]);
2420 ltr = NULL;
2421 for (i = 0; i < nel; i++) {
2422 rc = -ENOMEM;
2423 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2424 if (!tr)
2425 goto bad;
2426 if (ltr)
2427 ltr->next = tr;
2428 else
2429 p->role_tr = tr;
2430 rc = next_entry(buf, fp, sizeof(u32)*3);
2431 if (rc)
2432 goto bad;
2433
2434 rc = -EINVAL;
2435 tr->role = le32_to_cpu(buf[0]);
2436 tr->type = le32_to_cpu(buf[1]);
2437 tr->new_role = le32_to_cpu(buf[2]);
2438 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2439 rc = next_entry(buf, fp, sizeof(u32));
2440 if (rc)
2441 goto bad;
2442 tr->tclass = le32_to_cpu(buf[0]);
2443 } else
2444 tr->tclass = p->process_class;
2445
2446 rc = -EINVAL;
2447 if (!policydb_role_isvalid(p, tr->role) ||
2448 !policydb_type_isvalid(p, tr->type) ||
2449 !policydb_class_isvalid(p, tr->tclass) ||
2450 !policydb_role_isvalid(p, tr->new_role))
2451 goto bad;
2452 ltr = tr;
2453 }
2454
2455 rc = next_entry(buf, fp, sizeof(u32));
2456 if (rc)
2457 goto bad;
2458 nel = le32_to_cpu(buf[0]);
2459 lra = NULL;
2460 for (i = 0; i < nel; i++) {
2461 rc = -ENOMEM;
2462 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2463 if (!ra)
2464 goto bad;
2465 if (lra)
2466 lra->next = ra;
2467 else
2468 p->role_allow = ra;
2469 rc = next_entry(buf, fp, sizeof(u32)*2);
2470 if (rc)
2471 goto bad;
2472
2473 rc = -EINVAL;
2474 ra->role = le32_to_cpu(buf[0]);
2475 ra->new_role = le32_to_cpu(buf[1]);
2476 if (!policydb_role_isvalid(p, ra->role) ||
2477 !policydb_role_isvalid(p, ra->new_role))
2478 goto bad;
2479 lra = ra;
2480 }
2481
2482 rc = filename_trans_read(p, fp);
2483 if (rc)
2484 goto bad;
2485
2486 rc = policydb_index(p);
2487 if (rc)
2488 goto bad;
2489
2490 rc = -EINVAL;
2491 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2492 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2493 if (!p->process_trans_perms)
2494 goto bad;
2495
2496 rc = ocontext_read(p, info, fp);
2497 if (rc)
2498 goto bad;
2499
2500 rc = genfs_read(p, fp);
2501 if (rc)
2502 goto bad;
2503
2504 rc = range_read(p, fp);
2505 if (rc)
2506 goto bad;
2507
2508 p->type_attr_map_array = kvcalloc(p->p_types.nprim,
2509 sizeof(*p->type_attr_map_array),
2510 GFP_KERNEL);
2511 if (!p->type_attr_map_array)
2512 goto bad;
2513
2514 /* just in case ebitmap_init() becomes more than just a memset(0): */
2515 for (i = 0; i < p->p_types.nprim; i++)
2516 ebitmap_init(&p->type_attr_map_array[i]);
2517
2518 for (i = 0; i < p->p_types.nprim; i++) {
2519 struct ebitmap *e = &p->type_attr_map_array[i];
2520
2521 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2522 rc = ebitmap_read(e, fp);
2523 if (rc)
2524 goto bad;
2525 }
2526 /* add the type itself as the degenerate case */
2527 rc = ebitmap_set_bit(e, i, 1);
2528 if (rc)
2529 goto bad;
2530 }
2531
2532 rc = policydb_bounds_sanity_check(p);
2533 if (rc)
2534 goto bad;
2535
2536 rc = 0;
2537 out:
2538 return rc;
2539 bad:
2540 policydb_destroy(p);
2541 goto out;
2542 }
2543
2544 /*
2545 * Write a MLS level structure to a policydb binary
2546 * representation file.
2547 */
mls_write_level(struct mls_level * l,void * fp)2548 static int mls_write_level(struct mls_level *l, void *fp)
2549 {
2550 __le32 buf[1];
2551 int rc;
2552
2553 buf[0] = cpu_to_le32(l->sens);
2554 rc = put_entry(buf, sizeof(u32), 1, fp);
2555 if (rc)
2556 return rc;
2557
2558 rc = ebitmap_write(&l->cat, fp);
2559 if (rc)
2560 return rc;
2561
2562 return 0;
2563 }
2564
2565 /*
2566 * Write a MLS range structure to a policydb binary
2567 * representation file.
2568 */
mls_write_range_helper(struct mls_range * r,void * fp)2569 static int mls_write_range_helper(struct mls_range *r, void *fp)
2570 {
2571 __le32 buf[3];
2572 size_t items;
2573 int rc, eq;
2574
2575 eq = mls_level_eq(&r->level[1], &r->level[0]);
2576
2577 if (eq)
2578 items = 2;
2579 else
2580 items = 3;
2581 buf[0] = cpu_to_le32(items-1);
2582 buf[1] = cpu_to_le32(r->level[0].sens);
2583 if (!eq)
2584 buf[2] = cpu_to_le32(r->level[1].sens);
2585
2586 BUG_ON(items > ARRAY_SIZE(buf));
2587
2588 rc = put_entry(buf, sizeof(u32), items, fp);
2589 if (rc)
2590 return rc;
2591
2592 rc = ebitmap_write(&r->level[0].cat, fp);
2593 if (rc)
2594 return rc;
2595 if (!eq) {
2596 rc = ebitmap_write(&r->level[1].cat, fp);
2597 if (rc)
2598 return rc;
2599 }
2600
2601 return 0;
2602 }
2603
sens_write(void * vkey,void * datum,void * ptr)2604 static int sens_write(void *vkey, void *datum, void *ptr)
2605 {
2606 char *key = vkey;
2607 struct level_datum *levdatum = datum;
2608 struct policy_data *pd = ptr;
2609 void *fp = pd->fp;
2610 __le32 buf[2];
2611 size_t len;
2612 int rc;
2613
2614 len = strlen(key);
2615 buf[0] = cpu_to_le32(len);
2616 buf[1] = cpu_to_le32(levdatum->isalias);
2617 rc = put_entry(buf, sizeof(u32), 2, fp);
2618 if (rc)
2619 return rc;
2620
2621 rc = put_entry(key, 1, len, fp);
2622 if (rc)
2623 return rc;
2624
2625 rc = mls_write_level(levdatum->level, fp);
2626 if (rc)
2627 return rc;
2628
2629 return 0;
2630 }
2631
cat_write(void * vkey,void * datum,void * ptr)2632 static int cat_write(void *vkey, void *datum, void *ptr)
2633 {
2634 char *key = vkey;
2635 struct cat_datum *catdatum = datum;
2636 struct policy_data *pd = ptr;
2637 void *fp = pd->fp;
2638 __le32 buf[3];
2639 size_t len;
2640 int rc;
2641
2642 len = strlen(key);
2643 buf[0] = cpu_to_le32(len);
2644 buf[1] = cpu_to_le32(catdatum->value);
2645 buf[2] = cpu_to_le32(catdatum->isalias);
2646 rc = put_entry(buf, sizeof(u32), 3, fp);
2647 if (rc)
2648 return rc;
2649
2650 rc = put_entry(key, 1, len, fp);
2651 if (rc)
2652 return rc;
2653
2654 return 0;
2655 }
2656
role_trans_write(struct policydb * p,void * fp)2657 static int role_trans_write(struct policydb *p, void *fp)
2658 {
2659 struct role_trans *r = p->role_tr;
2660 struct role_trans *tr;
2661 u32 buf[3];
2662 size_t nel;
2663 int rc;
2664
2665 nel = 0;
2666 for (tr = r; tr; tr = tr->next)
2667 nel++;
2668 buf[0] = cpu_to_le32(nel);
2669 rc = put_entry(buf, sizeof(u32), 1, fp);
2670 if (rc)
2671 return rc;
2672 for (tr = r; tr; tr = tr->next) {
2673 buf[0] = cpu_to_le32(tr->role);
2674 buf[1] = cpu_to_le32(tr->type);
2675 buf[2] = cpu_to_le32(tr->new_role);
2676 rc = put_entry(buf, sizeof(u32), 3, fp);
2677 if (rc)
2678 return rc;
2679 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2680 buf[0] = cpu_to_le32(tr->tclass);
2681 rc = put_entry(buf, sizeof(u32), 1, fp);
2682 if (rc)
2683 return rc;
2684 }
2685 }
2686
2687 return 0;
2688 }
2689
role_allow_write(struct role_allow * r,void * fp)2690 static int role_allow_write(struct role_allow *r, void *fp)
2691 {
2692 struct role_allow *ra;
2693 u32 buf[2];
2694 size_t nel;
2695 int rc;
2696
2697 nel = 0;
2698 for (ra = r; ra; ra = ra->next)
2699 nel++;
2700 buf[0] = cpu_to_le32(nel);
2701 rc = put_entry(buf, sizeof(u32), 1, fp);
2702 if (rc)
2703 return rc;
2704 for (ra = r; ra; ra = ra->next) {
2705 buf[0] = cpu_to_le32(ra->role);
2706 buf[1] = cpu_to_le32(ra->new_role);
2707 rc = put_entry(buf, sizeof(u32), 2, fp);
2708 if (rc)
2709 return rc;
2710 }
2711 return 0;
2712 }
2713
2714 /*
2715 * Write a security context structure
2716 * to a policydb binary representation file.
2717 */
context_write(struct policydb * p,struct context * c,void * fp)2718 static int context_write(struct policydb *p, struct context *c,
2719 void *fp)
2720 {
2721 int rc;
2722 __le32 buf[3];
2723
2724 buf[0] = cpu_to_le32(c->user);
2725 buf[1] = cpu_to_le32(c->role);
2726 buf[2] = cpu_to_le32(c->type);
2727
2728 rc = put_entry(buf, sizeof(u32), 3, fp);
2729 if (rc)
2730 return rc;
2731
2732 rc = mls_write_range_helper(&c->range, fp);
2733 if (rc)
2734 return rc;
2735
2736 return 0;
2737 }
2738
2739 /*
2740 * The following *_write functions are used to
2741 * write the symbol data to a policy database
2742 * binary representation file.
2743 */
2744
perm_write(void * vkey,void * datum,void * fp)2745 static int perm_write(void *vkey, void *datum, void *fp)
2746 {
2747 char *key = vkey;
2748 struct perm_datum *perdatum = datum;
2749 __le32 buf[2];
2750 size_t len;
2751 int rc;
2752
2753 len = strlen(key);
2754 buf[0] = cpu_to_le32(len);
2755 buf[1] = cpu_to_le32(perdatum->value);
2756 rc = put_entry(buf, sizeof(u32), 2, fp);
2757 if (rc)
2758 return rc;
2759
2760 rc = put_entry(key, 1, len, fp);
2761 if (rc)
2762 return rc;
2763
2764 return 0;
2765 }
2766
common_write(void * vkey,void * datum,void * ptr)2767 static int common_write(void *vkey, void *datum, void *ptr)
2768 {
2769 char *key = vkey;
2770 struct common_datum *comdatum = datum;
2771 struct policy_data *pd = ptr;
2772 void *fp = pd->fp;
2773 __le32 buf[4];
2774 size_t len;
2775 int rc;
2776
2777 len = strlen(key);
2778 buf[0] = cpu_to_le32(len);
2779 buf[1] = cpu_to_le32(comdatum->value);
2780 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2781 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2782 rc = put_entry(buf, sizeof(u32), 4, fp);
2783 if (rc)
2784 return rc;
2785
2786 rc = put_entry(key, 1, len, fp);
2787 if (rc)
2788 return rc;
2789
2790 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2791 if (rc)
2792 return rc;
2793
2794 return 0;
2795 }
2796
type_set_write(struct type_set * t,void * fp)2797 static int type_set_write(struct type_set *t, void *fp)
2798 {
2799 int rc;
2800 __le32 buf[1];
2801
2802 if (ebitmap_write(&t->types, fp))
2803 return -EINVAL;
2804 if (ebitmap_write(&t->negset, fp))
2805 return -EINVAL;
2806
2807 buf[0] = cpu_to_le32(t->flags);
2808 rc = put_entry(buf, sizeof(u32), 1, fp);
2809 if (rc)
2810 return -EINVAL;
2811
2812 return 0;
2813 }
2814
write_cons_helper(struct policydb * p,struct constraint_node * node,void * fp)2815 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2816 void *fp)
2817 {
2818 struct constraint_node *c;
2819 struct constraint_expr *e;
2820 __le32 buf[3];
2821 u32 nel;
2822 int rc;
2823
2824 for (c = node; c; c = c->next) {
2825 nel = 0;
2826 for (e = c->expr; e; e = e->next)
2827 nel++;
2828 buf[0] = cpu_to_le32(c->permissions);
2829 buf[1] = cpu_to_le32(nel);
2830 rc = put_entry(buf, sizeof(u32), 2, fp);
2831 if (rc)
2832 return rc;
2833 for (e = c->expr; e; e = e->next) {
2834 buf[0] = cpu_to_le32(e->expr_type);
2835 buf[1] = cpu_to_le32(e->attr);
2836 buf[2] = cpu_to_le32(e->op);
2837 rc = put_entry(buf, sizeof(u32), 3, fp);
2838 if (rc)
2839 return rc;
2840
2841 switch (e->expr_type) {
2842 case CEXPR_NAMES:
2843 rc = ebitmap_write(&e->names, fp);
2844 if (rc)
2845 return rc;
2846 if (p->policyvers >=
2847 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2848 rc = type_set_write(e->type_names, fp);
2849 if (rc)
2850 return rc;
2851 }
2852 break;
2853 default:
2854 break;
2855 }
2856 }
2857 }
2858
2859 return 0;
2860 }
2861
class_write(void * vkey,void * datum,void * ptr)2862 static int class_write(void *vkey, void *datum, void *ptr)
2863 {
2864 char *key = vkey;
2865 struct class_datum *cladatum = datum;
2866 struct policy_data *pd = ptr;
2867 void *fp = pd->fp;
2868 struct policydb *p = pd->p;
2869 struct constraint_node *c;
2870 __le32 buf[6];
2871 u32 ncons;
2872 size_t len, len2;
2873 int rc;
2874
2875 len = strlen(key);
2876 if (cladatum->comkey)
2877 len2 = strlen(cladatum->comkey);
2878 else
2879 len2 = 0;
2880
2881 ncons = 0;
2882 for (c = cladatum->constraints; c; c = c->next)
2883 ncons++;
2884
2885 buf[0] = cpu_to_le32(len);
2886 buf[1] = cpu_to_le32(len2);
2887 buf[2] = cpu_to_le32(cladatum->value);
2888 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2889 if (cladatum->permissions.table)
2890 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2891 else
2892 buf[4] = 0;
2893 buf[5] = cpu_to_le32(ncons);
2894 rc = put_entry(buf, sizeof(u32), 6, fp);
2895 if (rc)
2896 return rc;
2897
2898 rc = put_entry(key, 1, len, fp);
2899 if (rc)
2900 return rc;
2901
2902 if (cladatum->comkey) {
2903 rc = put_entry(cladatum->comkey, 1, len2, fp);
2904 if (rc)
2905 return rc;
2906 }
2907
2908 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2909 if (rc)
2910 return rc;
2911
2912 rc = write_cons_helper(p, cladatum->constraints, fp);
2913 if (rc)
2914 return rc;
2915
2916 /* write out the validatetrans rule */
2917 ncons = 0;
2918 for (c = cladatum->validatetrans; c; c = c->next)
2919 ncons++;
2920
2921 buf[0] = cpu_to_le32(ncons);
2922 rc = put_entry(buf, sizeof(u32), 1, fp);
2923 if (rc)
2924 return rc;
2925
2926 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2927 if (rc)
2928 return rc;
2929
2930 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2931 buf[0] = cpu_to_le32(cladatum->default_user);
2932 buf[1] = cpu_to_le32(cladatum->default_role);
2933 buf[2] = cpu_to_le32(cladatum->default_range);
2934
2935 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2936 if (rc)
2937 return rc;
2938 }
2939
2940 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2941 buf[0] = cpu_to_le32(cladatum->default_type);
2942 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2943 if (rc)
2944 return rc;
2945 }
2946
2947 return 0;
2948 }
2949
role_write(void * vkey,void * datum,void * ptr)2950 static int role_write(void *vkey, void *datum, void *ptr)
2951 {
2952 char *key = vkey;
2953 struct role_datum *role = datum;
2954 struct policy_data *pd = ptr;
2955 void *fp = pd->fp;
2956 struct policydb *p = pd->p;
2957 __le32 buf[3];
2958 size_t items, len;
2959 int rc;
2960
2961 len = strlen(key);
2962 items = 0;
2963 buf[items++] = cpu_to_le32(len);
2964 buf[items++] = cpu_to_le32(role->value);
2965 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2966 buf[items++] = cpu_to_le32(role->bounds);
2967
2968 BUG_ON(items > ARRAY_SIZE(buf));
2969
2970 rc = put_entry(buf, sizeof(u32), items, fp);
2971 if (rc)
2972 return rc;
2973
2974 rc = put_entry(key, 1, len, fp);
2975 if (rc)
2976 return rc;
2977
2978 rc = ebitmap_write(&role->dominates, fp);
2979 if (rc)
2980 return rc;
2981
2982 rc = ebitmap_write(&role->types, fp);
2983 if (rc)
2984 return rc;
2985
2986 return 0;
2987 }
2988
type_write(void * vkey,void * datum,void * ptr)2989 static int type_write(void *vkey, void *datum, void *ptr)
2990 {
2991 char *key = vkey;
2992 struct type_datum *typdatum = datum;
2993 struct policy_data *pd = ptr;
2994 struct policydb *p = pd->p;
2995 void *fp = pd->fp;
2996 __le32 buf[4];
2997 int rc;
2998 size_t items, len;
2999
3000 len = strlen(key);
3001 items = 0;
3002 buf[items++] = cpu_to_le32(len);
3003 buf[items++] = cpu_to_le32(typdatum->value);
3004 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3005 u32 properties = 0;
3006
3007 if (typdatum->primary)
3008 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3009
3010 if (typdatum->attribute)
3011 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3012
3013 buf[items++] = cpu_to_le32(properties);
3014 buf[items++] = cpu_to_le32(typdatum->bounds);
3015 } else {
3016 buf[items++] = cpu_to_le32(typdatum->primary);
3017 }
3018 BUG_ON(items > ARRAY_SIZE(buf));
3019 rc = put_entry(buf, sizeof(u32), items, fp);
3020 if (rc)
3021 return rc;
3022
3023 rc = put_entry(key, 1, len, fp);
3024 if (rc)
3025 return rc;
3026
3027 return 0;
3028 }
3029
user_write(void * vkey,void * datum,void * ptr)3030 static int user_write(void *vkey, void *datum, void *ptr)
3031 {
3032 char *key = vkey;
3033 struct user_datum *usrdatum = datum;
3034 struct policy_data *pd = ptr;
3035 struct policydb *p = pd->p;
3036 void *fp = pd->fp;
3037 __le32 buf[3];
3038 size_t items, len;
3039 int rc;
3040
3041 len = strlen(key);
3042 items = 0;
3043 buf[items++] = cpu_to_le32(len);
3044 buf[items++] = cpu_to_le32(usrdatum->value);
3045 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3046 buf[items++] = cpu_to_le32(usrdatum->bounds);
3047 BUG_ON(items > ARRAY_SIZE(buf));
3048 rc = put_entry(buf, sizeof(u32), items, fp);
3049 if (rc)
3050 return rc;
3051
3052 rc = put_entry(key, 1, len, fp);
3053 if (rc)
3054 return rc;
3055
3056 rc = ebitmap_write(&usrdatum->roles, fp);
3057 if (rc)
3058 return rc;
3059
3060 rc = mls_write_range_helper(&usrdatum->range, fp);
3061 if (rc)
3062 return rc;
3063
3064 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3065 if (rc)
3066 return rc;
3067
3068 return 0;
3069 }
3070
3071 static int (*write_f[SYM_NUM]) (void *key, void *datum,
3072 void *datap) =
3073 {
3074 common_write,
3075 class_write,
3076 role_write,
3077 type_write,
3078 user_write,
3079 cond_write_bool,
3080 sens_write,
3081 cat_write,
3082 };
3083
ocontext_write(struct policydb * p,struct policydb_compat_info * info,void * fp)3084 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3085 void *fp)
3086 {
3087 unsigned int i, j, rc;
3088 size_t nel, len;
3089 __be64 prefixbuf[1];
3090 __le32 buf[3];
3091 u32 nodebuf[8];
3092 struct ocontext *c;
3093 for (i = 0; i < info->ocon_num; i++) {
3094 nel = 0;
3095 for (c = p->ocontexts[i]; c; c = c->next)
3096 nel++;
3097 buf[0] = cpu_to_le32(nel);
3098 rc = put_entry(buf, sizeof(u32), 1, fp);
3099 if (rc)
3100 return rc;
3101 for (c = p->ocontexts[i]; c; c = c->next) {
3102 switch (i) {
3103 case OCON_ISID:
3104 buf[0] = cpu_to_le32(c->sid[0]);
3105 rc = put_entry(buf, sizeof(u32), 1, fp);
3106 if (rc)
3107 return rc;
3108 rc = context_write(p, &c->context[0], fp);
3109 if (rc)
3110 return rc;
3111 break;
3112 case OCON_FS:
3113 case OCON_NETIF:
3114 len = strlen(c->u.name);
3115 buf[0] = cpu_to_le32(len);
3116 rc = put_entry(buf, sizeof(u32), 1, fp);
3117 if (rc)
3118 return rc;
3119 rc = put_entry(c->u.name, 1, len, fp);
3120 if (rc)
3121 return rc;
3122 rc = context_write(p, &c->context[0], fp);
3123 if (rc)
3124 return rc;
3125 rc = context_write(p, &c->context[1], fp);
3126 if (rc)
3127 return rc;
3128 break;
3129 case OCON_PORT:
3130 buf[0] = cpu_to_le32(c->u.port.protocol);
3131 buf[1] = cpu_to_le32(c->u.port.low_port);
3132 buf[2] = cpu_to_le32(c->u.port.high_port);
3133 rc = put_entry(buf, sizeof(u32), 3, fp);
3134 if (rc)
3135 return rc;
3136 rc = context_write(p, &c->context[0], fp);
3137 if (rc)
3138 return rc;
3139 break;
3140 case OCON_NODE:
3141 nodebuf[0] = c->u.node.addr; /* network order */
3142 nodebuf[1] = c->u.node.mask; /* network order */
3143 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3144 if (rc)
3145 return rc;
3146 rc = context_write(p, &c->context[0], fp);
3147 if (rc)
3148 return rc;
3149 break;
3150 case OCON_FSUSE:
3151 buf[0] = cpu_to_le32(c->v.behavior);
3152 len = strlen(c->u.name);
3153 buf[1] = cpu_to_le32(len);
3154 rc = put_entry(buf, sizeof(u32), 2, fp);
3155 if (rc)
3156 return rc;
3157 rc = put_entry(c->u.name, 1, len, fp);
3158 if (rc)
3159 return rc;
3160 rc = context_write(p, &c->context[0], fp);
3161 if (rc)
3162 return rc;
3163 break;
3164 case OCON_NODE6:
3165 for (j = 0; j < 4; j++)
3166 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3167 for (j = 0; j < 4; j++)
3168 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3169 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3170 if (rc)
3171 return rc;
3172 rc = context_write(p, &c->context[0], fp);
3173 if (rc)
3174 return rc;
3175 break;
3176 case OCON_IBPKEY:
3177 /* subnet_prefix is in CPU order */
3178 prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
3179
3180 rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
3181 if (rc)
3182 return rc;
3183
3184 buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
3185 buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
3186
3187 rc = put_entry(buf, sizeof(u32), 2, fp);
3188 if (rc)
3189 return rc;
3190 rc = context_write(p, &c->context[0], fp);
3191 if (rc)
3192 return rc;
3193 break;
3194 case OCON_IBENDPORT:
3195 len = strlen(c->u.ibendport.dev_name);
3196 buf[0] = cpu_to_le32(len);
3197 buf[1] = cpu_to_le32(c->u.ibendport.port);
3198 rc = put_entry(buf, sizeof(u32), 2, fp);
3199 if (rc)
3200 return rc;
3201 rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
3202 if (rc)
3203 return rc;
3204 rc = context_write(p, &c->context[0], fp);
3205 if (rc)
3206 return rc;
3207 break;
3208 }
3209 }
3210 }
3211 return 0;
3212 }
3213
genfs_write(struct policydb * p,void * fp)3214 static int genfs_write(struct policydb *p, void *fp)
3215 {
3216 struct genfs *genfs;
3217 struct ocontext *c;
3218 size_t len;
3219 __le32 buf[1];
3220 int rc;
3221
3222 len = 0;
3223 for (genfs = p->genfs; genfs; genfs = genfs->next)
3224 len++;
3225 buf[0] = cpu_to_le32(len);
3226 rc = put_entry(buf, sizeof(u32), 1, fp);
3227 if (rc)
3228 return rc;
3229 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3230 len = strlen(genfs->fstype);
3231 buf[0] = cpu_to_le32(len);
3232 rc = put_entry(buf, sizeof(u32), 1, fp);
3233 if (rc)
3234 return rc;
3235 rc = put_entry(genfs->fstype, 1, len, fp);
3236 if (rc)
3237 return rc;
3238 len = 0;
3239 for (c = genfs->head; c; c = c->next)
3240 len++;
3241 buf[0] = cpu_to_le32(len);
3242 rc = put_entry(buf, sizeof(u32), 1, fp);
3243 if (rc)
3244 return rc;
3245 for (c = genfs->head; c; c = c->next) {
3246 len = strlen(c->u.name);
3247 buf[0] = cpu_to_le32(len);
3248 rc = put_entry(buf, sizeof(u32), 1, fp);
3249 if (rc)
3250 return rc;
3251 rc = put_entry(c->u.name, 1, len, fp);
3252 if (rc)
3253 return rc;
3254 buf[0] = cpu_to_le32(c->v.sclass);
3255 rc = put_entry(buf, sizeof(u32), 1, fp);
3256 if (rc)
3257 return rc;
3258 rc = context_write(p, &c->context[0], fp);
3259 if (rc)
3260 return rc;
3261 }
3262 }
3263 return 0;
3264 }
3265
hashtab_cnt(void * key,void * data,void * ptr)3266 static int hashtab_cnt(void *key, void *data, void *ptr)
3267 {
3268 int *cnt = ptr;
3269 *cnt = *cnt + 1;
3270
3271 return 0;
3272 }
3273
range_write_helper(void * key,void * data,void * ptr)3274 static int range_write_helper(void *key, void *data, void *ptr)
3275 {
3276 __le32 buf[2];
3277 struct range_trans *rt = key;
3278 struct mls_range *r = data;
3279 struct policy_data *pd = ptr;
3280 void *fp = pd->fp;
3281 struct policydb *p = pd->p;
3282 int rc;
3283
3284 buf[0] = cpu_to_le32(rt->source_type);
3285 buf[1] = cpu_to_le32(rt->target_type);
3286 rc = put_entry(buf, sizeof(u32), 2, fp);
3287 if (rc)
3288 return rc;
3289 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3290 buf[0] = cpu_to_le32(rt->target_class);
3291 rc = put_entry(buf, sizeof(u32), 1, fp);
3292 if (rc)
3293 return rc;
3294 }
3295 rc = mls_write_range_helper(r, fp);
3296 if (rc)
3297 return rc;
3298
3299 return 0;
3300 }
3301
range_write(struct policydb * p,void * fp)3302 static int range_write(struct policydb *p, void *fp)
3303 {
3304 __le32 buf[1];
3305 int rc, nel;
3306 struct policy_data pd;
3307
3308 pd.p = p;
3309 pd.fp = fp;
3310
3311 /* count the number of entries in the hashtab */
3312 nel = 0;
3313 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
3314 if (rc)
3315 return rc;
3316
3317 buf[0] = cpu_to_le32(nel);
3318 rc = put_entry(buf, sizeof(u32), 1, fp);
3319 if (rc)
3320 return rc;
3321
3322 /* actually write all of the entries */
3323 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3324 if (rc)
3325 return rc;
3326
3327 return 0;
3328 }
3329
filename_write_helper(void * key,void * data,void * ptr)3330 static int filename_write_helper(void *key, void *data, void *ptr)
3331 {
3332 __le32 buf[4];
3333 struct filename_trans *ft = key;
3334 struct filename_trans_datum *otype = data;
3335 void *fp = ptr;
3336 int rc;
3337 u32 len;
3338
3339 len = strlen(ft->name);
3340 buf[0] = cpu_to_le32(len);
3341 rc = put_entry(buf, sizeof(u32), 1, fp);
3342 if (rc)
3343 return rc;
3344
3345 rc = put_entry(ft->name, sizeof(char), len, fp);
3346 if (rc)
3347 return rc;
3348
3349 buf[0] = cpu_to_le32(ft->stype);
3350 buf[1] = cpu_to_le32(ft->ttype);
3351 buf[2] = cpu_to_le32(ft->tclass);
3352 buf[3] = cpu_to_le32(otype->otype);
3353
3354 rc = put_entry(buf, sizeof(u32), 4, fp);
3355 if (rc)
3356 return rc;
3357
3358 return 0;
3359 }
3360
filename_trans_write(struct policydb * p,void * fp)3361 static int filename_trans_write(struct policydb *p, void *fp)
3362 {
3363 u32 nel;
3364 __le32 buf[1];
3365 int rc;
3366
3367 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3368 return 0;
3369
3370 nel = 0;
3371 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3372 if (rc)
3373 return rc;
3374
3375 buf[0] = cpu_to_le32(nel);
3376 rc = put_entry(buf, sizeof(u32), 1, fp);
3377 if (rc)
3378 return rc;
3379
3380 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3381 if (rc)
3382 return rc;
3383
3384 return 0;
3385 }
3386
3387 /*
3388 * Write the configuration data in a policy database
3389 * structure to a policy database binary representation
3390 * file.
3391 */
policydb_write(struct policydb * p,void * fp)3392 int policydb_write(struct policydb *p, void *fp)
3393 {
3394 unsigned int i, num_syms;
3395 int rc;
3396 __le32 buf[4];
3397 u32 config;
3398 size_t len;
3399 struct policydb_compat_info *info;
3400
3401 /*
3402 * refuse to write policy older than compressed avtab
3403 * to simplify the writer. There are other tests dropped
3404 * since we assume this throughout the writer code. Be
3405 * careful if you ever try to remove this restriction
3406 */
3407 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3408 pr_err("SELinux: refusing to write policy version %d."
3409 " Because it is less than version %d\n", p->policyvers,
3410 POLICYDB_VERSION_AVTAB);
3411 return -EINVAL;
3412 }
3413
3414 config = 0;
3415 if (p->mls_enabled)
3416 config |= POLICYDB_CONFIG_MLS;
3417
3418 if (p->reject_unknown)
3419 config |= REJECT_UNKNOWN;
3420 if (p->allow_unknown)
3421 config |= ALLOW_UNKNOWN;
3422
3423 /* Write the magic number and string identifiers. */
3424 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3425 len = strlen(POLICYDB_STRING);
3426 buf[1] = cpu_to_le32(len);
3427 rc = put_entry(buf, sizeof(u32), 2, fp);
3428 if (rc)
3429 return rc;
3430 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3431 if (rc)
3432 return rc;
3433
3434 /* Write the version, config, and table sizes. */
3435 info = policydb_lookup_compat(p->policyvers);
3436 if (!info) {
3437 pr_err("SELinux: compatibility lookup failed for policy "
3438 "version %d", p->policyvers);
3439 return -EINVAL;
3440 }
3441
3442 buf[0] = cpu_to_le32(p->policyvers);
3443 buf[1] = cpu_to_le32(config);
3444 buf[2] = cpu_to_le32(info->sym_num);
3445 buf[3] = cpu_to_le32(info->ocon_num);
3446
3447 rc = put_entry(buf, sizeof(u32), 4, fp);
3448 if (rc)
3449 return rc;
3450
3451 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3452 rc = ebitmap_write(&p->policycaps, fp);
3453 if (rc)
3454 return rc;
3455 }
3456
3457 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3458 rc = ebitmap_write(&p->permissive_map, fp);
3459 if (rc)
3460 return rc;
3461 }
3462
3463 num_syms = info->sym_num;
3464 for (i = 0; i < num_syms; i++) {
3465 struct policy_data pd;
3466
3467 pd.fp = fp;
3468 pd.p = p;
3469
3470 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3471 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3472
3473 rc = put_entry(buf, sizeof(u32), 2, fp);
3474 if (rc)
3475 return rc;
3476 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3477 if (rc)
3478 return rc;
3479 }
3480
3481 rc = avtab_write(p, &p->te_avtab, fp);
3482 if (rc)
3483 return rc;
3484
3485 rc = cond_write_list(p, p->cond_list, fp);
3486 if (rc)
3487 return rc;
3488
3489 rc = role_trans_write(p, fp);
3490 if (rc)
3491 return rc;
3492
3493 rc = role_allow_write(p->role_allow, fp);
3494 if (rc)
3495 return rc;
3496
3497 rc = filename_trans_write(p, fp);
3498 if (rc)
3499 return rc;
3500
3501 rc = ocontext_write(p, info, fp);
3502 if (rc)
3503 return rc;
3504
3505 rc = genfs_write(p, fp);
3506 if (rc)
3507 return rc;
3508
3509 rc = range_write(p, fp);
3510 if (rc)
3511 return rc;
3512
3513 for (i = 0; i < p->p_types.nprim; i++) {
3514 struct ebitmap *e = &p->type_attr_map_array[i];
3515
3516 rc = ebitmap_write(e, fp);
3517 if (rc)
3518 return rc;
3519 }
3520
3521 return 0;
3522 }
3523