[PATCH v2 1/8] bfd: add _bfd_elf_relax_delete_range_ok
Harshit Kumar
harshit7kr@gmail.com
Fri Nov 6 18:10:01 GMT 2026
Hi Jan,
Thanks for the review.
> > +/* Validate that deleting COUNT bytes starting at ADDR up to TOADDR
> > + is within the section bounds of SEC.
>
> This is ambiguous; as worded my first and second reading pass resulted in
> "then ADDR + COUNT == TOADDR". TOADDR really looks to be an upper bound,
> which wants expressing unambiguously.
>
> Further, ADDR (also in TOADDR) isn't quite right here. There are section
> offsets, so OFFS may be better. TOADDR (as said, apparently being an
> upper bound) may want naming e.g. LIMIT [...]
Agreed on both. I'll reword the comment to say explicitly that LIMIT is
an upper bound, and rename the parameters to OFFS and LIMIT.
> > +bool
> > +_bfd_elf_relax_delete_range_ok (bfd *abfd,
> > + asection *sec,
>
> Both can be pointer-to-const, can't they?
Yes, I'll make them const.
> > + if (addr <= toaddr
> > + && count <= toaddr - addr
> > + && toaddr <= sec->size)
>
> Is this last condition violated anywhere? If not, can it become an
> assertion?
It can be violated, so I'd like to keep it as a check. In elf32-rx.c,
elf32_rx_relax_delete_bytes sets toaddr from alignment_rel->r_offset,
which comes from the input file and isn't validated. A crafted object
can therefore put it beyond sec->size, and the linker should reject that
with an error rather than abort.
> > + abfd, (uint64_t) count, (uint64_t) addr, sec, (uint64_t) sec->size);
>
> This doesn't express at all the case addr > toaddr. Together with the
> remark right above: You may want to log toaddr here instead, and then
> re-word the message text accordingly.
Agreed. Since the size check stays, I'll log the offset, count, limit
and section size, so that all three failure cases can be told apart.
Thanks,
Harshit
On Fri, 6 Nov 2026 at 19:13, Jan Beulich <jbeulich@suse.com> wrote:
>
> On 16.10.2026 14:36, Harshit Kumar wrote:
> > --- a/bfd/elf.c
> > +++ b/bfd/elf.c
> > @@ -14237,6 +14237,32 @@ _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
> > #endif
> > }
> >
> > +/* Validate that deleting COUNT bytes starting at ADDR up to TOADDR
> > + is within the section bounds of SEC.
>
> This is ambiguous; as worded my first and second reading pass resulted in
> "then ADDR + COUNT == TOADDR". TOADDR really looks to be an upper bound,
> which wants expressing unambiguously.
>
> Further, ADDR (also in TOADDR) isn't quite right here. There are section
> offsets, so OFFS may be better. TOADDR (as said, apparently being an
> upper bound) may want naming e.g. LIMIT (not just because TOOFFS wouldn't
> look quite nice, at least to me).
>
> > ADDR is derived from an
> > + unvalidated relocation offset; without this check the subtraction
> > + (TOADDR - ADDR - COUNT) wraps around, corrupting the heap buffer. */
> > +
> > +bool
> > +_bfd_elf_relax_delete_range_ok (bfd *abfd,
> > + asection *sec,
>
> Both can be pointer-to-const, can't they?
>
> > + bfd_vma addr,
> > + bfd_vma count,
> > + bfd_vma toaddr)
> > +{
> > + if (addr <= toaddr
> > + && count <= toaddr - addr
> > + && toaddr <= sec->size)
>
> Is this last condition violated anywhere? If not, can it become an
> assertion?
>
> > + return true;
> > +
> > + _bfd_error_handler
> > + /* xgettext:c-format */
> > + (_("%pB: bad relaxation: cannot delete %" PRIu64 " bytes at %#" PRIx64
> > + " in section `%pA' of size %#" PRIx64),
> > + abfd, (uint64_t) count, (uint64_t) addr, sec, (uint64_t) sec->size);
>
> This doesn't express at all the case addr > toaddr. Together with the
> remark right above: You may want to log toaddr here instead, and then
> re-word the message text accordingly.
>
> Jan
More information about the Binutils
mailing list