SELinux: Use dentry name in new object labeling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / selinux / ss / policydb.c
blob159c818067602ef2bcb123fc1ddb143a0eb85742
1 /*
2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
7 /*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
10 * Support for enhanced MLS infrastructure.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
18 * Added support for the policy capability bitmap
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.
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"
37 #include "policydb.h"
38 #include "conditional.h"
39 #include "mls.h"
40 #include "services.h"
42 #define _DEBUG_HASHES
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",
55 #endif
57 static unsigned int symtab_sizes[SYM_NUM] = {
59 32,
60 16,
61 512,
62 128,
63 16,
64 16,
65 16,
68 struct policydb_compat_info {
69 int version;
70 int sym_num;
71 int ocon_num;
74 /* These need to be updated if SYM_NUM or OCON_NUM changes */
75 static struct policydb_compat_info policydb_compat[] = {
77 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
82 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
87 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM,
92 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM,
97 .version = POLICYDB_VERSION_MLS,
98 .sym_num = SYM_NUM,
99 .ocon_num = OCON_NUM,
102 .version = POLICYDB_VERSION_AVTAB,
103 .sym_num = SYM_NUM,
104 .ocon_num = OCON_NUM,
107 .version = POLICYDB_VERSION_RANGETRANS,
108 .sym_num = SYM_NUM,
109 .ocon_num = OCON_NUM,
112 .version = POLICYDB_VERSION_POLCAP,
113 .sym_num = SYM_NUM,
114 .ocon_num = OCON_NUM,
117 .version = POLICYDB_VERSION_PERMISSIVE,
118 .sym_num = SYM_NUM,
119 .ocon_num = OCON_NUM,
122 .version = POLICYDB_VERSION_BOUNDARY,
123 .sym_num = SYM_NUM,
124 .ocon_num = OCON_NUM,
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
128 .sym_num = SYM_NUM,
129 .ocon_num = OCON_NUM,
133 static struct policydb_compat_info *policydb_lookup_compat(int version)
135 int i;
136 struct policydb_compat_info *info = NULL;
138 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
139 if (policydb_compat[i].version == version) {
140 info = &policydb_compat[i];
141 break;
144 return info;
148 * Initialize the role table.
150 static int roles_init(struct policydb *p)
152 char *key = NULL;
153 int rc;
154 struct role_datum *role;
156 rc = -ENOMEM;
157 role = kzalloc(sizeof(*role), GFP_KERNEL);
158 if (!role)
159 goto out;
161 rc = -EINVAL;
162 role->value = ++p->p_roles.nprim;
163 if (role->value != OBJECT_R_VAL)
164 goto out;
166 rc = -ENOMEM;
167 key = kstrdup(OBJECT_R, GFP_KERNEL);
168 if (!key)
169 goto out;
171 rc = hashtab_insert(p->p_roles.table, key, role);
172 if (rc)
173 goto out;
175 return 0;
176 out:
177 kfree(key);
178 kfree(role);
179 return rc;
182 static u32 rangetr_hash(struct hashtab *h, const void *k)
184 const struct range_trans *key = k;
185 return (key->source_type + (key->target_type << 3) +
186 (key->target_class << 5)) & (h->size - 1);
189 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
191 const struct range_trans *key1 = k1, *key2 = k2;
192 int v;
194 v = key1->source_type - key2->source_type;
195 if (v)
196 return v;
198 v = key1->target_type - key2->target_type;
199 if (v)
200 return v;
202 v = key1->target_class - key2->target_class;
204 return v;
208 * Initialize a policy database structure.
210 static int policydb_init(struct policydb *p)
212 int i, rc;
214 memset(p, 0, sizeof(*p));
216 for (i = 0; i < SYM_NUM; i++) {
217 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
218 if (rc)
219 goto out;
222 rc = avtab_init(&p->te_avtab);
223 if (rc)
224 goto out;
226 rc = roles_init(p);
227 if (rc)
228 goto out;
230 rc = cond_policydb_init(p);
231 if (rc)
232 goto out;
234 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
235 if (!p->range_tr)
236 goto out;
238 ebitmap_init(&p->policycaps);
239 ebitmap_init(&p->permissive_map);
241 return 0;
242 out:
243 for (i = 0; i < SYM_NUM; i++)
244 hashtab_destroy(p->symtab[i].table);
245 return rc;
249 * The following *_index functions are used to
250 * define the val_to_name and val_to_struct arrays
251 * in a policy database structure. The val_to_name
252 * arrays are used when converting security context
253 * structures into string representations. The
254 * val_to_struct arrays are used when the attributes
255 * of a class, role, or user are needed.
258 static int common_index(void *key, void *datum, void *datap)
260 struct policydb *p;
261 struct common_datum *comdatum;
262 struct flex_array *fa;
264 comdatum = datum;
265 p = datap;
266 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
267 return -EINVAL;
269 fa = p->sym_val_to_name[SYM_COMMONS];
270 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
271 GFP_KERNEL | __GFP_ZERO))
272 BUG();
273 return 0;
276 static int class_index(void *key, void *datum, void *datap)
278 struct policydb *p;
279 struct class_datum *cladatum;
280 struct flex_array *fa;
282 cladatum = datum;
283 p = datap;
284 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
285 return -EINVAL;
286 fa = p->sym_val_to_name[SYM_CLASSES];
287 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
288 GFP_KERNEL | __GFP_ZERO))
289 BUG();
290 p->class_val_to_struct[cladatum->value - 1] = cladatum;
291 return 0;
294 static int role_index(void *key, void *datum, void *datap)
296 struct policydb *p;
297 struct role_datum *role;
298 struct flex_array *fa;
300 role = datum;
301 p = datap;
302 if (!role->value
303 || role->value > p->p_roles.nprim
304 || role->bounds > p->p_roles.nprim)
305 return -EINVAL;
307 fa = p->sym_val_to_name[SYM_ROLES];
308 if (flex_array_put_ptr(fa, role->value - 1, key,
309 GFP_KERNEL | __GFP_ZERO))
310 BUG();
311 p->role_val_to_struct[role->value - 1] = role;
312 return 0;
315 static int type_index(void *key, void *datum, void *datap)
317 struct policydb *p;
318 struct type_datum *typdatum;
319 struct flex_array *fa;
321 typdatum = datum;
322 p = datap;
324 if (typdatum->primary) {
325 if (!typdatum->value
326 || typdatum->value > p->p_types.nprim
327 || typdatum->bounds > p->p_types.nprim)
328 return -EINVAL;
329 fa = p->sym_val_to_name[SYM_TYPES];
330 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
331 GFP_KERNEL | __GFP_ZERO))
332 BUG();
334 fa = p->type_val_to_struct_array;
335 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
336 GFP_KERNEL | __GFP_ZERO))
337 BUG();
340 return 0;
343 static int user_index(void *key, void *datum, void *datap)
345 struct policydb *p;
346 struct user_datum *usrdatum;
347 struct flex_array *fa;
349 usrdatum = datum;
350 p = datap;
351 if (!usrdatum->value
352 || usrdatum->value > p->p_users.nprim
353 || usrdatum->bounds > p->p_users.nprim)
354 return -EINVAL;
356 fa = p->sym_val_to_name[SYM_USERS];
357 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
358 GFP_KERNEL | __GFP_ZERO))
359 BUG();
360 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
361 return 0;
364 static int sens_index(void *key, void *datum, void *datap)
366 struct policydb *p;
367 struct level_datum *levdatum;
368 struct flex_array *fa;
370 levdatum = datum;
371 p = datap;
373 if (!levdatum->isalias) {
374 if (!levdatum->level->sens ||
375 levdatum->level->sens > p->p_levels.nprim)
376 return -EINVAL;
377 fa = p->sym_val_to_name[SYM_LEVELS];
378 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
379 GFP_KERNEL | __GFP_ZERO))
380 BUG();
383 return 0;
386 static int cat_index(void *key, void *datum, void *datap)
388 struct policydb *p;
389 struct cat_datum *catdatum;
390 struct flex_array *fa;
392 catdatum = datum;
393 p = datap;
395 if (!catdatum->isalias) {
396 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
397 return -EINVAL;
398 fa = p->sym_val_to_name[SYM_CATS];
399 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
400 GFP_KERNEL | __GFP_ZERO))
401 BUG();
404 return 0;
407 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
409 common_index,
410 class_index,
411 role_index,
412 type_index,
413 user_index,
414 cond_index_bool,
415 sens_index,
416 cat_index,
419 #ifdef DEBUG_HASHES
420 static void symtab_hash_eval(struct symtab *s)
422 int i;
424 for (i = 0; i < SYM_NUM; i++) {
425 struct hashtab *h = s[i].table;
426 struct hashtab_info info;
428 hashtab_stat(h, &info);
429 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
430 "longest chain length %d\n", symtab_name[i], h->nel,
431 info.slots_used, h->size, info.max_chain_len);
435 static void rangetr_hash_eval(struct hashtab *h)
437 struct hashtab_info info;
439 hashtab_stat(h, &info);
440 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
441 "longest chain length %d\n", h->nel,
442 info.slots_used, h->size, info.max_chain_len);
444 #else
445 static inline void rangetr_hash_eval(struct hashtab *h)
448 #endif
451 * Define the other val_to_name and val_to_struct arrays
452 * in a policy database structure.
454 * Caller must clean up on failure.
456 static int policydb_index(struct policydb *p)
458 int i, rc;
460 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
461 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
462 if (p->mls_enabled)
463 printk(", %d sens, %d cats", p->p_levels.nprim,
464 p->p_cats.nprim);
465 printk("\n");
467 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
468 p->p_classes.nprim, p->te_avtab.nel);
470 #ifdef DEBUG_HASHES
471 avtab_hash_eval(&p->te_avtab, "rules");
472 symtab_hash_eval(p->symtab);
473 #endif
475 rc = -ENOMEM;
476 p->class_val_to_struct =
477 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
478 GFP_KERNEL);
479 if (!p->class_val_to_struct)
480 goto out;
482 rc = -ENOMEM;
483 p->role_val_to_struct =
484 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
485 GFP_KERNEL);
486 if (!p->role_val_to_struct)
487 goto out;
489 rc = -ENOMEM;
490 p->user_val_to_struct =
491 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
492 GFP_KERNEL);
493 if (!p->user_val_to_struct)
494 goto out;
496 /* Yes, I want the sizeof the pointer, not the structure */
497 rc = -ENOMEM;
498 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
499 p->p_types.nprim,
500 GFP_KERNEL | __GFP_ZERO);
501 if (!p->type_val_to_struct_array)
502 goto out;
504 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
505 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
506 if (rc)
507 goto out;
509 rc = -ENOMEM;
510 if (cond_init_bool_indexes(p))
511 goto out;
513 for (i = 0; i < SYM_NUM; i++) {
514 rc = -ENOMEM;
515 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
516 p->symtab[i].nprim,
517 GFP_KERNEL | __GFP_ZERO);
518 if (!p->sym_val_to_name[i])
519 goto out;
521 rc = flex_array_prealloc(p->sym_val_to_name[i],
522 0, p->symtab[i].nprim - 1,
523 GFP_KERNEL | __GFP_ZERO);
524 if (rc)
525 goto out;
527 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
528 if (rc)
529 goto out;
531 rc = 0;
532 out:
533 return rc;
537 * The following *_destroy functions are used to
538 * free any memory allocated for each kind of
539 * symbol data in the policy database.
542 static int perm_destroy(void *key, void *datum, void *p)
544 kfree(key);
545 kfree(datum);
546 return 0;
549 static int common_destroy(void *key, void *datum, void *p)
551 struct common_datum *comdatum;
553 kfree(key);
554 if (datum) {
555 comdatum = datum;
556 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
557 hashtab_destroy(comdatum->permissions.table);
559 kfree(datum);
560 return 0;
563 static int cls_destroy(void *key, void *datum, void *p)
565 struct class_datum *cladatum;
566 struct constraint_node *constraint, *ctemp;
567 struct constraint_expr *e, *etmp;
569 kfree(key);
570 if (datum) {
571 cladatum = datum;
572 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
573 hashtab_destroy(cladatum->permissions.table);
574 constraint = cladatum->constraints;
575 while (constraint) {
576 e = constraint->expr;
577 while (e) {
578 ebitmap_destroy(&e->names);
579 etmp = e;
580 e = e->next;
581 kfree(etmp);
583 ctemp = constraint;
584 constraint = constraint->next;
585 kfree(ctemp);
588 constraint = cladatum->validatetrans;
589 while (constraint) {
590 e = constraint->expr;
591 while (e) {
592 ebitmap_destroy(&e->names);
593 etmp = e;
594 e = e->next;
595 kfree(etmp);
597 ctemp = constraint;
598 constraint = constraint->next;
599 kfree(ctemp);
602 kfree(cladatum->comkey);
604 kfree(datum);
605 return 0;
608 static int role_destroy(void *key, void *datum, void *p)
610 struct role_datum *role;
612 kfree(key);
613 if (datum) {
614 role = datum;
615 ebitmap_destroy(&role->dominates);
616 ebitmap_destroy(&role->types);
618 kfree(datum);
619 return 0;
622 static int type_destroy(void *key, void *datum, void *p)
624 kfree(key);
625 kfree(datum);
626 return 0;
629 static int user_destroy(void *key, void *datum, void *p)
631 struct user_datum *usrdatum;
633 kfree(key);
634 if (datum) {
635 usrdatum = datum;
636 ebitmap_destroy(&usrdatum->roles);
637 ebitmap_destroy(&usrdatum->range.level[0].cat);
638 ebitmap_destroy(&usrdatum->range.level[1].cat);
639 ebitmap_destroy(&usrdatum->dfltlevel.cat);
641 kfree(datum);
642 return 0;
645 static int sens_destroy(void *key, void *datum, void *p)
647 struct level_datum *levdatum;
649 kfree(key);
650 if (datum) {
651 levdatum = datum;
652 ebitmap_destroy(&levdatum->level->cat);
653 kfree(levdatum->level);
655 kfree(datum);
656 return 0;
659 static int cat_destroy(void *key, void *datum, void *p)
661 kfree(key);
662 kfree(datum);
663 return 0;
666 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
668 common_destroy,
669 cls_destroy,
670 role_destroy,
671 type_destroy,
672 user_destroy,
673 cond_destroy_bool,
674 sens_destroy,
675 cat_destroy,
678 static int range_tr_destroy(void *key, void *datum, void *p)
680 struct mls_range *rt = datum;
681 kfree(key);
682 ebitmap_destroy(&rt->level[0].cat);
683 ebitmap_destroy(&rt->level[1].cat);
684 kfree(datum);
685 cond_resched();
686 return 0;
689 static void ocontext_destroy(struct ocontext *c, int i)
691 if (!c)
692 return;
694 context_destroy(&c->context[0]);
695 context_destroy(&c->context[1]);
696 if (i == OCON_ISID || i == OCON_FS ||
697 i == OCON_NETIF || i == OCON_FSUSE)
698 kfree(c->u.name);
699 kfree(c);
703 * Free any memory allocated by a policy database structure.
705 void policydb_destroy(struct policydb *p)
707 struct ocontext *c, *ctmp;
708 struct genfs *g, *gtmp;
709 int i;
710 struct role_allow *ra, *lra = NULL;
711 struct role_trans *tr, *ltr = NULL;
712 struct filename_trans *ft, *nft;
714 for (i = 0; i < SYM_NUM; i++) {
715 cond_resched();
716 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
717 hashtab_destroy(p->symtab[i].table);
720 for (i = 0; i < SYM_NUM; i++) {
721 if (p->sym_val_to_name[i])
722 flex_array_free(p->sym_val_to_name[i]);
725 kfree(p->class_val_to_struct);
726 kfree(p->role_val_to_struct);
727 kfree(p->user_val_to_struct);
728 if (p->type_val_to_struct_array)
729 flex_array_free(p->type_val_to_struct_array);
731 avtab_destroy(&p->te_avtab);
733 for (i = 0; i < OCON_NUM; i++) {
734 cond_resched();
735 c = p->ocontexts[i];
736 while (c) {
737 ctmp = c;
738 c = c->next;
739 ocontext_destroy(ctmp, i);
741 p->ocontexts[i] = NULL;
744 g = p->genfs;
745 while (g) {
746 cond_resched();
747 kfree(g->fstype);
748 c = g->head;
749 while (c) {
750 ctmp = c;
751 c = c->next;
752 ocontext_destroy(ctmp, OCON_FSUSE);
754 gtmp = g;
755 g = g->next;
756 kfree(gtmp);
758 p->genfs = NULL;
760 cond_policydb_destroy(p);
762 for (tr = p->role_tr; tr; tr = tr->next) {
763 cond_resched();
764 kfree(ltr);
765 ltr = tr;
767 kfree(ltr);
769 for (ra = p->role_allow; ra; ra = ra->next) {
770 cond_resched();
771 kfree(lra);
772 lra = ra;
774 kfree(lra);
776 hashtab_map(p->range_tr, range_tr_destroy, NULL);
777 hashtab_destroy(p->range_tr);
779 if (p->type_attr_map_array) {
780 for (i = 0; i < p->p_types.nprim; i++) {
781 struct ebitmap *e;
783 e = flex_array_get(p->type_attr_map_array, i);
784 if (!e)
785 continue;
786 ebitmap_destroy(e);
788 flex_array_free(p->type_attr_map_array);
791 ft = p->filename_trans;
792 while (ft) {
793 nft = ft->next;
794 kfree(ft->name);
795 kfree(ft);
796 ft = nft;
799 ebitmap_destroy(&p->policycaps);
800 ebitmap_destroy(&p->permissive_map);
802 return;
806 * Load the initial SIDs specified in a policy database
807 * structure into a SID table.
809 int policydb_load_isids(struct policydb *p, struct sidtab *s)
811 struct ocontext *head, *c;
812 int rc;
814 rc = sidtab_init(s);
815 if (rc) {
816 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
817 goto out;
820 head = p->ocontexts[OCON_ISID];
821 for (c = head; c; c = c->next) {
822 rc = -EINVAL;
823 if (!c->context[0].user) {
824 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
825 c->u.name);
826 goto out;
829 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
830 if (rc) {
831 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
832 c->u.name);
833 goto out;
836 rc = 0;
837 out:
838 return rc;
841 int policydb_class_isvalid(struct policydb *p, unsigned int class)
843 if (!class || class > p->p_classes.nprim)
844 return 0;
845 return 1;
848 int policydb_role_isvalid(struct policydb *p, unsigned int role)
850 if (!role || role > p->p_roles.nprim)
851 return 0;
852 return 1;
855 int policydb_type_isvalid(struct policydb *p, unsigned int type)
857 if (!type || type > p->p_types.nprim)
858 return 0;
859 return 1;
863 * Return 1 if the fields in the security context
864 * structure `c' are valid. Return 0 otherwise.
866 int policydb_context_isvalid(struct policydb *p, struct context *c)
868 struct role_datum *role;
869 struct user_datum *usrdatum;
871 if (!c->role || c->role > p->p_roles.nprim)
872 return 0;
874 if (!c->user || c->user > p->p_users.nprim)
875 return 0;
877 if (!c->type || c->type > p->p_types.nprim)
878 return 0;
880 if (c->role != OBJECT_R_VAL) {
882 * Role must be authorized for the type.
884 role = p->role_val_to_struct[c->role - 1];
885 if (!ebitmap_get_bit(&role->types, c->type - 1))
886 /* role may not be associated with type */
887 return 0;
890 * User must be authorized for the role.
892 usrdatum = p->user_val_to_struct[c->user - 1];
893 if (!usrdatum)
894 return 0;
896 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
897 /* user may not be associated with role */
898 return 0;
901 if (!mls_context_isvalid(p, c))
902 return 0;
904 return 1;
908 * Read a MLS range structure from a policydb binary
909 * representation file.
911 static int mls_read_range_helper(struct mls_range *r, void *fp)
913 __le32 buf[2];
914 u32 items;
915 int rc;
917 rc = next_entry(buf, fp, sizeof(u32));
918 if (rc)
919 goto out;
921 rc = -EINVAL;
922 items = le32_to_cpu(buf[0]);
923 if (items > ARRAY_SIZE(buf)) {
924 printk(KERN_ERR "SELinux: mls: range overflow\n");
925 goto out;
928 rc = next_entry(buf, fp, sizeof(u32) * items);
929 if (rc) {
930 printk(KERN_ERR "SELinux: mls: truncated range\n");
931 goto out;
934 r->level[0].sens = le32_to_cpu(buf[0]);
935 if (items > 1)
936 r->level[1].sens = le32_to_cpu(buf[1]);
937 else
938 r->level[1].sens = r->level[0].sens;
940 rc = ebitmap_read(&r->level[0].cat, fp);
941 if (rc) {
942 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
943 goto out;
945 if (items > 1) {
946 rc = ebitmap_read(&r->level[1].cat, fp);
947 if (rc) {
948 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
949 goto bad_high;
951 } else {
952 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
953 if (rc) {
954 printk(KERN_ERR "SELinux: mls: out of memory\n");
955 goto bad_high;
959 return 0;
960 bad_high:
961 ebitmap_destroy(&r->level[0].cat);
962 out:
963 return rc;
967 * Read and validate a security context structure
968 * from a policydb binary representation file.
970 static int context_read_and_validate(struct context *c,
971 struct policydb *p,
972 void *fp)
974 __le32 buf[3];
975 int rc;
977 rc = next_entry(buf, fp, sizeof buf);
978 if (rc) {
979 printk(KERN_ERR "SELinux: context truncated\n");
980 goto out;
982 c->user = le32_to_cpu(buf[0]);
983 c->role = le32_to_cpu(buf[1]);
984 c->type = le32_to_cpu(buf[2]);
985 if (p->policyvers >= POLICYDB_VERSION_MLS) {
986 rc = mls_read_range_helper(&c->range, fp);
987 if (rc) {
988 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
989 goto out;
993 rc = -EINVAL;
994 if (!policydb_context_isvalid(p, c)) {
995 printk(KERN_ERR "SELinux: invalid security context\n");
996 context_destroy(c);
997 goto out;
999 rc = 0;
1000 out:
1001 return rc;
1005 * The following *_read functions are used to
1006 * read the symbol data from a policy database
1007 * binary representation file.
1010 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1012 char *key = NULL;
1013 struct perm_datum *perdatum;
1014 int rc;
1015 __le32 buf[2];
1016 u32 len;
1018 rc = -ENOMEM;
1019 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1020 if (!perdatum)
1021 goto bad;
1023 rc = next_entry(buf, fp, sizeof buf);
1024 if (rc)
1025 goto bad;
1027 len = le32_to_cpu(buf[0]);
1028 perdatum->value = le32_to_cpu(buf[1]);
1030 rc = -ENOMEM;
1031 key = kmalloc(len + 1, GFP_KERNEL);
1032 if (!key)
1033 goto bad;
1035 rc = next_entry(key, fp, len);
1036 if (rc)
1037 goto bad;
1038 key[len] = '\0';
1040 rc = hashtab_insert(h, key, perdatum);
1041 if (rc)
1042 goto bad;
1044 return 0;
1045 bad:
1046 perm_destroy(key, perdatum, NULL);
1047 return rc;
1050 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1052 char *key = NULL;
1053 struct common_datum *comdatum;
1054 __le32 buf[4];
1055 u32 len, nel;
1056 int i, rc;
1058 rc = -ENOMEM;
1059 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1060 if (!comdatum)
1061 goto bad;
1063 rc = next_entry(buf, fp, sizeof buf);
1064 if (rc)
1065 goto bad;
1067 len = le32_to_cpu(buf[0]);
1068 comdatum->value = le32_to_cpu(buf[1]);
1070 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1071 if (rc)
1072 goto bad;
1073 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1074 nel = le32_to_cpu(buf[3]);
1076 rc = -ENOMEM;
1077 key = kmalloc(len + 1, GFP_KERNEL);
1078 if (!key)
1079 goto bad;
1081 rc = next_entry(key, fp, len);
1082 if (rc)
1083 goto bad;
1084 key[len] = '\0';
1086 for (i = 0; i < nel; i++) {
1087 rc = perm_read(p, comdatum->permissions.table, fp);
1088 if (rc)
1089 goto bad;
1092 rc = hashtab_insert(h, key, comdatum);
1093 if (rc)
1094 goto bad;
1095 return 0;
1096 bad:
1097 common_destroy(key, comdatum, NULL);
1098 return rc;
1101 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1102 int allowxtarget, void *fp)
1104 struct constraint_node *c, *lc;
1105 struct constraint_expr *e, *le;
1106 __le32 buf[3];
1107 u32 nexpr;
1108 int rc, i, j, depth;
1110 lc = NULL;
1111 for (i = 0; i < ncons; i++) {
1112 c = kzalloc(sizeof(*c), GFP_KERNEL);
1113 if (!c)
1114 return -ENOMEM;
1116 if (lc)
1117 lc->next = c;
1118 else
1119 *nodep = c;
1121 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1122 if (rc)
1123 return rc;
1124 c->permissions = le32_to_cpu(buf[0]);
1125 nexpr = le32_to_cpu(buf[1]);
1126 le = NULL;
1127 depth = -1;
1128 for (j = 0; j < nexpr; j++) {
1129 e = kzalloc(sizeof(*e), GFP_KERNEL);
1130 if (!e)
1131 return -ENOMEM;
1133 if (le)
1134 le->next = e;
1135 else
1136 c->expr = e;
1138 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1139 if (rc)
1140 return rc;
1141 e->expr_type = le32_to_cpu(buf[0]);
1142 e->attr = le32_to_cpu(buf[1]);
1143 e->op = le32_to_cpu(buf[2]);
1145 switch (e->expr_type) {
1146 case CEXPR_NOT:
1147 if (depth < 0)
1148 return -EINVAL;
1149 break;
1150 case CEXPR_AND:
1151 case CEXPR_OR:
1152 if (depth < 1)
1153 return -EINVAL;
1154 depth--;
1155 break;
1156 case CEXPR_ATTR:
1157 if (depth == (CEXPR_MAXDEPTH - 1))
1158 return -EINVAL;
1159 depth++;
1160 break;
1161 case CEXPR_NAMES:
1162 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1163 return -EINVAL;
1164 if (depth == (CEXPR_MAXDEPTH - 1))
1165 return -EINVAL;
1166 depth++;
1167 rc = ebitmap_read(&e->names, fp);
1168 if (rc)
1169 return rc;
1170 break;
1171 default:
1172 return -EINVAL;
1174 le = e;
1176 if (depth != 0)
1177 return -EINVAL;
1178 lc = c;
1181 return 0;
1184 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1186 char *key = NULL;
1187 struct class_datum *cladatum;
1188 __le32 buf[6];
1189 u32 len, len2, ncons, nel;
1190 int i, rc;
1192 rc = -ENOMEM;
1193 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1194 if (!cladatum)
1195 goto bad;
1197 rc = next_entry(buf, fp, sizeof(u32)*6);
1198 if (rc)
1199 goto bad;
1201 len = le32_to_cpu(buf[0]);
1202 len2 = le32_to_cpu(buf[1]);
1203 cladatum->value = le32_to_cpu(buf[2]);
1205 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1206 if (rc)
1207 goto bad;
1208 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1209 nel = le32_to_cpu(buf[4]);
1211 ncons = le32_to_cpu(buf[5]);
1213 rc = -ENOMEM;
1214 key = kmalloc(len + 1, GFP_KERNEL);
1215 if (!key)
1216 goto bad;
1218 rc = next_entry(key, fp, len);
1219 if (rc)
1220 goto bad;
1221 key[len] = '\0';
1223 if (len2) {
1224 rc = -ENOMEM;
1225 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1226 if (!cladatum->comkey)
1227 goto bad;
1228 rc = next_entry(cladatum->comkey, fp, len2);
1229 if (rc)
1230 goto bad;
1231 cladatum->comkey[len2] = '\0';
1233 rc = -EINVAL;
1234 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1235 if (!cladatum->comdatum) {
1236 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1237 goto bad;
1240 for (i = 0; i < nel; i++) {
1241 rc = perm_read(p, cladatum->permissions.table, fp);
1242 if (rc)
1243 goto bad;
1246 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1247 if (rc)
1248 goto bad;
1250 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1251 /* grab the validatetrans rules */
1252 rc = next_entry(buf, fp, sizeof(u32));
1253 if (rc)
1254 goto bad;
1255 ncons = le32_to_cpu(buf[0]);
1256 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1257 if (rc)
1258 goto bad;
1261 rc = hashtab_insert(h, key, cladatum);
1262 if (rc)
1263 goto bad;
1265 return 0;
1266 bad:
1267 cls_destroy(key, cladatum, NULL);
1268 return rc;
1271 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1273 char *key = NULL;
1274 struct role_datum *role;
1275 int rc, to_read = 2;
1276 __le32 buf[3];
1277 u32 len;
1279 rc = -ENOMEM;
1280 role = kzalloc(sizeof(*role), GFP_KERNEL);
1281 if (!role)
1282 goto bad;
1284 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1285 to_read = 3;
1287 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1288 if (rc)
1289 goto bad;
1291 len = le32_to_cpu(buf[0]);
1292 role->value = le32_to_cpu(buf[1]);
1293 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1294 role->bounds = le32_to_cpu(buf[2]);
1296 rc = -ENOMEM;
1297 key = kmalloc(len + 1, GFP_KERNEL);
1298 if (!key)
1299 goto bad;
1301 rc = next_entry(key, fp, len);
1302 if (rc)
1303 goto bad;
1304 key[len] = '\0';
1306 rc = ebitmap_read(&role->dominates, fp);
1307 if (rc)
1308 goto bad;
1310 rc = ebitmap_read(&role->types, fp);
1311 if (rc)
1312 goto bad;
1314 if (strcmp(key, OBJECT_R) == 0) {
1315 rc = -EINVAL;
1316 if (role->value != OBJECT_R_VAL) {
1317 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1318 OBJECT_R, role->value);
1319 goto bad;
1321 rc = 0;
1322 goto bad;
1325 rc = hashtab_insert(h, key, role);
1326 if (rc)
1327 goto bad;
1328 return 0;
1329 bad:
1330 role_destroy(key, role, NULL);
1331 return rc;
1334 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1336 char *key = NULL;
1337 struct type_datum *typdatum;
1338 int rc, to_read = 3;
1339 __le32 buf[4];
1340 u32 len;
1342 rc = -ENOMEM;
1343 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1344 if (!typdatum)
1345 goto bad;
1347 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1348 to_read = 4;
1350 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1351 if (rc)
1352 goto bad;
1354 len = le32_to_cpu(buf[0]);
1355 typdatum->value = le32_to_cpu(buf[1]);
1356 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1357 u32 prop = le32_to_cpu(buf[2]);
1359 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1360 typdatum->primary = 1;
1361 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1362 typdatum->attribute = 1;
1364 typdatum->bounds = le32_to_cpu(buf[3]);
1365 } else {
1366 typdatum->primary = le32_to_cpu(buf[2]);
1369 rc = -ENOMEM;
1370 key = kmalloc(len + 1, GFP_KERNEL);
1371 if (!key)
1372 goto bad;
1373 rc = next_entry(key, fp, len);
1374 if (rc)
1375 goto bad;
1376 key[len] = '\0';
1378 rc = hashtab_insert(h, key, typdatum);
1379 if (rc)
1380 goto bad;
1381 return 0;
1382 bad:
1383 type_destroy(key, typdatum, NULL);
1384 return rc;
1389 * Read a MLS level structure from a policydb binary
1390 * representation file.
1392 static int mls_read_level(struct mls_level *lp, void *fp)
1394 __le32 buf[1];
1395 int rc;
1397 memset(lp, 0, sizeof(*lp));
1399 rc = next_entry(buf, fp, sizeof buf);
1400 if (rc) {
1401 printk(KERN_ERR "SELinux: mls: truncated level\n");
1402 return rc;
1404 lp->sens = le32_to_cpu(buf[0]);
1406 rc = ebitmap_read(&lp->cat, fp);
1407 if (rc) {
1408 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1409 return rc;
1411 return 0;
1414 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1416 char *key = NULL;
1417 struct user_datum *usrdatum;
1418 int rc, to_read = 2;
1419 __le32 buf[3];
1420 u32 len;
1422 rc = -ENOMEM;
1423 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1424 if (!usrdatum)
1425 goto bad;
1427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428 to_read = 3;
1430 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1431 if (rc)
1432 goto bad;
1434 len = le32_to_cpu(buf[0]);
1435 usrdatum->value = le32_to_cpu(buf[1]);
1436 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1437 usrdatum->bounds = le32_to_cpu(buf[2]);
1439 rc = -ENOMEM;
1440 key = kmalloc(len + 1, GFP_KERNEL);
1441 if (!key)
1442 goto bad;
1443 rc = next_entry(key, fp, len);
1444 if (rc)
1445 goto bad;
1446 key[len] = '\0';
1448 rc = ebitmap_read(&usrdatum->roles, fp);
1449 if (rc)
1450 goto bad;
1452 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1453 rc = mls_read_range_helper(&usrdatum->range, fp);
1454 if (rc)
1455 goto bad;
1456 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1457 if (rc)
1458 goto bad;
1461 rc = hashtab_insert(h, key, usrdatum);
1462 if (rc)
1463 goto bad;
1464 return 0;
1465 bad:
1466 user_destroy(key, usrdatum, NULL);
1467 return rc;
1470 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1472 char *key = NULL;
1473 struct level_datum *levdatum;
1474 int rc;
1475 __le32 buf[2];
1476 u32 len;
1478 rc = -ENOMEM;
1479 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1480 if (!levdatum)
1481 goto bad;
1483 rc = next_entry(buf, fp, sizeof buf);
1484 if (rc)
1485 goto bad;
1487 len = le32_to_cpu(buf[0]);
1488 levdatum->isalias = le32_to_cpu(buf[1]);
1490 rc = -ENOMEM;
1491 key = kmalloc(len + 1, GFP_ATOMIC);
1492 if (!key)
1493 goto bad;
1494 rc = next_entry(key, fp, len);
1495 if (rc)
1496 goto bad;
1497 key[len] = '\0';
1499 rc = -ENOMEM;
1500 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1501 if (!levdatum->level)
1502 goto bad;
1504 rc = mls_read_level(levdatum->level, fp);
1505 if (rc)
1506 goto bad;
1508 rc = hashtab_insert(h, key, levdatum);
1509 if (rc)
1510 goto bad;
1511 return 0;
1512 bad:
1513 sens_destroy(key, levdatum, NULL);
1514 return rc;
1517 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1519 char *key = NULL;
1520 struct cat_datum *catdatum;
1521 int rc;
1522 __le32 buf[3];
1523 u32 len;
1525 rc = -ENOMEM;
1526 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1527 if (!catdatum)
1528 goto bad;
1530 rc = next_entry(buf, fp, sizeof buf);
1531 if (rc)
1532 goto bad;
1534 len = le32_to_cpu(buf[0]);
1535 catdatum->value = le32_to_cpu(buf[1]);
1536 catdatum->isalias = le32_to_cpu(buf[2]);
1538 rc = -ENOMEM;
1539 key = kmalloc(len + 1, GFP_ATOMIC);
1540 if (!key)
1541 goto bad;
1542 rc = next_entry(key, fp, len);
1543 if (rc)
1544 goto bad;
1545 key[len] = '\0';
1547 rc = hashtab_insert(h, key, catdatum);
1548 if (rc)
1549 goto bad;
1550 return 0;
1551 bad:
1552 cat_destroy(key, catdatum, NULL);
1553 return rc;
1556 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1558 common_read,
1559 class_read,
1560 role_read,
1561 type_read,
1562 user_read,
1563 cond_read_bool,
1564 sens_read,
1565 cat_read,
1568 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1570 struct user_datum *upper, *user;
1571 struct policydb *p = datap;
1572 int depth = 0;
1574 upper = user = datum;
1575 while (upper->bounds) {
1576 struct ebitmap_node *node;
1577 unsigned long bit;
1579 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1580 printk(KERN_ERR "SELinux: user %s: "
1581 "too deep or looped boundary",
1582 (char *) key);
1583 return -EINVAL;
1586 upper = p->user_val_to_struct[upper->bounds - 1];
1587 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1588 if (ebitmap_get_bit(&upper->roles, bit))
1589 continue;
1591 printk(KERN_ERR
1592 "SELinux: boundary violated policy: "
1593 "user=%s role=%s bounds=%s\n",
1594 sym_name(p, SYM_USERS, user->value - 1),
1595 sym_name(p, SYM_ROLES, bit),
1596 sym_name(p, SYM_USERS, upper->value - 1));
1598 return -EINVAL;
1602 return 0;
1605 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1607 struct role_datum *upper, *role;
1608 struct policydb *p = datap;
1609 int depth = 0;
1611 upper = role = datum;
1612 while (upper->bounds) {
1613 struct ebitmap_node *node;
1614 unsigned long bit;
1616 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1617 printk(KERN_ERR "SELinux: role %s: "
1618 "too deep or looped bounds\n",
1619 (char *) key);
1620 return -EINVAL;
1623 upper = p->role_val_to_struct[upper->bounds - 1];
1624 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1625 if (ebitmap_get_bit(&upper->types, bit))
1626 continue;
1628 printk(KERN_ERR
1629 "SELinux: boundary violated policy: "
1630 "role=%s type=%s bounds=%s\n",
1631 sym_name(p, SYM_ROLES, role->value - 1),
1632 sym_name(p, SYM_TYPES, bit),
1633 sym_name(p, SYM_ROLES, upper->value - 1));
1635 return -EINVAL;
1639 return 0;
1642 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1644 struct type_datum *upper;
1645 struct policydb *p = datap;
1646 int depth = 0;
1648 upper = datum;
1649 while (upper->bounds) {
1650 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1651 printk(KERN_ERR "SELinux: type %s: "
1652 "too deep or looped boundary\n",
1653 (char *) key);
1654 return -EINVAL;
1657 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1658 upper->bounds - 1);
1659 BUG_ON(!upper);
1661 if (upper->attribute) {
1662 printk(KERN_ERR "SELinux: type %s: "
1663 "bounded by attribute %s",
1664 (char *) key,
1665 sym_name(p, SYM_TYPES, upper->value - 1));
1666 return -EINVAL;
1670 return 0;
1673 static int policydb_bounds_sanity_check(struct policydb *p)
1675 int rc;
1677 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1678 return 0;
1680 rc = hashtab_map(p->p_users.table,
1681 user_bounds_sanity_check, p);
1682 if (rc)
1683 return rc;
1685 rc = hashtab_map(p->p_roles.table,
1686 role_bounds_sanity_check, p);
1687 if (rc)
1688 return rc;
1690 rc = hashtab_map(p->p_types.table,
1691 type_bounds_sanity_check, p);
1692 if (rc)
1693 return rc;
1695 return 0;
1698 extern int ss_initialized;
1700 u16 string_to_security_class(struct policydb *p, const char *name)
1702 struct class_datum *cladatum;
1704 cladatum = hashtab_search(p->p_classes.table, name);
1705 if (!cladatum)
1706 return 0;
1708 return cladatum->value;
1711 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1713 struct class_datum *cladatum;
1714 struct perm_datum *perdatum = NULL;
1715 struct common_datum *comdatum;
1717 if (!tclass || tclass > p->p_classes.nprim)
1718 return 0;
1720 cladatum = p->class_val_to_struct[tclass-1];
1721 comdatum = cladatum->comdatum;
1722 if (comdatum)
1723 perdatum = hashtab_search(comdatum->permissions.table,
1724 name);
1725 if (!perdatum)
1726 perdatum = hashtab_search(cladatum->permissions.table,
1727 name);
1728 if (!perdatum)
1729 return 0;
1731 return 1U << (perdatum->value-1);
1734 static int range_read(struct policydb *p, void *fp)
1736 struct range_trans *rt = NULL;
1737 struct mls_range *r = NULL;
1738 int i, rc;
1739 __le32 buf[2];
1740 u32 nel;
1742 if (p->policyvers < POLICYDB_VERSION_MLS)
1743 return 0;
1745 rc = next_entry(buf, fp, sizeof(u32));
1746 if (rc)
1747 goto out;
1749 nel = le32_to_cpu(buf[0]);
1750 for (i = 0; i < nel; i++) {
1751 rc = -ENOMEM;
1752 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1753 if (!rt)
1754 goto out;
1756 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1757 if (rc)
1758 goto out;
1760 rt->source_type = le32_to_cpu(buf[0]);
1761 rt->target_type = le32_to_cpu(buf[1]);
1762 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1763 rc = next_entry(buf, fp, sizeof(u32));
1764 if (rc)
1765 goto out;
1766 rt->target_class = le32_to_cpu(buf[0]);
1767 } else
1768 rt->target_class = p->process_class;
1770 rc = -EINVAL;
1771 if (!policydb_type_isvalid(p, rt->source_type) ||
1772 !policydb_type_isvalid(p, rt->target_type) ||
1773 !policydb_class_isvalid(p, rt->target_class))
1774 goto out;
1776 rc = -ENOMEM;
1777 r = kzalloc(sizeof(*r), GFP_KERNEL);
1778 if (!r)
1779 goto out;
1781 rc = mls_read_range_helper(r, fp);
1782 if (rc)
1783 goto out;
1785 rc = -EINVAL;
1786 if (!mls_range_isvalid(p, r)) {
1787 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1788 goto out;
1791 rc = hashtab_insert(p->range_tr, rt, r);
1792 if (rc)
1793 goto out;
1795 rt = NULL;
1796 r = NULL;
1798 rangetr_hash_eval(p->range_tr);
1799 rc = 0;
1800 out:
1801 kfree(rt);
1802 kfree(r);
1803 return rc;
1806 static int filename_trans_read(struct policydb *p, void *fp)
1808 struct filename_trans *ft, *last;
1809 u32 nel, len;
1810 char *name;
1811 __le32 buf[4];
1812 int rc, i;
1814 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1815 return 0;
1817 rc = next_entry(buf, fp, sizeof(u32));
1818 if (rc)
1819 goto out;
1820 nel = le32_to_cpu(buf[0]);
1822 printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1824 last = p->filename_trans;
1825 while (last && last->next)
1826 last = last->next;
1828 for (i = 0; i < nel; i++) {
1829 rc = -ENOMEM;
1830 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1831 if (!ft)
1832 goto out;
1834 /* add it to the tail of the list */
1835 if (!last)
1836 p->filename_trans = ft;
1837 else
1838 last->next = ft;
1839 last = ft;
1841 /* length of the path component string */
1842 rc = next_entry(buf, fp, sizeof(u32));
1843 if (rc)
1844 goto out;
1845 len = le32_to_cpu(buf[0]);
1847 rc = -ENOMEM;
1848 name = kmalloc(len + 1, GFP_KERNEL);
1849 if (!name)
1850 goto out;
1852 ft->name = name;
1854 /* path component string */
1855 rc = next_entry(name, fp, len);
1856 if (rc)
1857 goto out;
1858 name[len] = 0;
1860 printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1862 rc = next_entry(buf, fp, sizeof(u32) * 4);
1863 if (rc)
1864 goto out;
1866 ft->stype = le32_to_cpu(buf[0]);
1867 ft->ttype = le32_to_cpu(buf[1]);
1868 ft->tclass = le32_to_cpu(buf[2]);
1869 ft->otype = le32_to_cpu(buf[3]);
1871 rc = 0;
1872 out:
1873 return rc;
1876 static int genfs_read(struct policydb *p, void *fp)
1878 int i, j, rc;
1879 u32 nel, nel2, len, len2;
1880 __le32 buf[1];
1881 struct ocontext *l, *c;
1882 struct ocontext *newc = NULL;
1883 struct genfs *genfs_p, *genfs;
1884 struct genfs *newgenfs = NULL;
1886 rc = next_entry(buf, fp, sizeof(u32));
1887 if (rc)
1888 goto out;
1889 nel = le32_to_cpu(buf[0]);
1891 for (i = 0; i < nel; i++) {
1892 rc = next_entry(buf, fp, sizeof(u32));
1893 if (rc)
1894 goto out;
1895 len = le32_to_cpu(buf[0]);
1897 rc = -ENOMEM;
1898 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1899 if (!newgenfs)
1900 goto out;
1902 rc = -ENOMEM;
1903 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1904 if (!newgenfs->fstype)
1905 goto out;
1907 rc = next_entry(newgenfs->fstype, fp, len);
1908 if (rc)
1909 goto out;
1911 newgenfs->fstype[len] = 0;
1913 for (genfs_p = NULL, genfs = p->genfs; genfs;
1914 genfs_p = genfs, genfs = genfs->next) {
1915 rc = -EINVAL;
1916 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1917 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1918 newgenfs->fstype);
1919 goto out;
1921 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1922 break;
1924 newgenfs->next = genfs;
1925 if (genfs_p)
1926 genfs_p->next = newgenfs;
1927 else
1928 p->genfs = newgenfs;
1929 genfs = newgenfs;
1930 newgenfs = NULL;
1932 rc = next_entry(buf, fp, sizeof(u32));
1933 if (rc)
1934 goto out;
1936 nel2 = le32_to_cpu(buf[0]);
1937 for (j = 0; j < nel2; j++) {
1938 rc = next_entry(buf, fp, sizeof(u32));
1939 if (rc)
1940 goto out;
1941 len = le32_to_cpu(buf[0]);
1943 rc = -ENOMEM;
1944 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1945 if (!newc)
1946 goto out;
1948 rc = -ENOMEM;
1949 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1950 if (!newc->u.name)
1951 goto out;
1953 rc = next_entry(newc->u.name, fp, len);
1954 if (rc)
1955 goto out;
1956 newc->u.name[len] = 0;
1958 rc = next_entry(buf, fp, sizeof(u32));
1959 if (rc)
1960 goto out;
1962 newc->v.sclass = le32_to_cpu(buf[0]);
1963 rc = context_read_and_validate(&newc->context[0], p, fp);
1964 if (rc)
1965 goto out;
1967 for (l = NULL, c = genfs->head; c;
1968 l = c, c = c->next) {
1969 rc = -EINVAL;
1970 if (!strcmp(newc->u.name, c->u.name) &&
1971 (!c->v.sclass || !newc->v.sclass ||
1972 newc->v.sclass == c->v.sclass)) {
1973 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1974 genfs->fstype, c->u.name);
1975 goto out;
1977 len = strlen(newc->u.name);
1978 len2 = strlen(c->u.name);
1979 if (len > len2)
1980 break;
1983 newc->next = c;
1984 if (l)
1985 l->next = newc;
1986 else
1987 genfs->head = newc;
1988 newc = NULL;
1991 rc = 0;
1992 out:
1993 if (newgenfs)
1994 kfree(newgenfs->fstype);
1995 kfree(newgenfs);
1996 ocontext_destroy(newc, OCON_FSUSE);
1998 return rc;
2001 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2002 void *fp)
2004 int i, j, rc;
2005 u32 nel, len;
2006 __le32 buf[3];
2007 struct ocontext *l, *c;
2008 u32 nodebuf[8];
2010 for (i = 0; i < info->ocon_num; i++) {
2011 rc = next_entry(buf, fp, sizeof(u32));
2012 if (rc)
2013 goto out;
2014 nel = le32_to_cpu(buf[0]);
2016 l = NULL;
2017 for (j = 0; j < nel; j++) {
2018 rc = -ENOMEM;
2019 c = kzalloc(sizeof(*c), GFP_KERNEL);
2020 if (!c)
2021 goto out;
2022 if (l)
2023 l->next = c;
2024 else
2025 p->ocontexts[i] = c;
2026 l = c;
2028 switch (i) {
2029 case OCON_ISID:
2030 rc = next_entry(buf, fp, sizeof(u32));
2031 if (rc)
2032 goto out;
2034 c->sid[0] = le32_to_cpu(buf[0]);
2035 rc = context_read_and_validate(&c->context[0], p, fp);
2036 if (rc)
2037 goto out;
2038 break;
2039 case OCON_FS:
2040 case OCON_NETIF:
2041 rc = next_entry(buf, fp, sizeof(u32));
2042 if (rc)
2043 goto out;
2044 len = le32_to_cpu(buf[0]);
2046 rc = -ENOMEM;
2047 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2048 if (!c->u.name)
2049 goto out;
2051 rc = next_entry(c->u.name, fp, len);
2052 if (rc)
2053 goto out;
2055 c->u.name[len] = 0;
2056 rc = context_read_and_validate(&c->context[0], p, fp);
2057 if (rc)
2058 goto out;
2059 rc = context_read_and_validate(&c->context[1], p, fp);
2060 if (rc)
2061 goto out;
2062 break;
2063 case OCON_PORT:
2064 rc = next_entry(buf, fp, sizeof(u32)*3);
2065 if (rc)
2066 goto out;
2067 c->u.port.protocol = le32_to_cpu(buf[0]);
2068 c->u.port.low_port = le32_to_cpu(buf[1]);
2069 c->u.port.high_port = le32_to_cpu(buf[2]);
2070 rc = context_read_and_validate(&c->context[0], p, fp);
2071 if (rc)
2072 goto out;
2073 break;
2074 case OCON_NODE:
2075 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2076 if (rc)
2077 goto out;
2078 c->u.node.addr = nodebuf[0]; /* network order */
2079 c->u.node.mask = nodebuf[1]; /* network order */
2080 rc = context_read_and_validate(&c->context[0], p, fp);
2081 if (rc)
2082 goto out;
2083 break;
2084 case OCON_FSUSE:
2085 rc = next_entry(buf, fp, sizeof(u32)*2);
2086 if (rc)
2087 goto out;
2089 rc = -EINVAL;
2090 c->v.behavior = le32_to_cpu(buf[0]);
2091 if (c->v.behavior > SECURITY_FS_USE_NONE)
2092 goto out;
2094 rc = -ENOMEM;
2095 len = le32_to_cpu(buf[1]);
2096 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2097 if (!c->u.name)
2098 goto out;
2100 rc = next_entry(c->u.name, fp, len);
2101 if (rc)
2102 goto out;
2103 c->u.name[len] = 0;
2104 rc = context_read_and_validate(&c->context[0], p, fp);
2105 if (rc)
2106 goto out;
2107 break;
2108 case OCON_NODE6: {
2109 int k;
2111 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2112 if (rc)
2113 goto out;
2114 for (k = 0; k < 4; k++)
2115 c->u.node6.addr[k] = nodebuf[k];
2116 for (k = 0; k < 4; k++)
2117 c->u.node6.mask[k] = nodebuf[k+4];
2118 rc = context_read_and_validate(&c->context[0], p, fp);
2119 if (rc)
2120 goto out;
2121 break;
2126 rc = 0;
2127 out:
2128 return rc;
2132 * Read the configuration data from a policy database binary
2133 * representation file into a policy database structure.
2135 int policydb_read(struct policydb *p, void *fp)
2137 struct role_allow *ra, *lra;
2138 struct role_trans *tr, *ltr;
2139 int i, j, rc;
2140 __le32 buf[4];
2141 u32 len, nprim, nel;
2143 char *policydb_str;
2144 struct policydb_compat_info *info;
2146 rc = policydb_init(p);
2147 if (rc)
2148 return rc;
2150 /* Read the magic number and string length. */
2151 rc = next_entry(buf, fp, sizeof(u32) * 2);
2152 if (rc)
2153 goto bad;
2155 rc = -EINVAL;
2156 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2157 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2158 "not match expected magic number 0x%x\n",
2159 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2160 goto bad;
2163 rc = -EINVAL;
2164 len = le32_to_cpu(buf[1]);
2165 if (len != strlen(POLICYDB_STRING)) {
2166 printk(KERN_ERR "SELinux: policydb string length %d does not "
2167 "match expected length %Zu\n",
2168 len, strlen(POLICYDB_STRING));
2169 goto bad;
2172 rc = -ENOMEM;
2173 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2174 if (!policydb_str) {
2175 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2176 "string of length %d\n", len);
2177 goto bad;
2180 rc = next_entry(policydb_str, fp, len);
2181 if (rc) {
2182 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2183 kfree(policydb_str);
2184 goto bad;
2187 rc = -EINVAL;
2188 policydb_str[len] = '\0';
2189 if (strcmp(policydb_str, POLICYDB_STRING)) {
2190 printk(KERN_ERR "SELinux: policydb string %s does not match "
2191 "my string %s\n", policydb_str, POLICYDB_STRING);
2192 kfree(policydb_str);
2193 goto bad;
2195 /* Done with policydb_str. */
2196 kfree(policydb_str);
2197 policydb_str = NULL;
2199 /* Read the version and table sizes. */
2200 rc = next_entry(buf, fp, sizeof(u32)*4);
2201 if (rc)
2202 goto bad;
2204 rc = -EINVAL;
2205 p->policyvers = le32_to_cpu(buf[0]);
2206 if (p->policyvers < POLICYDB_VERSION_MIN ||
2207 p->policyvers > POLICYDB_VERSION_MAX) {
2208 printk(KERN_ERR "SELinux: policydb version %d does not match "
2209 "my version range %d-%d\n",
2210 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2211 goto bad;
2214 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2215 p->mls_enabled = 1;
2217 rc = -EINVAL;
2218 if (p->policyvers < POLICYDB_VERSION_MLS) {
2219 printk(KERN_ERR "SELinux: security policydb version %d "
2220 "(MLS) not backwards compatible\n",
2221 p->policyvers);
2222 goto bad;
2225 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2226 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2228 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2229 rc = ebitmap_read(&p->policycaps, fp);
2230 if (rc)
2231 goto bad;
2234 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2235 rc = ebitmap_read(&p->permissive_map, fp);
2236 if (rc)
2237 goto bad;
2240 rc = -EINVAL;
2241 info = policydb_lookup_compat(p->policyvers);
2242 if (!info) {
2243 printk(KERN_ERR "SELinux: unable to find policy compat info "
2244 "for version %d\n", p->policyvers);
2245 goto bad;
2248 rc = -EINVAL;
2249 if (le32_to_cpu(buf[2]) != info->sym_num ||
2250 le32_to_cpu(buf[3]) != info->ocon_num) {
2251 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2252 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2253 le32_to_cpu(buf[3]),
2254 info->sym_num, info->ocon_num);
2255 goto bad;
2258 for (i = 0; i < info->sym_num; i++) {
2259 rc = next_entry(buf, fp, sizeof(u32)*2);
2260 if (rc)
2261 goto bad;
2262 nprim = le32_to_cpu(buf[0]);
2263 nel = le32_to_cpu(buf[1]);
2264 for (j = 0; j < nel; j++) {
2265 rc = read_f[i](p, p->symtab[i].table, fp);
2266 if (rc)
2267 goto bad;
2270 p->symtab[i].nprim = nprim;
2273 rc = avtab_read(&p->te_avtab, fp, p);
2274 if (rc)
2275 goto bad;
2277 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2278 rc = cond_read_list(p, fp);
2279 if (rc)
2280 goto bad;
2283 rc = next_entry(buf, fp, sizeof(u32));
2284 if (rc)
2285 goto bad;
2286 nel = le32_to_cpu(buf[0]);
2287 ltr = NULL;
2288 for (i = 0; i < nel; i++) {
2289 rc = -ENOMEM;
2290 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2291 if (!tr)
2292 goto bad;
2293 if (ltr)
2294 ltr->next = tr;
2295 else
2296 p->role_tr = tr;
2297 rc = next_entry(buf, fp, sizeof(u32)*3);
2298 if (rc)
2299 goto bad;
2301 rc = -EINVAL;
2302 tr->role = le32_to_cpu(buf[0]);
2303 tr->type = le32_to_cpu(buf[1]);
2304 tr->new_role = le32_to_cpu(buf[2]);
2305 if (!policydb_role_isvalid(p, tr->role) ||
2306 !policydb_type_isvalid(p, tr->type) ||
2307 !policydb_role_isvalid(p, tr->new_role))
2308 goto bad;
2309 ltr = tr;
2312 rc = next_entry(buf, fp, sizeof(u32));
2313 if (rc)
2314 goto bad;
2315 nel = le32_to_cpu(buf[0]);
2316 lra = NULL;
2317 for (i = 0; i < nel; i++) {
2318 rc = -ENOMEM;
2319 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2320 if (!ra)
2321 goto bad;
2322 if (lra)
2323 lra->next = ra;
2324 else
2325 p->role_allow = ra;
2326 rc = next_entry(buf, fp, sizeof(u32)*2);
2327 if (rc)
2328 goto bad;
2330 rc = -EINVAL;
2331 ra->role = le32_to_cpu(buf[0]);
2332 ra->new_role = le32_to_cpu(buf[1]);
2333 if (!policydb_role_isvalid(p, ra->role) ||
2334 !policydb_role_isvalid(p, ra->new_role))
2335 goto bad;
2336 lra = ra;
2339 rc = filename_trans_read(p, fp);
2340 if (rc)
2341 goto bad;
2343 rc = policydb_index(p);
2344 if (rc)
2345 goto bad;
2347 rc = -EINVAL;
2348 p->process_class = string_to_security_class(p, "process");
2349 if (!p->process_class)
2350 goto bad;
2352 rc = -EINVAL;
2353 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2354 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2355 if (!p->process_trans_perms)
2356 goto bad;
2358 rc = ocontext_read(p, info, fp);
2359 if (rc)
2360 goto bad;
2362 rc = genfs_read(p, fp);
2363 if (rc)
2364 goto bad;
2366 rc = range_read(p, fp);
2367 if (rc)
2368 goto bad;
2370 rc = -ENOMEM;
2371 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2372 p->p_types.nprim,
2373 GFP_KERNEL | __GFP_ZERO);
2374 if (!p->type_attr_map_array)
2375 goto bad;
2377 /* preallocate so we don't have to worry about the put ever failing */
2378 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2379 GFP_KERNEL | __GFP_ZERO);
2380 if (rc)
2381 goto bad;
2383 for (i = 0; i < p->p_types.nprim; i++) {
2384 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2386 BUG_ON(!e);
2387 ebitmap_init(e);
2388 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2389 rc = ebitmap_read(e, fp);
2390 if (rc)
2391 goto bad;
2393 /* add the type itself as the degenerate case */
2394 rc = ebitmap_set_bit(e, i, 1);
2395 if (rc)
2396 goto bad;
2399 rc = policydb_bounds_sanity_check(p);
2400 if (rc)
2401 goto bad;
2403 rc = 0;
2404 out:
2405 return rc;
2406 bad:
2407 policydb_destroy(p);
2408 goto out;
2412 * Write a MLS level structure to a policydb binary
2413 * representation file.
2415 static int mls_write_level(struct mls_level *l, void *fp)
2417 __le32 buf[1];
2418 int rc;
2420 buf[0] = cpu_to_le32(l->sens);
2421 rc = put_entry(buf, sizeof(u32), 1, fp);
2422 if (rc)
2423 return rc;
2425 rc = ebitmap_write(&l->cat, fp);
2426 if (rc)
2427 return rc;
2429 return 0;
2433 * Write a MLS range structure to a policydb binary
2434 * representation file.
2436 static int mls_write_range_helper(struct mls_range *r, void *fp)
2438 __le32 buf[3];
2439 size_t items;
2440 int rc, eq;
2442 eq = mls_level_eq(&r->level[1], &r->level[0]);
2444 if (eq)
2445 items = 2;
2446 else
2447 items = 3;
2448 buf[0] = cpu_to_le32(items-1);
2449 buf[1] = cpu_to_le32(r->level[0].sens);
2450 if (!eq)
2451 buf[2] = cpu_to_le32(r->level[1].sens);
2453 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2455 rc = put_entry(buf, sizeof(u32), items, fp);
2456 if (rc)
2457 return rc;
2459 rc = ebitmap_write(&r->level[0].cat, fp);
2460 if (rc)
2461 return rc;
2462 if (!eq) {
2463 rc = ebitmap_write(&r->level[1].cat, fp);
2464 if (rc)
2465 return rc;
2468 return 0;
2471 static int sens_write(void *vkey, void *datum, void *ptr)
2473 char *key = vkey;
2474 struct level_datum *levdatum = datum;
2475 struct policy_data *pd = ptr;
2476 void *fp = pd->fp;
2477 __le32 buf[2];
2478 size_t len;
2479 int rc;
2481 len = strlen(key);
2482 buf[0] = cpu_to_le32(len);
2483 buf[1] = cpu_to_le32(levdatum->isalias);
2484 rc = put_entry(buf, sizeof(u32), 2, fp);
2485 if (rc)
2486 return rc;
2488 rc = put_entry(key, 1, len, fp);
2489 if (rc)
2490 return rc;
2492 rc = mls_write_level(levdatum->level, fp);
2493 if (rc)
2494 return rc;
2496 return 0;
2499 static int cat_write(void *vkey, void *datum, void *ptr)
2501 char *key = vkey;
2502 struct cat_datum *catdatum = datum;
2503 struct policy_data *pd = ptr;
2504 void *fp = pd->fp;
2505 __le32 buf[3];
2506 size_t len;
2507 int rc;
2509 len = strlen(key);
2510 buf[0] = cpu_to_le32(len);
2511 buf[1] = cpu_to_le32(catdatum->value);
2512 buf[2] = cpu_to_le32(catdatum->isalias);
2513 rc = put_entry(buf, sizeof(u32), 3, fp);
2514 if (rc)
2515 return rc;
2517 rc = put_entry(key, 1, len, fp);
2518 if (rc)
2519 return rc;
2521 return 0;
2524 static int role_trans_write(struct role_trans *r, void *fp)
2526 struct role_trans *tr;
2527 u32 buf[3];
2528 size_t nel;
2529 int rc;
2531 nel = 0;
2532 for (tr = r; tr; tr = tr->next)
2533 nel++;
2534 buf[0] = cpu_to_le32(nel);
2535 rc = put_entry(buf, sizeof(u32), 1, fp);
2536 if (rc)
2537 return rc;
2538 for (tr = r; tr; tr = tr->next) {
2539 buf[0] = cpu_to_le32(tr->role);
2540 buf[1] = cpu_to_le32(tr->type);
2541 buf[2] = cpu_to_le32(tr->new_role);
2542 rc = put_entry(buf, sizeof(u32), 3, fp);
2543 if (rc)
2544 return rc;
2547 return 0;
2550 static int role_allow_write(struct role_allow *r, void *fp)
2552 struct role_allow *ra;
2553 u32 buf[2];
2554 size_t nel;
2555 int rc;
2557 nel = 0;
2558 for (ra = r; ra; ra = ra->next)
2559 nel++;
2560 buf[0] = cpu_to_le32(nel);
2561 rc = put_entry(buf, sizeof(u32), 1, fp);
2562 if (rc)
2563 return rc;
2564 for (ra = r; ra; ra = ra->next) {
2565 buf[0] = cpu_to_le32(ra->role);
2566 buf[1] = cpu_to_le32(ra->new_role);
2567 rc = put_entry(buf, sizeof(u32), 2, fp);
2568 if (rc)
2569 return rc;
2571 return 0;
2575 * Write a security context structure
2576 * to a policydb binary representation file.
2578 static int context_write(struct policydb *p, struct context *c,
2579 void *fp)
2581 int rc;
2582 __le32 buf[3];
2584 buf[0] = cpu_to_le32(c->user);
2585 buf[1] = cpu_to_le32(c->role);
2586 buf[2] = cpu_to_le32(c->type);
2588 rc = put_entry(buf, sizeof(u32), 3, fp);
2589 if (rc)
2590 return rc;
2592 rc = mls_write_range_helper(&c->range, fp);
2593 if (rc)
2594 return rc;
2596 return 0;
2600 * The following *_write functions are used to
2601 * write the symbol data to a policy database
2602 * binary representation file.
2605 static int perm_write(void *vkey, void *datum, void *fp)
2607 char *key = vkey;
2608 struct perm_datum *perdatum = datum;
2609 __le32 buf[2];
2610 size_t len;
2611 int rc;
2613 len = strlen(key);
2614 buf[0] = cpu_to_le32(len);
2615 buf[1] = cpu_to_le32(perdatum->value);
2616 rc = put_entry(buf, sizeof(u32), 2, fp);
2617 if (rc)
2618 return rc;
2620 rc = put_entry(key, 1, len, fp);
2621 if (rc)
2622 return rc;
2624 return 0;
2627 static int common_write(void *vkey, void *datum, void *ptr)
2629 char *key = vkey;
2630 struct common_datum *comdatum = datum;
2631 struct policy_data *pd = ptr;
2632 void *fp = pd->fp;
2633 __le32 buf[4];
2634 size_t len;
2635 int rc;
2637 len = strlen(key);
2638 buf[0] = cpu_to_le32(len);
2639 buf[1] = cpu_to_le32(comdatum->value);
2640 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2641 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2642 rc = put_entry(buf, sizeof(u32), 4, fp);
2643 if (rc)
2644 return rc;
2646 rc = put_entry(key, 1, len, fp);
2647 if (rc)
2648 return rc;
2650 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2651 if (rc)
2652 return rc;
2654 return 0;
2657 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2658 void *fp)
2660 struct constraint_node *c;
2661 struct constraint_expr *e;
2662 __le32 buf[3];
2663 u32 nel;
2664 int rc;
2666 for (c = node; c; c = c->next) {
2667 nel = 0;
2668 for (e = c->expr; e; e = e->next)
2669 nel++;
2670 buf[0] = cpu_to_le32(c->permissions);
2671 buf[1] = cpu_to_le32(nel);
2672 rc = put_entry(buf, sizeof(u32), 2, fp);
2673 if (rc)
2674 return rc;
2675 for (e = c->expr; e; e = e->next) {
2676 buf[0] = cpu_to_le32(e->expr_type);
2677 buf[1] = cpu_to_le32(e->attr);
2678 buf[2] = cpu_to_le32(e->op);
2679 rc = put_entry(buf, sizeof(u32), 3, fp);
2680 if (rc)
2681 return rc;
2683 switch (e->expr_type) {
2684 case CEXPR_NAMES:
2685 rc = ebitmap_write(&e->names, fp);
2686 if (rc)
2687 return rc;
2688 break;
2689 default:
2690 break;
2695 return 0;
2698 static int class_write(void *vkey, void *datum, void *ptr)
2700 char *key = vkey;
2701 struct class_datum *cladatum = datum;
2702 struct policy_data *pd = ptr;
2703 void *fp = pd->fp;
2704 struct policydb *p = pd->p;
2705 struct constraint_node *c;
2706 __le32 buf[6];
2707 u32 ncons;
2708 size_t len, len2;
2709 int rc;
2711 len = strlen(key);
2712 if (cladatum->comkey)
2713 len2 = strlen(cladatum->comkey);
2714 else
2715 len2 = 0;
2717 ncons = 0;
2718 for (c = cladatum->constraints; c; c = c->next)
2719 ncons++;
2721 buf[0] = cpu_to_le32(len);
2722 buf[1] = cpu_to_le32(len2);
2723 buf[2] = cpu_to_le32(cladatum->value);
2724 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2725 if (cladatum->permissions.table)
2726 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2727 else
2728 buf[4] = 0;
2729 buf[5] = cpu_to_le32(ncons);
2730 rc = put_entry(buf, sizeof(u32), 6, fp);
2731 if (rc)
2732 return rc;
2734 rc = put_entry(key, 1, len, fp);
2735 if (rc)
2736 return rc;
2738 if (cladatum->comkey) {
2739 rc = put_entry(cladatum->comkey, 1, len2, fp);
2740 if (rc)
2741 return rc;
2744 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2745 if (rc)
2746 return rc;
2748 rc = write_cons_helper(p, cladatum->constraints, fp);
2749 if (rc)
2750 return rc;
2752 /* write out the validatetrans rule */
2753 ncons = 0;
2754 for (c = cladatum->validatetrans; c; c = c->next)
2755 ncons++;
2757 buf[0] = cpu_to_le32(ncons);
2758 rc = put_entry(buf, sizeof(u32), 1, fp);
2759 if (rc)
2760 return rc;
2762 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2763 if (rc)
2764 return rc;
2766 return 0;
2769 static int role_write(void *vkey, void *datum, void *ptr)
2771 char *key = vkey;
2772 struct role_datum *role = datum;
2773 struct policy_data *pd = ptr;
2774 void *fp = pd->fp;
2775 struct policydb *p = pd->p;
2776 __le32 buf[3];
2777 size_t items, len;
2778 int rc;
2780 len = strlen(key);
2781 items = 0;
2782 buf[items++] = cpu_to_le32(len);
2783 buf[items++] = cpu_to_le32(role->value);
2784 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2785 buf[items++] = cpu_to_le32(role->bounds);
2787 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2789 rc = put_entry(buf, sizeof(u32), items, fp);
2790 if (rc)
2791 return rc;
2793 rc = put_entry(key, 1, len, fp);
2794 if (rc)
2795 return rc;
2797 rc = ebitmap_write(&role->dominates, fp);
2798 if (rc)
2799 return rc;
2801 rc = ebitmap_write(&role->types, fp);
2802 if (rc)
2803 return rc;
2805 return 0;
2808 static int type_write(void *vkey, void *datum, void *ptr)
2810 char *key = vkey;
2811 struct type_datum *typdatum = datum;
2812 struct policy_data *pd = ptr;
2813 struct policydb *p = pd->p;
2814 void *fp = pd->fp;
2815 __le32 buf[4];
2816 int rc;
2817 size_t items, len;
2819 len = strlen(key);
2820 items = 0;
2821 buf[items++] = cpu_to_le32(len);
2822 buf[items++] = cpu_to_le32(typdatum->value);
2823 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2824 u32 properties = 0;
2826 if (typdatum->primary)
2827 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2829 if (typdatum->attribute)
2830 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2832 buf[items++] = cpu_to_le32(properties);
2833 buf[items++] = cpu_to_le32(typdatum->bounds);
2834 } else {
2835 buf[items++] = cpu_to_le32(typdatum->primary);
2837 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2838 rc = put_entry(buf, sizeof(u32), items, fp);
2839 if (rc)
2840 return rc;
2842 rc = put_entry(key, 1, len, fp);
2843 if (rc)
2844 return rc;
2846 return 0;
2849 static int user_write(void *vkey, void *datum, void *ptr)
2851 char *key = vkey;
2852 struct user_datum *usrdatum = datum;
2853 struct policy_data *pd = ptr;
2854 struct policydb *p = pd->p;
2855 void *fp = pd->fp;
2856 __le32 buf[3];
2857 size_t items, len;
2858 int rc;
2860 len = strlen(key);
2861 items = 0;
2862 buf[items++] = cpu_to_le32(len);
2863 buf[items++] = cpu_to_le32(usrdatum->value);
2864 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2865 buf[items++] = cpu_to_le32(usrdatum->bounds);
2866 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2867 rc = put_entry(buf, sizeof(u32), items, fp);
2868 if (rc)
2869 return rc;
2871 rc = put_entry(key, 1, len, fp);
2872 if (rc)
2873 return rc;
2875 rc = ebitmap_write(&usrdatum->roles, fp);
2876 if (rc)
2877 return rc;
2879 rc = mls_write_range_helper(&usrdatum->range, fp);
2880 if (rc)
2881 return rc;
2883 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2884 if (rc)
2885 return rc;
2887 return 0;
2890 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2891 void *datap) =
2893 common_write,
2894 class_write,
2895 role_write,
2896 type_write,
2897 user_write,
2898 cond_write_bool,
2899 sens_write,
2900 cat_write,
2903 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2904 void *fp)
2906 unsigned int i, j, rc;
2907 size_t nel, len;
2908 __le32 buf[3];
2909 u32 nodebuf[8];
2910 struct ocontext *c;
2911 for (i = 0; i < info->ocon_num; i++) {
2912 nel = 0;
2913 for (c = p->ocontexts[i]; c; c = c->next)
2914 nel++;
2915 buf[0] = cpu_to_le32(nel);
2916 rc = put_entry(buf, sizeof(u32), 1, fp);
2917 if (rc)
2918 return rc;
2919 for (c = p->ocontexts[i]; c; c = c->next) {
2920 switch (i) {
2921 case OCON_ISID:
2922 buf[0] = cpu_to_le32(c->sid[0]);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2924 if (rc)
2925 return rc;
2926 rc = context_write(p, &c->context[0], fp);
2927 if (rc)
2928 return rc;
2929 break;
2930 case OCON_FS:
2931 case OCON_NETIF:
2932 len = strlen(c->u.name);
2933 buf[0] = cpu_to_le32(len);
2934 rc = put_entry(buf, sizeof(u32), 1, fp);
2935 if (rc)
2936 return rc;
2937 rc = put_entry(c->u.name, 1, len, fp);
2938 if (rc)
2939 return rc;
2940 rc = context_write(p, &c->context[0], fp);
2941 if (rc)
2942 return rc;
2943 rc = context_write(p, &c->context[1], fp);
2944 if (rc)
2945 return rc;
2946 break;
2947 case OCON_PORT:
2948 buf[0] = cpu_to_le32(c->u.port.protocol);
2949 buf[1] = cpu_to_le32(c->u.port.low_port);
2950 buf[2] = cpu_to_le32(c->u.port.high_port);
2951 rc = put_entry(buf, sizeof(u32), 3, fp);
2952 if (rc)
2953 return rc;
2954 rc = context_write(p, &c->context[0], fp);
2955 if (rc)
2956 return rc;
2957 break;
2958 case OCON_NODE:
2959 nodebuf[0] = c->u.node.addr; /* network order */
2960 nodebuf[1] = c->u.node.mask; /* network order */
2961 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2962 if (rc)
2963 return rc;
2964 rc = context_write(p, &c->context[0], fp);
2965 if (rc)
2966 return rc;
2967 break;
2968 case OCON_FSUSE:
2969 buf[0] = cpu_to_le32(c->v.behavior);
2970 len = strlen(c->u.name);
2971 buf[1] = cpu_to_le32(len);
2972 rc = put_entry(buf, sizeof(u32), 2, fp);
2973 if (rc)
2974 return rc;
2975 rc = put_entry(c->u.name, 1, len, fp);
2976 if (rc)
2977 return rc;
2978 rc = context_write(p, &c->context[0], fp);
2979 if (rc)
2980 return rc;
2981 break;
2982 case OCON_NODE6:
2983 for (j = 0; j < 4; j++)
2984 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2985 for (j = 0; j < 4; j++)
2986 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2987 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2988 if (rc)
2989 return rc;
2990 rc = context_write(p, &c->context[0], fp);
2991 if (rc)
2992 return rc;
2993 break;
2997 return 0;
3000 static int genfs_write(struct policydb *p, void *fp)
3002 struct genfs *genfs;
3003 struct ocontext *c;
3004 size_t len;
3005 __le32 buf[1];
3006 int rc;
3008 len = 0;
3009 for (genfs = p->genfs; genfs; genfs = genfs->next)
3010 len++;
3011 buf[0] = cpu_to_le32(len);
3012 rc = put_entry(buf, sizeof(u32), 1, fp);
3013 if (rc)
3014 return rc;
3015 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3016 len = strlen(genfs->fstype);
3017 buf[0] = cpu_to_le32(len);
3018 rc = put_entry(buf, sizeof(u32), 1, fp);
3019 if (rc)
3020 return rc;
3021 rc = put_entry(genfs->fstype, 1, len, fp);
3022 if (rc)
3023 return rc;
3024 len = 0;
3025 for (c = genfs->head; c; c = c->next)
3026 len++;
3027 buf[0] = cpu_to_le32(len);
3028 rc = put_entry(buf, sizeof(u32), 1, fp);
3029 if (rc)
3030 return rc;
3031 for (c = genfs->head; c; c = c->next) {
3032 len = strlen(c->u.name);
3033 buf[0] = cpu_to_le32(len);
3034 rc = put_entry(buf, sizeof(u32), 1, fp);
3035 if (rc)
3036 return rc;
3037 rc = put_entry(c->u.name, 1, len, fp);
3038 if (rc)
3039 return rc;
3040 buf[0] = cpu_to_le32(c->v.sclass);
3041 rc = put_entry(buf, sizeof(u32), 1, fp);
3042 if (rc)
3043 return rc;
3044 rc = context_write(p, &c->context[0], fp);
3045 if (rc)
3046 return rc;
3049 return 0;
3052 static int range_count(void *key, void *data, void *ptr)
3054 int *cnt = ptr;
3055 *cnt = *cnt + 1;
3057 return 0;
3060 static int range_write_helper(void *key, void *data, void *ptr)
3062 __le32 buf[2];
3063 struct range_trans *rt = key;
3064 struct mls_range *r = data;
3065 struct policy_data *pd = ptr;
3066 void *fp = pd->fp;
3067 struct policydb *p = pd->p;
3068 int rc;
3070 buf[0] = cpu_to_le32(rt->source_type);
3071 buf[1] = cpu_to_le32(rt->target_type);
3072 rc = put_entry(buf, sizeof(u32), 2, fp);
3073 if (rc)
3074 return rc;
3075 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3076 buf[0] = cpu_to_le32(rt->target_class);
3077 rc = put_entry(buf, sizeof(u32), 1, fp);
3078 if (rc)
3079 return rc;
3081 rc = mls_write_range_helper(r, fp);
3082 if (rc)
3083 return rc;
3085 return 0;
3088 static int range_write(struct policydb *p, void *fp)
3090 size_t nel;
3091 __le32 buf[1];
3092 int rc;
3093 struct policy_data pd;
3095 pd.p = p;
3096 pd.fp = fp;
3098 /* count the number of entries in the hashtab */
3099 nel = 0;
3100 rc = hashtab_map(p->range_tr, range_count, &nel);
3101 if (rc)
3102 return rc;
3104 buf[0] = cpu_to_le32(nel);
3105 rc = put_entry(buf, sizeof(u32), 1, fp);
3106 if (rc)
3107 return rc;
3109 /* actually write all of the entries */
3110 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3111 if (rc)
3112 return rc;
3114 return 0;
3117 static int filename_trans_write(struct policydb *p, void *fp)
3119 struct filename_trans *ft;
3120 u32 len, nel = 0;
3121 __le32 buf[4];
3122 int rc;
3124 for (ft = p->filename_trans; ft; ft = ft->next)
3125 nel++;
3127 buf[0] = cpu_to_le32(nel);
3128 rc = put_entry(buf, sizeof(u32), 1, fp);
3129 if (rc)
3130 return rc;
3132 for (ft = p->filename_trans; ft; ft = ft->next) {
3133 len = strlen(ft->name);
3134 buf[0] = cpu_to_le32(len);
3135 rc = put_entry(buf, sizeof(u32), 1, fp);
3136 if (rc)
3137 return rc;
3139 rc = put_entry(ft->name, sizeof(char), len, fp);
3140 if (rc)
3141 return rc;
3143 buf[0] = ft->stype;
3144 buf[1] = ft->ttype;
3145 buf[2] = ft->tclass;
3146 buf[3] = ft->otype;
3148 rc = put_entry(buf, sizeof(u32), 4, fp);
3149 if (rc)
3150 return rc;
3152 return 0;
3155 * Write the configuration data in a policy database
3156 * structure to a policy database binary representation
3157 * file.
3159 int policydb_write(struct policydb *p, void *fp)
3161 unsigned int i, num_syms;
3162 int rc;
3163 __le32 buf[4];
3164 u32 config;
3165 size_t len;
3166 struct policydb_compat_info *info;
3169 * refuse to write policy older than compressed avtab
3170 * to simplify the writer. There are other tests dropped
3171 * since we assume this throughout the writer code. Be
3172 * careful if you ever try to remove this restriction
3174 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3175 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3176 " Because it is less than version %d\n", p->policyvers,
3177 POLICYDB_VERSION_AVTAB);
3178 return -EINVAL;
3181 config = 0;
3182 if (p->mls_enabled)
3183 config |= POLICYDB_CONFIG_MLS;
3185 if (p->reject_unknown)
3186 config |= REJECT_UNKNOWN;
3187 if (p->allow_unknown)
3188 config |= ALLOW_UNKNOWN;
3190 /* Write the magic number and string identifiers. */
3191 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3192 len = strlen(POLICYDB_STRING);
3193 buf[1] = cpu_to_le32(len);
3194 rc = put_entry(buf, sizeof(u32), 2, fp);
3195 if (rc)
3196 return rc;
3197 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3198 if (rc)
3199 return rc;
3201 /* Write the version, config, and table sizes. */
3202 info = policydb_lookup_compat(p->policyvers);
3203 if (!info) {
3204 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3205 "version %d", p->policyvers);
3206 return -EINVAL;
3209 buf[0] = cpu_to_le32(p->policyvers);
3210 buf[1] = cpu_to_le32(config);
3211 buf[2] = cpu_to_le32(info->sym_num);
3212 buf[3] = cpu_to_le32(info->ocon_num);
3214 rc = put_entry(buf, sizeof(u32), 4, fp);
3215 if (rc)
3216 return rc;
3218 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3219 rc = ebitmap_write(&p->policycaps, fp);
3220 if (rc)
3221 return rc;
3224 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3225 rc = ebitmap_write(&p->permissive_map, fp);
3226 if (rc)
3227 return rc;
3230 num_syms = info->sym_num;
3231 for (i = 0; i < num_syms; i++) {
3232 struct policy_data pd;
3234 pd.fp = fp;
3235 pd.p = p;
3237 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3238 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3240 rc = put_entry(buf, sizeof(u32), 2, fp);
3241 if (rc)
3242 return rc;
3243 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3244 if (rc)
3245 return rc;
3248 rc = avtab_write(p, &p->te_avtab, fp);
3249 if (rc)
3250 return rc;
3252 rc = cond_write_list(p, p->cond_list, fp);
3253 if (rc)
3254 return rc;
3256 rc = role_trans_write(p->role_tr, fp);
3257 if (rc)
3258 return rc;
3260 rc = role_allow_write(p->role_allow, fp);
3261 if (rc)
3262 return rc;
3264 rc = filename_trans_write(p, fp);
3265 if (rc)
3266 return rc;
3268 rc = ocontext_write(p, info, fp);
3269 if (rc)
3270 return rc;
3272 rc = genfs_write(p, fp);
3273 if (rc)
3274 return rc;
3276 rc = range_write(p, fp);
3277 if (rc)
3278 return rc;
3280 for (i = 0; i < p->p_types.nprim; i++) {
3281 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3283 BUG_ON(!e);
3284 rc = ebitmap_write(e, fp);
3285 if (rc)
3286 return rc;
3289 return 0;