Fix several issues in test_progs.c
Subsystems (3)
v2 cover Fix several issues in test_progs.c
From: Feng Yang <[email protected]>
Fix several issues in test_progs.c
v2: Fix several issues raised by sashiko-bot
v1: https://lore.kernel.org/all/[email protected]/
Feng Yang (5):
selftests/bpf: Fix double free of subtest_state->name selftests/bpf: Fix missing allocation null checks in test_progs.c selftests/bpf: Use calloc to allocate subtest_states selftests/bpf: Fix memory leak on subtest_states reallocation selftests/bpf: Fix potential NULL pointer dereference in strscpy call
tools/testing/selftests/bpf/test_progs.c | 50 ++++++++++++++++++------ 1 file changed, 39 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/5 selftests/bpf: Fix double free of subtest_state->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
PATCH v2 2/5 selftests/bpf: Fix missing allocation null checks in test_progs.c
In reply to Feng YangFrom: Feng Yang <[email protected]>
Add null checks after memory allocations to prevent potential segmentation faults.
--- tools/testing/selftests/bpf/test_progs.c | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_progs.c +31 -2
@@ -730,11 +730,14 @@ int compare_map_keys(int map1_fd, int map2_fd)int compare_stack_ips (int smap_fd,int amap_fd,int stack_trace_len) {__u32 key, next_key, *cur_key_p, *next_key_p; -char *val_buf1, *val_buf2; -int i, err =0 ; +char *val_buf1 =NULL , *val_buf2 =NULL ; +int i, err = -ENOMEM ; val_buf1 =malloc (stack_trace_len); val_buf2 =malloc (stack_trace_len); +if (!val_buf1 || !val_buf2) + goto out; + err =0 ; cur_key_p =NULL ; next_key_p = &key;while (bpf_map_get_next_key (smap_fd, cur_key_p, next_key_p) ==0 ) {
@@ -1514,6 +1517,8 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)int subtest_num = state->subtest_num; state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); +if (!state->subtest_states) +return -ENOMEM ;for (int i =0 ; i < subtest_num; i++) { subtest_state = &state->subtest_states[i];
@@ -1732,9 +1737,24 @@ static void server_main(void)sigaction (SIGINT , &sigact_int,NULL ); dispatcher_threads =calloc (sizeof (pthread_t), env.workers); +if (!dispatcher_threads) { +perror ("Failed to calloc dispatcher_threads" ); +exit (EXIT_ERR_SETUP_INFRA ); + } data =calloc (sizeof (struct dispatch_data ), env.workers); +if (!data) { +perror ("Failed to calloc data" ); +free (dispatcher_threads); +exit (EXIT_ERR_SETUP_INFRA ); + } env.worker_current_test =calloc (sizeof (int ), env.workers); +if (!env.worker_current_test) { +perror ("Failed to calloc env.worker_current_test" ); +free (data); +free (dispatcher_threads); +exit (EXIT_ERR_SETUP_INFRA ); + }for (i =0 ; i < env.workers; i++) {int rc;
@@ -2094,7 +2114,16 @@ int main(int argc, char **argv) env.worker_id =-1 ;/* main process */ if (env.workers) { env.worker_pids =calloc (sizeof (pid_t), env.workers); +if (!env.worker_pids) { +perror ("Failed to calloc worker_pids" ); +return -ENOMEM ; + } env.worker_socks =calloc (sizeof (int ), env.workers); +if (!env.worker_socks) { +perror ("Failed to calloc worker_socks" ); +free (env.worker_pids); +return -ENOMEM ; + }if (env.debug)fprintf (stdout,"Launching %d workers.\n" , env.workers);for (i =0 ; i < env.workers; i++) {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 bpf-next 2/5] selftests/bpf: Fix missing allocation null checks in test_progs.c
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The newly added NULL check prevents an immediate crash but introduces a segmentation fault…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The newly added NULL check prevents an immediate crash but introduces a segmentation fault during program teardown due to inconsistent state.
- [Medium] Direct return on allocation failure in main() and server_main() bypasses the cleanup path, leaving the test kernel module loaded. --
--- Patch [2]: [PATCH v2 bpf-next 2/5] selftests/bpf: Fix missing allocation null checks in test_progs.c --- commit 5b8390b4a3f20a060d50a407c4941f4877ed4709
Author: Feng Yang <[email protected]>
selftests/bpf: Fix missing allocation null checks in test_progs.c
Add null checks after memory allocations to prevent potential segmentation faults.
Quoted 4 lines
tools/testing/selftests/bpf/test_progs.c +0
-0
[ ... ]
tools/testing/selftests/bpf/test_progs.c static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state) · line 1521
Quoted 6 lines
tools/testing/selftests/bpf/test_progs.c +2
-0@@ -1514,6 +1517,8 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)int subtest_num = state->subtest_num; state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); +if (!state->subtest_states) +return -ENOMEM ;
[Severity: Medium]
Will this lead to a segmentation fault during program teardown?
If this allocation fails, we return without clearing state->subtest_num. Later during teardown, free_test_states() loops over test_state->subtest_num and accesses test_state->subtest_states[j], which calculates an offset from a NULL array:
tools/testing/selftests/bpf/test_progs.c:free_test_states() {
...
for (j = 0; j < test_state->subtest_num; j++)
free_subtest_state(&test_state->subtest_states[j]);
...
}Should state->subtest_num be set to 0 before returning here?
[ ... ]
tools/testing/selftests/bpf/test_progs.c static void server_main(void) · line 1743
Quoted 8 lines
tools/testing/selftests/bpf/test_progs.c +4
-0@@ -1732,9 +1737,24 @@ static void server_main(void)sigaction (SIGINT , &sigact_int,NULL ); dispatcher_threads =calloc (sizeof (pthread_t), env.workers); +if (!dispatcher_threads) { +perror ("Failed to calloc dispatcher_threads" ); +exit (EXIT_ERR_SETUP_INFRA ); + }
[Severity: Medium]
Does exiting directly here leak the loaded bpf_testmod kernel module?
Normally, main() handles cleanup by going to the out label and calling unload_bpf_testmod(), but calling exit() bypasses this cleanup path.
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/test_progs.c +0
-0@@ -2094,7 +2114,16 @@ int main(int argc, char **argv) env.worker_id =-1 ;/* main process */ if (env.workers) { env.worker_pids =calloc (sizeof (pid_t), env.workers);
tools/testing/selftests/bpf/test_progs.c int main(int argc, char **argv) · line 2126
Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +9
-0+if (!env.worker_pids) { +perror ("Failed to calloc worker_pids" ); +return -ENOMEM ; + } env.worker_socks =calloc (sizeof (int ), env.workers); +if (!env.worker_socks) { +perror ("Failed to calloc worker_socks" ); +free (env.worker_pids); +return -ENOMEM ; + }
[Severity: Medium]
Could this direct return also bypass the cleanup path?
Returning directly skips the out label at the end of main(), which is responsible for unloading the bpf_testmod module if it was loaded. Should this use a goto to reach the cleanup code?
Quoted 2 lines
tools/testing/selftests/bpf/test_progs.c +0
-0if (env.debug)fprintf (stdout,"Launching %d workers.\n" , env.workers);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v2 bpf-next 2/5] selftests/bpf: Fix missing allocation null checks in test_progs.c
In reply to Feng YangThis looks like a bug fix, but there is no Fixes: tag. Most of the corrected allocation sites (dispatcher_threads, data, and env.worker_current_test in server_main(), and env.worker_pids and…
Quoted 5 lines
selftests/bpf: Fix missing allocation null checks in
test_progs.cAdd null checks after memory allocations to prevent potential segmentation faults.
Signed-off-by Feng Yang <[email protected]>
This looks like a bug fix, but there is no Fixes: tag. Most of the corrected allocation sites (dispatcher_threads, data, and env.worker_current_test in server_main(), and env.worker_pids and env.worker_socks in main()) were added without null checks when the parallel worker infrastructure went in. Should this carry:
The change also touches two allocation sites from other commits, the val_buf1/val_buf2 checks in compare_stack_ips() and the state->subtest_states check in dispatch_thread_send_subtests(), so more than one Fixes: tag may be warranted, but 91b2c0afd00c covers the bulk of the change.
---
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/29903681308
PATCH v2 3/5 selftests/bpf: Use calloc to allocate subtest_states
In reply to Feng YangFrom: Feng Yang <[email protected]>
An early return triggered by read_prog_test_msg leaves uninitialized elements, which leads to memory corruption during free_test_states cleanup.
--- tools/testing/selftests/bpf/test_progs.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/bpf/test_progs.c +1 -3
@@ -1516,15 +1516,13 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)struct subtest_state *subtest_state;int subtest_num = state->subtest_num; - state->subtest_states =malloc (subtest_num *sizeof (*subtest_state)); + state->subtest_states =calloc (subtest_num,sizeof (*subtest_state));if (!state->subtest_states)return -ENOMEM ;for (int i =0 ; i < subtest_num; i++) { subtest_state = &state->subtest_states[i]; -memset (subtest_state,0 ,sizeof (*subtest_state)); -if (read_prog_test_msg (sock_fd, &msg,MSG_SUBTEST_DONE ))return 1 ;
Signature · 1 line
2.43.0
PATCH v2 4/5 selftests/bpf: Fix memory leak on subtest_states reallocation
In reply to Feng YangFrom: Feng Yang <[email protected]>
Fix memory leak in subtest_states reallocation,
and revert subtest_num if allocation fails.
--- tools/testing/selftests/bpf/test_progs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/test_progs.c +5 -4
@@ -573,18 +573,19 @@ bool test__start_subtest_with_desc(const char *subtest_name, const char *subteststruct subtest_state *subtest_state;const char *subtest_display_name;size_t sub_state_size =sizeof (*subtest_state); +void *tmp;if (env.subtest_state)test__end_subtest (); state->subtest_num++; - state->subtest_states = -realloc (state->subtest_states, - state->subtest_num * sub_state_size); -if (!state->subtest_states) { + tmp =realloc (state->subtest_states, state->subtest_num * sub_state_size); +if (!tmp) { + state->subtest_num--;fprintf (stderr,"Not enough memory to allocate subtest result\n" );return false; } + state->subtest_states = tmp; subtest_state = &state->subtest_states[state->subtest_num -1 ];
Signature · 1 line
2.43.0
PATCH v2 5/5 selftests/bpf: Fix potential NULL pointer dereference in strscpy call
In reply to Feng YangFrom: Feng Yang <[email protected]>
In test__start_subtest_with_desc, if strdup fails or an empty name is passed, the function returns early. Later, worker_main_send_subtests copies the name via strscpy, which leads to a NULL pointer dereference.
--- tools/testing/selftests/bpf/test_progs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/test_progs.c +2 -1
@@ -1882,7 +1882,8 @@ static int worker_main_send_subtests(int sock, struct test_state *state) msg.subtest_done.num = i; -strscpy (msg.subtest_done.name, subtest_state->name,MAX_SUBTEST_NAME ); +const char *name = subtest_state->name ? :"" ; +strscpy (msg.subtest_done.name, name,MAX_SUBTEST_NAME ); msg.subtest_done.error_cnt = subtest_state->error_cnt; msg.subtest_done.skipped = subtest_state->skipped;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 bpf-next 5/5] selftests/bpf: Fix potential NULL pointer dereference in strscpy call
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] The patch prevents a NULL pointer dereference in the worker thread by replacing a NULL subtest_state->name with an empty string, but misses other instances where a NULL subtest_state->name causes undefined behavior or crashes (e.g., in single-process mode and server thread IPC parsing). --
--- Patch [5]: [PATCH v2 bpf-next 5/5] selftests/bpf: Fix potential NULL pointer dereference in strscpy call --- commit b6c49e61911a21bc238d913cde0b42bf364476d1
Author: Feng Yang <[email protected]>
selftests/bpf: Fix potential NULL pointer dereference in strscpy call
In test__start_subtest_with_desc, if strdup fails or an empty name is passed, the function returns early. Later, worker_main_send_subtests copies the name via strscpy, which leads to a NULL pointer dereference.
tools/testing/selftests/bpf/test_progs.c static int worker_main_send_subtests(int sock, struct test_state *state) · line 1886
Quoted 11 lines
tools/testing/selftests/bpf/test_progs.c +2
-1@@ -1882,7 +1882,8 @@ static int worker_main_send_subtests(int sock, struct test_state *state) msg.subtest_done.num = i; -strscpy (msg.subtest_done.name, subtest_state->name,MAX_SUBTEST_NAME ); +const char *name = subtest_state->name ? :"" ; +strscpy (msg.subtest_done.name, name,MAX_SUBTEST_NAME );
[Severity: Medium]
This isn't a bug introduced by this patch, but since we are handling a NULL subtest_state->name here, could a NULL name also cause issues in other places?
For example, in single-process mode, print_subtest_name() receives a NULL subtest_name and passes it directly to fprintf() as a %s format argument, triggering undefined behavior:
tools/testing/selftests/bpf/test_progs.c:print_subtest_name() {
...
fprintf(env.stdout_saved, "#%-*s %s/%s",
TEST_NUM_WIDTH, test_num_str,
test_name, subtest_name);
...
}When JSON output is requested, dump_test_log() passes it to the JSON writer:
tools/testing/selftests/bpf/test_progs.c:dump_test_log() {
...
jsonw_string_field(w, "name", subtest_state->name);
...
}This leads to a crash when jsonw_puts() dereferences it unconditionally:
tools/bpf/bpftool/json_writer.c:jsonw_puts() {
...
for (; *str; ++str)
...
}Also, could a strdup() failure in dispatch_thread_send_subtests() lead to subtest_state->name being NULL again on the server side?
tools/testing/selftests/bpf/test_progs.c:dispatch_thread_send_subtests() {
...
subtest_state->name = strdup(msg.subtest_done.name);
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Quoted 9 lines · On Wed, 22 Jul 2026 15:47:43 +0800 you wrote:
From: Feng Yang <[email protected]>
Fix several issues in
test_progs.cv2: Fix several issues raised by sashiko-bot
v1: https://lore.kernel.org/all/[email protected]/
[...]
Here is the summary with links:
- [v2,bpf-next,1/5] selftests/bpf: Fix double free of subtest_state->name
https://git.kernel.org/bpf/bpf-next/c/791841e038c4 - [v2,bpf-next,2/5] selftests/bpf: Fix missing allocation null checks in test_progs.c (no matching commit) - [v2,bpf-next,3/5] selftests/bpf: Use calloc to allocate subtest_states (no matching commit) - [v2,bpf-next,4/5] selftests/bpf: Fix memory leak on subtest_states reallocation https://git.kernel.org/bpf/bpf-next/c/06efb01c6530
- [v2,bpf-next,5/5] selftests/bpf: Fix potential NULL pointer dereference in strscpy call (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html