Skip to contentSkip to conversation

Fix unaligned enum values in btf_dump

PATCH New Hungyu Lin 2 participants 4 messages Nov 16

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

v3 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 adds the tests Alexei posted in the v1 review.

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.

v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/

Hungyu Lin (2):
libbpf: Fix sign extension of unaligned enums in
btf_dump_get_enum_value()
selftests/bpf: Dump unaligned enums and a zeroed fs_context

 tools/lib/bpf/btf_dump.c                          | 13 ++++---------
 tools/testing/selftests/bpf/prog_tests/btf_dump.c | 10 +++++++++-
 2 files changed, 13 insertions(+), 10 deletions(-)

base-commit: 8835113107b2cf75df33a71e265cdeec5d5d13e0

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/2 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.

Fixes 920d16af9b42 ("libbpf: BTF dumper support for typed data")
---
 tools/lib/bpf/btf_dump.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
tools/lib/bpf/btf_dump.c +4 -9
index bbe86928b3ec..050a5fbb9533 100644
@@ -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
author
#2 lore raw

PATCH v3 2/2 selftests/bpf: Dump unaligned enums and a zeroed fs_context

In reply to Hungyu Lin

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.

---
 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
index b5c90f729339..d6fd9933ff25 100644
@@ -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
#3 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: Dump unaligned enums and a zeroed fs_context

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/bpf: enum bitfield OOB read in btf_dump_type_data_check_zero() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/bpf: enum bitfield OOB read in btf_dump_type_data_check_zero()
--

commit b08982c8f9a5f24d9685e27855c93f8136d3ee81
Author: Hungyu Lin <[email protected]>

selftests/bpf: Dump unaligned enums and a zeroed fs_context

This commit extends the BTF dump selftests to dump a signed enum from an unaligned address and a zeroed struct fs_context without emit_zeroes.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/btf_dump.c +0 -0
index b5c90f7293398..d6fd9933ff258 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/btf_dump.c static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d, · line 1195

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/btf_dump.c +6 -0
@@ -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,

[Severity: Medium]
Does dumping struct fs_context with emit_zeroes unset read 1 byte past zero_data + type_sz when checking the phase enum bitfield?

In struct fs_context, phase is an 8-bit enum bitfield at byte offset type_sz - 3 whose underlying enum type has t->size == 4. btf_dump_type_data_check_overflow() only checks that the 1 byte covering bit_sz fits before data_end, but btf_dump_type_data_check_zero() ignores bit_sz for BTF_KIND_ENUM and reads a full t->size (4-byte) enum via btf_dump_get_enum_value():

tools/lib/bpf/btf_dump.c:btf_dump_type_data_check_zero() {
	case BTF_KIND_INT:
		if (bit_sz)
			return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
		return btf_dump_base_type_check_zero(d, t, id, data);
...
	case BTF_KIND_ENUM:
	case BTF_KIND_ENUM64:
		err = btf_dump_get_enum_value(d, t, data, id, &value);
}

Because data is unaligned at offset type_sz - 3, btf_dump_get_enum_value() calls memcpy(buf, data, t->size), reading 1 byte past data_end and mixing adjacent bitfields (such as need_free) and trailing bytes into the zero check. Even if btf_dump_bitfield_check_zero() were used, btf_dump_get_bitfield_value() bounds-checks nr_bytes (1) against data_end before looping over t->size (4) bytes, which also reads 1 byte past data_end. Passing the 512-byte zero_data stack buffer here masks the out-of-bounds read from AddressSanitizer and from the return-value check.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2