Fix unaligned enum values in btf_dump
Subsystems (4)
v4 cover Fix unaligned enum values in btf_dump
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/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/
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
Signature · 1 line
2.43.0
PATCH v4 1/3 libbpf: Fix sign extension of unaligned enums in btf_dump_get_enum_value()
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.
--- tools/lib/bpf/btf_dump.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
tools/lib/bpf/btf_dump.c +4 -9
@@ -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) {
Signature · 1 line
2.43.0
PATCH v4 2/3 libbpf: Fix zero detection of enum bitfields in BTF dumper
In reply to Hungyu Linbtf_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.
--- tools/lib/bpf/btf_dump.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tools/lib/bpf/btf_dump.c +6 -4
@@ -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;
Signature · 1 line
2.43.0
PATCH v4 3/3 selftests/bpf: Dump unaligned enums and a zeroed fs_context
In reply to Hungyu LinDump 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.
--- tools/testing/selftests/bpf/prog_tests/btf_dump.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/btf_dump.c +9 -1
@@ -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,
Signature · 1 line
2.43.0