* [PATCH net-next v3] net: tn40xx: avoid early firmware node release
@ 2026-11-18 16:36 Johan Hovold
0 siblings, 0 replies; only message in thread
From: Johan Hovold @ 2026-11-18 16:36 UTC (permalink / raw)
To: FUJITA Tomonori, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: netdev, Johan Hovold, Hans-Frieder Vogt
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. This is currently done unconditionally however, which
could result in the dropping of a reference that was never taken if
registrations fails early on.
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.
Avoid ever releasing the MDIO software node before it is deregistered
and fix the asymmetry and layering violation by making the software node
a primary node which is released when freeing the mdiobus.
Cc: Hans-Frieder Vogt <hfdevel@gmx.net>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
This was something I stumbled over when reviewing the software node
implementation and the patch has only been compile tested.
Johan
Changes in v3:
- squash the early release hardening and the primary firmware node cleanup
into one patch
v2: https://lore.kernel.org/all/20261116091149.730586-1-johan@kernel.org/
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/20261110100051.2567443-1-johan@kernel.org/
drivers/net/ethernet/tehuti/tn40_mdio.c | 18 ++++--------------
1 file changed, 4 insertions(+), 14 deletions(-)
diff --git a/drivers/net/ethernet/tehuti/tn40_mdio.c b/drivers/net/ethernet/tehuti/tn40_mdio.c
index fb1a4a2e4dbc..4130d75fbcaf 100644
--- a/drivers/net/ethernet/tehuti/tn40_mdio.c
+++ b/drivers/net/ethernet/tehuti/tn40_mdio.c
@@ -156,16 +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) {
- 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;
@@ -191,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);
@@ -209,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;
--
2.56.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-18 16:36 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 16:36 [PATCH net-next v3] net: tn40xx: avoid early firmware node release Johan Hovold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox