usb: dwc3: gadget: fix gadget workqueue use-after-free
Subsystems (2)
PATCH 1/1 usb: dwc3: gadget: fix gadget workqueue use-after-free
dwc3_gadget_soft_disconnect function, called as part of device_del(&gadget->dev), queues a new work item to the gadget workqueue after the workqueue has been flushed in usb_del_gadget.
This leads to a potential use-after-free issue.
To fix this, flush the workqueue in the release function before freeing the gadget. This ensures that all work items are processed before the gadget is destroyed.
--- drivers/usb/dwc3/gadget.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/dwc3/gadget.c +1 -0
@@ -4580,6 +4580,7 @@ static void dwc_gadget_release(struct device *dev) { struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); +flush_work (&gadget->work);kfree (gadget); }
base-commit: f066b5a6c7a06adfb666b7652cc99b4ff264f4ed
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
Thinh Nguyen
maintainerQuoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part of
The dwc3_gadget_soft_disconnect() isn't directly part of device_del(&gadget->dev). It should be part of disconnect.
Can you provide the full sequence of events so I can have more context? The handling of the flushing of gadget->work should not be part of the dwc3.
Quoted 9 lines
device_del(&gadget->dev), queues a new work item to the gadget workqueue after the workqueue has been flushed inusb_del_gadget.
This leads to a potential use-after-free issue.To fix this, flush the workqueue in the
releasefunction before freeing the gadget. This ensures that all work items are processed before the gadget is destroyed.Fixes 1ff24d40b3c3 ("usb: dwc3: gadget: Fix incorrect UDC state after manual deconfiguration")
Since the patch above is now in the mainline, may need to add a stable tag.
Thanks,
Thinh
Quoted 22 lines
Signed-off-by Roy Luo <[email protected]>--- drivers/usb/dwc3/gadget.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/dwc3/gadget.c +1
-0@@ -4580,6 +4580,7 @@ static void dwc_gadget_release(struct device *dev) { struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); +flush_work (&gadget->work);kfree (gadget); }base-commit:
f066b5a6c7a06adfb666b7652cc99b4ff264f4edSignature · 1 line
2.48.0.rc2.279.g1de40edade-goog
Quoted 10 lines · Thinh Nguyen
Quoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part ofThe
dwc3_gadget_soft_disconnect()isn't directly part ofdevice_del(&gadget->dev). It should be part of disconnect.Can you provide the full sequence of events so I can have more context? The handling of the flushing of
gadget->workshould not be part of the dwc3.
Yes, it's a part of disconnect, and disconnect is a part of gadget unbind. Let me try my best to explain. Here's the call stack for usb_del_gadget:
-> usb_del_gadget
-> flush_work(&gadget->work)
-> device_del
-> bus_remove_device
-> device_release_driver
-> gadget_unbind_driver
-> usb_gadget_disconnect_locked
-> dwc3_gadget_pullup
-> dwc3_gadget_soft_disconnect
-> usb_gadget_set_state
-> schedule_work(&gadget->work)Then when usb_put_gadget is called, gadget could be freed before gadget->work is executed.
-> usb_put_gadget
-> put_device
-> kobject_put
-> device_release
-> dwc_gadget_release
-> kfree(gadget)
Quoted 3 lines
Since the patch above is now in the mainline, may need to add a stable tag.
Ack, will cc stable in the next revision.
Regards,
Roy Luo
Thinh Nguyen
maintainerCc Alan
Quoted 27 lines · Roy Luo
Quoted 10 lines · Thinh Nguyen
Quoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part ofThe
dwc3_gadget_soft_disconnect()isn't directly part ofdevice_del(&gadget->dev). It should be part of disconnect.Can you provide the full sequence of events so I can have more context? The handling of the flushing of
gadget->workshould not be part of the dwc3.Yes, it's a part of disconnect, and disconnect is a part of gadget unbind. Let me try my best to explain. Here's the call stack for
usb_del_gadget:
->usb_del_gadget-> flush_work(&gadget->work) -> device_del -> bus_remove_device -> device_release_driver -> gadget_unbind_driver -> usb_gadget_disconnect_locked -> dwc3_gadget_pullup -> dwc3_gadget_soft_disconnect -> usb_gadget_set_state -> schedule_work(&gadget->work)
Quoted 9 lines
Then when
usb_put_gadgetis called, gadget could be freed beforegadget->workis executed.
->usb_put_gadget
->put_device
->kobject_put
->device_release
->dwc_gadget_release
-> kfree(gadget)
Thanks for the details!
The UDC core is initiating and handling the gadget->work, so the flushing of the gadget->work should also be handled there.
Since the usb_gadget_disconnect_locked() may trigger a state change work on unbind, the flushing of the gadget->work should to be moved to gadget_unbind_driver() instead:
drivers/usb/gadget/udc/core.c +2 -1
@@ -1568,7 +1568,6 @@ void usb_del_gadget(struct usb_gadget *gadget)kobject_uevent (&udc->dev.kobj,KOBJ_REMOVE );sysfs_remove_link (&udc->dev.kobj,"gadget" ); -flush_work (&gadget->work);device_del (&gadget->dev);ida_free (&gadget_id_numbers, gadget->id_number);cancel_work_sync (&udc->vbus_work);
@@ -1694,6 +1693,8 @@ static void gadget_unbind_driver(struct device *dev)synchronize_irq (gadget->irq);mutex_unlock (&udc->connect_lock); +flush_work (&gadget->work); + udc->driver->unbind (gadget);mutex_lock (&udc->connect_lock);
BR,
Thinh
Quoted 62 lines · Thinh Nguyen
Cc Alan
Quoted 27 lines · Roy Luo
Quoted 10 lines · Thinh Nguyen
Quoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part ofThe
dwc3_gadget_soft_disconnect()isn't directly part ofdevice_del(&gadget->dev). It should be part of disconnect.Can you provide the full sequence of events so I can have more context? The handling of the flushing of
gadget->workshould not be part of the dwc3.Yes, it's a part of disconnect, and disconnect is a part of gadget unbind. Let me try my best to explain. Here's the call stack for
usb_del_gadget:
->usb_del_gadget-> flush_work(&gadget->work) -> device_del -> bus_remove_device -> device_release_driver -> gadget_unbind_driver -> usb_gadget_disconnect_locked -> dwc3_gadget_pullup -> dwc3_gadget_soft_disconnect -> usb_gadget_set_state -> schedule_work(&gadget->work)Quoted 9 lines
Then when
usb_put_gadgetis called, gadget could be freed beforegadget->workis executed.
->usb_put_gadget
->put_device
->kobject_put
->device_release
->dwc_gadget_release
-> kfree(gadget)Thanks for the details!
The UDC core is initiating and handling the
gadget->work, so the flushing of thegadget->workshould also be handled there.Since the
usb_gadget_disconnect_locked()may trigger a state change work on unbind, the flushing of thegadget->workshould to be moved togadget_unbind_driver()instead:drivers/usb/gadget/udc/core.c +0
-1@@ -1568,7 +1568,6 @@ void usb_del_gadget(struct usb_gadget *gadget)kobject_uevent (&udc->dev.kobj,KOBJ_REMOVE );sysfs_remove_link (&udc->dev.kobj,"gadget" ); -flush_work (&gadget->work);device_del (&gadget->dev);ida_free (&gadget_id_numbers, gadget->id_number);cancel_work_sync (&udc->vbus_work);
drivers/usb/gadget/udc/core.c static void gadget_unbind_driver(struct device *dev) · line 1700
Quoted 8 lines
drivers/usb/gadget/udc/core.c +2
-0@@ -1694,6 +1693,8 @@ static void gadget_unbind_driver(struct device *dev)synchronize_irq (gadget->irq);mutex_unlock (&udc->connect_lock); +flush_work (&gadget->work); + udc->driver->unbind (gadget);mutex_lock (&udc->connect_lock);
What about instead moving the flush_work() call down just one line, after the device_del(&gadget->dev) call rather than before it?
The work queue doesn't need to be flushed every time a driver unbinds from the gadget, only when the gadget is about to be deallocated.
Alan Stern
Thinh Nguyen
maintainerQuoted 70 lines · Alan Stern
Quoted 69 lines · Thinh Nguyen
Cc Alan
Quoted 27 lines · Roy Luo
Quoted 10 lines · Thinh Nguyen
Quoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part ofThe
dwc3_gadget_soft_disconnect()isn't directly part ofdevice_del(&gadget->dev). It should be part of disconnect.Can you provide the full sequence of events so I can have more context? The handling of the flushing of
gadget->workshould not be part of the dwc3.Yes, it's a part of disconnect, and disconnect is a part of gadget unbind. Let me try my best to explain. Here's the call stack for
usb_del_gadget:
->usb_del_gadget-> flush_work(&gadget->work) -> device_del -> bus_remove_device -> device_release_driver -> gadget_unbind_driver -> usb_gadget_disconnect_locked -> dwc3_gadget_pullup -> dwc3_gadget_soft_disconnect -> usb_gadget_set_state -> schedule_work(&gadget->work)Quoted 9 lines
Then when
usb_put_gadgetis called, gadget could be freed beforegadget->workis executed.
->usb_put_gadget
->put_device
->kobject_put
->device_release
->dwc_gadget_release
-> kfree(gadget)Thanks for the details!
The UDC core is initiating and handling the
gadget->work, so the flushing of thegadget->workshould also be handled there.Since the
usb_gadget_disconnect_locked()may trigger a state change work on unbind, the flushing of thegadget->workshould to be moved togadget_unbind_driver()instead:drivers/usb/gadget/udc/core.c +2
-1@@ -1568,7 +1568,6 @@ void usb_del_gadget(struct usb_gadget *gadget)kobject_uevent (&udc->dev.kobj,KOBJ_REMOVE );sysfs_remove_link (&udc->dev.kobj,"gadget" ); -flush_work (&gadget->work);device_del (&gadget->dev);ida_free (&gadget_id_numbers, gadget->id_number);cancel_work_sync (&udc->vbus_work);@@ -1694,6 +1693,8 @@ static void gadget_unbind_driver(struct device *dev)synchronize_irq (gadget->irq);mutex_unlock (&udc->connect_lock); +flush_work (&gadget->work); + udc->driver->unbind (gadget);
Quoted 8 lines
drivers/usb/gadget/udc/core.c static void gadget_unbind_driver(struct device *dev) · line 1700
Quoted 1 line
drivers/usb/gadget/udc/core.c +0
-0mutex_lock (&udc->connect_lock);What about instead moving the
flush_work()call down just one line, after thedevice_del(&gadget->dev)call rather than before it?The work queue doesn't need to be flushed every time a driver unbinds from the gadget, only when the gadget is about to be deallocated.
That sounds good to me.
Thanks,
Thinh
Quoted 77 lines · Thinh Nguyen
Quoted 75 lines · Alan Stern
Quoted 62 lines · Thinh Nguyen
Cc Alan
Quoted 27 lines · Roy Luo
Quoted 10 lines · Thinh Nguyen
Quoted 1 line · Roy Luo
dwc3_gadget_soft_disconnectfunction, called as part ofThe
dwc3_gadget_soft_disconnect()isn't directly part ofdevice_del(&gadget->dev). It should be part of disconnect.Can you provide the full sequence of events so I can have more context? The handling of the flushing of
gadget->workshould not be part of the dwc3.Yes, it's a part of disconnect, and disconnect is a part of gadget unbind. Let me try my best to explain. Here's the call stack for
usb_del_gadget:
->usb_del_gadget-> flush_work(&gadget->work) -> device_del -> bus_remove_device -> device_release_driver -> gadget_unbind_driver -> usb_gadget_disconnect_locked -> dwc3_gadget_pullup -> dwc3_gadget_soft_disconnect -> usb_gadget_set_state -> schedule_work(&gadget->work)Quoted 9 lines
Then when
usb_put_gadgetis called, gadget could be freed beforegadget->workis executed.
->usb_put_gadget
->put_device
->kobject_put
->device_release
->dwc_gadget_release
-> kfree(gadget)Thanks for the details!
The UDC core is initiating and handling the
gadget->work, so the flushing of thegadget->workshould also be handled there.Since the
usb_gadget_disconnect_locked()may trigger a state change work on unbind, the flushing of thegadget->workshould to be moved togadget_unbind_driver()instead:drivers/usb/gadget/udc/core.c +0
-1@@ -1568,7 +1568,6 @@ void usb_del_gadget(struct usb_gadget *gadget)kobject_uevent (&udc->dev.kobj,KOBJ_REMOVE );sysfs_remove_link (&udc->dev.kobj,"gadget" ); -flush_work (&gadget->work);device_del (&gadget->dev);ida_free (&gadget_id_numbers, gadget->id_number);cancel_work_sync (&udc->vbus_work);drivers/usb/gadget/udc/core.c static void gadget_unbind_driver(struct device *dev) · line 1700
Quoted 8 lines
drivers/usb/gadget/udc/core.c +2
-0@@ -1694,6 +1693,8 @@ static void gadget_unbind_driver(struct device *dev)synchronize_irq (gadget->irq);mutex_unlock (&udc->connect_lock); +flush_work (&gadget->work); + udc->driver->unbind (gadget);mutex_lock (&udc->connect_lock);What about instead moving the
flush_work()call down just one line, after thedevice_del(&gadget->dev)call rather than before it?
Quoted 8 lines
Quoted 3 lines
The work queue doesn't need to be flushed every time a driver unbinds from the gadget, only when the gadget is about to be deallocated.
That sounds good to me.
Thanks,
Thinh
Thank both of you for the review!
I've sent out https://lore.kernel.org/linux-usb/[email protected]
per Alan's suggestion.
Regards,
Roy