linux/fs/btrfs
Filipe Manana 64287cd456 btrfs: fix race between quota rescan and disable leading to NULL pointer deref
commit b7adbf9ada upstream.

If we have one task trying to start the quota rescan worker while another
one is trying to disable quotas, we can end up hitting a race that results
in the quota rescan worker doing a NULL pointer dereference. The steps for
this are the following:

1) Quotas are enabled;

2) Task A calls the quota rescan ioctl and enters btrfs_qgroup_rescan().
   It calls qgroup_rescan_init() which returns 0 (success) and then joins a
   transaction and commits it;

3) Task B calls the quota disable ioctl and enters btrfs_quota_disable().
   It clears the bit BTRFS_FS_QUOTA_ENABLED from fs_info->flags and calls
   btrfs_qgroup_wait_for_completion(), which returns immediately since the
   rescan worker is not yet running.
   Then it starts a transaction and locks fs_info->qgroup_ioctl_lock;

4) Task A queues the rescan worker, by calling btrfs_queue_work();

5) The rescan worker starts, and calls rescan_should_stop() at the start
   of its while loop, which results in 0 iterations of the loop, since
   the flag BTRFS_FS_QUOTA_ENABLED was cleared from fs_info->flags by
   task B at step 3);

6) Task B sets fs_info->quota_root to NULL;

7) The rescan worker tries to start a transaction and uses
   fs_info->quota_root as the root argument for btrfs_start_transaction().
   This results in a NULL pointer dereference down the call chain of
   btrfs_start_transaction(). The stack trace is something like the one
   reported in Link tag below:

   general protection fault, probably for non-canonical address 0xdffffc0000000041: 0000 [#1] PREEMPT SMP KASAN
   KASAN: null-ptr-deref in range [0x0000000000000208-0x000000000000020f]
   CPU: 1 PID: 34 Comm: kworker/u4:2 Not tainted 6.1.0-syzkaller-13872-gb6bb9676f216 #0
   Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022
   Workqueue: btrfs-qgroup-rescan btrfs_work_helper
   RIP: 0010:start_transaction+0x48/0x10f0 fs/btrfs/transaction.c:564
   Code: 48 89 fb 48 (...)
   RSP: 0018:ffffc90000ab7ab0 EFLAGS: 00010206
   RAX: 0000000000000041 RBX: 0000000000000208 RCX: ffff88801779ba80
   RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000000
   RBP: dffffc0000000000 R08: 0000000000000001 R09: fffff52000156f5d
   R10: fffff52000156f5d R11: 1ffff92000156f5c R12: 0000000000000000
   R13: 0000000000000001 R14: 0000000000000001 R15: 0000000000000003
   FS:  0000000000000000(0000) GS:ffff8880b9900000(0000) knlGS:0000000000000000
   CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
   CR2: 00007f2bea75b718 CR3: 000000001d0cc000 CR4: 00000000003506e0
   DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
   DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
   Call Trace:
    <TASK>
    btrfs_qgroup_rescan_worker+0x3bb/0x6a0 fs/btrfs/qgroup.c:3402
    btrfs_work_helper+0x312/0x850 fs/btrfs/async-thread.c:280
    process_one_work+0x877/0xdb0 kernel/workqueue.c:2289
    worker_thread+0xb14/0x1330 kernel/workqueue.c:2436
    kthread+0x266/0x300 kernel/kthread.c:376
    ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308
    </TASK>
   Modules linked in:

So fix this by having the rescan worker function not attempt to start a
transaction if it didn't do any rescan work.

Reported-by: syzbot+96977faa68092ad382c4@syzkaller.appspotmail.com
Link: https://lore.kernel.org/linux-btrfs/000000000000e5454b05f065a803@google.com/
Fixes: e804861bd4 ("btrfs: fix deadlock between quota disable and qgroup rescan worker")
CC: stable@vger.kernel.org # 5.4+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-01-24 07:22:46 +01:00
..
tests btrfs: remove pointless and double ulist frees in error paths of qgroup tests 2022-11-26 09:24:32 +01:00
Kconfig
Makefile
acl.c
async-thread.c
async-thread.h
backref.c btrfs: fix resolving backrefs for inline extent followed by prealloc 2023-01-12 11:58:50 +01:00
backref.h btrfs: sink iterator parameter to btrfs_ioctl_logical_to_ino 2022-12-08 11:28:38 +01:00
block-group.c btrfs: enhance unsupported compat RO flags handling 2022-10-29 10:12:53 +02:00
block-group.h btrfs: fix space cache corruption and potential double allocations 2022-09-05 10:30:12 +02:00
block-rsv.c
block-rsv.h
btrfs_inode.h btrfs: put initial index value of a directory in a constant 2022-08-31 17:16:35 +02:00
check-integrity.c btrfs: rename btrfs_bio to btrfs_io_context 2022-07-21 21:24:32 +02:00
check-integrity.h
compression.c btrfs: remove unused parameter nr_pages in add_ra_bio_pages() 2022-04-20 09:34:04 +02:00
compression.h
ctree.c btrfs: fix lockdep splat with reloc root extent buffers 2022-09-05 10:30:12 +02:00
ctree.h btrfs: fix space cache corruption and potential double allocations 2022-09-05 10:30:12 +02:00
delalloc-space.c btrfs: convert count_max_extents() to use fs_info->max_extent_size 2022-08-31 17:16:34 +02:00
delalloc-space.h
delayed-inode.c
delayed-inode.h
delayed-ref.c
delayed-ref.h btrfs: add additional parameters to btrfs_init_tree_ref/btrfs_init_data_ref 2022-07-12 16:34:50 +02:00
dev-replace.c btrfs: add info when mount fails due to stale replace target 2022-08-31 17:16:46 +02:00
dev-replace.h
dir-item.c
discard.c
discard.h
disk-io.c btrfs: do not abort transaction on failure to write log tree when syncing log 2023-01-24 07:22:46 +01:00
disk-io.h btrfs: make thaw time super block check to also verify checksum 2023-01-12 11:59:20 +01:00
export.c btrfs: fix type of parameter generation in btrfs_get_dentry 2022-11-10 18:15:38 +01:00
export.h btrfs: fix type of parameter generation in btrfs_get_dentry 2022-11-10 18:15:38 +01:00
extent-io-tree.h
extent-tree.c btrfs: always report error in run_one_delayed_ref() 2023-01-24 07:22:42 +01:00
extent_io.c btrfs: fix lockdep splat with reloc root extent buffers 2022-09-05 10:30:12 +02:00
extent_io.h btrfs: fix qgroup reserve overflow the qgroup limit 2022-04-13 20:59:23 +02:00
extent_map.c btrfs: rename btrfs_bio to btrfs_io_context 2022-07-21 21:24:32 +02:00
extent_map.h
file-item.c btrfs: make search_csum_tree return 0 if we get -EFBIG 2022-04-08 14:23:58 +02:00
file.c btrfs: do not BUG_ON() on ENOMEM when dropping extent items for a range 2022-12-31 13:14:47 +01:00
free-space-cache.c btrfs: dump extra info if one free space cache has more bitmaps than it should 2022-10-26 12:35:44 +02:00
free-space-cache.h
free-space-tree.c btrfs: fix invalid delayed ref after subvolume creation failure 2022-07-12 16:34:50 +02:00
free-space-tree.h
inode-item.c
inode.c btrfs: remove root argument from btrfs_unlink_inode() 2022-09-05 10:30:09 +02:00
ioctl.c btrfs: replace strncpy() with strscpy() 2023-01-12 11:59:05 +01:00
locking.c btrfs: fix lockdep splat with reloc root extent buffers 2022-09-05 10:30:12 +02:00
locking.h btrfs: fix lockdep splat with reloc root extent buffers 2022-09-05 10:30:12 +02:00
lzo.c
misc.h
ordered-data.c
ordered-data.h
orphan.c
print-tree.c
print-tree.h
props.c
props.h
qgroup.c btrfs: fix race between quota rescan and disable leading to NULL pointer deref 2023-01-24 07:22:46 +01:00
qgroup.h
raid56.c btrfs: raid56: properly handle the error when unable to find the missing stripe 2022-11-26 09:24:31 +01:00
raid56.h btrfs: rename btrfs_bio to btrfs_io_context 2022-07-21 21:24:32 +02:00
rcu-string.h btrfs: replace strncpy() with strscpy() 2023-01-12 11:59:05 +01:00
reada.c btrfs: rename btrfs_bio to btrfs_io_context 2022-07-21 21:24:32 +02:00
ref-verify.c
ref-verify.h
reflink.c btrfs: fix unexpected error path when reflinking an inline extent 2022-04-08 14:23:11 +02:00
reflink.h
relocation.c btrfs: fix lockdep splat with reloc root extent buffers 2022-09-05 10:30:12 +02:00
root-tree.c btrfs: fix silent failure when deleting root reference 2022-08-31 17:16:46 +02:00
scrub.c btrfs: scrub: try to fix super block errors 2022-10-26 12:35:44 +02:00
send.c btrfs: send: avoid unaligned encoded writes when attempting to clone range 2022-12-14 11:37:16 +01:00
send.h
space-info.c btrfs: extend locking to all space_info members accesses 2022-04-08 14:23:02 +02:00
space-info.h
struct-funcs.c
subpage.c
subpage.h
super.c btrfs: make thaw time super block check to also verify checksum 2023-01-12 11:59:20 +01:00
sysfs.c btrfs: sysfs: normalize the error handling branch in btrfs_init_sysfs() 2022-12-02 17:41:12 +01:00
sysfs.h
transaction.c btrfs: make send work with concurrent block group relocation 2022-03-16 14:23:46 +01:00
transaction.h
tree-checker.c btrfs: tree-checker: check for overlapping extent items 2022-09-05 10:30:12 +02:00
tree-checker.h
tree-defrag.c btrfs: fix an error handling path in btrfs_defrag_leaves() 2023-01-12 11:59:08 +01:00
tree-log.c btrfs: do not abort transaction on failure to write log tree when syncing log 2023-01-24 07:22:46 +01:00
tree-log.h btrfs: pass the dentry to btrfs_log_new_name() instead of the inode 2022-08-31 17:16:36 +02:00
tree-mod-log.c
tree-mod-log.h
ulist.c
ulist.h
uuid-tree.c
verity.c
volumes.c btrfs: check superblock to ensure the fs was not modified at thaw time 2023-01-12 11:59:17 +01:00
volumes.h btrfs: zoned: initialize device's zone info for seeding 2022-11-16 09:58:27 +01:00
xattr.c btrfs: check if root is readonly while setting security xattr 2022-08-31 17:16:46 +02:00
xattr.h
zlib.c
zoned.c btrfs: use kvcalloc in btrfs_get_dev_zone_info 2022-12-02 17:41:12 +01:00
zoned.h btrfs: zoned: revive max_zone_append_bytes 2022-08-31 17:16:34 +02:00
zstd.c