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