[PATCH net 1/2] net: phy: restore timestamping state on probe failure
From: Kimberly Morris
Date: Wed Nov 18 2026 - 05:34:14 EST
A PHY driver can install a devm-backed mii_timestamper and select it as
the default before its probe sequence has completed. If a later probe
step fails, the driver core releases the driver's devres, but the
phy_device retains both timestamping fields.
If the generic PHY driver later attaches, skb_defer_rx_timestamp() can
dereference the freed mii_timestamper. This also affects non-PTP packets
because the receive path tests hwtstamp_set before classifying the
packet.
Save the incoming mii_ts and default_timestamp state and restore it on
every probe error path. Restoring the previous values also preserves a
timestamper installed externally before the probe.
On an x86_64 KASAN kernel, a KUnit reproducer using a real mii_bus,
phy_device and phy_driver triggered a slab use-after-free after the
failed probe, driver-core devres cleanup and generic-PHY fallback. The
same test passes after this change.
Fixes: 39db6be781cd ("net: phy: broadcom: Add PTP support for some
Broadcom PHYs.")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kimberly Morris <gy770616@xxxxxxxxx>
---
drivers/net/phy/phy_device.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945..4017568 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -3812,8 +3812,10 @@ EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
static int phy_probe(struct device *dev)
{
struct phy_device *phydev = to_phy_device(dev);
+ struct mii_timestamper *mii_ts = phydev->mii_ts;
struct device_driver *drv = phydev->mdio.dev.driver;
struct phy_driver *phydrv = to_phy_driver(drv);
+ bool default_timestamp = phydev->default_timestamp;
int err = 0;
phydev->drv = phydrv;
@@ -3951,6 +3953,9 @@ static int phy_probe(struct device *dev)
phydev->drv->remove(phydev);
out_reset:
+ phydev->mii_ts = mii_ts;
+ phydev->default_timestamp = default_timestamp;
+
/* Re-assert the reset signal on error */
phy_device_reset(phydev, 1);
--
2.55.0.windows.3
On Wed, 18 Nov 2026 05:33:45 -0500, Kimberly Morris <gy770616@xxxxxxxxx> wrote:
> Hello,
>
> Following Greg's request for an applyable patch, this series addresses the
> automatic PHY probe-failure lifetime path from the earlier report. It does
> not rely on the root-only sysfs driver-unbind trigger.
>
> A PHY driver can publish a devres-backed mii_timestamper before its probe
> has completed. If a later probe step fails, driver-core cleanup frees the
> owner while the phy_device can retain the published timestamping state. A
> later generic-PHY fallback can then reach the freed object from
> skb_defer_rx_timestamp(). Broadcom BCM54210E/BCM54213PE also leaves a
> registered PHC backed by the same freed storage.
>
> Patch 1 restores the timestamping state that existed before phy_probe().
> Patch 2 adds the missing managed Broadcom PHC and timestamper cleanup.
>
> I reproduced the first issue on an x86_64 KASAN kernel with a KUnit test
> that registers a real mii_bus, phy_device and phy_driver, follows the real
> failed-probe and devres cleanup paths, attaches the generic PHY driver, and
> passes a non-PTP skb to skb_defer_rx_timestamp(). Before the fix KASAN
> reported a slab use-after-free; after the fix the test passed without a
> KASAN report.
>
> Tested on:
>
> - upstream master 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
> - net/main 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5
>
> The submitted series is generated against current upstream master
> 63fdb511c27afcebce797e3db580b3534ddfa639. I have not tested it on physical
> BCM54213PE hardware, and I am not claiming an attacker-controlled probe
> failure, privilege escalation, or code execution.
>
> Please let me know if the rollback belongs in individual PHY drivers rather
> than phylib core.
>
> Thank you.
>
> Kimberly Morris (2):
> net: phy: restore timestamping state on probe failure
> net: phy: broadcom: release PTP clock with PHY devres
>
> drivers/net/phy/bcm-phy-ptp.c | 19 +++++++++++++++++++
> drivers/net/phy/phy_device.c | 5 +++++
> 2 files changed, 24 insertions(+)