]> git.hungrycats.org Git - linux/commit
btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl
authorFilipe Manana <fdmanana@suse.com>
Fri, 16 Oct 2026 16:53:00 +0000 (17:53 +0100)
committerDavid Sterba <dsterba@suse.com>
Sun, 8 Nov 2026 06:07:55 +0000 (01:07 -0500)
commitd735bd6b20f9231aa77c9161e0cfe1fc2f75e5af
treecd28bfdba504504f1133afa1dc3f38a9ccad53c5
parent352f79eff849fd8cb1d7112efeacf947cd5a3f32
btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl

If the scrub progress ioctl is called when replace is about to finish, we
have a race that can result in a use-after-free of the scrub context:

1) A scrub is finishing and is at btrfs_scrub_dev();

2) A concurrent task called the scrub progress ioctl and is in
   btrfs_scrub_progress(), where it takes the device_list_mutex and grabs
   the device. Then it starts using the device's scrub context (for a
   memcpy operation);

3) While the scrub progress ioctl task is running, the scrub task sets
   the device's scrub_ctx to NULL (acquiring fs_info->scrub_lock) and
   then calls scrub_put_ctx() against the scrub context, dropping the
   last reference and freeing the memory - while the scrub progress ioctl
   task is still using it, resulting in a use-after-free.

Fix this by changing btrfs_scrub_progress() to take fs_info->scrub_lock
before using the scrub context.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Daniel Vacek <neelx@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
(cherry picked from btrfs for-next commit 9f618b59ff51ccefadc45c66ab4e88beffacf2be)
fs/btrfs/scrub.c