misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open
Message ID | 20230417165246.467723-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 b10csp2275567vqo; Mon, 17 Apr 2023 10:09:57 -0700 (PDT) X-Google-Smtp-Source: AKy350aoRrmw0a1YWnq6vc2IiNu+P7j6B7EI/3awOtoAmABroG8k9SjXUlZRM2en/6ayVvDH3rxM X-Received: by 2002:a05:6a00:2450:b0:626:cc72:51ac with SMTP id d16-20020a056a00245000b00626cc7251acmr22207412pfj.30.1681751397252; Mon, 17 Apr 2023 10:09:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681751397; cv=none; d=google.com; s=arc-20160816; b=BXZrgr2T2kZorUECpStU31EVGNN8UF6CyShFSP8xPULBwW98Z7kXFdXwXP9bAMmvyy YicRTmC1ucVDMMdEnhNr7GPerL6xq/d8tJFhIJ1ukGVPoejqlb8geSp1nzjPzUwrZl09 MD+glzK2zwC2QTRMiSnvaezilCQtecRzb9b3M9hkqgRCPRwZhUJWYzy7H2ZTfKFyOHAu FbTaxKaMAwR2XI6XE7XGMOXn01mzr5EgnkvKPsxzumDj+wmkYHwCv3TmhSA3LkXAaGrC HwkdSRbPjDD822UdSvDVDYF1wM3TDq6fFfZaYDJkdYi/E4tFanBUZvGgDu0eZCib3kSX u6XA== 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=difeqNYROJLd3h7pJgHbiQGat6ELIM7KbJhDgj4H5js=; b=NUPNMVo9xEIU8PgAkhENl/hQ15LpTgEHUcXLzNua6vTSbB+HJc0AecTydbU9jMC3KS pSbNdcE8SoxIXz64DyCkRJtyyYKTix245487vkWtg2WTvkIx/DVESqmFJUpgzkqAqhLT h21vYw4PmdSqHVB/G4MRKhdZahcJV4QFlefTNG49RTwks4MegBd8pFGvIybOFLm8DYpu 1inGHKhRAzakoQQJsKiO9H9Gk7dZuNLjVtXyUgvmruVUmBNEkyZGMTstHMt656Sbo3vc 2e1OTU+/YNX2xaWoPFDFog8iXuQQx+dW412nDea9PXQ24AIopY144LijHpRxDk4EjJfR iygA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=EKu60oX0; 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 x11-20020aa79a4b000000b0063b8dcfdcdasi3742145pfj.37.2023.04.17.10.09.43; Mon, 17 Apr 2023 10:09:57 -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=EKu60oX0; 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 S229824AbjDQQxP (ORCPT <rfc822;leviz.kernel.dev@gmail.com> + 99 others); Mon, 17 Apr 2023 12:53:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjDQQxO (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 17 Apr 2023 12:53:14 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F3AE78F for <linux-kernel@vger.kernel.org>; Mon, 17 Apr 2023 09:53:12 -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=difeq NYROJLd3h7pJgHbiQGat6ELIM7KbJhDgj4H5js=; b=EKu60oX0w16R8PltMmtIK YbgiRhHMdG4GiNpUM7mRAczlD4wCqyYmHmZIXqLOWM5uA7KHL4B3DhyVBYCdAoZ1 C4Mw5/aaysBCenWZS+JM+vH2uySewzwG75X4YY34rwpPtD2NbXZsTwFBo6vaTriU X3Fxhx+h4aFERFCkZv37Lg= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g3-3 (Coremail) with SMTP id _____wB33mZgeT1kpY0GBw--.48629S2; Tue, 18 Apr 2023 00:52:48 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: matt.hsiao@hpe.com Cc: arnd@arndb.de, 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] misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open Date: Tue, 18 Apr 2023 00:52:46 +0800 Message-Id: <20230417165246.467723-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wB33mZgeT1kpY0GBw--.48629S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxGryUJFyDuFWDKFyxZr4kXrb_yoW5Ww1xpF W5Xa45CF4rXrZrWF4Utr4DCFyay34xtrykGrWIk3s5ZF1Svr1vgF18ta4UZFy5tFZ5XF13 JF1YgryrG3WUJaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziaiiDUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXAFUU1Xl6SOPWAAAs3 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,T_SCC_BODY_TEXT_LINE 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?1763444153005529348?= X-GMAIL-MSGID: =?utf-8?q?1763444153005529348?= |
Series |
misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open
|
|
Commit Message
Zheng Wang
April 17, 2023, 4:52 p.m. UTC
A race condition may occur if the user physically removes the ilo device
while calling ilo_open.
If we remove the driver which will call ilo_remove to make cleanup, there
is no guarantee to make sure there's no more ilo_open invoking.
The possible sequence is as follows:
Fix it by adding a refcount check to ilo_remove() function to free the
"ilo_hwinfo " structure after the last file is closed.
CPU0 CPU1
|ilo_open
ilo_remove |
kfree(ilo_hw) |
//free |
|hw = container_of(ip->i_cdev,
|struct ilo_hwinfo, cdev);
|hw->ccb_alloc[slot]
|//use
Fixes: 89bcb05d9bbf ("HP iLO driver")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
drivers/misc/hpilo.c | 16 +++++++++++++++-
drivers/misc/hpilo.h | 1 +
2 files changed, 16 insertions(+), 1 deletion(-)
Comments
On Tue, Apr 18, 2023 at 12:52:46AM +0800, Zheng Wang wrote: > --- a/drivers/misc/hpilo.h > +++ b/drivers/misc/hpilo.h > @@ -62,6 +62,7 @@ struct ilo_hwinfo { > spinlock_t fifo_lock; > > struct cdev cdev; > + struct kref refcnt; You now have 2 different structures doing reference counting logic on the same structure. That's just not ok at all, and is totally broken. greg k-h
Greg KH <gregkh@linuxfoundation.org> 于2023年4月18日周二 01:43写道: > > On Tue, Apr 18, 2023 at 12:52:46AM +0800, Zheng Wang wrote: > > --- a/drivers/misc/hpilo.h > > +++ b/drivers/misc/hpilo.h > > @@ -62,6 +62,7 @@ struct ilo_hwinfo { > > spinlock_t fifo_lock; > > > > struct cdev cdev; > > + struct kref refcnt; > > You now have 2 different structures doing reference counting logic on > the same structure. That's just not ok at all, and is totally broken. > Thanks for your reply. I'll look into it and figure out effective fix. Best regards, Zheng > greg k-h
Hi Zheng, kernel test robot noticed the following build warnings: https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/misc-hpilo-Fix-use-after-free-bug-in-ilo_remove-due-to-race-condition-with-ilo_open/20230418-005502 base: char-misc/char-misc-testing patch link: https://lore.kernel.org/r/20230417165246.467723-1-zyytlz.wz%40163.com patch subject: [PATCH] misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230430/202304300909.ALHN6Vry-lkp@intel.com/config) compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Reported-by: Dan Carpenter <error27@gmail.com> | Link: https://lore.kernel.org/r/202304300909.ALHN6Vry-lkp@intel.com/ smatch warnings: drivers/misc/hpilo.c:755 ilo_delete() warn: can 'ilo_hw' even be NULL? vim +/ilo_hw +755 drivers/misc/hpilo.c 13a22f45e6cb1e Zheng Wang 2023-04-18 749 static void ilo_delete(struct kref *kref) 13a22f45e6cb1e Zheng Wang 2023-04-18 750 { 13a22f45e6cb1e Zheng Wang 2023-04-18 751 int i, minor; 13a22f45e6cb1e Zheng Wang 2023-04-18 752 struct ilo_hwinfo *ilo_hw = container_of(kref, struct ilo_hwinfo, refcnt); 13a22f45e6cb1e Zheng Wang 2023-04-18 753 struct pci_dev *pdev = ilo_hw->ilo_dev; 89bcb05d9bbf8b David Altobelli 2008-07-02 754 ebf1b764aa5cb3 Mark Rusk 2012-11-06 @755 if (!ilo_hw) ^^^^^^^ ->refcnt member isn't the first struct member so this NULL check doesn't make sense. Generally checking container_of() is ugly. Some people do it and add BUILD_BUG_ON() to ensure that they're checking the first struct member but really it's best to avoid that if possible. ebf1b764aa5cb3 Mark Rusk 2012-11-06 756 return; ebf1b764aa5cb3 Mark Rusk 2012-11-06 757 89bcb05d9bbf8b David Altobelli 2008-07-02 758 clear_device(ilo_hw); 89bcb05d9bbf8b David Altobelli 2008-07-02 759 89bcb05d9bbf8b David Altobelli 2008-07-02 760 minor = MINOR(ilo_hw->cdev.dev); 98dcd59dd063dd Camuso, Tony 2012-06-10 761 for (i = minor; i < minor + max_ccb; i++) 89bcb05d9bbf8b David Altobelli 2008-07-02 762 device_destroy(ilo_class, MKDEV(ilo_major, i)); 89bcb05d9bbf8b David Altobelli 2008-07-02 763 89bcb05d9bbf8b David Altobelli 2008-07-02 764 cdev_del(&ilo_hw->cdev); 9f7048412163d8 David Altobelli 2009-08-17 765 ilo_disable_interrupts(ilo_hw); 9f7048412163d8 David Altobelli 2009-08-17 766 free_irq(pdev->irq, ilo_hw); 89bcb05d9bbf8b David Altobelli 2008-07-02 767 ilo_unmap_device(pdev, ilo_hw); 89bcb05d9bbf8b David Altobelli 2008-07-02 768 pci_release_regions(pdev); bcdee04ea7ae04 Jiri Slaby 2012-09-13 769 /* bcdee04ea7ae04 Jiri Slaby 2012-09-13 770 * pci_disable_device(pdev) used to be here. But this PCI device has bcdee04ea7ae04 Jiri Slaby 2012-09-13 771 * two functions with interrupt lines connected to a single pin. The bcdee04ea7ae04 Jiri Slaby 2012-09-13 772 * other one is a USB host controller. So when we disable the PIN here bcdee04ea7ae04 Jiri Slaby 2012-09-13 773 * e.g. by rmmod hpilo, the controller stops working. It is because bcdee04ea7ae04 Jiri Slaby 2012-09-13 774 * the interrupt link is disabled in ACPI since it is not refcounted bcdee04ea7ae04 Jiri Slaby 2012-09-13 775 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable. bcdee04ea7ae04 Jiri Slaby 2012-09-13 776 */ 89bcb05d9bbf8b David Altobelli 2008-07-02 777 kfree(ilo_hw); 98dcd59dd063dd Camuso, Tony 2012-06-10 778 ilo_hwdev[(minor / max_ccb)] = 0; 89bcb05d9bbf8b David Altobelli 2008-07-02 779 }
Dear Dan, Thanks for your reply. I'm not so familiar with the code here. Is the warning says we should check ilo_hw to see if it's not NULL? Best regards, Zheng Dan Carpenter <dan.carpenter@linaro.org> 于2023年5月2日周二 19:46写道: > > Hi Zheng, > > kernel test robot noticed the following build warnings: > > https://git-scm.com/docs/git-format-patch#_base_tree_information] > > url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/misc-hpilo-Fix-use-after-free-bug-in-ilo_remove-due-to-race-condition-with-ilo_open/20230418-005502 > base: char-misc/char-misc-testing > patch link: https://lore.kernel.org/r/20230417165246.467723-1-zyytlz.wz%40163.com > patch subject: [PATCH] misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open > config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230430/202304300909.ALHN6Vry-lkp@intel.com/config) > compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 > > If you fix the issue, kindly add following tag where applicable > | Reported-by: kernel test robot <lkp@intel.com> > | Reported-by: Dan Carpenter <error27@gmail.com> > | Link: https://lore.kernel.org/r/202304300909.ALHN6Vry-lkp@intel.com/ > > smatch warnings: > drivers/misc/hpilo.c:755 ilo_delete() warn: can 'ilo_hw' even be NULL? > > vim +/ilo_hw +755 drivers/misc/hpilo.c > > 13a22f45e6cb1e Zheng Wang 2023-04-18 749 static void ilo_delete(struct kref *kref) > 13a22f45e6cb1e Zheng Wang 2023-04-18 750 { > 13a22f45e6cb1e Zheng Wang 2023-04-18 751 int i, minor; > 13a22f45e6cb1e Zheng Wang 2023-04-18 752 struct ilo_hwinfo *ilo_hw = container_of(kref, struct ilo_hwinfo, refcnt); > 13a22f45e6cb1e Zheng Wang 2023-04-18 753 struct pci_dev *pdev = ilo_hw->ilo_dev; > 89bcb05d9bbf8b David Altobelli 2008-07-02 754 > ebf1b764aa5cb3 Mark Rusk 2012-11-06 @755 if (!ilo_hw) > ^^^^^^^ > > ->refcnt member isn't the first struct member so this NULL check > doesn't make sense. Generally checking container_of() is ugly. Some > people do it and add BUILD_BUG_ON() to ensure that they're checking the > first struct member but really it's best to avoid that if possible. > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 756 return; > ebf1b764aa5cb3 Mark Rusk 2012-11-06 757 > 89bcb05d9bbf8b David Altobelli 2008-07-02 758 clear_device(ilo_hw); > 89bcb05d9bbf8b David Altobelli 2008-07-02 759 > 89bcb05d9bbf8b David Altobelli 2008-07-02 760 minor = MINOR(ilo_hw->cdev.dev); > 98dcd59dd063dd Camuso, Tony 2012-06-10 761 for (i = minor; i < minor + max_ccb; i++) > 89bcb05d9bbf8b David Altobelli 2008-07-02 762 device_destroy(ilo_class, MKDEV(ilo_major, i)); > 89bcb05d9bbf8b David Altobelli 2008-07-02 763 > 89bcb05d9bbf8b David Altobelli 2008-07-02 764 cdev_del(&ilo_hw->cdev); > 9f7048412163d8 David Altobelli 2009-08-17 765 ilo_disable_interrupts(ilo_hw); > 9f7048412163d8 David Altobelli 2009-08-17 766 free_irq(pdev->irq, ilo_hw); > 89bcb05d9bbf8b David Altobelli 2008-07-02 767 ilo_unmap_device(pdev, ilo_hw); > 89bcb05d9bbf8b David Altobelli 2008-07-02 768 pci_release_regions(pdev); > bcdee04ea7ae04 Jiri Slaby 2012-09-13 769 /* > bcdee04ea7ae04 Jiri Slaby 2012-09-13 770 * pci_disable_device(pdev) used to be here. But this PCI device has > bcdee04ea7ae04 Jiri Slaby 2012-09-13 771 * two functions with interrupt lines connected to a single pin. The > bcdee04ea7ae04 Jiri Slaby 2012-09-13 772 * other one is a USB host controller. So when we disable the PIN here > bcdee04ea7ae04 Jiri Slaby 2012-09-13 773 * e.g. by rmmod hpilo, the controller stops working. It is because > bcdee04ea7ae04 Jiri Slaby 2012-09-13 774 * the interrupt link is disabled in ACPI since it is not refcounted > bcdee04ea7ae04 Jiri Slaby 2012-09-13 775 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable. > bcdee04ea7ae04 Jiri Slaby 2012-09-13 776 */ > 89bcb05d9bbf8b David Altobelli 2008-07-02 777 kfree(ilo_hw); > 98dcd59dd063dd Camuso, Tony 2012-06-10 778 ilo_hwdev[(minor / max_ccb)] = 0; > 89bcb05d9bbf8b David Altobelli 2008-07-02 779 } > > -- > 0-DAY CI Kernel Test Service > https://github.com/intel/lkp-tests >
On Fri, May 05, 2023 at 12:01:17PM +0800, Zheng Hacker wrote: > Dear Dan, > > Thanks for your reply. I'm not so familiar with the code here. Is the > warning says we should check ilo_hw to see if it's not NULL? > Just delete the NULL check. It's impossible. regards, dan carpenter > Best regards, > Zheng > > Dan Carpenter <dan.carpenter@linaro.org> 于2023年5月2日周二 19:46写道: > > > > Hi Zheng, > > > > kernel test robot noticed the following build warnings: > > > > https://git-scm.com/docs/git-format-patch#_base_tree_information] > > > > url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/misc-hpilo-Fix-use-after-free-bug-in-ilo_remove-due-to-race-condition-with-ilo_open/20230418-005502 > > base: char-misc/char-misc-testing > > patch link: https://lore.kernel.org/r/20230417165246.467723-1-zyytlz.wz%40163.com > > patch subject: [PATCH] misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open > > config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230430/202304300909.ALHN6Vry-lkp@intel.com/config) > > compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 > > > > If you fix the issue, kindly add following tag where applicable > > | Reported-by: kernel test robot <lkp@intel.com> > > | Reported-by: Dan Carpenter <error27@gmail.com> > > | Link: https://lore.kernel.org/r/202304300909.ALHN6Vry-lkp@intel.com/ > > > > smatch warnings: > > drivers/misc/hpilo.c:755 ilo_delete() warn: can 'ilo_hw' even be NULL? > > > > vim +/ilo_hw +755 drivers/misc/hpilo.c > > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 749 static void ilo_delete(struct kref *kref) > > 13a22f45e6cb1e Zheng Wang 2023-04-18 750 { > > 13a22f45e6cb1e Zheng Wang 2023-04-18 751 int i, minor; > > 13a22f45e6cb1e Zheng Wang 2023-04-18 752 struct ilo_hwinfo *ilo_hw = container_of(kref, struct ilo_hwinfo, refcnt); > > 13a22f45e6cb1e Zheng Wang 2023-04-18 753 struct pci_dev *pdev = ilo_hw->ilo_dev; > > 89bcb05d9bbf8b David Altobelli 2008-07-02 754 > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 @755 if (!ilo_hw) > > ^^^^^^^ > > > > ->refcnt member isn't the first struct member so this NULL check > > doesn't make sense. Generally checking container_of() is ugly. Some > > people do it and add BUILD_BUG_ON() to ensure that they're checking the > > first struct member but really it's best to avoid that if possible. > > > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 756 return; > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 757 > > 89bcb05d9bbf8b David Altobelli 2008-07-02 758 clear_device(ilo_hw); > > 89bcb05d9bbf8b David Altobelli 2008-07-02 759 > > 89bcb05d9bbf8b David Altobelli 2008-07-02 760 minor = MINOR(ilo_hw->cdev.dev); > > 98dcd59dd063dd Camuso, Tony 2012-06-10 761 for (i = minor; i < minor + max_ccb; i++) > > 89bcb05d9bbf8b David Altobelli 2008-07-02 762 device_destroy(ilo_class, MKDEV(ilo_major, i)); > > 89bcb05d9bbf8b David Altobelli 2008-07-02 763 > > 89bcb05d9bbf8b David Altobelli 2008-07-02 764 cdev_del(&ilo_hw->cdev); > > 9f7048412163d8 David Altobelli 2009-08-17 765 ilo_disable_interrupts(ilo_hw); > > 9f7048412163d8 David Altobelli 2009-08-17 766 free_irq(pdev->irq, ilo_hw); > > 89bcb05d9bbf8b David Altobelli 2008-07-02 767 ilo_unmap_device(pdev, ilo_hw); > > 89bcb05d9bbf8b David Altobelli 2008-07-02 768 pci_release_regions(pdev); > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 769 /* > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 770 * pci_disable_device(pdev) used to be here. But this PCI device has > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 771 * two functions with interrupt lines connected to a single pin. The > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 772 * other one is a USB host controller. So when we disable the PIN here > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 773 * e.g. by rmmod hpilo, the controller stops working. It is because > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 774 * the interrupt link is disabled in ACPI since it is not refcounted > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 775 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable. > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 776 */ > > 89bcb05d9bbf8b David Altobelli 2008-07-02 777 kfree(ilo_hw); > > 98dcd59dd063dd Camuso, Tony 2012-06-10 778 ilo_hwdev[(minor / max_ccb)] = 0; > > 89bcb05d9bbf8b David Altobelli 2008-07-02 779 } > > > > -- > > 0-DAY CI Kernel Test Service > > https://github.com/intel/lkp-tests > >
Dan Carpenter <dan.carpenter@linaro.org> 于2023年5月5日周五 15:31写道: > > On Fri, May 05, 2023 at 12:01:17PM +0800, Zheng Hacker wrote: > > Dear Dan, > > > > Thanks for your reply. I'm not so familiar with the code here. Is the > > warning says we should check ilo_hw to see if it's not NULL? > > > > Just delete the NULL check. It's impossible. > Thanks for your explaination. Best regards, Zheng > regards, > dan carpenter > > > Best regards, > > Zheng > > > > Dan Carpenter <dan.carpenter@linaro.org> 于2023年5月2日周二 19:46写道: > > > > > > Hi Zheng, > > > > > > kernel test robot noticed the following build warnings: > > > > > > https://git-scm.com/docs/git-format-patch#_base_tree_information] > > > > > > url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Wang/misc-hpilo-Fix-use-after-free-bug-in-ilo_remove-due-to-race-condition-with-ilo_open/20230418-005502 > > > base: char-misc/char-misc-testing > > > patch link: https://lore.kernel.org/r/20230417165246.467723-1-zyytlz.wz%40163.com > > > patch subject: [PATCH] misc: hpilo: Fix use after free bug in ilo_remove due to race condition with ilo_open > > > config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230430/202304300909.ALHN6Vry-lkp@intel.com/config) > > > compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 > > > > > > If you fix the issue, kindly add following tag where applicable > > > | Reported-by: kernel test robot <lkp@intel.com> > > > | Reported-by: Dan Carpenter <error27@gmail.com> > > > | Link: https://lore.kernel.org/r/202304300909.ALHN6Vry-lkp@intel.com/ > > > > > > smatch warnings: > > > drivers/misc/hpilo.c:755 ilo_delete() warn: can 'ilo_hw' even be NULL? > > > > > > vim +/ilo_hw +755 drivers/misc/hpilo.c > > > > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 749 static void ilo_delete(struct kref *kref) > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 750 { > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 751 int i, minor; > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 752 struct ilo_hwinfo *ilo_hw = container_of(kref, struct ilo_hwinfo, refcnt); > > > 13a22f45e6cb1e Zheng Wang 2023-04-18 753 struct pci_dev *pdev = ilo_hw->ilo_dev; > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 754 > > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 @755 if (!ilo_hw) > > > ^^^^^^^ > > > > > > ->refcnt member isn't the first struct member so this NULL check > > > doesn't make sense. Generally checking container_of() is ugly. Some > > > people do it and add BUILD_BUG_ON() to ensure that they're checking the > > > first struct member but really it's best to avoid that if possible. > > > > > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 756 return; > > > ebf1b764aa5cb3 Mark Rusk 2012-11-06 757 > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 758 clear_device(ilo_hw); > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 759 > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 760 minor = MINOR(ilo_hw->cdev.dev); > > > 98dcd59dd063dd Camuso, Tony 2012-06-10 761 for (i = minor; i < minor + max_ccb; i++) > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 762 device_destroy(ilo_class, MKDEV(ilo_major, i)); > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 763 > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 764 cdev_del(&ilo_hw->cdev); > > > 9f7048412163d8 David Altobelli 2009-08-17 765 ilo_disable_interrupts(ilo_hw); > > > 9f7048412163d8 David Altobelli 2009-08-17 766 free_irq(pdev->irq, ilo_hw); > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 767 ilo_unmap_device(pdev, ilo_hw); > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 768 pci_release_regions(pdev); > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 769 /* > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 770 * pci_disable_device(pdev) used to be here. But this PCI device has > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 771 * two functions with interrupt lines connected to a single pin. The > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 772 * other one is a USB host controller. So when we disable the PIN here > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 773 * e.g. by rmmod hpilo, the controller stops working. It is because > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 774 * the interrupt link is disabled in ACPI since it is not refcounted > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 775 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable. > > > bcdee04ea7ae04 Jiri Slaby 2012-09-13 776 */ > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 777 kfree(ilo_hw); > > > 98dcd59dd063dd Camuso, Tony 2012-06-10 778 ilo_hwdev[(minor / max_ccb)] = 0; > > > 89bcb05d9bbf8b David Altobelli 2008-07-02 779 } > > > > > > -- > > > 0-DAY CI Kernel Test Service > > > https://github.com/intel/lkp-tests > > >
diff --git a/drivers/misc/hpilo.c b/drivers/misc/hpilo.c index 8d00df9243c4..2e9af39e91a4 100644 --- a/drivers/misc/hpilo.c +++ b/drivers/misc/hpilo.c @@ -37,6 +37,8 @@ static const struct pci_device_id ilo_blacklist[] = { {} }; +static void ilo_delete(struct kref *kref); + static inline int get_entry_id(int entry) { return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR; @@ -559,6 +561,7 @@ static int ilo_close(struct inode *ip, struct file *fp) hw->ccb_alloc[slot]->ccb_cnt--; spin_unlock(&hw->open_lock); + kref_put(&hw->refcnt, ilo_delete); return 0; } @@ -578,6 +581,7 @@ static int ilo_open(struct inode *ip, struct file *fp) if (!data) return -ENOMEM; + kref_get(&hw->refcnt); spin_lock(&hw->open_lock); /* each fd private_data holds sw/hw view of ccb */ @@ -633,6 +637,8 @@ static int ilo_open(struct inode *ip, struct file *fp) if (!error) fp->private_data = hw->ccb_alloc[slot]; + else + kref_put(&hw->refcnt, ilo_delete); return error; } @@ -742,8 +748,15 @@ static int ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw) static void ilo_remove(struct pci_dev *pdev) { - int i, minor; struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev); + kref_put(&ilo_hw->refcnt, ilo_delete); +} + +static void ilo_delete(struct kref *kref) +{ + int i, minor; + struct ilo_hwinfo *ilo_hw = container_of(kref, struct ilo_hwinfo, refcnt); + struct pci_dev *pdev = ilo_hw->ilo_dev; if (!ilo_hw) return; @@ -807,6 +820,7 @@ static int ilo_probe(struct pci_dev *pdev, goto out; ilo_hw->ilo_dev = pdev; + kref_init(&ilo_hw->refcnt); spin_lock_init(&ilo_hw->alloc_lock); spin_lock_init(&ilo_hw->fifo_lock); spin_lock_init(&ilo_hw->open_lock); diff --git a/drivers/misc/hpilo.h b/drivers/misc/hpilo.h index d57c34680b09..ebc677eb45ae 100644 --- a/drivers/misc/hpilo.h +++ b/drivers/misc/hpilo.h @@ -62,6 +62,7 @@ struct ilo_hwinfo { spinlock_t fifo_lock; struct cdev cdev; + struct kref refcnt; }; /* offset from mmio_vaddr for enabling doorbell interrupts */