Skip to contentSkip to conversation

libsepol: Fix double free in copy_avrule_block

PATCH In review James Carter 2 participants 3 messages May 21 – May 26 SELinux security module

lore
Share & tools
lore
#0 lore raw

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
index 188b4020..e0da6575 100644
@@ -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
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · James Carter

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().

Quoted 39 lines
---
 libsepol/src/link.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
libsepol/src/link.c +7 -7
index 188b4020..e0da6575 100644
@@ -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

maintainer
#2 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 6 lines · James Carter

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.

Quoted 6 lines
Quoted 4 lines

Instead, just link new_decl into new_block after the call to copy_avrule_decl().

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
index 188b4020..e0da6575 100644
@@ -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