From: Chenguang Zhao <redacted>
phylink_connect_phy() may be called from probe without RTNL while the
net_device is still NETREG_UNINITIALIZED. If phylink_bringup_phy()
fails, phy_detach() uses rtnl_dereference(dev->hwprov) and lockdep
reports suspicious RCU usage.
hwprov cannot have been assigned before register_netdevice(). Relax the
RCU check for that state only; registered devices still require RTNL.
Fixes: 35f7cad1743e ("net: Add the possibility to support a selected hwtstamp in netdevice")
Reported-by: syzbot+694b49f41098a5df4fd7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=694b49f41098a5df4fd7
Signed-off-by: Chenguang Zhao <redacted>
---
v2:
phy_attach() does not set dev->hwprov. It is only written by ethtool
TSCONFIG after register_netdevice(). when calling phy_detach(), we
need to check whether hwprov is NULL to clean up dangling pointers.
This patch uses `NETREG_UNINITIALIZED` as a flag. When PHY registration
fails, TSCONFIG remains unset; hence `dev->hwprov` should not be accessed.
v1:
https://lore.kernel.org/all/20260916021505.238990-1-chenguang.zhao@linux.dev/ (local)
drivers/net/phy/phy_device.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 94b2e85e00a3..a3a7d5c75970 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1941,11 +1941,13 @@ void phy_detach(struct phy_device *phydev)
/* hwprov may technically be protected by ops lock but
* not for devices with a phydev, see phy_link_topo_add_phy()
*/
- hwprov = rtnl_dereference(dev->hwprov);
- /* Disable timestamp if it is the one selected */
- if (hwprov && hwprov->phydev == phydev) {
- rcu_assign_pointer(dev->hwprov, NULL);
- kfree_rcu(hwprov, rcu_head);
+ if (dev->reg_state != NETREG_UNINITIALIZED) {
+ hwprov = rtnl_dereference(dev->hwprov);
+ /* Disable timestamp if it is the one selected */
+ if (hwprov && hwprov->phydev == phydev) {
+ rcu_assign_pointer(dev->hwprov, NULL);
+ kfree_rcu(hwprov, rcu_head);
+ }
}
phydev->attached_dev->phydev = NULL;--
2.25.1