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 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
17 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, version 2.
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
30 #include "conditional.h"
36 static char *symtab_name
[SYM_NUM
] = {
48 int selinux_mls_enabled
= 0;
50 static unsigned int symtab_sizes
[SYM_NUM
] = {
61 struct policydb_compat_info
{
67 /* These need to be updated if SYM_NUM or OCON_NUM changes */
68 static struct policydb_compat_info policydb_compat
[] = {
70 .version
= POLICYDB_VERSION_BASE
,
71 .sym_num
= SYM_NUM
- 3,
72 .ocon_num
= OCON_NUM
- 1,
75 .version
= POLICYDB_VERSION_BOOL
,
76 .sym_num
= SYM_NUM
- 2,
77 .ocon_num
= OCON_NUM
- 1,
80 .version
= POLICYDB_VERSION_IPV6
,
81 .sym_num
= SYM_NUM
- 2,
85 .version
= POLICYDB_VERSION_NLCLASS
,
86 .sym_num
= SYM_NUM
- 2,
90 .version
= POLICYDB_VERSION_MLS
,
96 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
99 struct policydb_compat_info
*info
= NULL
;
101 for (i
= 0; i
< sizeof(policydb_compat
)/sizeof(*info
); i
++) {
102 if (policydb_compat
[i
].version
== version
) {
103 info
= &policydb_compat
[i
];
111 * Initialize the role table.
113 static int roles_init(struct policydb
*p
)
117 struct role_datum
*role
;
119 role
= kmalloc(sizeof(*role
), GFP_KERNEL
);
124 memset(role
, 0, sizeof(*role
));
125 role
->value
= ++p
->p_roles
.nprim
;
126 if (role
->value
!= OBJECT_R_VAL
) {
130 key
= kmalloc(strlen(OBJECT_R
)+1,GFP_KERNEL
);
135 strcpy(key
, OBJECT_R
);
136 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
150 * Initialize a policy database structure.
152 static int policydb_init(struct policydb
*p
)
156 memset(p
, 0, sizeof(*p
));
158 for (i
= 0; i
< SYM_NUM
; i
++) {
159 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
161 goto out_free_symtab
;
164 rc
= avtab_init(&p
->te_avtab
);
166 goto out_free_symtab
;
172 rc
= cond_policydb_init(p
);
180 avtab_destroy(&p
->te_avtab
);
183 for (i
= 0; i
< SYM_NUM
; i
++)
184 hashtab_destroy(p
->symtab
[i
].table
);
189 * The following *_index functions are used to
190 * define the val_to_name and val_to_struct arrays
191 * in a policy database structure. The val_to_name
192 * arrays are used when converting security context
193 * structures into string representations. The
194 * val_to_struct arrays are used when the attributes
195 * of a class, role, or user are needed.
198 static int common_index(void *key
, void *datum
, void *datap
)
201 struct common_datum
*comdatum
;
205 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
207 p
->p_common_val_to_name
[comdatum
->value
- 1] = key
;
211 static int class_index(void *key
, void *datum
, void *datap
)
214 struct class_datum
*cladatum
;
218 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
220 p
->p_class_val_to_name
[cladatum
->value
- 1] = key
;
221 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
225 static int role_index(void *key
, void *datum
, void *datap
)
228 struct role_datum
*role
;
232 if (!role
->value
|| role
->value
> p
->p_roles
.nprim
)
234 p
->p_role_val_to_name
[role
->value
- 1] = key
;
235 p
->role_val_to_struct
[role
->value
- 1] = role
;
239 static int type_index(void *key
, void *datum
, void *datap
)
242 struct type_datum
*typdatum
;
247 if (typdatum
->primary
) {
248 if (!typdatum
->value
|| typdatum
->value
> p
->p_types
.nprim
)
250 p
->p_type_val_to_name
[typdatum
->value
- 1] = key
;
256 static int user_index(void *key
, void *datum
, void *datap
)
259 struct user_datum
*usrdatum
;
263 if (!usrdatum
->value
|| usrdatum
->value
> p
->p_users
.nprim
)
265 p
->p_user_val_to_name
[usrdatum
->value
- 1] = key
;
266 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
270 static int sens_index(void *key
, void *datum
, void *datap
)
273 struct level_datum
*levdatum
;
278 if (!levdatum
->isalias
) {
279 if (!levdatum
->level
->sens
||
280 levdatum
->level
->sens
> p
->p_levels
.nprim
)
282 p
->p_sens_val_to_name
[levdatum
->level
->sens
- 1] = key
;
288 static int cat_index(void *key
, void *datum
, void *datap
)
291 struct cat_datum
*catdatum
;
296 if (!catdatum
->isalias
) {
297 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
299 p
->p_cat_val_to_name
[catdatum
->value
- 1] = key
;
305 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
318 * Define the common val_to_name array and the class
319 * val_to_name and val_to_struct arrays in a policy
320 * database structure.
322 * Caller must clean up upon failure.
324 static int policydb_index_classes(struct policydb
*p
)
328 p
->p_common_val_to_name
=
329 kmalloc(p
->p_commons
.nprim
* sizeof(char *), GFP_KERNEL
);
330 if (!p
->p_common_val_to_name
) {
335 rc
= hashtab_map(p
->p_commons
.table
, common_index
, p
);
339 p
->class_val_to_struct
=
340 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)), GFP_KERNEL
);
341 if (!p
->class_val_to_struct
) {
346 p
->p_class_val_to_name
=
347 kmalloc(p
->p_classes
.nprim
* sizeof(char *), GFP_KERNEL
);
348 if (!p
->p_class_val_to_name
) {
353 rc
= hashtab_map(p
->p_classes
.table
, class_index
, p
);
359 static void symtab_hash_eval(struct symtab
*s
)
363 for (i
= 0; i
< SYM_NUM
; i
++) {
364 struct hashtab
*h
= s
[i
].table
;
365 struct hashtab_info info
;
367 hashtab_stat(h
, &info
);
368 printk(KERN_INFO
"%s: %d entries and %d/%d buckets used, "
369 "longest chain length %d\n", symtab_name
[i
], h
->nel
,
370 info
.slots_used
, h
->size
, info
.max_chain_len
);
376 * Define the other val_to_name and val_to_struct arrays
377 * in a policy database structure.
379 * Caller must clean up on failure.
381 static int policydb_index_others(struct policydb
*p
)
385 printk(KERN_INFO
"security: %d users, %d roles, %d types, %d bools",
386 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
387 if (selinux_mls_enabled
)
388 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
392 printk(KERN_INFO
"security: %d classes, %d rules\n",
393 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
396 avtab_hash_eval(&p
->te_avtab
, "rules");
397 symtab_hash_eval(p
->symtab
);
400 p
->role_val_to_struct
=
401 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
403 if (!p
->role_val_to_struct
) {
408 p
->user_val_to_struct
=
409 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
411 if (!p
->user_val_to_struct
) {
416 if (cond_init_bool_indexes(p
)) {
421 for (i
= SYM_ROLES
; i
< SYM_NUM
; i
++) {
422 p
->sym_val_to_name
[i
] =
423 kmalloc(p
->symtab
[i
].nprim
* sizeof(char *), GFP_KERNEL
);
424 if (!p
->sym_val_to_name
[i
]) {
428 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
438 * The following *_destroy functions are used to
439 * free any memory allocated for each kind of
440 * symbol data in the policy database.
443 static int perm_destroy(void *key
, void *datum
, void *p
)
450 static int common_destroy(void *key
, void *datum
, void *p
)
452 struct common_datum
*comdatum
;
456 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
457 hashtab_destroy(comdatum
->permissions
.table
);
462 static int class_destroy(void *key
, void *datum
, void *p
)
464 struct class_datum
*cladatum
;
465 struct constraint_node
*constraint
, *ctemp
;
466 struct constraint_expr
*e
, *etmp
;
470 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
471 hashtab_destroy(cladatum
->permissions
.table
);
472 constraint
= cladatum
->constraints
;
474 e
= constraint
->expr
;
476 ebitmap_destroy(&e
->names
);
482 constraint
= constraint
->next
;
486 constraint
= cladatum
->validatetrans
;
488 e
= constraint
->expr
;
490 ebitmap_destroy(&e
->names
);
496 constraint
= constraint
->next
;
500 kfree(cladatum
->comkey
);
505 static int role_destroy(void *key
, void *datum
, void *p
)
507 struct role_datum
*role
;
511 ebitmap_destroy(&role
->dominates
);
512 ebitmap_destroy(&role
->types
);
517 static int type_destroy(void *key
, void *datum
, void *p
)
524 static int user_destroy(void *key
, void *datum
, void *p
)
526 struct user_datum
*usrdatum
;
530 ebitmap_destroy(&usrdatum
->roles
);
531 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
532 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
533 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
538 static int sens_destroy(void *key
, void *datum
, void *p
)
540 struct level_datum
*levdatum
;
544 ebitmap_destroy(&levdatum
->level
->cat
);
545 kfree(levdatum
->level
);
550 static int cat_destroy(void *key
, void *datum
, void *p
)
557 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
569 static void ocontext_destroy(struct ocontext
*c
, int i
)
571 context_destroy(&c
->context
[0]);
572 context_destroy(&c
->context
[1]);
573 if (i
== OCON_ISID
|| i
== OCON_FS
||
574 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
580 * Free any memory allocated by a policy database structure.
582 void policydb_destroy(struct policydb
*p
)
584 struct ocontext
*c
, *ctmp
;
585 struct genfs
*g
, *gtmp
;
588 for (i
= 0; i
< SYM_NUM
; i
++) {
589 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
590 hashtab_destroy(p
->symtab
[i
].table
);
593 for (i
= 0; i
< SYM_NUM
; i
++) {
594 if (p
->sym_val_to_name
[i
])
595 kfree(p
->sym_val_to_name
[i
]);
598 if (p
->class_val_to_struct
)
599 kfree(p
->class_val_to_struct
);
600 if (p
->role_val_to_struct
)
601 kfree(p
->role_val_to_struct
);
602 if (p
->user_val_to_struct
)
603 kfree(p
->user_val_to_struct
);
605 avtab_destroy(&p
->te_avtab
);
607 for (i
= 0; i
< OCON_NUM
; i
++) {
612 ocontext_destroy(ctmp
,i
);
623 ocontext_destroy(ctmp
,OCON_FSUSE
);
630 cond_policydb_destroy(p
);
636 * Load the initial SIDs specified in a policy database
637 * structure into a SID table.
639 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
641 struct ocontext
*head
, *c
;
646 printk(KERN_ERR
"security: out of memory on SID table init\n");
650 head
= p
->ocontexts
[OCON_ISID
];
651 for (c
= head
; c
; c
= c
->next
) {
652 if (!c
->context
[0].user
) {
653 printk(KERN_ERR
"security: SID %s was never "
654 "defined.\n", c
->u
.name
);
658 if (sidtab_insert(s
, c
->sid
[0], &c
->context
[0])) {
659 printk(KERN_ERR
"security: unable to load initial "
660 "SID %s.\n", c
->u
.name
);
670 * Return 1 if the fields in the security context
671 * structure `c' are valid. Return 0 otherwise.
673 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
675 struct role_datum
*role
;
676 struct user_datum
*usrdatum
;
678 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
681 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
684 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
687 if (c
->role
!= OBJECT_R_VAL
) {
689 * Role must be authorized for the type.
691 role
= p
->role_val_to_struct
[c
->role
- 1];
692 if (!ebitmap_get_bit(&role
->types
,
694 /* role may not be associated with type */
698 * User must be authorized for the role.
700 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
704 if (!ebitmap_get_bit(&usrdatum
->roles
,
706 /* user may not be associated with role */
710 if (!mls_context_isvalid(p
, c
))
717 * Read a MLS range structure from a policydb binary
718 * representation file.
720 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
725 rc
= next_entry(buf
, fp
, sizeof(u32
));
729 items
= le32_to_cpu(buf
[0]);
730 if (items
> ARRAY_SIZE(buf
)) {
731 printk(KERN_ERR
"security: mls: range overflow\n");
735 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
737 printk(KERN_ERR
"security: mls: truncated range\n");
740 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
742 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
744 r
->level
[1].sens
= r
->level
[0].sens
;
746 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
748 printk(KERN_ERR
"security: mls: error reading low "
753 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
755 printk(KERN_ERR
"security: mls: error reading high "
760 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
762 printk(KERN_ERR
"security: mls: out of memory\n");
771 ebitmap_destroy(&r
->level
[0].cat
);
776 * Read and validate a security context structure
777 * from a policydb binary representation file.
779 static int context_read_and_validate(struct context
*c
,
786 rc
= next_entry(buf
, fp
, sizeof buf
);
788 printk(KERN_ERR
"security: context truncated\n");
791 c
->user
= le32_to_cpu(buf
[0]);
792 c
->role
= le32_to_cpu(buf
[1]);
793 c
->type
= le32_to_cpu(buf
[2]);
794 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
795 if (mls_read_range_helper(&c
->range
, fp
)) {
796 printk(KERN_ERR
"security: error reading MLS range of "
803 if (!policydb_context_isvalid(p
, c
)) {
804 printk(KERN_ERR
"security: invalid security context\n");
813 * The following *_read functions are used to
814 * read the symbol data from a policy database
815 * binary representation file.
818 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
821 struct perm_datum
*perdatum
;
825 perdatum
= kmalloc(sizeof(*perdatum
), GFP_KERNEL
);
830 memset(perdatum
, 0, sizeof(*perdatum
));
832 rc
= next_entry(buf
, fp
, sizeof buf
);
836 len
= le32_to_cpu(buf
[0]);
837 perdatum
->value
= le32_to_cpu(buf
[1]);
839 key
= kmalloc(len
+ 1,GFP_KERNEL
);
844 rc
= next_entry(key
, fp
, len
);
849 rc
= hashtab_insert(h
, key
, perdatum
);
855 perm_destroy(key
, perdatum
, NULL
);
859 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
862 struct common_datum
*comdatum
;
863 u32 buf
[4], len
, nel
;
866 comdatum
= kmalloc(sizeof(*comdatum
), GFP_KERNEL
);
871 memset(comdatum
, 0, sizeof(*comdatum
));
873 rc
= next_entry(buf
, fp
, sizeof buf
);
877 len
= le32_to_cpu(buf
[0]);
878 comdatum
->value
= le32_to_cpu(buf
[1]);
880 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
883 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
884 nel
= le32_to_cpu(buf
[3]);
886 key
= kmalloc(len
+ 1,GFP_KERNEL
);
891 rc
= next_entry(key
, fp
, len
);
896 for (i
= 0; i
< nel
; i
++) {
897 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
902 rc
= hashtab_insert(h
, key
, comdatum
);
908 common_destroy(key
, comdatum
, NULL
);
912 static int read_cons_helper(struct constraint_node
**nodep
, int ncons
,
913 int allowxtarget
, void *fp
)
915 struct constraint_node
*c
, *lc
;
916 struct constraint_expr
*e
, *le
;
921 for (i
= 0; i
< ncons
; i
++) {
922 c
= kmalloc(sizeof(*c
), GFP_KERNEL
);
925 memset(c
, 0, sizeof(*c
));
933 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
936 c
->permissions
= le32_to_cpu(buf
[0]);
937 nexpr
= le32_to_cpu(buf
[1]);
940 for (j
= 0; j
< nexpr
; j
++) {
941 e
= kmalloc(sizeof(*e
), GFP_KERNEL
);
944 memset(e
, 0, sizeof(*e
));
952 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
955 e
->expr_type
= le32_to_cpu(buf
[0]);
956 e
->attr
= le32_to_cpu(buf
[1]);
957 e
->op
= le32_to_cpu(buf
[2]);
959 switch (e
->expr_type
) {
971 if (depth
== (CEXPR_MAXDEPTH
- 1))
976 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
978 if (depth
== (CEXPR_MAXDEPTH
- 1))
981 if (ebitmap_read(&e
->names
, fp
))
997 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1000 struct class_datum
*cladatum
;
1001 u32 buf
[6], len
, len2
, ncons
, nel
;
1004 cladatum
= kmalloc(sizeof(*cladatum
), GFP_KERNEL
);
1009 memset(cladatum
, 0, sizeof(*cladatum
));
1011 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1015 len
= le32_to_cpu(buf
[0]);
1016 len2
= le32_to_cpu(buf
[1]);
1017 cladatum
->value
= le32_to_cpu(buf
[2]);
1019 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1022 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1023 nel
= le32_to_cpu(buf
[4]);
1025 ncons
= le32_to_cpu(buf
[5]);
1027 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1032 rc
= next_entry(key
, fp
, len
);
1038 cladatum
->comkey
= kmalloc(len2
+ 1,GFP_KERNEL
);
1039 if (!cladatum
->comkey
) {
1043 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1046 cladatum
->comkey
[len2
] = 0;
1048 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
,
1050 if (!cladatum
->comdatum
) {
1051 printk(KERN_ERR
"security: unknown common %s\n",
1057 for (i
= 0; i
< nel
; i
++) {
1058 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1063 rc
= read_cons_helper(&cladatum
->constraints
, ncons
, 0, fp
);
1067 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1068 /* grab the validatetrans rules */
1069 rc
= next_entry(buf
, fp
, sizeof(u32
));
1072 ncons
= le32_to_cpu(buf
[0]);
1073 rc
= read_cons_helper(&cladatum
->validatetrans
, ncons
, 1, fp
);
1078 rc
= hashtab_insert(h
, key
, cladatum
);
1086 class_destroy(key
, cladatum
, NULL
);
1090 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1093 struct role_datum
*role
;
1097 role
= kmalloc(sizeof(*role
), GFP_KERNEL
);
1102 memset(role
, 0, sizeof(*role
));
1104 rc
= next_entry(buf
, fp
, sizeof buf
);
1108 len
= le32_to_cpu(buf
[0]);
1109 role
->value
= le32_to_cpu(buf
[1]);
1111 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1116 rc
= next_entry(key
, fp
, len
);
1121 rc
= ebitmap_read(&role
->dominates
, fp
);
1125 rc
= ebitmap_read(&role
->types
, fp
);
1129 if (strcmp(key
, OBJECT_R
) == 0) {
1130 if (role
->value
!= OBJECT_R_VAL
) {
1131 printk(KERN_ERR
"Role %s has wrong value %d\n",
1132 OBJECT_R
, role
->value
);
1140 rc
= hashtab_insert(h
, key
, role
);
1146 role_destroy(key
, role
, NULL
);
1150 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1153 struct type_datum
*typdatum
;
1157 typdatum
= kmalloc(sizeof(*typdatum
),GFP_KERNEL
);
1162 memset(typdatum
, 0, sizeof(*typdatum
));
1164 rc
= next_entry(buf
, fp
, sizeof buf
);
1168 len
= le32_to_cpu(buf
[0]);
1169 typdatum
->value
= le32_to_cpu(buf
[1]);
1170 typdatum
->primary
= le32_to_cpu(buf
[2]);
1172 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1177 rc
= next_entry(key
, fp
, len
);
1182 rc
= hashtab_insert(h
, key
, typdatum
);
1188 type_destroy(key
, typdatum
, NULL
);
1194 * Read a MLS level structure from a policydb binary
1195 * representation file.
1197 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1202 memset(lp
, 0, sizeof(*lp
));
1204 rc
= next_entry(buf
, fp
, sizeof buf
);
1206 printk(KERN_ERR
"security: mls: truncated level\n");
1209 lp
->sens
= le32_to_cpu(buf
[0]);
1211 if (ebitmap_read(&lp
->cat
, fp
)) {
1212 printk(KERN_ERR
"security: mls: error reading level "
1222 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1225 struct user_datum
*usrdatum
;
1229 usrdatum
= kmalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1234 memset(usrdatum
, 0, sizeof(*usrdatum
));
1236 rc
= next_entry(buf
, fp
, sizeof buf
);
1240 len
= le32_to_cpu(buf
[0]);
1241 usrdatum
->value
= le32_to_cpu(buf
[1]);
1243 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1248 rc
= next_entry(key
, fp
, len
);
1253 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1257 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1258 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1261 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1266 rc
= hashtab_insert(h
, key
, usrdatum
);
1272 user_destroy(key
, usrdatum
, NULL
);
1276 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1279 struct level_datum
*levdatum
;
1283 levdatum
= kmalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1288 memset(levdatum
, 0, sizeof(*levdatum
));
1290 rc
= next_entry(buf
, fp
, sizeof buf
);
1294 len
= le32_to_cpu(buf
[0]);
1295 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1297 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1302 rc
= next_entry(key
, fp
, len
);
1307 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1308 if (!levdatum
->level
) {
1312 if (mls_read_level(levdatum
->level
, fp
)) {
1317 rc
= hashtab_insert(h
, key
, levdatum
);
1323 sens_destroy(key
, levdatum
, NULL
);
1327 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1330 struct cat_datum
*catdatum
;
1334 catdatum
= kmalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1339 memset(catdatum
, 0, sizeof(*catdatum
));
1341 rc
= next_entry(buf
, fp
, sizeof buf
);
1345 len
= le32_to_cpu(buf
[0]);
1346 catdatum
->value
= le32_to_cpu(buf
[1]);
1347 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1349 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1354 rc
= next_entry(key
, fp
, len
);
1359 rc
= hashtab_insert(h
, key
, catdatum
);
1366 cat_destroy(key
, catdatum
, NULL
);
1370 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1382 extern int ss_initialized
;
1385 * Read the configuration data from a policy database binary
1386 * representation file into a policy database structure.
1388 int policydb_read(struct policydb
*p
, void *fp
)
1390 struct role_allow
*ra
, *lra
;
1391 struct role_trans
*tr
, *ltr
;
1392 struct ocontext
*l
, *c
, *newc
;
1393 struct genfs
*genfs_p
, *genfs
, *newgenfs
;
1395 u32 buf
[8], len
, len2
, config
, nprim
, nel
, nel2
;
1397 struct policydb_compat_info
*info
;
1398 struct range_trans
*rt
, *lrt
;
1402 rc
= policydb_init(p
);
1406 /* Read the magic number and string length. */
1407 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1411 for (i
= 0; i
< 2; i
++)
1412 buf
[i
] = le32_to_cpu(buf
[i
]);
1414 if (buf
[0] != POLICYDB_MAGIC
) {
1415 printk(KERN_ERR
"security: policydb magic number 0x%x does "
1416 "not match expected magic number 0x%x\n",
1417 buf
[0], POLICYDB_MAGIC
);
1422 if (len
!= strlen(POLICYDB_STRING
)) {
1423 printk(KERN_ERR
"security: policydb string length %d does not "
1424 "match expected length %Zu\n",
1425 len
, strlen(POLICYDB_STRING
));
1428 policydb_str
= kmalloc(len
+ 1,GFP_KERNEL
);
1429 if (!policydb_str
) {
1430 printk(KERN_ERR
"security: unable to allocate memory for policydb "
1431 "string of length %d\n", len
);
1435 rc
= next_entry(policydb_str
, fp
, len
);
1437 printk(KERN_ERR
"security: truncated policydb string identifier\n");
1438 kfree(policydb_str
);
1441 policydb_str
[len
] = 0;
1442 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
1443 printk(KERN_ERR
"security: policydb string %s does not match "
1444 "my string %s\n", policydb_str
, POLICYDB_STRING
);
1445 kfree(policydb_str
);
1448 /* Done with policydb_str. */
1449 kfree(policydb_str
);
1450 policydb_str
= NULL
;
1452 /* Read the version, config, and table sizes. */
1453 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
1456 for (i
= 0; i
< 4; i
++)
1457 buf
[i
] = le32_to_cpu(buf
[i
]);
1459 p
->policyvers
= buf
[0];
1460 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
1461 p
->policyvers
> POLICYDB_VERSION_MAX
) {
1462 printk(KERN_ERR
"security: policydb version %d does not match "
1463 "my version range %d-%d\n",
1464 buf
[0], POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
1468 if ((buf
[1] & POLICYDB_CONFIG_MLS
)) {
1469 if (ss_initialized
&& !selinux_mls_enabled
) {
1470 printk(KERN_ERR
"Cannot switch between non-MLS and MLS "
1474 selinux_mls_enabled
= 1;
1475 config
|= POLICYDB_CONFIG_MLS
;
1477 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
1478 printk(KERN_ERR
"security policydb version %d (MLS) "
1479 "not backwards compatible\n", p
->policyvers
);
1483 if (ss_initialized
&& selinux_mls_enabled
) {
1484 printk(KERN_ERR
"Cannot switch between MLS and non-MLS "
1490 info
= policydb_lookup_compat(p
->policyvers
);
1492 printk(KERN_ERR
"security: unable to find policy compat info "
1493 "for version %d\n", p
->policyvers
);
1497 if (buf
[2] != info
->sym_num
|| buf
[3] != info
->ocon_num
) {
1498 printk(KERN_ERR
"security: policydb table sizes (%d,%d) do "
1499 "not match mine (%d,%d)\n", buf
[2], buf
[3],
1500 info
->sym_num
, info
->ocon_num
);
1504 for (i
= 0; i
< info
->sym_num
; i
++) {
1505 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1508 nprim
= le32_to_cpu(buf
[0]);
1509 nel
= le32_to_cpu(buf
[1]);
1510 for (j
= 0; j
< nel
; j
++) {
1511 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
1516 p
->symtab
[i
].nprim
= nprim
;
1519 rc
= avtab_read(&p
->te_avtab
, fp
, config
);
1523 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
1524 rc
= cond_read_list(p
, fp
);
1529 rc
= next_entry(buf
, fp
, sizeof(u32
));
1532 nel
= le32_to_cpu(buf
[0]);
1534 for (i
= 0; i
< nel
; i
++) {
1535 tr
= kmalloc(sizeof(*tr
), GFP_KERNEL
);
1540 memset(tr
, 0, sizeof(*tr
));
1546 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1549 tr
->role
= le32_to_cpu(buf
[0]);
1550 tr
->type
= le32_to_cpu(buf
[1]);
1551 tr
->new_role
= le32_to_cpu(buf
[2]);
1555 rc
= next_entry(buf
, fp
, sizeof(u32
));
1558 nel
= le32_to_cpu(buf
[0]);
1560 for (i
= 0; i
< nel
; i
++) {
1561 ra
= kmalloc(sizeof(*ra
), GFP_KERNEL
);
1566 memset(ra
, 0, sizeof(*ra
));
1572 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1575 ra
->role
= le32_to_cpu(buf
[0]);
1576 ra
->new_role
= le32_to_cpu(buf
[1]);
1580 rc
= policydb_index_classes(p
);
1584 rc
= policydb_index_others(p
);
1588 for (i
= 0; i
< info
->ocon_num
; i
++) {
1589 rc
= next_entry(buf
, fp
, sizeof(u32
));
1592 nel
= le32_to_cpu(buf
[0]);
1594 for (j
= 0; j
< nel
; j
++) {
1595 c
= kmalloc(sizeof(*c
), GFP_KERNEL
);
1600 memset(c
, 0, sizeof(*c
));
1604 p
->ocontexts
[i
] = c
;
1610 rc
= next_entry(buf
, fp
, sizeof(u32
));
1613 c
->sid
[0] = le32_to_cpu(buf
[0]);
1614 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1620 rc
= next_entry(buf
, fp
, sizeof(u32
));
1623 len
= le32_to_cpu(buf
[0]);
1624 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1629 rc
= next_entry(c
->u
.name
, fp
, len
);
1633 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1636 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
1641 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1644 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
1645 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
1646 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
1647 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1652 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1655 c
->u
.node
.addr
= le32_to_cpu(buf
[0]);
1656 c
->u
.node
.mask
= le32_to_cpu(buf
[1]);
1657 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1662 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1665 c
->v
.behavior
= le32_to_cpu(buf
[0]);
1666 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
1668 len
= le32_to_cpu(buf
[1]);
1669 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1674 rc
= next_entry(c
->u
.name
, fp
, len
);
1678 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1685 rc
= next_entry(buf
, fp
, sizeof(u32
) * 8);
1688 for (k
= 0; k
< 4; k
++)
1689 c
->u
.node6
.addr
[k
] = le32_to_cpu(buf
[k
]);
1690 for (k
= 0; k
< 4; k
++)
1691 c
->u
.node6
.mask
[k
] = le32_to_cpu(buf
[k
+4]);
1692 if (context_read_and_validate(&c
->context
[0], p
, fp
))
1700 rc
= next_entry(buf
, fp
, sizeof(u32
));
1703 nel
= le32_to_cpu(buf
[0]);
1706 for (i
= 0; i
< nel
; i
++) {
1707 rc
= next_entry(buf
, fp
, sizeof(u32
));
1710 len
= le32_to_cpu(buf
[0]);
1711 newgenfs
= kmalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1716 memset(newgenfs
, 0, sizeof(*newgenfs
));
1718 newgenfs
->fstype
= kmalloc(len
+ 1,GFP_KERNEL
);
1719 if (!newgenfs
->fstype
) {
1724 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
1726 kfree(newgenfs
->fstype
);
1730 newgenfs
->fstype
[len
] = 0;
1731 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1732 genfs_p
= genfs
, genfs
= genfs
->next
) {
1733 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1734 printk(KERN_ERR
"security: dup genfs "
1735 "fstype %s\n", newgenfs
->fstype
);
1736 kfree(newgenfs
->fstype
);
1740 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1743 newgenfs
->next
= genfs
;
1745 genfs_p
->next
= newgenfs
;
1747 p
->genfs
= newgenfs
;
1748 rc
= next_entry(buf
, fp
, sizeof(u32
));
1751 nel2
= le32_to_cpu(buf
[0]);
1752 for (j
= 0; j
< nel2
; j
++) {
1753 rc
= next_entry(buf
, fp
, sizeof(u32
));
1756 len
= le32_to_cpu(buf
[0]);
1758 newc
= kmalloc(sizeof(*newc
), GFP_KERNEL
);
1763 memset(newc
, 0, sizeof(*newc
));
1765 newc
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1766 if (!newc
->u
.name
) {
1770 rc
= next_entry(newc
->u
.name
, fp
, len
);
1773 newc
->u
.name
[len
] = 0;
1774 rc
= next_entry(buf
, fp
, sizeof(u32
));
1777 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
1778 if (context_read_and_validate(&newc
->context
[0], p
, fp
))
1780 for (l
= NULL
, c
= newgenfs
->head
; c
;
1781 l
= c
, c
= c
->next
) {
1782 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
1783 (!c
->v
.sclass
|| !newc
->v
.sclass
||
1784 newc
->v
.sclass
== c
->v
.sclass
)) {
1785 printk(KERN_ERR
"security: dup genfs "
1787 newgenfs
->fstype
, c
->u
.name
);
1790 len
= strlen(newc
->u
.name
);
1791 len2
= strlen(c
->u
.name
);
1800 newgenfs
->head
= newc
;
1804 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1805 rc
= next_entry(buf
, fp
, sizeof(u32
));
1808 nel
= le32_to_cpu(buf
[0]);
1810 for (i
= 0; i
< nel
; i
++) {
1811 rt
= kmalloc(sizeof(*rt
), GFP_KERNEL
);
1816 memset(rt
, 0, sizeof(*rt
));
1821 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1824 rt
->dom
= le32_to_cpu(buf
[0]);
1825 rt
->type
= le32_to_cpu(buf
[1]);
1826 rc
= mls_read_range_helper(&rt
->range
, fp
);
1837 ocontext_destroy(newc
,OCON_FSUSE
);
1841 policydb_destroy(p
);