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