linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Pranav Tyagi <pranav.tyagi03@gmail.com>
Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
	linux-kernel-mentees@lists.linux.dev
Subject: Re: [PATCH] binfmt_elf: use check_mul_overflow() for size calc
Date: Mon, 9 Jun 2025 21:04:36 -0700	[thread overview]
Message-ID: <202506092053.827AD89DC5@keescook> (raw)
In-Reply-To: <20250607082844.8779-1-pranav.tyagi03@gmail.com>

On Sat, Jun 07, 2025 at 01:58:44PM +0530, Pranav Tyagi wrote:
> Use check_mul_overflow() to safely compute the total size of ELF program
> headers instead of relying on direct multiplication.
> 
> Directly multiplying sizeof(struct elf_phdr) with e_phnum risks integer
> overflow, especially on 32-bit systems or with malformed ELF binaries
> crafted to trigger wrap-around. If an overflow occurs, kmalloc() could
> allocate insufficient memory, potentially leading to out-of-bound
> accesses, memory corruption or security vulnerabilities.
> 
> Using check_mul_overflow() ensures the multiplication is performed
> safely and detects overflows before memory allocation. This change makes
> the function more robust when handling untrusted or corrupted binaries.
> 
> Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> Link: https://github.com/KSPP/linux/issues/92
> ---
>  fs/binfmt_elf.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
> index a43363d593e5..774e705798b8 100644
> --- a/fs/binfmt_elf.c
> +++ b/fs/binfmt_elf.c
> @@ -518,7 +518,10 @@ static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex,
>  
>  	/* Sanity check the number of program headers... */
>  	/* ...and their total size. */
> -	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;

size is unsigned int, which has a maximum value of 4,294,967,295.

elf_ex->e_phnum is a u16 (2 bytes) and will not be changing:

$ pahole -C elf64_hdr */fs/binfmt_elf.o
struct elf64_hdr {
	...
        Elf64_Half                 e_phnum;              /*    56     2 */
	...
$ pahole -C Elf64_Half */fs/binfmt_elf.o
typedef __u16 Elf64_Half;

So it has a maximum value of 65,535.

sizeof(struct elf_phdr) is a fixed value, 56:

$ pahole -C elf64_phdr */fs/binfmt_elf.o
struct elf64_phdr {
	...
        /* size: 56, cachelines: 1, members: 8 */
        /* last cacheline: 56 bytes */
};

So the maximum product of the two is 3,669,960.

It is not possible for this calculation to overflow.

> +	
> +	if (check_mul_overflow(sizeof(struct elf_phdr), elf_ex->e_phnum, &size))
> +		goto out;
> +

You can even see that the entire check would be elided by the compiler:

#include <elf.h>

unsigned int unchecked(Elf64_Ehdr *elf_ex)
{
    unsigned int size;

    size = sizeof(Elf64_Phdr) * elf_ex->e_phnum;

    return size;
}

unsigned int checked(Elf64_Ehdr *elf_ex)
{
    unsigned int size;

    if (__builtin_mul_overflow(sizeof(Elf64_Phdr), elf_ex->e_phnum, &size))
        return 0;

    return size;
}

...produces this assembler, identical for both functions:

unchecked:
        movzx   eax, WORD PTR [rdi+56]
        imul    eax, eax, 56
        ret
checked:
        movzx   eax, WORD PTR [rdi+56]
        imul    eax, eax, 56
        ret


https://godbolt.org/z/hTEef8cT9

-Kees

-- 
Kees Cook

  parent reply	other threads:[~2025-06-10  4:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-07  8:28 [PATCH] binfmt_elf: use check_mul_overflow() for size calc Pranav Tyagi
2025-06-09 11:25 ` Jan Kara
2025-06-10  4:04 ` Kees Cook [this message]
2025-06-10  7:59   ` Jan Kara
2025-06-11 14:17     ` Pranav Tyagi

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=202506092053.827AD89DC5@keescook \
    --to=kees@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel-mentees@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pranav.tyagi03@gmail.com \
    --cc=skhan@linuxfoundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).