mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net: phy: reject non-PHY mdio devices in phy_connect()
@ 2026-11-15 16:18 Ziheng Wei
  2026-11-15 16:30 ` Andrew Lunn
  0 siblings, 1 reply; 5+ messages in thread
From: Ziheng Wei @ 2026-11-15 16:18 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel, stable

phy_connect() resolves the PHY by name using bus_find_device_by_name()
and then casts the result to struct phy_device without verifying that
it actually is one.

Every mdio device is registered with the same name format (PHY_ID_FMT,
bus id and address), whether it is a struct phy_device or a bare
struct mdio_device. A DT node under an MDIO bus with a compatible
string that is not an ethernet-phy-* string is registered as a bare
mdio_device, which is the normal way to describe switches and PCS
devices on the same bus.

If such a device sits at the address a MAC driver passes to
phy_connect(), the name lookup succeeds and to_phy_device() derives a
struct phy_device pointer from a smaller struct mdio_device. Accessing
any field past the embedded mdio_device then reads out of bounds.

With a test module that registers a bare mdio_device and calls
phy_connect() on its bus id (QEMU, KASAN, v7.3.1):

  BUG: KASAN: slab-out-of-bounds in phy_connect+0x196/0x200 [libphy]
  Read of size 4 at addr ffff88801b270f2c by task insmod/162
  Call Trace:
   phy_connect+0x196/0x200 [libphy]

The same problem was fixed for of_phy_find_device() in commit
6ed742363b9c ("of: of_mdio: Ensure mdio device is a PHY"), and
fwnode_phy_find_device() in this file performs the check as well.
phy_connect() was left without it. Check MDIO_DEVICE_FLAG_PHY before
calling to_phy_device() and return -ENODEV for a non-PHY device.

Found by LLM-assisted source review. Reproduced only with the synthetic
test module above; no board is known to hit this. The fix was build
tested only; it was not run against the test module, selftests or
hardware.

Fixes: a9049e0c513c ("mdio: Add support for mdio drivers.")
Cc: stable@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---

Notes:
    The changelog was drafted with an AI assistant from my analysis;
    I have reviewed the code, the fix and the text.

 drivers/net/phy/phy_device.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945ce..4f8f9cbc3 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1184,6 +1184,11 @@ struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
 		pr_err("PHY %s not found\n", bus_id);
 		return ERR_PTR(-ENODEV);
 	}
+	if (!(to_mdio_device(d)->flags & MDIO_DEVICE_FLAG_PHY)) {
+		pr_err("MDIO device %s is not a PHY\n", bus_id);
+		put_device(d);
+		return ERR_PTR(-ENODEV);
+	}
 	phydev = to_phy_device(d);
 
 	rc = phy_connect_direct(dev, phydev, handler, interface);

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-15 17:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 16:18 [PATCH net] net: phy: reject non-PHY mdio devices in phy_connect() Ziheng Wei
2026-11-15 16:30 ` Andrew Lunn
2026-11-15 16:42   ` Ziheng Wei
2026-11-15 16:53     ` Andrew Lunn
2026-11-15 17:03       ` Ziheng Wei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®