public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: fix reference leak in usb_new_device()
@ 2024-12-17  3:53 Ma Ke
  2024-12-17 11:00 ` Greg KH
  2024-12-17 16:14 ` Alan Stern
  0 siblings, 2 replies; 3+ messages in thread
From: Ma Ke @ 2024-12-17  3:53 UTC (permalink / raw)
  To: gregkh, stern, mka, christophe.jaillet, quic_ugoswami, oneukum,
	stanley_chang, javier.carrasco
  Cc: linux-usb, linux-kernel, Ma Ke, stable

When device_add(&udev->dev) failed, calling put_device() to explicitly
release udev->dev. Otherwise, it could cause double free problem.

Found by code review.

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);
 fail:
 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
 	pm_runtime_disable(&udev->dev);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: fix reference leak in usb_new_device()
  2024-12-17  3:53 [PATCH] usb: fix reference leak in usb_new_device() Ma Ke
@ 2024-12-17 11:00 ` Greg KH
  2024-12-17 16:14 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2024-12-17 11:00 UTC (permalink / raw)
  To: Ma Ke
  Cc: stern, mka, christophe.jaillet, quic_ugoswami, oneukum,
	stanley_chang, javier.carrasco, linux-usb, linux-kernel, stable

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.
> 
> Found by code review.
> 
> 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);
>  fail:
>  	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
>  	pm_runtime_disable(&udev->dev);

How was this change tested?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: fix reference leak in usb_new_device()
  2024-12-17  3:53 [PATCH] usb: fix reference leak in usb_new_device() Ma Ke
  2024-12-17 11:00 ` Greg KH
@ 2024-12-17 16:14 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Stern @ 2024-12-17 16:14 UTC (permalink / raw)
  To: Ma Ke
  Cc: gregkh, mka, christophe.jaillet, quic_ugoswami, oneukum,
	stanley_chang, javier.carrasco, linux-usb, linux-kernel, stable

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
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-17 16:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17  3:53 [PATCH] usb: fix reference leak in usb_new_device() Ma Ke
2024-12-17 11:00 ` Greg KH
2024-12-17 16:14 ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox