libbpf: Fix type size overflow in btf_type_size_unknown
PATCH 1/1 libbpf: Fix type size overflow in btf_type_size_unknown
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/[email protected]/
--- tools/lib/bpf/btf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +9 -1
@@ -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 *treturn -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)
Signature · 1 line
2.39.3
Quoted 13 lines · Alan Maguire
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 largetype_sizeis added to thenext_type[1]; a sufficiently largetype_sizeon a 32-bit system could lead to the bounds checkif (
next_type+type_size>end_type)not triggering (since
next_type+type_sizewraps to a value low in the 32-bit address space). This would lead to a bad value fornext_typewhen we add the type size. Avoid this by ensuring that thetype_sizewe return frombtf_type_size_unknown()falls within the range of <current type,end_type>.
Quoted 6 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Reported-by sashiko <[email protected]>Fixes 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")Signed-off-by Alan Maguire <[email protected]>---
Beginner question: how does targeting bpf tree helps libbpf patch, it's not available unless we sync to github and release a new libbpf version, isn't it? I would rebase this on bpf-next: it does not apply cleanly on bpf-next and dealing with conflicts for this change does not sound amazing.
Quoted 34 lines
tools/lib/bpf/btf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)tools/lib/bpf/btf.c +9
-1@@ -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 *treturn -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)
Quoted 16 lines · Mykyta Yatsenko
Quoted 15 lines · Alan Maguire
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 largetype_sizeis added to thenext_type[1]; a sufficiently largetype_sizeon a 32-bit system could lead to the bounds checkif (
next_type+type_size>end_type)not triggering (since
next_type+type_sizewraps to a value low in the 32-bit address space). This would lead to a bad value fornext_typewhen we add the type size. Avoid this by ensuring that thetype_sizewe return frombtf_type_size_unknown()falls within the range of <current type,end_type>.
Quoted 8 lines
Quoted 4 lines
Reported-by sashiko <[email protected]>Fixes 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")Signed-off-by Alan Maguire <[email protected]>---
Beginner question: how does targeting bpf tree helps libbpf patch, it's not available unless we sync to github and release a new libbpf version, isn't it? I would rebase this on bpf-next: it does not apply cleanly on bpf-next and dealing with conflicts for this change does not sound amazing.
Sure, happy to do this if it makes things easier. In this case the bug did exist prior to the vlen expansion work, but only for a short period of time. As I understand it bpf fixes are resynced regularly with bpf-next, but that does require merging. In this case the merge should be straightforward I think, but if maintainers would prefer a bpf-next-targeted patch I'll do that instead. Thanks!
Quoted 32 lines
Quoted 31 lines
tools/lib/bpf/btf.c| 10 +++++++++-1 file changed, 9 insertions(+), 1 deletion(-)tools/lib/bpf/btf.c +9
-0@@ -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)
Quoted 27 lines · Alan Maguire
Quoted 16 lines · Mykyta Yatsenko
Quoted 15 lines · Alan Maguire
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 largetype_sizeis added to thenext_type[1]; a sufficiently largetype_sizeon a 32-bit system could lead to the bounds checkif (
next_type+type_size>end_type)not triggering (since
next_type+type_sizewraps to a value low in the 32-bit address space). This would lead to a bad value fornext_typewhen we add the type size. Avoid this by ensuring that thetype_sizewe return frombtf_type_size_unknown()falls within the range of <current type,end_type>.Quoted 8 lines
Quoted 4 lines
Reported-by sashiko <[email protected]>Fixes 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")Signed-off-by Alan Maguire <[email protected]>---
Beginner question: how does targeting bpf tree helps libbpf patch, it's not available unless we sync to github and release a new libbpf version, isn't it? I would rebase this on bpf-next: it does not apply cleanly on bpf-next and dealing with conflicts for this change does not sound amazing.
Quoted 5 lines
Sure, happy to do this if it makes things easier. In this case the bug did exist prior to the vlen expansion work, but only for a short period of time. As I understand it bpf fixes are resynced regularly with bpf-next, but that does require merging. In this case the merge should be straightforward I think, but if maintainers would prefer a bpf-next-targeted patch I'll do that instead. Thanks!
Mykyta's point is that 'bpf' tree is for kernel fixes. We send libbpf fixes to bpf tree only when they're part of kernel fixes and they have to go together.
Not the case here.
pw-bot: cr
[email protected]
AI reviewSashiko AI review found 2 potential issue(s): - [High] This problem wasn't introduced by this patch, but the patch attempts to fix a pointer wrap-around vulnerability and leaves it completely…
Sashiko AI review found 2 potential issue(s):
- [High] This problem wasn't introduced by this patch, but the patch attempts to fix a pointer wrap-around vulnerability and leaves it completely unfixed for standard BTF kinds.
- [Medium] The bounds check added in btf_type_size_unknown() performs pointer subtraction between unrelated memory allocations, leading to C Undefined Behavior. --
commit ac478ac510f0298a2cc2458f409eff3e04fdd3b5
Author: Alan Maguire <[email protected]>
libbpf: Fix type size overflow in btf_type_size_unknown
This commit attempts to fix a 32-bit pointer arithmetic wrap-around in btf_parse_type_sec() by adding a bounds check in btf_type_size_unknown(). This aims to prevent a large type_size from wrapping when added to next_type, which would bypass the existing bounds checks.
Quoted 16 lines
tools/lib/bpf/btf.c +2
-0@@ -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) {
[ ... ]
tools/lib/bpf/btf.c static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *t · line 454
Quoted 7 lines
tools/lib/bpf/btf.c +2
-1@@ -448,7 +450,13 @@ static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *treturn -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)) {
Is it safe to subtract these pointers? When btf_type_size_unknown() is used outside of parsing, such as in btf_dedup(), t might point to a newly allocated new_types buffer while end_type points to the old btf->types_data.
Subtracting pointers from distinct memory allocations is undefined behavior in C, which can cause crashes if tools linking libbpf are compiled with UBSan using -fsanitize=pointer-subtract. Should this bounds check be relocated into btf_parse_type_sec() instead?
tools/lib/bpf/btf.c static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *t · line 460
Quoted 6 lines
tools/lib/bpf/btf.c +5
-0+pr_debug ("Overflow in type size %d for kind %u\n" , + type_size, kind); +return -EINVAL ; + } +return type_size; }
This patch attempts to fix a pointer wrap-around vulnerability, but does this approach leave standard BTF kinds vulnerable?
Since the bounds check is only added to btf_type_size_unknown(), standard kinds like BTF_KIND_STRUCT are evaluated directly in btf_type_size() without any bounds check.
If a malicious BTF file provides a BTF_KIND_STRUCT with a maximum vlen, it can yield a type_size up to ~786KB. On a 32-bit system, if the buffer is allocated near the top of the address space, adding this size could wrap around to a small value.
The overflow check next_type + type_size > end_type in btf_parse_type_sec() would then evaluate to false, potentially bypassing validation and leading to an out-of-bounds write in btf_bswap_type_rest().
Would it be safer to fix the vulnerability for all kinds by changing the check in btf_parse_type_sec() to avoid pointer addition entirely?
For example:
if ((size_t)type_size > (size_t)(end_type - next_type))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 14 lines · Alan Maguire
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 largetype_sizeis added to thenext_type[1]; a sufficiently largetype_sizeon a 32-bit system could lead to the bounds checkif (
next_type+type_size>end_type)not triggering (since
next_type+type_sizewraps to a value low in the 32-bit address space). This would lead to a bad value fornext_typewhen we add the type size. Avoid this by ensuring that thetype_sizewe return frombtf_type_size_unknown()falls within the range of <current type,end_type>.
Quoted 9 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Reported-by sashiko <[email protected]>Fixes 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")Signed-off-by Alan Maguire <[email protected]>--- tools/lib/bpf/btf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
Is this a real issue? If provided BTF data is corrupted, we don't really guarantee anything, do we? And if it's not corrupted, then 32-bit overflow cannot happen because btf_header cannot even express a type section bigger than 4GB.
I'd prefer not to pollute logic with unnecessary defensive checks meant to satisfy AI complaints.
Quoted 34 lines
tools/lib/bpf/btf.c +9
-2@@ -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 *treturn -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
Quoted 25 lines · Andrii Nakryiko
Quoted 14 lines · Alan Maguire
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 largetype_sizeis added to thenext_type[1]; a sufficiently largetype_sizeon a 32-bit system could lead to the bounds checkif (
next_type+type_size>end_type)not triggering (since
next_type+type_sizewraps to a value low in the 32-bit address space). This would lead to a bad value fornext_typewhen we add the type size. Avoid this by ensuring that thetype_sizewe return frombtf_type_size_unknown()falls within the range of <current type,end_type>.Quoted 9 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Reported-by sashiko <[email protected]>Fixes 2ecbe53e0e991 ("libbpf: Use layout to compute an unknown kind size")Signed-off-by Alan Maguire <[email protected]>--- tools/lib/bpf/btf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
Quoted 5 lines
Is this a real issue? If provided BTF data is corrupted, we don't really guarantee anything, do we? And if it's not corrupted, then 32-bit overflow cannot happen because
btf_headercannot even express a type section bigger than 4GB.
As I understand it, it's the sum of the type_size and the current type pointer that could overflow on 32-bit systems; so AI seemed to be saying if the current type pointer was high in the 32-bit address space I guess it'd be a theoretical possibility for legit BTF, but I'd assume other things would fall over prior to that (how would we even read that BTF fully into memory?). Maybe there's a way we could finesse the AI prompting to avoid garbage-in-garbage-out style problems for libbpf, not sure.
Quoted 3 lines
I'd prefer not to pollute logic with unnecessary defensive checks meant to satisfy AI complaints.