Re: [PATCH v2 2/2] phy: mdio: fix memory leak

From: Dan Carpenter
Date: Wed Sep 29 2021 - 02:25:12 EST

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:
> Fixes: 46abc02175b3 ("phylib: give mdio buses a device tree presence")
> Reported-and-tested-by: syzbot+398e7dc692ddbbb4cfec@xxxxxxxxxxxxxxxxxxxxxxxxx
> Signed-off-by: Pavel Skripkin <paskripkin@xxxxxxxxx>


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

dan carpenter