usbip: vudc: Fix use after free bug in vudc_remove due to race condition
Subsystems (2)
PATCH v3 1/1 usbip: vudc: Fix use after free bug in vudc_remove due to race condition
In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.
When it calls usbip_sockfd_store, it will call v_start_timer to start the timer work.
When we call vudc_remove to remove the driver, theremay be a sequence as follows:
Fix it by shutdown the timer work before cleanup in vudc_remove.
Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove |
kfree(udc); |
//free shost |
|udc->gadget
|//useThe udc might be removed before v_timer finished, and UAF happens.
This bug was found by Codeql static analysis and might by false positive.
---
v3:
- fix the issue by adding del_timer_sync in v_stop_timer and invoke it in vudc_remove
v2:
- add more details about how the bug was found suggested by Shuah
--- drivers/usb/usbip/vudc_dev.c | 1 + drivers/usb/usbip/vudc_transfer.c | 2 ++ 2 files changed, 3 insertions(+)
drivers/usb/usbip/vudc_dev.c +1 -0
@@ -633,6 +633,7 @@ int vudc_remove(struct platform_device *pdev) {struct vudc *udc =platform_get_drvdata (pdev); +v_stop_timer (udc);usb_del_gadget_udc (&udc->gadget);cleanup_vudc_hw (udc);kfree (udc);
drivers/usb/usbip/vudc_transfer.c +2 -0
@@ -492,5 +492,7 @@ void v_stop_timer(struct vudc *udc)/* timer itself will take care of stopping */ dev_dbg (&udc->pdev->dev,"timer stop" ); + +del_timer_sync (&t->timer); t->state =VUDC_TR_STOPPED ; }
Signature · 1 line
2.25.1
Shuah Khan
maintainerQuoted 23 lines · Zheng Wang
In
vudc_probe, it callsinit_vudc_hw, which bound&udc->timerwithv_timer.When it calls
usbip_sockfd_store, it will callv_start_timerto start the timer work.When we call
vudc_removeto remove the driver, theremay be a sequence as follows:Fix it by shutdown the timer work before cleanup in
vudc_remove.Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove | kfree(udc); | //free shost | |udc->gadget |//use
Quoted 3 lines
The udc might be removed before
v_timerfinished, and UAF happens.This bug was found by Codeql static analysis and might by false positive.
This statement that this could be a false positive makes me hesitate taking this patch.
What kind of testing have you done with this fix? Were you able to test the scenario of unplugging usb?
thanks,
-- Shuah
Quoted 27 lines · Shuah Khan <[email protected]> 于2023年3月18日周六 06:53写道:
Quoted 25 lines · Zheng Wang
In
vudc_probe, it callsinit_vudc_hw, which bound&udc->timerwithv_timer.When it calls
usbip_sockfd_store, it will callv_start_timerto start the timer work.When we call
vudc_removeto remove the driver, theremay be a sequence as follows:Fix it by shutdown the timer work before cleanup in
vudc_remove.Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove | kfree(udc); | //free shost | |udc->gadget |//useThe udc might be removed before
v_timerfinished, and UAF happens.
Quoted 8 lines
Quoted 1 line
This bug was found by Codeql static analysis and might by false positive.
This statement that this could be a false positive makes me hesitate taking this patch.
What kind of testing have you done with this fix? Were you able to test the scenario of unplugging usb?
Sorry I did't make a full test for I did't have the device. The attacking scenario if based on other cases.
Best regads,
Zheng
Quoted 2 lines
thanks,
-- Shuah
Friendly ping about the issue.
Sorry that I couldn't make test about the driver.
Thanks,
Zheng
Quoted 46 lines · Zheng Hacker <[email protected]> 于2023年3月18日周六 15:39写道:
Quoted 27 lines · Shuah Khan <[email protected]> 于2023年3月18日周六 06:53写道:
Quoted 25 lines · Zheng Wang
In
vudc_probe, it callsinit_vudc_hw, which bound&udc->timerwithv_timer.When it calls
usbip_sockfd_store, it will callv_start_timerto start the timer work.When we call
vudc_removeto remove the driver, theremay be a sequence as follows:Fix it by shutdown the timer work before cleanup in
vudc_remove.Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove | kfree(udc); | //free shost | |udc->gadget |//useThe udc might be removed before
v_timerfinished, and UAF happens.Quoted 8 lines
Quoted 1 line
This bug was found by Codeql static analysis and might by false positive.
This statement that this could be a false positive makes me hesitate taking this patch.
What kind of testing have you done with this fix? Were you able to test the scenario of unplugging usb?
Sorry I did't make a full test for I did't have the device. The attacking scenario if based on other cases.
Best regads,
ZhengQuoted 2 lines
thanks,
-- Shuah
Shuah Khan
maintainerQuoted 48 lines · Zheng Hacker
Friendly ping about the issue.
Sorry that I couldn't make test about the driver.Thanks,
ZhengQuoted 41 lines · Zheng Hacker <[email protected]> 于2023年3月18日周六 15:39写道:
Quoted 27 lines · Shuah Khan <[email protected]> 于2023年3月18日周六 06:53写道:
Quoted 25 lines · Zheng Wang
In
vudc_probe, it callsinit_vudc_hw, which bound&udc->timerwithv_timer.When it calls
usbip_sockfd_store, it will callv_start_timerto start the timer work.When we call
vudc_removeto remove the driver, theremay be a sequence as follows:Fix it by shutdown the timer work before cleanup in
vudc_remove.Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove | kfree(udc); | //free shost | |udc->gadget |//useThe udc might be removed before
v_timerfinished, and UAF happens.Quoted 8 lines
Quoted 1 line
This bug was found by Codeql static analysis and might by false positive.
This statement that this could be a false positive makes me hesitate taking this patch.
What kind of testing have you done with this fix? Were you able to test the scenario of unplugging usb?
Sorry I did't make a full test for I did't have the device. The attacking scenario if based on other cases.
Sorry. I really need for you test this and provide information on how it was tested.
thanks,
-- Shuah
Hi Shuah,
I got it. I'll try it.
Best regards,
Zheng
Quoted 57 lines · Shuah Khan <[email protected]> 于2023年4月14日周五 02:01写道:
Quoted 48 lines · Zheng Hacker
Friendly ping about the issue.
Sorry that I couldn't make test about the driver.Thanks,
ZhengQuoted 41 lines · Zheng Hacker <[email protected]> 于2023年3月18日周六 15:39写道:
Quoted 27 lines · Shuah Khan <[email protected]> 于2023年3月18日周六 06:53写道:
Quoted 25 lines · Zheng Wang
In
vudc_probe, it callsinit_vudc_hw, which bound&udc->timerwithv_timer.When it calls
usbip_sockfd_store, it will callv_start_timerto start the timer work.When we call
vudc_removeto remove the driver, theremay be a sequence as follows:Fix it by shutdown the timer work before cleanup in
vudc_remove.Note that removing a driver is a root-only operation, and should never happen. But the attacker can directly unplug the usb to trigger the remove function.
CPU0 CPU1
|v_timer
vudc_remove | kfree(udc); | //free shost | |udc->gadget |//useThe udc might be removed before
v_timerfinished, and UAF happens.Quoted 8 lines
Quoted 1 line
This bug was found by Codeql static analysis and might by false positive.
This statement that this could be a false positive makes me hesitate taking this patch.
What kind of testing have you done with this fix? Were you able to test the scenario of unplugging usb?
Sorry I did't make a full test for I did't have the device. The attacking scenario if based on other cases.
Sorry. I really need for you test this and provide information on how it was tested.
thanks,
-- Shuah
PATCH 1/1 usbip: vudc: Fix use after free bug in vudc_remove due to race condition
In reply to Zheng WangThis patch follows up Zheng Wang's 2023 report of a use-after-free in vudc_remove(). The original thread stalled on Shuah Khan's request for runtime testing of the unplug/unbind path. This patch supplies that testing and keeps Zheng's original fix shape.
In vudc_probe(), v_init_timer() binds udc->tr_timer.timer to v_timer(). usbip_sockfd_store() starts the timer via v_start_timer()/v_kick_timer(). vudc_remove() can then free the containing struct vudc while the timer is still pending or executing.
KASAN confirms the race on an unpatched x86_64 QEMU guest with CONFIG_KASAN=y, CONFIG_USBIP_VUDC=y, CONFIG_USB_ZERO=y, and a tight loop that repeatedly writes a socket fd to usbip_sockfd, closes the socket pair, and unbinds/rebinds usbip-vudc.0:
BUG: KASAN: slab-use-after-free in __run_timer_base.part.0+0x8ba/0x8e0
Write of size 8 at addr ffff888001b80740 by task trigger_and_unb/239
Allocated by task 239:
vudc_probe+0x4d/0xaa0
Freed by task 239:
kfree+0x18f/0x520
device_release_driver_internal+0x388/0x540
unbind_store+0xd9/0x100This lands in the timer core rather than v_timer() itself because the embedded timer_list is being walked after its containing struct vudc has already been freed. The underlying lifetime bug is the same one Zheng reported.
With v_stop_timer() called from vudc_remove() and the timer deleted synchronously, the same harness completed 5000 bind/unbind iterations with no KASAN report.
--- drivers/usb/usbip/vudc_dev.c | 1 + drivers/usb/usbip/vudc_transfer.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/usb/usbip/vudc_dev.c +1 -0
@@ -632,6 +632,7 @@ void vudc_remove(struct platform_device *pdev) {struct vudc *udc =platform_get_drvdata (pdev); +v_stop_timer (udc);usb_del_gadget_udc (&udc->gadget);cleanup_vudc_hw (udc);kfree (udc);
drivers/usb/usbip/vudc_transfer.c +2 -1
@@ -490,7 +490,8 @@ void v_stop_timer(struct vudc *udc) {struct transfer_timer *t = &udc->tr_timer; -/* timer itself will take care of stopping */ +/* 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 ; }
Signature · 1 line
2.53.0
Quoted 59 lines · Michael Bommarito
This patch follows up Zheng Wang's 2023 report of a use-after-free in
vudc_remove(). The original thread stalled on Shuah Khan's request for runtime testing of the unplug/unbind path. This patch supplies that testing and keeps Zheng's original fix shape.In
vudc_probe(),v_init_timer()bindsudc->tr_timer.timertov_timer().usbip_sockfd_store()starts the timer via v_start_timer()/v_kick_timer().vudc_remove()can then free the containing struct vudc while the timer is still pending or executing.KASAN confirms the race on an unpatched
x86_64QEMU guest withCONFIG_KASAN=y,CONFIG_USBIP_VUDC=y,CONFIG_USB_ZERO=y, and a tight loop that repeatedly writes a socket fd tousbip_sockfd, closes the socket pair, and unbinds/rebinds usbip-vudc.0:BUG: KASAN: slab-use-after-free in __run_timer_base.part.0+0x8ba/0x8e0 Write of size 8 at addr ffff888001b80740 by task trigger_and_unb/239 Allocated by task 239: vudc_probe+0x4d/0xaa0 Freed by task 239: kfree+0x18f/0x520 device_release_driver_internal+0x388/0x540 unbind_store+0xd9/0x100This lands in the timer core rather than
v_timer()itself because the embeddedtimer_listis being walked after its containing struct vudc has already been freed. The underlying lifetime bug is the same one Zheng reported.With
v_stop_timer()called fromvudc_remove()and the timer deleted synchronously, the same harness completed 5000 bind/unbind iterations with no KASAN report.Fixes b6a0ca111867 ("usbip: vudc: Add UDC specific ops")Reported-by Zheng Wang <[email protected]>Signed-off-by Michael Bommarito <[email protected]>--- drivers/usb/usbip/vudc_dev.c | 1 + drivers/usb/usbip/vudc_transfer.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)drivers/usb/usbip/vudc_dev.c +1
-0@@ -632,6 +632,7 @@ void vudc_remove(struct platform_device *pdev) {struct vudc *udc =platform_get_drvdata (pdev); +v_stop_timer (udc);usb_del_gadget_udc (&udc->gadget);cleanup_vudc_hw (udc);kfree (udc);drivers/usb/usbip/vudc_transfer.c +0
-0
drivers/usb/usbip/vudc_transfer.c void v_stop_timer(struct vudc *udc) · line 497
Quoted 9 lines
drivers/usb/usbip/vudc_transfer.c +2
-1@@ -490,7 +490,8 @@ void v_stop_timer(struct vudc *udc) {struct transfer_timer *t = &udc->tr_timer; -/* timer itself will take care of stopping */ +/* 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 ; }
Looks good to me.
thanks,
-- Shuah