[PATCH 1/2] s390: reject a relocation whose field lies outside the section
Harshit Kumar
harshit7kr@gmail.com
Thu Nov 26 10:03:06 GMT 2026
elf_s390_relocate_section does not check that a relocation's field lies
inside the section before the relocation is processed. An ordinary
relocation is caught later by _bfd_final_link_relocate, but only with
"reloc against `sym': error 4", and ld -r copies such a relocation
without any check. The TLS transitions read and rewrite the
instruction at R_390_TLS_LOAD, R_390_TLS_GDCALL and R_390_TLS_LDCALL
before any check, and those relocations have no howto size: a crafted
object made ld read past the section.
Check the field at the top of the relocate loop, before the
relocatable-link handling, so ld -r rejects such an object too. The
TLS relocations get the size of the instruction the transitions access:
6 octets (lg or brasl) for s390x, and 4 for s390, where the 6-octet
brasl call is checked where it is rewritten. R_390_NONE has no field
and is not checked. The message names the relocation.
bfd/
* elf32-s390.c (elf_s390_reloc_offset_error): New function.
(elf_s390_reloc_offset_in_range): New function.
(elf_s390_reloc_field_octets): New function.
(elf_s390_relocate_section): Reject a relocation whose field lies
outside the section.
* elf64-s390.c (elf_s390_reloc_offset_error): New function.
(elf_s390_reloc_offset_in_range): New function.
(elf_s390_reloc_field_octets): New function.
(elf_s390_relocate_section): Reject a relocation whose field lies
outside the section.
ld/
* testsuite/ld-s390/reloc-offset-bounds.s: New file.
* testsuite/ld-s390/reloc-offset-bounds_31.d: New test.
* testsuite/ld-s390/reloc-offset-bounds_64.d: New test.
* testsuite/ld-s390/reloc-offset-bounds-r_64.d: New test.
* testsuite/ld-s390/reloc-offset-fit_64.d: New test.
* testsuite/ld-s390/reloc-offset-fit_64.s: New file.
* testsuite/ld-s390/reloc-tls-gdcall_64.d: New test.
* testsuite/ld-s390/reloc-tls-gdcall_64.s: New file.
* testsuite/ld-s390/s390.exp: Run them.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elf32-s390.c | 75 +++++++++++++++++++
bfd/elf64-s390.c | 74 ++++++++++++++++++
.../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/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 | 6 ++
11 files changed, 210 insertions(+)
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_64.d
create mode 100644 ld/testsuite/ld-s390/reloc-tls-gdcall_64.s
diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c
index 1f2e94aa94..56edfd9242 100644
--- a/bfd/elf32-s390.c
+++ b/bfd/elf32-s390.c
@@ -1977,6 +1977,67 @@ tpoff (struct bfd_link_info *info, bfd_vma address)
return htab->tls_size + htab->tls_sec->vma - address;
}
+/* Report a relocation NAME whose field does not lie inside its section.
+ A relocation at OFFSET accesses a field of SIZE octets, and the
+ section holds only LIMIT octets. */
+
+static void
+elf_s390_reloc_offset_error (const bfd *abfd, const asection *sec,
+ const char *name, bfd_vma offset, bfd_vma size,
+ bfd_vma limit)
+{
+ if (offset > limit)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " > %#" PRIx64 ") "
+ "for %s in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) limit, name, sec);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " + %#" PRIx64 " > %#" PRIx64 ") "
+ "for %s in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) size, (uint64_t) limit, name, sec);
+ bfd_set_error (bfd_error_bad_value);
+}
+
+/* Return true if a field of SIZE octets at OFFSET lies inside SEC. The
+ section contents hold LIMIT octets. NAME is the relocation, for the
+ diagnostic. */
+
+static bool
+elf_s390_reloc_offset_in_range (const bfd *abfd, const asection *sec,
+ const char *name, bfd_vma offset,
+ bfd_vma size, bfd_vma limit)
+{
+ if (offset > limit || size > limit - offset)
+ {
+ elf_s390_reloc_offset_error (abfd, sec, name, offset, size, limit);
+ return false;
+ }
+ return true;
+}
+
+/* Return the number of octets the relocation of type R_TYPE accesses in
+ the section contents. R_390_TLS_LOAD, R_390_TLS_GDCALL and
+ R_390_TLS_LDCALL have no howto size, but the TLS transitions read and
+ rewrite the instruction at their offset; a 6-octet brasl
+ call is checked where it is rewritten. */
+
+static bfd_vma
+elf_s390_reloc_field_octets (unsigned int r_type, reloc_howto_type *howto)
+{
+ switch (r_type)
+ {
+ case R_390_TLS_LOAD:
+ case R_390_TLS_GDCALL:
+ case R_390_TLS_LDCALL:
+ return 4;
+ default:
+ return bfd_get_reloc_size (howto);
+ }
+}
+
/* Complain if TLS instruction relocation is against an invalid
instruction. */
@@ -2057,6 +2118,20 @@ elf_s390_relocate_section (struct bfd_link_info *info,
}
howto = elf_howto_table + r_type;
+
+ /* The relocation applies a field to the section contents, so the
+ field must lie inside the section. R_390_NONE has no field.
+ The check runs before the relocatable-link handling, so ld -r
+ rejects a bad offset too. */
+ if (r_type != R_390_NONE
+ && !elf_s390_reloc_offset_in_range (input_bfd, input_section,
+ howto->name, rel->r_offset,
+ elf_s390_reloc_field_octets
+ (r_type, howto),
+ bfd_get_section_limit_octets
+ (input_bfd, input_section)))
+ return false;
+
r_symndx = ELF32_R_SYM (rel->r_info);
h = NULL;
diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 23482e2646..f1d8042d1d 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -2264,6 +2264,66 @@ tpoff (struct bfd_link_info *info, bfd_vma address)
return htab->tls_size + htab->tls_sec->vma - address;
}
+/* Report a relocation NAME whose field does not lie inside its section.
+ A relocation at OFFSET accesses a field of SIZE octets, and the
+ section holds only LIMIT octets. */
+
+static void
+elf_s390_reloc_offset_error (const bfd *abfd, const asection *sec,
+ const char *name, bfd_vma offset, bfd_vma size,
+ bfd_vma limit)
+{
+ if (offset > limit)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " > %#" PRIx64 ") "
+ "for %s in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) limit, name, sec);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64 " + %#" PRIx64 " > %#" PRIx64 ") "
+ "for %s in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) size, (uint64_t) limit, name, sec);
+ bfd_set_error (bfd_error_bad_value);
+}
+
+/* Return true if a field of SIZE octets at OFFSET lies inside SEC. The
+ section contents hold LIMIT octets. NAME is the relocation, for the
+ diagnostic. */
+
+static bool
+elf_s390_reloc_offset_in_range (const bfd *abfd, const asection *sec,
+ const char *name, bfd_vma offset,
+ bfd_vma size, bfd_vma limit)
+{
+ if (offset > limit || size > limit - offset)
+ {
+ elf_s390_reloc_offset_error (abfd, sec, name, offset, size, limit);
+ return false;
+ }
+ return true;
+}
+
+/* Return the number of octets the relocation of type R_TYPE accesses in
+ the section contents. R_390_TLS_LOAD, R_390_TLS_GDCALL and
+ R_390_TLS_LDCALL have no howto size, but the TLS transitions read and
+ rewrite the instruction at their offset, a 6-octet lg or brasl. */
+
+static bfd_vma
+elf_s390_reloc_field_octets (unsigned int r_type, reloc_howto_type *howto)
+{
+ switch (r_type)
+ {
+ case R_390_TLS_LOAD:
+ case R_390_TLS_GDCALL:
+ case R_390_TLS_LDCALL:
+ return 6;
+ default:
+ return bfd_get_reloc_size (howto);
+ }
+}
+
/* Complain if TLS instruction relocation is against an invalid
instruction. */
@@ -2346,6 +2406,20 @@ elf_s390_relocate_section (struct bfd_link_info *info,
}
howto = elf_howto_table + r_type;
+
+ /* The relocation applies a field to the section contents, so the
+ field must lie inside the section. R_390_NONE has no field.
+ The check runs before the relocatable-link handling, so ld -r
+ rejects a bad offset too. */
+ if (r_type != R_390_NONE
+ && !elf_s390_reloc_offset_in_range (input_bfd, input_section,
+ howto->name, rel->r_offset,
+ elf_s390_reloc_field_octets
+ (r_type, howto),
+ bfd_get_section_limit_octets
+ (input_bfd, input_section)))
+ return false;
+
r_symndx = ELF64_R_SYM (rel->r_info);
h = NULL;
diff --git a/ld/testsuite/ld-s390/reloc-offset-bounds-r_64.d b/ld/testsuite/ld-s390/reloc-offset-bounds-r_64.d
new file mode 100644
index 0000000000..fd62007dff
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-bounds-r_64.d
@@ -0,0 +1,5 @@
+#name: s390 relocation field outside the section is rejected by ld -r
+#source: reloc-offset-bounds.s
+#as: -m64
+#ld: -r -melf64_s390
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_390_32 in section `\.data'
diff --git a/ld/testsuite/ld-s390/reloc-offset-bounds.s b/ld/testsuite/ld-s390/reloc-offset-bounds.s
new file mode 100644
index 0000000000..e66fe1d854
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-bounds.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ br %r14
+ .data
+ .long 0
+ /* A 4-byte R_390_32 field at offset 2 of a 4-byte section runs two
+ octets past the end. */
+ .reloc 2, R_390_32, _start
diff --git a/ld/testsuite/ld-s390/reloc-offset-bounds_31.d b/ld/testsuite/ld-s390/reloc-offset-bounds_31.d
new file mode 100644
index 0000000000..8a771fcbed
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-bounds_31.d
@@ -0,0 +1,5 @@
+#name: s390 relocation field outside the section is rejected (31-bit)
+#source: reloc-offset-bounds.s
+#as: -m31
+#ld: -melf_s390
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_390_32 in section `\.data'
diff --git a/ld/testsuite/ld-s390/reloc-offset-bounds_64.d b/ld/testsuite/ld-s390/reloc-offset-bounds_64.d
new file mode 100644
index 0000000000..b56dc59ab8
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-bounds_64.d
@@ -0,0 +1,5 @@
+#name: s390 relocation field outside the section is rejected
+#source: reloc-offset-bounds.s
+#as: -m64
+#ld: -melf64_s390
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_390_32 in section `\.data'
diff --git a/ld/testsuite/ld-s390/reloc-offset-fit_64.d b/ld/testsuite/ld-s390/reloc-offset-fit_64.d
new file mode 100644
index 0000000000..6359656c05
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-fit_64.d
@@ -0,0 +1,9 @@
+#name: s390 relocation field ending at the section end still links
+#source: reloc-offset-fit_64.s
+#as: -m64
+#ld: -melf64_s390
+#objdump: -s -j .data
+#...
+Contents of section \.data:
+ [0-9a-f]+ [0-9a-f]{8} [0-9a-f]{8} .*
+#pass
diff --git a/ld/testsuite/ld-s390/reloc-offset-fit_64.s b/ld/testsuite/ld-s390/reloc-offset-fit_64.s
new file mode 100644
index 0000000000..f62aecb47c
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-offset-fit_64.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ br %r14
+ .data
+ /* An 8-byte R_390_64 field that ends exactly at the end of this
+ 8-byte section is valid and must link. */
+ .quad _start
diff --git a/ld/testsuite/ld-s390/reloc-tls-gdcall_64.d b/ld/testsuite/ld-s390/reloc-tls-gdcall_64.d
new file mode 100644
index 0000000000..3f9f123dcd
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-tls-gdcall_64.d
@@ -0,0 +1,5 @@
+#name: s390 TLS_GDCALL instruction outside the section is rejected
+#source: reloc-tls-gdcall_64.s
+#as: -m64
+#ld: -melf64_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_64.s b/ld/testsuite/ld-s390/reloc-tls-gdcall_64.s
new file mode 100644
index 0000000000..59a9d23c0b
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-tls-gdcall_64.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ /* R_390_TLS_GDCALL marks a 6-octet brasl, which runs past this
+ 4-octet section. */
+ .long 0
+ .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 80536e2e39..a3e9488c6e 100644
--- a/ld/testsuite/ld-s390/s390.exp
+++ b/ld/testsuite/ld-s390/s390.exp
@@ -177,6 +177,7 @@ set s390xtests {
if { [istarget "s390-*-*"] || [istarget "s390x-*-*"] } {
run_ld_link_tests $s390tests
+ run_dump_test "reloc-offset-bounds_31"
run_dump_test "pr26918-1"
run_dump_test "reloccheck-1"
run_dump_test "reloctlsle-1"
@@ -197,4 +198,9 @@ if [istarget "s390x-*-*"] {
# Test --no-ld-generated-unwind-info
run_dump_test "sframe-command-line-2"
}
+
+ run_dump_test "reloc-offset-bounds_64"
+ run_dump_test "reloc-offset-bounds-r_64"
+ run_dump_test "reloc-offset-fit_64"
+ run_dump_test "reloc-tls-gdcall_64"
}
--
2.55.0
More information about the Binutils
mailing list