[PATCH 0/2] s390: reject out-of-range relocations instead of reading and writing past the section
Harshit Kumar
harshit7kr@gmail.com
Thu Nov 26 10:03:05 GMT 2026
Ordinary s390 relocations whose field lies outside the section are
already caught, late, by _bfd_final_link_relocate ("error 4"), but ld -r
copies them unchecked, and several paths access the contents before
that check or beside the field. AddressSanitizer on current master
reports, for crafted objects:
- R_390_TLS_LOAD, R_390_TLS_GDCALL and R_390_TLS_LDCALL near the
section end: the TLS transitions read and rewrite the instruction
(these relocations have no howto size);
- on s390, a brasl call marked as a TLS call at the section end: the
GD->LE transition writes past the section and the link succeeds;
- R_390_GOTENT or R_390_GOT20 at offset 0 or 1: the GOT relaxation
reads before the section;
- R_390_PLT32DBL at a crafted offset: elf_s390_check_relocs reads far
outside the section before any relocation is applied.
This is the per-backend form Alan suggested when he declined my generic
RFC, as for AArch64 and ARM:
https://inbox.sourceware.org/binutils/awTNk-zhXssks5h_@squeak.grove.modra.org/
Patch 1 checks the field at the top of the relocate loop, before the
relocatable-link handling, with the instruction size for the three TLS
relocations; it skips R_390_NONE and names the relocation in the
message. Patch 2 checks the accesses that are not at the field; it
includes the r_offset >= 2 test for the GOT relaxation.
Tested on s390x-linux-gnu, on master 9a69857ad3: check-ld gains only the
6 new tests (875 -> 881 expected passes, no failures); each patch builds
with -Werror and passes on its own. The rejection tests fail before
their patch and pass after; the test with a field ending exactly at the
section end links with and without the series. An ASan run over every
s390 relocation type (s390 and s390x; 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. Code built with clang for
s390x, including GD->LE TLS transitions, links byte-identically with and
without the series. The series is independent of the two s390 local
IFUNC patches on the list; they apply in either order.
Both patches touch only s390, so they need an s390 maintainer's ack.
Harshit Kumar (2):
s390: reject a relocation whose field lies outside the section
s390: check the instructions that relocations access beside their
field
bfd/elf32-s390.c | 93 +++++++++++++++++++
bfd/elf64-s390.c | 80 +++++++++++++++-
.../ld-s390/reloc-offset-bounds-r_64.d | 5 +
ld/testsuite/ld-s390/reloc-offset-bounds.s | 9 ++
ld/testsuite/ld-s390/reloc-offset-bounds_31.d | 5 +
ld/testsuite/ld-s390/reloc-offset-bounds_64.d | 5 +
ld/testsuite/ld-s390/reloc-offset-fit_64.d | 9 ++
ld/testsuite/ld-s390/reloc-offset-fit_64.s | 8 ++
.../ld-s390/reloc-tls-gdcall-brasl_31.d | 5 +
.../ld-s390/reloc-tls-gdcall-brasl_31.s | 10 ++
ld/testsuite/ld-s390/reloc-tls-gdcall_64.d | 5 +
ld/testsuite/ld-s390/reloc-tls-gdcall_64.s | 9 ++
ld/testsuite/ld-s390/s390.exp | 7 ++
13 files changed, 249 insertions(+), 1 deletion(-)
create mode 100644 ld/testsuite/ld-s390/reloc-offset-bounds-r_64.d
create mode 100644 ld/testsuite/ld-s390/reloc-offset-bounds.s
create mode 100644 ld/testsuite/ld-s390/reloc-offset-bounds_31.d
create mode 100644 ld/testsuite/ld-s390/reloc-offset-bounds_64.d
create mode 100644 ld/testsuite/ld-s390/reloc-offset-fit_64.d
create mode 100644 ld/testsuite/ld-s390/reloc-offset-fit_64.s
create mode 100644 ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.d
create mode 100644 ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.s
create mode 100644 ld/testsuite/ld-s390/reloc-tls-gdcall_64.d
create mode 100644 ld/testsuite/ld-s390/reloc-tls-gdcall_64.s
base-commit: 9a69857ad3ef825d3f4ba782f37f3f334b131c86
--
2.55.0
More information about the Binutils
mailing list