2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
10 * Support for enhanced MLS infrastructure.
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14 * Added conditional policy language extensions
16 * Updated: Hewlett-Packard <paul@paul-moore.com>
18 * Added support for the policy capability bitmap
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
21 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, version 2.
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/audit.h>
34 #include <linux/flex_array.h>
38 #include "conditional.h"
45 static const char *symtab_name
[SYM_NUM
] = {
57 static unsigned int symtab_sizes
[SYM_NUM
] = {
68 struct policydb_compat_info
{
74 /* These need to be updated if SYM_NUM or OCON_NUM changes */
75 static struct policydb_compat_info policydb_compat
[] = {
77 .version
= POLICYDB_VERSION_BASE
,
78 .sym_num
= SYM_NUM
- 3,
79 .ocon_num
= OCON_NUM
- 1,
82 .version
= POLICYDB_VERSION_BOOL
,
83 .sym_num
= SYM_NUM
- 2,
84 .ocon_num
= OCON_NUM
- 1,
87 .version
= POLICYDB_VERSION_IPV6
,
88 .sym_num
= SYM_NUM
- 2,
92 .version
= POLICYDB_VERSION_NLCLASS
,
93 .sym_num
= SYM_NUM
- 2,
97 .version
= POLICYDB_VERSION_MLS
,
102 .version
= POLICYDB_VERSION_AVTAB
,
104 .ocon_num
= OCON_NUM
,
107 .version
= POLICYDB_VERSION_RANGETRANS
,
109 .ocon_num
= OCON_NUM
,
112 .version
= POLICYDB_VERSION_POLCAP
,
114 .ocon_num
= OCON_NUM
,
117 .version
= POLICYDB_VERSION_PERMISSIVE
,
119 .ocon_num
= OCON_NUM
,
122 .version
= POLICYDB_VERSION_BOUNDARY
,
124 .ocon_num
= OCON_NUM
,
127 .version
= POLICYDB_VERSION_FILENAME_TRANS
,
129 .ocon_num
= OCON_NUM
,
132 .version
= POLICYDB_VERSION_ROLETRANS
,
134 .ocon_num
= OCON_NUM
,
137 .version
= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
,
139 .ocon_num
= OCON_NUM
,
142 .version
= POLICYDB_VERSION_DEFAULT_TYPE
,
144 .ocon_num
= OCON_NUM
,
148 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
151 struct policydb_compat_info
*info
= NULL
;
153 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
154 if (policydb_compat
[i
].version
== version
) {
155 info
= &policydb_compat
[i
];
163 * Initialize the role table.
165 static int roles_init(struct policydb
*p
)
169 struct role_datum
*role
;
172 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
177 role
->value
= ++p
->p_roles
.nprim
;
178 if (role
->value
!= OBJECT_R_VAL
)
182 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
186 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
197 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
199 const struct filename_trans
*ft
= k
;
201 unsigned int byte_num
;
204 hash
= ft
->stype
^ ft
->ttype
^ ft
->tclass
;
207 while ((focus
= ft
->name
[byte_num
++]))
208 hash
= partial_name_hash(focus
, hash
);
209 return hash
& (h
->size
- 1);
212 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
214 const struct filename_trans
*ft1
= k1
;
215 const struct filename_trans
*ft2
= k2
;
218 v
= ft1
->stype
- ft2
->stype
;
222 v
= ft1
->ttype
- ft2
->ttype
;
226 v
= ft1
->tclass
- ft2
->tclass
;
230 return strcmp(ft1
->name
, ft2
->name
);
234 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
236 const struct range_trans
*key
= k
;
237 return (key
->source_type
+ (key
->target_type
<< 3) +
238 (key
->target_class
<< 5)) & (h
->size
- 1);
241 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
243 const struct range_trans
*key1
= k1
, *key2
= k2
;
246 v
= key1
->source_type
- key2
->source_type
;
250 v
= key1
->target_type
- key2
->target_type
;
254 v
= key1
->target_class
- key2
->target_class
;
260 * Initialize a policy database structure.
262 static int policydb_init(struct policydb
*p
)
266 memset(p
, 0, sizeof(*p
));
268 for (i
= 0; i
< SYM_NUM
; i
++) {
269 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
274 rc
= avtab_init(&p
->te_avtab
);
282 rc
= cond_policydb_init(p
);
286 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
287 if (!p
->filename_trans
)
290 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
294 ebitmap_init(&p
->filename_trans_ttypes
);
295 ebitmap_init(&p
->policycaps
);
296 ebitmap_init(&p
->permissive_map
);
300 hashtab_destroy(p
->filename_trans
);
301 hashtab_destroy(p
->range_tr
);
302 for (i
= 0; i
< SYM_NUM
; i
++)
303 hashtab_destroy(p
->symtab
[i
].table
);
308 * The following *_index functions are used to
309 * define the val_to_name and val_to_struct arrays
310 * in a policy database structure. The val_to_name
311 * arrays are used when converting security context
312 * structures into string representations. The
313 * val_to_struct arrays are used when the attributes
314 * of a class, role, or user are needed.
317 static int common_index(void *key
, void *datum
, void *datap
)
320 struct common_datum
*comdatum
;
321 struct flex_array
*fa
;
325 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
328 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
329 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
330 GFP_KERNEL
| __GFP_ZERO
))
335 static int class_index(void *key
, void *datum
, void *datap
)
338 struct class_datum
*cladatum
;
339 struct flex_array
*fa
;
343 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
345 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
346 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
347 GFP_KERNEL
| __GFP_ZERO
))
349 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
353 static int role_index(void *key
, void *datum
, void *datap
)
356 struct role_datum
*role
;
357 struct flex_array
*fa
;
362 || role
->value
> p
->p_roles
.nprim
363 || role
->bounds
> p
->p_roles
.nprim
)
366 fa
= p
->sym_val_to_name
[SYM_ROLES
];
367 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
368 GFP_KERNEL
| __GFP_ZERO
))
370 p
->role_val_to_struct
[role
->value
- 1] = role
;
374 static int type_index(void *key
, void *datum
, void *datap
)
377 struct type_datum
*typdatum
;
378 struct flex_array
*fa
;
383 if (typdatum
->primary
) {
385 || typdatum
->value
> p
->p_types
.nprim
386 || typdatum
->bounds
> p
->p_types
.nprim
)
388 fa
= p
->sym_val_to_name
[SYM_TYPES
];
389 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
390 GFP_KERNEL
| __GFP_ZERO
))
393 fa
= p
->type_val_to_struct_array
;
394 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
395 GFP_KERNEL
| __GFP_ZERO
))
402 static int user_index(void *key
, void *datum
, void *datap
)
405 struct user_datum
*usrdatum
;
406 struct flex_array
*fa
;
411 || usrdatum
->value
> p
->p_users
.nprim
412 || usrdatum
->bounds
> p
->p_users
.nprim
)
415 fa
= p
->sym_val_to_name
[SYM_USERS
];
416 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
417 GFP_KERNEL
| __GFP_ZERO
))
419 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
423 static int sens_index(void *key
, void *datum
, void *datap
)
426 struct level_datum
*levdatum
;
427 struct flex_array
*fa
;
432 if (!levdatum
->isalias
) {
433 if (!levdatum
->level
->sens
||
434 levdatum
->level
->sens
> p
->p_levels
.nprim
)
436 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
437 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
438 GFP_KERNEL
| __GFP_ZERO
))
445 static int cat_index(void *key
, void *datum
, void *datap
)
448 struct cat_datum
*catdatum
;
449 struct flex_array
*fa
;
454 if (!catdatum
->isalias
) {
455 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
457 fa
= p
->sym_val_to_name
[SYM_CATS
];
458 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
459 GFP_KERNEL
| __GFP_ZERO
))
466 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
479 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
481 struct hashtab_info info
;
483 hashtab_stat(h
, &info
);
484 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
485 "longest chain length %d\n", hash_name
, h
->nel
,
486 info
.slots_used
, h
->size
, info
.max_chain_len
);
489 static void symtab_hash_eval(struct symtab
*s
)
493 for (i
= 0; i
< SYM_NUM
; i
++)
494 hash_eval(s
[i
].table
, symtab_name
[i
]);
498 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
504 * Define the other val_to_name and val_to_struct arrays
505 * in a policy database structure.
507 * Caller must clean up on failure.
509 static int policydb_index(struct policydb
*p
)
513 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
514 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
516 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
520 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
521 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
524 avtab_hash_eval(&p
->te_avtab
, "rules");
525 symtab_hash_eval(p
->symtab
);
529 p
->class_val_to_struct
=
530 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
532 if (!p
->class_val_to_struct
)
536 p
->role_val_to_struct
=
537 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
539 if (!p
->role_val_to_struct
)
543 p
->user_val_to_struct
=
544 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
546 if (!p
->user_val_to_struct
)
549 /* Yes, I want the sizeof the pointer, not the structure */
551 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
553 GFP_KERNEL
| __GFP_ZERO
);
554 if (!p
->type_val_to_struct_array
)
557 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
558 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
562 rc
= cond_init_bool_indexes(p
);
566 for (i
= 0; i
< SYM_NUM
; i
++) {
568 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
570 GFP_KERNEL
| __GFP_ZERO
);
571 if (!p
->sym_val_to_name
[i
])
574 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
575 0, p
->symtab
[i
].nprim
,
576 GFP_KERNEL
| __GFP_ZERO
);
580 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
590 * The following *_destroy functions are used to
591 * free any memory allocated for each kind of
592 * symbol data in the policy database.
595 static int perm_destroy(void *key
, void *datum
, void *p
)
602 static int common_destroy(void *key
, void *datum
, void *p
)
604 struct common_datum
*comdatum
;
609 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
610 hashtab_destroy(comdatum
->permissions
.table
);
616 static int cls_destroy(void *key
, void *datum
, void *p
)
618 struct class_datum
*cladatum
;
619 struct constraint_node
*constraint
, *ctemp
;
620 struct constraint_expr
*e
, *etmp
;
625 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
626 hashtab_destroy(cladatum
->permissions
.table
);
627 constraint
= cladatum
->constraints
;
629 e
= constraint
->expr
;
631 ebitmap_destroy(&e
->names
);
637 constraint
= constraint
->next
;
641 constraint
= cladatum
->validatetrans
;
643 e
= constraint
->expr
;
645 ebitmap_destroy(&e
->names
);
651 constraint
= constraint
->next
;
655 kfree(cladatum
->comkey
);
661 static int role_destroy(void *key
, void *datum
, void *p
)
663 struct role_datum
*role
;
668 ebitmap_destroy(&role
->dominates
);
669 ebitmap_destroy(&role
->types
);
675 static int type_destroy(void *key
, void *datum
, void *p
)
682 static int user_destroy(void *key
, void *datum
, void *p
)
684 struct user_datum
*usrdatum
;
689 ebitmap_destroy(&usrdatum
->roles
);
690 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
691 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
692 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
698 static int sens_destroy(void *key
, void *datum
, void *p
)
700 struct level_datum
*levdatum
;
705 ebitmap_destroy(&levdatum
->level
->cat
);
706 kfree(levdatum
->level
);
712 static int cat_destroy(void *key
, void *datum
, void *p
)
719 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
731 static int filenametr_destroy(void *key
, void *datum
, void *p
)
733 struct filename_trans
*ft
= key
;
741 static int range_tr_destroy(void *key
, void *datum
, void *p
)
743 struct mls_range
*rt
= datum
;
745 ebitmap_destroy(&rt
->level
[0].cat
);
746 ebitmap_destroy(&rt
->level
[1].cat
);
752 static void ocontext_destroy(struct ocontext
*c
, int i
)
757 context_destroy(&c
->context
[0]);
758 context_destroy(&c
->context
[1]);
759 if (i
== OCON_ISID
|| i
== OCON_FS
||
760 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
766 * Free any memory allocated by a policy database structure.
768 void policydb_destroy(struct policydb
*p
)
770 struct ocontext
*c
, *ctmp
;
771 struct genfs
*g
, *gtmp
;
773 struct role_allow
*ra
, *lra
= NULL
;
774 struct role_trans
*tr
, *ltr
= NULL
;
776 for (i
= 0; i
< SYM_NUM
; i
++) {
778 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
779 hashtab_destroy(p
->symtab
[i
].table
);
782 for (i
= 0; i
< SYM_NUM
; i
++) {
783 if (p
->sym_val_to_name
[i
])
784 flex_array_free(p
->sym_val_to_name
[i
]);
787 kfree(p
->class_val_to_struct
);
788 kfree(p
->role_val_to_struct
);
789 kfree(p
->user_val_to_struct
);
790 if (p
->type_val_to_struct_array
)
791 flex_array_free(p
->type_val_to_struct_array
);
793 avtab_destroy(&p
->te_avtab
);
795 for (i
= 0; i
< OCON_NUM
; i
++) {
801 ocontext_destroy(ctmp
, i
);
803 p
->ocontexts
[i
] = NULL
;
814 ocontext_destroy(ctmp
, OCON_FSUSE
);
822 cond_policydb_destroy(p
);
824 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
831 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
838 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
839 hashtab_destroy(p
->filename_trans
);
841 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
842 hashtab_destroy(p
->range_tr
);
844 if (p
->type_attr_map_array
) {
845 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
848 e
= flex_array_get(p
->type_attr_map_array
, i
);
853 flex_array_free(p
->type_attr_map_array
);
856 ebitmap_destroy(&p
->filename_trans_ttypes
);
857 ebitmap_destroy(&p
->policycaps
);
858 ebitmap_destroy(&p
->permissive_map
);
864 * Load the initial SIDs specified in a policy database
865 * structure into a SID table.
867 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
869 struct ocontext
*head
, *c
;
874 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
878 head
= p
->ocontexts
[OCON_ISID
];
879 for (c
= head
; c
; c
= c
->next
) {
881 if (!c
->context
[0].user
) {
882 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
887 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
889 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
899 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
901 if (!class || class > p
->p_classes
.nprim
)
906 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
908 if (!role
|| role
> p
->p_roles
.nprim
)
913 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
915 if (!type
|| type
> p
->p_types
.nprim
)
921 * Return 1 if the fields in the security context
922 * structure `c' are valid. Return 0 otherwise.
924 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
926 struct role_datum
*role
;
927 struct user_datum
*usrdatum
;
929 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
932 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
935 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
938 if (c
->role
!= OBJECT_R_VAL
) {
940 * Role must be authorized for the type.
942 role
= p
->role_val_to_struct
[c
->role
- 1];
943 if (!ebitmap_get_bit(&role
->types
, c
->type
- 1))
944 /* role may not be associated with type */
948 * User must be authorized for the role.
950 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
954 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
955 /* user may not be associated with role */
959 if (!mls_context_isvalid(p
, c
))
966 * Read a MLS range structure from a policydb binary
967 * representation file.
969 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
975 rc
= next_entry(buf
, fp
, sizeof(u32
));
980 items
= le32_to_cpu(buf
[0]);
981 if (items
> ARRAY_SIZE(buf
)) {
982 printk(KERN_ERR
"SELinux: mls: range overflow\n");
986 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
988 printk(KERN_ERR
"SELinux: mls: truncated range\n");
992 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
994 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
996 r
->level
[1].sens
= r
->level
[0].sens
;
998 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1000 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1004 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1006 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1010 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1012 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1019 ebitmap_destroy(&r
->level
[0].cat
);
1025 * Read and validate a security context structure
1026 * from a policydb binary representation file.
1028 static int context_read_and_validate(struct context
*c
,
1035 rc
= next_entry(buf
, fp
, sizeof buf
);
1037 printk(KERN_ERR
"SELinux: context truncated\n");
1040 c
->user
= le32_to_cpu(buf
[0]);
1041 c
->role
= le32_to_cpu(buf
[1]);
1042 c
->type
= le32_to_cpu(buf
[2]);
1043 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1044 rc
= mls_read_range_helper(&c
->range
, fp
);
1046 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1052 if (!policydb_context_isvalid(p
, c
)) {
1053 printk(KERN_ERR
"SELinux: invalid security context\n");
1063 * The following *_read functions are used to
1064 * read the symbol data from a policy database
1065 * binary representation file.
1068 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1071 struct perm_datum
*perdatum
;
1077 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1081 rc
= next_entry(buf
, fp
, sizeof buf
);
1085 len
= le32_to_cpu(buf
[0]);
1086 perdatum
->value
= le32_to_cpu(buf
[1]);
1089 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1093 rc
= next_entry(key
, fp
, len
);
1098 rc
= hashtab_insert(h
, key
, perdatum
);
1104 perm_destroy(key
, perdatum
, NULL
);
1108 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1111 struct common_datum
*comdatum
;
1117 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1121 rc
= next_entry(buf
, fp
, sizeof buf
);
1125 len
= le32_to_cpu(buf
[0]);
1126 comdatum
->value
= le32_to_cpu(buf
[1]);
1128 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1131 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1132 nel
= le32_to_cpu(buf
[3]);
1135 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1139 rc
= next_entry(key
, fp
, len
);
1144 for (i
= 0; i
< nel
; i
++) {
1145 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1150 rc
= hashtab_insert(h
, key
, comdatum
);
1155 common_destroy(key
, comdatum
, NULL
);
1159 static int read_cons_helper(struct constraint_node
**nodep
, int ncons
,
1160 int allowxtarget
, void *fp
)
1162 struct constraint_node
*c
, *lc
;
1163 struct constraint_expr
*e
, *le
;
1166 int rc
, i
, j
, depth
;
1169 for (i
= 0; i
< ncons
; i
++) {
1170 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1179 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1182 c
->permissions
= le32_to_cpu(buf
[0]);
1183 nexpr
= le32_to_cpu(buf
[1]);
1186 for (j
= 0; j
< nexpr
; j
++) {
1187 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1196 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1199 e
->expr_type
= le32_to_cpu(buf
[0]);
1200 e
->attr
= le32_to_cpu(buf
[1]);
1201 e
->op
= le32_to_cpu(buf
[2]);
1203 switch (e
->expr_type
) {
1215 if (depth
== (CEXPR_MAXDEPTH
- 1))
1220 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1222 if (depth
== (CEXPR_MAXDEPTH
- 1))
1225 rc
= ebitmap_read(&e
->names
, fp
);
1242 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1245 struct class_datum
*cladatum
;
1247 u32 len
, len2
, ncons
, nel
;
1251 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1255 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1259 len
= le32_to_cpu(buf
[0]);
1260 len2
= le32_to_cpu(buf
[1]);
1261 cladatum
->value
= le32_to_cpu(buf
[2]);
1263 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1266 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1267 nel
= le32_to_cpu(buf
[4]);
1269 ncons
= le32_to_cpu(buf
[5]);
1272 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1276 rc
= next_entry(key
, fp
, len
);
1283 cladatum
->comkey
= kmalloc(len2
+ 1, GFP_KERNEL
);
1284 if (!cladatum
->comkey
)
1286 rc
= next_entry(cladatum
->comkey
, fp
, len2
);
1289 cladatum
->comkey
[len2
] = '\0';
1292 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1293 if (!cladatum
->comdatum
) {
1294 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1298 for (i
= 0; i
< nel
; i
++) {
1299 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1304 rc
= read_cons_helper(&cladatum
->constraints
, ncons
, 0, fp
);
1308 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1309 /* grab the validatetrans rules */
1310 rc
= next_entry(buf
, fp
, sizeof(u32
));
1313 ncons
= le32_to_cpu(buf
[0]);
1314 rc
= read_cons_helper(&cladatum
->validatetrans
, ncons
, 1, fp
);
1319 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1320 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1324 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1325 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1326 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1329 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1330 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1333 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1336 rc
= hashtab_insert(h
, key
, cladatum
);
1342 cls_destroy(key
, cladatum
, NULL
);
1346 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1349 struct role_datum
*role
;
1350 int rc
, to_read
= 2;
1355 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1359 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1362 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1366 len
= le32_to_cpu(buf
[0]);
1367 role
->value
= le32_to_cpu(buf
[1]);
1368 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1369 role
->bounds
= le32_to_cpu(buf
[2]);
1372 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1376 rc
= next_entry(key
, fp
, len
);
1381 rc
= ebitmap_read(&role
->dominates
, fp
);
1385 rc
= ebitmap_read(&role
->types
, fp
);
1389 if (strcmp(key
, OBJECT_R
) == 0) {
1391 if (role
->value
!= OBJECT_R_VAL
) {
1392 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1393 OBJECT_R
, role
->value
);
1400 rc
= hashtab_insert(h
, key
, role
);
1405 role_destroy(key
, role
, NULL
);
1409 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1412 struct type_datum
*typdatum
;
1413 int rc
, to_read
= 3;
1418 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1422 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1425 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1429 len
= le32_to_cpu(buf
[0]);
1430 typdatum
->value
= le32_to_cpu(buf
[1]);
1431 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1432 u32 prop
= le32_to_cpu(buf
[2]);
1434 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1435 typdatum
->primary
= 1;
1436 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1437 typdatum
->attribute
= 1;
1439 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1441 typdatum
->primary
= le32_to_cpu(buf
[2]);
1445 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1448 rc
= next_entry(key
, fp
, len
);
1453 rc
= hashtab_insert(h
, key
, typdatum
);
1458 type_destroy(key
, typdatum
, NULL
);
1464 * Read a MLS level structure from a policydb binary
1465 * representation file.
1467 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1472 memset(lp
, 0, sizeof(*lp
));
1474 rc
= next_entry(buf
, fp
, sizeof buf
);
1476 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1479 lp
->sens
= le32_to_cpu(buf
[0]);
1481 rc
= ebitmap_read(&lp
->cat
, fp
);
1483 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1489 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1492 struct user_datum
*usrdatum
;
1493 int rc
, to_read
= 2;
1498 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1502 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1505 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1509 len
= le32_to_cpu(buf
[0]);
1510 usrdatum
->value
= le32_to_cpu(buf
[1]);
1511 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1512 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1515 key
= kmalloc(len
+ 1, GFP_KERNEL
);
1518 rc
= next_entry(key
, fp
, len
);
1523 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1527 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1528 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1531 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1536 rc
= hashtab_insert(h
, key
, usrdatum
);
1541 user_destroy(key
, usrdatum
, NULL
);
1545 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1548 struct level_datum
*levdatum
;
1554 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1558 rc
= next_entry(buf
, fp
, sizeof buf
);
1562 len
= le32_to_cpu(buf
[0]);
1563 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1566 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1569 rc
= next_entry(key
, fp
, len
);
1575 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1576 if (!levdatum
->level
)
1579 rc
= mls_read_level(levdatum
->level
, fp
);
1583 rc
= hashtab_insert(h
, key
, levdatum
);
1588 sens_destroy(key
, levdatum
, NULL
);
1592 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1595 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]);
1614 key
= kmalloc(len
+ 1, GFP_ATOMIC
);
1617 rc
= next_entry(key
, fp
, len
);
1622 rc
= hashtab_insert(h
, key
, catdatum
);
1627 cat_destroy(key
, catdatum
, NULL
);
1631 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1643 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1645 struct user_datum
*upper
, *user
;
1646 struct policydb
*p
= datap
;
1649 upper
= user
= datum
;
1650 while (upper
->bounds
) {
1651 struct ebitmap_node
*node
;
1654 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1655 printk(KERN_ERR
"SELinux: user %s: "
1656 "too deep or looped boundary",
1661 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1662 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1663 if (ebitmap_get_bit(&upper
->roles
, bit
))
1667 "SELinux: boundary violated policy: "
1668 "user=%s role=%s bounds=%s\n",
1669 sym_name(p
, SYM_USERS
, user
->value
- 1),
1670 sym_name(p
, SYM_ROLES
, bit
),
1671 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1680 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1682 struct role_datum
*upper
, *role
;
1683 struct policydb
*p
= datap
;
1686 upper
= role
= datum
;
1687 while (upper
->bounds
) {
1688 struct ebitmap_node
*node
;
1691 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1692 printk(KERN_ERR
"SELinux: role %s: "
1693 "too deep or looped bounds\n",
1698 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1699 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1700 if (ebitmap_get_bit(&upper
->types
, bit
))
1704 "SELinux: boundary violated policy: "
1705 "role=%s type=%s bounds=%s\n",
1706 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1707 sym_name(p
, SYM_TYPES
, bit
),
1708 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1717 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1719 struct type_datum
*upper
;
1720 struct policydb
*p
= datap
;
1724 while (upper
->bounds
) {
1725 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1726 printk(KERN_ERR
"SELinux: type %s: "
1727 "too deep or looped boundary\n",
1732 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1736 if (upper
->attribute
) {
1737 printk(KERN_ERR
"SELinux: type %s: "
1738 "bounded by attribute %s",
1740 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1748 static int policydb_bounds_sanity_check(struct policydb
*p
)
1752 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1755 rc
= hashtab_map(p
->p_users
.table
,
1756 user_bounds_sanity_check
, p
);
1760 rc
= hashtab_map(p
->p_roles
.table
,
1761 role_bounds_sanity_check
, p
);
1765 rc
= hashtab_map(p
->p_types
.table
,
1766 type_bounds_sanity_check
, p
);
1773 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1775 struct class_datum
*cladatum
;
1777 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1781 return cladatum
->value
;
1784 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1786 struct class_datum
*cladatum
;
1787 struct perm_datum
*perdatum
= NULL
;
1788 struct common_datum
*comdatum
;
1790 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1793 cladatum
= p
->class_val_to_struct
[tclass
-1];
1794 comdatum
= cladatum
->comdatum
;
1796 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1799 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1804 return 1U << (perdatum
->value
-1);
1807 static int range_read(struct policydb
*p
, void *fp
)
1809 struct range_trans
*rt
= NULL
;
1810 struct mls_range
*r
= NULL
;
1815 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1818 rc
= next_entry(buf
, fp
, sizeof(u32
));
1822 nel
= le32_to_cpu(buf
[0]);
1823 for (i
= 0; i
< nel
; i
++) {
1825 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1829 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1833 rt
->source_type
= le32_to_cpu(buf
[0]);
1834 rt
->target_type
= le32_to_cpu(buf
[1]);
1835 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1836 rc
= next_entry(buf
, fp
, sizeof(u32
));
1839 rt
->target_class
= le32_to_cpu(buf
[0]);
1841 rt
->target_class
= p
->process_class
;
1844 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1845 !policydb_type_isvalid(p
, rt
->target_type
) ||
1846 !policydb_class_isvalid(p
, rt
->target_class
))
1850 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1854 rc
= mls_read_range_helper(r
, fp
);
1859 if (!mls_range_isvalid(p
, r
)) {
1860 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1864 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1871 hash_eval(p
->range_tr
, "rangetr");
1879 static int filename_trans_read(struct policydb
*p
, void *fp
)
1881 struct filename_trans
*ft
;
1882 struct filename_trans_datum
*otype
;
1888 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1891 rc
= next_entry(buf
, fp
, sizeof(u32
));
1894 nel
= le32_to_cpu(buf
[0]);
1896 for (i
= 0; i
< nel
; i
++) {
1902 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1907 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1911 /* length of the path component string */
1912 rc
= next_entry(buf
, fp
, sizeof(u32
));
1915 len
= le32_to_cpu(buf
[0]);
1918 name
= kmalloc(len
+ 1, GFP_KERNEL
);
1924 /* path component string */
1925 rc
= next_entry(name
, fp
, len
);
1930 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1934 ft
->stype
= le32_to_cpu(buf
[0]);
1935 ft
->ttype
= le32_to_cpu(buf
[1]);
1936 ft
->tclass
= le32_to_cpu(buf
[2]);
1938 otype
->otype
= le32_to_cpu(buf
[3]);
1940 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1944 hashtab_insert(p
->filename_trans
, ft
, otype
);
1946 hash_eval(p
->filename_trans
, "filenametr");
1956 static int genfs_read(struct policydb
*p
, void *fp
)
1959 u32 nel
, nel2
, len
, len2
;
1961 struct ocontext
*l
, *c
;
1962 struct ocontext
*newc
= NULL
;
1963 struct genfs
*genfs_p
, *genfs
;
1964 struct genfs
*newgenfs
= NULL
;
1966 rc
= next_entry(buf
, fp
, sizeof(u32
));
1969 nel
= le32_to_cpu(buf
[0]);
1971 for (i
= 0; i
< nel
; i
++) {
1972 rc
= next_entry(buf
, fp
, sizeof(u32
));
1975 len
= le32_to_cpu(buf
[0]);
1978 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
1983 newgenfs
->fstype
= kmalloc(len
+ 1, GFP_KERNEL
);
1984 if (!newgenfs
->fstype
)
1987 rc
= next_entry(newgenfs
->fstype
, fp
, len
);
1991 newgenfs
->fstype
[len
] = 0;
1993 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
1994 genfs_p
= genfs
, genfs
= genfs
->next
) {
1996 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
1997 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2001 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2004 newgenfs
->next
= genfs
;
2006 genfs_p
->next
= newgenfs
;
2008 p
->genfs
= newgenfs
;
2012 rc
= next_entry(buf
, fp
, sizeof(u32
));
2016 nel2
= le32_to_cpu(buf
[0]);
2017 for (j
= 0; j
< nel2
; j
++) {
2018 rc
= next_entry(buf
, fp
, sizeof(u32
));
2021 len
= le32_to_cpu(buf
[0]);
2024 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2029 newc
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2033 rc
= next_entry(newc
->u
.name
, fp
, len
);
2036 newc
->u
.name
[len
] = 0;
2038 rc
= next_entry(buf
, fp
, sizeof(u32
));
2042 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2043 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2047 for (l
= NULL
, c
= genfs
->head
; c
;
2048 l
= c
, c
= c
->next
) {
2050 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2051 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2052 newc
->v
.sclass
== c
->v
.sclass
)) {
2053 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2054 genfs
->fstype
, c
->u
.name
);
2057 len
= strlen(newc
->u
.name
);
2058 len2
= strlen(c
->u
.name
);
2074 kfree(newgenfs
->fstype
);
2076 ocontext_destroy(newc
, OCON_FSUSE
);
2081 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2087 struct ocontext
*l
, *c
;
2090 for (i
= 0; i
< info
->ocon_num
; i
++) {
2091 rc
= next_entry(buf
, fp
, sizeof(u32
));
2094 nel
= le32_to_cpu(buf
[0]);
2097 for (j
= 0; j
< nel
; j
++) {
2099 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2105 p
->ocontexts
[i
] = c
;
2110 rc
= next_entry(buf
, fp
, sizeof(u32
));
2114 c
->sid
[0] = le32_to_cpu(buf
[0]);
2115 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2121 rc
= next_entry(buf
, fp
, sizeof(u32
));
2124 len
= le32_to_cpu(buf
[0]);
2127 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2131 rc
= next_entry(c
->u
.name
, fp
, len
);
2136 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2139 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2144 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2147 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2148 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2149 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2150 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2155 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2158 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2159 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2160 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2165 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2170 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2171 if (c
->v
.behavior
> SECURITY_FS_USE_NONE
)
2175 len
= le32_to_cpu(buf
[1]);
2176 c
->u
.name
= kmalloc(len
+ 1, GFP_KERNEL
);
2180 rc
= next_entry(c
->u
.name
, fp
, len
);
2184 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2191 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2194 for (k
= 0; k
< 4; k
++)
2195 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2196 for (k
= 0; k
< 4; k
++)
2197 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2198 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2212 * Read the configuration data from a policy database binary
2213 * representation file into a policy database structure.
2215 int policydb_read(struct policydb
*p
, void *fp
)
2217 struct role_allow
*ra
, *lra
;
2218 struct role_trans
*tr
, *ltr
;
2221 u32 len
, nprim
, nel
;
2224 struct policydb_compat_info
*info
;
2226 rc
= policydb_init(p
);
2230 /* Read the magic number and string length. */
2231 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2236 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2237 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2238 "not match expected magic number 0x%x\n",
2239 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2244 len
= le32_to_cpu(buf
[1]);
2245 if (len
!= strlen(POLICYDB_STRING
)) {
2246 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2247 "match expected length %Zu\n",
2248 len
, strlen(POLICYDB_STRING
));
2253 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2254 if (!policydb_str
) {
2255 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2256 "string of length %d\n", len
);
2260 rc
= next_entry(policydb_str
, fp
, len
);
2262 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2263 kfree(policydb_str
);
2268 policydb_str
[len
] = '\0';
2269 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2270 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2271 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2272 kfree(policydb_str
);
2275 /* Done with policydb_str. */
2276 kfree(policydb_str
);
2277 policydb_str
= NULL
;
2279 /* Read the version and table sizes. */
2280 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2285 p
->policyvers
= le32_to_cpu(buf
[0]);
2286 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2287 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2288 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2289 "my version range %d-%d\n",
2290 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2294 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2298 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2299 printk(KERN_ERR
"SELinux: security policydb version %d "
2300 "(MLS) not backwards compatible\n",
2305 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2306 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2308 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2309 rc
= ebitmap_read(&p
->policycaps
, fp
);
2314 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2315 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2321 info
= policydb_lookup_compat(p
->policyvers
);
2323 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2324 "for version %d\n", p
->policyvers
);
2329 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2330 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2331 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2332 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2333 le32_to_cpu(buf
[3]),
2334 info
->sym_num
, info
->ocon_num
);
2338 for (i
= 0; i
< info
->sym_num
; i
++) {
2339 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2342 nprim
= le32_to_cpu(buf
[0]);
2343 nel
= le32_to_cpu(buf
[1]);
2344 for (j
= 0; j
< nel
; j
++) {
2345 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2350 p
->symtab
[i
].nprim
= nprim
;
2354 p
->process_class
= string_to_security_class(p
, "process");
2355 if (!p
->process_class
)
2358 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2362 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2363 rc
= cond_read_list(p
, fp
);
2368 rc
= next_entry(buf
, fp
, sizeof(u32
));
2371 nel
= le32_to_cpu(buf
[0]);
2373 for (i
= 0; i
< nel
; i
++) {
2375 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2382 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2387 tr
->role
= le32_to_cpu(buf
[0]);
2388 tr
->type
= le32_to_cpu(buf
[1]);
2389 tr
->new_role
= le32_to_cpu(buf
[2]);
2390 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2391 rc
= next_entry(buf
, fp
, sizeof(u32
));
2394 tr
->tclass
= le32_to_cpu(buf
[0]);
2396 tr
->tclass
= p
->process_class
;
2398 if (!policydb_role_isvalid(p
, tr
->role
) ||
2399 !policydb_type_isvalid(p
, tr
->type
) ||
2400 !policydb_class_isvalid(p
, tr
->tclass
) ||
2401 !policydb_role_isvalid(p
, tr
->new_role
))
2406 rc
= next_entry(buf
, fp
, sizeof(u32
));
2409 nel
= le32_to_cpu(buf
[0]);
2411 for (i
= 0; i
< nel
; i
++) {
2413 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2420 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2425 ra
->role
= le32_to_cpu(buf
[0]);
2426 ra
->new_role
= le32_to_cpu(buf
[1]);
2427 if (!policydb_role_isvalid(p
, ra
->role
) ||
2428 !policydb_role_isvalid(p
, ra
->new_role
))
2433 rc
= filename_trans_read(p
, fp
);
2437 rc
= policydb_index(p
);
2442 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2443 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2444 if (!p
->process_trans_perms
)
2447 rc
= ocontext_read(p
, info
, fp
);
2451 rc
= genfs_read(p
, fp
);
2455 rc
= range_read(p
, fp
);
2460 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2462 GFP_KERNEL
| __GFP_ZERO
);
2463 if (!p
->type_attr_map_array
)
2466 /* preallocate so we don't have to worry about the put ever failing */
2467 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2468 GFP_KERNEL
| __GFP_ZERO
);
2472 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2473 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2477 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2478 rc
= ebitmap_read(e
, fp
);
2482 /* add the type itself as the degenerate case */
2483 rc
= ebitmap_set_bit(e
, i
, 1);
2488 rc
= policydb_bounds_sanity_check(p
);
2496 policydb_destroy(p
);
2501 * Write a MLS level structure to a policydb binary
2502 * representation file.
2504 static int mls_write_level(struct mls_level
*l
, void *fp
)
2509 buf
[0] = cpu_to_le32(l
->sens
);
2510 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2514 rc
= ebitmap_write(&l
->cat
, fp
);
2522 * Write a MLS range structure to a policydb binary
2523 * representation file.
2525 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2531 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2537 buf
[0] = cpu_to_le32(items
-1);
2538 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2540 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2542 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
2544 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2548 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2552 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2560 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2563 struct level_datum
*levdatum
= datum
;
2564 struct policy_data
*pd
= ptr
;
2571 buf
[0] = cpu_to_le32(len
);
2572 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2573 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2577 rc
= put_entry(key
, 1, len
, fp
);
2581 rc
= mls_write_level(levdatum
->level
, fp
);
2588 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2591 struct cat_datum
*catdatum
= datum
;
2592 struct policy_data
*pd
= ptr
;
2599 buf
[0] = cpu_to_le32(len
);
2600 buf
[1] = cpu_to_le32(catdatum
->value
);
2601 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2602 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2606 rc
= put_entry(key
, 1, len
, fp
);
2613 static int role_trans_write(struct policydb
*p
, void *fp
)
2615 struct role_trans
*r
= p
->role_tr
;
2616 struct role_trans
*tr
;
2622 for (tr
= r
; tr
; tr
= tr
->next
)
2624 buf
[0] = cpu_to_le32(nel
);
2625 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2628 for (tr
= r
; tr
; tr
= tr
->next
) {
2629 buf
[0] = cpu_to_le32(tr
->role
);
2630 buf
[1] = cpu_to_le32(tr
->type
);
2631 buf
[2] = cpu_to_le32(tr
->new_role
);
2632 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2635 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2636 buf
[0] = cpu_to_le32(tr
->tclass
);
2637 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2646 static int role_allow_write(struct role_allow
*r
, void *fp
)
2648 struct role_allow
*ra
;
2654 for (ra
= r
; ra
; ra
= ra
->next
)
2656 buf
[0] = cpu_to_le32(nel
);
2657 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2660 for (ra
= r
; ra
; ra
= ra
->next
) {
2661 buf
[0] = cpu_to_le32(ra
->role
);
2662 buf
[1] = cpu_to_le32(ra
->new_role
);
2663 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2671 * Write a security context structure
2672 * to a policydb binary representation file.
2674 static int context_write(struct policydb
*p
, struct context
*c
,
2680 buf
[0] = cpu_to_le32(c
->user
);
2681 buf
[1] = cpu_to_le32(c
->role
);
2682 buf
[2] = cpu_to_le32(c
->type
);
2684 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2688 rc
= mls_write_range_helper(&c
->range
, fp
);
2696 * The following *_write functions are used to
2697 * write the symbol data to a policy database
2698 * binary representation file.
2701 static int perm_write(void *vkey
, void *datum
, void *fp
)
2704 struct perm_datum
*perdatum
= datum
;
2710 buf
[0] = cpu_to_le32(len
);
2711 buf
[1] = cpu_to_le32(perdatum
->value
);
2712 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2716 rc
= put_entry(key
, 1, len
, fp
);
2723 static int common_write(void *vkey
, void *datum
, void *ptr
)
2726 struct common_datum
*comdatum
= datum
;
2727 struct policy_data
*pd
= ptr
;
2734 buf
[0] = cpu_to_le32(len
);
2735 buf
[1] = cpu_to_le32(comdatum
->value
);
2736 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2737 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2738 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2742 rc
= put_entry(key
, 1, len
, fp
);
2746 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2753 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2756 struct constraint_node
*c
;
2757 struct constraint_expr
*e
;
2762 for (c
= node
; c
; c
= c
->next
) {
2764 for (e
= c
->expr
; e
; e
= e
->next
)
2766 buf
[0] = cpu_to_le32(c
->permissions
);
2767 buf
[1] = cpu_to_le32(nel
);
2768 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2771 for (e
= c
->expr
; e
; e
= e
->next
) {
2772 buf
[0] = cpu_to_le32(e
->expr_type
);
2773 buf
[1] = cpu_to_le32(e
->attr
);
2774 buf
[2] = cpu_to_le32(e
->op
);
2775 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2779 switch (e
->expr_type
) {
2781 rc
= ebitmap_write(&e
->names
, fp
);
2794 static int class_write(void *vkey
, void *datum
, void *ptr
)
2797 struct class_datum
*cladatum
= datum
;
2798 struct policy_data
*pd
= ptr
;
2800 struct policydb
*p
= pd
->p
;
2801 struct constraint_node
*c
;
2808 if (cladatum
->comkey
)
2809 len2
= strlen(cladatum
->comkey
);
2814 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2817 buf
[0] = cpu_to_le32(len
);
2818 buf
[1] = cpu_to_le32(len2
);
2819 buf
[2] = cpu_to_le32(cladatum
->value
);
2820 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2821 if (cladatum
->permissions
.table
)
2822 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2825 buf
[5] = cpu_to_le32(ncons
);
2826 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2830 rc
= put_entry(key
, 1, len
, fp
);
2834 if (cladatum
->comkey
) {
2835 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2840 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2844 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2848 /* write out the validatetrans rule */
2850 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2853 buf
[0] = cpu_to_le32(ncons
);
2854 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2858 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2862 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2863 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2864 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2865 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2867 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2872 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2873 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2874 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2882 static int role_write(void *vkey
, void *datum
, void *ptr
)
2885 struct role_datum
*role
= datum
;
2886 struct policy_data
*pd
= ptr
;
2888 struct policydb
*p
= pd
->p
;
2895 buf
[items
++] = cpu_to_le32(len
);
2896 buf
[items
++] = cpu_to_le32(role
->value
);
2897 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2898 buf
[items
++] = cpu_to_le32(role
->bounds
);
2900 BUG_ON(items
> (sizeof(buf
)/sizeof(buf
[0])));
2902 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2906 rc
= put_entry(key
, 1, len
, fp
);
2910 rc
= ebitmap_write(&role
->dominates
, fp
);
2914 rc
= ebitmap_write(&role
->types
, fp
);
2921 static int type_write(void *vkey
, void *datum
, void *ptr
)
2924 struct type_datum
*typdatum
= datum
;
2925 struct policy_data
*pd
= ptr
;
2926 struct policydb
*p
= pd
->p
;
2934 buf
[items
++] = cpu_to_le32(len
);
2935 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2936 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2939 if (typdatum
->primary
)
2940 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2942 if (typdatum
->attribute
)
2943 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2945 buf
[items
++] = cpu_to_le32(properties
);
2946 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2948 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2950 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
2951 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2955 rc
= put_entry(key
, 1, len
, fp
);
2962 static int user_write(void *vkey
, void *datum
, void *ptr
)
2965 struct user_datum
*usrdatum
= datum
;
2966 struct policy_data
*pd
= ptr
;
2967 struct policydb
*p
= pd
->p
;
2975 buf
[items
++] = cpu_to_le32(len
);
2976 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
2977 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2978 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
2979 BUG_ON(items
> (sizeof(buf
) / sizeof(buf
[0])));
2980 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2984 rc
= put_entry(key
, 1, len
, fp
);
2988 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
2992 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
2996 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3003 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3016 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3019 unsigned int i
, j
, rc
;
3024 for (i
= 0; i
< info
->ocon_num
; i
++) {
3026 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3028 buf
[0] = cpu_to_le32(nel
);
3029 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3032 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3035 buf
[0] = cpu_to_le32(c
->sid
[0]);
3036 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3039 rc
= context_write(p
, &c
->context
[0], fp
);
3045 len
= strlen(c
->u
.name
);
3046 buf
[0] = cpu_to_le32(len
);
3047 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3050 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3053 rc
= context_write(p
, &c
->context
[0], fp
);
3056 rc
= context_write(p
, &c
->context
[1], fp
);
3061 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3062 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3063 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3064 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3067 rc
= context_write(p
, &c
->context
[0], fp
);
3072 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3073 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3074 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3077 rc
= context_write(p
, &c
->context
[0], fp
);
3082 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3083 len
= strlen(c
->u
.name
);
3084 buf
[1] = cpu_to_le32(len
);
3085 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3088 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3091 rc
= context_write(p
, &c
->context
[0], fp
);
3096 for (j
= 0; j
< 4; j
++)
3097 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3098 for (j
= 0; j
< 4; j
++)
3099 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3100 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3103 rc
= context_write(p
, &c
->context
[0], fp
);
3113 static int genfs_write(struct policydb
*p
, void *fp
)
3115 struct genfs
*genfs
;
3122 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3124 buf
[0] = cpu_to_le32(len
);
3125 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3128 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3129 len
= strlen(genfs
->fstype
);
3130 buf
[0] = cpu_to_le32(len
);
3131 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3134 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3138 for (c
= genfs
->head
; c
; c
= c
->next
)
3140 buf
[0] = cpu_to_le32(len
);
3141 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3144 for (c
= genfs
->head
; c
; c
= c
->next
) {
3145 len
= strlen(c
->u
.name
);
3146 buf
[0] = cpu_to_le32(len
);
3147 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3150 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3153 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3154 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3157 rc
= context_write(p
, &c
->context
[0], fp
);
3165 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3173 static int range_write_helper(void *key
, void *data
, void *ptr
)
3176 struct range_trans
*rt
= key
;
3177 struct mls_range
*r
= data
;
3178 struct policy_data
*pd
= ptr
;
3180 struct policydb
*p
= pd
->p
;
3183 buf
[0] = cpu_to_le32(rt
->source_type
);
3184 buf
[1] = cpu_to_le32(rt
->target_type
);
3185 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3188 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3189 buf
[0] = cpu_to_le32(rt
->target_class
);
3190 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3194 rc
= mls_write_range_helper(r
, fp
);
3201 static int range_write(struct policydb
*p
, void *fp
)
3206 struct policy_data pd
;
3211 /* count the number of entries in the hashtab */
3213 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3217 buf
[0] = cpu_to_le32(nel
);
3218 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3222 /* actually write all of the entries */
3223 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3230 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3233 struct filename_trans
*ft
= key
;
3234 struct filename_trans_datum
*otype
= data
;
3239 len
= strlen(ft
->name
);
3240 buf
[0] = cpu_to_le32(len
);
3241 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3245 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3251 buf
[2] = ft
->tclass
;
3252 buf
[3] = otype
->otype
;
3254 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3261 static int filename_trans_write(struct policydb
*p
, void *fp
)
3267 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3271 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3275 buf
[0] = cpu_to_le32(nel
);
3276 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3280 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3288 * Write the configuration data in a policy database
3289 * structure to a policy database binary representation
3292 int policydb_write(struct policydb
*p
, void *fp
)
3294 unsigned int i
, num_syms
;
3299 struct policydb_compat_info
*info
;
3302 * refuse to write policy older than compressed avtab
3303 * to simplify the writer. There are other tests dropped
3304 * since we assume this throughout the writer code. Be
3305 * careful if you ever try to remove this restriction
3307 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3308 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3309 " Because it is less than version %d\n", p
->policyvers
,
3310 POLICYDB_VERSION_AVTAB
);
3316 config
|= POLICYDB_CONFIG_MLS
;
3318 if (p
->reject_unknown
)
3319 config
|= REJECT_UNKNOWN
;
3320 if (p
->allow_unknown
)
3321 config
|= ALLOW_UNKNOWN
;
3323 /* Write the magic number and string identifiers. */
3324 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3325 len
= strlen(POLICYDB_STRING
);
3326 buf
[1] = cpu_to_le32(len
);
3327 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3330 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3334 /* Write the version, config, and table sizes. */
3335 info
= policydb_lookup_compat(p
->policyvers
);
3337 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3338 "version %d", p
->policyvers
);
3342 buf
[0] = cpu_to_le32(p
->policyvers
);
3343 buf
[1] = cpu_to_le32(config
);
3344 buf
[2] = cpu_to_le32(info
->sym_num
);
3345 buf
[3] = cpu_to_le32(info
->ocon_num
);
3347 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3351 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3352 rc
= ebitmap_write(&p
->policycaps
, fp
);
3357 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3358 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3363 num_syms
= info
->sym_num
;
3364 for (i
= 0; i
< num_syms
; i
++) {
3365 struct policy_data pd
;
3370 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3371 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3373 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3376 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3381 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3385 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3389 rc
= role_trans_write(p
, fp
);
3393 rc
= role_allow_write(p
->role_allow
, fp
);
3397 rc
= filename_trans_write(p
, fp
);
3401 rc
= ocontext_write(p
, info
, fp
);
3405 rc
= genfs_write(p
, fp
);
3409 rc
= range_write(p
, fp
);
3413 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3414 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3417 rc
= ebitmap_write(e
, fp
);