From patchwork Tue Jun 13 00:11:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13277754 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD005C88CB2 for ; Tue, 13 Jun 2023 00:13:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66F868E0023; Mon, 12 Jun 2023 20:12:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F71C8E0003; Mon, 12 Jun 2023 20:12:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35D4C8E002A; Mon, 12 Jun 2023 20:12:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F38058E0023 for ; Mon, 12 Jun 2023 20:12:46 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D2AA51C7C69 for ; Tue, 13 Jun 2023 00:12:46 +0000 (UTC) X-FDA: 80895798732.27.090FA3A Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf19.hostedemail.com (Postfix) with ESMTP id B60F51A0004 for ; Tue, 13 Jun 2023 00:12:44 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dq1TNI4D; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686615165; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ym1IUf+WN11sfR5WNNfTjB6hlFyLrR1ZQd5U/3cGxxM=; b=HcRr4+nPVGvj/lQ8pG/Nb1h8dJDyl98iJd0HsSpxzHdwCFMqrCaeTwh/vawCgY5JVKfAD7 nnoNXw1HnN1FjbzDJ6EiiNKxpMhugOYOXtsIXfAWrJb5WkWhdI9EOYcWEq2TlXY9izr6xw P3rsRQJbugt4i/tl/CPe/eT/n2x7NmI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dq1TNI4D; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686615165; a=rsa-sha256; cv=none; b=dMA+PDJ5VFz3yKfKCPDhdFZDZ5ljKuPfq0zs3tZcQ/q8J3FywaeJgZBNeKlIUiV01pfnO6 q1rjL89kJPHRdPsU83whURHmPctwV+Qt+X9vAO0jFow46thGnUo494c8FMO2m4cb4Caefk 9uLUbqXu1kl3ihptTp6ctDnnHDCOl3Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686615164; x=1718151164; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YlC9UAklwmTYEYA/IeAnmisSABN50M2A88ovR11LHkA=; b=dq1TNI4D4/I9KBjNQOIFw0377GDC7VfWLKhFQKHwPN6pDdODQSa0kn4h HjNAhtfVBr+VDgzyHQYf2xKmAakAlyJQqj20tOs4ZBfga4lhuxQlcNTPs Vbk28uXaRaxn9PEP/WJ5wRMSr+dUCXUN4gN0Y32MMnFJoacXNKS3rKFQL 7mmMfb72Sg4aYNC0WXlgLIIv78AYM0CHgWfhhhnGQEhww1w+rTdjXQHxy XvrO51ahFv/CD2617url1maM98ExBfa9OuKQtOtKIqUG3ipd+ESw5WbCh SaZCUyitQn0sJNLRPkBYiFTScf5oKMEEDZInZj9/o+TIP2mW4HeH0ETPq A==; X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="361557621" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="361557621" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="835671179" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="835671179" Received: from almeisch-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.amr.corp.intel.com) ([10.209.42.242]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:42 -0700 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , 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 , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , 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, torvalds@linux-foundation.org, broonie@kernel.org Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Pengfei Xu Subject: [PATCH v9 40/42] x86: Add PTRACE interface for shadow stack Date: Mon, 12 Jun 2023 17:11:06 -0700 Message-Id: <20230613001108.3040476-41-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> References: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B60F51A0004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: kfu1s8j8dc97b8f6sico88m37cn7rf8b X-HE-Tag: 1686615164-146832 X-HE-Meta: U2FsdGVkX1+mWsuY13B4lm/6g9LlA8JEtkcTD/7PaNA0NUpvmwWiISgKes1vlgLfcpwgWlh1pcdUD+T8qMlXKQz5amOsG0b94DW8+bjXHpbIrnBvpk3mWVNrwA/eukHXnPGdoyeJtNqYyyf6Lf7lZQgQ9wSF2gpTa7OGj/3u7NGHXLF72nGEr94jQiyWhs6xBkmCqvvwf20hcpXoWkOYalU4lP3CitMGNDoOmpmQqvMhSDhtnrTC1/CfneLLuxa9VFGS3FkKWOxlzNvJoAOXFNRjWrLSmSLH2dmyZNlNOBIt9/BJNe+4D2aseDEleYDzUhBbBaVrV9w1P6oXzlyRfdmJrD7pGRc86CCzngyKbxgcoDXGA8TPj955xF4m4WaYqUUUM2e2C06UbFJ7Cbg6osSkCLDovv1Wx+B2UIlMI8X6bKA8HmQNVXhzqLwWZMMltXzZdxs3J47DHgK2Vfxnf1CYfxu4kiu+9lWZ0EwWlxJ3U2EQinqxZiVg9EWAVnuAZXVVJdIoN6WAc1fZmdUvJ1SOEwXyNoKdqVKkQml1vk0i3ARx5wfGSgOONYn5Fvl2MWkAslsiYUhHkbrjLPaG9GWTnp77KOCjYWensV7LqgljqDKRhIpuL1W1c8c82bGOFbmh1eVjGRZ0cPNJoeSDyUqIz90xJAJNI7fSRJfn4CqfzEhB86O8juGooQmaq7+PNOdjMtM8772L20/X2QUoHz5/2e/HzY/BD2r4UinaDxBc3VjLK/4Dz7TetyML+nh2Cp8OKmFnoVO37fE+QNR2ADO6hk/vUWFXWyO+u3Oo3RGedNgd0FAG3kWRdar7zcecPyvr2sBXkrT7bIO1ZR6kTj0903sIMfakX7cl6e1qSOZXNLW65+tQYyqWWZoEhgSeKDvKgNH9k6FpIPNuObj5kve71ioOqyo93PgE79v7x7x8kI16zayVfD7nW6xk4trKS2FVle6NWS2Mhw3Vt/x y9fM/4K7 DM2f28dIFDWUbXmIQxPsctdIHM5XtGZw7SKJF+yVASrnpHEszWjg2ysBQO62QG2UD8RomDsaXYRinpFSqGwi86wXid5IlWMPEZoq7CgExArtHbtvLxYy8HOsozbAUhBOXH7umFg0qnDUPRUQ3nPz2lFWNOcKxe7PAhsfldTZL7wQyM7q4E+YXXUU0pzqgz5JY6QuzL7LNLEBfpVozayaAVAyrh0wUi8r9yLNwwPvbg40MjjkUlo7Du5nSOZwlKL8udpknyMyfArp549nUgc1kMFJxmrfdHNkOlZ7OJlH1Q7QiICTVtqJ45196A8ZYtMtAmjgVAVa4bW6wo4kQ5bz/I4YEnOFb1HV2U0J5aqGZdjq9zcsqhKJnlIAewQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 a 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 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 ptrace today. So only expose the SSP, which is something that userspace already has indirect control over. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v9: - Squash "Enforce only whole copies for ssp_set()" fix that previously was in tip. --- arch/x86/include/asm/fpu/regset.h | 7 +-- arch/x86/kernel/fpu/regset.c | 81 +++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 12 +++++ include/uapi/linux/elf.h | 2 + 4 files changed, 99 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 -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..6bc1eb2a21bd 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "context.h" #include "internal.h" @@ -174,6 +175,86 @@ 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 (target->thread.features & ARCH_SHSTK_SHSTK) + 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 (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return -ENODEV; + + sync_fpstate(fpu); + cetregs = get_xsave_addr(&fpu->fpstate->regs.xsave, XFEATURE_CET_USER); + if (WARN_ON(!cetregs)) { + /* + * This shouldn't ever be NULL because shadow stack was + * verified to be enabled above. This means + * MSR_IA32_U_CET.CET_SHSTK_EN should be 1 and so + * XFEATURE_CET_USER should not be in the init state. + */ + return -ENODEV; + } + + 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 (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !ssp_active(target, regset)) + return -ENODEV; + + if (pos != 0 || count != sizeof(user_ssp)) + return -EINVAL; + + 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); + + cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER); + if (WARN_ON(!cetregs)) { + /* + * This shouldn't ever be NULL because shadow stack was + * verified to be enabled above. This means + * MSR_IA32_U_CET.CET_SHSTK_EN should be 1 and so + * XFEATURE_CET_USER should not be in the init state. + */ + 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 ac3da855fb19..fa1ceeae2596 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 */