LWN.net Logo

Re: integer overflows in kernel/relay.c

From:  Andrew Morton <akpm-AT-linux-foundation.org>
To:  Jens Axboe <axboe-AT-kernel.dk>
Subject:  Re: integer overflows in kernel/relay.c
Date:  Wed, 8 Feb 2012 14:25:13 -0800
Message-ID:  <20120208142513.4db2493a.akpm@linux-foundation.org>
Cc:  Dan Carpenter <dan.carpenter-AT-oracle.com>, linux-kernel-AT-vger.kernel.org
Archive-link:  Article, Thread

On Wed, 08 Feb 2012 09:34:16 +0100
Jens Axboe <axboe@kernel.dk> wrote:

> On 02/07/2012 03:11 PM, Dan Carpenter wrote:
> > My static checker is warning about integer overflows in kernel/relay.c
> > 
> > relay_create_buf()
> >    170  
> >    171          buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
> >                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > This can only overflow on 32bit systems.
> 
> Correct

This is a relatively common problem.  Converting a kzalloc() call to
kcalloc() fixes it.  But we do not have a non-zeroing version of
kcalloc().

Please, someone complete this patch and send it to me!


--- a/include/linux/slab.h~a
+++ a/include/linux/slab.h
@@ -240,11 +240,16 @@ size_t ksize(const void *);
  * for general use, and so are not documented here. For a full list of
  * potential flags, always refer to linux/gfp.h.
  */
-static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+static inline void *wtf_do_i_call_this(size_t n, size_t size, gfp_t flags)
 {
 	if (size != 0 && n > ULONG_MAX / size)
 		return NULL;
-	return __kmalloc(n * size, flags | __GFP_ZERO);
+	return __kmalloc(n * size, flags);
+}
+
+static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+{
+	return wtf_do_i_call_this(n, size, flags | __GFP_ZERO);
 }
 
 #if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
_



(Log in to post comments)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds