Skip to contentSkip to conversation

btrfs: minor fixes in handle_indirect_tree_backref()

PATCH In review Qu Wenruo 2 participants 5 messages Nov 23 1 message not in the archive Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v2 cover btrfs: minor fixes in handle_indirect_tree_backref()

[CHANGELOG]
v2:
- Add unlikely() for both patches

The first one is reported by Sashiko, the second one is another bug exposed during fixing the first one.

Qu Wenruo (2):
btrfs: reject too high tree level during backref walk
btrfs: avoid NULL-ptr-dreference after a lowest_level search

 fs/btrfs/backref.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v2 1/2 btrfs: reject too high tree level during backref walk

In handle_indirect_tree_backref(), if we got @cur->level set to 7 (the max level valid), @level will be 8, then later path->slots[level] and path->nodes[level] will all go beyond the array boundary.

Fix it by rejecting such high level before calling btrfs_search_slot().

---
 fs/btrfs/backref.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/btrfs/backref.c +5 -0
index a0fb90b7a9ed..e9434e033372 100644
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 	}
 
 	level = cur->level + 1;
+	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
+		btrfs_put_root(root);
+		btrfs_err(fs_info, "invalid searcch level %d", level);
+		return -EUCLEAN;
+	}
 
 	/* Search the tree to find parent blocks referring to the block */
 	path->search_commit_root = true;
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH v2 1/2] btrfs: reject too high tree level during backref walk

Qu Wenruo @ 2026-11-24 07:07 +1030:

Quoted 17 lines

In handle_indirect_tree_backref(), if we got @cur->level set to 7 (the max level valid), @level will be 8, then later path->slots[level] and path->nodes[level] will all go beyond the array boundary.

Fix it by rejecting such high level before calling btrfs_search_slot().

---
 fs/btrfs/backref.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/btrfs/backref.c +0 -0
index a0fb90b7a9ed..e9434e033372 100644

fs/btrfs/backref.c static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans, · line 3321

Quoted 6 lines
fs/btrfs/backref.c +3 -0
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 	}
 
 	level = cur->level + 1;
+	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
+		btrfs_put_root(root);
+		btrfs_err(fs_info, "invalid searcch level %d", level);

Very minor, mind the typo in "search".

Quoted 5 lines
fs/btrfs/backref.c +2 -0
+		return -EUCLEAN;
+	}
 
 	/* Search the tree to find parent blocks referring to the block */
 	path->search_commit_root = true;

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#4 lore raw

Re: [PATCH v2 1/2] btrfs: reject too high tree level during backref walk

Quoted 21 lines · Miquel Sabaté Solà

Qu Wenruo @ 2026-11-24 07:07 +1030:

Quoted 19 lines

In handle_indirect_tree_backref(), if we got @cur->level set to 7 (the max level valid), @level will be 8, then later path->slots[level] and path->nodes[level] will all go beyond the array boundary.

Fix it by rejecting such high level before calling btrfs_search_slot().

---
  fs/btrfs/backref.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/btrfs/backref.c +0 -0
index a0fb90b7a9ed..e9434e033372 100644
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
  	}
 
Quoted 6 lines

fs/btrfs/backref.c static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans, · line 3321

Quoted 4 lines
fs/btrfs/backref.c +3 -0
  	level = cur->level + 1;
+	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
+		btrfs_put_root(root);
+		btrfs_err(fs_info, "invalid searcch level %d", level);

Very minor, mind the typo in "search".

Thanks, I'll get it fixed during merge.

Quoted 6 lines
Quoted 6 lines
fs/btrfs/backref.c +2 -0
+		return -EUCLEAN;
+	}
 
  	/* Search the tree to find parent blocks referring to the block */
  	path->search_commit_root = true;
author
#5 lore raw

PATCH v2 2/2 btrfs: avoid NULL-ptr-dreference after a lowest_level search

In reply to Qu Wenruo

In handle_indirect_tree_backref(), if we got a corrupted backref item that has a valid level (e.g. 2), but higher than the subvolume root level (e.g. 0), btrfs_search_slot() will return 0 or 1 depending on if the key can be found in the leaf.

But the returned path->nodes[] will be all be NULL.

Later we call btrfs_node_blockptr(path->nodes[level]) will trigger a NULL pointer dereference.

Fix it by checking if we got a valid eb pointer before calling btrfs_node_blockptr(), and reject it as we can not find the parent tree block.

---
 fs/btrfs/backref.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/backref.c +2 -1
index e9434e033372..29737246d481 100644
@@ -3336,7 +3336,8 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 		path->slots[level]--;
 
 	eb = path->nodes[level];
-	if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) {
+	if (unlikely(!eb || btrfs_node_blockptr(eb, path->slots[level]) !=
+		     cur->bytenr)) {
 		btrfs_err(fs_info,
 "couldn't find block (%llu) (level %d) in tree (%llu) with key " BTRFS_KEY_FMT,
 			  cur->bytenr, level - 1, btrfs_root_id(root),
Signature · 1 line
2.56.1