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=1796380315; b=s3WfMwqDiO7K56Gnt21V8pLAxnnNPQYZnjBvW2MfZUQsyGnS4K4xQbK4N6qvB88PDgpO o0UxkKE7Lyc3IQrEAK/phVVxXTOLkjXjxOYFXAB2SAX1i3sFQOZeFtDl/HO7fvz0luzrE as70WmgpXb8VLxnJA/NNWeaoNBMiMKAfjleinsSMXT4kcTIjCacZMToJ64vk2Tk/2GX/V ltebeLyd49JgYe8+9bd9PD6WL/cfwqDPiworKHQ5Hx5UzUYQ3DjIM8VeOFdIvv8eIi3qj jeFQqSU9eLWouYdoJ/cTR8W79c6omHfY6hrss+4KAkxG4JXrjCjbiOf3jmCZWUlyoMA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380315; 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=WHvvJHgsJrbjiLOcMWjW1pQVgbRlOrDzJ+0ARkxqnbA=; b=lWFovEOiGl3QjjEl9uVg0a6aVoQMMMNHZYmAeWgs/iFJFe6Oop7SajVYfuwgfxn/+OED 0M9Wn4rNxPNs2yoX6Uvgbmpcwnfbi4au8HnB+IN/ulojNstFsb3a5YjJWNOHOHq/nrgQU vCL+lRbohjrWQudtumeSTgTxKMVSlYp6KGWgvv1ce0HpeqsLvvxopHYEdP839/oAkOC5F j24FS1Oiwt0RP2XfTLFZ1pGMOO+EMrJus8imUPFz0blaF+kJMRhSolEvLPDvJdqsnyREh gdW0ecBVIHAaecMOStVXbHiAYhn9EDKl1X+0161SPPvQQ2Rzm6jzTMgG7qO67phIn7g== 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="kZNF/vK5"; 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 ED0B0606A68C; Fri, 4 Dec 2026 10:31:54 +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=kZNF/vK5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 678781F00A0A; Fri, 4 Dec 2026 10:31:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380314; bh=WHvvJHgsJrbjiLOcMWjW1pQVgbRlOrDzJ+0ARkxqnbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kZNF/vK57oUVvTKLUQ3KqSWNx9Y8Odrs2AjGvy06yOPFSylhhcvOKJMRGydmBMpb3 I884VaP2aETSd+Oe72MqUmXDOs16HL1YRKpFcVEQG2wN0Jtm4iVzdAs1OA3EBpja04 OEr7zTQgEqan8zVVLJfdduAqAmnaOyFPzVd3MtYg= 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 7.3 218/742] btrfs: zstd: fix UAF due to race between putting workspace and reclaim Date: Fri, 4 Dec 2026 19:01:22 +0900 Message-ID: <20261204100640.869650943@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 082f653464c8e..610f87da19faf 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