libbpf: bounds-check float size in btf_dump_float_data()
PATCH 1/1 libbpf: bounds-check float size in btf_dump_float_data()
btf_dump_float_data() copies t->size bytes from the object being dumped into a fixed-size 16-byte on-stack union float_data whenever the source data is not naturally aligned:
union float_data fl;
int sz = t->size;
...
if (!ptr_is_aligned(d->btf, type_id, data)) {
memcpy(&fl, data, sz);
flp = &fl;
}sz comes straight from the BTF type and is never bounded. libbpf does not validate the size of a BTF_KIND_FLOAT type while parsing BTF (btf_validate_type() just breaks for it), and btf_dump_type_data_check_overflow() accepts any size below INT_MAX. A BTF blob describing a FLOAT larger than sizeof(union float_data) therefore makes the memcpy() write past fl and smash the stack when such a value is formatted via btf_dump__dump_type_data().
btf_dump_int_data() already rejects out-of-range sizes before its copy; add the same check to the float path.
---
tools/lib/bpf/btf_dump.c +5 -0
@@ -1983,6 +1983,11 @@ static int btf_dump_float_data(struct btf_dump *d,union float_data fl;int sz = t->size; +if (sz <0 || sz >sizeof (fl)) { +pr_warn ("unexpected size %d for id [%u]\n" , sz, type_id); +return -EINVAL ; + } +/* handle unaligned data; copy to local union */ if (!ptr_is_aligned (d->btf, type_id, data)) {memcpy (&fl, data, sz);
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Architecture-dependent bounds check breaks dumping of 16-byte floats on 32-bit architectures --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Architecture-dependent bounds check breaks dumping of 16-byte floats on 32-bit architectures --
commit 31fd1a7664b926003f6e360a24ed9cc2bcd8b9c5
Author: Naveed Khan <[email protected]>
libbpf: bounds-check float size in btf_dump_float_data()
This commit adds a bounds check to btf_dump_float_data() in libbpf to prevent a stack buffer overflow. It ensures the size of the float type from the BTF data doesn't exceed the size of the local union used for unaligned copies.
tools/lib/bpf/btf_dump.c static int btf_dump_float_data(struct btf_dump *d, · line 1990
Quoted 13 lines
tools/lib/bpf/btf_dump.c +5
-0@@ -1983,6 +1983,11 @@ static int btf_dump_float_data(struct btf_dump *d,union float_data fl;int sz = t->size; +if (sz <0 || sz >sizeof (fl)) { +pr_warn ("unexpected size %d for id [%u]\n" , sz, type_id); +return -EINVAL ; + } +
[Severity: Medium]
Does this bounds check prevent dumping 16-byte floats when running on 32-bit architectures?
On 32-bit architectures, sizeof(union float_data) may be 8 or 12 depending on the size of long double. If we process a valid float type with sz = 16, this check will evaluate to true and return -EINVAL.
Looking further down in btf_dump_float_data(), the function has a handler for 16-byte floats:
switch (sz) {
case 16:
btf_dump_type_values(d, "%Lf", flp->ld);
break;Because sz == 16 is expected, this check makes the case 16: statement unreachable on those platforms.
Should this code allocate a fixed 16-byte stack buffer to receive the memcpy() instead of relying on the architecture-dependent size of union float_data?
Quoted 3 lines
tools/lib/bpf/btf_dump.c +0
-0/* handle unaligned data; copy to local union */ if (!ptr_is_aligned (d->btf, type_id, data)) {memcpy (&fl, data, sz);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThis looks like a fix for a stack buffer overflow that has been present since the typed data dumper was added. Should this carry a Fixes: tag?
Quoted 19 lines
btf_dump_float_data()copiest->sizebytes from the object being dumped into a fixed-size 16-byte on-stack unionfloat_datawhenever the source data is not naturally aligned:union float_data fl; int sz = t->size; ... if (!ptr_is_aligned(d->btf, type_id, data)) { memcpy(&fl, data, sz); flp = &fl; }sz comes straight from the BTF type and is never bounded. libbpf does not validate the size of a
BTF_KIND_FLOATtype while parsing BTF (btf_validate_type()just breaks for it), andbtf_dump_type_data_check_overflow()accepts any size belowINT_MAX. A BTF blob describing a FLOAT larger thansizeof(union float_data)therefore makes thememcpy()write past fl and smash the stack when such a value is formatted viabtf_dump__dump_type_data().btf_dump_int_data()already rejects out-of-range sizes before its copy; add the same check to the float path.
Quoted 1 line
Signed-off-by Naveed Khan <[email protected]>
This looks like a fix for a stack buffer overflow that has been present since the typed data dumper was added. Should this carry a Fixes: tag?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/28011525735
Quoted 31 lines · Naveed Khan
btf_dump_float_data()copiest->sizebytes from the object being dumped into a fixed-size 16-byte on-stack unionfloat_datawhenever the source data is not naturally aligned:union float_data fl; int sz = t->size; ... if (!ptr_is_aligned(d->btf, type_id, data)) { memcpy(&fl, data, sz); flp = &fl; }sz comes straight from the BTF type and is never bounded. libbpf does not validate the size of a
BTF_KIND_FLOATtype while parsing BTF (btf_validate_type()just breaks for it), andbtf_dump_type_data_check_overflow()accepts any size belowINT_MAX. A BTF blob describing a FLOAT larger thansizeof(union float_data)therefore makes thememcpy()write past fl and smash the stack when such a value is formatted viabtf_dump__dump_type_data().
btf_dump_int_data()already rejects out-of-range sizes before its copy; add the same check to the float path.Signed-off-by Naveed Khan <[email protected]>---
tools/lib/bpf/btf_dump.c +0
-0
tools/lib/bpf/btf_dump.c static int btf_dump_float_data(struct btf_dump *d, · line 1990
Quoted 8 lines
tools/lib/bpf/btf_dump.c +5
-0@@ -1983,6 +1983,11 @@ static int btf_dump_float_data(struct btf_dump *d,union float_data fl;int sz = t->size; +if (sz <0 || sz >sizeof (fl)) { +pr_warn ("unexpected size %d for id [%u]\n" , sz, type_id); +return -EINVAL ; + } +
earlier btf_dump_type_data_check_overflow() should prevent memcpy below from reading below data boundaries, so there is no need to protect from that. And then subsequent switch will validate one of expected/supported float sizes. This logic seems to be fine as is.
pw-bot: cr
Quoted 6 lines
tools/lib/bpf/btf_dump.c +0
-1/* handle unaligned data; copy to local union */ if (!ptr_is_aligned (d->btf, type_id, data)) {memcpy (&fl, data, sz); --2.52.0
PATCH v2 1/1 libbpf: bounds-check float size in btf_dump_float_data()
libbpf: bounds-check float size in btf_dump_float_data()
btf_dump_float_data() copies t->size bytes from the object being dumped into a fixed-size on-stack buffer whenever the source data is not naturally aligned:
union float_data fl;
int sz = t->size;
...
if (!ptr_is_aligned(d->btf, type_id, data)) {
memcpy(&fl, data, sz);
flp = &fl;
}sz comes straight from the BTF type and is never bounded. btf_validate_type() does not validate the size of a BTF_KIND_FLOAT type, and btf_dump_type_data_check_overflow() only bounds the *source* read against data_end, not the destination buffer. The switch on sz that rejects unsupported sizes runs only *after* the memcpy, so a BTF blob describing a FLOAT larger than the destination smashes the stack before the size is ever validated.
Validate sz before the copy, mirroring btf_dump_int_data(). Use a fixed 16-byte aligned buffer as the destination rather than union float_data, whose size is architecture-dependent (sizeof(long double) is 12 on some 32-bit ABIs), so that valid 16-byte long double floats are always accepted.
---
Quoted 1 line
From
27941d119a3e9815906e787728bac54de9737539Mon Sep 17 00:00:00 2001
From: Naveed Khan <[email protected]>
Date: Wed, 24 Jun 2026 15:23:56 +0530
Subject: [PATCH] libbpf: bounds-check float size in btf_dump_float_data()
btf_dump_float_data() copies t->size bytes from the object being dumped into a fixed-size on-stack buffer whenever the source data is not naturally aligned:
union float_data fl;
int sz = t->size;
...
if (!ptr_is_aligned(d->btf, type_id, data)) {
memcpy(&fl, data, sz);
flp = &fl;
}sz comes straight from the BTF type and is never bounded. btf_validate_type() does not validate the size of a BTF_KIND_FLOAT type, and btf_dump_type_data_check_overflow() only bounds the *source* read against data_end, not the destination buffer. The switch on sz that rejects unsupported sizes runs only *after* the memcpy, so a BTF blob describing a FLOAT larger than the destination smashes the stack before the size is ever validated.
Validate sz before the copy, mirroring btf_dump_int_data(). Use a fixed 16-byte aligned buffer as the destination rather than union float_data, whose size is architecture-dependent (sizeof(long double) is 12 on some 32-bit ABIs), so that valid 16-byte long double floats are always accepted.
--- tools/lib/bpf/btf_dump.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
tools/lib/bpf/btf_dump.c +9 -4
@@ -1980,13 +1980,18 @@ static int btf_dump_float_data(struct btf_dump *d, const void *data) {const union float_data *flp = data; -union float_data fl; + char buf[16] __attribute__((aligned(16)));int sz = t->size; -/* handle unaligned data; copy to local union */ +if (sz <0 || sz >sizeof (buf)) { +pr_warn ("unexpected size %d for id [%u]\n" , sz, type_id); +return -EINVAL ; + } + +/* handle unaligned data; copy to local buffer */ if (!ptr_is_aligned (d->btf, type_id, data)) { -memcpy (&fl, data, sz); - flp = &fl; +memcpy (buf, data, sz); + flp = (void *)buf; }switch (sz) {
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] A malicious BTF can bypass size checks by defining a FLOAT or PTR member with a bitfield size, leading to an out-of-bounds memory read.
- [High] A mismatch between the target BTF alignment and the host architecture alignment requirements can cause an unaligned memory access crash when reading floats. --
commit 8b775a18b4de3665e91e05ddb075a75dc6ae1b43
Author: Naveed Khan <[email protected]>
Subject: libbpf: bounds-check float size in btf_dump_float_data()
This commit fixes a stack buffer overflow in btf_dump_float_data() by validating the float size before copying it into an on-stack buffer. It also changes the buffer to be 16-byte aligned so that valid long double floats are always accepted.
Quoted 5 lines
tools/lib/bpf/btf_dump.c +0
-0@@ -1980,13 +1980,18 @@ static int btf_dump_float_data(struct btf_dump *d,
[ ... ]
tools/lib/bpf/btf_dump.c static int btf_dump_float_data(struct btf_dump *d, · line 1993
Quoted 5 lines
tools/lib/bpf/btf_dump.c +1
-2/* handle unaligned data; copy to local buffer */ if (!ptr_is_aligned (d->btf, type_id, data)) { -memcpy (&fl, data, sz); - flp = &fl; +memcpy (buf, data, sz);
[Severity: High]
This isn't a bug introduced by this patch, but could a malicious BTF bypass size checks here by defining a FLOAT or PTR member with a bitfield size?
Looking at btf_dump_type_data_check_overflow(), it short-circuits the full size bounds check against data_end when bit_sz != 0, checking only the bitfield size:
if (bit_sz) {
__u8 nr_bytes = (bits_offset + bit_sz + 7) / 8;
return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : nr_bytes;
}However, we unconditionally read t->size bytes from data via memcpy above, which could lead to an out-of-bounds heap read if the actual data buffer is smaller than t->size.
tools/lib/bpf/btf_dump.c static int btf_dump_float_data(struct btf_dump *d, · line 1997
Quoted 4 lines
tools/lib/bpf/btf_dump.c +1
-0+ flp = (void *)buf; }switch (sz) {
[Severity: High]
This is a pre-existing issue, but could this cause an unaligned memory access crash on certain architectures when reading 16-byte floats?
ptr_is_aligned() checks alignment against the BTF target requirements, which caps at 8 bytes. If the host architecture requires 16-byte alignment for a 16-byte long double, and the data pointer is only 8-byte aligned, ptr_is_aligned() returns true and bypasses the local buffer copy.
Later in the switch statement:
case 16:
btf_dump_type_values(d, "%Lf", flp->ld);Casting that 8-byte aligned data pointer to union float_data and accessing flp->ld could cause an unaligned memory access trap.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 29 lines · Naveed Khan
libbpf: bounds-check float size in
btf_dump_float_data()
btf_dump_float_data()copiest->sizebytes from the object being dumped into a fixed-size on-stack buffer whenever the source data is not naturally aligned:union float_data fl; int sz = t->size; ... if (!ptr_is_aligned(d->btf, type_id, data)) { memcpy(&fl, data, sz); flp = &fl; }sz comes straight from the BTF type and is never bounded.
btf_validate_type()does not validate the size of aBTF_KIND_FLOATtype, andbtf_dump_type_data_check_overflow()only bounds the *source* read againstdata_end, not the destination buffer. The switch on sz that rejects unsupported sizes runs only *after* the memcpy, so a BTF blob describing a FLOAT larger than the destination smashes the stack before the size is ever validated.Validate sz before the copy, mirroring
btf_dump_int_data(). Use a fixed 16-byte aligned buffer as the destination rather than unionfloat_data, whose size is architecture-dependent (sizeof(long double) is 12 on some 32-bit ABIs), so that valid 16-byte long double floats are always accepted.
Quoted 3 lines
Fixes 920d16af9b42 ("libbpf: BTF dumper support for typed data")Signed-off-by Naveed Khan <[email protected]>---
hm, did you see my previous reply? doesn't seem like your patch addresses my concerns
Quoted 67 lines
Quoted 1 line
From
27941d119a3e9815906e787728bac54de9737539Mon Sep 17 00:00:00 2001From: Naveed Khan <[email protected]>
Date: Wed, 24 Jun 2026 15:23:56 +0530
Subject: [PATCH] libbpf: bounds-check float size inbtf_dump_float_data()
btf_dump_float_data()copiest->sizebytes from the object being dumped into a fixed-size on-stack buffer whenever the source data is not naturally aligned:union float_data fl; int sz = t->size; ... if (!ptr_is_aligned(d->btf, type_id, data)) { memcpy(&fl, data, sz); flp = &fl; }sz comes straight from the BTF type and is never bounded.
btf_validate_type()does not validate the size of aBTF_KIND_FLOATtype, andbtf_dump_type_data_check_overflow()only bounds the *source* read againstdata_end, not the destination buffer. The switch on sz that rejects unsupported sizes runs only *after* the memcpy, so a BTF blob describing a FLOAT larger than the destination smashes the stack before the size is ever validated.Validate sz before the copy, mirroring
btf_dump_int_data(). Use a fixed 16-byte aligned buffer as the destination rather than unionfloat_data, whose size is architecture-dependent (sizeof(long double) is 12 on some 32-bit ABIs), so that valid 16-byte long double floats are always accepted.Fixes 920d16af9b42 ("libbpf: BTF dumper support for typed data")Signed-off-by Naveed Khan <[email protected]>--- tools/lib/bpf/btf_dump.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)tools/lib/bpf/btf_dump.c +9
-5@@ -1980,13 +1980,18 @@ static int btf_dump_float_data(struct btf_dump *d, const void *data) {const union float_data *flp = data; -union float_data fl; + char buf[16] __attribute__((aligned(16)));int sz = t->size; -/* handle unaligned data; copy to local union */ +if (sz <0 || sz >sizeof (buf)) { +pr_warn ("unexpected size %d for id [%u]\n" , sz, type_id); +return -EINVAL ; + } + +/* handle unaligned data; copy to local buffer */ if (!ptr_is_aligned (d->btf, type_id, data)) { -memcpy (&fl, data, sz); - flp = &fl; +memcpy (buf, data, sz); + flp = (void *)buf; }switch (sz) { --2.52.0
Thanks for the review. Both of these are pre-existing and orthogonal to the stack overflow this patch fixes, so I'd rather not pull them into this change, but to answer the questions directly:
Quoted 3 lines
could a malicious BTF bypass size checks here by defining a FLOAT or PTR member with a bitfield size? ... we unconditionally read
t->sizebytes from data via memcpy above, which could lead to an out-of-bounds heap read
This patch bounds the copy to <= sizeof(buf) (16), so it strictly reduces that exposure rather than introducing it: the old code memcpy'd an unbounded t->size, the new code copies at most 16 bytes. The remaining concern is the bit_sz short-circuit in btf_dump_type_data_check_overflow(), which validates nr_bytes derived from bit_sz instead of t->size on the source side. That check is independent of the float buffer and predates this change. A FLOAT/PTR member carrying a non-zero bitfield is malformed BTF to begin with; tightening check_overflow() to reject it is worth doing, but as a separate patch rather than widening the scope of this one.
Quoted 3 lines
could this cause an unaligned memory access crash ... If the host architecture requires 16-byte alignment for a 16-byte long double, and the data pointer is only 8-byte aligned
Also pre-existing, and this patch doesn't make it worse. btf__align_of() returns min(ptr_sz, t->size) for a float, so ptr_is_aligned() does cap at the target pointer size (8 on 64-bit). The case described is the *aligned* path, where flp->ld is read in place; that direct cast is unchanged here and is shared with the int/ptr/enum paths, so it should be hardened separately if needed. The *unaligned* path this patch touches now copies into a buffer declared __attribute__((aligned(16))), which is strictly better than the old union float_data fl that only had natural alignment.