Hi Mark,
On Sat, Nov 14, 2026 at 8:04 PM Mark Wielaard <[email protected]> wrote:
>
> When __elf_getphdrnum_rdlock returns an error, __elf_getphdrnum_rdlock
> will still try to sanity check the number of phdrs. It uses the ehdr
> for this check, but one of the errors __elf_getphdrnum_rdlock returns
> is that the ehdr doesn't exist, so this sanity check might then crash.
>
> * libelf/elf_getphdrnum.c (__elf_getphdrnum_chk_rdlock): Only
> sanity check the phdr number if __elf_getphdrnum_rdlock result
> was zero.
>
> Reported-by: Aaron Merey <[email protected]>
> Signed-off-by: Mark Wielaard <[email protected]>
LGTM.
Aaron
> ---
>
> This is patch is needed to make the "libelf: Simplify gelf_update_phdr
> and gelf_getphdr ndx check" patch safe.
>
> libelf/elf_getphdrnum.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/libelf/elf_getphdrnum.c b/libelf/elf_getphdrnum.c
> index 60ebec58cb0c..d3eeb9ec5e44 100644
> --- a/libelf/elf_getphdrnum.c
> +++ b/libelf/elf_getphdrnum.c
> @@ -95,7 +95,7 @@ __elf_getphdrnum_chk_rdlock (Elf *elf, size_t *dst)
>
> /* If the phdrs haven't been created or read in yet then do some
> sanity checking to make sure phnum and phoff are consistent. */
> - if (elf->state.elf.phdr == NULL)
> + if (result == 0 && elf->state.elf.phdr == NULL)
> {
> Elf64_Off off = (elf->class == ELFCLASS32
> ? elf->state.elf32.ehdr->e_phoff
> --
> 2.56.1
>