[PATCH 1/2] arm: reject a relocation whose field lies outside the section

Harshit Kumar harshit7kr@gmail.com
Tue Nov 24 08:00:16 GMT 2026


elf32_arm_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 handling, which rewrites REL addends in the contents, so ld -r
rejects such an object too.  The accessed field can be wider than the
howto size: the R_ARM_THM_BF16, R_ARM_THM_BF12 and R_ARM_THM_BF18
relocations patch both halfwords of a 32-bit Thumb instruction, and
R_ARM_LDR_PC_G0 a 32-bit ARM instruction, so a helper returns the real
width.  R_ARM_NONE has no field and is not checked.  The message names
the relocation.

bfd/
	* elf32-arm.c (arm_reloc_offset_error): New function.
	(arm_reloc_offset_in_range): New function.
	(arm_reloc_write_octets): New function.
	(elf32_arm_relocate_section): Reject a relocation whose field lies
	outside the section.

ld/
	* testsuite/ld-arm/reloc-offset-bounds.s: New test.
	* testsuite/ld-arm/reloc-offset-bounds.d: New test.
	* testsuite/ld-arm/reloc-offset-bounds-r.d: New test.
	* testsuite/ld-arm/reloc-offset-fit.s: New test.
	* testsuite/ld-arm/reloc-offset-fit.d: New test.
	* testsuite/ld-arm/reloc-offset-marker.s: New test.
	* testsuite/ld-arm/reloc-offset-marker.d: New test.
	* testsuite/ld-arm/reloc-bf-span.s: New test.
	* testsuite/ld-arm/reloc-bf-span.d: New test.
	* testsuite/ld-arm/reloc-ldr-pc-g0.s: New test.
	* testsuite/ld-arm/reloc-ldr-pc-g0.d: New test.
	* testsuite/ld-arm/arm-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elf32-arm.c                             | 75 +++++++++++++++++++++
 ld/testsuite/ld-arm/arm-elf.exp             |  8 +++
 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 +++
 13 files changed, 164 insertions(+)
 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

diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 17963aa9cc..1bca033c60 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -7735,6 +7735,67 @@ check_use_blx (struct elf32_arm_link_hash_table *globals)
     }
 }
 
+/* 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
+arm_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
+arm_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)
+    {
+      arm_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.  The R_ARM_THM_BF* relocations patch both
+   halfwords of a 32-bit Thumb instruction, and R_ARM_LDR_PC_G0 a 32-bit
+   ARM instruction, although their howto size is smaller.  */
+
+static bfd_vma
+arm_reloc_write_octets (unsigned int r_type, reloc_howto_type *howto)
+{
+  switch (r_type)
+    {
+    case R_ARM_THM_BF16:
+    case R_ARM_THM_BF12:
+    case R_ARM_THM_BF18:
+    case R_ARM_LDR_PC_G0:
+      return 4;
+    default:
+      return bfd_get_reloc_size (howto);
+    }
+}
+
 bool
 bfd_elf32_arm_process_before_allocation (bfd *abfd,
 					 struct bfd_link_info *link_info)
@@ -13061,6 +13122,20 @@ elf32_arm_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_ARM_NONE has no field.  A
+	 few relocations patch a field wider than the howto size, so use
+	 the real width.  The check runs before the relocatable-link
+	 handling, which rewrites REL addends in the contents.  */
+      if (howto->type != R_ARM_NONE
+	  && !arm_reloc_offset_in_range (input_bfd, input_section,
+					 howto->name, rel->r_offset,
+					 arm_reloc_write_octets (r_type,
+								 howto),
+					 bfd_get_section_limit_octets
+					 (input_bfd, input_section)))
+	return false;
+
       h = NULL;
       sym = NULL;
       sec = NULL;
diff --git a/ld/testsuite/ld-arm/arm-elf.exp b/ld/testsuite/ld-arm/arm-elf.exp
index f50c0c1795..106db47e7f 100644
--- a/ld/testsuite/ld-arm/arm-elf.exp
+++ b/ld/testsuite/ld-arm/arm-elf.exp
@@ -1296,6 +1296,14 @@ run_dump_test "non-contiguous-arm5"
 run_dump_test "non-contiguous-arm6"
 run_dump_test "non-contiguous-arm7"
 
