Message ID | 20230928204440.543-1-andrew.kanner@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3830545vqu; Fri, 29 Sep 2023 00:02:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDxAsE0oiDn6ixPzgqwKgLmGilwxMPGCcptAC0iVGopiRd3Mcx/LRnXd8QBH5pyivBzppF X-Received: by 2002:a17:902:e748:b0:1c3:a4f2:7ca3 with SMTP id p8-20020a170902e74800b001c3a4f27ca3mr3920133plf.66.1695970958971; Fri, 29 Sep 2023 00:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695970958; cv=none; d=google.com; s=arc-20160816; b=Ut7BiVFXCCBfO20o7xW7j4cIlWebFkqfCar4MaCOAw8skki1GBW0i1kv6tFLF+BI5t 39BuaECkWeayaLc1WfYSKkGIztMpJ2P+REylwhNj9f8TB+yMk+PT7vnDld2VGLwZM9JU 6go6RxZIzMloWPkzeYMuUVpYfWrvZK/N1KTYIM07mxiiFojZovtLdcvxExC7MyHXnEEZ UOOas2hFatgUEXiLgBWU9LVi3H6Anb0nyxzInY7BKIKswR+lRxP+RecnfOXbMDhCru1u qNNAewa9hm7YQAW6IywRCUj5t1RRbgaAxHdFS/2Z1hn7nLLjeHxmF+TCf9tWAkYjvBYd Jr1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hPgUe9lX4McMY8NXu/5cUyHU5+8iuToZI/13QzjOzwU=; fh=6lcuo2TodNFEVWy8PlSVKe6GaJR0lTIagoaSLp7X5og=; b=zSqATjoewdXvPIbAed14mmfEUsZdP3ymBlmFNNqDGzYgCoqESklfHAyE9SBJ5brHuI JQLBodiKH8qqpWDfPF0U76jGINOsgo3bBbKKIzyEjnP9uWgF6vZhMh6BXaNN2DHlyLxD rOS8fgL2ZpsoUx/xoQrYTPISCAdrr2G1IQO8Cri/twO/H0pDWNog90PxHUM5r1BvdRDj BHoFXQYPuiQxD7G0/yFqB+EpGJHMb+/uyc3D7dJDbvqmDfjuOwvmNRee/rUgGYmN4hjV 9W1rU41lNATZL4OC/ihfde/N/qI9g4kR6Myhkm7XnPrc4zwQk+rv5dsl/kpM7UVd2pCW gpOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gHp6ZGRR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id p6-20020a170902780600b001bf095dfb74si9987266pll.66.2023.09.29.00.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 00:02:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gHp6ZGRR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B968182C52F7; Thu, 28 Sep 2023 13:46:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232039AbjI1Uqg (ORCPT <rfc822;pwkd43@gmail.com> + 22 others); Thu, 28 Sep 2023 16:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjI1Uqf (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 28 Sep 2023 16:46:35 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8C43180; Thu, 28 Sep 2023 13:46:33 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-504a7f9204eso3170888e87.3; Thu, 28 Sep 2023 13:46:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695933992; x=1696538792; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hPgUe9lX4McMY8NXu/5cUyHU5+8iuToZI/13QzjOzwU=; b=gHp6ZGRRyrpPAAIoIDqy4zR7OMQGU3m/Jz1PUvgLP5hVRJu16MZjlc14NefR/u530k 5ENoefhL7cs6vSyzQo5G3p/P8my8CpUeR+Az/wnpFwubWR3IBm/m+odzuZRI3tTFIbX1 z3rguvmSB2koYZHAbIWV71Ze1+WXZFSX0HzW2PmmTo7tBuJOD/wgtGyOcddUfUo6gFDG hr5dt3RpmK+LHZdniTcFrcrpMlvIytv1oTQtvem0omLUwzJ9XD21Hw+mp4nDQMQ8sQ3L PzEQEOpj8Aq2L76btCr6JAfNfInZMHbXx+OHCPFnf3QinQkq/jM7tUn9FpV+5bijaUYd aJhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695933992; x=1696538792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hPgUe9lX4McMY8NXu/5cUyHU5+8iuToZI/13QzjOzwU=; b=eX6dxRTuhC48DTmWg0t4QfAc7L16+d+t2DXbwS50u76cWrfw16mFuDlpCxg7uWtQVQ sruAs57Kj+5S9+1Qmt3iyjGvqy9ZkpyzRItdNpzEMmUCmNVXBdBkXVEsiM3NNllZnDR4 PRZFWBDNFt203jHV63brC22mZ/F/AQE5YW/pX4fGiuLtaZ5gIpj9mS8wM2VrvrFjQXTW d9H9Ibk/UTeEU+IZwPhXCVgtaYoMPl3v54wn3Sb2dFoMZ/9arXjjmEGDnNYuFXC2uSKO YXXMn+OIPEEqQUK5drWwNvzLlBK+Zoxr/E1C7rpUoUG8Eq5mulJ5uuXbAkzPyrdllXBY ZPQw== X-Gm-Message-State: AOJu0Yy7erSpjaHWfladBs0kjgA5uFP0VTBgEeV91xunBKTka9VQM4KX 3gfUacqbaoSKwphZaoQm2OU= X-Received: by 2002:a05:6512:3451:b0:500:8f65:c624 with SMTP id j17-20020a056512345100b005008f65c624mr1822930lfr.53.1695933991637; Thu, 28 Sep 2023 13:46:31 -0700 (PDT) Received: from localhost.localdomain ([154.133.201.230]) by smtp.gmail.com with ESMTPSA id l23-20020a19c217000000b00501c77ad909sm3233139lfc.208.2023.09.28.13.46.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 13:46:31 -0700 (PDT) From: Andrew Kanner <andrew.kanner@gmail.com> To: bjorn@kernel.org, magnus.karlsson@intel.com, maciej.fijalkowski@intel.com, jonathan.lemon@gmail.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, aleksander.lobakin@intel.com, xuanzhuo@linux.alibaba.com Cc: linux-kernel-mentees@lists.linuxfoundation.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+fae676d3cf469331fc89@syzkaller.appspotmail.com, Andrew Kanner <andrew.kanner@gmail.com> Subject: [PATCH net-next v1] net/xdp: fix zero-size allocation warning in xskq_create() Date: Thu, 28 Sep 2023 23:44:40 +0300 Message-Id: <20230928204440.543-1-andrew.kanner@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <000000000000c84b4705fb31741e@google.com> References: <000000000000c84b4705fb31741e@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 28 Sep 2023 13:46:39 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778354444438361425 X-GMAIL-MSGID: 1778354444438361425 |
Series |
[net-next,v1] net/xdp: fix zero-size allocation warning in xskq_create()
|
|
Commit Message
Andrew Kanner
Sept. 28, 2023, 8:44 p.m. UTC
Syzkaller reported the following issue:
------------[ cut here ]------------
WARNING: CPU: 0 PID: 2807 at mm/vmalloc.c:3247 __vmalloc_node_range (mm/vmalloc.c:3361)
Modules linked in:
CPU: 0 PID: 2807 Comm: repro Not tainted 6.6.0-rc2+ #12
Hardware name: Generic DT based system
unwind_backtrace from show_stack (arch/arm/kernel/traps.c:258)
show_stack from dump_stack_lvl (lib/dump_stack.c:107 (discriminator 1))
dump_stack_lvl from __warn (kernel/panic.c:633 kernel/panic.c:680)
__warn from warn_slowpath_fmt (./include/linux/context_tracking.h:153 kernel/panic.c:700)
warn_slowpath_fmt from __vmalloc_node_range (mm/vmalloc.c:3361 (discriminator 3))
__vmalloc_node_range from vmalloc_user (mm/vmalloc.c:3478)
vmalloc_user from xskq_create (net/xdp/xsk_queue.c:40)
xskq_create from xsk_setsockopt (net/xdp/xsk.c:953 net/xdp/xsk.c:1286)
xsk_setsockopt from __sys_setsockopt (net/socket.c:2308)
__sys_setsockopt from ret_fast_syscall (arch/arm/kernel/entry-common.S:68)
xskq_get_ring_size() uses struct_size() macro to safely calculate the
size of struct xsk_queue and q->nentries of desc members. But the
syzkaller repro was able to set q->nentries with the value initially
taken from copy_from_sockptr() high enough to return SIZE_MAX by
struct_size(). The next PAGE_ALIGN(size) is such case will overflow
the size_t value and set it to 0. This will trigger WARN_ON_ONCE in
vmalloc_user() -> __vmalloc_node_range().
The issue is reproducible on 32-bit arm kernel.
Reported-and-tested-by: syzbot+fae676d3cf469331fc89@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/000000000000c84b4705fb31741e@google.com/T/
Link: https://syzkaller.appspot.com/bug?extid=fae676d3cf469331fc89
Fixes: 9f78bf330a66 ("xsk: support use vaddr as ring")
Signed-off-by: Andrew Kanner <andrew.kanner@gmail.com>
---
RFC notes:
It was found that net/xdp/xsk.c:xsk_setsockopt() uses
copy_from_sockptr() to get the number of entries (int) for cases with
XDP_RX_RING/XDP_TX_RING and XDP_UMEM_FILL_RING/XDP_UMEM_COMPLETION_RING.
Next in xsk_init_queue() there're 2 sanity checks (entries == 0) and
(!is_power_of_2(entries)) for which -EINVAL will be returned.
After that net/xdp/xsk_queue.c:xskq_create() will calculate the size
multipling the number of entries (int) with the size of u64, at least.
I wonder if there should be the upper bound (e.g. the 3rd sanity check
inside xsk_init_queue()). It seems that without the upper limit it's
quiet easy to overflow the allocated size (SIZE_MAX), especially for
32-bit architectures, for example arm nodes which were used by the
syzkaller.
In this patch I added a naive check for SIZE_MAX which helped to
skip zero-size allocation after overflow, but maybe it's not quite
right. Please, suggest if you have any thoughts about the appropriate
limit for the size of these xdp rings.
PS: the initial number of entries is 0x20000000 in syzkaller repro:
syscall(__NR_setsockopt, (intptr_t)r[0], 0x11b, 3, 0x20000040, 0x20);
Link: https://syzkaller.appspot.com/text?tag=ReproC&x=10910f18280000
net/xdp/xsk_queue.c | 3 +++
1 file changed, 3 insertions(+)
Comments
From: Andrew Kanner <andrew.kanner@gmail.com> Date: Thu, 28 Sep 2023 23:44:40 +0300 > Syzkaller reported the following issue: [...] > PS: the initial number of entries is 0x20000000 in syzkaller repro: > syscall(__NR_setsockopt, (intptr_t)r[0], 0x11b, 3, 0x20000040, 0x20); > > Link: https://syzkaller.appspot.com/text?tag=ReproC&x=10910f18280000 > > net/xdp/xsk_queue.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/net/xdp/xsk_queue.c b/net/xdp/xsk_queue.c > index f8905400ee07..1bc7fb1f14ae 100644 > --- a/net/xdp/xsk_queue.c > +++ b/net/xdp/xsk_queue.c > @@ -34,6 +34,9 @@ struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) > q->ring_mask = nentries - 1; > > size = xskq_get_ring_size(q, umem_queue); > + if (size == SIZE_MAX) unlikely(). > + return NULL; > + > size = PAGE_ALIGN(size); > > q->ring = vmalloc_user(size); Thanks, Olek
On Mon, Oct 02, 2023 at 03:52:44PM +0200, Alexander Lobakin wrote: > From: Andrew Kanner <andrew.kanner@gmail.com> > Date: Thu, 28 Sep 2023 23:44:40 +0300 > > > Syzkaller reported the following issue: > > [...] > > > PS: the initial number of entries is 0x20000000 in syzkaller repro: > > syscall(__NR_setsockopt, (intptr_t)r[0], 0x11b, 3, 0x20000040, 0x20); > > > > Link: https://syzkaller.appspot.com/text?tag=ReproC&x=10910f18280000 > > > > net/xdp/xsk_queue.c | 3 +++ > > 1 file changed, 3 insertions(+) > > > > diff --git a/net/xdp/xsk_queue.c b/net/xdp/xsk_queue.c > > index f8905400ee07..1bc7fb1f14ae 100644 > > --- a/net/xdp/xsk_queue.c > > +++ b/net/xdp/xsk_queue.c > > @@ -34,6 +34,9 @@ struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) > > q->ring_mask = nentries - 1; > > > > size = xskq_get_ring_size(q, umem_queue); > > + if (size == SIZE_MAX) > > unlikely(). > > > + return NULL; > > + > > size = PAGE_ALIGN(size); > > > > q->ring = vmalloc_user(size); > > Thanks, > Olek Thanks, Olek. That is a reasonable optimization, I'll add it in v2. -- pw-bot: cr Andrew Kanner
diff --git a/net/xdp/xsk_queue.c b/net/xdp/xsk_queue.c index f8905400ee07..1bc7fb1f14ae 100644 --- a/net/xdp/xsk_queue.c +++ b/net/xdp/xsk_queue.c @@ -34,6 +34,9 @@ struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) q->ring_mask = nentries - 1; size = xskq_get_ring_size(q, umem_queue); + if (size == SIZE_MAX) + return NULL; + size = PAGE_ALIGN(size); q->ring = vmalloc_user(size);