drivers/usb/gadget/udc/core.c | 17 ++++++++++++++++- include/linux/usb/gadget.h | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-)
A race condition during gadget teardown can lead to a use-after-free
in usb_gadget_state_work(), as reported by KASAN:
BUG: KASAN: invalid-access in sysfs_notify+0_x_2c/0_x_d0
Workqueue: events usb_gadget_state_work
The fundamental race occurs because a concurrent event (e.g., an
interrupt) can call usb_gadget_set_state() and schedule gadget->work
at any time during the cleanup process in usb_del_gadget().
Commit 399a45e5237c ("usb: gadget: core: flush gadget workqueue after
device removal") attempted to fix this by moving flush_work() to after
device_del(). However, this does not fully solve the race, as a new
work item can still be scheduled *after* flush_work() completes but
before the gadget's memory is freed, leading to the same use-after-free.
This patch fixes the race condition robustly by introducing a 'teardown'
flag and a 'state_lock' spinlock to the usb_gadget struct. The flag is
set during cleanup in usb_del_gadget() *before* calling flush_work() to
prevent any new work from being scheduled once cleanup has commenced.
The scheduling site, usb_gadget_set_state(), now checks this flag under
the lock before queueing the work, thus safely closing the race window.
Changes in v2:
- Removed redundant inline comments as suggested by Alan Stern.
Fixes: 5702f75375aa9 ("usb: gadget: udc-core: move sysfs_notify() to a workqueue")
Signed-off-by: Jimmy Hu <hhhuuu@google.com>
Cc: stable@vger.kernel.org
---
drivers/usb/gadget/udc/core.c | 17 ++++++++++++++++-
include/linux/usb/gadget.h | 5 +++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index d709e24c1fd4..66d2428835da 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1123,8 +1123,13 @@ static void usb_gadget_state_work(struct work_struct *work)
void usb_gadget_set_state(struct usb_gadget *gadget,
enum usb_device_state state)
{
+ unsigned long flags;
+
+ spin_lock_irqsave(&gadget->state_lock, flags);
gadget->state = state;
- schedule_work(&gadget->work);
+ if (!gadget->teardown)
+ schedule_work(&gadget->work);
+ spin_unlock_irqrestore(&gadget->state_lock, flags);
}
EXPORT_SYMBOL_GPL(usb_gadget_set_state);
@@ -1357,6 +1362,8 @@ static void usb_udc_nop_release(struct device *dev)
void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
void (*release)(struct device *dev))
{
+ spin_lock_init(&gadget->state_lock);
+ gadget->teardown = false;
INIT_WORK(&gadget->work, usb_gadget_state_work);
gadget->dev.parent = parent;
@@ -1531,6 +1538,7 @@ EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
void usb_del_gadget(struct usb_gadget *gadget)
{
struct usb_udc *udc = gadget->udc;
+ unsigned long flags;
if (!udc)
return;
@@ -1544,6 +1552,13 @@ void usb_del_gadget(struct usb_gadget *gadget)
kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
sysfs_remove_link(&udc->dev.kobj, "gadget");
device_del(&gadget->dev);
+ /*
+ * Set the teardown flag before flushing the work to prevent new work
+ * from being scheduled while we are cleaning up.
+ */
+ spin_lock_irqsave(&gadget->state_lock, flags);
+ gadget->teardown = true;
+ spin_unlock_irqrestore(&gadget->state_lock, flags);
flush_work(&gadget->work);
ida_free(&gadget_id_numbers, gadget->id_number);
cancel_work_sync(&udc->vbus_work);
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 0f28c5512fcb..8b5e593f7966 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -351,6 +351,9 @@ struct usb_gadget_ops {
* can handle. The UDC must support this and all slower speeds and lower
* number of lanes.
* @state: the state we are now (attached, suspended, configured, etc)
+ * @state_lock: Spinlock protecting the `state` and `teardown` members.
+ * @teardown: True if the device is undergoing teardown, used to prevent
+ * new work from being scheduled during cleanup.
* @name: Identifies the controller hardware type. Used in diagnostics
* and sometimes configuration.
* @dev: Driver model state for this abstract device.
@@ -426,6 +429,8 @@ struct usb_gadget {
enum usb_ssp_rate max_ssp_rate;
enum usb_device_state state;
+ spinlock_t state_lock;
+ bool teardown;
const char *name;
struct device dev;
unsigned isoch_delay;
--
2.51.0.760.g7b8bcc2412-goog
On Tue, Oct 14, 2025 at 08:51:56AM +0000, Jimmy Hu wrote:
> A race condition during gadget teardown can lead to a use-after-free
> in usb_gadget_state_work(), as reported by KASAN:
>
> BUG: KASAN: invalid-access in sysfs_notify+0_x_2c/0_x_d0
> Workqueue: events usb_gadget_state_work
>
> The fundamental race occurs because a concurrent event (e.g., an
> interrupt) can call usb_gadget_set_state() and schedule gadget->work
> at any time during the cleanup process in usb_del_gadget().
>
> Commit 399a45e5237c ("usb: gadget: core: flush gadget workqueue after
> device removal") attempted to fix this by moving flush_work() to after
> device_del(). However, this does not fully solve the race, as a new
> work item can still be scheduled *after* flush_work() completes but
> before the gadget's memory is freed, leading to the same use-after-free.
>
> This patch fixes the race condition robustly by introducing a 'teardown'
> flag and a 'state_lock' spinlock to the usb_gadget struct. The flag is
> set during cleanup in usb_del_gadget() *before* calling flush_work() to
> prevent any new work from being scheduled once cleanup has commenced.
> The scheduling site, usb_gadget_set_state(), now checks this flag under
> the lock before queueing the work, thus safely closing the race window.
>
> Changes in v2:
> - Removed redundant inline comments as suggested by Alan Stern.
This goes below the --- line.
> 2.51.0.760.g7b8bcc2412-goog
This does not apply to my usb-linus branch, what kernel was it made
against? Can you rebase and resubmit it based on that one?
On Tue, Oct 14, 2025 at 08:51:56AM +0000, Jimmy Hu wrote:
> A race condition during gadget teardown can lead to a use-after-free
> in usb_gadget_state_work(), as reported by KASAN:
>
> BUG: KASAN: invalid-access in sysfs_notify+0_x_2c/0_x_d0
> Workqueue: events usb_gadget_state_work
>
> The fundamental race occurs because a concurrent event (e.g., an
> interrupt) can call usb_gadget_set_state() and schedule gadget->work
> at any time during the cleanup process in usb_del_gadget().
>
> Commit 399a45e5237c ("usb: gadget: core: flush gadget workqueue after
> device removal") attempted to fix this by moving flush_work() to after
> device_del(). However, this does not fully solve the race, as a new
> work item can still be scheduled *after* flush_work() completes but
> before the gadget's memory is freed, leading to the same use-after-free.
>
> This patch fixes the race condition robustly by introducing a 'teardown'
> flag and a 'state_lock' spinlock to the usb_gadget struct. The flag is
> set during cleanup in usb_del_gadget() *before* calling flush_work() to
> prevent any new work from being scheduled once cleanup has commenced.
> The scheduling site, usb_gadget_set_state(), now checks this flag under
> the lock before queueing the work, thus safely closing the race window.
>
> Changes in v2:
> - Removed redundant inline comments as suggested by Alan Stern.
>
> Fixes: 5702f75375aa9 ("usb: gadget: udc-core: move sysfs_notify() to a workqueue")
> Signed-off-by: Jimmy Hu <hhhuuu@google.com>
> Cc: stable@vger.kernel.org
> ---
Reviewed-by: Alan Stern <stern@rowland.harvard.edu>
> drivers/usb/gadget/udc/core.c | 17 ++++++++++++++++-
> include/linux/usb/gadget.h | 5 +++++
> 2 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index d709e24c1fd4..66d2428835da 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
> @@ -1123,8 +1123,13 @@ static void usb_gadget_state_work(struct work_struct *work)
> void usb_gadget_set_state(struct usb_gadget *gadget,
> enum usb_device_state state)
> {
> + unsigned long flags;
> +
> + spin_lock_irqsave(&gadget->state_lock, flags);
> gadget->state = state;
> - schedule_work(&gadget->work);
> + if (!gadget->teardown)
> + schedule_work(&gadget->work);
> + spin_unlock_irqrestore(&gadget->state_lock, flags);
> }
> EXPORT_SYMBOL_GPL(usb_gadget_set_state);
>
> @@ -1357,6 +1362,8 @@ static void usb_udc_nop_release(struct device *dev)
> void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
> void (*release)(struct device *dev))
> {
> + spin_lock_init(&gadget->state_lock);
> + gadget->teardown = false;
> INIT_WORK(&gadget->work, usb_gadget_state_work);
> gadget->dev.parent = parent;
>
> @@ -1531,6 +1538,7 @@ EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
> void usb_del_gadget(struct usb_gadget *gadget)
> {
> struct usb_udc *udc = gadget->udc;
> + unsigned long flags;
>
> if (!udc)
> return;
> @@ -1544,6 +1552,13 @@ void usb_del_gadget(struct usb_gadget *gadget)
> kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
> sysfs_remove_link(&udc->dev.kobj, "gadget");
> device_del(&gadget->dev);
> + /*
> + * Set the teardown flag before flushing the work to prevent new work
> + * from being scheduled while we are cleaning up.
> + */
> + spin_lock_irqsave(&gadget->state_lock, flags);
> + gadget->teardown = true;
> + spin_unlock_irqrestore(&gadget->state_lock, flags);
> flush_work(&gadget->work);
> ida_free(&gadget_id_numbers, gadget->id_number);
> cancel_work_sync(&udc->vbus_work);
> diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
> index 0f28c5512fcb..8b5e593f7966 100644
> --- a/include/linux/usb/gadget.h
> +++ b/include/linux/usb/gadget.h
> @@ -351,6 +351,9 @@ struct usb_gadget_ops {
> * can handle. The UDC must support this and all slower speeds and lower
> * number of lanes.
> * @state: the state we are now (attached, suspended, configured, etc)
> + * @state_lock: Spinlock protecting the `state` and `teardown` members.
> + * @teardown: True if the device is undergoing teardown, used to prevent
> + * new work from being scheduled during cleanup.
> * @name: Identifies the controller hardware type. Used in diagnostics
> * and sometimes configuration.
> * @dev: Driver model state for this abstract device.
> @@ -426,6 +429,8 @@ struct usb_gadget {
> enum usb_ssp_rate max_ssp_rate;
>
> enum usb_device_state state;
> + spinlock_t state_lock;
> + bool teardown;
> const char *name;
> struct device dev;
> unsigned isoch_delay;
> --
> 2.51.0.760.g7b8bcc2412-goog
>
© 2016 - 2025 Red Hat, Inc.