[v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition
Message ID | 20230313165128.3763626-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 v21csp1298494wrd; Mon, 13 Mar 2023 10:02:01 -0700 (PDT) X-Google-Smtp-Source: AK7set8tAtq0xJ28MNszYp+J6SYXuSd28q0Ruy08Sn6Ra6zwC1DQ8itvk+krrVSFz2Wmuw4eRpU2 X-Received: by 2002:a62:7b0c:0:b0:61d:e8bb:1cb0 with SMTP id w12-20020a627b0c000000b0061de8bb1cb0mr12859703pfc.1.1678726921400; Mon, 13 Mar 2023 10:02:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678726921; cv=none; d=google.com; s=arc-20160816; b=qY3RfEZjopzuSWX+d8expBwCHl4L6kXEtii3nRB44uNF/gZSjtjHZOvjH1S6YuxyON JZzV9TAr6Y83DOtDKt1Euo6FojcHSHudIYcVm+cNiI6gkJtjoy9jlFUuFGBdNUzolDNY zr3taNEBNfvE9ZDatM/eGwCEi3ouMWV5FyR2HndZQuvQ500uy6UxTd2VvEBDE7DT38DY 9ePLnDseVSkddBIA0MfkXaRMKGD8uRKWsTNBCtLLd0savvFMWFEM+fA0e5e6xUAzJyMW thja7PiQPfjwtUXTDVw5q0w1sGPyy/kTqCn/S9Qi6Gs3GYbYedTBn8IHugtXcny3ifau RbWw== 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=2yqG3Kr7Cl9HIQ+InYzhFymxRJDXG2QuIzgYGnQnHiE=; b=tw7BceR1OSA650mhKbBnBXuWAkk7NFZ1pTIPwGkjdwsgcPnsDqUaOiI4mQkp2P4qLK ZI6AGIhQn/z8bp1iwKI9T/jaeNUGAyZM6eL4E0PwFwh3yM6ytlsqwM/xybjBQcBwKTRM /LookmoY2Vpu06cIH20pwd7BfFF+X6VM6FVm9jgH51gDlCwNaTzULrsqCkDZ+5uCDL2Y IOx0pjhlCg/lkW7GJXH9h/z2hlCH00qLpGICPFBZ21Ki7Dssu2JYlkNRkRxc72n6Kg9X R11soKBgAmCoOXSMOhyFQ54NFmSj+p902UHz7/8qBnurLjdLWAYZSHxJr8APr2JfJTSe H4bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=Tyzw6sHW; 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 u5-20020a63d345000000b0050aeaf9f26bsi313667pgi.154.2023.03.13.10.01.47; Mon, 13 Mar 2023 10:02:01 -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=Tyzw6sHW; 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 S230374AbjCMQwq (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Mon, 13 Mar 2023 12:52:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230072AbjCMQwk (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 13 Mar 2023 12:52:40 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.196]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 725267B9A7; Mon, 13 Mar 2023 09:52:26 -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=2yqG3 Kr7Cl9HIQ+InYzhFymxRJDXG2QuIzgYGnQnHiE=; b=Tyzw6sHWWKFwf2WW3wQ/j zOuN/sMru+RtSc6vLr9gnUSgp+DonJU7/4WfRJGi+W3S+bYGT+R+gCD/sy7ydllW FLyE/qOarNnCbQ2+GxhzGc/AdotVvs0HTpuQCBpHyj3vZV25Tq8sUWzj0diXkLvk OWLv7oWjNvxLBIboXdpm0o= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g3-2 (Coremail) with SMTP id _____wAHk4CSVA9kolEUAA--.15536S2; Tue, 14 Mar 2023 00:51:30 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: gregkh@linuxfoundation.org Cc: p.zabel@pengutronix.de, biju.das.jz@bp.renesas.com, phil.edworthy@renesas.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, yoshihiro.shimoda.uh@renesas.com, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition Date: Tue, 14 Mar 2023 00:51:28 +0800 Message-Id: <20230313165128.3763626-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wAHk4CSVA9kolEUAA--.15536S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7tF1DGryfJr47ArWxZFyDAwb_yoW8WF1fpF WDGFW5Ar4rGFWjq3y7GFykZFyrCF9rKry7ZFW7tw4xuF1rG3y0qryIqa1jkF1xJFZayr4F q3WDu340qa47u37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziID73UUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiGgMxU1aEEjd7pgACsT X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_RPBL,SPF_HELO_NONE,SPF_PASS autolearn=no 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?1760272760480547683?= X-GMAIL-MSGID: =?utf-8?q?1760272760480547683?= |
Series |
[v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition
|
|
Commit Message
Zheng Wang
March 13, 2023, 4:51 p.m. UTC
In renesas_usb3_probe, &usb3->role_work is bound with renesas_usb3_role_work. renesas_usb3_start will be called to start the work. If we remove the driver which will call usbhs_remove, there may be an unfinished work. The possible sequence is as follows: Fix it by canceling the work before cleanup in the renesas_usb3_remove CPU0 CPU1 |renesas_usb3_role_work renesas_usb3_remove | usb_role_switch_unregister | device_unregister | kfree(sw) | free usb3->role_sw | | usb_role_switch_set_role | //use usb3->role_sw Fixes: 39facfa01c9f ("usb: gadget: udc: renesas_usb3: Add register of usb role switch") Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> Signed-off-by: Zheng Wang <zyytlz.wz@163.com> --- v3: - modify the commit message to make it clearer suggested by Yoshihiro Shimoda v2: - fix typo, use clearer commit message and only cancel the UAF-related work suggested by Yoshihiro Shimoda --- drivers/usb/gadget/udc/renesas_usb3.c | 1 + 1 file changed, 1 insertion(+)
Comments
Hi Zheng, > From: Zheng Wang, Sent: Tuesday, March 14, 2023 1:51 AM > Subject: [PATCH v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition > > In renesas_usb3_probe, &usb3->role_work is bound with > renesas_usb3_role_work. renesas_usb3_start will be called > to start the work. > > If we remove the driver which will call usbhs_remove, there may be > an unfinished work. The possible sequence is as follows: > > Fix it by canceling the work before cleanup in the renesas_usb3_remove > > CPU0 CPU1 > > |renesas_usb3_role_work > renesas_usb3_remove | > usb_role_switch_unregister | > device_unregister | > kfree(sw) | > free usb3->role_sw | > | usb_role_switch_set_role > | //use usb3->role_sw > > Fixes: 39facfa01c9f ("usb: gadget: udc: renesas_usb3: Add register of usb role switch") > Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> JFYI, but IIUC, - In general, my Reviewed-by is described here, not top of your Signed-off-by. - Also, you don't need to submit such this patch. If you would like to submit such a patch for some reason (ping or so on), incrementing patch version is better because original v3 [1] doesn't have my Reviewed-by tag. [1] https://lore.kernel.org/all/20230313094852.3344288-1-zyytlz.wz@163.com/ Best regards, Yoshihiro Shimoda > --- > v3: > - modify the commit message to make it clearer suggested by Yoshihiro Shimoda > v2: > - fix typo, use clearer commit message and only cancel the UAF-related work suggested by Yoshihiro Shimoda > --- > drivers/usb/gadget/udc/renesas_usb3.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c > index bee6bceafc4f..a301af66bd91 100644 > --- a/drivers/usb/gadget/udc/renesas_usb3.c > +++ b/drivers/usb/gadget/udc/renesas_usb3.c > @@ -2661,6 +2661,7 @@ static int renesas_usb3_remove(struct platform_device *pdev) > debugfs_remove_recursive(usb3->dentry); > device_remove_file(&pdev->dev, &dev_attr_role); > > + cancel_work_sync(&usb3->role_work); > usb_role_switch_unregister(usb3->role_sw); > > usb_del_gadget_udc(&usb3->gadget); > -- > 2.25.1
Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> 于2023年3月14日周二 08:16写道: > > Hi Zheng, > > > From: Zheng Wang, Sent: Tuesday, March 14, 2023 1:51 AM > > Subject: [PATCH v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition > > > > In renesas_usb3_probe, &usb3->role_work is bound with > > renesas_usb3_role_work. renesas_usb3_start will be called > > to start the work. > > > > If we remove the driver which will call usbhs_remove, there may be > > an unfinished work. The possible sequence is as follows: > > > > Fix it by canceling the work before cleanup in the renesas_usb3_remove > > > > CPU0 CPU1 > > > > |renesas_usb3_role_work > > renesas_usb3_remove | > > usb_role_switch_unregister | > > device_unregister | > > kfree(sw) | > > free usb3->role_sw | > > | usb_role_switch_set_role > > | //use usb3->role_sw > > > > Fixes: 39facfa01c9f ("usb: gadget: udc: renesas_usb3: Add register of usb role switch") > > Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > JFYI, but IIUC, > - In general, my Reviewed-by is described here, not top of your Signed-off-by. > - Also, you don't need to submit such this patch. If you would like to submit > such a patch for some reason (ping or so on), incrementing patch version is > better because original v3 [1] doesn't have my Reviewed-by tag. > > [1] > https://lore.kernel.org/all/20230313094852.3344288-1-zyytlz.wz@163.com/ > Dear Yoshihir, Thanks for your advice. I am not familiar with the submission rules, thanks for your kind reminder. I'll keep that in mind in the future. Have a good day :) Best regards, Zheng > Best regards, > Yoshihiro Shimoda > > > --- > > v3: > > - modify the commit message to make it clearer suggested by Yoshihiro Shimoda > > v2: > > - fix typo, use clearer commit message and only cancel the UAF-related work suggested by Yoshihiro Shimoda > > --- > > drivers/usb/gadget/udc/renesas_usb3.c | 1 + > > 1 file changed, 1 insertion(+) > > > > diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c > > index bee6bceafc4f..a301af66bd91 100644 > > --- a/drivers/usb/gadget/udc/renesas_usb3.c > > +++ b/drivers/usb/gadget/udc/renesas_usb3.c > > @@ -2661,6 +2661,7 @@ static int renesas_usb3_remove(struct platform_device *pdev) > > debugfs_remove_recursive(usb3->dentry); > > device_remove_file(&pdev->dev, &dev_attr_role); > > > > + cancel_work_sync(&usb3->role_work); > > usb_role_switch_unregister(usb3->role_sw); > > > > usb_del_gadget_udc(&usb3->gadget); > > -- > > 2.25.1 >
On Tue, Mar 14, 2023 at 12:51:28AM +0800, Zheng Wang wrote: > In renesas_usb3_probe, &usb3->role_work is bound with > renesas_usb3_role_work. renesas_usb3_start will be called > to start the work. > > If we remove the driver which will call usbhs_remove, there may be > an unfinished work. The possible sequence is as follows: > > Fix it by canceling the work before cleanup in the renesas_usb3_remove > > CPU0 CPU1 > > |renesas_usb3_role_work > renesas_usb3_remove | > usb_role_switch_unregister | > device_unregister | > kfree(sw) | > free usb3->role_sw | > | usb_role_switch_set_role > | //use usb3->role_sw > > Fixes: 39facfa01c9f ("usb: gadget: udc: renesas_usb3: Add register of usb role switch") > Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > --- > v3: > - modify the commit message to make it clearer suggested by Yoshihiro Shimoda I see 2 v3 patches in the mailing list, which is obviously not correct. Please resubmit a v4 properly. thanks, greg k-h
Greg KH <gregkh@linuxfoundation.org> 于2023年3月16日周四 18:55写道: > > On Tue, Mar 14, 2023 at 12:51:28AM +0800, Zheng Wang wrote: > > In renesas_usb3_probe, &usb3->role_work is bound with > > renesas_usb3_role_work. renesas_usb3_start will be called > > to start the work. > > > > If we remove the driver which will call usbhs_remove, there may be > > an unfinished work. The possible sequence is as follows: > > > > Fix it by canceling the work before cleanup in the renesas_usb3_remove > > > > CPU0 CPU1 > > > > |renesas_usb3_role_work > > renesas_usb3_remove | > > usb_role_switch_unregister | > > device_unregister | > > kfree(sw) | > > free usb3->role_sw | > > | usb_role_switch_set_role > > | //use usb3->role_sw > > > > Fixes: 39facfa01c9f ("usb: gadget: udc: renesas_usb3: Add register of usb role switch") > > Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > --- > > v3: > > - modify the commit message to make it clearer suggested by Yoshihiro Shimoda > > I see 2 v3 patches in the mailing list, which is obviously not correct. > > Please resubmit a v4 properly. > Hi Greg, Thanks for your kind reminder. I'll resubmit a v4 patch later. Best regards, Zheng > thanks, > > greg k-h
diff --git a/drivers/usb/gadget/udc/renesas_usb3.c b/drivers/usb/gadget/udc/renesas_usb3.c index bee6bceafc4f..a301af66bd91 100644 --- a/drivers/usb/gadget/udc/renesas_usb3.c +++ b/drivers/usb/gadget/udc/renesas_usb3.c @@ -2661,6 +2661,7 @@ static int renesas_usb3_remove(struct platform_device *pdev) debugfs_remove_recursive(usb3->dentry); device_remove_file(&pdev->dev, &dev_attr_role); + cancel_work_sync(&usb3->role_work); usb_role_switch_unregister(usb3->role_sw); usb_del_gadget_udc(&usb3->gadget);