Skip to contentSkip to conversation

bpf: test_run: fix ctx leak in bpf_prog_test_run_xdp error path

PATCH Applied Shardul Bankar 4 participants 4 messages Oct 14, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: test_run: fix ctx leak in bpf_prog_test_run_xdp error path

Fix a memory leak in bpf_prog_test_run_xdp() where the context buffer allocated by bpf_ctx_init() is not freed when the function returns early due to a data size check.

On the failing path:
ctx = bpf_ctx_init(...);
if (kattr->test.data_size_in - meta_sz < ETH_HLEN)

      return -EINVAL;

The early return bypasses the cleanup label that kfree()s ctx, leading to a leak detectable by kmemleak under fuzzing. Change the return to jump to the existing free_ctx label.

Fixes fe9544ed1a2e ("bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN")
Reported-by BPF Runtime Fuzzer (BRF)
---
 net/bpf/test_run.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bpf/test_run.c +1 -1
index dfb03ee0bb62..1782e83de2cb 100644
@@ -1269,7 +1269,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 		goto free_ctx;
 
 	if (kattr->test.data_size_in - meta_sz < ETH_HLEN)
-		return -EINVAL;
+		goto free_ctx;
 
 	data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
 	if (IS_ERR(data)) {
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Shardul Bankar

Fix a memory leak in bpf_prog_test_run_xdp() where the context buffer allocated by bpf_ctx_init() is not freed when the function returns early due to a data size check.

On the failing path:
ctx = bpf_ctx_init(...);
if (kattr->test.data_size_in - meta_sz < ETH_HLEN)

      return -EINVAL;
Quoted 7 lines

The early return bypasses the cleanup label that kfree()s ctx, leading to a leak detectable by kmemleak under fuzzing. Change the return to jump to the existing free_ctx label.

Fixes fe9544ed1a2e ("bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN")
Reported-by BPF Runtime Fuzzer (BRF)

jirka

Quoted 20 lines
---
 net/bpf/test_run.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bpf/test_run.c +1 -1
index dfb03ee0bb62..1782e83de2cb 100644
@@ -1269,7 +1269,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 		goto free_ctx;
 
 	if (kattr->test.data_size_in - meta_sz < ETH_HLEN)
-		return -EINVAL;
+		goto free_ctx;
 
 	data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
 	if (IS_ERR(data)) {
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Shardul Bankar
Quoted 9 lines · Shardul Bankar

Fix a memory leak in bpf_prog_test_run_xdp() where the context buffer allocated by bpf_ctx_init() is not freed when the function returns early due to a data size check.

On the failing path:

   ctx = bpf_ctx_init(...);
   if (kattr->test.data_size_in - meta_sz < ETH_HLEN)
       return -EINVAL;
Quoted 7 lines

The early return bypasses the cleanup label that kfree()s ctx, leading to a leak detectable by kmemleak under fuzzing. Change the return to jump to the existing free_ctx label.

Fixes fe9544ed1a2e ("bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN")
Reported-by BPF Runtime Fuzzer (BRF)
#3 lore raw
In reply to Shardul Bankar
This patch was applied to bpf/bpf.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

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

Quoted 10 lines · On Tue, 14 Oct 2025 17:30:37 +0530 you wrote:

Fix a memory leak in bpf_prog_test_run_xdp() where the context buffer allocated by bpf_ctx_init() is not freed when the function returns early due to a data size check.

On the failing path:
ctx = bpf_ctx_init(...);
if (kattr->test.data_size_in - meta_sz < ETH_HLEN)

      return -EINVAL;

[...]

Here is the summary with links:
- [bpf,1/1] bpf: test_run: fix ctx leak in bpf_prog_test_run_xdp error path

    https://git.kernel.org/bpf/bpf/c/7f9ee5fc97e1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html