All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Markus Stockhausen" <markus.stockhausen@gmx.de>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH net-next v14 04/13] net: phy: add phy_detach_internal() helper
Date: Tue, 18 Aug 2026 16:27:28 +0000	[thread overview]
Message-ID: <20260818162728.4DC631F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260817162632.1208891-5-markus.stockhausen@gmx.de>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Use-after-free of MDIO bus when detaching a PHY.
- [High] Error path in `phy_attach_direct` catastrophically destroys existing PHY attachment and leaks references.
--

--- Patch [4]: [PATCH net-next v14 04/13] net: phy: add phy_detach_internal() helper ---
commit cd30833ec89f19b0a5658d942adf201f6894916e
Author: Markus Stockhausen <markus.stockhausen@gmx.de>

net: phy: add phy_detach_internal() helper

For the upcoming MDIO bus notification enhancements the PHY detach path
needs to inform the bus about these actions. Until now phy_detach() is
called normally but also in the phy_direct_attch() cleanup parts. So in
the future there must be a clear indication if a notification is needed
(normal path) or not (cleanup path).

Carve out the phy_detach() code into a new helper phy_detach_internal()
and make phy_detach() a short stub that calls the new helper.

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 94b2e85e00a37..835d71306b4d0 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -1734,6 +1734,91 @@ static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
>  	return phydrv->config_intr && phydrv->handle_interrupt;
>  }
>  
> +static void phy_detach_internal(struct phy_device *phydev)
> +{
> +	struct net_device *dev = phydev->attached_dev;
> +	struct module *ndev_owner = NULL;
> +	struct mii_bus *bus;
> +

[ ... ]

> +	phy_suspend(phydev);
> +	if (dev) {
> +		struct hwtstamp_provider *hwprov;
> +
> +		/* hwprov may technically be protected by ops lock but
> +		 * not for devices with a phydev, see phy_link_topo_add_phy()
> +		 */
> +		hwprov = rtnl_dereference(dev->hwprov);
> +		/* Disable timestamp if it is the one selected */
> +		if (hwprov && hwprov->phydev == phydev) {
> +			rcu_assign_pointer(dev->hwprov, NULL);
> +			kfree_rcu(hwprov, rcu_head);
> +		}
> +
> +		phydev->attached_dev->phydev = NULL;
> +		phydev->attached_dev = NULL;

[Severity: High]
This is a pre-existing issue, but does this catastrophically destroy
existing PHY attachments when called from the phy_attach_direct() error
path?

If phy_attach_direct() is called for a PHY that is already attached (e.g.,
due to a device tree misconfiguration), it correctly detects this with
"if (phydev->attached_dev)", but then jumps to the error label which calls
into this detachment path.

This unconditionally destroys the existing valid attachment (clearing
attached_dev->phydev and resetting PHY state) instead of gracefully
aborting the new attachment attempt. Additionally, balancing the old
attachment's references here permanently leaks the new module and device
references acquired earlier in phy_attach_direct().

> +		phy_link_topo_del_phy(dev, phydev);
> +	}
> +
> +	phydev->phy_link_change = NULL;

[ ... ]

> +	/* Assert the reset signal */
> +	phy_device_reset(phydev, 1);
> +
> +	/*
> +	 * The phydev might go away on the put_device() below, so avoid
> +	 * a use-after-free bug by reading the underlying bus first.
> +	 */
> +	bus = phydev->mdio.bus;
> +
> +	put_device(&phydev->mdio.dev);
> +	if (dev)
> +		ndev_owner = dev->dev.parent->driver->owner;
> +	if (ndev_owner != bus->owner)
> +		module_put(bus->owner);

[Severity: High]
This isn't a bug introduced by this patch, but can this dereference a
freed bus pointer?

If the MDIO bus has been unregistered and its primary references dropped,
the PHY device may hold the final reference to the bus via its parent
pointer. When phy_detach_internal() calls put_device(&phydev->mdio.dev),
it can drop the last reference to the bus, causing the struct mii_bus to
be freed.

The subsequent check "if (ndev_owner != bus->owner)" and
module_put(bus->owner) will then dereference the freed bus pointer.

> +}
> +
> +/**
> + * phy_detach - detach a PHY device from its network device

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260817162632.1208891-1-markus.stockhausen@gmx.de?part=4

  parent reply	other threads:[~2026-08-18 16:27 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-17 16:26 [PATCH net-next v14 00/13] net: mdio: realtek-rtl9300: Add RTL83xx support Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 01/13] dt-bindings: net: realtek,rtl9301-mdio: Add RTL83xx series Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 02/13] net: mdio: realtek-rtl9300: Add polling documentation Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 03/13] net: mdio: realtek-rtl9300: deny C45 over C22 access Markus Stockhausen
2026-08-18 16:27   ` sashiko-bot
2026-08-17 16:26 ` [PATCH net-next v14 04/13] net: phy: add phy_detach_internal() helper Markus Stockhausen
2026-08-17 16:41   ` Andrew Lunn
2026-08-18 16:27   ` sashiko-bot [this message]
2026-08-17 16:26 ` [PATCH net-next v14 05/13] net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus Markus Stockhausen
2026-08-17 16:46   ` Andrew Lunn
2026-08-17 16:26 ` [PATCH net-next v14 06/13] net: mdio: realtek-rtl9300: suppress sysfs bind/unbind attributes Markus Stockhausen
2026-08-17 16:47   ` Andrew Lunn
2026-08-18 16:27   ` sashiko-bot
2026-08-17 16:26 ` [PATCH net-next v14 07/13] net: mdio: realtek-rtl9300: Configure hardware polling during probing Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 08/13] net: mdio: realtek-rtl9300: Add page tracking Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 09/13] net: mdio: realtek-rtl9300: Increase MDIO timeout Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 10/13] net: mdio: realtek-rtl9300: Open up C22 and C45 space in parallel Markus Stockhausen
2026-08-17 17:03   ` Andrew Lunn
2026-08-17 17:29     ` AW: " Markus Stockhausen
2026-08-17 19:35       ` Andrew Lunn
2026-08-17 20:05         ` AW: " Markus Stockhausen
2026-08-17 20:10           ` Andrew Lunn
2026-08-17 20:25             ` AW: " Markus Stockhausen
2026-08-17 20:31               ` Andrew Lunn
2026-08-18 17:44                 ` AW: " Markus Stockhausen
2026-08-18 19:32                   ` Andrew Lunn
2026-08-18 16:27   ` sashiko-bot
2026-08-17 16:26 ` [PATCH net-next v14 11/13] net: mdio: realtek-rtl9300: Add support for RTL838x Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 12/13] net: mdio: realtek-rtl9300: Add support for RTL839x Markus Stockhausen
2026-08-17 16:26 ` [PATCH net-next v14 13/13] net: mdio: realtek-rtl9300: reword Kconfig and module description Markus Stockhausen
2026-08-17 17:25 ` [PATCH net-next v14 00/13] net: mdio: realtek-rtl9300: Add RTL83xx support Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260818162728.4DC631F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=markus.stockhausen@gmx.de \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.