thunderbolt: request work may outlive control channel teardown
Subsystems (2)
Hi,
I found a possible lifetime issue between asynchronous XDomain response work and control-channel teardown. The source path below is from Linux v7.4-rc1, commit 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4.
Source path
-----------
An incoming XDomain request is scheduled by tb_xdp_schedule_request() in drivers/thunderbolt/xdomain.c; its worker is tb_xdp_handle_request() at line 762. For example, the LINK_STATE_CHANGE_REQUEST case at lines 896-906 calls tb_xdp_link_state_change_response(), which sends the response through __tb_xdomain_response() (line 608).
__tb_xdomain_response() allocates a tb_cfg_request and calls tb_cfg_request() with no response buffer. In drivers/thunderbolt/ctl.c, tb_cfg_request() schedules req->work on the system workqueue at line 570. tb_cfg_request_work() at line 527 invokes the callback, then calls tb_cfg_request_dequeue() at line 534. Dequeue reads the raw req->ctl pointer and locks ctl->request_queue_lock at line 161.
The teardown path is separate. tb_domain_remove() in drivers/thunderbolt/domain.c:501 calls tb_ctl_stop() (ctl.c:750), which stops the rings, warns if requests remain, and reinitializes the request-list head. It does not cancel or synchronously drain every request work item on the system workqueue. tb_domain_remove() flushes tb->wq (domain.c:510), but the request worker uses the system workqueue. The domain release handler later calls tb_ctl_free() (domain.c:317-322, ctl.c:704), freeing the control channel. The host NHI PCI remove path reaches tb_domain_remove() through nhi_pci_do_remove() in drivers/thunderbolt/pci.c:594-619.
The request kref protects req; I do not see a request-held reference that pins req->ctl through dequeue. A delayed work item could therefore lock a freed control-channel mutex. The ctl.c and domain.c blobs at this baseline match upstream comparison snapshot 4dce97c6c265544342e716e62fd24bbb232590c5.
Reproduction
------------
The attached thunderbolt_test_injection.patch adds a test-only built-in hook. It creates a minimal tb_ctl and tb_cfg_request with the real allocator and enqueue helper, reinitializes the request-list head, optionally frees ctl, then directly invokes the real tb_cfg_request_work(). This forces the lifetime order; it does not call tb_ctl_stop() or tb_domain_remove(), queue the worker through the real system workqueue, send an XDomain packet, or use a physical Thunderbolt/USB4 controller.
The attached config.selected is the complete x86_64 configuration used; it has CONFIG_USB4=y, CONFIG_KASAN=y, and CONFIG_KASAN_GENERIC=y. With the attached patch and config, the build and QEMU steps are:
```sh git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux-tb-repro cd linux-tb-repro git checkout 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 git apply thunderbolt_test_injection.patch cp config.selected .config make ARCH=x86_64 olddefconfig make -j"$(nproc)" ARCH=x86_64 bzImage qemu-system-x86_64 -m 512M -smp 1 \ -kernel arch/x86/boot/bzImage -initrd initramfs.cpio.gz \ -append "console=ttyS0 rdinit=/init panic=-1 thunderbolt.test_request_uaf=1 thunderbolt.test_drop_ctl=1" \ -nographic -no-reboot ```
The attached initramfs.cpio.gz contains /init; the hook runs at late_initcall, before userspace, and the initramfs is only needed to complete guest boot. For the control run, repeat the QEMU command with thunderbolt.test_drop_ctl=0.
With test_drop_ctl=1, KASAN reports:
```text BUG: KASAN: slab-use-after-free in mutex_lock+0x74/0xd0 tb_cfg_request_dequeue+0x3e/0x1a0 Freed by kfree in tb_ctl_test_request_uaf_init() ```
With test_drop_ctl=0, the work runs before ctl is freed and the guest reports no KASAN issue. The attached baseline and control serial logs contain the complete output. These results establish the UAF for the forced ordering; they do not establish that a real peer or ordinary user can cause that order.
Could you confirm whether another lifetime or serialization guarantee covers this path, and whether a supported controller can reach this ordering during NHI removal? If so, what is the preferred way to drain or pin outstanding request work before freeing the control channel?
Best,
Bin
Attachments
-----------
- thunderbolt_test_injection.patch (test-only hook)
SHA-256: 77EBD027C6D35C3CF78897062237DB20678E642AB253EDA52064BCA101EC74B1
- config.selected
SHA-256: 6509D7B5631DC0282B9E3BD2BCC44ABB3C8517DE321EA4D895DFC62B5B38FFD3
- initramfs.cpio.gz
SHA-256: C7D2E02BACC581D762BA096ECA73A7C8D94F3EBD82C592D6637B00DE264B13E8
- baseline.serial.log
SHA-256: 132D84FDDCA7BE7E8FD31A02FC847E7950753F1FC7401AC19ECE02483E588B4F
- control.serial.log
SHA-256: 9D7C27E5D0F0B2E38A10155BD868DD323D75E02015EF453DAAAC6FFDEC290253
Hi,
Correction and a self-contained reproducer for my previous message: the previous message listed files as attachments, but the mail did not include those attachments. Sorry for the confusion. The test-only patch and exact reproduction steps are included below; no local files are required.
Baseline: Linux v7.4-rc1,6628e7d4c6b25c8d18f1345e3a20d21829aae7e.
Build an x86_64 KASAN kernel with the test hook:
```sh git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux-tb-repro cd linux-tb-repro git checkout 6628e7d4c6b25c8d18f1345e3a20d21829aae7e git apply thunderbolt_test_injection.patch make ARCH=x86_64 x86_64_defconfig scripts/config --enable USB4 --enable KASAN --enable KASAN_GENERIC scripts/config --enable SERIAL_8250 --enable SERIAL_8250_CONSOLE make ARCH=x86_64 olddefconfig make -j"$(nproc)" ARCH=x86_64 bzImage ```
The test is a late_initcall, so it runs before userspace. No initramfs is needed to trigger it. QEMU will later panic because this test boot has no root filesystem; that expected panic is after the test result. Run both modes:
```sh timeout 45s qemu-system-x86_64 -m 512M -smp 1 \ -kernel arch/x86/boot/bzImage \ -append "console=ttyS0 panic=-1 thunderbolt.test_request_uaf=1 thunderbolt.test_drop_ctl=1" \ -nographic -no-reboot timeout 45s qemu-system-x86_64 -m 512M -smp 1 \ -kernel arch/x86/boot/bzImage \ -append "console=ttyS0 panic=-1 thunderbolt.test_request_uaf=1 thunderbolt.test_drop_ctl=0" \ -nographic -no-reboot ```
The injected run reports BUG: KASAN: slab-use-after-free in mutex_lock() from tb_cfg_request_dequeue(). The control run reports no KASAN issue before the expected no-root-filesystem panic.
The hook directly resets the request-list head, optionally frees ctl, and calls the real tb_cfg_request_work() synchronously. It demonstrates the UAF if that order occurs; it does not call tb_ctl_stop()/tb_domain_remove(), use the system workqueue, send an XDomain packet, or prove a peer can trigger host NHI removal.
Test-only injection patch (not a proposed production fix):
--- BEGIN TEST-ONLY PATCH ---
drivers/thunderbolt/ctl.c +58 -1
@@ -9,5 +9,7 @@#include <linux/crc32.h> #include <linux/delay.h> +#include <linux/init.h> +#include <linux/moduleparam.h> #include <linux/slab.h> #include <linux/pci.h> #include <linux/dmapool.h>
@@ -535,7 +537,63 @@ static void tb_cfg_request_work(struct work_struct *work)tb_cfg_request_dequeue (req); } +static bool tb_ctl_test_request_uaf; +module_param_named (test_request_uaf, tb_ctl_test_request_uaf, bool,0400 ); +static bool tb_ctl_test_drop_ctl; +module_param_named (test_drop_ctl, tb_ctl_test_drop_ctl, bool,0400 ); + +static void tb_ctl_test_callback (void *data) +{ +} + +static int __init tb_ctl_test_request_uaf_init (void ) +{ +struct tb_ctl *ctl; +struct tb_cfg_request *req; +int ret; + +if (!tb_ctl_test_request_uaf) +return 0 ; + + ctl =kzalloc_obj (*ctl,GFP_KERNEL ); +if (!ctl) +return -ENOMEM ; + + req =tb_cfg_request_alloc (); +if (!req) { +kfree (ctl); +return -ENOMEM ; + } + +mutex_init (&ctl->request_queue_lock); +INIT_LIST_HEAD (&ctl->request_queue); + ctl->running = true; + req->callback = tb_ctl_test_callback; +INIT_WORK (&req->work, tb_cfg_request_work); +INIT_LIST_HEAD (&req->list); + + ret =tb_cfg_request_enqueue (ctl, req); +if (ret) { +tb_cfg_request_put (req); +kfree (ctl); +return ret; + } + +/* Model stop orphaning an active request before control-channel free. */ +INIT_LIST_HEAD (&ctl->request_queue); +if (tb_ctl_test_drop_ctl) +kfree (ctl); + +tb_cfg_request_work (&req->work); + +if (!tb_ctl_test_drop_ctl) +kfree (ctl); +tb_cfg_request_put (req); +return 0 ; +} +late_initcall (tb_ctl_test_request_uaf_init); +/** * tb_cfg_request() - Start control request not waiting for it to complete * @ctl: Control channel to use --- END TEST-ONLY PATCH ---
Best,
Bin
Hi,
Quoted 49 lines · Bin Luo
Hi,
I found a possible lifetime issue between asynchronous XDomain response work and control-channel teardown. The source path below is from Linux v7.4-rc1, commit
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4.Source path
-----------An incoming XDomain request is scheduled by
tb_xdp_schedule_request()indrivers/thunderbolt/xdomain.c; its worker istb_xdp_handle_request()at line 762. For example, theLINK_STATE_CHANGE_REQUESTcase at lines 896-906 callstb_xdp_link_state_change_response(), which sends the response through__tb_xdomain_response()(line 608).
__tb_xdomain_response()allocates atb_cfg_requestand callstb_cfg_request()with no response buffer. Indrivers/thunderbolt/ctl.c,tb_cfg_request()schedulesreq->workon the system workqueue at line 570.tb_cfg_request_work()at line 527 invokes the callback, then callstb_cfg_request_dequeue()at line 534. Dequeue reads the rawreq->ctlpointer and locksctl->request_queue_lockat line 161.The teardown path is separate.
tb_domain_remove()indrivers/thunderbolt/domain.c:501callstb_ctl_stop()(ctl.c:750), which stops the rings, warns if requests remain, and reinitializes the request-list head. It does not cancel or synchronously drain every request work item on the system workqueue.tb_domain_remove()flushestb->wq(domain.c:510), but the request worker uses the system workqueue. The domain release handler later callstb_ctl_free()(domain.c:317-322,ctl.c:704), freeing the control channel. The host NHI PCI remove path reachestb_domain_remove()throughnhi_pci_do_remove()indrivers/thunderbolt/pci.c:594-619.The request kref protects
req; I do not see a request-held reference that pinsreq->ctlthrough dequeue. A delayed work item could therefore lock a freed control-channel mutex. Thectl.canddomain.cblobs at this baseline match upstream comparison snapshot4dce97c6c265544342e716e62fd24bbb232590c5.Reproduction
------------The attached
thunderbolt_test_injection.patchadds a test-only built-in hook. It creates a minimaltb_ctlandtb_cfg_requestwith the real allocator and enqueue helper, reinitializes the request-list head, optionally freesctl, then directly invokes the realtb_cfg_request_work(). This forces the lifetime order; it does not calltb_ctl_stop()ortb_domain_remove(), queue the worker through the real system workqueue, send an XDomain packet, or use a physical Thunderbolt/USB4 controller.
Quoted 3 lines
The attached
config.selectedis the completex86_64configuration used; it hasCONFIG_USB4=y,CONFIG_KASAN=y, andCONFIG_KASAN_GENERIC=y. With the attached patch and config, the build and QEMU steps are:
Thank you for the report!
Can you provide the exact steps we can run on real hardware to reproduce this?
Like:
1. Boot the system, nothing connected.
2. Connect device router X to port Y.
3. Verify USB4 link via ..
...