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
) {
302 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
308 ebitmap_init(&p
->filename_trans_ttypes
);
309 ebitmap_init(&p
->policycaps
);
310 ebitmap_init(&p
->permissive_map
);
314 hashtab_destroy(p
->filename_trans
);
315 hashtab_destroy(p
->range_tr
);
316 for (i
= 0; i
< SYM_NUM
; i
++)
317 hashtab_destroy(p
->symtab
[i
].table
);
322 * The following *_index functions are used to
323 * define the val_to_name and val_to_struct arrays
324 * in a policy database structure. The val_to_name
325 * arrays are used when converting security context
326 * structures into string representations. The
327 * val_to_struct arrays are used when the attributes
328 * of a class, role, or user are needed.
331 static int common_index(void *key
, void *datum
, void *datap
)
334 struct common_datum
*comdatum
;
335 struct flex_array
*fa
;
339 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
342 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
343 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
344 GFP_KERNEL
| __GFP_ZERO
))
349 static int class_index(void *key
, void *datum
, void *datap
)
352 struct class_datum
*cladatum
;
353 struct flex_array
*fa
;
357 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
359 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
360 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
361 GFP_KERNEL
| __GFP_ZERO
))
363 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
367 static int role_index(void *key
, void *datum
, void *datap
)
370 struct role_datum
*role
;
371 struct flex_array
*fa
;
376 || role
->value
> p
->p_roles
.nprim
377 || role
->bounds
> p
->p_roles
.nprim
)
380 fa
= p
->sym_val_to_name
[SYM_ROLES
];
381 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
382 GFP_KERNEL
| __GFP_ZERO
))
384 p
->role_val_to_struct
[role
->value
- 1] = role
;
388 static int type_index(void *key
, void *datum
, void *datap
)
391 struct type_datum
*typdatum
;
392 struct flex_array
*fa
;
397 if (typdatum
->primary
) {
399 || typdatum
->value
> p
->p_types
.nprim
400 || typdatum
->bounds
> p
->p_types
.nprim
)
402 fa
= p
->sym_val_to_name
[SYM_TYPES
];
403 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
404 GFP_KERNEL
| __GFP_ZERO
))
407 fa
= p
->type_val_to_struct_array
;
408 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
409 GFP_KERNEL
| __GFP_ZERO
))
416 static int user_index(void *key
, void *datum
, void *datap
)
419 struct user_datum
*usrdatum
;
420 struct flex_array
*fa
;
425 || usrdatum
->value
> p
->p_users
.nprim
426 || usrdatum
->bounds
> p
->p_users
.nprim
)
429 fa
= p
->sym_val_to_name
[SYM_USERS
];
430 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
431 GFP_KERNEL
| __GFP_ZERO
))
433 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
437 static int sens_index(void *key
, void *datum
, void *datap
)
440 struct level_datum
*levdatum
;
441 struct flex_array
*fa
;
446 if (!levdatum
->isalias
) {
447 if (!levdatum
->level
->sens
||
448 levdatum
->level
->sens
> p
->p_levels
.nprim
)
450 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
451 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
452 GFP_KERNEL
| __GFP_ZERO
))
459 static int cat_index(void *key
, void *datum
, void *datap
)
462 struct cat_datum
*catdatum
;
463 struct flex_array
*fa
;
468 if (!catdatum
->isalias
) {
469 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
471 fa
= p
->sym_val_to_name
[SYM_CATS
];
472 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
473 GFP_KERNEL
| __GFP_ZERO
))
480 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
493 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
495 struct hashtab_info info
;
497 hashtab_stat(h
, &info
);
498 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
499 "longest chain length %d\n", hash_name
, h
->nel
,
500 info
.slots_used
, h
->size
, info
.max_chain_len
);
503 static void symtab_hash_eval(struct symtab
*s
)
507 for (i
= 0; i
< SYM_NUM
; i
++)
508 hash_eval(s
[i
].table
, symtab_name
[i
]);
512 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
518 * Define the other val_to_name and val_to_struct arrays
519 * in a policy database structure.
521 * Caller must clean up on failure.
523 static int policydb_index(struct policydb
*p
)
527 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
528 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
530 printk(KERN_CONT
", %d sens, %d cats", p
->p_levels
.nprim
,
532 printk(KERN_CONT
"\n");
534 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
535 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
538 avtab_hash_eval(&p
->te_avtab
, "rules");
539 symtab_hash_eval(p
->symtab
);
543 p
->class_val_to_struct
=
544 kzalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
546 if (!p
->class_val_to_struct
)
550 p
->role_val_to_struct
=
551 kzalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
553 if (!p
->role_val_to_struct
)
557 p
->user_val_to_struct
=
558 kzalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
560 if (!p
->user_val_to_struct
)
563 /* Yes, I want the sizeof the pointer, not the structure */
565 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
567 GFP_KERNEL
| __GFP_ZERO
);
568 if (!p
->type_val_to_struct_array
)
571 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
572 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
576 rc
= cond_init_bool_indexes(p
);
580 for (i
= 0; i
< SYM_NUM
; i
++) {
582 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
584 GFP_KERNEL
| __GFP_ZERO
);
585 if (!p
->sym_val_to_name
[i
])
588 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
589 0, p
->symtab
[i
].nprim
,
590 GFP_KERNEL
| __GFP_ZERO
);
594 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
604 * The following *_destroy functions are used to
605 * free any memory allocated for each kind of
606 * symbol data in the policy database.
609 static int perm_destroy(void *key
, void *datum
, void *p
)
616 static int common_destroy(void *key
, void *datum
, void *p
)
618 struct common_datum
*comdatum
;
623 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
624 hashtab_destroy(comdatum
->permissions
.table
);
630 static void constraint_expr_destroy(struct constraint_expr
*expr
)
633 ebitmap_destroy(&expr
->names
);
634 if (expr
->type_names
) {
635 ebitmap_destroy(&expr
->type_names
->types
);
636 ebitmap_destroy(&expr
->type_names
->negset
);
637 kfree(expr
->type_names
);
643 static int cls_destroy(void *key
, void *datum
, void *p
)
645 struct class_datum
*cladatum
;
646 struct constraint_node
*constraint
, *ctemp
;
647 struct constraint_expr
*e
, *etmp
;
652 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
653 hashtab_destroy(cladatum
->permissions
.table
);
654 constraint
= cladatum
->constraints
;
656 e
= constraint
->expr
;
660 constraint_expr_destroy(etmp
);
663 constraint
= constraint
->next
;
667 constraint
= cladatum
->validatetrans
;
669 e
= constraint
->expr
;
673 constraint_expr_destroy(etmp
);
676 constraint
= constraint
->next
;
679 kfree(cladatum
->comkey
);
685 static int role_destroy(void *key
, void *datum
, void *p
)
687 struct role_datum
*role
;
692 ebitmap_destroy(&role
->dominates
);
693 ebitmap_destroy(&role
->types
);
699 static int type_destroy(void *key
, void *datum
, void *p
)
706 static int user_destroy(void *key
, void *datum
, void *p
)
708 struct user_datum
*usrdatum
;
713 ebitmap_destroy(&usrdatum
->roles
);
714 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
715 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
716 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
722 static int sens_destroy(void *key
, void *datum
, void *p
)
724 struct level_datum
*levdatum
;
729 ebitmap_destroy(&levdatum
->level
->cat
);
730 kfree(levdatum
->level
);
736 static int cat_destroy(void *key
, void *datum
, void *p
)
743 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
755 static int filenametr_destroy(void *key
, void *datum
, void *p
)
757 struct filename_trans
*ft
= key
;
765 static int range_tr_destroy(void *key
, void *datum
, void *p
)
767 struct mls_range
*rt
= datum
;
769 ebitmap_destroy(&rt
->level
[0].cat
);
770 ebitmap_destroy(&rt
->level
[1].cat
);
776 static void ocontext_destroy(struct ocontext
*c
, int i
)
781 context_destroy(&c
->context
[0]);
782 context_destroy(&c
->context
[1]);
783 if (i
== OCON_ISID
|| i
== OCON_FS
||
784 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
790 * Free any memory allocated by a policy database structure.
792 void policydb_destroy(struct policydb
*p
)
794 struct ocontext
*c
, *ctmp
;
795 struct genfs
*g
, *gtmp
;
797 struct role_allow
*ra
, *lra
= NULL
;
798 struct role_trans
*tr
, *ltr
= NULL
;
800 for (i
= 0; i
< SYM_NUM
; i
++) {
802 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
803 hashtab_destroy(p
->symtab
[i
].table
);
806 for (i
= 0; i
< SYM_NUM
; i
++) {
807 if (p
->sym_val_to_name
[i
])
808 flex_array_free(p
->sym_val_to_name
[i
]);
811 kfree(p
->class_val_to_struct
);
812 kfree(p
->role_val_to_struct
);
813 kfree(p
->user_val_to_struct
);
814 if (p
->type_val_to_struct_array
)
815 flex_array_free(p
->type_val_to_struct_array
);
817 avtab_destroy(&p
->te_avtab
);
819 for (i
= 0; i
< OCON_NUM
; i
++) {
825 ocontext_destroy(ctmp
, i
);
827 p
->ocontexts
[i
] = NULL
;
838 ocontext_destroy(ctmp
, OCON_FSUSE
);
846 cond_policydb_destroy(p
);
848 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
855 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
862 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
863 hashtab_destroy(p
->filename_trans
);
865 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
866 hashtab_destroy(p
->range_tr
);
868 if (p
->type_attr_map_array
) {
869 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
872 e
= flex_array_get(p
->type_attr_map_array
, i
);
877 flex_array_free(p
->type_attr_map_array
);
880 ebitmap_destroy(&p
->filename_trans_ttypes
);
881 ebitmap_destroy(&p
->policycaps
);
882 ebitmap_destroy(&p
->permissive_map
);
888 * Load the initial SIDs specified in a policy database
889 * structure into a SID table.
891 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
893 struct ocontext
*head
, *c
;
898 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
902 head
= p
->ocontexts
[OCON_ISID
];
903 for (c
= head
; c
; c
= c
->next
) {
905 if (!c
->context
[0].user
) {
906 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
911 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
913 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
923 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
925 if (!class || class > p
->p_classes
.nprim
)
930 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
932 if (!role
|| role
> p
->p_roles
.nprim
)
937 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
939 if (!type
|| type
> p
->p_types
.nprim
)
945 * Return 1 if the fields in the security context
946 * structure `c' are valid. Return 0 otherwise.
948 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
950 struct role_datum
*role
;
951 struct user_datum
*usrdatum
;
953 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
956 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
959 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
962 if (c
->role
!= OBJECT_R_VAL
) {
964 * Role must be authorized for the type.
966 role
= p
->role_val_to_struct
[c
->role
- 1];
967 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
968 /* role may not be associated with type */
972 * User must be authorized for the role.
974 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
978 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
979 /* user may not be associated with role */
983 if (!mls_context_isvalid(p
, c
))
990 * Read a MLS range structure from a policydb binary
991 * representation file.
993 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
999 rc
= next_entry(buf
, fp
, sizeof(u32
));
1004 items
= le32_to_cpu(buf
[0]);
1005 if (items
> ARRAY_SIZE(buf
)) {
1006 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1010 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1012 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1016 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1018 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1020 r
->level
[1].sens
= r
->level
[0].sens
;
1022 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1024 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1028 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1030 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1034 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1036 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1043 ebitmap_destroy(&r
->level
[0].cat
);
1049 * Read and validate a security context structure
1050 * from a policydb binary representation file.
1052 static int context_read_and_validate(struct context
*c
,
1059 rc
= next_entry(buf
, fp
, sizeof buf
);
1061 printk(KERN_ERR
"SELinux: context truncated\n");
1064 c
->user
= le32_to_cpu(buf
[0]);
1065 c
->role
= le32_to_cpu(buf
[1]);
1066 c
->type
= le32_to_cpu(buf
[2]);
1067 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1068 rc
= mls_read_range_helper(&c
->range
, fp
);
1070 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1076 if (!policydb_context_isvalid(p
, c
)) {
1077 printk(KERN_ERR
"SELinux: invalid security context\n");
1087 * The following *_read functions are used to
1088 * read the symbol data from a policy database
1089 * binary representation file.
1092 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1097 if ((len
== 0) || (len
== (u32
)-1))
1100 str
= kmalloc(len
+ 1, flags
);
1104 /* it's expected the caller should free the str */
1107 rc
= next_entry(str
, fp
, len
);
1115 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1118 struct perm_datum
*perdatum
;
1124 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1128 rc
= next_entry(buf
, fp
, sizeof buf
);
1132 len
= le32_to_cpu(buf
[0]);
1133 perdatum
->value
= le32_to_cpu(buf
[1]);
1135 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1139 rc
= hashtab_insert(h
, key
, perdatum
);
1145 perm_destroy(key
, perdatum
, NULL
);
1149 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1152 struct common_datum
*comdatum
;
1158 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1162 rc
= next_entry(buf
, fp
, sizeof buf
);
1166 len
= le32_to_cpu(buf
[0]);
1167 comdatum
->value
= le32_to_cpu(buf
[1]);
1169 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1172 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1173 nel
= le32_to_cpu(buf
[3]);
1175 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1179 for (i
= 0; i
< nel
; i
++) {
1180 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1185 rc
= hashtab_insert(h
, key
, comdatum
);
1190 common_destroy(key
, comdatum
, NULL
);
1194 static void type_set_init(struct type_set
*t
)
1196 ebitmap_init(&t
->types
);
1197 ebitmap_init(&t
->negset
);
1200 static int type_set_read(struct type_set
*t
, void *fp
)
1205 if (ebitmap_read(&t
->types
, fp
))
1207 if (ebitmap_read(&t
->negset
, fp
))
1210 rc
= next_entry(buf
, fp
, sizeof(u32
));
1213 t
->flags
= le32_to_cpu(buf
[0]);
1219 static int read_cons_helper(struct policydb
*p
,
1220 struct constraint_node
**nodep
,
1221 int ncons
, int allowxtarget
, void *fp
)
1223 struct constraint_node
*c
, *lc
;
1224 struct constraint_expr
*e
, *le
;
1227 int rc
, i
, j
, depth
;
1230 for (i
= 0; i
< ncons
; i
++) {
1231 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1240 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1243 c
->permissions
= le32_to_cpu(buf
[0]);
1244 nexpr
= le32_to_cpu(buf
[1]);
1247 for (j
= 0; j
< nexpr
; j
++) {
1248 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1257 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1260 e
->expr_type
= le32_to_cpu(buf
[0]);
1261 e
->attr
= le32_to_cpu(buf
[1]);
1262 e
->op
= le32_to_cpu(buf
[2]);
1264 switch (e
->expr_type
) {
1276 if (depth
== (CEXPR_MAXDEPTH
- 1))
1281 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1283 if (depth
== (CEXPR_MAXDEPTH
- 1))
1286 rc
= ebitmap_read(&e
->names
, fp
);
1289 if (p
->policyvers
>=
1290 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1291 e
->type_names
= kzalloc(sizeof
1296 type_set_init(e
->type_names
);
1297 rc
= type_set_read(e
->type_names
, fp
);
1315 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1318 struct class_datum
*cladatum
;
1320 u32 len
, len2
, ncons
, nel
;
1324 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1328 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1332 len
= le32_to_cpu(buf
[0]);
1333 len2
= le32_to_cpu(buf
[1]);
1334 cladatum
->value
= le32_to_cpu(buf
[2]);
1336 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1339 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1340 nel
= le32_to_cpu(buf
[4]);
1342 ncons
= le32_to_cpu(buf
[5]);
1344 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1349 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1354 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1355 if (!cladatum
->comdatum
) {
1356 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1360 for (i
= 0; i
< nel
; i
++) {
1361 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1366 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1370 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1371 /* grab the validatetrans rules */
1372 rc
= next_entry(buf
, fp
, sizeof(u32
));
1375 ncons
= le32_to_cpu(buf
[0]);
1376 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1382 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1383 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1387 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1388 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1389 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1392 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1393 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1396 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1399 rc
= hashtab_insert(h
, key
, cladatum
);
1405 cls_destroy(key
, cladatum
, NULL
);
1409 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1412 struct role_datum
*role
;
1413 int rc
, to_read
= 2;
1418 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1422 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1425 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1429 len
= le32_to_cpu(buf
[0]);
1430 role
->value
= le32_to_cpu(buf
[1]);
1431 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1432 role
->bounds
= le32_to_cpu(buf
[2]);
1434 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1438 rc
= ebitmap_read(&role
->dominates
, fp
);
1442 rc
= ebitmap_read(&role
->types
, fp
);
1446 if (strcmp(key
, OBJECT_R
) == 0) {
1448 if (role
->value
!= OBJECT_R_VAL
) {
1449 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1450 OBJECT_R
, role
->value
);
1457 rc
= hashtab_insert(h
, key
, role
);
1462 role_destroy(key
, role
, NULL
);
1466 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1469 struct type_datum
*typdatum
;
1470 int rc
, to_read
= 3;
1475 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1479 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1482 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1486 len
= le32_to_cpu(buf
[0]);
1487 typdatum
->value
= le32_to_cpu(buf
[1]);
1488 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1489 u32 prop
= le32_to_cpu(buf
[2]);
1491 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1492 typdatum
->primary
= 1;
1493 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1494 typdatum
->attribute
= 1;
1496 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1498 typdatum
->primary
= le32_to_cpu(buf
[2]);
1501 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1505 rc
= hashtab_insert(h
, key
, typdatum
);
1510 type_destroy(key
, typdatum
, NULL
);
1516 * Read a MLS level structure from a policydb binary
1517 * representation file.
1519 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1524 memset(lp
, 0, sizeof(*lp
));
1526 rc
= next_entry(buf
, fp
, sizeof buf
);
1528 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1531 lp
->sens
= le32_to_cpu(buf
[0]);
1533 rc
= ebitmap_read(&lp
->cat
, fp
);
1535 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1541 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1544 struct user_datum
*usrdatum
;
1545 int rc
, to_read
= 2;
1550 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1554 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1557 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1561 len
= le32_to_cpu(buf
[0]);
1562 usrdatum
->value
= le32_to_cpu(buf
[1]);
1563 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1564 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1566 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1570 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1574 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1575 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1578 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1583 rc
= hashtab_insert(h
, key
, usrdatum
);
1588 user_destroy(key
, usrdatum
, NULL
);
1592 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1595 struct level_datum
*levdatum
;
1601 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1605 rc
= next_entry(buf
, fp
, sizeof buf
);
1609 len
= le32_to_cpu(buf
[0]);
1610 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1612 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1617 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1618 if (!levdatum
->level
)
1621 rc
= mls_read_level(levdatum
->level
, fp
);
1625 rc
= hashtab_insert(h
, key
, levdatum
);
1630 sens_destroy(key
, levdatum
, NULL
);
1634 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1637 struct cat_datum
*catdatum
;
1643 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1647 rc
= next_entry(buf
, fp
, sizeof buf
);
1651 len
= le32_to_cpu(buf
[0]);
1652 catdatum
->value
= le32_to_cpu(buf
[1]);
1653 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1655 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1659 rc
= hashtab_insert(h
, key
, catdatum
);
1664 cat_destroy(key
, catdatum
, NULL
);
1668 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1680 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1682 struct user_datum
*upper
, *user
;
1683 struct policydb
*p
= datap
;
1686 upper
= user
= datum
;
1687 while (upper
->bounds
) {
1688 struct ebitmap_node
*node
;
1691 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1692 printk(KERN_ERR
"SELinux: user %s: "
1693 "too deep or looped boundary",
1698 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1699 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1700 if (ebitmap_get_bit(&upper
->roles
, bit
))
1704 "SELinux: boundary violated policy: "
1705 "user=%s role=%s bounds=%s\n",
1706 sym_name(p
, SYM_USERS
, user
->value
- 1),
1707 sym_name(p
, SYM_ROLES
, bit
),
1708 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1717 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1719 struct role_datum
*upper
, *role
;
1720 struct policydb
*p
= datap
;
1723 upper
= role
= datum
;
1724 while (upper
->bounds
) {
1725 struct ebitmap_node
*node
;
1728 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1729 printk(KERN_ERR
"SELinux: role %s: "
1730 "too deep or looped bounds\n",
1735 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1736 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1737 if (ebitmap_get_bit(&upper
->types
, bit
))
1741 "SELinux: boundary violated policy: "
1742 "role=%s type=%s bounds=%s\n",
1743 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1744 sym_name(p
, SYM_TYPES
, bit
),
1745 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1754 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1756 struct type_datum
*upper
;
1757 struct policydb
*p
= datap
;
1761 while (upper
->bounds
) {
1762 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1763 printk(KERN_ERR
"SELinux: type %s: "
1764 "too deep or looped boundary\n",
1769 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1773 if (upper
->attribute
) {
1774 printk(KERN_ERR
"SELinux: type %s: "
1775 "bounded by attribute %s",
1777 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1785 static int policydb_bounds_sanity_check(struct policydb
*p
)
1789 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1792 rc
= hashtab_map(p
->p_users
.table
,
1793 user_bounds_sanity_check
, p
);
1797 rc
= hashtab_map(p
->p_roles
.table
,
1798 role_bounds_sanity_check
, p
);
1802 rc
= hashtab_map(p
->p_types
.table
,
1803 type_bounds_sanity_check
, p
);
1810 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1812 struct class_datum
*cladatum
;
1814 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1818 return cladatum
->value
;
1821 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1823 struct class_datum
*cladatum
;
1824 struct perm_datum
*perdatum
= NULL
;
1825 struct common_datum
*comdatum
;
1827 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1830 cladatum
= p
->class_val_to_struct
[tclass
-1];
1831 comdatum
= cladatum
->comdatum
;
1833 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1836 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1841 return 1U << (perdatum
->value
-1);
1844 static int range_read(struct policydb
*p
, void *fp
)
1846 struct range_trans
*rt
= NULL
;
1847 struct mls_range
*r
= NULL
;
1852 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1855 rc
= next_entry(buf
, fp
, sizeof(u32
));
1859 nel
= le32_to_cpu(buf
[0]);
1860 for (i
= 0; i
< nel
; i
++) {
1862 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1866 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1870 rt
->source_type
= le32_to_cpu(buf
[0]);
1871 rt
->target_type
= le32_to_cpu(buf
[1]);
1872 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1873 rc
= next_entry(buf
, fp
, sizeof(u32
));
1876 rt
->target_class
= le32_to_cpu(buf
[0]);
1878 rt
->target_class
= p
->process_class
;
1881 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1882 !policydb_type_isvalid(p
, rt
->target_type
) ||
1883 !policydb_class_isvalid(p
, rt
->target_class
))
1887 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1891 rc
= mls_read_range_helper(r
, fp
);
1896 if (!mls_range_isvalid(p
, r
)) {
1897 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1901 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1908 hash_eval(p
->range_tr
, "rangetr");
1916 static int filename_trans_read(struct policydb
*p
, void *fp
)
1918 struct filename_trans
*ft
;
1919 struct filename_trans_datum
*otype
;
1925 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1928 rc
= next_entry(buf
, fp
, sizeof(u32
));
1931 nel
= le32_to_cpu(buf
[0]);
1933 for (i
= 0; i
< nel
; i
++) {
1939 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1944 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1948 /* length of the path component string */
1949 rc
= next_entry(buf
, fp
, sizeof(u32
));
1952 len
= le32_to_cpu(buf
[0]);
1954 /* path component string */
1955 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1961 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1965 ft
->stype
= le32_to_cpu(buf
[0]);
1966 ft
->ttype
= le32_to_cpu(buf
[1]);
1967 ft
->tclass
= le32_to_cpu(buf
[2]);
1969 otype
->otype
= le32_to_cpu(buf
[3]);
1971 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1975 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1978 * Do not return -EEXIST to the caller, or the system
1983 /* But free memory to avoid memory leak. */
1989 hash_eval(p
->filename_trans
, "filenametr");
1999 static int genfs_read(struct policydb
*p
, void *fp
)
2002 u32 nel
, nel2
, len
, len2
;
2004 struct ocontext
*l
, *c
;
2005 struct ocontext
*newc
= NULL
;
2006 struct genfs
*genfs_p
, *genfs
;
2007 struct genfs
*newgenfs
= NULL
;
2009 rc
= next_entry(buf
, fp
, sizeof(u32
));
2012 nel
= le32_to_cpu(buf
[0]);
2014 for (i
= 0; i
< nel
; i
++) {
2015 rc
= next_entry(buf
, fp
, sizeof(u32
));
2018 len
= le32_to_cpu(buf
[0]);
2021 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2025 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2029 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2030 genfs_p
= genfs
, genfs
= genfs
->next
) {
2032 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2033 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2037 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2040 newgenfs
->next
= genfs
;
2042 genfs_p
->next
= newgenfs
;
2044 p
->genfs
= newgenfs
;
2048 rc
= next_entry(buf
, fp
, sizeof(u32
));
2052 nel2
= le32_to_cpu(buf
[0]);
2053 for (j
= 0; j
< nel2
; j
++) {
2054 rc
= next_entry(buf
, fp
, sizeof(u32
));
2057 len
= le32_to_cpu(buf
[0]);
2060 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2064 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2068 rc
= next_entry(buf
, fp
, sizeof(u32
));
2072 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2073 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2077 for (l
= NULL
, c
= genfs
->head
; c
;
2078 l
= c
, c
= c
->next
) {
2080 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2081 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2082 newc
->v
.sclass
== c
->v
.sclass
)) {
2083 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2084 genfs
->fstype
, c
->u
.name
);
2087 len
= strlen(newc
->u
.name
);
2088 len2
= strlen(c
->u
.name
);
2104 kfree(newgenfs
->fstype
);
2106 ocontext_destroy(newc
, OCON_FSUSE
);
2111 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2117 struct ocontext
*l
, *c
;
2120 for (i
= 0; i
< info
->ocon_num
; i
++) {
2121 rc
= next_entry(buf
, fp
, sizeof(u32
));
2124 nel
= le32_to_cpu(buf
[0]);
2127 for (j
= 0; j
< nel
; j
++) {
2129 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2135 p
->ocontexts
[i
] = c
;
2140 rc
= next_entry(buf
, fp
, sizeof(u32
));
2144 c
->sid
[0] = le32_to_cpu(buf
[0]);
2145 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2151 rc
= next_entry(buf
, fp
, sizeof(u32
));
2154 len
= le32_to_cpu(buf
[0]);
2156 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2160 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2163 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2168 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2171 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2172 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2173 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2174 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2179 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2182 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2183 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2184 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2189 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2194 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2195 /* Determined at runtime, not in policy DB. */
2196 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2198 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2201 len
= le32_to_cpu(buf
[1]);
2202 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2206 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2213 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2216 for (k
= 0; k
< 4; k
++)
2217 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2218 for (k
= 0; k
< 4; k
++)
2219 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2220 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2234 * Read the configuration data from a policy database binary
2235 * representation file into a policy database structure.
2237 int policydb_read(struct policydb
*p
, void *fp
)
2239 struct role_allow
*ra
, *lra
;
2240 struct role_trans
*tr
, *ltr
;
2243 u32 len
, nprim
, nel
;
2246 struct policydb_compat_info
*info
;
2248 rc
= policydb_init(p
);
2252 /* Read the magic number and string length. */
2253 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2258 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2259 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2260 "not match expected magic number 0x%x\n",
2261 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2266 len
= le32_to_cpu(buf
[1]);
2267 if (len
!= strlen(POLICYDB_STRING
)) {
2268 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2269 "match expected length %zu\n",
2270 len
, strlen(POLICYDB_STRING
));
2275 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2276 if (!policydb_str
) {
2277 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2278 "string of length %d\n", len
);
2282 rc
= next_entry(policydb_str
, fp
, len
);
2284 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2285 kfree(policydb_str
);
2290 policydb_str
[len
] = '\0';
2291 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2292 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2293 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2294 kfree(policydb_str
);
2297 /* Done with policydb_str. */
2298 kfree(policydb_str
);
2299 policydb_str
= NULL
;
2301 /* Read the version and table sizes. */
2302 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2307 p
->policyvers
= le32_to_cpu(buf
[0]);
2308 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2309 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2310 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2311 "my version range %d-%d\n",
2312 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2316 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2320 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2321 printk(KERN_ERR
"SELinux: security policydb version %d "
2322 "(MLS) not backwards compatible\n",
2327 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2328 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2330 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2331 rc
= ebitmap_read(&p
->policycaps
, fp
);
2336 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2337 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2343 info
= policydb_lookup_compat(p
->policyvers
);
2345 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2346 "for version %d\n", p
->policyvers
);
2351 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2352 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2353 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2354 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2355 le32_to_cpu(buf
[3]),
2356 info
->sym_num
, info
->ocon_num
);
2360 for (i
= 0; i
< info
->sym_num
; i
++) {
2361 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2364 nprim
= le32_to_cpu(buf
[0]);
2365 nel
= le32_to_cpu(buf
[1]);
2366 for (j
= 0; j
< nel
; j
++) {
2367 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2372 p
->symtab
[i
].nprim
= nprim
;
2376 p
->process_class
= string_to_security_class(p
, "process");
2377 if (!p
->process_class
)
2380 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2384 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2385 rc
= cond_read_list(p
, fp
);
2390 rc
= next_entry(buf
, fp
, sizeof(u32
));
2393 nel
= le32_to_cpu(buf
[0]);
2395 for (i
= 0; i
< nel
; i
++) {
2397 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2404 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2409 tr
->role
= le32_to_cpu(buf
[0]);
2410 tr
->type
= le32_to_cpu(buf
[1]);
2411 tr
->new_role
= le32_to_cpu(buf
[2]);
2412 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2413 rc
= next_entry(buf
, fp
, sizeof(u32
));
2416 tr
->tclass
= le32_to_cpu(buf
[0]);
2418 tr
->tclass
= p
->process_class
;
2421 if (!policydb_role_isvalid(p
, tr
->role
) ||
2422 !policydb_type_isvalid(p
, tr
->type
) ||
2423 !policydb_class_isvalid(p
, tr
->tclass
) ||
2424 !policydb_role_isvalid(p
, tr
->new_role
))
2429 rc
= next_entry(buf
, fp
, sizeof(u32
));
2432 nel
= le32_to_cpu(buf
[0]);
2434 for (i
= 0; i
< nel
; i
++) {
2436 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2443 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2448 ra
->role
= le32_to_cpu(buf
[0]);
2449 ra
->new_role
= le32_to_cpu(buf
[1]);
2450 if (!policydb_role_isvalid(p
, ra
->role
) ||
2451 !policydb_role_isvalid(p
, ra
->new_role
))
2456 rc
= filename_trans_read(p
, fp
);
2460 rc
= policydb_index(p
);
2465 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2466 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2467 if (!p
->process_trans_perms
)
2470 rc
= ocontext_read(p
, info
, fp
);
2474 rc
= genfs_read(p
, fp
);
2478 rc
= range_read(p
, fp
);
2483 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2485 GFP_KERNEL
| __GFP_ZERO
);
2486 if (!p
->type_attr_map_array
)
2489 /* preallocate so we don't have to worry about the put ever failing */
2490 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2491 GFP_KERNEL
| __GFP_ZERO
);
2495 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2496 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2500 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2501 rc
= ebitmap_read(e
, fp
);
2505 /* add the type itself as the degenerate case */
2506 rc
= ebitmap_set_bit(e
, i
, 1);
2511 rc
= policydb_bounds_sanity_check(p
);
2519 policydb_destroy(p
);
2524 * Write a MLS level structure to a policydb binary
2525 * representation file.
2527 static int mls_write_level(struct mls_level
*l
, void *fp
)
2532 buf
[0] = cpu_to_le32(l
->sens
);
2533 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2537 rc
= ebitmap_write(&l
->cat
, fp
);
2545 * Write a MLS range structure to a policydb binary
2546 * representation file.
2548 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2554 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2560 buf
[0] = cpu_to_le32(items
-1);
2561 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2563 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2565 BUG_ON(items
> ARRAY_SIZE(buf
));
2567 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2571 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2575 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2583 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2586 struct level_datum
*levdatum
= datum
;
2587 struct policy_data
*pd
= ptr
;
2594 buf
[0] = cpu_to_le32(len
);
2595 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2596 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2600 rc
= put_entry(key
, 1, len
, fp
);
2604 rc
= mls_write_level(levdatum
->level
, fp
);
2611 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2614 struct cat_datum
*catdatum
= datum
;
2615 struct policy_data
*pd
= ptr
;
2622 buf
[0] = cpu_to_le32(len
);
2623 buf
[1] = cpu_to_le32(catdatum
->value
);
2624 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2625 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2629 rc
= put_entry(key
, 1, len
, fp
);
2636 static int role_trans_write(struct policydb
*p
, void *fp
)
2638 struct role_trans
*r
= p
->role_tr
;
2639 struct role_trans
*tr
;
2645 for (tr
= r
; tr
; tr
= tr
->next
)
2647 buf
[0] = cpu_to_le32(nel
);
2648 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2651 for (tr
= r
; tr
; tr
= tr
->next
) {
2652 buf
[0] = cpu_to_le32(tr
->role
);
2653 buf
[1] = cpu_to_le32(tr
->type
);
2654 buf
[2] = cpu_to_le32(tr
->new_role
);
2655 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2658 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2659 buf
[0] = cpu_to_le32(tr
->tclass
);
2660 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2669 static int role_allow_write(struct role_allow
*r
, void *fp
)
2671 struct role_allow
*ra
;
2677 for (ra
= r
; ra
; ra
= ra
->next
)
2679 buf
[0] = cpu_to_le32(nel
);
2680 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2683 for (ra
= r
; ra
; ra
= ra
->next
) {
2684 buf
[0] = cpu_to_le32(ra
->role
);
2685 buf
[1] = cpu_to_le32(ra
->new_role
);
2686 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2694 * Write a security context structure
2695 * to a policydb binary representation file.
2697 static int context_write(struct policydb
*p
, struct context
*c
,
2703 buf
[0] = cpu_to_le32(c
->user
);
2704 buf
[1] = cpu_to_le32(c
->role
);
2705 buf
[2] = cpu_to_le32(c
->type
);
2707 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2711 rc
= mls_write_range_helper(&c
->range
, fp
);
2719 * The following *_write functions are used to
2720 * write the symbol data to a policy database
2721 * binary representation file.
2724 static int perm_write(void *vkey
, void *datum
, void *fp
)
2727 struct perm_datum
*perdatum
= datum
;
2733 buf
[0] = cpu_to_le32(len
);
2734 buf
[1] = cpu_to_le32(perdatum
->value
);
2735 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2739 rc
= put_entry(key
, 1, len
, fp
);
2746 static int common_write(void *vkey
, void *datum
, void *ptr
)
2749 struct common_datum
*comdatum
= datum
;
2750 struct policy_data
*pd
= ptr
;
2757 buf
[0] = cpu_to_le32(len
);
2758 buf
[1] = cpu_to_le32(comdatum
->value
);
2759 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2760 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2761 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2765 rc
= put_entry(key
, 1, len
, fp
);
2769 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2776 static int type_set_write(struct type_set
*t
, void *fp
)
2781 if (ebitmap_write(&t
->types
, fp
))
2783 if (ebitmap_write(&t
->negset
, fp
))
2786 buf
[0] = cpu_to_le32(t
->flags
);
2787 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2794 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2797 struct constraint_node
*c
;
2798 struct constraint_expr
*e
;
2803 for (c
= node
; c
; c
= c
->next
) {
2805 for (e
= c
->expr
; e
; e
= e
->next
)
2807 buf
[0] = cpu_to_le32(c
->permissions
);
2808 buf
[1] = cpu_to_le32(nel
);
2809 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2812 for (e
= c
->expr
; e
; e
= e
->next
) {
2813 buf
[0] = cpu_to_le32(e
->expr_type
);
2814 buf
[1] = cpu_to_le32(e
->attr
);
2815 buf
[2] = cpu_to_le32(e
->op
);
2816 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2820 switch (e
->expr_type
) {
2822 rc
= ebitmap_write(&e
->names
, fp
);
2825 if (p
->policyvers
>=
2826 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2827 rc
= type_set_write(e
->type_names
, fp
);
2841 static int class_write(void *vkey
, void *datum
, void *ptr
)
2844 struct class_datum
*cladatum
= datum
;
2845 struct policy_data
*pd
= ptr
;
2847 struct policydb
*p
= pd
->p
;
2848 struct constraint_node
*c
;
2855 if (cladatum
->comkey
)
2856 len2
= strlen(cladatum
->comkey
);
2861 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2864 buf
[0] = cpu_to_le32(len
);
2865 buf
[1] = cpu_to_le32(len2
);
2866 buf
[2] = cpu_to_le32(cladatum
->value
);
2867 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2868 if (cladatum
->permissions
.table
)
2869 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2872 buf
[5] = cpu_to_le32(ncons
);
2873 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2877 rc
= put_entry(key
, 1, len
, fp
);
2881 if (cladatum
->comkey
) {
2882 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2887 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2891 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2895 /* write out the validatetrans rule */
2897 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2900 buf
[0] = cpu_to_le32(ncons
);
2901 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2905 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2909 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2910 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2911 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2912 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2914 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2919 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2920 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2921 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2929 static int role_write(void *vkey
, void *datum
, void *ptr
)
2932 struct role_datum
*role
= datum
;
2933 struct policy_data
*pd
= ptr
;
2935 struct policydb
*p
= pd
->p
;
2942 buf
[items
++] = cpu_to_le32(len
);
2943 buf
[items
++] = cpu_to_le32(role
->value
);
2944 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2945 buf
[items
++] = cpu_to_le32(role
->bounds
);
2947 BUG_ON(items
> ARRAY_SIZE(buf
));
2949 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2953 rc
= put_entry(key
, 1, len
, fp
);
2957 rc
= ebitmap_write(&role
->dominates
, fp
);
2961 rc
= ebitmap_write(&role
->types
, fp
);
2968 static int type_write(void *vkey
, void *datum
, void *ptr
)
2971 struct type_datum
*typdatum
= datum
;
2972 struct policy_data
*pd
= ptr
;
2973 struct policydb
*p
= pd
->p
;
2981 buf
[items
++] = cpu_to_le32(len
);
2982 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2983 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2986 if (typdatum
->primary
)
2987 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2989 if (typdatum
->attribute
)
2990 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2992 buf
[items
++] = cpu_to_le32(properties
);
2993 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2995 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2997 BUG_ON(items
> ARRAY_SIZE(buf
));
2998 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3002 rc
= put_entry(key
, 1, len
, fp
);
3009 static int user_write(void *vkey
, void *datum
, void *ptr
)
3012 struct user_datum
*usrdatum
= datum
;
3013 struct policy_data
*pd
= ptr
;
3014 struct policydb
*p
= pd
->p
;
3022 buf
[items
++] = cpu_to_le32(len
);
3023 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3024 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3025 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3026 BUG_ON(items
> ARRAY_SIZE(buf
));
3027 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3031 rc
= put_entry(key
, 1, len
, fp
);
3035 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3039 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3043 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3050 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3063 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3066 unsigned int i
, j
, rc
;
3071 for (i
= 0; i
< info
->ocon_num
; i
++) {
3073 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3075 buf
[0] = cpu_to_le32(nel
);
3076 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3079 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3082 buf
[0] = cpu_to_le32(c
->sid
[0]);
3083 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3086 rc
= context_write(p
, &c
->context
[0], fp
);
3092 len
= strlen(c
->u
.name
);
3093 buf
[0] = cpu_to_le32(len
);
3094 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3097 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3100 rc
= context_write(p
, &c
->context
[0], fp
);
3103 rc
= context_write(p
, &c
->context
[1], fp
);
3108 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3109 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3110 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3111 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3114 rc
= context_write(p
, &c
->context
[0], fp
);
3119 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3120 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3121 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3124 rc
= context_write(p
, &c
->context
[0], fp
);
3129 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3130 len
= strlen(c
->u
.name
);
3131 buf
[1] = cpu_to_le32(len
);
3132 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3135 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3138 rc
= context_write(p
, &c
->context
[0], fp
);
3143 for (j
= 0; j
< 4; j
++)
3144 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3145 for (j
= 0; j
< 4; j
++)
3146 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3147 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3150 rc
= context_write(p
, &c
->context
[0], fp
);
3160 static int genfs_write(struct policydb
*p
, void *fp
)
3162 struct genfs
*genfs
;
3169 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3171 buf
[0] = cpu_to_le32(len
);
3172 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3175 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3176 len
= strlen(genfs
->fstype
);
3177 buf
[0] = cpu_to_le32(len
);
3178 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3181 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3185 for (c
= genfs
->head
; c
; c
= c
->next
)
3187 buf
[0] = cpu_to_le32(len
);
3188 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3191 for (c
= genfs
->head
; c
; c
= c
->next
) {
3192 len
= strlen(c
->u
.name
);
3193 buf
[0] = cpu_to_le32(len
);
3194 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3197 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3200 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3201 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3204 rc
= context_write(p
, &c
->context
[0], fp
);
3212 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3220 static int range_write_helper(void *key
, void *data
, void *ptr
)
3223 struct range_trans
*rt
= key
;
3224 struct mls_range
*r
= data
;
3225 struct policy_data
*pd
= ptr
;
3227 struct policydb
*p
= pd
->p
;
3230 buf
[0] = cpu_to_le32(rt
->source_type
);
3231 buf
[1] = cpu_to_le32(rt
->target_type
);
3232 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3235 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3236 buf
[0] = cpu_to_le32(rt
->target_class
);
3237 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3241 rc
= mls_write_range_helper(r
, fp
);
3248 static int range_write(struct policydb
*p
, void *fp
)
3252 struct policy_data pd
;
3257 /* count the number of entries in the hashtab */
3259 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3263 buf
[0] = cpu_to_le32(nel
);
3264 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3268 /* actually write all of the entries */
3269 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3276 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3279 struct filename_trans
*ft
= key
;
3280 struct filename_trans_datum
*otype
= data
;
3285 len
= strlen(ft
->name
);
3286 buf
[0] = cpu_to_le32(len
);
3287 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3291 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3295 buf
[0] = cpu_to_le32(ft
->stype
);
3296 buf
[1] = cpu_to_le32(ft
->ttype
);
3297 buf
[2] = cpu_to_le32(ft
->tclass
);
3298 buf
[3] = cpu_to_le32(otype
->otype
);
3300 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3307 static int filename_trans_write(struct policydb
*p
, void *fp
)
3313 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3317 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3321 buf
[0] = cpu_to_le32(nel
);
3322 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3326 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3334 * Write the configuration data in a policy database
3335 * structure to a policy database binary representation
3338 int policydb_write(struct policydb
*p
, void *fp
)
3340 unsigned int i
, num_syms
;
3345 struct policydb_compat_info
*info
;
3348 * refuse to write policy older than compressed avtab
3349 * to simplify the writer. There are other tests dropped
3350 * since we assume this throughout the writer code. Be
3351 * careful if you ever try to remove this restriction
3353 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3354 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3355 " Because it is less than version %d\n", p
->policyvers
,
3356 POLICYDB_VERSION_AVTAB
);
3362 config
|= POLICYDB_CONFIG_MLS
;
3364 if (p
->reject_unknown
)
3365 config
|= REJECT_UNKNOWN
;
3366 if (p
->allow_unknown
)
3367 config
|= ALLOW_UNKNOWN
;
3369 /* Write the magic number and string identifiers. */
3370 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3371 len
= strlen(POLICYDB_STRING
);
3372 buf
[1] = cpu_to_le32(len
);
3373 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3376 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3380 /* Write the version, config, and table sizes. */
3381 info
= policydb_lookup_compat(p
->policyvers
);
3383 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3384 "version %d", p
->policyvers
);
3388 buf
[0] = cpu_to_le32(p
->policyvers
);
3389 buf
[1] = cpu_to_le32(config
);
3390 buf
[2] = cpu_to_le32(info
->sym_num
);
3391 buf
[3] = cpu_to_le32(info
->ocon_num
);
3393 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3397 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3398 rc
= ebitmap_write(&p
->policycaps
, fp
);
3403 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3404 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3409 num_syms
= info
->sym_num
;
3410 for (i
= 0; i
< num_syms
; i
++) {
3411 struct policy_data pd
;
3416 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3417 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3419 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3422 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3427 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3431 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3435 rc
= role_trans_write(p
, fp
);
3439 rc
= role_allow_write(p
->role_allow
, fp
);
3443 rc
= filename_trans_write(p
, fp
);
3447 rc
= ocontext_write(p
, info
, fp
);
3451 rc
= genfs_write(p
, fp
);
3455 rc
= range_write(p
, fp
);
3459 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3460 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3463 rc
= ebitmap_write(e
, fp
);