public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc()
@ 2026-04-12 16:15 Guangshuo Li
  2026-04-13  7:40 ` Johan Hovold
  0 siblings, 1 reply; 3+ messages in thread
From: Guangshuo Li @ 2026-04-12 16:15 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Marco Crivellari, Ben Hoff, Guangshuo Li,
	Yuhao Jiang, William Wu, Terry Junge, Andrzej Pietrasiewicz,
	John Keeping, Lee Jones, linux-usb, linux-kernel
  Cc: stable

hidg_alloc() initializes hidg->dev with device_initialize() before
calling dev_set_name(). If dev_set_name() fails, the function currently
jumps to err_unlock and returns without calling put_device().

This leaves the device reference unbalanced and prevents hidg_release()
from being called. Calling put_device() here is also safe, since
hidg_release() only frees resources owned by hidg.

Route the dev_set_name() failure path through err_put_device so the
device reference is dropped properly.

Fixes: 944fe915d00d ("usb: gadget: f_hid: tidy error handling in hidg_alloc")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/usb/gadget/function/f_hid.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index 3ddfd4f66f0b..2734ebd35bda 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -1610,7 +1610,7 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
 	hidg->dev.devt = MKDEV(major, opts->minor);
 	ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
 	if (ret)
-		goto err_unlock;
+		goto err_put_device;
 
 	hidg->bInterfaceSubClass = opts->subclass;
 	hidg->bInterfaceProtocol = opts->protocol;
@@ -1647,7 +1647,6 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
 
 err_put_device:
 	put_device(&hidg->dev);
-err_unlock:
 	mutex_unlock(&opts->lock);
 	return ERR_PTR(ret);
 }
-- 
2.43.0


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

* Re: [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc()
  2026-04-12 16:15 [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc() Guangshuo Li
@ 2026-04-13  7:40 ` Johan Hovold
  2026-04-13  7:58   ` Guangshuo Li
  0 siblings, 1 reply; 3+ messages in thread
From: Johan Hovold @ 2026-04-13  7:40 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Greg Kroah-Hartman, Marco Crivellari, Ben Hoff, Yuhao Jiang,
	William Wu, Terry Junge, Andrzej Pietrasiewicz, John Keeping,
	Lee Jones, linux-usb, linux-kernel, stable

On Mon, Apr 13, 2026 at 12:15:55AM +0800, Guangshuo Li wrote:
> hidg_alloc() initializes hidg->dev with device_initialize() before
> calling dev_set_name(). If dev_set_name() fails, the function currently
> jumps to err_unlock and returns without calling put_device().
> 
> This leaves the device reference unbalanced and prevents hidg_release()
> from being called. Calling put_device() here is also safe, since
> hidg_release() only frees resources owned by hidg.

Good catch.

> Route the dev_set_name() failure path through err_put_device so the
> device reference is dropped properly.
> 
> Fixes: 944fe915d00d ("usb: gadget: f_hid: tidy error handling in hidg_alloc")

This isn't the commit that introduced the issue, though. This should be:

Fixes: 89ff3dfac604 ("usb: gadget: f_hid: fix f_hidg lifetime vs cdev")

With that fixed you can add my:

Reviewed-by: Johan Hovold <johan@kernel.org>

Johan

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

* Re: [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc()
  2026-04-13  7:40 ` Johan Hovold
@ 2026-04-13  7:58   ` Guangshuo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Guangshuo Li @ 2026-04-13  7:58 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Greg Kroah-Hartman, Marco Crivellari, Ben Hoff, Yuhao Jiang,
	William Wu, Terry Junge, Andrzej Pietrasiewicz, John Keeping,
	Lee Jones, linux-usb, linux-kernel, stable

Hi Johan,

Thanks for the review and for pointing that out.

You're right, the correct Fixes tag should be:

Fixes: 89ff3dfac604 ("usb: gadget: f_hid: fix f_hidg lifetime vs cdev")

I'll send a v2 shortly with that fixed and include your Reviewed-by tag.

Reviewed-by: Johan Hovold johan@kernel.org

Thanks,
Guangshuo

Johan Hovold <johan@kernel.org> 于2026年4月13日周一 15:40写道:
>
> On Mon, Apr 13, 2026 at 12:15:55AM +0800, Guangshuo Li wrote:
> > hidg_alloc() initializes hidg->dev with device_initialize() before
> > calling dev_set_name(). If dev_set_name() fails, the function currently
> > jumps to err_unlock and returns without calling put_device().
> >
> > This leaves the device reference unbalanced and prevents hidg_release()
> > from being called. Calling put_device() here is also safe, since
> > hidg_release() only frees resources owned by hidg.
>
> Good catch.
>
> > Route the dev_set_name() failure path through err_put_device so the
> > device reference is dropped properly.
> >
> > Fixes: 944fe915d00d ("usb: gadget: f_hid: tidy error handling in hidg_alloc")
>
> This isn't the commit that introduced the issue, though. This should be:
>
> Fixes: 89ff3dfac604 ("usb: gadget: f_hid: fix f_hidg lifetime vs cdev")
>
> With that fixed you can add my:
>
> Reviewed-by: Johan Hovold <johan@kernel.org>
>
> Johan

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

end of thread, other threads:[~2026-04-13  7:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-12 16:15 [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc() Guangshuo Li
2026-04-13  7:40 ` Johan Hovold
2026-04-13  7:58   ` Guangshuo Li

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