From: Johan Hovold <johan@kernel.org>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, Johan Hovold <johan@kernel.org>,
Hans-Frieder Vogt <hfdevel@gmx.net>
Subject: [PATCH net-next v3] net: tn40xx: avoid early firmware node release
Date: Wed, 18 Nov 2026 17:36:02 +0100 [thread overview]
Message-ID: <20261118163602.1141227-1-johan@kernel.org> (raw)
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
reply other threads:[~2026-11-18 16:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261118163602.1141227-1-johan@kernel.org \
--to=johan@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=hfdevel@gmx.net \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox