[v3] usb: gadget: udc: renesas_usb3: Fix use after free bug in renesas_usb3_remove due to race condition
Message ID | 20230313094852.3344288-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 v21csp1094568wrd; Mon, 13 Mar 2023 03:07:12 -0700 (PDT) X-Google-Smtp-Source: AK7set8VI0wUCQqJjvX0JXwEzeBTU26rWLGwgm5VhoGjB+mLabmExrkExDDDaNTiNssSGmw/1VAB X-Received: by 2002:aa7:9798:0:b0:5a8:5424:d13b with SMTP id o24-20020aa79798000000b005a85424d13bmr28842337pfp.21.1678702032279; Mon, 13 Mar 2023 03:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678702032; cv=none; d=google.com; s=arc-20160816; b=v6mB0lMabeIYUdUBzJpidZLbmanQKdCDq6VPGNrKY2n/jilVgnECKbGqAII0IYrb8l ByzsykefO7EHyz/S2E2/SFz0Bifap+5xiLye77PmQV2qMGcJ8ixnzL0E2ZwQ4mMPN69t dl+RtF9gEdah9Ito0oigkKrXQUBt3ksmb1epRs/z0BA5Nx0uh/AYaeRx9MbxpFTSMBoo vHzXX/D8Or2/4BkltzB8BK7p9iWbOJfjZQ+tfnTfSyx//olBQGt+j6T2dw3JVcpB/LlT AYLl1Txg6tb+ekW9jNLqmzXdZDKJFqqc5D3n1d1aQJWYAENjNnXPhIvCS/K9qXqam86d DbSQ== 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=9F7Zwy+tF+g79zFIcf+LAkwXxzpFiwec9imyLWjebwM=; b=dlYrj/zUlW5h5tWy/T4wUnwfo8magw5zUMb7J+TpB/JGNMZBRf5g+8VmjfpoDhRa2q OTs2xM92ZnCZ4tE1bY5rQH6joSEuOQM2NzuO53vIT74WcSjAMtz94vRKQBfeHGKQZ6+H eujpqmWLh7XqMlxxxph2NT2SthAO4m4ZZHVaExW/20QqgnSRbglTdyUAPd85hJw8qtZR rpNnQfXudSJIMli4nu9G7q9F8xctmp5mOEGNxRhAyib8/ZGZZK2SLTznslHQ0+YVgoMf T9zd7jFtXiLc+osYqzYo5JYYsz8EiYJarsd4PqGPa6BxXsu5AYDLvIRF/s2FlEe85J2/ eK5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=gOXwjUK4; 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 u3-20020a656703000000b004f22f5297b4si6182006pgf.742.2023.03.13.03.06.57; Mon, 13 Mar 2023 03:07:12 -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=gOXwjUK4; 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 S231179AbjCMJtw (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Mon, 13 Mar 2023 05:49:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231338AbjCMJtt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 13 Mar 2023 05:49:49 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.198]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1F417132E2; Mon, 13 Mar 2023 02:49:36 -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=9F7Zw y+tF+g79zFIcf+LAkwXxzpFiwec9imyLWjebwM=; b=gOXwjUK4irrRBdg/qOsq7 i3I9fQHuTm3v08vNiRMqwGes/PF9Yzw56TuZu7g9AP+qjNp97Q4aIu2vLRhGAw+L yTSz2UqLKph6sM+lbuRs9L62A4rBdsDTgrJaze3EY7Sxs4OQgvK2F/NN7iuGyJD8 t7fGRb4uiqUEKMYydFJUYA= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g2-1 (Coremail) with SMTP id _____wAXpD2F8Q5kCkUlAA--.7153S2; Mon, 13 Mar 2023 17:48:53 +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: Mon, 13 Mar 2023 17:48:52 +0800 Message-Id: <20230313094852.3344288-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wAXpD2F8Q5kCkUlAA--.7153S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7tF1DGryfJr47ArWxZFyDAwb_yoW8Xw1xpF WDGFW5Ar4rGFWjq3y7GFykZF1rCasrKry7ZrW7Kw48uF1rG3y0qryIqa1jkF1xJFZ3Ar4F q3WDW340qa47u37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zi-eOJUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/xtbBzgYxU2I0Xnj2zgAAsi 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_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?1760246661788103036?= X-GMAIL-MSGID: =?utf-8?q?1760246661788103036?= |
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, 9:48 a.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")
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: Monday, March 13, 2023 6:49 PM > > 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") > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> Thank you for the patch! Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.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月13日周一 20:56写道: > > Hi Zheng, > > > From: Zheng Wang, Sent: Monday, March 13, 2023 6:49 PM > > > > 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") > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > Thank you for the patch! > > Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com> > Thanks for the detailed review :) Best regards, Zheng > > > --- > > 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 >
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);