Skip to contentSkip to conversation

net: phy: don't crash in phy_read/_write_mmd without a PHY driver

PATCH Applied Vladimir Oltean 4 participants 6 messages Jan 16, 2020 – Jan 20, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: don't crash in phy_read/_write_mmd without a PHY driver

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Check that drv is not NULL before reading from it.

---
If this hasn't been reported by now I assume it wasn't an issue so far. So I've targeted the patch for net-next and not provided a Fixes: tag.

 drivers/net/phy/phy-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/phy-core.c +2 -2
index 769a076514b0..a4d2d59fceca 100644
@@ -387,7 +387,7 @@ int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv->read_mmd) {
+	if (phydev->drv && phydev->drv->read_mmd) {
 		val = phydev->drv->read_mmd(phydev, devad, regnum);
 	} else if (phydev->is_c45) {
 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
@@ -444,7 +444,7 @@ int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv->write_mmd) {
+	if (phydev->drv && phydev->drv->write_mmd) {
 		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
 	} else if (phydev->is_c45) {
 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Vladimir Oltean

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Quoted 4 lines

Check that drv is not NULL before reading from it.

---

Ugh. Can I just add here:

without resending?

Quoted 29 lines

If this hasn't been reported by now I assume it wasn't an issue so far. So I've targeted the patch for net-next and not provided a Fixes: tag.

 drivers/net/phy/phy-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/phy-core.c +2 -3
index 769a076514b0..a4d2d59fceca 100644
@@ -387,7 +387,7 @@ int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
        if (regnum > (u16)~0 || devad > 32)
                return -EINVAL;
 
-       if (phydev->drv->read_mmd) {
+       if (phydev->drv && phydev->drv->read_mmd) {
                val = phydev->drv->read_mmd(phydev, devad, regnum);
        } else if (phydev->is_c45) {
                u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
@@ -444,7 +444,7 @@ int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
        if (regnum > (u16)~0 || devad > 32)
                return -EINVAL;
 
-       if (phydev->drv->write_mmd) {
+       if (phydev->drv && phydev->drv->write_mmd) {
                ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
        } else if (phydev->is_c45) {
                u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
--
Quoted 2 lines

2.17.1

Sorry,
-Vladimir

#2 lore raw
Quoted 11 lines · Vladimir Oltean
Quoted 10 lines · Vladimir Oltean

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Check that drv is not NULL before reading from it.

Quoted 8 lines
Quoted 2 lines

---

Ugh. Can I just add here:

without resending?

That's a tag that patchwork understands, so yes, that works:

http://patchwork.ozlabs.org/patch/1224348/mbox/

Quoted 36 lines
Quoted 30 lines

If this hasn't been reported by now I assume it wasn't an issue so far. So I've targeted the patch for net-next and not provided a Fixes: tag.

 drivers/net/phy/phy-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/phy-core.c +2 -3
index 769a076514b0..a4d2d59fceca 100644
@@ -387,7 +387,7 @@ int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
        if (regnum > (u16)~0 || devad > 32)
                return -EINVAL;
 
-       if (phydev->drv->read_mmd) {
+       if (phydev->drv && phydev->drv->read_mmd) {
                val = phydev->drv->read_mmd(phydev, devad, regnum);
        } else if (phydev->is_c45) {
                u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
@@ -444,7 +444,7 @@ int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
        if (regnum > (u16)~0 || devad > 32)
                return -EINVAL;
 
-       if (phydev->drv->write_mmd) {
+       if (phydev->drv && phydev->drv->write_mmd) {
                ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
        } else if (phydev->is_c45) {
                u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
--
Quoted 2 lines

2.17.1

Sorry,
-Vladimir

Signature · 1 line
Florian
maintainer
#3 lore raw
In reply to Vladimir Oltean
Quoted 9 lines · Vladimir Oltean

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Check that drv is not NULL before reading from it.

If there's no dedicated PHY driver, then the genphy driver is bound. I think therefore we don't face issues with the current code. But the change looks reasonable.

Quoted 31 lines

---
If this hasn't been reported by now I assume it wasn't an issue so far. So I've targeted the patch for net-next and not provided a Fixes: tag.

 drivers/net/phy/phy-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/phy-core.c +2 -2
index 769a076514b0..a4d2d59fceca 100644
@@ -387,7 +387,7 @@ int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv->read_mmd) {
+	if (phydev->drv && phydev->drv->read_mmd) {
 		val = phydev->drv->read_mmd(phydev, devad, regnum);
 	} else if (phydev->is_c45) {
 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
@@ -444,7 +444,7 @@ int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv->write_mmd) {
+	if (phydev->drv && phydev->drv->write_mmd) {
 		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
 	} else if (phydev->is_c45) {
 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
#4 lore raw
Quoted 8 lines · Heiner Kallweit
Quoted 7 lines · Vladimir Oltean

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Quoted 5 lines
Quoted 2 lines

Check that drv is not NULL before reading from it.

If there's no dedicated PHY driver, then the genphy driver is bound. I think therefore we don't face issues with the current code. But the change looks reasonable.

You can unbind the driver from sysfs and observe the problem.

Signature · 1 line
Florian
maintainer Applied
#5 lore raw
In reply to Vladimir Oltean

From: Vladimir Oltean <[email protected]>
Date: Thu, 16 Jan 2020 19:46:28 +0200

Quoted 10 lines

From: Alex Marginean <[email protected]>

The APIs can be used by Ethernet drivers without actually loading a PHY driver. This may become more widespread in the future with 802.3z compatible MAC PCS devices being locally driven by the MAC driver when configuring for a PHY mode with in-band negotiation.

Check that drv is not NULL before reading from it.

Applied, thank you.