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;
152 role = kzalloc(sizeof(*role), GFP_KERNEL);
157 role->value = ++p->p_roles.nprim;
158 if (role->value != OBJECT_R_VAL)
162 key = kstrdup(OBJECT_R, GFP_KERNEL);
166 rc = hashtab_insert(p->p_roles.table, key, role);
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;
189 v = key1->source_type - key2->source_type;
193 v = key1->target_type - key2->target_type;
197 v = key1->target_class - key2->target_class;
203 * Initialize a policy database structure.
205 static int policydb_init(struct policydb *p)
209 memset(p, 0, sizeof(*p));
211 for (i = 0; i < SYM_NUM; i++) {
212 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
217 rc = avtab_init(&p->te_avtab);
225 rc = cond_policydb_init(p);
229 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
233 ebitmap_init(&p->policycaps);
234 ebitmap_init(&p->permissive_map);
238 for (i = 0; i < SYM_NUM; i++)
239 hashtab_destroy(p->symtab[i].table);
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)
256 struct common_datum *comdatum;
260 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
262 p->p_common_val_to_name[comdatum->value - 1] = key;
266 static int class_index(void *key, void *datum, void *datap)
269 struct class_datum *cladatum;
273 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
275 p->p_class_val_to_name[cladatum->value - 1] = key;
276 p->class_val_to_struct[cladatum->value - 1] = cladatum;
280 static int role_index(void *key, void *datum, void *datap)
283 struct role_datum *role;
288 || role->value > p->p_roles.nprim
289 || role->bounds > p->p_roles.nprim)
291 p->p_role_val_to_name[role->value - 1] = key;
292 p->role_val_to_struct[role->value - 1] = role;
296 static int type_index(void *key, void *datum, void *datap)
299 struct type_datum *typdatum;
304 if (typdatum->primary) {
306 || typdatum->value > p->p_types.nprim
307 || typdatum->bounds > p->p_types.nprim)
309 p->p_type_val_to_name[typdatum->value - 1] = key;
310 /* this flex array was all preallocated, this cannot fail */
311 if (flex_array_put_ptr(p->type_val_to_struct_array,
312 typdatum->value - 1, typdatum,
313 GFP_KERNEL | __GFP_ZERO))
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)
395 p->p_common_val_to_name =
396 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
397 if (!p->p_common_val_to_name)
400 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)
411 p->p_class_val_to_name =
412 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
413 if (!p->p_class_val_to_name)
416 rc = hashtab_map(p->p_classes.table, class_index, p);
422 static void symtab_hash_eval(struct symtab *s)
426 for (i = 0; i < SYM_NUM; i++) {
427 struct hashtab *h = s[i].table;
428 struct hashtab_info info;
430 hashtab_stat(h, &info);
431 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
432 "longest chain length %d\n", symtab_name[i], h->nel,
433 info.slots_used, h->size, info.max_chain_len);
437 static void rangetr_hash_eval(struct hashtab *h)
439 struct hashtab_info info;
441 hashtab_stat(h, &info);
442 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
443 "longest chain length %d\n", h->nel,
444 info.slots_used, h->size, info.max_chain_len);
447 static inline void rangetr_hash_eval(struct hashtab *h)
453 * Define the other val_to_name and val_to_struct arrays
454 * in a policy database structure.
456 * Caller must clean up on failure.
458 static int policydb_index_others(struct policydb *p)
462 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
463 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
465 printk(", %d sens, %d cats", p->p_levels.nprim,
469 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
470 p->p_classes.nprim, p->te_avtab.nel);
473 avtab_hash_eval(&p->te_avtab, "rules");
474 symtab_hash_eval(p->symtab);
478 p->role_val_to_struct =
479 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
481 if (!p->role_val_to_struct)
485 p->user_val_to_struct =
486 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
488 if (!p->user_val_to_struct)
491 /* Yes, I want the sizeof the pointer, not the structure */
493 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
495 GFP_KERNEL | __GFP_ZERO);
496 if (!p->type_val_to_struct_array)
499 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
500 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
505 if (cond_init_bool_indexes(p))
508 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])
514 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
524 * The following *_destroy functions are used to
525 * free any memory allocated for each kind of
526 * symbol data in the policy database.
529 static int perm_destroy(void *key, void *datum, void *p)
536 static int common_destroy(void *key, void *datum, void *p)
538 struct common_datum *comdatum;
543 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
544 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;
559 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
560 hashtab_destroy(cladatum->permissions.table);
561 constraint = cladatum->constraints;
563 e = constraint->expr;
565 ebitmap_destroy(&e->names);
571 constraint = constraint->next;
575 constraint = cladatum->validatetrans;
577 e = constraint->expr;
579 ebitmap_destroy(&e->names);
585 constraint = constraint->next;
589 kfree(cladatum->comkey);
595 static int role_destroy(void *key, void *datum, void *p)
597 struct role_datum *role;
602 ebitmap_destroy(&role->dominates);
603 ebitmap_destroy(&role->types);
609 static int type_destroy(void *key, void *datum, void *p)
616 static int user_destroy(void *key, void *datum, void *p)
618 struct user_datum *usrdatum;
623 ebitmap_destroy(&usrdatum->roles);
624 ebitmap_destroy(&usrdatum->range.level[0].cat);
625 ebitmap_destroy(&usrdatum->range.level[1].cat);
626 ebitmap_destroy(&usrdatum->dfltlevel.cat);
632 static int sens_destroy(void *key, void *datum, void *p)
634 struct level_datum *levdatum;
639 ebitmap_destroy(&levdatum->level->cat);
640 kfree(levdatum->level);
646 static int cat_destroy(void *key, void *datum, void *p)
653 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
665 static int range_tr_destroy(void *key, void *datum, void *p)
667 struct mls_range *rt = datum;
669 ebitmap_destroy(&rt->level[0].cat);
670 ebitmap_destroy(&rt->level[1].cat);
676 static void ocontext_destroy(struct ocontext *c, int i)
681 context_destroy(&c->context[0]);
682 context_destroy(&c->context[1]);
683 if (i == OCON_ISID || i == OCON_FS ||
684 i == OCON_NETIF || i == OCON_FSUSE)
690 * Free any memory allocated by a policy database structure.
692 void policydb_destroy(struct policydb *p)
694 struct ocontext *c, *ctmp;
695 struct genfs *g, *gtmp;
697 struct role_allow *ra, *lra = NULL;
698 struct role_trans *tr, *ltr = NULL;
700 for (i = 0; i < SYM_NUM; i++) {
702 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
703 hashtab_destroy(p->symtab[i].table);
706 for (i = 0; i < SYM_NUM; i++)
707 kfree(p->sym_val_to_name[i]);
709 kfree(p->class_val_to_struct);
710 kfree(p->role_val_to_struct);
711 kfree(p->user_val_to_struct);
712 if (p->type_val_to_struct_array)
713 flex_array_free(p->type_val_to_struct_array);
715 avtab_destroy(&p->te_avtab);
717 for (i = 0; i < OCON_NUM; i++) {
723 ocontext_destroy(ctmp, i);
725 p->ocontexts[i] = NULL;
736 ocontext_destroy(ctmp, OCON_FSUSE);
744 cond_policydb_destroy(p);
746 for (tr = p->role_tr; tr; tr = tr->next) {
753 for (ra = p->role_allow; ra; ra = ra->next) {
760 hashtab_map(p->range_tr, range_tr_destroy, NULL);
761 hashtab_destroy(p->range_tr);
763 if (p->type_attr_map_array) {
764 for (i = 0; i < p->p_types.nprim; i++) {
767 e = flex_array_get(p->type_attr_map_array, i);
772 flex_array_free(p->type_attr_map_array);
774 ebitmap_destroy(&p->policycaps);
775 ebitmap_destroy(&p->permissive_map);
781 * Load the initial SIDs specified in a policy database
782 * structure into a SID table.
784 int policydb_load_isids(struct policydb *p, struct sidtab *s)
786 struct ocontext *head, *c;
791 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
795 head = p->ocontexts[OCON_ISID];
796 for (c = head; c; c = c->next) {
798 if (!c->context[0].user) {
799 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
804 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
806 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
816 int policydb_class_isvalid(struct policydb *p, unsigned int class)
818 if (!class || class > p->p_classes.nprim)
823 int policydb_role_isvalid(struct policydb *p, unsigned int role)
825 if (!role || role > p->p_roles.nprim)
830 int policydb_type_isvalid(struct policydb *p, unsigned int type)
832 if (!type || type > p->p_types.nprim)
838 * Return 1 if the fields in the security context
839 * structure `c' are valid. Return 0 otherwise.
841 int policydb_context_isvalid(struct policydb *p, struct context *c)
843 struct role_datum *role;
844 struct user_datum *usrdatum;
846 if (!c->role || c->role > p->p_roles.nprim)
849 if (!c->user || c->user > p->p_users.nprim)
852 if (!c->type || c->type > p->p_types.nprim)
855 if (c->role != OBJECT_R_VAL) {
857 * Role must be authorized for the type.
859 role = p->role_val_to_struct[c->role - 1];
860 if (!ebitmap_get_bit(&role->types, c->type - 1))
861 /* role may not be associated with type */
865 * User must be authorized for the role.
867 usrdatum = p->user_val_to_struct[c->user - 1];
871 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
872 /* user may not be associated with role */
876 if (!mls_context_isvalid(p, c))
883 * Read a MLS range structure from a policydb binary
884 * representation file.
886 static int mls_read_range_helper(struct mls_range *r, void *fp)
892 rc = next_entry(buf, fp, sizeof(u32));
897 items = le32_to_cpu(buf[0]);
898 if (items > ARRAY_SIZE(buf)) {
899 printk(KERN_ERR "SELinux: mls: range overflow\n");
903 rc = next_entry(buf, fp, sizeof(u32) * items);
905 printk(KERN_ERR "SELinux: mls: truncated range\n");
909 r->level[0].sens = le32_to_cpu(buf[0]);
911 r->level[1].sens = le32_to_cpu(buf[1]);
913 r->level[1].sens = r->level[0].sens;
915 rc = ebitmap_read(&r->level[0].cat, fp);
917 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
921 rc = ebitmap_read(&r->level[1].cat, fp);
923 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
927 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
929 printk(KERN_ERR "SELinux: mls: out of memory\n");
936 ebitmap_destroy(&r->level[0].cat);
942 * Read and validate a security context structure
943 * from a policydb binary representation file.
945 static int context_read_and_validate(struct context *c,
952 rc = next_entry(buf, fp, sizeof buf);
954 printk(KERN_ERR "SELinux: context truncated\n");
957 c->user = le32_to_cpu(buf[0]);
958 c->role = le32_to_cpu(buf[1]);
959 c->type = le32_to_cpu(buf[2]);
960 if (p->policyvers >= POLICYDB_VERSION_MLS) {
961 rc = mls_read_range_helper(&c->range, fp);
963 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
969 if (!policydb_context_isvalid(p, c)) {
970 printk(KERN_ERR "SELinux: invalid security context\n");
980 * The following *_read functions are used to
981 * read the symbol data from a policy database
982 * binary representation file.
985 static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
988 struct perm_datum *perdatum;
994 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
998 rc = next_entry(buf, fp, sizeof buf);
1002 len = le32_to_cpu(buf[0]);
1003 perdatum->value = le32_to_cpu(buf[1]);
1006 key = kmalloc(len + 1, GFP_KERNEL);
1010 rc = next_entry(key, fp, len);
1015 rc = hashtab_insert(h, key, perdatum);
1021 perm_destroy(key, perdatum, NULL);
1025 static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1028 struct common_datum *comdatum;
1034 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
1038 rc = next_entry(buf, fp, sizeof buf);
1042 len = le32_to_cpu(buf[0]);
1043 comdatum->value = le32_to_cpu(buf[1]);
1045 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1048 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1049 nel = le32_to_cpu(buf[3]);
1052 key = kmalloc(len + 1, GFP_KERNEL);
1056 rc = next_entry(key, fp, len);
1061 for (i = 0; i < nel; i++) {
1062 rc = perm_read(p, comdatum->permissions.table, fp);
1067 rc = hashtab_insert(h, key, comdatum);
1072 common_destroy(key, comdatum, NULL);
1076 static int read_cons_helper(struct constraint_node **nodep, int ncons,
1077 int allowxtarget, void *fp)
1079 struct constraint_node *c, *lc;
1080 struct constraint_expr *e, *le;
1083 int rc, i, j, depth;
1086 for (i = 0; i < ncons; i++) {
1087 c = kzalloc(sizeof(*c), GFP_KERNEL);
1096 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1099 c->permissions = le32_to_cpu(buf[0]);
1100 nexpr = le32_to_cpu(buf[1]);
1103 for (j = 0; j < nexpr; j++) {
1104 e = kzalloc(sizeof(*e), GFP_KERNEL);
1113 rc = next_entry(buf, fp, (sizeof(u32) * 3));
1116 e->expr_type = le32_to_cpu(buf[0]);
1117 e->attr = le32_to_cpu(buf[1]);
1118 e->op = le32_to_cpu(buf[2]);
1120 switch (e->expr_type) {
1132 if (depth == (CEXPR_MAXDEPTH - 1))
1137 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1139 if (depth == (CEXPR_MAXDEPTH - 1))
1142 rc = ebitmap_read(&e->names, fp);
1159 static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1162 struct class_datum *cladatum;
1164 u32 len, len2, ncons, nel;
1168 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
1172 rc = next_entry(buf, fp, sizeof(u32)*6);
1176 len = le32_to_cpu(buf[0]);
1177 len2 = le32_to_cpu(buf[1]);
1178 cladatum->value = le32_to_cpu(buf[2]);
1180 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1183 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1184 nel = le32_to_cpu(buf[4]);
1186 ncons = le32_to_cpu(buf[5]);
1189 key = kmalloc(len + 1, GFP_KERNEL);
1193 rc = next_entry(key, fp, len);
1200 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
1201 if (!cladatum->comkey)
1203 rc = next_entry(cladatum->comkey, fp, len2);
1206 cladatum->comkey[len2] = '\0';
1209 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
1210 if (!cladatum->comdatum) {
1211 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
1215 for (i = 0; i < nel; i++) {
1216 rc = perm_read(p, cladatum->permissions.table, fp);
1221 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1225 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1226 /* grab the validatetrans rules */
1227 rc = next_entry(buf, fp, sizeof(u32));
1230 ncons = le32_to_cpu(buf[0]);
1231 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1236 rc = hashtab_insert(h, key, cladatum);
1242 cls_destroy(key, cladatum, NULL);
1246 static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1249 struct role_datum *role;
1250 int rc, to_read = 2;
1255 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]);
1272 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) {
1291 if (role->value != OBJECT_R_VAL) {
1292 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
1293 OBJECT_R, role->value);
1300 rc = hashtab_insert(h, key, role);
1305 role_destroy(key, role, NULL);
1309 static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1312 struct type_datum *typdatum;
1313 int rc, to_read = 3;
1318 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
1322 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1325 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1329 len = le32_to_cpu(buf[0]);
1330 typdatum->value = le32_to_cpu(buf[1]);
1331 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1332 u32 prop = le32_to_cpu(buf[2]);
1334 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1335 typdatum->primary = 1;
1336 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1337 typdatum->attribute = 1;
1339 typdatum->bounds = le32_to_cpu(buf[3]);
1341 typdatum->primary = le32_to_cpu(buf[2]);
1345 key = kmalloc(len + 1, GFP_KERNEL);
1348 rc = next_entry(key, fp, len);
1353 rc = hashtab_insert(h, key, typdatum);
1358 type_destroy(key, typdatum, NULL);
1364 * Read a MLS level structure from a policydb binary
1365 * representation file.
1367 static int mls_read_level(struct mls_level *lp, void *fp)
1372 memset(lp, 0, sizeof(*lp));
1374 rc = next_entry(buf, fp, sizeof buf);
1376 printk(KERN_ERR "SELinux: mls: truncated level\n");
1379 lp->sens = le32_to_cpu(buf[0]);
1381 rc = ebitmap_read(&lp->cat, fp);
1383 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1389 static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1392 struct user_datum *usrdatum;
1393 int rc, to_read = 2;
1398 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
1402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1405 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
1409 len = le32_to_cpu(buf[0]);
1410 usrdatum->value = le32_to_cpu(buf[1]);
1411 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1412 usrdatum->bounds = le32_to_cpu(buf[2]);
1415 key = kmalloc(len + 1, GFP_KERNEL);
1418 rc = next_entry(key, fp, len);
1423 rc = ebitmap_read(&usrdatum->roles, fp);
1427 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1428 rc = mls_read_range_helper(&usrdatum->range, fp);
1431 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1436 rc = hashtab_insert(h, key, usrdatum);
1441 user_destroy(key, usrdatum, NULL);
1445 static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1448 struct level_datum *levdatum;
1454 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
1458 rc = next_entry(buf, fp, sizeof buf);
1462 len = le32_to_cpu(buf[0]);
1463 levdatum->isalias = le32_to_cpu(buf[1]);
1466 key = kmalloc(len + 1, GFP_ATOMIC);
1469 rc = next_entry(key, fp, len);
1475 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
1476 if (!levdatum->level)
1479 rc = mls_read_level(levdatum->level, fp);
1483 rc = hashtab_insert(h, key, levdatum);
1488 sens_destroy(key, levdatum, NULL);
1492 static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1495 struct cat_datum *catdatum;
1501 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
1505 rc = next_entry(buf, fp, sizeof buf);
1509 len = le32_to_cpu(buf[0]);
1510 catdatum->value = le32_to_cpu(buf[1]);
1511 catdatum->isalias = le32_to_cpu(buf[2]);
1514 key = kmalloc(len + 1, GFP_ATOMIC);
1517 rc = next_entry(key, fp, len);
1522 rc = hashtab_insert(h, key, catdatum);
1527 cat_destroy(key, catdatum, NULL);
1531 static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1543 static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1545 struct user_datum *upper, *user;
1546 struct policydb *p = datap;
1549 upper = user = datum;
1550 while (upper->bounds) {
1551 struct ebitmap_node *node;
1554 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1555 printk(KERN_ERR "SELinux: user %s: "
1556 "too deep or looped boundary",
1561 upper = p->user_val_to_struct[upper->bounds - 1];
1562 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1563 if (ebitmap_get_bit(&upper->roles, bit))
1567 "SELinux: boundary violated policy: "
1568 "user=%s role=%s bounds=%s\n",
1569 p->p_user_val_to_name[user->value - 1],
1570 p->p_role_val_to_name[bit],
1571 p->p_user_val_to_name[upper->value - 1]);
1580 static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1582 struct role_datum *upper, *role;
1583 struct policydb *p = datap;
1586 upper = role = datum;
1587 while (upper->bounds) {
1588 struct ebitmap_node *node;
1591 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1592 printk(KERN_ERR "SELinux: role %s: "
1593 "too deep or looped bounds\n",
1598 upper = p->role_val_to_struct[upper->bounds - 1];
1599 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1600 if (ebitmap_get_bit(&upper->types, bit))
1604 "SELinux: boundary violated policy: "
1605 "role=%s type=%s bounds=%s\n",
1606 p->p_role_val_to_name[role->value - 1],
1607 p->p_type_val_to_name[bit],
1608 p->p_role_val_to_name[upper->value - 1]);
1617 static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1619 struct type_datum *upper;
1620 struct policydb *p = datap;
1624 while (upper->bounds) {
1625 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1626 printk(KERN_ERR "SELinux: type %s: "
1627 "too deep or looped boundary\n",
1632 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1636 if (upper->attribute) {
1637 printk(KERN_ERR "SELinux: type %s: "
1638 "bounded by attribute %s",
1640 p->p_type_val_to_name[upper->value - 1]);
1648 static int policydb_bounds_sanity_check(struct policydb *p)
1652 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1655 rc = hashtab_map(p->p_users.table,
1656 user_bounds_sanity_check, p);
1660 rc = hashtab_map(p->p_roles.table,
1661 role_bounds_sanity_check, p);
1665 rc = hashtab_map(p->p_types.table,
1666 type_bounds_sanity_check, p);
1673 extern int ss_initialized;
1675 u16 string_to_security_class(struct policydb *p, const char *name)
1677 struct class_datum *cladatum;
1679 cladatum = hashtab_search(p->p_classes.table, name);
1683 return cladatum->value;
1686 u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1688 struct class_datum *cladatum;
1689 struct perm_datum *perdatum = NULL;
1690 struct common_datum *comdatum;
1692 if (!tclass || tclass > p->p_classes.nprim)
1695 cladatum = p->class_val_to_struct[tclass-1];
1696 comdatum = cladatum->comdatum;
1698 perdatum = hashtab_search(comdatum->permissions.table,
1701 perdatum = hashtab_search(cladatum->permissions.table,
1706 return 1U << (perdatum->value-1);
1709 static int range_read(struct policydb *p, void *fp)
1711 struct range_trans *rt = NULL;
1712 struct mls_range *r = NULL;
1717 if (p->policyvers < POLICYDB_VERSION_MLS)
1720 rc = next_entry(buf, fp, sizeof(u32));
1724 nel = le32_to_cpu(buf[0]);
1725 for (i = 0; i < nel; i++) {
1727 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1731 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1735 rt->source_type = le32_to_cpu(buf[0]);
1736 rt->target_type = le32_to_cpu(buf[1]);
1737 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1738 rc = next_entry(buf, fp, sizeof(u32));
1741 rt->target_class = le32_to_cpu(buf[0]);
1743 rt->target_class = p->process_class;
1746 if (!policydb_type_isvalid(p, rt->source_type) ||
1747 !policydb_type_isvalid(p, rt->target_type) ||
1748 !policydb_class_isvalid(p, rt->target_class))
1752 r = kzalloc(sizeof(*r), GFP_KERNEL);
1756 rc = mls_read_range_helper(r, fp);
1761 if (!mls_range_isvalid(p, r)) {
1762 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1766 rc = hashtab_insert(p->range_tr, rt, r);
1773 rangetr_hash_eval(p->range_tr);
1781 static int genfs_read(struct policydb *p, void *fp)
1784 u32 nel, nel2, len, len2;
1786 struct ocontext *l, *c;
1787 struct ocontext *newc = NULL;
1788 struct genfs *genfs_p, *genfs;
1789 struct genfs *newgenfs = NULL;
1791 rc = next_entry(buf, fp, sizeof(u32));
1794 nel = le32_to_cpu(buf[0]);
1796 for (i = 0; i < nel; i++) {
1797 rc = next_entry(buf, fp, sizeof(u32));
1800 len = le32_to_cpu(buf[0]);
1803 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1808 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1809 if (!newgenfs->fstype)
1812 rc = next_entry(newgenfs->fstype, fp, len);
1816 newgenfs->fstype[len] = 0;
1818 for (genfs_p = NULL, genfs = p->genfs; genfs;
1819 genfs_p = genfs, genfs = genfs->next) {
1821 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1822 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1826 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1829 newgenfs->next = genfs;
1831 genfs_p->next = newgenfs;
1833 p->genfs = newgenfs;
1837 rc = next_entry(buf, fp, sizeof(u32));
1841 nel2 = le32_to_cpu(buf[0]);
1842 for (j = 0; j < nel2; j++) {
1843 rc = next_entry(buf, fp, sizeof(u32));
1846 len = le32_to_cpu(buf[0]);
1849 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1854 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1858 rc = next_entry(newc->u.name, fp, len);
1861 newc->u.name[len] = 0;
1863 rc = next_entry(buf, fp, sizeof(u32));
1867 newc->v.sclass = le32_to_cpu(buf[0]);
1868 rc = context_read_and_validate(&newc->context[0], p, fp);
1872 for (l = NULL, c = genfs->head; c;
1873 l = c, c = c->next) {
1875 if (!strcmp(newc->u.name, c->u.name) &&
1876 (!c->v.sclass || !newc->v.sclass ||
1877 newc->v.sclass == c->v.sclass)) {
1878 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1879 genfs->fstype, c->u.name);
1882 len = strlen(newc->u.name);
1883 len2 = strlen(c->u.name);
1899 kfree(newgenfs->fstype);
1901 ocontext_destroy(newc, OCON_FSUSE);
1906 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1912 struct ocontext *l, *c;
1915 for (i = 0; i < info->ocon_num; i++) {
1916 rc = next_entry(buf, fp, sizeof(u32));
1919 nel = le32_to_cpu(buf[0]);
1922 for (j = 0; j < nel; j++) {
1924 c = kzalloc(sizeof(*c), GFP_KERNEL);
1930 p->ocontexts[i] = c;
1935 rc = next_entry(buf, fp, sizeof(u32));
1939 c->sid[0] = le32_to_cpu(buf[0]);
1940 rc = context_read_and_validate(&c->context[0], p, fp);
1946 rc = next_entry(buf, fp, sizeof(u32));
1949 len = le32_to_cpu(buf[0]);
1952 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1956 rc = next_entry(c->u.name, fp, len);
1961 rc = context_read_and_validate(&c->context[0], p, fp);
1964 rc = context_read_and_validate(&c->context[1], p, fp);
1969 rc = next_entry(buf, fp, sizeof(u32)*3);
1972 c->u.port.protocol = le32_to_cpu(buf[0]);
1973 c->u.port.low_port = le32_to_cpu(buf[1]);
1974 c->u.port.high_port = le32_to_cpu(buf[2]);
1975 rc = context_read_and_validate(&c->context[0], p, fp);
1980 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1983 c->u.node.addr = nodebuf[0]; /* network order */
1984 c->u.node.mask = nodebuf[1]; /* network order */
1985 rc = context_read_and_validate(&c->context[0], p, fp);
1990 rc = next_entry(buf, fp, sizeof(u32)*2);
1995 c->v.behavior = le32_to_cpu(buf[0]);
1996 if (c->v.behavior > SECURITY_FS_USE_NONE)
2000 len = le32_to_cpu(buf[1]);
2001 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2005 rc = next_entry(c->u.name, fp, len);
2009 rc = context_read_and_validate(&c->context[0], p, fp);
2016 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2019 for (k = 0; k < 4; k++)
2020 c->u.node6.addr[k] = nodebuf[k];
2021 for (k = 0; k < 4; k++)
2022 c->u.node6.mask[k] = nodebuf[k+4];
2023 rc = context_read_and_validate(&c->context[0], p, fp);
2037 * Read the configuration data from a policy database binary
2038 * representation file into a policy database structure.
2040 int policydb_read(struct policydb *p, void *fp)
2042 struct role_allow *ra, *lra;
2043 struct role_trans *tr, *ltr;
2046 u32 len, nprim, nel;
2049 struct policydb_compat_info *info;
2051 rc = policydb_init(p);
2055 /* Read the magic number and string length. */
2056 rc = next_entry(buf, fp, sizeof(u32) * 2);
2061 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
2062 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
2063 "not match expected magic number 0x%x\n",
2064 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
2069 len = le32_to_cpu(buf[1]);
2070 if (len != strlen(POLICYDB_STRING)) {
2071 printk(KERN_ERR "SELinux: policydb string length %d does not "
2072 "match expected length %Zu\n",
2073 len, strlen(POLICYDB_STRING));
2078 policydb_str = kmalloc(len + 1, GFP_KERNEL);
2079 if (!policydb_str) {
2080 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
2081 "string of length %d\n", len);
2085 rc = next_entry(policydb_str, fp, len);
2087 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
2088 kfree(policydb_str);
2093 policydb_str[len] = '\0';
2094 if (strcmp(policydb_str, POLICYDB_STRING)) {
2095 printk(KERN_ERR "SELinux: policydb string %s does not match "
2096 "my string %s\n", policydb_str, POLICYDB_STRING);
2097 kfree(policydb_str);
2100 /* Done with policydb_str. */
2101 kfree(policydb_str);
2102 policydb_str = NULL;
2104 /* Read the version and table sizes. */
2105 rc = next_entry(buf, fp, sizeof(u32)*4);
2110 p->policyvers = le32_to_cpu(buf[0]);
2111 if (p->policyvers < POLICYDB_VERSION_MIN ||
2112 p->policyvers > POLICYDB_VERSION_MAX) {
2113 printk(KERN_ERR "SELinux: policydb version %d does not match "
2114 "my version range %d-%d\n",
2115 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2119 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
2123 if (p->policyvers < POLICYDB_VERSION_MLS) {
2124 printk(KERN_ERR "SELinux: security policydb version %d "
2125 "(MLS) not backwards compatible\n",
2130 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2131 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
2133 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2134 rc = ebitmap_read(&p->policycaps, fp);
2139 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2140 rc = ebitmap_read(&p->permissive_map, fp);
2146 info = policydb_lookup_compat(p->policyvers);
2148 printk(KERN_ERR "SELinux: unable to find policy compat info "
2149 "for version %d\n", p->policyvers);
2154 if (le32_to_cpu(buf[2]) != info->sym_num ||
2155 le32_to_cpu(buf[3]) != info->ocon_num) {
2156 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
2157 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2158 le32_to_cpu(buf[3]),
2159 info->sym_num, info->ocon_num);
2163 for (i = 0; i < info->sym_num; i++) {
2164 rc = next_entry(buf, fp, sizeof(u32)*2);
2167 nprim = le32_to_cpu(buf[0]);
2168 nel = le32_to_cpu(buf[1]);
2169 for (j = 0; j < nel; j++) {
2170 rc = read_f[i](p, p->symtab[i].table, fp);
2175 p->symtab[i].nprim = nprim;
2178 rc = avtab_read(&p->te_avtab, fp, p);
2182 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2183 rc = cond_read_list(p, fp);
2188 rc = next_entry(buf, fp, sizeof(u32));
2191 nel = le32_to_cpu(buf[0]);
2193 for (i = 0; i < nel; i++) {
2195 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
2202 rc = next_entry(buf, fp, sizeof(u32)*3);
2207 tr->role = le32_to_cpu(buf[0]);
2208 tr->type = le32_to_cpu(buf[1]);
2209 tr->new_role = le32_to_cpu(buf[2]);
2210 if (!policydb_role_isvalid(p, tr->role) ||
2211 !policydb_type_isvalid(p, tr->type) ||
2212 !policydb_role_isvalid(p, tr->new_role))
2217 rc = next_entry(buf, fp, sizeof(u32));
2220 nel = le32_to_cpu(buf[0]);
2222 for (i = 0; i < nel; i++) {
2224 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
2231 rc = next_entry(buf, fp, sizeof(u32)*2);
2236 ra->role = le32_to_cpu(buf[0]);
2237 ra->new_role = le32_to_cpu(buf[1]);
2238 if (!policydb_role_isvalid(p, ra->role) ||
2239 !policydb_role_isvalid(p, ra->new_role))
2244 rc = policydb_index_classes(p);
2248 rc = policydb_index_others(p);
2253 p->process_class = string_to_security_class(p, "process");
2254 if (!p->process_class)
2258 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2259 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
2260 if (!p->process_trans_perms)
2263 rc = ocontext_read(p, info, fp);
2267 rc = genfs_read(p, fp);
2271 rc = range_read(p, fp);
2276 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2278 GFP_KERNEL | __GFP_ZERO);
2279 if (!p->type_attr_map_array)
2282 /* preallocate so we don't have to worry about the put ever failing */
2283 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2284 GFP_KERNEL | __GFP_ZERO);
2288 for (i = 0; i < p->p_types.nprim; i++) {
2289 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2293 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
2294 rc = ebitmap_read(e, fp);
2298 /* add the type itself as the degenerate case */
2299 rc = ebitmap_set_bit(e, i, 1);
2304 rc = policydb_bounds_sanity_check(p);
2312 policydb_destroy(p);
2317 * Write a MLS level structure to a policydb binary
2318 * representation file.
2320 static int mls_write_level(struct mls_level *l, void *fp)
2325 buf[0] = cpu_to_le32(l->sens);
2326 rc = put_entry(buf, sizeof(u32), 1, fp);
2330 rc = ebitmap_write(&l->cat, fp);
2338 * Write a MLS range structure to a policydb binary
2339 * representation file.
2341 static int mls_write_range_helper(struct mls_range *r, void *fp)
2347 eq = mls_level_eq(&r->level[1], &r->level[0]);
2353 buf[0] = cpu_to_le32(items-1);
2354 buf[1] = cpu_to_le32(r->level[0].sens);
2356 buf[2] = cpu_to_le32(r->level[1].sens);
2358 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2360 rc = put_entry(buf, sizeof(u32), items, fp);
2364 rc = ebitmap_write(&r->level[0].cat, fp);
2368 rc = ebitmap_write(&r->level[1].cat, fp);
2376 static int sens_write(void *vkey, void *datum, void *ptr)
2379 struct level_datum *levdatum = datum;
2380 struct policy_data *pd = ptr;
2387 buf[0] = cpu_to_le32(len);
2388 buf[1] = cpu_to_le32(levdatum->isalias);
2389 rc = put_entry(buf, sizeof(u32), 2, fp);
2393 rc = put_entry(key, 1, len, fp);
2397 rc = mls_write_level(levdatum->level, fp);
2404 static int cat_write(void *vkey, void *datum, void *ptr)
2407 struct cat_datum *catdatum = datum;
2408 struct policy_data *pd = ptr;
2415 buf[0] = cpu_to_le32(len);
2416 buf[1] = cpu_to_le32(catdatum->value);
2417 buf[2] = cpu_to_le32(catdatum->isalias);
2418 rc = put_entry(buf, sizeof(u32), 3, fp);
2422 rc = put_entry(key, 1, len, fp);
2429 static int role_trans_write(struct role_trans *r, void *fp)
2431 struct role_trans *tr;
2437 for (tr = r; tr; tr = tr->next)
2439 buf[0] = cpu_to_le32(nel);
2440 rc = put_entry(buf, sizeof(u32), 1, fp);
2443 for (tr = r; tr; tr = tr->next) {
2444 buf[0] = cpu_to_le32(tr->role);
2445 buf[1] = cpu_to_le32(tr->type);
2446 buf[2] = cpu_to_le32(tr->new_role);
2447 rc = put_entry(buf, sizeof(u32), 3, fp);
2455 static int role_allow_write(struct role_allow *r, void *fp)
2457 struct role_allow *ra;
2463 for (ra = r; ra; ra = ra->next)
2465 buf[0] = cpu_to_le32(nel);
2466 rc = put_entry(buf, sizeof(u32), 1, fp);
2469 for (ra = r; ra; ra = ra->next) {
2470 buf[0] = cpu_to_le32(ra->role);
2471 buf[1] = cpu_to_le32(ra->new_role);
2472 rc = put_entry(buf, sizeof(u32), 2, fp);
2480 * Write a security context structure
2481 * to a policydb binary representation file.
2483 static int context_write(struct policydb *p, struct context *c,
2489 buf[0] = cpu_to_le32(c->user);
2490 buf[1] = cpu_to_le32(c->role);
2491 buf[2] = cpu_to_le32(c->type);
2493 rc = put_entry(buf, sizeof(u32), 3, fp);
2497 rc = mls_write_range_helper(&c->range, fp);
2505 * The following *_write functions are used to
2506 * write the symbol data to a policy database
2507 * binary representation file.
2510 static int perm_write(void *vkey, void *datum, void *fp)
2513 struct perm_datum *perdatum = datum;
2519 buf[0] = cpu_to_le32(len);
2520 buf[1] = cpu_to_le32(perdatum->value);
2521 rc = put_entry(buf, sizeof(u32), 2, fp);
2525 rc = put_entry(key, 1, len, fp);
2532 static int common_write(void *vkey, void *datum, void *ptr)
2535 struct common_datum *comdatum = datum;
2536 struct policy_data *pd = ptr;
2543 buf[0] = cpu_to_le32(len);
2544 buf[1] = cpu_to_le32(comdatum->value);
2545 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2546 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2547 rc = put_entry(buf, sizeof(u32), 4, fp);
2551 rc = put_entry(key, 1, len, fp);
2555 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2562 static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2565 struct constraint_node *c;
2566 struct constraint_expr *e;
2571 for (c = node; c; c = c->next) {
2573 for (e = c->expr; e; e = e->next)
2575 buf[0] = cpu_to_le32(c->permissions);
2576 buf[1] = cpu_to_le32(nel);
2577 rc = put_entry(buf, sizeof(u32), 2, fp);
2580 for (e = c->expr; e; e = e->next) {
2581 buf[0] = cpu_to_le32(e->expr_type);
2582 buf[1] = cpu_to_le32(e->attr);
2583 buf[2] = cpu_to_le32(e->op);
2584 rc = put_entry(buf, sizeof(u32), 3, fp);
2588 switch (e->expr_type) {
2590 rc = ebitmap_write(&e->names, fp);
2603 static int class_write(void *vkey, void *datum, void *ptr)
2606 struct class_datum *cladatum = datum;
2607 struct policy_data *pd = ptr;
2609 struct policydb *p = pd->p;
2610 struct constraint_node *c;
2617 if (cladatum->comkey)
2618 len2 = strlen(cladatum->comkey);
2623 for (c = cladatum->constraints; c; c = c->next)
2626 buf[0] = cpu_to_le32(len);
2627 buf[1] = cpu_to_le32(len2);
2628 buf[2] = cpu_to_le32(cladatum->value);
2629 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2630 if (cladatum->permissions.table)
2631 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2634 buf[5] = cpu_to_le32(ncons);
2635 rc = put_entry(buf, sizeof(u32), 6, fp);
2639 rc = put_entry(key, 1, len, fp);
2643 if (cladatum->comkey) {
2644 rc = put_entry(cladatum->comkey, 1, len2, fp);
2649 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2653 rc = write_cons_helper(p, cladatum->constraints, fp);
2657 /* write out the validatetrans rule */
2659 for (c = cladatum->validatetrans; c; c = c->next)
2662 buf[0] = cpu_to_le32(ncons);
2663 rc = put_entry(buf, sizeof(u32), 1, fp);
2667 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2674 static int role_write(void *vkey, void *datum, void *ptr)
2677 struct role_datum *role = datum;
2678 struct policy_data *pd = ptr;
2680 struct policydb *p = pd->p;
2687 buf[items++] = cpu_to_le32(len);
2688 buf[items++] = cpu_to_le32(role->value);
2689 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2690 buf[items++] = cpu_to_le32(role->bounds);
2692 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2694 rc = put_entry(buf, sizeof(u32), items, fp);
2698 rc = put_entry(key, 1, len, fp);
2702 rc = ebitmap_write(&role->dominates, fp);
2706 rc = ebitmap_write(&role->types, fp);
2713 static int type_write(void *vkey, void *datum, void *ptr)
2716 struct type_datum *typdatum = datum;
2717 struct policy_data *pd = ptr;
2718 struct policydb *p = pd->p;
2726 buf[items++] = cpu_to_le32(len);
2727 buf[items++] = cpu_to_le32(typdatum->value);
2728 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2731 if (typdatum->primary)
2732 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2734 if (typdatum->attribute)
2735 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2737 buf[items++] = cpu_to_le32(properties);
2738 buf[items++] = cpu_to_le32(typdatum->bounds);
2740 buf[items++] = cpu_to_le32(typdatum->primary);
2742 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2743 rc = put_entry(buf, sizeof(u32), items, fp);
2747 rc = put_entry(key, 1, len, fp);
2754 static int user_write(void *vkey, void *datum, void *ptr)
2757 struct user_datum *usrdatum = datum;
2758 struct policy_data *pd = ptr;
2759 struct policydb *p = pd->p;
2767 buf[items++] = cpu_to_le32(len);
2768 buf[items++] = cpu_to_le32(usrdatum->value);
2769 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2770 buf[items++] = cpu_to_le32(usrdatum->bounds);
2771 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2772 rc = put_entry(buf, sizeof(u32), items, fp);
2776 rc = put_entry(key, 1, len, fp);
2780 rc = ebitmap_write(&usrdatum->roles, fp);
2784 rc = mls_write_range_helper(&usrdatum->range, fp);
2788 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2795 static int (*write_f[SYM_NUM]) (void *key, void *datum,
2808 static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2811 unsigned int i, j, rc;
2816 for (i = 0; i < info->ocon_num; i++) {
2818 for (c = p->ocontexts[i]; c; c = c->next)
2820 buf[0] = cpu_to_le32(nel);
2821 rc = put_entry(buf, sizeof(u32), 1, fp);
2824 for (c = p->ocontexts[i]; c; c = c->next) {
2827 buf[0] = cpu_to_le32(c->sid[0]);
2828 rc = put_entry(buf, sizeof(u32), 1, fp);
2831 rc = context_write(p, &c->context[0], fp);
2837 len = strlen(c->u.name);
2838 buf[0] = cpu_to_le32(len);
2839 rc = put_entry(buf, sizeof(u32), 1, fp);
2842 rc = put_entry(c->u.name, 1, len, fp);
2845 rc = context_write(p, &c->context[0], fp);
2848 rc = context_write(p, &c->context[1], fp);
2853 buf[0] = cpu_to_le32(c->u.port.protocol);
2854 buf[1] = cpu_to_le32(c->u.port.low_port);
2855 buf[2] = cpu_to_le32(c->u.port.high_port);
2856 rc = put_entry(buf, sizeof(u32), 3, fp);
2859 rc = context_write(p, &c->context[0], fp);
2864 nodebuf[0] = c->u.node.addr; /* network order */
2865 nodebuf[1] = c->u.node.mask; /* network order */
2866 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2869 rc = context_write(p, &c->context[0], fp);
2874 buf[0] = cpu_to_le32(c->v.behavior);
2875 len = strlen(c->u.name);
2876 buf[1] = cpu_to_le32(len);
2877 rc = put_entry(buf, sizeof(u32), 2, fp);
2880 rc = put_entry(c->u.name, 1, len, fp);
2883 rc = context_write(p, &c->context[0], fp);
2888 for (j = 0; j < 4; j++)
2889 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2890 for (j = 0; j < 4; j++)
2891 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2892 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2895 rc = context_write(p, &c->context[0], fp);
2905 static int genfs_write(struct policydb *p, void *fp)
2907 struct genfs *genfs;
2914 for (genfs = p->genfs; genfs; genfs = genfs->next)
2916 buf[0] = cpu_to_le32(len);
2917 rc = put_entry(buf, sizeof(u32), 1, fp);
2920 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2921 len = strlen(genfs->fstype);
2922 buf[0] = cpu_to_le32(len);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2926 rc = put_entry(genfs->fstype, 1, len, fp);
2930 for (c = genfs->head; c; c = c->next)
2932 buf[0] = cpu_to_le32(len);
2933 rc = put_entry(buf, sizeof(u32), 1, fp);
2936 for (c = genfs->head; c; c = c->next) {
2937 len = strlen(c->u.name);
2938 buf[0] = cpu_to_le32(len);
2939 rc = put_entry(buf, sizeof(u32), 1, fp);
2942 rc = put_entry(c->u.name, 1, len, fp);
2945 buf[0] = cpu_to_le32(c->v.sclass);
2946 rc = put_entry(buf, sizeof(u32), 1, fp);
2949 rc = context_write(p, &c->context[0], fp);
2957 static int range_count(void *key, void *data, void *ptr)
2965 static int range_write_helper(void *key, void *data, void *ptr)
2968 struct range_trans *rt = key;
2969 struct mls_range *r = data;
2970 struct policy_data *pd = ptr;
2972 struct policydb *p = pd->p;
2975 buf[0] = cpu_to_le32(rt->source_type);
2976 buf[1] = cpu_to_le32(rt->target_type);
2977 rc = put_entry(buf, sizeof(u32), 2, fp);
2980 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2981 buf[0] = cpu_to_le32(rt->target_class);
2982 rc = put_entry(buf, sizeof(u32), 1, fp);
2986 rc = mls_write_range_helper(r, fp);
2993 static int range_write(struct policydb *p, void *fp)
2998 struct policy_data pd;
3003 /* count the number of entries in the hashtab */
3005 rc = hashtab_map(p->range_tr, range_count, &nel);
3009 buf[0] = cpu_to_le32(nel);
3010 rc = put_entry(buf, sizeof(u32), 1, fp);
3014 /* actually write all of the entries */
3015 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3023 * Write the configuration data in a policy database
3024 * structure to a policy database binary representation
3027 int policydb_write(struct policydb *p, void *fp)
3029 unsigned int i, num_syms;
3034 struct policydb_compat_info *info;
3037 * refuse to write policy older than compressed avtab
3038 * to simplify the writer. There are other tests dropped
3039 * since we assume this throughout the writer code. Be
3040 * careful if you ever try to remove this restriction
3042 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3043 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3044 " Because it is less than version %d\n", p->policyvers,
3045 POLICYDB_VERSION_AVTAB);
3051 config |= POLICYDB_CONFIG_MLS;
3053 if (p->reject_unknown)
3054 config |= REJECT_UNKNOWN;
3055 if (p->allow_unknown)
3056 config |= ALLOW_UNKNOWN;
3058 /* Write the magic number and string identifiers. */
3059 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3060 len = strlen(POLICYDB_STRING);
3061 buf[1] = cpu_to_le32(len);
3062 rc = put_entry(buf, sizeof(u32), 2, fp);
3065 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3069 /* Write the version, config, and table sizes. */
3070 info = policydb_lookup_compat(p->policyvers);
3072 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3073 "version %d", p->policyvers);
3077 buf[0] = cpu_to_le32(p->policyvers);
3078 buf[1] = cpu_to_le32(config);
3079 buf[2] = cpu_to_le32(info->sym_num);
3080 buf[3] = cpu_to_le32(info->ocon_num);
3082 rc = put_entry(buf, sizeof(u32), 4, fp);
3086 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3087 rc = ebitmap_write(&p->policycaps, fp);
3092 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3093 rc = ebitmap_write(&p->permissive_map, fp);
3098 num_syms = info->sym_num;
3099 for (i = 0; i < num_syms; i++) {
3100 struct policy_data pd;
3105 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3106 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3108 rc = put_entry(buf, sizeof(u32), 2, fp);
3111 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3116 rc = avtab_write(p, &p->te_avtab, fp);
3120 rc = cond_write_list(p, p->cond_list, fp);
3124 rc = role_trans_write(p->role_tr, fp);
3128 rc = role_allow_write(p->role_allow, fp);
3132 rc = ocontext_write(p, info, fp);
3136 rc = genfs_write(p, fp);
3140 rc = range_write(p, fp);
3144 for (i = 0; i < p->p_types.nprim; i++) {
3145 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3148 rc = ebitmap_write(e, fp);