]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
net: ethtool: phy: avoid NULL deref when PHY driver is unbound
authorDavid Carlier <devnexen@gmail.com>
Sat, 9 May 2026 21:50:46 +0000 (22:50 +0100)
committerJakub Kicinski <kuba@kernel.org>
Wed, 13 May 2026 00:45:12 +0000 (17:45 -0700)
phydev->drv can become NULL while the phy_device is still attached to
its net_device, namely after the PHY driver is unbound via sysfs:

echo <mdio_id> > /sys/bus/mdio_bus/drivers/<phy_drv>/unbind

phy_remove() clears phydev->drv but doesn't call phy_detach(), so the
phy_device stays in the link topology xarray and ethnl_req_get_phydev()
still hands it back. ETHTOOL_MSG_PHY_GET then oopses on:

rep_data->drvname = kstrdup(phydev->drv->name, GFP_KERNEL);

drvname is already treated as optional by phy_reply_size(),
phy_fill_reply() and phy_cleanup_data(), so just skip the allocation
when there is no driver bound.

Fixes: 9dd2ad5e92b9 ("net: ethtool: phy: Convert the PHY_GET command to generic phy dump")
Cc: stable@vger.kernel.org # 6.13.x
Signed-off-by: David Carlier <devnexen@gmail.com>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Tested-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Link: https://patch.msgid.link/20260509215046.107157-1-devnexen@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/ethtool/phy.c

index f76d94d848d6da14bbb5da267f13890c2819d7e1..ddc6eab701ed65ec87fd7a286c45c3431fd46e4d 100644 (file)
@@ -94,10 +94,12 @@ static int phy_prepare_data(const struct ethnl_req_info *req_info,
        if (!rep_data->name)
                return -ENOMEM;
 
-       rep_data->drvname = kstrdup(phydev->drv->name, GFP_KERNEL);
-       if (!rep_data->drvname) {
-               ret = -ENOMEM;
-               goto err_free_name;
+       if (phydev->drv) {
+               rep_data->drvname = kstrdup(phydev->drv->name, GFP_KERNEL);
+               if (!rep_data->drvname) {
+                       ret = -ENOMEM;
+                       goto err_free_name;
+               }
        }
 
        rep_data->upstream_type = pdn->upstream_type;