[2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes

Message ID 20221110151702.1478763-3-peterx@redhat.com
State New
Headers
Series mm/migrate: Fix writable pte for read migration entry |

Commit Message

Peter Xu Nov. 10, 2022, 3:17 p.m. UTC
  Let's add one sanity check for CONFIG_DEBUG_VM on the write bit in whatever
chance we have when walking through the pgtables.  It can bring the error
earlier even before the app notices the data was corrupted on the snapshot.
Also it helps us to identify this is a wrong pgtable setup, so hopefully a
great information to have for debugging too.

Wrapping with CONFIG_DEBUG_VM is not that useful considering we have that
in many distros already, but still do that just in case some custom build
doesn't want anything like it.

Cc: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/include/asm/pgtable.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
  

Comments

Nadav Amit Nov. 10, 2022, 6:43 p.m. UTC | #1
On Nov 10, 2022, at 7:17 AM, Peter Xu <peterx@redhat.com> wrote:

> +#ifdef CONFIG_DEBUG_VM
> +	/*
> +	 * Having write bit for wr-protect-marked present ptes is fatal,
> +	 * because it means the uffd-wp bit will be ignored and write will
> +	 * just go through.
> +	 *
> +	 * Use any chance of pgtable walking to verify this (e.g., when
> +	 * page swapped out or being migrated for all purposes). It means
> +	 * something is already wrong.  Tell the admin even before the
> +	 * process crashes. We also nail it with wrong pgtable setup.
> +	 */
> +	WARN_ON_ONCE(wp && pte_write(pte));

How about VM_WARN_ON_ONCE() and no ifdef?
  
Peter Xu Nov. 10, 2022, 8:28 p.m. UTC | #2
On Thu, Nov 10, 2022 at 10:43:25AM -0800, Nadav Amit wrote:
> On Nov 10, 2022, at 7:17 AM, Peter Xu <peterx@redhat.com> wrote:
> 
> > +#ifdef CONFIG_DEBUG_VM
> > +	/*
> > +	 * Having write bit for wr-protect-marked present ptes is fatal,
> > +	 * because it means the uffd-wp bit will be ignored and write will
> > +	 * just go through.
> > +	 *
> > +	 * Use any chance of pgtable walking to verify this (e.g., when
> > +	 * page swapped out or being migrated for all purposes). It means
> > +	 * something is already wrong.  Tell the admin even before the
> > +	 * process crashes. We also nail it with wrong pgtable setup.
> > +	 */
> > +	WARN_ON_ONCE(wp && pte_write(pte));
> 
> How about VM_WARN_ON_ONCE() and no ifdef?

Oops.. Will quickly respin, thanks.
  

Patch

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 5059799bebe3..27fff6b14929 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -291,7 +291,21 @@  static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
 static inline int pte_uffd_wp(pte_t pte)
 {
-	return pte_flags(pte) & _PAGE_UFFD_WP;
+	bool wp = pte_flags(pte) & _PAGE_UFFD_WP;
+#ifdef CONFIG_DEBUG_VM
+	/*
+	 * Having write bit for wr-protect-marked present ptes is fatal,
+	 * because it means the uffd-wp bit will be ignored and write will
+	 * just go through.
+	 *
+	 * Use any chance of pgtable walking to verify this (e.g., when
+	 * page swapped out or being migrated for all purposes). It means
+	 * something is already wrong.  Tell the admin even before the
+	 * process crashes. We also nail it with wrong pgtable setup.
+	 */
+	WARN_ON_ONCE(wp && pte_write(pte));
+#endif
+	return wp;
 }
 
 static inline pte_t pte_mkuffd_wp(pte_t pte)