btf_dump_type_data_check_zero() recurses into struct/union members and array elements without checking the data against d->typed_dump->data_end. The dump path performs an overflow check before reading a value, but that check does not apply to the zero-scan recursion, so a caller passing a data_sz smaller than the type size gets reads past the end of the buffer instead of the -E2BIG the API promises, similar to the OOB read fixed by commit 5714ca8cba5e ("libbpf: Fix OOB read in btf_dump_get_bitfield_value"). Check for overflow in btf_dump_type_data_check_zero(), after the toplevel exceptions: every member and element recursion passes through it, mirroring what the dump path does before reading a value. Also drop the direct *(char *)data == 0 test in the array branch; the recursion for element 0 gives the same answer and is bounds-checked now. Fixes: 920d16af9b42 ("libbpf: BTF dumper support for typed data") Suggested-by: Alexei Starovoitov Signed-off-by: Gu Bin --- tools/lib/bpf/btf_dump.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 7dedd3975c39..cda56f34921e 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -2504,6 +2504,11 @@ static int btf_dump_type_data_check_zero(struct btf_dump *d, !d->typed_dump->is_array_char)) return 0; + /* members and elements get here without a check against data_end */ + err = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz); + if (err < 0) + return err; + t = skip_mods_and_typedefs(d->btf, id, NULL); switch (btf_kind(t)) { @@ -2533,14 +2538,12 @@ static int btf_dump_type_data_check_zero(struct btf_dump *d, * non-zero because the string is terminated. */ for (i = 0; i < array->nelems; i++) { - if (i == 0 && ischar && *(char *)data == 0) - return -ENODATA; err = btf_dump_type_data_check_zero(d, elem_type, elem_type_id, data + (i * elem_size), bits_offset, 0); - if (err != -ENODATA) + if (err != -ENODATA || ischar) return err; } return -ENODATA; -- 2.55.0.windows.3 Add a TEST_BTF_DUMP_DATA_OVER() case that dumps an all-zero union bpf_iter_link_info with a data_sz one byte short of the type size. Without the overflow check in btf_dump_type_data_check_zero() the zero-scan reads past data_end; with it, btf_dump__dump_type_data() returns -E2BIG. Suggested-by: Alexei Starovoitov Signed-off-by: Gu Bin --- tools/testing/selftests/bpf/prog_tests/btf_dump.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c index 35f214e254da..68f33f8a54ab 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c @@ -1156,6 +1156,15 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d, "(union bpf_iter_link_info){.map = (struct){.map_fd = (__u32)1,},.cgroup = (struct){.order = (enum bpf_cgroup_iter_order)BPF_CGROUP_ITER_SELF_ONLY,.cgroup_fd = (__u32)1,},.task = (struct){.tid = (__u32)1,.pid = (__u32)1,},}", { .cgroup = { .order = 1, .cgroup_fd = 1, }}); + /* + * overflow test for all-zero union: with a data_sz one byte short + * of the type size, the zero-scan must not read past data_end and + * must return -E2BIG. + */ + TEST_BTF_DUMP_DATA_OVER(btf, d, "union", str, union bpf_iter_link_info, + sizeof(union bpf_iter_link_info) - 1, + "(union bpf_iter_link_info){\n", {}); + /* struct skb with nested structs/unions; because type output is so * complex, we don't do a string comparison, just verify we return * the type size as the amount of data displayed. -- 2.55.0.windows.3