All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kyle Zeng <zengyhkyle@gmail.com>
To: David Laight <David.Laight@aculab.com>
Cc: 'Eric Dumazet' <edumazet@google.com>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"eric.dumazet@gmail.com" <eric.dumazet@gmail.com>,
	syzbot <syzkaller@googlegroups.com>,
	Kees Cook <keescook@chromium.org>,
	Vlastimil Babka <vbabka@suse.cz>
Subject: Re: [PATCH net] net: deal with integer overflows in kmalloc_reserve()
Date: Mon, 4 Sep 2023 16:49:25 -0700	[thread overview]
Message-ID: <ZPZtBWm06f321Tp/@westworld> (raw)
In-Reply-To: <837a03d12d8345bfa7e9874c1e7d9156@AcuMS.aculab.com>

[-- Attachment #1: Type: text/plain, Size: 2585 bytes --]

On Mon, Sep 04, 2023 at 09:27:28AM +0000, David Laight wrote:
> From: Eric Dumazet <edumazet@google.com>
> > Sent: 04 September 2023 10:06
> > To: David Laight <David.Laight@ACULAB.COM>
> > Cc: David S . Miller <davem@davemloft.net>; Jakub Kicinski <kuba@kernel.org>; Paolo Abeni
> > <pabeni@redhat.com>; netdev@vger.kernel.org; eric.dumazet@gmail.com; syzbot
> > <syzkaller@googlegroups.com>; Kyle Zeng <zengyhkyle@gmail.com>; Kees Cook <keescook@chromium.org>;
> > Vlastimil Babka <vbabka@suse.cz>
> > Subject: Re: [PATCH net] net: deal with integer overflows in kmalloc_reserve()
> > 
> > On Mon, Sep 4, 2023 at 10:41 AM David Laight <David.Laight@aculab.com> wrote:
> > >
> > > From: Eric Dumazet
> > > > Sent: 31 August 2023 19:38
> > > >
> > > > Blamed commit changed:
> > > >     ptr = kmalloc(size);
> > > >     if (ptr)
> > > >       size = ksize(ptr);
> > > >
> > > > to:
> > > >     size = kmalloc_size_roundup(size);
> > > >     ptr = kmalloc(size);
> > > >
> > > > This allowed various crash as reported by syzbot [1]
> > > > and Kyle Zeng.
> > > >
> > > > Problem is that if @size is bigger than 0x80000001,
> > > > kmalloc_size_roundup(size) returns 2^32.
> > > >
> > > > kmalloc_reserve() uses a 32bit variable (obj_size),
> > > > so 2^32 is truncated to 0.
> > >
> > > Can this happen on 32bit arch?
> > > In that case kmalloc_size_roundup() will return 0.
> > 
> > Maybe, but this would be a bug in kmalloc_size_roundup()
> 
> That contains:
> 	/* Short-circuit saturated "too-large" case. */
> 	if (unlikely(size == SIZE_MAX))
> 		return SIZE_MAX;
> 
> It can also return 0 on failure, I can't remember if kmalloc(0)
> is guaranteed to be NULL (malloc(0) can do 'other things').
> 
> Which is entirely hopeless since MAX_SIZE is (size_t)-1.
> 
> IIRC kmalloc() has a size limit (max 'order' of pages) so
> kmalloc_size_roundup() ought check for that (or its max value).
> 
> The final:
> 	/* The flags don't matter since size_index is common to all. */
> 	c = kmalloc_slab(size, GFP_KERNEL);
> 	return c ? c->object_size : 0;
> probably ought to return size if c is even NULL.
> 
> 	David
> 
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)

> It can also return 0 on failure, I can't remember if kmalloc(0)
> is guaranteed to be NULL (malloc(0) can do 'other things').
kmalloc(0) returns ZERO_SIZE_PTR (16).

My proposed patch is to check the return value of kmalloc, making sure it is neither NULL or ZERO_SIZE_PTR. The patch is attached. It should work for both 32bit and 64bit systems.

[-- Attachment #2: 0001-properly-check-for-integer-overflow-in-__alloc_skb.patch --]
[-- Type: text/x-diff, Size: 1070 bytes --]

From 034ac600c639bfa93c54e317ea3712538c749de6 Mon Sep 17 00:00:00 2001
From: Kyle Zeng <zengyhkyle@gmail.com>
Date: Mon, 28 Aug 2023 17:53:40 -0700
Subject: [PATCH] properly check for integer overflow in __alloc_skb

kmalloc_reserve may return ZERO_SIZE_PTR(0x10) when integer overflow
happens since size is controlled by users.
Make sure we handle this case properly.

Signed-off-by: Kyle Zeng <zengyhkyle@gmail.com>
---
 net/core/skbuff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index a298992060e..f219fef5a16 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -642,7 +642,7 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
 	 * Both skb->head and skb_shared_info are cache line aligned.
 	 */
 	data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
-	if (unlikely(!data))
+	if (unlikely(ZERO_OR_NULL_PTR(data)))
 		goto nodata;
 	/* kmalloc_size_roundup() might give us more room than requested.
 	 * Put skb_shared_info exactly at the end of allocated zone,
-- 
2.34.1


  reply	other threads:[~2023-09-04 23:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31 18:37 [PATCH net] net: deal with integer overflows in kmalloc_reserve() Eric Dumazet
2023-09-04  5:58 ` patchwork-bot+netdevbpf
2023-09-04  8:41 ` David Laight
2023-09-04  9:06   ` Eric Dumazet
2023-09-04  9:27     ` David Laight
2023-09-04 23:49       ` Kyle Zeng [this message]
2023-09-05  3:41         ` Eric Dumazet
2023-09-05  3:49           ` Eric Dumazet
2023-09-05  8:36           ` David Laight
2023-09-05 12:27             ` Eric Dumazet
2023-09-05 12:34               ` Eric Dumazet
2023-09-05 12:44                 ` David Laight

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=ZPZtBWm06f321Tp/@westworld \
    --to=zengyhkyle@gmail.com \
    --cc=David.Laight@aculab.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eric.dumazet@gmail.com \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=syzkaller@googlegroups.com \
    --cc=vbabka@suse.cz \
    /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.