[PATCH 2/2] s390: check the instructions that relocations access beside their field
Harshit Kumar
harshit7kr@gmail.com
Thu Nov 26 10:03:07 GMT 2026
Three paths access section contents away from the relocated field,
where the field check in elf_s390_relocate_section does not reach:
- R_390_GOTENT and R_390_GOT20: the GOT relaxation reads the
instruction that starts two octets before the field, without the
"rel->r_offset >= 2" test its sibling paths have, so a relocation at
offset 0 or 1 read before the section.
- R_390_PLT32DBL: in an executable, elf_s390_check_relocs (s390x) reads
the two octets before the field to tell larl from brasl. It runs
before any relocation is applied and checked only r_offset >= 2, so a
crafted offset read far outside the section.
- R_390_TLS_GDCALL and R_390_TLS_LDCALL on s390: for a brasl call the
TLS transitions write the 2 octets after the 4 they read, so a brasl
ending past the section was written past it. Check the 6-octet call
before rewriting it.
bfd/
* elf32-s390.c (elf_s390_relocate_section): Require r_offset >= 2
before relaxing R_390_GOTENT and R_390_GOT20. Check that a brasl
call marked by R_390_TLS_GDCALL or R_390_TLS_LDCALL lies inside the
section before rewriting it.
* elf64-s390.c (elf_s390_check_relocs): Read the instruction before
an R_390_PLT32DBL field only if the field starts inside the section.
(elf_s390_relocate_section): Require r_offset >= 2 before relaxing
R_390_GOTENT and R_390_GOT20.
ld/
* testsuite/ld-s390/reloc-tls-gdcall-brasl_31.d: New test.
* testsuite/ld-s390/reloc-tls-gdcall-brasl_31.s: New file.
* testsuite/ld-s390/s390.exp: Run it.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elf32-s390.c | 18 ++++++++++++++++++
bfd/elf64-s390.c | 6 +++++-
.../ld-s390/reloc-tls-gdcall-brasl_31.d | 5 +++++
.../ld-s390/reloc-tls-gdcall-brasl_31.s | 10 ++++++++++
ld/testsuite/ld-s390/s390.exp | 1 +
5 files changed, 39 insertions(+), 1 deletion(-)
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
diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c
index 56edfd9242..99d3b96e9e 100644
--- a/bfd/elf32-s390.c
+++ b/bfd/elf32-s390.c
@@ -2331,6 +2331,9 @@ elf_s390_relocate_section (struct bfd_link_info *info,
if (relax
&& h->def_regular
&& SYMBOL_REFERENCES_LOCAL (info, h)
+ /* The instruction starts two octets before the
+ field. */
+ && rel->r_offset >= 2
/* lrl rx,sym@GOTENT -> larl rx, sym */
&& ((r_type == R_390_GOTENT
&& (bfd_get_16 (input_bfd,
@@ -3040,6 +3043,13 @@ elf_s390_relocate_section (struct bfd_link_info *info,
invalid_tls_insn (input_bfd, input_section, rel);
return false;
}
+ /* A brasl call is 6 octets. */
+ if ((insn & 0xffff0000) == 0xc0e50000
+ && !elf_s390_reloc_offset_in_range
+ (input_bfd, input_section, howto->name, rel->r_offset,
+ 6, bfd_get_section_limit_octets (input_bfd,
+ input_section)))
+ return false;
if (!bfd_link_pic (info) && (h == NULL || h->dynindx == -1))
{
if ((insn & 0xff000000) == 0x0d000000)
@@ -3105,6 +3115,14 @@ elf_s390_relocate_section (struct bfd_link_info *info,
invalid_tls_insn (input_bfd, input_section, rel);
return false;
}
+ /* A brasl call is 6 octets. */
+ if ((insn & 0xffff0000) == 0xc0e50000
+ && !elf_s390_reloc_offset_in_range
+ (input_bfd, input_section, howto->name,
+ rel->r_offset, 6,
+ bfd_get_section_limit_octets (input_bfd,
+ input_section)))
+ return false;
if ((insn & 0xff000000) == 0x0d000000)
{
diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index f1d8042d1d..55c8dcc79f 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -1085,7 +1085,8 @@ elf_s390_check_relocs (bfd *abfd,
address taking and require pointer equality. */
if (bfd_link_executable (info)
&& r_type == R_390_PLT32DBL
- && rel->r_offset >= 2)
+ && rel->r_offset >= 2
+ && rel->r_offset <= bfd_get_section_limit_octets (abfd, sec))
{
bfd_byte *contents;
void *insn_start;
@@ -2638,6 +2639,9 @@ elf_s390_relocate_section (struct bfd_link_info *info,
if (relax
&& h->def_regular
&& SYMBOL_REFERENCES_LOCAL (info, h)
+ /* The instruction starts two octets before the
+ field. */
+ && rel->r_offset >= 2
/* lgrl rx,sym@GOTENT -> larl rx, sym */
&& ((r_type == R_390_GOTENT
&& (bfd_get_16 (input_bfd,
diff --git a/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.d b/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.d
new file mode 100644
index 0000000000..71c31dd82f
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.d
@@ -0,0 +1,5 @@
+#name: s390 TLS_GDCALL brasl outside the section is rejected (31-bit)
+#source: reloc-tls-gdcall-brasl_31.s
+#as: -m31
+#ld: -melf_s390
+#error: .*bad reloc offset \(0 \+ 0x6 > 0x4\) for R_390_TLS_GDCALL in section `\.text'
diff --git a/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.s b/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.s
new file mode 100644
index 0000000000..56f78edf25
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-tls-gdcall-brasl_31.s
@@ -0,0 +1,10 @@
+ .text
+ .globl _start
+_start:
+ /* The first four octets of a brasl, marked as a TLS call: the
+ GD->LE transition rewrites all six octets of it, which run past
+ this 4-octet section. */
+ .long 0xc0e50000
+ .reloc 0, R_390_TLS_GDCALL, tv
+ .section .tbss,"awT",@nobits
+tv: .zero 4
diff --git a/ld/testsuite/ld-s390/s390.exp b/ld/testsuite/ld-s390/s390.exp
index a3e9488c6e..de17a87934 100644
--- a/ld/testsuite/ld-s390/s390.exp
+++ b/ld/testsuite/ld-s390/s390.exp
@@ -178,6 +178,7 @@ if { [istarget "s390-*-*"] || [istarget "s390x-*-*"] } {
run_ld_link_tests $s390tests
run_dump_test "reloc-offset-bounds_31"
+ run_dump_test "reloc-tls-gdcall-brasl_31"
run_dump_test "pr26918-1"
run_dump_test "reloccheck-1"
run_dump_test "reloctlsle-1"
--
2.55.0
More information about the Binutils
mailing list