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.moore@hp.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
,
128 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
131 struct policydb_compat_info
*info
= NULL
;
133 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
134 if (policydb_compat
[i
].version
== version
) {
135 info
= &policydb_compat
[i
];
143 * Initialize the role table.
145 static int roles_init(struct policydb
*p
)
149 struct role_datum
*role
;
152 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
157 role
->value
= ++p
->p_roles
.nprim
;
158 if (role
->value
!= OBJECT_R_VAL
)
162 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
166 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
177 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
179 const struct range_trans
*key
= k
;
180 return (key
->source_type
+ (key
->target_type
<< 3) +
181 (key
->target_class
<< 5)) & (h
->size
- 1);
184 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
186 const struct range_trans
*key1
= k1
, *key2
= k2
;
189 v
= key1
->source_type
- key2
->source_type
;
193 v
= key1
->target_type
- key2
->target_type
;
197 v
= key1
->target_class
- key2
->target_class
;
203 * Initialize a policy database structure.
205 static int policydb_init(struct policydb
*p
)
209 memset(p
, 0, sizeof(*p
));
211 for (i
= 0; i
< SYM_NUM
; i
++) {
212 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
217 rc
= avtab_init(&p
->te_avtab
);
225 rc
= cond_policydb_init(p
);
229 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
233 ebitmap_init(&p
->policycaps
);
234 ebitmap_init(&p
->permissive_map
);
238 for (i
= 0; i
< SYM_NUM
; i
++)
239 hashtab_destroy(p
->symtab
[i
].table
);
244 * The following *_index functions are used to
245 * define the val_to_name and val_to_struct arrays
246 * in a policy database structure. The val_to_name
247 * arrays are used when converting security context
248 * structures into string representations. The
249 * val_to_struct arrays are used when the attributes
250 * of a class, role, or user are needed.
253 static int common_index(void *key
, void *datum
, void *datap
)
256 struct common_datum
*comdatum
;
257 struct flex_array
*fa
;
261 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
264 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
265 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
266 GFP_KERNEL
| __GFP_ZERO
))
271 static int class_index(void *key
, void *datum
, void *datap
)
274 struct class_datum
*cladatum
;
275 struct flex_array
*fa
;
279 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
281 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
282 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
283 GFP_KERNEL
| __GFP_ZERO
))
285 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
289 static int role_index(void *key
, void *datum
, void *datap
)
292 struct role_datum
*role
;
293 struct flex_array
*fa
;
298 || role
->value
> p
->p_roles
.nprim
299 || role
->bounds
> p
->p_roles
.nprim
)
302 fa
= p
->sym_val_to_name
[SYM_ROLES
];
303 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
304 GFP_KERNEL
| __GFP_ZERO
))
306 p
->role_val_to_struct
[role
->value
- 1] = role
;
310 static int type_index(void *key
, void *datum
, void *datap
)
313 struct type_datum
*typdatum
;
314 struct flex_array
*fa
;
319 if (typdatum
->primary
) {
321 || typdatum
->value
> p
->p_types
.nprim
322 || typdatum
->bounds
> p
->p_types
.nprim
)
324 fa
= p
->sym_val_to_name
[SYM_TYPES
];
325 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
326 GFP_KERNEL
| __GFP_ZERO
))
329 fa
= p
->type_val_to_struct_array
;
330 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
331 GFP_KERNEL
| __GFP_ZERO
))
338 static int user_index(void *key
, void *datum
, void *datap
)
341 struct user_datum
*usrdatum
;
342 struct flex_array
*fa
;
347 || usrdatum
->value
> p
->p_users
.nprim
348 || usrdatum
->bounds
> p
->p_users
.nprim
)
351 fa
= p
->sym_val_to_name
[SYM_USERS
];
352 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
353 GFP_KERNEL
| __GFP_ZERO
))
355 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
359 static int sens_index(void *key
, void *datum
, void *datap
)
362 struct level_datum
*levdatum
;
363 struct flex_array
*fa
;
368 if (!levdatum
->isalias
) {
369 if (!levdatum
->level
->sens
||
370 levdatum
->level
->sens
> p
->p_levels
.nprim
)
372 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
373 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
374 GFP_KERNEL
| __GFP_ZERO
))
381 static int cat_index(void *key
, void *datum
, void *datap
)
384 struct cat_datum
*catdatum
;
385 struct flex_array
*fa
;
390 if (!catdatum
->isalias
) {
391 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
393 fa
= p
->sym_val_to_name
[SYM_CATS
];
394 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
395 GFP_KERNEL
| __GFP_ZERO
))
402 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
415 static void symtab_hash_eval(struct symtab
*s
)
419 for (i
= 0; i
< SYM_NUM
; i
++) {
420 struct hashtab
*h
= s
[i
].table
;
421 struct hashtab_info info
;
423 hashtab_stat(h
, &info
);
424 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
425 "longest chain length %d\n", symtab_name
[i
], h
->nel
,
426 info
.slots_used
, h
->size
, info
.max_chain_len
);
430 static void rangetr_hash_eval(struct hashtab
*h
)
432 struct hashtab_info info
;
434 hashtab_stat(h
, &info
);
435 printk(KERN_DEBUG
"SELinux: rangetr: %d entries and %d/%d buckets used, "
436 "longest chain length %d\n", h
->nel
,
437 info
.slots_used
, h
->size
, info
.max_chain_len
);
440 static inline void rangetr_hash_eval(struct hashtab
*h
)
446 * Define the other val_to_name and val_to_struct arrays
447 * in a policy database structure.
449 * Caller must clean up on failure.
451 static int policydb_index(struct policydb
*p
)
455 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
456 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
458 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
462 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
463 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
466 avtab_hash_eval(&p
->te_avtab
, "rules");
467 symtab_hash_eval(p
->symtab
);
471 p
->class_val_to_struct
=
472 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
474 if (!p
->class_val_to_struct
)
478 p
->role_val_to_struct
=
479 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
481 if (!p
->role_val_to_struct
)
485 p
->user_val_to_struct
=
486 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
488 if (!p
->user_val_to_struct
)
491 /* Yes, I want the sizeof the pointer, not the structure */
493 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
495 GFP_KERNEL
| __GFP_ZERO
);
496 if (!p
->type_val_to_struct_array
)
499 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
500 p
->p_types
.nprim
- 1, GFP_KERNEL
| __GFP_ZERO
);
505 if (cond_init_bool_indexes(p
))
508 for (i
= 0; i
< SYM_NUM
; i
++) {
510 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
512 GFP_KERNEL
| __GFP_ZERO
);
513 if (!p
->sym_val_to_name
[i
])
516 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
517 0, p
->symtab
[i
].nprim
- 1,
518 GFP_KERNEL
| __GFP_ZERO
);
522 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
532 * The following *_destroy functions are used to
533 * free any memory allocated for each kind of
534 * symbol data in the policy database.
537 static int perm_destroy(void *key
, void *datum
, void *p
)
544 static int common_destroy(void *key
, void *datum
, void *p
)
546 struct common_datum
*comdatum
;
551 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
552 hashtab_destroy(comdatum
->permissions
.table
);
558 static int cls_destroy(void *key
, void *datum
, void *p
)
560 struct class_datum
*cladatum
;
561 struct constraint_node
*constraint
, *ctemp
;
562 struct constraint_expr
*e
, *etmp
;
567 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
568 hashtab_destroy(cladatum
->permissions
.table
);
569 constraint
= cladatum
->constraints
;
571 e
= constraint
->expr
;
573 ebitmap_destroy(&e
->names
);
579 constraint
= constraint
->next
;
583 constraint
= cladatum
->validatetrans
;
585 e
= constraint
->expr
;
587 ebitmap_destroy(&e
->names
);
593 constraint
= constraint
->next
;
597 kfree(cladatum
->comkey
);
603 static int role_destroy(void *key
, void *datum
, void *p
)
605 struct role_datum
*role
;
610 ebitmap_destroy(&role
->dominates
);
611 ebitmap_destroy(&role
->types
);
617 static int type_destroy(void *key
, void *datum
, void *p
)
624 static int user_destroy(void *key
, void *datum
, void *p
)
626 struct user_datum
*usrdatum
;
631 ebitmap_destroy(&usrdatum
->roles
);
632 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
633 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
634 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
640 static int sens_destroy(void *key
, void *datum
, void *p
)
642 struct level_datum
*levdatum
;
647 ebitmap_destroy(&levdatum
->level
->cat
);
648 kfree(levdatum
->level
);
654 static int cat_destroy(void *key
, void *datum
, void *p
)
661 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
673 static int range_tr_destroy(void *key
, void *datum
, void *p
)
675 struct mls_range
*rt
= datum
;
677 ebitmap_destroy(&rt
->level
[0].cat
);
678 ebitmap_destroy(&rt
->level
[1].cat
);
684 static void ocontext_destroy(struct ocontext
*c
, int i
)
689 context_destroy(&c
->context
[0]);
690 context_destroy(&c
->context
[1]);
691 if (i
== OCON_ISID
|| i
== OCON_FS
||
692 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
698 * Free any memory allocated by a policy database structure.
700 void policydb_destroy(struct policydb
*p
)
702 struct ocontext
*c
, *ctmp
;
703 struct genfs
*g
, *gtmp
;
705 struct role_allow
*ra
, *lra
= NULL
;
706 struct role_trans
*tr
, *ltr
= NULL
;
708 for (i
= 0; i
< SYM_NUM
; i
++) {
710 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
711 hashtab_destroy(p
->symtab
[i
].table
);
714 for (i
= 0; i
< SYM_NUM
; i
++) {
715 if (p
->sym_val_to_name
[i
])
716 flex_array_free(p
->sym_val_to_name
[i
]);
719 kfree(p
->class_val_to_struct
);
720 kfree(p
->role_val_to_struct
);
721 kfree(p
->user_val_to_struct
);
722 if (p
->type_val_to_struct_array
)
723 flex_array_free(p
->type_val_to_struct_array
);
725 avtab_destroy(&p
->te_avtab
);
727 for (i
= 0; i
< OCON_NUM
; i
++) {
733 ocontext_destroy(ctmp
, i
);
735 p
->ocontexts
[i
] = NULL
;
746 ocontext_destroy(ctmp
, OCON_FSUSE
);
754 cond_policydb_destroy(p
);
756 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
763 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
770 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
771 hashtab_destroy(p
->range_tr
);
773 if (p
->type_attr_map_array
) {
774 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
777 e
= flex_array_get(p
->type_attr_map_array
, i
);
782 flex_array_free(p
->type_attr_map_array
);
784 ebitmap_destroy(&p
->policycaps
);
785 ebitmap_destroy(&p
->permissive_map
);
791 * Load the initial SIDs specified in a policy database
792 * structure into a SID table.
794 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
796 struct ocontext
*head
, *c
;
801 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
805 head
= p
->ocontexts
[OCON_ISID
];
806 for (c
= head
; c
; c
= c
->next
) {
808 if (!c
->context
[0].user
) {
809 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
814 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
816 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
826 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
828 if (!class || class > p
->p_classes
.nprim
)
833 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
835 if (!role
|| role
> p
->p_roles
.nprim
)
840 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
842 if (!type
|| type
> p
->p_types
.nprim
)
848 * Return 1 if the fields in the security context
849 * structure `c' are valid. Return 0 otherwise.
851 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
853 struct role_datum
*role
;
854 struct user_datum
*usrdatum
;
856 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
859 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
862 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
865 if (c
->role
!= OBJECT_R_VAL
) {
867 * Role must be authorized for the type.
869 role
= p
->role_val_to_struct
[c
->role
- 1];
870 if (!ebitmap_get_bit(&role
->types
, c
->type
- 1))
871 /* role may not be associated with type */
875 * User must be authorized for the role.
877 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
881 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
882 /* user may not be associated with role */
886 if (!mls_context_isvalid(p
, c
))
893 * Read a MLS range structure from a policydb binary
894 * representation file.
896 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
902 rc
= next_entry(buf
, fp
, sizeof(u32
));
907 items
= le32_to_cpu(buf
[0]);
908 if (items
> ARRAY_SIZE(buf
)) {
909 printk(KERN_ERR
"SELinux: mls: range overflow\n");
913 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
915 printk(KERN_ERR
"SELinux: mls: truncated range\n");
919 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
921 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
923 r
->level
[1].sens
= r
->level
[0].sens
;
925 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
927 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
931 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
933 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
937 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
939 printk(KERN_ERR
"SELinux: mls: out of memory\n");
946 ebitmap_destroy(&r
->level
[0].cat
);
952 * Read and validate a security context structure
953 * from a policydb binary representation file.
955 static int context_read_and_validate(struct context
*c
,
962 rc
= next_entry(buf
, fp
, sizeof buf
);
964 printk(KERN_ERR
"SELinux: context truncated\n");
967 c
->user
= le32_to_cpu(buf
[0]);
968 c
->role
= le32_to_cpu(buf
[1]);
969 c
->type
= le32_to_cpu(buf
[2]);
970 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
971 rc
= mls_read_range_helper(&c
->range
, fp
);
973 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
979 if (!policydb_context_isvalid(p
, c
)) {
980 printk(KERN_ERR
"SELinux: invalid security context\n");
990 * The following *_read functions are used to
991 * read the symbol data from a policy database
992 * binary representation file.
995 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
998 struct perm_datum
*perdatum
;
1004 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1008 rc
= next_entry(buf
, fp
, sizeof buf
);
1012 len
= le32_to_cpu(buf
[0]);
1013 perdatum
->value
= le32_to_cpu(buf
[1]);
1016 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1020 rc
= next_entry(key
, fp
, len
);
1025 rc
= hashtab_insert(h
, key
, perdatum
);
1031 perm_destroy(key
, perdatum
, NULL
);
1035 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1038 struct common_datum
*comdatum
;
1044 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1048 rc
= next_entry(buf
, fp
, sizeof buf
);
1052 len
= le32_to_cpu(buf
[0]);
1053 comdatum
->value
= le32_to_cpu(buf
[1]);
1055 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1058 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1059 nel
= le32_to_cpu(buf
[3]);
1062 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1066 rc
= next_entry(key
, fp
, len
);
1071 for (i
= 0; i
< nel
; i
++) {
1072 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1077 rc
= hashtab_insert(h
, key
, comdatum
);
1082 common_destroy(key
, comdatum
, NULL
);
1086 static int read_cons_helper(struct constraint_node
**nodep
, int ncons
,
1087 int allowxtarget
, void *fp
)
1089 struct constraint_node
*c
, *lc
;
1090 struct constraint_expr
*e
, *le
;
1093 int rc
, i
, j
, depth
;
1096 for (i
= 0; i
< ncons
; i
++) {
1097 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1106 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1109 c
->permissions
= le32_to_cpu(buf
[0]);
1110 nexpr
= le32_to_cpu(buf
[1]);
1113 for (j
= 0; j
< nexpr
; j
++) {
1114 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1123 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1126 e
->expr_type
= le32_to_cpu(buf
[0]);
1127 e
->attr
= le32_to_cpu(buf
[1]);
1128 e
->op
= le32_to_cpu(buf
[2]);
1130 switch (e
->expr_type
) {
1142 if (depth
== (CEXPR_MAXDEPTH
- 1))
1147 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1149 if (depth
== (CEXPR_MAXDEPTH
- 1))
1152 rc
= ebitmap_read(&e
->names
, fp
);
1169 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1172 struct class_datum
*cladatum
;
1174 u32 len
, len2
, ncons
, nel
;
1178 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1182 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1186 len
= le32_to_cpu(buf
[0]);
1187 len2
= le32_to_cpu(buf
[1]);
1188 cladatum
->value
= le32_to_cpu(buf
[2]);
1190 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1193 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1194 nel
= le32_to_cpu(buf
[4]);
1196 ncons
= le32_to_cpu(buf
[5]);
1199 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1203 rc
= next_entry(key
, fp
, len
);
1210 cladatum
->comkey
= kmalloc(len2
+ 1, GFP_KERNEL
);
1211 if (!cladatum
->comkey
)
1213 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1216 cladatum
->comkey
[len2
] = '\0';
1219 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1220 if (!cladatum
->comdatum
) {
1221 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1225 for (i
= 0; i
< nel
; i
++) {
1226 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1231 rc
= read_cons_helper(&cladatum
->constraints
, ncons
, 0, fp
);
1235 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1236 /* grab the validatetrans rules */
1237 rc
= next_entry(buf
, fp
, sizeof(u32
));
1240 ncons
= le32_to_cpu(buf
[0]);
1241 rc
= read_cons_helper(&cladatum
->validatetrans
, ncons
, 1, fp
);
1246 rc
= hashtab_insert(h
, key
, cladatum
);
1252 cls_destroy(key
, cladatum
, NULL
);
1256 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1259 struct role_datum
*role
;
1260 int rc
, to_read
= 2;
1265 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1269 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1272 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1276 len
= le32_to_cpu(buf
[0]);
1277 role
->value
= le32_to_cpu(buf
[1]);
1278 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1279 role
->bounds
= le32_to_cpu(buf
[2]);
1282 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1286 rc
= next_entry(key
, fp
, len
);
1291 rc
= ebitmap_read(&role
->dominates
, fp
);
1295 rc
= ebitmap_read(&role
->types
, fp
);
1299 if (strcmp(key
, OBJECT_R
) == 0) {
1301 if (role
->value
!= OBJECT_R_VAL
) {
1302 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1303 OBJECT_R
, role
->value
);
1310 rc
= hashtab_insert(h
, key
, role
);
1315 role_destroy(key
, role
, NULL
);
1319 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1322 struct type_datum
*typdatum
;
1323 int rc
, to_read
= 3;
1328 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1332 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1335 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1339 len
= le32_to_cpu(buf
[0]);
1340 typdatum
->value
= le32_to_cpu(buf
[1]);
1341 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1342 u32 prop
= le32_to_cpu(buf
[2]);
1344 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1345 typdatum
->primary
= 1;
1346 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1347 typdatum
->attribute
= 1;
1349 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1351 typdatum
->primary
= le32_to_cpu(buf
[2]);
1355 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1358 rc
= next_entry(key
, fp
, len
);
1363 rc
= hashtab_insert(h
, key
, typdatum
);
1368 type_destroy(key
, typdatum
, NULL
);
1374 * Read a MLS level structure from a policydb binary
1375 * representation file.
1377 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1382 memset(lp
, 0, sizeof(*lp
));
1384 rc
= next_entry(buf
, fp
, sizeof buf
);
1386 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1389 lp
->sens
= le32_to_cpu(buf
[0]);
1391 rc
= ebitmap_read(&lp
->cat
, fp
);
1393 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1399 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1402 struct user_datum
*usrdatum
;
1403 int rc
, to_read
= 2;
1408 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1412 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1415 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1419 len
= le32_to_cpu(buf
[0]);
1420 usrdatum
->value
= le32_to_cpu(buf
[1]);
1421 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1422 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1425 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1428 rc
= next_entry(key
, fp
, len
);
1433 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1437 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1438 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1441 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1446 rc
= hashtab_insert(h
, key
, usrdatum
);
1451 user_destroy(key
, usrdatum
, NULL
);
1455 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1458 struct level_datum
*levdatum
;
1464 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1468 rc
= next_entry(buf
, fp
, sizeof buf
);
1472 len
= le32_to_cpu(buf
[0]);
1473 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1476 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1479 rc
= next_entry(key
, fp
, len
);
1485 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1486 if (!levdatum
->level
)
1489 rc
= mls_read_level(levdatum
->level
, fp
);
1493 rc
= hashtab_insert(h
, key
, levdatum
);
1498 sens_destroy(key
, levdatum
, NULL
);
1502 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1505 struct cat_datum
*catdatum
;
1511 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1515 rc
= next_entry(buf
, fp
, sizeof buf
);
1519 len
= le32_to_cpu(buf
[0]);
1520 catdatum
->value
= le32_to_cpu(buf
[1]);
1521 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1524 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1527 rc
= next_entry(key
, fp
, len
);
1532 rc
= hashtab_insert(h
, key
, catdatum
);
1537 cat_destroy(key
, catdatum
, NULL
);
1541 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1553 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1555 struct user_datum
*upper
, *user
;
1556 struct policydb
*p
= datap
;
1559 upper
= user
= datum
;
1560 while (upper
->bounds
) {
1561 struct ebitmap_node
*node
;
1564 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1565 printk(KERN_ERR
"SELinux: user %s: "
1566 "too deep or looped boundary",
1571 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1572 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1573 if (ebitmap_get_bit(&upper
->roles
, bit
))
1577 "SELinux: boundary violated policy: "
1578 "user=%s role=%s bounds=%s\n",
1579 sym_name(p
, SYM_USERS
, user
->value
- 1),
1580 sym_name(p
, SYM_ROLES
, bit
),
1581 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1590 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1592 struct role_datum
*upper
, *role
;
1593 struct policydb
*p
= datap
;
1596 upper
= role
= datum
;
1597 while (upper
->bounds
) {
1598 struct ebitmap_node
*node
;
1601 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1602 printk(KERN_ERR
"SELinux: role %s: "
1603 "too deep or looped bounds\n",
1608 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1609 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1610 if (ebitmap_get_bit(&upper
->types
, bit
))
1614 "SELinux: boundary violated policy: "
1615 "role=%s type=%s bounds=%s\n",
1616 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1617 sym_name(p
, SYM_TYPES
, bit
),
1618 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1627 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1629 struct type_datum
*upper
;
1630 struct policydb
*p
= datap
;
1634 while (upper
->bounds
) {
1635 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1636 printk(KERN_ERR
"SELinux: type %s: "
1637 "too deep or looped boundary\n",
1642 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1646 if (upper
->attribute
) {
1647 printk(KERN_ERR
"SELinux: type %s: "
1648 "bounded by attribute %s",
1650 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1658 static int policydb_bounds_sanity_check(struct policydb
*p
)
1662 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1665 rc
= hashtab_map(p
->p_users
.table
,
1666 user_bounds_sanity_check
, p
);
1670 rc
= hashtab_map(p
->p_roles
.table
,
1671 role_bounds_sanity_check
, p
);
1675 rc
= hashtab_map(p
->p_types
.table
,
1676 type_bounds_sanity_check
, p
);
1683 extern int ss_initialized
;
1685 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1687 struct class_datum
*cladatum
;
1689 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1693 return cladatum
->value
;
1696 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1698 struct class_datum
*cladatum
;
1699 struct perm_datum
*perdatum
= NULL
;
1700 struct common_datum
*comdatum
;
1702 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1705 cladatum
= p
->class_val_to_struct
[tclass
-1];
1706 comdatum
= cladatum
->comdatum
;
1708 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1711 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1716 return 1U << (perdatum
->value
-1);
1719 static int range_read(struct policydb
*p
, void *fp
)
1721 struct range_trans
*rt
= NULL
;
1722 struct mls_range
*r
= NULL
;
1727 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1730 rc
= next_entry(buf
, fp
, sizeof(u32
));
1734 nel
= le32_to_cpu(buf
[0]);
1735 for (i
= 0; i
< nel
; i
++) {
1737 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1741 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1745 rt
->source_type
= le32_to_cpu(buf
[0]);
1746 rt
->target_type
= le32_to_cpu(buf
[1]);
1747 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1748 rc
= next_entry(buf
, fp
, sizeof(u32
));
1751 rt
->target_class
= le32_to_cpu(buf
[0]);
1753 rt
->target_class
= p
->process_class
;
1756 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1757 !policydb_type_isvalid(p
, rt
->target_type
) ||
1758 !policydb_class_isvalid(p
, rt
->target_class
))
1762 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1766 rc
= mls_read_range_helper(r
, fp
);
1771 if (!mls_range_isvalid(p
, r
)) {
1772 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1776 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1783 rangetr_hash_eval(p
->range_tr
);
1791 static int genfs_read(struct policydb
*p
, void *fp
)
1794 u32 nel
, nel2
, len
, len2
;
1796 struct ocontext
*l
, *c
;
1797 struct ocontext
*newc
= NULL
;
1798 struct genfs
*genfs_p
, *genfs
;
1799 struct genfs
*newgenfs
= NULL
;
1801 rc
= next_entry(buf
, fp
, sizeof(u32
));
1804 nel
= le32_to_cpu(buf
[0]);
1806 for (i
= 0; i
< nel
; i
++) {
1807 rc
= next_entry(buf
, fp
, sizeof(u32
));
1810 len
= le32_to_cpu(buf
[0]);
1813 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1818 newgenfs
->fstype
= kmalloc(len
+ 1, GFP_KERNEL
);
1819 if (!newgenfs
->fstype
)
1822 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
1826 newgenfs
->fstype
[len
] = 0;
1828 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1829 genfs_p
= genfs
, genfs
= genfs
->next
) {
1831 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1832 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
1836 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1839 newgenfs
->next
= genfs
;
1841 genfs_p
->next
= newgenfs
;
1843 p
->genfs
= newgenfs
;
1847 rc
= next_entry(buf
, fp
, sizeof(u32
));
1851 nel2
= le32_to_cpu(buf
[0]);
1852 for (j
= 0; j
< nel2
; j
++) {
1853 rc
= next_entry(buf
, fp
, sizeof(u32
));
1856 len
= le32_to_cpu(buf
[0]);
1859 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
1864 newc
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
1868 rc
= next_entry(newc
->u
.name
, fp
, len
);
1871 newc
->u
.name
[len
] = 0;
1873 rc
= next_entry(buf
, fp
, sizeof(u32
));
1877 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
1878 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
1882 for (l
= NULL
, c
= genfs
->head
; c
;
1883 l
= c
, c
= c
->next
) {
1885 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
1886 (!c
->v
.sclass
|| !newc
->v
.sclass
||
1887 newc
->v
.sclass
== c
->v
.sclass
)) {
1888 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
1889 genfs
->fstype
, c
->u
.name
);
1892 len
= strlen(newc
->u
.name
);
1893 len2
= strlen(c
->u
.name
);
1909 kfree(newgenfs
->fstype
);
1911 ocontext_destroy(newc
, OCON_FSUSE
);
1916 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
1922 struct ocontext
*l
, *c
;
1925 for (i
= 0; i
< info
->ocon_num
; i
++) {
1926 rc
= next_entry(buf
, fp
, sizeof(u32
));
1929 nel
= le32_to_cpu(buf
[0]);
1932 for (j
= 0; j
< nel
; j
++) {
1934 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1940 p
->ocontexts
[i
] = c
;
1945 rc
= next_entry(buf
, fp
, sizeof(u32
));
1949 c
->sid
[0] = le32_to_cpu(buf
[0]);
1950 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1956 rc
= next_entry(buf
, fp
, sizeof(u32
));
1959 len
= le32_to_cpu(buf
[0]);
1962 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
1966 rc
= next_entry(c
->u
.name
, fp
, len
);
1971 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1974 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
1979 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
1982 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
1983 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
1984 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
1985 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
1990 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
1993 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
1994 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
1995 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2000 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2005 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2006 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
2010 len
= le32_to_cpu(buf
[1]);
2011 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2015 rc
= next_entry(c
->u
.name
, fp
, len
);
2019 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2026 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2029 for (k
= 0; k
< 4; k
++)
2030 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2031 for (k
= 0; k
< 4; k
++)
2032 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2033 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2047 * Read the configuration data from a policy database binary
2048 * representation file into a policy database structure.
2050 int policydb_read(struct policydb
*p
, void *fp
)
2052 struct role_allow
*ra
, *lra
;
2053 struct role_trans
*tr
, *ltr
;
2056 u32 len
, nprim
, nel
;
2059 struct policydb_compat_info
*info
;
2061 rc
= policydb_init(p
);
2065 /* Read the magic number and string length. */
2066 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2071 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2072 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2073 "not match expected magic number 0x%x\n",
2074 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2079 len
= le32_to_cpu(buf
[1]);
2080 if (len
!= strlen(POLICYDB_STRING
)) {
2081 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2082 "match expected length %Zu\n",
2083 len
, strlen(POLICYDB_STRING
));
2088 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2089 if (!policydb_str
) {
2090 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2091 "string of length %d\n", len
);
2095 rc
= next_entry(policydb_str
, fp
, len
);
2097 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2098 kfree(policydb_str
);
2103 policydb_str
[len
] = '\0';
2104 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2105 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2106 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2107 kfree(policydb_str
);
2110 /* Done with policydb_str. */
2111 kfree(policydb_str
);
2112 policydb_str
= NULL
;
2114 /* Read the version and table sizes. */
2115 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2120 p
->policyvers
= le32_to_cpu(buf
[0]);
2121 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2122 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2123 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2124 "my version range %d-%d\n",
2125 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2129 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2133 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2134 printk(KERN_ERR
"SELinux: security policydb version %d "
2135 "(MLS) not backwards compatible\n",
2140 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2141 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2143 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2144 rc
= ebitmap_read(&p
->policycaps
, fp
);
2149 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2150 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2156 info
= policydb_lookup_compat(p
->policyvers
);
2158 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2159 "for version %d\n", p
->policyvers
);
2164 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2165 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2166 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2167 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2168 le32_to_cpu(buf
[3]),
2169 info
->sym_num
, info
->ocon_num
);
2173 for (i
= 0; i
< info
->sym_num
; i
++) {
2174 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2177 nprim
= le32_to_cpu(buf
[0]);
2178 nel
= le32_to_cpu(buf
[1]);
2179 for (j
= 0; j
< nel
; j
++) {
2180 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2185 p
->symtab
[i
].nprim
= nprim
;
2188 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2192 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2193 rc
= cond_read_list(p
, fp
);
2198 rc
= next_entry(buf
, fp
, sizeof(u32
));
2201 nel
= le32_to_cpu(buf
[0]);
2203 for (i
= 0; i
< nel
; i
++) {
2205 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2212 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2217 tr
->role
= le32_to_cpu(buf
[0]);
2218 tr
->type
= le32_to_cpu(buf
[1]);
2219 tr
->new_role
= le32_to_cpu(buf
[2]);
2220 if (!policydb_role_isvalid(p
, tr
->role
) ||
2221 !policydb_type_isvalid(p
, tr
->type
) ||
2222 !policydb_role_isvalid(p
, tr
->new_role
))
2227 rc
= next_entry(buf
, fp
, sizeof(u32
));
2230 nel
= le32_to_cpu(buf
[0]);
2232 for (i
= 0; i
< nel
; i
++) {
2234 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2241 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2246 ra
->role
= le32_to_cpu(buf
[0]);
2247 ra
->new_role
= le32_to_cpu(buf
[1]);
2248 if (!policydb_role_isvalid(p
, ra
->role
) ||
2249 !policydb_role_isvalid(p
, ra
->new_role
))
2254 rc
= policydb_index(p
);
2259 p
->process_class
= string_to_security_class(p
, "process");
2260 if (!p
->process_class
)
2264 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2265 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2266 if (!p
->process_trans_perms
)
2269 rc
= ocontext_read(p
, info
, fp
);
2273 rc
= genfs_read(p
, fp
);
2277 rc
= range_read(p
, fp
);
2282 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2284 GFP_KERNEL
| __GFP_ZERO
);
2285 if (!p
->type_attr_map_array
)
2288 /* preallocate so we don't have to worry about the put ever failing */
2289 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
- 1,
2290 GFP_KERNEL
| __GFP_ZERO
);
2294 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2295 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2299 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2300 rc
= ebitmap_read(e
, fp
);
2304 /* add the type itself as the degenerate case */
2305 rc
= ebitmap_set_bit(e
, i
, 1);
2310 rc
= policydb_bounds_sanity_check(p
);
2318 policydb_destroy(p
);
2323 * Write a MLS level structure to a policydb binary
2324 * representation file.
2326 static int mls_write_level(struct mls_level
*l
, void *fp
)
2331 buf
[0] = cpu_to_le32(l
->sens
);
2332 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2336 rc
= ebitmap_write(&l
->cat
, fp
);
2344 * Write a MLS range structure to a policydb binary
2345 * representation file.
2347 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2353 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2359 buf
[0] = cpu_to_le32(items
-1);
2360 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2362 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2364 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
2366 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2370 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2374 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2382 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2385 struct level_datum
*levdatum
= datum
;
2386 struct policy_data
*pd
= ptr
;
2393 buf
[0] = cpu_to_le32(len
);
2394 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2395 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2399 rc
= put_entry(key
, 1, len
, fp
);
2403 rc
= mls_write_level(levdatum
->level
, fp
);
2410 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2413 struct cat_datum
*catdatum
= datum
;
2414 struct policy_data
*pd
= ptr
;
2421 buf
[0] = cpu_to_le32(len
);
2422 buf
[1] = cpu_to_le32(catdatum
->value
);
2423 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2424 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2428 rc
= put_entry(key
, 1, len
, fp
);
2435 static int role_trans_write(struct role_trans
*r
, void *fp
)
2437 struct role_trans
*tr
;
2443 for (tr
= r
; tr
; tr
= tr
->next
)
2445 buf
[0] = cpu_to_le32(nel
);
2446 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2449 for (tr
= r
; tr
; tr
= tr
->next
) {
2450 buf
[0] = cpu_to_le32(tr
->role
);
2451 buf
[1] = cpu_to_le32(tr
->type
);
2452 buf
[2] = cpu_to_le32(tr
->new_role
);
2453 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2461 static int role_allow_write(struct role_allow
*r
, void *fp
)
2463 struct role_allow
*ra
;
2469 for (ra
= r
; ra
; ra
= ra
->next
)
2471 buf
[0] = cpu_to_le32(nel
);
2472 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2475 for (ra
= r
; ra
; ra
= ra
->next
) {
2476 buf
[0] = cpu_to_le32(ra
->role
);
2477 buf
[1] = cpu_to_le32(ra
->new_role
);
2478 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2486 * Write a security context structure
2487 * to a policydb binary representation file.
2489 static int context_write(struct policydb
*p
, struct context
*c
,
2495 buf
[0] = cpu_to_le32(c
->user
);
2496 buf
[1] = cpu_to_le32(c
->role
);
2497 buf
[2] = cpu_to_le32(c
->type
);
2499 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2503 rc
= mls_write_range_helper(&c
->range
, fp
);
2511 * The following *_write functions are used to
2512 * write the symbol data to a policy database
2513 * binary representation file.
2516 static int perm_write(void *vkey
, void *datum
, void *fp
)
2519 struct perm_datum
*perdatum
= datum
;
2525 buf
[0] = cpu_to_le32(len
);
2526 buf
[1] = cpu_to_le32(perdatum
->value
);
2527 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2531 rc
= put_entry(key
, 1, len
, fp
);
2538 static int common_write(void *vkey
, void *datum
, void *ptr
)
2541 struct common_datum
*comdatum
= datum
;
2542 struct policy_data
*pd
= ptr
;
2549 buf
[0] = cpu_to_le32(len
);
2550 buf
[1] = cpu_to_le32(comdatum
->value
);
2551 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2552 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2553 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2557 rc
= put_entry(key
, 1, len
, fp
);
2561 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2568 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2571 struct constraint_node
*c
;
2572 struct constraint_expr
*e
;
2577 for (c
= node
; c
; c
= c
->next
) {
2579 for (e
= c
->expr
; e
; e
= e
->next
)
2581 buf
[0] = cpu_to_le32(c
->permissions
);
2582 buf
[1] = cpu_to_le32(nel
);
2583 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2586 for (e
= c
->expr
; e
; e
= e
->next
) {
2587 buf
[0] = cpu_to_le32(e
->expr_type
);
2588 buf
[1] = cpu_to_le32(e
->attr
);
2589 buf
[2] = cpu_to_le32(e
->op
);
2590 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2594 switch (e
->expr_type
) {
2596 rc
= ebitmap_write(&e
->names
, fp
);
2609 static int class_write(void *vkey
, void *datum
, void *ptr
)
2612 struct class_datum
*cladatum
= datum
;
2613 struct policy_data
*pd
= ptr
;
2615 struct policydb
*p
= pd
->p
;
2616 struct constraint_node
*c
;
2623 if (cladatum
->comkey
)
2624 len2
= strlen(cladatum
->comkey
);
2629 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2632 buf
[0] = cpu_to_le32(len
);
2633 buf
[1] = cpu_to_le32(len2
);
2634 buf
[2] = cpu_to_le32(cladatum
->value
);
2635 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2636 if (cladatum
->permissions
.table
)
2637 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2640 buf
[5] = cpu_to_le32(ncons
);
2641 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2645 rc
= put_entry(key
, 1, len
, fp
);
2649 if (cladatum
->comkey
) {
2650 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2655 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2659 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2663 /* write out the validatetrans rule */
2665 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2668 buf
[0] = cpu_to_le32(ncons
);
2669 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2673 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2680 static int role_write(void *vkey
, void *datum
, void *ptr
)
2683 struct role_datum
*role
= datum
;
2684 struct policy_data
*pd
= ptr
;
2686 struct policydb
*p
= pd
->p
;
2693 buf
[items
++] = cpu_to_le32(len
);
2694 buf
[items
++] = cpu_to_le32(role
->value
);
2695 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2696 buf
[items
++] = cpu_to_le32(role
->bounds
);
2698 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
2700 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2704 rc
= put_entry(key
, 1, len
, fp
);
2708 rc
= ebitmap_write(&role
->dominates
, fp
);
2712 rc
= ebitmap_write(&role
->types
, fp
);
2719 static int type_write(void *vkey
, void *datum
, void *ptr
)
2722 struct type_datum
*typdatum
= datum
;
2723 struct policy_data
*pd
= ptr
;
2724 struct policydb
*p
= pd
->p
;
2732 buf
[items
++] = cpu_to_le32(len
);
2733 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2734 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2737 if (typdatum
->primary
)
2738 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2740 if (typdatum
->attribute
)
2741 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2743 buf
[items
++] = cpu_to_le32(properties
);
2744 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2746 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2748 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
2749 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2753 rc
= put_entry(key
, 1, len
, fp
);
2760 static int user_write(void *vkey
, void *datum
, void *ptr
)
2763 struct user_datum
*usrdatum
= datum
;
2764 struct policy_data
*pd
= ptr
;
2765 struct policydb
*p
= pd
->p
;
2773 buf
[items
++] = cpu_to_le32(len
);
2774 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
2775 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2776 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
2777 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
2778 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2782 rc
= put_entry(key
, 1, len
, fp
);
2786 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
2790 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
2794 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
2801 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
2814 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
2817 unsigned int i
, j
, rc
;
2822 for (i
= 0; i
< info
->ocon_num
; i
++) {
2824 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
2826 buf
[0] = cpu_to_le32(nel
);
2827 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2830 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
2833 buf
[0] = cpu_to_le32(c
->sid
[0]);
2834 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2837 rc
= context_write(p
, &c
->context
[0], fp
);
2843 len
= strlen(c
->u
.name
);
2844 buf
[0] = cpu_to_le32(len
);
2845 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2848 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
2851 rc
= context_write(p
, &c
->context
[0], fp
);
2854 rc
= context_write(p
, &c
->context
[1], fp
);
2859 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
2860 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
2861 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
2862 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2865 rc
= context_write(p
, &c
->context
[0], fp
);
2870 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
2871 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
2872 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
2875 rc
= context_write(p
, &c
->context
[0], fp
);
2880 buf
[0] = cpu_to_le32(c
->v
.behavior
);
2881 len
= strlen(c
->u
.name
);
2882 buf
[1] = cpu_to_le32(len
);
2883 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2886 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
2889 rc
= context_write(p
, &c
->context
[0], fp
);
2894 for (j
= 0; j
< 4; j
++)
2895 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
2896 for (j
= 0; j
< 4; j
++)
2897 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
2898 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
2901 rc
= context_write(p
, &c
->context
[0], fp
);
2911 static int genfs_write(struct policydb
*p
, void *fp
)
2913 struct genfs
*genfs
;
2920 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
2922 buf
[0] = cpu_to_le32(len
);
2923 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2926 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
2927 len
= strlen(genfs
->fstype
);
2928 buf
[0] = cpu_to_le32(len
);
2929 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2932 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
2936 for (c
= genfs
->head
; c
; c
= c
->next
)
2938 buf
[0] = cpu_to_le32(len
);
2939 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2942 for (c
= genfs
->head
; c
; c
= c
->next
) {
2943 len
= strlen(c
->u
.name
);
2944 buf
[0] = cpu_to_le32(len
);
2945 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2948 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
2951 buf
[0] = cpu_to_le32(c
->v
.sclass
);
2952 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2955 rc
= context_write(p
, &c
->context
[0], fp
);
2963 static int range_count(void *key
, void *data
, void *ptr
)
2971 static int range_write_helper(void *key
, void *data
, void *ptr
)
2974 struct range_trans
*rt
= key
;
2975 struct mls_range
*r
= data
;
2976 struct policy_data
*pd
= ptr
;
2978 struct policydb
*p
= pd
->p
;
2981 buf
[0] = cpu_to_le32(rt
->source_type
);
2982 buf
[1] = cpu_to_le32(rt
->target_type
);
2983 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2986 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
2987 buf
[0] = cpu_to_le32(rt
->target_class
);
2988 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2992 rc
= mls_write_range_helper(r
, fp
);
2999 static int range_write(struct policydb
*p
, void *fp
)
3004 struct policy_data pd
;
3009 /* count the number of entries in the hashtab */
3011 rc
= hashtab_map(p
->range_tr
, range_count
, &nel
);
3015 buf
[0] = cpu_to_le32(nel
);
3016 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3020 /* actually write all of the entries */
3021 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3029 * Write the configuration data in a policy database
3030 * structure to a policy database binary representation
3033 int policydb_write(struct policydb
*p
, void *fp
)
3035 unsigned int i
, num_syms
;
3040 struct policydb_compat_info
*info
;
3043 * refuse to write policy older than compressed avtab
3044 * to simplify the writer. There are other tests dropped
3045 * since we assume this throughout the writer code. Be
3046 * careful if you ever try to remove this restriction
3048 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3049 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3050 " Because it is less than version %d\n", p
->policyvers
,
3051 POLICYDB_VERSION_AVTAB
);
3057 config
|= POLICYDB_CONFIG_MLS
;
3059 if (p
->reject_unknown
)
3060 config
|= REJECT_UNKNOWN
;
3061 if (p
->allow_unknown
)
3062 config
|= ALLOW_UNKNOWN
;
3064 /* Write the magic number and string identifiers. */
3065 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3066 len
= strlen(POLICYDB_STRING
);
3067 buf
[1] = cpu_to_le32(len
);
3068 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3071 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3075 /* Write the version, config, and table sizes. */
3076 info
= policydb_lookup_compat(p
->policyvers
);
3078 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3079 "version %d", p
->policyvers
);
3083 buf
[0] = cpu_to_le32(p
->policyvers
);
3084 buf
[1] = cpu_to_le32(config
);
3085 buf
[2] = cpu_to_le32(info
->sym_num
);
3086 buf
[3] = cpu_to_le32(info
->ocon_num
);
3088 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3092 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3093 rc
= ebitmap_write(&p
->policycaps
, fp
);
3098 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3099 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3104 num_syms
= info
->sym_num
;
3105 for (i
= 0; i
< num_syms
; i
++) {
3106 struct policy_data pd
;
3111 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3112 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3114 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3117 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3122 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3126 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3130 rc
= role_trans_write(p
->role_tr
, fp
);
3134 rc
= role_allow_write(p
->role_allow
, fp
);
3138 rc
= ocontext_write(p
, info
, fp
);
3142 rc
= genfs_write(p
, fp
);
3146 rc
= range_write(p
, fp
);
3150 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3151 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3154 rc
= ebitmap_write(e
, fp
);