[net] 9p/xen : Fix use after free bug in xen_9pfs_front_remove due to race condition
Message ID | 20230313041303.3158458-1-zyytlz.wz@163.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1000099wrd; Sun, 12 Mar 2023 21:37:38 -0700 (PDT) X-Google-Smtp-Source: AK7set8ByE6u3X2iZEe/tWR9/O2hXtd7RI/33ZS9wJwtHs5TfClI0K35v/BtY/6sMpeBFY/xDqZL X-Received: by 2002:a17:90a:6006:b0:239:cb9b:6886 with SMTP id y6-20020a17090a600600b00239cb9b6886mr33999811pji.20.1678682258291; Sun, 12 Mar 2023 21:37:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678682258; cv=none; d=google.com; s=arc-20160816; b=tcVVbLP+cLOFoyFG2iCMCjr+VJMGfG7xHNvNEVQ4JE/5R9wutH8iAd1H4a1wAn9D+/ joD9Ac5iC+NYAIjgXijE1x244hYzhyur1WW95Qb54ypDad5izgrLs7mQ8CfSK9uvhdl3 1eYOAysSWUs/Rb8/vAJef+78ZCR2VS7FqpMBvwO1zDdsKkKiGJVM0sKwoQV8K10EVOzm LPEA6rMNmPCWRb8iJneYW9waVmrpCvWNsVO5pZfz2cxIGMJcnT2DL4Xa3SQ3nIG+Z/ZP kcK+ASXJnjZRx+3DmRgrWreplzmfUy1PL7ifpZBYpry2NW0Da4/MiObHOBfjR+g8z2Of 6Lvg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=SHitx/p2v515lWIUtrn7VLSosh3Eis87AfBcSEwzhzA=; b=A7hL1HAVuQEnB3Pf6juejAeeuHOebaojsSZ164aTNsYj3Kh6IdiLT/6AN4Yvdq3410 F1H/LeB9p6BRBZ4ZfmWRaTtb92O3xluYUa3r6MykI8RONHTv1J1A72rY8O4jaa+31lxA zI/AJPhGUkqTJ4U7do3yD/Y+PxB2EyzOSPWJriE938O/yG23RDMENkALTDXjvtkhTDAY FStSV4FqJLvVeN7b8g6PcNtfpdAHucqPFdl8QQr799gfArRd2SNYSJMiDvuAMJUZQ5in mbyORubdjpqOI9WQW2AsBIqTUy2hNBXSwpOmGOMkNht/MtPModwgzhXW4Wa/T+mra/Zt v6Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b="jxU/5wgc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r5-20020a17090b050500b002340f36b545si5402931pjz.18.2023.03.12.21.37.23; Sun, 12 Mar 2023 21:37:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b="jxU/5wgc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229872AbjCMEPH (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Mon, 13 Mar 2023 00:15:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229656AbjCMEPE (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 13 Mar 2023 00:15:04 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C12152886E; Sun, 12 Mar 2023 21:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=SHitx /p2v515lWIUtrn7VLSosh3Eis87AfBcSEwzhzA=; b=jxU/5wgcfYNdDyK7u6Hjc DjkHb77etGnELIsNQN/ri/2VB+L9s/MYXYheXz0Ie/NKcr0oPNGpYrCMCrYdSea0 eK9QeazJYKHM0Ig3giywQfJpnz36lEWQR2jw08WBNSaWKG2YaWNnlLzlPGeR+lLX FYeMbjn3pEWORK8silVnt8= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g4-0 (Coremail) with SMTP id _____wBXdh_Rog5kJ4YGAA--.1868S2; Mon, 13 Mar 2023 12:13:05 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: ericvh@gmail.com Cc: lucho@ionkov.net, asmadeus@codewreck.org, linux_oss@crudebyte.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, v9fs-developer@lists.sourceforge.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH net] 9p/xen : Fix use after free bug in xen_9pfs_front_remove due to race condition Date: Mon, 13 Mar 2023 12:13:03 +0800 Message-Id: <20230313041303.3158458-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wBXdh_Rog5kJ4YGAA--.1868S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7tF15Cw48JFy5tr1xuF45Wrg_yoW8WF4Upa 1Skrn5AFyqya1YyFsYy3WxJ3WYkw4rGr1Iga12kw4fJr98Ary8XrZ5tr1Yg34UAr4YqF4r Gw1DXa98JFZrAw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziaZXrUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXQ8xU1WBo41K9wABs2 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,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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760225927647844423?= X-GMAIL-MSGID: =?utf-8?q?1760225927647844423?= |
Series |
[net] 9p/xen : Fix use after free bug in xen_9pfs_front_remove due to race condition
|
|
Commit Message
Zheng Wang
March 13, 2023, 4:13 a.m. UTC
In xen_9pfs_front_probe, it calls xen_9pfs_front_alloc_dataring
to init priv->rings and bound &ring->work with p9_xen_response.
When it calls xen_9pfs_front_event_handler to handle IRQ requests,
it will finally call schedule_work to start the work.
When we call xen_9pfs_front_remove to remove the driver, there
may be a sequence as follows:
Fix it by finishing the work before cleanup in xen_9pfs_front_free.
Note that, this bug is found by static analysis, which might be
false positive.
CPU0 CPU1
|p9_xen_response
xen_9pfs_front_remove|
xen_9pfs_front_free|
kfree(priv) |
//free priv |
|p9_tag_lookup
|//use priv->client
Fixes: 71ebd71921e4 ("xen/9pfs: connect to the backend")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
net/9p/trans_xen.c | 5 +++++
1 file changed, 5 insertions(+)
Comments
Hi Zheng, Thank you for the patch! Yet something to improve: [auto build test ERROR on net/master] url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/9p-xen-Fix-use-after-free-bug-in-xen_9pfs_front_remove-due-to-race-condition/20230313-121534 patch link: https://lore.kernel.org/r/20230313041303.3158458-1-zyytlz.wz%40163.com patch subject: [PATCH net] 9p/xen : Fix use after free bug in xen_9pfs_front_remove due to race condition config: arm-randconfig-r046-20230313 (https://download.01.org/0day-ci/archive/20230313/202303131508.drILo2xA-lkp@intel.com/config) compiler: arm-linux-gnueabi-gcc (GCC) 12.1.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/bf2159b54bb14c42221106b8681c471d005e7345 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Zheng-Wang/9p-xen-Fix-use-after-free-bug-in-xen_9pfs_front_remove-due-to-race-condition/20230313-121534 git checkout bf2159b54bb14c42221106b8681c471d005e7345 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash net/9p/ If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202303131508.drILo2xA-lkp@intel.com/ All errors (new ones prefixed by >>): net/9p/trans_xen.c: In function 'xen_9pfs_front_free': >> net/9p/trans_xen.c:284:24: error: incompatible types when assigning to type 'struct xen_9pfs_dataring *' from type 'struct xen_9pfs_dataring' 284 | ring = priv->rings[i]; | ^~~~ vim +284 net/9p/trans_xen.c 273 274 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv) 275 { 276 int i, j; 277 struct xen_9pfs_dataring *ring = NULL; 278 279 write_lock(&xen_9pfs_lock); 280 list_del(&priv->list); 281 write_unlock(&xen_9pfs_lock); 282 283 for (i = 0; i < priv->num_rings; i++) { > 284 ring = priv->rings[i]; 285 if (!priv->rings[i].intf) 286 break; 287 if (priv->rings[i].irq > 0) 288 unbind_from_irqhandler(priv->rings[i].irq, priv->dev); 289 290 cancel_work_sync(&ring->work); 291 292 if (priv->rings[i].data.in) { 293 for (j = 0; 294 j < (1 << priv->rings[i].intf->ring_order); 295 j++) { 296 grant_ref_t ref; 297 298 ref = priv->rings[i].intf->ref[j]; 299 gnttab_end_foreign_access(ref, NULL); 300 } 301 free_pages_exact(priv->rings[i].data.in, 302 1UL << (priv->rings[i].intf->ring_order + 303 XEN_PAGE_SHIFT)); 304 } 305 gnttab_end_foreign_access(priv->rings[i].ref, NULL); 306 free_page((unsigned long)priv->rings[i].intf); 307 } 308 kfree(priv->rings); 309 kfree(priv->tag); 310 kfree(priv); 311 } 312
diff --git a/net/9p/trans_xen.c b/net/9p/trans_xen.c index c64050e839ac..60adb3aadd63 100644 --- a/net/9p/trans_xen.c +++ b/net/9p/trans_xen.c @@ -274,16 +274,21 @@ static const struct xenbus_device_id xen_9pfs_front_ids[] = { static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv) { int i, j; + struct xen_9pfs_dataring *ring = NULL; write_lock(&xen_9pfs_lock); list_del(&priv->list); write_unlock(&xen_9pfs_lock); for (i = 0; i < priv->num_rings; i++) { + ring = priv->rings[i]; if (!priv->rings[i].intf) break; if (priv->rings[i].irq > 0) unbind_from_irqhandler(priv->rings[i].irq, priv->dev); + + cancel_work_sync(&ring->work); + if (priv->rings[i].data.in) { for (j = 0; j < (1 << priv->rings[i].intf->ring_order);