linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Rick Edgecombe <rick.p.edgecombe@intel.com>
To: x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-mm@kvack.org, linux-arch@vger.kernel.org,
	linux-api@vger.kernel.org, Arnd Bergmann <arnd@arndb.de>,
	Andy Lutomirski <luto@kernel.org>,
	Balbir Singh <bsingharora@gmail.com>,
	Borislav Petkov <bp@alien8.de>,
	Cyrill Gorcunov <gorcunov@gmail.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Eugene Syromiatnikov <esyr@redhat.com>,
	Florian Weimer <fweimer@redhat.com>,
	"H . J . Lu" <hjl.tools@gmail.com>, Jann Horn <jannh@google.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Kees Cook <keescook@chromium.org>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Nadav Amit <nadav.amit@gmail.com>,
	Oleg Nesterov <oleg@redhat.com>, Pavel Machek <pavel@ucw.cz>,
	Peter Zijlstra <peterz@infradead.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Weijiang Yang <weijiang.yang@intel.com>,
	"Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>,
	John Allen <john.allen@amd.com>,
	kcc@google.com, eranian@google.com, rppt@kernel.org,
	jamorris@linux.microsoft.com, dethoma@microsoft.com,
	akpm@linux-foundation.org, Andrew.Cooper3@citrix.com,
	christina.schimpe@intel.com, david@redhat.com,
	debug@rivosinc.com, szabolcs.nagy@arm.com
Cc: rick.p.edgecombe@intel.com
Subject: [PATCH v8 26/40] mm: Warn on shadow stack memory in wrong vma
Date: Sat, 18 Mar 2023 17:15:21 -0700	[thread overview]
Message-ID: <20230319001535.23210-27-rick.p.edgecombe@intel.com> (raw)
In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com>

The x86 Control-flow Enforcement Technology (CET) feature includes a new
type of memory called shadow stack. This shadow stack memory has some
unusual properties, which requires some core mm changes to function
properly.

One sharp edge is that PTEs that are both Write=0 and Dirty=1 are
treated as shadow by the CPU, but this combination used to be created by
the kernel on x86. Previous patches have changed the kernel to now avoid
creating these PTEs unless they are for shadow stack memory. In case any
missed corners of the kernel are still creating PTEs like this for
non-shadow stack memory, and to catch any re-introductions of the logic,
warn if any shadow stack PTEs (Write=0, Dirty=1) are found in non-shadow
stack VMAs when they are being zapped. This won't catch transient cases
but should have decent coverage. It will be compiled out when shadow
stack is not configured.

In order to check if a PTE is shadow stack in core mm code, add two arch
breakouts arch_check_zapped_pte/pmd(). This will allow shadow stack
specific code to be kept in arch/x86.

Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Mike Rapoport (IBM) <rppt@kernel.org>
Tested-by: Pengfei Xu <pengfei.xu@intel.com>
Tested-by: John Allen <john.allen@amd.com>
Tested-by: Kees Cook <keescook@chromium.org>

---
v8:
 - Update commit log verbaige (Boris)

v6:
 - Add arch breakout to remove shstk from core MM code.

v5:
 - Fix typo in commit log

v3:
 - New patch
---
 arch/x86/include/asm/pgtable.h |  6 ++++++
 arch/x86/mm/pgtable.c          | 12 ++++++++++++
 include/linux/pgtable.h        | 14 ++++++++++++++
 mm/huge_memory.c               |  1 +
 mm/memory.c                    |  1 +
 5 files changed, 34 insertions(+)

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 2e3d8cca1195..e5b3dce0d9fe 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1684,6 +1684,12 @@ static inline bool arch_has_hw_pte_young(void)
 	return true;
 }
 
+#define arch_check_zapped_pte arch_check_zapped_pte
+void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte);
+
+#define arch_check_zapped_pmd arch_check_zapped_pmd
+void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd);
+
 #ifdef CONFIG_XEN_PV
 #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young
 static inline bool arch_has_hw_nonleaf_pmd_young(void)
diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c
index 98856bcc8102..afab0bc7862b 100644
--- a/arch/x86/mm/pgtable.c
+++ b/arch/x86/mm/pgtable.c
@@ -906,3 +906,15 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 
 	return pmd;
 }
+
+void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte)
+{
+	VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) &&
+			pte_shstk(pte));
+}
+
+void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd)
+{
+	VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) &&
+			pmd_shstk(pmd));
+}
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index c63cd44777ec..4a8970b9fb11 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -291,6 +291,20 @@ static inline bool arch_has_hw_pte_young(void)
 }
 #endif
 
+#ifndef arch_check_zapped_pte
+static inline void arch_check_zapped_pte(struct vm_area_struct *vma,
+					 pte_t pte)
+{
+}
+#endif
+
+#ifndef arch_check_zapped_pmd
+static inline void arch_check_zapped_pmd(struct vm_area_struct *vma,
+					 pmd_t pmd)
+{
+}
+#endif
+
 #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long address,
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index aaf815838144..24797be05fcb 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1689,6 +1689,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	 */
 	orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
 						tlb->fullmm);
