Skip to contentSkip to conversation

bpf: correctly free bpf_scc_info objects referenced in env->scc_info

PATCH Applied Eduard Zingerman 4 participants 4 messages Aug 1, 2025 – Aug 2, 2025

Subsystems (2)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH 1/1 bpf: correctly free bpf_scc_info objects referenced in env->scc_info

env->scc_info array contains references to bpf_scc_info objects allocated lazily in verifier.c:scc_visit_alloc(). env->scc_cnt was supposed to track env->scc_info array size in order to free referenced objects in verifier.c:free_states(). Initialization of env->scc_cnt was omitted in verifier.c:compute_scc(), which is fixed by this commit.

To reproduce the bug:
- build with CONFIG_DEBUG_KMEMLEAK
- boot and load bpf program with loops, e.g.:
./veristat -q pyperf180.bpf.o
- initiate memleak scan and check results:
echo scan > /sys/kernel/debug/kmemleak
cat /sys/kernel/debug/kmemleak

---
 kernel/bpf/verifier.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/verifier.c +3 -0
index 0806295945e4..c4f69a9e9af6 100644
@@ -23114,6 +23114,8 @@ static void free_states(struct bpf_verifier_env *env)
 
 	for (i = 0; i < env->scc_cnt; ++i) {
 		info = env->scc_info[i];
+		if (!info)
+			continue;
 		for (j = 0; j < info->num_visits; j++)
 			free_backedges(&info->visits[j]);
 		kvfree(info);
@@ -24554,6 +24556,7 @@ static int compute_scc(struct bpf_verifier_env *env)
 		err = -ENOMEM;
 		goto exit;
 	}
+	env->scc_cnt = next_scc_id;
 exit:
 	kvfree(stack);
 	kvfree(pre);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 7 lines · Eduard Zingerman

env->scc_info array contains references to bpf_scc_info objects allocated lazily in verifier.c:scc_visit_alloc(). env->scc_cnt was supposed to track env->scc_info array size in order to free referenced objects in verifier.c:free_states(). Initialization of env->scc_cnt was omitted in verifier.c:compute_scc(), which is fixed by this commit.

Applied to bpf tree and slightly reworded above commit log and subject to use imperative language.

#2 lore raw
In reply to Eduard Zingerman
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Fri, 1 Aug 2025 16:23:30 -0700 you wrote:

env->scc_info array contains references to bpf_scc_info objects allocated lazily in verifier.c:scc_visit_alloc(). env->scc_cnt was supposed to track env->scc_info array size in order to free referenced objects in verifier.c:free_states(). Initialization of env->scc_cnt was omitted in verifier.c:compute_scc(), which is fixed by this commit.

[...]

Here is the summary with links:
- [bpf,v1] bpf: correctly free bpf_scc_info objects referenced in env->scc_info

    https://git.kernel.org/bpf/bpf/c/989705e34ad3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Tested-by on v1 1/1
#3 lore raw
In reply to Eduard Zingerman
Quoted 7 lines · Eduard Zingerman

env->scc_info array contains references to bpf_scc_info objects allocated lazily in verifier.c:scc_visit_alloc(). env->scc_cnt was supposed to track env->scc_info array size in order to free referenced objects in verifier.c:free_states(). Initialization of env->scc_cnt was omitted in verifier.c:compute_scc(), which is fixed by this commit.

Quoted 7 lines

To reproduce the bug:
- build with CONFIG_DEBUG_KMEMLEAK
- boot and load bpf program with loops, e.g.:
./veristat -q pyperf180.bpf.o
- initiate memleak scan and check results:
echo scan > /sys/kernel/debug/kmemleak
cat /sys/kernel/debug/kmemleak

Thanks for fixing this. Even though it's already applied, I did test it:

Signature · 1 line
Jens Axboe