Re: [PATCH v4 1/2] fs/binfmt_elf: Fix AT_PHDR for unusual ELF files

From: Kees Cook
Date: Thu Jan 27 2022 - 11:23:54 EST


On Thu, Jan 27, 2022 at 09:56:43PM +0900, Akira Kawata wrote:
> On Wed, Jan 26, 2022 at 09:01:30PM -0800, Kees Cook wrote:
> > [...]
> > 1) The ELF spec says e_phoff is 0 if there's no program header table.
> >
> > The old code would just pass the load_addr as a result. This patch will
> > now retain the same result (phdr_addr defaults to 0). I wonder if there
> > is a bug in this behavior, though? (To be addressed in a different patch
> > if needed...)
> >
>
> It is better to return NULL from load_elf_phdrs when e_phoff == 0, I
> think.

Yeah, right now it just returns a pointer to file offset 0.

I also wonder if we should sanity-check e_phoff vs PT_PHDR? Right now
Linux ignores PT_PHDR. Should we reject loading when e_phoff != PT_PHDR
file offset? (And I wonder if there are "broken" binaries right now that
have bad PT_PHDR segments that have gone unnoticed...)

And now I'm thinking about the excellent ELF loading analysis at:
https://nathanotterness.com/2021/10/tiny_elf_modernized.html

;)

--
Kees Cook