Revert "net: mdiobus: Fix memory leak in __mdiobus_register"
Subsystems (3)
PATCH v3 1/2 Revert "net: mdiobus: Fix memory leak in __mdiobus_register"
This reverts commit ab609f25d19858513919369ff3d9a63c02cd9e2e.
This patch is correct in the sense that we _should_ call device_put() in case of device_register() failure, but the problem in this code is more vast.
We need to set bus->state to UNMDIOBUS_REGISTERED before calling device_register() to correctly release the device in mdiobus_free(). This patch prevents us from doing it, since in case of device_register() failure put_device() will be called 2 times and it will cause UAF or something else.
Also, Reported-by: tag in revered commit was wrong, since syzbot reported different leak in same function.
---
Changes in v3:
CC Yanfei -> Acked-by Yanfei
Changes in v2:
Added this revert
--- drivers/net/phy/mdio_bus.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/phy/mdio_bus.c +0 -1
@@ -537,7 +537,6 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner) err =device_register (&bus->dev);if (err) {pr_err ("mii_bus %s failed to register\n" , bus->id); -put_device (&bus->dev);return -EINVAL ; }
Signature · 1 line
2.33.0
PATCH v3 2/2 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.
---
Changes in v3:
s/==/=/ Added Dan's Reviewed-by tag
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(+)
drivers/net/phy/mdio_bus.c +7 -0
@@ -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);
Signature · 1 line
2.33.0
Re: [PATCH v3 2/2] phy: mdio: fix memory leak
Quoted 41 lines · Pavel Skripkin
Syzbot reported memory leak in MDIO bus interface, the problem was in wrong state logic.
MDIOBUS_ALLOCATEDindicates 2 states:
1. Bus is only allocated
2. Bus allocated and__mdiobus_register()fails, butdevice_register()was calledIn case of
device_register()has been called we should callput_device()to correctly free the memory allocated for this device, butmdiobus_free()calls just kfree(dev) in case ofMDIOBUS_ALLOCATEDstateTo avoid this behaviour we need to set
bus->statetoMDIOBUS_UNREGISTERED_before_ callingdevice_register(), becauseput_device()should be called even in case ofdevice_register()failure.Fixes 46abc02175b3 ("phylib: give mdio buses a device tree presence")Reported-and-tested-by [email protected]Reviewed-by Dan Carpenter <[email protected]>Signed-off-by Pavel Skripkin <[email protected]>---
Changes in v3:
s/==/=/ Added Dan's Reviewed-by tagChanges in v2:
Removed new state, sinceMDIOBUS_UNREGISTEREDcan be used. Also, movedbus->stateinitialization _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(+)drivers/net/phy/mdio_bus.c +0
-0
drivers/net/phy/mdio_bus.c int __mdiobus_register(struct mii_bus *bus, struct module *owner) · line 537
Quoted 4 lines
drivers/net/phy/mdio_bus.c +1
-0@@ -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
Typo here. s/realese/release/
Quoted 9 lines
drivers/net/phy/mdio_bus.c +6
-0+* 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);