Skip to contentSkip to conversation

net: mdio_bus: validate "addr" for mdiobus_is_registered_device()

PATCH Applied Russell King (Oracle) 3 participants 3 messages Jul 24, 2023 – Jul 26, 2023

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

PATCH 1/1 net: mdio_bus: validate "addr" for mdiobus_is_registered_device()

mdiobus_is_registered_device() doesn't checking that "addr" was valid before dereferencing bus->mdio_map[]. Extract the code that checks this from mdiobus_get_phy(), and use it here as well.

---
I've had this patch kicking about for a while - probably a good idea? It shouldn't cause a regression, but if it does it means we're already dereferencing the arrray outside its bounds in mdiobus_is_registered_device().

 drivers/net/phy/mdio_bus.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/phy/mdio_bus.c +9 -4
index bc04048de2fa..25dcaa49ab8b 100644
@@ -107,16 +107,21 @@ int mdiobus_unregister_device(struct mdio_device *mdiodev)
 }
 EXPORT_SYMBOL(mdiobus_unregister_device);
 
-struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
+static struct mdio_device *mdiobus_find_device(struct mii_bus *bus, int addr)
 {
 	bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map);
-	struct mdio_device *mdiodev;
 
 	if (WARN_ONCE(!addr_valid, "addr %d out of range\n", addr))
 		return NULL;
 
-	mdiodev = bus->mdio_map[addr];
+	return bus->mdio_map[addr];
+}
 
+struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
+{
+	struct mdio_device *mdiodev;
+
+	mdiodev = mdiobus_find_device(bus, addr);
 	if (!mdiodev)
 		return NULL;
 
@@ -129,7 +134,7 @@ EXPORT_SYMBOL(mdiobus_get_phy);
 
 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
 {
-	return bus->mdio_map[addr];
+	return mdiobus_find_device(bus, addr) != NULL;
 }
 EXPORT_SYMBOL(mdiobus_is_registered_device);
 
Signature · 1 line
2.30.2
#2 lore raw
In reply to Russell King (Oracle)
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 4 lines · On Mon, 24 Jul 2023 16:57:14 +0100 you wrote:

mdiobus_is_registered_device() doesn't checking that "addr" was valid before dereferencing bus->mdio_map[]. Extract the code that checks this from mdiobus_get_phy(), and use it here as well.

Quoted 8 lines

---
I've had this patch kicking about for a while - probably a good idea? It shouldn't cause a regression, but if it does it means we're already dereferencing the arrray outside its bounds in mdiobus_is_registered_device().

[...]

Here is the summary with links:
- [net-next] net: mdio_bus: validate "addr" for mdiobus_is_registered_device()

    https://git.kernel.org/netdev/net-next/c/09bd2d7ddaed

You are awesome, thank you!

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