[PATCH v3 1/8] bfd: add _bfd_elf_relax_delete_range_ok
Harshit Kumar
harshit7kr@gmail.com
Fri Nov 13 08:58:29 GMT 2026
> Generally we permit format string to go past 80 cols, but ...
> ... 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.
No objection at all, thank you. One thing goes with it: the shared
test helper ld_relax_underflow_test in 2/8 (ld/testsuite/lib/ld-lib.exp)
accepts a rejection only if the output contains "bad relaxation", so with
the new wording that match would need to become "*cannot delete*". I am
happy to send an updated 2/8 if that is easier for you.
Separately, while re-checking the series I found that
ft32_elf_relax_section's error_return returns true, the same problem
6/8 fixed for rl78, so a range-check failure on FT32 still lets the link
succeed. I'll send a one-line follow-up patch for that instead of
respinning the series.
Thanks,
Harshit
On Fri, 13 Nov 2026 at 14:18, Jan Beulich <jbeulich@suse.com> wrote:
>
> 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