2 * Implementation of the policy database.
4 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
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 * Update: Mellanox Techonologies
22 * Added Infiniband support
24 * Copyright (C) 2016 Mellanox Techonologies
25 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
26 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
27 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/audit.h>
39 #include <linux/flex_array.h>
43 #include "conditional.h"
50 static const char *symtab_name
[SYM_NUM
] = {
62 static unsigned int symtab_sizes
[SYM_NUM
] = {
73 struct policydb_compat_info
{
79 /* These need to be updated if SYM_NUM or OCON_NUM changes */
80 static struct policydb_compat_info policydb_compat
[] = {
82 .version
= POLICYDB_VERSION_BASE
,
83 .sym_num
= SYM_NUM
- 3,
84 .ocon_num
= OCON_NUM
- 3,
87 .version
= POLICYDB_VERSION_BOOL
,
88 .sym_num
= SYM_NUM
- 2,
89 .ocon_num
= OCON_NUM
- 3,
92 .version
= POLICYDB_VERSION_IPV6
,
93 .sym_num
= SYM_NUM
- 2,
94 .ocon_num
= OCON_NUM
- 2,
97 .version
= POLICYDB_VERSION_NLCLASS
,
98 .sym_num
= SYM_NUM
- 2,
99 .ocon_num
= OCON_NUM
- 2,
102 .version
= POLICYDB_VERSION_MLS
,
104 .ocon_num
= OCON_NUM
- 2,
107 .version
= POLICYDB_VERSION_AVTAB
,
109 .ocon_num
= OCON_NUM
- 2,
112 .version
= POLICYDB_VERSION_RANGETRANS
,
114 .ocon_num
= OCON_NUM
- 2,
117 .version
= POLICYDB_VERSION_POLCAP
,
119 .ocon_num
= OCON_NUM
- 2,
122 .version
= POLICYDB_VERSION_PERMISSIVE
,
124 .ocon_num
= OCON_NUM
- 2,
127 .version
= POLICYDB_VERSION_BOUNDARY
,
129 .ocon_num
= OCON_NUM
- 2,
132 .version
= POLICYDB_VERSION_FILENAME_TRANS
,
134 .ocon_num
= OCON_NUM
- 2,
137 .version
= POLICYDB_VERSION_ROLETRANS
,
139 .ocon_num
= OCON_NUM
- 2,
142 .version
= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
,
144 .ocon_num
= OCON_NUM
- 2,
147 .version
= POLICYDB_VERSION_DEFAULT_TYPE
,
149 .ocon_num
= OCON_NUM
- 2,
152 .version
= POLICYDB_VERSION_CONSTRAINT_NAMES
,
154 .ocon_num
= OCON_NUM
- 2,
157 .version
= POLICYDB_VERSION_XPERMS_IOCTL
,
159 .ocon_num
= OCON_NUM
- 2,
162 .version
= POLICYDB_VERSION_INFINIBAND
,
164 .ocon_num
= OCON_NUM
,
168 static struct policydb_compat_info
*policydb_lookup_compat(int version
)
171 struct policydb_compat_info
*info
= NULL
;
173 for (i
= 0; i
< ARRAY_SIZE(policydb_compat
); i
++) {
174 if (policydb_compat
[i
].version
== version
) {
175 info
= &policydb_compat
[i
];
183 * Initialize the role table.
185 static int roles_init(struct policydb
*p
)
189 struct role_datum
*role
;
191 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
196 role
->value
= ++p
->p_roles
.nprim
;
197 if (role
->value
!= OBJECT_R_VAL
)
201 key
= kstrdup(OBJECT_R
, GFP_KERNEL
);
205 rc
= hashtab_insert(p
->p_roles
.table
, key
, role
);
216 static u32
filenametr_hash(struct hashtab
*h
, const void *k
)
218 const struct filename_trans
*ft
= k
;
220 unsigned int byte_num
;
223 hash
= ft
->stype
^ ft
->ttype
^ ft
->tclass
;
226 while ((focus
= ft
->name
[byte_num
++]))
227 hash
= partial_name_hash(focus
, hash
);
228 return hash
& (h
->size
- 1);
231 static int filenametr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
233 const struct filename_trans
*ft1
= k1
;
234 const struct filename_trans
*ft2
= k2
;
237 v
= ft1
->stype
- ft2
->stype
;
241 v
= ft1
->ttype
- ft2
->ttype
;
245 v
= ft1
->tclass
- ft2
->tclass
;
249 return strcmp(ft1
->name
, ft2
->name
);
253 static u32
rangetr_hash(struct hashtab
*h
, const void *k
)
255 const struct range_trans
*key
= k
;
256 return (key
->source_type
+ (key
->target_type
<< 3) +
257 (key
->target_class
<< 5)) & (h
->size
- 1);
260 static int rangetr_cmp(struct hashtab
*h
, const void *k1
, const void *k2
)
262 const struct range_trans
*key1
= k1
, *key2
= k2
;
265 v
= key1
->source_type
- key2
->source_type
;
269 v
= key1
->target_type
- key2
->target_type
;
273 v
= key1
->target_class
- key2
->target_class
;
279 * Initialize a policy database structure.
281 static int policydb_init(struct policydb
*p
)
285 memset(p
, 0, sizeof(*p
));
287 for (i
= 0; i
< SYM_NUM
; i
++) {
288 rc
= symtab_init(&p
->symtab
[i
], symtab_sizes
[i
]);
293 rc
= avtab_init(&p
->te_avtab
);
301 rc
= cond_policydb_init(p
);
305 p
->filename_trans
= hashtab_create(filenametr_hash
, filenametr_cmp
, (1 << 10));
306 if (!p
->filename_trans
) {
311 p
->range_tr
= hashtab_create(rangetr_hash
, rangetr_cmp
, 256);
317 ebitmap_init(&p
->filename_trans_ttypes
);
318 ebitmap_init(&p
->policycaps
);
319 ebitmap_init(&p
->permissive_map
);
323 hashtab_destroy(p
->filename_trans
);
324 hashtab_destroy(p
->range_tr
);
325 for (i
= 0; i
< SYM_NUM
; i
++)
326 hashtab_destroy(p
->symtab
[i
].table
);
331 * The following *_index functions are used to
332 * define the val_to_name and val_to_struct arrays
333 * in a policy database structure. The val_to_name
334 * arrays are used when converting security context
335 * structures into string representations. The
336 * val_to_struct arrays are used when the attributes
337 * of a class, role, or user are needed.
340 static int common_index(void *key
, void *datum
, void *datap
)
343 struct common_datum
*comdatum
;
344 struct flex_array
*fa
;
348 if (!comdatum
->value
|| comdatum
->value
> p
->p_commons
.nprim
)
351 fa
= p
->sym_val_to_name
[SYM_COMMONS
];
352 if (flex_array_put_ptr(fa
, comdatum
->value
- 1, key
,
353 GFP_KERNEL
| __GFP_ZERO
))
358 static int class_index(void *key
, void *datum
, void *datap
)
361 struct class_datum
*cladatum
;
362 struct flex_array
*fa
;
366 if (!cladatum
->value
|| cladatum
->value
> p
->p_classes
.nprim
)
368 fa
= p
->sym_val_to_name
[SYM_CLASSES
];
369 if (flex_array_put_ptr(fa
, cladatum
->value
- 1, key
,
370 GFP_KERNEL
| __GFP_ZERO
))
372 p
->class_val_to_struct
[cladatum
->value
- 1] = cladatum
;
376 static int role_index(void *key
, void *datum
, void *datap
)
379 struct role_datum
*role
;
380 struct flex_array
*fa
;
385 || role
->value
> p
->p_roles
.nprim
386 || role
->bounds
> p
->p_roles
.nprim
)
389 fa
= p
->sym_val_to_name
[SYM_ROLES
];
390 if (flex_array_put_ptr(fa
, role
->value
- 1, key
,
391 GFP_KERNEL
| __GFP_ZERO
))
393 p
->role_val_to_struct
[role
->value
- 1] = role
;
397 static int type_index(void *key
, void *datum
, void *datap
)
400 struct type_datum
*typdatum
;
401 struct flex_array
*fa
;
406 if (typdatum
->primary
) {
408 || typdatum
->value
> p
->p_types
.nprim
409 || typdatum
->bounds
> p
->p_types
.nprim
)
411 fa
= p
->sym_val_to_name
[SYM_TYPES
];
412 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, key
,
413 GFP_KERNEL
| __GFP_ZERO
))
416 fa
= p
->type_val_to_struct_array
;
417 if (flex_array_put_ptr(fa
, typdatum
->value
- 1, typdatum
,
418 GFP_KERNEL
| __GFP_ZERO
))
425 static int user_index(void *key
, void *datum
, void *datap
)
428 struct user_datum
*usrdatum
;
429 struct flex_array
*fa
;
434 || usrdatum
->value
> p
->p_users
.nprim
435 || usrdatum
->bounds
> p
->p_users
.nprim
)
438 fa
= p
->sym_val_to_name
[SYM_USERS
];
439 if (flex_array_put_ptr(fa
, usrdatum
->value
- 1, key
,
440 GFP_KERNEL
| __GFP_ZERO
))
442 p
->user_val_to_struct
[usrdatum
->value
- 1] = usrdatum
;
446 static int sens_index(void *key
, void *datum
, void *datap
)
449 struct level_datum
*levdatum
;
450 struct flex_array
*fa
;
455 if (!levdatum
->isalias
) {
456 if (!levdatum
->level
->sens
||
457 levdatum
->level
->sens
> p
->p_levels
.nprim
)
459 fa
= p
->sym_val_to_name
[SYM_LEVELS
];
460 if (flex_array_put_ptr(fa
, levdatum
->level
->sens
- 1, key
,
461 GFP_KERNEL
| __GFP_ZERO
))
468 static int cat_index(void *key
, void *datum
, void *datap
)
471 struct cat_datum
*catdatum
;
472 struct flex_array
*fa
;
477 if (!catdatum
->isalias
) {
478 if (!catdatum
->value
|| catdatum
->value
> p
->p_cats
.nprim
)
480 fa
= p
->sym_val_to_name
[SYM_CATS
];
481 if (flex_array_put_ptr(fa
, catdatum
->value
- 1, key
,
482 GFP_KERNEL
| __GFP_ZERO
))
489 static int (*index_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
502 static void hash_eval(struct hashtab
*h
, const char *hash_name
)
504 struct hashtab_info info
;
506 hashtab_stat(h
, &info
);
507 printk(KERN_DEBUG
"SELinux: %s: %d entries and %d/%d buckets used, "
508 "longest chain length %d\n", hash_name
, h
->nel
,
509 info
.slots_used
, h
->size
, info
.max_chain_len
);
512 static void symtab_hash_eval(struct symtab
*s
)
516 for (i
= 0; i
< SYM_NUM
; i
++)
517 hash_eval(s
[i
].table
, symtab_name
[i
]);
521 static inline void hash_eval(struct hashtab
*h
, char *hash_name
)
527 * Define the other val_to_name and val_to_struct arrays
528 * in a policy database structure.
530 * Caller must clean up on failure.
532 static int policydb_index(struct policydb
*p
)
536 printk(KERN_DEBUG
"SELinux: %d users, %d roles, %d types, %d bools",
537 p
->p_users
.nprim
, p
->p_roles
.nprim
, p
->p_types
.nprim
, p
->p_bools
.nprim
);
539 printk(KERN_CONT
", %d sens, %d cats", p
->p_levels
.nprim
,
541 printk(KERN_CONT
"\n");
543 printk(KERN_DEBUG
"SELinux: %d classes, %d rules\n",
544 p
->p_classes
.nprim
, p
->te_avtab
.nel
);
547 avtab_hash_eval(&p
->te_avtab
, "rules");
548 symtab_hash_eval(p
->symtab
);
551 p
->class_val_to_struct
= kcalloc(p
->p_classes
.nprim
,
552 sizeof(*p
->class_val_to_struct
),
554 if (!p
->class_val_to_struct
)
557 p
->role_val_to_struct
= kcalloc(p
->p_roles
.nprim
,
558 sizeof(*p
->role_val_to_struct
),
560 if (!p
->role_val_to_struct
)
563 p
->user_val_to_struct
= kcalloc(p
->p_users
.nprim
,
564 sizeof(*p
->user_val_to_struct
),
566 if (!p
->user_val_to_struct
)
569 /* Yes, I want the sizeof the pointer, not the structure */
570 p
->type_val_to_struct_array
= flex_array_alloc(sizeof(struct type_datum
*),
572 GFP_KERNEL
| __GFP_ZERO
);
573 if (!p
->type_val_to_struct_array
)
576 rc
= flex_array_prealloc(p
->type_val_to_struct_array
, 0,
577 p
->p_types
.nprim
, GFP_KERNEL
| __GFP_ZERO
);
581 rc
= cond_init_bool_indexes(p
);
585 for (i
= 0; i
< SYM_NUM
; i
++) {
586 p
->sym_val_to_name
[i
] = flex_array_alloc(sizeof(char *),
588 GFP_KERNEL
| __GFP_ZERO
);
589 if (!p
->sym_val_to_name
[i
])
592 rc
= flex_array_prealloc(p
->sym_val_to_name
[i
],
593 0, p
->symtab
[i
].nprim
,
594 GFP_KERNEL
| __GFP_ZERO
);
598 rc
= hashtab_map(p
->symtab
[i
].table
, index_f
[i
], p
);
608 * The following *_destroy functions are used to
609 * free any memory allocated for each kind of
610 * symbol data in the policy database.
613 static int perm_destroy(void *key
, void *datum
, void *p
)
620 static int common_destroy(void *key
, void *datum
, void *p
)
622 struct common_datum
*comdatum
;
627 hashtab_map(comdatum
->permissions
.table
, perm_destroy
, NULL
);
628 hashtab_destroy(comdatum
->permissions
.table
);
634 static void constraint_expr_destroy(struct constraint_expr
*expr
)
637 ebitmap_destroy(&expr
->names
);
638 if (expr
->type_names
) {
639 ebitmap_destroy(&expr
->type_names
->types
);
640 ebitmap_destroy(&expr
->type_names
->negset
);
641 kfree(expr
->type_names
);
647 static int cls_destroy(void *key
, void *datum
, void *p
)
649 struct class_datum
*cladatum
;
650 struct constraint_node
*constraint
, *ctemp
;
651 struct constraint_expr
*e
, *etmp
;
656 hashtab_map(cladatum
->permissions
.table
, perm_destroy
, NULL
);
657 hashtab_destroy(cladatum
->permissions
.table
);
658 constraint
= cladatum
->constraints
;
660 e
= constraint
->expr
;
664 constraint_expr_destroy(etmp
);
667 constraint
= constraint
->next
;
671 constraint
= cladatum
->validatetrans
;
673 e
= constraint
->expr
;
677 constraint_expr_destroy(etmp
);
680 constraint
= constraint
->next
;
683 kfree(cladatum
->comkey
);
689 static int role_destroy(void *key
, void *datum
, void *p
)
691 struct role_datum
*role
;
696 ebitmap_destroy(&role
->dominates
);
697 ebitmap_destroy(&role
->types
);
703 static int type_destroy(void *key
, void *datum
, void *p
)
710 static int user_destroy(void *key
, void *datum
, void *p
)
712 struct user_datum
*usrdatum
;
717 ebitmap_destroy(&usrdatum
->roles
);
718 ebitmap_destroy(&usrdatum
->range
.level
[0].cat
);
719 ebitmap_destroy(&usrdatum
->range
.level
[1].cat
);
720 ebitmap_destroy(&usrdatum
->dfltlevel
.cat
);
726 static int sens_destroy(void *key
, void *datum
, void *p
)
728 struct level_datum
*levdatum
;
734 ebitmap_destroy(&levdatum
->level
->cat
);
735 kfree(levdatum
->level
);
741 static int cat_destroy(void *key
, void *datum
, void *p
)
748 static int (*destroy_f
[SYM_NUM
]) (void *key
, void *datum
, void *datap
) =
760 static int filenametr_destroy(void *key
, void *datum
, void *p
)
762 struct filename_trans
*ft
= key
;
770 static int range_tr_destroy(void *key
, void *datum
, void *p
)
772 struct mls_range
*rt
= datum
;
774 ebitmap_destroy(&rt
->level
[0].cat
);
775 ebitmap_destroy(&rt
->level
[1].cat
);
781 static void ocontext_destroy(struct ocontext
*c
, int i
)
786 context_destroy(&c
->context
[0]);
787 context_destroy(&c
->context
[1]);
788 if (i
== OCON_ISID
|| i
== OCON_FS
||
789 i
== OCON_NETIF
|| i
== OCON_FSUSE
)
795 * Free any memory allocated by a policy database structure.
797 void policydb_destroy(struct policydb
*p
)
799 struct ocontext
*c
, *ctmp
;
800 struct genfs
*g
, *gtmp
;
802 struct role_allow
*ra
, *lra
= NULL
;
803 struct role_trans
*tr
, *ltr
= NULL
;
805 for (i
= 0; i
< SYM_NUM
; i
++) {
807 hashtab_map(p
->symtab
[i
].table
, destroy_f
[i
], NULL
);
808 hashtab_destroy(p
->symtab
[i
].table
);
811 for (i
= 0; i
< SYM_NUM
; i
++) {
812 if (p
->sym_val_to_name
[i
])
813 flex_array_free(p
->sym_val_to_name
[i
]);
816 kfree(p
->class_val_to_struct
);
817 kfree(p
->role_val_to_struct
);
818 kfree(p
->user_val_to_struct
);
819 if (p
->type_val_to_struct_array
)
820 flex_array_free(p
->type_val_to_struct_array
);
822 avtab_destroy(&p
->te_avtab
);
824 for (i
= 0; i
< OCON_NUM
; i
++) {
830 ocontext_destroy(ctmp
, i
);
832 p
->ocontexts
[i
] = NULL
;
843 ocontext_destroy(ctmp
, OCON_FSUSE
);
851 cond_policydb_destroy(p
);
853 for (tr
= p
->role_tr
; tr
; tr
= tr
->next
) {
860 for (ra
= p
->role_allow
; ra
; ra
= ra
->next
) {
867 hashtab_map(p
->filename_trans
, filenametr_destroy
, NULL
);
868 hashtab_destroy(p
->filename_trans
);
870 hashtab_map(p
->range_tr
, range_tr_destroy
, NULL
);
871 hashtab_destroy(p
->range_tr
);
873 if (p
->type_attr_map_array
) {
874 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
877 e
= flex_array_get(p
->type_attr_map_array
, i
);
882 flex_array_free(p
->type_attr_map_array
);
885 ebitmap_destroy(&p
->filename_trans_ttypes
);
886 ebitmap_destroy(&p
->policycaps
);
887 ebitmap_destroy(&p
->permissive_map
);
891 * Load the initial SIDs specified in a policy database
892 * structure into a SID table.
894 int policydb_load_isids(struct policydb
*p
, struct sidtab
*s
)
896 struct ocontext
*head
, *c
;
901 printk(KERN_ERR
"SELinux: out of memory on SID table init\n");
905 head
= p
->ocontexts
[OCON_ISID
];
906 for (c
= head
; c
; c
= c
->next
) {
908 if (!c
->context
[0].user
) {
909 printk(KERN_ERR
"SELinux: SID %s was never defined.\n",
914 rc
= sidtab_insert(s
, c
->sid
[0], &c
->context
[0]);
916 printk(KERN_ERR
"SELinux: unable to load initial SID %s.\n",
926 int policydb_class_isvalid(struct policydb
*p
, unsigned int class)
928 if (!class || class > p
->p_classes
.nprim
)
933 int policydb_role_isvalid(struct policydb
*p
, unsigned int role
)
935 if (!role
|| role
> p
->p_roles
.nprim
)
940 int policydb_type_isvalid(struct policydb
*p
, unsigned int type
)
942 if (!type
|| type
> p
->p_types
.nprim
)
948 * Return 1 if the fields in the security context
949 * structure `c' are valid. Return 0 otherwise.
951 int policydb_context_isvalid(struct policydb
*p
, struct context
*c
)
953 struct role_datum
*role
;
954 struct user_datum
*usrdatum
;
956 if (!c
->role
|| c
->role
> p
->p_roles
.nprim
)
959 if (!c
->user
|| c
->user
> p
->p_users
.nprim
)
962 if (!c
->type
|| c
->type
> p
->p_types
.nprim
)
965 if (c
->role
!= OBJECT_R_VAL
) {
967 * Role must be authorized for the type.
969 role
= p
->role_val_to_struct
[c
->role
- 1];
970 if (!role
|| !ebitmap_get_bit(&role
->types
, c
->type
- 1))
971 /* role may not be associated with type */
975 * User must be authorized for the role.
977 usrdatum
= p
->user_val_to_struct
[c
->user
- 1];
981 if (!ebitmap_get_bit(&usrdatum
->roles
, c
->role
- 1))
982 /* user may not be associated with role */
986 if (!mls_context_isvalid(p
, c
))
993 * Read a MLS range structure from a policydb binary
994 * representation file.
996 static int mls_read_range_helper(struct mls_range
*r
, void *fp
)
1002 rc
= next_entry(buf
, fp
, sizeof(u32
));
1007 items
= le32_to_cpu(buf
[0]);
1008 if (items
> ARRAY_SIZE(buf
)) {
1009 printk(KERN_ERR
"SELinux: mls: range overflow\n");
1013 rc
= next_entry(buf
, fp
, sizeof(u32
) * items
);
1015 printk(KERN_ERR
"SELinux: mls: truncated range\n");
1019 r
->level
[0].sens
= le32_to_cpu(buf
[0]);
1021 r
->level
[1].sens
= le32_to_cpu(buf
[1]);
1023 r
->level
[1].sens
= r
->level
[0].sens
;
1025 rc
= ebitmap_read(&r
->level
[0].cat
, fp
);
1027 printk(KERN_ERR
"SELinux: mls: error reading low categories\n");
1031 rc
= ebitmap_read(&r
->level
[1].cat
, fp
);
1033 printk(KERN_ERR
"SELinux: mls: error reading high categories\n");
1037 rc
= ebitmap_cpy(&r
->level
[1].cat
, &r
->level
[0].cat
);
1039 printk(KERN_ERR
"SELinux: mls: out of memory\n");
1046 ebitmap_destroy(&r
->level
[0].cat
);
1052 * Read and validate a security context structure
1053 * from a policydb binary representation file.
1055 static int context_read_and_validate(struct context
*c
,
1062 rc
= next_entry(buf
, fp
, sizeof buf
);
1064 printk(KERN_ERR
"SELinux: context truncated\n");
1067 c
->user
= le32_to_cpu(buf
[0]);
1068 c
->role
= le32_to_cpu(buf
[1]);
1069 c
->type
= le32_to_cpu(buf
[2]);
1070 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1071 rc
= mls_read_range_helper(&c
->range
, fp
);
1073 printk(KERN_ERR
"SELinux: error reading MLS range of context\n");
1079 if (!policydb_context_isvalid(p
, c
)) {
1080 printk(KERN_ERR
"SELinux: invalid security context\n");
1090 * The following *_read functions are used to
1091 * read the symbol data from a policy database
1092 * binary representation file.
1095 static int str_read(char **strp
, gfp_t flags
, void *fp
, u32 len
)
1100 if ((len
== 0) || (len
== (u32
)-1))
1103 str
= kmalloc(len
+ 1, flags
| __GFP_NOWARN
);
1107 /* it's expected the caller should free the str */
1110 rc
= next_entry(str
, fp
, len
);
1118 static int perm_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1121 struct perm_datum
*perdatum
;
1126 perdatum
= kzalloc(sizeof(*perdatum
), GFP_KERNEL
);
1130 rc
= next_entry(buf
, fp
, sizeof buf
);
1134 len
= le32_to_cpu(buf
[0]);
1135 perdatum
->value
= le32_to_cpu(buf
[1]);
1137 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1141 rc
= hashtab_insert(h
, key
, perdatum
);
1147 perm_destroy(key
, perdatum
, NULL
);
1151 static int common_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1154 struct common_datum
*comdatum
;
1159 comdatum
= kzalloc(sizeof(*comdatum
), GFP_KERNEL
);
1163 rc
= next_entry(buf
, fp
, sizeof buf
);
1167 len
= le32_to_cpu(buf
[0]);
1168 comdatum
->value
= le32_to_cpu(buf
[1]);
1170 rc
= symtab_init(&comdatum
->permissions
, PERM_SYMTAB_SIZE
);
1173 comdatum
->permissions
.nprim
= le32_to_cpu(buf
[2]);
1174 nel
= le32_to_cpu(buf
[3]);
1176 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1180 for (i
= 0; i
< nel
; i
++) {
1181 rc
= perm_read(p
, comdatum
->permissions
.table
, fp
);
1186 rc
= hashtab_insert(h
, key
, comdatum
);
1191 common_destroy(key
, comdatum
, NULL
);
1195 static void type_set_init(struct type_set
*t
)
1197 ebitmap_init(&t
->types
);
1198 ebitmap_init(&t
->negset
);
1201 static int type_set_read(struct type_set
*t
, void *fp
)
1206 if (ebitmap_read(&t
->types
, fp
))
1208 if (ebitmap_read(&t
->negset
, fp
))
1211 rc
= next_entry(buf
, fp
, sizeof(u32
));
1214 t
->flags
= le32_to_cpu(buf
[0]);
1220 static int read_cons_helper(struct policydb
*p
,
1221 struct constraint_node
**nodep
,
1222 int ncons
, int allowxtarget
, void *fp
)
1224 struct constraint_node
*c
, *lc
;
1225 struct constraint_expr
*e
, *le
;
1228 int rc
, i
, j
, depth
;
1231 for (i
= 0; i
< ncons
; i
++) {
1232 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1241 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1244 c
->permissions
= le32_to_cpu(buf
[0]);
1245 nexpr
= le32_to_cpu(buf
[1]);
1248 for (j
= 0; j
< nexpr
; j
++) {
1249 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1258 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 3));
1261 e
->expr_type
= le32_to_cpu(buf
[0]);
1262 e
->attr
= le32_to_cpu(buf
[1]);
1263 e
->op
= le32_to_cpu(buf
[2]);
1265 switch (e
->expr_type
) {
1277 if (depth
== (CEXPR_MAXDEPTH
- 1))
1282 if (!allowxtarget
&& (e
->attr
& CEXPR_XTARGET
))
1284 if (depth
== (CEXPR_MAXDEPTH
- 1))
1287 rc
= ebitmap_read(&e
->names
, fp
);
1290 if (p
->policyvers
>=
1291 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
1292 e
->type_names
= kzalloc(sizeof
1297 type_set_init(e
->type_names
);
1298 rc
= type_set_read(e
->type_names
, fp
);
1316 static int class_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1319 struct class_datum
*cladatum
;
1321 u32 len
, len2
, ncons
, nel
;
1324 cladatum
= kzalloc(sizeof(*cladatum
), GFP_KERNEL
);
1328 rc
= next_entry(buf
, fp
, sizeof(u32
)*6);
1332 len
= le32_to_cpu(buf
[0]);
1333 len2
= le32_to_cpu(buf
[1]);
1334 cladatum
->value
= le32_to_cpu(buf
[2]);
1336 rc
= symtab_init(&cladatum
->permissions
, PERM_SYMTAB_SIZE
);
1339 cladatum
->permissions
.nprim
= le32_to_cpu(buf
[3]);
1340 nel
= le32_to_cpu(buf
[4]);
1342 ncons
= le32_to_cpu(buf
[5]);
1344 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1349 rc
= str_read(&cladatum
->comkey
, GFP_KERNEL
, fp
, len2
);
1354 cladatum
->comdatum
= hashtab_search(p
->p_commons
.table
, cladatum
->comkey
);
1355 if (!cladatum
->comdatum
) {
1356 printk(KERN_ERR
"SELinux: unknown common %s\n", cladatum
->comkey
);
1360 for (i
= 0; i
< nel
; i
++) {
1361 rc
= perm_read(p
, cladatum
->permissions
.table
, fp
);
1366 rc
= read_cons_helper(p
, &cladatum
->constraints
, ncons
, 0, fp
);
1370 if (p
->policyvers
>= POLICYDB_VERSION_VALIDATETRANS
) {
1371 /* grab the validatetrans rules */
1372 rc
= next_entry(buf
, fp
, sizeof(u32
));
1375 ncons
= le32_to_cpu(buf
[0]);
1376 rc
= read_cons_helper(p
, &cladatum
->validatetrans
,
1382 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
1383 rc
= next_entry(buf
, fp
, sizeof(u32
) * 3);
1387 cladatum
->default_user
= le32_to_cpu(buf
[0]);
1388 cladatum
->default_role
= le32_to_cpu(buf
[1]);
1389 cladatum
->default_range
= le32_to_cpu(buf
[2]);
1392 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
1393 rc
= next_entry(buf
, fp
, sizeof(u32
) * 1);
1396 cladatum
->default_type
= le32_to_cpu(buf
[0]);
1399 rc
= hashtab_insert(h
, key
, cladatum
);
1405 cls_destroy(key
, cladatum
, NULL
);
1409 static int role_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1412 struct role_datum
*role
;
1413 int rc
, to_read
= 2;
1417 role
= kzalloc(sizeof(*role
), GFP_KERNEL
);
1421 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1424 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1428 len
= le32_to_cpu(buf
[0]);
1429 role
->value
= le32_to_cpu(buf
[1]);
1430 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1431 role
->bounds
= le32_to_cpu(buf
[2]);
1433 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1437 rc
= ebitmap_read(&role
->dominates
, fp
);
1441 rc
= ebitmap_read(&role
->types
, fp
);
1445 if (strcmp(key
, OBJECT_R
) == 0) {
1447 if (role
->value
!= OBJECT_R_VAL
) {
1448 printk(KERN_ERR
"SELinux: Role %s has wrong value %d\n",
1449 OBJECT_R
, role
->value
);
1456 rc
= hashtab_insert(h
, key
, role
);
1461 role_destroy(key
, role
, NULL
);
1465 static int type_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1468 struct type_datum
*typdatum
;
1469 int rc
, to_read
= 3;
1473 typdatum
= kzalloc(sizeof(*typdatum
), GFP_KERNEL
);
1477 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1480 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1484 len
= le32_to_cpu(buf
[0]);
1485 typdatum
->value
= le32_to_cpu(buf
[1]);
1486 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
1487 u32 prop
= le32_to_cpu(buf
[2]);
1489 if (prop
& TYPEDATUM_PROPERTY_PRIMARY
)
1490 typdatum
->primary
= 1;
1491 if (prop
& TYPEDATUM_PROPERTY_ATTRIBUTE
)
1492 typdatum
->attribute
= 1;
1494 typdatum
->bounds
= le32_to_cpu(buf
[3]);
1496 typdatum
->primary
= le32_to_cpu(buf
[2]);
1499 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1503 rc
= hashtab_insert(h
, key
, typdatum
);
1508 type_destroy(key
, typdatum
, NULL
);
1514 * Read a MLS level structure from a policydb binary
1515 * representation file.
1517 static int mls_read_level(struct mls_level
*lp
, void *fp
)
1522 memset(lp
, 0, sizeof(*lp
));
1524 rc
= next_entry(buf
, fp
, sizeof buf
);
1526 printk(KERN_ERR
"SELinux: mls: truncated level\n");
1529 lp
->sens
= le32_to_cpu(buf
[0]);
1531 rc
= ebitmap_read(&lp
->cat
, fp
);
1533 printk(KERN_ERR
"SELinux: mls: error reading level categories\n");
1539 static int user_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1542 struct user_datum
*usrdatum
;
1543 int rc
, to_read
= 2;
1547 usrdatum
= kzalloc(sizeof(*usrdatum
), GFP_KERNEL
);
1551 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1554 rc
= next_entry(buf
, fp
, sizeof(buf
[0]) * to_read
);
1558 len
= le32_to_cpu(buf
[0]);
1559 usrdatum
->value
= le32_to_cpu(buf
[1]);
1560 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
1561 usrdatum
->bounds
= le32_to_cpu(buf
[2]);
1563 rc
= str_read(&key
, GFP_KERNEL
, fp
, len
);
1567 rc
= ebitmap_read(&usrdatum
->roles
, fp
);
1571 if (p
->policyvers
>= POLICYDB_VERSION_MLS
) {
1572 rc
= mls_read_range_helper(&usrdatum
->range
, fp
);
1575 rc
= mls_read_level(&usrdatum
->dfltlevel
, fp
);
1580 rc
= hashtab_insert(h
, key
, usrdatum
);
1585 user_destroy(key
, usrdatum
, NULL
);
1589 static int sens_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1592 struct level_datum
*levdatum
;
1597 levdatum
= kzalloc(sizeof(*levdatum
), GFP_ATOMIC
);
1601 rc
= next_entry(buf
, fp
, sizeof buf
);
1605 len
= le32_to_cpu(buf
[0]);
1606 levdatum
->isalias
= le32_to_cpu(buf
[1]);
1608 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1613 levdatum
->level
= kmalloc(sizeof(*levdatum
->level
), GFP_ATOMIC
);
1614 if (!levdatum
->level
)
1617 rc
= mls_read_level(levdatum
->level
, fp
);
1621 rc
= hashtab_insert(h
, key
, levdatum
);
1626 sens_destroy(key
, levdatum
, NULL
);
1630 static int cat_read(struct policydb
*p
, struct hashtab
*h
, void *fp
)
1633 struct cat_datum
*catdatum
;
1638 catdatum
= kzalloc(sizeof(*catdatum
), GFP_ATOMIC
);
1642 rc
= next_entry(buf
, fp
, sizeof buf
);
1646 len
= le32_to_cpu(buf
[0]);
1647 catdatum
->value
= le32_to_cpu(buf
[1]);
1648 catdatum
->isalias
= le32_to_cpu(buf
[2]);
1650 rc
= str_read(&key
, GFP_ATOMIC
, fp
, len
);
1654 rc
= hashtab_insert(h
, key
, catdatum
);
1659 cat_destroy(key
, catdatum
, NULL
);
1663 static int (*read_f
[SYM_NUM
]) (struct policydb
*p
, struct hashtab
*h
, void *fp
) =
1675 static int user_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1677 struct user_datum
*upper
, *user
;
1678 struct policydb
*p
= datap
;
1681 upper
= user
= datum
;
1682 while (upper
->bounds
) {
1683 struct ebitmap_node
*node
;
1686 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1687 printk(KERN_ERR
"SELinux: user %s: "
1688 "too deep or looped boundary",
1693 upper
= p
->user_val_to_struct
[upper
->bounds
- 1];
1694 ebitmap_for_each_positive_bit(&user
->roles
, node
, bit
) {
1695 if (ebitmap_get_bit(&upper
->roles
, bit
))
1699 "SELinux: boundary violated policy: "
1700 "user=%s role=%s bounds=%s\n",
1701 sym_name(p
, SYM_USERS
, user
->value
- 1),
1702 sym_name(p
, SYM_ROLES
, bit
),
1703 sym_name(p
, SYM_USERS
, upper
->value
- 1));
1712 static int role_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1714 struct role_datum
*upper
, *role
;
1715 struct policydb
*p
= datap
;
1718 upper
= role
= datum
;
1719 while (upper
->bounds
) {
1720 struct ebitmap_node
*node
;
1723 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1724 printk(KERN_ERR
"SELinux: role %s: "
1725 "too deep or looped bounds\n",
1730 upper
= p
->role_val_to_struct
[upper
->bounds
- 1];
1731 ebitmap_for_each_positive_bit(&role
->types
, node
, bit
) {
1732 if (ebitmap_get_bit(&upper
->types
, bit
))
1736 "SELinux: boundary violated policy: "
1737 "role=%s type=%s bounds=%s\n",
1738 sym_name(p
, SYM_ROLES
, role
->value
- 1),
1739 sym_name(p
, SYM_TYPES
, bit
),
1740 sym_name(p
, SYM_ROLES
, upper
->value
- 1));
1749 static int type_bounds_sanity_check(void *key
, void *datum
, void *datap
)
1751 struct type_datum
*upper
;
1752 struct policydb
*p
= datap
;
1756 while (upper
->bounds
) {
1757 if (++depth
== POLICYDB_BOUNDS_MAXDEPTH
) {
1758 printk(KERN_ERR
"SELinux: type %s: "
1759 "too deep or looped boundary\n",
1764 upper
= flex_array_get_ptr(p
->type_val_to_struct_array
,
1768 if (upper
->attribute
) {
1769 printk(KERN_ERR
"SELinux: type %s: "
1770 "bounded by attribute %s",
1772 sym_name(p
, SYM_TYPES
, upper
->value
- 1));
1780 static int policydb_bounds_sanity_check(struct policydb
*p
)
1784 if (p
->policyvers
< POLICYDB_VERSION_BOUNDARY
)
1787 rc
= hashtab_map(p
->p_users
.table
,
1788 user_bounds_sanity_check
, p
);
1792 rc
= hashtab_map(p
->p_roles
.table
,
1793 role_bounds_sanity_check
, p
);
1797 rc
= hashtab_map(p
->p_types
.table
,
1798 type_bounds_sanity_check
, p
);
1805 u16
string_to_security_class(struct policydb
*p
, const char *name
)
1807 struct class_datum
*cladatum
;
1809 cladatum
= hashtab_search(p
->p_classes
.table
, name
);
1813 return cladatum
->value
;
1816 u32
string_to_av_perm(struct policydb
*p
, u16 tclass
, const char *name
)
1818 struct class_datum
*cladatum
;
1819 struct perm_datum
*perdatum
= NULL
;
1820 struct common_datum
*comdatum
;
1822 if (!tclass
|| tclass
> p
->p_classes
.nprim
)
1825 cladatum
= p
->class_val_to_struct
[tclass
-1];
1826 comdatum
= cladatum
->comdatum
;
1828 perdatum
= hashtab_search(comdatum
->permissions
.table
,
1831 perdatum
= hashtab_search(cladatum
->permissions
.table
,
1836 return 1U << (perdatum
->value
-1);
1839 static int range_read(struct policydb
*p
, void *fp
)
1841 struct range_trans
*rt
= NULL
;
1842 struct mls_range
*r
= NULL
;
1847 if (p
->policyvers
< POLICYDB_VERSION_MLS
)
1850 rc
= next_entry(buf
, fp
, sizeof(u32
));
1854 nel
= le32_to_cpu(buf
[0]);
1855 for (i
= 0; i
< nel
; i
++) {
1857 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
1861 rc
= next_entry(buf
, fp
, (sizeof(u32
) * 2));
1865 rt
->source_type
= le32_to_cpu(buf
[0]);
1866 rt
->target_type
= le32_to_cpu(buf
[1]);
1867 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
1868 rc
= next_entry(buf
, fp
, sizeof(u32
));
1871 rt
->target_class
= le32_to_cpu(buf
[0]);
1873 rt
->target_class
= p
->process_class
;
1876 if (!policydb_type_isvalid(p
, rt
->source_type
) ||
1877 !policydb_type_isvalid(p
, rt
->target_type
) ||
1878 !policydb_class_isvalid(p
, rt
->target_class
))
1882 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
1886 rc
= mls_read_range_helper(r
, fp
);
1891 if (!mls_range_isvalid(p
, r
)) {
1892 printk(KERN_WARNING
"SELinux: rangetrans: invalid range\n");
1896 rc
= hashtab_insert(p
->range_tr
, rt
, r
);
1903 hash_eval(p
->range_tr
, "rangetr");
1911 static int filename_trans_read(struct policydb
*p
, void *fp
)
1913 struct filename_trans
*ft
;
1914 struct filename_trans_datum
*otype
;
1920 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
1923 rc
= next_entry(buf
, fp
, sizeof(u32
));
1926 nel
= le32_to_cpu(buf
[0]);
1928 for (i
= 0; i
< nel
; i
++) {
1933 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
1938 otype
= kmalloc(sizeof(*otype
), GFP_KERNEL
);
1942 /* length of the path component string */
1943 rc
= next_entry(buf
, fp
, sizeof(u32
));
1946 len
= le32_to_cpu(buf
[0]);
1948 /* path component string */
1949 rc
= str_read(&name
, GFP_KERNEL
, fp
, len
);
1955 rc
= next_entry(buf
, fp
, sizeof(u32
) * 4);
1959 ft
->stype
= le32_to_cpu(buf
[0]);
1960 ft
->ttype
= le32_to_cpu(buf
[1]);
1961 ft
->tclass
= le32_to_cpu(buf
[2]);
1963 otype
->otype
= le32_to_cpu(buf
[3]);
1965 rc
= ebitmap_set_bit(&p
->filename_trans_ttypes
, ft
->ttype
, 1);
1969 rc
= hashtab_insert(p
->filename_trans
, ft
, otype
);
1972 * Do not return -EEXIST to the caller, or the system
1977 /* But free memory to avoid memory leak. */
1983 hash_eval(p
->filename_trans
, "filenametr");
1993 static int genfs_read(struct policydb
*p
, void *fp
)
1996 u32 nel
, nel2
, len
, len2
;
1998 struct ocontext
*l
, *c
;
1999 struct ocontext
*newc
= NULL
;
2000 struct genfs
*genfs_p
, *genfs
;
2001 struct genfs
*newgenfs
= NULL
;
2003 rc
= next_entry(buf
, fp
, sizeof(u32
));
2006 nel
= le32_to_cpu(buf
[0]);
2008 for (i
= 0; i
< nel
; i
++) {
2009 rc
= next_entry(buf
, fp
, sizeof(u32
));
2012 len
= le32_to_cpu(buf
[0]);
2015 newgenfs
= kzalloc(sizeof(*newgenfs
), GFP_KERNEL
);
2019 rc
= str_read(&newgenfs
->fstype
, GFP_KERNEL
, fp
, len
);
2023 for (genfs_p
= NULL
, genfs
= p
->genfs
; genfs
;
2024 genfs_p
= genfs
, genfs
= genfs
->next
) {
2026 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) == 0) {
2027 printk(KERN_ERR
"SELinux: dup genfs fstype %s\n",
2031 if (strcmp(newgenfs
->fstype
, genfs
->fstype
) < 0)
2034 newgenfs
->next
= genfs
;
2036 genfs_p
->next
= newgenfs
;
2038 p
->genfs
= newgenfs
;
2042 rc
= next_entry(buf
, fp
, sizeof(u32
));
2046 nel2
= le32_to_cpu(buf
[0]);
2047 for (j
= 0; j
< nel2
; j
++) {
2048 rc
= next_entry(buf
, fp
, sizeof(u32
));
2051 len
= le32_to_cpu(buf
[0]);
2054 newc
= kzalloc(sizeof(*newc
), GFP_KERNEL
);
2058 rc
= str_read(&newc
->u
.name
, GFP_KERNEL
, fp
, len
);
2062 rc
= next_entry(buf
, fp
, sizeof(u32
));
2066 newc
->v
.sclass
= le32_to_cpu(buf
[0]);
2067 rc
= context_read_and_validate(&newc
->context
[0], p
, fp
);
2071 for (l
= NULL
, c
= genfs
->head
; c
;
2072 l
= c
, c
= c
->next
) {
2074 if (!strcmp(newc
->u
.name
, c
->u
.name
) &&
2075 (!c
->v
.sclass
|| !newc
->v
.sclass
||
2076 newc
->v
.sclass
== c
->v
.sclass
)) {
2077 printk(KERN_ERR
"SELinux: dup genfs entry (%s,%s)\n",
2078 genfs
->fstype
, c
->u
.name
);
2081 len
= strlen(newc
->u
.name
);
2082 len2
= strlen(c
->u
.name
);
2098 kfree(newgenfs
->fstype
);
2101 ocontext_destroy(newc
, OCON_FSUSE
);
2106 static int ocontext_read(struct policydb
*p
, struct policydb_compat_info
*info
,
2111 __be64 prefixbuf
[1];
2113 struct ocontext
*l
, *c
;
2116 for (i
= 0; i
< info
->ocon_num
; i
++) {
2117 rc
= next_entry(buf
, fp
, sizeof(u32
));
2120 nel
= le32_to_cpu(buf
[0]);
2123 for (j
= 0; j
< nel
; j
++) {
2125 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
2131 p
->ocontexts
[i
] = c
;
2136 rc
= next_entry(buf
, fp
, sizeof(u32
));
2140 c
->sid
[0] = le32_to_cpu(buf
[0]);
2141 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2147 rc
= next_entry(buf
, fp
, sizeof(u32
));
2150 len
= le32_to_cpu(buf
[0]);
2152 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2156 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2159 rc
= context_read_and_validate(&c
->context
[1], p
, fp
);
2164 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2167 c
->u
.port
.protocol
= le32_to_cpu(buf
[0]);
2168 c
->u
.port
.low_port
= le32_to_cpu(buf
[1]);
2169 c
->u
.port
.high_port
= le32_to_cpu(buf
[2]);
2170 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2175 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 2);
2178 c
->u
.node
.addr
= nodebuf
[0]; /* network order */
2179 c
->u
.node
.mask
= nodebuf
[1]; /* network order */
2180 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2185 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2190 c
->v
.behavior
= le32_to_cpu(buf
[0]);
2191 /* Determined at runtime, not in policy DB. */
2192 if (c
->v
.behavior
== SECURITY_FS_USE_MNTPOINT
)
2194 if (c
->v
.behavior
> SECURITY_FS_USE_MAX
)
2197 len
= le32_to_cpu(buf
[1]);
2198 rc
= str_read(&c
->u
.name
, GFP_KERNEL
, fp
, len
);
2202 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2209 rc
= next_entry(nodebuf
, fp
, sizeof(u32
) * 8);
2212 for (k
= 0; k
< 4; k
++)
2213 c
->u
.node6
.addr
[k
] = nodebuf
[k
];
2214 for (k
= 0; k
< 4; k
++)
2215 c
->u
.node6
.mask
[k
] = nodebuf
[k
+4];
2216 rc
= context_read_and_validate(&c
->context
[0], p
, fp
);
2222 u32 pkey_lo
, pkey_hi
;
2224 rc
= next_entry(prefixbuf
, fp
, sizeof(u64
));
2228 /* we need to have subnet_prefix in CPU order */
2229 c
->u
.ibpkey
.subnet_prefix
= be64_to_cpu(prefixbuf
[0]);
2231 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2235 pkey_lo
= le32_to_cpu(buf
[0]);
2236 pkey_hi
= le32_to_cpu(buf
[1]);
2238 if (pkey_lo
> U16_MAX
|| pkey_hi
> U16_MAX
) {
2243 c
->u
.ibpkey
.low_pkey
= pkey_lo
;
2244 c
->u
.ibpkey
.high_pkey
= pkey_hi
;
2246 rc
= context_read_and_validate(&c
->context
[0],
2253 case OCON_IBENDPORT
: {
2256 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2259 len
= le32_to_cpu(buf
[0]);
2261 rc
= str_read(&c
->u
.ibendport
.dev_name
, GFP_KERNEL
, fp
, len
);
2265 port
= le32_to_cpu(buf
[1]);
2266 if (port
> U8_MAX
|| port
== 0) {
2271 c
->u
.ibendport
.port
= port
;
2273 rc
= context_read_and_validate(&c
->context
[0],
2289 * Read the configuration data from a policy database binary
2290 * representation file into a policy database structure.
2292 int policydb_read(struct policydb
*p
, void *fp
)
2294 struct role_allow
*ra
, *lra
;
2295 struct role_trans
*tr
, *ltr
;
2298 u32 len
, nprim
, nel
;
2301 struct policydb_compat_info
*info
;
2303 rc
= policydb_init(p
);
2307 /* Read the magic number and string length. */
2308 rc
= next_entry(buf
, fp
, sizeof(u32
) * 2);
2313 if (le32_to_cpu(buf
[0]) != POLICYDB_MAGIC
) {
2314 printk(KERN_ERR
"SELinux: policydb magic number 0x%x does "
2315 "not match expected magic number 0x%x\n",
2316 le32_to_cpu(buf
[0]), POLICYDB_MAGIC
);
2321 len
= le32_to_cpu(buf
[1]);
2322 if (len
!= strlen(POLICYDB_STRING
)) {
2323 printk(KERN_ERR
"SELinux: policydb string length %d does not "
2324 "match expected length %zu\n",
2325 len
, strlen(POLICYDB_STRING
));
2330 policydb_str
= kmalloc(len
+ 1, GFP_KERNEL
);
2331 if (!policydb_str
) {
2332 printk(KERN_ERR
"SELinux: unable to allocate memory for policydb "
2333 "string of length %d\n", len
);
2337 rc
= next_entry(policydb_str
, fp
, len
);
2339 printk(KERN_ERR
"SELinux: truncated policydb string identifier\n");
2340 kfree(policydb_str
);
2345 policydb_str
[len
] = '\0';
2346 if (strcmp(policydb_str
, POLICYDB_STRING
)) {
2347 printk(KERN_ERR
"SELinux: policydb string %s does not match "
2348 "my string %s\n", policydb_str
, POLICYDB_STRING
);
2349 kfree(policydb_str
);
2352 /* Done with policydb_str. */
2353 kfree(policydb_str
);
2354 policydb_str
= NULL
;
2356 /* Read the version and table sizes. */
2357 rc
= next_entry(buf
, fp
, sizeof(u32
)*4);
2362 p
->policyvers
= le32_to_cpu(buf
[0]);
2363 if (p
->policyvers
< POLICYDB_VERSION_MIN
||
2364 p
->policyvers
> POLICYDB_VERSION_MAX
) {
2365 printk(KERN_ERR
"SELinux: policydb version %d does not match "
2366 "my version range %d-%d\n",
2367 le32_to_cpu(buf
[0]), POLICYDB_VERSION_MIN
, POLICYDB_VERSION_MAX
);
2371 if ((le32_to_cpu(buf
[1]) & POLICYDB_CONFIG_MLS
)) {
2375 if (p
->policyvers
< POLICYDB_VERSION_MLS
) {
2376 printk(KERN_ERR
"SELinux: security policydb version %d "
2377 "(MLS) not backwards compatible\n",
2382 p
->reject_unknown
= !!(le32_to_cpu(buf
[1]) & REJECT_UNKNOWN
);
2383 p
->allow_unknown
= !!(le32_to_cpu(buf
[1]) & ALLOW_UNKNOWN
);
2385 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
2386 rc
= ebitmap_read(&p
->policycaps
, fp
);
2391 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
2392 rc
= ebitmap_read(&p
->permissive_map
, fp
);
2398 info
= policydb_lookup_compat(p
->policyvers
);
2400 printk(KERN_ERR
"SELinux: unable to find policy compat info "
2401 "for version %d\n", p
->policyvers
);
2406 if (le32_to_cpu(buf
[2]) != info
->sym_num
||
2407 le32_to_cpu(buf
[3]) != info
->ocon_num
) {
2408 printk(KERN_ERR
"SELinux: policydb table sizes (%d,%d) do "
2409 "not match mine (%d,%d)\n", le32_to_cpu(buf
[2]),
2410 le32_to_cpu(buf
[3]),
2411 info
->sym_num
, info
->ocon_num
);
2415 for (i
= 0; i
< info
->sym_num
; i
++) {
2416 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2419 nprim
= le32_to_cpu(buf
[0]);
2420 nel
= le32_to_cpu(buf
[1]);
2421 for (j
= 0; j
< nel
; j
++) {
2422 rc
= read_f
[i
](p
, p
->symtab
[i
].table
, fp
);
2427 p
->symtab
[i
].nprim
= nprim
;
2431 p
->process_class
= string_to_security_class(p
, "process");
2432 if (!p
->process_class
)
2435 rc
= avtab_read(&p
->te_avtab
, fp
, p
);
2439 if (p
->policyvers
>= POLICYDB_VERSION_BOOL
) {
2440 rc
= cond_read_list(p
, fp
);
2445 rc
= next_entry(buf
, fp
, sizeof(u32
));
2448 nel
= le32_to_cpu(buf
[0]);
2450 for (i
= 0; i
< nel
; i
++) {
2452 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
2459 rc
= next_entry(buf
, fp
, sizeof(u32
)*3);
2464 tr
->role
= le32_to_cpu(buf
[0]);
2465 tr
->type
= le32_to_cpu(buf
[1]);
2466 tr
->new_role
= le32_to_cpu(buf
[2]);
2467 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2468 rc
= next_entry(buf
, fp
, sizeof(u32
));
2471 tr
->tclass
= le32_to_cpu(buf
[0]);
2473 tr
->tclass
= p
->process_class
;
2476 if (!policydb_role_isvalid(p
, tr
->role
) ||
2477 !policydb_type_isvalid(p
, tr
->type
) ||
2478 !policydb_class_isvalid(p
, tr
->tclass
) ||
2479 !policydb_role_isvalid(p
, tr
->new_role
))
2484 rc
= next_entry(buf
, fp
, sizeof(u32
));
2487 nel
= le32_to_cpu(buf
[0]);
2489 for (i
= 0; i
< nel
; i
++) {
2491 ra
= kzalloc(sizeof(*ra
), GFP_KERNEL
);
2498 rc
= next_entry(buf
, fp
, sizeof(u32
)*2);
2503 ra
->role
= le32_to_cpu(buf
[0]);
2504 ra
->new_role
= le32_to_cpu(buf
[1]);
2505 if (!policydb_role_isvalid(p
, ra
->role
) ||
2506 !policydb_role_isvalid(p
, ra
->new_role
))
2511 rc
= filename_trans_read(p
, fp
);
2515 rc
= policydb_index(p
);
2520 p
->process_trans_perms
= string_to_av_perm(p
, p
->process_class
, "transition");
2521 p
->process_trans_perms
|= string_to_av_perm(p
, p
->process_class
, "dyntransition");
2522 if (!p
->process_trans_perms
)
2525 rc
= ocontext_read(p
, info
, fp
);
2529 rc
= genfs_read(p
, fp
);
2533 rc
= range_read(p
, fp
);
2538 p
->type_attr_map_array
= flex_array_alloc(sizeof(struct ebitmap
),
2540 GFP_KERNEL
| __GFP_ZERO
);
2541 if (!p
->type_attr_map_array
)
2544 /* preallocate so we don't have to worry about the put ever failing */
2545 rc
= flex_array_prealloc(p
->type_attr_map_array
, 0, p
->p_types
.nprim
,
2546 GFP_KERNEL
| __GFP_ZERO
);
2550 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
2551 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
2555 if (p
->policyvers
>= POLICYDB_VERSION_AVTAB
) {
2556 rc
= ebitmap_read(e
, fp
);
2560 /* add the type itself as the degenerate case */
2561 rc
= ebitmap_set_bit(e
, i
, 1);
2566 rc
= policydb_bounds_sanity_check(p
);
2574 policydb_destroy(p
);
2579 * Write a MLS level structure to a policydb binary
2580 * representation file.
2582 static int mls_write_level(struct mls_level
*l
, void *fp
)
2587 buf
[0] = cpu_to_le32(l
->sens
);
2588 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2592 rc
= ebitmap_write(&l
->cat
, fp
);
2600 * Write a MLS range structure to a policydb binary
2601 * representation file.
2603 static int mls_write_range_helper(struct mls_range
*r
, void *fp
)
2609 eq
= mls_level_eq(&r
->level
[1], &r
->level
[0]);
2615 buf
[0] = cpu_to_le32(items
-1);
2616 buf
[1] = cpu_to_le32(r
->level
[0].sens
);
2618 buf
[2] = cpu_to_le32(r
->level
[1].sens
);
2620 BUG_ON(items
> ARRAY_SIZE(buf
));
2622 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
2626 rc
= ebitmap_write(&r
->level
[0].cat
, fp
);
2630 rc
= ebitmap_write(&r
->level
[1].cat
, fp
);
2638 static int sens_write(void *vkey
, void *datum
, void *ptr
)
2641 struct level_datum
*levdatum
= datum
;
2642 struct policy_data
*pd
= ptr
;
2649 buf
[0] = cpu_to_le32(len
);
2650 buf
[1] = cpu_to_le32(levdatum
->isalias
);
2651 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2655 rc
= put_entry(key
, 1, len
, fp
);
2659 rc
= mls_write_level(levdatum
->level
, fp
);
2666 static int cat_write(void *vkey
, void *datum
, void *ptr
)
2669 struct cat_datum
*catdatum
= datum
;
2670 struct policy_data
*pd
= ptr
;
2677 buf
[0] = cpu_to_le32(len
);
2678 buf
[1] = cpu_to_le32(catdatum
->value
);
2679 buf
[2] = cpu_to_le32(catdatum
->isalias
);
2680 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2684 rc
= put_entry(key
, 1, len
, fp
);
2691 static int role_trans_write(struct policydb
*p
, void *fp
)
2693 struct role_trans
*r
= p
->role_tr
;
2694 struct role_trans
*tr
;
2700 for (tr
= r
; tr
; tr
= tr
->next
)
2702 buf
[0] = cpu_to_le32(nel
);
2703 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2706 for (tr
= r
; tr
; tr
= tr
->next
) {
2707 buf
[0] = cpu_to_le32(tr
->role
);
2708 buf
[1] = cpu_to_le32(tr
->type
);
2709 buf
[2] = cpu_to_le32(tr
->new_role
);
2710 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2713 if (p
->policyvers
>= POLICYDB_VERSION_ROLETRANS
) {
2714 buf
[0] = cpu_to_le32(tr
->tclass
);
2715 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2724 static int role_allow_write(struct role_allow
*r
, void *fp
)
2726 struct role_allow
*ra
;
2732 for (ra
= r
; ra
; ra
= ra
->next
)
2734 buf
[0] = cpu_to_le32(nel
);
2735 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2738 for (ra
= r
; ra
; ra
= ra
->next
) {
2739 buf
[0] = cpu_to_le32(ra
->role
);
2740 buf
[1] = cpu_to_le32(ra
->new_role
);
2741 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2749 * Write a security context structure
2750 * to a policydb binary representation file.
2752 static int context_write(struct policydb
*p
, struct context
*c
,
2758 buf
[0] = cpu_to_le32(c
->user
);
2759 buf
[1] = cpu_to_le32(c
->role
);
2760 buf
[2] = cpu_to_le32(c
->type
);
2762 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2766 rc
= mls_write_range_helper(&c
->range
, fp
);
2774 * The following *_write functions are used to
2775 * write the symbol data to a policy database
2776 * binary representation file.
2779 static int perm_write(void *vkey
, void *datum
, void *fp
)
2782 struct perm_datum
*perdatum
= datum
;
2788 buf
[0] = cpu_to_le32(len
);
2789 buf
[1] = cpu_to_le32(perdatum
->value
);
2790 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2794 rc
= put_entry(key
, 1, len
, fp
);
2801 static int common_write(void *vkey
, void *datum
, void *ptr
)
2804 struct common_datum
*comdatum
= datum
;
2805 struct policy_data
*pd
= ptr
;
2812 buf
[0] = cpu_to_le32(len
);
2813 buf
[1] = cpu_to_le32(comdatum
->value
);
2814 buf
[2] = cpu_to_le32(comdatum
->permissions
.nprim
);
2815 buf
[3] = cpu_to_le32(comdatum
->permissions
.table
->nel
);
2816 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
2820 rc
= put_entry(key
, 1, len
, fp
);
2824 rc
= hashtab_map(comdatum
->permissions
.table
, perm_write
, fp
);
2831 static int type_set_write(struct type_set
*t
, void *fp
)
2836 if (ebitmap_write(&t
->types
, fp
))
2838 if (ebitmap_write(&t
->negset
, fp
))
2841 buf
[0] = cpu_to_le32(t
->flags
);
2842 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2849 static int write_cons_helper(struct policydb
*p
, struct constraint_node
*node
,
2852 struct constraint_node
*c
;
2853 struct constraint_expr
*e
;
2858 for (c
= node
; c
; c
= c
->next
) {
2860 for (e
= c
->expr
; e
; e
= e
->next
)
2862 buf
[0] = cpu_to_le32(c
->permissions
);
2863 buf
[1] = cpu_to_le32(nel
);
2864 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
2867 for (e
= c
->expr
; e
; e
= e
->next
) {
2868 buf
[0] = cpu_to_le32(e
->expr_type
);
2869 buf
[1] = cpu_to_le32(e
->attr
);
2870 buf
[2] = cpu_to_le32(e
->op
);
2871 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
2875 switch (e
->expr_type
) {
2877 rc
= ebitmap_write(&e
->names
, fp
);
2880 if (p
->policyvers
>=
2881 POLICYDB_VERSION_CONSTRAINT_NAMES
) {
2882 rc
= type_set_write(e
->type_names
, fp
);
2896 static int class_write(void *vkey
, void *datum
, void *ptr
)
2899 struct class_datum
*cladatum
= datum
;
2900 struct policy_data
*pd
= ptr
;
2902 struct policydb
*p
= pd
->p
;
2903 struct constraint_node
*c
;
2910 if (cladatum
->comkey
)
2911 len2
= strlen(cladatum
->comkey
);
2916 for (c
= cladatum
->constraints
; c
; c
= c
->next
)
2919 buf
[0] = cpu_to_le32(len
);
2920 buf
[1] = cpu_to_le32(len2
);
2921 buf
[2] = cpu_to_le32(cladatum
->value
);
2922 buf
[3] = cpu_to_le32(cladatum
->permissions
.nprim
);
2923 if (cladatum
->permissions
.table
)
2924 buf
[4] = cpu_to_le32(cladatum
->permissions
.table
->nel
);
2927 buf
[5] = cpu_to_le32(ncons
);
2928 rc
= put_entry(buf
, sizeof(u32
), 6, fp
);
2932 rc
= put_entry(key
, 1, len
, fp
);
2936 if (cladatum
->comkey
) {
2937 rc
= put_entry(cladatum
->comkey
, 1, len2
, fp
);
2942 rc
= hashtab_map(cladatum
->permissions
.table
, perm_write
, fp
);
2946 rc
= write_cons_helper(p
, cladatum
->constraints
, fp
);
2950 /* write out the validatetrans rule */
2952 for (c
= cladatum
->validatetrans
; c
; c
= c
->next
)
2955 buf
[0] = cpu_to_le32(ncons
);
2956 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
2960 rc
= write_cons_helper(p
, cladatum
->validatetrans
, fp
);
2964 if (p
->policyvers
>= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS
) {
2965 buf
[0] = cpu_to_le32(cladatum
->default_user
);
2966 buf
[1] = cpu_to_le32(cladatum
->default_role
);
2967 buf
[2] = cpu_to_le32(cladatum
->default_range
);
2969 rc
= put_entry(buf
, sizeof(uint32_t), 3, fp
);
2974 if (p
->policyvers
>= POLICYDB_VERSION_DEFAULT_TYPE
) {
2975 buf
[0] = cpu_to_le32(cladatum
->default_type
);
2976 rc
= put_entry(buf
, sizeof(uint32_t), 1, fp
);
2984 static int role_write(void *vkey
, void *datum
, void *ptr
)
2987 struct role_datum
*role
= datum
;
2988 struct policy_data
*pd
= ptr
;
2990 struct policydb
*p
= pd
->p
;
2997 buf
[items
++] = cpu_to_le32(len
);
2998 buf
[items
++] = cpu_to_le32(role
->value
);
2999 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3000 buf
[items
++] = cpu_to_le32(role
->bounds
);
3002 BUG_ON(items
> ARRAY_SIZE(buf
));
3004 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3008 rc
= put_entry(key
, 1, len
, fp
);
3012 rc
= ebitmap_write(&role
->dominates
, fp
);
3016 rc
= ebitmap_write(&role
->types
, fp
);
3023 static int type_write(void *vkey
, void *datum
, void *ptr
)
3026 struct type_datum
*typdatum
= datum
;
3027 struct policy_data
*pd
= ptr
;
3028 struct policydb
*p
= pd
->p
;
3036 buf
[items
++] = cpu_to_le32(len
);
3037 buf
[items
++] = cpu_to_le32(typdatum
->value
);
3038 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
) {
3041 if (typdatum
->primary
)
3042 properties
|= TYPEDATUM_PROPERTY_PRIMARY
;
3044 if (typdatum
->attribute
)
3045 properties
|= TYPEDATUM_PROPERTY_ATTRIBUTE
;
3047 buf
[items
++] = cpu_to_le32(properties
);
3048 buf
[items
++] = cpu_to_le32(typdatum
->bounds
);
3050 buf
[items
++] = cpu_to_le32(typdatum
->primary
);
3052 BUG_ON(items
> ARRAY_SIZE(buf
));
3053 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3057 rc
= put_entry(key
, 1, len
, fp
);
3064 static int user_write(void *vkey
, void *datum
, void *ptr
)
3067 struct user_datum
*usrdatum
= datum
;
3068 struct policy_data
*pd
= ptr
;
3069 struct policydb
*p
= pd
->p
;
3077 buf
[items
++] = cpu_to_le32(len
);
3078 buf
[items
++] = cpu_to_le32(usrdatum
->value
);
3079 if (p
->policyvers
>= POLICYDB_VERSION_BOUNDARY
)
3080 buf
[items
++] = cpu_to_le32(usrdatum
->bounds
);
3081 BUG_ON(items
> ARRAY_SIZE(buf
));
3082 rc
= put_entry(buf
, sizeof(u32
), items
, fp
);
3086 rc
= put_entry(key
, 1, len
, fp
);
3090 rc
= ebitmap_write(&usrdatum
->roles
, fp
);
3094 rc
= mls_write_range_helper(&usrdatum
->range
, fp
);
3098 rc
= mls_write_level(&usrdatum
->dfltlevel
, fp
);
3105 static int (*write_f
[SYM_NUM
]) (void *key
, void *datum
,
3118 static int ocontext_write(struct policydb
*p
, struct policydb_compat_info
*info
,
3121 unsigned int i
, j
, rc
;
3123 __be64 prefixbuf
[1];
3127 for (i
= 0; i
< info
->ocon_num
; i
++) {
3129 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
)
3131 buf
[0] = cpu_to_le32(nel
);
3132 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3135 for (c
= p
->ocontexts
[i
]; c
; c
= c
->next
) {
3138 buf
[0] = cpu_to_le32(c
->sid
[0]);
3139 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3142 rc
= context_write(p
, &c
->context
[0], fp
);
3148 len
= strlen(c
->u
.name
);
3149 buf
[0] = cpu_to_le32(len
);
3150 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3153 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3156 rc
= context_write(p
, &c
->context
[0], fp
);
3159 rc
= context_write(p
, &c
->context
[1], fp
);
3164 buf
[0] = cpu_to_le32(c
->u
.port
.protocol
);
3165 buf
[1] = cpu_to_le32(c
->u
.port
.low_port
);
3166 buf
[2] = cpu_to_le32(c
->u
.port
.high_port
);
3167 rc
= put_entry(buf
, sizeof(u32
), 3, fp
);
3170 rc
= context_write(p
, &c
->context
[0], fp
);
3175 nodebuf
[0] = c
->u
.node
.addr
; /* network order */
3176 nodebuf
[1] = c
->u
.node
.mask
; /* network order */
3177 rc
= put_entry(nodebuf
, sizeof(u32
), 2, fp
);
3180 rc
= context_write(p
, &c
->context
[0], fp
);
3185 buf
[0] = cpu_to_le32(c
->v
.behavior
);
3186 len
= strlen(c
->u
.name
);
3187 buf
[1] = cpu_to_le32(len
);
3188 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3191 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3194 rc
= context_write(p
, &c
->context
[0], fp
);
3199 for (j
= 0; j
< 4; j
++)
3200 nodebuf
[j
] = c
->u
.node6
.addr
[j
]; /* network order */
3201 for (j
= 0; j
< 4; j
++)
3202 nodebuf
[j
+ 4] = c
->u
.node6
.mask
[j
]; /* network order */
3203 rc
= put_entry(nodebuf
, sizeof(u32
), 8, fp
);
3206 rc
= context_write(p
, &c
->context
[0], fp
);
3211 /* subnet_prefix is in CPU order */
3212 prefixbuf
[0] = cpu_to_be64(c
->u
.ibpkey
.subnet_prefix
);
3214 rc
= put_entry(prefixbuf
, sizeof(u64
), 1, fp
);
3218 buf
[0] = cpu_to_le32(c
->u
.ibpkey
.low_pkey
);
3219 buf
[1] = cpu_to_le32(c
->u
.ibpkey
.high_pkey
);
3221 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3224 rc
= context_write(p
, &c
->context
[0], fp
);
3228 case OCON_IBENDPORT
:
3229 len
= strlen(c
->u
.ibendport
.dev_name
);
3230 buf
[0] = cpu_to_le32(len
);
3231 buf
[1] = cpu_to_le32(c
->u
.ibendport
.port
);
3232 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3235 rc
= put_entry(c
->u
.ibendport
.dev_name
, 1, len
, fp
);
3238 rc
= context_write(p
, &c
->context
[0], fp
);
3248 static int genfs_write(struct policydb
*p
, void *fp
)
3250 struct genfs
*genfs
;
3257 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
)
3259 buf
[0] = cpu_to_le32(len
);
3260 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3263 for (genfs
= p
->genfs
; genfs
; genfs
= genfs
->next
) {
3264 len
= strlen(genfs
->fstype
);
3265 buf
[0] = cpu_to_le32(len
);
3266 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3269 rc
= put_entry(genfs
->fstype
, 1, len
, fp
);
3273 for (c
= genfs
->head
; c
; c
= c
->next
)
3275 buf
[0] = cpu_to_le32(len
);
3276 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3279 for (c
= genfs
->head
; c
; c
= c
->next
) {
3280 len
= strlen(c
->u
.name
);
3281 buf
[0] = cpu_to_le32(len
);
3282 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3285 rc
= put_entry(c
->u
.name
, 1, len
, fp
);
3288 buf
[0] = cpu_to_le32(c
->v
.sclass
);
3289 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3292 rc
= context_write(p
, &c
->context
[0], fp
);
3300 static int hashtab_cnt(void *key
, void *data
, void *ptr
)
3308 static int range_write_helper(void *key
, void *data
, void *ptr
)
3311 struct range_trans
*rt
= key
;
3312 struct mls_range
*r
= data
;
3313 struct policy_data
*pd
= ptr
;
3315 struct policydb
*p
= pd
->p
;
3318 buf
[0] = cpu_to_le32(rt
->source_type
);
3319 buf
[1] = cpu_to_le32(rt
->target_type
);
3320 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3323 if (p
->policyvers
>= POLICYDB_VERSION_RANGETRANS
) {
3324 buf
[0] = cpu_to_le32(rt
->target_class
);
3325 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3329 rc
= mls_write_range_helper(r
, fp
);
3336 static int range_write(struct policydb
*p
, void *fp
)
3340 struct policy_data pd
;
3345 /* count the number of entries in the hashtab */
3347 rc
= hashtab_map(p
->range_tr
, hashtab_cnt
, &nel
);
3351 buf
[0] = cpu_to_le32(nel
);
3352 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3356 /* actually write all of the entries */
3357 rc
= hashtab_map(p
->range_tr
, range_write_helper
, &pd
);
3364 static int filename_write_helper(void *key
, void *data
, void *ptr
)
3367 struct filename_trans
*ft
= key
;
3368 struct filename_trans_datum
*otype
= data
;
3373 len
= strlen(ft
->name
);
3374 buf
[0] = cpu_to_le32(len
);
3375 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3379 rc
= put_entry(ft
->name
, sizeof(char), len
, fp
);
3383 buf
[0] = cpu_to_le32(ft
->stype
);
3384 buf
[1] = cpu_to_le32(ft
->ttype
);
3385 buf
[2] = cpu_to_le32(ft
->tclass
);
3386 buf
[3] = cpu_to_le32(otype
->otype
);
3388 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3395 static int filename_trans_write(struct policydb
*p
, void *fp
)
3401 if (p
->policyvers
< POLICYDB_VERSION_FILENAME_TRANS
)
3405 rc
= hashtab_map(p
->filename_trans
, hashtab_cnt
, &nel
);
3409 buf
[0] = cpu_to_le32(nel
);
3410 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
3414 rc
= hashtab_map(p
->filename_trans
, filename_write_helper
, fp
);
3422 * Write the configuration data in a policy database
3423 * structure to a policy database binary representation
3426 int policydb_write(struct policydb
*p
, void *fp
)
3428 unsigned int i
, num_syms
;
3433 struct policydb_compat_info
*info
;
3436 * refuse to write policy older than compressed avtab
3437 * to simplify the writer. There are other tests dropped
3438 * since we assume this throughout the writer code. Be
3439 * careful if you ever try to remove this restriction
3441 if (p
->policyvers
< POLICYDB_VERSION_AVTAB
) {
3442 printk(KERN_ERR
"SELinux: refusing to write policy version %d."
3443 " Because it is less than version %d\n", p
->policyvers
,
3444 POLICYDB_VERSION_AVTAB
);
3450 config
|= POLICYDB_CONFIG_MLS
;
3452 if (p
->reject_unknown
)
3453 config
|= REJECT_UNKNOWN
;
3454 if (p
->allow_unknown
)
3455 config
|= ALLOW_UNKNOWN
;
3457 /* Write the magic number and string identifiers. */
3458 buf
[0] = cpu_to_le32(POLICYDB_MAGIC
);
3459 len
= strlen(POLICYDB_STRING
);
3460 buf
[1] = cpu_to_le32(len
);
3461 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3464 rc
= put_entry(POLICYDB_STRING
, 1, len
, fp
);
3468 /* Write the version, config, and table sizes. */
3469 info
= policydb_lookup_compat(p
->policyvers
);
3471 printk(KERN_ERR
"SELinux: compatibility lookup failed for policy "
3472 "version %d", p
->policyvers
);
3476 buf
[0] = cpu_to_le32(p
->policyvers
);
3477 buf
[1] = cpu_to_le32(config
);
3478 buf
[2] = cpu_to_le32(info
->sym_num
);
3479 buf
[3] = cpu_to_le32(info
->ocon_num
);
3481 rc
= put_entry(buf
, sizeof(u32
), 4, fp
);
3485 if (p
->policyvers
>= POLICYDB_VERSION_POLCAP
) {
3486 rc
= ebitmap_write(&p
->policycaps
, fp
);
3491 if (p
->policyvers
>= POLICYDB_VERSION_PERMISSIVE
) {
3492 rc
= ebitmap_write(&p
->permissive_map
, fp
);
3497 num_syms
= info
->sym_num
;
3498 for (i
= 0; i
< num_syms
; i
++) {
3499 struct policy_data pd
;
3504 buf
[0] = cpu_to_le32(p
->symtab
[i
].nprim
);
3505 buf
[1] = cpu_to_le32(p
->symtab
[i
].table
->nel
);
3507 rc
= put_entry(buf
, sizeof(u32
), 2, fp
);
3510 rc
= hashtab_map(p
->symtab
[i
].table
, write_f
[i
], &pd
);
3515 rc
= avtab_write(p
, &p
->te_avtab
, fp
);
3519 rc
= cond_write_list(p
, p
->cond_list
, fp
);
3523 rc
= role_trans_write(p
, fp
);
3527 rc
= role_allow_write(p
->role_allow
, fp
);
3531 rc
= filename_trans_write(p
, fp
);
3535 rc
= ocontext_write(p
, info
, fp
);
3539 rc
= genfs_write(p
, fp
);
3543 rc
= range_write(p
, fp
);
3547 for (i
= 0; i
< p
->p_types
.nprim
; i
++) {
3548 struct ebitmap
*e
= flex_array_get(p
->type_attr_map_array
, i
);
3551 rc
= ebitmap_write(e
, fp
);