Skip to contentSkip to conversation

btrfs: a couple bug fixes for log replay

PATCH In review [email protected] 3 participants 5 messages Jun 3, 2025 – Jun 4, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v2 cover btrfs: a couple bug fixes for log replay

From: Filipe Manana <[email protected]>

Fix invalid inode pointer dereferences in error paths of log replay and stop ignoring invalid extent types.

V2: Updated patch 1/2 to avoid NULL pointer dereference for the case
where we find an unexpected/invalid extent type and added RB tag. Added patch 2/2.

Filipe Manana (2):
btrfs: fix invalid inode pointer dereferences during log replay
btrfs: don't silently ignore unexpected extent type when replaying log

 fs/btrfs/tree-log.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v2 1/2 btrfs: fix invalid inode pointer dereferences during log replay

From: Filipe Manana <[email protected]>

In a few places where we call read_one_inode(), if we get a NULL pointer we end up jumping into an error path, or fallthrough in case of __add_inode_ref(), where we then do something like this:

   iput(&inode->vfs_inode);

which results in an invalid inode pointer that triggers an invalid memory access, resulting in a crash.

Fix this by making sure we don't do such dereferences.

Fixes b4c50cbb01a1 ("btrfs: return a btrfs_inode from read_one_inode()")
---
 fs/btrfs/tree-log.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
fs/btrfs/tree-log.c +6 -8
index 34ed9b2b1b83..c8dcc7d3f4b0 100644
@@ -668,15 +668,12 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
 		extent_end = ALIGN(start + size,
 				   fs_info->sectorsize);
 	} else {
-		ret = 0;
-		goto out;
+		return 0;
 	}
 
 	inode = read_one_inode(root, key->objectid);
-	if (!inode) {
-		ret = -EIO;
-		goto out;
-	}
+	if (!inode)
+		return -EIO;
 
 	/*
 	 * first check to see if we already have this extent in the
@@ -961,7 +958,8 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
 	ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
 out:
 	kfree(name.name);
-	iput(&inode->vfs_inode);
+	if (inode)
+		iput(&inode->vfs_inode);
 	return ret;
 }
 
@@ -1176,8 +1174,8 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
 					ret = unlink_inode_for_log_replay(trans,
 							victim_parent,
 							inode, &victim_name);
+					iput(&victim_parent->vfs_inode);
 				}
-				iput(&victim_parent->vfs_inode);
 				kfree(victim_name.name);
 				if (ret)
 					return ret;
Signature · 1 line
2.47.2
#2 lore raw

PATCH v2 2/2 btrfs: don't silently ignore unexpected extent type when replaying log

In reply to [email protected]

From: Filipe Manana <[email protected]>

If there's an unexpected (invalid) extent type, we just silently ignore it. This means a corruption or some bug somewhere, so instead return -EUCLEAN to the caller, making log replay fail, and print an error message with relevant information.

---
 fs/btrfs/tree-log.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/tree-log.c +4 -1
index c8dcc7d3f4b0..3f5593fe1215 100644
@@ -668,7 +668,10 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
 		extent_end = ALIGN(start + size,
 				   fs_info->sectorsize);
 	} else {
-		return 0;
+		btrfs_err(fs_info,
+		  "unexpected extent type=%d root=%llu inode=%llu offset=%llu",
+			  found_type, btrfs_root_id(root), key->objectid, key->offset);
+		return -EUCLEAN;
 	}
 
 	inode = read_one_inode(root, key->objectid);
Signature · 1 line
2.47.2
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] btrfs: don't silently ignore unexpected extent type when replaying log

Quoted 8 lines · On Tue, Jun 03, 2025 at 10:19:58PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

If there's an unexpected (invalid) extent type, we just silently ignore it. This means a corruption or some bug somewhere, so instead return -EUCLEAN to the caller, making log replay fail, and print an error message with relevant information.

Quoted 23 lines
---
 fs/btrfs/tree-log.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/tree-log.c +4 -1
index c8dcc7d3f4b0..3f5593fe1215 100644
@@ -668,7 +668,10 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
 		extent_end = ALIGN(start + size,
 				   fs_info->sectorsize);
 	} else {
-		return 0;
+		btrfs_err(fs_info,
+		  "unexpected extent type=%d root=%llu inode=%llu offset=%llu",
+			  found_type, btrfs_root_id(root), key->objectid, key->offset);
+		return -EUCLEAN;
 	}
 
 	inode = read_one_inode(root, key->objectid);
Signature · 1 line
2.47.2
Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] btrfs: don't silently ignore unexpected extent type when replaying log

In reply to [email protected]
Quoted 8 lines · On Tue, Jun 03, 2025 at 10:19:58PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

If there's an unexpected (invalid) extent type, we just silently ignore it. This means a corruption or some bug somewhere, so instead return -EUCLEAN to the caller, making log replay fail, and print an error message with relevant information.