[PATCH bpf-next v4 2/3] libbpf: Fix zero detection of enum bitfields in BTF dumper

From: Hungyu Lin

Date: Wed Nov 25 2026 - 20:13:06 EST


btf_dump_type_data_check_zero() ignores bit_sz for enums and reads the
whole enum through btf_dump_get_enum_value(). For an enum bitfield that
value includes the bitfields next to it. In struct fs_context,
purpose:8 and phase:8 are followed by need_free:1, so with need_free
set the dump prints both enums as 0 without emit_zeroes. phase starts 3
bytes before the end of the struct, so its 4-byte read also goes 1 byte
past the data.

Check enum bitfields with btf_dump_bitfield_check_zero(), as is already
done for int bitfields. btf_dump_get_bitfield_value() bounds-checks only
the bytes that hold the bitfield but then reads t->size bytes, so read
only those bytes.

Fixes: 920d16af9b42 ("libbpf: BTF dumper support for typed data")
Suggested-by: Alexei Starovoitov <ast@xxxxxxxxxx>
Link: https://lore.kernel.org/bpf/DN40PKFY2KUF.13KK78H8OW2J9@xxxxxxxxx/
Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
---
tools/lib/bpf/btf_dump.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 85a30b373788..3387ceb2a3d3 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -1879,7 +1879,7 @@ static int btf_dump_get_bitfield_value(struct btf_dump *d,
return -E2BIG;

/* Maximum supported bitfield size is 64 bits */
- if (t->size > 8) {
+ if (t->size > 8 || nr_bytes > 8) {
pr_warn("unexpected bitfield size %u\n", t->size);
return -EINVAL;
}
@@ -1888,13 +1888,13 @@ static int btf_dump_get_bitfield_value(struct btf_dump *d,
* stored in num, then we left/right shift num to eliminate irrelevant bits.
*/
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
- for (i = t->size - 1; i >= 0; i--)
+ for (i = nr_bytes - 1; i >= 0; i--)
num = num * 256 + bytes[i];
nr_copy_bits = bit_sz + bits_offset;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
- for (i = 0; i < t->size; i++)
+ for (i = 0; i < nr_bytes; i++)
num = num * 256 + bytes[i];
- nr_copy_bits = t->size * 8 - bits_offset;
+ nr_copy_bits = nr_bytes * 8 - bits_offset;
#else
# error "Unrecognized __BYTE_ORDER__"
#endif
@@ -2574,6 +2574,8 @@ static int btf_dump_type_data_check_zero(struct btf_dump *d,
}
case BTF_KIND_ENUM:
case BTF_KIND_ENUM64:
+ if (bit_sz)
+ return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
err = btf_dump_get_enum_value(d, t, data, id, &value);
if (err)
return err;
--
2.43.0