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, Yu-cheng Yu <yu-cheng.yu@intel.com>
Subject: [PATCH v8 31/40] x86/shstk: Introduce routines modifying shstk
Date: Sat, 18 Mar 2023 17:15:26 -0700	[thread overview]
Message-ID: <20230319001535.23210-32-rick.p.edgecombe@intel.com> (raw)
In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com>

Shadow stacks are normally written to via CALL/RET or specific CET
instructions like RSTORSSP/SAVEPREVSSP. However, sometimes the kernel will
need to write to the shadow stack directly using the ring-0 only WRUSS
instruction.

A shadow stack restore token marks a restore point of the shadow stack, and
the address in a token must point directly above the token, which is within
the same shadow stack. This is distinctively different from other pointers
on the shadow stack, since those pointers point to executable code area.

Introduce token setup and verify routines. Also introduce WRUSS, which is
a kernel-mode instruction but writes directly to user shadow stack.

In future patches that enable shadow stack to work with signals, the kernel
will need something to denote the point in the stack where sigreturn may be
called. This will prevent attackers calling sigreturn at arbitrary places
in the stack, in order to help prevent SROP attacks.

To do this, something that can only be written by the kernel needs to be
placed on the shadow stack. This can be accomplished by setting bit 63 in
the frame written to the shadow stack. Userspace return addresses can't
have this bit set as it is in the kernel range. It also can't be a valid
restore token.

Co-developed-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
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 verbiage (Boris)
 - Use define instead of magic BIT(63) (Boris)

v5:
 - Fix typo in commit log

v3:
 - Drop shstk_check_rstor_token()
 - Fail put_shstk_data() if bit 63 is set in the data (Kees)
 - Add comment in create_rstor_token() (Kees)
 - Pull in create_rstor_token() changes from future patch (Kees)

v2:
 - Add data helpers for writing to shadow stack.
---
 arch/x86/include/asm/special_insns.h | 13 +++++
 arch/x86/kernel/shstk.c              | 75 ++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)

diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h
index de48d1389936..d6cd9344f6c7 100644
--- a/arch/x86/include/asm/special_insns.h
+++ b/arch/x86/include/asm/special_insns.h
@@ -202,6 +202,19 @@ static inline void clwb(volatile void *__p)
 		: [pax] "a" (p));
 }
 
+#ifdef CONFIG_X86_USER_SHADOW_STACK
+static inline int write_user_shstk_64(u64 __user *addr, u64 val)
+{
+	asm_volatile_goto("1: wrussq %[val], (%[addr])\n"
+			  _ASM_EXTABLE(1b, %l[fail])
+			  :: [addr] "r" (addr), [val] "r" (val)
+			  :: fail);
+	return 0;
+fail:
+	return -EFAULT;
+}
+#endif /* CONFIG_X86_USER_SHADOW_STACK */
+
 #define nop() asm volatile ("nop")
 
 static inline void serialize(void)
diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c
index bd9cdc3a7338..e22928c63ffc 100644
--- a/arch/x86/kernel/shstk.c
+++ b/arch/x86/kernel/shstk.c
@@ -25,6 +25,8 @@
 #include <asm/fpu/api.h>
 #include <asm/prctl.h>
 
+#define SS_FRAME_SIZE 8
+
 static bool features_enabled(unsigned long features)
 {
 	return current->thread.features & features;
@@ -40,6 +42,35 @@ static void features_clr(unsigned long features)
 	current->thread.features &= ~features;
 }
 
+/*
+ * Create a restore token on the shadow stack.  A token is always 8-byte
+ * and aligned to 8.
+ */
+static int create_rstor_token(unsigned long ssp, unsigned long *token_addr)
+{
+	unsigned long addr;
+
+	/* Token must be aligned */
+	if (!IS_ALIGNED(ssp, 8))
+		return -EINVAL;
+
+	addr = ssp - SS_FRAME_SIZE;
+
+	/*
+	 * SSP is aligned, so reserved bits and mode bit are a zero, just mark
+	 * the token 64-bit.
+	 */
+	ssp |= BIT(0);
+
+	if (write_user_shstk_64((u64 __user *)addr, (u64)ssp))
+		return -EFAULT;
+
+	if (token_addr)
+		*token_addr = addr;
+
+	return 0;
+}
+
 static unsigned long alloc_shstk(unsigned long size)
 {
 	int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G;
@@ -157,6 +188,50 @@ unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long cl
 	return addr + size;
 }
 
+static unsigned long get_user_shstk_addr(void)
+{
+	unsigned long long ssp;
+
+	fpregs_lock_and_load();
+
+	rdmsrl(MSR_IA32_PL3_SSP, ssp);
+
+	fpregs_unlock();
+
+	return ssp;
+}
+
+#define SHSTK_DATA_BIT BIT(63)
+
+static int put_shstk_data(u64 __user *addr, u64 data)
+{
+	if (WARN_ON_ONCE(data & SHSTK_DATA_BIT))
+		return -EINVAL;
+
+	/*
+	 * Mark the high bit so that the sigframe can't be processed as a
+	 * return address.
+	 */
+	if (write_user_shstk_64(addr, data | SHSTK_DATA_BIT))
+		return -EFAULT;
+	return 0;
+}
+
+static int get_shstk_data(unsigned long *data, unsigned long __user *addr)
+{
+	unsigned long ldata;
+
+	if (unlikely(get_user(ldata, addr)))
+		return -EFAULT;
+
+	if (!(ldata & SHSTK_DATA_BIT))
+		return -EINVAL;
+
+	*data = ldata & ~SHSTK_DATA_BIT;
+
+	return 0;
+}
+
 void shstk_free(struct task_struct *tsk)
 {
 	struct thread_shstk *shstk = &tsk->thread.shstk;
-- 
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 ` [PATCH v8 26/40] mm: Warn on shadow stack memory in wrong vma Rick Edgecombe
2023-03-20 11:00   ` 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 ` Rick Edgecombe [this message]
2023-03-19  0:15 ` [PATCH v8 32/40] x86/shstk: Handle signals for " 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-32-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 \
    --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).