Skip to content

Commit 22b10cb

Browse files
inwardvesselgregkh
authored andcommitted
btrfs: prevent use-after-free on folio private data in btrfs_subpage_clear_uptodate()
This is a stable-only patch. The issue was inadvertently fixed in 6.17 [0] as part of a refactoring, but this patch serves as a minimal targeted fix for prior kernels. Users of filemap_lock_folio() need to guard against the situation where release_folio() has been invoked during reclaim but the folio was ultimately not removed from the page cache. This patch covers one location that was overlooked. After acquiring the folio, use set_folio_extent_mapped() to ensure the folio private state is valid. This is especially important in the subpage case, where the private field is an allocated struct containing bitmap and lock data. Without this protection, the race below is possible: [mm] page cache reclaim path [fs] relocation in subpage mode shrink_folio_list() folio_trylock() /* lock acquired */ filemap_release_folio() mapping->a_ops->release_folio() btrfs_release_folio() __btrfs_release_folio() clear_folio_extent_mapped() btrfs_detach_subpage() subpage = folio_detach_private(folio) btrfs_free_subpage(subpage) kfree(subpage) /* point A */ prealloc_file_extent_cluster() filemap_lock_folio() folio_try_get() /* inc refcount */ folio_lock() /* wait for lock */ if (...) ... else if (!mapping || !__remove_mapping(..)) /* * __remove_mapping() returns zero when * folio_ref_freeze(folio, refcount) fails /* point B */ */ goto keep_locked /* folio remains in cache */ keep_locked: folio_unlock(folio) /* lock released */ /* lock acquired */ btrfs_subpage_clear_uptodate() /* use-after-free */ subpage = folio_get_private(folio) [0] 4e346ba ("btrfs: reloc: unconditionally invalidate the page cache for each cluster") Fixes: 9d9ea1e ("btrfs: subpage: fix relocation potentially overwriting last page data") Cc: stable@vger.kernel.org # 6.10-6.16 Signed-off-by: JP Kobryn <inwardvessel@gmail.com> Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6214b2b commit 22b10cb

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

fs/btrfs/relocation.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,20 @@ static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control
28112811
* will re-read the whole page anyway.
28122812
*/
28132813
if (!IS_ERR(folio)) {
2814+
/*
2815+
* release_folio() could have cleared the folio private data
2816+
* while we were not holding the lock. Reset the mapping if
2817+
* needed so subpage operations can access a valid private
2818+
* folio state.
2819+
*/
2820+
ret = set_folio_extent_mapped(folio);
2821+
if (ret) {
2822+
folio_unlock(folio);
2823+
folio_put(folio);
2824+
2825+
return ret;
2826+
}
2827+
28142828
btrfs_subpage_clear_uptodate(fs_info, folio, i_size,
28152829
round_up(i_size, PAGE_SIZE) - i_size);
28162830
folio_unlock(folio);

0 commit comments

Comments
 (0)