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 <[email protected]>
---
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
On 23.3.2023 10.13, Jia-Ju Bai wrote:
> 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.
hcd shouldn't be null in usb_remove_hcd()
That was a bug which was properly fixed shortly after in:
4a593a62a9e3 xhci: Fix null pointer dereference in remove if xHC has only one roothub
I guess commit d7de14d74d65 ("usb: xhci_plat_remove: avoid NULL dereference") Isn't really
needed either anymore, but no harm in keeping it.
>
> 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
These hcd checks shouldn't be needed.
Thanks
Mathias