]> git.baikalelectronics.ru Git - kernel.git/commit
phy: mdio: fix memory leak
authorPavel Skripkin <paskripkin@gmail.com>
Thu, 30 Sep 2021 17:50:28 +0000 (20:50 +0300)
committerJakub Kicinski <kuba@kernel.org>
Fri, 1 Oct 2021 00:11:59 +0000 (17:11 -0700)
commit204ddbc0e413d34a64aeeebb0cd82a8ea3a9fb06
tree29443de2880c9ebb8ff7028e883238c3ace48ab2
parent042ab4bcdac59c52fe676cbe3b09f0859326f1ef
phy: mdio: fix memory leak

Syzbot reported memory leak in MDIO bus interface, the problem was in
wrong state logic.

MDIOBUS_ALLOCATED indicates 2 states:
1. Bus is only allocated
2. Bus allocated and __mdiobus_register() fails, but
   device_register() was called

In case of device_register() has been called we should call put_device()
to correctly free the memory allocated for this device, but mdiobus_free()
calls just kfree(dev) in case of MDIOBUS_ALLOCATED state

To avoid this behaviour we need to set bus->state to MDIOBUS_UNREGISTERED
_before_ calling device_register(), because put_device() should be
called even in case of device_register() failure.

Link: https://lore.kernel.org/netdev/YVMRWNDZDUOvQjHL@shell.armlinux.org.uk/
Fixes: f20c0459fac0 ("phylib: give mdio buses a device tree presence")
Reported-and-tested-by: syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com
Reviewed-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
Link: https://lore.kernel.org/r/eceae1429fbf8fa5c73dd2a0d39d525aa905074d.1633024062.git.paskripkin@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/phy/mdio_bus.c