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