Skip to contentSkip to conversation

btrfs: minor fixes in

PATCH Superseded Qu Wenruo 2 participants 4 messages Nov 22 – Nov 23 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: minor fixes in

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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Signature · 1 line
2.56.1
author
#1 lore raw

PATCH 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..d56476b21fe8 100644
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 	}
 
 	level = cur->level + 1;
+	if (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
author
#3 lore raw

PATCH 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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/backref.c +1 -1
index d56476b21fe8..83301ffbfa87 100644
@@ -3336,7 +3336,7 @@ 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 (!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
#2 lore raw

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

In reply to Qu Wenruo
Quoted 17 lines · Qu Wenruo

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..d56476b21fe8 100644

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

Quoted 4 lines
fs/btrfs/backref.c +1 -0
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 	}
 
 	level = cur->level + 1;
+	if (level >= BTRFS_MAX_LEVEL) {

For EUCLEAN conditions you can add unlikely()