selftests/bpf: Fix double free of name
Subsystems (3)
PATCH 1/1 selftests/bpf: Fix double free of name
From: Feng Yang <[email protected]>
The name has already been freed in the free_subtest_state function and does not need to be freed again.
--- tools/testing/selftests/bpf/test_progs.c | 1 - 1 file changed, 1 deletion(-)
tools/testing/selftests/bpf/test_progs.c +0 -1
@@ -1886,7 +1886,6 @@ static int worker_main_send_subtests(int sock, struct test_state *state)worker_main_send_log (sock, subtest_state->log_buf, subtest_state->log_cnt);free_subtest_state (subtest_state); -free (subtest_state->name); } out:
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [Medium] Missing NULL check for malloc() return value when allocating subtest_states array in dispatch_thread_send_subtests().
- [Medium] Early return in dispatch_thread_send_subtests() leaves array elements uninitialized, leading to memory corruption when freed.
- [Medium] realloc() failure in test__start_subtest_with_desc causes a memory leak and subsequent NULL pointer dereference.
- [Medium] Missing checks for NULL subtest name in test__start_subtest_with_desc() lead to a segfault via strscpy(). --
--- Patch [1]: [PATCH bpf-next] selftests/bpf: Fix double free of name --- commit 297db6a341af110e628f3b4e73b2087ba183e417
Author: Feng Yang <[email protected]>
selftests/bpf: Fix double free of name
This commit removes an unnecessary free of subtest_state->name in worker_main_send_subtests to fix a double-free issue, since it is already handled by free_subtest_state.
tools/testing/selftests/bpf/test_progs.c static int worker_main_send_subtests(int sock, struct test_state *state) · line 1891
Quoted 12 lines
tools/testing/selftests/bpf/test_progs.c +0
-1@@ -1886,7 +1886,6 @@ static int worker_main_send_subtests(int sock, struct test_state *state)worker_main_send_log (sock, subtest_state->log_buf, subtest_state->log_cnt);free_subtest_state (subtest_state); -free (subtest_state->name); } out:
This is a pre-existing issue, but looking at dispatch_thread_send_subtests, there doesn't seem to be a NULL check after allocating subtest_states:
tools/testing/selftests/bpf/test_progs.c:dispatch_thread_send_subtests() {
...
state->subtest_states = malloc(subtest_num * sizeof(*subtest_state));
for (int i = 0; i < subtest_num; i++) {
subtest_state = &state->subtest_states[i];
memset(subtest_state, 0, sizeof(*subtest_state));
...
}[Severity: Medium]
If malloc fails, could this cause a segmentation fault when the loop attempts to memset the address relative to NULL?
Additionally, if read_prog_test_msg returns early:
tools/testing/selftests/bpf/test_progs.c:dispatch_thread_send_subtests() {
...
memset(subtest_state, 0, sizeof(*subtest_state));
if (read_prog_test_msg(sock_fd, &msg, MSG_SUBTEST_DONE))
return 1;
...
}[Severity: Medium]
This is a pre-existing issue, but since the array was allocated with malloc rather than calloc, does this early return leave the remaining array elements uninitialized? If so, when free_test_states iterates up to subtest_num and calls free on state->name or state->log_buf, could it cause memory corruption by freeing garbage pointers?
This isn't a bug introduced by this patch, but looking at test__start_subtest_with_desc, the realloc pattern appears to overwrite the pointer on failure:
tools/testing/selftests/bpf/test_progs.c:test__start_subtest_with_desc() {
...
state->subtest_states =
realloc(state->subtest_states,
state->subtest_num * sub_state_size);
if (!state->subtest_states) {
fprintf(stderr, "Not enough memory to allocate subtest result\n");
return false;
}
...
}[Severity: Medium]
If realloc fails, does this overwrite the only pointer to the allocated memory, causing a leak? Furthermore, since state->subtest_num was already incremented without being rolled back, could the loop in worker_main_send_subtests later dereference the NULL array?
This is a pre-existing issue, but if strdup fails or an empty name is provided in test__start_subtest_with_desc, it returns early:
tools/testing/selftests/bpf/test_progs.c:test__start_subtest_with_desc() {
...
subtest_state->name = strdup(subtest_display_name);
if (!subtest_state->name) {
fprintf(env.stderr_saved,
"Subtest #%d: failed to copy subtest name!\n",
state->subtest_num);
return false;
}
...
}[Severity: Medium]
Because state->subtest_num is already incremented before this early return, the subtest state is left partially initialized with name as NULL. When worker_main_send_subtests later unconditionally copies the name:
tools/testing/selftests/bpf/test_progs.c:worker_main_send_subtests() {
...
strscpy(msg.subtest_done.name, subtest_state->name, MAX_SUBTEST_NAME);
...
}Could this result in a NULL pointer dereference, as strscpy does not validate the source pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThe redundant free() looks like it was introduced together with free_subtest_state() in the same commit. Should this include a Fixes: tag?
Quoted 6 lines
selftests/bpf: Fix double free of name
The name has already been freed in the
free_subtest_statefunction and does not need to be freed again.Signed-off-by Feng Yang <[email protected]>
The redundant free() looks like it was introduced together with free_subtest_state() in the same commit. Should this include a Fixes: tag?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/29821735776