All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc()
Date: Fri, 24 May 2024 12:23:50 -0700	[thread overview]
Message-ID: <20240524122350.a22ca8dfe07a21f3eb862159@linux-foundation.org> (raw)
In-Reply-To: <20240524191459.949731-1-visitorckw@gmail.com>

On Sat, 25 May 2024 03:14:59 +0800 Kuan-Wei Chiu <visitorckw@gmail.com> wrote:

> In kmalloc(), add a check to ensure that the pointer 'ret' is not NULL
> before attempting to memset it when the __GFP_ZERO flag is set. This
> prevents a potential NULL pointer dereference.
> 
> ...
>
> --- a/tools/lib/slab.c
> +++ b/tools/lib/slab.c
> @@ -22,7 +22,7 @@ void *kmalloc(size_t size, gfp_t gfp)
>  	uatomic_inc(&kmalloc_nr_allocated);
>  	if (kmalloc_verbose)
>  		printf("Allocating %p from malloc\n", ret);
> -	if (gfp & __GFP_ZERO)
> +	if (gfp & __GFP_ZERO && ret)
>  		memset(ret, 0, size);
>  	return ret;
>  }

I suspect we have a lot of unchecked mallocs in our userspace code.  If
there's an argument for fixing them all(?) then it would be best to do
this in a wholesale fashion rather than patch-at-a-time piecemeal.


  reply	other threads:[~2024-05-24 19:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-24 19:14 [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc() Kuan-Wei Chiu
2024-05-24 19:23 ` Andrew Morton [this message]
2024-05-24 19:46   ` Kuan-Wei Chiu

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=20240524122350.a22ca8dfe07a21f3eb862159@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=jserv@ccns.ncku.edu.tw \
    --cc=linux-kernel@vger.kernel.org \
    --cc=visitorckw@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.