]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
net/mdiobus: Fix potential out-of-bounds read/write access
authorJakub Raczynski <j.raczynski@samsung.com>
Mon, 9 Jun 2025 15:31:46 +0000 (17:31 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 27 Jun 2025 10:07:23 +0000 (11:07 +0100)
[ Upstream commit 0e629694126ca388916f059453a1c36adde219c4 ]

When using publicly available tools like 'mdio-tools' to read/write data
from/to network interface and its PHY via mdiobus, there is no verification of
parameters passed to the ioctl and it accepts any mdio address.
Currently there is support for 32 addresses in kernel via PHY_MAX_ADDR define,
but it is possible to pass higher value than that via ioctl.
While read/write operation should generally fail in this case,
mdiobus provides stats array, where wrong address may allow out-of-bounds
read/write.

Fix that by adding address verification before read/write operation.
While this excludes this access from any statistics, it improves security of
read/write operation.

Fixes: 080bb352fad00 ("net: phy: Maintain MDIO device and bus statistics")
Signed-off-by: Jakub Raczynski <j.raczynski@samsung.com>
Reported-by: Wenjing Shan <wenjing.shan@samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/phy/mdio_bus.c

index ee5fc73cbe07534640e9649cb604255c976f4214..7a2dce8d12433b8c971138ba0d141f3703f1b3f5 100644 (file)
@@ -764,6 +764,9 @@ int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
 
        lockdep_assert_held_once(&bus->mdio_lock);
 
+       if (addr >= PHY_MAX_ADDR)
+               return -ENXIO;
+
        if (bus->read)
                retval = bus->read(bus, addr, regnum);
        else
@@ -793,6 +796,9 @@ int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
 
        lockdep_assert_held_once(&bus->mdio_lock);
 
+       if (addr >= PHY_MAX_ADDR)
+               return -ENXIO;
+
        if (bus->write)
                err = bus->write(bus, addr, regnum, val);
        else