1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of the policy database.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
9 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
11 * Support for enhanced MLS infrastructure.
13 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
15 * Added conditional policy language extensions
17 * Updated: Hewlett-Packard <paul@paul-moore.com>
19 * Added support for the policy capability bitmap
21 * Update: Mellanox Techonologies
23 * Added Infiniband support
25 * Copyright (C) 2016 Mellanox Techonologies
26 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
27 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
28 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/audit.h>
40 #include "conditional.h"
47 static const char *symtab_name
[SYM_NUM
] = {
59 static unsigned int symtab_sizes
[SYM_NUM
] = {
70 struct policydb_compat_info
{
76 /* These need to be updated if SYM_NUM or OCON_NUM changes */
77 static struct policydb_compat_info policydb_compat
[] = {
79 .version
= POLICYDB_VERSION_BASE
,
80 .sym_num
= SYM_NUM
- 3,
81 .ocon_num
= OCON_NUM
- 3,
84 .version
= POLICYDB_VERSION_BOOL
,
85 .sym_num
= SYM_NUM
- 2,
86 .ocon_num
= OCON_NUM
- 3,
89 .version
= POLICYDB_VERSION_IPV6
,
90 .sym_num
= SYM_NUM
- 2,
91 .ocon_num
= OCON_NUM
- 2,
94 .version
= POLICYDB_VERSION_NLCLASS
,
95 .sym_num
= SYM_NUM
- 2,
96 .ocon_num
= OCON_NUM
- 2,
99 .version
= POLICYDB_VERSION_MLS
,
101 .ocon_num
= OCON_NUM
- 2,
104 .version
= POLICYDB_VERSION_AVTAB
,
106 .ocon_num
= OCON_NUM
- 2,
109 .version
= POLICYDB_VERSION_RANGETRANS
,
111 .ocon_num
= OCON_NUM
- 2,
114 .version
= POLICYDB_VERSION_POLCAP
,
116 .ocon_num
= OCON_NUM
- 2,
119 .version
= POLICYDB_VERSION_PERMISSIVE
,
121 .ocon_num
= OCON_NUM
- 2,
124 .version
= POLICYDB_VERSION_BOUNDARY
,
126 .ocon_num
= OCON_NUM
- 2,
129 .version
= POLICYDB_VERSION_FILENAME_TRANS
,
131 .ocon_num
= OCON_NUM
- 2,
134 .version
= POLICYDB_VERSION_ROLETRANS
,
136 .ocon_num
= OCON_NUM
- 2,
139 .version
= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
,
141 .ocon_num
= OCON_NUM
- 2,
144 .version
= POLICYDB_VERSION_DEFAULT_TYPE
,
146 .ocon_num
= OCON_NUM
- 2,
149 .version
= POLICYDB_VERSION_CONSTRAINT_NAMES
,
151 .ocon_num
= OCON_NUM
- 2,
154 .version
= POLICYDB_VERSION_XPERMS_IOCTL
,
156 .ocon_num
= OCON_NUM
- 2,
159 .version
= POLICYDB_VERSION_INFINIBAND
,
161 .ocon_num
= OCON_NUM
,
165 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
168 struct policydb_compat_info
*info
= NULL
;
170 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
171 if (policydb_compat
[i
].version
== version
) {
172 info
= &policydb_compat
[i
];
180 * Initialize the role table.
182 static int roles_init(struct policydb
*p
)
186 struct role_datum
*role
;
188 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
193 role
->value
= ++p
->p_roles
.nprim
;
194 if (role
->value
!= OBJECT_R_VAL
)
198 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
202 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
213 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
215 const struct filename_trans
*ft
= k
;
217 unsigned int byte_num
;
220 hash
= ft
->stype
^ ft
->ttype
^ ft
->tclass
;
223 while ((focus
= ft
->name
[byte_num
++]))
224 hash
= partial_name_hash(focus
, hash
);
225 return hash
& (h
->size
- 1);
228 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
230 const struct filename_trans
*ft1
= k1
;
231 const struct filename_trans
*ft2
= k2
;
234 v
= ft1
->stype
- ft2
->stype
;
238 v
= ft1
->ttype
- ft2
->ttype
;
242 v
= ft1
->tclass
- ft2
->tclass
;
246 return strcmp(ft1
->name
, ft2
->name
);
250 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
252 const struct range_trans
*key
= k
;
253 return (key
->source_type
+ (key
->target_type
<< 3) +
254 (key
->target_class
<< 5)) & (h
->size
- 1);
257 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
259 const struct range_trans
*key1
= k1
, *key2
= k2
;
262 v
= key1
->source_type
- key2
->source_type
;
266 v
= key1
->target_type
- key2
->target_type
;
270 v
= key1
->target_class
- key2
->target_class
;
275 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
);
278 * Initialize a policy database structure.
280 static int policydb_init(struct policydb
*p
)
284 memset(p
, 0, sizeof(*p
));
286 for (i
= 0; i
< SYM_NUM
; i
++) {
287 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
292 rc
= avtab_init(&p
->te_avtab
);
300 rc
= cond_policydb_init(p
);
304 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
305 if (!p
->filename_trans
) {
310 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
316 ebitmap_init(&p
->filename_trans_ttypes
);
317 ebitmap_init(&p
->policycaps
);
318 ebitmap_init(&p
->permissive_map
);
322 hashtab_destroy(p
->filename_trans
);
323 hashtab_destroy(p
->range_tr
);
324 for (i
= 0; i
< SYM_NUM
; i
++) {
325 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
326 hashtab_destroy(p
->symtab
[i
].table
);
332 * The following *_index functions are used to
333 * define the val_to_name and val_to_struct arrays
334 * in a policy database structure. The val_to_name
335 * arrays are used when converting security context
336 * structures into string representations. The
337 * val_to_struct arrays are used when the attributes
338 * of a class, role, or user are needed.
341 static int common_index(void *key
, void *datum
, void *datap
)
344 struct common_datum
*comdatum
;
348 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
351 p
->sym_val_to_name
[SYM_COMMONS
][comdatum
->value
- 1] = key
;
356 static int class_index(void *key
, void *datum
, void *datap
)
359 struct class_datum
*cladatum
;
363 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
366 p
->sym_val_to_name
[SYM_CLASSES
][cladatum
->value
- 1] = key
;
367 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
371 static int role_index(void *key
, void *datum
, void *datap
)
374 struct role_datum
*role
;
379 || role
->value
> p
->p_roles
.nprim
380 || role
->bounds
> p
->p_roles
.nprim
)
383 p
->sym_val_to_name
[SYM_ROLES
][role
->value
- 1] = key
;
384 p
->role_val_to_struct
[role
->value
- 1] = role
;
388 static int type_index(void *key
, void *datum
, void *datap
)
391 struct type_datum
*typdatum
;
396 if (typdatum
->primary
) {
398 || typdatum
->value
> p
->p_types
.nprim
399 || typdatum
->bounds
> p
->p_types
.nprim
)
401 p
->sym_val_to_name
[SYM_TYPES
][typdatum
->value
- 1] = key
;
402 p
->type_val_to_struct_array
[typdatum
->value
- 1] = typdatum
;
408 static int user_index(void *key
, void *datum
, void *datap
)
411 struct user_datum
*usrdatum
;
416 || usrdatum
->value
> p
->p_users
.nprim
417 || usrdatum
->bounds
> p
->p_users
.nprim
)
420 p
->sym_val_to_name
[SYM_USERS
][usrdatum
->value
- 1] = key
;
421 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
425 static int sens_index(void *key
, void *datum
, void *datap
)
428 struct level_datum
*levdatum
;
433 if (!levdatum
->isalias
) {
434 if (!levdatum
->level
->sens
||
435 levdatum
->level
->sens
> p
->p_levels
.nprim
)
438 p
->sym_val_to_name
[SYM_LEVELS
][levdatum
->level
->sens
- 1] = key
;
444 static int cat_index(void *key
, void *datum
, void *datap
)
447 struct cat_datum
*catdatum
;
452 if (!catdatum
->isalias
) {
453 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
456 p
->sym_val_to_name
[SYM_CATS
][catdatum
->value
- 1] = key
;
462 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
475 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
477 struct hashtab_info info
;
479 hashtab_stat(h
, &info
);
480 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
481 "longest chain length %d\n", hash_name
, h
->nel
,
482 info
.slots_used
, h
->size
, info
.max_chain_len
);
485 static void symtab_hash_eval(struct symtab
*s
)
489 for (i
= 0; i
< SYM_NUM
; i
++)
490 hash_eval(s
[i
].table
, symtab_name
[i
]);
494 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
500 * Define the other val_to_name and val_to_struct arrays
501 * in a policy database structure.
503 * Caller must clean up on failure.
505 static int policydb_index(struct policydb
*p
)
510 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
511 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
,
512 p
->p_bools
.nprim
, p
->p_levels
.nprim
, p
->p_cats
.nprim
);
514 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
515 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
,
518 pr_debug("SELinux: %d classes, %d rules\n",
519 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
522 avtab_hash_eval(&p
->te_avtab
, "rules");
523 symtab_hash_eval(p
->symtab
);
526 p
->class_val_to_struct
= kcalloc(p
->p_classes
.nprim
,
527 sizeof(*p
->class_val_to_struct
),
529 if (!p
->class_val_to_struct
)
532 p
->role_val_to_struct
= kcalloc(p
->p_roles
.nprim
,
533 sizeof(*p
->role_val_to_struct
),
535 if (!p
->role_val_to_struct
)
538 p
->user_val_to_struct
= kcalloc(p
->p_users
.nprim
,
539 sizeof(*p
->user_val_to_struct
),
541 if (!p
->user_val_to_struct
)
544 p
->type_val_to_struct_array
= kvcalloc(p
->p_types
.nprim
,
545 sizeof(*p
->type_val_to_struct_array
),
547 if (!p
->type_val_to_struct_array
)
550 rc
= cond_init_bool_indexes(p
);
554 for (i
= 0; i
< SYM_NUM
; i
++) {
555 p
->sym_val_to_name
[i
] = kvcalloc(p
->symtab
[i
].nprim
,
558 if (!p
->sym_val_to_name
[i
])
561 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
571 * The following *_destroy functions are used to
572 * free any memory allocated for each kind of
573 * symbol data in the policy database.
576 static int perm_destroy(void *key
, void *datum
, void *p
)
583 static int common_destroy(void *key
, void *datum
, void *p
)
585 struct common_datum
*comdatum
;
590 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
591 hashtab_destroy(comdatum
->permissions
.table
);
597 static void constraint_expr_destroy(struct constraint_expr
*expr
)
600 ebitmap_destroy(&expr
->names
);
601 if (expr
->type_names
) {
602 ebitmap_destroy(&expr
->type_names
->types
);
603 ebitmap_destroy(&expr
->type_names
->negset
);
604 kfree(expr
->type_names
);
610 static int cls_destroy(void *key
, void *datum
, void *p
)
612 struct class_datum
*cladatum
;
613 struct constraint_node
*constraint
, *ctemp
;
614 struct constraint_expr
*e
, *etmp
;
619 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
620 hashtab_destroy(cladatum
->permissions
.table
);
621 constraint
= cladatum
->constraints
;
623 e
= constraint
->expr
;
627 constraint_expr_destroy(etmp
);
630 constraint
= constraint
->next
;
634 constraint
= cladatum
->validatetrans
;
636 e
= constraint
->expr
;
640 constraint_expr_destroy(etmp
);
643 constraint
= constraint
->next
;
646 kfree(cladatum
->comkey
);
652 static int role_destroy(void *key
, void *datum
, void *p
)
654 struct role_datum
*role
;
659 ebitmap_destroy(&role
->dominates
);
660 ebitmap_destroy(&role
->types
);
666 static int type_destroy(void *key
, void *datum
, void *p
)
673 static int user_destroy(void *key
, void *datum
, void *p
)
675 struct user_datum
*usrdatum
;
680 ebitmap_destroy(&usrdatum
->roles
);
681 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
682 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
683 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
689 static int sens_destroy(void *key
, void *datum
, void *p
)
691 struct level_datum
*levdatum
;
697 ebitmap_destroy(&levdatum
->level
->cat
);
698 kfree(levdatum
->level
);
704 static int cat_destroy(void *key
, void *datum
, void *p
)
711 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
723 static int filenametr_destroy(void *key
, void *datum
, void *p
)
725 struct filename_trans
*ft
= key
;
733 static int range_tr_destroy(void *key
, void *datum
, void *p
)
735 struct mls_range
*rt
= datum
;
737 ebitmap_destroy(&rt
->level
[0].cat
);
738 ebitmap_destroy(&rt
->level
[1].cat
);
744 static void ocontext_destroy(struct ocontext
*c
, int i
)
749 context_destroy(&c
->context
[0]);
750 context_destroy(&c
->context
[1]);
751 if (i
== OCON_ISID
|| i
== OCON_FS
||
752 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
758 * Free any memory allocated by a policy database structure.
760 void policydb_destroy(struct policydb
*p
)
762 struct ocontext
*c
, *ctmp
;
763 struct genfs
*g
, *gtmp
;
765 struct role_allow
*ra
, *lra
= NULL
;
766 struct role_trans
*tr
, *ltr
= NULL
;
768 for (i
= 0; i
< SYM_NUM
; i
++) {
770 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
771 hashtab_destroy(p
->symtab
[i
].table
);
774 for (i
= 0; i
< SYM_NUM
; i
++)
775 kvfree(p
->sym_val_to_name
[i
]);
777 kfree(p
->class_val_to_struct
);
778 kfree(p
->role_val_to_struct
);
779 kfree(p
->user_val_to_struct
);
780 kvfree(p
->type_val_to_struct_array
);
782 avtab_destroy(&p
->te_avtab
);
784 for (i
= 0; i
< OCON_NUM
; i
++) {
790 ocontext_destroy(ctmp
, i
);
792 p
->ocontexts
[i
] = NULL
;
803 ocontext_destroy(ctmp
, OCON_FSUSE
);
811 cond_policydb_destroy(p
);
813 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
820 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
827 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
828 hashtab_destroy(p
->filename_trans
);
830 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
831 hashtab_destroy(p
->range_tr
);
833 if (p
->type_attr_map_array
) {
834 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
835 ebitmap_destroy(&p
->type_attr_map_array
[i
]);
836 kvfree(p
->type_attr_map_array
);
839 ebitmap_destroy(&p
->filename_trans_ttypes
);
840 ebitmap_destroy(&p
->policycaps
);
841 ebitmap_destroy(&p
->permissive_map
);
845 * Load the initial SIDs specified in a policy database
846 * structure into a SID table.
848 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
850 struct ocontext
*head
, *c
;
855 pr_err("SELinux: out of memory on SID table init\n");
859 head
= p
->ocontexts
[OCON_ISID
];
860 for (c
= head
; c
; c
= c
->next
) {
862 if (!c
->context
[0].user
) {
863 pr_err("SELinux: SID %s was never defined.\n",
868 if (c
->sid
[0] == SECSID_NULL
|| c
->sid
[0] > SECINITSID_NUM
) {
869 pr_err("SELinux: Initial SID %s out of range.\n",
875 rc
= sidtab_set_initial(s
, c
->sid
[0], &c
->context
[0]);
877 pr_err("SELinux: unable to load initial SID %s.\n",
888 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
890 if (!class || class > p
->p_classes
.nprim
)
895 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
897 if (!role
|| role
> p
->p_roles
.nprim
)
902 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
904 if (!type
|| type
> p
->p_types
.nprim
)
910 * Return 1 if the fields in the security context
911 * structure `c' are valid. Return 0 otherwise.
913 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
915 struct role_datum
*role
;
916 struct user_datum
*usrdatum
;
918 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
921 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
924 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
927 if (c
->role
!= OBJECT_R_VAL
) {
929 * Role must be authorized for the type.
931 role
= p
->role_val_to_struct
[c
->role
- 1];
932 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
933 /* role may not be associated with type */
937 * User must be authorized for the role.
939 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
943 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
944 /* user may not be associated with role */
948 if (!mls_context_isvalid(p
, c
))
955 * Read a MLS range structure from a policydb binary
956 * representation file.
958 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
964 rc
= next_entry(buf
, fp
, sizeof(u32
));
969 items
= le32_to_cpu(buf
[0]);
970 if (items
> ARRAY_SIZE(buf
)) {
971 pr_err("SELinux: mls: range overflow\n");
975 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
977 pr_err("SELinux: mls: truncated range\n");
981 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
983 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
985 r
->level
[1].sens
= r
->level
[0].sens
;
987 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
989 pr_err("SELinux: mls: error reading low categories\n");
993 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
995 pr_err("SELinux: mls: error reading high categories\n");
999 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1001 pr_err("SELinux: mls: out of memory\n");
1008 ebitmap_destroy(&r
->level
[0].cat
);
1014 * Read and validate a security context structure
1015 * from a policydb binary representation file.
1017 static int context_read_and_validate(struct context
*c
,
1024 rc
= next_entry(buf
, fp
, sizeof buf
);
1026 pr_err("SELinux: context truncated\n");
1029 c
->user
= le32_to_cpu(buf
[0]);
1030 c
->role
= le32_to_cpu(buf
[1]);
1031 c
->type
= le32_to_cpu(buf
[2]);
1032 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1033 rc
= mls_read_range_helper(&c
->range
, fp
);
1035 pr_err("SELinux: error reading MLS range of context\n");
1041 if (!policydb_context_isvalid(p
, c
)) {
1042 pr_err("SELinux: invalid security context\n");
1052 * The following *_read functions are used to
1053 * read the symbol data from a policy database
1054 * binary representation file.
1057 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1062 if ((len
== 0) || (len
== (u32
)-1))
1065 str
= kmalloc(len
+ 1, flags
| __GFP_NOWARN
);
1069 /* it's expected the caller should free the str */
1072 rc
= next_entry(str
, fp
, len
);
1080 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1083 struct perm_datum
*perdatum
;
1088 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1092 rc
= next_entry(buf
, fp
, sizeof buf
);
1096 len
= le32_to_cpu(buf
[0]);
1097 perdatum
->value
= le32_to_cpu(buf
[1]);
1099 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1103 rc
= hashtab_insert(h
, key
, perdatum
);
1109 perm_destroy(key
, perdatum
, NULL
);
1113 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1116 struct common_datum
*comdatum
;
1121 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1125 rc
= next_entry(buf
, fp
, sizeof buf
);
1129 len
= le32_to_cpu(buf
[0]);
1130 comdatum
->value
= le32_to_cpu(buf
[1]);
1132 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1135 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1136 nel
= le32_to_cpu(buf
[3]);
1138 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1142 for (i
= 0; i
< nel
; i
++) {
1143 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1148 rc
= hashtab_insert(h
, key
, comdatum
);
1153 common_destroy(key
, comdatum
, NULL
);
1157 static void type_set_init(struct type_set
*t
)
1159 ebitmap_init(&t
->types
);
1160 ebitmap_init(&t
->negset
);
1163 static int type_set_read(struct type_set
*t
, void *fp
)
1168 if (ebitmap_read(&t
->types
, fp
))
1170 if (ebitmap_read(&t
->negset
, fp
))
1173 rc
= next_entry(buf
, fp
, sizeof(u32
));
1176 t
->flags
= le32_to_cpu(buf
[0]);
1182 static int read_cons_helper(struct policydb
*p
,
1183 struct constraint_node
**nodep
,
1184 int ncons
, int allowxtarget
, void *fp
)
1186 struct constraint_node
*c
, *lc
;
1187 struct constraint_expr
*e
, *le
;
1190 int rc
, i
, j
, depth
;
1193 for (i
= 0; i
< ncons
; i
++) {
1194 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1203 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1206 c
->permissions
= le32_to_cpu(buf
[0]);
1207 nexpr
= le32_to_cpu(buf
[1]);
1210 for (j
= 0; j
< nexpr
; j
++) {
1211 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1220 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1223 e
->expr_type
= le32_to_cpu(buf
[0]);
1224 e
->attr
= le32_to_cpu(buf
[1]);
1225 e
->op
= le32_to_cpu(buf
[2]);
1227 switch (e
->expr_type
) {
1239 if (depth
== (CEXPR_MAXDEPTH
- 1))
1244 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1246 if (depth
== (CEXPR_MAXDEPTH
- 1))
1249 rc
= ebitmap_read(&e
->names
, fp
);
1252 if (p
->policyvers
>=
1253 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1254 e
->type_names
= kzalloc(sizeof
1259 type_set_init(e
->type_names
);
1260 rc
= type_set_read(e
->type_names
, fp
);
1278 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1281 struct class_datum
*cladatum
;
1283 u32 len
, len2
, ncons
, nel
;
1286 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1290 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1294 len
= le32_to_cpu(buf
[0]);
1295 len2
= le32_to_cpu(buf
[1]);
1296 cladatum
->value
= le32_to_cpu(buf
[2]);
1298 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1301 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1302 nel
= le32_to_cpu(buf
[4]);
1304 ncons
= le32_to_cpu(buf
[5]);
1306 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1311 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1316 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1317 if (!cladatum
->comdatum
) {
1318 pr_err("SELinux: unknown common %s\n",
1323 for (i
= 0; i
< nel
; i
++) {
1324 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1329 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1333 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1334 /* grab the validatetrans rules */
1335 rc
= next_entry(buf
, fp
, sizeof(u32
));
1338 ncons
= le32_to_cpu(buf
[0]);
1339 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1345 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1346 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1350 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1351 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1352 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1355 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1356 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1359 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1362 rc
= hashtab_insert(h
, key
, cladatum
);
1368 cls_destroy(key
, cladatum
, NULL
);
1372 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1375 struct role_datum
*role
;
1376 int rc
, to_read
= 2;
1380 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1384 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1387 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1391 len
= le32_to_cpu(buf
[0]);
1392 role
->value
= le32_to_cpu(buf
[1]);
1393 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1394 role
->bounds
= le32_to_cpu(buf
[2]);
1396 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1400 rc
= ebitmap_read(&role
->dominates
, fp
);
1404 rc
= ebitmap_read(&role
->types
, fp
);
1408 if (strcmp(key
, OBJECT_R
) == 0) {
1410 if (role
->value
!= OBJECT_R_VAL
) {
1411 pr_err("SELinux: Role %s has wrong value %d\n",
1412 OBJECT_R
, role
->value
);
1419 rc
= hashtab_insert(h
, key
, role
);
1424 role_destroy(key
, role
, NULL
);
1428 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1431 struct type_datum
*typdatum
;
1432 int rc
, to_read
= 3;
1436 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1440 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1443 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1447 len
= le32_to_cpu(buf
[0]);
1448 typdatum
->value
= le32_to_cpu(buf
[1]);
1449 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1450 u32 prop
= le32_to_cpu(buf
[2]);
1452 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1453 typdatum
->primary
= 1;
1454 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1455 typdatum
->attribute
= 1;
1457 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1459 typdatum
->primary
= le32_to_cpu(buf
[2]);
1462 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1466 rc
= hashtab_insert(h
, key
, typdatum
);
1471 type_destroy(key
, typdatum
, NULL
);
1477 * Read a MLS level structure from a policydb binary
1478 * representation file.
1480 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1485 memset(lp
, 0, sizeof(*lp
));
1487 rc
= next_entry(buf
, fp
, sizeof buf
);
1489 pr_err("SELinux: mls: truncated level\n");
1492 lp
->sens
= le32_to_cpu(buf
[0]);
1494 rc
= ebitmap_read(&lp
->cat
, fp
);
1496 pr_err("SELinux: mls: error reading level categories\n");
1502 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1505 struct user_datum
*usrdatum
;
1506 int rc
, to_read
= 2;
1510 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1514 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1517 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1521 len
= le32_to_cpu(buf
[0]);
1522 usrdatum
->value
= le32_to_cpu(buf
[1]);
1523 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1524 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1526 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1530 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1534 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1535 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1538 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1543 rc
= hashtab_insert(h
, key
, usrdatum
);
1548 user_destroy(key
, usrdatum
, NULL
);
1552 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1555 struct level_datum
*levdatum
;
1560 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1564 rc
= next_entry(buf
, fp
, sizeof buf
);
1568 len
= le32_to_cpu(buf
[0]);
1569 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1571 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1576 levdatum
->level
= kmalloc(sizeof(*levdatum
->level
), GFP_ATOMIC
);
1577 if (!levdatum
->level
)
1580 rc
= mls_read_level(levdatum
->level
, fp
);
1584 rc
= hashtab_insert(h
, key
, levdatum
);
1589 sens_destroy(key
, levdatum
, NULL
);
1593 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1596 struct cat_datum
*catdatum
;
1601 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1605 rc
= next_entry(buf
, fp
, sizeof buf
);
1609 len
= le32_to_cpu(buf
[0]);
1610 catdatum
->value
= le32_to_cpu(buf
[1]);
1611 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1613 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1617 rc
= hashtab_insert(h
, key
, catdatum
);
1622 cat_destroy(key
, catdatum
, NULL
);
1626 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1638 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1640 struct user_datum
*upper
, *user
;
1641 struct policydb
*p
= datap
;
1644 upper
= user
= datum
;
1645 while (upper
->bounds
) {
1646 struct ebitmap_node
*node
;
1649 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1650 pr_err("SELinux: user %s: "
1651 "too deep or looped boundary",
1656 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1657 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1658 if (ebitmap_get_bit(&upper
->roles
, bit
))
1661 pr_err("SELinux: boundary violated policy: "
1662 "user=%s role=%s bounds=%s\n",
1663 sym_name(p
, SYM_USERS
, user
->value
- 1),
1664 sym_name(p
, SYM_ROLES
, bit
),
1665 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1674 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1676 struct role_datum
*upper
, *role
;
1677 struct policydb
*p
= datap
;
1680 upper
= role
= datum
;
1681 while (upper
->bounds
) {
1682 struct ebitmap_node
*node
;
1685 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1686 pr_err("SELinux: role %s: "
1687 "too deep or looped bounds\n",
1692 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1693 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1694 if (ebitmap_get_bit(&upper
->types
, bit
))
1697 pr_err("SELinux: boundary violated policy: "
1698 "role=%s type=%s bounds=%s\n",
1699 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1700 sym_name(p
, SYM_TYPES
, bit
),
1701 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1710 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1712 struct type_datum
*upper
;
1713 struct policydb
*p
= datap
;
1717 while (upper
->bounds
) {
1718 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1719 pr_err("SELinux: type %s: "
1720 "too deep or looped boundary\n",
1725 upper
= p
->type_val_to_struct_array
[upper
->bounds
- 1];
1728 if (upper
->attribute
) {
1729 pr_err("SELinux: type %s: "
1730 "bounded by attribute %s",
1732 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1740 static int policydb_bounds_sanity_check(struct policydb
*p
)
1744 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1747 rc
= hashtab_map(p
->p_users
.table
,
1748 user_bounds_sanity_check
, p
);
1752 rc
= hashtab_map(p
->p_roles
.table
,
1753 role_bounds_sanity_check
, p
);
1757 rc
= hashtab_map(p
->p_types
.table
,
1758 type_bounds_sanity_check
, p
);
1765 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1767 struct class_datum
*cladatum
;
1769 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1773 return cladatum
->value
;
1776 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1778 struct class_datum
*cladatum
;
1779 struct perm_datum
*perdatum
= NULL
;
1780 struct common_datum
*comdatum
;
1782 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1785 cladatum
= p
->class_val_to_struct
[tclass
-1];
1786 comdatum
= cladatum
->comdatum
;
1788 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1791 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1796 return 1U << (perdatum
->value
-1);
1799 static int range_read(struct policydb
*p
, void *fp
)
1801 struct range_trans
*rt
= NULL
;
1802 struct mls_range
*r
= NULL
;
1807 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1810 rc
= next_entry(buf
, fp
, sizeof(u32
));
1814 nel
= le32_to_cpu(buf
[0]);
1815 for (i
= 0; i
< nel
; i
++) {
1817 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1821 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1825 rt
->source_type
= le32_to_cpu(buf
[0]);
1826 rt
->target_type
= le32_to_cpu(buf
[1]);
1827 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1828 rc
= next_entry(buf
, fp
, sizeof(u32
));
1831 rt
->target_class
= le32_to_cpu(buf
[0]);
1833 rt
->target_class
= p
->process_class
;
1836 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1837 !policydb_type_isvalid(p
, rt
->target_type
) ||
1838 !policydb_class_isvalid(p
, rt
->target_class
))
1842 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1846 rc
= mls_read_range_helper(r
, fp
);
1851 if (!mls_range_isvalid(p
, r
)) {
1852 pr_warn("SELinux: rangetrans: invalid range\n");
1856 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1863 hash_eval(p
->range_tr
, "rangetr");
1871 static int filename_trans_read(struct policydb
*p
, void *fp
)
1873 struct filename_trans
*ft
;
1874 struct filename_trans_datum
*otype
;
1880 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1883 rc
= next_entry(buf
, fp
, sizeof(u32
));
1886 nel
= le32_to_cpu(buf
[0]);
1888 for (i
= 0; i
< nel
; i
++) {
1893 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1898 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1902 /* length of the path component string */
1903 rc
= next_entry(buf
, fp
, sizeof(u32
));
1906 len
= le32_to_cpu(buf
[0]);
1908 /* path component string */
1909 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1915 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1919 ft
->stype
= le32_to_cpu(buf
[0]);
1920 ft
->ttype
= le32_to_cpu(buf
[1]);
1921 ft
->tclass
= le32_to_cpu(buf
[2]);
1923 otype
->otype
= le32_to_cpu(buf
[3]);
1925 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1929 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1932 * Do not return -EEXIST to the caller, or the system
1937 /* But free memory to avoid memory leak. */
1943 hash_eval(p
->filename_trans
, "filenametr");
1953 static int genfs_read(struct policydb
*p
, void *fp
)
1956 u32 nel
, nel2
, len
, len2
;
1958 struct ocontext
*l
, *c
;
1959 struct ocontext
*newc
= NULL
;
1960 struct genfs
*genfs_p
, *genfs
;
1961 struct genfs
*newgenfs
= NULL
;
1963 rc
= next_entry(buf
, fp
, sizeof(u32
));
1966 nel
= le32_to_cpu(buf
[0]);
1968 for (i
= 0; i
< nel
; i
++) {
1969 rc
= next_entry(buf
, fp
, sizeof(u32
));
1972 len
= le32_to_cpu(buf
[0]);
1975 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1979 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
1983 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1984 genfs_p
= genfs
, genfs
= genfs
->next
) {
1986 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1987 pr_err("SELinux: dup genfs fstype %s\n",
1991 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1994 newgenfs
->next
= genfs
;
1996 genfs_p
->next
= newgenfs
;
1998 p
->genfs
= newgenfs
;
2002 rc
= next_entry(buf
, fp
, sizeof(u32
));
2006 nel2
= le32_to_cpu(buf
[0]);
2007 for (j
= 0; j
< nel2
; j
++) {
2008 rc
= next_entry(buf
, fp
, sizeof(u32
));
2011 len
= le32_to_cpu(buf
[0]);
2014 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2018 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2022 rc
= next_entry(buf
, fp
, sizeof(u32
));
2026 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2027 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2031 for (l
= NULL
, c
= genfs
->head
; c
;
2032 l
= c
, c
= c
->next
) {
2034 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2035 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2036 newc
->v
.sclass
== c
->v
.sclass
)) {
2037 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2038 genfs
->fstype
, c
->u
.name
);
2041 len
= strlen(newc
->u
.name
);
2042 len2
= strlen(c
->u
.name
);
2058 kfree(newgenfs
->fstype
);
2061 ocontext_destroy(newc
, OCON_FSUSE
);
2066 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2071 __be64 prefixbuf
[1];
2073 struct ocontext
*l
, *c
;
2076 for (i
= 0; i
< info
->ocon_num
; i
++) {
2077 rc
= next_entry(buf
, fp
, sizeof(u32
));
2080 nel
= le32_to_cpu(buf
[0]);
2083 for (j
= 0; j
< nel
; j
++) {
2085 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2091 p
->ocontexts
[i
] = c
;
2096 rc
= next_entry(buf
, fp
, sizeof(u32
));
2100 c
->sid
[0] = le32_to_cpu(buf
[0]);
2101 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2107 rc
= next_entry(buf
, fp
, sizeof(u32
));
2110 len
= le32_to_cpu(buf
[0]);
2112 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2116 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2119 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2124 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2127 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2128 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2129 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2130 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2135 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2138 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2139 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2140 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2145 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2150 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2151 /* Determined at runtime, not in policy DB. */
2152 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2154 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2157 len
= le32_to_cpu(buf
[1]);
2158 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2162 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2169 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2172 for (k
= 0; k
< 4; k
++)
2173 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2174 for (k
= 0; k
< 4; k
++)
2175 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2176 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2182 u32 pkey_lo
, pkey_hi
;
2184 rc
= next_entry(prefixbuf
, fp
, sizeof(u64
));
2188 /* we need to have subnet_prefix in CPU order */
2189 c
->u
.ibpkey
.subnet_prefix
= be64_to_cpu(prefixbuf
[0]);
2191 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2195 pkey_lo
= le32_to_cpu(buf
[0]);
2196 pkey_hi
= le32_to_cpu(buf
[1]);
2198 if (pkey_lo
> U16_MAX
|| pkey_hi
> U16_MAX
) {
2203 c
->u
.ibpkey
.low_pkey
= pkey_lo
;
2204 c
->u
.ibpkey
.high_pkey
= pkey_hi
;
2206 rc
= context_read_and_validate(&c
->context
[0],
2213 case OCON_IBENDPORT
: {
2216 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2219 len
= le32_to_cpu(buf
[0]);
2221 rc
= str_read(&c
->u
.ibendport
.dev_name
, GFP_KERNEL
, fp
, len
);
2225 port
= le32_to_cpu(buf
[1]);
2226 if (port
> U8_MAX
|| port
== 0) {
2231 c
->u
.ibendport
.port
= port
;
2233 rc
= context_read_and_validate(&c
->context
[0],
2249 * Read the configuration data from a policy database binary
2250 * representation file into a policy database structure.
2252 int policydb_read(struct policydb
*p
, void *fp
)
2254 struct role_allow
*ra
, *lra
;
2255 struct role_trans
*tr
, *ltr
;
2258 u32 len
, nprim
, nel
;
2261 struct policydb_compat_info
*info
;
2263 rc
= policydb_init(p
);
2267 /* Read the magic number and string length. */
2268 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2273 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2274 pr_err("SELinux: policydb magic number 0x%x does "
2275 "not match expected magic number 0x%x\n",
2276 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2281 len
= le32_to_cpu(buf
[1]);
2282 if (len
!= strlen(POLICYDB_STRING
)) {
2283 pr_err("SELinux: policydb string length %d does not "
2284 "match expected length %zu\n",
2285 len
, strlen(POLICYDB_STRING
));
2290 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2291 if (!policydb_str
) {
2292 pr_err("SELinux: unable to allocate memory for policydb "
2293 "string of length %d\n", len
);
2297 rc
= next_entry(policydb_str
, fp
, len
);
2299 pr_err("SELinux: truncated policydb string identifier\n");
2300 kfree(policydb_str
);
2305 policydb_str
[len
] = '\0';
2306 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2307 pr_err("SELinux: policydb string %s does not match "
2308 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2309 kfree(policydb_str
);
2312 /* Done with policydb_str. */
2313 kfree(policydb_str
);
2314 policydb_str
= NULL
;
2316 /* Read the version and table sizes. */
2317 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2322 p
->policyvers
= le32_to_cpu(buf
[0]);
2323 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2324 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2325 pr_err("SELinux: policydb version %d does not match "
2326 "my version range %d-%d\n",
2327 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2331 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2335 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2336 pr_err("SELinux: security policydb version %d "
2337 "(MLS) not backwards compatible\n",
2342 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2343 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2345 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2346 rc
= ebitmap_read(&p
->policycaps
, fp
);
2351 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2352 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2358 info
= policydb_lookup_compat(p
->policyvers
);
2360 pr_err("SELinux: unable to find policy compat info "
2361 "for version %d\n", p
->policyvers
);
2366 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2367 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2368 pr_err("SELinux: policydb table sizes (%d,%d) do "
2369 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2370 le32_to_cpu(buf
[3]),
2371 info
->sym_num
, info
->ocon_num
);
2375 for (i
= 0; i
< info
->sym_num
; i
++) {
2376 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2379 nprim
= le32_to_cpu(buf
[0]);
2380 nel
= le32_to_cpu(buf
[1]);
2381 for (j
= 0; j
< nel
; j
++) {
2382 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2387 p
->symtab
[i
].nprim
= nprim
;
2391 p
->process_class
= string_to_security_class(p
, "process");
2392 if (!p
->process_class
)
2395 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2399 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2400 rc
= cond_read_list(p
, fp
);
2405 rc
= next_entry(buf
, fp
, sizeof(u32
));
2408 nel
= le32_to_cpu(buf
[0]);
2410 for (i
= 0; i
< nel
; i
++) {
2412 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2419 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2424 tr
->role
= le32_to_cpu(buf
[0]);
2425 tr
->type
= le32_to_cpu(buf
[1]);
2426 tr
->new_role
= le32_to_cpu(buf
[2]);
2427 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2428 rc
= next_entry(buf
, fp
, sizeof(u32
));
2431 tr
->tclass
= le32_to_cpu(buf
[0]);
2433 tr
->tclass
= p
->process_class
;
2436 if (!policydb_role_isvalid(p
, tr
->role
) ||
2437 !policydb_type_isvalid(p
, tr
->type
) ||
2438 !policydb_class_isvalid(p
, tr
->tclass
) ||
2439 !policydb_role_isvalid(p
, tr
->new_role
))
2444 rc
= next_entry(buf
, fp
, sizeof(u32
));
2447 nel
= le32_to_cpu(buf
[0]);
2449 for (i
= 0; i
< nel
; i
++) {
2451 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2458 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2463 ra
->role
= le32_to_cpu(buf
[0]);
2464 ra
->new_role
= le32_to_cpu(buf
[1]);
2465 if (!policydb_role_isvalid(p
, ra
->role
) ||
2466 !policydb_role_isvalid(p
, ra
->new_role
))
2471 rc
= filename_trans_read(p
, fp
);
2475 rc
= policydb_index(p
);
2480 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2481 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2482 if (!p
->process_trans_perms
)
2485 rc
= ocontext_read(p
, info
, fp
);
2489 rc
= genfs_read(p
, fp
);
2493 rc
= range_read(p
, fp
);
2497 p
->type_attr_map_array
= kvcalloc(p
->p_types
.nprim
,
2498 sizeof(*p
->type_attr_map_array
),
2500 if (!p
->type_attr_map_array
)
2503 /* just in case ebitmap_init() becomes more than just a memset(0): */
2504 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
2505 ebitmap_init(&p
->type_attr_map_array
[i
]);
2507 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2508 struct ebitmap
*e
= &p
->type_attr_map_array
[i
];
2510 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2511 rc
= ebitmap_read(e
, fp
);
2515 /* add the type itself as the degenerate case */
2516 rc
= ebitmap_set_bit(e
, i
, 1);
2521 rc
= policydb_bounds_sanity_check(p
);
2529 policydb_destroy(p
);
2534 * Write a MLS level structure to a policydb binary
2535 * representation file.
2537 static int mls_write_level(struct mls_level
*l
, void *fp
)
2542 buf
[0] = cpu_to_le32(l
->sens
);
2543 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2547 rc
= ebitmap_write(&l
->cat
, fp
);
2555 * Write a MLS range structure to a policydb binary
2556 * representation file.
2558 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2564 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2570 buf
[0] = cpu_to_le32(items
-1);
2571 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2573 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2575 BUG_ON(items
> ARRAY_SIZE(buf
));
2577 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2581 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2585 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2593 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2596 struct level_datum
*levdatum
= datum
;
2597 struct policy_data
*pd
= ptr
;
2604 buf
[0] = cpu_to_le32(len
);
2605 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2606 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2610 rc
= put_entry(key
, 1, len
, fp
);
2614 rc
= mls_write_level(levdatum
->level
, fp
);
2621 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2624 struct cat_datum
*catdatum
= datum
;
2625 struct policy_data
*pd
= ptr
;
2632 buf
[0] = cpu_to_le32(len
);
2633 buf
[1] = cpu_to_le32(catdatum
->value
);
2634 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2635 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2639 rc
= put_entry(key
, 1, len
, fp
);
2646 static int role_trans_write(struct policydb
*p
, void *fp
)
2648 struct role_trans
*r
= p
->role_tr
;
2649 struct role_trans
*tr
;
2655 for (tr
= r
; tr
; tr
= tr
->next
)
2657 buf
[0] = cpu_to_le32(nel
);
2658 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2661 for (tr
= r
; tr
; tr
= tr
->next
) {
2662 buf
[0] = cpu_to_le32(tr
->role
);
2663 buf
[1] = cpu_to_le32(tr
->type
);
2664 buf
[2] = cpu_to_le32(tr
->new_role
);
2665 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2668 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2669 buf
[0] = cpu_to_le32(tr
->tclass
);
2670 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2679 static int role_allow_write(struct role_allow
*r
, void *fp
)
2681 struct role_allow
*ra
;
2687 for (ra
= r
; ra
; ra
= ra
->next
)
2689 buf
[0] = cpu_to_le32(nel
);
2690 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2693 for (ra
= r
; ra
; ra
= ra
->next
) {
2694 buf
[0] = cpu_to_le32(ra
->role
);
2695 buf
[1] = cpu_to_le32(ra
->new_role
);
2696 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2704 * Write a security context structure
2705 * to a policydb binary representation file.
2707 static int context_write(struct policydb
*p
, struct context
*c
,
2713 buf
[0] = cpu_to_le32(c
->user
);
2714 buf
[1] = cpu_to_le32(c
->role
);
2715 buf
[2] = cpu_to_le32(c
->type
);
2717 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2721 rc
= mls_write_range_helper(&c
->range
, fp
);
2729 * The following *_write functions are used to
2730 * write the symbol data to a policy database
2731 * binary representation file.
2734 static int perm_write(void *vkey
, void *datum
, void *fp
)
2737 struct perm_datum
*perdatum
= datum
;
2743 buf
[0] = cpu_to_le32(len
);
2744 buf
[1] = cpu_to_le32(perdatum
->value
);
2745 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2749 rc
= put_entry(key
, 1, len
, fp
);
2756 static int common_write(void *vkey
, void *datum
, void *ptr
)
2759 struct common_datum
*comdatum
= datum
;
2760 struct policy_data
*pd
= ptr
;
2767 buf
[0] = cpu_to_le32(len
);
2768 buf
[1] = cpu_to_le32(comdatum
->value
);
2769 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2770 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2771 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2775 rc
= put_entry(key
, 1, len
, fp
);
2779 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2786 static int type_set_write(struct type_set
*t
, void *fp
)
2791 if (ebitmap_write(&t
->types
, fp
))
2793 if (ebitmap_write(&t
->negset
, fp
))
2796 buf
[0] = cpu_to_le32(t
->flags
);
2797 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2804 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2807 struct constraint_node
*c
;
2808 struct constraint_expr
*e
;
2813 for (c
= node
; c
; c
= c
->next
) {
2815 for (e
= c
->expr
; e
; e
= e
->next
)
2817 buf
[0] = cpu_to_le32(c
->permissions
);
2818 buf
[1] = cpu_to_le32(nel
);
2819 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2822 for (e
= c
->expr
; e
; e
= e
->next
) {
2823 buf
[0] = cpu_to_le32(e
->expr_type
);
2824 buf
[1] = cpu_to_le32(e
->attr
);
2825 buf
[2] = cpu_to_le32(e
->op
);
2826 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2830 switch (e
->expr_type
) {
2832 rc
= ebitmap_write(&e
->names
, fp
);
2835 if (p
->policyvers
>=
2836 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2837 rc
= type_set_write(e
->type_names
, fp
);
2851 static int class_write(void *vkey
, void *datum
, void *ptr
)
2854 struct class_datum
*cladatum
= datum
;
2855 struct policy_data
*pd
= ptr
;
2857 struct policydb
*p
= pd
->p
;
2858 struct constraint_node
*c
;
2865 if (cladatum
->comkey
)
2866 len2
= strlen(cladatum
->comkey
);
2871 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2874 buf
[0] = cpu_to_le32(len
);
2875 buf
[1] = cpu_to_le32(len2
);
2876 buf
[2] = cpu_to_le32(cladatum
->value
);
2877 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2878 if (cladatum
->permissions
.table
)
2879 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2882 buf
[5] = cpu_to_le32(ncons
);
2883 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2887 rc
= put_entry(key
, 1, len
, fp
);
2891 if (cladatum
->comkey
) {
2892 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2897 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2901 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2905 /* write out the validatetrans rule */
2907 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2910 buf
[0] = cpu_to_le32(ncons
);
2911 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2915 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2919 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2920 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2921 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2922 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2924 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2929 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2930 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2931 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2939 static int role_write(void *vkey
, void *datum
, void *ptr
)
2942 struct role_datum
*role
= datum
;
2943 struct policy_data
*pd
= ptr
;
2945 struct policydb
*p
= pd
->p
;
2952 buf
[items
++] = cpu_to_le32(len
);
2953 buf
[items
++] = cpu_to_le32(role
->value
);
2954 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2955 buf
[items
++] = cpu_to_le32(role
->bounds
);
2957 BUG_ON(items
> ARRAY_SIZE(buf
));
2959 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2963 rc
= put_entry(key
, 1, len
, fp
);
2967 rc
= ebitmap_write(&role
->dominates
, fp
);
2971 rc
= ebitmap_write(&role
->types
, fp
);
2978 static int type_write(void *vkey
, void *datum
, void *ptr
)
2981 struct type_datum
*typdatum
= datum
;
2982 struct policy_data
*pd
= ptr
;
2983 struct policydb
*p
= pd
->p
;
2991 buf
[items
++] = cpu_to_le32(len
);
2992 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2993 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2996 if (typdatum
->primary
)
2997 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2999 if (typdatum
->attribute
)
3000 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
3002 buf
[items
++] = cpu_to_le32(properties
);
3003 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
3005 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
3007 BUG_ON(items
> ARRAY_SIZE(buf
));
3008 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3012 rc
= put_entry(key
, 1, len
, fp
);
3019 static int user_write(void *vkey
, void *datum
, void *ptr
)
3022 struct user_datum
*usrdatum
= datum
;
3023 struct policy_data
*pd
= ptr
;
3024 struct policydb
*p
= pd
->p
;
3032 buf
[items
++] = cpu_to_le32(len
);
3033 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3034 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3035 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3036 BUG_ON(items
> ARRAY_SIZE(buf
));
3037 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3041 rc
= put_entry(key
, 1, len
, fp
);
3045 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3049 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3053 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3060 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3073 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3076 unsigned int i
, j
, rc
;
3078 __be64 prefixbuf
[1];
3082 for (i
= 0; i
< info
->ocon_num
; i
++) {
3084 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3086 buf
[0] = cpu_to_le32(nel
);
3087 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3090 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3093 buf
[0] = cpu_to_le32(c
->sid
[0]);
3094 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3097 rc
= context_write(p
, &c
->context
[0], fp
);
3103 len
= strlen(c
->u
.name
);
3104 buf
[0] = cpu_to_le32(len
);
3105 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3108 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3111 rc
= context_write(p
, &c
->context
[0], fp
);
3114 rc
= context_write(p
, &c
->context
[1], fp
);
3119 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3120 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3121 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3122 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3125 rc
= context_write(p
, &c
->context
[0], fp
);
3130 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3131 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3132 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3135 rc
= context_write(p
, &c
->context
[0], fp
);
3140 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3141 len
= strlen(c
->u
.name
);
3142 buf
[1] = cpu_to_le32(len
);
3143 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3146 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3149 rc
= context_write(p
, &c
->context
[0], fp
);
3154 for (j
= 0; j
< 4; j
++)
3155 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3156 for (j
= 0; j
< 4; j
++)
3157 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3158 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3161 rc
= context_write(p
, &c
->context
[0], fp
);
3166 /* subnet_prefix is in CPU order */
3167 prefixbuf
[0] = cpu_to_be64(c
->u
.ibpkey
.subnet_prefix
);
3169 rc
= put_entry(prefixbuf
, sizeof(u64
), 1, fp
);
3173 buf
[0] = cpu_to_le32(c
->u
.ibpkey
.low_pkey
);
3174 buf
[1] = cpu_to_le32(c
->u
.ibpkey
.high_pkey
);
3176 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3179 rc
= context_write(p
, &c
->context
[0], fp
);
3183 case OCON_IBENDPORT
:
3184 len
= strlen(c
->u
.ibendport
.dev_name
);
3185 buf
[0] = cpu_to_le32(len
);
3186 buf
[1] = cpu_to_le32(c
->u
.ibendport
.port
);
3187 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3190 rc
= put_entry(c
->u
.ibendport
.dev_name
, 1, len
, fp
);
3193 rc
= context_write(p
, &c
->context
[0], fp
);
3203 static int genfs_write(struct policydb
*p
, void *fp
)
3205 struct genfs
*genfs
;
3212 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3214 buf
[0] = cpu_to_le32(len
);
3215 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3218 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3219 len
= strlen(genfs
->fstype
);
3220 buf
[0] = cpu_to_le32(len
);
3221 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3224 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3228 for (c
= genfs
->head
; c
; c
= c
->next
)
3230 buf
[0] = cpu_to_le32(len
);
3231 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3234 for (c
= genfs
->head
; c
; c
= c
->next
) {
3235 len
= strlen(c
->u
.name
);
3236 buf
[0] = cpu_to_le32(len
);
3237 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3240 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3243 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3244 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3247 rc
= context_write(p
, &c
->context
[0], fp
);
3255 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3263 static int range_write_helper(void *key
, void *data
, void *ptr
)
3266 struct range_trans
*rt
= key
;
3267 struct mls_range
*r
= data
;
3268 struct policy_data
*pd
= ptr
;
3270 struct policydb
*p
= pd
->p
;
3273 buf
[0] = cpu_to_le32(rt
->source_type
);
3274 buf
[1] = cpu_to_le32(rt
->target_type
);
3275 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3278 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3279 buf
[0] = cpu_to_le32(rt
->target_class
);
3280 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3284 rc
= mls_write_range_helper(r
, fp
);
3291 static int range_write(struct policydb
*p
, void *fp
)
3295 struct policy_data pd
;
3300 /* count the number of entries in the hashtab */
3302 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3306 buf
[0] = cpu_to_le32(nel
);
3307 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3311 /* actually write all of the entries */
3312 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3319 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3322 struct filename_trans
*ft
= key
;
3323 struct filename_trans_datum
*otype
= data
;
3328 len
= strlen(ft
->name
);
3329 buf
[0] = cpu_to_le32(len
);
3330 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3334 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3338 buf
[0] = cpu_to_le32(ft
->stype
);
3339 buf
[1] = cpu_to_le32(ft
->ttype
);
3340 buf
[2] = cpu_to_le32(ft
->tclass
);
3341 buf
[3] = cpu_to_le32(otype
->otype
);
3343 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3350 static int filename_trans_write(struct policydb
*p
, void *fp
)
3356 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3360 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3364 buf
[0] = cpu_to_le32(nel
);
3365 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3369 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3377 * Write the configuration data in a policy database
3378 * structure to a policy database binary representation
3381 int policydb_write(struct policydb
*p
, void *fp
)
3383 unsigned int i
, num_syms
;
3388 struct policydb_compat_info
*info
;
3391 * refuse to write policy older than compressed avtab
3392 * to simplify the writer. There are other tests dropped
3393 * since we assume this throughout the writer code. Be
3394 * careful if you ever try to remove this restriction
3396 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3397 pr_err("SELinux: refusing to write policy version %d."
3398 " Because it is less than version %d\n", p
->policyvers
,
3399 POLICYDB_VERSION_AVTAB
);
3405 config
|= POLICYDB_CONFIG_MLS
;
3407 if (p
->reject_unknown
)
3408 config
|= REJECT_UNKNOWN
;
3409 if (p
->allow_unknown
)
3410 config
|= ALLOW_UNKNOWN
;
3412 /* Write the magic number and string identifiers. */
3413 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3414 len
= strlen(POLICYDB_STRING
);
3415 buf
[1] = cpu_to_le32(len
);
3416 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3419 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3423 /* Write the version, config, and table sizes. */
3424 info
= policydb_lookup_compat(p
->policyvers
);
3426 pr_err("SELinux: compatibility lookup failed for policy "
3427 "version %d", p
->policyvers
);
3431 buf
[0] = cpu_to_le32(p
->policyvers
);
3432 buf
[1] = cpu_to_le32(config
);
3433 buf
[2] = cpu_to_le32(info
->sym_num
);
3434 buf
[3] = cpu_to_le32(info
->ocon_num
);
3436 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3440 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3441 rc
= ebitmap_write(&p
->policycaps
, fp
);
3446 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3447 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3452 num_syms
= info
->sym_num
;
3453 for (i
= 0; i
< num_syms
; i
++) {
3454 struct policy_data pd
;
3459 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3460 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3462 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3465 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3470 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3474 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3478 rc
= role_trans_write(p
, fp
);
3482 rc
= role_allow_write(p
->role_allow
, fp
);
3486 rc
= filename_trans_write(p
, fp
);
3490 rc
= ocontext_write(p
, info
, fp
);
3494 rc
= genfs_write(p
, fp
);
3498 rc
= range_write(p
, fp
);
3502 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3503 struct ebitmap
*e
= &p
->type_attr_map_array
[i
];
3505 rc
= ebitmap_write(e
, fp
);