linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org,
	syzbot+283673dbc38527ef9f3d@syzkaller.appspotmail.com
Subject: Re: [PATCH] btrfs: fix a NULL pointer dereference when failed to start a new trasacntion
Date: Mon, 30 Sep 2024 14:09:33 -0400	[thread overview]
Message-ID: <20240930180933.GC667556@perftesting> (raw)
In-Reply-To: <c3663ebd875fb3576710f61aefedc25bf452d141.1727476551.git.wqu@suse.com>

On Sat, Sep 28, 2024 at 08:05:58AM +0930, Qu Wenruo wrote:
> [BUG]
> Syzbot reported a NULL pointer dereference with the following crash:
> 
> FAULT_INJECTION: forcing a failure.
>  start_transaction+0x830/0x1670 fs/btrfs/transaction.c:676
>  prepare_to_relocate+0x31f/0x4c0 fs/btrfs/relocation.c:3642
>  relocate_block_group+0x169/0xd20 fs/btrfs/relocation.c:3678
> ...
> BTRFS info (device loop0): balance: ended with status: -12
> Oops: general protection fault, probably for non-canonical address 0xdffffc00000000cc: 0000 [#1] PREEMPT SMP KASAN NOPTI
> KASAN: null-ptr-deref in range [0x0000000000000660-0x0000000000000667]
> RIP: 0010:btrfs_update_reloc_root+0x362/0xa80 fs/btrfs/relocation.c:926
> Call Trace:
>  <TASK>
>  commit_fs_roots+0x2ee/0x720 fs/btrfs/transaction.c:1496
>  btrfs_commit_transaction+0xfaf/0x3740 fs/btrfs/transaction.c:2430
>  del_balance_item fs/btrfs/volumes.c:3678 [inline]
>  reset_balance_state+0x25e/0x3c0 fs/btrfs/volumes.c:3742
>  btrfs_balance+0xead/0x10c0 fs/btrfs/volumes.c:4574
>  btrfs_ioctl_balance+0x493/0x7c0 fs/btrfs/ioctl.c:3673
>  vfs_ioctl fs/ioctl.c:51 [inline]
>  __do_sys_ioctl fs/ioctl.c:907 [inline]
>  __se_sys_ioctl+0xf9/0x170 fs/ioctl.c:893
>  do_syscall_x64 arch/x86/entry/common.c:52 [inline]
>  do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
>  entry_SYSCALL_64_after_hwframe+0x77/0x7f
> ---[ end trace 0000000000000000 ]---
> 
> [CAUSE]
> The allocation failure happens at the start_transaction() inside
> prepare_to_relocate(), and during the error handling we call
> unset_reloc_control(), which makes fs_info->balance_ctl to be NULL.
> 
> Then we continue the error path cleanup in btrfs_balance() by calling
> reset_balance_state() which will call del_balance_item() to fully delete
> the balance item in the root tree.
> 
> However during the small window between set_reloc_contrl() and
> unset_reloc_control(), we can have a subvolume tree update and created a
> reloc_root for that subvolume.
> 
> Then we go into the final btrfs_commit_transaction() of
> del_balance_item(), and into btrfs_update_reloc_root() inside
> commit_fs_roots().
> 
> That function checks if fs_info->reloc_ctl is in the merge_reloc_tree
> stage, but since fs_info->reloc_ctl is NULL, it results a NULL pointer
> dereference.
> 
> [FIX]
> Just add extra check on fs_info->reloc_ctl inside
> btrfs_update_reloc_root(), before checking
> fs_info->reloc_ctl->merge_reloc_tree.
> 
> That DEAD_RELOC_TREE handling is to prevent further modification to the
> reloc tree during merge stage, but since there is no reloc_ctl at all,
> we do not need to bother that.
> 
> Reported-by: syzbot+283673dbc38527ef9f3d@syzkaller.appspotmail.com
> Link: https://lore.kernel.org/linux-btrfs/66f6bfa7.050a0220.38ace9.0019.GAE@google.com/
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

      reply	other threads:[~2024-09-30 18:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-27 22:35 [PATCH] btrfs: fix a NULL pointer dereference when failed to start a new trasacntion Qu Wenruo
2024-09-30 18:09 ` Josef Bacik [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240930180933.GC667556@perftesting \
    --to=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=syzbot+283673dbc38527ef9f3d@syzkaller.appspotmail.com \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).