diff mbox series

[v2] btrfs: Call btrfs_set_header_generation() before btrfs_clean_tree_block()

Message ID 112cdf17-374f-fdc5-58ae-d8db3f695ac4@I-love.SAKURA.ne.jp (mailing list archive)
State New, archived
Headers show
Series [v2] btrfs: Call btrfs_set_header_generation() before btrfs_clean_tree_block() | expand

Commit Message

Tetsuo Handa Sept. 20, 2022, 1:43 p.m. UTC
syzbot is reporting uninit-value in btrfs_clean_tree_block() [1], for
commit bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")
missed that btrfs_set_header_generation() in btrfs_init_new_buffer() must
not be moved to after clean_tree_block() because clean_tree_block() is
calling btrfs_header_generation() since commit 55c69072d6bd5be1 ("Btrfs:
Fix extent_buffer usage when nodesize != leafsize").

Since memzero_extent_buffer() will reset "struct btrfs_header" part, we
can't move btrfs_set_header_generation() to before memzero_extent_buffer().
Just re-add btrfs_set_header_generation() before btrfs_clean_tree_block().

Link: https://syzkaller.appspot.com/bug?extid=fba8e2116a12609b6c59 [1]
Reported-by: syzbot <syzbot+fba8e2116a12609b6c59@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Fixes: bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")
---
Changes in v2:
  Don't remove btrfs_set_header_generation() after memzero_extent_buffer(),
  pointed out by Johannes Thumshirn <Johannes.Thumshirn@wdc.com>.

 fs/btrfs/extent-tree.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

David Sterba Sept. 22, 2022, 4:50 p.m. UTC | #1
On Tue, Sep 20, 2022 at 10:43:51PM +0900, Tetsuo Handa wrote:
> syzbot is reporting uninit-value in btrfs_clean_tree_block() [1], for
> commit bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")
> missed that btrfs_set_header_generation() in btrfs_init_new_buffer() must
> not be moved to after clean_tree_block() because clean_tree_block() is
> calling btrfs_header_generation() since commit 55c69072d6bd5be1 ("Btrfs:
> Fix extent_buffer usage when nodesize != leafsize").
> 
> Since memzero_extent_buffer() will reset "struct btrfs_header" part, we
> can't move btrfs_set_header_generation() to before memzero_extent_buffer().
> Just re-add btrfs_set_header_generation() before btrfs_clean_tree_block().
> 
> Link: https://syzkaller.appspot.com/bug?extid=fba8e2116a12609b6c59 [1]
> Reported-by: syzbot <syzbot+fba8e2116a12609b6c59@syzkaller.appspotmail.com>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> Fixes: bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")

Added to misc-next, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 6914cd8024ba..cfbbd7dc3c46 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4888,6 +4888,9 @@  btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	    !test_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &root->state))
 		lockdep_owner = BTRFS_FS_TREE_OBJECTID;
 
+	/* btrfs_clean_tree_block() accesses generation field. */
+	btrfs_set_header_generation(buf, trans->transid);
+
 	/*
 	 * This needs to stay, because we could allocate a freed block from an
 	 * old tree into a new tree, so we need to make sure this new block is