Skip to contentSkip to conversation

bpf: Fix a crash when btf_parse_base() returns an error pointer

PATCH Applied Martin KaFai Lau 4 participants 4 messages Aug 30, 2024 BPF

lore
Share & tools
lore

Martin KaFai Lau

author reviewer
#0 lore raw

PATCH 1/1 bpf: Fix a crash when btf_parse_base() returns an error pointer

From: Martin KaFai Lau <[email protected]>

The pointer returned by btf_parse_base could be an error pointer. IS_ERR() check is needed before calling btf_free(base_btf).

Cc Alan Maguire <[email protected]>
Fixes 8646db238997 ("libbpf,bpf: Share BTF relocate-related code with kernel")

---
It was discovered in a bpf CI run that crashed in s390 due to the wrong endian in the btf header. It caused the btf_parse_base() to fail and triggered this crash.

This patch is tested in the bpf CI. It fails to load the bpf_testmod but does not crash the kernel:
https://github.com/kernel-patches/bpf/actions/runs/10623574366/job/29450422150?pr=7630

 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
index 520f49f422fe..e3377dd61f7e 100644
@@ -6283,7 +6283,7 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,
 
 errout:
 	btf_verifier_env_free(env);
-	if (base_btf != vmlinux_btf)
+	if (!IS_ERR(base_btf) && base_btf != vmlinux_btf)
 		btf_free(base_btf);
 	if (btf) {
 		kvfree(btf->data);
Signature · 1 line
2.43.5
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

The pointer returned by btf_parse_base could be an error pointer. IS_ERR() check is needed before calling btf_free(base_btf).

Cc Alan Maguire <[email protected]>
Fixes 8646db238997 ("libbpf,bpf: Share BTF relocate-related code with kernel")

---

[...]

Reviewed-by on v1 1/1
#2 lore raw
In reply to Martin KaFai Lau
Quoted 8 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

The pointer returned by btf_parse_base could be an error pointer. IS_ERR() check is needed before calling btf_free(base_btf).

Cc Alan Maguire <[email protected]>
Fixes 8646db238997 ("libbpf,bpf: Share BTF relocate-related code with kernel")

Thanks!

Alan

#3 lore raw
In reply to Martin KaFai Lau
This patch was applied to bpf/bpf.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

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

Quoted 10 lines · On Thu, 29 Aug 2024 18:22:14 -0700 you wrote:

From: Martin KaFai Lau <[email protected]>

The pointer returned by btf_parse_base could be an error pointer. IS_ERR() check is needed before calling btf_free(base_btf).

Cc Alan Maguire <[email protected]>
Fixes 8646db238997 ("libbpf,bpf: Share BTF relocate-related code with kernel")

[...]

Here is the summary with links:
- [bpf] bpf: Fix a crash when btf_parse_base() returns an error pointer

    https://git.kernel.org/bpf/bpf/c/b408473ea01b

You are awesome, thank you!

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