From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0859F3B4EA9 for ; Mon, 27 Jul 2026 15:41:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785166882; cv=none; b=NMAhr4+wzb6bB+gfN+xeA71vBBaK1VqKJ+4cTYYKToAizTR/7renJJZVtjOPIzhc4S83KDy3tB1F+9ttfHRCnJyRP9UhzozkFoj7m15ZDstIleRcg3cB8G8P6U62F3AmXB70i9jjpEZXGk3aFdaEwe6B6dxorM0R6FsTteH0oFE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785166882; c=relaxed/simple; bh=u6deszz0S4KrzhoUTHcCD3PRgOooHRZv1LABpni+xgw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lac4foNHU/Fft3D14O8Fz3PD7USe5LqWAmNxPXBu0bXCPvUxkqK7g92+p8QJHQ9gVUOyTCEKlTCfu/2iU1l60gu3qr0Jdg1+190FH9gPtvv7qn4c8yspoSglDxz5R53H1vj43iJVhi9YLdtpj7/NNhM/STL9jfxxS8ZJxLVlkrc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BNTSHMzT; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BNTSHMzT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 734301F00ACF; Mon, 27 Jul 2026 15:41:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785166880; bh=mCJ+iN0R6KbpnAQY1EAKSIanHIS9ymhk3YxVSIPyDZ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BNTSHMzTf7Kqcj/3R+lZ76RJ3qPkctPEx3/xP4/YbgJ40RLL1DzcnXT4oxAdjks1A GemHMEJn54RNoXcYqczI6xW/HzXwGM3fKpEwTxTJoCSf65oOAXuBs3Ig19EeSUXyH8 kCGT0ghR+dzeoDtEZD/grBk0yZWOsfIlVaLrGKc8KpQZL5DpfcyZfyDcXGvnq0WL6E AM4vd4EsX+QC+o0bAy4+l5wpBqJr0Au4wBpn7neHvwR2rV79z4qihfpTRZl6nZf+xL nDupFqwrdj8yo7r/NZTzRYIiqnXMtUQu0azfLWt7pffLb0kkjTzNGT1ZvQbdXvpDfW eM57s79686J3A== From: Sasha Levin To: stable@vger.kernel.org Cc: Michael Bommarito , Mika Westerberg , Sasha Levin Subject: [PATCH 5.15.y 6/6] thunderbolt: Prevent XDomain delayed work use-after-free on disconnect Date: Mon, 27 Jul 2026 11:41:15 -0400 Message-ID: <20260727154115.1516559-6-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260727154115.1516559-1-sashal@kernel.org> References: <2026072001-numeric-suffice-a245@gregkh> <20260727154115.1516559-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Michael Bommarito [ Upstream commit 2c5d2d3c3f70cde2565d7b279b544893a2035842 ] tb_xdp_handle_request() runs on system_wq and queues xd->state_work via queue_delayed_work() in three request handlers: PROPERTIES_CHANGED_REQUEST, UUID_REQUEST (via start_handshake), and LINK_STATE_CHANGE_REQUEST. Similarly, update_xdomain() queues xd->properties_changed_work when local properties change. Concurrently, tb_xdomain_remove() calls stop_handshake() which does cancel_delayed_work_sync() on both delayed works. Later, tb_xdomain_unregister() calls device_unregister() which eventually frees the xdomain. Since commit 559c1e1e0134 ("thunderbolt: Run tb_xdp_handle_request() in system workqueue") moved the request handler off tb->wq, the handler and the remove path are no longer serialized. If queue_delayed_work() executes after cancel_delayed_work_sync() but before the xdomain is freed, the delayed work fires on a freed object. Add xd->removing that tb_xdomain_remove() sets under xd->lock before calling stop_handshake(). Each external queue site holds the same lock and checks removing before calling queue_delayed_work(). This provides the mutual exclusion needed: either the queue site acquires the lock first and queues work that the subsequent cancel will see, or the remove path acquires the lock first and the queue site observes removing == true and skips the queue. Fixes: 559c1e1e0134 ("thunderbolt: Run tb_xdp_handle_request() in system workqueue") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Michael Bommarito Signed-off-by: Mika Westerberg Signed-off-by: Sasha Levin --- drivers/thunderbolt/xdomain.c | 22 +++++++++++++++++----- include/linux/thunderbolt.h | 3 +++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index a6ec667c7bc9e6..b31c4e439f278d 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -630,9 +630,13 @@ static void tb_xdp_handle_request(struct work_struct *work) * the xdomain related to this connection as well in * case there is a change in services it offers. */ - if (xd && device_is_registered(&xd->dev)) { - queue_delayed_work(tb->wq, &xd->get_properties_work, - msecs_to_jiffies(50)); + if (xd) { + mutex_lock(&xd->lock); + if (!xd->removing && device_is_registered(&xd->dev)) + queue_delayed_work(tb->wq, + &xd->get_properties_work, + msecs_to_jiffies(50)); + mutex_unlock(&xd->lock); } break; @@ -1486,6 +1490,10 @@ static int unregister_service(struct device *dev, void *data) */ void tb_xdomain_remove(struct tb_xdomain *xd) { + mutex_lock(&xd->lock); + xd->removing = true; + mutex_unlock(&xd->lock); + stop_handshake(xd); if (!device_is_registered(&xd->dev)) { @@ -1885,8 +1893,12 @@ static int update_xdomain(struct device *dev, void *data) xd = tb_to_xdomain(dev); if (xd) { - queue_delayed_work(xd->tb->wq, &xd->properties_changed_work, - msecs_to_jiffies(50)); + mutex_lock(&xd->lock); + if (!xd->removing) + queue_delayed_work(xd->tb->wq, + &xd->properties_changed_work, + msecs_to_jiffies(50)); + mutex_unlock(&xd->lock); } return 0; diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h index 124e13cb1469c6..b44a36d181608a 100644 --- a/include/linux/thunderbolt.h +++ b/include/linux/thunderbolt.h @@ -188,6 +188,8 @@ void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir); * @link_speed: Speed of the link in Gb/s * @link_width: Width of the link (1 or 2) * @is_unplugged: The XDomain is unplugged + * @removing: Set by tb_xdomain_remove() under @lock to prevent + * concurrent delayed work queueing * @needs_uuid: If the XDomain does not have @remote_uuid it will be * queried first * @service_ids: Used to generate IDs for the services @@ -235,6 +237,7 @@ struct tb_xdomain { unsigned int link_speed; unsigned int link_width; bool is_unplugged; + bool removing; bool needs_uuid; struct ida service_ids; struct ida in_hopids; -- 2.53.0