diff mbox series

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

Message ID 20221110203132.1498183-3-peterx@redhat.com (mailing list archive)
State New
Headers show
Series mm/migrate: Fix writable pte for read migration entry | expand

Commit Message

Peter Xu Nov. 10, 2022, 8:31 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.

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

kernel test robot Nov. 11, 2022, 10:06 p.m. UTC | #1
Hi Peter,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Peter-Xu/mm-migrate-Fix-writable-pte-for-read-migration-entry/20221111-043302
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20221110203132.1498183-3-peterx%40redhat.com
patch subject: [PATCH v2 2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes
config: x86_64-randconfig-a015
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/b103eae2926a263a15e262611524ca9efa8b969e
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Peter-Xu/mm-migrate-Fix-writable-pte-for-read-migration-entry/20221111-043302
        git checkout b103eae2926a263a15e262611524ca9efa8b969e
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from include/linux/pgtable.h:6,
                    from mm/hugetlb_vmemmap.c:13:
   arch/x86/include/asm/pgtable.h: In function 'pte_uffd_wp':
>> arch/x86/include/asm/pgtable.h:306:9: error: implicit declaration of function 'VM_WARN_ON_ONCE'; did you mean 'WARN_ON_ONCE'? [-Werror=implicit-function-declaration]
     306 |         VM_WARN_ON_ONCE(wp && pte_write(pte));
         |         ^~~~~~~~~~~~~~~
         |         WARN_ON_ONCE
   cc1: some warnings being treated as errors


vim +306 arch/x86/include/asm/pgtable.h

   290	
   291	#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
   292	static inline int pte_uffd_wp(pte_t pte)
   293	{
   294		bool wp = pte_flags(pte) & _PAGE_UFFD_WP;
   295	
   296		/*
   297		 * Having write bit for wr-protect-marked present ptes is fatal,
   298		 * because it means the uffd-wp bit will be ignored and write will
   299		 * just go through.
   300		 *
   301		 * Use any chance of pgtable walking to verify this (e.g., when
   302		 * page swapped out or being migrated for all purposes). It means
   303		 * something is already wrong.  Tell the admin even before the
   304		 * process crashes. We also nail it with wrong pgtable setup.
   305		 */
 > 306		VM_WARN_ON_ONCE(wp && pte_write(pte));
   307	
   308		return wp;
   309	}
   310
kernel test robot Nov. 12, 2022, 2:59 a.m. UTC | #2
Hi Peter,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Peter-Xu/mm-migrate-Fix-writable-pte-for-read-migration-entry/20221111-043302
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20221110203132.1498183-3-peterx%40redhat.com
patch subject: [PATCH v2 2/2] mm/uffd: Sanity check write bit for uffd-wp protected ptes
config: x86_64-randconfig-a012
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/b103eae2926a263a15e262611524ca9efa8b969e
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Peter-Xu/mm-migrate-Fix-writable-pte-for-read-migration-entry/20221111-043302
        git checkout b103eae2926a263a15e262611524ca9efa8b969e
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from arch/x86/kernel/early_printk.c:11:
   In file included from include/linux/pgtable.h:6:
>> arch/x86/include/asm/pgtable.h:306:2: error: implicit declaration of function 'VM_WARN_ON_ONCE' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           VM_WARN_ON_ONCE(wp && pte_write(pte));
           ^
   1 error generated.


vim +/VM_WARN_ON_ONCE +306 arch/x86/include/asm/pgtable.h

   290	
   291	#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
   292	static inline int pte_uffd_wp(pte_t pte)
   293	{
   294		bool wp = pte_flags(pte) & _PAGE_UFFD_WP;
   295	
   296		/*
   297		 * Having write bit for wr-protect-marked present ptes is fatal,
   298		 * because it means the uffd-wp bit will be ignored and write will
   299		 * just go through.
   300		 *
   301		 * Use any chance of pgtable walking to verify this (e.g., when
   302		 * page swapped out or being migrated for all purposes). It means
   303		 * something is already wrong.  Tell the admin even before the
   304		 * process crashes. We also nail it with wrong pgtable setup.
   305		 */
 > 306		VM_WARN_ON_ONCE(wp && pte_write(pte));
   307	
   308		return wp;
   309	}
   310
Peter Xu Nov. 13, 2022, 10:33 p.m. UTC | #3
On Sat, Nov 12, 2022 at 06:06:22AM +0800, kernel test robot wrote:
> vim +306 arch/x86/include/asm/pgtable.h
> 
>    290	
>    291	#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
>    292	static inline int pte_uffd_wp(pte_t pte)
>    293	{
>    294		bool wp = pte_flags(pte) & _PAGE_UFFD_WP;
>    295	
>    296		/*
>    297		 * Having write bit for wr-protect-marked present ptes is fatal,
>    298		 * because it means the uffd-wp bit will be ignored and write will
>    299		 * just go through.
>    300		 *
>    301		 * Use any chance of pgtable walking to verify this (e.g., when
>    302		 * page swapped out or being migrated for all purposes). It means
>    303		 * something is already wrong.  Tell the admin even before the
>    304		 * process crashes. We also nail it with wrong pgtable setup.
>    305		 */
>  > 306		VM_WARN_ON_ONCE(wp && pte_write(pte));

I just saw the comment in check_pgprot() right below:

	/* mmdebug.h can not be included here because of dependencies */
#ifdef CONFIG_DEBUG_VM
...
#endif

I'll repost, will be the same as v1 code-wise.
diff mbox series

Patch

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 5059799bebe3..64141acf70c8 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;
+
+	/*
+	 * 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.
+	 */
+	VM_WARN_ON_ONCE(wp && pte_write(pte));
+
+	return wp;
 }
 
 static inline pte_t pte_mkuffd_wp(pte_t pte)