stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] userfaultfd: mark uffd_wp regardless of VM_WRITE flag
@ 2022-02-17 21:16 Nadav Amit
  2022-02-17 21:28 ` Nadav Amit
  2022-02-18  1:58 ` Peter Xu
  0 siblings, 2 replies; 12+ messages in thread
From: Nadav Amit @ 2022-02-17 21:16 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, Nadav Amit, Mike Rapoport, Andrea Arcangeli, Peter Xu, stable

From: Nadav Amit <namit@vmware.com>

When a PTE is set by UFFD operations such as UFFDIO_COPY, the PTE is
currently only marked as write-protected if the VMA has VM_WRITE flag
set. This seems incorrect or at least would be unexpected by the users.

Consider the following sequence of operations that are being performed
on a certain page:

	mprotect(PROT_READ)
	UFFDIO_COPY(UFFDIO_COPY_MODE_WP)
	mprotect(PROT_READ|PROT_WRITE)

At this point the user would expect to still get UFFD notification when
the page is accessed for write, but the user would not get one, since
the PTE was not marked as UFFD_WP during UFFDIO_COPY.

Fix it by always marking PTEs as UFFD_WP regardless on the
write-permission in the VMA flags.

Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Nadav Amit <namit@vmware.com>
---
 mm/userfaultfd.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 0780c2a57ff1..885e5adb0168 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -72,12 +72,15 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
 	_dst_pte = pte_mkdirty(_dst_pte);
 	if (page_in_cache && !vm_shared)
 		writable = false;
-	if (writable) {
-		if (wp_copy)
-			_dst_pte = pte_mkuffd_wp(_dst_pte);
-		else
-			_dst_pte = pte_mkwrite(_dst_pte);
-	}
+
+	/*
+	 * Always mark a PTE as write-protected when needed, regardless of
+	 * VM_WRITE, which the user might change.
+	 */
+	if (wp_copy)
+		_dst_pte = pte_mkuffd_wp(_dst_pte);
+	else if (writable)
+		_dst_pte = pte_mkwrite(_dst_pte);
 
 	dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-03-17  0:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-17 21:16 [PATCH] userfaultfd: mark uffd_wp regardless of VM_WRITE flag Nadav Amit
2022-02-17 21:28 ` Nadav Amit
2022-02-18  1:58 ` Peter Xu
2022-02-18  2:23   ` Nadav Amit
2022-02-18  3:56     ` Peter Xu
2022-02-18  4:00     ` Nadav Amit
2022-02-18  4:05       ` Nadav Amit
2022-03-16 22:05         ` Andrew Morton
2022-03-17  0:11           ` Peter Xu
2022-03-17  0:20             ` Andrew Morton
2022-02-21  6:23       ` Peter Xu
2022-02-28 18:31         ` Nadav Amit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).