linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
@ 2015-06-05 11:11 Sergey Senozhatsky
  2015-06-08 20:55 ` Andrew Morton
  2015-06-10  0:03 ` Minchan Kim
  0 siblings, 2 replies; 6+ messages in thread
From: Sergey Senozhatsky @ 2015-06-05 11:11 UTC (permalink / raw)
  To: Andrew Morton, Minchan Kim
  Cc: linux-mm, linux-kernel, Sergey Senozhatsky, Sergey Senozhatsky

zs_destroy_pool()->destroy_handle_cache() invoked from
zs_create_pool() can pass a NULL ->handle_cachep pointer
to kmem_cache_destroy(), which will dereference it.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 mm/zsmalloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 33d5126..c766240 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -285,7 +285,8 @@ static int create_handle_cache(struct zs_pool *pool)
 
 static void destroy_handle_cache(struct zs_pool *pool)
 {
-	kmem_cache_destroy(pool->handle_cachep);
+	if (pool->handle_cachep)
+		kmem_cache_destroy(pool->handle_cachep);
 }
 
 static unsigned long alloc_handle(struct zs_pool *pool)
-- 
2.4.2.387.gf86f31a


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
  2015-06-05 11:11 [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache() Sergey Senozhatsky
@ 2015-06-08 20:55 ` Andrew Morton
  2015-06-09  0:38   ` Joonsoo Kim
  2015-06-09  3:57   ` Sergey Senozhatsky
  2015-06-10  0:03 ` Minchan Kim
  1 sibling, 2 replies; 6+ messages in thread
From: Andrew Morton @ 2015-06-08 20:55 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Minchan Kim, linux-mm, linux-kernel, Sergey Senozhatsky,
	Christoph Lameter, Pekka Enberg, Joonsoo Kim, David Rientjes

On Fri,  5 Jun 2015 20:11:30 +0900 Sergey Senozhatsky <sergey.senozhatsky@gmail.com> wrote:

> zs_destroy_pool()->destroy_handle_cache() invoked from
> zs_create_pool() can pass a NULL ->handle_cachep pointer
> to kmem_cache_destroy(), which will dereference it.
>

That's slightly lacking in details (under what circumstances will it
crash) so I changed it to

: If zs_create_pool()->create_handle_cache()->kmem_cache_create() fails,
: zs_create_pool()->destroy_handle_cache() will dereference the NULL
: pool->handle_cachep.
:
: Modify destroy_handle_cache() to avoid this.


> ...
>
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -285,7 +285,8 @@ static int create_handle_cache(struct zs_pool *pool)
>  
>  static void destroy_handle_cache(struct zs_pool *pool)
>  {
> -	kmem_cache_destroy(pool->handle_cachep);
> +	if (pool->handle_cachep)
> +		kmem_cache_destroy(pool->handle_cachep);
>  }
>  
>  static unsigned long alloc_handle(struct zs_pool *pool)

I'll apply this, but...  from a bit of grepping I'm estimating that we
have approximately 200 instances of

	if (foo)
		kmem_cache_destroy(foo);

so obviously kmem_cache_destroy() should be doing the check.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
  2015-06-08 20:55 ` Andrew Morton
@ 2015-06-09  0:38   ` Joonsoo Kim
  2015-06-09  0:43     ` Andrew Morton
  2015-06-09  3:57   ` Sergey Senozhatsky
  1 sibling, 1 reply; 6+ messages in thread
From: Joonsoo Kim @ 2015-06-09  0:38 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Sergey Senozhatsky, Minchan Kim, linux-mm, linux-kernel,
	Sergey Senozhatsky, Christoph Lameter, Pekka Enberg,
	David Rientjes

On Mon, Jun 08, 2015 at 01:55:32PM -0700, Andrew Morton wrote:
> On Fri,  5 Jun 2015 20:11:30 +0900 Sergey Senozhatsky <sergey.senozhatsky@gmail.com> wrote:
> 
> > zs_destroy_pool()->destroy_handle_cache() invoked from
> > zs_create_pool() can pass a NULL ->handle_cachep pointer
> > to kmem_cache_destroy(), which will dereference it.
> >
> 
> That's slightly lacking in details (under what circumstances will it
> crash) so I changed it to
> 
> : If zs_create_pool()->create_handle_cache()->kmem_cache_create() fails,
> : zs_create_pool()->destroy_handle_cache() will dereference the NULL
> : pool->handle_cachep.
> :
> : Modify destroy_handle_cache() to avoid this.
> 
> 
> > ...
> >
> > --- a/mm/zsmalloc.c
> > +++ b/mm/zsmalloc.c
> > @@ -285,7 +285,8 @@ static int create_handle_cache(struct zs_pool *pool)
> >  
> >  static void destroy_handle_cache(struct zs_pool *pool)
> >  {
> > -	kmem_cache_destroy(pool->handle_cachep);
> > +	if (pool->handle_cachep)
> > +		kmem_cache_destroy(pool->handle_cachep);
> >  }
> >  
> >  static unsigned long alloc_handle(struct zs_pool *pool)
> 
> I'll apply this, but...  from a bit of grepping I'm estimating that we
> have approximately 200 instances of
> 
> 	if (foo)
> 		kmem_cache_destroy(foo);
> 
> so obviously kmem_cache_destroy() should be doing the check.

Hello, Andrew.

