]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
btrfs: add a sanity check for btrfs root in btrfs_search_slot()
authorLizhi Xu <lizhi.xu@windriver.com>
Fri, 25 Oct 2024 04:55:53 +0000 (12:55 +0800)
committerDavid Sterba <dsterba@suse.com>
Fri, 29 Nov 2024 15:50:40 +0000 (16:50 +0100)
Syzbot reports a null-ptr-deref in btrfs_search_slot().

The reproducer is using rescue=ibadroots, and the extent tree root is
corrupted thus the extent tree is NULL.

When scrub tries to search the extent tree to gather the needed extent
info, btrfs_search_slot() doesn't check if the target root is NULL or
not, resulting the null-ptr-deref.

Add sanity check for btrfs root before using it in btrfs_search_slot().

Reported-by: syzbot+3030e17bd57a73d39bd7@syzkaller.appspotmail.com
Fixes: 42437a6386ff ("btrfs: introduce mount option rescue=ignorebadroots")
Link: https://syzkaller.appspot.com/bug?extid=3030e17bd57a73d39bd7
CC: stable@vger.kernel.org # 5.15+
Reviewed-by: Qu Wenruo <wqu@suse.com>
Tested-by: syzbot+3030e17bd57a73d39bd7@syzkaller.appspotmail.com
Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.c

index 148648ea1c8b9513bca44c13a7c6012b3d3a00db..693dc27ffb8979c06da18996aa6bb98c72a6e75e 100644 (file)
@@ -2046,7 +2046,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
                      const struct btrfs_key *key, struct btrfs_path *p,
                      int ins_len, int cow)
 {
-       struct btrfs_fs_info *fs_info = root->fs_info;
+       struct btrfs_fs_info *fs_info;
        struct extent_buffer *b;
        int slot;
        int ret;
@@ -2059,6 +2059,10 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
        int min_write_lock_level;
        int prev_cmp;
 
+       if (!root)
+               return -EINVAL;
+
+       fs_info = root->fs_info;
        might_sleep();
 
        lowest_level = p->lowest_level;