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>,
"Ravi V . Shankar" <ravi.v.shankar@intel.com>,
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
Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu <yu-cheng.yu@intel.com>
Subject: [PATCH v3 17/37] mm: Fixup places that call pte_mkwrite() directly
Date: Fri, 4 Nov 2022 15:35:44 -0700 [thread overview]
Message-ID: <20221104223604.29615-18-rick.p.edgecombe@intel.com> (raw)
In-Reply-To: <20221104223604.29615-1-rick.p.edgecombe@intel.com>
From: Yu-cheng Yu <yu-cheng.yu@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.
With the introduction of shadow stack memory there are two ways a pte can
be writable: regular writable memory and shadow stack memory.
In past patches, maybe_mkwrite() has been updated to apply pte_mkwrite()
or pte_mkwrite_shstk() depending on the VMA flag. This covers most cases
where a PTE is made writable. However, there are places where pte_mkwrite()
is called directly and the logic should now also create a shadow stack PTE
in the case of a shadow stack VMA.
- do_anonymous_page() and migrate_vma_insert_page() check VM_WRITE
directly and call pte_mkwrite(). Teach it about pte_mkwrite_shstk()
- When userfaultfd is creating a PTE after userspace handles the fault
it calls pte_mkwrite() directly. Teach it about pte_mkwrite_shstk()
To make the code cleaner, introduce is_shstk_write() which simplifies
checking for VM_WRITE | VM_SHADOW_STACK together.
In other cases where pte_mkwrite() is called directly, the VMA will not
be VM_SHADOW_STACK, and so shadow stack memory should not be created.
- In the case of pte_savedwrite(), shadow stack VMA's are excluded.
- In the case of the "dirty_accountable" optimization in mprotect(),
shadow stack VMA's won't be VM_SHARED, so it is not nessary.
Tested-by: Pengfei Xu <pengfei.xu@intel.com>
Tested-by: John Allen <john.allen@amd.com>
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: Kees Cook <keescook@chromium.org>
---
v3:
- Restore do_anonymous_page() that accidetally moved commits (Kirill)
- Open code maybe_mkwrite() cases from v2, so the behavior doesn't change
to mark that non-writable PTEs dirty. (Nadav)
v2:
- Updated commit log with comment's from Dave Hansen
- Dave also suggested (I understood) to maybe tweak vm_get_page_prot()
to avoid having to call maybe_mkwrite(). After playing around with
this I opted to *not* do this. Shadow stack memory memory is
effectively writable, so having the default permissions be writable
ended up mapping the zero page as writable and other surprises. So
creating shadow stack memory needs to be done with manual logic
like pte_mkwrite().
- Drop change in change_pte_range() because it couldn't actually trigger
for shadow stack VMAs.
- Clarify reasoning for skipped cases of pte_mkwrite().
Yu-cheng v25:
- Apply same changes to do_huge_pmd_numa_page() as to do_numa_page().
arch/x86/include/asm/pgtable.h | 3 +++
arch/x86/mm/pgtable.c | 6 ++++++
include/linux/pgtable.h | 7 +++++++
mm/memory.c | 5 ++++-
mm/migrate_device.c | 4 +++-
mm/userfaultfd.c | 10 +++++++---
6 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index df67bcf9f69e..d57dc1b2d3e8 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -919,6 +919,9 @@ static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
}
#endif /* CONFIG_PAGE_TABLE_ISOLATION */
+#define is_shstk_write is_shstk_write
+extern bool is_shstk_write(unsigned long vm_flags);
+
#endif /* __ASSEMBLY__ */
diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c
index 8525f2876fb4..f0e536bea3ca 100644
--- a/arch/x86/mm/pgtable.c
+++ b/arch/x86/mm/pgtable.c
@@ -876,3 +876,9 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
#endif /* CONFIG_X86_64 */
#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
+
+bool is_shstk_write(unsigned long vm_flags)
+{
+ return (vm_flags & (VM_SHADOW_STACK | VM_WRITE)) ==
+ (VM_SHADOW_STACK | VM_WRITE);
+}
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 5ce6732a6b65..36926a207b6d 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1567,6 +1567,13 @@ static inline bool arch_has_pfn_modify_check(void)
}
#endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */
+#ifndef is_shstk_write
+static inline bool is_shstk_write(unsigned long vm_flags)
+{
+ return false;
+}
+#endif
+
/*
* Architecture PAGE_KERNEL_* fallbacks
*
diff --git a/mm/memory.c b/mm/memory.c
index f88c351aecd4..b9bee283aad3 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4128,7 +4128,10 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
entry = mk_pte(page, vma->vm_page_prot);
entry = pte_sw_mkyoung(entry);
- if (vma->vm_flags & VM_WRITE)
+
+ if (is_shstk_write(vma->vm_flags))
+ entry = pte_mkwrite_shstk(pte_mkdirty(entry));
+ else if (vma->vm_flags & VM_WRITE)
entry = pte_mkwrite(pte_mkdirty(entry));
vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 6fa682eef7a0..4c21c600bf46 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -641,7 +641,9 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate,
goto abort;
}
entry = mk_pte(page, vma->vm_page_prot);
- if (vma->vm_flags & VM_WRITE)
+ if (is_shstk_write(vma->vm_flags))
+ entry = pte_mkwrite_shstk(pte_mkdirty(entry));
+ else if (vma->vm_flags & VM_WRITE)
entry = pte_mkwrite(pte_mkdirty(entry));
}
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 3d0fef3980b3..503135b079b6 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -63,6 +63,7 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
int ret;
pte_t _dst_pte, *dst_pte;
bool writable = dst_vma->vm_flags & VM_WRITE;
+ bool shstk = dst_vma->vm_flags & VM_SHADOW_STACK;
bool vm_shared = dst_vma->vm_flags & VM_SHARED;
bool page_in_cache = page->mapping;
spinlock_t *ptl;
@@ -83,9 +84,12 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
writable = false;
}
- if (writable)
- _dst_pte = pte_mkwrite(_dst_pte);
- else
+ if (writable) {
+ if (shstk)
+ _dst_pte = pte_mkwrite_shstk(_dst_pte);
+ else
+ _dst_pte = pte_mkwrite(_dst_pte);
+ } else
/*
* We need this to make sure write bit removed; as mk_pte()
* could return a pte with write bit set.
--
2.17.1
next prev parent reply other threads:[~2022-11-04 22:42 UTC|newest]
Thread overview: 104+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-04 22:35 [PATCH v3 00/37] Shadow stacks for userspace Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 01/37] Documentation/x86: Add CET description Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 02/37] x86/cet/shstk: Add Kconfig option for Shadow Stack Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 03/37] x86/cpufeatures: Add CPU feature flags for shadow stacks Rick Edgecombe
2022-11-07 17:41 ` Borislav Petkov
2022-11-04 22:35 ` [PATCH v3 04/37] x86/cpufeatures: Enable CET CR4 bit for shadow stack Rick Edgecombe
2022-11-07 18:00 ` Borislav Petkov
2022-11-07 18:19 ` Edgecombe, Rick P
2022-11-07 18:37 ` Borislav Petkov
2022-11-07 19:19 ` Edgecombe, Rick P
2022-11-07 19:30 ` Borislav Petkov
2022-11-07 19:33 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 05/37] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 06/37] x86/fpu: Add helper for modifying xstate Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 07/37] x86/cet: Add user control-protection fault handler Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 08/37] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 09/37] x86/mm: Move pmd_write(), pud_write() up in the file Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 10/37] x86/mm: Introduce _PAGE_COW Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 11/37] x86/mm: Update pte_modify for _PAGE_COW Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 12/37] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 13/37] mm: Move VM_UFFD_MINOR_BIT from 37 to 38 Rick Edgecombe
2022-11-15 11:20 ` Peter Zijlstra
2022-11-15 17:18 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 14/37] mm: Introduce VM_SHADOW_STACK for shadow stack memory Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 15/37] x86/mm: Check Shadow Stack page fault errors Rick Edgecombe
2022-11-15 11:47 ` Peter Zijlstra
2022-11-15 20:03 ` Edgecombe, Rick P
2022-11-15 21:07 ` Peter Zijlstra
2022-11-15 23:13 ` Edgecombe, Rick P
2022-11-16 10:09 ` Peter Zijlstra
2022-11-04 22:35 ` [PATCH v3 16/37] x86/mm: Update maybe_mkwrite() for shadow stack Rick Edgecombe
2022-11-04 22:35 ` Rick Edgecombe [this message]
2022-11-04 22:35 ` [PATCH v3 18/37] mm: Add guard pages around a " Rick Edgecombe
2022-11-15 12:04 ` Peter Zijlstra
2022-11-15 20:40 ` Edgecombe, Rick P
2022-11-15 20:56 ` Peter Zijlstra
2022-11-15 21:49 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 19/37] mm/mmap: Add shadow stack pages to memory accounting Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 20/37] mm/mprotect: Exclude shadow stack from preserve_write Rick Edgecombe
2022-11-15 12:05 ` Peter Zijlstra
2022-11-15 20:41 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 21/37] mm: Re-introduce vm_flags to do_mmap() Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 22/37] mm: Don't allow write GUPs to shadow stack memory Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 23/37] mm: Warn on shadow stack memory in wrong vma Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 24/37] x86: Introduce userspace API for CET enabling Rick Edgecombe
2022-11-15 12:26 ` Peter Zijlstra
2022-11-15 13:03 ` Peter Zijlstra
2022-11-15 20:55 ` Edgecombe, Rick P
2022-11-15 14:25 ` Peter Zijlstra
2022-11-15 20:55 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 25/37] x86/shstk: Add user-mode shadow stack support Rick Edgecombe
2022-11-15 12:32 ` Peter Zijlstra
2022-11-15 21:46 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 26/37] x86/shstk: Handle thread shadow stack Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 27/37] x86/shstk: Introduce routines modifying shstk Rick Edgecombe
2022-11-15 14:18 ` Peter Zijlstra
2022-11-15 23:42 ` Edgecombe, Rick P
2022-11-16 10:18 ` Peter Zijlstra
2022-11-16 22:38 ` Edgecombe, Rick P
2022-11-17 14:17 ` Peter Zijlstra
2022-11-18 17:05 ` Edgecombe, Rick P
2022-11-15 14:22 ` Peter Zijlstra
2022-11-15 20:56 ` Edgecombe, Rick P
2022-11-04 22:35 ` [PATCH v3 28/37] x86/shstk: Handle signals for shadow stack Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 29/37] x86/shstk: Introduce map_shadow_stack syscall Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 30/37] x86/shstk: Support wrss for userspace Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 31/37] x86: Expose thread features in /proc/$PID/status Rick Edgecombe
2022-11-04 22:35 ` [PATCH v3 32/37] x86/cet/shstk: Wire in CET interface Rick Edgecombe
2022-11-04 22:36 ` [PATCH v3 33/37] selftests/x86: Add shadow stack test Rick Edgecombe
2022-11-04 22:36 ` [PATCH v3 34/37] x86/fpu: Add helper for initing features Rick Edgecombe
2022-11-04 22:36 ` [PATCH v3 35/37] x86/cet: Add PTRACE interface for CET Rick Edgecombe
2022-11-15 14:43 ` Peter Zijlstra
2022-11-15 22:23 ` Edgecombe, Rick P
2022-11-17 12:25 ` Schimpe, Christina
2022-11-17 14:14 ` Peter Zijlstra
2022-11-18 17:20 ` Edgecombe, Rick P
2022-11-18 17:25 ` Schimpe, Christina
2022-11-17 19:57 ` Edgecombe, Rick P
2022-11-18 16:21 ` Schimpe, Christina
2022-11-18 17:18 ` Edgecombe, Rick P
2022-11-21 7:40 ` Mike Rapoport
2022-11-21 15:52 ` Edgecombe, Rick P
2022-11-22 9:36 ` Mike Rapoport
2022-11-04 22:36 ` [PATCH v3 36/37] x86/cet/shstk: Add ARCH_CET_UNLOCK Rick Edgecombe
2022-11-15 14:47 ` Peter Zijlstra
2022-11-15 20:01 ` Edgecombe, Rick P
2022-11-15 20:57 ` Peter Zijlstra
2022-11-15 21:00 ` Dave Hansen
2022-11-15 21:21 ` Peter Zijlstra
2022-11-04 22:36 ` [RFC 37/37] fs/binfmt_elf: Block old shstk elf bit Rick Edgecombe
2022-11-04 22:56 ` H.J. Lu
2022-11-06 9:33 ` Florian Weimer
2022-11-07 16:49 ` Edgecombe, Rick P
2022-11-07 16:55 ` Florian Weimer
2022-11-07 17:37 ` Edgecombe, Rick P
2022-11-07 19:10 ` H.J. Lu
2022-11-07 21:10 ` Edgecombe, Rick P
2022-11-07 21:21 ` H.J. Lu
2022-11-07 21:34 ` Edgecombe, Rick P
2022-11-07 21:47 ` H.J. Lu
2022-11-07 22:46 ` Edgecombe, Rick P
2022-11-07 23:45 ` H.J. Lu
2022-11-08 9:14 ` Florian Weimer
2022-11-07 16:49 ` Edgecombe, Rick P
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=20221104223604.29615-18-rick.p.edgecombe@intel.com \
--to=rick.p.edgecombe@intel.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=bp@alien8.de \
--cc=bsingharora@gmail.com \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.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=ravi.v.shankar@intel.com \
--cc=rdunlap@infradead.org \
--cc=rppt@kernel.org \
--cc=tglx@linutronix.de \
--cc=weijiang.yang@intel.com \
--cc=x86@kernel.org \
--cc=yu-cheng.yu@intel.com \
/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).