From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8C41722F773; Wed, 28 May 2025 21:56:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748469388; cv=none; b=rBciQ0lqu7XZ9LXf0kWIrucZpbgl1SEX0wrE6pbSsBeXN4Y8FgzRo6gYz56+As9/b7oR4NjJ3ORpXVmzLLtFVs0z0VbS1wFsnVu0qAWlkJYzXprCEFOqeRGZMoVvz0fuNuYv+CwIBn80wY9pstZE1R2cXvABhMDWErQcWq1pRW8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748469388; c=relaxed/simple; bh=MCZd/iWjYbUsuvm/RmPIwChcn8pcnR3PdyFWbQlGmwA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nspkY/ANeH1991ckO2xGL5CBNgtb7oNwIAHp+qaMdcyqc6XAwUswFHb0bOK+w09I5FUr270Yra+n8LC3QyP6yh8DYF4IS9L82v0w6oFBP1tStRIWHR8WmOOhUrscMAkjbiangobPSSizZV1YUnH+AX72XuyxFS9BO+Gs8+U+jfc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f66FTtVy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f66FTtVy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24B8BC4CEEE; Wed, 28 May 2025 21:56:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748469388; bh=MCZd/iWjYbUsuvm/RmPIwChcn8pcnR3PdyFWbQlGmwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f66FTtVyxuiQHRVzEbjSVr6Jc6mEfQOChXlQAMxgx3BY7v1GMbE85sAm8TgQklOuV W8HBaYW3T9S5ICnBsfjKP4WGufphVSXFFZeFix1lL3mzg6i4menb9CZh60iHbxrvXS HlMEeLtRiwVP11BQtqOZZ0ssO7PoHlHvwkd5ExoxJK3S66Ri/oUO/6GyxyqZcijI7f pKbw9QhHL5hJ+tOuJLckHueB5N989dbi776pT+5B5lHiPu3kBzI3eXFoUR7a3dfjIh u293GCyYyChn3qEDqAIMXBBz3Ml4myC35CyH6StJJE89bM7eH9JIq1VjapuPCowsQc BiEOIeI2K0a/g== From: Sasha Levin To: patches@lists.linux.dev, stable@vger.kernel.org Cc: Filipe Manana , David Sterba , Sasha Levin , clm@fb.com, josef@toxicpanda.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH AUTOSEL 6.12 4/7] btrfs: exit after state split error at set_extent_bit() Date: Wed, 28 May 2025 17:56:19 -0400 Message-Id: <20250528215622.1983622-4-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250528215622.1983622-1-sashal@kernel.org> References: <20250528215622.1983622-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.12.30 Content-Transfer-Encoding: 8bit From: Filipe Manana [ Upstream commit 41d69d4d78d8b179bf3bcdfc56d28a12b3a608d2 ] If split_state() returned an error we call extent_io_tree_panic() which will trigger a BUG() call. However if CONFIG_BUG is disabled, which is an uncommon and exotic scenario, then we fallthrough and hit a use after free when calling set_state_bits() since the extent state record which the local variable 'prealloc' points to was freed by split_state(). So jump to the label 'out' after calling extent_io_tree_panic() and set the 'prealloc' pointer to NULL since split_state() has already freed it when it hit an error. Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- **YES** This commit should be backported to stable kernel trees. Here's my extensive analysis: ## Analysis of the Code Changes The commit fixes a critical use-after-free bug in the btrfs filesystem's extent I/O tree management. Examining the specific code changes: **Problem Location**: In `fs/btrfs/extent-io-tree.c`, function `set_extent_bit()` around lines 1254-1256: ```c ret = split_state(tree, state, prealloc, end + 1); if (ret) extent_io_tree_panic(tree, state, "split", ret); ``` **The Bug**: After `split_state()` fails and `extent_io_tree_panic()` is called, the code continues to execute `set_state_bits(tree, prealloc, bits, changeset)` on line 1258. However, when `split_state()` fails, it frees the `prealloc` extent state, making this a use-after-free vulnerability. **The Fix**: The commit adds proper error handling: ```c ret = split_state(tree, state, prealloc, end + 1); if (ret) { extent_io_tree_panic(tree, state, "split", ret); prealloc = NULL; goto out; } ``` ## Why This Should Be Backported ### 1. **Critical Security Issue** - **Use-after-free vulnerability**: This is a serious memory safety issue that can lead to kernel crashes, data corruption, or potentially exploitable conditions - **Affects btrfs filesystem**: A widely used filesystem in production environments ### 2. **Specific Edge Case Scenario** From my examination of the kernel code: - `extent_io_tree_panic()` calls `btrfs_panic()` which calls `BUG()` - When `CONFIG_BUG` is disabled (uncommon but possible), `BUG()` becomes a no-op loop instead of halting execution - This allows execution to continue to the use-after-free code ### 3. **Minimal Risk Change** - **Small, targeted fix**: Only adds 3 lines of code - **Clear logic**: Sets pointer to NULL and jumps to cleanup - **No functional changes**: Doesn't alter normal operation paths - **No architectural changes**: Pure bug fix without affecting subsystem design ### 4. **Comparison with Similar Commits** Looking at the reference commits: - Similar to "Similar Commit #2" (marked YES) which also improved error handling in extent bit operations - Unlike commits marked NO, this doesn't add features or make architectural changes - Follows the same pattern as other accepted backports for memory safety fixes ### 5. **Clear Bug Fix Criteria** - **Fixes a real bug**: Use-after-free is a concrete, exploitable issue - **Minimal scope**: Only affects error path in one function - **No side effects**: Change only affects already-failing code paths - **Well-understood impact**: Risk is contained to btrfs extent I/O operations ### 6. **Production Impact** - While `CONFIG_BUG` being disabled is rare, when it occurs this creates a serious vulnerability - btrfs is used in production environments where memory safety is critical - The fix prevents potential kernel crashes or memory corruption ### 7. **Stable Tree Compatibility** - **Self-contained**: Doesn't depend on other recent changes - **Backwards compatible**: No API changes - **Low regression risk**: Only affects error handling path that was already broken The commit perfectly fits stable tree criteria: it's a small, important bugfix with minimal risk that addresses a real security issue in a widely-used filesystem. fs/btrfs/extent-io-tree.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c index bb3aaf610652a..5f9a43734812e 100644 --- a/fs/btrfs/extent-io-tree.c +++ b/fs/btrfs/extent-io-tree.c @@ -1252,8 +1252,11 @@ static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, if (!prealloc) goto search_again; ret = split_state(tree, state, prealloc, end + 1); - if (ret) + if (ret) { extent_io_tree_panic(tree, state, "split", ret); + prealloc = NULL; + goto out; + } set_state_bits(tree, prealloc, bits, changeset); cache_state(prealloc, cached_state); -- 2.39.5