Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87EC6C74A4B for ; Mon, 13 Mar 2023 17:22:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229776AbjCMRWR (ORCPT ); Mon, 13 Mar 2023 13:22:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231526AbjCMRVp (ORCPT ); Mon, 13 Mar 2023 13:21:45 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.214]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 92E9E7EA2D; Mon, 13 Mar 2023 10:20:48 -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=eLJUV 6sjqfvqr0F03Ruwnu9Zfyv3B41EL3cm8DE360Q=; b=Rq7/INmZhRuUz7zMSWFOv FpClKUpVM8om0Z91hkaERbkX7bqlunG0xfvS3YvbEzHSu/itud0cFhGYWUe46OnN FhdneKlDi+SzZYW7HCCIk3sNbxpzMjzsAQz9rA/JX2S3N9A1fxoT86KI9WtgHPi9 ztHBjLTScGPtjDGhmevdl0= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g4-3 (Coremail) with SMTP id _____wD3UUyAVg9kwRISAA--.11282S2; Tue, 14 Mar 2023 00:59:45 +0800 (CST) From: Zheng Wang To: ericvh@gmail.com Cc: michal.swiatkowski@linux.intel.com, 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 Subject: [PATCH net v3] 9p/xen : Fix use after free bug in xen_9pfs_front_remove due to race condition Date: Tue, 14 Mar 2023 00:59:41 +0800 Message-Id: <20230313165941.3772964-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wD3UUyAVg9kwRISAA--.11282S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7tF15Cw48JFy5Kr1kWF4DJwb_yoW8Xw43pa naka15CFy8AF10yFsYy3WxJ3WFkw48Gr1Iga12kw4fJr98Zry8XFZ5t34Yga4UArs0qF4r Cw1jgFWDGFWDA3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zE2YLtUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXRQxU1WBo5PgAwABst Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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") Reviewed-by: Michal Swiatkowski Signed-off-by: Zheng Wang --- v3: - remove unnecessary comment and move definition to the for loop suggested by Michal Swiatkowski v2: - fix type error of ring found by kernel test robot --- net/9p/trans_xen.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net/9p/trans_xen.c b/net/9p/trans_xen.c index c64050e839ac..df467ffb52d0 100644 --- a/net/9p/trans_xen.c +++ b/net/9p/trans_xen.c @@ -280,6 +280,10 @@ static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv) write_unlock(&xen_9pfs_lock); for (i = 0; i < priv->num_rings; i++) { + struct xen_9pfs_dataring *ring = &priv->rings[i]; + + cancel_work_sync(&ring->work); + if (!priv->rings[i].intf) break; if (priv->rings[i].irq > 0) -- 2.25.1