+	arch_check_zapped_pmd(vma, orig_pmd);
 	tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 	if (vma_is_special_huge(vma)) {
 		if (arch_needs_pgtable_deposit())
diff --git a/mm/memory.c b/mm/memory.c
index d0972d2d6f36..c953c2c4588c 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1389,6 +1389,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 				continue;
 			ptent = ptep_get_and_clear_full(mm, addr, pte,
 							tlb->fullmm);
+			arch_check_zapped_pte(vma, ptent);
 			tlb_remove_tlb_entry(tlb, pte, addr);
 			zap_install_uffd_wp_if_needed(vma, addr, pte, details,
 						      ptent);
-- 
2.17.1



  parent reply	other threads:[~2023-03-19  0:16 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-19  0:14 [PATCH v8 00/40] Shadow stacks for userspace Rick Edgecombe
2023-03-19  0:14 ` [PATCH v8 01/40] Documentation/x86: Add CET shadow stack description Rick Edgecombe
2023-03-19  0:14 ` [PATCH v8 02/40] x86/shstk: Add Kconfig option for shadow stack Rick Edgecombe
2023-03-19  0:14 ` [PATCH v8 03/40] x86/cpufeatures: Add CPU feature flags for shadow stacks Rick Edgecombe
2023-03-19  0:14 ` [PATCH v8 04/40] x86/cpufeatures: Enable CET CR4 bit for shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 05/40] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 06/40] x86/fpu: Add helper for modifying xstate Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 07/40] x86/traps: Move control protection handler to separate file Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 08/40] x86/shstk: Add user control-protection fault handler Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 09/40] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 10/40] x86/mm: Move pmd_write(), pud_write() up in the file Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 11/40] mm: Introduce pte_mkwrite_kernel() Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 12/40] s390/mm: Introduce pmd_mkwrite_kernel() Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 13/40] mm: Make pte_mkwrite() take a VMA Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 14/40] x86/mm: Introduce _PAGE_SAVED_DIRTY Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 15/40] x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 16/40] x86/mm: Start actually marking _PAGE_SAVED_DIRTY Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 17/40] mm: Move VM_UFFD_MINOR_BIT from 37 to 38 Rick Edgecombe
2023-03-20 10:55   ` David Hildenbrand
2023-03-19  0:15 ` [PATCH v8 18/40] mm: Introduce VM_SHADOW_STACK for shadow stack memory Rick Edgecombe
2023-03-20 10:55   ` David Hildenbrand
2023-03-19  0:15 ` [PATCH v8 19/40] x86/mm: Check shadow stack page fault errors Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 20/40] x86/mm: Teach pte_mkwrite() about stack memory Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 21/40] mm: Add guard pages around a shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 22/40] mm/mmap: Add shadow stack pages to memory accounting Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 23/40] mm: Re-introduce vm_flags to do_mmap() Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 24/40] mm: Don't allow write GUPs to shadow stack memory Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 25/40] x86/mm: Introduce MAP_ABOVE4G Rick Edgecombe
2023-03-19  0:15 ` Rick Edgecombe [this message]
2023-03-20 11:00   ` [PATCH v8 26/40] mm: Warn on shadow stack memory in wrong vma David Hildenbrand
2023-03-19  0:15 ` [PATCH v8 27/40] x86/mm: Warn if create Write=0,Dirty=1 with raw prot Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 28/40] x86: Introduce userspace API for shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 29/40] x86/shstk: Add user-mode shadow stack support Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 30/40] x86/shstk: Handle thread shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 31/40] x86/shstk: Introduce routines modifying shstk Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 32/40] x86/shstk: Handle signals for shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 33/40] x86/shstk: Introduce map_shadow_stack syscall Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 34/40] x86/shstk: Support WRSS for userspace Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 35/40] x86: Expose thread features in /proc/$PID/status Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 36/40] x86/shstk: Wire in shadow stack interface Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 37/40] selftests/x86: Add shadow stack test Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 38/40] x86: Add PTRACE interface for shadow stack Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 39/40] x86/shstk: Add ARCH_SHSTK_UNLOCK Rick Edgecombe
2023-03-19  0:15 ` [PATCH v8 40/40] x86/shstk: Add ARCH_SHSTK_STATUS Rick Edgecombe
2023-03-19 14:00 ` [PATCH v8 00/40] Shadow stacks for userspace Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230319001535.23210-27-rick.p.edgecombe@intel.com \
    --to=rick.p.edgecombe@intel.com \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=bsingharora@gmail.com \
    --cc=christina.schimpe@intel.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=debug@rivosinc.com \
    --cc=dethoma@microsoft.com \
    --cc=eranian@google.com \
    --cc=esyr@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=gorcunov@gmail.com \
    --cc=hjl.tools@gmail.com \
    --cc=hpa@zytor.com \
    --cc=jamorris@linux.microsoft.com \
    --cc=jannh@google.com \
    --cc=john.allen@amd.com \
    --cc=kcc@google.com \
    --cc=keescook@chromium.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mike.kravetz@oracle.com \
    --cc=mingo@redhat.com \
    --cc=nadav.amit@gmail.com \
    --cc=oleg@redhat.com \
    --cc=pavel@ucw.cz \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=rppt@kernel.org \
    --cc=szabolcs.nagy@arm.com \
    --cc=tglx@linutronix.de \
    --cc=weijiang.yang@intel.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).