Message ID | 20230316180940.1601515-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:604a:0:0:0:0:0 with SMTP id j10csp629393wrt; Thu, 16 Mar 2023 11:14:55 -0700 (PDT) X-Google-Smtp-Source: AK7set+wa9cXPf4jIlPhMT6eLEBpg+Zr69ubuYY+YWOvqFIkapzDyclj7r/m3VV9SRy7bg8S34FX X-Received: by 2002:a17:90b:1c90:b0:233:ee50:d26d with SMTP id oo16-20020a17090b1c9000b00233ee50d26dmr5111505pjb.4.1678990495299; Thu, 16 Mar 2023 11:14:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678990495; cv=none; d=google.com; s=arc-20160816; b=g+pKFXhrSQDJbeLlRw0by1q23WtTGUetgxigEVFqn9UnehxSdYB13LTPvWJqxavThe UE1sdkDkJxhe/FA+ThFdZCVn1jln+s8T/bJnl6OuPP8i9AU7MsISGsHep+XIjmqBAoID Vu/JoFvnQENCjGT4AxxMyUimgLaTtSeAr6cKs8nlqrZ4bL3w2yBKN64DJQwbWI1uFvc4 PZ1hVtrmYK4OTB0Lt9xN7llGXOx1lMTSIFH8ISR0MIMLLqsM87+AqLfhHnCXpepEtmUX kUGq39PBRTC6JlEdYIQQFq9L8BDcbtEruMjqCWem1tm5/ysL4H2/mboJdu+fh3JG8T4H 8xcQ== 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=A0H0V2O0pfEGsz1NojrmtfRRySfrSnyGWaGppw4kXvg=; b=uILtIobWKFQ1zvE5OUXC+SaPP9VPvqay/ktO38bWwsLb4m3DlGAA117Dk864iul4ds 7DYS6z9VVC2dNiN4g2/xkKTInZ6qXM4DjD8hmYBkJugpxpYiJmWOfaFcYp2mmjDX6zOA z6VYhCAxZtETw8GhMl0ooNNhnQyaMri+6KFKATJ5IZTd+6Equ32zY9BlxRFJfTAizpjw 2M50/BYhCL5GsFBZFP6h+z86IKoSksb47aauChv5+0vJVS6PezNHJcdx2On/hOnnzodi ywiL1lNqb8mj3g6HpNo1ixwKWegCTcOhj6SJXlPT9RztF7TCLWZMiHTf/BQOOMZUHp9h TKgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=BYUYQ0ta; 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 k62-20020a638441000000b0050be758e45fsi4062360pgd.554.2023.03.16.11.14.31; Thu, 16 Mar 2023 11:14:55 -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=BYUYQ0ta; 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 S230450AbjCPSJ4 (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Thu, 16 Mar 2023 14:09:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230460AbjCPSJt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 16 Mar 2023 14:09:49 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.216]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1134469CEC; Thu, 16 Mar 2023 11:09:47 -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=A0H0V 2O0pfEGsz1NojrmtfRRySfrSnyGWaGppw4kXvg=; b=BYUYQ0taeZkEQpbkqguYX WARVIRKN50zK5t5w10IhWbjRiosnjoirUe/ebP0+kZN/3ACR6I1ityzVYLtwQmyW bQMexNW32PQU3+7hkyShp7HxR1PK0Vjc6e3pk7K3rFK4pJOKbwpUwYStPMF0UbJJ bHUdwU7b20vc8Sx8/Wyiwo= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g4-1 (Coremail) with SMTP id _____wC3y0FmWxNk2oMdAQ--.18175S2; Fri, 17 Mar 2023 02:09:42 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: valentina.manea.m@gmail.com Cc: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, skhan@linuxfoundation.org, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH v2] usbip: vudc: Fix use after free bug in vudc_remove due to race condition Date: Fri, 17 Mar 2023 02:09:40 +0800 Message-Id: <20230316180940.1601515-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wC3y0FmWxNk2oMdAQ--.18175S2 X-Coremail-Antispam: 1Uf129KBjvJXoWrtF1UGFy5Jw45ZF18GrWkXrb_yoW8JryUpr ZxWFWxCr4xGFs2qa17Ar90vryrJw43tryY9rWIkayxurW3Ar4UXas0yF1jkFWxAFZaqr43 tF4vvwn5WFykAaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziID7fUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiQgg0U1aEEsHvmQAAsa 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?1760547714473855025?= X-GMAIL-MSGID: =?utf-8?q?1760549137936641254?= |
Series |
[v2] usbip: vudc: Fix use after free bug in vudc_remove due to race condition
|
|
Commit Message
Zheng Wang
March 16, 2023, 6:09 p.m. UTC
In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.
When it calls usbip_sockfd_store, it will call v_start_timer to start the
timer work.
When we call vudc_remove to remove the driver, theremay be a sequence as
follows:
Fix it by shutdown the timer work before cleanup in vudc_remove.
Note that removing a driver is a root-only operation, and should never
happen.
CPU0 CPU1
|v_timer
vudc_remove |
kfree(udc); |
//free shost |
|udc->gadget
|//use
This bug was found by static analysis.
Fixes: b6a0ca111867 ("usbip: vudc: Add UDC specific ops")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
v2:
- add more details about how the bug was found suggested by Shuah
---
drivers/usb/usbip/vudc_dev.c | 1 +
1 file changed, 1 insertion(+)
Comments
On 3/16/23 12:09, Zheng Wang wrote: > In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer. > > When it calls usbip_sockfd_store, it will call v_start_timer to start the > timer work. > > When we call vudc_remove to remove the driver, theremay be a sequence as > follows: > > Fix it by shutdown the timer work before cleanup in vudc_remove. > > Note that removing a driver is a root-only operation, and should never > happen. > > CPU0 CPU1 > > |v_timer > vudc_remove | > kfree(udc); | > //free shost | > |udc->gadget > |//use > > This bug was found by static analysis. Tell me which static analysis tool did you use to find this and the output from the tool. > > Fixes: b6a0ca111867 ("usbip: vudc: Add UDC specific ops") > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > --- > v2: > - add more details about how the bug was found suggested by Shuah > --- > drivers/usb/usbip/vudc_dev.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/drivers/usb/usbip/vudc_dev.c b/drivers/usb/usbip/vudc_dev.c > index 2bc428f2e261..33d0991755bb 100644 > --- a/drivers/usb/usbip/vudc_dev.c > +++ b/drivers/usb/usbip/vudc_dev.c > @@ -633,6 +633,7 @@ int vudc_remove(struct platform_device *pdev) > { > struct vudc *udc = platform_get_drvdata(pdev); > > + timer_shutdown_sync(&udc->timer); > usb_del_gadget_udc(&udc->gadget); > cleanup_vudc_hw(udc); > kfree(udc); thanks, -- Shuah
On Fri, Mar 17, 2023 at 02:27:05AM +0800, Zheng Hacker wrote: > 在 2023年3月17日星期五,Shuah Khan <skhan@linuxfoundation.org> 写道: > > > On 3/16/23 12:09, Zheng Wang wrote: > > > >> In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with > >> v_timer. > >> > >> When it calls usbip_sockfd_store, it will call v_start_timer to start the > >> timer work. > >> > >> When we call vudc_remove to remove the driver, theremay be a sequence as > >> follows: > >> > >> Fix it by shutdown the timer work before cleanup in vudc_remove. > >> > >> Note that removing a driver is a root-only operation, and should never > >> happen. > >> > >> CPU0 CPU1 > >> > >> |v_timer > >> vudc_remove | > >> kfree(udc); | > >> //free shost | > >> |udc->gadget > >> |//use > >> > >> This bug was found by static analysis. > >> > > > > Tell me which static analysis tool did you use to find this and > > the output from the tool. > > > This is found by codeql,the rule is complicated. It finally found > there is no timer stop behavior in remove function. When using tools like this, you are required to follow the rules in Documentation/process/researcher-guidelines.rst Please do so here. thanks, greg k-h
Hi Zheng, Thank you for the patch! Yet something to improve: [auto build test ERROR on usb/usb-testing] [also build test ERROR on usb/usb-next usb/usb-linus linus/master v6.3-rc2 next-20230316] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/usbip-vudc-Fix-use-after-free-bug-in-vudc_remove-due-to-race-condition/20230317-021228 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing patch link: https://lore.kernel.org/r/20230316180940.1601515-1-zyytlz.wz%40163.com patch subject: [PATCH v2] usbip: vudc: Fix use after free bug in vudc_remove due to race condition config: i386-randconfig-a015-20230313 (https://download.01.org/0day-ci/archive/20230317/202303170604.MoMGMPvW-lkp@intel.com/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1) 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/46cc0947344ed93f7f1f4639209c5c6cce16fad3 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Zheng-Wang/usbip-vudc-Fix-use-after-free-bug-in-vudc_remove-due-to-race-condition/20230317-021228 git checkout 46cc0947344ed93f7f1f4639209c5c6cce16fad3 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/usb/usbip/ 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/202303170604.MoMGMPvW-lkp@intel.com/ All errors (new ones prefixed by >>): >> drivers/usb/usbip/vudc_dev.c:636:28: error: no member named 'timer' in 'struct vudc' timer_shutdown_sync(&udc->timer); ~~~ ^ 1 error generated. vim +636 drivers/usb/usbip/vudc_dev.c 631 632 int vudc_remove(struct platform_device *pdev) 633 { 634 struct vudc *udc = platform_get_drvdata(pdev); 635 > 636 timer_shutdown_sync(&udc->timer);
gregkh@linuxfoundation.org <gregkh@linuxfoundation.org> 于2023年3月17日周五 03:09写道: > > On Fri, Mar 17, 2023 at 02:27:05AM +0800, Zheng Hacker wrote: > > 在 2023年3月17日星期五,Shuah Khan <skhan@linuxfoundation.org> 写道: > > > > > On 3/16/23 12:09, Zheng Wang wrote: > > > > > >> In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with > > >> v_timer. > > >> > > >> When it calls usbip_sockfd_store, it will call v_start_timer to start the > > >> timer work. > > >> > > >> When we call vudc_remove to remove the driver, theremay be a sequence as > > >> follows: > > >> > > >> Fix it by shutdown the timer work before cleanup in vudc_remove. > > >> > > >> Note that removing a driver is a root-only operation, and should never > > >> happen. > > >> > > >> CPU0 CPU1 > > >> > > >> |v_timer > > >> vudc_remove | > > >> kfree(udc); | > > >> //free shost | > > >> |udc->gadget > > >> |//use > > >> > > >> This bug was found by static analysis. > > >> > > > > > > Tell me which static analysis tool did you use to find this and > > > the output from the tool. > > > > > > This is found by codeql,the rule is complicated. It finally found > > there is no timer stop behavior in remove function. > > When using tools like this, you are required to follow the rules in > Documentation/process/researcher-guidelines.rst > > Please do so here. > Thanks for your assistance. I'll read it carefully and keep that in mind. And I'll append more information in the next version of patch. Best regards, Zheng > thanks, > > greg k-h
diff --git a/drivers/usb/usbip/vudc_dev.c b/drivers/usb/usbip/vudc_dev.c index 2bc428f2e261..33d0991755bb 100644 --- a/drivers/usb/usbip/vudc_dev.c +++ b/drivers/usb/usbip/vudc_dev.c @@ -633,6 +633,7 @@ int vudc_remove(struct platform_device *pdev) { struct vudc *udc = platform_get_drvdata(pdev); + timer_shutdown_sync(&udc->timer); usb_del_gadget_udc(&udc->gadget); cleanup_vudc_hw(udc); kfree(udc);