[PATCH 1/2] aarch64: reject a relocation whose field lies outside the section
Harshit Kumar
harshit7kr@gmail.com
Tue Nov 24 07:31:26 GMT 2026
elfNN_aarch64_relocate_section applied each relocation without checking
that the relocated field lies inside the section, so a crafted object
with a bad r_offset made the linker read and write past the section
contents. elf64-x86-64.c and elf32-i386.c already range-check.
Add the check at the top of the relocate loop, before the relocatable
link test, so ld -r rejects such an object too. The written field can
be wider than the howto size: R_AARCH64_TLS_DTPREL in a non-alloc
section is stored with bfd_put_64, so a helper returns the real write
width. R_AARCH64_NONE has no field and is not checked. The message
names the relocation.
bfd/
* elfnn-aarch64.c (aarch64_reloc_offset_error): New function.
(aarch64_reloc_offset_in_range): New function.
(aarch64_reloc_write_octets): New function.
(elfNN_aarch64_relocate_section): Reject a relocation whose field
lies outside the section.
ld/
* testsuite/ld-aarch64/reloc-offset-bounds.s: New test.
* testsuite/ld-aarch64/reloc-offset-bounds.d: New test.
* testsuite/ld-aarch64/reloc-offset-bounds-r.d: New test.
* testsuite/ld-aarch64/reloc-offset-fit.s: New test.
* testsuite/ld-aarch64/reloc-offset-fit.d: New test.
* testsuite/ld-aarch64/reloc-offset-marker.s: New test.
* testsuite/ld-aarch64/reloc-offset-marker.d: New test.
* testsuite/ld-aarch64/reloc-tls-dtprel.s: New test.
* testsuite/ld-aarch64/reloc-tls-dtprel.d: New test.
* testsuite/ld-aarch64/aarch64-elf.exp: Run them.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elfnn-aarch64.c | 70 +++++++++++++++++++
ld/testsuite/ld-aarch64/aarch64-elf.exp | 7 ++
.../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 ++++
11 files changed, 143 insertions(+)
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
diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index dbb29549df..c6643fe2c5 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -5739,6 +5739,62 @@ aarch64_relocation_aginst_gp_p (bfd_reloc_code_real_type reloc)
|| reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G1);
}
+/* Report a relocation NAME whose field does not lie inside its section.
+ A relocation at OFFSET writes a field of SIZE octets, and the section
+ holds only LIMIT octets. */
+
+static void
+aarch64_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. Some AArch64 relocations and TLS
+ relaxations write a field wider than the howto size records, so the
+ span is passed explicitly rather than taken from the howto. NAME is
+ the relocation, for the diagnostic. */
+
+static bool
+aarch64_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)
+ {
+ aarch64_reloc_offset_error (abfd, sec, name, offset, size, limit);
+ return false;
+ }
+ return true;
+}
+
+/* Return the number of octets the relocation of type R_TYPE writes into
+ the section contents. R_AARCH64_TLS_DTPREL in a non-alloc section is
+ stored with bfd_put_64, which is wider than its howto size, so the
+ written field does not match bfd_get_reloc_size. */
+
+static bfd_vma
+aarch64_reloc_write_octets (unsigned int r_type, reloc_howto_type *howto)
+{
+ if (r_type == AARCH64_R (TLS_DTPREL))
+ return 8;
+ return bfd_get_reloc_size (howto);
+}
+
/* Perform a relocation as part of a final link. The input relocation type
should be TLS relaxed. */
@@ -7100,6 +7156,20 @@ elfNN_aarch64_relocate_section (struct bfd_link_info *info,
if (howto == NULL)
return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
+ /* The relocation applies a field to the section contents, so the
+ field must lie inside the section. R_AARCH64_NONE has no field.
+ The check runs before the relocatable-link test, so ld -r rejects
+ a bad offset too. A wider TLS relaxation span is checked where
+ it is written. */
+ if (howto->type != R_AARCH64_NONE
+ && !aarch64_reloc_offset_in_range (input_bfd, input_section,
+ howto->name, rel->r_offset,
+ aarch64_reloc_write_octets
+ (r_type, howto),
+ bfd_get_section_limit_octets
+ (input_bfd, input_section)))
+ return false;
+
bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
h = NULL;
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index f4941b0151..fb5dbccaef 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -331,6 +331,13 @@ run_dump_test "tls-relax-gd-ie-3"
run_dump_test "tls-relax-ie-le-4"
run_dump_test "tls-gd-symbolic"
+# Relocation field bounds (PR ld/NNNNN).
+run_dump_test_lp64 "reloc-offset-bounds"
+run_dump_test_lp64 "reloc-offset-bounds-r"
+run_dump_test_lp64 "reloc-offset-fit"
+run_dump_test_lp64 "reloc-offset-marker"
+run_dump_test_lp64 "reloc-tls-dtprel"
+
# ifunc tests
run_dump_test "ifunc-1"
run_dump_test "ifunc-1-local"
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-bounds-r.d b/ld/testsuite/ld-aarch64/reloc-offset-bounds-r.d
new file mode 100644
index 0000000000..303a6789ca
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-bounds-r.d
@@ -0,0 +1,4 @@
+#name: AArch64 relocation field outside the section is rejected by ld -r
+#source: reloc-offset-bounds.s
+#ld: -r
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_AARCH64_ABS32 in section `\.data'
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-bounds.d b/ld/testsuite/ld-aarch64/reloc-offset-bounds.d
new file mode 100644
index 0000000000..11629cd62e
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-bounds.d
@@ -0,0 +1,4 @@
+#name: AArch64 relocation field outside the section is rejected
+#source: reloc-offset-bounds.s
+#ld:
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_AARCH64_ABS32 in section `\.data'
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-bounds.s b/ld/testsuite/ld-aarch64/reloc-offset-bounds.s
new file mode 100644
index 0000000000..bdf470bb24
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-bounds.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ ret
+ .data
+ .word 0
+ /* A 4-byte R_AARCH64_ABS32 field at offset 2 of a 4-byte section
+ runs two octets past the end. */
+ .reloc 2, R_AARCH64_ABS32, _start
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-fit.d b/ld/testsuite/ld-aarch64/reloc-offset-fit.d
new file mode 100644
index 0000000000..7e4e6ab61a
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-fit.d
@@ -0,0 +1,8 @@
+#name: AArch64 relocation field ending at the section end still links
+#source: reloc-offset-fit.s
+#ld:
+#objdump: -s -j .data
+#...
+Contents of section \.data:
+ [0-9a-f]+ [0-9a-f]{8} .*
+#pass
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-fit.s b/ld/testsuite/ld-aarch64/reloc-offset-fit.s
new file mode 100644
index 0000000000..3e3c0a04bd
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-fit.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ ret
+ .data
+ /* A 4-byte R_AARCH64_ABS32 field that ends exactly at the end of
+ this 4-byte section is valid and must link. */
+ .word _start
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-marker.d b/ld/testsuite/ld-aarch64/reloc-offset-marker.d
new file mode 100644
index 0000000000..9e4a3a5736
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-marker.d
@@ -0,0 +1,8 @@
+#name: AArch64 zero-width marker at the section end still links
+#source: reloc-offset-marker.s
+#ld:
+#objdump: -s -j .data
+#...
+Contents of section \.data:
+ [0-9a-f]+ 00000000 .*
+#pass
diff --git a/ld/testsuite/ld-aarch64/reloc-offset-marker.s b/ld/testsuite/ld-aarch64/reloc-offset-marker.s
new file mode 100644
index 0000000000..dc07eecce3
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-offset-marker.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ ret
+ .data
+ .word 0
+ /* A zero-width R_AARCH64_NONE marker at the section end is valid
+ and must still link. */
+ .reloc 4, R_AARCH64_NONE, _start
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-dtprel.d b/ld/testsuite/ld-aarch64/reloc-tls-dtprel.d
new file mode 100644
index 0000000000..b147b1d0e3
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-dtprel.d
@@ -0,0 +1,4 @@
+#name: AArch64 TLS DTPREL store outside the section is rejected
+#source: reloc-tls-dtprel.s
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x8 > 0x4\) for R_AARCH64_TLS_DTPREL64 in section `\.probe'
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-dtprel.s b/ld/testsuite/ld-aarch64/reloc-tls-dtprel.s
new file mode 100644
index 0000000000..cf340a2b1f
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-dtprel.s
@@ -0,0 +1,12 @@
+ .text
+ .globl _start
+_start:
+ ret
+ .section .tdata,"awT",%progbits
+tv:
+ .word 0
+ .section .probe,"",%progbits
+ .word 0
+ /* R_AARCH64_TLS_DTPREL stores 8 octets, past the end of this
+ 4-octet non-alloc section. */
+ .reloc 0, R_AARCH64_TLS_DTPREL64, tv
--
2.55.0
More information about the Binutils
mailing list