[RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition
Message ID | 20230318081743.797531-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 j10csp194235wrt; Sat, 18 Mar 2023 01:53:11 -0700 (PDT) X-Google-Smtp-Source: AK7set+B8QQeKJ+Q3KAfDrI4B7w4JVlMgdumEAXa/fyxGUaZ5aV28kVrceEJsFnimWZABy4vT+l9 X-Received: by 2002:a17:902:e74b:b0:19f:3e7a:4290 with SMTP id p11-20020a170902e74b00b0019f3e7a4290mr11195932plf.17.1679129590865; Sat, 18 Mar 2023 01:53:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679129590; cv=none; d=google.com; s=arc-20160816; b=Rn1L4rq7nOiHI6K6/K98oTq0nIowoz3XpHRUyB+IPMoW9A1ClNZjuMaTmM+3lTkBG7 AH4JAhksGFd8QaWYbna8Irtw7wcVYaLD7H2iogozws+zLOLy2csWWq3f+Qy25nAOSp6i iK8CYj17ntjGlnx8l+/Jjh+TcJnHE0yM7wYWWSKZ377qzJlCV7Dp8KXx82A6pVFiJcgC 8FKmOPij7umTHMDrRpY46oMJcuC5wFCbXgpllhzGUcANOi61UkL6zUU95OkaaU5at7EG 5/3w7DyuBt5pW2CFSmVTpbfFh2UUeI2cwIMy+sjCFE9bOZ1FKEA1K3HNQrLtgjaaR6Q3 7w3A== 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=R6PigHiiBwTtJAt6wF4POpu3Up9Ey4iqAzf3m+gNp9g=; b=PmpKxgbxVo+z5wJ5acuoWAWe3BKqV5tPR+gA7Vb3z4QJs31k92AriiAfSo0w7Yus9N 7sGF7rN4zPB/VaidN5j41Cn/PcuNgrkgMELLTGkcGbRlrM0W7B6NLPD6QPNoJgHRBPdv u6kBNlhNkWuuOQoju6iE6RXGPhVOFL1HiIsqJVNy32DKyqhqKpYj+nn2lgfxuyqeGIGZ NIGq51ReZGn6oSoIPWvfcOjhLJC5I/iBjYxwf3OdPeHwAkxdD39Ed6QYC+Nni3DXiN1v tVa0OOSHS3nKjDpZ99ln34kAuacfO3kJ2II2jGEFJcg08j2TaoW7mG1I8pFyiGAZyeWz 0Z7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=OmQBc2rB; 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 i5-20020a17090332c500b001a05f8ed0cbsi5522375plr.589.2023.03.18.01.52.54; Sat, 18 Mar 2023 01:53:10 -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=OmQBc2rB; 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 S229794AbjCRISe (ORCPT <rfc822;pusanteemu@gmail.com> + 99 others); Sat, 18 Mar 2023 04:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229599AbjCRISb (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 18 Mar 2023 04:18:31 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.196]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C526D5B9C for <linux-kernel@vger.kernel.org>; Sat, 18 Mar 2023 01:18:27 -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=R6Pig HiiBwTtJAt6wF4POpu3Up9Ey4iqAzf3m+gNp9g=; b=OmQBc2rBQ4u16V53iQvd0 uTwEN783dw5cQBi5k0Wh2BiQdEcAKJpCKNHOKQs0F0F8PeYU3vkCP+9nb1d4ESLp HXs+N2dlc5YUd2NF0l5omlPx+x4DR1kCo+24RITJqMjmuI+tEyOrTzf+zI+MYJFy VG5sr78CAZrcVcyIWXCQlc= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g4-3 (Coremail) with SMTP id _____wCnrUascxVkDUZnAQ--.44039S2; Sat, 18 Mar 2023 16:17:48 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: arnd@arndb.de Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition Date: Sat, 18 Mar 2023 16:17:43 +0800 Message-Id: <20230318081743.797531-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wCnrUascxVkDUZnAQ--.44039S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Ar1kXFykXrWxKF4fJF1DWrg_yoW8Wr1Dpa yjgrW29rW8KFWIqw4DJr4Uua42kw47t34YgF47Cw43Z34YkrWYgFnFvryIvFZayFZ5tr4F yr18ZrZxWa9rZ3JanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziBT5ZUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXBA2U1Xl5+NHrgAAs0 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?1760694990192252824?= X-GMAIL-MSGID: =?utf-8?q?1760694990192252824?= |
Series |
[RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition
|
|
Commit Message
Zheng Wang
March 18, 2023, 8:17 a.m. UTC
In kim_probe, it called st_core_init and bound &st_gdata->work_write_wakeup
with work_fn_write_wakeup.
When it calls st_tty_wakeup, it will finally call schedule_work to start
the work.
When we call kim_remove to remove the driver, there
may be a sequence as follows:
Fix it by finishing the work before cleanup in kim_remove
CPU0 CPU1
|work_fn_write_wakeup
kim_remove |
st_core_exit |
kfree(st_gdata)|
|st_tx_wakeup
|//use st_gdata
Fixes: b05b7c7cc032 ("ti-st: use worker instead of calling st_int_write in wake up")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
drivers/misc/ti-st/st_kim.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Comments
Hi Zheng, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on char-misc/char-misc-testing] [also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus soc/for-next linus/master v6.3-rc2 next-20230317] [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/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 patch link: https://lore.kernel.org/r/20230318081743.797531-1-zyytlz.wz%40163.com patch subject: [PATCH RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition config: i386-randconfig-a005 (https://download.01.org/0day-ci/archive/20230318/202303181753.kJU7eetL-lkp@intel.com/config) compiler: gcc-11 (Debian 11.3.0-8) 11.3.0 reproduce (this is a W=1 build): # https://github.com/intel-lab-lkp/linux/commit/ecbe190b970112b4b10e2a1766da933168ae5fdd git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Zheng-Wang/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 git checkout ecbe190b970112b4b10e2a1766da933168ae5fdd # save the config file mkdir build_dir && cp config build_dir/.config make W=1 O=build_dir ARCH=i386 olddefconfig make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/misc/ti-st/ 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/202303181753.kJU7eetL-lkp@intel.com/ All warnings (new ones prefixed by >>): drivers/misc/ti-st/st_kim.c: In function 'kim_remove': >> drivers/misc/ti-st/st_kim.c:788:27: warning: 'kim_gdata' is used uninitialized [-Wuninitialized] 788 | struct st_data_s *st_gdata = kim_gdata->core_data; | ^~~~~~~~ vim +/kim_gdata +788 drivers/misc/ti-st/st_kim.c 782 783 static int kim_remove(struct platform_device *pdev) 784 { 785 /* free the GPIOs requested */ 786 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 787 struct kim_data_s *kim_gdata; > 788 struct st_data_s *st_gdata = kim_gdata->core_data; 789 790 kim_gdata = platform_get_drvdata(pdev); 791 792 cancel_work_sync(&st_gdata->work_write_wakeup); 793 794 /* 795 * Free the Bluetooth/FM/GPIO 796 * nShutdown gpio from the system 797 */ 798 gpio_free(pdata->nshutdown_gpio); 799 pr_info("nshutdown GPIO Freed"); 800 801 debugfs_remove_recursive(kim_debugfs_dir); 802 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); 803 pr_info("sysfs entries removed"); 804 805 kim_gdata->kim_pdev = NULL; 806 st_core_exit(st_gdata); 807 808 kfree(kim_gdata); 809 kim_gdata = NULL; 810 return 0; 811 } 812
Hi Zheng, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on char-misc/char-misc-testing] [also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus soc/for-next linus/master v6.3-rc2 next-20230317] [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/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 patch link: https://lore.kernel.org/r/20230318081743.797531-1-zyytlz.wz%40163.com patch subject: [PATCH RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition config: i386-randconfig-a004 (https://download.01.org/0day-ci/archive/20230318/202303181724.0bgWKpe9-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/ecbe190b970112b4b10e2a1766da933168ae5fdd git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Zheng-Wang/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 git checkout ecbe190b970112b4b10e2a1766da933168ae5fdd # 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/misc/ti-st/ 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/202303181724.0bgWKpe9-lkp@intel.com/ All warnings (new ones prefixed by >>): >> drivers/misc/ti-st/st_kim.c:788:31: warning: variable 'kim_gdata' is uninitialized when used here [-Wuninitialized] struct st_data_s *st_gdata = kim_gdata->core_data; ^~~~~~~~~ drivers/misc/ti-st/st_kim.c:787:30: note: initialize the variable 'kim_gdata' to silence this warning struct kim_data_s *kim_gdata; ^ = NULL 1 warning generated. vim +/kim_gdata +788 drivers/misc/ti-st/st_kim.c 782 783 static int kim_remove(struct platform_device *pdev) 784 { 785 /* free the GPIOs requested */ 786 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 787 struct kim_data_s *kim_gdata; > 788 struct st_data_s *st_gdata = kim_gdata->core_data; 789 790 kim_gdata = platform_get_drvdata(pdev); 791 792 cancel_work_sync(&st_gdata->work_write_wakeup); 793 794 /* 795 * Free the Bluetooth/FM/GPIO 796 * nShutdown gpio from the system 797 */ 798 gpio_free(pdata->nshutdown_gpio); 799 pr_info("nshutdown GPIO Freed"); 800 801 debugfs_remove_recursive(kim_debugfs_dir); 802 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); 803 pr_info("sysfs entries removed"); 804 805 kim_gdata->kim_pdev = NULL; 806 st_core_exit(st_gdata); 807 808 kfree(kim_gdata); 809 kim_gdata = NULL; 810 return 0; 811 } 812
Hi Zheng, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on char-misc/char-misc-testing] [also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus soc/for-next v6.3-rc2 next-20230317] [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/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 patch link: https://lore.kernel.org/r/20230318081743.797531-1-zyytlz.wz%40163.com patch subject: [PATCH RESEND] misc: ti-st: st_kim: Fix use after free bug in kim_remove due to race condition config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20230318/202303181850.RI6iAAhO-lkp@intel.com/config) compiler: m68k-linux-gcc (GCC) 12.1.0 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/ecbe190b970112b4b10e2a1766da933168ae5fdd git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Zheng-Wang/misc-ti-st-st_kim-Fix-use-after-free-bug-in-kim_remove-due-to-race-condition/20230318-161853 git checkout ecbe190b970112b4b10e2a1766da933168ae5fdd # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k SHELL=/bin/bash 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/202303181850.RI6iAAhO-lkp@intel.com/ All warnings (new ones prefixed by >>): drivers/misc/ti-st/st_kim.c: In function 'kim_remove': >> drivers/misc/ti-st/st_kim.c:788:27: warning: 'kim_gdata' is used uninitialized [-Wuninitialized] 788 | struct st_data_s *st_gdata = kim_gdata->core_data; | ^~~~~~~~ drivers/misc/ti-st/st_kim.c:787:34: note: 'kim_gdata' was declared here 787 | struct kim_data_s *kim_gdata; | ^~~~~~~~~ vim +/kim_gdata +788 drivers/misc/ti-st/st_kim.c 782 783 static int kim_remove(struct platform_device *pdev) 784 { 785 /* free the GPIOs requested */ 786 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 787 struct kim_data_s *kim_gdata; > 788 struct st_data_s *st_gdata = kim_gdata->core_data; 789 790 kim_gdata = platform_get_drvdata(pdev); 791 792 cancel_work_sync(&st_gdata->work_write_wakeup); 793 794 /* 795 * Free the Bluetooth/FM/GPIO 796 * nShutdown gpio from the system 797 */ 798 gpio_free(pdata->nshutdown_gpio); 799 pr_info("nshutdown GPIO Freed"); 800 801 debugfs_remove_recursive(kim_debugfs_dir); 802 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); 803 pr_info("sysfs entries removed"); 804 805 kim_gdata->kim_pdev = NULL; 806 st_core_exit(st_gdata); 807 808 kfree(kim_gdata); 809 kim_gdata = NULL; 810 return 0; 811 } 812
diff --git a/drivers/misc/ti-st/st_kim.c b/drivers/misc/ti-st/st_kim.c index f2f6cab97c08..160258a78c7b 100644 --- a/drivers/misc/ti-st/st_kim.c +++ b/drivers/misc/ti-st/st_kim.c @@ -785,9 +785,12 @@ static int kim_remove(struct platform_device *pdev) /* free the GPIOs requested */ struct ti_st_plat_data *pdata = pdev->dev.platform_data; struct kim_data_s *kim_gdata; + struct st_data_s *st_gdata = kim_gdata->core_data; kim_gdata = platform_get_drvdata(pdev); + cancel_work_sync(&st_gdata->work_write_wakeup); + /* * Free the Bluetooth/FM/GPIO * nShutdown gpio from the system @@ -800,7 +803,7 @@ static int kim_remove(struct platform_device *pdev) pr_info("sysfs entries removed"); kim_gdata->kim_pdev = NULL; - st_core_exit(kim_gdata->core_data); + st_core_exit(st_gdata); kfree(kim_gdata); kim_gdata = NULL;