🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Johan Hovold Mon, 16 Nov 2026 09:11
[PATCH net-next v2 1/2] net: tn40xx: avoid early firmware node release
#17008
Author: Johan Hovold
Date: Mon, 16 Nov 2026 09:11
48 lines
9734 bytes
The mdiobus allocated by tn40_mdiobus_init() has no primary firmware
node so its firmware node pointer points to the secondary software node
added by device_add_software_node() and whose reference is released by
device_remove_software_node().

As device_remove_software_node() clears the firmware node pointer, any
reference taken by __mdiobus_register() needs to dropped before removing
the software node.

Add a comment explaining why the driver drops a reference it never took
itself, and only drop the firmware node reference if it was actually
taken by __mdiobus_register() as indicated by the bus state.

Note that __mdiobus_register() will essentially only return early
without taking the reference during development when the bus structure
has not been properly initialised. And currently, not even that would
lead to a use-after-free as the child PHY node holds a reference to the
parent MDIO node, the child is deregistered before the parent, and
software_node_unregister() returns early if the parent node has already
been freed.

Cc: Hans-Frieder Vogt <hfdevel@gmx.net>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/net/ethernet/tehuti/tn40_mdio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/tehuti/tn40_mdio.c b/drivers/net/ethernet/tehuti/tn40_mdio.c
index fb1a4a2e4dbc..438f0d86c2a1 100644
--- a/drivers/net/ethernet/tehuti/tn40_mdio.c
+++ b/drivers/net/ethernet/tehuti/tn40_mdio.c
@@ -157,7 +157,12 @@ void tn40_swnodes_cleanup(struct tn40_priv *priv)
 {
 	/* cleanup of swnodes is only needed for AQR105-based cards */
 	if (priv->pdev->device == PCI_DEVICE_ID_TEHUTI_TN9510) {
-		fwnode_handle_put(dev_fwnode(&priv->mdio->dev));
+		/*
+		 * drop any reference taken by __mdiobus_register() before
+		 * device_remove_software_node() clears the fwnode pointer
+		 */
+		if (priv->mdio->state != MDIOBUS_ALLOCATED)
+			fwnode_handle_put(dev_fwnode(&priv->mdio->dev));
 		device_remove_software_node(&priv->mdio->dev);
 		software_node_unregister_node_group(priv->nodes.group);
 	}
-- 
2.56.1
Re: [PATCH net-next v2 1/2] net: tn40xx: avoid early firmware node release
#18471
Author: FUJITA Tomonori
Date: Wed, 18 Nov 2026 10:55
38 lines
14373 bytes
On Mon, 16 Nov 2026 10:11:48 +0100
Johan Hovold <johan@kernel.org> wrote:

> Note that __mdiobus_register() will essentially only return early
> without taking the reference during development when the bus structure
> has not been properly initialised. And currently, not even that would
> lead to a use-after-free as the child PHY node holds a reference to the
> parent MDIO node, the child is deregistered before the parent, and
> software_node_unregister() returns early if the parent node has already
> been freed.

[...]

> --- a/drivers/net/ethernet/tehuti/tn40_mdio.c
> +++ b/drivers/net/ethernet/tehuti/tn40_mdio.c
> @@ -157,7 +157,12 @@ void tn40_swnodes_cleanup(struct tn40_priv *priv)
>  {
>  	/* cleanup of swnodes is only needed for AQR105-based cards */
>  	if (priv->pdev->device == PCI_DEVICE_ID_TEHUTI_TN9510) {
> -		fwnode_handle_put(dev_fwnode(&priv->mdio->dev));
> +		/*
> +		 * drop any reference taken by __mdiobus_register() before
> +		 * device_remove_software_node() clears the fwnode pointer
> +		 */
> +		if (priv->mdio->state != MDIOBUS_ALLOCATED)
> +			fwnode_handle_put(dev_fwnode(&priv->mdio->dev));

mdio->state is internal to the MDIO core. I don't think drivers should
look at it.

Also, 2/2 removes this code. The put is unbalanced when mdio bus
registration bails out before taking the reference, but it does no
real harm, as you explain in the commit message.

So I think we can drop this patch and fix the problem with 2/2 only.
The explanation in this commit message could go into the 2/2 commit
message.
Re: [PATCH net-next v2 1/2] net: tn40xx: avoid early firmware node release
#18626
Author: Johan Hovold
Date: Wed, 18 Nov 2026 16:03
50 lines
9843 bytes
On Wed, Nov 18, 2026 at 07:55:34PM +0900, FUJITA Tomonori wrote:
> On Mon, 16 Nov 2026 10:11:48 +0100
> Johan Hovold <johan@kernel.org> wrote:
> 
> > Note that __mdiobus_register() will essentially only return early
> > without taking the reference during development when the bus structure
> > has not been properly initialised. And currently, not even that would
> > lead to a use-after-free as the child PHY node holds a reference to the
> > parent MDIO node, the child is deregistered before the parent, and
> > software_node_unregister() returns early if the parent node has already
> > been freed.
> 
> [...]
> 
> > --- a/drivers/net/ethernet/tehuti/tn40_mdio.c
> > +++ b/drivers/net/ethernet/tehuti/tn40_mdio.c
> > @@ -157,7 +157,12 @@ void tn40_swnodes_cleanup(struct tn40_priv *priv)
> >  {
> >  	/* cleanup of swnodes is only needed for AQR105-based cards */
> >  	if (priv->pdev->device == PCI_DEVICE_ID_TEHUTI_TN9510) {
> > -		fwnode_handle_put(dev_fwnode(&priv->mdio->dev));
> > +		/*
> > +		 * drop any reference taken by __mdiobus_register() before
> > +		 * device_remove_software_node() clears the fwnode pointer
> > +		 */
> > +		if (priv->mdio->state != MDIOBUS_ALLOCATED)
> > +			fwnode_handle_put(dev_fwnode(&priv->mdio->dev));
> 
> mdio->state is internal to the MDIO core. I don't think drivers should
> look at it.

Some are, including this driver (if only for an error message).

I agree that they probably should not, but this highlights the existing
problem with the driver further.
 
> Also, 2/2 removes this code. The put is unbalanced when mdio bus
> registration bails out before taking the reference, but it does no
> real harm, as you explain in the commit message.

But that's mostly by chance. The whole thing is quite fragile.
 
> So I think we can drop this patch and fix the problem with 2/2 only.
> The explanation in this commit message could go into the 2/2 commit
> message.

Sure, I'll squash them.

Johan
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads