Skip to contentSkip to conversation

usbip: vudc: Prevent transfer timer rearm during teardown

PATCH Superseded Myeonghun Pak 3 participants 4 messages Sep 21 – Sep 23

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usbip: vudc: Prevent transfer timer rearm during teardown

Commit d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition") made v_stop_timer() delete the transfer timer synchronously before vudc_remove() frees its containing struct vudc.

That does not close the race with the receive thread, which is stopped later through usb_del_gadget_udc() and vudc_shutdown(). The thread calls v_kick_timer() for CMD_SUBMIT and CMD_UNLINK packets, and v_kick_timer() deliberately calls mod_timer() when the transfer state is VUDC_TR_STOPPED. A packet received after v_stop_timer() returns can therefore enqueue the timer again before the vudc is freed.

timer_delete_sync() only drains the current timer instance and cannot prevent such a rearm. Use timer_shutdown_sync(), which makes subsequent attempts to arm the timer no-ops. The transfer state no longer needs to be updated, as it is freed along with the timer.

On a KASAN and DEBUG_OBJECTS enabled x86_64 QEMU guest, repeatedly binding the device, submitting a request and unbinding it produced a "free active timer_list" warning followed by a slab-use-after-free in v_timer(). With this change the same harness completed 4000 iterations cleanly.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Fixes d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition")
Assisted-by LLM
---
 drivers/usb/usbip/vudc_transfer.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/usb/usbip/vudc_transfer.c +2 -4
index d4ce85c4c6a2..4146b746a320 100644
@@ -441,7 +441,7 @@ static void v_timer(struct timer_list *t)
 	spin_unlock_irqrestore(&udc->lock, flags);
 }
 
-/* All timer functions are run with udc->lock held */
+/* v_start_timer() and v_kick_timer() are called with udc->lock held. */
 
 void v_init_timer(struct vudc *udc)
 {
@@ -490,8 +490,6 @@ void v_stop_timer(struct vudc *udc)
 {
 	struct transfer_timer *t = &udc->tr_timer;
 
-	/* Delete the timer synchronously before teardown frees udc. */
 	dev_dbg(&udc->pdev->dev, "timer stop");
-	timer_delete_sync(&t->timer);
-	t->state = VUDC_TR_STOPPED;
+	timer_shutdown_sync(&t->timer);
 }

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5

Signature · 1 line
2.53.0
#1 lore raw
Quoted 42 lines · Myeonghun Pak

Commit d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition") made v_stop_timer() delete the transfer timer synchronously before vudc_remove() frees its containing struct vudc.

That does not close the race with the receive thread, which is stopped later through usb_del_gadget_udc() and vudc_shutdown(). The thread calls v_kick_timer() for CMD_SUBMIT and CMD_UNLINK packets, and v_kick_timer() deliberately calls mod_timer() when the transfer state is VUDC_TR_STOPPED. A packet received after v_stop_timer() returns can therefore enqueue the timer again before the vudc is freed.

timer_delete_sync() only drains the current timer instance and cannot prevent such a rearm. Use timer_shutdown_sync(), which makes subsequent attempts to arm the timer no-ops. The transfer state no longer needs to be updated, as it is freed along with the timer.

On a KASAN and DEBUG_OBJECTS enabled x86_64 QEMU guest, repeatedly binding the device, submitting a request and unbinding it produced a "free active timer_list" warning followed by a slab-use-after-free in v_timer(). With this change the same harness completed 4000 iterations cleanly.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Fixes d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition")
Assisted-by LLM
---
 drivers/usb/usbip/vudc_transfer.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/usb/usbip/vudc_transfer.c +0 -0
index d4ce85c4c6a2..4146b746a320 100644

drivers/usb/usbip/vudc_transfer.c static void v_timer(struct timer_list *t) · line 444

Quoted 5 lines
drivers/usb/usbip/vudc_transfer.c +1 -1
@@ -441,7 +441,7 @@ static void v_timer(struct timer_list *t)
 	spin_unlock_irqrestore(&udc->lock, flags);
 }
 
-/* All timer functions are run with udc->lock held */
+/* v_start_timer() and v_kick_timer() are called with udc->lock held. */

If this is true, use the correct documentation/metadata style for this, so that checkers can catch this. A comment like this will not work.

thanks,

greg k-h

maintainer
#2 lore raw
In reply to Myeonghun Pak
Quoted 17 lines · Myeonghun Pak

Commit d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition") made v_stop_timer() delete the transfer timer synchronously before vudc_remove() frees its containing struct vudc.

That does not close the race with the receive thread, which is stopped later through usb_del_gadget_udc() and vudc_shutdown(). The thread calls v_kick_timer() for CMD_SUBMIT and CMD_UNLINK packets, and v_kick_timer() deliberately calls mod_timer() when the transfer state is VUDC_TR_STOPPED. A packet received after v_stop_timer() returns can therefore enqueue the timer again before the vudc is freed.

timer_delete_sync() only drains the current timer instance and cannot prevent such a rearm. Use timer_shutdown_sync(), which makes subsequent attempts to arm the timer no-ops. The transfer state no longer needs to be updated, as it is freed along with the timer.

Quoted 8 lines

On a KASAN and DEBUG_OBJECTS enabled x86_64 QEMU guest, repeatedly binding the device, submitting a request and unbinding it produced a "free active timer_list" warning followed by a slab-use-after-free in v_timer(). With this change the same harness completed 4000 iterations cleanly.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Were you able to reproduce this problem to test and verify that this patch fixes it?

Quoted 36 lines
Fixes d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition")
Assisted-by LLM
---
  drivers/usb/usbip/vudc_transfer.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
drivers/usb/usbip/vudc_transfer.c +2 -4
index d4ce85c4c6a2..4146b746a320 100644
@@ -441,7 +441,7 @@ static void v_timer(struct timer_list *t)
  	spin_unlock_irqrestore(&udc->lock, flags);
  }
 
-/* All timer functions are run with udc->lock held */
+/* v_start_timer() and v_kick_timer() are called with udc->lock held. */
 
  void v_init_timer(struct vudc *udc)
  {
@@ -490,8 +490,6 @@ void v_stop_timer(struct vudc *udc)
  {
  	struct transfer_timer *t = &udc->tr_timer;
 
-	/* Delete the timer synchronously before teardown frees udc. */
  	dev_dbg(&udc->pdev->dev, "timer stop");
-	timer_delete_sync(&t->timer);
-	t->state = VUDC_TR_STOPPED;
+	timer_shutdown_sync(&t->timer);
  }
Quoted 1 line

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5

thanks,
-- Shuah

#3 lore raw

Yes. On an x86_64 QEMU guest running v7.2 with KASAN and DEBUG_OBJECTS_TIMERS, the harness binds usbip-vudc.0, sends USBIP_CMD_SUBMIT while unbinding it, and repeats.

Without this change that hits the free-active timer warning and a KASAN use-after-free in v_timer(). With timer_shutdown_sync(), the same harness completed 4000 iterations with no KASAN, ODEBUG, or WARNING. v2 does not change that timer fix.

thanks,

Myeonghun Pak

2026년 9월 22일 (화) 오전 5:47, Shuah Khan <[email protected]>님이 작성:

Quoted 72 lines · Myeonghun Pak
Quoted 17 lines

Commit d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition") made v_stop_timer() delete the transfer timer synchronously before vudc_remove() frees its containing struct vudc.

That does not close the race with the receive thread, which is stopped later through usb_del_gadget_udc() and vudc_shutdown(). The thread calls v_kick_timer() for CMD_SUBMIT and CMD_UNLINK packets, and v_kick_timer() deliberately calls mod_timer() when the transfer state is VUDC_TR_STOPPED. A packet received after v_stop_timer() returns can therefore enqueue the timer again before the vudc is freed.

timer_delete_sync() only drains the current timer instance and cannot prevent such a rearm. Use timer_shutdown_sync(), which makes subsequent attempts to arm the timer no-ops. The transfer state no longer needs to be updated, as it is freed along with the timer.

Quoted 8 lines

On a KASAN and DEBUG_OBJECTS enabled x86_64 QEMU guest, repeatedly binding the device, submitting a request and unbinding it produced a "free active timer_list" warning followed by a slab-use-after-free in v_timer(). With this change the same harness completed 4000 iterations cleanly.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Were you able to reproduce this problem to test and verify that this patch fixes it?

Quoted 36 lines
Fixes d96209626a29 ("usbip: vudc: Fix use after free bug in vudc_remove due to race condition")
Assisted-by LLM
---
  drivers/usb/usbip/vudc_transfer.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
drivers/usb/usbip/vudc_transfer.c +2 -4
index d4ce85c4c6a2..4146b746a320 100644
@@ -441,7 +441,7 @@ static void v_timer(struct timer_list *t)
      spin_unlock_irqrestore(&udc->lock, flags);
  }
 
-/* All timer functions are run with udc->lock held */
+/* v_start_timer() and v_kick_timer() are called with udc->lock held. */
 
  void v_init_timer(struct vudc *udc)
  {
@@ -490,8 +490,6 @@ void v_stop_timer(struct vudc *udc)
  {
      struct transfer_timer *t = &udc->tr_timer;
 
-     /* Delete the timer synchronously before teardown frees udc. */
      dev_dbg(&udc->pdev->dev, "timer stop");
-     timer_delete_sync(&t->timer);
-     t->state = VUDC_TR_STOPPED;
+     timer_shutdown_sync(&t->timer);
  }
Quoted 1 line

base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5

thanks,
-- Shuah