[net,v2] net: ethernet: fix use after free bug in ns83820_remove_one due to race condition
Message ID | 20230413071401.210599-1-zyytlz.wz@163.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp846758vqo; Thu, 13 Apr 2023 00:23:25 -0700 (PDT) X-Google-Smtp-Source: AKy350YUJaAH8VHsLp3HYGawYaX5B9URv1nR3Lmdc0kSY1wV5FC7krs++ZJy41FYulylY1SrbIIP X-Received: by 2002:a17:907:1b1b:b0:94a:930e:b226 with SMTP id mp27-20020a1709071b1b00b0094a930eb226mr1886246ejc.50.1681370605058; Thu, 13 Apr 2023 00:23:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681370605; cv=none; d=google.com; s=arc-20160816; b=0+plxXot9VIuQ0jV5e4mmxOxpa90lvpYHanvXrAoQKV+sX3+Jalcu1jJViXbsCQ1T5 +ZfU3/AO7hgzdLbW3XRyxu0OSiHX7246mDNEf/Tvec7aDJ/gJp5Lf4tKBSoG2RYGs1id rTModX+tJOln4TNE6EIfHhv2MysVbjfK2l9crtYE15LowqaYx6TJu7Nx4hBByuH1YsDq A9M+q0NOl8RYCGDp46XjTPdKhjBx9caYgyp3+knJXYtli0xzr2s7ZG+IPgOj0kyiTcjr FMLWyWNGMZAoiJmIoxZQ2XghDKfbkrPiM7AzlffTuIlCdAbbmtRkwYLtNWl/HMoLRZv1 k6ZA== 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=J68mshiUh+tWg8G3Lc53Yvgg2fGvMvWtMKcuYE3nO5I=; b=0Yo705zaoUrbeCfBkWB0FzAZwfl/YYfWozdJ82dqMCBwYzeL1/tW/xQy77lgD7ZBgb FqQdOKDfgnmLsJpAToUU/raR0EoWglU/PywwxDBKDwhykLHqhx3ams77IYWsfT8E3B2t THY5tN9MxyF5fQEHOxan5S3t0vhzolIzoVRrt6sxn8olX8aPyiRJXZTh707x1HCAHR91 YOSk49if4+nwNZmqhA1wCmQjxaFmvYAS+JNWQDe2quSC3IXPTTTLrmEtdfZTKWfv29fG oXwgqcltlyyvO3dGWQ76rLHdUrvGUBKkmMYyjeembTgsQEkzZMGSehUPChwzAzpMwP8N jIfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=HYlqsnyL; 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 eq19-20020a170907291300b009334f4a14cfsi1175299ejc.483.2023.04.13.00.23.01; Thu, 13 Apr 2023 00:23:25 -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=HYlqsnyL; 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 S229790AbjDMHOr (ORCPT <rfc822;peter110.wang@gmail.com> + 99 others); Thu, 13 Apr 2023 03:14:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229579AbjDMHOp (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 13 Apr 2023 03:14:45 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.214]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 206F14C19; Thu, 13 Apr 2023 00:14:43 -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=J68ms hiUh+tWg8G3Lc53Yvgg2fGvMvWtMKcuYE3nO5I=; b=HYlqsnyLgt0bx1UIy1aiM wH6Co+vP7KviGJW3OVaGOAMb5Jz78biTlarSH91awvBVN6DOoOQTErvQA23S11SS KYe8XTHds+cg+cZQHqth+V1JTQG3q7n67l2ltf26CyrxGbZF+o3LiTd4RzORTSAi YRIlr/WWIq4+sQh2XrhQQU= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g4-1 (Coremail) with SMTP id _____wDXGOy7qzdkZEIsBQ--.1078S2; Thu, 13 Apr 2023 15:14:03 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: davem@davemloft.net Cc: edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, 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 v2] net: ethernet: fix use after free bug in ns83820_remove_one due to race condition Date: Thu, 13 Apr 2023 15:14:01 +0800 Message-Id: <20230413071401.210599-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wDXGOy7qzdkZEIsBQ--.1078S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7AFykCw1xJr1UWw47Zr1DZFb_yoW8Xw1rp3 90kFyfuF1ktw4UWw1UJr40qry5XFs8t3yYgayIyw4avas5Zr4vgF4UKFWUZr18GrWqvr4f Aw45Zw43uas8ZaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziFAprUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiGg1QU1aEE4gpSgAEs6 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_MSPIKE_H2, 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?1763044863359112263?= X-GMAIL-MSGID: =?utf-8?q?1763044863359112263?= |
Series |
[net,v2] net: ethernet: fix use after free bug in ns83820_remove_one due to race condition
|
|
Commit Message
Zheng Wang
April 13, 2023, 7:14 a.m. UTC
In ns83820_init_one, dev->tq_refill was bound with queue_refill.
If irq happens, it will call ns83820_irq->ns83820_do_isr.
Then it invokes tasklet_schedule(&dev->rx_tasklet) to start
rx_action function. And rx_action will call ns83820_rx_kick
and finally start queue_refill function.
If we remove the driver without finishing the work, there
may be a race condition between ndev, which may cause UAF
bug.
CPU0 CPU1
|queue_refill
ns83820_remove_one |
free_netdev |
put_device |
free ndev |
|rx_refill
|//use ndev
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
v2:
- cancel the work after unregister_netdev to make sure there
is no more request suggested by Jakub Kicinski
---
drivers/net/ethernet/natsemi/ns83820.c | 5 +++++
1 file changed, 5 insertions(+)
Comments
The 04/13/2023 15:14, Zheng Wang wrote: Hi Zheng, > > In ns83820_init_one, dev->tq_refill was bound with queue_refill. > > If irq happens, it will call ns83820_irq->ns83820_do_isr. > Then it invokes tasklet_schedule(&dev->rx_tasklet) to start > rx_action function. And rx_action will call ns83820_rx_kick > and finally start queue_refill function. > > If we remove the driver without finishing the work, there > may be a race condition between ndev, which may cause UAF > bug. > > CPU0 CPU1 > > |queue_refill > ns83820_remove_one | > free_netdev | > put_device | > free ndev | > |rx_refill > |//use ndev Will you not have the same issue if you remove the driver after you schedule rx_tasklet? Because rx_action will use also ndev. > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > --- > v2: > - cancel the work after unregister_netdev to make sure there > is no more request suggested by Jakub Kicinski > --- > drivers/net/ethernet/natsemi/ns83820.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/drivers/net/ethernet/natsemi/ns83820.c b/drivers/net/ethernet/natsemi/ns83820.c > index 998586872599..2e84b9fcd8e9 100644 > --- a/drivers/net/ethernet/natsemi/ns83820.c > +++ b/drivers/net/ethernet/natsemi/ns83820.c > @@ -2208,8 +2208,13 @@ static void ns83820_remove_one(struct pci_dev *pci_dev) > > ns83820_disable_interrupts(dev); /* paranoia */ > > + netif_carrier_off(ndev); > + netif_tx_disable(ndev); > + > unregister_netdev(ndev); > free_irq(dev->pci_dev->irq, ndev); > + cancel_work_sync(&dev->tq_refill); > + > iounmap(dev->base); > dma_free_coherent(&dev->pci_dev->dev, 4 * DESC_SIZE * NR_TX_DESC, > dev->tx_descs, dev->tx_phy_descs); > -- > 2.25.1 >
Horatiu Vultur <horatiu.vultur@microchip.com> 于2023年4月13日周四 18:01写道: > > The 04/13/2023 15:14, Zheng Wang wrote: > > Hi Zheng, > > > > > In ns83820_init_one, dev->tq_refill was bound with queue_refill. > > > > If irq happens, it will call ns83820_irq->ns83820_do_isr. > > Then it invokes tasklet_schedule(&dev->rx_tasklet) to start > > rx_action function. And rx_action will call ns83820_rx_kick > > and finally start queue_refill function. > > > > If we remove the driver without finishing the work, there > > may be a race condition between ndev, which may cause UAF > > bug. > > > > CPU0 CPU1 > > > > |queue_refill > > ns83820_remove_one | > > free_netdev | > > put_device | > > free ndev | > > |rx_refill > > |//use ndev > > Will you not have the same issue if you remove the driver after you > schedule rx_tasklet? Because rx_action will use also ndev. > Hello Horatiu, Thanks for your reply. In ns83820_remove_one, there is an invoking: free_irq(dev->pci_dev->irq, ndev); This will prevent the driver from handling more irq, But it couldn't prevent the rx_tasklet from being scheduled. So I think we should add the following code: tasklet_kill(&dev->rx_tasklet); after free_irq invoking. Is there anything wrong about my analysis? Thanks again for pointing the mistake out. Best regards, Zheng > > > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > --- > > v2: > > - cancel the work after unregister_netdev to make sure there > > is no more request suggested by Jakub Kicinski > > --- > > drivers/net/ethernet/natsemi/ns83820.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/drivers/net/ethernet/natsemi/ns83820.c b/drivers/net/ethernet/natsemi/ns83820.c > > index 998586872599..2e84b9fcd8e9 100644 > > --- a/drivers/net/ethernet/natsemi/ns83820.c > > +++ b/drivers/net/ethernet/natsemi/ns83820.c > > @@ -2208,8 +2208,13 @@ static void ns83820_remove_one(struct pci_dev *pci_dev) > > > > ns83820_disable_interrupts(dev); /* paranoia */ > > > > + netif_carrier_off(ndev); > > + netif_tx_disable(ndev); > > + > > unregister_netdev(ndev); > > free_irq(dev->pci_dev->irq, ndev); > > + cancel_work_sync(&dev->tq_refill); > > + > > iounmap(dev->base); > > dma_free_coherent(&dev->pci_dev->dev, 4 * DESC_SIZE * NR_TX_DESC, > > dev->tx_descs, dev->tx_phy_descs); > > -- > > 2.25.1 > > > > -- > /Horatiu
The 04/13/2023 18:49, Zheng Hacker wrote: Hi Zheng, > > Horatiu Vultur <horatiu.vultur@microchip.com> 于2023年4月13日周四 18:01写道: > > > > The 04/13/2023 15:14, Zheng Wang wrote: > > > > Hi Zheng, > > > > > > > > In ns83820_init_one, dev->tq_refill was bound with queue_refill. > > > > > > If irq happens, it will call ns83820_irq->ns83820_do_isr. > > > Then it invokes tasklet_schedule(&dev->rx_tasklet) to start > > > rx_action function. And rx_action will call ns83820_rx_kick > > > and finally start queue_refill function. > > > > > > If we remove the driver without finishing the work, there > > > may be a race condition between ndev, which may cause UAF > > > bug. > > > > > > CPU0 CPU1 > > > > > > |queue_refill > > > ns83820_remove_one | > > > free_netdev | > > > put_device | > > > free ndev | > > > |rx_refill > > > |//use ndev > > > > Will you not have the same issue if you remove the driver after you > > schedule rx_tasklet? Because rx_action will use also ndev. > > > > Hello Horatiu, > > Thanks for your reply. In ns83820_remove_one, there is an invoking: > > free_irq(dev->pci_dev->irq, ndev); > > This will prevent the driver from handling more irq, But it couldn't prevent > the rx_tasklet from being scheduled. So I think we should add the > following code: > > tasklet_kill(&dev->rx_tasklet); > > after free_irq invoking. Is there anything wrong about my analysis? I think you are right, I don't see a problem. > > Thanks again for pointing the mistake out. > > Best regards, > Zheng > > > > > > > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > > --- > > > v2: > > > - cancel the work after unregister_netdev to make sure there > > > is no more request suggested by Jakub Kicinski > > > --- > > > drivers/net/ethernet/natsemi/ns83820.c | 5 +++++ > > > 1 file changed, 5 insertions(+) > > > > > > diff --git a/drivers/net/ethernet/natsemi/ns83820.c b/drivers/net/ethernet/natsemi/ns83820.c > > > index 998586872599..2e84b9fcd8e9 100644 > > > --- a/drivers/net/ethernet/natsemi/ns83820.c > > > +++ b/drivers/net/ethernet/natsemi/ns83820.c > > > @@ -2208,8 +2208,13 @@ static void ns83820_remove_one(struct pci_dev *pci_dev) > > > > > > ns83820_disable_interrupts(dev); /* paranoia */ > > > > > > + netif_carrier_off(ndev); > > > + netif_tx_disable(ndev); > > > + > > > unregister_netdev(ndev); > > > free_irq(dev->pci_dev->irq, ndev); > > > + cancel_work_sync(&dev->tq_refill); > > > + > > > iounmap(dev->base); > > > dma_free_coherent(&dev->pci_dev->dev, 4 * DESC_SIZE * NR_TX_DESC, > > > dev->tx_descs, dev->tx_phy_descs); > > > -- > > > 2.25.1 > > > > > > > -- > > /Horatiu
diff --git a/drivers/net/ethernet/natsemi/ns83820.c b/drivers/net/ethernet/natsemi/ns83820.c index 998586872599..2e84b9fcd8e9 100644 --- a/drivers/net/ethernet/natsemi/ns83820.c +++ b/drivers/net/ethernet/natsemi/ns83820.c @@ -2208,8 +2208,13 @@ static void ns83820_remove_one(struct pci_dev *pci_dev) ns83820_disable_interrupts(dev); /* paranoia */ + netif_carrier_off(ndev); + netif_tx_disable(ndev); + unregister_netdev(ndev); free_irq(dev->pci_dev->irq, ndev); + cancel_work_sync(&dev->tq_refill); + iounmap(dev->base); dma_free_coherent(&dev->pci_dev->dev, 4 * DESC_SIZE * NR_TX_DESC, dev->tx_descs, dev->tx_phy_descs);