+# Relocation field bounds.
+run_dump_test "reloc-offset-bounds"
+run_dump_test "reloc-offset-bounds-r"
+run_dump_test "reloc-offset-fit"
+run_dump_test "reloc-offset-marker"
+run_dump_test "reloc-bf-span"
+run_dump_test "reloc-ldr-pc-g0"
+
 if { [check_shared_lib_support] } {
     run_dump_test "thumb-plt"
     run_dump_test "thumb-plt-got"
diff --git a/ld/testsuite/ld-arm/reloc-bf-span.d b/ld/testsuite/ld-arm/reloc-bf-span.d
new file mode 100644
index 0000000000..4b320923b1
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-bf-span.d
@@ -0,0 +1,5 @@
+#name: ARM Thumb BF relocation field outside the section is rejected
+#source: reloc-bf-span.s
+#as: -march=armv8.1-m.main
+#ld:
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_ARM_THM_BF16 in section `\.text'
diff --git a/ld/testsuite/ld-arm/reloc-bf-span.s b/ld/testsuite/ld-arm/reloc-bf-span.s
new file mode 100644
index 0000000000..85e58a1c16
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-bf-span.s
@@ -0,0 +1,11 @@
+	.syntax unified
+	.thumb
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	.hword 0xbf00, 0xbf00
+	/* R_ARM_THM_BF16 patches the second halfword of a 32-bit Thumb
+	   instruction, four octets from offset 2, past this 4-octet
+	   section.  Its howto size is two.  */
+	.reloc 2, R_ARM_THM_BF16, _start
diff --git a/ld/testsuite/ld-arm/reloc-ldr-pc-g0.d b/ld/testsuite/ld-arm/reloc-ldr-pc-g0.d
new file mode 100644
index 0000000000..fd0adffa9e
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-ldr-pc-g0.d
@@ -0,0 +1,4 @@
+#name: ARM LDR_PC_G0 relocation field outside the section is rejected
+#source: reloc-ldr-pc-g0.s
+#ld:
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_ARM_LDR_PC_G0 in section `\.text'
diff --git a/ld/testsuite/ld-arm/reloc-ldr-pc-g0.s b/ld/testsuite/ld-arm/reloc-ldr-pc-g0.s
new file mode 100644
index 0000000000..7916ac1868
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-ldr-pc-g0.s
@@ -0,0 +1,9 @@
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	bx lr
+	/* R_ARM_LDR_PC_G0 patches a 4-octet ARM instruction, although its
+	   howto size is one, so at offset 2 it runs past this 4-octet
+	   section.  */
+	.reloc 2, R_ARM_LDR_PC_G0, _start
diff --git a/ld/testsuite/ld-arm/reloc-offset-bounds-r.d b/ld/testsuite/ld-arm/reloc-offset-bounds-r.d
new file mode 100644
index 0000000000..c462ef2f2c
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-bounds-r.d
@@ -0,0 +1,4 @@
+#name: ARM 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_ARM_ABS32 in section `\.data'
diff --git a/ld/testsuite/ld-arm/reloc-offset-bounds.d b/ld/testsuite/ld-arm/reloc-offset-bounds.d
new file mode 100644
index 0000000000..fd6fd17e78
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-bounds.d
@@ -0,0 +1,4 @@
+#name: ARM relocation field outside the section is rejected
+#source: reloc-offset-bounds.s
+#ld:
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for R_ARM_ABS32 in section `\.data'
diff --git a/ld/testsuite/ld-arm/reloc-offset-bounds.s b/ld/testsuite/ld-arm/reloc-offset-bounds.s
new file mode 100644
index 0000000000..a18156b56d
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-bounds.s
@@ -0,0 +1,10 @@
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	bx lr
+	.data
+	.word 0
+	/* A 4-byte R_ARM_ABS32 field at offset 2 of a 4-byte section runs
+	   two octets past the end.  */
+	.reloc 2, R_ARM_ABS32, _start
diff --git a/ld/testsuite/ld-arm/reloc-offset-fit.d b/ld/testsuite/ld-arm/reloc-offset-fit.d
new file mode 100644
index 0000000000..a5bca7316b
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-fit.d
@@ -0,0 +1,8 @@
+#name: ARM 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-arm/reloc-offset-fit.s b/ld/testsuite/ld-arm/reloc-offset-fit.s
new file mode 100644
index 0000000000..00cdf3e33a
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-fit.s
@@ -0,0 +1,9 @@
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	bx lr
+	.data
+	/* A 4-byte R_ARM_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-arm/reloc-offset-marker.d b/ld/testsuite/ld-arm/reloc-offset-marker.d
new file mode 100644
index 0000000000..a9af276a81
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-marker.d
@@ -0,0 +1,8 @@
+#name: ARM 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-arm/reloc-offset-marker.s b/ld/testsuite/ld-arm/reloc-offset-marker.s
new file mode 100644
index 0000000000..9207bd81b4
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-offset-marker.s
@@ -0,0 +1,9 @@
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	bx lr
+	.data
+	.word 0
+	/* A zero-width R_ARM_NONE marker at the section end still links.  */
+	.reloc 4, R_ARM_NONE, _start
-- 
2.55.0




More information about the Binutils mailing list