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
;
270 * Initialize a policy database structure.
272 static int policydb_init(struct policydb
*p
)
276 memset(p
, 0, sizeof(*p
));
278 for (i
= 0; i
< SYM_NUM
; i
++) {
279 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
284 rc
= avtab_init(&p
->te_avtab
);
292 rc
= cond_policydb_init(p
);
296 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
297 if (!p
->filename_trans
)
300 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
304 ebitmap_init(&p
->filename_trans_ttypes
);
305 ebitmap_init(&p
->policycaps
);
306 ebitmap_init(&p
->permissive_map
);
310 hashtab_destroy(p
->filename_trans
);
311 hashtab_destroy(p
->range_tr
);
312 for (i
= 0; i
< SYM_NUM
; i
++)
313 hashtab_destroy(p
->symtab
[i
].table
);
318 * The following *_index functions are used to
319 * define the val_to_name and val_to_struct arrays
320 * in a policy database structure. The val_to_name
321 * arrays are used when converting security context
322 * structures into string representations. The
323 * val_to_struct arrays are used when the attributes
324 * of a class, role, or user are needed.
327 static int common_index(void *key
, void *datum
, void *datap
)
330 struct common_datum
*comdatum
;
331 struct flex_array
*fa
;
335 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
338 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
339 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
340 GFP_KERNEL
| __GFP_ZERO
))
345 static int class_index(void *key
, void *datum
, void *datap
)
348 struct class_datum
*cladatum
;
349 struct flex_array
*fa
;
353 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
355 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
356 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
357 GFP_KERNEL
| __GFP_ZERO
))
359 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
363 static int role_index(void *key
, void *datum
, void *datap
)
366 struct role_datum
*role
;
367 struct flex_array
*fa
;
372 || role
->value
> p
->p_roles
.nprim
373 || role
->bounds
> p
->p_roles
.nprim
)
376 fa
= p
->sym_val_to_name
[SYM_ROLES
];
377 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
378 GFP_KERNEL
| __GFP_ZERO
))
380 p
->role_val_to_struct
[role
->value
- 1] = role
;
384 static int type_index(void *key
, void *datum
, void *datap
)
387 struct type_datum
*typdatum
;
388 struct flex_array
*fa
;
393 if (typdatum
->primary
) {
395 || typdatum
->value
> p
->p_types
.nprim
396 || typdatum
->bounds
> p
->p_types
.nprim
)
398 fa
= p
->sym_val_to_name
[SYM_TYPES
];
399 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
400 GFP_KERNEL
| __GFP_ZERO
))
403 fa
= p
->type_val_to_struct_array
;
404 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
405 GFP_KERNEL
| __GFP_ZERO
))
412 static int user_index(void *key
, void *datum
, void *datap
)
415 struct user_datum
*usrdatum
;
416 struct flex_array
*fa
;
421 || usrdatum
->value
> p
->p_users
.nprim
422 || usrdatum
->bounds
> p
->p_users
.nprim
)
425 fa
= p
->sym_val_to_name
[SYM_USERS
];
426 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
427 GFP_KERNEL
| __GFP_ZERO
))
429 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
433 static int sens_index(void *key
, void *datum
, void *datap
)
436 struct level_datum
*levdatum
;
437 struct flex_array
*fa
;
442 if (!levdatum
->isalias
) {
443 if (!levdatum
->level
->sens
||
444 levdatum
->level
->sens
> p
->p_levels
.nprim
)
446 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
447 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
448 GFP_KERNEL
| __GFP_ZERO
))
455 static int cat_index(void *key
, void *datum
, void *datap
)
458 struct cat_datum
*catdatum
;
459 struct flex_array
*fa
;
464 if (!catdatum
->isalias
) {
465 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
467 fa
= p
->sym_val_to_name
[SYM_CATS
];
468 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
469 GFP_KERNEL
| __GFP_ZERO
))
476 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
489 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
491 struct hashtab_info info
;
493 hashtab_stat(h
, &info
);
494 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
495 "longest chain length %d\n", hash_name
, h
->nel
,
496 info
.slots_used
, h
->size
, info
.max_chain_len
);
499 static void symtab_hash_eval(struct symtab
*s
)
503 for (i
= 0; i
< SYM_NUM
; i
++)
504 hash_eval(s
[i
].table
, symtab_name
[i
]);
508 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
514 * Define the other val_to_name and val_to_struct arrays
515 * in a policy database structure.
517 * Caller must clean up on failure.
519 static int policydb_index(struct policydb
*p
)
523 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
524 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
526 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
530 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
531 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
534 avtab_hash_eval(&p
->te_avtab
, "rules");
535 symtab_hash_eval(p
->symtab
);
539 p
->class_val_to_struct
=
540 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
542 if (!p
->class_val_to_struct
)
546 p
->role_val_to_struct
=
547 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
549 if (!p
->role_val_to_struct
)
553 p
->user_val_to_struct
=
554 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
556 if (!p
->user_val_to_struct
)
559 /* Yes, I want the sizeof the pointer, not the structure */
561 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
563 GFP_KERNEL
| __GFP_ZERO
);
564 if (!p
->type_val_to_struct_array
)
567 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
568 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
572 rc
= cond_init_bool_indexes(p
);
576 for (i
= 0; i
< SYM_NUM
; i
++) {
578 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
580 GFP_KERNEL
| __GFP_ZERO
);
581 if (!p
->sym_val_to_name
[i
])
584 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
585 0, p
->symtab
[i
].nprim
,
586 GFP_KERNEL
| __GFP_ZERO
);
590 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
600 * The following *_destroy functions are used to
601 * free any memory allocated for each kind of
602 * symbol data in the policy database.
605 static int perm_destroy(void *key
, void *datum
, void *p
)
612 static int common_destroy(void *key
, void *datum
, void *p
)
614 struct common_datum
*comdatum
;
619 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
620 hashtab_destroy(comdatum
->permissions
.table
);
627 static void constraint_expr_destroy(struct constraint_expr
*expr
)
630 ebitmap_destroy(&expr
->names
);
631 if (expr
->type_names
) {
632 ebitmap_destroy(&expr
->type_names
->types
);
633 ebitmap_destroy(&expr
->type_names
->negset
);
634 kfree(expr
->type_names
);
640 static int cls_destroy(void *key
, void *datum
, void *p
)
642 struct class_datum
*cladatum
;
643 struct constraint_node
*constraint
, *ctemp
;
644 struct constraint_expr
*e
, *etmp
;
649 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
650 hashtab_destroy(cladatum
->permissions
.table
);
651 constraint
= cladatum
->constraints
;
653 e
= constraint
->expr
;
657 constraint_expr_destroy(etmp
);
660 constraint
= constraint
->next
;
664 constraint
= cladatum
->validatetrans
;
666 e
= constraint
->expr
;
670 constraint_expr_destroy(etmp
);
673 constraint
= constraint
->next
;
676 kfree(cladatum
->comkey
);
682 static int role_destroy(void *key
, void *datum
, void *p
)
684 struct role_datum
*role
;
689 ebitmap_destroy(&role
->dominates
);
690 ebitmap_destroy(&role
->types
);
696 static int type_destroy(void *key
, void *datum
, void *p
)
703 static int user_destroy(void *key
, void *datum
, void *p
)
705 struct user_datum
*usrdatum
;
710 ebitmap_destroy(&usrdatum
->roles
);
711 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
712 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
713 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
719 static int sens_destroy(void *key
, void *datum
, void *p
)
721 struct level_datum
*levdatum
;
726 ebitmap_destroy(&levdatum
->level
->cat
);
727 kfree(levdatum
->level
);
733 static int cat_destroy(void *key
, void *datum
, void *p
)
740 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
752 static int filenametr_destroy(void *key
, void *datum
, void *p
)
754 struct filename_trans
*ft
= key
;
762 static int range_tr_destroy(void *key
, void *datum
, void *p
)
764 struct mls_range
*rt
= datum
;
766 ebitmap_destroy(&rt
->level
[0].cat
);
767 ebitmap_destroy(&rt
->level
[1].cat
);
773 static void ocontext_destroy(struct ocontext
*c
, int i
)
778 context_destroy(&c
->context
[0]);
779 context_destroy(&c
->context
[1]);
780 if (i
== OCON_ISID
|| i
== OCON_FS
||
781 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
787 * Free any memory allocated by a policy database structure.
789 void policydb_destroy(struct policydb
*p
)
791 struct ocontext
*c
, *ctmp
;
792 struct genfs
*g
, *gtmp
;
794 struct role_allow
*ra
, *lra
= NULL
;
795 struct role_trans
*tr
, *ltr
= NULL
;
797 for (i
= 0; i
< SYM_NUM
; i
++) {
799 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
800 hashtab_destroy(p
->symtab
[i
].table
);
803 for (i
= 0; i
< SYM_NUM
; i
++) {
804 if (p
->sym_val_to_name
[i
])
805 flex_array_free(p
->sym_val_to_name
[i
]);
808 kfree(p
->class_val_to_struct
);
809 kfree(p
->role_val_to_struct
);
810 kfree(p
->user_val_to_struct
);
811 if (p
->type_val_to_struct_array
)
812 flex_array_free(p
->type_val_to_struct_array
);
814 avtab_destroy(&p
->te_avtab
);
816 for (i
= 0; i
< OCON_NUM
; i
++) {
822 ocontext_destroy(ctmp
, i
);
824 p
->ocontexts
[i
] = NULL
;
835 ocontext_destroy(ctmp
, OCON_FSUSE
);
843 cond_policydb_destroy(p
);
845 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
852 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
859 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
860 hashtab_destroy(p
->filename_trans
);
862 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
863 hashtab_destroy(p
->range_tr
);
865 if (p
->type_attr_map_array
) {
866 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
869 e
= flex_array_get(p
->type_attr_map_array
, i
);
874 flex_array_free(p
->type_attr_map_array
);
877 ebitmap_destroy(&p
->filename_trans_ttypes
);
878 ebitmap_destroy(&p
->policycaps
);
879 ebitmap_destroy(&p
->permissive_map
);
885 * Load the initial SIDs specified in a policy database
886 * structure into a SID table.
888 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
890 struct ocontext
*head
, *c
;
895 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
899 head
= p
->ocontexts
[OCON_ISID
];
900 for (c
= head
; c
; c
= c
->next
) {
902 if (!c
->context
[0].user
) {
903 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
908 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
910 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
920 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
922 if (!class || class > p
->p_classes
.nprim
)
927 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
929 if (!role
|| role
> p
->p_roles
.nprim
)
934 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
936 if (!type
|| type
> p
->p_types
.nprim
)
942 * Return 1 if the fields in the security context
943 * structure `c' are valid. Return 0 otherwise.
945 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
947 struct role_datum
*role
;
948 struct user_datum
*usrdatum
;
950 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
953 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
956 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
959 if (c
->role
!= OBJECT_R_VAL
) {
961 * Role must be authorized for the type.
963 role
= p
->role_val_to_struct
[c
->role
- 1];
964 if (!ebitmap_get_bit(&role
->types
, c
->type
- 1))
965 /* role may not be associated with type */
969 * User must be authorized for the role.
971 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
975 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
976 /* user may not be associated with role */
980 if (!mls_context_isvalid(p
, c
))
987 * Read a MLS range structure from a policydb binary
988 * representation file.
990 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
996 rc
= next_entry(buf
, fp
, sizeof(u32
));
1001 items
= le32_to_cpu(buf
[0]);
1002 if (items
> ARRAY_SIZE(buf
)) {
1003 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1007 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1009 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1013 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1015 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1017 r
->level
[1].sens
= r
->level
[0].sens
;
1019 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1021 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1025 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1027 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1031 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1033 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1040 ebitmap_destroy(&r
->level
[0].cat
);
1046 * Read and validate a security context structure
1047 * from a policydb binary representation file.
1049 static int context_read_and_validate(struct context
*c
,
1056 rc
= next_entry(buf
, fp
, sizeof buf
);
1058 printk(KERN_ERR
"SELinux: context truncated\n");
1061 c
->user
= le32_to_cpu(buf
[0]);
1062 c
->role
= le32_to_cpu(buf
[1]);
1063 c
->type
= le32_to_cpu(buf
[2]);
1064 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1065 rc
= mls_read_range_helper(&c
->range
, fp
);
1067 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1073 if (!policydb_context_isvalid(p
, c
)) {
1074 printk(KERN_ERR
"SELinux: invalid security context\n");
1084 * The following *_read functions are used to
1085 * read the symbol data from a policy database
1086 * binary representation file.
1089 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1092 struct perm_datum
*perdatum
;
1098 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1102 rc
= next_entry(buf
, fp
, sizeof buf
);
1106 len
= le32_to_cpu(buf
[0]);
1107 perdatum
->value
= le32_to_cpu(buf
[1]);
1110 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1114 rc
= next_entry(key
, fp
, len
);
1119 rc
= hashtab_insert(h
, key
, perdatum
);
1125 perm_destroy(key
, perdatum
, NULL
);
1129 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1132 struct common_datum
*comdatum
;
1138 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1142 rc
= next_entry(buf
, fp
, sizeof buf
);
1146 len
= le32_to_cpu(buf
[0]);
1147 comdatum
->value
= le32_to_cpu(buf
[1]);
1149 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1152 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1153 nel
= le32_to_cpu(buf
[3]);
1156 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1160 rc
= next_entry(key
, fp
, len
);
1165 for (i
= 0; i
< nel
; i
++) {
1166 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1171 rc
= hashtab_insert(h
, key
, comdatum
);
1176 common_destroy(key
, comdatum
, NULL
);
1180 static void type_set_init(struct type_set
*t
)
1182 ebitmap_init(&t
->types
);
1183 ebitmap_init(&t
->negset
);
1186 static int type_set_read(struct type_set
*t
, void *fp
)
1191 if (ebitmap_read(&t
->types
, fp
))
1193 if (ebitmap_read(&t
->negset
, fp
))
1196 rc
= next_entry(buf
, fp
, sizeof(u32
));
1199 t
->flags
= le32_to_cpu(buf
[0]);
1205 static int read_cons_helper(struct policydb
*p
,
1206 struct constraint_node
**nodep
,
1207 int ncons
, int allowxtarget
, void *fp
)
1209 struct constraint_node
*c
, *lc
;
1210 struct constraint_expr
*e
, *le
;
1213 int rc
, i
, j
, depth
;
1216 for (i
= 0; i
< ncons
; i
++) {
1217 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1226 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1229 c
->permissions
= le32_to_cpu(buf
[0]);
1230 nexpr
= le32_to_cpu(buf
[1]);
1233 for (j
= 0; j
< nexpr
; j
++) {
1234 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1243 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1246 e
->expr_type
= le32_to_cpu(buf
[0]);
1247 e
->attr
= le32_to_cpu(buf
[1]);
1248 e
->op
= le32_to_cpu(buf
[2]);
1250 switch (e
->expr_type
) {
1262 if (depth
== (CEXPR_MAXDEPTH
- 1))
1267 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1269 if (depth
== (CEXPR_MAXDEPTH
- 1))
1272 rc
= ebitmap_read(&e
->names
, fp
);
1275 if (p
->policyvers
>=
1276 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1277 e
->type_names
= kzalloc(sizeof
1282 type_set_init(e
->type_names
);
1283 rc
= type_set_read(e
->type_names
, fp
);
1301 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1304 struct class_datum
*cladatum
;
1306 u32 len
, len2
, ncons
, nel
;
1310 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1314 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1318 len
= le32_to_cpu(buf
[0]);
1319 len2
= le32_to_cpu(buf
[1]);
1320 cladatum
->value
= le32_to_cpu(buf
[2]);
1322 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1325 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1326 nel
= le32_to_cpu(buf
[4]);
1328 ncons
= le32_to_cpu(buf
[5]);
1331 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1335 rc
= next_entry(key
, fp
, len
);
1342 cladatum
->comkey
= kmalloc(len2
+ 1, GFP_KERNEL
);
1343 if (!cladatum
->comkey
)
1345 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1348 cladatum
->comkey
[len2
] = '\0';
1351 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1352 if (!cladatum
->comdatum
) {
1353 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1357 for (i
= 0; i
< nel
; i
++) {
1358 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1363 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1367 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1368 /* grab the validatetrans rules */
1369 rc
= next_entry(buf
, fp
, sizeof(u32
));
1372 ncons
= le32_to_cpu(buf
[0]);
1373 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1379 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1380 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1384 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1385 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1386 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1389 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1390 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1393 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1396 rc
= hashtab_insert(h
, key
, cladatum
);
1402 cls_destroy(key
, cladatum
, NULL
);
1406 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1409 struct role_datum
*role
;
1410 int rc
, to_read
= 2;
1415 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1419 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1422 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1426 len
= le32_to_cpu(buf
[0]);
1427 role
->value
= le32_to_cpu(buf
[1]);
1428 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1429 role
->bounds
= le32_to_cpu(buf
[2]);
1432 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1436 rc
= next_entry(key
, fp
, len
);
1441 rc
= ebitmap_read(&role
->dominates
, fp
);
1445 rc
= ebitmap_read(&role
->types
, fp
);
1449 if (strcmp(key
, OBJECT_R
) == 0) {
1451 if (role
->value
!= OBJECT_R_VAL
) {
1452 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1453 OBJECT_R
, role
->value
);
1460 rc
= hashtab_insert(h
, key
, role
);
1465 role_destroy(key
, role
, NULL
);
1469 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1472 struct type_datum
*typdatum
;
1473 int rc
, to_read
= 3;
1478 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1482 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1485 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1489 len
= le32_to_cpu(buf
[0]);
1490 typdatum
->value
= le32_to_cpu(buf
[1]);
1491 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1492 u32 prop
= le32_to_cpu(buf
[2]);
1494 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1495 typdatum
->primary
= 1;
1496 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1497 typdatum
->attribute
= 1;
1499 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1501 typdatum
->primary
= le32_to_cpu(buf
[2]);
1505 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1508 rc
= next_entry(key
, fp
, len
);
1513 rc
= hashtab_insert(h
, key
, typdatum
);
1518 panic("SELinux:Failed to type read");
1520 type_destroy(key
, typdatum
, NULL
);
1526 * Read a MLS level structure from a policydb binary
1527 * representation file.
1529 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1534 memset(lp
, 0, sizeof(*lp
));
1536 rc
= next_entry(buf
, fp
, sizeof buf
);
1538 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1541 lp
->sens
= le32_to_cpu(buf
[0]);
1543 rc
= ebitmap_read(&lp
->cat
, fp
);
1545 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1551 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1554 struct user_datum
*usrdatum
;
1555 int rc
, to_read
= 2;
1560 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1564 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1567 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1571 len
= le32_to_cpu(buf
[0]);
1572 usrdatum
->value
= le32_to_cpu(buf
[1]);
1573 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1574 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1577 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1580 rc
= next_entry(key
, fp
, len
);
1585 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1589 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1590 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1593 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1598 rc
= hashtab_insert(h
, key
, usrdatum
);
1603 user_destroy(key
, usrdatum
, NULL
);
1607 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1610 struct level_datum
*levdatum
;
1616 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1620 rc
= next_entry(buf
, fp
, sizeof buf
);
1624 len
= le32_to_cpu(buf
[0]);
1625 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1628 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1631 rc
= next_entry(key
, fp
, len
);
1637 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1638 if (!levdatum
->level
)
1641 rc
= mls_read_level(levdatum
->level
, fp
);
1645 rc
= hashtab_insert(h
, key
, levdatum
);
1650 sens_destroy(key
, levdatum
, NULL
);
1654 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1657 struct cat_datum
*catdatum
;
1663 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1667 rc
= next_entry(buf
, fp
, sizeof buf
);
1671 len
= le32_to_cpu(buf
[0]);
1672 catdatum
->value
= le32_to_cpu(buf
[1]);
1673 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1676 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1679 rc
= next_entry(key
, fp
, len
);
1684 rc
= hashtab_insert(h
, key
, catdatum
);
1689 cat_destroy(key
, catdatum
, NULL
);
1693 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1705 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1707 struct user_datum
*upper
, *user
;
1708 struct policydb
*p
= datap
;
1711 upper
= user
= datum
;
1712 while (upper
->bounds
) {
1713 struct ebitmap_node
*node
;
1716 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1717 printk(KERN_ERR
"SELinux: user %s: "
1718 "too deep or looped boundary",
1723 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1724 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1725 if (ebitmap_get_bit(&upper
->roles
, bit
))
1729 "SELinux: boundary violated policy: "
1730 "user=%s role=%s bounds=%s\n",
1731 sym_name(p
, SYM_USERS
, user
->value
- 1),
1732 sym_name(p
, SYM_ROLES
, bit
),
1733 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1742 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1744 struct role_datum
*upper
, *role
;
1745 struct policydb
*p
= datap
;
1748 upper
= role
= datum
;
1749 while (upper
->bounds
) {
1750 struct ebitmap_node
*node
;
1753 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1754 printk(KERN_ERR
"SELinux: role %s: "
1755 "too deep or looped bounds\n",
1760 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1761 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1762 if (ebitmap_get_bit(&upper
->types
, bit
))
1766 "SELinux: boundary violated policy: "
1767 "role=%s type=%s bounds=%s\n",
1768 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1769 sym_name(p
, SYM_TYPES
, bit
),
1770 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1779 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1781 struct type_datum
*upper
;
1782 struct policydb
*p
= datap
;
1786 while (upper
->bounds
) {
1787 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1788 printk(KERN_ERR
"SELinux: type %s: "
1789 "too deep or looped boundary\n",
1794 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1798 if (upper
->attribute
) {
1799 printk(KERN_ERR
"SELinux: type %s: "
1800 "bounded by attribute %s",
1802 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1810 static int policydb_bounds_sanity_check(struct policydb
*p
)
1814 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1817 rc
= hashtab_map(p
->p_users
.table
,
1818 user_bounds_sanity_check
, p
);
1822 rc
= hashtab_map(p
->p_roles
.table
,
1823 role_bounds_sanity_check
, p
);
1827 rc
= hashtab_map(p
->p_types
.table
,
1828 type_bounds_sanity_check
, p
);
1835 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1837 struct class_datum
*cladatum
;
1839 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1843 return cladatum
->value
;
1846 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1848 struct class_datum
*cladatum
;
1849 struct perm_datum
*perdatum
= NULL
;
1850 struct common_datum
*comdatum
;
1852 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1855 cladatum
= p
->class_val_to_struct
[tclass
-1];
1856 comdatum
= cladatum
->comdatum
;
1858 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1861 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1866 return 1U << (perdatum
->value
-1);
1869 static int range_read(struct policydb
*p
, void *fp
)
1871 struct range_trans
*rt
= NULL
;
1872 struct mls_range
*r
= NULL
;
1877 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1880 rc
= next_entry(buf
, fp
, sizeof(u32
));
1884 nel
= le32_to_cpu(buf
[0]);
1885 for (i
= 0; i
< nel
; i
++) {
1887 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1891 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1895 rt
->source_type
= le32_to_cpu(buf
[0]);
1896 rt
->target_type
= le32_to_cpu(buf
[1]);
1897 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1898 rc
= next_entry(buf
, fp
, sizeof(u32
));
1901 rt
->target_class
= le32_to_cpu(buf
[0]);
1903 rt
->target_class
= p
->process_class
;
1906 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1907 !policydb_type_isvalid(p
, rt
->target_type
) ||
1908 !policydb_class_isvalid(p
, rt
->target_class
))
1912 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1916 rc
= mls_read_range_helper(r
, fp
);
1921 if (!mls_range_isvalid(p
, r
)) {
1922 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1926 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1933 hash_eval(p
->range_tr
, "rangetr");
1941 static int filename_trans_read(struct policydb
*p
, void *fp
)
1943 struct filename_trans
*ft
;
1944 struct filename_trans_datum
*otype
;
1950 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1953 rc
= next_entry(buf
, fp
, sizeof(u32
));
1956 nel
= le32_to_cpu(buf
[0]);
1958 for (i
= 0; i
< nel
; i
++) {
1964 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1969 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1973 /* length of the path component string */
1974 rc
= next_entry(buf
, fp
, sizeof(u32
));
1977 len
= le32_to_cpu(buf
[0]);
1980 name
= kmalloc(len
+ 1, GFP_KERNEL
);
1986 /* path component string */
1987 rc
= next_entry(name
, fp
, len
);
1992 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1996 ft
->stype
= le32_to_cpu(buf
[0]);
1997 ft
->ttype
= le32_to_cpu(buf
[1]);
1998 ft
->tclass
= le32_to_cpu(buf
[2]);
2000 otype
->otype
= le32_to_cpu(buf
[3]);
2002 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
2006 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
2009 * Do not return -EEXIST to the caller, or the system
2014 /* But free memory to avoid memory leak. */
2020 hash_eval(p
->filename_trans
, "filenametr");
2030 static int genfs_read(struct policydb
*p
, void *fp
)
2033 u32 nel
, nel2
, len
, len2
;
2035 struct ocontext
*l
, *c
;
2036 struct ocontext
*newc
= NULL
;
2037 struct genfs
*genfs_p
, *genfs
;
2038 struct genfs
*newgenfs
= NULL
;
2040 rc
= next_entry(buf
, fp
, sizeof(u32
));
2043 nel
= le32_to_cpu(buf
[0]);
2045 for (i
= 0; i
< nel
; i
++) {
2046 rc
= next_entry(buf
, fp
, sizeof(u32
));
2049 len
= le32_to_cpu(buf
[0]);
2052 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2057 newgenfs
->fstype
= kmalloc(len
+ 1, GFP_KERNEL
);
2058 if (!newgenfs
->fstype
)
2061 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
2065 newgenfs
->fstype
[len
] = 0;
2067 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2068 genfs_p
= genfs
, genfs
= genfs
->next
) {
2070 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2071 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2075 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2078 newgenfs
->next
= genfs
;
2080 genfs_p
->next
= newgenfs
;
2082 p
->genfs
= newgenfs
;
2086 rc
= next_entry(buf
, fp
, sizeof(u32
));
2090 nel2
= le32_to_cpu(buf
[0]);
2091 for (j
= 0; j
< nel2
; j
++) {
2092 rc
= next_entry(buf
, fp
, sizeof(u32
));
2095 len
= le32_to_cpu(buf
[0]);
2098 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2103 newc
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2107 rc
= next_entry(newc
->u
.name
, fp
, len
);
2110 newc
->u
.name
[len
] = 0;
2112 rc
= next_entry(buf
, fp
, sizeof(u32
));
2116 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2117 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2121 for (l
= NULL
, c
= genfs
->head
; c
;
2122 l
= c
, c
= c
->next
) {
2124 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2125 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2126 newc
->v
.sclass
== c
->v
.sclass
)) {
2127 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2128 genfs
->fstype
, c
->u
.name
);
2131 len
= strlen(newc
->u
.name
);
2132 len2
= strlen(c
->u
.name
);
2148 kfree(newgenfs
->fstype
);
2150 ocontext_destroy(newc
, OCON_FSUSE
);
2155 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2161 struct ocontext
*l
, *c
;
2164 for (i
= 0; i
< info
->ocon_num
; i
++) {
2165 rc
= next_entry(buf
, fp
, sizeof(u32
));
2168 nel
= le32_to_cpu(buf
[0]);
2171 for (j
= 0; j
< nel
; j
++) {
2173 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2179 p
->ocontexts
[i
] = c
;
2184 rc
= next_entry(buf
, fp
, sizeof(u32
));
2188 c
->sid
[0] = le32_to_cpu(buf
[0]);
2189 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2195 rc
= next_entry(buf
, fp
, sizeof(u32
));
2198 len
= le32_to_cpu(buf
[0]);
2201 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2205 rc
= next_entry(c
->u
.name
, fp
, len
);
2210 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2213 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2218 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2221 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2222 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2223 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2224 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2229 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2232 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2233 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2234 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2239 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2244 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2245 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
2249 len
= le32_to_cpu(buf
[1]);
2250 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2254 rc
= next_entry(c
->u
.name
, fp
, len
);
2258 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2265 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2268 for (k
= 0; k
< 4; k
++)
2269 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2270 for (k
= 0; k
< 4; k
++)
2271 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2272 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2286 * Read the configuration data from a policy database binary
2287 * representation file into a policy database structure.
2289 int policydb_read(struct policydb
*p
, void *fp
)
2291 struct role_allow
*ra
, *lra
;
2292 struct role_trans
*tr
, *ltr
;
2295 u32 len
, nprim
, nel
;
2298 struct policydb_compat_info
*info
;
2300 rc
= policydb_init(p
);
2304 /* Read the magic number and string length. */
2305 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2310 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2311 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2312 "not match expected magic number 0x%x\n",
2313 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2318 len
= le32_to_cpu(buf
[1]);
2319 if (len
!= strlen(POLICYDB_STRING
)) {
2320 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2321 "match expected length %Zu\n",
2322 len
, strlen(POLICYDB_STRING
));
2327 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2328 if (!policydb_str
) {
2329 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2330 "string of length %d\n", len
);
2334 rc
= next_entry(policydb_str
, fp
, len
);
2336 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2337 kfree(policydb_str
);
2342 policydb_str
[len
] = '\0';
2343 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2344 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2345 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2346 kfree(policydb_str
);
2349 /* Done with policydb_str. */
2350 kfree(policydb_str
);
2351 policydb_str
= NULL
;
2353 /* Read the version and table sizes. */
2354 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2359 p
->policyvers
= le32_to_cpu(buf
[0]);
2360 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2361 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2362 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2363 "my version range %d-%d\n",
2364 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2368 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2372 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2373 printk(KERN_ERR
"SELinux: security policydb version %d "
2374 "(MLS) not backwards compatible\n",
2379 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2380 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2382 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2383 rc
= ebitmap_read(&p
->policycaps
, fp
);
2388 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2389 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2395 info
= policydb_lookup_compat(p
->policyvers
);
2397 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2398 "for version %d\n", p
->policyvers
);
2403 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2404 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2405 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2406 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2407 le32_to_cpu(buf
[3]),
2408 info
->sym_num
, info
->ocon_num
);
2412 for (i
= 0; i
< info
->sym_num
; i
++) {
2413 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2416 nprim
= le32_to_cpu(buf
[0]);
2417 nel
= le32_to_cpu(buf
[1]);
2418 for (j
= 0; j
< nel
; j
++) {
2419 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2424 p
->symtab
[i
].nprim
= nprim
;
2428 p
->process_class
= string_to_security_class(p
, "process");
2429 if (!p
->process_class
)
2432 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2436 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2437 rc
= cond_read_list(p
, fp
);
2442 rc
= next_entry(buf
, fp
, sizeof(u32
));
2445 nel
= le32_to_cpu(buf
[0]);
2447 for (i
= 0; i
< nel
; i
++) {
2449 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2456 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2461 tr
->role
= le32_to_cpu(buf
[0]);
2462 tr
->type
= le32_to_cpu(buf
[1]);
2463 tr
->new_role
= le32_to_cpu(buf
[2]);
2464 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2465 rc
= next_entry(buf
, fp
, sizeof(u32
));
2468 tr
->tclass
= le32_to_cpu(buf
[0]);
2470 tr
->tclass
= p
->process_class
;
2472 if (!policydb_role_isvalid(p
, tr
->role
) ||
2473 !policydb_type_isvalid(p
, tr
->type
) ||
2474 !policydb_class_isvalid(p
, tr
->tclass
) ||
2475 !policydb_role_isvalid(p
, tr
->new_role
))
2480 rc
= next_entry(buf
, fp
, sizeof(u32
));
2483 nel
= le32_to_cpu(buf
[0]);
2485 for (i
= 0; i
< nel
; i
++) {
2487 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2494 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2499 ra
->role
= le32_to_cpu(buf
[0]);
2500 ra
->new_role
= le32_to_cpu(buf
[1]);
2501 if (!policydb_role_isvalid(p
, ra
->role
) ||
2502 !policydb_role_isvalid(p
, ra
->new_role
))
2507 rc
= filename_trans_read(p
, fp
);
2511 rc
= policydb_index(p
);
2516 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2517 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2518 if (!p
->process_trans_perms
)
2521 rc
= ocontext_read(p
, info
, fp
);
2525 rc
= genfs_read(p
, fp
);
2529 rc
= range_read(p
, fp
);
2534 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2536 GFP_KERNEL
| __GFP_ZERO
);
2537 if (!p
->type_attr_map_array
)
2540 /* preallocate so we don't have to worry about the put ever failing */
2541 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2542 GFP_KERNEL
| __GFP_ZERO
);
2546 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2547 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2551 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2552 rc
= ebitmap_read(e
, fp
);
2556 /* add the type itself as the degenerate case */
2557 rc
= ebitmap_set_bit(e
, i
, 1);
2562 rc
= policydb_bounds_sanity_check(p
);
2570 panic("SELinux:Failed to load policy");
2572 policydb_destroy(p
);
2577 * Write a MLS level structure to a policydb binary
2578 * representation file.
2580 static int mls_write_level(struct mls_level
*l
, void *fp
)
2585 buf
[0] = cpu_to_le32(l
->sens
);
2586 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2590 rc
= ebitmap_write(&l
->cat
, fp
);
2598 * Write a MLS range structure to a policydb binary
2599 * representation file.
2601 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2607 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2613 buf
[0] = cpu_to_le32(items
-1);
2614 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2616 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2618 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
2620 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2624 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2628 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2636 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2639 struct level_datum
*levdatum
= datum
;
2640 struct policy_data
*pd
= ptr
;
2647 buf
[0] = cpu_to_le32(len
);
2648 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2649 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2653 rc
= put_entry(key
, 1, len
, fp
);
2657 rc
= mls_write_level(levdatum
->level
, fp
);
2664 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2667 struct cat_datum
*catdatum
= datum
;
2668 struct policy_data
*pd
= ptr
;
2675 buf
[0] = cpu_to_le32(len
);
2676 buf
[1] = cpu_to_le32(catdatum
->value
);
2677 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2678 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2682 rc
= put_entry(key
, 1, len
, fp
);
2689 static int role_trans_write(struct policydb
*p
, void *fp
)
2691 struct role_trans
*r
= p
->role_tr
;
2692 struct role_trans
*tr
;
2698 for (tr
= r
; tr
; tr
= tr
->next
)
2700 buf
[0] = cpu_to_le32(nel
);
2701 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2704 for (tr
= r
; tr
; tr
= tr
->next
) {
2705 buf
[0] = cpu_to_le32(tr
->role
);
2706 buf
[1] = cpu_to_le32(tr
->type
);
2707 buf
[2] = cpu_to_le32(tr
->new_role
);
2708 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2711 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2712 buf
[0] = cpu_to_le32(tr
->tclass
);
2713 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2722 static int role_allow_write(struct role_allow
*r
, void *fp
)
2724 struct role_allow
*ra
;
2730 for (ra
= r
; ra
; ra
= ra
->next
)
2732 buf
[0] = cpu_to_le32(nel
);
2733 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2736 for (ra
= r
; ra
; ra
= ra
->next
) {
2737 buf
[0] = cpu_to_le32(ra
->role
);
2738 buf
[1] = cpu_to_le32(ra
->new_role
);
2739 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2747 * Write a security context structure
2748 * to a policydb binary representation file.
2750 static int context_write(struct policydb
*p
, struct context
*c
,
2756 buf
[0] = cpu_to_le32(c
->user
);
2757 buf
[1] = cpu_to_le32(c
->role
);
2758 buf
[2] = cpu_to_le32(c
->type
);
2760 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2764 rc
= mls_write_range_helper(&c
->range
, fp
);
2772 * The following *_write functions are used to
2773 * write the symbol data to a policy database
2774 * binary representation file.
2777 static int perm_write(void *vkey
, void *datum
, void *fp
)
2780 struct perm_datum
*perdatum
= datum
;
2786 buf
[0] = cpu_to_le32(len
);
2787 buf
[1] = cpu_to_le32(perdatum
->value
);
2788 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2792 rc
= put_entry(key
, 1, len
, fp
);
2799 static int common_write(void *vkey
, void *datum
, void *ptr
)
2802 struct common_datum
*comdatum
= datum
;
2803 struct policy_data
*pd
= ptr
;
2810 buf
[0] = cpu_to_le32(len
);
2811 buf
[1] = cpu_to_le32(comdatum
->value
);
2812 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2813 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2814 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2818 rc
= put_entry(key
, 1, len
, fp
);
2822 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2829 static int type_set_write(struct type_set
*t
, void *fp
)
2834 if (ebitmap_write(&t
->types
, fp
))
2836 if (ebitmap_write(&t
->negset
, fp
))
2839 buf
[0] = cpu_to_le32(t
->flags
);
2840 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2847 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2850 struct constraint_node
*c
;
2851 struct constraint_expr
*e
;
2856 for (c
= node
; c
; c
= c
->next
) {
2858 for (e
= c
->expr
; e
; e
= e
->next
)
2860 buf
[0] = cpu_to_le32(c
->permissions
);
2861 buf
[1] = cpu_to_le32(nel
);
2862 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2865 for (e
= c
->expr
; e
; e
= e
->next
) {
2866 buf
[0] = cpu_to_le32(e
->expr_type
);
2867 buf
[1] = cpu_to_le32(e
->attr
);
2868 buf
[2] = cpu_to_le32(e
->op
);
2869 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2873 switch (e
->expr_type
) {
2875 rc
= ebitmap_write(&e
->names
, fp
);
2878 if (p
->policyvers
>=
2879 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2880 rc
= type_set_write(e
->type_names
, fp
);
2894 static int class_write(void *vkey
, void *datum
, void *ptr
)
2897 struct class_datum
*cladatum
= datum
;
2898 struct policy_data
*pd
= ptr
;
2900 struct policydb
*p
= pd
->p
;
2901 struct constraint_node
*c
;
2908 if (cladatum
->comkey
)
2909 len2
= strlen(cladatum
->comkey
);
2914 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2917 buf
[0] = cpu_to_le32(len
);
2918 buf
[1] = cpu_to_le32(len2
);
2919 buf
[2] = cpu_to_le32(cladatum
->value
);
2920 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2921 if (cladatum
->permissions
.table
)
2922 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2925 buf
[5] = cpu_to_le32(ncons
);
2926 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2930 rc
= put_entry(key
, 1, len
, fp
);
2934 if (cladatum
->comkey
) {
2935 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2940 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2944 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2948 /* write out the validatetrans rule */
2950 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2953 buf
[0] = cpu_to_le32(ncons
);
2954 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2958 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2962 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2963 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2964 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2965 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2967 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2972 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2973 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2974 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2982 static int role_write(void *vkey
, void *datum
, void *ptr
)
2985 struct role_datum
*role
= datum
;
2986 struct policy_data
*pd
= ptr
;
2988 struct policydb
*p
= pd
->p
;
2995 buf
[items
++] = cpu_to_le32(len
);
2996 buf
[items
++] = cpu_to_le32(role
->value
);
2997 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2998 buf
[items
++] = cpu_to_le32(role
->bounds
);
3000 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
3002 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3006 rc
= put_entry(key
, 1, len
, fp
);
3010 rc
= ebitmap_write(&role
->dominates
, fp
);
3014 rc
= ebitmap_write(&role
->types
, fp
);
3021 static int type_write(void *vkey
, void *datum
, void *ptr
)
3024 struct type_datum
*typdatum
= datum
;
3025 struct policy_data
*pd
= ptr
;
3026 struct policydb
*p
= pd
->p
;
3034 buf
[items
++] = cpu_to_le32(len
);
3035 buf
[items
++] = cpu_to_le32(typdatum
->value
);
3036 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
3039 if (typdatum
->primary
)
3040 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
3042 if (typdatum
->attribute
)
3043 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
3045 buf
[items
++] = cpu_to_le32(properties
);
3046 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
3048 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
3050 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
3051 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3055 rc
= put_entry(key
, 1, len
, fp
);
3062 static int user_write(void *vkey
, void *datum
, void *ptr
)
3065 struct user_datum
*usrdatum
= datum
;
3066 struct policy_data
*pd
= ptr
;
3067 struct policydb
*p
= pd
->p
;
3075 buf
[items
++] = cpu_to_le32(len
);
3076 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3077 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3078 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3079 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
3080 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3084 rc
= put_entry(key
, 1, len
, fp
);
3088 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3092 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3096 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3103 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3116 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3119 unsigned int i
, j
, rc
;
3124 for (i
= 0; i
< info
->ocon_num
; i
++) {
3126 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3128 buf
[0] = cpu_to_le32(nel
);
3129 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3132 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3135 buf
[0] = cpu_to_le32(c
->sid
[0]);
3136 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3139 rc
= context_write(p
, &c
->context
[0], fp
);
3145 len
= strlen(c
->u
.name
);
3146 buf
[0] = cpu_to_le32(len
);
3147 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3150 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3153 rc
= context_write(p
, &c
->context
[0], fp
);
3156 rc
= context_write(p
, &c
->context
[1], fp
);
3161 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3162 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3163 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3164 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3167 rc
= context_write(p
, &c
->context
[0], fp
);
3172 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3173 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3174 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3177 rc
= context_write(p
, &c
->context
[0], fp
);
3182 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3183 len
= strlen(c
->u
.name
);
3184 buf
[1] = cpu_to_le32(len
);
3185 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3188 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3191 rc
= context_write(p
, &c
->context
[0], fp
);
3196 for (j
= 0; j
< 4; j
++)
3197 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3198 for (j
= 0; j
< 4; j
++)
3199 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3200 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3203 rc
= context_write(p
, &c
->context
[0], fp
);
3213 static int genfs_write(struct policydb
*p
, void *fp
)
3215 struct genfs
*genfs
;
3222 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3224 buf
[0] = cpu_to_le32(len
);
3225 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3228 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3229 len
= strlen(genfs
->fstype
);
3230 buf
[0] = cpu_to_le32(len
);
3231 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3234 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3238 for (c
= genfs
->head
; c
; c
= c
->next
)
3240 buf
[0] = cpu_to_le32(len
);
3241 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3244 for (c
= genfs
->head
; c
; c
= c
->next
) {
3245 len
= strlen(c
->u
.name
);
3246 buf
[0] = cpu_to_le32(len
);
3247 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3250 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3253 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3254 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3257 rc
= context_write(p
, &c
->context
[0], fp
);
3265 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3273 static int range_write_helper(void *key
, void *data
, void *ptr
)
3276 struct range_trans
*rt
= key
;
3277 struct mls_range
*r
= data
;
3278 struct policy_data
*pd
= ptr
;
3280 struct policydb
*p
= pd
->p
;
3283 buf
[0] = cpu_to_le32(rt
->source_type
);
3284 buf
[1] = cpu_to_le32(rt
->target_type
);
3285 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3288 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3289 buf
[0] = cpu_to_le32(rt
->target_class
);
3290 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3294 rc
= mls_write_range_helper(r
, fp
);
3301 static int range_write(struct policydb
*p
, void *fp
)
3306 struct policy_data pd
;
3311 /* count the number of entries in the hashtab */
3313 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3317 buf
[0] = cpu_to_le32(nel
);
3318 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3322 /* actually write all of the entries */
3323 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3330 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3333 struct filename_trans
*ft
= key
;
3334 struct filename_trans_datum
*otype
= data
;
3339 len
= strlen(ft
->name
);
3340 buf
[0] = cpu_to_le32(len
);
3341 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3345 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3349 buf
[0] = cpu_to_le32(ft
->stype
);
3350 buf
[1] = cpu_to_le32(ft
->ttype
);
3351 buf
[2] = cpu_to_le32(ft
->tclass
);
3352 buf
[3] = cpu_to_le32(otype
->otype
);
3354 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3361 static int filename_trans_write(struct policydb
*p
, void *fp
)
3367 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3371 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3375 buf
[0] = cpu_to_le32(nel
);
3376 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3380 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3388 * Write the configuration data in a policy database
3389 * structure to a policy database binary representation
3392 int policydb_write(struct policydb
*p
, void *fp
)
3394 unsigned int i
, num_syms
;
3399 struct policydb_compat_info
*info
;
3402 * refuse to write policy older than compressed avtab
3403 * to simplify the writer. There are other tests dropped
3404 * since we assume this throughout the writer code. Be
3405 * careful if you ever try to remove this restriction
3407 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3408 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3409 " Because it is less than version %d\n", p
->policyvers
,
3410 POLICYDB_VERSION_AVTAB
);
3416 config
|= POLICYDB_CONFIG_MLS
;
3418 if (p
->reject_unknown
)
3419 config
|= REJECT_UNKNOWN
;
3420 if (p
->allow_unknown
)
3421 config
|= ALLOW_UNKNOWN
;
3423 /* Write the magic number and string identifiers. */
3424 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3425 len
= strlen(POLICYDB_STRING
);
3426 buf
[1] = cpu_to_le32(len
);
3427 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3430 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3434 /* Write the version, config, and table sizes. */
3435 info
= policydb_lookup_compat(p
->policyvers
);
3437 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3438 "version %d", p
->policyvers
);
3442 buf
[0] = cpu_to_le32(p
->policyvers
);
3443 buf
[1] = cpu_to_le32(config
);
3444 buf
[2] = cpu_to_le32(info
->sym_num
);
3445 buf
[3] = cpu_to_le32(info
->ocon_num
);
3447 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3451 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3452 rc
= ebitmap_write(&p
->policycaps
, fp
);
3457 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3458 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3463 num_syms
= info
->sym_num
;
3464 for (i
= 0; i
< num_syms
; i
++) {
3465 struct policy_data pd
;
3470 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3471 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3473 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3476 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3481 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3485 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3489 rc
= role_trans_write(p
, fp
);
3493 rc
= role_allow_write(p
->role_allow
, fp
);
3497 rc
= filename_trans_write(p
, fp
);
3501 rc
= ocontext_write(p
, info
, fp
);
3505 rc
= genfs_write(p
, fp
);
3509 rc
= range_write(p
, fp
);
3513 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3514 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3517 rc
= ebitmap_write(e
, fp
);