All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wong Vee Khee <vee.khee.wong@linux.intel.com>
To: Pavel Skripkin <paskripkin@gmail.com>
Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, kuba@kernel.org, buytenh@marvell.com,
	afleming@freescale.com, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com,
	Dan Carpenter <dan.carpenter@oracle.com>
Subject: Re: [PATCH v3 2/2] phy: mdio: fix memory leak
Date: Thu, 30 Sep 2021 14:45:03 +0800	[thread overview]
Message-ID: <20210930064503.GA7992@linux.intel.com> (raw)
In-Reply-To: <5745dea4e12268a3f6b0772317f1cf0f49dab958.1632982651.git.paskripkin@gmail.com>

On Thu, Sep 30, 2021 at 09:19:54AM +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
> Reviewed-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
> ---
> 
> 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(+)
> 
> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> index 53f034fc2ef7..a5f910d4a7be 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

Typo here. s/realese/release/

> +	 * 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);

      reply	other threads:[~2021-09-30  6:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30  6:19 [PATCH v3 1/2] Revert "net: mdiobus: Fix memory leak in __mdiobus_register" Pavel Skripkin
2021-09-30  6:19 ` [PATCH v3 2/2] phy: mdio: fix memory leak Pavel Skripkin
2021-09-30  6:45   ` Wong Vee Khee [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210930064503.GA7992@linux.intel.com \
    --to=vee.khee.wong@linux.intel.com \
    --cc=afleming@freescale.com \
    --cc=andrew@lunn.ch \
    --cc=buytenh@marvell.com \
    --cc=dan.carpenter@oracle.com \
    --cc=davem@davemloft.net \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=paskripkin@gmail.com \
    --cc=syzbot+398e7dc692ddbbb4cfec@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.