[PATCH v3 1/8] bfd: add _bfd_elf_relax_delete_range_ok
Jan Beulich
jbeulich@suse.com
Fri Nov 13 08:48:27 GMT 2026
On 07.11.2026 08:50, Harshit Kumar wrote:
> 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.
Generally we permit format string to go past 80 cols, but ...
> --- 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),
... this one's really long (going to above 130). Still I'd like to suggest
shortening it some:
(_("%pB: relax: cannot delete %" PRIu64 " bytes at %#" PRIx64
" (limit %#" PRIx64 ") in section `%pA' of size %#" PRIx64),
This also improves the chances of a grep attempt actually hitting either
line. I can do this when committing, assuming you don't vehemently
disagree.
Jan
More information about the Binutils
mailing list