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@paul-moore.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
,
127 .version
= POLICYDB_VERSION_FILENAME_TRANS
,
129 .ocon_num
= OCON_NUM
,
132 .version
= POLICYDB_VERSION_ROLETRANS
,
134 .ocon_num
= OCON_NUM
,
137 .version
= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
,
139 .ocon_num
= OCON_NUM
,
142 .version
= POLICYDB_VERSION_DEFAULT_TYPE
,
144 .ocon_num
= OCON_NUM
,
147 .version
= POLICYDB_VERSION_CONSTRAINT_NAMES
,
149 .ocon_num
= OCON_NUM
,
152 .version
= POLICYDB_VERSION_XPERMS_IOCTL
,
154 .ocon_num
= OCON_NUM
,
158 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
161 struct policydb_compat_info
*info
= NULL
;
163 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
164 if (policydb_compat
[i
].version
== version
) {
165 info
= &policydb_compat
[i
];
173 * Initialize the role table.
175 static int roles_init(struct policydb
*p
)
179 struct role_datum
*role
;
182 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
187 role
->value
= ++p
->p_roles
.nprim
;
188 if (role
->value
!= OBJECT_R_VAL
)
192 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
196 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
207 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
209 const struct filename_trans
*ft
= k
;
211 unsigned int byte_num
;
214 hash
= ft
->stype
^ ft
->ttype
^ ft
->tclass
;
217 while ((focus
= ft
->name
[byte_num
++]))
218 hash
= partial_name_hash(focus
, hash
);
219 return hash
& (h
->size
- 1);
222 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
224 const struct filename_trans
*ft1
= k1
;
225 const struct filename_trans
*ft2
= k2
;
228 v
= ft1
->stype
- ft2
->stype
;
232 v
= ft1
->ttype
- ft2
->ttype
;
236 v
= ft1
->tclass
- ft2
->tclass
;
240 return strcmp(ft1
->name
, ft2
->name
);
244 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
246 const struct range_trans
*key
= k
;
247 return (key
->source_type
+ (key
->target_type
<< 3) +
248 (key
->target_class
<< 5)) & (h
->size
- 1);
251 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
253 const struct range_trans
*key1
= k1
, *key2
= k2
;
256 v
= key1
->source_type
- key2
->source_type
;
260 v
= key1
->target_type
- key2
->target_type
;
264 v
= key1
->target_class
- key2
->target_class
;
269 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
);
272 * Initialize a policy database structure.
274 static int policydb_init(struct policydb
*p
)
278 memset(p
, 0, sizeof(*p
));
280 for (i
= 0; i
< SYM_NUM
; i
++) {
281 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
286 rc
= avtab_init(&p
->te_avtab
);
294 rc
= cond_policydb_init(p
);
298 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
299 if (!p
->filename_trans
) {
304 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
310 ebitmap_init(&p
->filename_trans_ttypes
);
311 ebitmap_init(&p
->policycaps
);
312 ebitmap_init(&p
->permissive_map
);
316 hashtab_destroy(p
->filename_trans
);
317 hashtab_destroy(p
->range_tr
);
318 for (i
= 0; i
< SYM_NUM
; i
++) {
319 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
320 hashtab_destroy(p
->symtab
[i
].table
);
326 * The following *_index functions are used to
327 * define the val_to_name and val_to_struct arrays
328 * in a policy database structure. The val_to_name
329 * arrays are used when converting security context
330 * structures into string representations. The
331 * val_to_struct arrays are used when the attributes
332 * of a class, role, or user are needed.
335 static int common_index(void *key
, void *datum
, void *datap
)
338 struct common_datum
*comdatum
;
339 struct flex_array
*fa
;
343 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
346 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
347 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
348 GFP_KERNEL
| __GFP_ZERO
))
353 static int class_index(void *key
, void *datum
, void *datap
)
356 struct class_datum
*cladatum
;
357 struct flex_array
*fa
;
361 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
363 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
364 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
365 GFP_KERNEL
| __GFP_ZERO
))
367 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
371 static int role_index(void *key
, void *datum
, void *datap
)
374 struct role_datum
*role
;
375 struct flex_array
*fa
;
380 || role
->value
> p
->p_roles
.nprim
381 || role
->bounds
> p
->p_roles
.nprim
)
384 fa
= p
->sym_val_to_name
[SYM_ROLES
];
385 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
386 GFP_KERNEL
| __GFP_ZERO
))
388 p
->role_val_to_struct
[role
->value
- 1] = role
;
392 static int type_index(void *key
, void *datum
, void *datap
)
395 struct type_datum
*typdatum
;
396 struct flex_array
*fa
;
401 if (typdatum
->primary
) {
403 || typdatum
->value
> p
->p_types
.nprim
404 || typdatum
->bounds
> p
->p_types
.nprim
)
406 fa
= p
->sym_val_to_name
[SYM_TYPES
];
407 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
408 GFP_KERNEL
| __GFP_ZERO
))
411 fa
= p
->type_val_to_struct_array
;
412 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
413 GFP_KERNEL
| __GFP_ZERO
))
420 static int user_index(void *key
, void *datum
, void *datap
)
423 struct user_datum
*usrdatum
;
424 struct flex_array
*fa
;
429 || usrdatum
->value
> p
->p_users
.nprim
430 || usrdatum
->bounds
> p
->p_users
.nprim
)
433 fa
= p
->sym_val_to_name
[SYM_USERS
];
434 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
435 GFP_KERNEL
| __GFP_ZERO
))
437 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
441 static int sens_index(void *key
, void *datum
, void *datap
)
444 struct level_datum
*levdatum
;
445 struct flex_array
*fa
;
450 if (!levdatum
->isalias
) {
451 if (!levdatum
->level
->sens
||
452 levdatum
->level
->sens
> p
->p_levels
.nprim
)
454 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
455 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
456 GFP_KERNEL
| __GFP_ZERO
))
463 static int cat_index(void *key
, void *datum
, void *datap
)
466 struct cat_datum
*catdatum
;
467 struct flex_array
*fa
;
472 if (!catdatum
->isalias
) {
473 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
475 fa
= p
->sym_val_to_name
[SYM_CATS
];
476 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
477 GFP_KERNEL
| __GFP_ZERO
))
484 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
497 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
499 struct hashtab_info info
;
501 hashtab_stat(h
, &info
);
502 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
503 "longest chain length %d\n", hash_name
, h
->nel
,
504 info
.slots_used
, h
->size
, info
.max_chain_len
);
507 static void symtab_hash_eval(struct symtab
*s
)
511 for (i
= 0; i
< SYM_NUM
; i
++)
512 hash_eval(s
[i
].table
, symtab_name
[i
]);
516 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
522 * Define the other val_to_name and val_to_struct arrays
523 * in a policy database structure.
525 * Caller must clean up on failure.
527 static int policydb_index(struct policydb
*p
)
531 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
532 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
534 printk(KERN_CONT
", %d sens, %d cats", p
->p_levels
.nprim
,
536 printk(KERN_CONT
"\n");
538 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
539 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
542 avtab_hash_eval(&p
->te_avtab
, "rules");
543 symtab_hash_eval(p
->symtab
);
547 p
->class_val_to_struct
=
548 kzalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
550 if (!p
->class_val_to_struct
)
554 p
->role_val_to_struct
=
555 kzalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
557 if (!p
->role_val_to_struct
)
561 p
->user_val_to_struct
=
562 kzalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
564 if (!p
->user_val_to_struct
)
567 /* Yes, I want the sizeof the pointer, not the structure */
569 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
571 GFP_KERNEL
| __GFP_ZERO
);
572 if (!p
->type_val_to_struct_array
)
575 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
576 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
580 rc
= cond_init_bool_indexes(p
);
584 for (i
= 0; i
< SYM_NUM
; i
++) {
586 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
588 GFP_KERNEL
| __GFP_ZERO
);
589 if (!p
->sym_val_to_name
[i
])
592 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
593 0, p
->symtab
[i
].nprim
,
594 GFP_KERNEL
| __GFP_ZERO
);
598 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
608 * The following *_destroy functions are used to
609 * free any memory allocated for each kind of
610 * symbol data in the policy database.
613 static int perm_destroy(void *key
, void *datum
, void *p
)
620 static int common_destroy(void *key
, void *datum
, void *p
)
622 struct common_datum
*comdatum
;
627 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
628 hashtab_destroy(comdatum
->permissions
.table
);
634 static void constraint_expr_destroy(struct constraint_expr
*expr
)
637 ebitmap_destroy(&expr
->names
);
638 if (expr
->type_names
) {
639 ebitmap_destroy(&expr
->type_names
->types
);
640 ebitmap_destroy(&expr
->type_names
->negset
);
641 kfree(expr
->type_names
);
647 static int cls_destroy(void *key
, void *datum
, void *p
)
649 struct class_datum
*cladatum
;
650 struct constraint_node
*constraint
, *ctemp
;
651 struct constraint_expr
*e
, *etmp
;
656 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
657 hashtab_destroy(cladatum
->permissions
.table
);
658 constraint
= cladatum
->constraints
;
660 e
= constraint
->expr
;
664 constraint_expr_destroy(etmp
);
667 constraint
= constraint
->next
;
671 constraint
= cladatum
->validatetrans
;
673 e
= constraint
->expr
;
677 constraint_expr_destroy(etmp
);
680 constraint
= constraint
->next
;
683 kfree(cladatum
->comkey
);
689 static int role_destroy(void *key
, void *datum
, void *p
)
691 struct role_datum
*role
;
696 ebitmap_destroy(&role
->dominates
);
697 ebitmap_destroy(&role
->types
);
703 static int type_destroy(void *key
, void *datum
, void *p
)
710 static int user_destroy(void *key
, void *datum
, void *p
)
712 struct user_datum
*usrdatum
;
717 ebitmap_destroy(&usrdatum
->roles
);
718 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
719 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
720 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
726 static int sens_destroy(void *key
, void *datum
, void *p
)
728 struct level_datum
*levdatum
;
734 ebitmap_destroy(&levdatum
->level
->cat
);
735 kfree(levdatum
->level
);
741 static int cat_destroy(void *key
, void *datum
, void *p
)
748 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
760 static int filenametr_destroy(void *key
, void *datum
, void *p
)
762 struct filename_trans
*ft
= key
;
770 static int range_tr_destroy(void *key
, void *datum
, void *p
)
772 struct mls_range
*rt
= datum
;
774 ebitmap_destroy(&rt
->level
[0].cat
);
775 ebitmap_destroy(&rt
->level
[1].cat
);
781 static void ocontext_destroy(struct ocontext
*c
, int i
)
786 context_destroy(&c
->context
[0]);
787 context_destroy(&c
->context
[1]);
788 if (i
== OCON_ISID
|| i
== OCON_FS
||
789 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
795 * Free any memory allocated by a policy database structure.
797 void policydb_destroy(struct policydb
*p
)
799 struct ocontext
*c
, *ctmp
;
800 struct genfs
*g
, *gtmp
;
802 struct role_allow
*ra
, *lra
= NULL
;
803 struct role_trans
*tr
, *ltr
= NULL
;
805 for (i
= 0; i
< SYM_NUM
; i
++) {
807 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
808 hashtab_destroy(p
->symtab
[i
].table
);
811 for (i
= 0; i
< SYM_NUM
; i
++) {
812 if (p
->sym_val_to_name
[i
])
813 flex_array_free(p
->sym_val_to_name
[i
]);
816 kfree(p
->class_val_to_struct
);
817 kfree(p
->role_val_to_struct
);
818 kfree(p
->user_val_to_struct
);
819 if (p
->type_val_to_struct_array
)
820 flex_array_free(p
->type_val_to_struct_array
);
822 avtab_destroy(&p
->te_avtab
);
824 for (i
= 0; i
< OCON_NUM
; i
++) {
830 ocontext_destroy(ctmp
, i
);
832 p
->ocontexts
[i
] = NULL
;
843 ocontext_destroy(ctmp
, OCON_FSUSE
);
851 cond_policydb_destroy(p
);
853 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
860 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
867 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
868 hashtab_destroy(p
->filename_trans
);
870 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
871 hashtab_destroy(p
->range_tr
);
873 if (p
->type_attr_map_array
) {
874 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
877 e
= flex_array_get(p
->type_attr_map_array
, i
);
882 flex_array_free(p
->type_attr_map_array
);
885 ebitmap_destroy(&p
->filename_trans_ttypes
);
886 ebitmap_destroy(&p
->policycaps
);
887 ebitmap_destroy(&p
->permissive_map
);
893 * Load the initial SIDs specified in a policy database
894 * structure into a SID table.
896 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
898 struct ocontext
*head
, *c
;
903 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
907 head
= p
->ocontexts
[OCON_ISID
];
908 for (c
= head
; c
; c
= c
->next
) {
910 if (!c
->context
[0].user
) {
911 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
916 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
918 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
928 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
930 if (!class || class > p
->p_classes
.nprim
)
935 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
937 if (!role
|| role
> p
->p_roles
.nprim
)
942 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
944 if (!type
|| type
> p
->p_types
.nprim
)
950 * Return 1 if the fields in the security context
951 * structure `c' are valid. Return 0 otherwise.
953 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
955 struct role_datum
*role
;
956 struct user_datum
*usrdatum
;
958 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
961 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
964 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
967 if (c
->role
!= OBJECT_R_VAL
) {
969 * Role must be authorized for the type.
971 role
= p
->role_val_to_struct
[c
->role
- 1];
972 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
973 /* role may not be associated with type */
977 * User must be authorized for the role.
979 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
983 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
984 /* user may not be associated with role */
988 if (!mls_context_isvalid(p
, c
))
995 * Read a MLS range structure from a policydb binary
996 * representation file.
998 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
1004 rc
= next_entry(buf
, fp
, sizeof(u32
));
1009 items
= le32_to_cpu(buf
[0]);
1010 if (items
> ARRAY_SIZE(buf
)) {
1011 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1015 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1017 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1021 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1023 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1025 r
->level
[1].sens
= r
->level
[0].sens
;
1027 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1029 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1033 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1035 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1039 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1041 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1048 ebitmap_destroy(&r
->level
[0].cat
);
1054 * Read and validate a security context structure
1055 * from a policydb binary representation file.
1057 static int context_read_and_validate(struct context
*c
,
1064 rc
= next_entry(buf
, fp
, sizeof buf
);
1066 printk(KERN_ERR
"SELinux: context truncated\n");
1069 c
->user
= le32_to_cpu(buf
[0]);
1070 c
->role
= le32_to_cpu(buf
[1]);
1071 c
->type
= le32_to_cpu(buf
[2]);
1072 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1073 rc
= mls_read_range_helper(&c
->range
, fp
);
1075 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1081 if (!policydb_context_isvalid(p
, c
)) {
1082 printk(KERN_ERR
"SELinux: invalid security context\n");
1092 * The following *_read functions are used to
1093 * read the symbol data from a policy database
1094 * binary representation file.
1097 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1102 if ((len
== 0) || (len
== (u32
)-1))
1105 str
= kmalloc(len
+ 1, flags
| __GFP_NOWARN
);
1109 /* it's expected the caller should free the str */
1112 rc
= next_entry(str
, fp
, len
);
1120 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1123 struct perm_datum
*perdatum
;
1129 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1133 rc
= next_entry(buf
, fp
, sizeof buf
);
1137 len
= le32_to_cpu(buf
[0]);
1138 perdatum
->value
= le32_to_cpu(buf
[1]);
1140 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1144 rc
= hashtab_insert(h
, key
, perdatum
);
1150 perm_destroy(key
, perdatum
, NULL
);
1154 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1157 struct common_datum
*comdatum
;
1163 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1167 rc
= next_entry(buf
, fp
, sizeof buf
);
1171 len
= le32_to_cpu(buf
[0]);
1172 comdatum
->value
= le32_to_cpu(buf
[1]);
1174 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1177 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1178 nel
= le32_to_cpu(buf
[3]);
1180 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1184 for (i
= 0; i
< nel
; i
++) {
1185 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1190 rc
= hashtab_insert(h
, key
, comdatum
);
1195 common_destroy(key
, comdatum
, NULL
);
1199 static void type_set_init(struct type_set
*t
)
1201 ebitmap_init(&t
->types
);
1202 ebitmap_init(&t
->negset
);
1205 static int type_set_read(struct type_set
*t
, void *fp
)
1210 if (ebitmap_read(&t
->types
, fp
))
1212 if (ebitmap_read(&t
->negset
, fp
))
1215 rc
= next_entry(buf
, fp
, sizeof(u32
));
1218 t
->flags
= le32_to_cpu(buf
[0]);
1224 static int read_cons_helper(struct policydb
*p
,
1225 struct constraint_node
**nodep
,
1226 int ncons
, int allowxtarget
, void *fp
)
1228 struct constraint_node
*c
, *lc
;
1229 struct constraint_expr
*e
, *le
;
1232 int rc
, i
, j
, depth
;
1235 for (i
= 0; i
< ncons
; i
++) {
1236 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1245 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1248 c
->permissions
= le32_to_cpu(buf
[0]);
1249 nexpr
= le32_to_cpu(buf
[1]);
1252 for (j
= 0; j
< nexpr
; j
++) {
1253 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1262 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1265 e
->expr_type
= le32_to_cpu(buf
[0]);
1266 e
->attr
= le32_to_cpu(buf
[1]);
1267 e
->op
= le32_to_cpu(buf
[2]);
1269 switch (e
->expr_type
) {
1281 if (depth
== (CEXPR_MAXDEPTH
- 1))
1286 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1288 if (depth
== (CEXPR_MAXDEPTH
- 1))
1291 rc
= ebitmap_read(&e
->names
, fp
);
1294 if (p
->policyvers
>=
1295 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1296 e
->type_names
= kzalloc(sizeof
1301 type_set_init(e
->type_names
);
1302 rc
= type_set_read(e
->type_names
, fp
);
1320 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1323 struct class_datum
*cladatum
;
1325 u32 len
, len2
, ncons
, nel
;
1329 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1333 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1337 len
= le32_to_cpu(buf
[0]);
1338 len2
= le32_to_cpu(buf
[1]);
1339 cladatum
->value
= le32_to_cpu(buf
[2]);
1341 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1344 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1345 nel
= le32_to_cpu(buf
[4]);
1347 ncons
= le32_to_cpu(buf
[5]);
1349 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1354 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1359 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1360 if (!cladatum
->comdatum
) {
1361 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1365 for (i
= 0; i
< nel
; i
++) {
1366 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1371 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1375 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1376 /* grab the validatetrans rules */
1377 rc
= next_entry(buf
, fp
, sizeof(u32
));
1380 ncons
= le32_to_cpu(buf
[0]);
1381 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1387 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1388 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1392 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1393 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1394 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1397 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1398 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1401 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1404 rc
= hashtab_insert(h
, key
, cladatum
);
1410 cls_destroy(key
, cladatum
, NULL
);
1414 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1417 struct role_datum
*role
;
1418 int rc
, to_read
= 2;
1423 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1427 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1430 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1434 len
= le32_to_cpu(buf
[0]);
1435 role
->value
= le32_to_cpu(buf
[1]);
1436 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1437 role
->bounds
= le32_to_cpu(buf
[2]);
1439 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1443 rc
= ebitmap_read(&role
->dominates
, fp
);
1447 rc
= ebitmap_read(&role
->types
, fp
);
1451 if (strcmp(key
, OBJECT_R
) == 0) {
1453 if (role
->value
!= OBJECT_R_VAL
) {
1454 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1455 OBJECT_R
, role
->value
);
1462 rc
= hashtab_insert(h
, key
, role
);
1467 role_destroy(key
, role
, NULL
);
1471 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1474 struct type_datum
*typdatum
;
1475 int rc
, to_read
= 3;
1480 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1484 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1487 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1491 len
= le32_to_cpu(buf
[0]);
1492 typdatum
->value
= le32_to_cpu(buf
[1]);
1493 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1494 u32 prop
= le32_to_cpu(buf
[2]);
1496 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1497 typdatum
->primary
= 1;
1498 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1499 typdatum
->attribute
= 1;
1501 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1503 typdatum
->primary
= le32_to_cpu(buf
[2]);
1506 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1510 rc
= hashtab_insert(h
, key
, typdatum
);
1515 type_destroy(key
, typdatum
, NULL
);
1521 * Read a MLS level structure from a policydb binary
1522 * representation file.
1524 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1529 memset(lp
, 0, sizeof(*lp
));
1531 rc
= next_entry(buf
, fp
, sizeof buf
);
1533 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1536 lp
->sens
= le32_to_cpu(buf
[0]);
1538 rc
= ebitmap_read(&lp
->cat
, fp
);
1540 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1546 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1549 struct user_datum
*usrdatum
;
1550 int rc
, to_read
= 2;
1555 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1559 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1562 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1566 len
= le32_to_cpu(buf
[0]);
1567 usrdatum
->value
= le32_to_cpu(buf
[1]);
1568 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1569 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1571 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1575 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1579 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1580 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1583 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1588 rc
= hashtab_insert(h
, key
, usrdatum
);
1593 user_destroy(key
, usrdatum
, NULL
);
1597 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1600 struct level_datum
*levdatum
;
1606 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1610 rc
= next_entry(buf
, fp
, sizeof buf
);
1614 len
= le32_to_cpu(buf
[0]);
1615 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1617 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1622 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1623 if (!levdatum
->level
)
1626 rc
= mls_read_level(levdatum
->level
, fp
);
1630 rc
= hashtab_insert(h
, key
, levdatum
);
1635 sens_destroy(key
, levdatum
, NULL
);
1639 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1642 struct cat_datum
*catdatum
;
1648 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1652 rc
= next_entry(buf
, fp
, sizeof buf
);
1656 len
= le32_to_cpu(buf
[0]);
1657 catdatum
->value
= le32_to_cpu(buf
[1]);
1658 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1660 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1664 rc
= hashtab_insert(h
, key
, catdatum
);
1669 cat_destroy(key
, catdatum
, NULL
);
1673 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1685 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1687 struct user_datum
*upper
, *user
;
1688 struct policydb
*p
= datap
;
1691 upper
= user
= datum
;
1692 while (upper
->bounds
) {
1693 struct ebitmap_node
*node
;
1696 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1697 printk(KERN_ERR
"SELinux: user %s: "
1698 "too deep or looped boundary",
1703 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1704 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1705 if (ebitmap_get_bit(&upper
->roles
, bit
))
1709 "SELinux: boundary violated policy: "
1710 "user=%s role=%s bounds=%s\n",
1711 sym_name(p
, SYM_USERS
, user
->value
- 1),
1712 sym_name(p
, SYM_ROLES
, bit
),
1713 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1722 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1724 struct role_datum
*upper
, *role
;
1725 struct policydb
*p
= datap
;
1728 upper
= role
= datum
;
1729 while (upper
->bounds
) {
1730 struct ebitmap_node
*node
;
1733 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1734 printk(KERN_ERR
"SELinux: role %s: "
1735 "too deep or looped bounds\n",
1740 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1741 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1742 if (ebitmap_get_bit(&upper
->types
, bit
))
1746 "SELinux: boundary violated policy: "
1747 "role=%s type=%s bounds=%s\n",
1748 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1749 sym_name(p
, SYM_TYPES
, bit
),
1750 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1759 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1761 struct type_datum
*upper
;
1762 struct policydb
*p
= datap
;
1766 while (upper
->bounds
) {
1767 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1768 printk(KERN_ERR
"SELinux: type %s: "
1769 "too deep or looped boundary\n",
1774 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1778 if (upper
->attribute
) {
1779 printk(KERN_ERR
"SELinux: type %s: "
1780 "bounded by attribute %s",
1782 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1790 static int policydb_bounds_sanity_check(struct policydb
*p
)
1794 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1797 rc
= hashtab_map(p
->p_users
.table
,
1798 user_bounds_sanity_check
, p
);
1802 rc
= hashtab_map(p
->p_roles
.table
,
1803 role_bounds_sanity_check
, p
);
1807 rc
= hashtab_map(p
->p_types
.table
,
1808 type_bounds_sanity_check
, p
);
1815 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1817 struct class_datum
*cladatum
;
1819 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1823 return cladatum
->value
;
1826 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1828 struct class_datum
*cladatum
;
1829 struct perm_datum
*perdatum
= NULL
;
1830 struct common_datum
*comdatum
;
1832 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1835 cladatum
= p
->class_val_to_struct
[tclass
-1];
1836 comdatum
= cladatum
->comdatum
;
1838 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1841 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1846 return 1U << (perdatum
->value
-1);
1849 static int range_read(struct policydb
*p
, void *fp
)
1851 struct range_trans
*rt
= NULL
;
1852 struct mls_range
*r
= NULL
;
1857 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1860 rc
= next_entry(buf
, fp
, sizeof(u32
));
1864 nel
= le32_to_cpu(buf
[0]);
1865 for (i
= 0; i
< nel
; i
++) {
1867 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1871 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1875 rt
->source_type
= le32_to_cpu(buf
[0]);
1876 rt
->target_type
= le32_to_cpu(buf
[1]);
1877 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1878 rc
= next_entry(buf
, fp
, sizeof(u32
));
1881 rt
->target_class
= le32_to_cpu(buf
[0]);
1883 rt
->target_class
= p
->process_class
;
1886 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1887 !policydb_type_isvalid(p
, rt
->target_type
) ||
1888 !policydb_class_isvalid(p
, rt
->target_class
))
1892 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1896 rc
= mls_read_range_helper(r
, fp
);
1901 if (!mls_range_isvalid(p
, r
)) {
1902 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1906 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1913 hash_eval(p
->range_tr
, "rangetr");
1921 static int filename_trans_read(struct policydb
*p
, void *fp
)
1923 struct filename_trans
*ft
;
1924 struct filename_trans_datum
*otype
;
1930 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1933 rc
= next_entry(buf
, fp
, sizeof(u32
));
1936 nel
= le32_to_cpu(buf
[0]);
1938 for (i
= 0; i
< nel
; i
++) {
1944 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1949 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1953 /* length of the path component string */
1954 rc
= next_entry(buf
, fp
, sizeof(u32
));
1957 len
= le32_to_cpu(buf
[0]);
1959 /* path component string */
1960 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1966 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1970 ft
->stype
= le32_to_cpu(buf
[0]);
1971 ft
->ttype
= le32_to_cpu(buf
[1]);
1972 ft
->tclass
= le32_to_cpu(buf
[2]);
1974 otype
->otype
= le32_to_cpu(buf
[3]);
1976 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1980 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1983 * Do not return -EEXIST to the caller, or the system
1988 /* But free memory to avoid memory leak. */
1994 hash_eval(p
->filename_trans
, "filenametr");
2004 static int genfs_read(struct policydb
*p
, void *fp
)
2007 u32 nel
, nel2
, len
, len2
;
2009 struct ocontext
*l
, *c
;
2010 struct ocontext
*newc
= NULL
;
2011 struct genfs
*genfs_p
, *genfs
;
2012 struct genfs
*newgenfs
= NULL
;
2014 rc
= next_entry(buf
, fp
, sizeof(u32
));
2017 nel
= le32_to_cpu(buf
[0]);
2019 for (i
= 0; i
< nel
; i
++) {
2020 rc
= next_entry(buf
, fp
, sizeof(u32
));
2023 len
= le32_to_cpu(buf
[0]);
2026 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2030 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2034 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2035 genfs_p
= genfs
, genfs
= genfs
->next
) {
2037 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2038 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2042 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2045 newgenfs
->next
= genfs
;
2047 genfs_p
->next
= newgenfs
;
2049 p
->genfs
= newgenfs
;
2053 rc
= next_entry(buf
, fp
, sizeof(u32
));
2057 nel2
= le32_to_cpu(buf
[0]);
2058 for (j
= 0; j
< nel2
; j
++) {
2059 rc
= next_entry(buf
, fp
, sizeof(u32
));
2062 len
= le32_to_cpu(buf
[0]);
2065 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2069 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2073 rc
= next_entry(buf
, fp
, sizeof(u32
));
2077 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2078 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2082 for (l
= NULL
, c
= genfs
->head
; c
;
2083 l
= c
, c
= c
->next
) {
2085 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2086 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2087 newc
->v
.sclass
== c
->v
.sclass
)) {
2088 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2089 genfs
->fstype
, c
->u
.name
);
2092 len
= strlen(newc
->u
.name
);
2093 len2
= strlen(c
->u
.name
);
2109 kfree(newgenfs
->fstype
);
2111 ocontext_destroy(newc
, OCON_FSUSE
);
2116 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2122 struct ocontext
*l
, *c
;
2125 for (i
= 0; i
< info
->ocon_num
; i
++) {
2126 rc
= next_entry(buf
, fp
, sizeof(u32
));
2129 nel
= le32_to_cpu(buf
[0]);
2132 for (j
= 0; j
< nel
; j
++) {
2134 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2140 p
->ocontexts
[i
] = c
;
2145 rc
= next_entry(buf
, fp
, sizeof(u32
));
2149 c
->sid
[0] = le32_to_cpu(buf
[0]);
2150 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2156 rc
= next_entry(buf
, fp
, sizeof(u32
));
2159 len
= le32_to_cpu(buf
[0]);
2161 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2165 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2168 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2173 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2176 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2177 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2178 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2179 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2184 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2187 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2188 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2189 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2194 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2199 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2200 /* Determined at runtime, not in policy DB. */
2201 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2203 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2206 len
= le32_to_cpu(buf
[1]);
2207 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2211 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2218 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2221 for (k
= 0; k
< 4; k
++)
2222 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2223 for (k
= 0; k
< 4; k
++)
2224 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2225 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2239 * Read the configuration data from a policy database binary
2240 * representation file into a policy database structure.
2242 int policydb_read(struct policydb
*p
, void *fp
)
2244 struct role_allow
*ra
, *lra
;
2245 struct role_trans
*tr
, *ltr
;
2248 u32 len
, nprim
, nel
;
2251 struct policydb_compat_info
*info
;
2253 rc
= policydb_init(p
);
2257 /* Read the magic number and string length. */
2258 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2263 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2264 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2265 "not match expected magic number 0x%x\n",
2266 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2271 len
= le32_to_cpu(buf
[1]);
2272 if (len
!= strlen(POLICYDB_STRING
)) {
2273 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2274 "match expected length %Zu\n",
2275 len
, strlen(POLICYDB_STRING
));
2280 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2281 if (!policydb_str
) {
2282 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2283 "string of length %d\n", len
);
2287 rc
= next_entry(policydb_str
, fp
, len
);
2289 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2290 kfree(policydb_str
);
2295 policydb_str
[len
] = '\0';
2296 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2297 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2298 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2299 kfree(policydb_str
);
2302 /* Done with policydb_str. */
2303 kfree(policydb_str
);
2304 policydb_str
= NULL
;
2306 /* Read the version and table sizes. */
2307 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2312 p
->policyvers
= le32_to_cpu(buf
[0]);
2313 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2314 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2315 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2316 "my version range %d-%d\n",
2317 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2321 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2325 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2326 printk(KERN_ERR
"SELinux: security policydb version %d "
2327 "(MLS) not backwards compatible\n",
2332 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2333 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2335 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2336 rc
= ebitmap_read(&p
->policycaps
, fp
);
2341 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2342 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2348 info
= policydb_lookup_compat(p
->policyvers
);
2350 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2351 "for version %d\n", p
->policyvers
);
2356 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2357 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2358 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2359 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2360 le32_to_cpu(buf
[3]),
2361 info
->sym_num
, info
->ocon_num
);
2365 for (i
= 0; i
< info
->sym_num
; i
++) {
2366 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2369 nprim
= le32_to_cpu(buf
[0]);
2370 nel
= le32_to_cpu(buf
[1]);
2371 for (j
= 0; j
< nel
; j
++) {
2372 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2377 p
->symtab
[i
].nprim
= nprim
;
2381 p
->process_class
= string_to_security_class(p
, "process");
2382 if (!p
->process_class
)
2385 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2389 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2390 rc
= cond_read_list(p
, fp
);
2395 rc
= next_entry(buf
, fp
, sizeof(u32
));
2398 nel
= le32_to_cpu(buf
[0]);
2400 for (i
= 0; i
< nel
; i
++) {
2402 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2409 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2414 tr
->role
= le32_to_cpu(buf
[0]);
2415 tr
->type
= le32_to_cpu(buf
[1]);
2416 tr
->new_role
= le32_to_cpu(buf
[2]);
2417 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2418 rc
= next_entry(buf
, fp
, sizeof(u32
));
2421 tr
->tclass
= le32_to_cpu(buf
[0]);
2423 tr
->tclass
= p
->process_class
;
2426 if (!policydb_role_isvalid(p
, tr
->role
) ||
2427 !policydb_type_isvalid(p
, tr
->type
) ||
2428 !policydb_class_isvalid(p
, tr
->tclass
) ||
2429 !policydb_role_isvalid(p
, tr
->new_role
))
2434 rc
= next_entry(buf
, fp
, sizeof(u32
));
2437 nel
= le32_to_cpu(buf
[0]);
2439 for (i
= 0; i
< nel
; i
++) {
2441 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2448 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2453 ra
->role
= le32_to_cpu(buf
[0]);
2454 ra
->new_role
= le32_to_cpu(buf
[1]);
2455 if (!policydb_role_isvalid(p
, ra
->role
) ||
2456 !policydb_role_isvalid(p
, ra
->new_role
))
2461 rc
= filename_trans_read(p
, fp
);
2465 rc
= policydb_index(p
);
2470 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2471 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2472 if (!p
->process_trans_perms
)
2475 rc
= ocontext_read(p
, info
, fp
);
2479 rc
= genfs_read(p
, fp
);
2483 rc
= range_read(p
, fp
);
2488 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2490 GFP_KERNEL
| __GFP_ZERO
);
2491 if (!p
->type_attr_map_array
)
2494 /* preallocate so we don't have to worry about the put ever failing */
2495 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2496 GFP_KERNEL
| __GFP_ZERO
);
2500 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2501 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2505 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2506 rc
= ebitmap_read(e
, fp
);
2510 /* add the type itself as the degenerate case */
2511 rc
= ebitmap_set_bit(e
, i
, 1);
2516 rc
= policydb_bounds_sanity_check(p
);
2524 policydb_destroy(p
);
2529 * Write a MLS level structure to a policydb binary
2530 * representation file.
2532 static int mls_write_level(struct mls_level
*l
, void *fp
)
2537 buf
[0] = cpu_to_le32(l
->sens
);
2538 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2542 rc
= ebitmap_write(&l
->cat
, fp
);
2550 * Write a MLS range structure to a policydb binary
2551 * representation file.
2553 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2559 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2565 buf
[0] = cpu_to_le32(items
-1);
2566 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2568 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2570 BUG_ON(items
> ARRAY_SIZE(buf
));
2572 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2576 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2580 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2588 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2591 struct level_datum
*levdatum
= datum
;
2592 struct policy_data
*pd
= ptr
;
2599 buf
[0] = cpu_to_le32(len
);
2600 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2601 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2605 rc
= put_entry(key
, 1, len
, fp
);
2609 rc
= mls_write_level(levdatum
->level
, fp
);
2616 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2619 struct cat_datum
*catdatum
= datum
;
2620 struct policy_data
*pd
= ptr
;
2627 buf
[0] = cpu_to_le32(len
);
2628 buf
[1] = cpu_to_le32(catdatum
->value
);
2629 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2630 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2634 rc
= put_entry(key
, 1, len
, fp
);
2641 static int role_trans_write(struct policydb
*p
, void *fp
)
2643 struct role_trans
*r
= p
->role_tr
;
2644 struct role_trans
*tr
;
2650 for (tr
= r
; tr
; tr
= tr
->next
)
2652 buf
[0] = cpu_to_le32(nel
);
2653 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2656 for (tr
= r
; tr
; tr
= tr
->next
) {
2657 buf
[0] = cpu_to_le32(tr
->role
);
2658 buf
[1] = cpu_to_le32(tr
->type
);
2659 buf
[2] = cpu_to_le32(tr
->new_role
);
2660 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2663 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2664 buf
[0] = cpu_to_le32(tr
->tclass
);
2665 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2674 static int role_allow_write(struct role_allow
*r
, void *fp
)
2676 struct role_allow
*ra
;
2682 for (ra
= r
; ra
; ra
= ra
->next
)
2684 buf
[0] = cpu_to_le32(nel
);
2685 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2688 for (ra
= r
; ra
; ra
= ra
->next
) {
2689 buf
[0] = cpu_to_le32(ra
->role
);
2690 buf
[1] = cpu_to_le32(ra
->new_role
);
2691 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2699 * Write a security context structure
2700 * to a policydb binary representation file.
2702 static int context_write(struct policydb
*p
, struct context
*c
,
2708 buf
[0] = cpu_to_le32(c
->user
);
2709 buf
[1] = cpu_to_le32(c
->role
);
2710 buf
[2] = cpu_to_le32(c
->type
);
2712 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2716 rc
= mls_write_range_helper(&c
->range
, fp
);
2724 * The following *_write functions are used to
2725 * write the symbol data to a policy database
2726 * binary representation file.
2729 static int perm_write(void *vkey
, void *datum
, void *fp
)
2732 struct perm_datum
*perdatum
= datum
;
2738 buf
[0] = cpu_to_le32(len
);
2739 buf
[1] = cpu_to_le32(perdatum
->value
);
2740 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2744 rc
= put_entry(key
, 1, len
, fp
);
2751 static int common_write(void *vkey
, void *datum
, void *ptr
)
2754 struct common_datum
*comdatum
= datum
;
2755 struct policy_data
*pd
= ptr
;
2762 buf
[0] = cpu_to_le32(len
);
2763 buf
[1] = cpu_to_le32(comdatum
->value
);
2764 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2765 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2766 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2770 rc
= put_entry(key
, 1, len
, fp
);
2774 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2781 static int type_set_write(struct type_set
*t
, void *fp
)
2786 if (ebitmap_write(&t
->types
, fp
))
2788 if (ebitmap_write(&t
->negset
, fp
))
2791 buf
[0] = cpu_to_le32(t
->flags
);
2792 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2799 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2802 struct constraint_node
*c
;
2803 struct constraint_expr
*e
;
2808 for (c
= node
; c
; c
= c
->next
) {
2810 for (e
= c
->expr
; e
; e
= e
->next
)
2812 buf
[0] = cpu_to_le32(c
->permissions
);
2813 buf
[1] = cpu_to_le32(nel
);
2814 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2817 for (e
= c
->expr
; e
; e
= e
->next
) {
2818 buf
[0] = cpu_to_le32(e
->expr_type
);
2819 buf
[1] = cpu_to_le32(e
->attr
);
2820 buf
[2] = cpu_to_le32(e
->op
);
2821 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2825 switch (e
->expr_type
) {
2827 rc
= ebitmap_write(&e
->names
, fp
);
2830 if (p
->policyvers
>=
2831 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2832 rc
= type_set_write(e
->type_names
, fp
);
2846 static int class_write(void *vkey
, void *datum
, void *ptr
)
2849 struct class_datum
*cladatum
= datum
;
2850 struct policy_data
*pd
= ptr
;
2852 struct policydb
*p
= pd
->p
;
2853 struct constraint_node
*c
;
2860 if (cladatum
->comkey
)
2861 len2
= strlen(cladatum
->comkey
);
2866 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2869 buf
[0] = cpu_to_le32(len
);
2870 buf
[1] = cpu_to_le32(len2
);
2871 buf
[2] = cpu_to_le32(cladatum
->value
);
2872 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2873 if (cladatum
->permissions
.table
)
2874 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2877 buf
[5] = cpu_to_le32(ncons
);
2878 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2882 rc
= put_entry(key
, 1, len
, fp
);
2886 if (cladatum
->comkey
) {
2887 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2892 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2896 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2900 /* write out the validatetrans rule */
2902 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2905 buf
[0] = cpu_to_le32(ncons
);
2906 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2910 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2914 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2915 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2916 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2917 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2919 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2924 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2925 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2926 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2934 static int role_write(void *vkey
, void *datum
, void *ptr
)
2937 struct role_datum
*role
= datum
;
2938 struct policy_data
*pd
= ptr
;
2940 struct policydb
*p
= pd
->p
;
2947 buf
[items
++] = cpu_to_le32(len
);
2948 buf
[items
++] = cpu_to_le32(role
->value
);
2949 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2950 buf
[items
++] = cpu_to_le32(role
->bounds
);
2952 BUG_ON(items
> ARRAY_SIZE(buf
));
2954 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2958 rc
= put_entry(key
, 1, len
, fp
);
2962 rc
= ebitmap_write(&role
->dominates
, fp
);
2966 rc
= ebitmap_write(&role
->types
, fp
);
2973 static int type_write(void *vkey
, void *datum
, void *ptr
)
2976 struct type_datum
*typdatum
= datum
;
2977 struct policy_data
*pd
= ptr
;
2978 struct policydb
*p
= pd
->p
;
2986 buf
[items
++] = cpu_to_le32(len
);
2987 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2988 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2991 if (typdatum
->primary
)
2992 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2994 if (typdatum
->attribute
)
2995 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2997 buf
[items
++] = cpu_to_le32(properties
);
2998 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
3000 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
3002 BUG_ON(items
> ARRAY_SIZE(buf
));
3003 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3007 rc
= put_entry(key
, 1, len
, fp
);
3014 static int user_write(void *vkey
, void *datum
, void *ptr
)
3017 struct user_datum
*usrdatum
= datum
;
3018 struct policy_data
*pd
= ptr
;
3019 struct policydb
*p
= pd
->p
;
3027 buf
[items
++] = cpu_to_le32(len
);
3028 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3029 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3030 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3031 BUG_ON(items
> ARRAY_SIZE(buf
));
3032 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3036 rc
= put_entry(key
, 1, len
, fp
);
3040 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3044 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3048 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3055 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3068 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3071 unsigned int i
, j
, rc
;
3076 for (i
= 0; i
< info
->ocon_num
; i
++) {
3078 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3080 buf
[0] = cpu_to_le32(nel
);
3081 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3084 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3087 buf
[0] = cpu_to_le32(c
->sid
[0]);
3088 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3091 rc
= context_write(p
, &c
->context
[0], fp
);
3097 len
= strlen(c
->u
.name
);
3098 buf
[0] = cpu_to_le32(len
);
3099 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3102 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3105 rc
= context_write(p
, &c
->context
[0], fp
);
3108 rc
= context_write(p
, &c
->context
[1], fp
);
3113 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3114 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3115 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3116 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3119 rc
= context_write(p
, &c
->context
[0], fp
);
3124 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3125 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3126 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3129 rc
= context_write(p
, &c
->context
[0], fp
);
3134 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3135 len
= strlen(c
->u
.name
);
3136 buf
[1] = cpu_to_le32(len
);
3137 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3140 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3143 rc
= context_write(p
, &c
->context
[0], fp
);
3148 for (j
= 0; j
< 4; j
++)
3149 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3150 for (j
= 0; j
< 4; j
++)
3151 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3152 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3155 rc
= context_write(p
, &c
->context
[0], fp
);
3165 static int genfs_write(struct policydb
*p
, void *fp
)
3167 struct genfs
*genfs
;
3174 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3176 buf
[0] = cpu_to_le32(len
);
3177 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3180 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3181 len
= strlen(genfs
->fstype
);
3182 buf
[0] = cpu_to_le32(len
);
3183 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3186 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3190 for (c
= genfs
->head
; c
; c
= c
->next
)
3192 buf
[0] = cpu_to_le32(len
);
3193 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3196 for (c
= genfs
->head
; c
; c
= c
->next
) {
3197 len
= strlen(c
->u
.name
);
3198 buf
[0] = cpu_to_le32(len
);
3199 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3202 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3205 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3206 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3209 rc
= context_write(p
, &c
->context
[0], fp
);
3217 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3225 static int range_write_helper(void *key
, void *data
, void *ptr
)
3228 struct range_trans
*rt
= key
;
3229 struct mls_range
*r
= data
;
3230 struct policy_data
*pd
= ptr
;
3232 struct policydb
*p
= pd
->p
;
3235 buf
[0] = cpu_to_le32(rt
->source_type
);
3236 buf
[1] = cpu_to_le32(rt
->target_type
);
3237 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3240 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3241 buf
[0] = cpu_to_le32(rt
->target_class
);
3242 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3246 rc
= mls_write_range_helper(r
, fp
);
3253 static int range_write(struct policydb
*p
, void *fp
)
3257 struct policy_data pd
;
3262 /* count the number of entries in the hashtab */
3264 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3268 buf
[0] = cpu_to_le32(nel
);
3269 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3273 /* actually write all of the entries */
3274 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3281 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3284 struct filename_trans
*ft
= key
;
3285 struct filename_trans_datum
*otype
= data
;
3290 len
= strlen(ft
->name
);
3291 buf
[0] = cpu_to_le32(len
);
3292 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3296 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3300 buf
[0] = cpu_to_le32(ft
->stype
);
3301 buf
[1] = cpu_to_le32(ft
->ttype
);
3302 buf
[2] = cpu_to_le32(ft
->tclass
);
3303 buf
[3] = cpu_to_le32(otype
->otype
);
3305 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3312 static int filename_trans_write(struct policydb
*p
, void *fp
)
3318 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3322 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3326 buf
[0] = cpu_to_le32(nel
);
3327 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3331 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3339 * Write the configuration data in a policy database
3340 * structure to a policy database binary representation
3343 int policydb_write(struct policydb
*p
, void *fp
)
3345 unsigned int i
, num_syms
;
3350 struct policydb_compat_info
*info
;
3353 * refuse to write policy older than compressed avtab
3354 * to simplify the writer. There are other tests dropped
3355 * since we assume this throughout the writer code. Be
3356 * careful if you ever try to remove this restriction
3358 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3359 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3360 " Because it is less than version %d\n", p
->policyvers
,
3361 POLICYDB_VERSION_AVTAB
);
3367 config
|= POLICYDB_CONFIG_MLS
;
3369 if (p
->reject_unknown
)
3370 config
|= REJECT_UNKNOWN
;
3371 if (p
->allow_unknown
)
3372 config
|= ALLOW_UNKNOWN
;
3374 /* Write the magic number and string identifiers. */
3375 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3376 len
= strlen(POLICYDB_STRING
);
3377 buf
[1] = cpu_to_le32(len
);
3378 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3381 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3385 /* Write the version, config, and table sizes. */
3386 info
= policydb_lookup_compat(p
->policyvers
);
3388 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3389 "version %d", p
->policyvers
);
3393 buf
[0] = cpu_to_le32(p
->policyvers
);
3394 buf
[1] = cpu_to_le32(config
);
3395 buf
[2] = cpu_to_le32(info
->sym_num
);
3396 buf
[3] = cpu_to_le32(info
->ocon_num
);
3398 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3402 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3403 rc
= ebitmap_write(&p
->policycaps
, fp
);
3408 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3409 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3414 num_syms
= info
->sym_num
;
3415 for (i
= 0; i
< num_syms
; i
++) {
3416 struct policy_data pd
;
3421 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3422 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3424 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3427 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3432 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3436 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3440 rc
= role_trans_write(p
, fp
);
3444 rc
= role_allow_write(p
->role_allow
, fp
);
3448 rc
= filename_trans_write(p
, fp
);
3452 rc
= ocontext_write(p
, info
, fp
);
3456 rc
= genfs_write(p
, fp
);
3460 rc
= range_write(p
, fp
);
3464 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3465 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3468 rc
= ebitmap_write(e
, fp
);