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
,
100 .version
= POLICYDB_VERSION_RANGETRANS
,
102 .ocon_num
= OCON_NUM
,
106 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
109 struct policydb_compat_info
*info
= NULL
;
111 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
112 if (policydb_compat
[i
].version
== version
) {
113 info
= &policydb_compat
[i
];
121 * Initialize the role table.
123 static int roles_init(struct policydb
*p
)
127 struct role_datum
*role
;
129 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
134 role
->value
= ++p
->p_roles
.nprim
;
135 if (role
->value
!= OBJECT_R_VAL
) {
139 key
= kmalloc(strlen(OBJECT_R
)+1,GFP_KERNEL
);
144 strcpy(key
, OBJECT_R
);
145 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
159 * Initialize a policy database structure.
161 static int policydb_init(struct policydb
*p
)
165 memset(p
, 0, sizeof(*p
));
167 for (i
= 0; i
< SYM_NUM
; i
++) {
168 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
170 goto out_free_symtab
;
173 rc
= avtab_init(&p
->te_avtab
);
175 goto out_free_symtab
;
181 rc
= cond_policydb_init(p
);
189 avtab_destroy(&p
->te_avtab
);
192 for (i
= 0; i
< SYM_NUM
; i
++)
193 hashtab_destroy(p
->symtab
[i
].table
);
198 * The following *_index functions are used to
199 * define the val_to_name and val_to_struct arrays
200 * in a policy database structure. The val_to_name
201 * arrays are used when converting security context
202 * structures into string representations. The
203 * val_to_struct arrays are used when the attributes
204 * of a class, role, or user are needed.
207 static int common_index(void *key
, void *datum
, void *datap
)
210 struct common_datum
*comdatum
;
214 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
216 p
->p_common_val_to_name
[comdatum
->value
- 1] = key
;
220 static int class_index(void *key
, void *datum
, void *datap
)
223 struct class_datum
*cladatum
;
227 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
229 p
->p_class_val_to_name
[cladatum
->value
- 1] = key
;
230 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
234 static int role_index(void *key
, void *datum
, void *datap
)
237 struct role_datum
*role
;
241 if (!role
->value
|| role
->value
> p
->p_roles
.nprim
)
243 p
->p_role_val_to_name
[role
->value
- 1] = key
;
244 p
->role_val_to_struct
[role
->value
- 1] = role
;
248 static int type_index(void *key
, void *datum
, void *datap
)
251 struct type_datum
*typdatum
;
256 if (typdatum
->primary
) {
257 if (!typdatum
->value
|| typdatum
->value
> p
->p_types
.nprim
)
259 p
->p_type_val_to_name
[typdatum
->value
- 1] = key
;
265 static int user_index(void *key
, void *datum
, void *datap
)
268 struct user_datum
*usrdatum
;
272 if (!usrdatum
->value
|| usrdatum
->value
> p
->p_users
.nprim
)
274 p
->p_user_val_to_name
[usrdatum
->value
- 1] = key
;
275 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
279 static int sens_index(void *key
, void *datum
, void *datap
)
282 struct level_datum
*levdatum
;
287 if (!levdatum
->isalias
) {
288 if (!levdatum
->level
->sens
||
289 levdatum
->level
->sens
> p
->p_levels
.nprim
)
291 p
->p_sens_val_to_name
[levdatum
->level
->sens
- 1] = key
;
297 static int cat_index(void *key
, void *datum
, void *datap
)
300 struct cat_datum
*catdatum
;
305 if (!catdatum
->isalias
) {
306 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
308 p
->p_cat_val_to_name
[catdatum
->value
- 1] = key
;
314 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
327 * Define the common val_to_name array and the class
328 * val_to_name and val_to_struct arrays in a policy
329 * database structure.
331 * Caller must clean up upon failure.
333 static int policydb_index_classes(struct policydb
*p
)
337 p
->p_common_val_to_name
=
338 kmalloc(p
->p_commons
.nprim
* sizeof(char *), GFP_KERNEL
);
339 if (!p
->p_common_val_to_name
) {
344 rc
= hashtab_map(p
->p_commons
.table
, common_index
, p
);
348 p
->class_val_to_struct
=
349 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)), GFP_KERNEL
);
350 if (!p
->class_val_to_struct
) {
355 p
->p_class_val_to_name
=
356 kmalloc(p
->p_classes
.nprim
* sizeof(char *), GFP_KERNEL
);
357 if (!p
->p_class_val_to_name
) {
362 rc
= hashtab_map(p
->p_classes
.table
, class_index
, p
);
368 static void symtab_hash_eval(struct symtab
*s
)
372 for (i
= 0; i
< SYM_NUM
; i
++) {
373 struct hashtab
*h
= s
[i
].table
;
374 struct hashtab_info info
;
376 hashtab_stat(h
, &info
);
377 printk(KERN_INFO
"%s: %d entries and %d/%d buckets used, "
378 "longest chain length %d\n", symtab_name
[i
], h
->nel
,
379 info
.slots_used
, h
->size
, info
.max_chain_len
);
385 * Define the other val_to_name and val_to_struct arrays
386 * in a policy database structure.
388 * Caller must clean up on failure.
390 static int policydb_index_others(struct policydb
*p
)
394 printk(KERN_INFO
"security: %d users, %d roles, %d types, %d bools",
395 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
396 if (selinux_mls_enabled
)
397 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
401 printk(KERN_INFO
"security: %d classes, %d rules\n",
402 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
405 avtab_hash_eval(&p
->te_avtab
, "rules");
406 symtab_hash_eval(p
->symtab
);
409 p
->role_val_to_struct
=
410 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
412 if (!p
->role_val_to_struct
) {
417 p
->user_val_to_struct
=
418 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
420 if (!p
->user_val_to_struct
) {
425 if (cond_init_bool_indexes(p
)) {
430 for (i
= SYM_ROLES
; i
< SYM_NUM
; i
++) {
431 p
->sym_val_to_name
[i
] =
432 kmalloc(p
->symtab
[i
].nprim
* sizeof(char *), GFP_KERNEL
);
433 if (!p
->sym_val_to_name
[i
]) {
437 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
447 * The following *_destroy functions are used to
448 * free any memory allocated for each kind of
449 * symbol data in the policy database.
452 static int perm_destroy(void *key
, void *datum
, void *p
)
459 static int common_destroy(void *key
, void *datum
, void *p
)
461 struct common_datum
*comdatum
;
465 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
466 hashtab_destroy(comdatum
->permissions
.table
);
471 static int cls_destroy(void *key
, void *datum
, void *p
)
473 struct class_datum
*cladatum
;
474 struct constraint_node
*constraint
, *ctemp
;
475 struct constraint_expr
*e
, *etmp
;
479 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
480 hashtab_destroy(cladatum
->permissions
.table
);
481 constraint
= cladatum
->constraints
;
483 e
= constraint
->expr
;
485 ebitmap_destroy(&e
->names
);
491 constraint
= constraint
->next
;
495 constraint
= cladatum
->validatetrans
;
497 e
= constraint
->expr
;
499 ebitmap_destroy(&e
->names
);
505 constraint
= constraint
->next
;
509 kfree(cladatum
->comkey
);
514 static int role_destroy(void *key
, void *datum
, void *p
)
516 struct role_datum
*role
;
520 ebitmap_destroy(&role
->dominates
);
521 ebitmap_destroy(&role
->types
);
526 static int type_destroy(void *key
, void *datum
, void *p
)
533 static int user_destroy(void *key
, void *datum
, void *p
)
535 struct user_datum
*usrdatum
;
539 ebitmap_destroy(&usrdatum
->roles
);
540 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
541 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
542 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
547 static int sens_destroy(void *key
, void *datum
, void *p
)
549 struct level_datum
*levdatum
;
553 ebitmap_destroy(&levdatum
->level
->cat
);
554 kfree(levdatum
->level
);
559 static int cat_destroy(void *key
, void *datum
, void *p
)
566 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
578 static void ocontext_destroy(struct ocontext
*c
, int i
)
580 context_destroy(&c
->context
[0]);
581 context_destroy(&c
->context
[1]);
582 if (i
== OCON_ISID
|| i
== OCON_FS
||
583 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
589 * Free any memory allocated by a policy database structure.
591 void policydb_destroy(struct policydb
*p
)
593 struct ocontext
*c
, *ctmp
;
594 struct genfs
*g
, *gtmp
;
596 struct role_allow
*ra
, *lra
= NULL
;
597 struct role_trans
*tr
, *ltr
= NULL
;
598 struct range_trans
*rt
, *lrt
= NULL
;
600 for (i
= 0; i
< SYM_NUM
; i
++) {
601 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
602 hashtab_destroy(p
->symtab
[i
].table
);
605 for (i
= 0; i
< SYM_NUM
; i
++)
606 kfree(p
->sym_val_to_name
[i
]);
608 kfree(p
->class_val_to_struct
);
609 kfree(p
->role_val_to_struct
);
610 kfree(p
->user_val_to_struct
);
612 avtab_destroy(&p
->te_avtab
);
614 for (i
= 0; i
< OCON_NUM
; i
++) {
619 ocontext_destroy(ctmp
,i
);
621 p
->ocontexts
[i
] = NULL
;
631 ocontext_destroy(ctmp
,OCON_FSUSE
);
639 cond_policydb_destroy(p
);
641 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
647 for (ra
= p
->role_allow
; ra
; ra
= ra
-> next
) {
653 for (rt
= p
->range_tr
; rt
; rt
= rt
-> next
) {
655 ebitmap_destroy(&lrt
->target_range
.level
[0].cat
);
656 ebitmap_destroy(&lrt
->target_range
.level
[1].cat
);
662 ebitmap_destroy(&lrt
->target_range
.level
[0].cat
);
663 ebitmap_destroy(&lrt
->target_range
.level
[1].cat
);
667 if (p
->type_attr_map
) {
668 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
669 ebitmap_destroy(&p
->type_attr_map
[i
]);
671 kfree(p
->type_attr_map
);
677 * Load the initial SIDs specified in a policy database
678 * structure into a SID table.
680 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
682 struct ocontext
*head
, *c
;
687 printk(KERN_ERR
"security: out of memory on SID table init\n");
691 head
= p
->ocontexts
[OCON_ISID
];
692 for (c
= head
; c
; c
= c
->next
) {
693 if (!c
->context
[0].user
) {
694 printk(KERN_ERR
"security: SID %s was never "
695 "defined.\n", c
->u
.name
);
699 if (sidtab_insert(s
, c
->sid
[0], &c
->context
[0])) {
700 printk(KERN_ERR
"security: unable to load initial "
701 "SID %s.\n", c
->u
.name
);
711 * Return 1 if the fields in the security context
712 * structure `c' are valid. Return 0 otherwise.
714 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
716 struct role_datum
*role
;
717 struct user_datum
*usrdatum
;
719 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
722 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
725 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
728 if (c
->role
!= OBJECT_R_VAL
) {
730 * Role must be authorized for the type.
732 role
= p
->role_val_to_struct
[c
->role
- 1];
733 if (!ebitmap_get_bit(&role
->types
,
735 /* role may not be associated with type */
739 * User must be authorized for the role.
741 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
745 if (!ebitmap_get_bit(&usrdatum
->roles
,
747 /* user may not be associated with role */
751 if (!mls_context_isvalid(p
, c
))
758 * Read a MLS range structure from a policydb binary
759 * representation file.
761 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
767 rc
= next_entry(buf
, fp
, sizeof(u32
));
771 items
= le32_to_cpu(buf
[0]);
772 if (items
> ARRAY_SIZE(buf
)) {
773 printk(KERN_ERR
"security: mls: range overflow\n");
777 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
779 printk(KERN_ERR
"security: mls: truncated range\n");
782 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
784 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
786 r
->level
[1].sens
= r
->level
[0].sens
;
788 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
790 printk(KERN_ERR
"security: mls: error reading low "
795 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
797 printk(KERN_ERR
"security: mls: error reading high "
802 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
804 printk(KERN_ERR
"security: mls: out of memory\n");
813 ebitmap_destroy(&r
->level
[0].cat
);
818 * Read and validate a security context structure
819 * from a policydb binary representation file.
821 static int context_read_and_validate(struct context
*c
,
828 rc
= next_entry(buf
, fp
, sizeof buf
);
830 printk(KERN_ERR
"security: context truncated\n");
833 c
->user
= le32_to_cpu(buf
[0]);
834 c
->role
= le32_to_cpu(buf
[1]);
835 c
->type
= le32_to_cpu(buf
[2]);
836 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
837 if (mls_read_range_helper(&c
->range
, fp
)) {
838 printk(KERN_ERR
"security: error reading MLS range of "
845 if (!policydb_context_isvalid(p
, c
)) {
846 printk(KERN_ERR
"security: invalid security context\n");
855 * The following *_read functions are used to
856 * read the symbol data from a policy database
857 * binary representation file.
860 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
863 struct perm_datum
*perdatum
;
868 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
874 rc
= next_entry(buf
, fp
, sizeof buf
);
878 len
= le32_to_cpu(buf
[0]);
879 perdatum
->value
= le32_to_cpu(buf
[1]);
881 key
= kmalloc(len
+ 1,GFP_KERNEL
);
886 rc
= next_entry(key
, fp
, len
);
891 rc
= hashtab_insert(h
, key
, perdatum
);
897 perm_destroy(key
, perdatum
, NULL
);
901 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
904 struct common_datum
*comdatum
;
909 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
915 rc
= next_entry(buf
, fp
, sizeof buf
);
919 len
= le32_to_cpu(buf
[0]);
920 comdatum
->value
= le32_to_cpu(buf
[1]);
922 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
925 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
926 nel
= le32_to_cpu(buf
[3]);
928 key
= kmalloc(len
+ 1,GFP_KERNEL
);
933 rc
= next_entry(key
, fp
, len
);
938 for (i
= 0; i
< nel
; i
++) {
939 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
944 rc
= hashtab_insert(h
, key
, comdatum
);
950 common_destroy(key
, comdatum
, NULL
);
954 static int read_cons_helper(struct constraint_node
**nodep
, int ncons
,
955 int allowxtarget
, void *fp
)
957 struct constraint_node
*c
, *lc
;
958 struct constraint_expr
*e
, *le
;
964 for (i
= 0; i
< ncons
; i
++) {
965 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
975 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
978 c
->permissions
= le32_to_cpu(buf
[0]);
979 nexpr
= le32_to_cpu(buf
[1]);
982 for (j
= 0; j
< nexpr
; j
++) {
983 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
993 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
996 e
->expr_type
= le32_to_cpu(buf
[0]);
997 e
->attr
= le32_to_cpu(buf
[1]);
998 e
->op
= le32_to_cpu(buf
[2]);
1000 switch (e
->expr_type
) {
1012 if (depth
== (CEXPR_MAXDEPTH
- 1))
1017 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1019 if (depth
== (CEXPR_MAXDEPTH
- 1))
1022 if (ebitmap_read(&e
->names
, fp
))
1038 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1041 struct class_datum
*cladatum
;
1043 u32 len
, len2
, ncons
, nel
;
1046 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1052 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1056 len
= le32_to_cpu(buf
[0]);
1057 len2
= le32_to_cpu(buf
[1]);
1058 cladatum
->value
= le32_to_cpu(buf
[2]);
1060 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1063 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1064 nel
= le32_to_cpu(buf
[4]);
1066 ncons
= le32_to_cpu(buf
[5]);
1068 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1073 rc
= next_entry(key
, fp
, len
);
1079 cladatum
->comkey
= kmalloc(len2
+ 1,GFP_KERNEL
);
1080 if (!cladatum
->comkey
) {
1084 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1087 cladatum
->comkey
[len2
] = 0;
1089 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
,
1091 if (!cladatum
->comdatum
) {
1092 printk(KERN_ERR
"security: unknown common %s\n",
1098 for (i
= 0; i
< nel
; i
++) {
1099 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1104 rc
= read_cons_helper(&cladatum
->constraints
, ncons
, 0, fp
);
1108 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1109 /* grab the validatetrans rules */
1110 rc
= next_entry(buf
, fp
, sizeof(u32
));
1113 ncons
= le32_to_cpu(buf
[0]);
1114 rc
= read_cons_helper(&cladatum
->validatetrans
, ncons
, 1, fp
);
1119 rc
= hashtab_insert(h
, key
, cladatum
);
1127 cls_destroy(key
, cladatum
, NULL
);
1131 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1134 struct role_datum
*role
;
1139 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1145 rc
= next_entry(buf
, fp
, sizeof buf
);
1149 len
= le32_to_cpu(buf
[0]);
1150 role
->value
= le32_to_cpu(buf
[1]);
1152 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1157 rc
= next_entry(key
, fp
, len
);
1162 rc
= ebitmap_read(&role
->dominates
, fp
);
1166 rc
= ebitmap_read(&role
->types
, fp
);
1170 if (strcmp(key
, OBJECT_R
) == 0) {
1171 if (role
->value
!= OBJECT_R_VAL
) {
1172 printk(KERN_ERR
"Role %s has wrong value %d\n",
1173 OBJECT_R
, role
->value
);
1181 rc
= hashtab_insert(h
, key
, role
);
1187 role_destroy(key
, role
, NULL
);
1191 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1194 struct type_datum
*typdatum
;
1199 typdatum
= kzalloc(sizeof(*typdatum
),GFP_KERNEL
);
1205 rc
= next_entry(buf
, fp
, sizeof buf
);
1209 len
= le32_to_cpu(buf
[0]);
1210 typdatum
->value
= le32_to_cpu(buf
[1]);
1211 typdatum
->primary
= le32_to_cpu(buf
[2]);
1213 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1218 rc
= next_entry(key
, fp
, len
);
1223 rc
= hashtab_insert(h
, key
, typdatum
);
1229 type_destroy(key
, typdatum
, NULL
);
1235 * Read a MLS level structure from a policydb binary
1236 * representation file.
1238 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1243 memset(lp
, 0, sizeof(*lp
));
1245 rc
= next_entry(buf
, fp
, sizeof buf
);
1247 printk(KERN_ERR
"security: mls: truncated level\n");
1250 lp
->sens
= le32_to_cpu(buf
[0]);
1252 if (ebitmap_read(&lp
->cat
, fp
)) {
1253 printk(KERN_ERR
"security: mls: error reading level "
1263 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1266 struct user_datum
*usrdatum
;
1271 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1277 rc
= next_entry(buf
, fp
, sizeof buf
);
1281 len
= le32_to_cpu(buf
[0]);
1282 usrdatum
->value
= le32_to_cpu(buf
[1]);
1284 key
= kmalloc(len
+ 1,GFP_KERNEL
);
1289 rc
= next_entry(key
, fp
, len
);
1294 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1298 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1299 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1302 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1307 rc
= hashtab_insert(h
, key
, usrdatum
);
1313 user_destroy(key
, usrdatum
, NULL
);
1317 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1320 struct level_datum
*levdatum
;
1325 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1331 rc
= next_entry(buf
, fp
, sizeof buf
);
1335 len
= le32_to_cpu(buf
[0]);
1336 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1338 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1343 rc
= next_entry(key
, fp
, len
);
1348 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1349 if (!levdatum
->level
) {
1353 if (mls_read_level(levdatum
->level
, fp
)) {
1358 rc
= hashtab_insert(h
, key
, levdatum
);
1364 sens_destroy(key
, levdatum
, NULL
);
1368 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1371 struct cat_datum
*catdatum
;
1376 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1382 rc
= next_entry(buf
, fp
, sizeof buf
);
1386 len
= le32_to_cpu(buf
[0]);
1387 catdatum
->value
= le32_to_cpu(buf
[1]);
1388 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1390 key
= kmalloc(len
+ 1,GFP_ATOMIC
);
1395 rc
= next_entry(key
, fp
, len
);
1400 rc
= hashtab_insert(h
, key
, catdatum
);
1407 cat_destroy(key
, catdatum
, NULL
);
1411 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1423 extern int ss_initialized
;
1426 * Read the configuration data from a policy database binary
1427 * representation file into a policy database structure.
1429 int policydb_read(struct policydb
*p
, void *fp
)
1431 struct role_allow
*ra
, *lra
;
1432 struct role_trans
*tr
, *ltr
;
1433 struct ocontext
*l
, *c
, *newc
;
1434 struct genfs
*genfs_p
, *genfs
, *newgenfs
;
1437 u32 len
, len2
, config
, nprim
, nel
, nel2
;
1439 struct policydb_compat_info
*info
;
1440 struct range_trans
*rt
, *lrt
;
1444 rc
= policydb_init(p
);
1448 /* Read the magic number and string length. */
1449 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1453 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
1454 printk(KERN_ERR
"security: policydb magic number 0x%x does "
1455 "not match expected magic number 0x%x\n",
1456 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
1460 len
= le32_to_cpu(buf
[1]);
1461 if (len
!= strlen(POLICYDB_STRING
)) {
1462 printk(KERN_ERR
"security: policydb string length %d does not "
1463 "match expected length %Zu\n",
1464 len
, strlen(POLICYDB_STRING
));
1467 policydb_str
= kmalloc(len
+ 1,GFP_KERNEL
);
1468 if (!policydb_str
) {
1469 printk(KERN_ERR
"security: unable to allocate memory for policydb "
1470 "string of length %d\n", len
);
1474 rc
= next_entry(policydb_str
, fp
, len
);
1476 printk(KERN_ERR
"security: truncated policydb string identifier\n");
1477 kfree(policydb_str
);
1480 policydb_str
[len
] = 0;
1481 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
1482 printk(KERN_ERR
"security: policydb string %s does not match "
1483 "my string %s\n", policydb_str
, POLICYDB_STRING
);
1484 kfree(policydb_str
);
1487 /* Done with policydb_str. */
1488 kfree(policydb_str
);
1489 policydb_str
= NULL
;
1491 /* Read the version, config, and table sizes. */
1492 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
1496 p
->policyvers
= le32_to_cpu(buf
[0]);
1497 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
1498 p
->policyvers
> POLICYDB_VERSION_MAX
) {
1499 printk(KERN_ERR
"security: policydb version %d does not match "
1500 "my version range %d-%d\n",
1501 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
1505 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
1506 if (ss_initialized
&& !selinux_mls_enabled
) {
1507 printk(KERN_ERR
"Cannot switch between non-MLS and MLS "
1511 selinux_mls_enabled
= 1;
1512 config
|= POLICYDB_CONFIG_MLS
;
1514 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
1515 printk(KERN_ERR
"security policydb version %d (MLS) "
1516 "not backwards compatible\n", p
->policyvers
);
1520 if (ss_initialized
&& selinux_mls_enabled
) {
1521 printk(KERN_ERR
"Cannot switch between MLS and non-MLS "
1527 info
= policydb_lookup_compat(p
->policyvers
);
1529 printk(KERN_ERR
"security: unable to find policy compat info "
1530 "for version %d\n", p
->policyvers
);
1534 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
1535 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
1536 printk(KERN_ERR
"security: policydb table sizes (%d,%d) do "
1537 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
1538 le32_to_cpu(buf
[3]),
1539 info
->sym_num
, info
->ocon_num
);
1543 for (i
= 0; i
< info
->sym_num
; i
++) {
1544 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1547 nprim
= le32_to_cpu(buf
[0]);
1548 nel
= le32_to_cpu(buf
[1]);
1549 for (j
= 0; j
< nel
; j
++) {
1550 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
1555 p
->symtab
[i
].nprim
= nprim
;
1558 rc
= avtab_read(&p
->te_avtab
, fp
, p
->policyvers
);
1562 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
1563 rc
= cond_read_list(p
, fp
);
1568 rc
= next_entry(buf
, fp
, sizeof(u32
));
1571 nel
= le32_to_cpu(buf
[0]);
1573 for (i
= 0; i
< nel
; i
++) {
1574 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
1584 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1587 tr
->role
= le32_to_cpu(buf
[0]);
1588 tr
->type
= le32_to_cpu(buf
[1]);
1589 tr
->new_role
= le32_to_cpu(buf
[2]);
1593 rc
= next_entry(buf
, fp
, sizeof(u32
));
1596 nel
= le32_to_cpu(buf
[0]);
1598 for (i
= 0; i
< nel
; i
++) {
1599 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
1609 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1612 ra
->role
= le32_to_cpu(buf
[0]);
1613 ra
->new_role
= le32_to_cpu(buf
[1]);
1617 rc
= policydb_index_classes(p
);
1621 rc
= policydb_index_others(p
);
1625 for (i
= 0; i
< info
->ocon_num
; i
++) {
1626 rc
= next_entry(buf
, fp
, sizeof(u32
));
1629 nel
= le32_to_cpu(buf
[0]);
1631 for (j
= 0; j
< nel
; j
++) {
1632 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1640 p
->ocontexts
[i
] = c
;
1646 rc
= next_entry(buf
, fp
, sizeof(u32
));
1649 c
->sid
[0] = le32_to_cpu(buf
[0]);
1650 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1656 rc
= next_entry(buf
, fp
, sizeof(u32
));
1659 len
= le32_to_cpu(buf
[0]);
1660 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1665 rc
= next_entry(c
->u
.name
, fp
, len
);
1669 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1672 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
1677 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1680 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
1681 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
1682 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
1683 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1688 rc
= next_entry(buf
, fp
, sizeof(u32
)* 2);
1691 c
->u
.node
.addr
= le32_to_cpu(buf
[0]);
1692 c
->u
.node
.mask
= le32_to_cpu(buf
[1]);
1693 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1698 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
1701 c
->v
.behavior
= le32_to_cpu(buf
[0]);
1702 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
1704 len
= le32_to_cpu(buf
[1]);
1705 c
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1710 rc
= next_entry(c
->u
.name
, fp
, len
);
1714 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1721 rc
= next_entry(buf
, fp
, sizeof(u32
) * 8);
1724 for (k
= 0; k
< 4; k
++)
1725 c
->u
.node6
.addr
[k
] = le32_to_cpu(buf
[k
]);
1726 for (k
= 0; k
< 4; k
++)
1727 c
->u
.node6
.mask
[k
] = le32_to_cpu(buf
[k
+4]);
1728 if (context_read_and_validate(&c
->context
[0], p
, fp
))
1736 rc
= next_entry(buf
, fp
, sizeof(u32
));
1739 nel
= le32_to_cpu(buf
[0]);
1742 for (i
= 0; i
< nel
; i
++) {
1743 rc
= next_entry(buf
, fp
, sizeof(u32
));
1746 len
= le32_to_cpu(buf
[0]);
1747 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1753 newgenfs
->fstype
= kmalloc(len
+ 1,GFP_KERNEL
);
1754 if (!newgenfs
->fstype
) {
1759 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
1761 kfree(newgenfs
->fstype
);
1765 newgenfs
->fstype
[len
] = 0;
1766 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1767 genfs_p
= genfs
, genfs
= genfs
->next
) {
1768 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1769 printk(KERN_ERR
"security: dup genfs "
1770 "fstype %s\n", newgenfs
->fstype
);
1771 kfree(newgenfs
->fstype
);
1775 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1778 newgenfs
->next
= genfs
;
1780 genfs_p
->next
= newgenfs
;
1782 p
->genfs
= newgenfs
;
1783 rc
= next_entry(buf
, fp
, sizeof(u32
));
1786 nel2
= le32_to_cpu(buf
[0]);
1787 for (j
= 0; j
< nel2
; j
++) {
1788 rc
= next_entry(buf
, fp
, sizeof(u32
));
1791 len
= le32_to_cpu(buf
[0]);
1793 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
1799 newc
->u
.name
= kmalloc(len
+ 1,GFP_KERNEL
);
1800 if (!newc
->u
.name
) {
1804 rc
= next_entry(newc
->u
.name
, fp
, len
);
1807 newc
->u
.name
[len
] = 0;
1808 rc
= next_entry(buf
, fp
, sizeof(u32
));
1811 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
1812 if (context_read_and_validate(&newc
->context
[0], p
, fp
))
1814 for (l
= NULL
, c
= newgenfs
->head
; c
;
1815 l
= c
, c
= c
->next
) {
1816 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
1817 (!c
->v
.sclass
|| !newc
->v
.sclass
||
1818 newc
->v
.sclass
== c
->v
.sclass
)) {
1819 printk(KERN_ERR
"security: dup genfs "
1821 newgenfs
->fstype
, c
->u
.name
);
1824 len
= strlen(newc
->u
.name
);
1825 len2
= strlen(c
->u
.name
);
1834 newgenfs
->head
= newc
;
1838 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1839 int new_rangetr
= p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
;
1840 rc
= next_entry(buf
, fp
, sizeof(u32
));
1843 nel
= le32_to_cpu(buf
[0]);
1845 for (i
= 0; i
< nel
; i
++) {
1846 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1855 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1858 rt
->source_type
= le32_to_cpu(buf
[0]);
1859 rt
->target_type
= le32_to_cpu(buf
[1]);
1861 rc
= next_entry(buf
, fp
, sizeof(u32
));
1864 rt
->target_class
= le32_to_cpu(buf
[0]);
1866 rt
->target_class
= SECCLASS_PROCESS
;
1867 rc
= mls_read_range_helper(&rt
->target_range
, fp
);
1874 p
->type_attr_map
= kmalloc(p
->p_types
.nprim
*sizeof(struct ebitmap
), GFP_KERNEL
);
1875 if (!p
->type_attr_map
)
1878 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
1879 ebitmap_init(&p
->type_attr_map
[i
]);
1880 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
1881 if (ebitmap_read(&p
->type_attr_map
[i
], fp
))
1884 /* add the type itself as the degenerate case */
1885 if (ebitmap_set_bit(&p
->type_attr_map
[i
], i
, 1))
1893 ocontext_destroy(newc
,OCON_FSUSE
);
1897 policydb_destroy(p
);