Skip to contentSkip to conversation

net: mdio: validate parameter addr in mdiobus_get_phy()

PATCH Applied Heiner Kallweit 4 participants 5 messages Jan 15, 2023 – Jan 17, 2023

Subsystems (3)
lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

PATCH 1/1 net: mdio: validate parameter addr in mdiobus_get_phy()

The caller may pass any value as addr, what may result in an out-of-bounds access to array mdio_map. One existing case is stmmac_init_phy() that may pass -1 as addr. Therefore validate addr before using it.

Fixes 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
---
 drivers/net/phy/mdio_bus.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/phy/mdio_bus.c +6 -1
index 902e1c88e..132dd1f90 100644
@@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
 
 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
 {
-	struct mdio_device *mdiodev = bus->mdio_map[addr];
+	struct mdio_device *mdiodev;
+
+	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
+		return NULL;
+
+	mdiodev = bus->mdio_map[addr];
 
 	if (!mdiodev)
 		return NULL;
Signature · 1 line
2.39.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Heiner Kallweit

The caller may pass any value as addr, what may result in an out-of-bounds access to array mdio_map. One existing case is stmmac_init_phy() that may pass -1 as addr. Therefore validate addr before using it.

Fixes 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")

Hi Heiner

This makes sense, but we should also fix stmmac to not actually do this, since it is clearly wrong, and probably indicates a configuration problem for that driver.

Andrew

maintainer
#2 lore raw
In reply to Heiner Kallweit
Quoted 15 lines · Heiner Kallweit

The caller may pass any value as addr, what may result in an out-of-bounds access to array mdio_map. One existing case is stmmac_init_phy() that may pass -1 as addr. Therefore validate addr before using it.

Fixes 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
---
 drivers/net/phy/mdio_bus.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/phy/mdio_bus.c +0 -0
index 902e1c88e..132dd1f90 100644

drivers/net/phy/mdio_bus.c EXPORT_SYMBOL(mdiobus_unregister_device); · line 114

Quoted 8 lines
drivers/net/phy/mdio_bus.c +4 -1
@@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
 
 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
 {
-	struct mdio_device *mdiodev = bus->mdio_map[addr];
+	struct mdio_device *mdiodev;
+
+	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
+		return NULL;

Speaking of possible follow-ups, would it make sense to add a WARN_ON_ONCE() or similar on the above condition?

Thanks!

Paolo

Quoted 1 line
author maintainer
#3 lore raw
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Heiner Kallweit

The caller may pass any value as addr, what may result in an out-of-bounds access to array mdio_map. One existing case is stmmac_init_phy() that may pass -1 as addr. Therefore validate addr before using it.

Fixes 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
---
 drivers/net/phy/mdio_bus.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/phy/mdio_bus.c +0 -0
index 902e1c88e..132dd1f90 100644
@@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
 
Quoted 11 lines

drivers/net/phy/mdio_bus.c EXPORT_SYMBOL(mdiobus_unregister_device); · line 114

Quoted 7 lines
drivers/net/phy/mdio_bus.c +4 -1
 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
 {
-	struct mdio_device *mdiodev = bus->mdio_map[addr];
+	struct mdio_device *mdiodev;
+
+	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
+		return NULL;

Speaking of possible follow-ups, would it make sense to add a WARN_ON_ONCE() or similar on the above condition?

Yes, I think that's a good idea.
I'll send a follow-up patch.

Quoted 5 lines

Thanks!

Paolo

Quoted 1 line
#4 lore raw
In reply to Heiner Kallweit
This patch was applied to netdev/net.git (master) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by Paolo Abeni <[email protected]>:

Quoted 8 lines · On Sun, 15 Jan 2023 11:54:06 +0100 you wrote:

The caller may pass any value as addr, what may result in an out-of-bounds access to array mdio_map. One existing case is stmmac_init_phy() that may pass -1 as addr. Therefore validate addr before using it.

Fixes 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")

[...]

Here is the summary with links:
- [net] net: mdio: validate parameter addr in mdiobus_get_phy()

    https://git.kernel.org/netdev/net/c/867dbe784c50

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html