All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: Fix type size overflow in btf_type_size_unknown
@ 2026-04-22 10:32 Alan Maguire
  2026-04-22 14:19 ` Mykyta Yatsenko
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Alan Maguire @ 2026-04-22 10:32 UTC (permalink / raw)
  To: andrii, ast
  Cc: daniel, martin.lau, memxor, song, yonghong.song, jolsa, bpf,
	Alan Maguire, sashiko

A layout section with a large elem_sz/vlen can lead to a 32-bit
overflow in btf_parse_type_sec() at the point where a large
type_size is added to the next_type [1]; a sufficiently large type_size
on a 32-bit system could lead to the bounds check

	if (next_type + type_size > end_type)

not triggering (since next_type + type_size wraps to a value low
in the 32-bit address space).  This would lead to a bad value for
next_type when we add the type size.  Avoid this by ensuring that
the type_size we return from btf_type_size_unknown() falls within
the range of <current type, end_type>.

[1] https://lore.kernel.org/bpf/20260417170712.74E77C19425@smtp.kernel.org/

Reported-by: sashiko <sashiko-bot@kernel.org>
Fixes: 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/lib/bpf/btf.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index ceb57b46a878..d48ac5460d1d 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -420,9 +420,11 @@ static int btf_parse_layout_sec(struct btf *btf)
 static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *t)
 {
 	__u32 l_cnt = btf->hdr.layout_len / sizeof(struct btf_layout);
+	const void *end_type = btf->types_data + btf->hdr.type_len;
 	struct btf_layout *l = btf->layout;
 	__u16 vlen = btf_vlen(t);
 	__u32 kind = btf_kind(t);
+	int type_size;
 
 	/* Fall back to base BTF if needed as they share layout information */
 	if (!l) {
@@ -448,7 +450,13 @@ static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *t
 		return -EINVAL;
 	}
 
-	return sizeof(struct btf_type) + l[kind].info_sz + vlen * l[kind].elem_sz;
+	type_size = sizeof(struct btf_type) + l[kind].info_sz + vlen * l[kind].elem_sz;
+	if ((size_t)type_size > (size_t)(end_type - (const void *)t)) {
+		pr_debug("Overflow in type size %d for kind %u\n",
+			 type_size, kind);
+		return -EINVAL;
+	}
+	return type_size;
 }
 
 static int btf_type_size(const struct btf *btf, const struct btf_type *t)
-- 
2.39.3


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

end of thread, other threads:[~2026-04-23 16:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-22 10:32 [PATCH bpf] libbpf: Fix type size overflow in btf_type_size_unknown Alan Maguire
2026-04-22 14:19 ` Mykyta Yatsenko
2026-04-22 15:07   ` Alan Maguire
2026-04-22 15:10     ` Alexei Starovoitov
2026-04-22 19:10 ` sashiko-bot
2026-04-22 21:22 ` Andrii Nakryiko
2026-04-23 16:45   ` Alan Maguire

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.