Skip to content

Commit d7a0883

Browse files
Qi Zhengakpm00
authored andcommitted
mm: userfaultfd: fix unexpected change to src_folio when UFFDIO_MOVE fails
After ptep_clear_flush(), if we find that src_folio is pinned we will fail UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to src_folio->{mapping,index} is not restored in this process. This is not what we expected, so fix it. This can cause the rmap for that page to be invalid, possibly resulting in memory corruption. At least swapout+migration would no longer work, because we might fail to locate the mappings of that folio. Link: https://lkml.kernel.org/r/20240222080815.46291-1-zhengqi.arch@bytedance.com Fixes: adef440 ("userfaultfd: UFFDIO_MOVE uABI") Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Suren Baghdasaryan <surenb@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 803de90 commit d7a0883

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

mm/userfaultfd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,6 @@ static int move_present_pte(struct mm_struct *mm,
914914
goto out;
915915
}
916916

917-
folio_move_anon_rmap(src_folio, dst_vma);
918-
WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
919-
920917
orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte);
921918
/* Folio got pinned from under us. Put it back and fail the move. */
922919
if (folio_maybe_dma_pinned(src_folio)) {
@@ -925,6 +922,9 @@ static int move_present_pte(struct mm_struct *mm,
925922
goto out;
926923
}
927924

925+
folio_move_anon_rmap(src_folio, dst_vma);
926+
WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr));
927+
928928
orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot);
929929
/* Follow mremap() behavior and treat the entry dirty after the move */
930930
orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma);

0 commit comments

Comments
 (0)