0004-resolve_btfids-elf-word.patch 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. From a9355b201d160aa777d1480911c9ee5c7deeb88b Mon Sep 17 00:00:00 2001
  2. From: Tony Ambardar <tony.ambardar@gmail.com>
  3. Date: Thu, 17 Jun 2021 23:14:04 -0700
  4. Subject: [PATCH] bpf: Fix libelf endian handling in resolv_btfids
  5. [ Upstream commit 61e8aeda9398925f8c6fc290585bdd9727d154c4 ]
  6. The vmlinux ".BTF_ids" ELF section is declared in btf_ids.h to hold a list
  7. of zero-filled BTF IDs, which is then patched at link-time with correct
  8. values by resolv_btfids. The section is flagged as "allocable" to preclude
  9. compression, but notably the section contents (BTF IDs) are untyped.
  10. When patching the BTF IDs, resolve_btfids writes in host-native endianness
  11. and relies on libelf for any required translation on reading and updating
  12. vmlinux. However, since the type of the .BTF_ids section content defaults
  13. to ELF_T_BYTE (i.e. unsigned char), no translation occurs. This results in
  14. incorrect patched values when cross-compiling to non-native endianness,
  15. and can manifest as kernel Oops and test failures which are difficult to
  16. troubleshoot [1].
  17. Explicitly set the type of patched data to ELF_T_WORD, the architecture-
  18. neutral ELF type corresponding to the u32 BTF IDs. This enables libelf to
  19. transparently perform any needed endian conversions.
  20. Fixes: fbbb68de80a4 ("bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object")
  21. Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
  22. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  23. Acked-by: Jiri Olsa <jolsa@redhat.com>
  24. Cc: Frank Eigler <fche@redhat.com>
  25. Cc: Mark Wielaard <mark@klomp.org>
  26. Cc: Jiri Olsa <jolsa@kernel.org>
  27. Cc: Yonghong Song <yhs@fb.com>
  28. Link: https://lore.kernel.org/bpf/CAPGftE_eY-Zdi3wBcgDfkz_iOr1KF10n=9mJHm1_a_PykcsoeA@mail.gmail.com [1]
  29. Link: https://lore.kernel.org/bpf/20210618061404.818569-1-Tony.Ambardar@gmail.com
  30. Signed-off-by: Sasha Levin <sashal@kernel.org>
  31. ---
  32. tools/bpf/resolve_btfids/main.c | 3 +++
  33. 1 file changed, 3 insertions(+)
  34. diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
  35. index d636643ddd35..f32c059fbfb4 100644
  36. --- a/tools/bpf/resolve_btfids/main.c
  37. +++ b/tools/bpf/resolve_btfids/main.c
  38. @@ -649,6 +649,9 @@ static int symbols_patch(struct object *obj)
  39. if (sets_patch(obj))
  40. return -1;
  41. + /* Set type to ensure endian translation occurs. */
  42. + obj->efile.idlist->d_type = ELF_T_WORD;
  43. +
  44. elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
  45. err = elf_update(obj->efile.elf, ELF_C_WRITE);
  46. --
  47. 2.55.0