[PATCH 0/2] arm: reject out-of-range relocations instead of reading and writing past the section
Harshit Kumar
harshit7kr@gmail.com
Tue Nov 24 08:00:15 GMT 2026
elf32_arm_relocate_section applies relocations at contents + r_offset
without checking that the field lies inside the section, and three
other paths read an instruction away from the field without checking
it either. 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
elf32_arm_final_link_relocate;
- R_ARM_LDR_PC_G0 and R_ARM_THM_BF16/12/18 near the section end: a
4-octet access where the howto size is 1 or 2;
- R_ARM_TLS_GOTDESC whose field holds a distance pointing outside the
section: a read before or after the section, or far away;
- R_ARM_V4BX at the section end with --fix-v4bx-interworking: a read
in bfd_elf32_arm_process_before_allocation, before any relocation
is applied;
- R_ARM_THM_TLS_DESCSEQ on an unexpected 32-bit instruction at the
section end: a read for the error message.
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, as in the AArch64 series sent earlier today:
https://inbox.sourceware.org/binutils/awTNk-zhXssks5h_@squeak.grove.modra.org/
Patch 1 adds the field check at the top of the relocate loop, before
the relocatable-link handling (which rewrites REL addends in the
contents), widened to the real access size; it skips R_ARM_NONE and
names the relocation in the message. Patch 2 checks the three
instruction reads that are not at the field.
Tested on arm-linux-gnueabi and armeb-linux-gnueabi, on master
ba2f46b2e4: arm-elf.exp gains only the 8 new tests (333 -> 341 and
331 -> 339 expected passes, no failures), and the rest of check-ld is
unchanged. Patch 1 builds and passes 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 ARM relocation type (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. Real code built with clang (ARM and Thumb, both byte orders)
links byte-identically.
Both patches touch only ARM, so they need an ARM maintainer's ack.
Harshit Kumar (2):
arm: reject a relocation whose field lies outside the section
arm: check the instructions that relocations read beside their field
bfd/elf32-arm.c | 122 +++++++++++++++++++-
ld/testsuite/ld-arm/arm-elf.exp | 10 ++
ld/testsuite/ld-arm/reloc-bf-span.d | 5 +
ld/testsuite/ld-arm/reloc-bf-span.s | 11 ++
ld/testsuite/ld-arm/reloc-ldr-pc-g0.d | 4 +
ld/testsuite/ld-arm/reloc-ldr-pc-g0.s | 9 ++
ld/testsuite/ld-arm/reloc-offset-bounds-r.d | 4 +
ld/testsuite/ld-arm/reloc-offset-bounds.d | 4 +
ld/testsuite/ld-arm/reloc-offset-bounds.s | 10 ++
ld/testsuite/ld-arm/reloc-offset-fit.d | 8 ++
ld/testsuite/ld-arm/reloc-offset-fit.s | 9 ++
ld/testsuite/ld-arm/reloc-offset-marker.d | 8 ++
ld/testsuite/ld-arm/reloc-offset-marker.s | 9 ++
ld/testsuite/ld-arm/reloc-tls-gotdesc.d | 4 +
ld/testsuite/ld-arm/reloc-tls-gotdesc.s | 12 ++
ld/testsuite/ld-arm/reloc-v4bx.d | 4 +
ld/testsuite/ld-arm/reloc-v4bx.s | 7 ++
17 files changed, 234 insertions(+), 6 deletions(-)
create mode 100644 ld/testsuite/ld-arm/reloc-bf-span.d
create mode 100644 ld/testsuite/ld-arm/reloc-bf-span.s
create mode 100644 ld/testsuite/ld-arm/reloc-ldr-pc-g0.d
create mode 100644 ld/testsuite/ld-arm/reloc-ldr-pc-g0.s
create mode 100644 ld/testsuite/ld-arm/reloc-offset-bounds-r.d
create mode 100644 ld/testsuite/ld-arm/reloc-offset-bounds.d
create mode 100644 ld/testsuite/ld-arm/reloc-offset-bounds.s
create mode 100644 ld/testsuite/ld-arm/reloc-offset-fit.d
create mode 100644 ld/testsuite/ld-arm/reloc-offset-fit.s
create mode 100644 ld/testsuite/ld-arm/reloc-offset-marker.d
create mode 100644 ld/testsuite/ld-arm/reloc-offset-marker.s
create mode 100644 ld/testsuite/ld-arm/reloc-tls-gotdesc.d
create mode 100644 ld/testsuite/ld-arm/reloc-tls-gotdesc.s
create mode 100644 ld/testsuite/ld-arm/reloc-v4bx.d
create mode 100644 ld/testsuite/ld-arm/reloc-v4bx.s
base-commit: ba2f46b2e4dd20ebb744de86ca5361d17c468035
--
2.55.0
More information about the Binutils
mailing list