mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Ma Ke <make_ruc2021@163.com>
Cc: gregkh@linuxfoundation.org, mka@chromium.org,
	christophe.jaillet@wanadoo.fr, quic_ugoswami@quicinc.com,
	oneukum@suse.com, stanley_chang@realtek.com,
	javier.carrasco@wolfvision.net, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] usb: fix reference leak in usb_new_device()
Date: Tue, 17 Dec 2024 11:14:11 -0500	[thread overview]
Message-ID: <ccc1083b-5ae8-490b-b357-52e162ba0a1f@rowland.harvard.edu> (raw)
In-Reply-To: <20241217035353.2891942-1-make_ruc2021@163.com>

On Tue, Dec 17, 2024 at 11:53:52AM +0800, Ma Ke wrote:
> When device_add(&udev->dev) failed, calling put_device() to explicitly
> release udev->dev. Otherwise, it could cause double free problem.

If you're worried that the same object might be freed more than once 
(double free), how can calling put_device() help?  Won't that cause 
udev->dev to be freed a third time?

> Found by code review.

In your code review, did you check to see whether the routine which 
calls usb_new_device() will do the put_device() when an error occurs?

> Cc: stable@vger.kernel.org
> Fixes: 9f8b17e643fe ("USB: make usbdevices export their device nodes instead of using a separate class")
> Signed-off-by: Ma Ke <make_ruc2021@163.com>
> ---
>  drivers/usb/core/hub.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> index 4b93c0bd1d4b..05b778d2ad63 100644
> --- a/drivers/usb/core/hub.c
> +++ b/drivers/usb/core/hub.c
> @@ -2651,6 +2651,7 @@ int usb_new_device(struct usb_device *udev)
>  	err = device_add(&udev->dev);
>  	if (err) {
>  		dev_err(&udev->dev, "can't device_add, error %d\n", err);
> +		put_device(&udev->dev);
>  		goto fail;
>  	}
>  
> @@ -2683,6 +2684,9 @@ int usb_new_device(struct usb_device *udev)
>  	pm_runtime_put_sync_autosuspend(&udev->dev);
>  	return err;
>  
> +out_del_dev:
> +	device_del(&udev->dev);
> +	put_device(&udev->dev);

You added a new statement label but you did not add any jumps to that 
label.  As a result, these two lines will never be executed.

Alan Stern

>  fail:
>  	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
>  	pm_runtime_disable(&udev->dev);
> -- 
> 2.25.1
> 

      parent reply	other threads:[~2024-12-17 16:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17  3:53 Ma Ke
2024-12-17 11:00 ` Greg KH
2024-12-17 16:14 ` Alan Stern [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=ccc1083b-5ae8-490b-b357-52e162ba0a1f@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=gregkh@linuxfoundation.org \
    --cc=javier.carrasco@wolfvision.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=make_ruc2021@163.com \
    --cc=mka@chromium.org \
    --cc=oneukum@suse.com \
    --cc=quic_ugoswami@quicinc.com \
    --cc=stable@vger.kernel.org \
    --cc=stanley_chang@realtek.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®