I'm not sure if doing the check in kmem_cache_destroy() is better.
My quick grep for other pool based allocators(ex. mempool, zpool) also
says that they don't check whether passed pool pointer is NULL or not
in destroy function. I think that it's general convention that proper
pool pointer should be passed to pool based function APIs.

Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
  2015-06-09  0:38   ` Joonsoo Kim
@ 2015-06-09  0:43     ` Andrew Morton
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2015-06-09  0:43 UTC (permalink / raw)
  To: Joonsoo Kim
  Cc: Sergey Senozhatsky, Minchan Kim, linux-mm, linux-kernel,
	Sergey Senozhatsky, Christoph Lameter, Pekka Enberg,
	David Rientjes

On Tue, 9 Jun 2015 09:38:27 +0900 Joonsoo Kim <iamjoonsoo.kim@lge.com> wrote:

> > > ...
> > >
> > > --- a/mm/zsmalloc.c
> > > +++ b/mm/zsmalloc.c
> > > @@ -285,7 +285,8 @@ static int create_handle_cache(struct zs_pool *pool)
> > >  
> > >  static void destroy_handle_cache(struct zs_pool *pool)
> > >  {
> > > -	kmem_cache_destroy(pool->handle_cachep);
> > > +	if (pool->handle_cachep)
> > > +		kmem_cache_destroy(pool->handle_cachep);
> > >  }
> > >  
> > >  static unsigned long alloc_handle(struct zs_pool *pool)
> > 
> > I'll apply this, but...  from a bit of grepping I'm estimating that we
> > have approximately 200 instances of
> > 
> > 	if (foo)
> > 		kmem_cache_destroy(foo);
> > 
> > so obviously kmem_cache_destroy() should be doing the check.
> 
> Hello, Andrew.
> 
> I'm not sure if doing the check in kmem_cache_destroy() is better.

Of course it's better - we have *hundreds* of sites doing something
which could be done at a single site.  Where's the advantage in that?

> My quick grep for other pool based allocators(ex. mempool, zpool) also
> says that they don't check whether passed pool pointer is NULL or not
> in destroy function.

Maybe some of those should be converted as well.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
  2015-06-08 20:55 ` Andrew Morton
  2015-06-09  0:38   ` Joonsoo Kim
@ 2015-06-09  3:57   ` Sergey Senozhatsky
  1 sibling, 0 replies; 6+ messages in thread
From: Sergey Senozhatsky @ 2015-06-09  3:57 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Sergey Senozhatsky, Minchan Kim, linux-mm, linux-kernel,
	Sergey Senozhatsky, Christoph Lameter, Pekka Enberg, Joonsoo Kim,
	David Rientjes

On (06/08/15 13:55), Andrew Morton wrote:
[..]
> > zs_destroy_pool()->destroy_handle_cache() invoked from
> > zs_create_pool() can pass a NULL ->handle_cachep pointer
> > to kmem_cache_destroy(), which will dereference it.
> >
> 
> That's slightly lacking in details (under what circumstances will it
> crash) so I changed it to
> 
> : If zs_create_pool()->create_handle_cache()->kmem_cache_create() fails,
> : zs_create_pool()->destroy_handle_cache() will dereference the NULL
> : pool->handle_cachep.
> :
> : Modify destroy_handle_cache() to avoid this.
> 

Oh, sorry I first received "+ zsmalloc-fix-a-null-pointer-dereference-in-
destroy_handle_cache.patch added to -mm tree" message, so I replied
there. fetchmail works somewhat confusing over the last weeks.

> > ...
> >
> > --- a/mm/zsmalloc.c
> > +++ b/mm/zsmalloc.c
> > @@ -285,7 +285,8 @@ static int create_handle_cache(struct zs_pool *pool)
> >  
> >  static void destroy_handle_cache(struct zs_pool *pool)
> >  {
> > -	kmem_cache_destroy(pool->handle_cachep);
> > +	if (pool->handle_cachep)
> > +		kmem_cache_destroy(pool->handle_cachep);
> >  }
> >  
> >  static unsigned long alloc_handle(struct zs_pool *pool)
> 
> I'll apply this, but...  from a bit of grepping I'm estimating that we
> have approximately 200 instances of
> 
> 	if (foo)
> 		kmem_cache_destroy(foo);
> 
> so obviously kmem_cache_destroy() should be doing the check.

Yes, I thought about this.

A naive grepping gave me 563 occurrences

 git grep kmem_cache_destroy | wc -l
 563

So I decided to hold this activity. Well, I think I can create this
patch bomb, it's trivial.

	-ss

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache()
  2015-06-05 11:11 [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache() Sergey Senozhatsky
  2015-06-08 20:55 ` Andrew Morton
@ 2015-06-10  0:03 ` Minchan Kim
  1 sibling, 0 replies; 6+ messages in thread
From: Minchan Kim @ 2015-06-10  0:03 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Andrew Morton, linux-mm, linux-kernel, Sergey Senozhatsky

On Fri, Jun 05, 2015 at 08:11:30PM +0900, Sergey Senozhatsky wrote:
> zs_destroy_pool()->destroy_handle_cache() invoked from
> zs_create_pool() can pass a NULL ->handle_cachep pointer
> to kmem_cache_destroy(), which will dereference it.
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

Thanks, Sergey!

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-06-10  0:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-05 11:11 [PATCH] zsmalloc: fix a null pointer dereference in destroy_handle_cache() Sergey Senozhatsky
2015-06-08 20:55 ` Andrew Morton
2015-06-09  0:38   ` Joonsoo Kim
2015-06-09  0:43     ` Andrew Morton
2015-06-09  3:57   ` Sergey Senozhatsky
2015-06-10  0:03 ` Minchan Kim

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).