Skip to content

Commit 5028433

Browse files
committed
thunderbolt: Prevent use-after-free in resume from hibernate
Kenneth noticed that his laptop crashes randomly when resuming from hibernate if there is device connected and display tunneled. I was able to reproduce this as well with the following steps: 1. Boot the system up, nothing connected. 2. Connect Thunderbolt 4 dock to the host. 3. Connect monitor to the Thunderbolt 4 dock. 4. Verify that there is picture on the screen. 5. Enter hibernate. 6. Exit hibernate. 7. Wait for the system to resume. Expectation: System resumes just fine, the connected monitor still shows screen. Actual result: There is crash during resume, screen is blank. What happens is that during resume from hibernate we tear down any existing tunnels created by the boot kernel and this ends up calling tb_dp_dprx_stop() which calls tb_tunnel_put() dropping the reference count to zero even though we never called tb_dp_dprx_start() for it (we never do that for discovery). This makes the discovered DP tunnel memory to be released and any access after that causes use-after-free and possible crash. Fix this so that we only stop DPRX flow if it has been started in the first place. Reported-by: Kenneth Crudup <[email protected]> Closes: https://lore.kernel.org/linux-usb/[email protected]/ Cc: [email protected] Fixes: d6d458d ("thunderbolt: Handle DisplayPort tunnel activation asynchronously") Reviewed-by: Yehezkel Bernat <[email protected]> Signed-off-by: Mika Westerberg <[email protected]>
1 parent 7eb1721 commit 5028433

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

drivers/thunderbolt/tunnel.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,8 @@ static int tb_dp_dprx_start(struct tb_tunnel *tunnel)
10091009
*/
10101010
tb_tunnel_get(tunnel);
10111011

1012+
tunnel->dprx_started = true;
1013+
10121014
if (tunnel->callback) {
10131015
tunnel->dprx_timeout = dprx_timeout_to_ktime(dprx_timeout);
10141016
queue_delayed_work(tunnel->tb->wq, &tunnel->dprx_work, 0);
@@ -1021,9 +1023,12 @@ static int tb_dp_dprx_start(struct tb_tunnel *tunnel)
10211023

10221024
static void tb_dp_dprx_stop(struct tb_tunnel *tunnel)
10231025
{
1024-
tunnel->dprx_canceled = true;
1025-
cancel_delayed_work(&tunnel->dprx_work);
1026-
tb_tunnel_put(tunnel);
1026+
if (tunnel->dprx_started) {
1027+
tunnel->dprx_started = false;
1028+
tunnel->dprx_canceled = true;
1029+
cancel_delayed_work(&tunnel->dprx_work);
1030+
tb_tunnel_put(tunnel);
1031+
}
10271032
}
10281033

10291034
static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)

drivers/thunderbolt/tunnel.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ enum tb_tunnel_state {
6363
* @allocated_down: Allocated downstream bandwidth (only for USB3)
6464
* @bw_mode: DP bandwidth allocation mode registers can be used to
6565
* determine consumed and allocated bandwidth
66+
* @dprx_started: DPRX negotiation was started (tb_dp_dprx_start() was called for it)
6667
* @dprx_canceled: Was DPRX capabilities read poll canceled
6768
* @dprx_timeout: If set DPRX capabilities read poll work will timeout after this passes
6869
* @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read
@@ -100,6 +101,7 @@ struct tb_tunnel {
100101
int allocated_up;
101102
int allocated_down;
102103
bool bw_mode;
104+
bool dprx_started;
103105
bool dprx_canceled;
104106
ktime_t dprx_timeout;
105107
struct delayed_work dprx_work;

0 commit comments

Comments
 (0)