Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove
@ 2026-11-23  7:35 b.lane130
  2026-11-24  8:14 ` netdev-bot+sashiko
  0 siblings, 1 reply; 3+ messages in thread
From: b.lane130 @ 2026-11-23  7:35 UTC (permalink / raw)
  To: andreas, andrew+netdev, davem, edumazet, kuba, pabeni, kristoffer
  Cc: netdev, Bruce Lane

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove
@ 2026-11-23  7:38 netdev-bot+sinfo
  0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-11-23  7:38 UTC (permalink / raw)
  To: b.lane130
  Cc: andreas, andrew+netdev, davem, edumazet, kuba, pabeni, kristoffer,
	netdev

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

 - What hardware the change was tested on. For driver fixes please
   mention the device (and if relevant firmware version) used for
   testing, or say that the change was not tested on real hardware.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove
  2026-11-23  7:35 [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove b.lane130
@ 2026-11-24  8:14 ` netdev-bot+sashiko
  0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24  8:14 UTC (permalink / raw)
  To: b.lane130
  Cc: andreas, andrew+netdev, davem, edumazet, kuba, pabeni, kristoffer,
	netdev

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24  8:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  7:35 [PATCH net] net: ethernet: aeroflex: fix memory leak in greth probe and remove b.lane130
2026-11-24  8:14 ` netdev-bot+sashiko
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23  7:38 netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox