All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Feng Yang" <yangfeng59949@163.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v3 bpf-next 4/5] selftests/bpf: Use calloc to allocate subtest_states
Date: Thu, 23 Jul 2026 09:28:47 +0000	[thread overview]
Message-ID: <20260723092855.0EAB01F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260723085100.482147-5-yangfeng59949@163.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Memory leak and NULL pointer dereference in `test__start_subtest_with_desc()` on `realloc()` failure.
- [High] Protocol errors or worker crashes during log/subtest retrieval cause tests to be falsely reported as successful.
--

--- Patch [4]: [PATCH v3 bpf-next 4/5] selftests/bpf: Use calloc to allocate subtest_states ---
commit c27dc00f23d100647c7fe316391aa3eb9b2496b5
Author: Feng Yang <yangfeng@kylinos.cn>

selftests/bpf: Use calloc to allocate subtest_states
    
An early return triggered by read_prog_test_msg leaves uninitialized elements,
which leads to memory corruption during free_test_states cleanup.

> diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> index 301c6e11ceafa..07da45230c4bf 100644
> --- a/tools/testing/selftests/bpf/test_progs.c
> +++ b/tools/testing/selftests/bpf/test_progs.c
> @@ -1516,7 +1516,7 @@ 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));

[Severity: High]
This is a pre-existing issue, but is it possible to leak state->subtest_states
and cause a NULL pointer dereference in test__start_subtest_with_desc() on a
realloc() failure?

If realloc() fails, it returns NULL which overwrites the only pointer to
the existing array, leaking the previously allocated memory:

tools/testing/selftests/bpf/test_progs.c:test__start_subtest_with_desc() {
...
	state->subtest_num++;
	state->subtest_states =
		realloc(state->subtest_states,
			state->subtest_num * sub_state_size);
...
}

Because state->subtest_num is incremented before the failure, the cleanup
code in free_test_states() will attempt to iterate over the now-NULL
subtest_states 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]);
...
}

Can this compute an invalid address and cause a segmentation fault when trying
to free the test states?

>  	if (!state->subtest_states) {
>  		state->subtest_num = 0;
>  		return -ENOMEM;
> @@ -1525,8 +1525,6 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
>  	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;

[Severity: High]
This isn't a bug introduced by this patch, but what happens if a protocol error
or a worker crash occurs during log or subtest retrieval?

In dispatch_thread(), the test is marked as successful before all of its
logs and subtest states are collected:

tools/testing/selftests/bpf/test_progs.c:dispatch_thread() {
...
			state = &test_states[test_to_run];
			state->tested = true;
			state->error_cnt = msg.test_done.error_cnt;
...
			if (dispatch_thread_send_subtests(sock_fd, state))
				goto error;
...
}

If dispatch_thread_send_subtests() returns 1 because read_prog_test_msg()
fails (e.g., due to a worker crash or a closed socket), the dispatcher thread
jumps to the error label and exits.

Because state->tested is already true and error_cnt is 0 (assuming the main
test passed before the crash), calculate_summary_and_print_errors() adds the
test to succ_cnt. Does this falsely report the incomplete or crashed test as
successful and mask test suite failures?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260723085100.482147-1-yangfeng59949@163.com?part=4

  reply	other threads:[~2026-07-23  9:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23  8:50 [PATCH v3 bpf-next 0/5] selftests/bpf: Fix several issues in test_progs.c Feng Yang
2026-07-23  8:50 ` [PATCH v3 bpf-next 1/5] selftests/bpf: Fix double free of subtest_state->name Feng Yang
2026-07-23  8:50 ` [PATCH v3 bpf-next 2/5] selftests/bpf: Fix incorrect error checking for pthread_create Feng Yang
2026-07-23  8:50 ` [PATCH v3 bpf-next 3/5] selftests/bpf: Fix missing allocation null checks in test_progs.c Feng Yang
2026-07-23  9:17   ` sashiko-bot
2026-07-23  8:50 ` [PATCH v3 bpf-next 4/5] selftests/bpf: Use calloc to allocate subtest_states Feng Yang
2026-07-23  9:28   ` sashiko-bot [this message]
2026-07-23  9:48   ` bot+bpf-ci
2026-07-23  8:51 ` [PATCH v3 bpf-next 5/5] selftests/bpf: Fix memory leak on subtest_states reallocation Feng Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260723092855.0EAB01F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yangfeng59949@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.