Skip to contentSkip to conversation

usb: gadget: f_hid: fix device reference leak in hidg_alloc()

PATCH Superseded Guangshuo Li 2 participants 2 messages Apr 13

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: gadget: f_hid: fix device reference leak in hidg_alloc()

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 89ff3dfac604 ("usb: gadget: f_hid: fix f_hidg lifetime vs cdev")

---
v2:
- correct Fixes tag to 89ff3dfac604
- add Reviewed-by from Johan Hovold

 drivers/usb/gadget/function/f_hid.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/gadget/function/f_hid.c +1 -2
index 3ddfd4f66f0b..2734ebd35bda 100644
@@ -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);
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Guangshuo Li

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.

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

---
v2:
- correct Fixes tag to 89ff3dfac604
- add Reviewed-by from Johan Hovold

As you are using a tool to find/fix these things, you HAVE to mention that as per our documentation. Please go and read that and then resubmit all of thes patches that need that attribution added.

thanks,

greg k-h