Skip to contentSkip to conversation

net: tn40xx: avoid early firmware node release

PATCH Superseded Johan Hovold 2 participants 5 messages Nov 16 – Nov 18

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: tn40xx: avoid early firmware node release

The tn40xx driver works around a swnode implementation detail and drops a firmware node reference it has not taken itself.

The first patch avoids releasing the firmware node prematurely if mdio bus registration ever bails out before taking the reference, and the second patch cleans up the implementation by switching to using a primary software node.

This was something I stumbled over when reviewing the software node implementation and the series has only been compile tested.

Johan

Changes in v2
- only drop the fwnode reference if it has been taken
- amend commit message and explain why this does not lead to a UAF currently
- switch to primary software node (2/2, new patch)

v1: https://lore.kernel.org/all/[email protected]/

Johan Hovold (2):
net: tn40xx: avoid early firmware node release
net: tn40xx: switch to primary software node

 drivers/net/ethernet/tehuti/tn40_mdio.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH v2 1/2 net: tn40xx: avoid early firmware node release

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 <[email protected]>
---
 drivers/net/ethernet/tehuti/tn40_mdio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/ethernet/tehuti/tn40_mdio.c +6 -1
index fb1a4a2e4dbc..438f0d86c2a1 100644
@@ -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);
 	}
Signature · 1 line
2.56.1
#2 lore raw
Quoted 7 lines · On Mon, 16 Nov 2026 10:11:48 +0100 Johan Hovold <[email protected]> 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.

[...]

drivers/net/ethernet/tehuti/tn40_mdio.c void tn40_swnodes_cleanup(struct tn40_priv *priv) · line 165

Quoted 13 lines
drivers/net/ethernet/tehuti/tn40_mdio.c +6 -1
@@ -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.

#3 lore raw
Quoted 13 lines · FUJITA Tomonori
Quoted 7 lines · On Mon, 16 Nov 2026 10:11:48 +0100 Johan Hovold <[email protected]> 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.

[...]

Quoted 16 lines

drivers/net/ethernet/tehuti/tn40_mdio.c void tn40_swnodes_cleanup(struct tn40_priv *priv) · line 165

Quoted 13 lines
drivers/net/ethernet/tehuti/tn40_mdio.c +6 -1
@@ -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.

Quoted 3 lines

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.

Quoted 3 lines

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

#4 lore raw

PATCH v2 2/2 net: tn40xx: switch to primary software node

In reply to Johan Hovold

Some cards use a secondary software node which is removed from the device by device_remove_software_node() on probe failure and on driver unbind.

Removing a secondary software node this way involves clearing the mdiobus device firmware node pointer, which in turn means that the driver must drop any reference taken by __mdiobus_register() during registration instead of letting mdiobus_release() do it as it would for a primary firmware node.

Fix the asymmetry and layering violation and clean up the software node handling by making the software node a primary node which is released when freeing the mdiobus.

Cc Hans-Frieder Vogt <[email protected]>
---
 drivers/net/ethernet/tehuti/tn40_mdio.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
drivers/net/ethernet/tehuti/tn40_mdio.c +4 -19
index 438f0d86c2a1..4130d75fbcaf 100644
@@ -156,21 +156,14 @@ static int tn40_swnodes_register(struct tn40_priv *priv)
 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) {
-		/*
-		 * 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);
+	if (priv->pdev->device == PCI_DEVICE_ID_TEHUTI_TN9510)
 		software_node_unregister_node_group(priv->nodes.group);
-	}
 }
 
 int tn40_mdiobus_init(struct tn40_priv *priv)
 {
 	struct pci_dev *pdev = priv->pdev;
+	struct fwnode_handle *fwnode;
 	struct mii_bus *bus;
 	int ret;
 
@@ -196,13 +189,8 @@ int tn40_mdiobus_init(struct tn40_priv *priv)
 			return ret;
 		}
 
-		ret = device_add_software_node(&bus->dev,
-					       priv->nodes.group[SWNODE_MDIO]);
-		if (ret) {
-			dev_err(&pdev->dev,
-				"device_add_software_node failed: %d\n", ret);
-			goto err_swnodes_unregister;
-		}
+		fwnode = software_node_fwnode(priv->nodes.group[SWNODE_MDIO]);
+		bus->dev.fwnode = fwnode;
 	}
 
 	tn40_mdio_set_speed(priv, TN40_MDIO_SPEED_6MHZ);
@@ -214,9 +202,6 @@ int tn40_mdiobus_init(struct tn40_priv *priv)
 	}
 	return 0;
 
-err_swnodes_unregister:
-	software_node_unregister_node_group(priv->nodes.group);
-	return ret;
 err_swnodes_cleanup:
 	tn40_swnodes_cleanup(priv);
 	return ret;
Signature · 1 line
2.56.1