Skip to contentSkip to conversation

btrfs: fix a couple races between scrub and the scrub progress ioctl

PATCH In review [email protected] 3 participants 6 messages Oct 16 – Oct 18 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v2 cover btrfs: fix a couple races between scrub and the scrub progress ioctl

From: Filipe Manana <[email protected]>

Details in the change logs.

V2: Added a another patch to fix another race (patch 1/2).

Filipe Manana (2):
btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl
btrfs: scrub: fix race between scrub and scrub progress ioctl

 fs/btrfs/scrub.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/2 btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl

From: Filipe Manana <[email protected]>

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.

---
 fs/btrfs/scrub.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/scrub.c +6 -3
index 0eb30625439b..389ebb5f7d61 100644
@@ -3293,10 +3293,13 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info->fs_devices, &args);
-	if (dev)
+	if (dev) {
+		mutex_lock(&fs_info->scrub_lock);
 		sctx = dev->scrub_ctx;
-	if (sctx)
-		memcpy(progress, &sctx->stat, sizeof(*progress));
+		if (sctx)
+			memcpy(progress, &sctx->stat, sizeof(*progress));
+		mutex_unlock(&fs_info->scrub_lock);
+	}
 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 
 	return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Signature · 1 line
2.53.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl

Quoted 18 lines · On Fri, 16 Oct 2026 at 19:16, <[email protected]> wrote:

From: Filipe Manana <[email protected]>

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.

Quoted 4 lines

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

LGTM

Thanks.

Quoted 29 lines
---
 fs/btrfs/scrub.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/scrub.c +6 -4
index 0eb30625439b..389ebb5f7d61 100644
@@ -3293,10 +3293,13 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 
        mutex_lock(&fs_info->fs_devices->device_list_mutex);
        dev = btrfs_find_device(fs_info->fs_devices, &args);
-       if (dev)
+       if (dev) {
+               mutex_lock(&fs_info->scrub_lock);
                sctx = dev->scrub_ctx;
-       if (sctx)
-               memcpy(progress, &sctx->stat, sizeof(*progress));
+               if (sctx)
+                       memcpy(progress, &sctx->stat, sizeof(*progress));
+               mutex_unlock(&fs_info->scrub_lock);
+       }
        mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 
        return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
--

2.53.0

#3 lore raw

PATCH v2 2/2 btrfs: scrub: fix race between scrub and scrub progress ioctl

In reply to [email protected]

From: Filipe Manana <[email protected]>

The scrub progress ioctl is copying the scrub progress stats without acquiring the spinlock that protects it and a concurrent scrub can be updating the stats at the same time which can lead to load tearing issues. So take the spinlock when copying the progress stats.

---
 fs/btrfs/scrub.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/scrub.c +4 -1
index 389ebb5f7d61..e9ef17d4d43b 100644
@@ -3296,8 +3296,11 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 	if (dev) {
 		mutex_lock(&fs_info->scrub_lock);
 		sctx = dev->scrub_ctx;
-		if (sctx)
+		if (sctx) {
+			spin_lock(&sctx->stat_lock);
 			memcpy(progress, &sctx->stat, sizeof(*progress));
+			spin_unlock(&sctx->stat_lock);
+		}
 		mutex_unlock(&fs_info->scrub_lock);
 	}
 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Signature · 1 line
2.53.0
Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] btrfs: scrub: fix race between scrub and scrub progress ioctl

Quoted 8 lines · On Fri, 16 Oct 2026 at 20:19, <[email protected]> wrote:

From: Filipe Manana <[email protected]>

The scrub progress ioctl is copying the scrub progress stats without acquiring the spinlock that protects it and a concurrent scrub can be updating the stats at the same time which can lead to load tearing issues. So take the spinlock when copying the progress stats.

LGTM

Thanks.

Quoted 25 lines
---
 fs/btrfs/scrub.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/scrub.c +4 -2
index 389ebb5f7d61..e9ef17d4d43b 100644
@@ -3296,8 +3296,11 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
        if (dev) {
                mutex_lock(&fs_info->scrub_lock);
                sctx = dev->scrub_ctx;
-               if (sctx)
+               if (sctx) {
+                       spin_lock(&sctx->stat_lock);
                        memcpy(progress, &sctx->stat, sizeof(*progress));
+                       spin_unlock(&sctx->stat_lock);
+               }
                mutex_unlock(&fs_info->scrub_lock);
        }
        mutex_unlock(&fs_info->fs_devices->device_list_mutex);
--

2.53.0

Reviewed-by on the v2 series
#5 lore raw
In reply to [email protected]
Quoted 5 lines · 在 2026/10/17 03:39, [email protected] 写道:

From: Filipe Manana <[email protected]>

Details in the change logs.

V2: Added a another patch to fix another race (patch 1/2).

Sorry that I reviewed the v1 version of the second patch.

Still the whole series looks good to me.

Thanks,
Qu

Quoted 8 lines

Filipe Manana (2):
btrfs: scrub: fix race leading to use-after-free in the scrub progress ioctl
btrfs: scrub: fix race between scrub and scrub progress ioctl

  fs/btrfs/scrub.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)