[PATCH 0/2] aarch64: reject out-of-range relocations instead of writing past the section

Harshit Kumar harshit7kr@gmail.com
Tue Nov 24 07:31:25 GMT 2026


elfNN_aarch64_relocate_section and elfNN_aarch64_tls_relax write into
the section contents at the relocation offset without checking that the
field, or the relaxed instruction sequence, lies inside the section.
A crafted object can make ld read and write past the section.
AddressSanitizer on current master reports:

  - a relocation field at the section end: heap-buffer-overflow in
    _bfd_aarch64_elf_put_addend;
  - R_AARCH64_TLS_DTPREL in a short non-alloc section: an 8-octet store
    where the howto size is 4;
  - a TLS GD/TLSDESC relaxation whose 12 to 20 octet sequence runs past
    a short section;
  - a truncated TLS relaxation sequence: a read past the relocation
    array (only BFD_ASSERT guards it, which the default handler reports
    and then continues out of bounds).

elf64-x86-64.c and elf32-i386.c already range-check the relocation
offset.  This is the per-backend form Alan suggested when he declined my
generic RFC; the RISC-V series (PR ld/34987) does the same for RISC-V:
https://inbox.sourceware.org/binutils/awTNk-zhXssks5h_@squeak.grove.modra.org/

Patch 1 adds the same check, widened to the real write size so it also
covers the DTPREL store; it skips R_AARCH64_NONE, which has no field,
and names the relocation in the message.  Patch 2 bounds the TLS
relaxation spans and the relocation sequence length.

Tested on aarch64-linux-gnu, aarch64_be-linux-gnu and
aarch64-linux-gnu_ilp32, on master a2f37c50c7 (the patches apply
unchanged to ba2f46b2e4): aarch64-elf.exp gains only the 7 new tests
(302 -> 309, 295 -> 302 and 250 -> 257 expected passes), and every other
result is unchanged, including the failures big-endian and ILP32 already
have on master.  Each patch builds on its
own.  The rejection tests fail before their patch and pass after; the
two tests with a field ending exactly at the section end link with and
without the series.  An ASan run over every AArch64 relocation type
(LP64 and ILP32, both byte orders; static, --relax, -shared and -r)
found no remaining out-of-bounds access, and none of its objects whose
fields lie inside their section was rejected.

ILP32 shares this code; R_AARCH64_P32_TLS_DTPREL maps to the same
8-octet store and is covered by the same width check.

Both patches touch only AArch64, so they need an AArch64 maintainer's
ack.

Harshit Kumar (2):
  aarch64: reject a relocation whose field lies outside the section
  aarch64: bound the TLS relaxation spans and sequence length

 bfd/elfnn-aarch64.c                           | 129 +++++++++++++++++-
 ld/testsuite/ld-aarch64/aarch64-elf.exp       |   9 ++
 .../ld-aarch64/reloc-offset-bounds-r.d        |   4 +
 ld/testsuite/ld-aarch64/reloc-offset-bounds.d |   4 +
 ld/testsuite/ld-aarch64/reloc-offset-bounds.s |   9 ++
 ld/testsuite/ld-aarch64/reloc-offset-fit.d    |   8 ++
 ld/testsuite/ld-aarch64/reloc-offset-fit.s    |   8 ++
 ld/testsuite/ld-aarch64/reloc-offset-marker.d |   8 ++
 ld/testsuite/ld-aarch64/reloc-offset-marker.s |   9 ++
 ld/testsuite/ld-aarch64/reloc-tls-dtprel.d    |   4 +
 ld/testsuite/ld-aarch64/reloc-tls-dtprel.s    |  12 ++
 ld/testsuite/ld-aarch64/reloc-tls-seq.d       |   4 +
 ld/testsuite/ld-aarch64/reloc-tls-seq.s       |  13 ++
 ld/testsuite/ld-aarch64/reloc-tls-span.d      |   4 +
 ld/testsuite/ld-aarch64/reloc-tls-span.s      |  13 ++
 15 files changed, 236 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-bounds-r.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-bounds.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-bounds.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-fit.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-fit.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-marker.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-offset-marker.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-dtprel.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-dtprel.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-seq.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-seq.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-span.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-span.s


base-commit: ba2f46b2e4dd20ebb744de86ca5361d17c468035
-- 
2.55.0




More information about the Binutils mailing list