From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796225634; b=B2jkDAQcAq600EAEbK2eJphLAQbiyyFOW1QqzdtHJRt4P9p3s3ISLWJCgn+10CPNdgwA sYEQKxh3rE0tb+sFeONLYQ4kUgfb2x/iuGfRmCXho8z2x+GIwLwRxkqteLNcEboEpWG64 5iG/iw9dwKvcuh83xzQhl6IwF5pxYrjQmNPorXK6Yl+0/z1dX6MkFLUEOYTbiwsClO715 pZiOLiiBvgqTzvPNQAT/wJIk8WHb0mrYb1thmltYywtGsyuEFB4ED+bkaSMUvnTDqV0K0 7njZ2YCZL0XIjZnwLFZ9ZEEuZCtc9/hHXDaSltrqGx27pxDha3LZXLU6h1tUMP7MRFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796225634; 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=25xuo+b57RHJrCNiOLimoCFIa2q6nAvw0Paqu8jjvYk=; b=2mv6JX/4lyosApK2ATwWM6fkI1zZjGWKpKsVowz+cONFoNgnXcYGwCM28OEpfrGKm8Ed h/4VXr3Im0qhDIGiDUCQqEPBaLnUPJHeOLUklmANt0KvbR2VBbVb+P+GkEPrpFgHUYvUQ vO9EA1qi9yVF+JEmmOqCprZ89PnaMi6ANsd/YcC1nNVRrFNddmBfelxaVwryC+yLhnKRG SOpMs8E189N8Ys1BXi1TI6x/kgBVxjzkIzMlAP2eSHOQms/bKk2jMFiGWz3gk4q7YuRdr quQu9c7hEa9SV0Y8rCLB1FylS5A1sM0yBou+fTRCfp0AL1GIcX/0DQ63ssEHx0LafEg== 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=h0SdYiqh; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.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 60074606A331 for ; Wed, 2 Dec 2026 15:33:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 60074606A331 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=h0SdYiqh Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-4a68190a2a7so11002302f8f.1 for ; Wed, 02 Dec 2026 07:33:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796225633; x=1796830433; 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=25xuo+b57RHJrCNiOLimoCFIa2q6nAvw0Paqu8jjvYk=; b=h0SdYiqhUspOHwpyNUBRGWizX36O+uyOnRInorwtcaI9OCQl0VJIDv8xGlIBKKbY/j HGhnFntEB6LZOq2jKrNA8xZU1O9kJ+Ng82ym6oQKO3Arg9IOiU/FoGaZ8PBV64xWhrSI hbi5bTsaOtCYy7X32aOp/hcnotvbDYb3OfiFI3kryp1/QYrO7Zyw7yzhwYcrDU+wMImA yip05S5E9uUDQjzVfjItQQrmGDHtBFwfkIwytN7d89sqhXc54/eu/NEYykAarir4KlAS 6iK4R0LN/ZFf7bS4etQWCk1uMR+Sfu88vV9up8Q6PbwRQrXO0WAefRrXChuMsLZW+fYx x7CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796225633; x=1796830433; 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=25xuo+b57RHJrCNiOLimoCFIa2q6nAvw0Paqu8jjvYk=; b=xBmoqgnlfYgF+pjgyy+L6oP6bAgb1D5wBPaRhdsaKObtdM9QmF/eR+1XRWYZ+Skaya l/gf8RschGK/rMoyCJFyZUYMT7UCWRRUV1hKgR/O8Iov4AjY/gemrn/+vNJSzVEIVP2T tA65DmZNkMp9cKT+t0NRUf2yUPQBi2oSdTsCpEZGWHDyHAOZ2Uz4tLOPQtsqsjrLvEJy syvUaGSJ2BdXTMe+olKkdu8bbgXIkoB6EEC3G/WUL25l4rvvYzoa03qa6vo5DP0lBE6B wWyR0+V6ThN/sVCivfRqfy6qjWKIjPpkLXrNHURLjX59Op7kb49tOseAeFARGncJbqOf pKbA== X-Gm-Message-State: APuU4Z9QCHbqoKuTZtQNJ/8EZf1LSfgb/Mi96JN3Ux9B8M58jYo9qu1i eG6UInYLlUigWX5mVZy1bwfDYywnzl7jNOxKB0Il6XoKZnLM4MlW5j4q X-Gm-Gg: AZFdTZejfjazEe5MsDwdaJDQHHjo6MBa3duvvl/zDhvQ/tuPayMvv1H9usUsex32MOj Uo9dFXgQ0VstBJ9CrSgK6sYQvehX2E3DKiZaglGwX8zuBqSsac4w03YKos9ya3j1EmLXd1/IS0T Ud7FWxH8R3wDTGS7/GiwaAuLcnGNTiJZwF5xmMkHV2ZvmtWTfj/y8eapHt9dPWGMo9q7xRonLwO 6zTP41sbaMgviu07OkZEKCUN5d2RS+lD1LWPtLq9kuUaiIUBlePyYgMGd07xSFi7p+GaxNTwc1l o0/yLCO3gCMXTDRPEUp/sUraFJV9XP5afNte9lZwxlZbdwd+KqglmHdc81crUdmSTwXqKrJlTYM IU4LSOXxDvtCyxkuTqS0y2MtVO9ycSQw+VuefR1tSkOeO57Fhn7lQejrMoSn8ihDVKp6CsCmJgs 6Gwwim6rV9TGUKgwtjyQ5xJlPoyLJ9PjDfvZ9ioijkDP1OtWoDUd9ZXIesFouZE7eSRUO3/1GYJ KmOnAPjWlcjAexkPZPYyoFSn5kJ/AJYC+6w7WDe9/7GSatshTpYKx0Zedkag9ju1i1qlLOAhQAz BFNzPmCoPNzhJok9WeVqw3/CQ3hQTr7y5jCLtcZotnDe6ITJwgP8RSINeoxt0bxIuNz7xdCZcya x5Y0yx056LkUpQFnWeIjjkTxNw0I= X-Received: by 2002:a05:6000:470c:b0:4a7:557:fc0 with SMTP id ffacd0b85a97d-4abb225fb4bmr77062072f8f.32.1796225632721; Wed, 02 Dec 2026 07:33:52 -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 ffacd0b85a97d-4ab658e2e6dsm75198748f8f.6.2026.12.02.07.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 07:33:52 -0800 (PST) From: Lorenzo Ferrari To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Lorenzo Ferrari Subject: [PATCH] usb: phy: Link consumers to the PHY device in the devm helpers Date: Wed, 2 Dec 2026 16:33:45 +0100 Message-ID: <20261202153345.165581-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 At probe time, the PHY driver creates the struct usb_phy that describes the PHY: phy-generic, for instance, embeds it in a devm_kzalloc()'d struct usb_phy_generic. A consumer, such as a USB controller, obtains a pointer to it through devm_usb_get_phy() or devm_usb_get_phy_by_node() and uses it for as long as it is bound. This object belongs to the PHY driver, and devres frees it as soon as the PHY driver is unbound from its device. The references taken when the consumer gets the PHY, try_module_get() and get_device(), only pin the PHY driver module and the PHY struct device. They do not block the one event that actually frees the object, the unbinding of the PHY driver, and the consumer is not notified when it happens. The crash can be triggered in two steps: 1) Unbind the PHY driver: devres frees the struct usb_phy and dev->driver is cleared, but the consumer still holds a pointer that now points to freed memory. 2) Unbind the consumer: devm_usb_phy_release() calls usb_put_phy() on the freed struct usb_phy, which dereferences x->dev->driver->owner with a NULL driver: BUG: kernel NULL pointer dereference, address: 0000000000000010 RIP: 0010:devm_usb_phy_release+0x14/0x30 Call Trace: devres_release_all device_unbind_cleanup device_release_driver_internal unbind_store The NULL pointer dereference is only the symptom of a use-after-free: with KASAN, the first bad access is reported as a slab-use-after-free in devm_usb_phy_release(), on memory freed by the PHY driver unbind. Add a managed device link from the consumer to the PHY device in the devm helpers, which know the consumer device. The driver core then unbinds the consumer, releasing its PHY reference, before the PHY driver. DL_FLAG_AUTOREMOVE_CONSUMER drops the link when the consumer is unbound, together with the devres entry holding the reference. Failing to create the link is not fatal and keeps the current behaviour. usb_get_phy() has no consumer device and is left unchanged. As a side effect, unbinding the PHY driver now also unbinds its consumers, and the consumer is moved after the PHY in the suspend, resume and shutdown order. The link must be managed: a stateless one, as used by the generic PHY framework, does not unbind consumers. Fixes: 410219dcd2ba ("usb: otg: utils: devres: Add API's to associate a device with the phy") Assisted-by: Claude Opus 5.5 Signed-off-by: Lorenzo Ferrari --- Reproduced on 4405c18c95d2 in QEMU (x86_64, KASAN) with phy-generic and a small test module whose probe calls devm_usb_get_phy(dev, USB_PHY_TYPE_USB2): echo -n usb_phy_generic.0.auto > /sys/bus/platform/drivers/usb_phy_generic/unbind echo -n usbphy_consumer > /sys/bus/platform/drivers/usbphy_consumer/unbind Without this patch: the oops above, or a KASAN slab-use-after-free. With it: the PHY unbind unbinds the consumer first, KASAN reports nothing, and the link is removed on consumer unbind and recreated on rebind. First seen on an ARM32 board (same oops, faulting address 0x8). The test module is available on request. drivers/usb/phy/phy.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c index 5a9b9353f3..6e0f21f9a6 100644 --- a/drivers/usb/phy/phy.c +++ b/drivers/usb/phy/phy.c @@ -346,6 +346,20 @@ static void devm_usb_phy_release2(struct device *dev, void *_res) usb_put_phy(res->phy); } +/* + * The struct usb_phy belongs to the PHY driver, which usually allocates it + * with devm and frees it on unbind. Link the consumer to the PHY device so + * that the driver core unbinds the consumer, and thus drops its reference, + * before the PHY driver goes away. The link is dropped when the consumer is + * unbound, together with the devres entry holding the reference. + */ +static void usb_phy_link_consumer(struct device *dev, struct usb_phy *phy) +{ + if (!device_link_add(dev, phy->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) + dev_dbg(dev, "failed to create device link to %s\n", + dev_name(phy->dev)); +} + static void usb_charger_init(struct usb_phy *usb_phy) { usb_phy->chg_type = UNKNOWN_TYPE; @@ -450,7 +464,8 @@ static int usb_add_extcon(struct usb_phy *x) * * Gets the phy using usb_get_phy(), and associates a device with it using * devres. On driver detach, release function is invoked on the devres data, - * then, devres data is freed. + * then, devres data is freed. The device is also linked to the phy device, + * so that it is unbound before the phy driver. * * For use by USB host and peripheral drivers. */ @@ -466,6 +481,7 @@ struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) if (!IS_ERR(phy)) { *ptr = phy; devres_add(dev, ptr); + usb_phy_link_consumer(dev, phy); } else devres_free(ptr); @@ -520,7 +536,8 @@ EXPORT_SYMBOL_GPL(usb_get_phy); * -EPROBE_DEFER if the device is not yet loaded. While at that, it * also associates the device with * the phy using devres. On driver detach, release function is invoked - * on the devres data, then, devres data is freed. + * on the devres data, then, devres data is freed. The device is also + * linked to the phy device, so that it is unbound before the phy driver. * * For use by peripheral drivers for devices related to a phy, * such as a charger. @@ -563,6 +580,9 @@ struct usb_phy *devm_usb_get_phy_by_node(struct device *dev, err1: spin_unlock_irqrestore(&phy_lock, flags); + if (!IS_ERR(phy)) + usb_phy_link_consumer(dev, phy); + err0: return phy; base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd -- 2.56.1