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 struct policydb_compat_info
{
65 /* These need to be updated if SYM_NUM or OCON_NUM changes */
66 static struct policydb_compat_info policydb_compat
[] = {
68 .version
= POLICYDB_VERSION_BASE
,
69 .sym_num
= SYM_NUM
- 3,
70 .ocon_num
= OCON_NUM
- 3,
73 .version
= POLICYDB_VERSION_BOOL
,
74 .sym_num
= SYM_NUM
- 2,
75 .ocon_num
= OCON_NUM
- 3,
78 .version
= POLICYDB_VERSION_IPV6
,
79 .sym_num
= SYM_NUM
- 2,
80 .ocon_num
= OCON_NUM
- 2,
83 .version
= POLICYDB_VERSION_NLCLASS
,
84 .sym_num
= SYM_NUM
- 2,
85 .ocon_num
= OCON_NUM
- 2,
88 .version
= POLICYDB_VERSION_MLS
,
90 .ocon_num
= OCON_NUM
- 2,
93 .version
= POLICYDB_VERSION_AVTAB
,
95 .ocon_num
= OCON_NUM
- 2,
98 .version
= POLICYDB_VERSION_RANGETRANS
,
100 .ocon_num
= OCON_NUM
- 2,
103 .version
= POLICYDB_VERSION_POLCAP
,
105 .ocon_num
= OCON_NUM
- 2,
108 .version
= POLICYDB_VERSION_PERMISSIVE
,
110 .ocon_num
= OCON_NUM
- 2,
113 .version
= POLICYDB_VERSION_BOUNDARY
,
115 .ocon_num
= OCON_NUM
- 2,
118 .version
= POLICYDB_VERSION_FILENAME_TRANS
,
120 .ocon_num
= OCON_NUM
- 2,
123 .version
= POLICYDB_VERSION_ROLETRANS
,
125 .ocon_num
= OCON_NUM
- 2,
128 .version
= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
,
130 .ocon_num
= OCON_NUM
- 2,
133 .version
= POLICYDB_VERSION_DEFAULT_TYPE
,
135 .ocon_num
= OCON_NUM
- 2,
138 .version
= POLICYDB_VERSION_CONSTRAINT_NAMES
,
140 .ocon_num
= OCON_NUM
- 2,
143 .version
= POLICYDB_VERSION_XPERMS_IOCTL
,
145 .ocon_num
= OCON_NUM
- 2,
148 .version
= POLICYDB_VERSION_INFINIBAND
,
150 .ocon_num
= OCON_NUM
,
153 .version
= POLICYDB_VERSION_GLBLUB
,
155 .ocon_num
= OCON_NUM
,
159 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
162 struct policydb_compat_info
*info
= NULL
;
164 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
165 if (policydb_compat
[i
].version
== version
) {
166 info
= &policydb_compat
[i
];
174 * The following *_destroy functions are used to
175 * free any memory allocated for each kind of
176 * symbol data in the policy database.
179 static int perm_destroy(void *key
, void *datum
, void *p
)
186 static int common_destroy(void *key
, void *datum
, void *p
)
188 struct common_datum
*comdatum
;
193 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
194 hashtab_destroy(comdatum
->permissions
.table
);
200 static void constraint_expr_destroy(struct constraint_expr
*expr
)
203 ebitmap_destroy(&expr
->names
);
204 if (expr
->type_names
) {
205 ebitmap_destroy(&expr
->type_names
->types
);
206 ebitmap_destroy(&expr
->type_names
->negset
);
207 kfree(expr
->type_names
);
213 static int cls_destroy(void *key
, void *datum
, void *p
)
215 struct class_datum
*cladatum
;
216 struct constraint_node
*constraint
, *ctemp
;
217 struct constraint_expr
*e
, *etmp
;
222 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
223 hashtab_destroy(cladatum
->permissions
.table
);
224 constraint
= cladatum
->constraints
;
226 e
= constraint
->expr
;
230 constraint_expr_destroy(etmp
);
233 constraint
= constraint
->next
;
237 constraint
= cladatum
->validatetrans
;
239 e
= constraint
->expr
;
243 constraint_expr_destroy(etmp
);
246 constraint
= constraint
->next
;
249 kfree(cladatum
->comkey
);
255 static int role_destroy(void *key
, void *datum
, void *p
)
257 struct role_datum
*role
;
262 ebitmap_destroy(&role
->dominates
);
263 ebitmap_destroy(&role
->types
);
269 static int type_destroy(void *key
, void *datum
, void *p
)
276 static int user_destroy(void *key
, void *datum
, void *p
)
278 struct user_datum
*usrdatum
;
283 ebitmap_destroy(&usrdatum
->roles
);
284 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
285 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
286 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
292 static int sens_destroy(void *key
, void *datum
, void *p
)
294 struct level_datum
*levdatum
;
300 ebitmap_destroy(&levdatum
->level
->cat
);
301 kfree(levdatum
->level
);
307 static int cat_destroy(void *key
, void *datum
, void *p
)
314 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
326 static int filenametr_destroy(void *key
, void *datum
, void *p
)
328 struct filename_trans_key
*ft
= key
;
329 struct filename_trans_datum
*next
, *d
= datum
;
334 ebitmap_destroy(&d
->stypes
);
338 } while (unlikely(d
));
343 static int range_tr_destroy(void *key
, void *datum
, void *p
)
345 struct mls_range
*rt
= datum
;
348 ebitmap_destroy(&rt
->level
[0].cat
);
349 ebitmap_destroy(&rt
->level
[1].cat
);
355 static void ocontext_destroy(struct ocontext
*c
, int i
)
360 context_destroy(&c
->context
[0]);
361 context_destroy(&c
->context
[1]);
362 if (i
== OCON_ISID
|| i
== OCON_FS
||
363 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
369 * Initialize the role table.
371 static int roles_init(struct policydb
*p
)
375 struct role_datum
*role
;
377 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
382 role
->value
= ++p
->p_roles
.nprim
;
383 if (role
->value
!= OBJECT_R_VAL
)
387 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
391 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
402 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
404 const struct filename_trans_key
*ft
= k
;
406 unsigned int byte_num
;
409 hash
= ft
->ttype
^ ft
->tclass
;
412 while ((focus
= ft
->name
[byte_num
++]))
413 hash
= partial_name_hash(focus
, hash
);
414 return hash
& (h
->size
- 1);
417 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
419 const struct filename_trans_key
*ft1
= k1
;
420 const struct filename_trans_key
*ft2
= k2
;
423 v
= ft1
->ttype
- ft2
->ttype
;
427 v
= ft1
->tclass
- ft2
->tclass
;
431 return strcmp(ft1
->name
, ft2
->name
);
435 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
437 const struct range_trans
*key
= k
;
439 return (key
->source_type
+ (key
->target_type
<< 3) +
440 (key
->target_class
<< 5)) & (h
->size
- 1);
443 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
445 const struct range_trans
*key1
= k1
, *key2
= k2
;
448 v
= key1
->source_type
- key2
->source_type
;
452 v
= key1
->target_type
- key2
->target_type
;
456 v
= key1
->target_class
- key2
->target_class
;
462 * Initialize a policy database structure.
464 static int policydb_init(struct policydb
*p
)
466 memset(p
, 0, sizeof(*p
));
468 avtab_init(&p
->te_avtab
);
469 cond_policydb_init(p
);
471 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
,
473 if (!p
->filename_trans
)
476 ebitmap_init(&p
->filename_trans_ttypes
);
477 ebitmap_init(&p
->policycaps
);
478 ebitmap_init(&p
->permissive_map
);
484 * The following *_index functions are used to
485 * define the val_to_name and val_to_struct arrays
486 * in a policy database structure. The val_to_name
487 * arrays are used when converting security context
488 * structures into string representations. The
489 * val_to_struct arrays are used when the attributes
490 * of a class, role, or user are needed.
493 static int common_index(void *key
, void *datum
, void *datap
)
496 struct common_datum
*comdatum
;
500 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
503 p
->sym_val_to_name
[SYM_COMMONS
][comdatum
->value
- 1] = key
;
508 static int class_index(void *key
, void *datum
, void *datap
)
511 struct class_datum
*cladatum
;
515 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
518 p
->sym_val_to_name
[SYM_CLASSES
][cladatum
->value
- 1] = key
;
519 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
523 static int role_index(void *key
, void *datum
, void *datap
)
526 struct role_datum
*role
;
531 || role
->value
> p
->p_roles
.nprim
532 || role
->bounds
> p
->p_roles
.nprim
)
535 p
->sym_val_to_name
[SYM_ROLES
][role
->value
- 1] = key
;
536 p
->role_val_to_struct
[role
->value
- 1] = role
;
540 static int type_index(void *key
, void *datum
, void *datap
)
543 struct type_datum
*typdatum
;
548 if (typdatum
->primary
) {
550 || typdatum
->value
> p
->p_types
.nprim
551 || typdatum
->bounds
> p
->p_types
.nprim
)
553 p
->sym_val_to_name
[SYM_TYPES
][typdatum
->value
- 1] = key
;
554 p
->type_val_to_struct
[typdatum
->value
- 1] = typdatum
;
560 static int user_index(void *key
, void *datum
, void *datap
)
563 struct user_datum
*usrdatum
;
568 || usrdatum
->value
> p
->p_users
.nprim
569 || usrdatum
->bounds
> p
->p_users
.nprim
)
572 p
->sym_val_to_name
[SYM_USERS
][usrdatum
->value
- 1] = key
;
573 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
577 static int sens_index(void *key
, void *datum
, void *datap
)
580 struct level_datum
*levdatum
;
585 if (!levdatum
->isalias
) {
586 if (!levdatum
->level
->sens
||
587 levdatum
->level
->sens
> p
->p_levels
.nprim
)
590 p
->sym_val_to_name
[SYM_LEVELS
][levdatum
->level
->sens
- 1] = key
;
596 static int cat_index(void *key
, void *datum
, void *datap
)
599 struct cat_datum
*catdatum
;
604 if (!catdatum
->isalias
) {
605 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
608 p
->sym_val_to_name
[SYM_CATS
][catdatum
->value
- 1] = key
;
614 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
627 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
629 struct hashtab_info info
;
631 hashtab_stat(h
, &info
);
632 pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
633 hash_name
, h
->nel
, info
.slots_used
, h
->size
,
637 static void symtab_hash_eval(struct symtab
*s
)
641 for (i
= 0; i
< SYM_NUM
; i
++)
642 hash_eval(s
[i
].table
, symtab_name
[i
]);
646 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
652 * Define the other val_to_name and val_to_struct arrays
653 * in a policy database structure.
655 * Caller must clean up on failure.
657 static int policydb_index(struct policydb
*p
)
662 pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
663 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
,
664 p
->p_bools
.nprim
, p
->p_levels
.nprim
, p
->p_cats
.nprim
);
666 pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
667 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
,
670 pr_debug("SELinux: %d classes, %d rules\n",
671 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
674 avtab_hash_eval(&p
->te_avtab
, "rules");
675 symtab_hash_eval(p
->symtab
);
678 p
->class_val_to_struct
= kcalloc(p
->p_classes
.nprim
,
679 sizeof(*p
->class_val_to_struct
),
681 if (!p
->class_val_to_struct
)
684 p
->role_val_to_struct
= kcalloc(p
->p_roles
.nprim
,
685 sizeof(*p
->role_val_to_struct
),
687 if (!p
->role_val_to_struct
)
690 p
->user_val_to_struct
= kcalloc(p
->p_users
.nprim
,
691 sizeof(*p
->user_val_to_struct
),
693 if (!p
->user_val_to_struct
)
696 p
->type_val_to_struct
= kvcalloc(p
->p_types
.nprim
,
697 sizeof(*p
->type_val_to_struct
),
699 if (!p
->type_val_to_struct
)
702 rc
= cond_init_bool_indexes(p
);
706 for (i
= 0; i
< SYM_NUM
; i
++) {
707 p
->sym_val_to_name
[i
] = kvcalloc(p
->symtab
[i
].nprim
,
710 if (!p
->sym_val_to_name
[i
])
713 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
723 * Free any memory allocated by a policy database structure.
725 void policydb_destroy(struct policydb
*p
)
727 struct ocontext
*c
, *ctmp
;
728 struct genfs
*g
, *gtmp
;
730 struct role_allow
*ra
, *lra
= NULL
;
731 struct role_trans
*tr
, *ltr
= NULL
;
733 for (i
= 0; i
< SYM_NUM
; i
++) {
735 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
736 hashtab_destroy(p
->symtab
[i
].table
);
739 for (i
= 0; i
< SYM_NUM
; i
++)
740 kvfree(p
->sym_val_to_name
[i
]);
742 kfree(p
->class_val_to_struct
);
743 kfree(p
->role_val_to_struct
);
744 kfree(p
->user_val_to_struct
);
745 kvfree(p
->type_val_to_struct
);
747 avtab_destroy(&p
->te_avtab
);
749 for (i
= 0; i
< OCON_NUM
; i
++) {
755 ocontext_destroy(ctmp
, i
);
757 p
->ocontexts
[i
] = NULL
;
768 ocontext_destroy(ctmp
, OCON_FSUSE
);
776 cond_policydb_destroy(p
);
778 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
785 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
792 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
793 hashtab_destroy(p
->filename_trans
);
795 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
796 hashtab_destroy(p
->range_tr
);
798 if (p
->type_attr_map_array
) {
799 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
800 ebitmap_destroy(&p
->type_attr_map_array
[i
]);
801 kvfree(p
->type_attr_map_array
);
804 ebitmap_destroy(&p
->filename_trans_ttypes
);
805 ebitmap_destroy(&p
->policycaps
);
806 ebitmap_destroy(&p
->permissive_map
);
810 * Load the initial SIDs specified in a policy database
811 * structure into a SID table.
813 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
815 struct ocontext
*head
, *c
;
820 pr_err("SELinux: out of memory on SID table init\n");
824 head
= p
->ocontexts
[OCON_ISID
];
825 for (c
= head
; c
; c
= c
->next
) {
827 const char *name
= security_get_initial_sid_context(sid
);
829 if (sid
== SECSID_NULL
) {
830 pr_err("SELinux: SID 0 was assigned a context.\n");
835 /* Ignore initial SIDs unused by this kernel. */
839 rc
= context_add_hash(p
, &c
->context
[0]);
844 rc
= sidtab_set_initial(s
, sid
, &c
->context
[0]);
846 pr_err("SELinux: unable to load initial SID %s.\n",
857 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
859 if (!class || class > p
->p_classes
.nprim
)
864 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
866 if (!role
|| role
> p
->p_roles
.nprim
)
871 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
873 if (!type
|| type
> p
->p_types
.nprim
)
879 * Return 1 if the fields in the security context
880 * structure `c' are valid. Return 0 otherwise.
882 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
884 struct role_datum
*role
;
885 struct user_datum
*usrdatum
;
887 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
890 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
893 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
896 if (c
->role
!= OBJECT_R_VAL
) {
898 * Role must be authorized for the type.
900 role
= p
->role_val_to_struct
[c
->role
- 1];
901 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
902 /* role may not be associated with type */
906 * User must be authorized for the role.
908 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
912 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
913 /* user may not be associated with role */
917 if (!mls_context_isvalid(p
, c
))
924 * Read a MLS range structure from a policydb binary
925 * representation file.
927 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
933 rc
= next_entry(buf
, fp
, sizeof(u32
));
938 items
= le32_to_cpu(buf
[0]);
939 if (items
> ARRAY_SIZE(buf
)) {
940 pr_err("SELinux: mls: range overflow\n");
944 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
946 pr_err("SELinux: mls: truncated range\n");
950 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
952 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
954 r
->level
[1].sens
= r
->level
[0].sens
;
956 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
958 pr_err("SELinux: mls: error reading low categories\n");
962 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
964 pr_err("SELinux: mls: error reading high categories\n");
968 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
970 pr_err("SELinux: mls: out of memory\n");
977 ebitmap_destroy(&r
->level
[0].cat
);
983 * Read and validate a security context structure
984 * from a policydb binary representation file.
986 static int context_read_and_validate(struct context
*c
,
993 rc
= next_entry(buf
, fp
, sizeof buf
);
995 pr_err("SELinux: context truncated\n");
998 c
->user
= le32_to_cpu(buf
[0]);
999 c
->role
= le32_to_cpu(buf
[1]);
1000 c
->type
= le32_to_cpu(buf
[2]);
1001 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1002 rc
= mls_read_range_helper(&c
->range
, fp
);
1004 pr_err("SELinux: error reading MLS range of context\n");
1010 if (!policydb_context_isvalid(p
, c
)) {
1011 pr_err("SELinux: invalid security context\n");
1021 * The following *_read functions are used to
1022 * read the symbol data from a policy database
1023 * binary representation file.
1026 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1031 if ((len
== 0) || (len
== (u32
)-1))
1034 str
= kmalloc(len
+ 1, flags
| __GFP_NOWARN
);
1038 rc
= next_entry(str
, fp
, len
);
1049 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1052 struct perm_datum
*perdatum
;
1057 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1061 rc
= next_entry(buf
, fp
, sizeof buf
);
1065 len
= le32_to_cpu(buf
[0]);
1066 perdatum
->value
= le32_to_cpu(buf
[1]);
1068 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1072 rc
= hashtab_insert(h
, key
, perdatum
);
1078 perm_destroy(key
, perdatum
, NULL
);
1082 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1085 struct common_datum
*comdatum
;
1090 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1094 rc
= next_entry(buf
, fp
, sizeof buf
);
1098 len
= le32_to_cpu(buf
[0]);
1099 comdatum
->value
= le32_to_cpu(buf
[1]);
1100 nel
= le32_to_cpu(buf
[3]);
1102 rc
= symtab_init(&comdatum
->permissions
, nel
);
1105 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1107 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1111 for (i
= 0; i
< nel
; i
++) {
1112 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1117 rc
= hashtab_insert(h
, key
, comdatum
);
1122 common_destroy(key
, comdatum
, NULL
);
1126 static void type_set_init(struct type_set
*t
)
1128 ebitmap_init(&t
->types
);
1129 ebitmap_init(&t
->negset
);
1132 static int type_set_read(struct type_set
*t
, void *fp
)
1137 if (ebitmap_read(&t
->types
, fp
))
1139 if (ebitmap_read(&t
->negset
, fp
))
1142 rc
= next_entry(buf
, fp
, sizeof(u32
));
1145 t
->flags
= le32_to_cpu(buf
[0]);
1151 static int read_cons_helper(struct policydb
*p
,
1152 struct constraint_node
**nodep
,
1153 int ncons
, int allowxtarget
, void *fp
)
1155 struct constraint_node
*c
, *lc
;
1156 struct constraint_expr
*e
, *le
;
1159 int rc
, i
, j
, depth
;
1162 for (i
= 0; i
< ncons
; i
++) {
1163 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1172 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1175 c
->permissions
= le32_to_cpu(buf
[0]);
1176 nexpr
= le32_to_cpu(buf
[1]);
1179 for (j
= 0; j
< nexpr
; j
++) {
1180 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1189 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1192 e
->expr_type
= le32_to_cpu(buf
[0]);
1193 e
->attr
= le32_to_cpu(buf
[1]);
1194 e
->op
= le32_to_cpu(buf
[2]);
1196 switch (e
->expr_type
) {
1208 if (depth
== (CEXPR_MAXDEPTH
- 1))
1213 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1215 if (depth
== (CEXPR_MAXDEPTH
- 1))
1218 rc
= ebitmap_read(&e
->names
, fp
);
1221 if (p
->policyvers
>=
1222 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1223 e
->type_names
= kzalloc(sizeof
1224 (*e
->type_names
), GFP_KERNEL
);
1227 type_set_init(e
->type_names
);
1228 rc
= type_set_read(e
->type_names
, fp
);
1246 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1249 struct class_datum
*cladatum
;
1251 u32 len
, len2
, ncons
, nel
;
1254 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1258 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1262 len
= le32_to_cpu(buf
[0]);
1263 len2
= le32_to_cpu(buf
[1]);
1264 cladatum
->value
= le32_to_cpu(buf
[2]);
1265 nel
= le32_to_cpu(buf
[4]);
1267 rc
= symtab_init(&cladatum
->permissions
, nel
);
1270 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1272 ncons
= le32_to_cpu(buf
[5]);
1274 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1279 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1284 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1285 if (!cladatum
->comdatum
) {
1286 pr_err("SELinux: unknown common %s\n",
1291 for (i
= 0; i
< nel
; i
++) {
1292 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1297 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1301 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1302 /* grab the validatetrans rules */
1303 rc
= next_entry(buf
, fp
, sizeof(u32
));
1306 ncons
= le32_to_cpu(buf
[0]);
1307 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1313 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1314 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1318 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1319 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1320 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1323 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1324 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1327 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1330 rc
= hashtab_insert(h
, key
, cladatum
);
1336 cls_destroy(key
, cladatum
, NULL
);
1340 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1343 struct role_datum
*role
;
1344 int rc
, to_read
= 2;
1348 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1352 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1355 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1359 len
= le32_to_cpu(buf
[0]);
1360 role
->value
= le32_to_cpu(buf
[1]);
1361 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1362 role
->bounds
= le32_to_cpu(buf
[2]);
1364 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1368 rc
= ebitmap_read(&role
->dominates
, fp
);
1372 rc
= ebitmap_read(&role
->types
, fp
);
1376 if (strcmp(key
, OBJECT_R
) == 0) {
1378 if (role
->value
!= OBJECT_R_VAL
) {
1379 pr_err("SELinux: Role %s has wrong value %d\n",
1380 OBJECT_R
, role
->value
);
1387 rc
= hashtab_insert(h
, key
, role
);
1392 role_destroy(key
, role
, NULL
);
1396 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1399 struct type_datum
*typdatum
;
1400 int rc
, to_read
= 3;
1404 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1408 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1411 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1415 len
= le32_to_cpu(buf
[0]);
1416 typdatum
->value
= le32_to_cpu(buf
[1]);
1417 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1418 u32 prop
= le32_to_cpu(buf
[2]);
1420 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1421 typdatum
->primary
= 1;
1422 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1423 typdatum
->attribute
= 1;
1425 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1427 typdatum
->primary
= le32_to_cpu(buf
[2]);
1430 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1434 rc
= hashtab_insert(h
, key
, typdatum
);
1439 type_destroy(key
, typdatum
, NULL
);
1445 * Read a MLS level structure from a policydb binary
1446 * representation file.
1448 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1453 memset(lp
, 0, sizeof(*lp
));
1455 rc
= next_entry(buf
, fp
, sizeof buf
);
1457 pr_err("SELinux: mls: truncated level\n");
1460 lp
->sens
= le32_to_cpu(buf
[0]);
1462 rc
= ebitmap_read(&lp
->cat
, fp
);
1464 pr_err("SELinux: mls: error reading level categories\n");
1470 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1473 struct user_datum
*usrdatum
;
1474 int rc
, to_read
= 2;
1478 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1482 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1485 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1489 len
= le32_to_cpu(buf
[0]);
1490 usrdatum
->value
= le32_to_cpu(buf
[1]);
1491 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1492 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1494 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1498 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1502 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1503 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1506 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1511 rc
= hashtab_insert(h
, key
, usrdatum
);
1516 user_destroy(key
, usrdatum
, NULL
);
1520 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1523 struct level_datum
*levdatum
;
1528 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1532 rc
= next_entry(buf
, fp
, sizeof buf
);
1536 len
= le32_to_cpu(buf
[0]);
1537 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1539 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1544 levdatum
->level
= kmalloc(sizeof(*levdatum
->level
), GFP_ATOMIC
);
1545 if (!levdatum
->level
)
1548 rc
= mls_read_level(levdatum
->level
, fp
);
1552 rc
= hashtab_insert(h
, key
, levdatum
);
1557 sens_destroy(key
, levdatum
, NULL
);
1561 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1564 struct cat_datum
*catdatum
;
1569 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1573 rc
= next_entry(buf
, fp
, sizeof buf
);
1577 len
= le32_to_cpu(buf
[0]);
1578 catdatum
->value
= le32_to_cpu(buf
[1]);
1579 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1581 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1585 rc
= hashtab_insert(h
, key
, catdatum
);
1590 cat_destroy(key
, catdatum
, NULL
);
1594 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1606 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1608 struct user_datum
*upper
, *user
;
1609 struct policydb
*p
= datap
;
1612 upper
= user
= datum
;
1613 while (upper
->bounds
) {
1614 struct ebitmap_node
*node
;
1617 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1618 pr_err("SELinux: user %s: "
1619 "too deep or looped boundary",
1624 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1625 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1626 if (ebitmap_get_bit(&upper
->roles
, bit
))
1629 pr_err("SELinux: boundary violated policy: "
1630 "user=%s role=%s bounds=%s\n",
1631 sym_name(p
, SYM_USERS
, user
->value
- 1),
1632 sym_name(p
, SYM_ROLES
, bit
),
1633 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1642 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1644 struct role_datum
*upper
, *role
;
1645 struct policydb
*p
= datap
;
1648 upper
= role
= datum
;
1649 while (upper
->bounds
) {
1650 struct ebitmap_node
*node
;
1653 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1654 pr_err("SELinux: role %s: "
1655 "too deep or looped bounds\n",
1660 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1661 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1662 if (ebitmap_get_bit(&upper
->types
, bit
))
1665 pr_err("SELinux: boundary violated policy: "
1666 "role=%s type=%s bounds=%s\n",
1667 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1668 sym_name(p
, SYM_TYPES
, bit
),
1669 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1678 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1680 struct type_datum
*upper
;
1681 struct policydb
*p
= datap
;
1685 while (upper
->bounds
) {
1686 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1687 pr_err("SELinux: type %s: "
1688 "too deep or looped boundary\n",
1693 upper
= p
->type_val_to_struct
[upper
->bounds
- 1];
1696 if (upper
->attribute
) {
1697 pr_err("SELinux: type %s: "
1698 "bounded by attribute %s",
1700 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1708 static int policydb_bounds_sanity_check(struct policydb
*p
)
1712 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1715 rc
= hashtab_map(p
->p_users
.table
,
1716 user_bounds_sanity_check
, p
);
1720 rc
= hashtab_map(p
->p_roles
.table
,
1721 role_bounds_sanity_check
, p
);
1725 rc
= hashtab_map(p
->p_types
.table
,
1726 type_bounds_sanity_check
, p
);
1733 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1735 struct class_datum
*cladatum
;
1737 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1741 return cladatum
->value
;
1744 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1746 struct class_datum
*cladatum
;
1747 struct perm_datum
*perdatum
= NULL
;
1748 struct common_datum
*comdatum
;
1750 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1753 cladatum
= p
->class_val_to_struct
[tclass
-1];
1754 comdatum
= cladatum
->comdatum
;
1756 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1759 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1764 return 1U << (perdatum
->value
-1);
1767 static int range_read(struct policydb
*p
, void *fp
)
1769 struct range_trans
*rt
= NULL
;
1770 struct mls_range
*r
= NULL
;
1775 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1778 rc
= next_entry(buf
, fp
, sizeof(u32
));
1782 nel
= le32_to_cpu(buf
[0]);
1784 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, nel
);
1788 for (i
= 0; i
< nel
; i
++) {
1790 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1794 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1798 rt
->source_type
= le32_to_cpu(buf
[0]);
1799 rt
->target_type
= le32_to_cpu(buf
[1]);
1800 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1801 rc
= next_entry(buf
, fp
, sizeof(u32
));
1804 rt
->target_class
= le32_to_cpu(buf
[0]);
1806 rt
->target_class
= p
->process_class
;
1809 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1810 !policydb_type_isvalid(p
, rt
->target_type
) ||
1811 !policydb_class_isvalid(p
, rt
->target_class
))
1815 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1819 rc
= mls_read_range_helper(r
, fp
);
1824 if (!mls_range_isvalid(p
, r
)) {
1825 pr_warn("SELinux: rangetrans: invalid range\n");
1829 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1836 hash_eval(p
->range_tr
, "rangetr");
1844 static int filename_trans_read_one(struct policydb
*p
, void *fp
)
1846 struct filename_trans_key key
, *ft
= NULL
;
1847 struct filename_trans_datum
*last
, *datum
= NULL
;
1849 u32 len
, stype
, otype
;
1853 /* length of the path component string */
1854 rc
= next_entry(buf
, fp
, sizeof(u32
));
1857 len
= le32_to_cpu(buf
[0]);
1859 /* path component string */
1860 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1864 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1868 stype
= le32_to_cpu(buf
[0]);
1869 key
.ttype
= le32_to_cpu(buf
[1]);
1870 key
.tclass
= le32_to_cpu(buf
[2]);
1873 otype
= le32_to_cpu(buf
[3]);
1876 datum
= hashtab_search(p
->filename_trans
, &key
);
1878 if (unlikely(ebitmap_get_bit(&datum
->stypes
, stype
- 1))) {
1879 /* conflicting/duplicate rules are ignored */
1883 if (likely(datum
->otype
== otype
))
1886 datum
= datum
->next
;
1890 datum
= kmalloc(sizeof(*datum
), GFP_KERNEL
);
1894 ebitmap_init(&datum
->stypes
);
1895 datum
->otype
= otype
;
1898 if (unlikely(last
)) {
1902 ft
= kmemdup(&key
, sizeof(key
), GFP_KERNEL
);
1906 rc
= hashtab_insert(p
->filename_trans
, ft
, datum
);
1911 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
,
1918 return ebitmap_set_bit(&datum
->stypes
, stype
- 1, 1);
1927 static int filename_trans_read(struct policydb
*p
, void *fp
)
1933 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1936 rc
= next_entry(buf
, fp
, sizeof(u32
));
1939 nel
= le32_to_cpu(buf
[0]);
1941 p
->filename_trans_count
= nel
;
1943 for (i
= 0; i
< nel
; i
++) {
1944 rc
= filename_trans_read_one(p
, fp
);
1948 hash_eval(p
->filename_trans
, "filenametr");
1952 static int genfs_read(struct policydb
*p
, void *fp
)
1955 u32 nel
, nel2
, len
, len2
;
1957 struct ocontext
*l
, *c
;
1958 struct ocontext
*newc
= NULL
;
1959 struct genfs
*genfs_p
, *genfs
;
1960 struct genfs
*newgenfs
= NULL
;
1962 rc
= next_entry(buf
, fp
, sizeof(u32
));
1965 nel
= le32_to_cpu(buf
[0]);
1967 for (i
= 0; i
< nel
; i
++) {
1968 rc
= next_entry(buf
, fp
, sizeof(u32
));
1971 len
= le32_to_cpu(buf
[0]);
1974 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1978 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
1982 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1983 genfs_p
= genfs
, genfs
= genfs
->next
) {
1985 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1986 pr_err("SELinux: dup genfs fstype %s\n",
1990 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
1993 newgenfs
->next
= genfs
;
1995 genfs_p
->next
= newgenfs
;
1997 p
->genfs
= newgenfs
;
2001 rc
= next_entry(buf
, fp
, sizeof(u32
));
2005 nel2
= le32_to_cpu(buf
[0]);
2006 for (j
= 0; j
< nel2
; j
++) {
2007 rc
= next_entry(buf
, fp
, sizeof(u32
));
2010 len
= le32_to_cpu(buf
[0]);
2013 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2017 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2021 rc
= next_entry(buf
, fp
, sizeof(u32
));
2025 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2026 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2030 for (l
= NULL
, c
= genfs
->head
; c
;
2031 l
= c
, c
= c
->next
) {
2033 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2034 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2035 newc
->v
.sclass
== c
->v
.sclass
)) {
2036 pr_err("SELinux: dup genfs entry (%s,%s)\n",
2037 genfs
->fstype
, c
->u
.name
);
2040 len
= strlen(newc
->u
.name
);
2041 len2
= strlen(c
->u
.name
);
2057 kfree(newgenfs
->fstype
);
2060 ocontext_destroy(newc
, OCON_FSUSE
);
2065 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2070 __be64 prefixbuf
[1];
2072 struct ocontext
*l
, *c
;
2075 for (i
= 0; i
< info
->ocon_num
; i
++) {
2076 rc
= next_entry(buf
, fp
, sizeof(u32
));
2079 nel
= le32_to_cpu(buf
[0]);
2082 for (j
= 0; j
< nel
; j
++) {
2084 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2090 p
->ocontexts
[i
] = c
;
2095 rc
= next_entry(buf
, fp
, sizeof(u32
));
2099 c
->sid
[0] = le32_to_cpu(buf
[0]);
2100 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2106 rc
= next_entry(buf
, fp
, sizeof(u32
));
2109 len
= le32_to_cpu(buf
[0]);
2111 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2115 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2118 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2123 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2126 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2127 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2128 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2129 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2134 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2137 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2138 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2139 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2144 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2149 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2150 /* Determined at runtime, not in policy DB. */
2151 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2153 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2156 len
= le32_to_cpu(buf
[1]);
2157 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2161 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2168 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2171 for (k
= 0; k
< 4; k
++)
2172 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2173 for (k
= 0; k
< 4; k
++)
2174 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2175 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2181 u32 pkey_lo
, pkey_hi
;
2183 rc
= next_entry(prefixbuf
, fp
, sizeof(u64
));
2187 /* we need to have subnet_prefix in CPU order */
2188 c
->u
.ibpkey
.subnet_prefix
= be64_to_cpu(prefixbuf
[0]);
2190 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2194 pkey_lo
= le32_to_cpu(buf
[0]);
2195 pkey_hi
= le32_to_cpu(buf
[1]);
2197 if (pkey_lo
> U16_MAX
|| pkey_hi
> U16_MAX
) {
2202 c
->u
.ibpkey
.low_pkey
= pkey_lo
;
2203 c
->u
.ibpkey
.high_pkey
= pkey_hi
;
2205 rc
= context_read_and_validate(&c
->context
[0],
2212 case OCON_IBENDPORT
: {
2215 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2218 len
= le32_to_cpu(buf
[0]);
2220 rc
= str_read(&c
->u
.ibendport
.dev_name
, GFP_KERNEL
, fp
, len
);
2224 port
= le32_to_cpu(buf
[1]);
2225 if (port
> U8_MAX
|| port
== 0) {
2230 c
->u
.ibendport
.port
= port
;
2232 rc
= context_read_and_validate(&c
->context
[0],
2248 * Read the configuration data from a policy database binary
2249 * representation file into a policy database structure.
2251 int policydb_read(struct policydb
*p
, void *fp
)
2253 struct role_allow
*ra
, *lra
;
2254 struct role_trans
*tr
, *ltr
;
2257 u32 len
, nprim
, nel
;
2260 struct policydb_compat_info
*info
;
2262 rc
= policydb_init(p
);
2266 /* Read the magic number and string length. */
2267 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2272 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2273 pr_err("SELinux: policydb magic number 0x%x does "
2274 "not match expected magic number 0x%x\n",
2275 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2280 len
= le32_to_cpu(buf
[1]);
2281 if (len
!= strlen(POLICYDB_STRING
)) {
2282 pr_err("SELinux: policydb string length %d does not "
2283 "match expected length %zu\n",
2284 len
, strlen(POLICYDB_STRING
));
2289 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2290 if (!policydb_str
) {
2291 pr_err("SELinux: unable to allocate memory for policydb "
2292 "string of length %d\n", len
);
2296 rc
= next_entry(policydb_str
, fp
, len
);
2298 pr_err("SELinux: truncated policydb string identifier\n");
2299 kfree(policydb_str
);
2304 policydb_str
[len
] = '\0';
2305 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2306 pr_err("SELinux: policydb string %s does not match "
2307 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2308 kfree(policydb_str
);
2311 /* Done with policydb_str. */
2312 kfree(policydb_str
);
2313 policydb_str
= NULL
;
2315 /* Read the version and table sizes. */
2316 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2321 p
->policyvers
= le32_to_cpu(buf
[0]);
2322 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2323 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2324 pr_err("SELinux: policydb version %d does not match "
2325 "my version range %d-%d\n",
2326 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2330 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2334 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2335 pr_err("SELinux: security policydb version %d "
2336 "(MLS) not backwards compatible\n",
2341 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2342 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2344 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2345 rc
= ebitmap_read(&p
->policycaps
, fp
);
2350 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2351 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2357 info
= policydb_lookup_compat(p
->policyvers
);
2359 pr_err("SELinux: unable to find policy compat info "
2360 "for version %d\n", p
->policyvers
);
2365 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2366 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2367 pr_err("SELinux: policydb table sizes (%d,%d) do "
2368 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2369 le32_to_cpu(buf
[3]),
2370 info
->sym_num
, info
->ocon_num
);
2374 for (i
= 0; i
< info
->sym_num
; i
++) {
2375 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2378 nprim
= le32_to_cpu(buf
[0]);
2379 nel
= le32_to_cpu(buf
[1]);
2381 rc
= symtab_init(&p
->symtab
[i
], nel
);
2385 if (i
== SYM_ROLES
) {
2391 for (j
= 0; j
< nel
; j
++) {
2392 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2397 p
->symtab
[i
].nprim
= nprim
;
2401 p
->process_class
= string_to_security_class(p
, "process");
2402 if (!p
->process_class
)
2405 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2409 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2410 rc
= cond_read_list(p
, fp
);
2415 rc
= next_entry(buf
, fp
, sizeof(u32
));
2418 nel
= le32_to_cpu(buf
[0]);
2420 for (i
= 0; i
< nel
; i
++) {
2422 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2429 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2434 tr
->role
= le32_to_cpu(buf
[0]);
2435 tr
->type
= le32_to_cpu(buf
[1]);
2436 tr
->new_role
= le32_to_cpu(buf
[2]);
2437 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2438 rc
= next_entry(buf
, fp
, sizeof(u32
));
2441 tr
->tclass
= le32_to_cpu(buf
[0]);
2443 tr
->tclass
= p
->process_class
;
2446 if (!policydb_role_isvalid(p
, tr
->role
) ||
2447 !policydb_type_isvalid(p
, tr
->type
) ||
2448 !policydb_class_isvalid(p
, tr
->tclass
) ||
2449 !policydb_role_isvalid(p
, tr
->new_role
))
2454 rc
= next_entry(buf
, fp
, sizeof(u32
));
2457 nel
= le32_to_cpu(buf
[0]);
2459 for (i
= 0; i
< nel
; i
++) {
2461 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2468 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2473 ra
->role
= le32_to_cpu(buf
[0]);
2474 ra
->new_role
= le32_to_cpu(buf
[1]);
2475 if (!policydb_role_isvalid(p
, ra
->role
) ||
2476 !policydb_role_isvalid(p
, ra
->new_role
))
2481 rc
= filename_trans_read(p
, fp
);
2485 rc
= policydb_index(p
);
2490 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2491 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2492 if (!p
->process_trans_perms
)
2495 rc
= ocontext_read(p
, info
, fp
);
2499 rc
= genfs_read(p
, fp
);
2503 rc
= range_read(p
, fp
);
2508 p
->type_attr_map_array
= kvcalloc(p
->p_types
.nprim
,
2509 sizeof(*p
->type_attr_map_array
),
2511 if (!p
->type_attr_map_array
)
2514 /* just in case ebitmap_init() becomes more than just a memset(0): */
2515 for (i
= 0; i
< p
->p_types
.nprim
; i
++)
2516 ebitmap_init(&p
->type_attr_map_array
[i
]);
2518 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2519 struct ebitmap
*e
= &p
->type_attr_map_array
[i
];
2521 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2522 rc
= ebitmap_read(e
, fp
);
2526 /* add the type itself as the degenerate case */
2527 rc
= ebitmap_set_bit(e
, i
, 1);
2532 rc
= policydb_bounds_sanity_check(p
);
2540 policydb_destroy(p
);
2545 * Write a MLS level structure to a policydb binary
2546 * representation file.
2548 static int mls_write_level(struct mls_level
*l
, void *fp
)
2553 buf
[0] = cpu_to_le32(l
->sens
);
2554 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2558 rc
= ebitmap_write(&l
->cat
, fp
);
2566 * Write a MLS range structure to a policydb binary
2567 * representation file.
2569 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2575 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2581 buf
[0] = cpu_to_le32(items
-1);
2582 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2584 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2586 BUG_ON(items
> ARRAY_SIZE(buf
));
2588 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2592 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2596 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2604 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2607 struct level_datum
*levdatum
= datum
;
2608 struct policy_data
*pd
= ptr
;
2615 buf
[0] = cpu_to_le32(len
);
2616 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2617 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2621 rc
= put_entry(key
, 1, len
, fp
);
2625 rc
= mls_write_level(levdatum
->level
, fp
);
2632 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2635 struct cat_datum
*catdatum
= datum
;
2636 struct policy_data
*pd
= ptr
;
2643 buf
[0] = cpu_to_le32(len
);
2644 buf
[1] = cpu_to_le32(catdatum
->value
);
2645 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2646 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2650 rc
= put_entry(key
, 1, len
, fp
);
2657 static int role_trans_write(struct policydb
*p
, void *fp
)
2659 struct role_trans
*r
= p
->role_tr
;
2660 struct role_trans
*tr
;
2666 for (tr
= r
; tr
; tr
= tr
->next
)
2668 buf
[0] = cpu_to_le32(nel
);
2669 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2672 for (tr
= r
; tr
; tr
= tr
->next
) {
2673 buf
[0] = cpu_to_le32(tr
->role
);
2674 buf
[1] = cpu_to_le32(tr
->type
);
2675 buf
[2] = cpu_to_le32(tr
->new_role
);
2676 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2679 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2680 buf
[0] = cpu_to_le32(tr
->tclass
);
2681 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2690 static int role_allow_write(struct role_allow
*r
, void *fp
)
2692 struct role_allow
*ra
;
2698 for (ra
= r
; ra
; ra
= ra
->next
)
2700 buf
[0] = cpu_to_le32(nel
);
2701 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2704 for (ra
= r
; ra
; ra
= ra
->next
) {
2705 buf
[0] = cpu_to_le32(ra
->role
);
2706 buf
[1] = cpu_to_le32(ra
->new_role
);
2707 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2715 * Write a security context structure
2716 * to a policydb binary representation file.
2718 static int context_write(struct policydb
*p
, struct context
*c
,
2724 buf
[0] = cpu_to_le32(c
->user
);
2725 buf
[1] = cpu_to_le32(c
->role
);
2726 buf
[2] = cpu_to_le32(c
->type
);
2728 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2732 rc
= mls_write_range_helper(&c
->range
, fp
);
2740 * The following *_write functions are used to
2741 * write the symbol data to a policy database
2742 * binary representation file.
2745 static int perm_write(void *vkey
, void *datum
, void *fp
)
2748 struct perm_datum
*perdatum
= datum
;
2754 buf
[0] = cpu_to_le32(len
);
2755 buf
[1] = cpu_to_le32(perdatum
->value
);
2756 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2760 rc
= put_entry(key
, 1, len
, fp
);
2767 static int common_write(void *vkey
, void *datum
, void *ptr
)
2770 struct common_datum
*comdatum
= datum
;
2771 struct policy_data
*pd
= ptr
;
2778 buf
[0] = cpu_to_le32(len
);
2779 buf
[1] = cpu_to_le32(comdatum
->value
);
2780 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2781 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2782 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2786 rc
= put_entry(key
, 1, len
, fp
);
2790 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2797 static int type_set_write(struct type_set
*t
, void *fp
)
2802 if (ebitmap_write(&t
->types
, fp
))
2804 if (ebitmap_write(&t
->negset
, fp
))
2807 buf
[0] = cpu_to_le32(t
->flags
);
2808 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2815 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2818 struct constraint_node
*c
;
2819 struct constraint_expr
*e
;
2824 for (c
= node
; c
; c
= c
->next
) {
2826 for (e
= c
->expr
; e
; e
= e
->next
)
2828 buf
[0] = cpu_to_le32(c
->permissions
);
2829 buf
[1] = cpu_to_le32(nel
);
2830 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2833 for (e
= c
->expr
; e
; e
= e
->next
) {
2834 buf
[0] = cpu_to_le32(e
->expr_type
);
2835 buf
[1] = cpu_to_le32(e
->attr
);
2836 buf
[2] = cpu_to_le32(e
->op
);
2837 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2841 switch (e
->expr_type
) {
2843 rc
= ebitmap_write(&e
->names
, fp
);
2846 if (p
->policyvers
>=
2847 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2848 rc
= type_set_write(e
->type_names
, fp
);
2862 static int class_write(void *vkey
, void *datum
, void *ptr
)
2865 struct class_datum
*cladatum
= datum
;
2866 struct policy_data
*pd
= ptr
;
2868 struct policydb
*p
= pd
->p
;
2869 struct constraint_node
*c
;
2876 if (cladatum
->comkey
)
2877 len2
= strlen(cladatum
->comkey
);
2882 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2885 buf
[0] = cpu_to_le32(len
);
2886 buf
[1] = cpu_to_le32(len2
);
2887 buf
[2] = cpu_to_le32(cladatum
->value
);
2888 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2889 if (cladatum
->permissions
.table
)
2890 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2893 buf
[5] = cpu_to_le32(ncons
);
2894 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2898 rc
= put_entry(key
, 1, len
, fp
);
2902 if (cladatum
->comkey
) {
2903 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2908 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2912 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2916 /* write out the validatetrans rule */
2918 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2921 buf
[0] = cpu_to_le32(ncons
);
2922 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2926 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2930 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2931 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2932 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2933 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2935 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2940 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2941 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2942 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2950 static int role_write(void *vkey
, void *datum
, void *ptr
)
2953 struct role_datum
*role
= datum
;
2954 struct policy_data
*pd
= ptr
;
2956 struct policydb
*p
= pd
->p
;
2963 buf
[items
++] = cpu_to_le32(len
);
2964 buf
[items
++] = cpu_to_le32(role
->value
);
2965 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2966 buf
[items
++] = cpu_to_le32(role
->bounds
);
2968 BUG_ON(items
> ARRAY_SIZE(buf
));
2970 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2974 rc
= put_entry(key
, 1, len
, fp
);
2978 rc
= ebitmap_write(&role
->dominates
, fp
);
2982 rc
= ebitmap_write(&role
->types
, fp
);
2989 static int type_write(void *vkey
, void *datum
, void *ptr
)
2992 struct type_datum
*typdatum
= datum
;
2993 struct policy_data
*pd
= ptr
;
2994 struct policydb
*p
= pd
->p
;
3002 buf
[items
++] = cpu_to_le32(len
);
3003 buf
[items
++] = cpu_to_le32(typdatum
->value
);
3004 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
3007 if (typdatum
->primary
)
3008 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
3010 if (typdatum
->attribute
)
3011 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
3013 buf
[items
++] = cpu_to_le32(properties
);
3014 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
3016 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
3018 BUG_ON(items
> ARRAY_SIZE(buf
));
3019 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3023 rc
= put_entry(key
, 1, len
, fp
);
3030 static int user_write(void *vkey
, void *datum
, void *ptr
)
3033 struct user_datum
*usrdatum
= datum
;
3034 struct policy_data
*pd
= ptr
;
3035 struct policydb
*p
= pd
->p
;
3043 buf
[items
++] = cpu_to_le32(len
);
3044 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3045 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3046 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3047 BUG_ON(items
> ARRAY_SIZE(buf
));
3048 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3052 rc
= put_entry(key
, 1, len
, fp
);
3056 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3060 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3064 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3071 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3084 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3087 unsigned int i
, j
, rc
;
3089 __be64 prefixbuf
[1];
3093 for (i
= 0; i
< info
->ocon_num
; i
++) {
3095 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3097 buf
[0] = cpu_to_le32(nel
);
3098 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3101 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3104 buf
[0] = cpu_to_le32(c
->sid
[0]);
3105 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3108 rc
= context_write(p
, &c
->context
[0], fp
);
3114 len
= strlen(c
->u
.name
);
3115 buf
[0] = cpu_to_le32(len
);
3116 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3119 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3122 rc
= context_write(p
, &c
->context
[0], fp
);
3125 rc
= context_write(p
, &c
->context
[1], fp
);
3130 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3131 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3132 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3133 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3136 rc
= context_write(p
, &c
->context
[0], fp
);
3141 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3142 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3143 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3146 rc
= context_write(p
, &c
->context
[0], fp
);
3151 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3152 len
= strlen(c
->u
.name
);
3153 buf
[1] = cpu_to_le32(len
);
3154 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3157 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3160 rc
= context_write(p
, &c
->context
[0], fp
);
3165 for (j
= 0; j
< 4; j
++)
3166 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3167 for (j
= 0; j
< 4; j
++)
3168 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3169 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3172 rc
= context_write(p
, &c
->context
[0], fp
);
3177 /* subnet_prefix is in CPU order */
3178 prefixbuf
[0] = cpu_to_be64(c
->u
.ibpkey
.subnet_prefix
);
3180 rc
= put_entry(prefixbuf
, sizeof(u64
), 1, fp
);
3184 buf
[0] = cpu_to_le32(c
->u
.ibpkey
.low_pkey
);
3185 buf
[1] = cpu_to_le32(c
->u
.ibpkey
.high_pkey
);
3187 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3190 rc
= context_write(p
, &c
->context
[0], fp
);
3194 case OCON_IBENDPORT
:
3195 len
= strlen(c
->u
.ibendport
.dev_name
);
3196 buf
[0] = cpu_to_le32(len
);
3197 buf
[1] = cpu_to_le32(c
->u
.ibendport
.port
);
3198 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3201 rc
= put_entry(c
->u
.ibendport
.dev_name
, 1, len
, fp
);
3204 rc
= context_write(p
, &c
->context
[0], fp
);
3214 static int genfs_write(struct policydb
*p
, void *fp
)
3216 struct genfs
*genfs
;
3223 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3225 buf
[0] = cpu_to_le32(len
);
3226 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3229 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3230 len
= strlen(genfs
->fstype
);
3231 buf
[0] = cpu_to_le32(len
);
3232 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3235 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3239 for (c
= genfs
->head
; c
; c
= c
->next
)
3241 buf
[0] = cpu_to_le32(len
);
3242 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3245 for (c
= genfs
->head
; c
; c
= c
->next
) {
3246 len
= strlen(c
->u
.name
);
3247 buf
[0] = cpu_to_le32(len
);
3248 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3251 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3254 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3255 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3258 rc
= context_write(p
, &c
->context
[0], fp
);
3266 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3274 static int range_write_helper(void *key
, void *data
, void *ptr
)
3277 struct range_trans
*rt
= key
;
3278 struct mls_range
*r
= data
;
3279 struct policy_data
*pd
= ptr
;
3281 struct policydb
*p
= pd
->p
;
3284 buf
[0] = cpu_to_le32(rt
->source_type
);
3285 buf
[1] = cpu_to_le32(rt
->target_type
);
3286 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3289 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3290 buf
[0] = cpu_to_le32(rt
->target_class
);
3291 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3295 rc
= mls_write_range_helper(r
, fp
);
3302 static int range_write(struct policydb
*p
, void *fp
)
3306 struct policy_data pd
;
3311 /* count the number of entries in the hashtab */
3313 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3317 buf
[0] = cpu_to_le32(nel
);
3318 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3322 /* actually write all of the entries */
3323 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3330 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3332 struct filename_trans_key
*ft
= key
;
3333 struct filename_trans_datum
*datum
= data
;
3334 struct ebitmap_node
*node
;
3338 u32 bit
, len
= strlen(ft
->name
);
3341 ebitmap_for_each_positive_bit(&datum
->stypes
, node
, bit
) {
3342 buf
[0] = cpu_to_le32(len
);
3343 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3347 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3351 buf
[0] = cpu_to_le32(bit
+ 1);
3352 buf
[1] = cpu_to_le32(ft
->ttype
);
3353 buf
[2] = cpu_to_le32(ft
->tclass
);
3354 buf
[3] = cpu_to_le32(datum
->otype
);
3356 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3361 datum
= datum
->next
;
3362 } while (unlikely(datum
));
3367 static int filename_trans_write(struct policydb
*p
, void *fp
)
3372 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3375 buf
[0] = cpu_to_le32(p
->filename_trans_count
);
3376 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3380 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3388 * Write the configuration data in a policy database
3389 * structure to a policy database binary representation
3392 int policydb_write(struct policydb
*p
, void *fp
)
3394 unsigned int i
, num_syms
;
3399 struct policydb_compat_info
*info
;
3402 * refuse to write policy older than compressed avtab
3403 * to simplify the writer. There are other tests dropped
3404 * since we assume this throughout the writer code. Be
3405 * careful if you ever try to remove this restriction
3407 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3408 pr_err("SELinux: refusing to write policy version %d."
3409 " Because it is less than version %d\n", p
->policyvers
,
3410 POLICYDB_VERSION_AVTAB
);
3416 config
|= POLICYDB_CONFIG_MLS
;
3418 if (p
->reject_unknown
)
3419 config
|= REJECT_UNKNOWN
;
3420 if (p
->allow_unknown
)
3421 config
|= ALLOW_UNKNOWN
;
3423 /* Write the magic number and string identifiers. */
3424 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3425 len
= strlen(POLICYDB_STRING
);
3426 buf
[1] = cpu_to_le32(len
);
3427 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3430 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3434 /* Write the version, config, and table sizes. */
3435 info
= policydb_lookup_compat(p
->policyvers
);
3437 pr_err("SELinux: compatibility lookup failed for policy "
3438 "version %d", p
->policyvers
);
3442 buf
[0] = cpu_to_le32(p
->policyvers
);
3443 buf
[1] = cpu_to_le32(config
);
3444 buf
[2] = cpu_to_le32(info
->sym_num
);
3445 buf
[3] = cpu_to_le32(info
->ocon_num
);
3447 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3451 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3452 rc
= ebitmap_write(&p
->policycaps
, fp
);
3457 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3458 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3463 num_syms
= info
->sym_num
;
3464 for (i
= 0; i
< num_syms
; i
++) {
3465 struct policy_data pd
;
3470 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3471 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3473 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3476 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3481 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3485 rc
= cond_write_list(p
, fp
);
3489 rc
= role_trans_write(p
, fp
);
3493 rc
= role_allow_write(p
->role_allow
, fp
);
3497 rc
= filename_trans_write(p
, fp
);
3501 rc
= ocontext_write(p
, info
, fp
);
3505 rc
= genfs_write(p
, fp
);
3509 rc
= range_write(p
, fp
);
3513 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3514 struct ebitmap
*e
= &p
->type_attr_map_array
[i
];
3516 rc
= ebitmap_write(e
, fp
);