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
,
95 .version
= POLICYDB_VERSION_AVTAB
,
101 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
104 struct policydb_compat_info
*info
= NULL
;
106 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
107 if (policydb_compat
[i
].version
== version
) {
108 info
= &policydb_compat
[i
];
116 * Initialize the role table.
118 static int roles_init(struct policydb
*p
)
122 struct role_datum
*role
;
124 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
129 role
->value
= ++p
->p_roles
.nprim
;
130 if (role
->value
!= OBJECT_R_VAL
) {
134 key
= kmalloc(strlen(OBJECT_R
)+1,GFP_KERNEL
);
139 strcpy(key
, OBJECT_R
);
140 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
154 * Initialize a policy database structure.
156 static int policydb_init(struct policydb
*p
)
160 memset(p
, 0, sizeof(*p
));
162 for (i
= 0; i
< SYM_NUM
; i
++) {
163 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
165 goto out_free_symtab
;
168 rc
= avtab_init(&p
->te_avtab
);
170 goto out_free_symtab
;
176 rc
= cond_policydb_init(p
);
184 avtab_destroy(&p
->te_avtab
);
187 for (i
= 0; i
< SYM_NUM
; i
++)
188 hashtab_destroy(p
->symtab
[i
].table
);
193 * The following *_index functions are used to
194 * define the val_to_name and val_to_struct arrays
195 * in a policy database structure. The val_to_name
196 * arrays are used when converting security context
197 * structures into string representations. The
198 * val_to_struct arrays are used when the attributes
199 * of a class, role, or user are needed.
202 static int common_index(void *key
, void *datum
, void *datap
)
205 struct common_datum
*comdatum
;
209 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
211 p
->p_common_val_to_name
[comdatum
->value
- 1] = key
;
215 static int class_index(void *key
, void *datum
, void *datap
)
218 struct class_datum
*cladatum
;
222 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
224 p
->p_class_val_to_name
[cladatum
->value
- 1] = key
;
225 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
229 static int role_index(void *key
, void *datum
, void *datap
)
232 struct role_datum
*role
;
236 if (!role
->value
|| role
->value
> p
->p_roles
.nprim
)
238 p
->p_role_val_to_name
[role
->value
- 1] = key
;
239 p
->role_val_to_struct
[role
->value
- 1] = role
;
243 static int type_index(void *key
, void *datum
, void *datap
)
246 struct type_datum
*typdatum
;
251 if (typdatum
->primary
) {
252 if (!typdatum
->value
|| typdatum
->value
> p
->p_types
.nprim
)
254 p
->p_type_val_to_name
[typdatum
->value
- 1] = key
;
260 static int user_index(void *key
, void *datum
, void *datap
)
263 struct user_datum
*usrdatum
;
267 if (!usrdatum
->value
|| usrdatum
->value
> p
->p_users
.nprim
)
269 p
->p_user_val_to_name
[usrdatum
->value
- 1] = key
;
270 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
274 static int sens_index(void *key
, void *datum
, void *datap
)
277 struct level_datum
*levdatum
;
282 if (!levdatum
->isalias
) {
283 if (!levdatum
->level
->sens
||
284 levdatum
->level
->sens
> p
->p_levels
.nprim
)
286 p
->p_sens_val_to_name
[levdatum
->level
->sens
- 1] = key
;
292 static int cat_index(void *key
, void *datum
, void *datap
)
295 struct cat_datum
*catdatum
;
300 if (!catdatum
->isalias
) {
301 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
303 p
->p_cat_val_to_name
[catdatum
->value
- 1] = key
;
309 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
322 * Define the common val_to_name array and the class
323 * val_to_name and val_to_struct arrays in a policy
324 * database structure.
326 * Caller must clean up upon failure.
328 static int policydb_index_classes(struct policydb
*p
)
332 p
->p_common_val_to_name
=
333 kmalloc(p
->p_commons
.nprim
* sizeof(char *), GFP_KERNEL
);
334 if (!p
->p_common_val_to_name
) {
339 rc
= hashtab_map(p
->p_commons
.table
, common_index
, p
);
343 p
->class_val_to_struct
=
344 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)), GFP_KERNEL
);
345 if (!p
->class_val_to_struct
) {
350 p
->p_class_val_to_name
=
351 kmalloc(p
->p_classes
.nprim
* sizeof(char *), GFP_KERNEL
);
352 if (!p
->p_class_val_to_name
) {
357 rc
= hashtab_map(p
->p_classes
.table
, class_index
, p
);
363 static void symtab_hash_eval(struct symtab
*s
)
367 for (i
= 0; i
< SYM_NUM
; i
++) {
368 struct hashtab
*h
= s
[i
].table
;
369 struct hashtab_info info
;
371 hashtab_stat(h
, &info
);
372 printk(KERN_INFO
"%s: %d entries and %d/%d buckets used, "
373 "longest chain length %d\n", symtab_name
[i
], h
->nel
,
374 info
.slots_used
, h
->size
, info
.max_chain_len
);
380 * Define the other val_to_name and val_to_struct arrays
381 * in a policy database structure.
383 * Caller must clean up on failure.
385 static int policydb_index_others(struct policydb
*p
)
389 printk(KERN_INFO
"security: %d users, %d roles, %d types, %d bools",
390 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
391 if (selinux_mls_enabled
)
392 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
396 printk(KERN_INFO
"security: %d classes, %d rules\n",
397 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
400 avtab_hash_eval(&p
->te_avtab
, "rules");
401 symtab_hash_eval(p
->symtab
);
404 p
->role_val_to_struct
=
405 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
407 if (!p
->role_val_to_struct
) {
412 p
->user_val_to_struct
=
413 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
415 if (!p
->user_val_to_struct
) {
420 if (cond_init_bool_indexes(p
)) {
425 for (i
= SYM_ROLES
; i
< SYM_NUM
; i
++) {
426 p
->sym_val_to_name
[i
] =
427 kmalloc(p
->symtab
[i
].nprim
* sizeof(char *), GFP_KERNEL
);
428 if (!p
->sym_val_to_name
[i
]) {
432 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
442 * The following *_destroy functions are used to
443 * free any memory allocated for each kind of
444 * symbol data in the policy database.
447 static int perm_destroy(void *key
, void *datum
, void *p
)
454 static int common_destroy(void *key
, void *datum
, void *p
)
456 struct common_datum
*comdatum
;
460 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
461 hashtab_destroy(comdatum
->permissions
.table
);
466 static int class_destroy(void *key
, void *datum
, void *p
)
468 struct class_datum
*cladatum
;
469 struct constraint_node
*constraint
, *ctemp
;
470 struct constraint_expr
*e
, *etmp
;
474 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
475 hashtab_destroy(cladatum
->permissions
.table
);
476 constraint
= cladatum
->constraints
;
478 e
= constraint
->expr
;
480 ebitmap_destroy(&e
->names
);
486 constraint
= constraint
->next
;
490 constraint
= cladatum
->validatetrans
;
492 e
= constraint
->expr
;
494 ebitmap_destroy(&e
->names
);
500 constraint
= constraint
->next
;
504 kfree(cladatum
->comkey
);
509 static int role_destroy(void *key
, void *datum
, void *p
)
511 struct role_datum
*role
;
515 ebitmap_destroy(&role
->dominates
);
516 ebitmap_destroy(&role
->types
);
521 static int type_destroy(void *key
, void *datum
, void *p
)
528 static int user_destroy(void *key
, void *datum
, void *p
)
530 struct user_datum
*usrdatum
;
534 ebitmap_destroy(&usrdatum
->roles
);
535 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
536 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
537 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
542 static int sens_destroy(void *key
, void *datum
, void *p
)
544 struct level_datum
*levdatum
;
548 ebitmap_destroy(&levdatum
->level
->cat
);
549 kfree(levdatum
->level
);
554 static int cat_destroy(void *key
, void *datum
, void *p
)
561 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
573 static void ocontext_destroy(struct ocontext
*c
, int i
)
575 context_destroy(&c
->context
[0]);
576 context_destroy(&c
->context
[1]);
577 if (i
== OCON_ISID
|| i
== OCON_FS
||
578 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
584 * Free any memory allocated by a policy database structure.
586 void policydb_destroy(struct policydb
*p
)
588 struct ocontext
*c
, *ctmp
;
589 struct genfs
*g
, *gtmp
;
591 struct role_allow
*ra
, *lra
= NULL
;
592 struct role_trans
*tr
, *ltr
= NULL
;
593 struct range_trans
*rt
, *lrt
= NULL
;
595 for (i
= 0; i
< SYM_NUM
; i
++) {
596 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
597 hashtab_destroy(p
->symtab
[i
].table
);
600 for (i
= 0; i
< SYM_NUM
; i
++)
601 kfree(p
->sym_val_to_name
[i
]);
603 kfree(p
->class_val_to_struct
);
604 kfree(p
->role_val_to_struct
);
605 kfree(p
->user_val_to_struct
);
607 avtab_destroy(&p
->te_avtab
);
609 for (i
= 0; i
< OCON_NUM
; i
++) {
614 ocontext_destroy(ctmp
,i
);
625 ocontext_destroy(ctmp
,OCON_FSUSE
);
632 cond_policydb_destroy(p
);
634 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
640 for (ra
= p
->role_allow
; ra
; ra
= ra
-> next
) {
646 for (rt
= p
->range_tr
; rt
; rt
= rt
-> next
) {
652 if (p
->type_attr_map
) {
653 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
654 ebitmap_destroy(&p
->type_attr_map
[i
]);
656 kfree(p
->type_attr_map
);
662 * Load the initial SIDs specified in a policy database
663 * structure into a SID table.
665 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
667 struct ocontext
*head
, *c
;
672 printk(KERN_ERR
"security: out of memory on SID table init\n");
676 head
= p
->ocontexts
[OCON_ISID
];
677 for (c
= head
; c
; c
= c
->next
) {
678 if (!c
->context
[0].user
) {
679 printk(KERN_ERR
"security: SID %s was never "
680 "defined.\n", c
->u
.name
);
684 if (sidtab_insert(s
, c
->sid
[0], &c
->context
[0])) {
685 printk(KERN_ERR
"security: unable to load initial "
686 "SID %s.\n", c
->u
.name
);
696 * Return 1 if the fields in the security context
697 * structure `c' are valid. Return 0 otherwise.
699 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
701 struct role_datum
*role
;
702 struct user_datum
*usrdatum
;
704 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
707 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
710 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
713 if (c
->role
!= OBJECT_R_VAL
) {
715 * Role must be authorized for the type.
717 role
= p
->role_val_to_struct
[c
->role
- 1];
718 if (!ebitmap_get_bit(&role
->types
,
720 /* role may not be associated with type */
724 * User must be authorized for the role.
726 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
730 if (!ebitmap_get_bit(&usrdatum
->roles
,
732 /* user may not be associated with role */
736 if (!mls_context_isvalid(p
, c
))
743 * Read a MLS range structure from a policydb binary
744 * representation file.
746 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
752 rc
= next_entry(buf
, fp
, sizeof(u32
));
756 items
= le32_to_cpu(buf
[0]);
757 if (items
> ARRAY_SIZE(buf
)) {
758 printk(KERN_ERR
"security: mls: range overflow\n");
762 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
764 printk(KERN_ERR
"security: mls: truncated range\n");
767 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
769 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
771 r
->level
[1].sens
= r
->level
[0].sens
;
773 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
775 printk(KERN_ERR
"security: mls: error reading low "
780 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
782 printk(KERN_ERR
"security: mls: error reading high "
787 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
789 printk(KERN_ERR
"security: mls: out of memory\n");
798 ebitmap_destroy(&r
->level
[0].cat
);
803 * Read and validate a security context structure
804 * from a policydb binary representation file.
806 static int context_read_and_validate(struct context
*c
,
813 rc
= next_entry(buf
, fp
, sizeof buf
);
815 printk(KERN_ERR
"security: context truncated\n");
818 c
->user
= le32_to_cpu(buf
[0]);
819 c
->role
= le32_to_cpu(buf
[1]);
820 c
->type
= le32_to_cpu(buf
[2]);
821 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
822 if (mls_read_range_helper(&c
->range
, fp
)) {
823 printk(KERN_ERR
"security: error reading MLS range of "
830 if (!policydb_context_isvalid(p
, c
)) {
831 printk(KERN_ERR
"security: invalid security context\n");
840 * The following *_read functions are used to
841 * read the symbol data from a policy database
842 * binary representation file.
845 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
848 struct perm_datum
*perdatum
;
853 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
859 rc
= next_entry(buf
, fp
, sizeof buf
);
863 len
= le32_to_cpu(buf
[0]);
864 perdatum
->value
= le32_to_cpu(buf
[1]);
866 key
= kmalloc(len
+ 1,GFP_KERNEL
);
871 rc
= next_entry(key
, fp
, len
);
876 rc
= hashtab_insert(h
, key
, perdatum
);
882 perm_destroy(key
, perdatum
, NULL
);
886 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
889 struct common_datum
*comdatum
;
894 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
900 rc
= next_entry(buf
, fp
, sizeof buf
);
904 len
= le32_to_cpu(buf
[0]);
905 comdatum
->value
= le32_to_cpu(buf
[1]);
907 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
910 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
911 nel
= le32_to_cpu(buf
[3]);
913 key
= kmalloc(len
+ 1,GFP_KERNEL
);
918 rc
= next_entry(key
, fp
, len
);
923 for (i
= 0; i
< nel
; i
++) {
924 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
929 rc
= hashtab_insert(h
, key
, comdatum
);
935 common_destroy(key
, comdatum
, NULL
);
939 static int read_cons_helper(struct constraint_node
**nodep
, int ncons
,
940 int allowxtarget
, void *fp
)
942 struct constraint_node
*c
, *lc
;
943 struct constraint_expr
*e
, *le
;
949 for (i
= 0; i
< ncons
; i
++) {
950 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
960 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
963 c
->permissions
= le32_to_cpu(buf
[0]);
964 nexpr
= le32_to_cpu(buf
[1]);
967 for (j
= 0; j
< nexpr
; j
++) {
968 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
978 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
981 e
->expr_type
= le32_to_cpu(buf
[0]);
982 e
->attr
= le32_to_cpu(buf
[1]);
983 e
->op
= le32_to_cpu(buf
[2]);
985 switch (e
->expr_type
) {
997 if (depth
== (CEXPR_MAXDEPTH
- 1))
1002 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1004 if (depth
== (CEXPR_MAXDEPTH
- 1))
1007 if (ebitmap_read(&e
->names
, fp
))
1023 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1026 struct class_datum
*cladatum
;
1028 u32 len
, len2
, ncons
, nel
;
1031 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1037 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1041 len
= le32_to_cpu(buf
[0]);
1042 len2
= le32_to_cpu(buf
[1]);
1043 cladatum
->value
= le32_to_cpu(buf
[2]);
1045 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1048 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1049 nel
= le32_to_cpu(buf
[4]);
1051 ncons
= le32_to_cpu(buf
[5]);
1053 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1058 rc
= next_entry(key
, fp
, len
);
1064 cladatum
->comkey
= kmalloc(len2
+ 1,GFP_KERNEL
);
1065 if (!cladatum
->comkey
) {
1069 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1072 cladatum
->comkey
[len2
] = 0;
1074 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
,
1076 if (!cladatum
->comdatum
) {
1077 printk(KERN_ERR
"security: unknown common %s\n",
1083 for (i
= 0; i
< nel
; i
++) {
1084 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1089 rc
= read_cons_helper(&cladatum
->constraints
, ncons
, 0, fp
);
1093 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1094 /* grab the validatetrans rules */
1095 rc
= next_entry(buf
, fp
, sizeof(u32
));
1098 ncons
= le32_to_cpu(buf
[0]);
1099 rc
= read_cons_helper(&cladatum
->validatetrans
, ncons
, 1, fp
);
1104 rc
= hashtab_insert(h
, key
, cladatum
);
1112 class_destroy(key
, cladatum
, NULL
);
1116 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1119 struct role_datum
*role
;
1124 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1130 rc
= next_entry(buf
, fp
, sizeof buf
);
1134 len
= le32_to_cpu(buf
[0]);
1135 role
->value
= le32_to_cpu(buf
[1]);
1137 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1142 rc
= next_entry(key
, fp
, len
);
1147 rc
= ebitmap_read(&role
->dominates
, fp
);
1151 rc
= ebitmap_read(&role
->types
, fp
);
1155 if (strcmp(key
, OBJECT_R
) == 0) {
1156 if (role
->value
!= OBJECT_R_VAL
) {
1157 printk(KERN_ERR
"Role %s has wrong value %d\n",
1158 OBJECT_R
, role
->value
);
1166 rc
= hashtab_insert(h
, key
, role
);
1172 role_destroy(key
, role
, NULL
);
1176 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1179 struct type_datum
*typdatum
;
1184 typdatum
= kzalloc(sizeof(*typdatum
),GFP_KERNEL
);
1190 rc
= next_entry(buf
, fp
, sizeof buf
);
1194 len
= le32_to_cpu(buf
[0]);
1195 typdatum
->value
= le32_to_cpu(buf
[1]);
1196 typdatum
->primary
= le32_to_cpu(buf
[2]);
1198 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1203 rc
= next_entry(key
, fp
, len
);
1208 rc
= hashtab_insert(h
, key
, typdatum
);
1214 type_destroy(key
, typdatum
, NULL
);
1220 * Read a MLS level structure from a policydb binary
1221 * representation file.
1223 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1228 memset(lp
, 0, sizeof(*lp
));
1230 rc
= next_entry(buf
, fp
, sizeof buf
);
1232 printk(KERN_ERR
"security: mls: truncated level\n");
1235 lp
->sens
= le32_to_cpu(buf
[0]);
1237 if (ebitmap_read(&lp
->cat
, fp
)) {
1238 printk(KERN_ERR
"security: mls: error reading level "
1248 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1251 struct user_datum
*usrdatum
;
1256 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1262 rc
= next_entry(buf
, fp
, sizeof buf
);
1266 len
= le32_to_cpu(buf
[0]);
1267 usrdatum
->value
= le32_to_cpu(buf
[1]);
1269 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1274 rc
= next_entry(key
, fp
, len
);
1279 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1283 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1284 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1287 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1292 rc
= hashtab_insert(h
, key
, usrdatum
);
1298 user_destroy(key
, usrdatum
, NULL
);
1302 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1305 struct level_datum
*levdatum
;
1310 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1316 rc
= next_entry(buf
, fp
, sizeof buf
);
1320 len
= le32_to_cpu(buf
[0]);
1321 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1323 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1328 rc
= next_entry(key
, fp
, len
);
1333 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1334 if (!levdatum
->level
) {
1338 if (mls_read_level(levdatum
->level
, fp
)) {
1343 rc
= hashtab_insert(h
, key
, levdatum
);
1349 sens_destroy(key
, levdatum
, NULL
);
1353 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1356 struct cat_datum
*catdatum
;
1361 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1367 rc
= next_entry(buf
, fp
, sizeof buf
);
1371 len
= le32_to_cpu(buf
[0]);
1372 catdatum
->value
= le32_to_cpu(buf
[1]);
1373 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1375 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1380 rc
= next_entry(key
, fp
, len
);
1385 rc
= hashtab_insert(h
, key
, catdatum
);
1392 cat_destroy(key
, catdatum
, NULL
);
1396 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1408 extern int ss_initialized
;
1411 * Read the configuration data from a policy database binary
1412 * representation file into a policy database structure.
1414 int policydb_read(struct policydb
*p
, void *fp
)
1416 struct role_allow
*ra
, *lra
;
1417 struct role_trans
*tr
, *ltr
;
1418 struct ocontext
*l
, *c
, *newc
;
1419 struct genfs
*genfs_p
, *genfs
, *newgenfs
;
1422 u32 len
, len2
, config
, nprim
, nel
, nel2
;
1424 struct policydb_compat_info
*info
;
1425 struct range_trans
*rt
, *lrt
;
1429 rc
= policydb_init(p
);
1433 /* Read the magic number and string length. */
1434 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1438 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
1439 printk(KERN_ERR
"security: policydb magic number 0x%x does "
1440 "not match expected magic number 0x%x\n",
1441 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
1445 len
= le32_to_cpu(buf
[1]);
1446 if (len
!= strlen(POLICYDB_STRING
)) {
1447 printk(KERN_ERR
"security: policydb string length %d does not "
1448 "match expected length %Zu\n",
1449 len
, strlen(POLICYDB_STRING
));
1452 policydb_str
= kmalloc(len
+ 1,GFP_KERNEL
);
1453 if (!policydb_str
) {
1454 printk(KERN_ERR
"security: unable to allocate memory for policydb "
1455 "string of length %d\n", len
);
1459 rc
= next_entry(policydb_str
, fp
, len
);
1461 printk(KERN_ERR
"security: truncated policydb string identifier\n");
1462 kfree(policydb_str
);
1465 policydb_str
[len
] = 0;
1466 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
1467 printk(KERN_ERR
"security: policydb string %s does not match "
1468 "my string %s\n", policydb_str
, POLICYDB_STRING
);
1469 kfree(policydb_str
);
1472 /* Done with policydb_str. */
1473 kfree(policydb_str
);
1474 policydb_str
= NULL
;
1476 /* Read the version, config, and table sizes. */
1477 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
1481 p
->policyvers
= le32_to_cpu(buf
[0]);
1482 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
1483 p
->policyvers
> POLICYDB_VERSION_MAX
) {
1484 printk(KERN_ERR
"security: policydb version %d does not match "
1485 "my version range %d-%d\n",
1486 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
1490 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
1491 if (ss_initialized
&& !selinux_mls_enabled
) {
1492 printk(KERN_ERR
"Cannot switch between non-MLS and MLS "
1496 selinux_mls_enabled
= 1;
1497 config
|= POLICYDB_CONFIG_MLS
;
1499 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
1500 printk(KERN_ERR
"security policydb version %d (MLS) "
1501 "not backwards compatible\n", p
->policyvers
);
1505 if (ss_initialized
&& selinux_mls_enabled
) {
1506 printk(KERN_ERR
"Cannot switch between MLS and non-MLS "
1512 info
= policydb_lookup_compat(p
->policyvers
);
1514 printk(KERN_ERR
"security: unable to find policy compat info "
1515 "for version %d\n", p
->policyvers
);
1519 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
1520 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
1521 printk(KERN_ERR
"security: policydb table sizes (%d,%d) do "
1522 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
1523 le32_to_cpu(buf
[3]),
1524 info
->sym_num
, info
->ocon_num
);
1528 for (i
= 0; i
< info
->sym_num
; i
++) {
1529 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1532 nprim
= le32_to_cpu(buf
[0]);
1533 nel
= le32_to_cpu(buf
[1]);
1534 for (j
= 0; j
< nel
; j
++) {
1535 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
1540 p
->symtab
[i
].nprim
= nprim
;
1543 rc
= avtab_read(&p
->te_avtab
, fp
, p
->policyvers
);
1547 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
1548 rc
= cond_read_list(p
, fp
);
1553 rc
= next_entry(buf
, fp
, sizeof(u32
));
1556 nel
= le32_to_cpu(buf
[0]);
1558 for (i
= 0; i
< nel
; i
++) {
1559 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
1569 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1572 tr
->role
= le32_to_cpu(buf
[0]);
1573 tr
->type
= le32_to_cpu(buf
[1]);
1574 tr
->new_role
= le32_to_cpu(buf
[2]);
1578 rc
= next_entry(buf
, fp
, sizeof(u32
));
1581 nel
= le32_to_cpu(buf
[0]);
1583 for (i
= 0; i
< nel
; i
++) {
1584 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
1594 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1597 ra
->role
= le32_to_cpu(buf
[0]);
1598 ra
->new_role
= le32_to_cpu(buf
[1]);
1602 rc
= policydb_index_classes(p
);
1606 rc
= policydb_index_others(p
);
1610 for (i
= 0; i
< info
->ocon_num
; i
++) {
1611 rc
= next_entry(buf
, fp
, sizeof(u32
));
1614 nel
= le32_to_cpu(buf
[0]);
1616 for (j
= 0; j
< nel
; j
++) {
1617 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1625 p
->ocontexts
[i
] = c
;
1631 rc
= next_entry(buf
, fp
, sizeof(u32
));
1634 c
->sid
[0] = le32_to_cpu(buf
[0]);
1635 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1641 rc
= next_entry(buf
, fp
, sizeof(u32
));
1644 len
= le32_to_cpu(buf
[0]);
1645 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1650 rc
= next_entry(c
->u
.name
, fp
, len
);
1654 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1657 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
1662 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1665 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
1666 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
1667 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
1668 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1673 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1676 c
->u
.node
.addr
= le32_to_cpu(buf
[0]);
1677 c
->u
.node
.mask
= le32_to_cpu(buf
[1]);
1678 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1683 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1686 c
->v
.behavior
= le32_to_cpu(buf
[0]);
1687 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
1689 len
= le32_to_cpu(buf
[1]);
1690 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1695 rc
= next_entry(c
->u
.name
, fp
, len
);
1699 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1706 rc
= next_entry(buf
, fp
, sizeof(u32
) * 8);
1709 for (k
= 0; k
< 4; k
++)
1710 c
->u
.node6
.addr
[k
] = le32_to_cpu(buf
[k
]);
1711 for (k
= 0; k
< 4; k
++)
1712 c
->u
.node6
.mask
[k
] = le32_to_cpu(buf
[k
+4]);
1713 if (context_read_and_validate(&c
->context
[0], p
, fp
))
1721 rc
= next_entry(buf
, fp
, sizeof(u32
));
1724 nel
= le32_to_cpu(buf
[0]);
1727 for (i
= 0; i
< nel
; i
++) {
1728 rc
= next_entry(buf
, fp
, sizeof(u32
));
1731 len
= le32_to_cpu(buf
[0]);
1732 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1738 newgenfs
->fstype
= kmalloc(len
+ 1,GFP_KERNEL
);
1739 if (!newgenfs
->fstype
) {
1744 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
1746 kfree(newgenfs
->fstype
);
1750 newgenfs
->fstype
[len
] = 0;
1751 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1752 genfs_p
= genfs
, genfs
= genfs
->next
) {
1753 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1754 printk(KERN_ERR
"security: dup genfs "
1755 "fstype %s\n", newgenfs
->fstype
);
1756 kfree(newgenfs
->fstype
);
1760 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1763 newgenfs
->next
= genfs
;
1765 genfs_p
->next
= newgenfs
;
1767 p
->genfs
= newgenfs
;
1768 rc
= next_entry(buf
, fp
, sizeof(u32
));
1771 nel2
= le32_to_cpu(buf
[0]);
1772 for (j
= 0; j
< nel2
; j
++) {
1773 rc
= next_entry(buf
, fp
, sizeof(u32
));
1776 len
= le32_to_cpu(buf
[0]);
1778 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
1784 newc
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1785 if (!newc
->u
.name
) {
1789 rc
= next_entry(newc
->u
.name
, fp
, len
);
1792 newc
->u
.name
[len
] = 0;
1793 rc
= next_entry(buf
, fp
, sizeof(u32
));
1796 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
1797 if (context_read_and_validate(&newc
->context
[0], p
, fp
))
1799 for (l
= NULL
, c
= newgenfs
->head
; c
;
1800 l
= c
, c
= c
->next
) {
1801 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
1802 (!c
->v
.sclass
|| !newc
->v
.sclass
||
1803 newc
->v
.sclass
== c
->v
.sclass
)) {
1804 printk(KERN_ERR
"security: dup genfs "
1806 newgenfs
->fstype
, c
->u
.name
);
1809 len
= strlen(newc
->u
.name
);
1810 len2
= strlen(c
->u
.name
);
1819 newgenfs
->head
= newc
;
1823 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1824 rc
= next_entry(buf
, fp
, sizeof(u32
));
1827 nel
= le32_to_cpu(buf
[0]);
1829 for (i
= 0; i
< nel
; i
++) {
1830 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1839 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1842 rt
->dom
= le32_to_cpu(buf
[0]);
1843 rt
->type
= le32_to_cpu(buf
[1]);
1844 rc
= mls_read_range_helper(&rt
->range
, fp
);
1851 p
->type_attr_map
= kmalloc(p
->p_types
.nprim
*sizeof(struct ebitmap
), GFP_KERNEL
);
1852 if (!p
->type_attr_map
)
1855 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
1856 ebitmap_init(&p
->type_attr_map
[i
]);
1857 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
1858 if (ebitmap_read(&p
->type_attr_map
[i
], fp
))
1861 /* add the type itself as the degenerate case */
1862 if (ebitmap_set_bit(&p
->type_attr_map
[i
], i
, 1))
1870 ocontext_destroy(newc
,OCON_FSUSE
);
1874 policydb_destroy(p
);