Fix bugs related to policy declarations
cover Fix bugs related to policy declarations
In testing an upcoming patch to allow type attributes to be associated with other type attributes and expanding them before creating the kernel policy, I came across a bug in how type attributes were handled in role- type statements. This caused me to take a closer look at how declarations and the things associated to them were being handled.
With these patches, I feel that the code in link.c and expand.c is clearer and more robust.
James Carter (6):
Fix problem with bounds statements in optional blocks Provide a better error message for implicit role and user bounds Fix an error in the policyd validation of user datums
libsepol: Fix processing of levels for user rule in an optional block
libsepol: Fix problem with handling type attributes in role-types rule
libsepol: Expand role attributes when expanding instead of when
linking
checkpolicy/module_compiler.c | 34 +-- checkpolicy/policy_define.c | 11 +- libsepol/src/expand.c | 396 +++++++++++++++++++------------ libsepol/src/link.c | 153 ++---------- libsepol/src/module_to_cil.c | 23 +- libsepol/src/policydb_validate.c | 5 + 6 files changed, 296 insertions(+), 326 deletions(-)
Signature · 1 line
2.50.0
PATCH 1/6 Fix problem with bounds statements in optional blocks
Using a bounds statement in an optional will always cause that bounds to apply even when the optional block's requires are not met and the block is disabled.
Instead, handle bounds like type attributes and create a local declaration for the block and only copy the bounds to the global declaration when creating the kernel binary policy.
--- checkpolicy/policy_define.c | 11 +++++++---- libsepol/src/expand.c | 10 ++++++++++ libsepol/src/link.c | 24 +++++++++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-)
checkpolicy/policy_define.c +7 -4
@@ -1484,6 +1484,8 @@ int define_typeattribute(void)static int define_typebounds_helper (const char *bounds_id,const char *type_id) {type_datum_t *bounds, *type; +char *id; +uint32_t value;if (!is_id_in_scope (SYM_TYPES , bounds_id)) {yyerror2 ("type %s is not within scope" , bounds_id);
@@ -1507,10 +1509,11 @@ static int define_typebounds_helper(const char *bounds_id, const char *type_id)return -1 ; } -if (type->flavor ==TYPE_TYPE && !type->primary) { - type = policydbp->type_val_to_struct[type->s.value -1 ]; - }else if (type->flavor ==TYPE_ALIAS ) { - type = policydbp->type_val_to_struct[type->primary -1 ]; + id =strdup (type_id); + value = (type->flavor !=TYPE_ALIAS ) ? type->s.value : type->primary; + type =get_local_type (id, value,0 ); +if (!type) { +yyerror ("Out of memory!" ); }if (!type->bounds)
libsepol/src/expand.c +10 -0
@@ -3146,16 +3146,26 @@ int expand_module(sepol_handle_t * handle, (decl->p_types.table, attr_convert_callback, &state)) { goto cleanup; } +/* copy type bounds */ +if (hashtab_map (decl->p_types.table, + type_bounds_copy_callback, &state)) + goto cleanup;/* copy roles */ if (hashtab_map (decl->p_roles.table, role_copy_callback, &state)) goto cleanup; +if (hashtab_map (state.base->p_roles.table, + role_bounds_copy_callback, &state)) + goto cleanup;/* copy users */ if (hashtab_map (decl->p_users.table, user_copy_callback, &state)) goto cleanup; +if (hashtab_map (decl->p_users.table, + user_bounds_copy_callback, &state)) + goto cleanup; }
libsepol/src/link.c +21 -3
@@ -766,13 +766,19 @@ static int type_bounds_copy_callback(hashtab_key_t key,type_datum_t *type = (type_datum_t *) datum;type_datum_t *dest;uint32_t bounds_val; +hashtab_t type_tab;if (!type->bounds)return 0 ; +if (state->dest_decl ==NULL ) + type_tab = state->base->p_types.table; +else + type_tab = state->dest_decl->p_types.table; + bounds_val = state->cur->map[SYM_TYPES ][type->bounds -1 ]; - dest =hashtab_search (state->base->p_types.table, key); + dest =hashtab_search (type_tab, key);if (!dest) {ERR (state->handle,"Type lookup failed for %s" , (char *)key);
@@ -795,13 +801,19 @@ static int role_bounds_copy_callback(hashtab_key_t key,role_datum_t *role = (role_datum_t *) datum;role_datum_t *dest;uint32_t bounds_val; +hashtab_t role_tab;if (!role->bounds)return 0 ; +if (state->dest_decl ==NULL ) + role_tab = state->base->p_roles.table; +else + role_tab = state->dest_decl->p_roles.table; + bounds_val = state->cur->map[SYM_ROLES ][role->bounds -1 ]; - dest =hashtab_search (state->base->p_roles.table, key); + dest =hashtab_search (role_tab, key);if (!dest) {ERR (state->handle,"Role lookup failed for %s" , (char *)key);
@@ -824,13 +836,19 @@ static int user_bounds_copy_callback(hashtab_key_t key,user_datum_t *user = (user_datum_t *) datum;user_datum_t *dest;uint32_t bounds_val; +hashtab_t user_tab;if (!user->bounds)return 0 ; +if (state->dest_decl ==NULL ) + user_tab = state->base->p_users.table; +else + user_tab = state->dest_decl->p_users.table; + bounds_val = state->cur->map[SYM_USERS ][user->bounds -1 ]; - dest =hashtab_search (state->base->p_users.table, key); + dest =hashtab_search (user_tab, key);if (!dest) {ERR (state->handle,"User lookup failed for %s" , (char *)key);
Signature · 1 line
2.50.0
PATCH 2/6 Provide a better error message for implicit role and user bounds
In reply to James CarterImplicit role and user bounds requires the role and its parent to be in the same scope. The current error message says that the parent does not exist, but, in fact, the parent could exist in a different scope.
Instead, have the error message say that the parent was not found in the same scope.
--- checkpolicy/module_compiler.c | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-)
checkpolicy/module_compiler.c +8 -26
@@ -207,8 +207,7 @@ int declare_symbol(uint32_t symbol_type,return ret; } -static int role_implicit_bounds (hashtab_t roles_tab, -char *role_id,role_datum_t *role) +static int role_implicit_bounds (hashtab_t roles_tab,char *role_id,role_datum_t *role) {role_datum_t *bounds;char *bounds_id, *delim;
@@ -226,21 +225,13 @@ static int role_implicit_bounds(hashtab_t roles_tab, bounds =hashtab_search (roles_tab, bounds_id);if (!bounds) { -yyerror2 ("role %s doesn't exist, is implicit bounds of %s" , - bounds_id, role_id); +yyerror2 ("Implicit role bounds declared (%s), but the parent (%s) was not found in the same scope" , role_id, bounds_id);free (bounds_id);return -1 ; } -if (!role->bounds) - role->bounds = bounds->s.value; -else if (role->bounds != bounds->s.value) { -yyerror2 ("role %s has inconsistent bounds %s/%s" , - role_id, bounds_id, - policydbp->p_role_val_to_name[role->bounds -1 ]); -free (bounds_id); -return -1 ; - } + role->bounds = bounds->s.value; +free (bounds_id);return 0 ;
@@ -456,8 +447,7 @@ type_datum_t *declare_type(unsigned char primary, unsigned char isattr)return type; } -static int user_implicit_bounds (hashtab_t users_tab, -char *user_id,user_datum_t *user) +static int user_implicit_bounds (hashtab_t users_tab,char *user_id,user_datum_t *user) {user_datum_t *bounds;char *bounds_id, *delim;
@@ -475,21 +465,13 @@ static int user_implicit_bounds(hashtab_t users_tab, bounds =hashtab_search (users_tab, bounds_id);if (!bounds) { -yyerror2 ("user %s doesn't exist, is implicit bounds of %s" , - bounds_id, user_id); +yyerror2 ("Implicit user bounds declared (%s), but the parent (%s) was not found in the same scope" , user_id, bounds_id);free (bounds_id);return -1 ; } -if (!user->bounds) - user->bounds = bounds->s.value; -else if (user->bounds != bounds->s.value) { -yyerror2 ("user %s has inconsistent bounds %s/%s" , - user_id, bounds_id, - policydbp->p_role_val_to_name[user->bounds -1 ]); -free (bounds_id); -return -1 ; - } + user->bounds = bounds->s.value; +free (bounds_id);return 0 ;
Signature · 1 line
2.50.0
PATCH 3/6 Fix an error in the policyd validation of user datums
In reply to James CarterIf a user is in a require block in a base module, then a user datum will be created for the user. This will cause the policydb to fail validation when the base module is loaded if the required user is not in the base module because the MLS level and range will be empty.
Make sure that a level datum has a sensitivity and fail validation if it does not, but allow levels and ranges in the user datum to have no MLS information.
--- libsepol/src/policydb_validate.c | 5 +++++ 1 file changed, 5 insertions(+)
libsepol/src/policydb_validate.c +5 -0
@@ -665,6 +665,8 @@ bad:static int validate_mls_level (const mls_level_t *level,const validate_t *sens,const validate_t *cats) { +if (level->sens ==0 ) +return 0 ;if (validate_value (level->sens, sens)) goto bad;if (validate_ebitmap (&level->cat, cats))
@@ -681,6 +683,9 @@ static int validate_level_datum(sepol_handle_t *handle, const level_datum_t *levif (level->notdefined !=0 ) goto bad; +if (level->level->sens ==0 ) + goto bad; +if (validate_mls_level (level->level, &flavors[SYM_LEVELS ], &flavors[SYM_CATS ])) goto bad;
Signature · 1 line
2.50.0
PATCH 4/6 libsepol: Fix processing of levels for user rule in an optional block
In reply to James CarterWhen converting a module to CIL, MLS user statements in an optional block are not handled correctly. The string names of the sensitivities for the user's default level and range are not looked up correctly. There is special code to look up the name of a sensitivity directly from the sensitivity's id instead of with the usual -1 offset.
Instead, lookup the sensitivities for a MLS user statement in an optional block with thei usual -1 offset.
--- libsepol/src/module_to_cil.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)
libsepol/src/module_to_cil.c +8 -15
@@ -516,11 +516,11 @@ static int is_id_in_scope(struct policydb *pdb, struct stack *decl_stack, char *return is_id_in_scope_with_start (pdb, decl_stack, start, symbol_type, type); } -static int semantic_level_to_cil (struct policydb *pdb,int sens_offset,struct mls_semantic_level *level) +static int semantic_level_to_cil (struct policydb *pdb,struct mls_semantic_level *level) {struct mls_semantic_cat *cat; -cil_printf ("(%s " , pdb->p_sens_val_to_name[level->sens - sens_offset]); +cil_printf ("(%s " , pdb->p_sens_val_to_name[level->sens -1 ]);if (level->cat !=NULL ) {cil_printf ("(" );
@@ -1586,14 +1586,14 @@ static int range_trans_to_cil(int indent, struct policydb *pdb, struct range_tracil_printf ("(" ); - rc =semantic_level_to_cil (pdb,1 , &rule->trange.level[0 ]); + rc =semantic_level_to_cil (pdb, &rule->trange.level[0 ]);if (rc !=0 ) { goto exit; }cil_printf (" " ); - rc =semantic_level_to_cil (pdb,1 , &rule->trange.level[1 ]); + rc =semantic_level_to_cil (pdb, &rule->trange.level[1 ]);if (rc !=0 ) { goto exit; }
@@ -2320,7 +2320,7 @@ exit:return rc; } -static int user_to_cil (int indent,struct policydb *pdb,struct avrule_block *block,struct stack *UNUSED (decl_stack ),char *key,void *datum,int scope) +static int user_to_cil (int indent,struct policydb *pdb,struct avrule_block *UNUSED (block ),struct stack *UNUSED (decl_stack ),char *key,void *datum,int scope) {struct user_datum *user = datum;struct ebitmap roles = user->roles.roles;
@@ -2328,7 +2328,6 @@ static int user_to_cil(int indent, struct policydb *pdb, struct avrule_block *blstruct mls_semantic_range range = user->range;struct ebitmap_node *node;uint32_t i; -int sens_offset =1 ;if (scope ==SCOPE_DECL ) {cil_println (indent,"(user %s)" , key);
@@ -2341,16 +2340,10 @@ static int user_to_cil(int indent, struct policydb *pdb, struct avrule_block *blcil_println (indent,"(userrole %s %s)" , key, pdb->p_role_val_to_name[i]); } -if (block->flags &AVRULE_OPTIONAL ) { -// sensitivities in user statements in optionals do not have the -// standard -1 offset - sens_offset =0 ; - } -cil_indent (indent);cil_printf ("(userlevel %s " , key);if (pdb->mls) { -semantic_level_to_cil (pdb, sens_offset, &level); +semantic_level_to_cil (pdb, &level); }else {cil_printf (DEFAULT_LEVEL ); }
@@ -2359,9 +2352,9 @@ static int user_to_cil(int indent, struct policydb *pdb, struct avrule_block *blcil_indent (indent);cil_printf ("(userrange %s (" , key);if (pdb->mls) { -semantic_level_to_cil (pdb, sens_offset, &range.level[0 ]); +semantic_level_to_cil (pdb, &range.level[0 ]);cil_printf (" " ); -semantic_level_to_cil (pdb, sens_offset, &range.level[1 ]); +semantic_level_to_cil (pdb, &range.level[1 ]); }else {cil_printf (DEFAULT_LEVEL " " DEFAULT_LEVEL ); }
Signature · 1 line
2.50.0
PATCH 5/6 libsepol: Fix problem with handling type attributes in role-types rule
In reply to James CarterA role-types rule that has an attribute in the types expression will be improperly expanded if types are associated with the attribute in other modules.
For example:
In a base module there is the following:
attribute at1;
type t1a;
typeattribute t1a at1;
role r1a;
role r1a types at1;
In another module there is the following:
type t1b;
typeattribute t1b at1;
In this example, t1b will not be associated with r1a even though it is associated with at1.
The problem is with the order that types and roles are expanded. All types must be completely expanded before expanding the types of a role.
First, expand types completely, then expand roles completely, and then, expand users completely.
--- libsepol/src/expand.c | 229 ++++++++++++++++++++++-------------------- 1 file changed, 122 insertions(+), 107 deletions(-)
libsepol/src/expand.c +122 -107
@@ -747,21 +747,79 @@ static int alias_copy_callback(hashtab_key_t key, hashtab_datum_t datum,return 0 ; } -static int role_remap_dominates (hashtab_key_t key __attribute__ ((unused)),hashtab_datum_t datum,void *data) +static int role_dominates_copy_callback (hashtab_key_t key __attribute__ ((unused)),hashtab_datum_t datum,void *data) { -ebitmap_t mapped_roles; -role_datum_t *role = (role_datum_t *) datum; -expand_state_t *state = (expand_state_t *) data; +char *id; +role_datum_t *role, *new_role; +expand_state_t *state; +ebitmap_t mapped; -if (map_ebitmap (&role->dominates, &mapped_roles, state->rolemap)) + id = (char *)key; + role = (role_datum_t *) datum; + state = (expand_state_t *) data; + +if (!is_id_enabled (id, state->base,SYM_ROLES )) { +/* identifier's scope is not enabled */ +return 0 ; + } + +if (state->verbose) +INFO (state->handle,"Copying role dominates %s" , id); + + new_role = (role_datum_t *)hashtab_search (state->out->p_roles.table, id); +if (!new_role) { +ERR (state->handle,"Role lookup failed for %s" , id);return -1 ; + } -ebitmap_destroy (&role->dominates); - -if (ebitmap_cpy (&role->dominates, &mapped_roles)) +if (map_ebitmap (&role->dominates, &mapped, state->rolemap))return -1 ; +if (ebitmap_union (&new_role->dominates, &mapped)) +return -1 ; +ebitmap_destroy (&mapped); -ebitmap_destroy (&mapped_roles); +return 0 ; +} + +static int role_types_copy_callback (hashtab_key_t key,hashtab_datum_t datum,void *data) +{ +char *id; +role_datum_t *role, *new_role; +expand_state_t *state; +ebitmap_t tmp_union_types; + + id = key; + role = (role_datum_t *) datum; + state = (expand_state_t *)data; + +if (!is_id_enabled (id, state->base,SYM_ROLES )) { +/* identifier's scope is not enabled */ +return 0 ; + } + +if (state->verbose) +INFO (state->handle,"copying types for role %s" , id); + + new_role = (role_datum_t *)hashtab_search (state->out->p_roles.table, id); +if (!new_role) { +ERR (state->handle,"Could not find role %s" , id); +return -1 ; + } + +/* convert types in the role datum in the global symtab */ +ebitmap_init (&tmp_union_types); +if (expand_convert_type_set + (state->out, state->typemap, &role->types, &tmp_union_types,1 )) { +ebitmap_destroy (&tmp_union_types); +ERR (state->handle,"Out of memory!" ); +return -1 ; + } +if (ebitmap_union (&new_role->types.types, &tmp_union_types)) { +ERR (state->handle,"Out of memory!" ); +ebitmap_destroy (&tmp_union_types); +return -1 ; + } +ebitmap_destroy (&tmp_union_types);return 0 ; }
@@ -841,10 +899,8 @@ static int role_copy_callback(hashtab_key_t key, hashtab_datum_t datum, {int ret;char *id, *new_id; -role_datum_t *role; -role_datum_t *new_role; +role_datum_t *role, *new_role;expand_state_t *state; -ebitmap_t tmp_union_types; id = key; role = (role_datum_t *) datum;
@@ -864,8 +920,7 @@ static int role_copy_callback(hashtab_key_t key, hashtab_datum_t datum,if (state->verbose)INFO (state->handle,"copying role %s" , id); - new_role = - (role_datum_t *)hashtab_search (state->out->p_roles.table, id); + new_role = (role_datum_t *)hashtab_search (state->out->p_roles.table, id);if (!new_role) { new_role = (role_datum_t *)malloc (sizeof (role_datum_t));if (!new_role) {
@@ -897,31 +952,6 @@ static int role_copy_callback(hashtab_key_t key, hashtab_datum_t datum, } } -/* The dominates bitmap is going to be wrong for the moment, -* we'll come back later and remap them, after we are sure all -* the roles have been added */ -if (ebitmap_union (&new_role->dominates, &role->dominates)) { -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - -ebitmap_init (&tmp_union_types); - -/* convert types in the role datum in the global symtab */ -if (expand_convert_type_set - (state->out, state->typemap, &role->types, &tmp_union_types,1 )) { -ebitmap_destroy (&tmp_union_types); -ERR (state->handle,"Out of memory!" ); -return -1 ; - } - -if (ebitmap_union (&new_role->types.types, &tmp_union_types)) { -ERR (state->handle,"Out of memory!" ); -ebitmap_destroy (&tmp_union_types); -return -1 ; - } -ebitmap_destroy (&tmp_union_types); -return 0 ; }
@@ -3055,14 +3085,26 @@ int expand_module(sepol_handle_t * handle,/* order is important - types must be first */ /* copy types */ -if (hashtab_map (state.base->p_types.table, type_copy_callback, &state)) { +if (hashtab_map (state.base->p_types.table, type_copy_callback, &state)) goto cleanup; - } - -/* convert attribute type sets */ -if (hashtab_map - (state.base->p_types.table, attr_convert_callback, &state)) { +/* Needed for processing aliases */ +if (policydb_index_others (handle, out, verbose)) + goto cleanup; +if (hashtab_map (state.base->p_types.table, alias_copy_callback, &state)) + goto cleanup; +if (hashtab_map (state.base->p_types.table, type_bounds_copy_callback, &state)) + goto cleanup; +if (hashtab_map (state.base->p_types.table, attr_convert_callback, &state)) goto cleanup; +for (curblock = state.base->global; curblock !=NULL ; + curblock = curblock->next) { +avrule_decl_t *decl = curblock->enabled; +if (decl ==NULL ) +continue ; +if (hashtab_map (decl->p_types.table, attr_convert_callback, &state)) + goto cleanup; +if (hashtab_map (decl->p_types.table, type_bounds_copy_callback, &state)) + goto cleanup; }/* copy commons */
@@ -3078,16 +3120,7 @@ int expand_module(sepol_handle_t * handle, goto cleanup; } -/* copy type bounds */ -if (hashtab_map (state.base->p_types.table, - type_bounds_copy_callback, &state)) - goto cleanup; - -/* copy aliases */ -if (hashtab_map (state.base->p_types.table, alias_copy_callback, &state)) - goto cleanup; - -/* index here so that type indexes are available for role_copy_callback */ +/* index here so that type indexes are available for role_types_copy_callback */ if (policydb_index_others (handle, out, verbose)) {ERR (handle,"Error while indexing out symbols" ); goto cleanup;
@@ -3096,8 +3129,29 @@ int expand_module(sepol_handle_t * handle,/* copy roles */ if (hashtab_map (state.base->p_roles.table, role_copy_callback, &state)) goto cleanup; -if (hashtab_map (state.base->p_roles.table, - role_bounds_copy_callback, &state)) +/* Needed for evaluating role attributes and the types for each role */ +if (policydb_index_others (handle, out, verbose)) + goto cleanup; +if (hashtab_map (state.base->p_roles.table, role_bounds_copy_callback, &state)) + goto cleanup; +if (hashtab_map (state.base->p_roles.table, role_dominates_copy_callback, &state)) + goto cleanup; +if (hashtab_map (state.base->p_roles.table, role_types_copy_callback, &state)) + goto cleanup; +for (curblock = state.base->global; curblock !=NULL ; + curblock = curblock->next) { +avrule_decl_t *decl = curblock->enabled; +if (decl ==NULL ) +continue ; +if (hashtab_map (decl->p_roles.table, role_bounds_copy_callback, &state)) + goto cleanup; +if (hashtab_map (decl->p_roles.table, role_dominates_copy_callback, &state)) + goto cleanup; +if (hashtab_map (decl->p_roles.table, role_types_copy_callback, &state)) + goto cleanup; + } +/* Copy types in role attribute to all roles that belongs to it */ +if (hashtab_map (state.base->p_roles.table, role_fix_callback, &state)) goto cleanup;/* copy MLS's sensitivity level and categories - this needs to be done
@@ -3114,9 +3168,18 @@ int expand_module(sepol_handle_t * handle,/* copy users */ if (hashtab_map (state.base->p_users.table, user_copy_callback, &state)) goto cleanup; -if (hashtab_map (state.base->p_users.table, - user_bounds_copy_callback, &state)) +if (hashtab_map (state.base->p_users.table, user_bounds_copy_callback, &state)) goto cleanup; +for (curblock = state.base->global; curblock !=NULL ; + curblock = curblock->next) { +avrule_decl_t *decl = curblock->enabled; +if (decl ==NULL ) +continue ; +if (hashtab_map (decl->p_users.table, user_copy_callback, &state)) + goto cleanup; +if (hashtab_map (decl->p_users.table, user_bounds_copy_callback, &state)) + goto cleanup; + }/* copy bools */ if (hashtab_map (state.base->p_bools.table, bool_copy_callback, &state))
@@ -3131,54 +3194,6 @@ int expand_module(sepol_handle_t * handle, goto cleanup; } -/* loop through all decls and union attributes, roles, users */ -for (curblock = state.base->global; curblock !=NULL ; - curblock = curblock->next) { -avrule_decl_t *decl = curblock->enabled; - -if (decl ==NULL ) { -/* nothing was enabled within this block */ -continue ; - } - -/* convert attribute type sets */ -if (hashtab_map - (decl->p_types.table, attr_convert_callback, &state)) { - goto cleanup; - } -/* copy type bounds */ -if (hashtab_map (decl->p_types.table, - type_bounds_copy_callback, &state)) - goto cleanup; - -/* copy roles */ -if (hashtab_map - (decl->p_roles.table, role_copy_callback, &state)) - goto cleanup; -if (hashtab_map (state.base->p_roles.table, - role_bounds_copy_callback, &state)) - goto cleanup; - -/* copy users */ -if (hashtab_map - (decl->p_users.table, user_copy_callback, &state)) - goto cleanup; -if (hashtab_map (decl->p_users.table, - user_bounds_copy_callback, &state)) - goto cleanup; - - } - -/* remap role dominates bitmaps */ -if (hashtab_map (state.out->p_roles.table, role_remap_dominates, &state)) { - goto cleanup; - } - -/* escalate the type_set_t in a role attribute to all regular roles -* that belongs to it. */ -if (hashtab_map (state.base->p_roles.table, role_fix_callback, &state)) - goto cleanup; -if (copy_and_expand_avrule_block (&state) <0 ) {ERR (handle,"Error during expand" ); goto cleanup;
Signature · 1 line
2.50.0
PATCH 6/6 libsepol: Expand role attributes when expanding instead of when linking
In reply to James CarterRole attributes are being expanded during the linking phase rather than during the expand phase. This might be due to some concern over role attributes not being kept for the kernel policy.
Instead of this, expand role attributes during the expand phase. One extra step is needed to make sure the pp and mod files end up the same as before. The role attributes in the base policy need to be expanded as well as those in the kernel policy. This is because of a side effect that occurs when compiling a base module. When a base module is compiled it is linked and expanded inorder to verify that linking and expansion will work. Even though the resulting kernel policy is discarded, the base module is changed because the linking phase expands the role attributes in the base module.
With this change the pp files, mod files, and the final binary file (bin file) produced are identical to ones produced by an old toolchain without these changes. The only file that is different is the linked binary (lnk file). In the old toolchain, all roles in blocks are added to the role in the global scope and the role attributes are expanded, but this will be done again when the policy is expanded. Expanding a linked binary created with these patches using the old toolchain will produce the same kernel binary policy. Expanding a linked binary created with the old toolchain using the new toolchain will also produce the same kernel binary policy.
--- libsepol/src/expand.c | 179 +++++++++++++++++++++++++++++------------- libsepol/src/link.c | 129 ------------------------------ 2 files changed, 126 insertions(+), 182 deletions(-)
libsepol/src/expand.c +126 -53
@@ -824,27 +824,18 @@ static int role_types_copy_callback(hashtab_key_t key, hashtab_datum_t datum, voreturn 0 ; } -/* For the role attribute in the base module, escalate its counterpart's -* types.types ebitmap in the out module to the counterparts of all the -* regular role that belongs to the current role attribute. Note, must be -* invoked after role_copy_callback so that state->rolemap is available. -*/ -static int role_fix_callback (hashtab_key_t key,hashtab_datum_t datum, -void *data) +static int role_roles_copy_callback (hashtab_key_t key,hashtab_datum_t datum,void *data) { -char *id, *base_reg_role_id; -role_datum_t *role, *new_role, *regular_role; +char *id; +role_datum_t *role, *new_role;expand_state_t *state; -ebitmap_node_t *rnode; -unsigned int i; -ebitmap_t mapped_roles; +ebitmap_t mapped; id = key; - role = (role_datum_t *)datum; + role = (role_datum_t *) datum; state = (expand_state_t *)data; -if (strcmp (id,OBJECT_R ) ==0 ) { -/* object_r is never a role attribute by far */ +if (role->flavor !=ROLE_ATTRIB ) {return 0 ; }
@@ -853,39 +844,107 @@ static int role_fix_callback(hashtab_key_t key, hashtab_datum_t datum,return 0 ; } -if (role->flavor !=ROLE_ATTRIB ) -return 0 ; -if (state->verbose) -INFO (state->handle,"fixing role attribute %s" , id); +INFO (state->handle,"copying roles for role %s" , id); - new_role = - (role_datum_t *)hashtab_search (state->out->p_roles.table, id); - -assert (new_role !=NULL && new_role->flavor ==ROLE_ATTRIB ); + new_role = (role_datum_t *)hashtab_search (state->out->p_roles.table, id); +if (!new_role) { +ERR (state->handle,"Could not find role %s" , id); +return -1 ; + } -ebitmap_init (&mapped_roles); -if (map_ebitmap (&role->roles, &mapped_roles, state->rolemap)) +/* convert roles in the role datum in the global symtab */ +if (map_ebitmap (&role->roles, &mapped, state->rolemap))return -1 ; -if (ebitmap_union (&new_role->roles, &mapped_roles)) { +if (ebitmap_union (&new_role->roles, &mapped)) {ERR (state->handle,"Out of memory!" ); -ebitmap_destroy (&mapped_roles); +ebitmap_destroy (&mapped);return -1 ; } -ebitmap_destroy (&mapped_roles); +ebitmap_destroy (&mapped); + +return 0 ; +} +static int expand_role_attributes_in_attributes (sepol_handle_t *handle,policydb_t *p) +{ +ebitmap_t attrs, roles; +ebitmap_node_t *ni, *nj; +unsigned int i, j, reps =0 , done =0 ; +role_datum_t *rd, *ad; + +ebitmap_init (&attrs); +for (i=0 ; i < p->p_roles.nprim; i++) { + rd = p->role_val_to_struct[i]; +if (rd->flavor ==ROLE_ATTRIB ) { +ebitmap_set_bit (&attrs, i,1 ); + } + } + +while (!done && reps < p->p_roles.nprim) { + done =1 ; + reps++; +ebitmap_for_each_positive_bit (&attrs, ni, i) { + rd = p->role_val_to_struct[i]; +if (ebitmap_match_any (&rd->roles, &attrs)) { + done =0 ; +if (ebitmap_get_bit (&rd->roles, i)) { +ERR (handle,"Before: attr has own bit set: %d\n" , i); + } +ebitmap_init (&roles); +ebitmap_for_each_positive_bit (&rd->roles, nj, j) { +if (ebitmap_get_bit (&attrs, j)) { + ad = p->role_val_to_struct[j]; +ebitmap_union (&roles, &ad->roles); +ebitmap_set_bit (&rd->roles, j,0 ); + } + } +ebitmap_union (&rd->roles, &roles); +ebitmap_destroy (&roles); +if (ebitmap_get_bit (&rd->roles, i)) { +ERR (handle,"After: attr has own bit set: %d\n" , i); + done =1 ;/* Just end early */ + } + } + } + } -ebitmap_for_each_positive_bit (&role->roles, rnode, i) { -/* take advantage of sym_val_to_name[] -* of the base module */ - base_reg_role_id = state->base->p_role_val_to_name[i]; - regular_role = (role_datum_t *)hashtab_search ( - state->out->p_roles.table, - base_reg_role_id); -assert (regular_role !=NULL && - regular_role->flavor ==ROLE_ROLE ); - -if (ebitmap_union (®ular_role->types.types, - &new_role->types.types)) { +if (reps >= p->p_roles.nprim) { +ERR (handle,"Had to bail: reps = %u\n" , reps); + } + +ebitmap_destroy (&attrs); + +return 0 ; +} + +/* For the role attribute in the base module, escalate its counterpart's +* types.types ebitmap in the out module to the counterparts of all the +* regular role that belongs to the current role attribute. Note, must be +* invoked after role_copy_callback so that state->rolemap is available. +*/ +static int role_fix_callback (hashtab_key_t key,hashtab_datum_t datum, +void *data) +{ +char *id; +role_datum_t *attr, *role; +expand_state_t *state; +ebitmap_node_t *rnode; +unsigned int i; + + id = key; + attr = (role_datum_t *)datum; + state = (expand_state_t *)data; + +if (attr->flavor !=ROLE_ATTRIB ) +return 0 ; + +if (state->verbose) +INFO (state->handle,"fixing role attribute %s" , id); + +ebitmap_for_each_positive_bit (&attr->roles, rnode, i) { + role = state->out->role_val_to_struct[i]; +assert (role !=NULL && role->flavor ==ROLE_ROLE ); +if (ebitmap_union (&role->types.types, &attr->types.types)) {ERR (state->handle,"Out of memory!" );return -1 ; }
@@ -2494,7 +2553,7 @@ int expand_rule(sepol_handle_t * handle,* the regular role belongs to could be properly handled by * copy_role_trans and copy_role_allow. */ -int role_set_expand (role_set_t * x,ebitmap_t * r,policydb_t * out,policydb_t * base,uint32_t * rolemap) +int role_set_expand (role_set_t * x,ebitmap_t * r,policydb_t * out, __attribute__ ((unused))policydb_t * base,uint32_t * rolemap) {unsigned int i;ebitmap_node_t *rnode;
@@ -2515,29 +2574,25 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_tebitmap_init (&roles);if (rolemap) { -assert (base !=NULL ); -ebitmap_for_each_positive_bit (&x->roles, rnode, i) { -/* take advantage of p_role_val_to_struct[] -* of the base module */ - role = base->role_val_to_struct[i]; +if (map_ebitmap (&x->roles, &mapped_roles, rolemap)) + goto bad; +ebitmap_for_each_positive_bit (&mapped_roles, rnode, i) { + role = out->role_val_to_struct[i];assert (role !=NULL );if (role->flavor ==ROLE_ATTRIB ) { -if (ebitmap_union (&roles, - &role->roles)) +if (ebitmap_union (&roles, &role->roles)) goto bad; }else {if (ebitmap_set_bit (&roles, i,1 )) goto bad; } } -if (map_ebitmap (&roles, &mapped_roles, rolemap)) - goto bad; }else { -if (ebitmap_cpy (&mapped_roles, &x->roles)) +if (ebitmap_cpy (&roles, &x->roles)) goto bad; } -ebitmap_for_each_positive_bit (&mapped_roles, rnode, i) { +ebitmap_for_each_positive_bit (&roles, rnode, i) {if (ebitmap_set_bit (r, i,1 )) goto bad; }
@@ -3138,6 +3193,8 @@ int expand_module(sepol_handle_t * handle, goto cleanup;if (hashtab_map (state.base->p_roles.table, role_types_copy_callback, &state)) goto cleanup; +if (hashtab_map (state.base->p_roles.table, role_roles_copy_callback, &state)) + goto cleanup;for (curblock = state.base->global; curblock !=NULL ; curblock = curblock->next) {avrule_decl_t *decl = curblock->enabled;
@@ -3149,9 +3206,25 @@ int expand_module(sepol_handle_t * handle, goto cleanup;if (hashtab_map (decl->p_roles.table, role_types_copy_callback, &state)) goto cleanup; +if (hashtab_map (decl->p_roles.table, role_roles_copy_callback, &state)) + goto cleanup; + } +/* Expand any role attributes found in the roles ebitmap of each role attribute */ +if (expand_role_attributes_in_attributes (state.handle, state.out)) { + goto cleanup; } +/* When compiling a base module, the base module is linked and expanded (to +* verify that it could be done) and the resulting kernel module is discarded. +* But the base module is changed while linking because role attributes are +* expanded while linking instead of being expanded when expanding. +* To duplicate that behavior, expand the base module role attributes. +*/ +if (expand_role_attributes_in_attributes (state.handle, state.base)) { + goto cleanup; + } +/* Copy types in role attribute to all roles that belongs to it */ -if (hashtab_map (state.base->p_roles.table, role_fix_callback, &state)) +if (hashtab_map (state.out->p_roles.table, role_fix_callback, &state)) goto cleanup;/* copy MLS's sensitivity level and categories - this needs to be done
libsepol/src/link.c +0 -129
141 lines not on this page · raw message
Signature · 1 line
2.50.0
Quoted 27 lines · James Carter
In testing an upcoming patch to allow type attributes to be associated with other type attributes and expanding them before creating the kernel policy, I came across a bug in how type attributes were handled in role- type statements. This caused me to take a closer look at how declarations and the things associated to them were being handled.
With these patches, I feel that the code in
link.candexpand.cis clearer and more robust.James Carter (6):
Fix problem with bounds statements in optional blocks Provide a better error message for implicit role and user bounds Fix an error in the policyd validation of user datums
libsepol: Fix processing of levels for user rule in an optional block
libsepol: Fix problem with handling type attributes in role-types rule
libsepol: Expand role attributes when expanding instead of when
linkingcheckpolicy/module_compiler.c | 34 +-- checkpolicy/policy_define.c | 11 +- libsepol/src/expand.c | 396 +++++++++++++++++++------------ libsepol/src/link.c | 153 ++---------- libsepol/src/module_to_cil.c | 23 +- libsepol/src/policydb_validate.c | 5 + 6 files changed, 296 insertions(+), 326 deletions(-)
Quoted 3 lines
--
2.50.0
This series has been merged.
Jim