Skip to content

Commit 0dff1b4

Browse files
xzpeterakpm00
authored andcommitted
mm/pagemap: fix ioctl(PAGEMAP_SCAN) on vma check
Patch series "mm/pagemap: A few fixes to the recent PAGEMAP_SCAN". This series should fix two known reports from syzbot on the new PAGEMAP_SCAN ioctl(): https://lore.kernel.org/all/000000000000b0e576060a30ee3b@google.com/ https://lore.kernel.org/all/000000000000773fa7060a31e2cc@google.com/ The 3rd patch is something I found when testing these patches. This patch (of 3): The new ioctl(PAGEMAP_SCAN) relies on vma wr-protect capability provided by userfault, however in the vma test it didn't explicitly require the vma to have wr-protect function enabled, even if PM_SCAN_WP_MATCHING flag is set. It means the pagemap code can now apply uffd-wp bit to a page in the vma even if not registered to userfaultfd at all. Then in whatever way as long as the pte got written and page fault resolved, we'll apply the write bit even if uffd-wp bit is set. We'll see a pte that has both UFFD_WP and WRITE bit set. Anything later that looks up the pte for uffd-wp bit will trigger the warning: WARNING: CPU: 1 PID: 5071 at arch/x86/include/asm/pgtable.h:403 pte_uffd_wp arch/x86/include/asm/pgtable.h:403 [inline] Fix it by doing proper check over the vma attributes when PM_SCAN_WP_MATCHING is specified. Link: https://lkml.kernel.org/r/20231116201547.536857-1-peterx@redhat.com Link: https://lkml.kernel.org/r/20231116201547.536857-2-peterx@redhat.com Fixes: 52526ca ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs") Signed-off-by: Peter Xu <peterx@redhat.com> Reported-by: syzbot+e94c5aaf7890901ebf9b@syzkaller.appspotmail.com Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Andrei Vagin <avagin@gmail.com> Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 5f79489 commit 0dff1b4

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

fs/proc/task_mmu.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,15 +1982,31 @@ static int pagemap_scan_test_walk(unsigned long start, unsigned long end,
19821982
struct pagemap_scan_private *p = walk->private;
19831983
struct vm_area_struct *vma = walk->vma;
19841984
unsigned long vma_category = 0;
1985+
bool wp_allowed = userfaultfd_wp_async(vma) &&
1986+
userfaultfd_wp_use_markers(vma);
19851987

1986-
if (userfaultfd_wp_async(vma) && userfaultfd_wp_use_markers(vma))
1987-
vma_category |= PAGE_IS_WPALLOWED;
1988-
else if (p->arg.flags & PM_SCAN_CHECK_WPASYNC)
1989-
return -EPERM;
1988+
if (!wp_allowed) {
1989+
/* User requested explicit failure over wp-async capability */
1990+
if (p->arg.flags & PM_SCAN_CHECK_WPASYNC)
1991+
return -EPERM;
1992+
/*
1993+
* User requires wr-protect, and allows silently skipping
1994+
* unsupported vmas.
1995+
*/
1996+
if (p->arg.flags & PM_SCAN_WP_MATCHING)
1997+
return 1;
1998+
/*
1999+
* Then the request doesn't involve wr-protects at all,
2000+
* fall through to the rest checks, and allow vma walk.
2001+
*/
2002+
}
19902003

19912004
if (vma->vm_flags & VM_PFNMAP)
19922005
return 1;
19932006

2007+
if (wp_allowed)
2008+
vma_category |= PAGE_IS_WPALLOWED;
2009+
19942010
if (!pagemap_scan_is_interesting_vma(vma_category, p))
19952011
return 1;
19962012

0 commit comments

Comments
 (0)