Linux USB
 help / color / mirror / Atom feed
From: Bin Luo <luob87709@gmail.com>
To: linux-usb@vger.kernel.org
Cc: andreas.noever@gmail.com, westeri@kernel.org, YehezkelShB@gmail.com
Subject: Re: [BUG] thunderbolt: request work may outlive control channel teardown
Date: Wed, 25 Nov 2026 21:06:54 +0800	[thread overview]
Message-ID: <20261125130654.1449-1-luob87709@gmail.com> (raw)
In-Reply-To: <20261125130000.475-1-luob87709@gmail.com>

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 ---
diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
index 965988b1860..000000000 100644
--- a/drivers/thunderbolt/ctl.c
+++ b/drivers/thunderbolt/ctl.c
@@ -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

      reply	other threads:[~2026-11-25 13:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 13:00 [BUG] thunderbolt: request work may outlive control channel teardown Bin Luo
2026-11-25 13:06 ` Bin Luo [this message]

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=20261125130654.1449-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