Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Link: https://lore.kernel.org/r/E1qNxvu-00111m-1V@rmk-PC.armlinux.org.uk
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Russell King (Oracle) and committed by
Jakub Kicinski
09bd2d7d 8540336a

+9 -4
+9 -4
drivers/net/phy/mdio_bus.c
··· 107 107 } 108 108 EXPORT_SYMBOL(mdiobus_unregister_device); 109 109 110 - struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) 110 + static struct mdio_device *mdiobus_find_device(struct mii_bus *bus, int addr) 111 111 { 112 112 bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map); 113 - struct mdio_device *mdiodev; 114 113 115 114 if (WARN_ONCE(!addr_valid, "addr %d out of range\n", addr)) 116 115 return NULL; 117 116 118 - mdiodev = bus->mdio_map[addr]; 117 + return bus->mdio_map[addr]; 118 + } 119 119 120 + struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) 121 + { 122 + struct mdio_device *mdiodev; 123 + 124 + mdiodev = mdiobus_find_device(bus, addr); 120 125 if (!mdiodev) 121 126 return NULL; 122 127 ··· 134 129 135 130 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) 136 131 { 137 - return bus->mdio_map[addr]; 132 + return mdiobus_find_device(bus, addr) != NULL; 138 133 } 139 134 EXPORT_SYMBOL(mdiobus_is_registered_device); 140 135