From: Bin Luo <luob87709@gmail.com>
To: linux-usb@vger.kernel.org
Cc: andreas.noever@gmail.com, westeri@kernel.org, YehezkelShB@gmail.com
Subject: [BUG] thunderbolt: request work may outlive control channel teardown
Date: Wed, 25 Nov 2026 21:00:00 +0800 [thread overview]
Message-ID: <20261125130000.475-1-luob87709@gmail.com> (raw)
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`
next reply other threads:[~2026-11-25 13:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 13:00 Bin Luo [this message]
2026-11-25 13:06 ` [BUG] thunderbolt: request work may outlive control channel teardown Bin Luo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125130000.475-1-luob87709@gmail.com \
--to=luob87709@gmail.com \
--cc=YehezkelShB@gmail.com \
--cc=andreas.noever@gmail.com \
--cc=linux-usb@vger.kernel.org \
--cc=westeri@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox