[PATCH 0/5] RISC-V: reject relocations that fall outside their section (PR 34987)

Harshit Kumar harshit7kr@gmail.com
Tue Nov 10 20:15:17 GMT 2026


The RISC-V relocate loop and the relaxation helpers access section
contents at rel->r_offset without checking that the field lies inside
the section.  A crafted object can make ld write out of bounds
(PR ld/34987):

  - R_RISCV_SUB_ULEB128 at the section end makes perform_relocation
    pass (size_t) -1 to memset.  ld on current master then aborts with
    "double free or corruption (out)".
  - R_RISCV_TLSDESC_CALL at the section end makes
    riscv_resolve_tlsdesc_lo_relocs write 4 bytes past the section
    (ASan: heap-buffer-overflow in bfd_putl32).
  - The relaxation helpers only BFD_ASSERT their spans, which logs an
    error and then continues.

An unterminated ULEB128 field is also linked silently with exit
status 0.

Patch 1 is a small, separate cleanup: it frees the PC-relative
relocation state on the two IFUNC error paths, which returned without
it.  Patch 2 adds a range check in riscv_elf_relocate_section, patch 3
handles the variable-length ULEB128 fields, patch 4 rejects an unpaired
trailing R_RISCV_SET_ULEB128, and patch 5 replaces the relaxation
asserts with real checks.  Patches 2-5 each carry the tests for their
own change: the error cases fail, several by crashing ld, before the
patch that adds them, and the controls pass throughout.

With the series:

  $ ld -m elf64lriscv uleb-fail.o -o out
  ld: uleb-fail.o: bad reloc offset (0x10 + 0x1 > 0x10) for section `.text'
  ld: final link failed: bad value

Tested on riscv64-unknown-elf, riscv32-unknown-elf and
riscv64-unknown-linux-gnu: no regressions in the gas and ld
testsuites, and bfd, gas and ld build cleanly with -Werror.  Each
patch was also built and tested on its own (riscv64-linux, ld-riscv-elf
tests): no failures at any point in the series.

Separately, crafted objects with an R_RISCV_DELETE ending exactly at
the section end hit a NULL symbol table dereference in
_riscv_relax_delete_bytes when local symbols are omitted.  That exists
without this series and is not addressed here.

Harshit Kumar (5):
  RISC-V: free PC-relative relocation state on IFUNC errors
  RISC-V: reject relocation offsets outside the section
  RISC-V: check ULEB128 relocation field bounds
  RISC-V: reject unclosed ULEB128 relocation pairs
  RISC-V: check relocation fields during relaxation

 bfd/elfnn-riscv.c                             | 194 ++++++++++-
 ld/testsuite/ld-riscv-elf/align-zero.s        |   5 +
 .../call-bounds-norelax-control.d             |  13 +
 .../ld-riscv-elf/call-bounds-norelax-fail.d   |   5 +
 .../ld-riscv-elf/call-bounds-norelax-fail.s   |   7 +
 .../call-bounds-norelax-rv32-fail.d           |   6 +
 .../call-bounds-norelax-rv32-pass.d           |  14 +
 .../ld-riscv-elf/call-bounds-norelax.s        |   8 +
 .../call-plt-bounds-norelax-control.d         |  13 +
 .../call-plt-bounds-norelax-fail.d            |   5 +
 .../call-plt-bounds-norelax-fail.s            |   7 +
 .../ld-riscv-elf/call-plt-bounds-norelax.s    |   6 +
 ld/testsuite/ld-riscv-elf/deferred-delete.d   |  20 ++
 ld/testsuite/ld-riscv-elf/deferred-delete.s   |  11 +
 ld/testsuite/ld-riscv-elf/delete-boundary.s   |   7 +
 ld/testsuite/ld-riscv-elf/delete-end.s        |   5 +
 ld/testsuite/ld-riscv-elf/huge-offset.s       |   8 +
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  38 +++
 .../ld-riscv-elf/relax-align-bounds-control.d |  18 ++
 .../ld-riscv-elf/relax-align-bounds-pass.s    |  15 +
 .../ld-riscv-elf/relax-align-bounds.d         |   5 +
 .../ld-riscv-elf/relax-align-bounds.s         |   6 +
 .../ld-riscv-elf/relax-call-bounds-control.d  |  13 +
 .../ld-riscv-elf/relax-call-bounds-pass.s     |   6 +
 ld/testsuite/ld-riscv-elf/relax-call-bounds.d |   5 +
 ld/testsuite/ld-riscv-elf/relax-call-bounds.s |   6 +
 .../ld-riscv-elf/relax-call-section-end.d     |  20 ++
 .../ld-riscv-elf/relax-call-section-end.s     |   9 +
 ld/testsuite/ld-riscv-elf/relax-jal-bounds.d  |   5 +
 ld/testsuite/ld-riscv-elf/relax-jal-bounds.s  |   8 +
 ld/testsuite/ld-riscv-elf/relax-lui-bounds.d  |   5 +
 ld/testsuite/ld-riscv-elf/relax-lui-bounds.s  |   7 +
 ld/testsuite/ld-riscv-elf/relax-marker-end.d  |  10 +
 ld/testsuite/ld-riscv-elf/relax-marker-end.s  |   5 +
 ld/testsuite/ld-riscv-elf/relax-none-bounds.d |  15 +
 ld/testsuite/ld-riscv-elf/relax-none-bounds.s |  12 +
 .../ld-riscv-elf/relax-pcrel-bounds.d         |   5 +
 .../ld-riscv-elf/relax-pcrel-bounds.s         |   7 +
 .../ld-riscv-elf/relax-tls-le-bounds.d        |   5 +
 .../ld-riscv-elf/relax-tls-le-bounds.s        |   7 +
 .../ld-riscv-elf/relax-tlsdesc-bounds.d       |   5 +
 .../ld-riscv-elf/relax-tlsdesc-bounds.s       |   7 +
 .../ld-riscv-elf/riscv-reloc-offset.exp       | 306 ++++++++++++++++++
 .../ld-riscv-elf/set32-reloc-bounds-control.d |  10 +
 .../ld-riscv-elf/set32-reloc-bounds-pass.s    |   9 +
 .../ld-riscv-elf/set32-reloc-bounds.d         |   5 +
 .../ld-riscv-elf/set32-reloc-bounds.s         |   9 +
 .../ld-riscv-elf/set8-reloc-bounds-control.d  |  10 +
 .../ld-riscv-elf/set8-reloc-bounds-pass.s     |   9 +
 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d |   5 +
 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s |   8 +
 .../tlsdesc-call-bounds-control.d             |  15 +
 .../ld-riscv-elf/tlsdesc-call-bounds-pass.s   |  18 ++
 .../tlsdesc-call-bounds-rv32-control.d        |  16 +
 .../tlsdesc-call-bounds-rv32-pass.s           |  18 ++
 .../ld-riscv-elf/tlsdesc-call-bounds-rv32.d   |   6 +
 .../ld-riscv-elf/tlsdesc-call-bounds-rv32.s   |  17 +
 .../ld-riscv-elf/tlsdesc-call-bounds.d        |   5 +
 .../ld-riscv-elf/tlsdesc-call-bounds.s        |  17 +
 .../ld-riscv-elf/uleb128-mismatched-offset.d  |   5 +
 .../ld-riscv-elf/uleb128-mismatched-offset.s  |  10 +
 .../ld-riscv-elf/uleb128-multibyte-last.d     |  10 +
 .../ld-riscv-elf/uleb128-multibyte-last.s     |  10 +
 .../uleb128-reloc-bounds-control.d            |  10 +
 .../ld-riscv-elf/uleb128-reloc-bounds-pass.s  |  10 +
 .../ld-riscv-elf/uleb128-reloc-bounds-rv32.d  |   6 +
 .../ld-riscv-elf/uleb128-reloc-bounds.d       |   5 +
 .../ld-riscv-elf/uleb128-reloc-bounds.s       |   9 +
 .../ld-riscv-elf/uleb128-set-after-set.d      |   5 +
 .../ld-riscv-elf/uleb128-set-after-set.s      |   9 +
 .../ld-riscv-elf/uleb128-sub-no-set.d         |   5 +
 .../ld-riscv-elf/uleb128-sub-no-set.s         |   8 +
 ld/testsuite/ld-riscv-elf/uleb128-unclosed.d  |   5 +
 ld/testsuite/ld-riscv-elf/uleb128-unclosed.s  |   8 +
 .../uleb128-unterminated-control.d            |  10 +
 .../ld-riscv-elf/uleb128-unterminated-pass.s  |  10 +
 .../ld-riscv-elf/uleb128-unterminated.d       |   5 +
 .../ld-riscv-elf/uleb128-unterminated.s       |  12 +
 78 files changed, 1200 insertions(+), 13 deletions(-)
 create mode 100644 ld/testsuite/ld-riscv-elf/align-zero.s
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.s
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-fail.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-pass.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax.s
 create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.d
 create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.s
 create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax.s
 create mode 100644 ld/testsuite/ld-riscv-elf/deferred-delete.d
 create mode 100644 ld/testsuite/ld-riscv-elf/deferred-delete.s
 create mode 100644 ld/testsuite/ld-riscv-elf/delete-boundary.s
 create mode 100644 ld/testsuite/ld-riscv-elf/delete-end.s
 create mode 100644 ld/testsuite/ld-riscv-elf/huge-offset.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-section-end.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-call-section-end.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-jal-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-jal-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-lui-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-lui-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-marker-end.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-marker-end.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-none-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-none-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-pcrel-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-pcrel-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-tls-le-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-tls-le-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-tlsdesc-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-tlsdesc-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/riscv-reloc-offset.exp
 create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.d
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.s
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-multibyte-last.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds-rv32.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-reloc-bounds.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unclosed.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unclosed.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated-control.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated-pass.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unterminated.s


base-commit: 49465fb034a8e92b01589cf0ce8db2843de4218c
-- 
2.55.0




More information about the Binutils mailing list