From: Mike Rapoport <rppt@kernel.org>
To: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: 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, jamorris@linux.microsoft.com,
dethoma@microsoft.com, akpm@linux-foundation.org,
Andrew.Cooper3@citrix.com, christina.schimpe@intel.com,
Yu-cheng Yu <yu-cheng.yu@intel.com>
Subject: Re: [PATCH v4 37/39] x86: Add PTRACE interface for shadow stack
Date: Fri, 9 Dec 2022 19:04:24 +0200 [thread overview]
Message-ID: <Y5NqmLqXfXpowoSM@kernel.org> (raw)
In-Reply-To: <20221203003606.6838-38-rick.p.edgecombe@intel.com>
Hi Rick,
On Fri, Dec 02, 2022 at 04:36:04PM -0800, Rick Edgecombe wrote:
> From: Yu-cheng Yu <yu-cheng.yu@intel.com>
>
> Some applications (like GDB) would like to tweak shadow stack state via
> ptrace. This allows for existing functionality to continue to work for
> seized shadow stack applications. Provide an regset interface for
> manipulating the shadow stack pointer (SSP).
>
> There is already ptrace functionality for accessing xstate, but this
> does not include supervisor xfeatures. So there is not a completely
> clear place for where to put the shadow stack state. Adding it to the
> user xfeatures regset would complicate that code, as it currently shares
> logic with signals which should not have supervisor features.
>
> Don't add a general supervisor xfeature regset like the user one,
> because it is better to maintain flexibility for other supervisor
> xfeatures to define their own interface. For example, an xfeature may
> decide not to expose all of it's state to userspace, as is actually the
> case for shadow stack ptrace functionality. A lot of enum values remain
> to be used, so just put it in dedicated shadow stack regset.
>
> The only downside to not having a generic supervisor xfeature regset,
> is that apps need to be enlightened of any new supervisor xfeature
> exposed this way (i.e. they can't try to have generic save/restore
> logic). But maybe that is a good thing, because they have to think
> through each new xfeature instead of encountering issues when new a new
> supervisor xfeature was added.
>
> By adding a shadow stack regset, it also has the effect of including the
> shadow stack state in a core dump, which could be useful for debugging.
>
> The shadow stack specific xstate includes the SSP, and the shadow stack
> and WRSS enablement status. Enabling shadow stack or wrss in the kernel
> involves more than just flipping the bit. The kernel is made aware that
> it has to do extra things when cloning or handling signals. That logic
> is triggered off of separate feature enablement state kept in the task
> struct. So the flipping on HW shadow stack enforcement without notifying
> the kernel to change its behavior would severely limit what an application
> could do without crashing, and the results would depend on kernel
> internal implementation details. There is also no known use for controlling
> this state via prtace today. So only expose the SSP, which is something
> that userspace already has indirect control over.
>
> Tested-by: Pengfei Xu <pengfei.xu@intel.com>
> Tested-by: John Allen <john.allen@amd.com>
> Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
> Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
> ---
>
> v4:
> - Make shadow stack only. Reduce to only supporting SSP register, and
> remove CET references (peterz)
> - Add comment to not use 0x203, becuase binutils already looks for it in
> coredumps. (Christina Schimpe)
>
> v3:
> - Drop dependence on thread.shstk.size, and use thread.features bits
> - Drop 32 bit support
>
> v2:
> - Check alignment on ssp.
> - Block IBT bits.
> - Handle init states instead of returning error.
> - Add verbose commit log justifying the design.
>
> Yu-Cheng v12:
> - Return -ENODEV when CET registers are in INIT state.
> - Check reserved/non-support bits from user input.
>
> arch/x86/include/asm/fpu/regset.h | 7 +--
> arch/x86/kernel/fpu/regset.c | 87 +++++++++++++++++++++++++++++++
> arch/x86/kernel/ptrace.c | 12 +++++
> include/uapi/linux/elf.h | 2 +
> 4 files changed, 105 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/include/asm/fpu/regset.h b/arch/x86/include/asm/fpu/regset.h
> index 4f928d6a367b..697b77e96025 100644
> --- a/arch/x86/include/asm/fpu/regset.h
> +++ b/arch/x86/include/asm/fpu/regset.h
> @@ -7,11 +7,12 @@
>
> #include <linux/regset.h>
>
> -extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
> +extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active,
> + ssp_active;
> extern user_regset_get2_fn fpregs_get, xfpregs_get, fpregs_soft_get,
> - xstateregs_get;
> + xstateregs_get, ssp_get;
> extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
> - xstateregs_set;
> + xstateregs_set, ssp_set;
>
> /*
> * xstateregs_active == regset_fpregs_active. Please refer to the comment
> diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c
> index 6d056b68f4ed..00f3d5c9b682 100644
> --- a/arch/x86/kernel/fpu/regset.c
> +++ b/arch/x86/kernel/fpu/regset.c
> @@ -8,6 +8,7 @@
> #include <asm/fpu/api.h>
> #include <asm/fpu/signal.h>
> #include <asm/fpu/regset.h>
> +#include <asm/prctl.h>
>
> #include "context.h"
> #include "internal.h"
> @@ -174,6 +175,92 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
> return ret;
> }
>
> +
> +#ifdef CONFIG_X86_USER_SHADOW_STACK
> +int ssp_active(struct task_struct *target, const struct user_regset *regset)
> +{
> + if (shstk_enabled())
This is not going to work with ptrace as shstk_enabled() checks current
rather than target.
> + return regset->n;
> +
> + return 0;
> +}
> +
> +int ssp_get(struct task_struct *target, const struct user_regset *regset,
> + struct membuf to)
> +{
> + struct fpu *fpu = &target->thread.fpu;
> + struct cet_user_state *cetregs;
> +
> + if (!boot_cpu_has(X86_FEATURE_USER_SHSTK))
> + return -ENODEV;
> +
> + sync_fpstate(fpu);
> + cetregs = get_xsave_addr(&fpu->fpstate->regs.xsave, XFEATURE_CET_USER);
> + if (!cetregs) {
> + /*
> + * The registers are the in the init state. The init values for
> + * these regs are zero, so just zero the output buffer.
> + */
> + membuf_zero(&to, sizeof(cetregs->user_ssp));
> + return 0;
> + }
> +
> + return membuf_write(&to, (unsigned long *)&cetregs->user_ssp,
> + sizeof(cetregs->user_ssp));
> +}
> +
> +int ssp_set(struct task_struct *target, const struct user_regset *regset,
> + unsigned int pos, unsigned int count,
> + const void *kbuf, const void __user *ubuf)
> +{
> + struct fpu *fpu = &target->thread.fpu;
> + struct xregs_state *xsave = &fpu->fpstate->regs.xsave;
> + struct cet_user_state *cetregs;
> + unsigned long user_ssp;
> + int r;
> +
> + if (!boot_cpu_has(X86_FEATURE_USER_SHSTK) ||
> + !ssp_active(target, regset))
> + return -ENODEV;
> +
> + r = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_ssp, 0, -1);
> + if (r)
> + return r;
> +
> + /*
> + * Some kernel instructions (IRET, etc) can cause exceptions in the case
> + * of disallowed CET register values. Just prevent invalid values.
> + */
> + if ((user_ssp >= TASK_SIZE_MAX) || !IS_ALIGNED(user_ssp, 8))
> + return -EINVAL;
> +
> + fpu_force_restore(fpu);
> +
> + /*
> + * Don't want to init the xfeature until the kernel will definetely
> + * overwrite it, otherwise if it inits and then fails out, it would
> + * end up initing it to random data.
> + */
> + if (!xfeature_saved(xsave, XFEATURE_CET_USER) &&
> + WARN_ON(init_xfeature(xsave, XFEATURE_CET_USER)))
> + return -ENODEV;
> +
> + cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER);
> + if (WARN_ON(!cetregs)) {
> + /*
> + * This shouldn't ever be NULL because it was successfully
> + * inited above if needed. The only scenario would be if an
> + * xfeature was somehow saved in a buffer, but not enabled in
> + * xsave.
> + */
> + return -ENODEV;
> + }
> +
> + cetregs->user_ssp = user_ssp;
> + return 0;
> +}
> +#endif /* CONFIG_X86_USER_SHADOW_STACK */
> +
> #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
>
> /*
> diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
> index dfaa270a7cc9..095f04bdabdc 100644
> --- a/arch/x86/kernel/ptrace.c
> +++ b/arch/x86/kernel/ptrace.c
> @@ -58,6 +58,7 @@ enum x86_regset_64 {
> REGSET64_FP,
> REGSET64_IOPERM,
> REGSET64_XSTATE,
> + REGSET64_SSP,
> };
>
> #define REGSET_GENERAL \
> @@ -1267,6 +1268,17 @@ static struct user_regset x86_64_regsets[] __ro_after_init = {
> .active = ioperm_active,
> .regset_get = ioperm_get
> },
> +#ifdef CONFIG_X86_USER_SHADOW_STACK
> + [REGSET64_SSP] = {
> + .core_note_type = NT_X86_SHSTK,
> + .n = 1,
> + .size = sizeof(u64),
> + .align = sizeof(u64),
> + .active = ssp_active,
> + .regset_get = ssp_get,
> + .set = ssp_set
> + },
> +#endif
> };
>
> static const struct user_regset_view user_x86_64_view = {
> diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h
> index c7b056af9ef0..e9283f0641c4 100644
> --- a/include/uapi/linux/elf.h
> +++ b/include/uapi/linux/elf.h
> @@ -406,6 +406,8 @@ typedef struct elf64_shdr {
> #define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */
> #define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */
> #define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */
> +/* Old binutils treats 0x203 as a CET state */
> +#define NT_X86_SHSTK 0x204 /* x86 SHSTK state */
> #define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */
> #define NT_S390_TIMER 0x301 /* s390 timer register */
> #define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */
> --
> 2.17.1
>
--
Sincerely yours,
Mike.
next prev parent reply other threads:[~2022-12-09 17:07 UTC|newest]
Thread overview: 107+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-03 0:35 [PATCH v4 00/39] Shadow stacks for userspace Rick Edgecombe
2022-12-03 0:35 ` [PATCH v4 01/39] Documentation/x86: Add CET shadow stack description Rick Edgecombe
2022-12-03 2:20 ` Kees Cook
2022-12-03 8:58 ` Bagas Sanjaya
2022-12-05 21:20 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 02/39] x86/shstk: Add Kconfig option for Shadow Stack Rick Edgecombe
2022-12-03 2:20 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 03/39] x86/cpufeatures: Add CPU feature flags for shadow stacks Rick Edgecombe
2022-12-03 2:22 ` Kees Cook
2022-12-07 11:00 ` Borislav Petkov
2022-12-07 22:35 ` Edgecombe, Rick P
2022-12-08 11:10 ` Borislav Petkov
2022-12-03 0:35 ` [PATCH v4 04/39] x86/cpufeatures: Enable CET CR4 bit for shadow stack Rick Edgecombe
2022-12-03 2:23 ` Kees Cook
2022-12-07 12:49 ` Borislav Petkov
2022-12-07 18:35 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 05/39] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states Rick Edgecombe
2022-12-03 2:24 ` Kees Cook
2022-12-20 11:32 ` Borislav Petkov
2022-12-21 0:45 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 06/39] x86/fpu: Add helper for modifying xstate Rick Edgecombe
2022-12-03 2:25 ` Kees Cook
2022-12-20 12:04 ` Borislav Petkov
2022-12-21 0:03 ` Edgecombe, Rick P
2022-12-21 10:31 ` Borislav Petkov
2022-12-03 0:35 ` [PATCH v4 07/39] x86: Add user control-protection fault handler Rick Edgecombe
2022-12-03 2:28 ` Kees Cook
2022-12-20 16:19 ` Borislav Petkov
2022-12-21 0:37 ` Edgecombe, Rick P
2022-12-21 10:41 ` Borislav Petkov
2022-12-21 21:42 ` Edgecombe, Rick P
2023-01-04 12:50 ` Borislav Petkov
2022-12-20 21:21 ` Borislav Petkov
2022-12-21 0:38 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 08/39] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Rick Edgecombe
2022-12-03 2:29 ` Kees Cook
2022-12-20 19:11 ` Borislav Petkov
2022-12-03 0:35 ` [PATCH v4 09/39] x86/mm: Move pmd_write(), pud_write() up in the file Rick Edgecombe
2022-12-03 0:35 ` [PATCH v4 10/39] x86/mm: Introduce _PAGE_COW Rick Edgecombe
2022-12-03 2:31 ` Kees Cook
2022-12-20 21:29 ` Borislav Petkov
2022-12-21 0:45 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 11/39] x86/mm: Update pte_modify for _PAGE_COW Rick Edgecombe
2022-12-03 2:31 ` Kees Cook
2022-12-27 11:42 ` Borislav Petkov
2022-12-27 23:31 ` Edgecombe, Rick P
2023-01-04 13:25 ` Borislav Petkov
2023-01-05 1:06 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 12/39] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW Rick Edgecombe
2022-12-03 2:32 ` Kees Cook
2022-12-27 13:26 ` Borislav Petkov
2022-12-27 22:26 ` Edgecombe, Rick P
2023-01-04 13:28 ` Borislav Petkov
2022-12-03 0:35 ` [PATCH v4 13/39] x86/mm: Start actually marking _PAGE_COW Rick Edgecombe
2022-12-03 2:33 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 14/39] mm: Move VM_UFFD_MINOR_BIT from 37 to 38 Rick Edgecombe
2022-12-03 0:35 ` [PATCH v4 15/39] mm: Introduce VM_SHADOW_STACK for shadow stack memory Rick Edgecombe
2022-12-03 2:34 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 16/39] x86/mm: Check Shadow Stack page fault errors Rick Edgecombe
2023-01-04 14:32 ` Borislav Petkov
2023-01-05 1:29 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 17/39] x86/mm: Update maybe_mkwrite() for shadow stack Rick Edgecombe
2022-12-03 2:34 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 18/39] mm: Fixup places that call pte_mkwrite() directly Rick Edgecombe
2022-12-03 2:37 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 19/39] mm: Add guard pages around a shadow stack Rick Edgecombe
2022-12-03 0:35 ` [PATCH v4 20/39] mm/mmap: Add shadow stack pages to memory accounting Rick Edgecombe
2022-12-03 2:38 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 21/39] mm/mprotect: Exclude shadow stack from preserve_write Rick Edgecombe
2022-12-03 2:38 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 22/39] mm: Re-introduce vm_flags to do_mmap() Rick Edgecombe
2022-12-03 0:35 ` [PATCH v4 23/39] mm: Don't allow write GUPs to shadow stack memory Rick Edgecombe
2022-12-03 2:39 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 24/39] mm: Warn on shadow stack memory in wrong vma Rick Edgecombe
2022-12-03 2:40 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 25/39] x86: Introduce userspace API for shadow stack Rick Edgecombe
2022-12-03 2:42 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 26/39] x86/shstk: Add user-mode shadow stack support Rick Edgecombe
2022-12-03 2:43 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 27/39] x86/shstk: Handle thread shadow stack Rick Edgecombe
2022-12-03 2:44 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 28/39] x86/shstk: Introduce routines modifying shstk Rick Edgecombe
2022-12-03 2:45 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 29/39] x86/shstk: Handle signals for shadow stack Rick Edgecombe
2022-12-03 2:46 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 30/39] x86/shstk: Introduce map_shadow_stack syscall Rick Edgecombe
2022-12-03 2:51 ` Kees Cook
2022-12-05 22:19 ` Edgecombe, Rick P
2022-12-03 0:35 ` [PATCH v4 31/39] x86/shstk: Support wrss for userspace Rick Edgecombe
2022-12-03 2:52 ` Kees Cook
2022-12-03 0:35 ` [PATCH v4 32/39] x86: Expose thread features in /proc/$PID/status Rick Edgecombe
2022-12-03 2:52 ` Kees Cook
2022-12-03 0:36 ` [PATCH v4 33/39] x86: Prevent 32 bit operations for 64 bit shstk tasks Rick Edgecombe
2022-12-03 22:49 ` Andy Lutomirski
2022-12-04 20:51 ` Edgecombe, Rick P
2022-12-15 0:25 ` Edgecombe, Rick P
2022-12-03 0:36 ` [PATCH v4 34/39] x86/shstk: Wire in shadow stack interface Rick Edgecombe
2022-12-03 0:36 ` [PATCH v4 35/39] selftests/x86: Add shadow stack test Rick Edgecombe
2022-12-03 0:36 ` [PATCH v4 36/39] x86/fpu: Add helper for initing features Rick Edgecombe
2022-12-03 0:36 ` [PATCH v4 37/39] x86: Add PTRACE interface for shadow stack Rick Edgecombe
2022-12-03 2:55 ` Kees Cook
2022-12-09 17:04 ` Mike Rapoport [this message]
2022-12-09 17:08 ` Edgecombe, Rick P
2022-12-03 0:36 ` [PATCH v4 38/39] x86/shstk: Add ARCH_SHSTK_UNLOCK Rick Edgecombe
2022-12-03 2:56 ` Kees Cook
2022-12-03 0:36 ` [PATCH v4 39/39] x86/shstk: Add ARCH_SHSTK_STATUS Rick Edgecombe
2022-12-03 2:57 ` Kees Cook
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=Y5NqmLqXfXpowoSM@kernel.org \
--to=rppt@kernel.org \
--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=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=rick.p.edgecombe@intel.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).