lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230323081329.366459-1-baijiaju@buaa.edu.cn>
Date:   Thu, 23 Mar 2023 16:13:29 +0800
From:   Jia-Ju Bai <baijiaju@...a.edu.cn>
To:     mathias.nyman@...el.com, gregkh@...uxfoundation.org,
        p.zabel@...gutronix.de
Cc:     linux-usb@...r.kernel.org, linux-kernel@...r.kernel.org,
        Jia-Ju Bai <baijiaju@...a.edu.cn>
Subject: [PATCH] usb: host: xhci: Add a NULL check of hcd in xhci_plat_remove()

In a previous commit d7de14d74d65 ("usb: xhci_plat_remove: avoid NULL
dereference"), hcd can be NULL in usb_remove_hcd(), and thus it should
be checked before being used.

However, in the call stack of this commit, hci is also used to get xhci:

xhci_plat_remove()
  xhci = hcd_to_xhci(hcd)
    usb_hcd_is_primary_hcd(hcd)
      if (!hcd->primary_hcd) -> No check for hcd
    primary_hcd = hcd->primary_hcd; -> No check for hcd
  usb_remove_hcd(hcd)
    if (!hcd) -> Add a check by the previous commit

Thus, to avoid possible null-pointer dereferences, hci should be checked
before hcd_to_xhci() is called.

These bugs are reported by a static analysis tool implemented by myself,
and they are found by extending a known bug fixed in the previous commit.
Thus, they could be theoretical bugs.

Signed-off-by: Jia-Ju Bai <baijiaju@...a.edu.cn>
---
 drivers/usb/host/xhci-plat.c | 42 +++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index b9f9625467d6..168530b1e1b3 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -389,30 +389,38 @@ static int xhci_generic_plat_probe(struct platform_device *pdev)
 int xhci_plat_remove(struct platform_device *dev)
 {
 	struct usb_hcd	*hcd = platform_get_drvdata(dev);
-	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
-	struct clk *clk = xhci->clk;
-	struct clk *reg_clk = xhci->reg_clk;
-	struct usb_hcd *shared_hcd = xhci->shared_hcd;
+	struct xhci_hcd	*xhci;
+	struct clk *clk;
+	struct clk *reg_clk;
+	struct usb_hcd *shared_hcd;
 
 	pm_runtime_get_sync(&dev->dev);
-	xhci->xhc_state |= XHCI_STATE_REMOVING;
 
-	if (shared_hcd) {
-		usb_remove_hcd(shared_hcd);
-		xhci->shared_hcd = NULL;
-	}
+	if (hcd) {
+		xhci = hcd_to_xhci(hcd);
+		clk = xhci->clk;
+		reg_clk = xhci->reg_clk;
+		shared_hcd = xhci->shared_hcd;
 
-	usb_phy_shutdown(hcd->usb_phy);
+		xhci->xhc_state |= XHCI_STATE_REMOVING;
 
-	usb_remove_hcd(hcd);
+		if (shared_hcd) {
+			usb_remove_hcd(shared_hcd);
+			xhci->shared_hcd = NULL;
+		}
 
-	if (shared_hcd)
-		usb_put_hcd(shared_hcd);
+		usb_phy_shutdown(hcd->usb_phy);
 
-	clk_disable_unprepare(clk);
-	clk_disable_unprepare(reg_clk);
-	reset_control_assert(xhci->reset);
-	usb_put_hcd(hcd);
+		usb_remove_hcd(hcd);
+
+		if (shared_hcd)
+			usb_put_hcd(shared_hcd);
+	
+		clk_disable_unprepare(clk);
+		clk_disable_unprepare(reg_clk);
+		reset_control_assert(xhci->reset);
+		usb_put_hcd(hcd);
+	}
 
 	pm_runtime_disable(&dev->dev);
 	pm_runtime_put_noidle(&dev->dev);
-- 
2.34.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