Skip to contentSkip to conversation

usb: musb: fix double put of gadget device on setup failure

PATCH In review Anfernee Cao 3 participants 5 messages Oct 20 – Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: musb: fix double put of gadget device on setup failure

musb_gadget_setup() calls usb_add_gadget_udc() and, when that fails, calls device_unregister(&musb->g.dev). But usb_add_gadget_udc() already releases the gadget on failure: usb_add_gadget_udc_release() calls usb_put_gadget(), i.e. put_device(&gadget->dev), in its error path.

The device model documents the same rule in the kerneldoc of device_register() (drivers/base/core.c):

NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.

The extra device_unregister() therefore runs device_del() on a device that was never added (or was already deleted) and drops the reference a second time, causing a refcount underflow.

Drop the redundant cleanup and just return the error; the UDC core has already released the gadget.

This issue was found by manual code inspection.

Fixes 3301c215a2bb ("USB: UDC: Expand device model API interface")
Cc [email protected] # v5.10+
---
 drivers/usb/musb/musb_gadget.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/musb/musb_gadget.c +1 -5
index 016d3f3fc1e0..4bc4270088bc 100644
@@ -1794,13 +1794,9 @@ int musb_gadget_setup(struct musb *musb)
 
 	status = usb_add_gadget_udc(musb->controller, &musb->g);
 	if (status)
-		goto err;
+		return status;
 
 	return 0;
-err:
-	musb->g.dev.parent = NULL;
-	device_unregister(&musb->g.dev);
-	return status;
 }
 
 void musb_gadget_cleanup(struct musb *musb)
Signature · 1 line
2.54.0.windows.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 16 lines

musb_gadget_setup() calls usb_add_gadget_udc() and, when that fails, calls device_unregister(&musb->g.dev). But usb_add_gadget_udc() already releases the gadget on failure: usb_add_gadget_udc_release() calls usb_put_gadget(), i.e. put_device(&gadget->dev), in its error path.

The device model documents the same rule in the kerneldoc of device_register() (drivers/base/core.c):

NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.

The extra device_unregister() therefore runs device_del() on a device that was never added (or was already deleted) and drops the reference a second time, causing a refcount underflow.

Quoted 8 lines

Drop the redundant cleanup and just return the error; the UDC core has already released the gadget.

This issue was found by manual code inspection.

Fixes 3301c215a2bb ("USB: UDC: Expand device model API interface")
Cc [email protected] # v5.10+

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Anfernee Cao

Hi Greg,

Friendly ping on this one. Sashiko reviewed it and found no issues, but I don't see it in usb-linus/usb-next/usb-testing or mainline. Could you take a look, or should I resend it?

Thanks,
Anfernee

#3 lore raw
Quoted 5 lines · Anfernee Cao

Hi Greg,

Friendly ping on this one. Sashiko reviewed it and found no issues, but I don't see it in usb-linus/usb-next/usb-testing or mainline. Could you take a look, or should I resend it?

I have no context here.

#4 lore raw

musb_gadget_setup() calls usb_add_gadget_udc() and, when that fails, calls device_unregister(&musb->g.dev). But usb_add_gadget_udc() already releases the gadget on failure: usb_add_gadget_udc_release() calls usb_put_gadget(), i.e. put_device(&gadget->dev), in its error path.

The device model documents the same rule in the kerneldoc of device_register() (drivers/base/core.c):

NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.

The extra device_unregister() therefore runs device_del() on a device that was never added (or was already deleted) and drops the reference a second time, causing a refcount underflow.

Drop the redundant cleanup and just return the error; the UDC core has already released the gadget.

This issue was found by manual code inspection.

Fixes 3301c215a2bb ("USB: UDC: Expand device model API interface")
Cc [email protected] # v5.10+

---

Resending in full because the original submission fell out of the queue during the merge window. No changes from v1; Sashiko reviewed it and reported no issues.

 drivers/usb/musb/musb_gadget.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/musb/musb_gadget.c +1 -5
index 016d3f3fc1e0..4bc4270088bc 100644
@@ -1794,13 +1794,9 @@ int musb_gadget_setup(struct musb *musb)
 
 	status = usb_add_gadget_udc(musb->controller, &musb->g);
 	if (status)
-		goto err;
+		return status;
 
 	return 0;
-err:
-	musb->g.dev.parent = NULL;
-	device_unregister(&musb->g.dev);
-	return status;
 }
 
 void musb_gadget_cleanup(struct musb *musb)
Signature · 1 line
2.54.0.windows.1