Skip to contentSkip to conversation

bpf: Fix memory leak in __lookup_instance error path

PATCH Applied Shardul Bankar 3 participants 3 messages Oct 16, 2025 BPF

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Fix memory leak in __lookup_instance error path

When __lookup_instance() allocates a func_instance structure but fails to allocate the must_write_set array, it returns an error without freeing the previously allocated func_instance. This causes a memory leak of 192 bytes (sizeof(struct func_instance)) each time this error path is triggered.

Fix by freeing 'result' on must_write_set allocation failure.

Fixes b3698c356ad9 ("bpf: callchain sensitive stack liveness tracking using CFG")
Reported-by BPF Runtime Fuzzer (BRF)

v2: Resend with complete CC list.

---
 kernel/bpf/liveness.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/liveness.c +3 -1
index 3c611aba7f52..1e6538f59a78 100644
@@ -195,8 +195,10 @@ static struct func_instance *__lookup_instance(struct bpf_verifier_env *env,
 		return ERR_PTR(-ENOMEM);
 	result->must_write_set = kvcalloc(subprog_sz, sizeof(*result->must_write_set),
 					  GFP_KERNEL_ACCOUNT);
-	if (!result->must_write_set)
+	if (!result->must_write_set) {
+		kvfree(result);
 		return ERR_PTR(-ENOMEM);
+	}
 	memcpy(&result->callchain, callchain, sizeof(*callchain));
 	result->insn_cnt = subprog_sz;
 	hash_add(liveness->func_instances, &result->hl_node, key);
Signature · 1 line
2.34.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Shardul Bankar

When __lookup_instance() allocates a func_instance structure but fails to allocate the must_write_set array, it returns an error without freeing the previously allocated func_instance. This causes a memory leak of 192 bytes (sizeof(struct func_instance)) each time this error path is triggered.

Quoted 7 lines

Fix by freeing 'result' on must_write_set allocation failure.

Fixes b3698c356ad9 ("bpf: callchain sensitive stack liveness tracking using CFG")
Reported-by BPF Runtime Fuzzer (BRF)

v2: Resend with complete CC list.
---

Thank you for the fix!

[...]

#2 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 8 lines · On Thu, 16 Oct 2025 12:03:30 +0530 you wrote:

When __lookup_instance() allocates a func_instance structure but fails to allocate the must_write_set array, it returns an error without freeing the previously allocated func_instance. This causes a memory leak of 192 bytes (sizeof(struct func_instance)) each time this error path is triggered.

Fix by freeing 'result' on must_write_set allocation failure.

[...]

Here is the summary with links:
- [v2,bpf] bpf: Fix memory leak in __lookup_instance error path

    https://git.kernel.org/bpf/bpf/c/8adc4705e46c

You are awesome, thank you!

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