[binutils-gdb] bfd: add _bfd_elf_relax_delete_range_ok
Jan Beulich
jbeulich@sourceware.org
Fri Nov 13 13:26:13 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b3e0a66b5de7bf5f73d380c23d748b4b12c4d153
commit b3e0a66b5de7bf5f73d380c23d748b4b12c4d153
Author: Harshit Kumar <harshit7kr@gmail.com>
Date: Fri Nov 13 14:24:12 2026 +0100
bfd: add _bfd_elf_relax_delete_range_ok
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>
Diff:
---
bfd/elf-bfd.h | 2 ++
bfd/elf.c | 29 +++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index c51d41c8eb4..b9260529a6a 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 e6699a5ffc9..4fe5ee1495a 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -14245,6 +14245,35 @@ _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: relax: cannot delete %" PRIu64 " bytes at %#" PRIx64
+ " (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
More information about the Binutils-cvs
mailing list