linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Xujun Leng <lengxujun2007@126.com>, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm: fix potential invalid pointer dereference in kmemdup()
Date: Tue, 7 Mar 2023 11:04:37 +0100	[thread overview]
Message-ID: <4c223e9c-2d43-fb8f-7ac9-ad2121914170@redhat.com> (raw)
In-Reply-To: <20230307090358.21346-1-lengxujun2007@126.com>

On 07.03.23 10:03, Xujun Leng wrote:
> If kmemdup() was called with src == NULL, then memcpy() source address
> is fatal, and if kmemdup() was called with len == 0, kmalloc_track_caller()
> will return ZERO_SIZE_PTR to variable p, then memcpy() destination address
> is fatal. Both 2 cases will cause an invalid pointer dereference.
> 

"fix" in subject implies that there is actually a case broken. Is there, 
or is this rather a "sanitize" ?

> Signed-off-by: Xujun Leng <lengxujun2007@126.com>
> ---
>   mm/util.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/mm/util.c b/mm/util.c
> index dd12b9531ac4..d1a3b3d2988e 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -128,6 +128,9 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp)
>   {
>   	void *p;
>   
> +	if (!src || len == 0)
> +		return NULL;
> +
>   	p = kmalloc_track_caller(len, gfp);
>   	if (p)
>   		memcpy(p, src, len);

Why should we take care of kmemdup(), but not memdup_user() ? Shouldn't 
it suffer from similar problems?

-- 
Thanks,

David / dhildenb



  reply	other threads:[~2023-03-07 10:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-07  9:03 Xujun Leng
2023-03-07 10:04 ` David Hildenbrand [this message]
2023-03-09  6:46   ` Xujun Leng
2023-03-09  9:01     ` David Hildenbrand
     [not found]       ` <20230309100415.2382-1-lengxujun2007@126.com>
2023-03-09 13:22         ` David Hildenbrand
2023-03-10  8:06           ` Xujun Leng

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=4c223e9c-2d43-fb8f-7ac9-ad2121914170@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=lengxujun2007@126.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.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