2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
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>
38 #include "conditional.h"
45 static const char *symtab_name[SYM_NUM] = {
57 static unsigned int symtab_sizes[SYM_NUM] = {
68 struct policydb_compat_info {
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,
92 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
97 .version = POLICYDB_VERSION_MLS,
102 .version = POLICYDB_VERSION_AVTAB,
104 .ocon_num = OCON_NUM,
107 .version = POLICYDB_VERSION_RANGETRANS,
109 .ocon_num = OCON_NUM,
112 .version = POLICYDB_VERSION_POLCAP,
114 .ocon_num = OCON_NUM,
117 .version = POLICYDB_VERSION_PERMISSIVE,
119 .ocon_num = OCON_NUM,
122 .version = POLICYDB_VERSION_BOUNDARY,
124 .ocon_num = OCON_NUM,
128 static struct policydb_compat_info *policydb_lookup_compat(int version)
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];
143 * Initialize the role table.
145 static int roles_init(struct policydb *p)
149 struct role_datum *role;
151 role = kzalloc(sizeof(*role), GFP_KERNEL);
156 role->value = ++p->p_roles.nprim;
157 if (role->value != OBJECT_R_VAL) {
161 key = kstrdup(OBJECT_R, GFP_KERNEL);
166 rc = hashtab_insert(p->p_roles.table, key, role);
179 static u32 rangetr_hash(struct hashtab *h, const void *k)
181 const struct range_trans *key = k;
182 return (key->source_type + (key->target_type << 3) +
183 (key->target_class << 5)) & (h->size - 1);
186 static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
188 const struct range_trans *key1 = k1, *key2 = k2;
191 v = key1->source_type - key2->source_type;
195 v = key1->target_type - key2->target_type;
199 v = key1->target_class - key2->target_class;
205 * Initialize a policy database structure.
207 static int policydb_init(struct policydb *p)
211 memset(p, 0, sizeof(*p));
213 for (i = 0; i < SYM_NUM; i++) {
214 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
216 goto out_free_symtab;
219 rc = avtab_init(&p->te_avtab);
221 goto out_free_symtab;
225 goto out_free_symtab;
227 rc = cond_policydb_init(p);
229 goto out_free_symtab;
231 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
233 goto out_free_symtab;
235 ebitmap_init(&p->policycaps);
236 ebitmap_init(&p->permissive_map);
242 for (i = 0; i < SYM_NUM; i++)
243 hashtab_destroy(p->symtab[i].table);
248 * The following *_index functions are used to
249 * define the val_to_name and val_to_struct arrays
250 * in a policy database structure. The val_to_name
251 * arrays are used when converting security context
252 * structures into string representations. The
253 * val_to_struct arrays are used when the attributes
254 * of a class, role, or user are needed.
257 static int common_index(void *key, void *datum, void *datap)
260 struct common_datum *comdatum;
264 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
266 p->p_common_val_to_name[comdatum->value - 1] = key;
270 static int class_index(void *key, void *datum, void *datap)
273 struct class_datum *cladatum;
277 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
279 p->p_class_val_to_name[cladatum->value - 1] = key;
280 p->class_val_to_struct[cladatum->value - 1] = cladatum;
284 static int role_index(void *key, void *datum, void *datap)
287 struct role_datum *role;
292 || role->value > p->p_roles.nprim
293 || role->bounds > p->p_roles.nprim)
295 p->p_role_val_to_name[role->value - 1] = key;
296 p->role_val_to_struct[role->value - 1] = role;
300 static int type_index(void *key, void *datum, void *datap)
303 struct type_datum *typdatum;
308 if (typdatum->primary) {
310 || typdatum->value > p->p_types.nprim
311 || typdatum->bounds > p->p_types.nprim)
313 p->p_type_val_to_name[typdatum->value - 1] = key;
314 p->type_val_to_struct[typdatum->value - 1] = typdatum;
320 static int user_index(void *key, void *datum, void *datap)
323 struct user_datum *usrdatum;
328 || usrdatum->value > p->p_users.nprim
329 || usrdatum->bounds > p->p_users.nprim)
331 p->p_user_val_to_name[usrdatum->value - 1] = key;
332 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
336 static int sens_index(void *key, void *datum, void *datap)
339 struct level_datum *levdatum;
344 if (!levdatum->isalias) {
345 if (!levdatum->level->sens ||
346 levdatum->level->sens > p->p_levels.nprim)
348 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
354 static int cat_index(void *key, void *datum, void *datap)
357 struct cat_datum *catdatum;
362 if (!catdatum->isalias) {
363 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
365 p->p_cat_val_to_name[catdatum->value - 1] = key;
371 static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
384 * Define the common val_to_name array and the class
385 * val_to_name and val_to_struct arrays in a policy
386 * database structure.
388 * Caller must clean up upon failure.
390 static int policydb_index_classes(struct policydb *p)
394 p->p_common_val_to_name =
395 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
396 if (!p->p_common_val_to_name) {
401 rc = hashtab_map(p->p_commons.table, common_index, p);
405 p->class_val_to_struct =
406 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
407 if (!p->class_val_to_struct) {
412 p->p_class_val_to_name =
413 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
414 if (!p->p_class_val_to_name) {
419 rc = hashtab_map(p->p_classes.table, class_index, p);
425 static void symtab_hash_eval(struct symtab *s)
429 for (i = 0; i < SYM_NUM; i++) {
430 struct hashtab *h = s[i].table;
431 struct hashtab_info info;
433 hashtab_stat(h, &info);
434 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
435 "longest chain length %d\n", symtab_name[i], h->nel,
436 info.slots_used, h->size, info.max_chain_len);
440 static void rangetr_hash_eval(struct hashtab *h)
442 struct hashtab_info info;
444 hashtab_stat(h, &info);
445 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
446 "longest chain length %d\n", h->nel,
447 info.slots_used, h->size, info.max_chain_len);
450 static inline void rangetr_hash_eval(struct hashtab *h)
456 * Define the other val_to_name and val_to_struct arrays
457 * in a policy database structure.
459 * Caller must clean up on failure.
461 static int policydb_index_others(struct policydb *p)
465 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
466 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
468 printk(", %d sens, %d cats", p->p_levels.nprim,
472 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
473 p->p_classes.nprim, p->te_avtab.nel);
476 avtab_hash_eval(&p->te_avtab, "rules");
477 symtab_hash_eval(p->symtab);
480 p->role_val_to_struct =
481 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
483 if (!p->role_val_to_struct) {
488 p->user_val_to_struct =
489 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
491 if (!p->user_val_to_struct) {
496 p->type_val_to_struct =
497 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
499 if (!p->type_val_to_struct) {
504 if (cond_init_bool_indexes(p)) {
509 for (i = SYM_ROLES; i < SYM_NUM; i++) {
510 p->sym_val_to_name[i] =
511 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
512 if (!p->sym_val_to_name[i]) {
516 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
526 * The following *_destroy functions are used to
527 * free any memory allocated for each kind of
528 * symbol data in the policy database.
531 static int perm_destroy(void *key, void *datum, void *p)
538 static int common_destroy(void *key, void *datum, void *p)
540 struct common_datum *comdatum;
544 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
545 hashtab_destroy(comdatum->permissions.table);
550 static int cls_destroy(void *key, void *datum, void *p)
552 struct class_datum *cladatum;
553 struct constraint_node *constraint, *ctemp;
554 struct constraint_expr *e, *etmp;
558 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
559 hashtab_destroy(cladatum->permissions.table);
560 constraint = cladatum->constraints;
562 e = constraint->expr;
564 ebitmap_destroy(&e->names);
570 constraint = constraint->next;
574 constraint = cladatum->validatetrans;
576 e = constraint->expr;
578 ebitmap_destroy(&e->names);
584 constraint = constraint->next;
588 kfree(cladatum->comkey);
593 static int role_destroy(void *key, void *datum, void *p)
595 struct role_datum *role;
599 ebitmap_destroy(&role->dominates);
600 ebitmap_destroy(&role->types);
605 static int type_destroy(void *key, void *datum, void *p)
612 static int user_destroy(void *key, void *datum, void *p)
614 struct user_datum *usrdatum;
618 ebitmap_destroy(&usrdatum->roles);
619 ebitmap_destroy(&usrdatum->range.level[0].cat);
620 ebitmap_destroy(&usrdatum->range.level[1].cat);
621 ebitmap_destroy(&usrdatum->dfltlevel.cat);
626 static int sens_destroy(void *key, void *datum, void *p)
628 struct level_datum *levdatum;
632 ebitmap_destroy(&levdatum->level->cat);
633 kfree(levdatum->level);
638 static int cat_destroy(void *key, void *datum, void *p)
645 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
657 static int range_tr_destroy(void *key, void *datum, void *p)
659 struct mls_range *rt = datum;
661 ebitmap_destroy(&rt->level[0].cat);
662 ebitmap_destroy(&rt->level[1].cat);
668 static void ocontext_destroy(struct ocontext *c, int i)
673 context_destroy(&c->context[0]);
674 context_destroy(&c->context[1]);
675 if (i == OCON_ISID || i == OCON_FS ||
676 i == OCON_NETIF || i == OCON_FSUSE)
682 * Free any memory allocated by a policy database structure.
684 void policydb_destroy(struct policydb *p)
686 struct ocontext *c, *ctmp;
687 struct genfs *g, *gtmp;
689 struct role_allow *ra, *lra = NULL;
690 struct role_trans *tr, *ltr = NULL;
692 for (i = 0; i < SYM_NUM; i++) {
694 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
695 hashtab_destroy(p->symtab[i].table);
698 for (i = 0; i < SYM_NUM; i++)
699 kfree(p->sym_val_to_name[i]);
701 kfree(p->class_val_to_struct);
702 kfree(p->role_val_to_struct);
703 kfree(p->user_val_to_struct);
704 kfree(p->type_val_to_struct);
706 avtab_destroy(&p->te_avtab);
708 for (i = 0; i < OCON_NUM; i++) {
714 ocontext_destroy(ctmp, i);
716 p->ocontexts[i] = NULL;
727 ocontext_destroy(ctmp, OCON_FSUSE);
735 cond_policydb_destroy(p);
737 for (tr = p->role_tr; tr; tr = tr->next) {
744 for (ra = p->role_allow; ra; ra = ra->next) {
751 hashtab_map(p->range_tr, range_tr_destroy, NULL);
752 hashtab_destroy(p->range_tr);
754 if (p->type_attr_map_array) {
755 for (i = 0; i < p->p_types.nprim; i++) {
758 e = flex_array_get(p->type_attr_map_array, i);
763 flex_array_free(p->type_attr_map_array);
765 ebitmap_destroy(&p->policycaps);
766 ebitmap_destroy(&p->permissive_map);
772 * Load the initial SIDs specified in a policy database
773 * structure into a SID table.
775 int policydb_load_isids(struct policydb *p, struct sidtab *s)
777 struct ocontext *head, *c;
782 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
786 head = p->ocontexts[OCON_ISID];
787 for (c = head; c; c = c->next) {
788 if (!c->context[0].user) {
789 printk(KERN_ERR "SELinux: SID %s was never "
790 "defined.\n", c->u.name);
794 if (sidtab_insert(s, c->sid[0], &c->context[0])) {
795 printk(KERN_ERR "SELinux: unable to load initial "
796 "SID %s.\n", c->u.name);
805 int policydb_class_isvalid(struct policydb *p, unsigned int class)
807 if (!class || class > p->p_classes.nprim)
812 int policydb_role_isvalid(struct policydb *p, unsigned int role)
814 if (!role || role > p->p_roles.nprim)
819 int policydb_type_isvalid(struct policydb *p, unsigned int type)
821 if (!type || type > p->p_types.nprim)
827 * Return 1 if the fields in the security context
828 * structure `c' are valid. Return 0 otherwise.
830 int policydb_context_isvalid(struct policydb *p, struct context *c)
832 struct role_datum *role;
833 struct user_datum *usrdatum;
835 if (!c->role || c->role > p->p_roles.nprim)
838 if (!c->user || c->user > p->p_users.nprim)
841 if (!c->type || c->type > p->p_types.nprim)
844 if (c->role != OBJECT_R_VAL) {
846 * Role must be authorized for the type.
848 role = p->role_val_to_struct[c->role - 1];
849 if (!ebitmap_get_bit(&role->types,
851 /* role may not be associated with type */
855 * User must be authorized for the role.
857 usrdatum = p->user_val_to_struct[c->user - 1];
861 if (!ebitmap_get_bit(&usrdatum->roles,
863 /* user may not be associated with role */
867 if (!mls_context_isvalid(p, c))
874 * Read a MLS range structure from a policydb binary
875 * representation file.
877 static int mls_read_range_helper(struct mls_range *r, void *fp)
883 rc = next_entry(buf, fp, sizeof(u32));
887 items = le32_to_cpu(buf[0]);
888 if (items > ARRAY_SIZE(buf)) {
889 printk(KERN_ERR "SELinux: mls: range overflow\n");
893 rc = next_entry(buf, fp, sizeof(u32) * items);
895 printk(KERN_ERR "SELinux: mls: truncated range\n");
898 r->level[0].sens = le32_to_cpu(buf[0]);
900 r->level[1].sens = le32_to_cpu(buf[1]);
902 r->level[1].sens = r->level[0].sens;
904 rc = ebitmap_read(&r->level[0].cat, fp);
906 printk(KERN_ERR "SELinux: mls: error reading low "
911 rc = ebitmap_read(&r->level[1].cat, fp);
913 printk(KERN_ERR "SELinux: mls: error reading high "
918 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
920 printk(KERN_ERR "SELinux: mls: out of memory\n");
929 ebitmap_destroy(&r->level[0].cat);
934 * Read and validate a security context structure
935 * from a policydb binary representation file.
937 static int context_read_and_validate(struct context *c,
944 rc = next_entry(buf, fp, sizeof buf);
946 printk(KERN_ERR "SELinux: context truncated\n");
949 c->user = le32_to_cpu(buf[0]);
950 c->role = le32_to_cpu(buf[1]);
951 c->type = le32_to_cpu(buf[2]);
952 if (p->policyvers >= POLICYDB_VERSION_MLS) {
953 if (mls_read_range_helper(&c->range, fp)) {
954 printk(KERN_ERR "SELinux: error reading MLS range of "
961 if (!policydb_context_isvalid(p, c)) {
962 printk(KERN_ERR "SELinux: invalid security context\n");
971 * The following *_read functions are used to
972 * read the symbol data from a policy database
973 * binary representation file.
976 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
979 struct perm_datum *perdatum;
984 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
990 rc = next_entry(buf, fp, sizeof buf);
994 len = le32_to_cpu(buf[0]);
995 perdatum->value = le32_to_cpu(buf[1]);
997 key = kmalloc(len + 1, GFP_KERNEL);
1002 rc = next_entry(key, fp, len);
1007 rc = hashtab_insert(h, key, perdatum);
1013 perm_destroy(key, perdatum, NULL);
1017 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1020 struct common_datum *comdatum;
1025 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1031 rc = next_entry(buf, fp, sizeof buf);
1035 len = le32_to_cpu(buf[0]);
1036 comdatum->value = le32_to_cpu(buf[1]);
1038 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1041 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1042 nel = le32_to_cpu(buf[3]);
1044 key = kmalloc(len + 1, GFP_KERNEL);
1049 rc = next_entry(key, fp, len);
1054 for (i = 0; i < nel; i++) {
1055 rc = perm_read(p, comdatum->permissions.table, fp);
1060 rc = hashtab_insert(h, key, comdatum);
1066 common_destroy(key, comdatum, NULL);
1070 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1071 int allowxtarget, void *fp)
1073 struct constraint_node *c, *lc;
1074 struct constraint_expr *e, *le;
1077 int rc, i, j, depth;
1080 for (i = 0; i < ncons; i++) {
1081 c = kzalloc(sizeof(*c), GFP_KERNEL);
1090 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1093 c->permissions = le32_to_cpu(buf[0]);
1094 nexpr = le32_to_cpu(buf[1]);
1097 for (j = 0; j < nexpr; j++) {
1098 e = kzalloc(sizeof(*e), GFP_KERNEL);
1107 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1110 e->expr_type = le32_to_cpu(buf[0]);
1111 e->attr = le32_to_cpu(buf[1]);
1112 e->op = le32_to_cpu(buf[2]);
1114 switch (e->expr_type) {
1126 if (depth == (CEXPR_MAXDEPTH - 1))
1131 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1133 if (depth == (CEXPR_MAXDEPTH - 1))
1136 if (ebitmap_read(&e->names, fp))
1152 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1155 struct class_datum *cladatum;
1157 u32 len, len2, ncons, nel;
1160 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1166 rc = next_entry(buf, fp, sizeof(u32)*6);
1170 len = le32_to_cpu(buf[0]);
1171 len2 = le32_to_cpu(buf[1]);
1172 cladatum->value = le32_to_cpu(buf[2]);
1174 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1177 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1178 nel = le32_to_cpu(buf[4]);
1180 ncons = le32_to_cpu(buf[5]);
1182 key = kmalloc(len + 1, GFP_KERNEL);
1187 rc = next_entry(key, fp, len);
1193 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1194 if (!cladatum->comkey) {
1198 rc = next_entry(cladatum->comkey, fp, len2);
1201 cladatum->comkey[len2] = '\0';
1203 cladatum->comdatum = hashtab_search(p->p_commons.table,
1205 if (!cladatum->comdatum) {
1206 printk(KERN_ERR "SELinux: unknown common %s\n",
1212 for (i = 0; i < nel; i++) {
1213 rc = perm_read(p, cladatum->permissions.table, fp);
1218 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1222 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1223 /* grab the validatetrans rules */
1224 rc = next_entry(buf, fp, sizeof(u32));
1227 ncons = le32_to_cpu(buf[0]);
1228 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1233 rc = hashtab_insert(h, key, cladatum);
1241 cls_destroy(key, cladatum, NULL);
1245 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1248 struct role_datum *role;
1249 int rc, to_read = 2;
1253 role = kzalloc(sizeof(*role), GFP_KERNEL);
1259 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1262 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1266 len = le32_to_cpu(buf[0]);
1267 role->value = le32_to_cpu(buf[1]);
1268 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1269 role->bounds = le32_to_cpu(buf[2]);
1271 key = kmalloc(len + 1, GFP_KERNEL);
1276 rc = next_entry(key, fp, len);
1281 rc = ebitmap_read(&role->dominates, fp);
1285 rc = ebitmap_read(&role->types, fp);
1289 if (strcmp(key, OBJECT_R) == 0) {
1290 if (role->value != OBJECT_R_VAL) {
1291 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1292 OBJECT_R, role->value);
1300 rc = hashtab_insert(h, key, role);
1306 role_destroy(key, role, NULL);
1310 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1313 struct type_datum *typdatum;
1314 int rc, to_read = 3;
1318 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1324 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1327 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1331 len = le32_to_cpu(buf[0]);
1332 typdatum->value = le32_to_cpu(buf[1]);
1333 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1334 u32 prop = le32_to_cpu(buf[2]);
1336 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1337 typdatum->primary = 1;
1338 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1339 typdatum->attribute = 1;
1341 typdatum->bounds = le32_to_cpu(buf[3]);
1343 typdatum->primary = le32_to_cpu(buf[2]);
1346 key = kmalloc(len + 1, GFP_KERNEL);
1351 rc = next_entry(key, fp, len);
1356 rc = hashtab_insert(h, key, typdatum);
1362 type_destroy(key, typdatum, NULL);
1368 * Read a MLS level structure from a policydb binary
1369 * representation file.
1371 static int mls_read_level(struct mls_level *lp, void *fp)
1376 memset(lp, 0, sizeof(*lp));
1378 rc = next_entry(buf, fp, sizeof buf);
1380 printk(KERN_ERR "SELinux: mls: truncated level\n");
1383 lp->sens = le32_to_cpu(buf[0]);
1385 if (ebitmap_read(&lp->cat, fp)) {
1386 printk(KERN_ERR "SELinux: mls: error reading level "
1397 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1400 struct user_datum *usrdatum;
1401 int rc, to_read = 2;
1405 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1411 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1414 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1418 len = le32_to_cpu(buf[0]);
1419 usrdatum->value = le32_to_cpu(buf[1]);
1420 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1421 usrdatum->bounds = le32_to_cpu(buf[2]);
1423 key = kmalloc(len + 1, GFP_KERNEL);
1428 rc = next_entry(key, fp, len);
1433 rc = ebitmap_read(&usrdatum->roles, fp);
1437 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1438 rc = mls_read_range_helper(&usrdatum->range, fp);
1441 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1446 rc = hashtab_insert(h, key, usrdatum);
1452 user_destroy(key, usrdatum, NULL);
1456 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1459 struct level_datum *levdatum;
1464 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1470 rc = next_entry(buf, fp, sizeof buf);
1474 len = le32_to_cpu(buf[0]);
1475 levdatum->isalias = le32_to_cpu(buf[1]);
1477 key = kmalloc(len + 1, GFP_ATOMIC);
1482 rc = next_entry(key, fp, len);
1487 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1488 if (!levdatum->level) {
1492 if (mls_read_level(levdatum->level, fp)) {
1497 rc = hashtab_insert(h, key, levdatum);
1503 sens_destroy(key, levdatum, NULL);
1507 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1510 struct cat_datum *catdatum;
1515 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1521 rc = next_entry(buf, fp, sizeof buf);
1525 len = le32_to_cpu(buf[0]);
1526 catdatum->value = le32_to_cpu(buf[1]);
1527 catdatum->isalias = le32_to_cpu(buf[2]);
1529 key = kmalloc(len + 1, GFP_ATOMIC);
1534 rc = next_entry(key, fp, len);
1539 rc = hashtab_insert(h, key, catdatum);
1546 cat_destroy(key, catdatum, NULL);
1550 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1562 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1564 struct user_datum *upper, *user;
1565 struct policydb *p = datap;
1568 upper = user = datum;
1569 while (upper->bounds) {
1570 struct ebitmap_node *node;
1573 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1574 printk(KERN_ERR "SELinux: user %s: "
1575 "too deep or looped boundary",
1580 upper = p->user_val_to_struct[upper->bounds - 1];
1581 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1582 if (ebitmap_get_bit(&upper->roles, bit))
1586 "SELinux: boundary violated policy: "
1587 "user=%s role=%s bounds=%s\n",
1588 p->p_user_val_to_name[user->value - 1],
1589 p->p_role_val_to_name[bit],
1590 p->p_user_val_to_name[upper->value - 1]);
1599 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1601 struct role_datum *upper, *role;
1602 struct policydb *p = datap;
1605 upper = role = datum;
1606 while (upper->bounds) {
1607 struct ebitmap_node *node;
1610 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1611 printk(KERN_ERR "SELinux: role %s: "
1612 "too deep or looped bounds\n",
1617 upper = p->role_val_to_struct[upper->bounds - 1];
1618 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1619 if (ebitmap_get_bit(&upper->types, bit))
1623 "SELinux: boundary violated policy: "
1624 "role=%s type=%s bounds=%s\n",
1625 p->p_role_val_to_name[role->value - 1],
1626 p->p_type_val_to_name[bit],
1627 p->p_role_val_to_name[upper->value - 1]);
1636 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1638 struct type_datum *upper;
1639 struct policydb *p = datap;
1643 while (upper->bounds) {
1644 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1645 printk(KERN_ERR "SELinux: type %s: "
1646 "too deep or looped boundary\n",
1651 upper = p->type_val_to_struct[upper->bounds - 1];
1652 if (upper->attribute) {
1653 printk(KERN_ERR "SELinux: type %s: "
1654 "bounded by attribute %s",
1656 p->p_type_val_to_name[upper->value - 1]);
1664 static int policydb_bounds_sanity_check(struct policydb *p)
1668 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1671 rc = hashtab_map(p->p_users.table,
1672 user_bounds_sanity_check, p);
1676 rc = hashtab_map(p->p_roles.table,
1677 role_bounds_sanity_check, p);
1681 rc = hashtab_map(p->p_types.table,
1682 type_bounds_sanity_check, p);
1689 extern int ss_initialized;
1691 u16 string_to_security_class(struct policydb *p, const char *name)
1693 struct class_datum *cladatum;
1695 cladatum = hashtab_search(p->p_classes.table, name);
1699 return cladatum->value;
1702 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1704 struct class_datum *cladatum;
1705 struct perm_datum *perdatum = NULL;
1706 struct common_datum *comdatum;
1708 if (!tclass || tclass > p->p_classes.nprim)
1711 cladatum = p->class_val_to_struct[tclass-1];
1712 comdatum = cladatum->comdatum;
1714 perdatum = hashtab_search(comdatum->permissions.table,
1717 perdatum = hashtab_search(cladatum->permissions.table,
1722 return 1U << (perdatum->value-1);
1725 static int range_read(struct policydb *p, void *fp)
1727 struct range_trans *rt = NULL;
1728 struct mls_range *r = NULL;
1733 if (p->policyvers < POLICYDB_VERSION_MLS)
1736 rc = next_entry(buf, fp, sizeof(u32));
1740 nel = le32_to_cpu(buf[0]);
1741 for (i = 0; i < nel; i++) {
1743 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1747 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1751 rt->source_type = le32_to_cpu(buf[0]);
1752 rt->target_type = le32_to_cpu(buf[1]);
1753 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1754 rc = next_entry(buf, fp, sizeof(u32));
1757 rt->target_class = le32_to_cpu(buf[0]);
1759 rt->target_class = p->process_class;
1762 if (!policydb_type_isvalid(p, rt->source_type) ||
1763 !policydb_type_isvalid(p, rt->target_type) ||
1764 !policydb_class_isvalid(p, rt->target_class))
1768 r = kzalloc(sizeof(*r), GFP_KERNEL);
1772 rc = mls_read_range_helper(r, fp);
1777 if (!mls_range_isvalid(p, r)) {
1778 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1782 rc = hashtab_insert(p->range_tr, rt, r);
1789 rangetr_hash_eval(p->range_tr);
1797 static int genfs_read(struct policydb *p, void *fp)
1800 u32 nel, nel2, len, len2;
1802 struct ocontext *l, *c;
1803 struct ocontext *newc = NULL;
1804 struct genfs *genfs_p, *genfs;
1805 struct genfs *newgenfs = NULL;
1807 rc = next_entry(buf, fp, sizeof(u32));
1810 nel = le32_to_cpu(buf[0]);
1812 for (i = 0; i < nel; i++) {
1813 rc = next_entry(buf, fp, sizeof(u32));
1816 len = le32_to_cpu(buf[0]);
1819 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1824 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1825 if (!newgenfs->fstype)
1828 rc = next_entry(newgenfs->fstype, fp, len);
1832 newgenfs->fstype[len] = 0;
1834 for (genfs_p = NULL, genfs = p->genfs; genfs;
1835 genfs_p = genfs, genfs = genfs->next) {
1837 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1838 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1842 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1845 newgenfs->next = genfs;
1847 genfs_p->next = newgenfs;
1849 p->genfs = newgenfs;
1853 rc = next_entry(buf, fp, sizeof(u32));
1857 nel2 = le32_to_cpu(buf[0]);
1858 for (j = 0; j < nel2; j++) {
1859 rc = next_entry(buf, fp, sizeof(u32));
1862 len = le32_to_cpu(buf[0]);
1865 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1870 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1874 rc = next_entry(newc->u.name, fp, len);
1877 newc->u.name[len] = 0;
1879 rc = next_entry(buf, fp, sizeof(u32));
1883 newc->v.sclass = le32_to_cpu(buf[0]);
1884 rc = context_read_and_validate(&newc->context[0], p, fp);
1888 for (l = NULL, c = genfs->head; c;
1889 l = c, c = c->next) {
1891 if (!strcmp(newc->u.name, c->u.name) &&
1892 (!c->v.sclass || !newc->v.sclass ||
1893 newc->v.sclass == c->v.sclass)) {
1894 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1895 genfs->fstype, c->u.name);
1898 len = strlen(newc->u.name);
1899 len2 = strlen(c->u.name);
1915 kfree(newgenfs->fstype);
1917 ocontext_destroy(newc, OCON_FSUSE);
1922 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1928 struct ocontext *l, *c;
1931 for (i = 0; i < info->ocon_num; i++) {
1932 rc = next_entry(buf, fp, sizeof(u32));
1935 nel = le32_to_cpu(buf[0]);
1938 for (j = 0; j < nel; j++) {
1940 c = kzalloc(sizeof(*c), GFP_KERNEL);
1946 p->ocontexts[i] = c;
1951 rc = next_entry(buf, fp, sizeof(u32));
1955 c->sid[0] = le32_to_cpu(buf[0]);
1956 rc = context_read_and_validate(&c->context[0], p, fp);
1962 rc = next_entry(buf, fp, sizeof(u32));
1965 len = le32_to_cpu(buf[0]);
1968 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1972 rc = next_entry(c->u.name, fp, len);
1977 rc = context_read_and_validate(&c->context[0], p, fp);
1980 rc = context_read_and_validate(&c->context[1], p, fp);
1985 rc = next_entry(buf, fp, sizeof(u32)*3);
1988 c->u.port.protocol = le32_to_cpu(buf[0]);
1989 c->u.port.low_port = le32_to_cpu(buf[1]);
1990 c->u.port.high_port = le32_to_cpu(buf[2]);
1991 rc = context_read_and_validate(&c->context[0], p, fp);
1996 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1999 c->u.node.addr = nodebuf[0]; /* network order */
2000 c->u.node.mask = nodebuf[1]; /* network order */
2001 rc = context_read_and_validate(&c->context[0], p, fp);
2006 rc = next_entry(buf, fp, sizeof(u32)*2);
2011 c->v.behavior = le32_to_cpu(buf[0]);
2012 if (c->v.behavior > SECURITY_FS_USE_NONE)
2016 len = le32_to_cpu(buf[1]);
2017 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2021 rc = next_entry(c->u.name, fp, len);
2025 rc = context_read_and_validate(&c->context[0], p, fp);
2032 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2035 for (k = 0; k < 4; k++)
2036 c->u.node6.addr[k] = nodebuf[k];
2037 for (k = 0; k < 4; k++)
2038 c->u.node6.mask[k] = nodebuf[k+4];
2039 rc = context_read_and_validate(&c->context[0], p, fp);
2053 * Read the configuration data from a policy database binary
2054 * representation file into a policy database structure.
2056 int policydb_read(struct policydb *p, void *fp)
2058 struct role_allow *ra, *lra;
2059 struct role_trans *tr, *ltr;
2062 u32 len, nprim, nel;
2065 struct policydb_compat_info *info;
2067 rc = policydb_init(p);
2071 /* Read the magic number and string length. */
2072 rc = next_entry(buf, fp, sizeof(u32) * 2);
2076 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2077 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2078 "not match expected magic number 0x%x\n",
2079 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2083 len = le32_to_cpu(buf[1]);
2084 if (len != strlen(POLICYDB_STRING)) {
2085 printk(KERN_ERR "SELinux: policydb string length %d does not "
2086 "match expected length %Zu\n",
2087 len, strlen(POLICYDB_STRING));
2090 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2091 if (!policydb_str) {
2092 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2093 "string of length %d\n", len);
2097 rc = next_entry(policydb_str, fp, len);
2099 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2100 kfree(policydb_str);
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);
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);
2119 p->policyvers = le32_to_cpu(buf[0]);
2120 if (p->policyvers < POLICYDB_VERSION_MIN ||
2121 p->policyvers > POLICYDB_VERSION_MAX) {
2122 printk(KERN_ERR "SELinux: policydb version %d does not match "
2123 "my version range %d-%d\n",
2124 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2128 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2131 if (p->policyvers < POLICYDB_VERSION_MLS) {
2132 printk(KERN_ERR "SELinux: security policydb version %d "
2133 "(MLS) not backwards compatible\n",
2138 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2139 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2141 if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
2142 ebitmap_read(&p->policycaps, fp) != 0)
2145 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
2146 ebitmap_read(&p->permissive_map, fp) != 0)
2149 info = policydb_lookup_compat(p->policyvers);
2151 printk(KERN_ERR "SELinux: unable to find policy compat info "
2152 "for version %d\n", p->policyvers);
2156 if (le32_to_cpu(buf[2]) != info->sym_num ||
2157 le32_to_cpu(buf[3]) != info->ocon_num) {
2158 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2159 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2160 le32_to_cpu(buf[3]),
2161 info->sym_num, info->ocon_num);
2165 for (i = 0; i < info->sym_num; i++) {
2166 rc = next_entry(buf, fp, sizeof(u32)*2);
2169 nprim = le32_to_cpu(buf[0]);
2170 nel = le32_to_cpu(buf[1]);
2171 for (j = 0; j < nel; j++) {
2172 rc = read_f[i](p, p->symtab[i].table, fp);
2177 p->symtab[i].nprim = nprim;
2180 rc = avtab_read(&p->te_avtab, fp, p);
2184 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2185 rc = cond_read_list(p, fp);
2190 rc = next_entry(buf, fp, sizeof(u32));
2193 nel = le32_to_cpu(buf[0]);
2195 for (i = 0; i < nel; i++) {
2196 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2205 rc = next_entry(buf, fp, sizeof(u32)*3);
2208 tr->role = le32_to_cpu(buf[0]);
2209 tr->type = le32_to_cpu(buf[1]);
2210 tr->new_role = le32_to_cpu(buf[2]);
2211 if (!policydb_role_isvalid(p, tr->role) ||
2212 !policydb_type_isvalid(p, tr->type) ||
2213 !policydb_role_isvalid(p, tr->new_role)) {
2220 rc = next_entry(buf, fp, sizeof(u32));
2223 nel = le32_to_cpu(buf[0]);
2225 for (i = 0; i < nel; i++) {
2226 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2235 rc = next_entry(buf, fp, sizeof(u32)*2);
2238 ra->role = le32_to_cpu(buf[0]);
2239 ra->new_role = le32_to_cpu(buf[1]);
2240 if (!policydb_role_isvalid(p, ra->role) ||
2241 !policydb_role_isvalid(p, ra->new_role)) {
2248 rc = policydb_index_classes(p);
2252 rc = policydb_index_others(p);
2256 p->process_class = string_to_security_class(p, "process");
2257 if (!p->process_class)
2259 p->process_trans_perms = string_to_av_perm(p, p->process_class,
2261 p->process_trans_perms |= string_to_av_perm(p, p->process_class,
2263 if (!p->process_trans_perms)
2266 rc = ocontext_read(p, info, fp);
2270 rc = genfs_read(p, fp);
2274 rc = range_read(p, fp);
2279 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2281 GFP_KERNEL | __GFP_ZERO);
2282 if (!p->type_attr_map_array)
2285 /* preallocate so we don't have to worry about the put ever failing */
2286 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2287 GFP_KERNEL | __GFP_ZERO);
2291 for (i = 0; i < p->p_types.nprim; i++) {
2292 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2296 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2297 rc = ebitmap_read(e, fp);
2301 /* add the type itself as the degenerate case */
2302 rc = ebitmap_set_bit(e, i, 1);
2307 rc = policydb_bounds_sanity_check(p);
2317 policydb_destroy(p);
2322 * Write a MLS level structure to a policydb binary
2323 * representation file.
2325 static int mls_write_level(struct mls_level *l, void *fp)
2330 buf[0] = cpu_to_le32(l->sens);
2331 rc = put_entry(buf, sizeof(u32), 1, fp);
2335 rc = ebitmap_write(&l->cat, fp);
2343 * Write a MLS range structure to a policydb binary
2344 * representation file.
2346 static int mls_write_range_helper(struct mls_range *r, void *fp)
2352 eq = mls_level_eq(&r->level[1], &r->level[0]);
2358 buf[0] = cpu_to_le32(items-1);
2359 buf[1] = cpu_to_le32(r->level[0].sens);
2361 buf[2] = cpu_to_le32(r->level[1].sens);
2363 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2365 rc = put_entry(buf, sizeof(u32), items, fp);
2369 rc = ebitmap_write(&r->level[0].cat, fp);
2373 rc = ebitmap_write(&r->level[1].cat, fp);
2381 static int sens_write(void *vkey, void *datum, void *ptr)
2384 struct level_datum *levdatum = datum;
2385 struct policy_data *pd = ptr;
2392 buf[0] = cpu_to_le32(len);
2393 buf[1] = cpu_to_le32(levdatum->isalias);
2394 rc = put_entry(buf, sizeof(u32), 2, fp);
2398 rc = put_entry(key, 1, len, fp);
2402 rc = mls_write_level(levdatum->level, fp);
2409 static int cat_write(void *vkey, void *datum, void *ptr)
2412 struct cat_datum *catdatum = datum;
2413 struct policy_data *pd = ptr;
2420 buf[0] = cpu_to_le32(len);
2421 buf[1] = cpu_to_le32(catdatum->value);
2422 buf[2] = cpu_to_le32(catdatum->isalias);
2423 rc = put_entry(buf, sizeof(u32), 3, fp);
2427 rc = put_entry(key, 1, len, fp);
2434 static int role_trans_write(struct role_trans *r, void *fp)
2436 struct role_trans *tr;
2442 for (tr = r; tr; tr = tr->next)
2444 buf[0] = cpu_to_le32(nel);
2445 rc = put_entry(buf, sizeof(u32), 1, fp);
2448 for (tr = r; tr; tr = tr->next) {
2449 buf[0] = cpu_to_le32(tr->role);
2450 buf[1] = cpu_to_le32(tr->type);
2451 buf[2] = cpu_to_le32(tr->new_role);
2452 rc = put_entry(buf, sizeof(u32), 3, fp);
2460 static int role_allow_write(struct role_allow *r, void *fp)
2462 struct role_allow *ra;
2468 for (ra = r; ra; ra = ra->next)
2470 buf[0] = cpu_to_le32(nel);
2471 rc = put_entry(buf, sizeof(u32), 1, fp);
2474 for (ra = r; ra; ra = ra->next) {
2475 buf[0] = cpu_to_le32(ra->role);
2476 buf[1] = cpu_to_le32(ra->new_role);
2477 rc = put_entry(buf, sizeof(u32), 2, fp);
2485 * Write a security context structure
2486 * to a policydb binary representation file.
2488 static int context_write(struct policydb *p, struct context *c,
2494 buf[0] = cpu_to_le32(c->user);
2495 buf[1] = cpu_to_le32(c->role);
2496 buf[2] = cpu_to_le32(c->type);
2498 rc = put_entry(buf, sizeof(u32), 3, fp);
2502 rc = mls_write_range_helper(&c->range, fp);
2510 * The following *_write functions are used to
2511 * write the symbol data to a policy database
2512 * binary representation file.
2515 static int perm_write(void *vkey, void *datum, void *fp)
2518 struct perm_datum *perdatum = datum;
2524 buf[0] = cpu_to_le32(len);
2525 buf[1] = cpu_to_le32(perdatum->value);
2526 rc = put_entry(buf, sizeof(u32), 2, fp);
2530 rc = put_entry(key, 1, len, fp);
2537 static int common_write(void *vkey, void *datum, void *ptr)
2540 struct common_datum *comdatum = datum;
2541 struct policy_data *pd = ptr;
2548 buf[0] = cpu_to_le32(len);
2549 buf[1] = cpu_to_le32(comdatum->value);
2550 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2551 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2552 rc = put_entry(buf, sizeof(u32), 4, fp);
2556 rc = put_entry(key, 1, len, fp);
2560 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2567 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2570 struct constraint_node *c;
2571 struct constraint_expr *e;
2576 for (c = node; c; c = c->next) {
2578 for (e = c->expr; e; e = e->next)
2580 buf[0] = cpu_to_le32(c->permissions);
2581 buf[1] = cpu_to_le32(nel);
2582 rc = put_entry(buf, sizeof(u32), 2, fp);
2585 for (e = c->expr; e; e = e->next) {
2586 buf[0] = cpu_to_le32(e->expr_type);
2587 buf[1] = cpu_to_le32(e->attr);
2588 buf[2] = cpu_to_le32(e->op);
2589 rc = put_entry(buf, sizeof(u32), 3, fp);
2593 switch (e->expr_type) {
2595 rc = ebitmap_write(&e->names, fp);
2608 static int class_write(void *vkey, void *datum, void *ptr)
2611 struct class_datum *cladatum = datum;
2612 struct policy_data *pd = ptr;
2614 struct policydb *p = pd->p;
2615 struct constraint_node *c;
2622 if (cladatum->comkey)
2623 len2 = strlen(cladatum->comkey);
2628 for (c = cladatum->constraints; c; c = c->next)
2631 buf[0] = cpu_to_le32(len);
2632 buf[1] = cpu_to_le32(len2);
2633 buf[2] = cpu_to_le32(cladatum->value);
2634 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2635 if (cladatum->permissions.table)
2636 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2639 buf[5] = cpu_to_le32(ncons);
2640 rc = put_entry(buf, sizeof(u32), 6, fp);
2644 rc = put_entry(key, 1, len, fp);
2648 if (cladatum->comkey) {
2649 rc = put_entry(cladatum->comkey, 1, len2, fp);
2654 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2658 rc = write_cons_helper(p, cladatum->constraints, fp);
2662 /* write out the validatetrans rule */
2664 for (c = cladatum->validatetrans; c; c = c->next)
2667 buf[0] = cpu_to_le32(ncons);
2668 rc = put_entry(buf, sizeof(u32), 1, fp);
2672 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2679 static int role_write(void *vkey, void *datum, void *ptr)
2682 struct role_datum *role = datum;
2683 struct policy_data *pd = ptr;
2685 struct policydb *p = pd->p;
2692 buf[items++] = cpu_to_le32(len);
2693 buf[items++] = cpu_to_le32(role->value);
2694 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2695 buf[items++] = cpu_to_le32(role->bounds);
2697 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2699 rc = put_entry(buf, sizeof(u32), items, fp);
2703 rc = put_entry(key, 1, len, fp);
2707 rc = ebitmap_write(&role->dominates, fp);
2711 rc = ebitmap_write(&role->types, fp);
2718 static int type_write(void *vkey, void *datum, void *ptr)
2721 struct type_datum *typdatum = datum;
2722 struct policy_data *pd = ptr;
2723 struct policydb *p = pd->p;
2731 buf[items++] = cpu_to_le32(len);
2732 buf[items++] = cpu_to_le32(typdatum->value);
2733 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2736 if (typdatum->primary)
2737 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2739 if (typdatum->attribute)
2740 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2742 buf[items++] = cpu_to_le32(properties);
2743 buf[items++] = cpu_to_le32(typdatum->bounds);
2745 buf[items++] = cpu_to_le32(typdatum->primary);
2747 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2748 rc = put_entry(buf, sizeof(u32), items, fp);
2752 rc = put_entry(key, 1, len, fp);
2759 static int user_write(void *vkey, void *datum, void *ptr)
2762 struct user_datum *usrdatum = datum;
2763 struct policy_data *pd = ptr;
2764 struct policydb *p = pd->p;
2772 buf[items++] = cpu_to_le32(len);
2773 buf[items++] = cpu_to_le32(usrdatum->value);
2774 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2775 buf[items++] = cpu_to_le32(usrdatum->bounds);
2776 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2777 rc = put_entry(buf, sizeof(u32), items, fp);
2781 rc = put_entry(key, 1, len, fp);
2785 rc = ebitmap_write(&usrdatum->roles, fp);
2789 rc = mls_write_range_helper(&usrdatum->range, fp);
2793 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2800 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2813 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2816 unsigned int i, j, rc;
2821 for (i = 0; i < info->ocon_num; i++) {
2823 for (c = p->ocontexts[i]; c; c = c->next)
2825 buf[0] = cpu_to_le32(nel);
2826 rc = put_entry(buf, sizeof(u32), 1, fp);
2829 for (c = p->ocontexts[i]; c; c = c->next) {
2832 buf[0] = cpu_to_le32(c->sid[0]);
2833 rc = put_entry(buf, sizeof(u32), 1, fp);
2836 rc = context_write(p, &c->context[0], fp);
2842 len = strlen(c->u.name);
2843 buf[0] = cpu_to_le32(len);
2844 rc = put_entry(buf, sizeof(u32), 1, fp);
2847 rc = put_entry(c->u.name, 1, len, fp);
2850 rc = context_write(p, &c->context[0], fp);
2853 rc = context_write(p, &c->context[1], fp);
2858 buf[0] = cpu_to_le32(c->u.port.protocol);
2859 buf[1] = cpu_to_le32(c->u.port.low_port);
2860 buf[2] = cpu_to_le32(c->u.port.high_port);
2861 rc = put_entry(buf, sizeof(u32), 3, fp);
2864 rc = context_write(p, &c->context[0], fp);
2869 nodebuf[0] = c->u.node.addr; /* network order */
2870 nodebuf[1] = c->u.node.mask; /* network order */
2871 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2874 rc = context_write(p, &c->context[0], fp);
2879 buf[0] = cpu_to_le32(c->v.behavior);
2880 len = strlen(c->u.name);
2881 buf[1] = cpu_to_le32(len);
2882 rc = put_entry(buf, sizeof(u32), 2, fp);
2885 rc = put_entry(c->u.name, 1, len, fp);
2888 rc = context_write(p, &c->context[0], fp);
2893 for (j = 0; j < 4; j++)
2894 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2895 for (j = 0; j < 4; j++)
2896 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2897 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2900 rc = context_write(p, &c->context[0], fp);
2910 static int genfs_write(struct policydb *p, void *fp)
2912 struct genfs *genfs;
2919 for (genfs = p->genfs; genfs; genfs = genfs->next)
2921 buf[0] = cpu_to_le32(len);
2922 rc = put_entry(buf, sizeof(u32), 1, fp);
2925 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2926 len = strlen(genfs->fstype);
2927 buf[0] = cpu_to_le32(len);
2928 rc = put_entry(buf, sizeof(u32), 1, fp);
2931 rc = put_entry(genfs->fstype, 1, len, fp);
2935 for (c = genfs->head; c; c = c->next)
2937 buf[0] = cpu_to_le32(len);
2938 rc = put_entry(buf, sizeof(u32), 1, fp);
2941 for (c = genfs->head; c; c = c->next) {
2942 len = strlen(c->u.name);
2943 buf[0] = cpu_to_le32(len);
2944 rc = put_entry(buf, sizeof(u32), 1, fp);
2947 rc = put_entry(c->u.name, 1, len, fp);
2950 buf[0] = cpu_to_le32(c->v.sclass);
2951 rc = put_entry(buf, sizeof(u32), 1, fp);
2954 rc = context_write(p, &c->context[0], fp);
2962 static int range_count(void *key, void *data, void *ptr)
2970 static int range_write_helper(void *key, void *data, void *ptr)
2973 struct range_trans *rt = key;
2974 struct mls_range *r = data;
2975 struct policy_data *pd = ptr;
2977 struct policydb *p = pd->p;
2980 buf[0] = cpu_to_le32(rt->source_type);
2981 buf[1] = cpu_to_le32(rt->target_type);
2982 rc = put_entry(buf, sizeof(u32), 2, fp);
2985 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2986 buf[0] = cpu_to_le32(rt->target_class);
2987 rc = put_entry(buf, sizeof(u32), 1, fp);
2991 rc = mls_write_range_helper(r, fp);
2998 static int range_write(struct policydb *p, void *fp)
3003 struct policy_data pd;
3008 /* count the number of entries in the hashtab */
3010 rc = hashtab_map(p->range_tr, range_count, &nel);
3014 buf[0] = cpu_to_le32(nel);
3015 rc = put_entry(buf, sizeof(u32), 1, fp);
3019 /* actually write all of the entries */
3020 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3028 * Write the configuration data in a policy database
3029 * structure to a policy database binary representation
3032 int policydb_write(struct policydb *p, void *fp)
3034 unsigned int i, num_syms;
3039 struct policydb_compat_info *info;
3042 * refuse to write policy older than compressed avtab
3043 * to simplify the writer. There are other tests dropped
3044 * since we assume this throughout the writer code. Be
3045 * careful if you ever try to remove this restriction
3047 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3048 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3049 " Because it is less than version %d\n", p->policyvers,
3050 POLICYDB_VERSION_AVTAB);
3056 config |= POLICYDB_CONFIG_MLS;
3058 if (p->reject_unknown)
3059 config |= REJECT_UNKNOWN;
3060 if (p->allow_unknown)
3061 config |= ALLOW_UNKNOWN;
3063 /* Write the magic number and string identifiers. */
3064 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3065 len = strlen(POLICYDB_STRING);
3066 buf[1] = cpu_to_le32(len);
3067 rc = put_entry(buf, sizeof(u32), 2, fp);
3070 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3074 /* Write the version, config, and table sizes. */
3075 info = policydb_lookup_compat(p->policyvers);
3077 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3078 "version %d", p->policyvers);
3082 buf[0] = cpu_to_le32(p->policyvers);
3083 buf[1] = cpu_to_le32(config);
3084 buf[2] = cpu_to_le32(info->sym_num);
3085 buf[3] = cpu_to_le32(info->ocon_num);
3087 rc = put_entry(buf, sizeof(u32), 4, fp);
3091 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3092 rc = ebitmap_write(&p->policycaps, fp);
3097 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3098 rc = ebitmap_write(&p->permissive_map, fp);
3103 num_syms = info->sym_num;
3104 for (i = 0; i < num_syms; i++) {
3105 struct policy_data pd;
3110 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3111 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3113 rc = put_entry(buf, sizeof(u32), 2, fp);
3116 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3121 rc = avtab_write(p, &p->te_avtab, fp);
3125 rc = cond_write_list(p, p->cond_list, fp);
3129 rc = role_trans_write(p->role_tr, fp);
3133 rc = role_allow_write(p->role_allow, fp);
3137 rc = ocontext_write(p, info, fp);
3141 rc = genfs_write(p, fp);
3145 rc = range_write(p, fp);
3149 for (i = 0; i < p->p_types.nprim; i++) {
3150 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3153 rc = ebitmap_write(e, fp);