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
,
147 .version
= POLICYDB_VERSION_CONSTRAINT_NAMES
,
149 .ocon_num
= OCON_NUM
,
153 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
156 struct policydb_compat_info
*info
= NULL
;
158 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
159 if (policydb_compat
[i
].version
== version
) {
160 info
= &policydb_compat
[i
];
168 * Initialize the role table.
170 static int roles_init(struct policydb
*p
)
174 struct role_datum
*role
;
177 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
182 role
->value
= ++p
->p_roles
.nprim
;
183 if (role
->value
!= OBJECT_R_VAL
)
187 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
191 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
202 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
204 const struct filename_trans
*ft
= k
;
206 unsigned int byte_num
;
209 hash
= ft
->stype
^ ft
->ttype
^ ft
->tclass
;
212 while ((focus
= ft
->name
[byte_num
++]))
213 hash
= partial_name_hash(focus
, hash
);
214 return hash
& (h
->size
- 1);
217 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
219 const struct filename_trans
*ft1
= k1
;
220 const struct filename_trans
*ft2
= k2
;
223 v
= ft1
->stype
- ft2
->stype
;
227 v
= ft1
->ttype
- ft2
->ttype
;
231 v
= ft1
->tclass
- ft2
->tclass
;
235 return strcmp(ft1
->name
, ft2
->name
);
239 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
241 const struct range_trans
*key
= k
;
242 return (key
->source_type
+ (key
->target_type
<< 3) +
243 (key
->target_class
<< 5)) & (h
->size
- 1);
246 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
248 const struct range_trans
*key1
= k1
, *key2
= k2
;
251 v
= key1
->source_type
- key2
->source_type
;
255 v
= key1
->target_type
- key2
->target_type
;
259 v
= key1
->target_class
- key2
->target_class
;
265 * Initialize a policy database structure.
267 static int policydb_init(struct policydb
*p
)
271 memset(p
, 0, sizeof(*p
));
273 for (i
= 0; i
< SYM_NUM
; i
++) {
274 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
279 rc
= avtab_init(&p
->te_avtab
);
287 rc
= cond_policydb_init(p
);
291 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
292 if (!p
->filename_trans
) {
297 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
303 ebitmap_init(&p
->filename_trans_ttypes
);
304 ebitmap_init(&p
->policycaps
);
305 ebitmap_init(&p
->permissive_map
);
309 hashtab_destroy(p
->filename_trans
);
310 hashtab_destroy(p
->range_tr
);
311 for (i
= 0; i
< SYM_NUM
; i
++)
312 hashtab_destroy(p
->symtab
[i
].table
);
317 * The following *_index functions are used to
318 * define the val_to_name and val_to_struct arrays
319 * in a policy database structure. The val_to_name
320 * arrays are used when converting security context
321 * structures into string representations. The
322 * val_to_struct arrays are used when the attributes
323 * of a class, role, or user are needed.
326 static int common_index(void *key
, void *datum
, void *datap
)
329 struct common_datum
*comdatum
;
330 struct flex_array
*fa
;
334 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
337 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
338 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
339 GFP_KERNEL
| __GFP_ZERO
))
344 static int class_index(void *key
, void *datum
, void *datap
)
347 struct class_datum
*cladatum
;
348 struct flex_array
*fa
;
352 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
354 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
355 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
356 GFP_KERNEL
| __GFP_ZERO
))
358 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
362 static int role_index(void *key
, void *datum
, void *datap
)
365 struct role_datum
*role
;
366 struct flex_array
*fa
;
371 || role
->value
> p
->p_roles
.nprim
372 || role
->bounds
> p
->p_roles
.nprim
)
375 fa
= p
->sym_val_to_name
[SYM_ROLES
];
376 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
377 GFP_KERNEL
| __GFP_ZERO
))
379 p
->role_val_to_struct
[role
->value
- 1] = role
;
383 static int type_index(void *key
, void *datum
, void *datap
)
386 struct type_datum
*typdatum
;
387 struct flex_array
*fa
;
392 if (typdatum
->primary
) {
394 || typdatum
->value
> p
->p_types
.nprim
395 || typdatum
->bounds
> p
->p_types
.nprim
)
397 fa
= p
->sym_val_to_name
[SYM_TYPES
];
398 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
399 GFP_KERNEL
| __GFP_ZERO
))
402 fa
= p
->type_val_to_struct_array
;
403 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
404 GFP_KERNEL
| __GFP_ZERO
))
411 static int user_index(void *key
, void *datum
, void *datap
)
414 struct user_datum
*usrdatum
;
415 struct flex_array
*fa
;
420 || usrdatum
->value
> p
->p_users
.nprim
421 || usrdatum
->bounds
> p
->p_users
.nprim
)
424 fa
= p
->sym_val_to_name
[SYM_USERS
];
425 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
426 GFP_KERNEL
| __GFP_ZERO
))
428 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
432 static int sens_index(void *key
, void *datum
, void *datap
)
435 struct level_datum
*levdatum
;
436 struct flex_array
*fa
;
441 if (!levdatum
->isalias
) {
442 if (!levdatum
->level
->sens
||
443 levdatum
->level
->sens
> p
->p_levels
.nprim
)
445 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
446 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
447 GFP_KERNEL
| __GFP_ZERO
))
454 static int cat_index(void *key
, void *datum
, void *datap
)
457 struct cat_datum
*catdatum
;
458 struct flex_array
*fa
;
463 if (!catdatum
->isalias
) {
464 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
466 fa
= p
->sym_val_to_name
[SYM_CATS
];
467 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
468 GFP_KERNEL
| __GFP_ZERO
))
475 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
488 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
490 struct hashtab_info info
;
492 hashtab_stat(h
, &info
);
493 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
494 "longest chain length %d\n", hash_name
, h
->nel
,
495 info
.slots_used
, h
->size
, info
.max_chain_len
);
498 static void symtab_hash_eval(struct symtab
*s
)
502 for (i
= 0; i
< SYM_NUM
; i
++)
503 hash_eval(s
[i
].table
, symtab_name
[i
]);
507 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
513 * Define the other val_to_name and val_to_struct arrays
514 * in a policy database structure.
516 * Caller must clean up on failure.
518 static int policydb_index(struct policydb
*p
)
522 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
523 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
525 printk(", %d sens, %d cats", p
->p_levels
.nprim
,
529 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
530 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
533 avtab_hash_eval(&p
->te_avtab
, "rules");
534 symtab_hash_eval(p
->symtab
);
538 p
->class_val_to_struct
=
539 kmalloc(p
->p_classes
.nprim
* sizeof(*(p
->class_val_to_struct
)),
541 if (!p
->class_val_to_struct
)
545 p
->role_val_to_struct
=
546 kmalloc(p
->p_roles
.nprim
* sizeof(*(p
->role_val_to_struct
)),
548 if (!p
->role_val_to_struct
)
552 p
->user_val_to_struct
=
553 kmalloc(p
->p_users
.nprim
* sizeof(*(p
->user_val_to_struct
)),
555 if (!p
->user_val_to_struct
)
558 /* Yes, I want the sizeof the pointer, not the structure */
560 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
562 GFP_KERNEL
| __GFP_ZERO
);
563 if (!p
->type_val_to_struct_array
)
566 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
567 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
571 rc
= cond_init_bool_indexes(p
);
575 for (i
= 0; i
< SYM_NUM
; i
++) {
577 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
579 GFP_KERNEL
| __GFP_ZERO
);
580 if (!p
->sym_val_to_name
[i
])
583 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
584 0, p
->symtab
[i
].nprim
,
585 GFP_KERNEL
| __GFP_ZERO
);
589 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
599 * The following *_destroy functions are used to
600 * free any memory allocated for each kind of
601 * symbol data in the policy database.
604 static int perm_destroy(void *key
, void *datum
, void *p
)
611 static int common_destroy(void *key
, void *datum
, void *p
)
613 struct common_datum
*comdatum
;
618 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
619 hashtab_destroy(comdatum
->permissions
.table
);
625 static void constraint_expr_destroy(struct constraint_expr
*expr
)
628 ebitmap_destroy(&expr
->names
);
629 if (expr
->type_names
) {
630 ebitmap_destroy(&expr
->type_names
->types
);
631 ebitmap_destroy(&expr
->type_names
->negset
);
632 kfree(expr
->type_names
);
638 static int cls_destroy(void *key
, void *datum
, void *p
)
640 struct class_datum
*cladatum
;
641 struct constraint_node
*constraint
, *ctemp
;
642 struct constraint_expr
*e
, *etmp
;
647 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
648 hashtab_destroy(cladatum
->permissions
.table
);
649 constraint
= cladatum
->constraints
;
651 e
= constraint
->expr
;
655 constraint_expr_destroy(etmp
);
658 constraint
= constraint
->next
;
662 constraint
= cladatum
->validatetrans
;
664 e
= constraint
->expr
;
668 constraint_expr_destroy(etmp
);
671 constraint
= constraint
->next
;
674 kfree(cladatum
->comkey
);
680 static int role_destroy(void *key
, void *datum
, void *p
)
682 struct role_datum
*role
;
687 ebitmap_destroy(&role
->dominates
);
688 ebitmap_destroy(&role
->types
);
694 static int type_destroy(void *key
, void *datum
, void *p
)
701 static int user_destroy(void *key
, void *datum
, void *p
)
703 struct user_datum
*usrdatum
;
708 ebitmap_destroy(&usrdatum
->roles
);
709 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
710 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
711 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
717 static int sens_destroy(void *key
, void *datum
, void *p
)
719 struct level_datum
*levdatum
;
724 ebitmap_destroy(&levdatum
->level
->cat
);
725 kfree(levdatum
->level
);
731 static int cat_destroy(void *key
, void *datum
, void *p
)
738 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
750 static int filenametr_destroy(void *key
, void *datum
, void *p
)
752 struct filename_trans
*ft
= key
;
760 static int range_tr_destroy(void *key
, void *datum
, void *p
)
762 struct mls_range
*rt
= datum
;
764 ebitmap_destroy(&rt
->level
[0].cat
);
765 ebitmap_destroy(&rt
->level
[1].cat
);
771 static void ocontext_destroy(struct ocontext
*c
, int i
)
776 context_destroy(&c
->context
[0]);
777 context_destroy(&c
->context
[1]);
778 if (i
== OCON_ISID
|| i
== OCON_FS
||
779 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
785 * Free any memory allocated by a policy database structure.
787 void policydb_destroy(struct policydb
*p
)
789 struct ocontext
*c
, *ctmp
;
790 struct genfs
*g
, *gtmp
;
792 struct role_allow
*ra
, *lra
= NULL
;
793 struct role_trans
*tr
, *ltr
= NULL
;
795 for (i
= 0; i
< SYM_NUM
; i
++) {
797 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
798 hashtab_destroy(p
->symtab
[i
].table
);
801 for (i
= 0; i
< SYM_NUM
; i
++) {
802 if (p
->sym_val_to_name
[i
])
803 flex_array_free(p
->sym_val_to_name
[i
]);
806 kfree(p
->class_val_to_struct
);
807 kfree(p
->role_val_to_struct
);
808 kfree(p
->user_val_to_struct
);
809 if (p
->type_val_to_struct_array
)
810 flex_array_free(p
->type_val_to_struct_array
);
812 avtab_destroy(&p
->te_avtab
);
814 for (i
= 0; i
< OCON_NUM
; i
++) {
820 ocontext_destroy(ctmp
, i
);
822 p
->ocontexts
[i
] = NULL
;
833 ocontext_destroy(ctmp
, OCON_FSUSE
);
841 cond_policydb_destroy(p
);
843 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
850 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
857 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
858 hashtab_destroy(p
->filename_trans
);
860 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
861 hashtab_destroy(p
->range_tr
);
863 if (p
->type_attr_map_array
) {
864 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
867 e
= flex_array_get(p
->type_attr_map_array
, i
);
872 flex_array_free(p
->type_attr_map_array
);
875 ebitmap_destroy(&p
->filename_trans_ttypes
);
876 ebitmap_destroy(&p
->policycaps
);
877 ebitmap_destroy(&p
->permissive_map
);
883 * Load the initial SIDs specified in a policy database
884 * structure into a SID table.
886 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
888 struct ocontext
*head
, *c
;
893 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
897 head
= p
->ocontexts
[OCON_ISID
];
898 for (c
= head
; c
; c
= c
->next
) {
900 if (!c
->context
[0].user
) {
901 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
906 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
908 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
918 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
920 if (!class || class > p
->p_classes
.nprim
)
925 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
927 if (!role
|| role
> p
->p_roles
.nprim
)
932 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
934 if (!type
|| type
> p
->p_types
.nprim
)
940 * Return 1 if the fields in the security context
941 * structure `c' are valid. Return 0 otherwise.
943 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
945 struct role_datum
*role
;
946 struct user_datum
*usrdatum
;
948 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
951 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
954 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
957 if (c
->role
!= OBJECT_R_VAL
) {
959 * Role must be authorized for the type.
961 role
= p
->role_val_to_struct
[c
->role
- 1];
962 if (!ebitmap_get_bit(&role
->types
, c
->type
- 1))
963 /* role may not be associated with type */
967 * User must be authorized for the role.
969 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
973 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
974 /* user may not be associated with role */
978 if (!mls_context_isvalid(p
, c
))
985 * Read a MLS range structure from a policydb binary
986 * representation file.
988 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
994 rc
= next_entry(buf
, fp
, sizeof(u32
));
999 items
= le32_to_cpu(buf
[0]);
1000 if (items
> ARRAY_SIZE(buf
)) {
1001 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1005 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1007 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1011 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1013 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1015 r
->level
[1].sens
= r
->level
[0].sens
;
1017 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1019 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1023 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1025 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1029 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1031 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1038 ebitmap_destroy(&r
->level
[0].cat
);
1044 * Read and validate a security context structure
1045 * from a policydb binary representation file.
1047 static int context_read_and_validate(struct context
*c
,
1054 rc
= next_entry(buf
, fp
, sizeof buf
);
1056 printk(KERN_ERR
"SELinux: context truncated\n");
1059 c
->user
= le32_to_cpu(buf
[0]);
1060 c
->role
= le32_to_cpu(buf
[1]);
1061 c
->type
= le32_to_cpu(buf
[2]);
1062 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1063 rc
= mls_read_range_helper(&c
->range
, fp
);
1065 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1071 if (!policydb_context_isvalid(p
, c
)) {
1072 printk(KERN_ERR
"SELinux: invalid security context\n");
1082 * The following *_read functions are used to
1083 * read the symbol data from a policy database
1084 * binary representation file.
1087 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1092 str
= kmalloc(len
+ 1, flags
);
1096 /* it's expected the caller should free the str */
1099 rc
= next_entry(str
, fp
, len
);
1107 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1110 struct perm_datum
*perdatum
;
1116 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1120 rc
= next_entry(buf
, fp
, sizeof buf
);
1124 len
= le32_to_cpu(buf
[0]);
1125 perdatum
->value
= le32_to_cpu(buf
[1]);
1127 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1131 rc
= hashtab_insert(h
, key
, perdatum
);
1137 perm_destroy(key
, perdatum
, NULL
);
1141 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1144 struct common_datum
*comdatum
;
1150 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1154 rc
= next_entry(buf
, fp
, sizeof buf
);
1158 len
= le32_to_cpu(buf
[0]);
1159 comdatum
->value
= le32_to_cpu(buf
[1]);
1161 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1164 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1165 nel
= le32_to_cpu(buf
[3]);
1167 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1171 for (i
= 0; i
< nel
; i
++) {
1172 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1177 rc
= hashtab_insert(h
, key
, comdatum
);
1182 common_destroy(key
, comdatum
, NULL
);
1186 static void type_set_init(struct type_set
*t
)
1188 ebitmap_init(&t
->types
);
1189 ebitmap_init(&t
->negset
);
1192 static int type_set_read(struct type_set
*t
, void *fp
)
1197 if (ebitmap_read(&t
->types
, fp
))
1199 if (ebitmap_read(&t
->negset
, fp
))
1202 rc
= next_entry(buf
, fp
, sizeof(u32
));
1205 t
->flags
= le32_to_cpu(buf
[0]);
1211 static int read_cons_helper(struct policydb
*p
,
1212 struct constraint_node
**nodep
,
1213 int ncons
, int allowxtarget
, void *fp
)
1215 struct constraint_node
*c
, *lc
;
1216 struct constraint_expr
*e
, *le
;
1219 int rc
, i
, j
, depth
;
1222 for (i
= 0; i
< ncons
; i
++) {
1223 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1232 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1235 c
->permissions
= le32_to_cpu(buf
[0]);
1236 nexpr
= le32_to_cpu(buf
[1]);
1239 for (j
= 0; j
< nexpr
; j
++) {
1240 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1249 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1252 e
->expr_type
= le32_to_cpu(buf
[0]);
1253 e
->attr
= le32_to_cpu(buf
[1]);
1254 e
->op
= le32_to_cpu(buf
[2]);
1256 switch (e
->expr_type
) {
1268 if (depth
== (CEXPR_MAXDEPTH
- 1))
1273 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1275 if (depth
== (CEXPR_MAXDEPTH
- 1))
1278 rc
= ebitmap_read(&e
->names
, fp
);
1281 if (p
->policyvers
>=
1282 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1283 e
->type_names
= kzalloc(sizeof
1288 type_set_init(e
->type_names
);
1289 rc
= type_set_read(e
->type_names
, fp
);
1307 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1310 struct class_datum
*cladatum
;
1312 u32 len
, len2
, ncons
, nel
;
1316 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1320 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1324 len
= le32_to_cpu(buf
[0]);
1325 len2
= le32_to_cpu(buf
[1]);
1326 cladatum
->value
= le32_to_cpu(buf
[2]);
1328 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1331 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1332 nel
= le32_to_cpu(buf
[4]);
1334 ncons
= le32_to_cpu(buf
[5]);
1336 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1341 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1346 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1347 if (!cladatum
->comdatum
) {
1348 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1352 for (i
= 0; i
< nel
; i
++) {
1353 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1358 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1362 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1363 /* grab the validatetrans rules */
1364 rc
= next_entry(buf
, fp
, sizeof(u32
));
1367 ncons
= le32_to_cpu(buf
[0]);
1368 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1374 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1375 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1379 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1380 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1381 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1384 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1385 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1388 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1391 rc
= hashtab_insert(h
, key
, cladatum
);
1397 cls_destroy(key
, cladatum
, NULL
);
1401 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1404 struct role_datum
*role
;
1405 int rc
, to_read
= 2;
1410 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1414 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1417 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1421 len
= le32_to_cpu(buf
[0]);
1422 role
->value
= le32_to_cpu(buf
[1]);
1423 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1424 role
->bounds
= le32_to_cpu(buf
[2]);
1426 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1430 rc
= ebitmap_read(&role
->dominates
, fp
);
1434 rc
= ebitmap_read(&role
->types
, fp
);
1438 if (strcmp(key
, OBJECT_R
) == 0) {
1440 if (role
->value
!= OBJECT_R_VAL
) {
1441 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1442 OBJECT_R
, role
->value
);
1449 rc
= hashtab_insert(h
, key
, role
);
1454 role_destroy(key
, role
, NULL
);
1458 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1461 struct type_datum
*typdatum
;
1462 int rc
, to_read
= 3;
1467 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1471 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1474 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1478 len
= le32_to_cpu(buf
[0]);
1479 typdatum
->value
= le32_to_cpu(buf
[1]);
1480 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1481 u32 prop
= le32_to_cpu(buf
[2]);
1483 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1484 typdatum
->primary
= 1;
1485 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1486 typdatum
->attribute
= 1;
1488 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1490 typdatum
->primary
= le32_to_cpu(buf
[2]);
1493 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1497 rc
= hashtab_insert(h
, key
, typdatum
);
1502 type_destroy(key
, typdatum
, NULL
);
1508 * Read a MLS level structure from a policydb binary
1509 * representation file.
1511 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1516 memset(lp
, 0, sizeof(*lp
));
1518 rc
= next_entry(buf
, fp
, sizeof buf
);
1520 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1523 lp
->sens
= le32_to_cpu(buf
[0]);
1525 rc
= ebitmap_read(&lp
->cat
, fp
);
1527 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1533 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1536 struct user_datum
*usrdatum
;
1537 int rc
, to_read
= 2;
1542 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1546 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1549 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1553 len
= le32_to_cpu(buf
[0]);
1554 usrdatum
->value
= le32_to_cpu(buf
[1]);
1555 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1556 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1558 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1562 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1566 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1567 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1570 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1575 rc
= hashtab_insert(h
, key
, usrdatum
);
1580 user_destroy(key
, usrdatum
, NULL
);
1584 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1587 struct level_datum
*levdatum
;
1593 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1597 rc
= next_entry(buf
, fp
, sizeof buf
);
1601 len
= le32_to_cpu(buf
[0]);
1602 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1604 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1609 levdatum
->level
= kmalloc(sizeof(struct mls_level
), GFP_ATOMIC
);
1610 if (!levdatum
->level
)
1613 rc
= mls_read_level(levdatum
->level
, fp
);
1617 rc
= hashtab_insert(h
, key
, levdatum
);
1622 sens_destroy(key
, levdatum
, NULL
);
1626 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1629 struct cat_datum
*catdatum
;
1635 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1639 rc
= next_entry(buf
, fp
, sizeof buf
);
1643 len
= le32_to_cpu(buf
[0]);
1644 catdatum
->value
= le32_to_cpu(buf
[1]);
1645 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1647 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1651 rc
= hashtab_insert(h
, key
, catdatum
);
1656 cat_destroy(key
, catdatum
, NULL
);
1660 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1672 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1674 struct user_datum
*upper
, *user
;
1675 struct policydb
*p
= datap
;
1678 upper
= user
= datum
;
1679 while (upper
->bounds
) {
1680 struct ebitmap_node
*node
;
1683 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1684 printk(KERN_ERR
"SELinux: user %s: "
1685 "too deep or looped boundary",
1690 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1691 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1692 if (ebitmap_get_bit(&upper
->roles
, bit
))
1696 "SELinux: boundary violated policy: "
1697 "user=%s role=%s bounds=%s\n",
1698 sym_name(p
, SYM_USERS
, user
->value
- 1),
1699 sym_name(p
, SYM_ROLES
, bit
),
1700 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1709 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1711 struct role_datum
*upper
, *role
;
1712 struct policydb
*p
= datap
;
1715 upper
= role
= datum
;
1716 while (upper
->bounds
) {
1717 struct ebitmap_node
*node
;
1720 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1721 printk(KERN_ERR
"SELinux: role %s: "
1722 "too deep or looped bounds\n",
1727 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1728 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1729 if (ebitmap_get_bit(&upper
->types
, bit
))
1733 "SELinux: boundary violated policy: "
1734 "role=%s type=%s bounds=%s\n",
1735 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1736 sym_name(p
, SYM_TYPES
, bit
),
1737 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1746 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1748 struct type_datum
*upper
;
1749 struct policydb
*p
= datap
;
1753 while (upper
->bounds
) {
1754 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1755 printk(KERN_ERR
"SELinux: type %s: "
1756 "too deep or looped boundary\n",
1761 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1765 if (upper
->attribute
) {
1766 printk(KERN_ERR
"SELinux: type %s: "
1767 "bounded by attribute %s",
1769 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1777 static int policydb_bounds_sanity_check(struct policydb
*p
)
1781 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1784 rc
= hashtab_map(p
->p_users
.table
,
1785 user_bounds_sanity_check
, p
);
1789 rc
= hashtab_map(p
->p_roles
.table
,
1790 role_bounds_sanity_check
, p
);
1794 rc
= hashtab_map(p
->p_types
.table
,
1795 type_bounds_sanity_check
, p
);
1802 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1804 struct class_datum
*cladatum
;
1806 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1810 return cladatum
->value
;
1813 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1815 struct class_datum
*cladatum
;
1816 struct perm_datum
*perdatum
= NULL
;
1817 struct common_datum
*comdatum
;
1819 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1822 cladatum
= p
->class_val_to_struct
[tclass
-1];
1823 comdatum
= cladatum
->comdatum
;
1825 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1828 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1833 return 1U << (perdatum
->value
-1);
1836 static int range_read(struct policydb
*p
, void *fp
)
1838 struct range_trans
*rt
= NULL
;
1839 struct mls_range
*r
= NULL
;
1844 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1847 rc
= next_entry(buf
, fp
, sizeof(u32
));
1851 nel
= le32_to_cpu(buf
[0]);
1852 for (i
= 0; i
< nel
; i
++) {
1854 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1858 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1862 rt
->source_type
= le32_to_cpu(buf
[0]);
1863 rt
->target_type
= le32_to_cpu(buf
[1]);
1864 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1865 rc
= next_entry(buf
, fp
, sizeof(u32
));
1868 rt
->target_class
= le32_to_cpu(buf
[0]);
1870 rt
->target_class
= p
->process_class
;
1873 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1874 !policydb_type_isvalid(p
, rt
->target_type
) ||
1875 !policydb_class_isvalid(p
, rt
->target_class
))
1879 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1883 rc
= mls_read_range_helper(r
, fp
);
1888 if (!mls_range_isvalid(p
, r
)) {
1889 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1893 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1900 hash_eval(p
->range_tr
, "rangetr");
1908 static int filename_trans_read(struct policydb
*p
, void *fp
)
1910 struct filename_trans
*ft
;
1911 struct filename_trans_datum
*otype
;
1917 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1920 rc
= next_entry(buf
, fp
, sizeof(u32
));
1923 nel
= le32_to_cpu(buf
[0]);
1925 for (i
= 0; i
< nel
; i
++) {
1931 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1936 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1940 /* length of the path component string */
1941 rc
= next_entry(buf
, fp
, sizeof(u32
));
1944 len
= le32_to_cpu(buf
[0]);
1946 /* path component string */
1947 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1953 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1957 ft
->stype
= le32_to_cpu(buf
[0]);
1958 ft
->ttype
= le32_to_cpu(buf
[1]);
1959 ft
->tclass
= le32_to_cpu(buf
[2]);
1961 otype
->otype
= le32_to_cpu(buf
[3]);
1963 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1967 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1970 * Do not return -EEXIST to the caller, or the system
1975 /* But free memory to avoid memory leak. */
1981 hash_eval(p
->filename_trans
, "filenametr");
1991 static int genfs_read(struct policydb
*p
, void *fp
)
1994 u32 nel
, nel2
, len
, len2
;
1996 struct ocontext
*l
, *c
;
1997 struct ocontext
*newc
= NULL
;
1998 struct genfs
*genfs_p
, *genfs
;
1999 struct genfs
*newgenfs
= NULL
;
2001 rc
= next_entry(buf
, fp
, sizeof(u32
));
2004 nel
= le32_to_cpu(buf
[0]);
2006 for (i
= 0; i
< nel
; i
++) {
2007 rc
= next_entry(buf
, fp
, sizeof(u32
));
2010 len
= le32_to_cpu(buf
[0]);
2013 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2017 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2021 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2022 genfs_p
= genfs
, genfs
= genfs
->next
) {
2024 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2025 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2029 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2032 newgenfs
->next
= genfs
;
2034 genfs_p
->next
= newgenfs
;
2036 p
->genfs
= newgenfs
;
2040 rc
= next_entry(buf
, fp
, sizeof(u32
));
2044 nel2
= le32_to_cpu(buf
[0]);
2045 for (j
= 0; j
< nel2
; j
++) {
2046 rc
= next_entry(buf
, fp
, sizeof(u32
));
2049 len
= le32_to_cpu(buf
[0]);
2052 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2056 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2060 rc
= next_entry(buf
, fp
, sizeof(u32
));
2064 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2065 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2069 for (l
= NULL
, c
= genfs
->head
; c
;
2070 l
= c
, c
= c
->next
) {
2072 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2073 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2074 newc
->v
.sclass
== c
->v
.sclass
)) {
2075 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2076 genfs
->fstype
, c
->u
.name
);
2079 len
= strlen(newc
->u
.name
);
2080 len2
= strlen(c
->u
.name
);
2096 kfree(newgenfs
->fstype
);
2098 ocontext_destroy(newc
, OCON_FSUSE
);
2103 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2109 struct ocontext
*l
, *c
;
2112 for (i
= 0; i
< info
->ocon_num
; i
++) {
2113 rc
= next_entry(buf
, fp
, sizeof(u32
));
2116 nel
= le32_to_cpu(buf
[0]);
2119 for (j
= 0; j
< nel
; j
++) {
2121 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2127 p
->ocontexts
[i
] = c
;
2132 rc
= next_entry(buf
, fp
, sizeof(u32
));
2136 c
->sid
[0] = le32_to_cpu(buf
[0]);
2137 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2143 rc
= next_entry(buf
, fp
, sizeof(u32
));
2146 len
= le32_to_cpu(buf
[0]);
2148 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2152 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2155 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2160 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2163 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2164 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2165 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2166 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2171 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2174 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2175 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2176 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2181 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2186 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2187 /* Determined at runtime, not in policy DB. */
2188 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2190 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2193 len
= le32_to_cpu(buf
[1]);
2194 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2198 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2205 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2208 for (k
= 0; k
< 4; k
++)
2209 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2210 for (k
= 0; k
< 4; k
++)
2211 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2212 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2226 * Read the configuration data from a policy database binary
2227 * representation file into a policy database structure.
2229 int policydb_read(struct policydb
*p
, void *fp
)
2231 struct role_allow
*ra
, *lra
;
2232 struct role_trans
*tr
, *ltr
;
2235 u32 len
, nprim
, nel
;
2238 struct policydb_compat_info
*info
;
2240 rc
= policydb_init(p
);
2244 /* Read the magic number and string length. */
2245 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2250 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2251 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2252 "not match expected magic number 0x%x\n",
2253 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2258 len
= le32_to_cpu(buf
[1]);
2259 if (len
!= strlen(POLICYDB_STRING
)) {
2260 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2261 "match expected length %Zu\n",
2262 len
, strlen(POLICYDB_STRING
));
2267 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2268 if (!policydb_str
) {
2269 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2270 "string of length %d\n", len
);
2274 rc
= next_entry(policydb_str
, fp
, len
);
2276 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2277 kfree(policydb_str
);
2282 policydb_str
[len
] = '\0';
2283 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2284 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2285 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2286 kfree(policydb_str
);
2289 /* Done with policydb_str. */
2290 kfree(policydb_str
);
2291 policydb_str
= NULL
;
2293 /* Read the version and table sizes. */
2294 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2299 p
->policyvers
= le32_to_cpu(buf
[0]);
2300 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2301 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2302 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2303 "my version range %d-%d\n",
2304 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2308 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2312 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2313 printk(KERN_ERR
"SELinux: security policydb version %d "
2314 "(MLS) not backwards compatible\n",
2319 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2320 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2322 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2323 rc
= ebitmap_read(&p
->policycaps
, fp
);
2328 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2329 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2335 info
= policydb_lookup_compat(p
->policyvers
);
2337 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2338 "for version %d\n", p
->policyvers
);
2343 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2344 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2345 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2346 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2347 le32_to_cpu(buf
[3]),
2348 info
->sym_num
, info
->ocon_num
);
2352 for (i
= 0; i
< info
->sym_num
; i
++) {
2353 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2356 nprim
= le32_to_cpu(buf
[0]);
2357 nel
= le32_to_cpu(buf
[1]);
2358 for (j
= 0; j
< nel
; j
++) {
2359 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2364 p
->symtab
[i
].nprim
= nprim
;
2368 p
->process_class
= string_to_security_class(p
, "process");
2369 if (!p
->process_class
)
2372 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2376 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2377 rc
= cond_read_list(p
, fp
);
2382 rc
= next_entry(buf
, fp
, sizeof(u32
));
2385 nel
= le32_to_cpu(buf
[0]);
2387 for (i
= 0; i
< nel
; i
++) {
2389 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2396 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2401 tr
->role
= le32_to_cpu(buf
[0]);
2402 tr
->type
= le32_to_cpu(buf
[1]);
2403 tr
->new_role
= le32_to_cpu(buf
[2]);
2404 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2405 rc
= next_entry(buf
, fp
, sizeof(u32
));
2408 tr
->tclass
= le32_to_cpu(buf
[0]);
2410 tr
->tclass
= p
->process_class
;
2412 if (!policydb_role_isvalid(p
, tr
->role
) ||
2413 !policydb_type_isvalid(p
, tr
->type
) ||
2414 !policydb_class_isvalid(p
, tr
->tclass
) ||
2415 !policydb_role_isvalid(p
, tr
->new_role
))
2420 rc
= next_entry(buf
, fp
, sizeof(u32
));
2423 nel
= le32_to_cpu(buf
[0]);
2425 for (i
= 0; i
< nel
; i
++) {
2427 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2434 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2439 ra
->role
= le32_to_cpu(buf
[0]);
2440 ra
->new_role
= le32_to_cpu(buf
[1]);
2441 if (!policydb_role_isvalid(p
, ra
->role
) ||
2442 !policydb_role_isvalid(p
, ra
->new_role
))
2447 rc
= filename_trans_read(p
, fp
);
2451 rc
= policydb_index(p
);
2456 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2457 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2458 if (!p
->process_trans_perms
)
2461 rc
= ocontext_read(p
, info
, fp
);
2465 rc
= genfs_read(p
, fp
);
2469 rc
= range_read(p
, fp
);
2474 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2476 GFP_KERNEL
| __GFP_ZERO
);
2477 if (!p
->type_attr_map_array
)
2480 /* preallocate so we don't have to worry about the put ever failing */
2481 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2482 GFP_KERNEL
| __GFP_ZERO
);
2486 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2487 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2491 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2492 rc
= ebitmap_read(e
, fp
);
2496 /* add the type itself as the degenerate case */
2497 rc
= ebitmap_set_bit(e
, i
, 1);
2502 rc
= policydb_bounds_sanity_check(p
);
2510 policydb_destroy(p
);
2515 * Write a MLS level structure to a policydb binary
2516 * representation file.
2518 static int mls_write_level(struct mls_level
*l
, void *fp
)
2523 buf
[0] = cpu_to_le32(l
->sens
);
2524 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2528 rc
= ebitmap_write(&l
->cat
, fp
);
2536 * Write a MLS range structure to a policydb binary
2537 * representation file.
2539 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2545 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2551 buf
[0] = cpu_to_le32(items
-1);
2552 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2554 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2556 BUG_ON(items
> ARRAY_SIZE(buf
));
2558 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2562 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2566 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2574 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2577 struct level_datum
*levdatum
= datum
;
2578 struct policy_data
*pd
= ptr
;
2585 buf
[0] = cpu_to_le32(len
);
2586 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2587 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2591 rc
= put_entry(key
, 1, len
, fp
);
2595 rc
= mls_write_level(levdatum
->level
, fp
);
2602 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2605 struct cat_datum
*catdatum
= datum
;
2606 struct policy_data
*pd
= ptr
;
2613 buf
[0] = cpu_to_le32(len
);
2614 buf
[1] = cpu_to_le32(catdatum
->value
);
2615 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2616 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2620 rc
= put_entry(key
, 1, len
, fp
);
2627 static int role_trans_write(struct policydb
*p
, void *fp
)
2629 struct role_trans
*r
= p
->role_tr
;
2630 struct role_trans
*tr
;
2636 for (tr
= r
; tr
; tr
= tr
->next
)
2638 buf
[0] = cpu_to_le32(nel
);
2639 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2642 for (tr
= r
; tr
; tr
= tr
->next
) {
2643 buf
[0] = cpu_to_le32(tr
->role
);
2644 buf
[1] = cpu_to_le32(tr
->type
);
2645 buf
[2] = cpu_to_le32(tr
->new_role
);
2646 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2649 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2650 buf
[0] = cpu_to_le32(tr
->tclass
);
2651 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2660 static int role_allow_write(struct role_allow
*r
, void *fp
)
2662 struct role_allow
*ra
;
2668 for (ra
= r
; ra
; ra
= ra
->next
)
2670 buf
[0] = cpu_to_le32(nel
);
2671 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2674 for (ra
= r
; ra
; ra
= ra
->next
) {
2675 buf
[0] = cpu_to_le32(ra
->role
);
2676 buf
[1] = cpu_to_le32(ra
->new_role
);
2677 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2685 * Write a security context structure
2686 * to a policydb binary representation file.
2688 static int context_write(struct policydb
*p
, struct context
*c
,
2694 buf
[0] = cpu_to_le32(c
->user
);
2695 buf
[1] = cpu_to_le32(c
->role
);
2696 buf
[2] = cpu_to_le32(c
->type
);
2698 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2702 rc
= mls_write_range_helper(&c
->range
, fp
);
2710 * The following *_write functions are used to
2711 * write the symbol data to a policy database
2712 * binary representation file.
2715 static int perm_write(void *vkey
, void *datum
, void *fp
)
2718 struct perm_datum
*perdatum
= datum
;
2724 buf
[0] = cpu_to_le32(len
);
2725 buf
[1] = cpu_to_le32(perdatum
->value
);
2726 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2730 rc
= put_entry(key
, 1, len
, fp
);
2737 static int common_write(void *vkey
, void *datum
, void *ptr
)
2740 struct common_datum
*comdatum
= datum
;
2741 struct policy_data
*pd
= ptr
;
2748 buf
[0] = cpu_to_le32(len
);
2749 buf
[1] = cpu_to_le32(comdatum
->value
);
2750 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2751 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2752 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2756 rc
= put_entry(key
, 1, len
, fp
);
2760 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2767 static int type_set_write(struct type_set
*t
, void *fp
)
2772 if (ebitmap_write(&t
->types
, fp
))
2774 if (ebitmap_write(&t
->negset
, fp
))
2777 buf
[0] = cpu_to_le32(t
->flags
);
2778 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2785 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2788 struct constraint_node
*c
;
2789 struct constraint_expr
*e
;
2794 for (c
= node
; c
; c
= c
->next
) {
2796 for (e
= c
->expr
; e
; e
= e
->next
)
2798 buf
[0] = cpu_to_le32(c
->permissions
);
2799 buf
[1] = cpu_to_le32(nel
);
2800 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2803 for (e
= c
->expr
; e
; e
= e
->next
) {
2804 buf
[0] = cpu_to_le32(e
->expr_type
);
2805 buf
[1] = cpu_to_le32(e
->attr
);
2806 buf
[2] = cpu_to_le32(e
->op
);
2807 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2811 switch (e
->expr_type
) {
2813 rc
= ebitmap_write(&e
->names
, fp
);
2816 if (p
->policyvers
>=
2817 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2818 rc
= type_set_write(e
->type_names
, fp
);
2832 static int class_write(void *vkey
, void *datum
, void *ptr
)
2835 struct class_datum
*cladatum
= datum
;
2836 struct policy_data
*pd
= ptr
;
2838 struct policydb
*p
= pd
->p
;
2839 struct constraint_node
*c
;
2846 if (cladatum
->comkey
)
2847 len2
= strlen(cladatum
->comkey
);
2852 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2855 buf
[0] = cpu_to_le32(len
);
2856 buf
[1] = cpu_to_le32(len2
);
2857 buf
[2] = cpu_to_le32(cladatum
->value
);
2858 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2859 if (cladatum
->permissions
.table
)
2860 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2863 buf
[5] = cpu_to_le32(ncons
);
2864 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2868 rc
= put_entry(key
, 1, len
, fp
);
2872 if (cladatum
->comkey
) {
2873 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2878 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2882 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2886 /* write out the validatetrans rule */
2888 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2891 buf
[0] = cpu_to_le32(ncons
);
2892 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2896 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2900 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2901 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2902 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2903 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2905 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2910 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2911 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2912 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2920 static int role_write(void *vkey
, void *datum
, void *ptr
)
2923 struct role_datum
*role
= datum
;
2924 struct policy_data
*pd
= ptr
;
2926 struct policydb
*p
= pd
->p
;
2933 buf
[items
++] = cpu_to_le32(len
);
2934 buf
[items
++] = cpu_to_le32(role
->value
);
2935 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
2936 buf
[items
++] = cpu_to_le32(role
->bounds
);
2938 BUG_ON(items
> ARRAY_SIZE(buf
));
2940 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2944 rc
= put_entry(key
, 1, len
, fp
);
2948 rc
= ebitmap_write(&role
->dominates
, fp
);
2952 rc
= ebitmap_write(&role
->types
, fp
);
2959 static int type_write(void *vkey
, void *datum
, void *ptr
)
2962 struct type_datum
*typdatum
= datum
;
2963 struct policy_data
*pd
= ptr
;
2964 struct policydb
*p
= pd
->p
;
2972 buf
[items
++] = cpu_to_le32(len
);
2973 buf
[items
++] = cpu_to_le32(typdatum
->value
);
2974 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
2977 if (typdatum
->primary
)
2978 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
2980 if (typdatum
->attribute
)
2981 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
2983 buf
[items
++] = cpu_to_le32(properties
);
2984 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
2986 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
2988 BUG_ON(items
> ARRAY_SIZE(buf
));
2989 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2993 rc
= put_entry(key
, 1, len
, fp
);
3000 static int user_write(void *vkey
, void *datum
, void *ptr
)
3003 struct user_datum
*usrdatum
= datum
;
3004 struct policy_data
*pd
= ptr
;
3005 struct policydb
*p
= pd
->p
;
3013 buf
[items
++] = cpu_to_le32(len
);
3014 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3015 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3016 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3017 BUG_ON(items
> ARRAY_SIZE(buf
));
3018 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3022 rc
= put_entry(key
, 1, len
, fp
);
3026 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3030 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3034 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3041 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3054 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3057 unsigned int i
, j
, rc
;
3062 for (i
= 0; i
< info
->ocon_num
; i
++) {
3064 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3066 buf
[0] = cpu_to_le32(nel
);
3067 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3070 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3073 buf
[0] = cpu_to_le32(c
->sid
[0]);
3074 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3077 rc
= context_write(p
, &c
->context
[0], fp
);
3083 len
= strlen(c
->u
.name
);
3084 buf
[0] = cpu_to_le32(len
);
3085 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3088 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3091 rc
= context_write(p
, &c
->context
[0], fp
);
3094 rc
= context_write(p
, &c
->context
[1], fp
);
3099 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3100 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3101 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3102 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3105 rc
= context_write(p
, &c
->context
[0], fp
);
3110 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3111 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3112 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3115 rc
= context_write(p
, &c
->context
[0], fp
);
3120 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3121 len
= strlen(c
->u
.name
);
3122 buf
[1] = cpu_to_le32(len
);
3123 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3126 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3129 rc
= context_write(p
, &c
->context
[0], fp
);
3134 for (j
= 0; j
< 4; j
++)
3135 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3136 for (j
= 0; j
< 4; j
++)
3137 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3138 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3141 rc
= context_write(p
, &c
->context
[0], fp
);
3151 static int genfs_write(struct policydb
*p
, void *fp
)
3153 struct genfs
*genfs
;
3160 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3162 buf
[0] = cpu_to_le32(len
);
3163 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3166 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3167 len
= strlen(genfs
->fstype
);
3168 buf
[0] = cpu_to_le32(len
);
3169 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3172 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3176 for (c
= genfs
->head
; c
; c
= c
->next
)
3178 buf
[0] = cpu_to_le32(len
);
3179 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3182 for (c
= genfs
->head
; c
; c
= c
->next
) {
3183 len
= strlen(c
->u
.name
);
3184 buf
[0] = cpu_to_le32(len
);
3185 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3188 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3191 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3192 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3195 rc
= context_write(p
, &c
->context
[0], fp
);
3203 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3211 static int range_write_helper(void *key
, void *data
, void *ptr
)
3214 struct range_trans
*rt
= key
;
3215 struct mls_range
*r
= data
;
3216 struct policy_data
*pd
= ptr
;
3218 struct policydb
*p
= pd
->p
;
3221 buf
[0] = cpu_to_le32(rt
->source_type
);
3222 buf
[1] = cpu_to_le32(rt
->target_type
);
3223 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3226 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3227 buf
[0] = cpu_to_le32(rt
->target_class
);
3228 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3232 rc
= mls_write_range_helper(r
, fp
);
3239 static int range_write(struct policydb
*p
, void *fp
)
3243 struct policy_data pd
;
3248 /* count the number of entries in the hashtab */
3250 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3254 buf
[0] = cpu_to_le32(nel
);
3255 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3259 /* actually write all of the entries */
3260 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3267 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3270 struct filename_trans
*ft
= key
;
3271 struct filename_trans_datum
*otype
= data
;
3276 len
= strlen(ft
->name
);
3277 buf
[0] = cpu_to_le32(len
);
3278 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3282 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3286 buf
[0] = cpu_to_le32(ft
->stype
);
3287 buf
[1] = cpu_to_le32(ft
->ttype
);
3288 buf
[2] = cpu_to_le32(ft
->tclass
);
3289 buf
[3] = cpu_to_le32(otype
->otype
);
3291 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3298 static int filename_trans_write(struct policydb
*p
, void *fp
)
3304 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3308 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3312 buf
[0] = cpu_to_le32(nel
);
3313 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3317 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3325 * Write the configuration data in a policy database
3326 * structure to a policy database binary representation
3329 int policydb_write(struct policydb
*p
, void *fp
)
3331 unsigned int i
, num_syms
;
3336 struct policydb_compat_info
*info
;
3339 * refuse to write policy older than compressed avtab
3340 * to simplify the writer. There are other tests dropped
3341 * since we assume this throughout the writer code. Be
3342 * careful if you ever try to remove this restriction
3344 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3345 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3346 " Because it is less than version %d\n", p
->policyvers
,
3347 POLICYDB_VERSION_AVTAB
);
3353 config
|= POLICYDB_CONFIG_MLS
;
3355 if (p
->reject_unknown
)
3356 config
|= REJECT_UNKNOWN
;
3357 if (p
->allow_unknown
)
3358 config
|= ALLOW_UNKNOWN
;
3360 /* Write the magic number and string identifiers. */
3361 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3362 len
= strlen(POLICYDB_STRING
);
3363 buf
[1] = cpu_to_le32(len
);
3364 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3367 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3371 /* Write the version, config, and table sizes. */
3372 info
= policydb_lookup_compat(p
->policyvers
);
3374 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3375 "version %d", p
->policyvers
);
3379 buf
[0] = cpu_to_le32(p
->policyvers
);
3380 buf
[1] = cpu_to_le32(config
);
3381 buf
[2] = cpu_to_le32(info
->sym_num
);
3382 buf
[3] = cpu_to_le32(info
->ocon_num
);
3384 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3388 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3389 rc
= ebitmap_write(&p
->policycaps
, fp
);
3394 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3395 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3400 num_syms
= info
->sym_num
;
3401 for (i
= 0; i
< num_syms
; i
++) {
3402 struct policy_data pd
;
3407 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3408 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3410 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3413 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3418 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3422 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3426 rc
= role_trans_write(p
, fp
);
3430 rc
= role_allow_write(p
->role_allow
, fp
);
3434 rc
= filename_trans_write(p
, fp
);
3438 rc
= ocontext_write(p
, info
, fp
);
3442 rc
= genfs_write(p
, fp
);
3446 rc
= range_write(p
, fp
);
3450 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3451 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3454 rc
= ebitmap_write(e
, fp
);