The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH bpf-next v6] libbpf: avoid overflow in BTF.ext bounds check
@ 2026-08-14  0:43 Darren Carreras via B4 Relay
  2026-08-21 19:10 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Darren Carreras via B4 Relay @ 2026-08-14  0:43 UTC (permalink / raw)
  To: bpf
  Cc: daniel, song, yonghong.song, linux-kernel, jolsa, andrii.nakryiko,
	ast, eddyz87, emil, andrii, memxor, ihor.solodrai, martin.lau

From: Darren Carreras <carrerasdarren@gmail.com>

A malformed BTF.ext subsection length in an ELF input can wrap the
pointer addition used by btf_ext_parse_sec_info() on 32-bit builds. The
wrapped pointer passes the bounds check and parsing then reads beyond the
copied BTF.ext data.

Ensure the header fits in the copied data, then validate the subsection
offset and length with subtraction before forming its pointer.

Fixes: ae4ab4b4117d ("btf: expose API to work with raw btf_ext data")
Closes: https://issues.oss-fuzz.com/issues/477315119
Signed-off-by: Darren Carreras <carrerasdarren@gmail.com>
---
Changes in v6:
- Post the patch inline rather than as an attachment.
- Guard hdr_len locally before subtracting it from data_size.
- Clarify that the input is a malformed BTF.ext subsection in an ELF file.

Changes in v5:
- Resend from a clean compose window because Gmail retained a stale rich-text
  copy beside the v4 attachment, producing a duplicated malformed patch body.

Changes in v4:
- Drop the selftest because the malformed length is already rejected by the
  old check on 64-bit CI; the behavioral divergence is specific to 32-bit.
- Correct the Fixes tag to the commit that introduced the pointer-based check.

Changes in v3:
- Remove the nested mbox envelope and mail headers from the Gmail attachment
  so Patchwork's generated mbox applies with git am.

Changes in v2:
- Resend as plain text because Gmail mangled the v1 diff and Patchwork
  reported "Patch is empty."
- Include the authorized DCO Signed-off-by line.
---
 tools/lib/bpf/btf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 8417de92d..c78335997 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -3364,7 +3364,7 @@ static int btf_ext_parse_sec_info(struct btf_ext *btf_ext,
 {
 	const struct btf_ext_info_sec *sinfo;
 	struct btf_ext_info *ext_info;
-	__u32 info_left, record_size;
+	__u32 data_left, info_left, record_size;
 	size_t sec_cnt = 0;
 	void *info;
 
@@ -3377,16 +3377,20 @@ static int btf_ext_parse_sec_info(struct btf_ext *btf_ext,
 		return -EINVAL;
 	}
 
-	/* The start of the info sec (including the __u32 record_size). */
-	info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
-	info_left = ext_sec->len;
+	if (btf_ext->hdr->hdr_len > btf_ext->data_size)
+		return -EINVAL;
 
-	if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
+	data_left = btf_ext->data_size - btf_ext->hdr->hdr_len;
+	if (ext_sec->off > data_left || ext_sec->len > data_left - ext_sec->off) {
 		pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
 			 ext_sec->desc, ext_sec->off, ext_sec->len);
 		return -EINVAL;
 	}
 
+	/* The start of the info sec (including the __u32 record_size). */
+	info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
+	info_left = ext_sec->len;
+
 	/* At least a record size */
 	if (info_left < sizeof(__u32)) {
 		pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);

---
base-commit: 4d9551b39aeb5d32beb4d795ce2892e7690a3e93
change-id: 20260813-b4-libbpf-v6-send-20260813-ad79e6943a68

Best regards,
--  
Darren Carreras <carrerasdarren@gmail.com>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next v6] libbpf: avoid overflow in BTF.ext bounds check
  2026-08-14  0:43 [PATCH bpf-next v6] libbpf: avoid overflow in BTF.ext bounds check Darren Carreras via B4 Relay
@ 2026-08-21 19:10 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-08-21 19:10 UTC (permalink / raw)
  To: Darren Carreras
  Cc: bpf, daniel, song, yonghong.song, linux-kernel, jolsa,
	andrii.nakryiko, ast, eddyz87, emil, andrii, memxor,
	ihor.solodrai, martin.lau

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 13 Aug 2026 20:43:18 -0400 you wrote:
> From: Darren Carreras <carrerasdarren@gmail.com>
> 
> A malformed BTF.ext subsection length in an ELF input can wrap the
> pointer addition used by btf_ext_parse_sec_info() on 32-bit builds. The
> wrapped pointer passes the bounds check and parsing then reads beyond the
> copied BTF.ext data.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v6] libbpf: avoid overflow in BTF.ext bounds check
    https://git.kernel.org/bpf/bpf-next/c/e40f61a31688

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-21 19:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-14  0:43 [PATCH bpf-next v6] libbpf: avoid overflow in BTF.ext bounds check Darren Carreras via B4 Relay
2026-08-21 19:10 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox