From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796381977; b=ksd8xaiuMjp2aKB69DHC9TRegMzhslbzo4Al6TLqsvpqzbdBTnYkLi+Qzr/XFvxmtKfU wHQsGV9oPbCryUGTCabDE/B1TDyK84s3ywQFqV9WffzKVLZu9l27LsTUR3elqPnewvbdH pNWjDtptgZmaAtz62uVMLFLJkJTtac6sKlQHnJRTEiGAaoL/ENQ+qUx7zHGYXtkg0p2ke 9K2CV0ho0i9Mz9vz4L6/eXsh9MKgBmeGxqBY4G06/HUnRjTvZTOPlGmMpAiEI6rWpfEcP W2TLlzw9X/KpnfcT9KV3ycv+tgTxdzXdmJa3dsGGFiy4dpInoQlKjX0C/uhP9+OkYCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381977; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=vilrZ6PvhlrKnuG8ciiTNFKdRDgluJYPPGYCZfkx284=; b=H2JUF+6q6TjwnjOE9+XHBHefNyrmcfrKZMlKXPWTIbbJ9YdkgH4tMT52gqYYOC9euGoN SZ4GKR7ROkdXfxUDmFM5wJRT8B+IrtJBkxJB+iBkpvKT4Z7NCgZrE9+LYmffSNP5yZ6nd iuIVtzuawdalvthJKOglhpgcggGyf4tLZJ2jDv7tylLw/8A9QQ6AaQPTe/oOXOOHzZDDr nHnJ9jIc6Mq2SrA1rxrs+zsIkk6N2MYpl2XPTSSOOgKDS2XlK2AAOpvQyvZK6h5YRQBMf gW14U+WDOMxT9lzz2bx7p+5XIrog3Y0rlzQNXUrq/k13yFEfzdeXcOW82KzOJN+Oplw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=hQjWaY29; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D9C1606A322; Fri, 4 Dec 2026 10:59:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=hQjWaY29 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F04C01F000CE; Fri, 4 Dec 2026 10:59:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381977; bh=vilrZ6PvhlrKnuG8ciiTNFKdRDgluJYPPGYCZfkx284=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hQjWaY2973IYEDtsVTGa8eBrU2btk03GCgsmn2qCBX4u782IF1kyG3DNmhNHu2139 DBNUNmTU0CKe9YMfWtn6WpOJtoIKPBAc1DxH4VxfHGRV+ys6+HfU2/P0EC3bhEhFFn zWgjNnF4OsaNA3KSkBGPAjJ4AH6zb+KXOWt+AUkY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qu Wenruo , Filipe Manana , David Sterba , Sasha Levin Subject: [PATCH 6.18 255/703] btrfs: zstd: fix UAF due to race between putting workspace and reclaim Date: Fri, 4 Dec 2026 19:02:34 +0900 Message-ID: <20261204100634.577899412@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana [ Upstream commit 2d8b3d9b210a388129e41757372d70c66ae20a5b ] When we put a workspace with a level matching the requested level, we add it back to the lru list of the workspace manager and set the timer to run the reclaim function (zstd_reclaim_timer_fn()). If that workspace than gets used by some other task, obtained with zstd_find_workspace(), for some compression level lower than the workspace's level, we set the workspace's req_level field to the requested level (done in zstd_find_workspace()) and leave it in the lru list without updating the last_used field of the workspace. Once the last_used field ages past the ZSTD_BTRFS_RECLAIM_JIFFIES value, when zstd_put_workspace() is called it skips the lru update branch because the workspace's req_level does not match its level, and then it sets its req_level to 0 before releasing the workspace manager's lock - and after that and before zstd_put_workspace() does: if (workspace->level == clip_level(ZSTD_BTRFS_MAX_LEVEL)) cond_wake_up(&zwsm->wait); the reclaim function, zstd_reclaim_timer_fn(), can run, see the workspace in the lru list and see that it has a req_level of 0 and an expired last_used time, and therefore call zstd_free_workspace() to free the workspace, right before the task that is putting the workspace executes the code above to read its level. Fix this by reading the workspace's level before unlocking the workspace manager in zstd_put_workspace(). Link: https://sashiko.dev/#/bug/linux-92b08485-a86c-4bb5-9138-fa80c84389f0 Fixes: 3f93aef535c8 ("btrfs: add zstd compression level support") Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/zstd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/zstd.c b/fs/btrfs/zstd.c index ced6406425f9d..aaaa1f6657a9c 100644 --- a/fs/btrfs/zstd.c +++ b/fs/btrfs/zstd.c @@ -340,6 +340,7 @@ void zstd_put_workspace(struct btrfs_fs_info *fs_info, struct list_head *ws) { struct zstd_workspace_manager *zwsm = fs_info->compr_wsm[BTRFS_COMPRESS_ZSTD]; struct workspace *workspace = list_to_workspace(ws); + bool wakeup; ASSERT(zwsm); spin_lock_bh(&zwsm->lock); @@ -363,9 +364,14 @@ void zstd_put_workspace(struct btrfs_fs_info *fs_info, struct list_head *ws) list_add(&workspace->list, &zwsm->idle_ws[workspace->level]); workspace->req_level = 0; + /* + * Check if we should wakeup before releasing the lock because after + * that the reclaim function can run and free the workspace. + */ + wakeup = (workspace->level == clip_level(ZSTD_BTRFS_MAX_LEVEL)); spin_unlock_bh(&zwsm->lock); - if (workspace->level == clip_level(ZSTD_BTRFS_MAX_LEVEL)) + if (wakeup) cond_wake_up(&zwsm->wait); } -- 2.53.0