Commit 12450081 authored by Tony Ambardar's avatar Tony Ambardar Committed by Daniel Borkmann

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: 94a1fedd ("libbpf: Add btf__parse_raw() and generic btf__parse() APIs")
Signed-off-by: default avatarTony Ambardar <Tony.Ambardar@gmail.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/90f81508ecc57bc0da318e0fe0f45cfe49b17ea7.1600417359.git.Tony.Ambardar@gmail.com
parent 65c20439
...@@ -659,6 +659,12 @@ struct btf *btf__parse_raw(const char *path) ...@@ -659,6 +659,12 @@ struct btf *btf__parse_raw(const char *path)
err = -EIO; err = -EIO;
goto err_out; goto err_out;
} }
if (magic == __bswap_16(BTF_MAGIC)) {
/* non-native endian raw BTF */
pr_warn("non-native BTF endianness is not supported\n");
err = -LIBBPF_ERRNO__ENDIAN;
goto err_out;
}
if (magic != BTF_MAGIC) { if (magic != BTF_MAGIC) {
/* definitely not a raw BTF */ /* definitely not a raw BTF */
err = -EPROTO; err = -EPROTO;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment