Skip to content

Commit 3ed5185

Browse files
Lizhi Xukdave
authored andcommitted
btrfs: add a sanity check for btrfs root in btrfs_search_slot()
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: 42437a6 ("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>
1 parent ed67f2a commit 3ed5185

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

fs/btrfs/ctree.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20462046
const struct btrfs_key *key, struct btrfs_path *p,
20472047
int ins_len, int cow)
20482048
{
2049-
struct btrfs_fs_info *fs_info = root->fs_info;
2049+
struct btrfs_fs_info *fs_info;
20502050
struct extent_buffer *b;
20512051
int slot;
20522052
int ret;
@@ -2059,6 +2059,10 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20592059
int min_write_lock_level;
20602060
int prev_cmp;
20612061

2062+
if (!root)
2063+
return -EINVAL;
2064+
2065+
fs_info = root->fs_info;
20622066
might_sleep();
20632067

20642068
lowest_level = p->lowest_level;

0 commit comments

Comments
 (0)