* [PATCH bpf-next v4 0/3] Fix unaligned enum values in btf_dump
@ 2026-11-26 1:12 Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 1/3] libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value() Hungyu Lin
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Hungyu Lin @ 2026-11-26 1:12 UTC (permalink / raw)
To: andrii, bpf
Cc: ast, daniel, eddyz87, memxor, martin.lau, song, yonghong.song,
jolsa, emil, ihor.solodrai, alan.maguire, shuah, alexis.lothore,
luisflavieira, ameryhung, david.laight.linux, linux-kselftest,
linux-kernel, dennylin0707
btf_dump_get_enum_value() reads unaligned enum values through
btf_dump_get_bitfield_value() with a zero bit size, which shifts a
__u64 by 64. Patch 1 copies unaligned enum data into an aligned buffer
instead, as btf_dump_int_data() does. Patch 2 makes the zero check of
enum bitfields read only the bytes of the bitfield, as Alexei suggested
in the v3 review. Patch 3 adds the tests Alexei posted in the v1 review.
Changes in v4:
- Add patch 2 to fix the zero check of enum bitfields (Alexei).
Changes in v3:
- Do the alignment check before the size check (David).
Changes in v2:
- Describe the undefined shift in the commit log.
- Drop the data_end check, which broke the dump of struct fs_context
without emit_zeroes.
- Add the selftests from the v1 review.
v3: https://lore.kernel.org/bpf/20261116080151.44860-1-dennylin0707@gmail.com/
v2: https://lore.kernel.org/bpf/20261115084923.53182-1-dennylin0707@gmail.com/
v1: https://lore.kernel.org/bpf/20261113223449.97604-1-dennylin0707@gmail.com/
Hungyu Lin (3):
libbpf: Fix sign extension of unaligned enums in
btf_dump_get_enum_value()
libbpf: Fix zero detection of enum bitfields in BTF dumper
selftests/bpf: Dump unaligned enums and a zeroed fs_context
tools/lib/bpf/btf_dump.c | 23 ++++++++-----------
.../selftests/bpf/prog_tests/btf_dump.c | 10 +++++++-
2 files changed, 19 insertions(+), 14 deletions(-)
base-commit: 3cbbebf6819b42008e7ef85b6f0a7254c3074c19
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v4 1/3] libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value()
2026-11-26 1:12 [PATCH bpf-next v4 0/3] Fix unaligned enum values in btf_dump Hungyu Lin
@ 2026-11-26 1:12 ` Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 2/3] libbpf: Fix zero detection of enum bitfields in BTF dumper Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 3/3] selftests/bpf: Dump unaligned enums and a zeroed fs_context Hungyu Lin
2 siblings, 0 replies; 4+ messages in thread
From: Hungyu Lin @ 2026-11-26 1:12 UTC (permalink / raw)
To: andrii, bpf
Cc: ast, daniel, eddyz87, memxor, martin.lau, song, yonghong.song,
jolsa, emil, ihor.solodrai, alan.maguire, shuah, alexis.lothore,
luisflavieira, ameryhung, david.laight.linux, linux-kselftest,
linux-kernel, dennylin0707
btf_dump_get_enum_value() reads unaligned enum values through
btf_dump_get_bitfield_value() with a zero bit size. That makes
right_shift_bits 64 in
*value = (num << left_shift_bits) >> right_shift_bits;
which is undefined behavior for a __u64 on both endians. The path also
bypasses the signed loads used for aligned data, so a negative value of
a signed enum can lose its enumerator name when the data is unaligned.
Fix this by copying unaligned enum data into an aligned buffer, as
btf_dump_int_data() does, and reading it with the same loads as aligned
data.
Fixes: 920d16af9b42 ("libbpf: BTF dumper support for typed data")
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
tools/lib/bpf/btf_dump.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 08a4bbbf4ffd..85a30b373788 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -2329,16 +2329,11 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
__s64 *value)
{
bool is_signed = btf_kflag(t);
+ char buf[8] __attribute__((aligned(8)));
- if (!ptr_is_aligned(d->btf, id, data)) {
- __u64 val;
- int err;
-
- err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
- if (err)
- return err;
- *value = (__s64)val;
- return 0;
+ if (!ptr_is_aligned(d->btf, id, data) && t->size <= sizeof(buf)) {
+ memcpy(buf, data, t->size);
+ data = buf;
}
switch (t->size) {
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v4 2/3] libbpf: Fix zero detection of enum bitfields in BTF dumper
2026-11-26 1:12 [PATCH bpf-next v4 0/3] Fix unaligned enum values in btf_dump Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 1/3] libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value() Hungyu Lin
@ 2026-11-26 1:12 ` Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 3/3] selftests/bpf: Dump unaligned enums and a zeroed fs_context Hungyu Lin
2 siblings, 0 replies; 4+ messages in thread
From: Hungyu Lin @ 2026-11-26 1:12 UTC (permalink / raw)
To: andrii, bpf
Cc: ast, daniel, eddyz87, memxor, martin.lau, song, yonghong.song,
jolsa, emil, ihor.solodrai, alan.maguire, shuah, alexis.lothore,
luisflavieira, ameryhung, david.laight.linux, linux-kselftest,
linux-kernel, dennylin0707
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@kernel.org>
Link: https://lore.kernel.org/bpf/DN40PKFY2KUF.13KK78H8OW2J9@gmail.com/
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v4 3/3] selftests/bpf: Dump unaligned enums and a zeroed fs_context
2026-11-26 1:12 [PATCH bpf-next v4 0/3] Fix unaligned enum values in btf_dump Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 1/3] libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value() Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 2/3] libbpf: Fix zero detection of enum bitfields in BTF dumper Hungyu Lin
@ 2026-11-26 1:12 ` Hungyu Lin
2 siblings, 0 replies; 4+ messages in thread
From: Hungyu Lin @ 2026-11-26 1:12 UTC (permalink / raw)
To: andrii, bpf
Cc: ast, daniel, eddyz87, memxor, martin.lau, song, yonghong.song,
jolsa, emil, ihor.solodrai, alan.maguire, shuah, alexis.lothore,
luisflavieira, ameryhung, david.laight.linux, linux-kselftest,
linux-kernel, dennylin0707
Dump the signed enum value from an unaligned address in the enum data
test. Also dump a zeroed struct fs_context without emit_zeroes in the
struct data test, which covers phase, an enum bitfield 3 bytes before
the end of the struct.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
tools/testing/selftests/bpf/prog_tests/btf_dump.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index b5c90f729339..d6fd9933ff25 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -945,7 +945,7 @@ static void test_btf_dump_enum_data(struct btf *btf, struct btf_dump *d,
char *str)
{
__u32 high = 0x80000000;
- __s32 neg = -1;
+ __s32 neg[2] = { -1, -1 };
/* enum where enum value does (and does not) exist */
TEST_BTF_DUMP_DATA_C(btf, d, "enum", str, enum bpf_cmd, BTF_F_COMPACT,
@@ -994,6 +994,8 @@ static void test_btf_dump_enum_data(struct btf *btf, struct btf_dump *d,
"(enum test_enum_u32)TEST_ENUM_HIGH"), "dump unsigned enum");
ASSERT_OK(btf_dump_data(btf, d, "test_enum_s32", NULL, 0, &neg, 4, str,
"(enum test_enum_s32)TEST_ENUM_NEG"), "dump signed enum");
+ ASSERT_OK(btf_dump_data(btf, d, "test_enum_s32", NULL, 0, (void *)neg + 1, 4, str,
+ "(enum test_enum_s32)TEST_ENUM_NEG"), "dump unaligned enum");
btf_dump__free(d);
}
@@ -1182,6 +1184,12 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d,
type_sz = btf__resolve_size(btf, type_id);
str[0] = '\0';
+ /* 'phase' is an enum bitfield 3 bytes before the end */
+ ret = btf_dump__dump_type_data(d, type_id, zero_data, type_sz, &opts);
+ ASSERT_EQ(ret, type_sz,
+ "unexpected return value dumping zeroed fs_context");
+
+ str[0] = '\0';
opts.emit_zeroes = true;
ret = btf_dump__dump_type_data(d, type_id, zero_data, type_sz, &opts);
ASSERT_EQ(ret, type_sz,
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 1:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 1:12 [PATCH bpf-next v4 0/3] Fix unaligned enum values in btf_dump Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 1/3] libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value() Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 2/3] libbpf: Fix zero detection of enum bitfields in BTF dumper Hungyu Lin
2026-11-26 1:12 ` [PATCH bpf-next v4 3/3] selftests/bpf: Dump unaligned enums and a zeroed fs_context Hungyu Lin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®