[PATCH v3 1/8] bfd: add _bfd_elf_relax_delete_range_ok

Harshit Kumar harshit7kr@gmail.com
Sat Nov 7 07:50:56 GMT 2026


Multiple ELF backends implement relaxation by shifting remaining section
contents down over deleted bytes via memmove.  When relocations or section
headers specify an offset beyond the section boundary or a byte count that
exceeds remaining section bytes, the unsigned subtraction to compute the
transfer size underflows to near SIZE_MAX.  This triggers heap buffer
overflows or crashes.

Introduce shared helper _bfd_elf_relax_delete_range_ok in bfd/elf.c to
validate deletion intervals.  The helper takes limit as an upper bound and
checks offs <= limit, count <= limit - offs, and limit <= sec->size.  On
failure, report a descriptive bfd_error_bad_value error message.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
v3:
- Rename parameters addr/toaddr to offs/limit.
- Make abfd and sec pointer-to-const in declaration and definition.
- Clarify comment that limit is an upper bound.
- Include limit in diagnostic output, and wrap lines to 79 columns.

 bfd/elf-bfd.h |  2 ++
 bfd/elf.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index c51d41c8..b9260529 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2535,6 +2535,8 @@ extern bool _bfd_elf_find_inliner_info
 extern asymbol *_bfd_elf_find_function
   (bfd *, asymbol **, asection *, bfd_vma, const char **, const char **)
   ATTRIBUTE_HIDDEN;
+extern bool _bfd_elf_relax_delete_range_ok
+  (const bfd *, const asection *, bfd_vma, bfd_vma, bfd_vma) ATTRIBUTE_HIDDEN;
 #define _bfd_elf_read_minisymbols _bfd_generic_read_minisymbols
 #define _bfd_elf_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
 extern int _bfd_elf_sizeof_headers
diff --git a/bfd/elf.c b/bfd/elf.c
index e6699a5f..b80bba4a 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -14245,6 +14245,36 @@ _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
 #endif
 }
 
+/* Validate that deleting COUNT bytes starting at OFFS up to the upper
+   bound LIMIT is within the section bounds of SEC (OFFS <= LIMIT,
+   COUNT <= LIMIT - OFFS, and LIMIT <= SEC->SIZE).  OFFS is derived
+   from an unvalidated relocation offset; without this check the
+   subtraction (LIMIT - OFFS - COUNT) wraps around, corrupting the heap
+   buffer.  */
+
+bool
+_bfd_elf_relax_delete_range_ok (const bfd *abfd,
+				const asection *sec,
+				bfd_vma offs,
+				bfd_vma count,
+				bfd_vma limit)
+{
+  if (offs <= limit
+      && count <= limit - offs
+      && limit <= sec->size)
+    return true;
+
+  _bfd_error_handler
+    /* xgettext:c-format */
+    (_("%pB: bad relaxation: cannot delete %" PRIu64
+       " bytes at offset %#" PRIx64 " with limit %#" PRIx64
+       " in section `%pA' of size %#" PRIx64),
+     abfd, (uint64_t) count, (uint64_t) offs, (uint64_t) limit, sec,
+     (uint64_t) sec->size);
+  bfd_set_error (bfd_error_bad_value);
+  return false;
+}
+
 #ifndef OBJ_MAYBE_ELF_ATTRIBUTES
 
 bfd *_bfd_elf_link_setup_object_attributes
-- 
2.55.0




More information about the Binutils mailing list