From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376597; b=x7jMjAuvZavp3slD6+sQ3P/hlwHOn+s4nFaHQOukRjRBcvhuRqfIepf9hHJI1kcw6FVX DH5+yeUY4VBqal135s756Sl9mukI4xXmHmisJRwg/8wRIyGKAwdhpFMW3XxWpYFb76etS Dhh6h/8h2kBm0UE3hUnI56/IOtN+WTrbWfrer0wHhaS0CL+I4Ku1fwiJAEskT4za/3vS0 B6HOhkXVmNAwoBDWkGOkCDbMhT11ajFvHnuNQ8UhDP6jnYh96LgofAzbhETQIqQ3esRw4 YszHH95QbtMM0bklxeiLuFudFFzZIuGXg1ENuXQPzMeej8xtJ6V8JKxzZWHdSPaWO1w== 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-Forwarded-Encrypted: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=qlRGJZQR5tG6/zWImia8vV8zyG5m8VZDoaqFxHdJMGi3NAp3Lj57m0b62NPHCfIChPmD 7Nl3l3Kmdtt4qOQ4ZFpFmgJSJ33+3x+oHYOMaxTHPJ469QbMen6/qJZy7nR/SKXQ4ChqX oUDgcUIPWlUF+BLl4ul/cpG0KaDS3ISQhjvtI9Ees0WC4PnNuhGqR65aY6oMAv5/R37PZ 3Lfla/OKDHw34JQ9Rey/xJ/uxgx5TT1cVbtZWS3w3luvKWf6gze+QP71mqdfqoRPQDavt NYdzIkGQs9k405+NNmVvIPL++RiH0DKBq2188QZxIsvQofNeBQGDUhlccoM0f5wPtEA== 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.52 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 0BC37606A322 for ; Fri, 4 Dec 2026 09:29:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0BC37606A322 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-f52.google.com with SMTP id 5b1f17b1804b1-4bb5b8ef642so6362805e9.1 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=kJo77CtU7xTC9GwwJUaBrNOLRuM+1prFP8buCShmxtvkJBoXL0O1ukSw/gzXrvSTcN AUDC6p/VaZ8qc0ZDU+gmiykHLnpRslxvlxd+ZhM1p0aFjQL5suozfjw6V6kBF9soG+vR chcNe9qMGMC7DNnHdGa9a2t+fLcHXZuVHY1qhoujo1kMnHYt7IgZHPEyBXx+0GkX8tZj EC00gxPU4AbXDAx4ZXVk8UNJ6Mh1yoClRZR0Qw2B3DFxW7OjhXRf4zxxqEnbFlO+N/N6 6r7nQxNnGrulHZ3XnRsb63Itk/Sw35jaU+GodMSCKcMOqQa2vVXdApnMLCpdgoIBBTqW fXYQ== X-Forwarded-Encrypted: i=1; AL+hErGYM4PQA5rTRzj7xV5bo6U2YZD/D/00ljrcRk6u2lbwD5kqDBMv3XgMQ4A1cnHnU4+TD5LQHwE=@vger.kernel.org X-Gm-Message-State: APuU4Z/LVNh07KXG5PeEO5Lty2oL7KxKiCEgZ7f2ME1uSXmtcKmBX+/n LA2cwf1RtqpNCw4+FfWEY+G2qYeFKIt13ZQNZdEdr8WapWo8c/9oLID8 X-Gm-Gg: AZFdTZcolacd2crxKYw/5gyaUird8vAC65X3qrhUdsq6yfdzVPlFzl5oXn9zwD5Vq20 2ULwjx+eZKpm6X/9dMbmoTNZvk3blGSYQAfhJt1e57qfp5LgbeXuxdDEUmzQWVXRDF65geM594p iQ7LxiBIHXdZwhUCzB7Nt0y7xvdpjmGztl6/z9VWihazGFg/Xy7+cKQfzhz+M2bAtkx+JLSFdqK qsE5D/3yv3kHBc9/FTbGZ+LGIvqqtd9vKx0PclnKt2fbfgqDnmRz9DQsRDE+8nkya0gj96UvwOS k+QU1V/ILsTsHImx38ZnBiM2bFo/sOGpk8JsRT2kKCWsJ8J/U0qlo2ml/URrqrVwWBSwJC3yYIg qk81Pug7aTith8lQsOGwOdhXadHFiEtsqhRJERs2Zcrfz+2290L5X4tmwG/fQ1m1vmOaxoJN+TR Ad2FNte9QP1j+eJGK8kDKi3OUoKRp8Brmxed7RCC7QUfia7q3vCSjIl0xzSgYdIKdmkLafLKgeM vJTUXA4IhKh8iFD1lg9fhNtGQ5KknnDjTWqJC/oTlw3JhnnAkMvGJDWv1cIPvcfATLI1NSO1k90 Sik/Mug7ovfgb1dufQBG189WUTBZ8s+/ExuS6wII41vDGYCsM2fLHGEhQ9HYWdV8ZryxOAGlwlD PXX2Bf0A4410jgEZFpQkBokanC1rbK8wJBCIcAc57dG03SlxY 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: stable@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