[PATCH 2/2] arm: check the instructions that relocations read beside their field

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


Three paths read section contents away from the relocated field, where
the field check in elf32_arm_relocate_section does not reach:

- R_ARM_TLS_GOTDESC: elf32_arm_final_link_relocate reads the field as
  the signed distance from the instruction that uses it, then reads
  that instruction.  The distance comes from the input, so a crafted
  value read before or far outside the section.  Check that the
  instruction, both halfwords of a 32-bit Thumb one, lies inside the
  section; it may come before or after the field.
- R_ARM_V4BX: with --fix-v4bx-interworking,
  bfd_elf32_arm_process_before_allocation reads the BX instruction
  before any relocation is applied.  Check it there.
- R_ARM_THM_TLS_DESCSEQ: elf32_arm_tls_relax read the second halfword
  of an unexpected 32-bit instruction for its error message even at the
  end of the section.  Read it only if it lies inside the section.

bfd/
	* elf32-arm.c (bfd_elf32_arm_process_before_allocation): Check that
	an R_ARM_V4BX instruction lies inside the section.
	(elf32_arm_tls_relax): Read the second halfword of an unexpected
	R_ARM_THM_TLS_DESCSEQ instruction only inside the section.
	(elf32_arm_final_link_relocate): Check that the instruction an
	R_ARM_TLS_GOTDESC relocation refers to lies inside the section.

ld/
	* testsuite/ld-arm/reloc-tls-gotdesc.s: New test.
	* testsuite/ld-arm/reloc-tls-gotdesc.d: New test.
	* testsuite/ld-arm/reloc-v4bx.s: New test.
	* testsuite/ld-arm/reloc-v4bx.d: New test.
	* testsuite/ld-arm/arm-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elf32-arm.c                         | 47 +++++++++++++++++++++----
 ld/testsuite/ld-arm/arm-elf.exp         |  2 ++
 ld/testsuite/ld-arm/reloc-tls-gotdesc.d |  4 +++
 ld/testsuite/ld-arm/reloc-tls-gotdesc.s | 12 +++++++
 ld/testsuite/ld-arm/reloc-v4bx.d        |  4 +++
 ld/testsuite/ld-arm/reloc-v4bx.s        |  7 ++++
 6 files changed, 70 insertions(+), 6 deletions(-)
 create mode 100644 ld/testsuite/ld-arm/reloc-tls-gotdesc.d
 create mode 100644 ld/testsuite/ld-arm/reloc-tls-gotdesc.s
 create mode 100644 ld/testsuite/ld-arm/reloc-v4bx.d
 create mode 100644 ld/testsuite/ld-arm/reloc-v4bx.s

diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 1bca033c60..664190eb0e 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -7891,6 +7891,14 @@ bfd_elf32_arm_process_before_allocation (bfd *abfd,
 	    {
 	      int reg;
 
+	      /* This pass runs before the relocations are applied, so check
+		 here that the instruction lies inside the section.  */
+	      if (!arm_reloc_offset_in_range (abfd, sec, "R_ARM_V4BX",
+					      irel->r_offset, 4,
+					      bfd_get_section_limit_octets
+					      (abfd, sec)))
+		goto error_return;
+
 	      reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
 	      record_arm_bx_glue (link_info, reg);
 	      continue;
@@ -10048,9 +10056,11 @@ elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
 	}
       else
 	{
-	  if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
+	  if (((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
+	      && (bfd_get_section_limit_octets (input_bfd, input_sec)
+		  - rel->r_offset) >= 4)
 	    /* It's a 32 bit instruction, fetch the rest of it for
-	       error generation.  */
+	       error generation, if it lies inside the section.  */
 	    insn = (insn << 16)
 	      | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
 	  _bfd_error_handler
@@ -11911,18 +11921,43 @@ elf32_arm_final_link_relocate (reloc_howto_type *	    howto,
 	  {
 	    unsigned long data, insn;
 	    unsigned thumb;
+	    bfd_signed_vma dist;
+	    bfd_vma insn_off, limit, avail;
 
-	    data = bfd_get_signed_32 (input_bfd, hit_data);
+	    dist = bfd_get_signed_32 (input_bfd, hit_data);
+	    data = dist;
 	    thumb = data & 1;
 	    data &= ~1ul;
 
+	    /* DATA is the signed distance from the instruction that uses
+	       the field to the field; the instruction may come before or
+	       after it.  DATA comes from the input, so the instruction must
+	       lie inside the section.  */
+	    insn_off = rel->r_offset - (dist - thumb);
+	    limit = bfd_get_section_limit_octets (input_bfd, input_section);
+	    avail = insn_off <= limit ? limit - insn_off : 0;
+	    if (avail < (thumb ? 2 : 4))
+	      {
+		*error_message = _("R_ARM_TLS_GOTDESC refers to an "
+				   "instruction outside the section");
+		return bfd_reloc_dangerous;
+	      }
+
 	    if (thumb)
 	      {
 		insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
 		if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
-		  insn = (insn << 16)
-		    | bfd_get_16 (input_bfd,
-				  contents + rel->r_offset - data + 2);
+		  {
+		    if (avail < 4)
+		      {
+			*error_message = _("R_ARM_TLS_GOTDESC refers to an "
+					   "instruction outside the section");
+			return bfd_reloc_dangerous;
+		      }
+		    insn = (insn << 16)
+		      | bfd_get_16 (input_bfd,
+				    contents + rel->r_offset - data + 2);
+		  }
 		if ((insn & 0xf800c000) == 0xf000c000)
 		  /* bl/blx */
 		  value = -6;
diff --git a/ld/testsuite/ld-arm/arm-elf.exp b/ld/testsuite/ld-arm/arm-elf.exp
index 106db47e7f..4655319b91 100644
--- a/ld/testsuite/ld-arm/arm-elf.exp
+++ b/ld/testsuite/ld-arm/arm-elf.exp
@@ -1303,8 +1303,10 @@ 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"
+run_dump_test "reloc-v4bx"
 
 if { [check_shared_lib_support] } {
+    run_dump_test "reloc-tls-gotdesc"
     run_dump_test "thumb-plt"
     run_dump_test "thumb-plt-got"
 }
diff --git a/ld/testsuite/ld-arm/reloc-tls-gotdesc.d b/ld/testsuite/ld-arm/reloc-tls-gotdesc.d
new file mode 100644
index 0000000000..756a3edcf6
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-tls-gotdesc.d
@@ -0,0 +1,4 @@
+#name: ARM TLS_GOTDESC instruction outside the section is rejected
+#source: reloc-tls-gotdesc.s
+#ld: -shared
+#error: .*dangerous relocation: R_ARM_TLS_GOTDESC refers to an instruction outside the section
diff --git a/ld/testsuite/ld-arm/reloc-tls-gotdesc.s b/ld/testsuite/ld-arm/reloc-tls-gotdesc.s
new file mode 100644
index 0000000000..0317247dcd
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-tls-gotdesc.s
@@ -0,0 +1,12 @@
+	.syntax unified
+	.arm
+	.text
+	.globl	f
+f:	nop
+	/* The R_ARM_TLS_GOTDESC field holds the distance back to the
+	   instruction that uses it; 0x40 points before the section.  */
+1:	.word	0x40
+	.reloc	1b, R_ARM_TLS_GOTDESC, tv
+	.section .tbss,"awT",%nobits
+	.globl	tv
+tv:	.zero	4
diff --git a/ld/testsuite/ld-arm/reloc-v4bx.d b/ld/testsuite/ld-arm/reloc-v4bx.d
new file mode 100644
index 0000000000..6dee0a913d
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-v4bx.d
@@ -0,0 +1,4 @@
+#name: ARM V4BX instruction outside the section is rejected
+#source: reloc-v4bx.s
+#ld: --fix-v4bx-interworking
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for R_ARM_V4BX in section `\.text'
diff --git a/ld/testsuite/ld-arm/reloc-v4bx.s b/ld/testsuite/ld-arm/reloc-v4bx.s
new file mode 100644
index 0000000000..08cee85211
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-v4bx.s
@@ -0,0 +1,7 @@
+	.text
+	.globl	_start
+_start:
+	/* R_ARM_V4BX marks a 4-octet BX instruction, which runs past this
+	   2-octet section.  */
+	.short	0
+	.reloc	0, R_ARM_V4BX
-- 
2.55.0




More information about the Binutils mailing list