Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by b.lane130
Mon, 23 Nov 2026 07:35
[PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove
Author: b.lane130
Date: Mon, 23 Nov 2026 07:35
Date: Mon, 23 Nov 2026 07:35
42 lines
18401 bytes
18401 bytes
From: Bruce Lane <b.lane130@outlook.com>
greth_mdio_init() allocates a struct mii_bus and connects to
a phy_device in greth_mdio_probe(). But the path error3 in
the probe and the remove callback only unregisters mdiobus
device. Call phy_disconnect() and mdiobus_free() to free
these resources.
Fixes: d4c41139df6e ("net: Add Aeroflex Gaisler 10/100/1G Ethernet MAC driver")
Signed-off-by: Bruce Lane <b.lane130@outlook.com>
---
drivers/net/ethernet/aeroflex/greth.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/aeroflex/greth.c b/drivers/net/ethernet/aeroflex/greth.c
index fccb67beebee..f649f76361ac 100644
--- a/drivers/net/ethernet/aeroflex/greth.c
+++ b/drivers/net/ethernet/aeroflex/greth.c
@@ -1519,7 +1519,9 @@ static int greth_of_probe(struct platform_device *ofdev)
error4:
dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
error3:
+ phy_disconnect(dev->phydev);
mdiobus_unregister(greth->mdio);
+ mdiobus_free(greth->mdio);
error2:
of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
error1:
@@ -1538,8 +1540,9 @@ static void greth_of_remove(struct platform_device *of_dev)
dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
if (ndev->phydev)
- phy_stop(ndev->phydev);
+ phy_disconnect(ndev->phydev);
mdiobus_unregister(greth->mdio);
+ mdiobus_free(greth->mdio);
unregister_netdev(ndev);
--
2.25.1
Re: [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove
Author: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 08:14
Date: Tue, 24 Nov 2026 08:14
135 lines
12843 bytes
12843 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch changes greth_of_remove() to call
phy_disconnect(ndev->phydev), then mdiobus_unregister() and…
Pre-existing issues:
- [High] greth_of_remove() (drivers/net/ethernet/aeroflex/greth.c) frees
both coherent descriptor rings with dma_free_coherent(rx_bd_base) and…
--
net: ethernet: aeroflex: fix memory leak in greth probe and remove
This commit adds phy_disconnect() and mdiobus_free() to the error3 path
of greth_of_probe(). In greth_of_remove() it replaces phy_stop() with
phy_disconnect() and adds mdiobus_free(), so that the mii_bus and the
attached phy_device are no longer leaked.
> diff --git a/drivers/net/ethernet/aeroflex/greth.c b/drivers/net/ethernet/aeroflex/greth.c
> index fccb67beebee1..f649f76361acb 100644
> --- a/drivers/net/ethernet/aeroflex/greth.c
> +++ b/drivers/net/ethernet/aeroflex/greth.c
[ ... ]
> @@ -1538,8 +1540,9 @@ static void greth_of_remove(struct platform_device *of_dev)
> dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
Since the patch edits this teardown block, could the order be fixed here
too?
greth_of_remove() frees both coherent descriptor rings before it calls
unregister_netdev(). If the interface is up at remove time, the device
is only quiesced later, from unregister_netdev()->dev_close()->
greth_close():
greth_close() {
napi_disable(&greth->napi);
greth_disable_irqs(greth);
greth_disable_tx(greth);
greth_disable_rx(greth);
netif_stop_queue(dev);
free_irq(greth->irq, (void *) dev);
greth_clean_rings(greth);
}
Between the dma_free_coherent() calls and greth_close(), the MAC RX/TX
DMA engines are still enabled. regs->rx_desc_p and regs->tx_desc_p still
point at the freed memory. NAPI poll, ndo_start_xmit and
greth_interrupt() can also still touch the rings.
Can the RX engine then DMA received data to an address taken from
reused memory, if it reads a descriptor whose enable bit happens to be
set?
After that, greth_clean_rings() reads every descriptor's addr from the
freed rings:
dma_unmap_single(greth->dev,
greth_read_bd(&rx_bdp->addr),
MAX_FRAME_SIZE+NET_IP_ALIGN,
DMA_FROM_DEVICE);
Isn't this a use-after-free read of rx_bd_base and tx_bd_base? The
unmaps would use garbage handles, and the real streaming mappings would
leak.
You can reach this with a sysfs unbind or a module unload while the
interface is up. Would calling unregister_netdev() first, before the PHY
and MDIO teardown and the dma_free_coherent() calls, avoid this?
>
> if (ndev->phydev)
> - phy_stop(ndev->phydev);
> + phy_disconnect(ndev->phydev);
> mdiobus_unregister(greth->mdio);
> + mdiobus_free(greth->mdio);
>
> unregister_netdev(ndev);
[Severity: High]
Is it safe to call phy_disconnect() before unregister_netdev()? At this
point ndev->reg_state is still NETREG_REGISTERED and RTNL is not held.
phy_disconnect()->phy_detach()->phy_detach_internal() does:
hwprov = rcu_dereference_protected(dev->hwprov,
lockdep_rtnl_is_held() ||
dev->reg_state != NETREG_REGISTERED);
Neither condition holds here. Won't every remove on a CONFIG_PROVE_RCU
kernel produce a suspicious rcu_dereference_protected() usage splat?
The same unlocked section clears dev->hwprov and frees it with
kfree_rcu(). It also sets dev->phydev to NULL and calls
phy_link_topo_del_phy(). All of this can race with RTNL holders.
phy_detach_internal() also drops the attach reference with
put_device(&phydev->mdio.dev). After that, mdiobus_unregister() calls
device_remove() and device_free() on the mdio_map entries, which drops
the last reference and frees the phy_device while the netdev is still
registered.
An ethtool ETHTOOL_GLINKSETTINGS or ETHTOOL_SLINKSETTINGS request goes
through greth_ethtool_ops to phy_ethtool_get_link_ksettings() or
phy_ethtool_set_link_ksettings(). If that request loaded ndev->phydev
under RTNL before it was cleared, could it then use the freed
phy_device?
Before this patch, remove only called phy_stop(). The attach reference
stayed held, so the phy_device was leaked but never freed under a live
user.
Would this order work instead: unregister_netdev() first, then
phy_disconnect(), then mdiobus_unregister() and mdiobus_free(), and
finally the dma_free_coherent() calls?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/BN8PR06MB6177818FBD04A32DAEDFC589A4452%40BN8PR06MB6177.namprd06.prod.outlook.com
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