diff mbox series

[v2,2/2] phy: mdio: fix memory leak

Message ID 55e9785e2ae2eae03c4af850a07e3297c5a0b784.1632861239.git.paskripkin@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v2,1/2] Revert "net: mdiobus: Fix memory leak in __mdiobus_register" | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Pavel Skripkin Sept. 28, 2021, 8:40 p.m. UTC
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: 46abc02175b3 ("phylib: give mdio buses a device tree presence")
Reported-and-tested-by: syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---

Changes in v2:
	Removed new state, since MDIOBUS_UNREGISTERED can be used. Also, moved
	bus->state initialization _before_ device_register() call, because
	Yanfei's patch is reverted in [v2 1/2]

---
 drivers/net/phy/mdio_bus.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Dan Carpenter Sept. 29, 2021, 6:22 a.m. UTC | #1
On Tue, Sep 28, 2021 at 11:40:15PM +0300, Pavel Skripkin wrote:
> 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: 46abc02175b3 ("phylib: give mdio buses a device tree presence")
> Reported-and-tested-by: syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>

Thanks!

Reviewed-by: Dan Carpenter <dan.carpenter@oracle.com>

regards,
dan carpenter
Jakub Kicinski Sept. 29, 2021, 11:48 p.m. UTC | #2
On Tue, 28 Sep 2021 23:40:15 +0300 Pavel Skripkin wrote:
> +	/* We need to set state to MDIOBUS_UNREGISTERED to correctly realese
> +	 * the device in mdiobus_free()
> +	 *
> +	 * State will be updated later in this function in case of success
> +	 */
> +	bus->state == MDIOBUS_UNREGISTERED;

IDK how syzbot has tested it but clearly we should blindly 
depend on that.

s/==/=/

Compiler would have told you this.
Pavel Skripkin Sept. 30, 2021, 5:55 a.m. UTC | #3
On 9/30/21 02:48, Jakub Kicinski wrote:
> On Tue, 28 Sep 2021 23:40:15 +0300 Pavel Skripkin wrote:
>> +	/* We need to set state to MDIOBUS_UNREGISTERED to correctly realese
>> +	 * the device in mdiobus_free()
>> +	 *
>> +	 * State will be updated later in this function in case of success
>> +	 */
>> +	bus->state == MDIOBUS_UNREGISTERED;
> 
> IDK how syzbot has tested it but clearly we should blindly
> depend on that.
> 
> s/==/=/
> 
> Compiler would have told you this.
> 
whooops... sorry about that. syzbot has tested v1. v2 is same, but 
without new state (so, the logic in v1 and v2 is the same).

I guess, it's copy-paste error on my side :). Will send v3 this evening



With regards,
Pavel Skripkin
diff mbox series

Patch

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 53f034fc2ef7..2d16a93af1ae 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -534,6 +534,13 @@  int __mdiobus_register(struct mii_bus *bus, struct module *owner)
 	bus->dev.groups = NULL;
 	dev_set_name(&bus->dev, "%s", bus->id);
 
+	/* We need to set state to MDIOBUS_UNREGISTERED to correctly realese
+	 * the device in mdiobus_free()
+	 *
+	 * State will be updated later in this function in case of success
+	 */
+	bus->state == MDIOBUS_UNREGISTERED;
+
 	err = device_register(&bus->dev);
 	if (err) {
 		pr_err("mii_bus %s failed to register\n", bus->id);