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
= kcalloc(p
->p_classes
.nprim
,
544 sizeof(*p
->class_val_to_struct
),
546 if (!p
->class_val_to_struct
)
550 p
->role_val_to_struct
= kcalloc(p
->p_roles
.nprim
,
551 sizeof(*p
->role_val_to_struct
),
553 if (!p
->role_val_to_struct
)
557 p
->user_val_to_struct
= kcalloc(p
->p_users
.nprim
,
558 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
);
886 * Load the initial SIDs specified in a policy database
887 * structure into a SID table.
889 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
891 struct ocontext
*head
, *c
;
896 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
900 head
= p
->ocontexts
[OCON_ISID
];
901 for (c
= head
; c
; c
= c
->next
) {
903 if (!c
->context
[0].user
) {
904 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
909 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
911 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
921 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
923 if (!class || class > p
->p_classes
.nprim
)
928 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
930 if (!role
|| role
> p
->p_roles
.nprim
)
935 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
937 if (!type
|| type
> p
->p_types
.nprim
)
943 * Return 1 if the fields in the security context
944 * structure `c' are valid. Return 0 otherwise.
946 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
948 struct role_datum
*role
;
949 struct user_datum
*usrdatum
;
951 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
954 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
957 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
960 if (c
->role
!= OBJECT_R_VAL
) {
962 * Role must be authorized for the type.
964 role
= p
->role_val_to_struct
[c
->role
- 1];
965 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
966 /* role may not be associated with type */
970 * User must be authorized for the role.
972 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
976 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
977 /* user may not be associated with role */
981 if (!mls_context_isvalid(p
, c
))
988 * Read a MLS range structure from a policydb binary
989 * representation file.
991 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
997 rc
= next_entry(buf
, fp
, sizeof(u32
));
1002 items
= le32_to_cpu(buf
[0]);
1003 if (items
> ARRAY_SIZE(buf
)) {
1004 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1008 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1010 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1014 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1016 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1018 r
->level
[1].sens
= r
->level
[0].sens
;
1020 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1022 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1026 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1028 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1032 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1034 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1041 ebitmap_destroy(&r
->level
[0].cat
);
1047 * Read and validate a security context structure
1048 * from a policydb binary representation file.
1050 static int context_read_and_validate(struct context
*c
,
1057 rc
= next_entry(buf
, fp
, sizeof buf
);
1059 printk(KERN_ERR
"SELinux: context truncated\n");
1062 c
->user
= le32_to_cpu(buf
[0]);
1063 c
->role
= le32_to_cpu(buf
[1]);
1064 c
->type
= le32_to_cpu(buf
[2]);
1065 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1066 rc
= mls_read_range_helper(&c
->range
, fp
);
1068 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1074 if (!policydb_context_isvalid(p
, c
)) {
1075 printk(KERN_ERR
"SELinux: invalid security context\n");
1085 * The following *_read functions are used to
1086 * read the symbol data from a policy database
1087 * binary representation file.
1090 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1095 if ((len
== 0) || (len
== (u32
)-1))
1098 str
= kmalloc(len
+ 1, flags
);
1102 /* it's expected the caller should free the str */
1105 rc
= next_entry(str
, fp
, len
);
1113 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1116 struct perm_datum
*perdatum
;
1122 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1126 rc
= next_entry(buf
, fp
, sizeof buf
);
1130 len
= le32_to_cpu(buf
[0]);
1131 perdatum
->value
= le32_to_cpu(buf
[1]);
1133 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1137 rc
= hashtab_insert(h
, key
, perdatum
);
1143 perm_destroy(key
, perdatum
, NULL
);
1147 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1150 struct common_datum
*comdatum
;
1156 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1160 rc
= next_entry(buf
, fp
, sizeof buf
);
1164 len
= le32_to_cpu(buf
[0]);
1165 comdatum
->value
= le32_to_cpu(buf
[1]);
1167 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1170 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1171 nel
= le32_to_cpu(buf
[3]);
1173 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1177 for (i
= 0; i
< nel
; i
++) {
1178 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1183 rc
= hashtab_insert(h
, key
, comdatum
);
1188 common_destroy(key
, comdatum
, NULL
);
1192 static void type_set_init(struct type_set
*t
)
1194 ebitmap_init(&t
->types
);
1195 ebitmap_init(&t
->negset
);
1198 static int type_set_read(struct type_set
*t
, void *fp
)
1203 if (ebitmap_read(&t
->types
, fp
))
1205 if (ebitmap_read(&t
->negset
, fp
))
1208 rc
= next_entry(buf
, fp
, sizeof(u32
));
1211 t
->flags
= le32_to_cpu(buf
[0]);
1217 static int read_cons_helper(struct policydb
*p
,
1218 struct constraint_node
**nodep
,
1219 int ncons
, int allowxtarget
, void *fp
)
1221 struct constraint_node
*c
, *lc
;
1222 struct constraint_expr
*e
, *le
;
1225 int rc
, i
, j
, depth
;
1228 for (i
= 0; i
< ncons
; i
++) {
1229 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1238 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1241 c
->permissions
= le32_to_cpu(buf
[0]);
1242 nexpr
= le32_to_cpu(buf
[1]);
1245 for (j
= 0; j
< nexpr
; j
++) {
1246 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1255 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1258 e
->expr_type
= le32_to_cpu(buf
[0]);
1259 e
->attr
= le32_to_cpu(buf
[1]);
1260 e
->op
= le32_to_cpu(buf
[2]);
1262 switch (e
->expr_type
) {
1274 if (depth
== (CEXPR_MAXDEPTH
- 1))
1279 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1281 if (depth
== (CEXPR_MAXDEPTH
- 1))
1284 rc
= ebitmap_read(&e
->names
, fp
);
1287 if (p
->policyvers
>=
1288 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1289 e
->type_names
= kzalloc(sizeof
1294 type_set_init(e
->type_names
);
1295 rc
= type_set_read(e
->type_names
, fp
);
1313 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1316 struct class_datum
*cladatum
;
1318 u32 len
, len2
, ncons
, nel
;
1322 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1326 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1330 len
= le32_to_cpu(buf
[0]);
1331 len2
= le32_to_cpu(buf
[1]);
1332 cladatum
->value
= le32_to_cpu(buf
[2]);
1334 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1337 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1338 nel
= le32_to_cpu(buf
[4]);
1340 ncons
= le32_to_cpu(buf
[5]);
1342 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1347 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1352 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1353 if (!cladatum
->comdatum
) {
1354 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1358 for (i
= 0; i
< nel
; i
++) {
1359 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1364 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1368 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1369 /* grab the validatetrans rules */
1370 rc
= next_entry(buf
, fp
, sizeof(u32
));
1373 ncons
= le32_to_cpu(buf
[0]);
1374 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1380 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1381 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1385 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1386 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1387 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1390 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1391 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1394 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1397 rc
= hashtab_insert(h
, key
, cladatum
);
1403 cls_destroy(key
, cladatum
, NULL
);
1407 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1410 struct role_datum
*role
;
1411 int rc
, to_read
= 2;
1416 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1420 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1423 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1427 len
= le32_to_cpu(buf
[0]);
1428 role
->value
= le32_to_cpu(buf
[1]);
1429 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1430 role
->bounds
= le32_to_cpu(buf
[2]);
1432 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1436 rc
= ebitmap_read(&role
->dominates
, fp
);
1440 rc
= ebitmap_read(&role
->types
, fp
);
1444 if (strcmp(key
, OBJECT_R
) == 0) {
1446 if (role
->value
!= OBJECT_R_VAL
) {
1447 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1448 OBJECT_R
, role
->value
);
1455 rc
= hashtab_insert(h
, key
, role
);
1460 role_destroy(key
, role
, NULL
);
1464 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1467 struct type_datum
*typdatum
;
1468 int rc
, to_read
= 3;
1472 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1476 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1479 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1483 len
= le32_to_cpu(buf
[0]);
1484 typdatum
->value
= le32_to_cpu(buf
[1]);
1485 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1486 u32 prop
= le32_to_cpu(buf
[2]);
1488 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1489 typdatum
->primary
= 1;
1490 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1491 typdatum
->attribute
= 1;
1493 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1495 typdatum
->primary
= le32_to_cpu(buf
[2]);
1498 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1502 rc
= hashtab_insert(h
, key
, typdatum
);
1507 type_destroy(key
, typdatum
, NULL
);
1513 * Read a MLS level structure from a policydb binary
1514 * representation file.
1516 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1521 memset(lp
, 0, sizeof(*lp
));
1523 rc
= next_entry(buf
, fp
, sizeof buf
);
1525 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1528 lp
->sens
= le32_to_cpu(buf
[0]);
1530 rc
= ebitmap_read(&lp
->cat
, fp
);
1532 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1538 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1541 struct user_datum
*usrdatum
;
1542 int rc
, to_read
= 2;
1546 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1550 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1553 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1557 len
= le32_to_cpu(buf
[0]);
1558 usrdatum
->value
= le32_to_cpu(buf
[1]);
1559 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1560 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1562 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1566 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1570 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1571 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1574 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1579 rc
= hashtab_insert(h
, key
, usrdatum
);
1584 user_destroy(key
, usrdatum
, NULL
);
1588 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1591 struct level_datum
*levdatum
;
1596 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1600 rc
= next_entry(buf
, fp
, sizeof buf
);
1604 len
= le32_to_cpu(buf
[0]);
1605 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1607 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1612 levdatum
->level
= kmalloc(sizeof(*levdatum
->level
), GFP_ATOMIC
);
1613 if (!levdatum
->level
)
1616 rc
= mls_read_level(levdatum
->level
, fp
);
1620 rc
= hashtab_insert(h
, key
, levdatum
);
1625 sens_destroy(key
, levdatum
, NULL
);
1629 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1632 struct cat_datum
*catdatum
;
1637 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1641 rc
= next_entry(buf
, fp
, sizeof buf
);
1645 len
= le32_to_cpu(buf
[0]);
1646 catdatum
->value
= le32_to_cpu(buf
[1]);
1647 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1649 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1653 rc
= hashtab_insert(h
, key
, catdatum
);
1658 cat_destroy(key
, catdatum
, NULL
);
1662 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1674 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1676 struct user_datum
*upper
, *user
;
1677 struct policydb
*p
= datap
;
1680 upper
= user
= datum
;
1681 while (upper
->bounds
) {
1682 struct ebitmap_node
*node
;
1685 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1686 printk(KERN_ERR
"SELinux: user %s: "
1687 "too deep or looped boundary",
1692 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1693 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1694 if (ebitmap_get_bit(&upper
->roles
, bit
))
1698 "SELinux: boundary violated policy: "
1699 "user=%s role=%s bounds=%s\n",
1700 sym_name(p
, SYM_USERS
, user
->value
- 1),
1701 sym_name(p
, SYM_ROLES
, bit
),
1702 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1711 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1713 struct role_datum
*upper
, *role
;
1714 struct policydb
*p
= datap
;
1717 upper
= role
= datum
;
1718 while (upper
->bounds
) {
1719 struct ebitmap_node
*node
;
1722 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1723 printk(KERN_ERR
"SELinux: role %s: "
1724 "too deep or looped bounds\n",
1729 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1730 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1731 if (ebitmap_get_bit(&upper
->types
, bit
))
1735 "SELinux: boundary violated policy: "
1736 "role=%s type=%s bounds=%s\n",
1737 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1738 sym_name(p
, SYM_TYPES
, bit
),
1739 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1748 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1750 struct type_datum
*upper
;
1751 struct policydb
*p
= datap
;
1755 while (upper
->bounds
) {
1756 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1757 printk(KERN_ERR
"SELinux: type %s: "
1758 "too deep or looped boundary\n",
1763 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1767 if (upper
->attribute
) {
1768 printk(KERN_ERR
"SELinux: type %s: "
1769 "bounded by attribute %s",
1771 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1779 static int policydb_bounds_sanity_check(struct policydb
*p
)
1783 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1786 rc
= hashtab_map(p
->p_users
.table
,
1787 user_bounds_sanity_check
, p
);
1791 rc
= hashtab_map(p
->p_roles
.table
,
1792 role_bounds_sanity_check
, p
);
1796 rc
= hashtab_map(p
->p_types
.table
,
1797 type_bounds_sanity_check
, p
);
1804 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1806 struct class_datum
*cladatum
;
1808 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1812 return cladatum
->value
;
1815 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1817 struct class_datum
*cladatum
;
1818 struct perm_datum
*perdatum
= NULL
;
1819 struct common_datum
*comdatum
;
1821 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1824 cladatum
= p
->class_val_to_struct
[tclass
-1];
1825 comdatum
= cladatum
->comdatum
;
1827 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1830 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1835 return 1U << (perdatum
->value
-1);
1838 static int range_read(struct policydb
*p
, void *fp
)
1840 struct range_trans
*rt
;
1841 struct mls_range
*r
= NULL
;
1846 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1849 rc
= next_entry(buf
, fp
, sizeof(u32
));
1853 nel
= le32_to_cpu(buf
[0]);
1854 for (i
= 0; i
< nel
; i
++) {
1856 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1860 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1864 rt
->source_type
= le32_to_cpu(buf
[0]);
1865 rt
->target_type
= le32_to_cpu(buf
[1]);
1866 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1867 rc
= next_entry(buf
, fp
, sizeof(u32
));
1870 rt
->target_class
= le32_to_cpu(buf
[0]);
1872 rt
->target_class
= p
->process_class
;
1875 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1876 !policydb_type_isvalid(p
, rt
->target_type
) ||
1877 !policydb_class_isvalid(p
, rt
->target_class
))
1881 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1885 rc
= mls_read_range_helper(r
, fp
);
1890 if (!mls_range_isvalid(p
, r
)) {
1891 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1895 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1902 hash_eval(p
->range_tr
, "rangetr");
1910 static int filename_trans_read(struct policydb
*p
, void *fp
)
1912 struct filename_trans
*ft
;
1913 struct filename_trans_datum
*otype
;
1919 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1922 rc
= next_entry(buf
, fp
, sizeof(u32
));
1925 nel
= le32_to_cpu(buf
[0]);
1927 for (i
= 0; i
< nel
; i
++) {
1932 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1937 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1941 /* length of the path component string */
1942 rc
= next_entry(buf
, fp
, sizeof(u32
));
1945 len
= le32_to_cpu(buf
[0]);
1947 /* path component string */
1948 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1954 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1958 ft
->stype
= le32_to_cpu(buf
[0]);
1959 ft
->ttype
= le32_to_cpu(buf
[1]);
1960 ft
->tclass
= le32_to_cpu(buf
[2]);
1962 otype
->otype
= le32_to_cpu(buf
[3]);
1964 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1968 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1971 * Do not return -EEXIST to the caller, or the system
1976 /* But free memory to avoid memory leak. */
1982 hash_eval(p
->filename_trans
, "filenametr");
1992 static int genfs_read(struct policydb
*p
, void *fp
)
1995 u32 nel
, nel2
, len
, len2
;
1997 struct ocontext
*l
, *c
;
1998 struct ocontext
*newc
= NULL
;
1999 struct genfs
*genfs_p
, *genfs
;
2000 struct genfs
*newgenfs
= NULL
;
2002 rc
= next_entry(buf
, fp
, sizeof(u32
));
2005 nel
= le32_to_cpu(buf
[0]);
2007 for (i
= 0; i
< nel
; i
++) {
2008 rc
= next_entry(buf
, fp
, sizeof(u32
));
2011 len
= le32_to_cpu(buf
[0]);
2014 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2018 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2022 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2023 genfs_p
= genfs
, genfs
= genfs
->next
) {
2025 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2026 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2030 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2033 newgenfs
->next
= genfs
;
2035 genfs_p
->next
= newgenfs
;
2037 p
->genfs
= newgenfs
;
2041 rc
= next_entry(buf
, fp
, sizeof(u32
));
2045 nel2
= le32_to_cpu(buf
[0]);
2046 for (j
= 0; j
< nel2
; j
++) {
2047 rc
= next_entry(buf
, fp
, sizeof(u32
));
2050 len
= le32_to_cpu(buf
[0]);
2053 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2057 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2061 rc
= next_entry(buf
, fp
, sizeof(u32
));
2065 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2066 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2070 for (l
= NULL
, c
= genfs
->head
; c
;
2071 l
= c
, c
= c
->next
) {
2073 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2074 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2075 newc
->v
.sclass
== c
->v
.sclass
)) {
2076 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2077 genfs
->fstype
, c
->u
.name
);
2080 len
= strlen(newc
->u
.name
);
2081 len2
= strlen(c
->u
.name
);
2097 kfree(newgenfs
->fstype
);
2100 ocontext_destroy(newc
, OCON_FSUSE
);
2105 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2111 struct ocontext
*l
, *c
;
2114 for (i
= 0; i
< info
->ocon_num
; i
++) {
2115 rc
= next_entry(buf
, fp
, sizeof(u32
));
2118 nel
= le32_to_cpu(buf
[0]);
2121 for (j
= 0; j
< nel
; j
++) {
2123 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2129 p
->ocontexts
[i
] = c
;
2134 rc
= next_entry(buf
, fp
, sizeof(u32
));
2138 c
->sid
[0] = le32_to_cpu(buf
[0]);
2139 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2145 rc
= next_entry(buf
, fp
, sizeof(u32
));
2148 len
= le32_to_cpu(buf
[0]);
2150 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2154 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2157 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2162 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2165 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2166 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2167 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2168 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2173 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2176 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2177 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2178 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2183 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2188 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2189 /* Determined at runtime, not in policy DB. */
2190 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2192 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2195 len
= le32_to_cpu(buf
[1]);
2196 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2200 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2207 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2210 for (k
= 0; k
< 4; k
++)
2211 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2212 for (k
= 0; k
< 4; k
++)
2213 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2214 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2228 * Read the configuration data from a policy database binary
2229 * representation file into a policy database structure.
2231 int policydb_read(struct policydb
*p
, void *fp
)
2233 struct role_allow
*ra
, *lra
;
2234 struct role_trans
*tr
, *ltr
;
2237 u32 len
, nprim
, nel
;
2240 struct policydb_compat_info
*info
;
2242 rc
= policydb_init(p
);
2246 /* Read the magic number and string length. */
2247 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2252 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2253 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2254 "not match expected magic number 0x%x\n",
2255 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2260 len
= le32_to_cpu(buf
[1]);
2261 if (len
!= strlen(POLICYDB_STRING
)) {
2262 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2263 "match expected length %zu\n",
2264 len
, strlen(POLICYDB_STRING
));
2269 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2270 if (!policydb_str
) {
2271 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2272 "string of length %d\n", len
);
2276 rc
= next_entry(policydb_str
, fp
, len
);
2278 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2279 kfree(policydb_str
);
2284 policydb_str
[len
] = '\0';
2285 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2286 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2287 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2288 kfree(policydb_str
);
2291 /* Done with policydb_str. */
2292 kfree(policydb_str
);
2293 policydb_str
= NULL
;
2295 /* Read the version and table sizes. */
2296 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2301 p
->policyvers
= le32_to_cpu(buf
[0]);
2302 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2303 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2304 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2305 "my version range %d-%d\n",
2306 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2310 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2314 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2315 printk(KERN_ERR
"SELinux: security policydb version %d "
2316 "(MLS) not backwards compatible\n",
2321 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2322 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2324 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2325 rc
= ebitmap_read(&p
->policycaps
, fp
);
2330 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2331 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2337 info
= policydb_lookup_compat(p
->policyvers
);
2339 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2340 "for version %d\n", p
->policyvers
);
2345 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2346 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2347 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2348 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2349 le32_to_cpu(buf
[3]),
2350 info
->sym_num
, info
->ocon_num
);
2354 for (i
= 0; i
< info
->sym_num
; i
++) {
2355 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2358 nprim
= le32_to_cpu(buf
[0]);
2359 nel
= le32_to_cpu(buf
[1]);
2360 for (j
= 0; j
< nel
; j
++) {
2361 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2366 p
->symtab
[i
].nprim
= nprim
;
2370 p
->process_class
= string_to_security_class(p
, "process");
2371 if (!p
->process_class
)
2374 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2378 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2379 rc
= cond_read_list(p
, fp
);
2384 rc
= next_entry(buf
, fp
, sizeof(u32
));
2387 nel
= le32_to_cpu(buf
[0]);
2389 for (i
= 0; i
< nel
; i
++) {
2391 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2398 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2403 tr
->role
= le32_to_cpu(buf
[0]);
2404 tr
->type
= le32_to_cpu(buf
[1]);
2405 tr
->new_role
= le32_to_cpu(buf
[2]);
2406 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2407 rc
= next_entry(buf
, fp
, sizeof(u32
));
2410 tr
->tclass
= le32_to_cpu(buf
[0]);
2412 tr
->tclass
= p
->process_class
;
2415 if (!policydb_role_isvalid(p
, tr
->role
) ||
2416 !policydb_type_isvalid(p
, tr
->type
) ||
2417 !policydb_class_isvalid(p
, tr
->tclass
) ||
2418 !policydb_role_isvalid(p
, tr
->new_role
))
2423 rc
= next_entry(buf
, fp
, sizeof(u32
));
2426 nel
= le32_to_cpu(buf
[0]);
2428 for (i
= 0; i
< nel
; i
++) {
2430 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2437 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2442 ra
->role
= le32_to_cpu(buf
[0]);
2443 ra
->new_role
= le32_to_cpu(buf
[1]);
2444 if (!policydb_role_isvalid(p
, ra
->role
) ||
2445 !policydb_role_isvalid(p
, ra
->new_role
))
2450 rc
= filename_trans_read(p
, fp
);
2454 rc
= policydb_index(p
);
2459 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2460 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2461 if (!p
->process_trans_perms
)
2464 rc
= ocontext_read(p
, info
, fp
);
2468 rc
= genfs_read(p
, fp
);
2472 rc
= range_read(p
, fp
);
2477 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2479 GFP_KERNEL
| __GFP_ZERO
);
2480 if (!p
->type_attr_map_array
)
2483 /* preallocate so we don't have to worry about the put ever failing */
2484 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2485 GFP_KERNEL
| __GFP_ZERO
);
2489 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2490 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2494 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2495 rc
= ebitmap_read(e
, fp
);
2499 /* add the type itself as the degenerate case */
2500 rc
= ebitmap_set_bit(e
, i
, 1);
2505 rc
= policydb_bounds_sanity_check(p
);
2513 policydb_destroy(p
);
2518 * Write a MLS level structure to a policydb binary
2519 * representation file.
2521 static int mls_write_level(struct mls_level
*l
, void *fp
)
2526 buf
[0] = cpu_to_le32(l
->sens
);
2527 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2531 rc
= ebitmap_write(&l
->cat
, fp
);
2539 * Write a MLS range structure to a policydb binary
2540 * representation file.
2542 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2548 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2554 buf
[0] = cpu_to_le32(items
-1);
2555 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2557 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2559 BUG_ON(items
> ARRAY_SIZE(buf
));
2561 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2565 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2569 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2577 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2580 struct level_datum
*levdatum
= datum
;
2581 struct policy_data
*pd
= ptr
;
2588 buf
[0] = cpu_to_le32(len
);
2589 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2590 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2594 rc
= put_entry(key
, 1, len
, fp
);
2598 rc
= mls_write_level(levdatum
->level
, fp
);
2605 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2608 struct cat_datum
*catdatum
= datum
;
2609 struct policy_data
*pd
= ptr
;
2616 buf
[0] = cpu_to_le32(len
);
2617 buf
[1] = cpu_to_le32(catdatum
->value
);
2618 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2619 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2623 rc
= put_entry(key
, 1, len
, fp
);
2630 static int role_trans_write(struct policydb
*p
, void *fp
)
2632 struct role_trans
*r
= p
->role_tr
;
2633 struct role_trans
*tr
;
2639 for (tr
= r
; tr
; tr
= tr
->next
)
2641 buf
[0] = cpu_to_le32(nel
);
2642 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2645 for (tr
= r
; tr
; tr
= tr
->next
) {
2646 buf
[0] = cpu_to_le32(tr
->role
);
2647 buf
[1] = cpu_to_le32(tr
->type
);
2648 buf
[2] = cpu_to_le32(tr
->new_role
);
2649 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2652 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2653 buf
[0] = cpu_to_le32(tr
->tclass
);
2654 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2663 static int role_allow_write(struct role_allow
*r
, void *fp
)
2665 struct role_allow
*ra
;
2671 for (ra
= r
; ra
; ra
= ra
->next
)
2673 buf
[0] = cpu_to_le32(nel
);
2674 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2677 for (ra
= r
; ra
; ra
= ra
->next
) {
2678 buf
[0] = cpu_to_le32(ra
->role
);
2679 buf
[1] = cpu_to_le32(ra
->new_role
);
2680 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2688 * Write a security context structure
2689 * to a policydb binary representation file.
2691 static int context_write(struct policydb
*p
, struct context
*c
,
2697 buf
[0] = cpu_to_le32(c
->user
);
2698 buf
[1] = cpu_to_le32(c
->role
);
2699 buf
[2] = cpu_to_le32(c
->type
);
2701 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2705 rc
= mls_write_range_helper(&c
->range
, fp
);
2713 * The following *_write functions are used to
2714 * write the symbol data to a policy database
2715 * binary representation file.
2718 static int perm_write(void *vkey
, void *datum
, void *fp
)
2721 struct perm_datum
*perdatum
= datum
;
2727 buf
[0] = cpu_to_le32(len
);
2728 buf
[1] = cpu_to_le32(perdatum
->value
);
2729 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2733 rc
= put_entry(key
, 1, len
, fp
);
2740 static int common_write(void *vkey
, void *datum
, void *ptr
)
2743 struct common_datum
*comdatum
= datum
;
2744 struct policy_data
*pd
= ptr
;
2751 buf
[0] = cpu_to_le32(len
);
2752 buf
[1] = cpu_to_le32(comdatum
->value
);
2753 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2754 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2755 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2759 rc
= put_entry(key
, 1, len
, fp
);
2763 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2770 static int type_set_write(struct type_set
*t
, void *fp
)
2775 if (ebitmap_write(&t
->types
, fp
))
2777 if (ebitmap_write(&t
->negset
, fp
))
2780 buf
[0] = cpu_to_le32(t
->flags
);
2781 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2788 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2791 struct constraint_node
*c
;
2792 struct constraint_expr
*e
;
2797 for (c
= node
; c
; c
= c
->next
) {
2799 for (e
= c
->expr
; e
; e
= e
->next
)
2801 buf
[0] = cpu_to_le32(c
->permissions
);
2802 buf
[1] = cpu_to_le32(nel
);
2803 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2806 for (e
= c
->expr
; e
; e
= e
->next
) {
2807 buf
[0] = cpu_to_le32(e
->expr_type
);
2808 buf
[1] = cpu_to_le32(e
->attr
);
2809 buf
[2] = cpu_to_le32(e
->op
);
2810 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2814 switch (e
->expr_type
) {
2816 rc
= ebitmap_write(&e
->names
, fp
);
2819 if (p
->policyvers
>=
2820 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2821 rc
= type_set_write(e
->type_names
, fp
);
2835 static int class_write(void *vkey
, void *datum
, void *ptr
)
2838 struct class_datum
*cladatum
= datum
;
2839 struct policy_data
*pd
= ptr
;
2841 struct policydb
*p
= pd
->p
;
2842 struct constraint_node
*c
;
2849 if (cladatum
->comkey
)
2850 len2
= strlen(cladatum
->comkey
);
2855 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2858 buf
[0] = cpu_to_le32(len
);
2859 buf
[1] = cpu_to_le32(len2
);
2860 buf
[2] = cpu_to_le32(cladatum
->value
);
2861 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2862 if (cladatum
->permissions
.table
)
2863 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2866 buf
[5] = cpu_to_le32(ncons
);
2867 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2871 rc
= put_entry(key
, 1, len
, fp
);
2875 if (cladatum
->comkey
) {
2876 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2881 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2885 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2889 /* write out the validatetrans rule */
2891 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2894 buf
[0] = cpu_to_le32(ncons
);
2895 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2899 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2903 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2904 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2905 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2906 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2908 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2913 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2914 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2915 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2923 static int role_write(void *vkey
, void *datum
, void *ptr
)
2926 struct role_datum
*role
= datum
;
2927 struct policy_data
*pd
= ptr
;
2929 struct policydb
*p
= pd
->p
;
2936 buf
[items
++] = cpu_to_le32(len
);
2937 buf
[items
++] = cpu_to_le32(role
->value
);
2938 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2939 buf
[items
++] = cpu_to_le32(role
->bounds
);
2941 BUG_ON(items
> ARRAY_SIZE(buf
));
2943 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2947 rc
= put_entry(key
, 1, len
, fp
);
2951 rc
= ebitmap_write(&role
->dominates
, fp
);
2955 rc
= ebitmap_write(&role
->types
, fp
);
2962 static int type_write(void *vkey
, void *datum
, void *ptr
)
2965 struct type_datum
*typdatum
= datum
;
2966 struct policy_data
*pd
= ptr
;
2967 struct policydb
*p
= pd
->p
;
2975 buf
[items
++] = cpu_to_le32(len
);
2976 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2977 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2980 if (typdatum
->primary
)
2981 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2983 if (typdatum
->attribute
)
2984 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2986 buf
[items
++] = cpu_to_le32(properties
);
2987 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2989 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2991 BUG_ON(items
> ARRAY_SIZE(buf
));
2992 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2996 rc
= put_entry(key
, 1, len
, fp
);
3003 static int user_write(void *vkey
, void *datum
, void *ptr
)
3006 struct user_datum
*usrdatum
= datum
;
3007 struct policy_data
*pd
= ptr
;
3008 struct policydb
*p
= pd
->p
;
3016 buf
[items
++] = cpu_to_le32(len
);
3017 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3018 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3019 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3020 BUG_ON(items
> ARRAY_SIZE(buf
));
3021 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3025 rc
= put_entry(key
, 1, len
, fp
);
3029 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3033 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3037 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3044 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3057 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3060 unsigned int i
, j
, rc
;
3065 for (i
= 0; i
< info
->ocon_num
; i
++) {
3067 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3069 buf
[0] = cpu_to_le32(nel
);
3070 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3073 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3076 buf
[0] = cpu_to_le32(c
->sid
[0]);
3077 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3080 rc
= context_write(p
, &c
->context
[0], fp
);
3086 len
= strlen(c
->u
.name
);
3087 buf
[0] = cpu_to_le32(len
);
3088 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3091 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3094 rc
= context_write(p
, &c
->context
[0], fp
);
3097 rc
= context_write(p
, &c
->context
[1], fp
);
3102 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3103 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3104 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3105 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3108 rc
= context_write(p
, &c
->context
[0], fp
);
3113 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3114 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3115 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3118 rc
= context_write(p
, &c
->context
[0], fp
);
3123 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3124 len
= strlen(c
->u
.name
);
3125 buf
[1] = cpu_to_le32(len
);
3126 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3129 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3132 rc
= context_write(p
, &c
->context
[0], fp
);
3137 for (j
= 0; j
< 4; j
++)
3138 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3139 for (j
= 0; j
< 4; j
++)
3140 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3141 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3144 rc
= context_write(p
, &c
->context
[0], fp
);
3154 static int genfs_write(struct policydb
*p
, void *fp
)
3156 struct genfs
*genfs
;
3163 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3165 buf
[0] = cpu_to_le32(len
);
3166 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3169 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3170 len
= strlen(genfs
->fstype
);
3171 buf
[0] = cpu_to_le32(len
);
3172 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3175 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3179 for (c
= genfs
->head
; c
; c
= c
->next
)
3181 buf
[0] = cpu_to_le32(len
);
3182 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3185 for (c
= genfs
->head
; c
; c
= c
->next
) {
3186 len
= strlen(c
->u
.name
);
3187 buf
[0] = cpu_to_le32(len
);
3188 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3191 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3194 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3195 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3198 rc
= context_write(p
, &c
->context
[0], fp
);
3206 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3214 static int range_write_helper(void *key
, void *data
, void *ptr
)
3217 struct range_trans
*rt
= key
;
3218 struct mls_range
*r
= data
;
3219 struct policy_data
*pd
= ptr
;
3221 struct policydb
*p
= pd
->p
;
3224 buf
[0] = cpu_to_le32(rt
->source_type
);
3225 buf
[1] = cpu_to_le32(rt
->target_type
);
3226 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3229 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3230 buf
[0] = cpu_to_le32(rt
->target_class
);
3231 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3235 rc
= mls_write_range_helper(r
, fp
);
3242 static int range_write(struct policydb
*p
, void *fp
)
3246 struct policy_data pd
;
3251 /* count the number of entries in the hashtab */
3253 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3257 buf
[0] = cpu_to_le32(nel
);
3258 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3262 /* actually write all of the entries */
3263 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3270 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3273 struct filename_trans
*ft
= key
;
3274 struct filename_trans_datum
*otype
= data
;
3279 len
= strlen(ft
->name
);
3280 buf
[0] = cpu_to_le32(len
);
3281 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3285 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3289 buf
[0] = cpu_to_le32(ft
->stype
);
3290 buf
[1] = cpu_to_le32(ft
->ttype
);
3291 buf
[2] = cpu_to_le32(ft
->tclass
);
3292 buf
[3] = cpu_to_le32(otype
->otype
);
3294 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3301 static int filename_trans_write(struct policydb
*p
, void *fp
)
3307 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3311 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3315 buf
[0] = cpu_to_le32(nel
);
3316 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3320 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3328 * Write the configuration data in a policy database
3329 * structure to a policy database binary representation
3332 int policydb_write(struct policydb
*p
, void *fp
)
3334 unsigned int i
, num_syms
;
3339 struct policydb_compat_info
*info
;
3342 * refuse to write policy older than compressed avtab
3343 * to simplify the writer. There are other tests dropped
3344 * since we assume this throughout the writer code. Be
3345 * careful if you ever try to remove this restriction
3347 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3348 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3349 " Because it is less than version %d\n", p
->policyvers
,
3350 POLICYDB_VERSION_AVTAB
);
3356 config
|= POLICYDB_CONFIG_MLS
;
3358 if (p
->reject_unknown
)
3359 config
|= REJECT_UNKNOWN
;
3360 if (p
->allow_unknown
)
3361 config
|= ALLOW_UNKNOWN
;
3363 /* Write the magic number and string identifiers. */
3364 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3365 len
= strlen(POLICYDB_STRING
);
3366 buf
[1] = cpu_to_le32(len
);
3367 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3370 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3374 /* Write the version, config, and table sizes. */
3375 info
= policydb_lookup_compat(p
->policyvers
);
3377 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3378 "version %d", p
->policyvers
);
3382 buf
[0] = cpu_to_le32(p
->policyvers
);
3383 buf
[1] = cpu_to_le32(config
);
3384 buf
[2] = cpu_to_le32(info
->sym_num
);
3385 buf
[3] = cpu_to_le32(info
->ocon_num
);
3387 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3391 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3392 rc
= ebitmap_write(&p
->policycaps
, fp
);
3397 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3398 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3403 num_syms
= info
->sym_num
;
3404 for (i
= 0; i
< num_syms
; i
++) {
3405 struct policy_data pd
;
3410 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3411 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3413 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3416 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3421 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3425 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3429 rc
= role_trans_write(p
, fp
);
3433 rc
= role_allow_write(p
->role_allow
, fp
);
3437 rc
= filename_trans_write(p
, fp
);
3441 rc
= ocontext_write(p
, info
, fp
);
3445 rc
= genfs_write(p
, fp
);
3449 rc
= range_write(p
, fp
);
3453 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3454 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3457 rc
= ebitmap_write(e
, fp
);