Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/2] btrfs: minor fixes for handle_direct_tree_backref()
@ 2026-11-25  3:15 Qu Wenruo
  2026-11-25  3:15 ` [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL Qu Wenruo
  2026-11-25  3:15 ` [PATCH 2/2] btrfs: backref: reject direct parent if the current level is maxed out Qu Wenruo
  0 siblings, 2 replies; 3+ messages in thread
From: Qu Wenruo @ 2026-11-25  3:15 UTC (permalink / raw)
  To: linux-btrfs

The first one is a tree-checker enhancement to catch a tree block whose
level is already maxed out, but still has a parent that is not itself.
This should help us to detect such rare corruption early.

The second is a similar hardening at runtime, for
handle_direct_tree_backref(), which is also reported by Sashiko.

Qu Wenruo (2):
  btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL
  btrfs: backref: reject direct parent if the current level is maxed out

 fs/btrfs/backref.c      | 11 +++++++++++
 fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++---
 2 files changed, 32 insertions(+), 3 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL
  2026-11-25  3:15 [PATCH 0/2] btrfs: minor fixes for handle_direct_tree_backref() Qu Wenruo
@ 2026-11-25  3:15 ` Qu Wenruo
  2026-11-25  3:15 ` [PATCH 2/2] btrfs: backref: reject direct parent if the current level is maxed out Qu Wenruo
  1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2026-11-25  3:15 UTC (permalink / raw)
  To: linux-btrfs

Inspired by recent fixes to handle_indirect_tree_backref() and the
original sashiko report, if a tree block is already at the max level
(MAX_LEVEL - 1), it should not have a parent except itself.

The self-parent case is for reloc tree and legacy case where backref_rev
is smaller than BTRFS_MIXED_BACKREF_REV.

So reject them when checking the inlined extent item.
This is a very low priority hardening, as it's very hard to hit such
high level tree block in the real world.

Please note that, this is not going to cover all cases, keyed backref
can still introduce the same problem, but with the default skinny
metadata, we should catch most cases now.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index f021ac14babf..f7bc1ea19a0c 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -1537,6 +1537,7 @@ static int check_extent_item(struct extent_buffer *leaf,
 	struct btrfs_fs_info *fs_info = leaf->fs_info;
 	struct btrfs_extent_item *ei;
 	bool is_tree_block = false;
+	u8 level = 0;		/* Tree block level, only for tree blocks */
 	unsigned long ptr;	/* Current pointer inside inline refs */
 	unsigned long end;	/* Extent item end */
 	const u32 item_size = btrfs_item_size(leaf, slot);
@@ -1666,14 +1667,17 @@ static int check_extent_item(struct extent_buffer *leaf,
 		struct btrfs_tree_block_info *info;
 
 		info = (struct btrfs_tree_block_info *)ptr;
-		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
+		level = btrfs_tree_block_level(leaf, info);
+		if (unlikely(level >= BTRFS_MAX_LEVEL)) {
 			extent_err(leaf, slot,
 			"invalid tree block info level, have %u expect [0, %u]",
-				   btrfs_tree_block_level(leaf, info),
-				   BTRFS_MAX_LEVEL - 1);
+				   level, BTRFS_MAX_LEVEL - 1);
 			return -EUCLEAN;
 		}
 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
+	} else if (is_tree_block) {
+		/* METADATA_ITEM_KEY, key->offset is already verified. */
+		level = key->offset;
 	}
 
 	/* Check inline refs */
@@ -1719,6 +1723,20 @@ static int check_extent_item(struct extent_buffer *leaf,
 					   inline_offset, fs_info->sectorsize);
 				return -EUCLEAN;
 			}
+			/*
+			 * A tree block at the highest level can not have a
+			 * parent node.
+			 * The exception is the root of a reloc tree, or of a
+			 * legacy tree with backref_rev < BTRFS_MIXED_BACKREF_REV,
+			 * which use the block itself as the parent.
+			 */
+			if (unlikely(level == BTRFS_MAX_LEVEL - 1 &&
+				     inline_offset != key->objectid)) {
+				extent_err(leaf, slot,
+	"invalid inline shared block ref, tree block at level %u can not have parent %llu",
+					   level, inline_offset);
+				return -EUCLEAN;
+			}
 			inline_refs++;
 			break;
 		/*
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] btrfs: backref: reject direct parent if the current level is maxed out
  2026-11-25  3:15 [PATCH 0/2] btrfs: minor fixes for handle_direct_tree_backref() Qu Wenruo
  2026-11-25  3:15 ` [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL Qu Wenruo
@ 2026-11-25  3:15 ` Qu Wenruo
  1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2026-11-25  3:15 UTC (permalink / raw)
  To: linux-btrfs

Similar to the check in handle_indirect_tree_backref(), in
handle_direct_tree_backref() we have a similar problem that if a tree
block at level 7, searching for level 8 parent can lead to
out-of-boundary access for btrfs_path structure during tree search.

Fix it by rejecting such backref walk if it's not self-parenting (e.g.
reloc root).

Link: https://sashiko.dev/#/bug/linux-e90153be-7335-4621-9d72-6316f96cea82
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/backref.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index a0fb90b7a9ed..e8c299e1c010 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -3221,6 +3221,17 @@ static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,
 		return 0;
 	}
 
+	/*
+	 * Self-parent already checked, and if we're at MAX_LEVEL -1,
+	 * there can not be any higher level parent.
+	 */
+	if (cur->level >= BTRFS_MAX_LEVEL - 1) {
+		btrfs_err(cache->fs_info,
+			  "tree block at %llu level %u can not have any parent",
+			  cur->bytenr, cur->level);
+		return -EUCLEAN;
+	}
+
 	edge = btrfs_backref_alloc_edge(cache);
 	if (!edge)
 		return -ENOMEM;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  3:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  3:15 [PATCH 0/2] btrfs: minor fixes for handle_direct_tree_backref() Qu Wenruo
2026-11-25  3:15 ` [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL Qu Wenruo
2026-11-25  3:15 ` [PATCH 2/2] btrfs: backref: reject direct parent if the current level is maxed out Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox