]> git.hungrycats.org Git - linux/commitdiff
btrfs: zoned: fix possible UAF in wait_eb_writebacks()
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Wed, 9 Sep 2026 09:45:34 +0000 (11:45 +0200)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:17 +0000 (21:36 +0100)
wait_eb_writebacks() iterates the fs_info->buffer_tree xarray and waits
for the writeback of these extent-buffers. Waiting for writeback needs
to be done without the rcu_read_lock() held (because it can sleep) and
thus the loop drops the rcu_read_lock() before calling into
wait_on_extent_buffer_writeback(). But extent_buffers are freed through
RCU, so if btrfs_release_extent_buffer_rcu() is scheduled while we're
still waiting on the writeback, the extent_buffer will be freed causing
a use-after-free.

Similar to what is done in find_extent_buffer_nolock(), get a reference
to the extent_buffer before calling into
wait_on_extent_buffer_writeback() so a successful writeback does not
free the extent_buffer while we still have a reference to it.

Fixes: 2dd7e7bc0282 ("btrfs: zoned: wait for extent buffer IOs before finishing a zone")
Reviewed-by: Daniel Vacek <neelx@suse.com>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/zoned.c

index 43973d9809b3e33c13be93b5548e9e3f67270a19..a6df46c077d0e37364d61400dbacbb1d69967460 100644 (file)
@@ -2503,8 +2503,16 @@ static void wait_eb_writebacks(struct btrfs_block_group *block_group)
                        continue;
                if (eb->start >= end)
                        break;
+               /*
+                * Skip if we can't get the refcount for the extent buffer,
+                * i.e. because it was already written back. Otherwise an eb
+                * disappering underneath us can cause a use-after-free.
+                */
+               if (!refcount_inc_not_zero(&eb->refs))
+                       continue;
                rcu_read_unlock();
                wait_on_extent_buffer_writeback(eb);
+               free_extent_buffer(eb);
                rcu_read_lock();
        }
        rcu_read_unlock();