public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ELF: fix overflow in total mapping size calculation
Date: Tue, 5 Oct 2021 19:31:09 -0700	[thread overview]
Message-ID: <202110051929.37279B6B4A@keescook> (raw)
In-Reply-To: <YVmd7D0M6G/DcP4O@localhost.localdomain>

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@gmail.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?

>  #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);

> +			pt_load = true;
>  		}
>  	}
> -	if (first_idx == -1)
> -		return 0;
> -
> -	return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
> -				ELF_PAGESTART(cmds[first_idx].p_vaddr);
> +	return pt_load ? (max_addr - min_addr) : 0;
>  }
>  
>  static int elf_read(struct file *file, void *buf, size_t len, loff_t pos)

-Kees

-- 
Kees Cook

  parent reply	other threads:[~2021-10-06  2:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-03 12:11 [PATCH] ELF: fix overflow in total mapping size calculation Alexey Dobriyan
2021-10-06  0:21 ` Andrew Morton
2021-10-07 17:13   ` Alexey Dobriyan
2021-10-06  2:31 ` Kees Cook [this message]
2021-10-07 17:20   ` Alexey Dobriyan
2021-10-07 17:40     ` Kees Cook
2021-10-07 18:31       ` Alexey Dobriyan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202110051929.37279B6B4A@keescook \
    --to=keescook@chromium.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox