Skip to content

Commit 4b996a3

Browse files
fdmananagregkh
authored andcommitted
btrfs: fix race between quota enable and quota rescan ioctl
commit 331cd94 upstream. When enabling quotas, at btrfs_quota_enable(), after committing the transaction, we change fs_info->quota_root to point to the quota root we created and set BTRFS_FS_QUOTA_ENABLED at fs_info->flags. Then we try to start the qgroup rescan worker, first by initializing it with a call to qgroup_rescan_init() - however if that fails we end up freeing the quota root but we leave fs_info->quota_root still pointing to it, this can later result in a use-after-free somewhere else. We have previously set the flags BTRFS_FS_QUOTA_ENABLED and BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with -EINPROGRESS at btrfs_quota_enable(), which is possible if someone already called the quota rescan ioctl, and therefore started the rescan worker. So fix this by ignoring an -EINPROGRESS and asserting we can't get any other error. Reported-by: Ye Bin <yebin10@huawei.com> Link: https://lore.kernel.org/linux-btrfs/20220823015931.421355-1-yebin10@huawei.com/ CC: stable@vger.kernel.org # 4.19+ Reviewed-by: Qu Wenruo <wqu@suse.com> 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>
1 parent 0d94230 commit 4b996a3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

fs/btrfs/qgroup.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,21 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
11571157
fs_info->qgroup_rescan_running = true;
11581158
btrfs_queue_work(fs_info->qgroup_rescan_workers,
11591159
&fs_info->qgroup_rescan_work);
1160+
} else {
1161+
/*
1162+
* We have set both BTRFS_FS_QUOTA_ENABLED and
1163+
* BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with
1164+
* -EINPROGRESS. That can happen because someone started the
1165+
* rescan worker by calling quota rescan ioctl before we
1166+
* attempted to initialize the rescan worker. Failure due to
1167+
* quotas disabled in the meanwhile is not possible, because
1168+
* we are holding a write lock on fs_info->subvol_sem, which
1169+
* is also acquired when disabling quotas.
1170+
* Ignore such error, and any other error would need to undo
1171+
* everything we did in the transaction we just committed.
1172+
*/
1173+
ASSERT(ret == -EINPROGRESS);
1174+
ret = 0;
11601175
}
11611176

11621177
out_free_path:

0 commit comments

Comments
 (0)