Hi Aaron,

On Fri, Nov 13, 2026 at 09:40:26PM -0500, Aaron Merey wrote:
> On Tue, Nov 10, 2026 at 7:12 PM Mark Wielaard <[email protected]> wrote:
> > --- a/libelf/gelf_getphdr.c
> > +++ b/libelf/gelf_getphdr.c
> > @@ -60,6 +60,16 @@ gelf_getphdr (Elf *elf, int ndx, GElf_Phdr *dst)
> >
> >    rwlock_rdlock (elf->lock);
> >
> > +  /* Test whether the index is ok.  */
> > +  size_t phnum;
> > +  if (ndx < 0
> > +      || __elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
> > +      || (size_t) ndx >= phnum)
> > +    {
> > +      __libelf_seterrno (ELF_E_INVALID_INDEX);
> > +      goto out;
> > +    }
> 
> This patch exposes a preexisting bug in __elf_getphdrnum_chk_rdlock.
> This function calls __elf_getphdrnum_rdlock which returns -1 if the
> ehdr is NULL. This return value is ignored by
> __elf_getphdrnum_chk_rdlock which goes on to dereference the ehdr
> assuming it's non-NULL. Before this patch, a NULL edhr would cause
> gelf_getphdr and gelf_update_phdr to return early when loading phdrs.
> With this error check now occurring before phdr loading, a NULL ehdr
> will cause a segfault.
> 
> __elf_getphdrnum_chk_rdlock should return early if
> __elf_getphdrnum_rdlock returns -1.

O well, spotted. I'll sent a patch to do that before applying this
patch.

Thanks,

Mark

Reply via email to