All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
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: Sat, 25 May 2024 03:46:12 +0800	[thread overview]
Message-ID: <ZlDuhAPHNw5ZCfjH@visitorckw-System-Product-Name> (raw)
In-Reply-To: <20240524122350.a22ca8dfe07a21f3eb862159@linux-foundation.org>

On Fri, May 24, 2024 at 12:23:50PM -0700, Andrew Morton wrote:
> 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.
>
It seems likely that I'm not the first to notice the unchecked mallocs
in our userspace code if they're indeed widespread. Are there specific
reasons or guidelines indicating when malloc checks are necessary, and
when they can be omitted?

Regards,
Kuan-Wei


      reply	other threads:[~2024-05-24 19:46 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
2024-05-24 19:46   ` Kuan-Wei Chiu [this message]

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=ZlDuhAPHNw5ZCfjH@visitorckw-System-Product-Name \
    --to=visitorckw@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jserv@ccns.ncku.edu.tw \
    --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 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.