0001-binfmt_flat-do-not-stop-relocating-GOT-entries-prema.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. From 6045ab5fea4c849153ebeb0acb532da5f29d69c4 Mon Sep 17 00:00:00 2001
  2. From: Niklas Cassel <niklas.cassel@wdc.com>
  3. Date: Thu, 14 Apr 2022 11:10:18 +0200
  4. Subject: binfmt_flat: do not stop relocating GOT entries prematurely on riscv
  5. Upstream commit 6045ab5fea4c849153ebeb0acb532da5f29d69c4.
  6. bFLT binaries are usually created using elf2flt.
  7. The linker script used by elf2flt has defined the .data section like the
  8. following for the last 19 years:
  9. .data : {
  10. _sdata = . ;
  11. __data_start = . ;
  12. data_start = . ;
  13. *(.got.plt)
  14. *(.got)
  15. FILL(0) ;
  16. . = ALIGN(0x20) ;
  17. LONG(-1)
  18. . = ALIGN(0x20) ;
  19. ...
  20. }
  21. It places the .got.plt input section before the .got input section.
  22. The same is true for the default linker script (ld --verbose) on most
  23. architectures except x86/x86-64.
  24. The binfmt_flat loader should relocate all GOT entries until it encounters
  25. a -1 (the LONG(-1) in the linker script).
  26. The problem is that the .got.plt input section starts with a GOTPLT header
  27. (which has size 16 bytes on elf64-riscv and 8 bytes on elf32-riscv), where
  28. the first word is set to -1. See the binutils implementation for riscv [1].
  29. This causes the binfmt_flat loader to stop relocating GOT entries
  30. prematurely and thus causes the application to crash when running.
  31. Fix this by skipping the whole GOTPLT header, since the whole GOTPLT header
  32. is reserved for the dynamic linker.
  33. The GOTPLT header will only be skipped for bFLT binaries with flag
  34. FLAT_FLAG_GOTPIC set. This flag is unconditionally set by elf2flt if the
  35. supplied ELF binary has the symbol _GLOBAL_OFFSET_TABLE_ defined.
  36. ELF binaries without a .got input section should thus remain unaffected.
  37. Tested on RISC-V Canaan Kendryte K210 and RISC-V QEMU nommu_virt_defconfig.
  38. [1] https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elfnn-riscv.c;hb=binutils-2_38#l3275
  39. Cc: <stable@vger.kernel.org>
  40. Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
  41. Reviewed-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
  42. Link: https://lore.kernel.org/r/20220414091018.896737-1-niklas.cassel@wdc.com
  43. Fixed-by: kernel test robot <lkp@intel.com>
  44. Link: https://lore.kernel.org/lkml/202204182333.OIUOotK8-lkp@intel.com
  45. Signed-off-by: Kees Cook <keescook@chromium.org>
  46. ---
  47. fs/binfmt_flat.c | 27 ++++++++++++++++++++++++++-
  48. 1 file changed, 26 insertions(+), 1 deletion(-)
  49. diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
  50. index 6268981500112..dca0b6875f9c3 100644
  51. --- a/fs/binfmt_flat.c
  52. +++ b/fs/binfmt_flat.c
  53. @@ -440,6 +440,30 @@ static void old_reloc(unsigned long rl)
  54. /****************************************************************************/
  55. +static inline u32 __user *skip_got_header(u32 __user *rp)
  56. +{
  57. + if (IS_ENABLED(CONFIG_RISCV)) {
  58. + /*
  59. + * RISC-V has a 16 byte GOT PLT header for elf64-riscv
  60. + * and 8 byte GOT PLT header for elf32-riscv.
  61. + * Skip the whole GOT PLT header, since it is reserved
  62. + * for the dynamic linker (ld.so).
  63. + */
  64. + u32 rp_val0, rp_val1;
  65. +
  66. + if (get_user(rp_val0, rp))
  67. + return rp;
  68. + if (get_user(rp_val1, rp + 1))
  69. + return rp;
  70. +
  71. + if (rp_val0 == 0xffffffff && rp_val1 == 0xffffffff)
  72. + rp += 4;
  73. + else if (rp_val0 == 0xffffffff)
  74. + rp += 2;
  75. + }
  76. + return rp;
  77. +}
  78. +
  79. static int load_flat_file(struct linux_binprm *bprm,
  80. struct lib_info *libinfo, int id, unsigned long *extra_stack)
  81. {
  82. @@ -789,7 +813,8 @@ static int load_flat_file(struct linux_binprm *bprm,
  83. * image.
  84. */
  85. if (flags & FLAT_FLAG_GOTPIC) {
  86. - for (rp = (u32 __user *)datapos; ; rp++) {
  87. + rp = skip_got_header((u32 __user *) datapos);
  88. + for (; ; rp++) {
  89. u32 addr, rp_val;
  90. if (get_user(rp_val, rp))
  91. return -EFAULT;
  92. --
  93. cgit