From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376597; b=pkhsArcoR495TjaYfUTg0cey0nwmijtU0ftphbegWsoI1sE/eb7vkviqIepzhc76546f DS1Hsdca9wWuBPNn96wWUBkHxzyQtPxlYjNBeGO1n7Q+e2FPo2keZSpFAaWZQ3oIXkDjw FXOk+o+YATHotmGjDSeYIiYTV/J7n9Zj3DSj11OSNrpCTcb2cGxJzi0ryLHFsUjysXNdN wJY6x8dHRFxa4sOAJ5Tht+cg7FGcFFUe3Pn+RN42kTluZdrRXDpbFUrLnAlt2TULByezG r9GU396xLPWUht6QSH9V2BNL3hyJylvSg9zmA66xBxaZrfSXbVs2Wk0Ew4WJe6xC0Tg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796376597; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=M8CgYRCEFVzWEod5BLnW/VbWg94agr9IAT9szbX7hOU=; b=j4jz72UtvnLDWYvkQrIMVI0wWGfqavuyar77pZB4dPiCgl+UIyzCcS6PbUDLeLLxPKAj 8kmWEVxZpVK0VGz3acgkARj7pGAEcH1CdHF8MSPlI+OVOJLQhRViLHc8g1v8AkEJGkwNc FM63bqSE23BfaJAwf/nl0zKO4h/sv/L5j0egLET12/DQaBelAhPxk8neBdlF8UXZo2tmh VoqxAJZq98mu8oMNwtSiYFGOnDWxJ6DIMH86akWByQiGJLTNbTs1WD3xuvX+bxAS9Cxr7 yGnB521AjsDCKm2ked7emDZ0oZ6ElRsWMUiWVbfwnpm/Ya/OyNQIgLPhjpV9FYnCvHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=gTkeFHGU; arc=none smtp.remote-ip=209.85.128.42 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DAB3606A32C for ; Fri, 4 Dec 2026 09:29:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2DAB3606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=gTkeFHGU Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4bb6adaf0f1so5034545e9.0 for ; Fri, 04 Dec 2026 01:29:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796376595; x=1796981395; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=M8CgYRCEFVzWEod5BLnW/VbWg94agr9IAT9szbX7hOU=; b=gTkeFHGUDbr+t8LVvPvV1x9KA+RKGQ8bAjqmq0qBiX95IISNxDNYpb6APsb9uHnyxh ft/euKYFupvGHCTZefZ3l9Fqvq8JsIloEtCYP5br9uPHGjwxue5DZRnSgjtwgadpgfuM T39X3hl1/hr88Sn6U7wS5bXKKgIxGz4e1bcCDx1JDoNMN5LJIxwfLZzKdb8jFC5iRWp/ 3dIugi5lr+k23xpYY5udgKOoS+ewFthXFp3fTLcR8tEvYThHQ4NZGq3JWcQpPNDMDnvr t3JcsPLBCUcUcHmHCCjctmkSXXIXfXwd53xNg9O2vQC9ZHlF/79em3kzGlZSPphSzWgO zLnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796376595; x=1796981395; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=M8CgYRCEFVzWEod5BLnW/VbWg94agr9IAT9szbX7hOU=; b=QHWB3W3zDgneOfl6fbTjF8PNU73GDyvWNhF+qLPmiGII6UiL1GwtqUGBHB0a0mB1eg oB1YphsxyvJOTj6DG9egPyQhb7IF7W8Bt8SYI9xL21sMqWyflKE4wlQ2oNXlhsAI6/D7 ZKWWMdrWd9DU55Ce3b6XiO22eJf570XHnbUlBs6nsvlKSKi7zVBaH6cOdu8BcpUWZPXO gpz6QcYzLD5X7fKpouYpUHCbT+hGn5ajEi9xagKZx12FRAk1IY3BOabiWVdkJ0cZ8dj7 XFFFkobrqUMZhqpbJRWCNcj6vSux8+zuctkxKLf99g0GBXJnB2aTS/7h0rCof2VFSuRW TWHA== X-Gm-Message-State: APuU4Z+R7af/QsugjuG0edK6llIa5VMATdTWr7iGDG7N7rZ7Ji4jQjkh auD+aezYFCzLCRZysCLMyghRmT2RgARojcewQQylS8RWvLFyV55q09Hk X-Gm-Gg: AZFdTZcbQkX+XeDJr9yRc17+M/xHTAqLXEY6/O1Q1Zh9vq9gdJdnJKtXKOs7dSujnSv Z4MxIgLql9LTkTB3KRlyQtoo+d+s7uRhWE7jucilKCBacNFuv1UBqROYpnLVRfSWhd1aP2XpMdG k3gDfCgBxGnr8NZVzOZHlJLHjQOntH+i/qDkwbQ5XJZJm0mAr9XTL7pwcs+itrGTeaGfYXXNfH3 bTfz2LNQklaPezUb5ki9LxtPYHz3k3uk1gG0eCwsOYwqVGyucpSjyz8+xohv0kj2NYDJJlw8kum xgkeUPdhMht+f9IXIE6IILNqv2gbcj1P+15VtNi/vFOLFE5PcEZsF5HdQsNKezNe/UQit+f1cSx j/Tf2JnJxDxJcjtH5yqN2HlXnn4x/Q64gLl9Q9bjVlh/55Nqifyrsv/DfUCmbSCrmGrU/yja6o7 27LBRhVl5doSzXt6f78iIW7QP0/2N9S/XtQOuKlo1Wodx9kRwwpufAmC+LwjpRkFK5v6+AFZw1Q I4RS75G3vg6r61vSc6hTqSOCEjWCYUrQBT6Y1L/QTxSbNBBco896vWWmrsMtqxDDKfL8jwjAWgD 0TFTb+Sru8RXww9tRM2VouU5UUg1hBvE7umTwW3Vi7yVGuRQyDu3Zsc+wtB1GiMhz61xT45XrMZ GWP+p3uo6xMcu6OqMZLRTEMXb/js+lAqlbmkhCiagOXR4ok1r X-Received: by 2002:a05:600c:1993:b0:4bb:9001:1aa with SMTP id 5b1f17b1804b1-4bbeb877151mr20133945e9.10.1796376595407; Fri, 04 Dec 2026 01:29:55 -0800 (PST) Received: from fedora.pigeon-monitor.ts.net (host-79-29-251-162.business.telecomitalia.it. [79.29.251.162]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe0030c6fsm156264025e9.2.2026.12.04.01.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:29:55 -0800 (PST) From: Lorenzo Ferrari To: Bin Liu , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Rajan Yadav , Lorenzo Ferrari , stable@vger.kernel.org Subject: [PATCH] usb: musb: mediatek: Release the usb_phy before unregistering it Date: Fri, 4 Dec 2026 10:29:47 +0100 Message-ID: <20261204092947.272427-1-loreferro01@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Fixes: 0990366bab3c ("usb: musb: Add support for MediaTek musb controller") Cc: stable@vger.kernel.org Reported-by: Rajan Yadav Link: https://lore.kernel.org/all/20261110-musb-sunxi-unbind-v1-0-d821584fc4a4@gmail.com/ Assisted-by: Claude Opus 5.5 Signed-off-by: Lorenzo Ferrari --- 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(-) diff --git a/drivers/usb/musb/mediatek.c b/drivers/usb/musb/mediatek.c index 45eea9aaa8..a3821505dd 100644 --- a/drivers/usb/musb/mediatek.c +++ b/drivers/usb/musb/mediatek.c @@ -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 -- 2.56.1