* [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc()
@ 2024-05-24 19:14 Kuan-Wei Chiu
2024-05-24 19:23 ` Andrew Morton
0 siblings, 1 reply; 3+ messages in thread
From: Kuan-Wei Chiu @ 2024-05-24 19:14 UTC (permalink / raw)
To: akpm; +Cc: jserv, linux-kernel, Kuan-Wei Chiu
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.
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
tools/lib/slab.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/lib/slab.c b/tools/lib/slab.c
index 959997fb0652..aeaf535b422a 100644
--- 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;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc()
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
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2024-05-24 19:23 UTC (permalink / raw)
To: Kuan-Wei Chiu; +Cc: jserv, linux-kernel
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc()
2024-05-24 19:23 ` Andrew Morton
@ 2024-05-24 19:46 ` Kuan-Wei Chiu
0 siblings, 0 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2024-05-24 19:46 UTC (permalink / raw)
To: Andrew Morton; +Cc: jserv, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-05-24 19:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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.