Skip to contentSkip to conversation

net: mdio: ensure the type of mdio devices match mdio drivers

PATCH Superseded Russell King (Oracle) 3 participants 3 messages Sep 30, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: mdio: ensure the type of mdio devices match mdio drivers

On the MDIO bus, we have PHYLIB devices and drivers, and we have non- PHYLIB devices and drivers. PHYLIB devices are MDIO devices that are wrapped with a struct phy_device.

Trying to bind a MDIO device with a PHYLIB driver results in out-of- bounds accesses as we attempt to access struct phy_device members. So, let's prevent this by ensuring that the type of the MDIO device (indicated by the MDIO_DEVICE_FLAG_PHY flag) matches the type of the MDIO driver (indicated by the MDIO_DEVICE_IS_PHY flag.)

---
Tested locally in SolidRun Clearfog, DSA switch and PHY get probed correctly. Further testing welcomed.

 drivers/net/phy/mdio_bus.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/phy/mdio_bus.c +6 -0
index 53f034fc2ef7..779e49715e91 100644
@@ -939,6 +939,12 @@ EXPORT_SYMBOL_GPL(mdiobus_modify);
 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
 {
 	struct mdio_device *mdio = to_mdio_device(dev);
+	struct mdio_driver *mdiodrv = to_mdio_driver(drv);
+
+	/* Both the driver and device must type-match */
+	if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
+	    !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
+		return 0;
 
 	if (of_driver_match_device(dev, drv))
 		return 1;
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 24 lines · Russell King (Oracle)

On the MDIO bus, we have PHYLIB devices and drivers, and we have non- PHYLIB devices and drivers. PHYLIB devices are MDIO devices that are wrapped with a struct phy_device.

Trying to bind a MDIO device with a PHYLIB driver results in out-of- bounds accesses as we attempt to access struct phy_device members. So, let's prevent this by ensuring that the type of the MDIO device (indicated by the MDIO_DEVICE_FLAG_PHY flag) matches the type of the MDIO driver (indicated by the MDIO_DEVICE_IS_PHY flag.)

---
Tested locally in SolidRun Clearfog, DSA switch and PHY get probed correctly. Further testing welcomed.

 drivers/net/phy/mdio_bus.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/phy/mdio_bus.c +0 -0
index 53f034fc2ef7..779e49715e91 100644

drivers/net/phy/mdio_bus.c EXPORT_SYMBOL_GPL(mdiobus_modify); · line 942

Quoted 4 lines
drivers/net/phy/mdio_bus.c +1 -0
@@ -939,6 +939,12 @@ EXPORT_SYMBOL_GPL(mdiobus_modify);
 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
 {
 	struct mdio_device *mdio = to_mdio_device(dev);
+	struct mdio_driver *mdiodrv = to_mdio_driver(drv);

DaveM would request these two lines are swapped for reverse Christmas tree.

Andrew

#2 lore raw
Quoted 20 lines · Andrew Lunn
Quoted 19 lines · Russell King (Oracle)

On the MDIO bus, we have PHYLIB devices and drivers, and we have non- PHYLIB devices and drivers. PHYLIB devices are MDIO devices that are wrapped with a struct phy_device.

Trying to bind a MDIO device with a PHYLIB driver results in out-of- bounds accesses as we attempt to access struct phy_device members. So, let's prevent this by ensuring that the type of the MDIO device (indicated by the MDIO_DEVICE_FLAG_PHY flag) matches the type of the MDIO driver (indicated by the MDIO_DEVICE_IS_PHY flag.)

---
Tested locally in SolidRun Clearfog, DSA switch and PHY get probed correctly. Further testing welcomed.

 drivers/net/phy/mdio_bus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 11 lines

drivers/net/phy/mdio_bus.c EXPORT_SYMBOL_GPL(mdiobus_modify); · line 942

Quoted 9 lines
drivers/net/phy/mdio_bus.c +1 -0
index 53f034fc2ef7..779e49715e91 100644
@@ -939,6 +939,12 @@ EXPORT_SYMBOL_GPL(mdiobus_modify);
 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
 {
 	struct mdio_device *mdio = to_mdio_device(dev);
+	struct mdio_driver *mdiodrv = to_mdio_driver(drv);

DaveM would request these two lines are swapped for reverse Christmas tree.

Yep. I'll get around to that sometime.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!