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
;
1321 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1325 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1329 len
= le32_to_cpu(buf
[0]);
1330 len2
= le32_to_cpu(buf
[1]);
1331 cladatum
->value
= le32_to_cpu(buf
[2]);
1333 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1336 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1337 nel
= le32_to_cpu(buf
[4]);
1339 ncons
= le32_to_cpu(buf
[5]);
1341 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1346 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1351 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1352 if (!cladatum
->comdatum
) {
1353 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1357 for (i
= 0; i
< nel
; i
++) {
1358 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1363 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1367 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1368 /* grab the validatetrans rules */
1369 rc
= next_entry(buf
, fp
, sizeof(u32
));
1372 ncons
= le32_to_cpu(buf
[0]);
1373 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1379 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1380 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1384 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1385 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1386 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1389 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1390 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1393 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1396 rc
= hashtab_insert(h
, key
, cladatum
);
1402 cls_destroy(key
, cladatum
, NULL
);
1406 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1409 struct role_datum
*role
;
1410 int rc
, to_read
= 2;
1414 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1418 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1421 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1425 len
= le32_to_cpu(buf
[0]);
1426 role
->value
= le32_to_cpu(buf
[1]);
1427 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1428 role
->bounds
= le32_to_cpu(buf
[2]);
1430 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1434 rc
= ebitmap_read(&role
->dominates
, fp
);
1438 rc
= ebitmap_read(&role
->types
, fp
);
1442 if (strcmp(key
, OBJECT_R
) == 0) {
1444 if (role
->value
!= OBJECT_R_VAL
) {
1445 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1446 OBJECT_R
, role
->value
);
1453 rc
= hashtab_insert(h
, key
, role
);
1458 role_destroy(key
, role
, NULL
);
1462 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1465 struct type_datum
*typdatum
;
1466 int rc
, to_read
= 3;
1470 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1474 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1477 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1481 len
= le32_to_cpu(buf
[0]);
1482 typdatum
->value
= le32_to_cpu(buf
[1]);
1483 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1484 u32 prop
= le32_to_cpu(buf
[2]);
1486 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1487 typdatum
->primary
= 1;
1488 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1489 typdatum
->attribute
= 1;
1491 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1493 typdatum
->primary
= le32_to_cpu(buf
[2]);
1496 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1500 rc
= hashtab_insert(h
, key
, typdatum
);
1505 type_destroy(key
, typdatum
, NULL
);
1511 * Read a MLS level structure from a policydb binary
1512 * representation file.
1514 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1519 memset(lp
, 0, sizeof(*lp
));
1521 rc
= next_entry(buf
, fp
, sizeof buf
);
1523 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1526 lp
->sens
= le32_to_cpu(buf
[0]);
1528 rc
= ebitmap_read(&lp
->cat
, fp
);
1530 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1536 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1539 struct user_datum
*usrdatum
;
1540 int rc
, to_read
= 2;
1544 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1548 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1551 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1555 len
= le32_to_cpu(buf
[0]);
1556 usrdatum
->value
= le32_to_cpu(buf
[1]);
1557 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1558 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1560 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1564 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1568 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1569 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1572 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1577 rc
= hashtab_insert(h
, key
, usrdatum
);
1582 user_destroy(key
, usrdatum
, NULL
);
1586 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1589 struct level_datum
*levdatum
;
1594 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1598 rc
= next_entry(buf
, fp
, sizeof buf
);
1602 len
= le32_to_cpu(buf
[0]);
1603 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1605 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1610 levdatum
->level
= kmalloc(sizeof(*levdatum
->level
), GFP_ATOMIC
);
1611 if (!levdatum
->level
)
1614 rc
= mls_read_level(levdatum
->level
, fp
);
1618 rc
= hashtab_insert(h
, key
, levdatum
);
1623 sens_destroy(key
, levdatum
, NULL
);
1627 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1630 struct cat_datum
*catdatum
;
1635 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1639 rc
= next_entry(buf
, fp
, sizeof buf
);
1643 len
= le32_to_cpu(buf
[0]);
1644 catdatum
->value
= le32_to_cpu(buf
[1]);
1645 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1647 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1651 rc
= hashtab_insert(h
, key
, catdatum
);
1656 cat_destroy(key
, catdatum
, NULL
);
1660 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1672 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1674 struct user_datum
*upper
, *user
;
1675 struct policydb
*p
= datap
;
1678 upper
= user
= datum
;
1679 while (upper
->bounds
) {
1680 struct ebitmap_node
*node
;
1683 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1684 printk(KERN_ERR
"SELinux: user %s: "
1685 "too deep or looped boundary",
1690 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1691 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1692 if (ebitmap_get_bit(&upper
->roles
, bit
))
1696 "SELinux: boundary violated policy: "
1697 "user=%s role=%s bounds=%s\n",
1698 sym_name(p
, SYM_USERS
, user
->value
- 1),
1699 sym_name(p
, SYM_ROLES
, bit
),
1700 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1709 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1711 struct role_datum
*upper
, *role
;
1712 struct policydb
*p
= datap
;
1715 upper
= role
= datum
;
1716 while (upper
->bounds
) {
1717 struct ebitmap_node
*node
;
1720 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1721 printk(KERN_ERR
"SELinux: role %s: "
1722 "too deep or looped bounds\n",
1727 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1728 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1729 if (ebitmap_get_bit(&upper
->types
, bit
))
1733 "SELinux: boundary violated policy: "
1734 "role=%s type=%s bounds=%s\n",
1735 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1736 sym_name(p
, SYM_TYPES
, bit
),
1737 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1746 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1748 struct type_datum
*upper
;
1749 struct policydb
*p
= datap
;
1753 while (upper
->bounds
) {
1754 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1755 printk(KERN_ERR
"SELinux: type %s: "
1756 "too deep or looped boundary\n",
1761 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1765 if (upper
->attribute
) {
1766 printk(KERN_ERR
"SELinux: type %s: "
1767 "bounded by attribute %s",
1769 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1777 static int policydb_bounds_sanity_check(struct policydb
*p
)
1781 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1784 rc
= hashtab_map(p
->p_users
.table
,
1785 user_bounds_sanity_check
, p
);
1789 rc
= hashtab_map(p
->p_roles
.table
,
1790 role_bounds_sanity_check
, p
);
1794 rc
= hashtab_map(p
->p_types
.table
,
1795 type_bounds_sanity_check
, p
);
1802 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1804 struct class_datum
*cladatum
;
1806 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1810 return cladatum
->value
;
1813 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1815 struct class_datum
*cladatum
;
1816 struct perm_datum
*perdatum
= NULL
;
1817 struct common_datum
*comdatum
;
1819 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1822 cladatum
= p
->class_val_to_struct
[tclass
-1];
1823 comdatum
= cladatum
->comdatum
;
1825 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1828 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1833 return 1U << (perdatum
->value
-1);
1836 static int range_read(struct policydb
*p
, void *fp
)
1838 struct range_trans
*rt
;
1839 struct mls_range
*r
= NULL
;
1844 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1847 rc
= next_entry(buf
, fp
, sizeof(u32
));
1851 nel
= le32_to_cpu(buf
[0]);
1852 for (i
= 0; i
< nel
; i
++) {
1854 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1858 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1862 rt
->source_type
= le32_to_cpu(buf
[0]);
1863 rt
->target_type
= le32_to_cpu(buf
[1]);
1864 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1865 rc
= next_entry(buf
, fp
, sizeof(u32
));
1868 rt
->target_class
= le32_to_cpu(buf
[0]);
1870 rt
->target_class
= p
->process_class
;
1873 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1874 !policydb_type_isvalid(p
, rt
->target_type
) ||
1875 !policydb_class_isvalid(p
, rt
->target_class
))
1879 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1883 rc
= mls_read_range_helper(r
, fp
);
1888 if (!mls_range_isvalid(p
, r
)) {
1889 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1893 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1900 hash_eval(p
->range_tr
, "rangetr");
1908 static int filename_trans_read(struct policydb
*p
, void *fp
)
1910 struct filename_trans
*ft
;
1911 struct filename_trans_datum
*otype
;
1917 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1920 rc
= next_entry(buf
, fp
, sizeof(u32
));
1923 nel
= le32_to_cpu(buf
[0]);
1925 for (i
= 0; i
< nel
; i
++) {
1930 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1935 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1939 /* length of the path component string */
1940 rc
= next_entry(buf
, fp
, sizeof(u32
));
1943 len
= le32_to_cpu(buf
[0]);
1945 /* path component string */
1946 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1952 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1956 ft
->stype
= le32_to_cpu(buf
[0]);
1957 ft
->ttype
= le32_to_cpu(buf
[1]);
1958 ft
->tclass
= le32_to_cpu(buf
[2]);
1960 otype
->otype
= le32_to_cpu(buf
[3]);
1962 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1966 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1969 * Do not return -EEXIST to the caller, or the system
1974 /* But free memory to avoid memory leak. */
1980 hash_eval(p
->filename_trans
, "filenametr");
1990 static int genfs_read(struct policydb
*p
, void *fp
)
1993 u32 nel
, nel2
, len
, len2
;
1995 struct ocontext
*l
, *c
;
1996 struct ocontext
*newc
= NULL
;
1997 struct genfs
*genfs_p
, *genfs
;
1998 struct genfs
*newgenfs
= NULL
;
2000 rc
= next_entry(buf
, fp
, sizeof(u32
));
2003 nel
= le32_to_cpu(buf
[0]);
2005 for (i
= 0; i
< nel
; i
++) {
2006 rc
= next_entry(buf
, fp
, sizeof(u32
));
2009 len
= le32_to_cpu(buf
[0]);
2012 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2016 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2020 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2021 genfs_p
= genfs
, genfs
= genfs
->next
) {
2023 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2024 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2028 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2031 newgenfs
->next
= genfs
;
2033 genfs_p
->next
= newgenfs
;
2035 p
->genfs
= newgenfs
;
2039 rc
= next_entry(buf
, fp
, sizeof(u32
));
2043 nel2
= le32_to_cpu(buf
[0]);
2044 for (j
= 0; j
< nel2
; j
++) {
2045 rc
= next_entry(buf
, fp
, sizeof(u32
));
2048 len
= le32_to_cpu(buf
[0]);
2051 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2055 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2059 rc
= next_entry(buf
, fp
, sizeof(u32
));
2063 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2064 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2068 for (l
= NULL
, c
= genfs
->head
; c
;
2069 l
= c
, c
= c
->next
) {
2071 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2072 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2073 newc
->v
.sclass
== c
->v
.sclass
)) {
2074 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2075 genfs
->fstype
, c
->u
.name
);
2078 len
= strlen(newc
->u
.name
);
2079 len2
= strlen(c
->u
.name
);
2095 kfree(newgenfs
->fstype
);
2098 ocontext_destroy(newc
, OCON_FSUSE
);
2103 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2109 struct ocontext
*l
, *c
;
2112 for (i
= 0; i
< info
->ocon_num
; i
++) {
2113 rc
= next_entry(buf
, fp
, sizeof(u32
));
2116 nel
= le32_to_cpu(buf
[0]);
2119 for (j
= 0; j
< nel
; j
++) {
2121 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2127 p
->ocontexts
[i
] = c
;
2132 rc
= next_entry(buf
, fp
, sizeof(u32
));
2136 c
->sid
[0] = le32_to_cpu(buf
[0]);
2137 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2143 rc
= next_entry(buf
, fp
, sizeof(u32
));
2146 len
= le32_to_cpu(buf
[0]);
2148 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2152 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2155 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2160 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2163 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2164 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2165 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2166 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2171 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2174 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2175 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2176 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2181 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2186 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2187 /* Determined at runtime, not in policy DB. */
2188 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2190 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2193 len
= le32_to_cpu(buf
[1]);
2194 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2198 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2205 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2208 for (k
= 0; k
< 4; k
++)
2209 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2210 for (k
= 0; k
< 4; k
++)
2211 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2212 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2226 * Read the configuration data from a policy database binary
2227 * representation file into a policy database structure.
2229 int policydb_read(struct policydb
*p
, void *fp
)
2231 struct role_allow
*ra
, *lra
;
2232 struct role_trans
*tr
, *ltr
;
2235 u32 len
, nprim
, nel
;
2238 struct policydb_compat_info
*info
;
2240 rc
= policydb_init(p
);
2244 /* Read the magic number and string length. */
2245 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2250 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2251 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2252 "not match expected magic number 0x%x\n",
2253 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2258 len
= le32_to_cpu(buf
[1]);
2259 if (len
!= strlen(POLICYDB_STRING
)) {
2260 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2261 "match expected length %zu\n",
2262 len
, strlen(POLICYDB_STRING
));
2267 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2268 if (!policydb_str
) {
2269 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2270 "string of length %d\n", len
);
2274 rc
= next_entry(policydb_str
, fp
, len
);
2276 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2277 kfree(policydb_str
);
2282 policydb_str
[len
] = '\0';
2283 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2284 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2285 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2286 kfree(policydb_str
);
2289 /* Done with policydb_str. */
2290 kfree(policydb_str
);
2291 policydb_str
= NULL
;
2293 /* Read the version and table sizes. */
2294 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2299 p
->policyvers
= le32_to_cpu(buf
[0]);
2300 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2301 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2302 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2303 "my version range %d-%d\n",
2304 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2308 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2312 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2313 printk(KERN_ERR
"SELinux: security policydb version %d "
2314 "(MLS) not backwards compatible\n",
2319 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2320 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2322 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2323 rc
= ebitmap_read(&p
->policycaps
, fp
);
2328 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2329 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2335 info
= policydb_lookup_compat(p
->policyvers
);
2337 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2338 "for version %d\n", p
->policyvers
);
2343 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2344 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2345 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2346 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2347 le32_to_cpu(buf
[3]),
2348 info
->sym_num
, info
->ocon_num
);
2352 for (i
= 0; i
< info
->sym_num
; i
++) {
2353 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2356 nprim
= le32_to_cpu(buf
[0]);
2357 nel
= le32_to_cpu(buf
[1]);
2358 for (j
= 0; j
< nel
; j
++) {
2359 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2364 p
->symtab
[i
].nprim
= nprim
;
2368 p
->process_class
= string_to_security_class(p
, "process");
2369 if (!p
->process_class
)
2372 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2376 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2377 rc
= cond_read_list(p
, fp
);
2382 rc
= next_entry(buf
, fp
, sizeof(u32
));
2385 nel
= le32_to_cpu(buf
[0]);
2387 for (i
= 0; i
< nel
; i
++) {
2389 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2396 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2401 tr
->role
= le32_to_cpu(buf
[0]);
2402 tr
->type
= le32_to_cpu(buf
[1]);
2403 tr
->new_role
= le32_to_cpu(buf
[2]);
2404 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2405 rc
= next_entry(buf
, fp
, sizeof(u32
));
2408 tr
->tclass
= le32_to_cpu(buf
[0]);
2410 tr
->tclass
= p
->process_class
;
2413 if (!policydb_role_isvalid(p
, tr
->role
) ||
2414 !policydb_type_isvalid(p
, tr
->type
) ||
2415 !policydb_class_isvalid(p
, tr
->tclass
) ||
2416 !policydb_role_isvalid(p
, tr
->new_role
))
2421 rc
= next_entry(buf
, fp
, sizeof(u32
));
2424 nel
= le32_to_cpu(buf
[0]);
2426 for (i
= 0; i
< nel
; i
++) {
2428 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2435 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2440 ra
->role
= le32_to_cpu(buf
[0]);
2441 ra
->new_role
= le32_to_cpu(buf
[1]);
2442 if (!policydb_role_isvalid(p
, ra
->role
) ||
2443 !policydb_role_isvalid(p
, ra
->new_role
))
2448 rc
= filename_trans_read(p
, fp
);
2452 rc
= policydb_index(p
);
2457 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2458 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2459 if (!p
->process_trans_perms
)
2462 rc
= ocontext_read(p
, info
, fp
);
2466 rc
= genfs_read(p
, fp
);
2470 rc
= range_read(p
, fp
);
2475 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2477 GFP_KERNEL
| __GFP_ZERO
);
2478 if (!p
->type_attr_map_array
)
2481 /* preallocate so we don't have to worry about the put ever failing */
2482 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2483 GFP_KERNEL
| __GFP_ZERO
);
2487 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2488 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2492 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2493 rc
= ebitmap_read(e
, fp
);
2497 /* add the type itself as the degenerate case */
2498 rc
= ebitmap_set_bit(e
, i
, 1);
2503 rc
= policydb_bounds_sanity_check(p
);
2511 policydb_destroy(p
);
2516 * Write a MLS level structure to a policydb binary
2517 * representation file.
2519 static int mls_write_level(struct mls_level
*l
, void *fp
)
2524 buf
[0] = cpu_to_le32(l
->sens
);
2525 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2529 rc
= ebitmap_write(&l
->cat
, fp
);
2537 * Write a MLS range structure to a policydb binary
2538 * representation file.
2540 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2546 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2552 buf
[0] = cpu_to_le32(items
-1);
2553 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2555 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2557 BUG_ON(items
> ARRAY_SIZE(buf
));
2559 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2563 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2567 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2575 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2578 struct level_datum
*levdatum
= datum
;
2579 struct policy_data
*pd
= ptr
;
2586 buf
[0] = cpu_to_le32(len
);
2587 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2588 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2592 rc
= put_entry(key
, 1, len
, fp
);
2596 rc
= mls_write_level(levdatum
->level
, fp
);
2603 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2606 struct cat_datum
*catdatum
= datum
;
2607 struct policy_data
*pd
= ptr
;
2614 buf
[0] = cpu_to_le32(len
);
2615 buf
[1] = cpu_to_le32(catdatum
->value
);
2616 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2617 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2621 rc
= put_entry(key
, 1, len
, fp
);
2628 static int role_trans_write(struct policydb
*p
, void *fp
)
2630 struct role_trans
*r
= p
->role_tr
;
2631 struct role_trans
*tr
;
2637 for (tr
= r
; tr
; tr
= tr
->next
)
2639 buf
[0] = cpu_to_le32(nel
);
2640 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2643 for (tr
= r
; tr
; tr
= tr
->next
) {
2644 buf
[0] = cpu_to_le32(tr
->role
);
2645 buf
[1] = cpu_to_le32(tr
->type
);
2646 buf
[2] = cpu_to_le32(tr
->new_role
);
2647 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2650 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2651 buf
[0] = cpu_to_le32(tr
->tclass
);
2652 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2661 static int role_allow_write(struct role_allow
*r
, void *fp
)
2663 struct role_allow
*ra
;
2669 for (ra
= r
; ra
; ra
= ra
->next
)
2671 buf
[0] = cpu_to_le32(nel
);
2672 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2675 for (ra
= r
; ra
; ra
= ra
->next
) {
2676 buf
[0] = cpu_to_le32(ra
->role
);
2677 buf
[1] = cpu_to_le32(ra
->new_role
);
2678 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2686 * Write a security context structure
2687 * to a policydb binary representation file.
2689 static int context_write(struct policydb
*p
, struct context
*c
,
2695 buf
[0] = cpu_to_le32(c
->user
);
2696 buf
[1] = cpu_to_le32(c
->role
);
2697 buf
[2] = cpu_to_le32(c
->type
);
2699 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2703 rc
= mls_write_range_helper(&c
->range
, fp
);
2711 * The following *_write functions are used to
2712 * write the symbol data to a policy database
2713 * binary representation file.
2716 static int perm_write(void *vkey
, void *datum
, void *fp
)
2719 struct perm_datum
*perdatum
= datum
;
2725 buf
[0] = cpu_to_le32(len
);
2726 buf
[1] = cpu_to_le32(perdatum
->value
);
2727 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2731 rc
= put_entry(key
, 1, len
, fp
);
2738 static int common_write(void *vkey
, void *datum
, void *ptr
)
2741 struct common_datum
*comdatum
= datum
;
2742 struct policy_data
*pd
= ptr
;
2749 buf
[0] = cpu_to_le32(len
);
2750 buf
[1] = cpu_to_le32(comdatum
->value
);
2751 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2752 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2753 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2757 rc
= put_entry(key
, 1, len
, fp
);
2761 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2768 static int type_set_write(struct type_set
*t
, void *fp
)
2773 if (ebitmap_write(&t
->types
, fp
))
2775 if (ebitmap_write(&t
->negset
, fp
))
2778 buf
[0] = cpu_to_le32(t
->flags
);
2779 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2786 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2789 struct constraint_node
*c
;
2790 struct constraint_expr
*e
;
2795 for (c
= node
; c
; c
= c
->next
) {
2797 for (e
= c
->expr
; e
; e
= e
->next
)
2799 buf
[0] = cpu_to_le32(c
->permissions
);
2800 buf
[1] = cpu_to_le32(nel
);
2801 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2804 for (e
= c
->expr
; e
; e
= e
->next
) {
2805 buf
[0] = cpu_to_le32(e
->expr_type
);
2806 buf
[1] = cpu_to_le32(e
->attr
);
2807 buf
[2] = cpu_to_le32(e
->op
);
2808 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2812 switch (e
->expr_type
) {
2814 rc
= ebitmap_write(&e
->names
, fp
);
2817 if (p
->policyvers
>=
2818 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2819 rc
= type_set_write(e
->type_names
, fp
);
2833 static int class_write(void *vkey
, void *datum
, void *ptr
)
2836 struct class_datum
*cladatum
= datum
;
2837 struct policy_data
*pd
= ptr
;
2839 struct policydb
*p
= pd
->p
;
2840 struct constraint_node
*c
;
2847 if (cladatum
->comkey
)
2848 len2
= strlen(cladatum
->comkey
);
2853 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2856 buf
[0] = cpu_to_le32(len
);
2857 buf
[1] = cpu_to_le32(len2
);
2858 buf
[2] = cpu_to_le32(cladatum
->value
);
2859 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2860 if (cladatum
->permissions
.table
)
2861 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2864 buf
[5] = cpu_to_le32(ncons
);
2865 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2869 rc
= put_entry(key
, 1, len
, fp
);
2873 if (cladatum
->comkey
) {
2874 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2879 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2883 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2887 /* write out the validatetrans rule */
2889 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2892 buf
[0] = cpu_to_le32(ncons
);
2893 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2897 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2901 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2902 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2903 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2904 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2906 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2911 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2912 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2913 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2921 static int role_write(void *vkey
, void *datum
, void *ptr
)
2924 struct role_datum
*role
= datum
;
2925 struct policy_data
*pd
= ptr
;
2927 struct policydb
*p
= pd
->p
;
2934 buf
[items
++] = cpu_to_le32(len
);
2935 buf
[items
++] = cpu_to_le32(role
->value
);
2936 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2937 buf
[items
++] = cpu_to_le32(role
->bounds
);
2939 BUG_ON(items
> ARRAY_SIZE(buf
));
2941 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2945 rc
= put_entry(key
, 1, len
, fp
);
2949 rc
= ebitmap_write(&role
->dominates
, fp
);
2953 rc
= ebitmap_write(&role
->types
, fp
);
2960 static int type_write(void *vkey
, void *datum
, void *ptr
)
2963 struct type_datum
*typdatum
= datum
;
2964 struct policy_data
*pd
= ptr
;
2965 struct policydb
*p
= pd
->p
;
2973 buf
[items
++] = cpu_to_le32(len
);
2974 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2975 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2978 if (typdatum
->primary
)
2979 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2981 if (typdatum
->attribute
)
2982 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2984 buf
[items
++] = cpu_to_le32(properties
);
2985 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2987 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2989 BUG_ON(items
> ARRAY_SIZE(buf
));
2990 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2994 rc
= put_entry(key
, 1, len
, fp
);
3001 static int user_write(void *vkey
, void *datum
, void *ptr
)
3004 struct user_datum
*usrdatum
= datum
;
3005 struct policy_data
*pd
= ptr
;
3006 struct policydb
*p
= pd
->p
;
3014 buf
[items
++] = cpu_to_le32(len
);
3015 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3016 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3017 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3018 BUG_ON(items
> ARRAY_SIZE(buf
));
3019 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3023 rc
= put_entry(key
, 1, len
, fp
);
3027 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3031 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3035 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3042 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3055 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3058 unsigned int i
, j
, rc
;
3063 for (i
= 0; i
< info
->ocon_num
; i
++) {
3065 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3067 buf
[0] = cpu_to_le32(nel
);
3068 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3071 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3074 buf
[0] = cpu_to_le32(c
->sid
[0]);
3075 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3078 rc
= context_write(p
, &c
->context
[0], fp
);
3084 len
= strlen(c
->u
.name
);
3085 buf
[0] = cpu_to_le32(len
);
3086 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3089 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3092 rc
= context_write(p
, &c
->context
[0], fp
);
3095 rc
= context_write(p
, &c
->context
[1], fp
);
3100 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3101 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3102 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3103 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3106 rc
= context_write(p
, &c
->context
[0], fp
);
3111 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3112 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3113 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3116 rc
= context_write(p
, &c
->context
[0], fp
);
3121 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3122 len
= strlen(c
->u
.name
);
3123 buf
[1] = cpu_to_le32(len
);
3124 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3127 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3130 rc
= context_write(p
, &c
->context
[0], fp
);
3135 for (j
= 0; j
< 4; j
++)
3136 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3137 for (j
= 0; j
< 4; j
++)
3138 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3139 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3142 rc
= context_write(p
, &c
->context
[0], fp
);
3152 static int genfs_write(struct policydb
*p
, void *fp
)
3154 struct genfs
*genfs
;
3161 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3163 buf
[0] = cpu_to_le32(len
);
3164 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3167 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3168 len
= strlen(genfs
->fstype
);
3169 buf
[0] = cpu_to_le32(len
);
3170 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3173 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3177 for (c
= genfs
->head
; c
; c
= c
->next
)
3179 buf
[0] = cpu_to_le32(len
);
3180 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3183 for (c
= genfs
->head
; c
; c
= c
->next
) {
3184 len
= strlen(c
->u
.name
);
3185 buf
[0] = cpu_to_le32(len
);
3186 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3189 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3192 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3193 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3196 rc
= context_write(p
, &c
->context
[0], fp
);
3204 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3212 static int range_write_helper(void *key
, void *data
, void *ptr
)
3215 struct range_trans
*rt
= key
;
3216 struct mls_range
*r
= data
;
3217 struct policy_data
*pd
= ptr
;
3219 struct policydb
*p
= pd
->p
;
3222 buf
[0] = cpu_to_le32(rt
->source_type
);
3223 buf
[1] = cpu_to_le32(rt
->target_type
);
3224 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3227 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3228 buf
[0] = cpu_to_le32(rt
->target_class
);
3229 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3233 rc
= mls_write_range_helper(r
, fp
);
3240 static int range_write(struct policydb
*p
, void *fp
)
3244 struct policy_data pd
;
3249 /* count the number of entries in the hashtab */
3251 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3255 buf
[0] = cpu_to_le32(nel
);
3256 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3260 /* actually write all of the entries */
3261 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3268 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3271 struct filename_trans
*ft
= key
;
3272 struct filename_trans_datum
*otype
= data
;
3277 len
= strlen(ft
->name
);
3278 buf
[0] = cpu_to_le32(len
);
3279 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3283 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3287 buf
[0] = cpu_to_le32(ft
->stype
);
3288 buf
[1] = cpu_to_le32(ft
->ttype
);
3289 buf
[2] = cpu_to_le32(ft
->tclass
);
3290 buf
[3] = cpu_to_le32(otype
->otype
);
3292 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3299 static int filename_trans_write(struct policydb
*p
, void *fp
)
3305 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3309 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3313 buf
[0] = cpu_to_le32(nel
);
3314 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3318 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3326 * Write the configuration data in a policy database
3327 * structure to a policy database binary representation
3330 int policydb_write(struct policydb
*p
, void *fp
)
3332 unsigned int i
, num_syms
;
3337 struct policydb_compat_info
*info
;
3340 * refuse to write policy older than compressed avtab
3341 * to simplify the writer. There are other tests dropped
3342 * since we assume this throughout the writer code. Be
3343 * careful if you ever try to remove this restriction
3345 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3346 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3347 " Because it is less than version %d\n", p
->policyvers
,
3348 POLICYDB_VERSION_AVTAB
);
3354 config
|= POLICYDB_CONFIG_MLS
;
3356 if (p
->reject_unknown
)
3357 config
|= REJECT_UNKNOWN
;
3358 if (p
->allow_unknown
)
3359 config
|= ALLOW_UNKNOWN
;
3361 /* Write the magic number and string identifiers. */
3362 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3363 len
= strlen(POLICYDB_STRING
);
3364 buf
[1] = cpu_to_le32(len
);
3365 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3368 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3372 /* Write the version, config, and table sizes. */
3373 info
= policydb_lookup_compat(p
->policyvers
);
3375 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3376 "version %d", p
->policyvers
);
3380 buf
[0] = cpu_to_le32(p
->policyvers
);
3381 buf
[1] = cpu_to_le32(config
);
3382 buf
[2] = cpu_to_le32(info
->sym_num
);
3383 buf
[3] = cpu_to_le32(info
->ocon_num
);
3385 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3389 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3390 rc
= ebitmap_write(&p
->policycaps
, fp
);
3395 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3396 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3401 num_syms
= info
->sym_num
;
3402 for (i
= 0; i
< num_syms
; i
++) {
3403 struct policy_data pd
;
3408 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3409 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3411 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3414 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3419 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3423 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3427 rc
= role_trans_write(p
, fp
);
3431 rc
= role_allow_write(p
->role_allow
, fp
);
3435 rc
= filename_trans_write(p
, fp
);
3439 rc
= ocontext_write(p
, info
, fp
);
3443 rc
= genfs_write(p
, fp
);
3447 rc
= range_write(p
, fp
);
3451 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3452 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3455 rc
= ebitmap_write(e
, fp
);