libsepol: Fix double free in copy_avrule_block
PATCH 1/1 libsepol: Fix double free in copy_avrule_block
If there is a failure in copy_avrule_decl(), avrule_decl_destroy() is called on new_decl and then avrule_block_list_destroy() is called on new_block, but since new_decl has already been linked into new_block, new_decl will be freed again.
Instead, just link new_decl into new_block after the call to copy_avrule_decl().
--- libsepol/src/link.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
libsepol/src/link.c +7 -6
@@ -1757,12 +1757,6 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, } } -if (last_decl ==NULL ) { - new_block->branch_list = new_decl; - }else { - last_decl->next = new_decl; - } - last_decl = new_decl; state->base->decl_val_to_struct[state->next_decl_id -1 ] = new_decl; state->decl_to_mod[state->next_decl_id] = module->policy;
@@ -1775,6 +1769,13 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, goto cleanup; } +if (last_decl ==NULL ) { + new_block->branch_list = new_decl; + }else { + last_decl->next = new_decl; + } + last_decl = new_decl; + state->next_decl_id++; } state->last_avrule_block->next = new_block;
Signature · 1 line
2.54.0
Quoted 10 lines · James Carter
If there is a failure in
copy_avrule_decl(),avrule_decl_destroy()is called onnew_decland thenavrule_block_list_destroy()is called onnew_block, but sincenew_declhas already been linked intonew_block,new_declwill be freed again.Instead, just link
new_declintonew_blockafter the call tocopy_avrule_decl().Signed-off-by James Carter <[email protected]>
Quoted 39 lines
--- libsepol/src/link.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)libsepol/src/link.c +7
-7@@ -1757,12 +1757,6 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, } } -if (last_decl ==NULL ) { - new_block->branch_list = new_decl; - }else { - last_decl->next = new_decl; - } - last_decl = new_decl; state->base->decl_val_to_struct[state->next_decl_id -1 ] = new_decl; state->decl_to_mod[state->next_decl_id] = module->policy;@@ -1775,6 +1769,13 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, goto cleanup; } +if (last_decl ==NULL ) { + new_block->branch_list = new_decl; + }else { + last_decl->next = new_decl; + } + last_decl = new_decl; + state->next_decl_id++; } state->last_avrule_block->next = new_block; --2.54.0
Stephen Smalley
maintainerQuoted 8 lines · Stephen Smalley
Quoted 6 lines · James Carter
If there is a failure in
copy_avrule_decl(),avrule_decl_destroy()is called onnew_decland thenavrule_block_list_destroy()is called onnew_block, but sincenew_declhas already been linked intonew_block,new_declwill be freed again.
Quoted 6 lines
Quoted 4 lines
Instead, just link
new_declintonew_blockafter the call tocopy_avrule_decl().Signed-off-by James Carter <[email protected]>Acked-by Stephen Smalley <[email protected]>
Merged.
Quoted 40 lines
Quoted 40 lines
--- libsepol/src/link.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)libsepol/src/link.c +7
-7@@ -1757,12 +1757,6 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, } } -if (last_decl ==NULL ) { - new_block->branch_list = new_decl; - }else { - last_decl->next = new_decl; - } - last_decl = new_decl; state->base->decl_val_to_struct[state->next_decl_id -1 ] = new_decl; state->decl_to_mod[state->next_decl_id] = module->policy;@@ -1775,6 +1769,13 @@ static int copy_avrule_block(link_state_t * state, policy_module_t * module, goto cleanup; } +if (last_decl ==NULL ) { + new_block->branch_list = new_decl; + }else { + last_decl->next = new_decl; + } + last_decl = new_decl; + state->next_decl_id++; } state->last_avrule_block->next = new_block; --2.54.0