Linux USB
 help / color / mirror / Atom feed
* [PATCH] thunderbolt: fix NULL dereference in tb_remove_work()
@ 2026-08-31  7:58 Fedor Pchelkin
  2026-09-02  7:10 ` Mika Westerberg
  0 siblings, 1 reply; 2+ messages in thread
From: Fedor Pchelkin @ 2026-08-31  7:58 UTC (permalink / raw)
  To: Mika Westerberg, linux-usb
  Cc: Fedor Pchelkin, Andreas Noever, Yehezkel Bernat, linux-kernel,
	lvc-project, stable

There is a slight race between tb_remove_work() and tb_domain_remove()
which leads to dereferencing a NULL tb->root_switch pointer inside
tb_free_unplugged_xdomains():

        Thread A                            Thread B

  tb_remove_work()
                                    tb_domain_remove()
                                      mutex_lock(&tb->lock)
                                      tb_stop()
                                        /* doesn't cancel a running callback */
                                        cancel_delayed_work(&tcm->remove_work)
                                        ...
                                        tb_switch_remove(tb->root_switch)
                                        tb->root_switch = NULL
                                      mutex_unlock(&tb->lock)
    mutex_lock(&tb->lock)
    ...
    /* without checking ->root_switch */
    tb_free_unplugged_xdomains(tb->root_switch)
    mutex_unlock(&tb->lock)

Commit a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without
holding tb->lock") doesn't seem right to move tb_free_unplugged_xdomains()
out of the &tb->lock section and the check for tb->root_switch, in
particular.  It states:

  For this reason separate removing the XDomain from the topology data
  structures (where we need the lock) from unregistering the device from
  the bus (where remove callbacks of the drivers are being called).

tb_free_unplugged_xdomains() belongs to the former group of functions
requiring the lock.  And it also calls tb_xdomain_remove() which should
only be called with &tb->lock held.

Found by Linux Verification Center (linuxtesting.org) with Svace static
analysis tool.

Fixes: a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without holding tb->lock")
Cc: stable@vger.kernel.org
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
---

Reproducedd under QEMU with a passthrough'ed USB4/Thunderbolt NHI
controller.  Not sure if this trace gives something extra-helpful for the
commit message, I'll just place it here for now.

Oops: general protection fault, probably for non-canonical address 0xdffffc000000009b: 0000 [#1] SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x00000000000004d8-0x00000000000004df]
CPU: 7 UID: 0 PID: 866 Comm: kworker/u32:50 Not tainted 7.2.0+ #16 PREEMPT(full)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-9.fc43 06/10/2025
Workqueue: thunderbolt0 tb_remove_work
RIP: 0010:tb_free_unplugged_xdomains+0x39/0x4e0
Call Trace:
 <TASK>
 process_one_work+0xa3e/0x1a10
 worker_thread+0x70c/0x1040
 kthread+0x3de/0x4e0
 ret_from_fork+0x6c2/0xb00
 ret_from_fork_asm+0x1a/0x30
 </TASK>
Modules linked in:
RIP: 0010:tb_free_unplugged_xdomains+0x39/0x4e0

 drivers/thunderbolt/tb.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 47753a5c0f2e..f2e53feff5a6 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -3274,11 +3274,11 @@ static void tb_remove_work(struct work_struct *work)
 	struct tb *tb = tcm_to_tb(tcm);
 
 	mutex_lock(&tb->lock);
-	if (tb->root_switch)
+	if (tb->root_switch) {
 		tb_free_unplugged_children(tb->root_switch);
+		tb_free_unplugged_xdomains(tb->root_switch);
+	}
 	mutex_unlock(&tb->lock);
-
-	tb_free_unplugged_xdomains(tb->root_switch);
 }
 
 static int tb_runtime_resume(struct tb *tb)
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-02  7:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31  7:58 [PATCH] thunderbolt: fix NULL dereference in tb_remove_work() Fedor Pchelkin
2026-09-02  7:10 ` Mika Westerberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox