lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <202110071038.B589687@keescook>
Date:   Thu, 7 Oct 2021 10:40:01 -0700
From:   Kees Cook <keescook@...omium.org>
To:     Alexey Dobriyan <adobriyan@...il.com>
Cc:     akpm@...ux-foundation.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] ELF: fix overflow in total mapping size calculation

On Thu, Oct 07, 2021 at 08:20:03PM +0300, Alexey Dobriyan wrote:
> On Tue, Oct 05, 2021 at 07:31:09PM -0700, Kees Cook wrote:
> > On Sun, Oct 03, 2021 at 03:11:24PM +0300, Alexey Dobriyan wrote:
> > > Kernel assumes that ELF program headers are ordered by mapping address,
> > > but doesn't enforce it. It is possible to make mapping size extremely huge
> > > by simply shuffling first and last PT_LOAD segments.
> > > 
> > > As long as PT_LOAD segments do not overlap, it is silly to require
> > > sorting by v_addr anyway because mmap() doesn't care.
> > > 
> > > Don't assume PT_LOAD segments are sorted and calculate min and max
> > > addresses correctly.
> > 
> > Nice! Yes, this all make sense.
> > 
> > > 
> > > Signed-off-by: Alexey Dobriyan <adobriyan@...il.com>
> > > ---
> > > 
> > >  fs/binfmt_elf.c |   23 +++++++++++------------
> > >  1 file changed, 11 insertions(+), 12 deletions(-)
> > > 
> > > --- a/fs/binfmt_elf.c
> > > +++ b/fs/binfmt_elf.c
> > > @@ -93,7 +93,7 @@ static int elf_core_dump(struct coredump_params *cprm);
> > >  #define ELF_CORE_EFLAGS	0
> > >  #endif
> > >  
> > > -#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
> > > +#define ELF_PAGESTART(_v) ((_v) & ~(int)(ELF_MIN_ALIGN-1))
> > 
> > Errr, this I don't like. I assume this is because of the min() use
> > below?
> 
> Yes, this is to shut up the warning.
> 
> The macro is slightly incorrect because "_v" can be either uint32_t or
> uint64_t. But standard ALIGN macros are slightly incorrect too.

Right, but "int" is neither 64-sized nor unsigned. :P I would just leave
this macro as-is.

> 
> I don't want to clean this particular mess right now. Those are separate stables.
> 
> > >  #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
> > >  #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
> > >  
> > > @@ -399,22 +399,21 @@ static unsigned long elf_map(struct file *filep, unsigned long addr,
> > >  	return(map_addr);
> > >  }
> > >  
> > > -static unsigned long total_mapping_size(const struct elf_phdr *cmds, int nr)
> > > +static unsigned long total_mapping_size(const struct elf_phdr *phdr, int nr)
> > >  {
> > > -	int i, first_idx = -1, last_idx = -1;
> > > +	elf_addr_t min_addr = -1;
> > > +	elf_addr_t max_addr = 0;
> > > +	bool pt_load = false;
> > > +	int i;
> > >  
> > >  	for (i = 0; i < nr; i++) {
> > > -		if (cmds[i].p_type == PT_LOAD) {
> > > -			last_idx = i;
> > > -			if (first_idx == -1)
> > > -				first_idx = i;
> > > +		if (phdr[i].p_type == PT_LOAD) {
> > > +			min_addr = min(min_addr, ELF_PAGESTART(phdr[i].p_vaddr));
> > > +			max_addr = max(max_addr, phdr[i].p_vaddr + phdr[i].p_memsz);
> > 
> > How about:
> > 		min_addr = min_t(elf_addr_t, min_addr, ELF_PAGESTART(phdr[i].p_vaddr));
> > 		max_addr = max_t(elf_addr_t, max_addr, phdr[i].p_vaddr + phdr[i].p_memsz);
> 
> No! The proper fix is to fix ELF_PAGESTART().

Why? The warning from min() is about making sure there isn't an
unexpected type conversion. min_t() uses an explicit type, so why not
the above?

-- 
Kees Cook

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