usb: musb: mediatek: Release the usb_phy before unregistering it
Subsystems (3)
PATCH 1/1 usb: musb: mediatek: Release the usb_phy before unregistering it
mtk_musb_probe() registers a usb_phy_generic platform device and then takes a reference to it with devm_usb_get_phy(). mtk_musb_remove() and the probe error paths unregister that device by hand, which unbinds the phy-generic driver and frees the struct usb_phy, but the devm reference is only dropped later, when devres runs. devm_usb_phy_release() then calls usb_put_phy() on freed memory, which dereferences x->dev->driver->owner with a NULL driver. This happens whenever musb-mtk is unbound or removed, and when its probe fails after devm_usb_get_phy().
Let devres do both steps, in the right order. Register the unregistration of the usb_phy_generic device as a devm action right after creating it, before devm_usb_get_phy(). devres releases resources in reverse order, so the reference is dropped first and the device is unregistered afterwards.
This is the same fix as the one posted for the sunxi glue, where Rajan Yadav also pointed out that mediatek.c has the same problem.
---
Found by code inspection, after the same issue in sunxi.c. Not tested on MediaTek hardware.
Tested on 4405c18c95d2 in QEMU (x86_64, KASAN) with the real phy-generic driver and a test module laid out like mtk_musb_probe()/remove(): the same steps and error labels, and a child device that uses the usb_phy like musb-hdrc does. Without this patch, unbinding the glue, or failing its probe at the clk_bulk_prepare_enable() or musb-hdrc registration step, gives a KASAN slab-use-after-free in devm_usb_phy_release(). With it, KASAN reports nothing, the devres log (devres.log=1) shows devm_usb_phy_release() running before the device is unregistered, no usb_phy_generic device is left behind, and the child can be unbound and bound again while the glue stays bound. The same tests, plus ten unbind/bind cycles, also pass with lockdep, DEBUG_ATOMIC_SLEEP, DETECT_HUNG_TASK and DEBUG_OBJECTS enabled.
Compile-tested with W=1 for ARM (multi_v7_defconfig, clang) and x86_64 (COMPILE_TEST); the clang static analyzer reports nothing in mediatek.c before or after this patch.
drivers/usb/musb/mediatek.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/usb/musb/mediatek.c +15 -5
@@ -397,6 +397,11 @@ static const struct platform_device_info mtk_dev_info = { .dma_mask = DMA_BIT_MASK(32), }; +static void mtk_musb_unregister_usb_phy (void *usb_phy) +{ +usb_phy_generic_unregister (usb_phy); +} +static int mtk_musb_probe (struct platform_device *pdev) {struct musb_hdrc_platform_data *pdata;
@@ -461,11 +466,20 @@ static int mtk_musb_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (glue->usb_phy),"fail to registering usb-phy\n" ); +/* +* Registered before devm_usb_get_phy(), so that devres drops the +* reference to the PHY before the PHY device is unregistered. +*/ + ret =devm_add_action_or_reset (dev, mtk_musb_unregister_usb_phy, + glue->usb_phy); +if (ret) +return ret; + glue->xceiv =devm_usb_get_phy (dev,USB_PHY_TYPE_USB2 );if (IS_ERR (glue->xceiv)) { ret =PTR_ERR (glue->xceiv);dev_err (dev,"fail to getting usb-phy %d\n" , ret); - goto err_unregister_usb_phy; +return ret; }platform_set_drvdata (pdev, glue);
@@ -499,18 +513,14 @@ static int mtk_musb_probe(struct platform_device *pdev) err_enable_clk:pm_runtime_put_sync (dev);pm_runtime_disable (dev); -err_unregister_usb_phy: -usb_phy_generic_unregister (glue->usb_phy);return ret; }static void mtk_musb_remove (struct platform_device *pdev) {struct mtk_glue *glue =platform_get_drvdata (pdev); -struct platform_device *usb_phy = glue->usb_phy;platform_device_unregister (glue->musb_pdev); -usb_phy_generic_unregister (usb_phy); }#ifdef CONFIG_OF
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
Signature · 1 line
2.56.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
mtk_musb_probe()registers ausb_phy_genericplatform device and then takes a reference to it withdevm_usb_get_phy().mtk_musb_remove()and the probe error paths unregister that device by hand, which unbinds the phy-generic driver and frees the structusb_phy, but the devm reference is only dropped later, when devres runs.devm_usb_phy_release()then callsusb_put_phy()on freed memory, which dereferencesx->dev->driver->ownerwith a NULL driver. This happens whenever musb-mtk is unbound or removed, and when its probe fails afterdevm_usb_get_phy().Let devres do both steps, in the right order. Register the unregistration of the
usb_phy_genericdevice as a devm action right after creating it, beforedevm_usb_get_phy(). devres releases resources in reverse order, so the reference is dropped first and the device is unregistered afterwards.This is the same fix as the one posted for the sunxi glue, where Rajan Yadav also pointed out that
mediatek.chas the same problem.
Quoted 6 lines
Fixes 0990366bab3c ("usb: musb: Add support for MediaTek musb controller")Reported-by Rajan Yadav <[email protected]>Assisted-by Claude Opus 5.5 <[email protected]>Signed-off-by Lorenzo Ferrari <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1