selinux: return -ENOMEM when memory allocation fails
[linux-2.6/cjktty.git] / security / selinux / ss / policydb.c
blob57363562f0f886a455e900fdf59a56417b379127
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,
128 static struct policydb_compat_info *policydb_lookup_compat(int version)
130 int i;
131 struct policydb_compat_info *info = NULL;
133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
134 if (policydb_compat[i].version == version) {
135 info = &policydb_compat[i];
136 break;
139 return info;
143 * Initialize the role table.
145 static int roles_init(struct policydb *p)
147 char *key = NULL;
148 int rc;
149 struct role_datum *role;
151 rc = -ENOMEM;
152 role = kzalloc(sizeof(*role), GFP_KERNEL);
153 if (!role)
154 goto out;
156 rc = -EINVAL;
157 role->value = ++p->p_roles.nprim;
158 if (role->value != OBJECT_R_VAL)
159 goto out;
161 rc = -ENOMEM;
162 key = kstrdup(OBJECT_R, GFP_KERNEL);
163 if (!key)
164 goto out;
166 rc = hashtab_insert(p->p_roles.table, key, role);
167 if (rc)
168 goto out;
170 return 0;
171 out:
172 kfree(key);
173 kfree(role);
174 return rc;
177 static u32 rangetr_hash(struct hashtab *h, const void *k)
179 const struct range_trans *key = k;
180 return (key->source_type + (key->target_type << 3) +
181 (key->target_class << 5)) & (h->size - 1);
184 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
186 const struct range_trans *key1 = k1, *key2 = k2;
187 int v;
189 v = key1->source_type - key2->source_type;
190 if (v)
191 return v;
193 v = key1->target_type - key2->target_type;
194 if (v)
195 return v;
197 v = key1->target_class - key2->target_class;
199 return v;
203 * Initialize a policy database structure.
205 static int policydb_init(struct policydb *p)
207 int i, rc;
209 memset(p, 0, sizeof(*p));
211 for (i = 0; i < SYM_NUM; i++) {
212 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
213 if (rc)
214 goto out;
217 rc = avtab_init(&p->te_avtab);
218 if (rc)
219 goto out;
221 rc = roles_init(p);
222 if (rc)
223 goto out;
225 rc = cond_policydb_init(p);
226 if (rc)
227 goto out;
229 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
230 if (!p->range_tr)
231 goto out;
233 ebitmap_init(&p->policycaps);
234 ebitmap_init(&p->permissive_map);
236 return 0;
237 out:
238 for (i = 0; i < SYM_NUM; i++)
239 hashtab_destroy(p->symtab[i].table);
240 return rc;
244 * The following *_index functions are used to
245 * define the val_to_name and val_to_struct arrays
246 * in a policy database structure. The val_to_name
247 * arrays are used when converting security context
248 * structures into string representations. The
249 * val_to_struct arrays are used when the attributes
250 * of a class, role, or user are needed.
253 static int common_index(void *key, void *datum, void *datap)
255 struct policydb *p;
256 struct common_datum *comdatum;
257 struct flex_array *fa;
259 comdatum = datum;
260 p = datap;
261 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
262 return -EINVAL;
264 fa = p->sym_val_to_name[SYM_COMMONS];
265 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
266 GFP_KERNEL | __GFP_ZERO))
267 BUG();
268 return 0;
271 static int class_index(void *key, void *datum, void *datap)
273 struct policydb *p;
274 struct class_datum *cladatum;
275 struct flex_array *fa;
277 cladatum = datum;
278 p = datap;
279 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
280 return -EINVAL;
281 fa = p->sym_val_to_name[SYM_CLASSES];
282 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
283 GFP_KERNEL | __GFP_ZERO))
284 BUG();
285 p->class_val_to_struct[cladatum->value - 1] = cladatum;
286 return 0;
289 static int role_index(void *key, void *datum, void *datap)
291 struct policydb *p;
292 struct role_datum *role;
293 struct flex_array *fa;
295 role = datum;
296 p = datap;
297 if (!role->value
298 || role->value > p->p_roles.nprim
299 || role->bounds > p->p_roles.nprim)
300 return -EINVAL;
302 fa = p->sym_val_to_name[SYM_ROLES];
303 if (flex_array_put_ptr(fa, role->value - 1, key,
304 GFP_KERNEL | __GFP_ZERO))
305 BUG();
306 p->role_val_to_struct[role->value - 1] = role;
307 return 0;
310 static int type_index(void *key, void *datum, void *datap)
312 struct policydb *p;
313 struct type_datum *typdatum;
314 struct flex_array *fa;
316 typdatum = datum;
317 p = datap;
319 if (typdatum->primary) {
320 if (!typdatum->value
321 || typdatum->value > p->p_types.nprim
322 || typdatum->bounds > p->p_types.nprim)
323 return -EINVAL;
324 fa = p->sym_val_to_name[SYM_TYPES];
325 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
326 GFP_KERNEL | __GFP_ZERO))
327 BUG();
329 fa = p->type_val_to_struct_array;
330 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
331 GFP_KERNEL | __GFP_ZERO))
332 BUG();
335 return 0;
338 static int user_index(void *key, void *datum, void *datap)
340 struct policydb *p;
341 struct user_datum *usrdatum;
342 struct flex_array *fa;
344 usrdatum = datum;
345 p = datap;
346 if (!usrdatum->value
347 || usrdatum->value > p->p_users.nprim
348 || usrdatum->bounds > p->p_users.nprim)
349 return -EINVAL;
351 fa = p->sym_val_to_name[SYM_USERS];
352 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
353 GFP_KERNEL | __GFP_ZERO))
354 BUG();
355 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
356 return 0;
359 static int sens_index(void *key, void *datum, void *datap)
361 struct policydb *p;
362 struct level_datum *levdatum;
363 struct flex_array *fa;
365 levdatum = datum;
366 p = datap;
368 if (!levdatum->isalias) {
369 if (!levdatum->level->sens ||
370 levdatum->level->sens > p->p_levels.nprim)
371 return -EINVAL;
372 fa = p->sym_val_to_name[SYM_LEVELS];
373 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
374 GFP_KERNEL | __GFP_ZERO))
375 BUG();
378 return 0;
381 static int cat_index(void *key, void *datum, void *datap)
383 struct policydb *p;
384 struct cat_datum *catdatum;
385 struct flex_array *fa;
387 catdatum = datum;
388 p = datap;
390 if (!catdatum->isalias) {
391 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
392 return -EINVAL;
393 fa = p->sym_val_to_name[SYM_CATS];
394 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
396 BUG();
399 return 0;
402 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
404 common_index,
405 class_index,
406 role_index,
407 type_index,
408 user_index,
409 cond_index_bool,
410 sens_index,
411 cat_index,
414 #ifdef DEBUG_HASHES
415 static void symtab_hash_eval(struct symtab *s)
417 int i;
419 for (i = 0; i < SYM_NUM; i++) {
420 struct hashtab *h = s[i].table;
421 struct hashtab_info info;
423 hashtab_stat(h, &info);
424 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
425 "longest chain length %d\n", symtab_name[i], h->nel,
426 info.slots_used, h->size, info.max_chain_len);
430 static void rangetr_hash_eval(struct hashtab *h)
432 struct hashtab_info info;
434 hashtab_stat(h, &info);
435 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
436 "longest chain length %d\n", h->nel,
437 info.slots_used, h->size, info.max_chain_len);
439 #else
440 static inline void rangetr_hash_eval(struct hashtab *h)
443 #endif
446 * Define the other val_to_name and val_to_struct arrays
447 * in a policy database structure.
449 * Caller must clean up on failure.
451 static int policydb_index(struct policydb *p)
453 int i, rc;
455 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
456 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
457 if (p->mls_enabled)
458 printk(", %d sens, %d cats", p->p_levels.nprim,
459 p->p_cats.nprim);
460 printk("\n");
462 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
463 p->p_classes.nprim, p->te_avtab.nel);
465 #ifdef DEBUG_HASHES
466 avtab_hash_eval(&p->te_avtab, "rules");
467 symtab_hash_eval(p->symtab);
468 #endif
470 rc = -ENOMEM;
471 p->class_val_to_struct =
472 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
473 GFP_KERNEL);
474 if (!p->class_val_to_struct)
475 goto out;
477 rc = -ENOMEM;
478 p->role_val_to_struct =
479 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
480 GFP_KERNEL);
481 if (!p->role_val_to_struct)
482 goto out;
484 rc = -ENOMEM;
485 p->user_val_to_struct =
486 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
487 GFP_KERNEL);
488 if (!p->user_val_to_struct)
489 goto out;
491 /* Yes, I want the sizeof the pointer, not the structure */
492 rc = -ENOMEM;
493 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
494 p->p_types.nprim,
495 GFP_KERNEL | __GFP_ZERO);
496 if (!p->type_val_to_struct_array)
497 goto out;
499 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
500 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
501 if (rc)
502 goto out;
504 rc = cond_init_bool_indexes(p);
505 if (rc)
506 goto out;
508 for (i = 0; i < SYM_NUM; i++) {
509 rc = -ENOMEM;
510 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
511 p->symtab[i].nprim,
512 GFP_KERNEL | __GFP_ZERO);
513 if (!p->sym_val_to_name[i])
514 goto out;
516 rc = flex_array_prealloc(p->sym_val_to_name[i],
517 0, p->symtab[i].nprim - 1,
518 GFP_KERNEL | __GFP_ZERO);
519 if (rc)
520 goto out;
522 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
523 if (rc)
524 goto out;
526 rc = 0;
527 out:
528 return rc;
532 * The following *_destroy functions are used to
533 * free any memory allocated for each kind of
534 * symbol data in the policy database.
537 static int perm_destroy(void *key, void *datum, void *p)
539 kfree(key);
540 kfree(datum);
541 return 0;
544 static int common_destroy(void *key, void *datum, void *p)
546 struct common_datum *comdatum;
548 kfree(key);
549 if (datum) {
550 comdatum = datum;
551 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
552 hashtab_destroy(comdatum->permissions.table);
554 kfree(datum);
555 return 0;
558 static int cls_destroy(void *key, void *datum, void *p)
560 struct class_datum *cladatum;
561 struct constraint_node *constraint, *ctemp;
562 struct constraint_expr *e, *etmp;
564 kfree(key);
565 if (datum) {
566 cladatum = datum;
567 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
568 hashtab_destroy(cladatum->permissions.table);
569 constraint = cladatum->constraints;
570 while (constraint) {
571 e = constraint->expr;
572 while (e) {
573 ebitmap_destroy(&e->names);
574 etmp = e;
575 e = e->next;
576 kfree(etmp);
578 ctemp = constraint;
579 constraint = constraint->next;
580 kfree(ctemp);
583 constraint = cladatum->validatetrans;
584 while (constraint) {
585 e = constraint->expr;
586 while (e) {
587 ebitmap_destroy(&e->names);
588 etmp = e;
589 e = e->next;
590 kfree(etmp);
592 ctemp = constraint;
593 constraint = constraint->next;
594 kfree(ctemp);
597 kfree(cladatum->comkey);
599 kfree(datum);
600 return 0;
603 static int role_destroy(void *key, void *datum, void *p)
605 struct role_datum *role;
607 kfree(key);
608 if (datum) {
609 role = datum;
610 ebitmap_destroy(&role->dominates);
611 ebitmap_destroy(&role->types);
613 kfree(datum);
614 return 0;
617 static int type_destroy(void *key, void *datum, void *p)
619 kfree(key);
620 kfree(datum);
621 return 0;
624 static int user_destroy(void *key, void *datum, void *p)
626 struct user_datum *usrdatum;
628 kfree(key);
629 if (datum) {
630 usrdatum = datum;
631 ebitmap_destroy(&usrdatum->roles);
632 ebitmap_destroy(&usrdatum->range.level[0].cat);
633 ebitmap_destroy(&usrdatum->range.level[1].cat);
634 ebitmap_destroy(&usrdatum->dfltlevel.cat);
636 kfree(datum);
637 return 0;
640 static int sens_destroy(void *key, void *datum, void *p)
642 struct level_datum *levdatum;
644 kfree(key);
645 if (datum) {
646 levdatum = datum;
647 ebitmap_destroy(&levdatum->level->cat);
648 kfree(levdatum->level);
650 kfree(datum);
651 return 0;
654 static int cat_destroy(void *key, void *datum, void *p)
656 kfree(key);
657 kfree(datum);
658 return 0;
661 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
663 common_destroy,
664 cls_destroy,
665 role_destroy,
666 type_destroy,
667 user_destroy,
668 cond_destroy_bool,
669 sens_destroy,
670 cat_destroy,
673 static int range_tr_destroy(void *key, void *datum, void *p)
675 struct mls_range *rt = datum;
676 kfree(key);
677 ebitmap_destroy(&rt->level[0].cat);
678 ebitmap_destroy(&rt->level[1].cat);
679 kfree(datum);
680 cond_resched();
681 return 0;
684 static void ocontext_destroy(struct ocontext *c, int i)
686 if (!c)
687 return;
689 context_destroy(&c->context[0]);
690 context_destroy(&c->context[1]);
691 if (i == OCON_ISID || i == OCON_FS ||
692 i == OCON_NETIF || i == OCON_FSUSE)
693 kfree(c->u.name);
694 kfree(c);
698 * Free any memory allocated by a policy database structure.
700 void policydb_destroy(struct policydb *p)
702 struct ocontext *c, *ctmp;
703 struct genfs *g, *gtmp;
704 int i;
705 struct role_allow *ra, *lra = NULL;
706 struct role_trans *tr, *ltr = NULL;
708 for (i = 0; i < SYM_NUM; i++) {
709 cond_resched();
710 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
711 hashtab_destroy(p->symtab[i].table);
714 for (i = 0; i < SYM_NUM; i++) {
715 if (p->sym_val_to_name[i])
716 flex_array_free(p->sym_val_to_name[i]);
719 kfree(p->class_val_to_struct);
720 kfree(p->role_val_to_struct);
721 kfree(p->user_val_to_struct);
722 if (p->type_val_to_struct_array)
723 flex_array_free(p->type_val_to_struct_array);
725 avtab_destroy(&p->te_avtab);
727 for (i = 0; i < OCON_NUM; i++) {
728 cond_resched();
729 c = p->ocontexts[i];
730 while (c) {
731 ctmp = c;
732 c = c->next;
733 ocontext_destroy(ctmp, i);
735 p->ocontexts[i] = NULL;
738 g = p->genfs;
739 while (g) {
740 cond_resched();
741 kfree(g->fstype);
742 c = g->head;
743 while (c) {
744 ctmp = c;
745 c = c->next;
746 ocontext_destroy(ctmp, OCON_FSUSE);
748 gtmp = g;
749 g = g->next;
750 kfree(gtmp);
752 p->genfs = NULL;
754 cond_policydb_destroy(p);
756 for (tr = p->role_tr; tr; tr = tr->next) {
757 cond_resched();
758 kfree(ltr);
759 ltr = tr;
761 kfree(ltr);
763 for (ra = p->role_allow; ra; ra = ra->next) {
764 cond_resched();
765 kfree(lra);
766 lra = ra;
768 kfree(lra);
770 hashtab_map(p->range_tr, range_tr_destroy, NULL);
771 hashtab_destroy(p->range_tr);
773 if (p->type_attr_map_array) {
774 for (i = 0; i < p->p_types.nprim; i++) {
775 struct ebitmap *e;
777 e = flex_array_get(p->type_attr_map_array, i);
778 if (!e)
779 continue;
780 ebitmap_destroy(e);
782 flex_array_free(p->type_attr_map_array);
784 ebitmap_destroy(&p->policycaps);
785 ebitmap_destroy(&p->permissive_map);
787 return;
791 * Load the initial SIDs specified in a policy database
792 * structure into a SID table.
794 int policydb_load_isids(struct policydb *p, struct sidtab *s)
796 struct ocontext *head, *c;
797 int rc;
799 rc = sidtab_init(s);
800 if (rc) {
801 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
802 goto out;
805 head = p->ocontexts[OCON_ISID];
806 for (c = head; c; c = c->next) {
807 rc = -EINVAL;
808 if (!c->context[0].user) {
809 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
810 c->u.name);
811 goto out;
814 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
815 if (rc) {
816 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
817 c->u.name);
818 goto out;
821 rc = 0;
822 out:
823 return rc;
826 int policydb_class_isvalid(struct policydb *p, unsigned int class)
828 if (!class || class > p->p_classes.nprim)
829 return 0;
830 return 1;
833 int policydb_role_isvalid(struct policydb *p, unsigned int role)
835 if (!role || role > p->p_roles.nprim)
836 return 0;
837 return 1;
840 int policydb_type_isvalid(struct policydb *p, unsigned int type)
842 if (!type || type > p->p_types.nprim)
843 return 0;
844 return 1;
848 * Return 1 if the fields in the security context
849 * structure `c' are valid. Return 0 otherwise.
851 int policydb_context_isvalid(struct policydb *p, struct context *c)
853 struct role_datum *role;
854 struct user_datum *usrdatum;
856 if (!c->role || c->role > p->p_roles.nprim)
857 return 0;
859 if (!c->user || c->user > p->p_users.nprim)
860 return 0;
862 if (!c->type || c->type > p->p_types.nprim)
863 return 0;
865 if (c->role != OBJECT_R_VAL) {
867 * Role must be authorized for the type.
869 role = p->role_val_to_struct[c->role - 1];
870 if (!ebitmap_get_bit(&role->types, c->type - 1))
871 /* role may not be associated with type */
872 return 0;
875 * User must be authorized for the role.
877 usrdatum = p->user_val_to_struct[c->user - 1];
878 if (!usrdatum)
879 return 0;
881 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
882 /* user may not be associated with role */
883 return 0;
886 if (!mls_context_isvalid(p, c))
887 return 0;
889 return 1;
893 * Read a MLS range structure from a policydb binary
894 * representation file.
896 static int mls_read_range_helper(struct mls_range *r, void *fp)
898 __le32 buf[2];
899 u32 items;
900 int rc;
902 rc = next_entry(buf, fp, sizeof(u32));
903 if (rc)
904 goto out;
906 rc = -EINVAL;
907 items = le32_to_cpu(buf[0]);
908 if (items > ARRAY_SIZE(buf)) {
909 printk(KERN_ERR "SELinux: mls: range overflow\n");
910 goto out;
913 rc = next_entry(buf, fp, sizeof(u32) * items);
914 if (rc) {
915 printk(KERN_ERR "SELinux: mls: truncated range\n");
916 goto out;
919 r->level[0].sens = le32_to_cpu(buf[0]);
920 if (items > 1)
921 r->level[1].sens = le32_to_cpu(buf[1]);
922 else
923 r->level[1].sens = r->level[0].sens;
925 rc = ebitmap_read(&r->level[0].cat, fp);
926 if (rc) {
927 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
928 goto out;
930 if (items > 1) {
931 rc = ebitmap_read(&r->level[1].cat, fp);
932 if (rc) {
933 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
934 goto bad_high;
936 } else {
937 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
938 if (rc) {
939 printk(KERN_ERR "SELinux: mls: out of memory\n");
940 goto bad_high;
944 return 0;
945 bad_high:
946 ebitmap_destroy(&r->level[0].cat);
947 out:
948 return rc;
952 * Read and validate a security context structure
953 * from a policydb binary representation file.
955 static int context_read_and_validate(struct context *c,
956 struct policydb *p,
957 void *fp)
959 __le32 buf[3];
960 int rc;
962 rc = next_entry(buf, fp, sizeof buf);
963 if (rc) {
964 printk(KERN_ERR "SELinux: context truncated\n");
965 goto out;
967 c->user = le32_to_cpu(buf[0]);
968 c->role = le32_to_cpu(buf[1]);
969 c->type = le32_to_cpu(buf[2]);
970 if (p->policyvers >= POLICYDB_VERSION_MLS) {
971 rc = mls_read_range_helper(&c->range, fp);
972 if (rc) {
973 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
974 goto out;
978 rc = -EINVAL;
979 if (!policydb_context_isvalid(p, c)) {
980 printk(KERN_ERR "SELinux: invalid security context\n");
981 context_destroy(c);
982 goto out;
984 rc = 0;
985 out:
986 return rc;
990 * The following *_read functions are used to
991 * read the symbol data from a policy database
992 * binary representation file.
995 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
997 char *key = NULL;
998 struct perm_datum *perdatum;
999 int rc;
1000 __le32 buf[2];
1001 u32 len;
1003 rc = -ENOMEM;
1004 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
1005 if (!perdatum)
1006 goto bad;
1008 rc = next_entry(buf, fp, sizeof buf);
1009 if (rc)
1010 goto bad;
1012 len = le32_to_cpu(buf[0]);
1013 perdatum->value = le32_to_cpu(buf[1]);
1015 rc = -ENOMEM;
1016 key = kmalloc(len + 1, GFP_KERNEL);
1017 if (!key)
1018 goto bad;
1020 rc = next_entry(key, fp, len);
1021 if (rc)
1022 goto bad;
1023 key[len] = '\0';
1025 rc = hashtab_insert(h, key, perdatum);
1026 if (rc)
1027 goto bad;
1029 return 0;
1030 bad:
1031 perm_destroy(key, perdatum, NULL);
1032 return rc;
1035 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1037 char *key = NULL;
1038 struct common_datum *comdatum;
1039 __le32 buf[4];
1040 u32 len, nel;
1041 int i, rc;
1043 rc = -ENOMEM;
1044 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1045 if (!comdatum)
1046 goto bad;
1048 rc = next_entry(buf, fp, sizeof buf);
1049 if (rc)
1050 goto bad;
1052 len = le32_to_cpu(buf[0]);
1053 comdatum->value = le32_to_cpu(buf[1]);
1055 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1056 if (rc)
1057 goto bad;
1058 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1059 nel = le32_to_cpu(buf[3]);
1061 rc = -ENOMEM;
1062 key = kmalloc(len + 1, GFP_KERNEL);
1063 if (!key)
1064 goto bad;
1066 rc = next_entry(key, fp, len);
1067 if (rc)
1068 goto bad;
1069 key[len] = '\0';
1071 for (i = 0; i < nel; i++) {
1072 rc = perm_read(p, comdatum->permissions.table, fp);
1073 if (rc)
1074 goto bad;
1077 rc = hashtab_insert(h, key, comdatum);
1078 if (rc)
1079 goto bad;
1080 return 0;
1081 bad:
1082 common_destroy(key, comdatum, NULL);
1083 return rc;
1086 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1087 int allowxtarget, void *fp)
1089 struct constraint_node *c, *lc;
1090 struct constraint_expr *e, *le;
1091 __le32 buf[3];
1092 u32 nexpr;
1093 int rc, i, j, depth;
1095 lc = NULL;
1096 for (i = 0; i < ncons; i++) {
1097 c = kzalloc(sizeof(*c), GFP_KERNEL);
1098 if (!c)
1099 return -ENOMEM;
1101 if (lc)
1102 lc->next = c;
1103 else
1104 *nodep = c;
1106 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1107 if (rc)
1108 return rc;
1109 c->permissions = le32_to_cpu(buf[0]);
1110 nexpr = le32_to_cpu(buf[1]);
1111 le = NULL;
1112 depth = -1;
1113 for (j = 0; j < nexpr; j++) {
1114 e = kzalloc(sizeof(*e), GFP_KERNEL);
1115 if (!e)
1116 return -ENOMEM;
1118 if (le)
1119 le->next = e;
1120 else
1121 c->expr = e;
1123 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1124 if (rc)
1125 return rc;
1126 e->expr_type = le32_to_cpu(buf[0]);
1127 e->attr = le32_to_cpu(buf[1]);
1128 e->op = le32_to_cpu(buf[2]);
1130 switch (e->expr_type) {
1131 case CEXPR_NOT:
1132 if (depth < 0)
1133 return -EINVAL;
1134 break;
1135 case CEXPR_AND:
1136 case CEXPR_OR:
1137 if (depth < 1)
1138 return -EINVAL;
1139 depth--;
1140 break;
1141 case CEXPR_ATTR:
1142 if (depth == (CEXPR_MAXDEPTH - 1))
1143 return -EINVAL;
1144 depth++;
1145 break;
1146 case CEXPR_NAMES:
1147 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1148 return -EINVAL;
1149 if (depth == (CEXPR_MAXDEPTH - 1))
1150 return -EINVAL;
1151 depth++;
1152 rc = ebitmap_read(&e->names, fp);
1153 if (rc)
1154 return rc;
1155 break;
1156 default:
1157 return -EINVAL;
1159 le = e;
1161 if (depth != 0)
1162 return -EINVAL;
1163 lc = c;
1166 return 0;
1169 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1171 char *key = NULL;
1172 struct class_datum *cladatum;
1173 __le32 buf[6];
1174 u32 len, len2, ncons, nel;
1175 int i, rc;
1177 rc = -ENOMEM;
1178 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1179 if (!cladatum)
1180 goto bad;
1182 rc = next_entry(buf, fp, sizeof(u32)*6);
1183 if (rc)
1184 goto bad;
1186 len = le32_to_cpu(buf[0]);
1187 len2 = le32_to_cpu(buf[1]);
1188 cladatum->value = le32_to_cpu(buf[2]);
1190 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1191 if (rc)
1192 goto bad;
1193 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1194 nel = le32_to_cpu(buf[4]);
1196 ncons = le32_to_cpu(buf[5]);
1198 rc = -ENOMEM;
1199 key = kmalloc(len + 1, GFP_KERNEL);
1200 if (!key)
1201 goto bad;
1203 rc = next_entry(key, fp, len);
1204 if (rc)
1205 goto bad;
1206 key[len] = '\0';
1208 if (len2) {
1209 rc = -ENOMEM;
1210 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1211 if (!cladatum->comkey)
1212 goto bad;
1213 rc = next_entry(cladatum->comkey, fp, len2);
1214 if (rc)
1215 goto bad;
1216 cladatum->comkey[len2] = '\0';
1218 rc = -EINVAL;
1219 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1220 if (!cladatum->comdatum) {
1221 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1222 goto bad;
1225 for (i = 0; i < nel; i++) {
1226 rc = perm_read(p, cladatum->permissions.table, fp);
1227 if (rc)
1228 goto bad;
1231 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1232 if (rc)
1233 goto bad;
1235 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1236 /* grab the validatetrans rules */
1237 rc = next_entry(buf, fp, sizeof(u32));
1238 if (rc)
1239 goto bad;
1240 ncons = le32_to_cpu(buf[0]);
1241 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1242 if (rc)
1243 goto bad;
1246 rc = hashtab_insert(h, key, cladatum);
1247 if (rc)
1248 goto bad;
1250 return 0;
1251 bad:
1252 cls_destroy(key, cladatum, NULL);
1253 return rc;
1256 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1258 char *key = NULL;
1259 struct role_datum *role;
1260 int rc, to_read = 2;
1261 __le32 buf[3];
1262 u32 len;
1264 rc = -ENOMEM;
1265 role = kzalloc(sizeof(*role), GFP_KERNEL);
1266 if (!role)
1267 goto bad;
1269 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1270 to_read = 3;
1272 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1273 if (rc)
1274 goto bad;
1276 len = le32_to_cpu(buf[0]);
1277 role->value = le32_to_cpu(buf[1]);
1278 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1279 role->bounds = le32_to_cpu(buf[2]);
1281 rc = -ENOMEM;
1282 key = kmalloc(len + 1, GFP_KERNEL);
1283 if (!key)
1284 goto bad;
1286 rc = next_entry(key, fp, len);
1287 if (rc)
1288 goto bad;
1289 key[len] = '\0';
1291 rc = ebitmap_read(&role->dominates, fp);
1292 if (rc)
1293 goto bad;
1295 rc = ebitmap_read(&role->types, fp);
1296 if (rc)
1297 goto bad;
1299 if (strcmp(key, OBJECT_R) == 0) {
1300 rc = -EINVAL;
1301 if (role->value != OBJECT_R_VAL) {
1302 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1303 OBJECT_R, role->value);
1304 goto bad;
1306 rc = 0;
1307 goto bad;
1310 rc = hashtab_insert(h, key, role);
1311 if (rc)
1312 goto bad;
1313 return 0;
1314 bad:
1315 role_destroy(key, role, NULL);
1316 return rc;
1319 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1321 char *key = NULL;
1322 struct type_datum *typdatum;
1323 int rc, to_read = 3;
1324 __le32 buf[4];
1325 u32 len;
1327 rc = -ENOMEM;
1328 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1329 if (!typdatum)
1330 goto bad;
1332 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1333 to_read = 4;
1335 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1336 if (rc)
1337 goto bad;
1339 len = le32_to_cpu(buf[0]);
1340 typdatum->value = le32_to_cpu(buf[1]);
1341 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1342 u32 prop = le32_to_cpu(buf[2]);
1344 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1345 typdatum->primary = 1;
1346 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1347 typdatum->attribute = 1;
1349 typdatum->bounds = le32_to_cpu(buf[3]);
1350 } else {
1351 typdatum->primary = le32_to_cpu(buf[2]);
1354 rc = -ENOMEM;
1355 key = kmalloc(len + 1, GFP_KERNEL);
1356 if (!key)
1357 goto bad;
1358 rc = next_entry(key, fp, len);
1359 if (rc)
1360 goto bad;
1361 key[len] = '\0';
1363 rc = hashtab_insert(h, key, typdatum);
1364 if (rc)
1365 goto bad;
1366 return 0;
1367 bad:
1368 type_destroy(key, typdatum, NULL);
1369 return rc;
1374 * Read a MLS level structure from a policydb binary
1375 * representation file.
1377 static int mls_read_level(struct mls_level *lp, void *fp)
1379 __le32 buf[1];
1380 int rc;
1382 memset(lp, 0, sizeof(*lp));
1384 rc = next_entry(buf, fp, sizeof buf);
1385 if (rc) {
1386 printk(KERN_ERR "SELinux: mls: truncated level\n");
1387 return rc;
1389 lp->sens = le32_to_cpu(buf[0]);
1391 rc = ebitmap_read(&lp->cat, fp);
1392 if (rc) {
1393 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1394 return rc;
1396 return 0;
1399 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1401 char *key = NULL;
1402 struct user_datum *usrdatum;
1403 int rc, to_read = 2;
1404 __le32 buf[3];
1405 u32 len;
1407 rc = -ENOMEM;
1408 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1409 if (!usrdatum)
1410 goto bad;
1412 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1413 to_read = 3;
1415 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1416 if (rc)
1417 goto bad;
1419 len = le32_to_cpu(buf[0]);
1420 usrdatum->value = le32_to_cpu(buf[1]);
1421 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1422 usrdatum->bounds = le32_to_cpu(buf[2]);
1424 rc = -ENOMEM;
1425 key = kmalloc(len + 1, GFP_KERNEL);
1426 if (!key)
1427 goto bad;
1428 rc = next_entry(key, fp, len);
1429 if (rc)
1430 goto bad;
1431 key[len] = '\0';
1433 rc = ebitmap_read(&usrdatum->roles, fp);
1434 if (rc)
1435 goto bad;
1437 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1438 rc = mls_read_range_helper(&usrdatum->range, fp);
1439 if (rc)
1440 goto bad;
1441 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1442 if (rc)
1443 goto bad;
1446 rc = hashtab_insert(h, key, usrdatum);
1447 if (rc)
1448 goto bad;
1449 return 0;
1450 bad:
1451 user_destroy(key, usrdatum, NULL);
1452 return rc;
1455 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1457 char *key = NULL;
1458 struct level_datum *levdatum;
1459 int rc;
1460 __le32 buf[2];
1461 u32 len;
1463 rc = -ENOMEM;
1464 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1465 if (!levdatum)
1466 goto bad;
1468 rc = next_entry(buf, fp, sizeof buf);
1469 if (rc)
1470 goto bad;
1472 len = le32_to_cpu(buf[0]);
1473 levdatum->isalias = le32_to_cpu(buf[1]);
1475 rc = -ENOMEM;
1476 key = kmalloc(len + 1, GFP_ATOMIC);
1477 if (!key)
1478 goto bad;
1479 rc = next_entry(key, fp, len);
1480 if (rc)
1481 goto bad;
1482 key[len] = '\0';
1484 rc = -ENOMEM;
1485 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1486 if (!levdatum->level)
1487 goto bad;
1489 rc = mls_read_level(levdatum->level, fp);
1490 if (rc)
1491 goto bad;
1493 rc = hashtab_insert(h, key, levdatum);
1494 if (rc)
1495 goto bad;
1496 return 0;
1497 bad:
1498 sens_destroy(key, levdatum, NULL);
1499 return rc;
1502 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1504 char *key = NULL;
1505 struct cat_datum *catdatum;
1506 int rc;
1507 __le32 buf[3];
1508 u32 len;
1510 rc = -ENOMEM;
1511 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1512 if (!catdatum)
1513 goto bad;
1515 rc = next_entry(buf, fp, sizeof buf);
1516 if (rc)
1517 goto bad;
1519 len = le32_to_cpu(buf[0]);
1520 catdatum->value = le32_to_cpu(buf[1]);
1521 catdatum->isalias = le32_to_cpu(buf[2]);
1523 rc = -ENOMEM;
1524 key = kmalloc(len + 1, GFP_ATOMIC);
1525 if (!key)
1526 goto bad;
1527 rc = next_entry(key, fp, len);
1528 if (rc)
1529 goto bad;
1530 key[len] = '\0';
1532 rc = hashtab_insert(h, key, catdatum);
1533 if (rc)
1534 goto bad;
1535 return 0;
1536 bad:
1537 cat_destroy(key, catdatum, NULL);
1538 return rc;
1541 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1543 common_read,
1544 class_read,
1545 role_read,
1546 type_read,
1547 user_read,
1548 cond_read_bool,
1549 sens_read,
1550 cat_read,
1553 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1555 struct user_datum *upper, *user;
1556 struct policydb *p = datap;
1557 int depth = 0;
1559 upper = user = datum;
1560 while (upper->bounds) {
1561 struct ebitmap_node *node;
1562 unsigned long bit;
1564 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1565 printk(KERN_ERR "SELinux: user %s: "
1566 "too deep or looped boundary",
1567 (char *) key);
1568 return -EINVAL;
1571 upper = p->user_val_to_struct[upper->bounds - 1];
1572 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1573 if (ebitmap_get_bit(&upper->roles, bit))
1574 continue;
1576 printk(KERN_ERR
1577 "SELinux: boundary violated policy: "
1578 "user=%s role=%s bounds=%s\n",
1579 sym_name(p, SYM_USERS, user->value - 1),
1580 sym_name(p, SYM_ROLES, bit),
1581 sym_name(p, SYM_USERS, upper->value - 1));
1583 return -EINVAL;
1587 return 0;
1590 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1592 struct role_datum *upper, *role;
1593 struct policydb *p = datap;
1594 int depth = 0;
1596 upper = role = datum;
1597 while (upper->bounds) {
1598 struct ebitmap_node *node;
1599 unsigned long bit;
1601 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1602 printk(KERN_ERR "SELinux: role %s: "
1603 "too deep or looped bounds\n",
1604 (char *) key);
1605 return -EINVAL;
1608 upper = p->role_val_to_struct[upper->bounds - 1];
1609 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1610 if (ebitmap_get_bit(&upper->types, bit))
1611 continue;
1613 printk(KERN_ERR
1614 "SELinux: boundary violated policy: "
1615 "role=%s type=%s bounds=%s\n",
1616 sym_name(p, SYM_ROLES, role->value - 1),
1617 sym_name(p, SYM_TYPES, bit),
1618 sym_name(p, SYM_ROLES, upper->value - 1));
1620 return -EINVAL;
1624 return 0;
1627 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1629 struct type_datum *upper;
1630 struct policydb *p = datap;
1631 int depth = 0;
1633 upper = datum;
1634 while (upper->bounds) {
1635 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1636 printk(KERN_ERR "SELinux: type %s: "
1637 "too deep or looped boundary\n",
1638 (char *) key);
1639 return -EINVAL;
1642 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1643 upper->bounds - 1);
1644 BUG_ON(!upper);
1646 if (upper->attribute) {
1647 printk(KERN_ERR "SELinux: type %s: "
1648 "bounded by attribute %s",
1649 (char *) key,
1650 sym_name(p, SYM_TYPES, upper->value - 1));
1651 return -EINVAL;
1655 return 0;
1658 static int policydb_bounds_sanity_check(struct policydb *p)
1660 int rc;
1662 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1663 return 0;
1665 rc = hashtab_map(p->p_users.table,
1666 user_bounds_sanity_check, p);
1667 if (rc)
1668 return rc;
1670 rc = hashtab_map(p->p_roles.table,
1671 role_bounds_sanity_check, p);
1672 if (rc)
1673 return rc;
1675 rc = hashtab_map(p->p_types.table,
1676 type_bounds_sanity_check, p);
1677 if (rc)
1678 return rc;
1680 return 0;
1683 extern int ss_initialized;
1685 u16 string_to_security_class(struct policydb *p, const char *name)
1687 struct class_datum *cladatum;
1689 cladatum = hashtab_search(p->p_classes.table, name);
1690 if (!cladatum)
1691 return 0;
1693 return cladatum->value;
1696 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1698 struct class_datum *cladatum;
1699 struct perm_datum *perdatum = NULL;
1700 struct common_datum *comdatum;
1702 if (!tclass || tclass > p->p_classes.nprim)
1703 return 0;
1705 cladatum = p->class_val_to_struct[tclass-1];
1706 comdatum = cladatum->comdatum;
1707 if (comdatum)
1708 perdatum = hashtab_search(comdatum->permissions.table,
1709 name);
1710 if (!perdatum)
1711 perdatum = hashtab_search(cladatum->permissions.table,
1712 name);
1713 if (!perdatum)
1714 return 0;
1716 return 1U << (perdatum->value-1);
1719 static int range_read(struct policydb *p, void *fp)
1721 struct range_trans *rt = NULL;
1722 struct mls_range *r = NULL;
1723 int i, rc;
1724 __le32 buf[2];
1725 u32 nel;
1727 if (p->policyvers < POLICYDB_VERSION_MLS)
1728 return 0;
1730 rc = next_entry(buf, fp, sizeof(u32));
1731 if (rc)
1732 goto out;
1734 nel = le32_to_cpu(buf[0]);
1735 for (i = 0; i < nel; i++) {
1736 rc = -ENOMEM;
1737 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1738 if (!rt)
1739 goto out;
1741 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1742 if (rc)
1743 goto out;
1745 rt->source_type = le32_to_cpu(buf[0]);
1746 rt->target_type = le32_to_cpu(buf[1]);
1747 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1748 rc = next_entry(buf, fp, sizeof(u32));
1749 if (rc)
1750 goto out;
1751 rt->target_class = le32_to_cpu(buf[0]);
1752 } else
1753 rt->target_class = p->process_class;
1755 rc = -EINVAL;
1756 if (!policydb_type_isvalid(p, rt->source_type) ||
1757 !policydb_type_isvalid(p, rt->target_type) ||
1758 !policydb_class_isvalid(p, rt->target_class))
1759 goto out;
1761 rc = -ENOMEM;
1762 r = kzalloc(sizeof(*r), GFP_KERNEL);
1763 if (!r)
1764 goto out;
1766 rc = mls_read_range_helper(r, fp);
1767 if (rc)
1768 goto out;
1770 rc = -EINVAL;
1771 if (!mls_range_isvalid(p, r)) {
1772 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1773 goto out;
1776 rc = hashtab_insert(p->range_tr, rt, r);
1777 if (rc)
1778 goto out;
1780 rt = NULL;
1781 r = NULL;
1783 rangetr_hash_eval(p->range_tr);
1784 rc = 0;
1785 out:
1786 kfree(rt);
1787 kfree(r);
1788 return rc;
1791 static int genfs_read(struct policydb *p, void *fp)
1793 int i, j, rc;
1794 u32 nel, nel2, len, len2;
1795 __le32 buf[1];
1796 struct ocontext *l, *c;
1797 struct ocontext *newc = NULL;
1798 struct genfs *genfs_p, *genfs;
1799 struct genfs *newgenfs = NULL;
1801 rc = next_entry(buf, fp, sizeof(u32));
1802 if (rc)
1803 goto out;
1804 nel = le32_to_cpu(buf[0]);
1806 for (i = 0; i < nel; i++) {
1807 rc = next_entry(buf, fp, sizeof(u32));
1808 if (rc)
1809 goto out;
1810 len = le32_to_cpu(buf[0]);
1812 rc = -ENOMEM;
1813 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1814 if (!newgenfs)
1815 goto out;
1817 rc = -ENOMEM;
1818 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1819 if (!newgenfs->fstype)
1820 goto out;
1822 rc = next_entry(newgenfs->fstype, fp, len);
1823 if (rc)
1824 goto out;
1826 newgenfs->fstype[len] = 0;
1828 for (genfs_p = NULL, genfs = p->genfs; genfs;
1829 genfs_p = genfs, genfs = genfs->next) {
1830 rc = -EINVAL;
1831 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1832 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1833 newgenfs->fstype);
1834 goto out;
1836 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1837 break;
1839 newgenfs->next = genfs;
1840 if (genfs_p)
1841 genfs_p->next = newgenfs;
1842 else
1843 p->genfs = newgenfs;
1844 genfs = newgenfs;
1845 newgenfs = NULL;
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc)
1849 goto out;
1851 nel2 = le32_to_cpu(buf[0]);
1852 for (j = 0; j < nel2; j++) {
1853 rc = next_entry(buf, fp, sizeof(u32));
1854 if (rc)
1855 goto out;
1856 len = le32_to_cpu(buf[0]);
1858 rc = -ENOMEM;
1859 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1860 if (!newc)
1861 goto out;
1863 rc = -ENOMEM;
1864 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1865 if (!newc->u.name)
1866 goto out;
1868 rc = next_entry(newc->u.name, fp, len);
1869 if (rc)
1870 goto out;
1871 newc->u.name[len] = 0;
1873 rc = next_entry(buf, fp, sizeof(u32));
1874 if (rc)
1875 goto out;
1877 newc->v.sclass = le32_to_cpu(buf[0]);
1878 rc = context_read_and_validate(&newc->context[0], p, fp);
1879 if (rc)
1880 goto out;
1882 for (l = NULL, c = genfs->head; c;
1883 l = c, c = c->next) {
1884 rc = -EINVAL;
1885 if (!strcmp(newc->u.name, c->u.name) &&
1886 (!c->v.sclass || !newc->v.sclass ||
1887 newc->v.sclass == c->v.sclass)) {
1888 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1889 genfs->fstype, c->u.name);
1890 goto out;
1892 len = strlen(newc->u.name);
1893 len2 = strlen(c->u.name);
1894 if (len > len2)
1895 break;
1898 newc->next = c;
1899 if (l)
1900 l->next = newc;
1901 else
1902 genfs->head = newc;
1903 newc = NULL;
1906 rc = 0;
1907 out:
1908 if (newgenfs)
1909 kfree(newgenfs->fstype);
1910 kfree(newgenfs);
1911 ocontext_destroy(newc, OCON_FSUSE);
1913 return rc;
1916 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1917 void *fp)
1919 int i, j, rc;
1920 u32 nel, len;
1921 __le32 buf[3];
1922 struct ocontext *l, *c;
1923 u32 nodebuf[8];
1925 for (i = 0; i < info->ocon_num; i++) {
1926 rc = next_entry(buf, fp, sizeof(u32));
1927 if (rc)
1928 goto out;
1929 nel = le32_to_cpu(buf[0]);
1931 l = NULL;
1932 for (j = 0; j < nel; j++) {
1933 rc = -ENOMEM;
1934 c = kzalloc(sizeof(*c), GFP_KERNEL);
1935 if (!c)
1936 goto out;
1937 if (l)
1938 l->next = c;
1939 else
1940 p->ocontexts[i] = c;
1941 l = c;
1943 switch (i) {
1944 case OCON_ISID:
1945 rc = next_entry(buf, fp, sizeof(u32));
1946 if (rc)
1947 goto out;
1949 c->sid[0] = le32_to_cpu(buf[0]);
1950 rc = context_read_and_validate(&c->context[0], p, fp);
1951 if (rc)
1952 goto out;
1953 break;
1954 case OCON_FS:
1955 case OCON_NETIF:
1956 rc = next_entry(buf, fp, sizeof(u32));
1957 if (rc)
1958 goto out;
1959 len = le32_to_cpu(buf[0]);
1961 rc = -ENOMEM;
1962 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1963 if (!c->u.name)
1964 goto out;
1966 rc = next_entry(c->u.name, fp, len);
1967 if (rc)
1968 goto out;
1970 c->u.name[len] = 0;
1971 rc = context_read_and_validate(&c->context[0], p, fp);
1972 if (rc)
1973 goto out;
1974 rc = context_read_and_validate(&c->context[1], p, fp);
1975 if (rc)
1976 goto out;
1977 break;
1978 case OCON_PORT:
1979 rc = next_entry(buf, fp, sizeof(u32)*3);
1980 if (rc)
1981 goto out;
1982 c->u.port.protocol = le32_to_cpu(buf[0]);
1983 c->u.port.low_port = le32_to_cpu(buf[1]);
1984 c->u.port.high_port = le32_to_cpu(buf[2]);
1985 rc = context_read_and_validate(&c->context[0], p, fp);
1986 if (rc)
1987 goto out;
1988 break;
1989 case OCON_NODE:
1990 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1991 if (rc)
1992 goto out;
1993 c->u.node.addr = nodebuf[0]; /* network order */
1994 c->u.node.mask = nodebuf[1]; /* network order */
1995 rc = context_read_and_validate(&c->context[0], p, fp);
1996 if (rc)
1997 goto out;
1998 break;
1999 case OCON_FSUSE:
2000 rc = next_entry(buf, fp, sizeof(u32)*2);
2001 if (rc)
2002 goto out;
2004 rc = -EINVAL;
2005 c->v.behavior = le32_to_cpu(buf[0]);
2006 if (c->v.behavior > SECURITY_FS_USE_NONE)
2007 goto out;
2009 rc = -ENOMEM;
2010 len = le32_to_cpu(buf[1]);
2011 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2012 if (!c->u.name)
2013 goto out;
2015 rc = next_entry(c->u.name, fp, len);
2016 if (rc)
2017 goto out;
2018 c->u.name[len] = 0;
2019 rc = context_read_and_validate(&c->context[0], p, fp);
2020 if (rc)
2021 goto out;
2022 break;
2023 case OCON_NODE6: {
2024 int k;
2026 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2027 if (rc)
2028 goto out;
2029 for (k = 0; k < 4; k++)
2030 c->u.node6.addr[k] = nodebuf[k];
2031 for (k = 0; k < 4; k++)
2032 c->u.node6.mask[k] = nodebuf[k+4];
2033 rc = context_read_and_validate(&c->context[0], p, fp);
2034 if (rc)
2035 goto out;
2036 break;
2041 rc = 0;
2042 out:
2043 return rc;
2047 * Read the configuration data from a policy database binary
2048 * representation file into a policy database structure.
2050 int policydb_read(struct policydb *p, void *fp)
2052 struct role_allow *ra, *lra;
2053 struct role_trans *tr, *ltr;
2054 int i, j, rc;
2055 __le32 buf[4];
2056 u32 len, nprim, nel;
2058 char *policydb_str;
2059 struct policydb_compat_info *info;
2061 rc = policydb_init(p);
2062 if (rc)
2063 return rc;
2065 /* Read the magic number and string length. */
2066 rc = next_entry(buf, fp, sizeof(u32) * 2);
2067 if (rc)
2068 goto bad;
2070 rc = -EINVAL;
2071 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2072 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2073 "not match expected magic number 0x%x\n",
2074 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2075 goto bad;
2078 rc = -EINVAL;
2079 len = le32_to_cpu(buf[1]);
2080 if (len != strlen(POLICYDB_STRING)) {
2081 printk(KERN_ERR "SELinux: policydb string length %d does not "
2082 "match expected length %Zu\n",
2083 len, strlen(POLICYDB_STRING));
2084 goto bad;
2087 rc = -ENOMEM;
2088 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2089 if (!policydb_str) {
2090 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2091 "string of length %d\n", len);
2092 goto bad;
2095 rc = next_entry(policydb_str, fp, len);
2096 if (rc) {
2097 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2098 kfree(policydb_str);
2099 goto bad;
2102 rc = -EINVAL;
2103 policydb_str[len] = '\0';
2104 if (strcmp(policydb_str, POLICYDB_STRING)) {
2105 printk(KERN_ERR "SELinux: policydb string %s does not match "
2106 "my string %s\n", policydb_str, POLICYDB_STRING);
2107 kfree(policydb_str);
2108 goto bad;
2110 /* Done with policydb_str. */
2111 kfree(policydb_str);
2112 policydb_str = NULL;
2114 /* Read the version and table sizes. */
2115 rc = next_entry(buf, fp, sizeof(u32)*4);
2116 if (rc)
2117 goto bad;
2119 rc = -EINVAL;
2120 p->policyvers = le32_to_cpu(buf[0]);
2121 if (p->policyvers < POLICYDB_VERSION_MIN ||
2122 p->policyvers > POLICYDB_VERSION_MAX) {
2123 printk(KERN_ERR "SELinux: policydb version %d does not match "
2124 "my version range %d-%d\n",
2125 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2126 goto bad;
2129 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2130 p->mls_enabled = 1;
2132 rc = -EINVAL;
2133 if (p->policyvers < POLICYDB_VERSION_MLS) {
2134 printk(KERN_ERR "SELinux: security policydb version %d "
2135 "(MLS) not backwards compatible\n",
2136 p->policyvers);
2137 goto bad;
2140 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2141 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2143 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2144 rc = ebitmap_read(&p->policycaps, fp);
2145 if (rc)
2146 goto bad;
2149 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2150 rc = ebitmap_read(&p->permissive_map, fp);
2151 if (rc)
2152 goto bad;
2155 rc = -EINVAL;
2156 info = policydb_lookup_compat(p->policyvers);
2157 if (!info) {
2158 printk(KERN_ERR "SELinux: unable to find policy compat info "
2159 "for version %d\n", p->policyvers);
2160 goto bad;
2163 rc = -EINVAL;
2164 if (le32_to_cpu(buf[2]) != info->sym_num ||
2165 le32_to_cpu(buf[3]) != info->ocon_num) {
2166 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2167 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2168 le32_to_cpu(buf[3]),
2169 info->sym_num, info->ocon_num);
2170 goto bad;
2173 for (i = 0; i < info->sym_num; i++) {
2174 rc = next_entry(buf, fp, sizeof(u32)*2);
2175 if (rc)
2176 goto bad;
2177 nprim = le32_to_cpu(buf[0]);
2178 nel = le32_to_cpu(buf[1]);
2179 for (j = 0; j < nel; j++) {
2180 rc = read_f[i](p, p->symtab[i].table, fp);
2181 if (rc)
2182 goto bad;
2185 p->symtab[i].nprim = nprim;
2188 rc = avtab_read(&p->te_avtab, fp, p);
2189 if (rc)
2190 goto bad;
2192 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2193 rc = cond_read_list(p, fp);
2194 if (rc)
2195 goto bad;
2198 rc = next_entry(buf, fp, sizeof(u32));
2199 if (rc)
2200 goto bad;
2201 nel = le32_to_cpu(buf[0]);
2202 ltr = NULL;
2203 for (i = 0; i < nel; i++) {
2204 rc = -ENOMEM;
2205 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2206 if (!tr)
2207 goto bad;
2208 if (ltr)
2209 ltr->next = tr;
2210 else
2211 p->role_tr = tr;
2212 rc = next_entry(buf, fp, sizeof(u32)*3);
2213 if (rc)
2214 goto bad;
2216 rc = -EINVAL;
2217 tr->role = le32_to_cpu(buf[0]);
2218 tr->type = le32_to_cpu(buf[1]);
2219 tr->new_role = le32_to_cpu(buf[2]);
2220 if (!policydb_role_isvalid(p, tr->role) ||
2221 !policydb_type_isvalid(p, tr->type) ||
2222 !policydb_role_isvalid(p, tr->new_role))
2223 goto bad;
2224 ltr = tr;
2227 rc = next_entry(buf, fp, sizeof(u32));
2228 if (rc)
2229 goto bad;
2230 nel = le32_to_cpu(buf[0]);
2231 lra = NULL;
2232 for (i = 0; i < nel; i++) {
2233 rc = -ENOMEM;
2234 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2235 if (!ra)
2236 goto bad;
2237 if (lra)
2238 lra->next = ra;
2239 else
2240 p->role_allow = ra;
2241 rc = next_entry(buf, fp, sizeof(u32)*2);
2242 if (rc)
2243 goto bad;
2245 rc = -EINVAL;
2246 ra->role = le32_to_cpu(buf[0]);
2247 ra->new_role = le32_to_cpu(buf[1]);
2248 if (!policydb_role_isvalid(p, ra->role) ||
2249 !policydb_role_isvalid(p, ra->new_role))
2250 goto bad;
2251 lra = ra;
2254 rc = policydb_index(p);
2255 if (rc)
2256 goto bad;
2258 rc = -EINVAL;
2259 p->process_class = string_to_security_class(p, "process");
2260 if (!p->process_class)
2261 goto bad;
2263 rc = -EINVAL;
2264 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2265 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2266 if (!p->process_trans_perms)
2267 goto bad;
2269 rc = ocontext_read(p, info, fp);
2270 if (rc)
2271 goto bad;
2273 rc = genfs_read(p, fp);
2274 if (rc)
2275 goto bad;
2277 rc = range_read(p, fp);
2278 if (rc)
2279 goto bad;
2281 rc = -ENOMEM;
2282 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2283 p->p_types.nprim,
2284 GFP_KERNEL | __GFP_ZERO);
2285 if (!p->type_attr_map_array)
2286 goto bad;
2288 /* preallocate so we don't have to worry about the put ever failing */
2289 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2290 GFP_KERNEL | __GFP_ZERO);
2291 if (rc)
2292 goto bad;
2294 for (i = 0; i < p->p_types.nprim; i++) {
2295 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2297 BUG_ON(!e);
2298 ebitmap_init(e);
2299 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2300 rc = ebitmap_read(e, fp);
2301 if (rc)
2302 goto bad;
2304 /* add the type itself as the degenerate case */
2305 rc = ebitmap_set_bit(e, i, 1);
2306 if (rc)
2307 goto bad;
2310 rc = policydb_bounds_sanity_check(p);
2311 if (rc)
2312 goto bad;
2314 rc = 0;
2315 out:
2316 return rc;
2317 bad:
2318 policydb_destroy(p);
2319 goto out;
2323 * Write a MLS level structure to a policydb binary
2324 * representation file.
2326 static int mls_write_level(struct mls_level *l, void *fp)
2328 __le32 buf[1];
2329 int rc;
2331 buf[0] = cpu_to_le32(l->sens);
2332 rc = put_entry(buf, sizeof(u32), 1, fp);
2333 if (rc)
2334 return rc;
2336 rc = ebitmap_write(&l->cat, fp);
2337 if (rc)
2338 return rc;
2340 return 0;
2344 * Write a MLS range structure to a policydb binary
2345 * representation file.
2347 static int mls_write_range_helper(struct mls_range *r, void *fp)
2349 __le32 buf[3];
2350 size_t items;
2351 int rc, eq;
2353 eq = mls_level_eq(&r->level[1], &r->level[0]);
2355 if (eq)
2356 items = 2;
2357 else
2358 items = 3;
2359 buf[0] = cpu_to_le32(items-1);
2360 buf[1] = cpu_to_le32(r->level[0].sens);
2361 if (!eq)
2362 buf[2] = cpu_to_le32(r->level[1].sens);
2364 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2366 rc = put_entry(buf, sizeof(u32), items, fp);
2367 if (rc)
2368 return rc;
2370 rc = ebitmap_write(&r->level[0].cat, fp);
2371 if (rc)
2372 return rc;
2373 if (!eq) {
2374 rc = ebitmap_write(&r->level[1].cat, fp);
2375 if (rc)
2376 return rc;
2379 return 0;
2382 static int sens_write(void *vkey, void *datum, void *ptr)
2384 char *key = vkey;
2385 struct level_datum *levdatum = datum;
2386 struct policy_data *pd = ptr;
2387 void *fp = pd->fp;
2388 __le32 buf[2];
2389 size_t len;
2390 int rc;
2392 len = strlen(key);
2393 buf[0] = cpu_to_le32(len);
2394 buf[1] = cpu_to_le32(levdatum->isalias);
2395 rc = put_entry(buf, sizeof(u32), 2, fp);
2396 if (rc)
2397 return rc;
2399 rc = put_entry(key, 1, len, fp);
2400 if (rc)
2401 return rc;
2403 rc = mls_write_level(levdatum->level, fp);
2404 if (rc)
2405 return rc;
2407 return 0;
2410 static int cat_write(void *vkey, void *datum, void *ptr)
2412 char *key = vkey;
2413 struct cat_datum *catdatum = datum;
2414 struct policy_data *pd = ptr;
2415 void *fp = pd->fp;
2416 __le32 buf[3];
2417 size_t len;
2418 int rc;
2420 len = strlen(key);
2421 buf[0] = cpu_to_le32(len);
2422 buf[1] = cpu_to_le32(catdatum->value);
2423 buf[2] = cpu_to_le32(catdatum->isalias);
2424 rc = put_entry(buf, sizeof(u32), 3, fp);
2425 if (rc)
2426 return rc;
2428 rc = put_entry(key, 1, len, fp);
2429 if (rc)
2430 return rc;
2432 return 0;
2435 static int role_trans_write(struct role_trans *r, void *fp)
2437 struct role_trans *tr;
2438 u32 buf[3];
2439 size_t nel;
2440 int rc;
2442 nel = 0;
2443 for (tr = r; tr; tr = tr->next)
2444 nel++;
2445 buf[0] = cpu_to_le32(nel);
2446 rc = put_entry(buf, sizeof(u32), 1, fp);
2447 if (rc)
2448 return rc;
2449 for (tr = r; tr; tr = tr->next) {
2450 buf[0] = cpu_to_le32(tr->role);
2451 buf[1] = cpu_to_le32(tr->type);
2452 buf[2] = cpu_to_le32(tr->new_role);
2453 rc = put_entry(buf, sizeof(u32), 3, fp);
2454 if (rc)
2455 return rc;
2458 return 0;
2461 static int role_allow_write(struct role_allow *r, void *fp)
2463 struct role_allow *ra;
2464 u32 buf[2];
2465 size_t nel;
2466 int rc;
2468 nel = 0;
2469 for (ra = r; ra; ra = ra->next)
2470 nel++;
2471 buf[0] = cpu_to_le32(nel);
2472 rc = put_entry(buf, sizeof(u32), 1, fp);
2473 if (rc)
2474 return rc;
2475 for (ra = r; ra; ra = ra->next) {
2476 buf[0] = cpu_to_le32(ra->role);
2477 buf[1] = cpu_to_le32(ra->new_role);
2478 rc = put_entry(buf, sizeof(u32), 2, fp);
2479 if (rc)
2480 return rc;
2482 return 0;
2486 * Write a security context structure
2487 * to a policydb binary representation file.
2489 static int context_write(struct policydb *p, struct context *c,
2490 void *fp)
2492 int rc;
2493 __le32 buf[3];
2495 buf[0] = cpu_to_le32(c->user);
2496 buf[1] = cpu_to_le32(c->role);
2497 buf[2] = cpu_to_le32(c->type);
2499 rc = put_entry(buf, sizeof(u32), 3, fp);
2500 if (rc)
2501 return rc;
2503 rc = mls_write_range_helper(&c->range, fp);
2504 if (rc)
2505 return rc;
2507 return 0;
2511 * The following *_write functions are used to
2512 * write the symbol data to a policy database
2513 * binary representation file.
2516 static int perm_write(void *vkey, void *datum, void *fp)
2518 char *key = vkey;
2519 struct perm_datum *perdatum = datum;
2520 __le32 buf[2];
2521 size_t len;
2522 int rc;
2524 len = strlen(key);
2525 buf[0] = cpu_to_le32(len);
2526 buf[1] = cpu_to_le32(perdatum->value);
2527 rc = put_entry(buf, sizeof(u32), 2, fp);
2528 if (rc)
2529 return rc;
2531 rc = put_entry(key, 1, len, fp);
2532 if (rc)
2533 return rc;
2535 return 0;
2538 static int common_write(void *vkey, void *datum, void *ptr)
2540 char *key = vkey;
2541 struct common_datum *comdatum = datum;
2542 struct policy_data *pd = ptr;
2543 void *fp = pd->fp;
2544 __le32 buf[4];
2545 size_t len;
2546 int rc;
2548 len = strlen(key);
2549 buf[0] = cpu_to_le32(len);
2550 buf[1] = cpu_to_le32(comdatum->value);
2551 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2552 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2553 rc = put_entry(buf, sizeof(u32), 4, fp);
2554 if (rc)
2555 return rc;
2557 rc = put_entry(key, 1, len, fp);
2558 if (rc)
2559 return rc;
2561 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2562 if (rc)
2563 return rc;
2565 return 0;
2568 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2569 void *fp)
2571 struct constraint_node *c;
2572 struct constraint_expr *e;
2573 __le32 buf[3];
2574 u32 nel;
2575 int rc;
2577 for (c = node; c; c = c->next) {
2578 nel = 0;
2579 for (e = c->expr; e; e = e->next)
2580 nel++;
2581 buf[0] = cpu_to_le32(c->permissions);
2582 buf[1] = cpu_to_le32(nel);
2583 rc = put_entry(buf, sizeof(u32), 2, fp);
2584 if (rc)
2585 return rc;
2586 for (e = c->expr; e; e = e->next) {
2587 buf[0] = cpu_to_le32(e->expr_type);
2588 buf[1] = cpu_to_le32(e->attr);
2589 buf[2] = cpu_to_le32(e->op);
2590 rc = put_entry(buf, sizeof(u32), 3, fp);
2591 if (rc)
2592 return rc;
2594 switch (e->expr_type) {
2595 case CEXPR_NAMES:
2596 rc = ebitmap_write(&e->names, fp);
2597 if (rc)
2598 return rc;
2599 break;
2600 default:
2601 break;
2606 return 0;
2609 static int class_write(void *vkey, void *datum, void *ptr)
2611 char *key = vkey;
2612 struct class_datum *cladatum = datum;
2613 struct policy_data *pd = ptr;
2614 void *fp = pd->fp;
2615 struct policydb *p = pd->p;
2616 struct constraint_node *c;
2617 __le32 buf[6];
2618 u32 ncons;
2619 size_t len, len2;
2620 int rc;
2622 len = strlen(key);
2623 if (cladatum->comkey)
2624 len2 = strlen(cladatum->comkey);
2625 else
2626 len2 = 0;
2628 ncons = 0;
2629 for (c = cladatum->constraints; c; c = c->next)
2630 ncons++;
2632 buf[0] = cpu_to_le32(len);
2633 buf[1] = cpu_to_le32(len2);
2634 buf[2] = cpu_to_le32(cladatum->value);
2635 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2636 if (cladatum->permissions.table)
2637 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2638 else
2639 buf[4] = 0;
2640 buf[5] = cpu_to_le32(ncons);
2641 rc = put_entry(buf, sizeof(u32), 6, fp);
2642 if (rc)
2643 return rc;
2645 rc = put_entry(key, 1, len, fp);
2646 if (rc)
2647 return rc;
2649 if (cladatum->comkey) {
2650 rc = put_entry(cladatum->comkey, 1, len2, fp);
2651 if (rc)
2652 return rc;
2655 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2656 if (rc)
2657 return rc;
2659 rc = write_cons_helper(p, cladatum->constraints, fp);
2660 if (rc)
2661 return rc;
2663 /* write out the validatetrans rule */
2664 ncons = 0;
2665 for (c = cladatum->validatetrans; c; c = c->next)
2666 ncons++;
2668 buf[0] = cpu_to_le32(ncons);
2669 rc = put_entry(buf, sizeof(u32), 1, fp);
2670 if (rc)
2671 return rc;
2673 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2674 if (rc)
2675 return rc;
2677 return 0;
2680 static int role_write(void *vkey, void *datum, void *ptr)
2682 char *key = vkey;
2683 struct role_datum *role = datum;
2684 struct policy_data *pd = ptr;
2685 void *fp = pd->fp;
2686 struct policydb *p = pd->p;
2687 __le32 buf[3];
2688 size_t items, len;
2689 int rc;
2691 len = strlen(key);
2692 items = 0;
2693 buf[items++] = cpu_to_le32(len);
2694 buf[items++] = cpu_to_le32(role->value);
2695 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2696 buf[items++] = cpu_to_le32(role->bounds);
2698 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2700 rc = put_entry(buf, sizeof(u32), items, fp);
2701 if (rc)
2702 return rc;
2704 rc = put_entry(key, 1, len, fp);
2705 if (rc)
2706 return rc;
2708 rc = ebitmap_write(&role->dominates, fp);
2709 if (rc)
2710 return rc;
2712 rc = ebitmap_write(&role->types, fp);
2713 if (rc)
2714 return rc;
2716 return 0;
2719 static int type_write(void *vkey, void *datum, void *ptr)
2721 char *key = vkey;
2722 struct type_datum *typdatum = datum;
2723 struct policy_data *pd = ptr;
2724 struct policydb *p = pd->p;
2725 void *fp = pd->fp;
2726 __le32 buf[4];
2727 int rc;
2728 size_t items, len;
2730 len = strlen(key);
2731 items = 0;
2732 buf[items++] = cpu_to_le32(len);
2733 buf[items++] = cpu_to_le32(typdatum->value);
2734 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2735 u32 properties = 0;
2737 if (typdatum->primary)
2738 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2740 if (typdatum->attribute)
2741 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2743 buf[items++] = cpu_to_le32(properties);
2744 buf[items++] = cpu_to_le32(typdatum->bounds);
2745 } else {
2746 buf[items++] = cpu_to_le32(typdatum->primary);
2748 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2749 rc = put_entry(buf, sizeof(u32), items, fp);
2750 if (rc)
2751 return rc;
2753 rc = put_entry(key, 1, len, fp);
2754 if (rc)
2755 return rc;
2757 return 0;
2760 static int user_write(void *vkey, void *datum, void *ptr)
2762 char *key = vkey;
2763 struct user_datum *usrdatum = datum;
2764 struct policy_data *pd = ptr;
2765 struct policydb *p = pd->p;
2766 void *fp = pd->fp;
2767 __le32 buf[3];
2768 size_t items, len;
2769 int rc;
2771 len = strlen(key);
2772 items = 0;
2773 buf[items++] = cpu_to_le32(len);
2774 buf[items++] = cpu_to_le32(usrdatum->value);
2775 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2776 buf[items++] = cpu_to_le32(usrdatum->bounds);
2777 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2778 rc = put_entry(buf, sizeof(u32), items, fp);
2779 if (rc)
2780 return rc;
2782 rc = put_entry(key, 1, len, fp);
2783 if (rc)
2784 return rc;
2786 rc = ebitmap_write(&usrdatum->roles, fp);
2787 if (rc)
2788 return rc;
2790 rc = mls_write_range_helper(&usrdatum->range, fp);
2791 if (rc)
2792 return rc;
2794 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2795 if (rc)
2796 return rc;
2798 return 0;
2801 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2802 void *datap) =
2804 common_write,
2805 class_write,
2806 role_write,
2807 type_write,
2808 user_write,
2809 cond_write_bool,
2810 sens_write,
2811 cat_write,
2814 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2815 void *fp)
2817 unsigned int i, j, rc;
2818 size_t nel, len;
2819 __le32 buf[3];
2820 u32 nodebuf[8];
2821 struct ocontext *c;
2822 for (i = 0; i < info->ocon_num; i++) {
2823 nel = 0;
2824 for (c = p->ocontexts[i]; c; c = c->next)
2825 nel++;
2826 buf[0] = cpu_to_le32(nel);
2827 rc = put_entry(buf, sizeof(u32), 1, fp);
2828 if (rc)
2829 return rc;
2830 for (c = p->ocontexts[i]; c; c = c->next) {
2831 switch (i) {
2832 case OCON_ISID:
2833 buf[0] = cpu_to_le32(c->sid[0]);
2834 rc = put_entry(buf, sizeof(u32), 1, fp);
2835 if (rc)
2836 return rc;
2837 rc = context_write(p, &c->context[0], fp);
2838 if (rc)
2839 return rc;
2840 break;
2841 case OCON_FS:
2842 case OCON_NETIF:
2843 len = strlen(c->u.name);
2844 buf[0] = cpu_to_le32(len);
2845 rc = put_entry(buf, sizeof(u32), 1, fp);
2846 if (rc)
2847 return rc;
2848 rc = put_entry(c->u.name, 1, len, fp);
2849 if (rc)
2850 return rc;
2851 rc = context_write(p, &c->context[0], fp);
2852 if (rc)
2853 return rc;
2854 rc = context_write(p, &c->context[1], fp);
2855 if (rc)
2856 return rc;
2857 break;
2858 case OCON_PORT:
2859 buf[0] = cpu_to_le32(c->u.port.protocol);
2860 buf[1] = cpu_to_le32(c->u.port.low_port);
2861 buf[2] = cpu_to_le32(c->u.port.high_port);
2862 rc = put_entry(buf, sizeof(u32), 3, fp);
2863 if (rc)
2864 return rc;
2865 rc = context_write(p, &c->context[0], fp);
2866 if (rc)
2867 return rc;
2868 break;
2869 case OCON_NODE:
2870 nodebuf[0] = c->u.node.addr; /* network order */
2871 nodebuf[1] = c->u.node.mask; /* network order */
2872 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2873 if (rc)
2874 return rc;
2875 rc = context_write(p, &c->context[0], fp);
2876 if (rc)
2877 return rc;
2878 break;
2879 case OCON_FSUSE:
2880 buf[0] = cpu_to_le32(c->v.behavior);
2881 len = strlen(c->u.name);
2882 buf[1] = cpu_to_le32(len);
2883 rc = put_entry(buf, sizeof(u32), 2, fp);
2884 if (rc)
2885 return rc;
2886 rc = put_entry(c->u.name, 1, len, fp);
2887 if (rc)
2888 return rc;
2889 rc = context_write(p, &c->context[0], fp);
2890 if (rc)
2891 return rc;
2892 break;
2893 case OCON_NODE6:
2894 for (j = 0; j < 4; j++)
2895 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2896 for (j = 0; j < 4; j++)
2897 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2898 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2899 if (rc)
2900 return rc;
2901 rc = context_write(p, &c->context[0], fp);
2902 if (rc)
2903 return rc;
2904 break;
2908 return 0;
2911 static int genfs_write(struct policydb *p, void *fp)
2913 struct genfs *genfs;
2914 struct ocontext *c;
2915 size_t len;
2916 __le32 buf[1];
2917 int rc;
2919 len = 0;
2920 for (genfs = p->genfs; genfs; genfs = genfs->next)
2921 len++;
2922 buf[0] = cpu_to_le32(len);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2924 if (rc)
2925 return rc;
2926 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2927 len = strlen(genfs->fstype);
2928 buf[0] = cpu_to_le32(len);
2929 rc = put_entry(buf, sizeof(u32), 1, fp);
2930 if (rc)
2931 return rc;
2932 rc = put_entry(genfs->fstype, 1, len, fp);
2933 if (rc)
2934 return rc;
2935 len = 0;
2936 for (c = genfs->head; c; c = c->next)
2937 len++;
2938 buf[0] = cpu_to_le32(len);
2939 rc = put_entry(buf, sizeof(u32), 1, fp);
2940 if (rc)
2941 return rc;
2942 for (c = genfs->head; c; c = c->next) {
2943 len = strlen(c->u.name);
2944 buf[0] = cpu_to_le32(len);
2945 rc = put_entry(buf, sizeof(u32), 1, fp);
2946 if (rc)
2947 return rc;
2948 rc = put_entry(c->u.name, 1, len, fp);
2949 if (rc)
2950 return rc;
2951 buf[0] = cpu_to_le32(c->v.sclass);
2952 rc = put_entry(buf, sizeof(u32), 1, fp);
2953 if (rc)
2954 return rc;
2955 rc = context_write(p, &c->context[0], fp);
2956 if (rc)
2957 return rc;
2960 return 0;
2963 static int range_count(void *key, void *data, void *ptr)
2965 int *cnt = ptr;
2966 *cnt = *cnt + 1;
2968 return 0;
2971 static int range_write_helper(void *key, void *data, void *ptr)
2973 __le32 buf[2];
2974 struct range_trans *rt = key;
2975 struct mls_range *r = data;
2976 struct policy_data *pd = ptr;
2977 void *fp = pd->fp;
2978 struct policydb *p = pd->p;
2979 int rc;
2981 buf[0] = cpu_to_le32(rt->source_type);
2982 buf[1] = cpu_to_le32(rt->target_type);
2983 rc = put_entry(buf, sizeof(u32), 2, fp);
2984 if (rc)
2985 return rc;
2986 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2987 buf[0] = cpu_to_le32(rt->target_class);
2988 rc = put_entry(buf, sizeof(u32), 1, fp);
2989 if (rc)
2990 return rc;
2992 rc = mls_write_range_helper(r, fp);
2993 if (rc)
2994 return rc;
2996 return 0;
2999 static int range_write(struct policydb *p, void *fp)
3001 size_t nel;
3002 __le32 buf[1];
3003 int rc;
3004 struct policy_data pd;
3006 pd.p = p;
3007 pd.fp = fp;
3009 /* count the number of entries in the hashtab */
3010 nel = 0;
3011 rc = hashtab_map(p->range_tr, range_count, &nel);
3012 if (rc)
3013 return rc;
3015 buf[0] = cpu_to_le32(nel);
3016 rc = put_entry(buf, sizeof(u32), 1, fp);
3017 if (rc)
3018 return rc;
3020 /* actually write all of the entries */
3021 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3022 if (rc)
3023 return rc;
3025 return 0;
3029 * Write the configuration data in a policy database
3030 * structure to a policy database binary representation
3031 * file.
3033 int policydb_write(struct policydb *p, void *fp)
3035 unsigned int i, num_syms;
3036 int rc;
3037 __le32 buf[4];
3038 u32 config;
3039 size_t len;
3040 struct policydb_compat_info *info;
3043 * refuse to write policy older than compressed avtab
3044 * to simplify the writer. There are other tests dropped
3045 * since we assume this throughout the writer code. Be
3046 * careful if you ever try to remove this restriction
3048 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3049 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3050 " Because it is less than version %d\n", p->policyvers,
3051 POLICYDB_VERSION_AVTAB);
3052 return -EINVAL;
3055 config = 0;
3056 if (p->mls_enabled)
3057 config |= POLICYDB_CONFIG_MLS;
3059 if (p->reject_unknown)
3060 config |= REJECT_UNKNOWN;
3061 if (p->allow_unknown)
3062 config |= ALLOW_UNKNOWN;
3064 /* Write the magic number and string identifiers. */
3065 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3066 len = strlen(POLICYDB_STRING);
3067 buf[1] = cpu_to_le32(len);
3068 rc = put_entry(buf, sizeof(u32), 2, fp);
3069 if (rc)
3070 return rc;
3071 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3072 if (rc)
3073 return rc;
3075 /* Write the version, config, and table sizes. */
3076 info = policydb_lookup_compat(p->policyvers);
3077 if (!info) {
3078 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3079 "version %d", p->policyvers);
3080 return -EINVAL;
3083 buf[0] = cpu_to_le32(p->policyvers);
3084 buf[1] = cpu_to_le32(config);
3085 buf[2] = cpu_to_le32(info->sym_num);
3086 buf[3] = cpu_to_le32(info->ocon_num);
3088 rc = put_entry(buf, sizeof(u32), 4, fp);
3089 if (rc)
3090 return rc;
3092 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3093 rc = ebitmap_write(&p->policycaps, fp);
3094 if (rc)
3095 return rc;
3098 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3099 rc = ebitmap_write(&p->permissive_map, fp);
3100 if (rc)
3101 return rc;
3104 num_syms = info->sym_num;
3105 for (i = 0; i < num_syms; i++) {
3106 struct policy_data pd;
3108 pd.fp = fp;
3109 pd.p = p;
3111 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3112 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3114 rc = put_entry(buf, sizeof(u32), 2, fp);
3115 if (rc)
3116 return rc;
3117 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3118 if (rc)
3119 return rc;
3122 rc = avtab_write(p, &p->te_avtab, fp);
3123 if (rc)
3124 return rc;
3126 rc = cond_write_list(p, p->cond_list, fp);
3127 if (rc)
3128 return rc;
3130 rc = role_trans_write(p->role_tr, fp);
3131 if (rc)
3132 return rc;
3134 rc = role_allow_write(p->role_allow, fp);
3135 if (rc)
3136 return rc;
3138 rc = ocontext_write(p, info, fp);
3139 if (rc)
3140 return rc;
3142 rc = genfs_write(p, fp);
3143 if (rc)
3144 return rc;
3146 rc = range_write(p, fp);
3147 if (rc)
3148 return rc;
3150 for (i = 0; i < p->p_types.nprim; i++) {
3151 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3153 BUG_ON(!e);
3154 rc = ebitmap_write(e, fp);
3155 if (rc)
3156 return rc;
3159 return 0;