Skip to content

Commit 1245008

Browse files
guidosarducciborkmann
authored andcommitted
libbpf: Fix native endian assumption when parsing BTF
Code in btf__parse_raw() fails to detect raw BTF of non-native endianness and assumes it must be ELF data, which then fails to parse as ELF and yields a misleading error message: root:/# bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: failed to get EHDR from /sys/kernel/btf/vmlinux For example, this could occur after cross-compiling a BTF-enabled kernel for a target with non-native endianness, which is currently unsupported. Check for correct endianness and emit a clearer error message: root:/# bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: non-native BTF endianness is not supported Fixes: 94a1fed ("libbpf: Add btf__parse_raw() and generic btf__parse() APIs") Signed-off-by: Tony Ambardar <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/90f81508ecc57bc0da318e0fe0f45cfe49b17ea7.1600417359.git.Tony.Ambardar@gmail.com
1 parent 65c2043 commit 1245008

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

tools/lib/bpf/btf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,12 @@ struct btf *btf__parse_raw(const char *path)
659659
err = -EIO;
660660
goto err_out;
661661
}
662+
if (magic == __bswap_16(BTF_MAGIC)) {
663+
/* non-native endian raw BTF */
664+
pr_warn("non-native BTF endianness is not supported\n");
665+
err = -LIBBPF_ERRNO__ENDIAN;
666+
goto err_out;
667+
}
662668
if (magic != BTF_MAGIC) {
663669
/* definitely not a raw BTF */
664670
err = -EPROTO;

0 commit comments

Comments
 (0)