From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9FA5CC35247 for ; Wed, 5 Feb 2020 18:21:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6286621741 for ; Wed, 5 Feb 2020 18:21:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6286621741 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8EB836B0082; Wed, 5 Feb 2020 13:20:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 89A306B0088; Wed, 5 Feb 2020 13:20:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7624A6B0089; Wed, 5 Feb 2020 13:20:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0147.hostedemail.com [216.40.44.147]) by kanga.kvack.org (Postfix) with ESMTP id 599066B0082 for ; Wed, 5 Feb 2020 13:20:38 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id EC4B08248D51 for ; Wed, 5 Feb 2020 18:20:37 +0000 (UTC) X-FDA: 76456888914.02.money06_79d5fdcb20d07 X-HE-Tag: money06_79d5fdcb20d07 X-Filterd-Recvd-Size: 9167 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Wed, 5 Feb 2020 18:20:37 +0000 (UTC) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Feb 2020 10:20:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,406,1574150400"; d="scan'208";a="279447862" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by FMSMGA003.fm.intel.com with ESMTP; 05 Feb 2020 10:20:30 -0800 From: Yu-cheng Yu 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 , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , x86-patch-review@intel.com Cc: Yu-cheng Yu Subject: [RFC PATCH v9 27/27] x86/cet/shstk: Add arch_prctl functions for Shadow Stack Date: Wed, 5 Feb 2020 10:19:35 -0800 Message-Id: <20200205181935.3712-28-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200205181935.3712-1-yu-cheng.yu@intel.com> References: <20200205181935.3712-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: arch_prctl(ARCH_X86_CET_STATUS, unsigned long *addr) Return CET feature status. The parameter 'addr' is a pointer to a user buffer. On returning to the caller, the kernel fills the following information: *addr =3D SHSTK/IBT status *(addr + 1) =3D SHSTK base address *(addr + 2) =3D SHSTK size arch_prctl(ARCH_X86_CET_DISABLE, unsigned long features) Disable CET features specified in 'features'. Return -EPERM if CET i= s locked. arch_prctl(ARCH_X86_CET_LOCK) Lock in CET feature. arch_prctl(ARCH_X86_CET_ALLOC_SHSTK, unsigned long *addr) Allocate a new SHSTK. The parameter 'addr' is a pointer to a user buffer and indicates the desired SHSTK size to allocate. On returning to the caller the buffe= r contains the address of the new SHSTK. There is no CET enabling arch_prctl function. By design, CET is enabled automatically if the binary and the system can support it. The parameters passed are always unsigned 64-bit. When an IA32 applicati= on passing pointers, it should only use the lower 32 bits. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cet.h | 4 ++ arch/x86/include/uapi/asm/prctl.h | 5 ++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/cet.c | 29 +++++++++++ arch/x86/kernel/cet_prctl.c | 84 +++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 4 +- 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 arch/x86/kernel/cet_prctl.c diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index 9a3e2da9c1c4..b64f6d810ae0 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -14,16 +14,20 @@ struct sc_ext; struct cet_status { unsigned long shstk_base; unsigned long shstk_size; + unsigned int locked:1; unsigned int shstk_enabled:1; }; =20 #ifdef CONFIG_X86_INTEL_CET +int prctl_cet(int option, unsigned long arg2); int cet_setup_shstk(void); int cet_setup_thread_shstk(struct task_struct *p); +int cet_alloc_shstk(unsigned long *arg); void cet_disable_free_shstk(struct task_struct *p); int cet_restore_signal(bool ia32, struct sc_ext *sc); int cet_setup_signal(bool ia32, unsigned long rstor, struct sc_ext *sc); #else +static inline int prctl_cet(int option, unsigned long arg2) { return -EI= NVAL; } static inline int cet_setup_thread_shstk(struct task_struct *p) { return= 0; } static inline void cet_disable_free_shstk(struct task_struct *p) {} static inline int cet_restore_signal(bool ia32, struct sc_ext *sc) { ret= urn -EINVAL; } diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/as= m/prctl.h index 5a6aac9fa41f..d962f0ec9ccf 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -14,4 +14,9 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 =20 +#define ARCH_X86_CET_STATUS 0x3001 +#define ARCH_X86_CET_DISABLE 0x3002 +#define ARCH_X86_CET_LOCK 0x3003 +#define ARCH_X86_CET_ALLOC_SHSTK 0x3004 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index b8c1ea4ab7eb..69a19957e200 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -142,7 +142,7 @@ obj-$(CONFIG_UNWINDER_ORC) +=3D unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) +=3D unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) +=3D unwind_guess.o =20 -obj-$(CONFIG_X86_INTEL_CET) +=3D cet.o +obj-$(CONFIG_X86_INTEL_CET) +=3D cet.o cet_prctl.o =20 ### # 64 bit specific files diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 5b45abda80a1..01aa24c40a5d 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -145,6 +145,35 @@ static int create_rstor_token(bool ia32, unsigned lo= ng ssp, return 0; } =20 +int cet_alloc_shstk(unsigned long *arg) +{ + unsigned long len =3D *arg; + unsigned long addr; + unsigned long token; + unsigned long ssp; + + addr =3D alloc_shstk(len); + + if (IS_ERR((void *)addr)) + return PTR_ERR((void *)addr); + + /* Restore token is 8 bytes and aligned to 8 bytes */ + ssp =3D addr + len; + token =3D ssp; + + if (!in_ia32_syscall()) + token |=3D TOKEN_MODE_64; + ssp -=3D 8; + + if (write_user_shstk_64(ssp, token)) { + vm_munmap(addr, len); + return -EINVAL; + } + + *arg =3D addr; + return 0; +} + int cet_setup_shstk(void) { unsigned long addr, size; diff --git a/arch/x86/kernel/cet_prctl.c b/arch/x86/kernel/cet_prctl.c new file mode 100644 index 000000000000..6cf8f87e3d98 --- /dev/null +++ b/arch/x86/kernel/cet_prctl.c @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* See Documentation/x86/intel_cet.rst. */ + +static int handle_get_status(unsigned long arg2) +{ + struct cet_status *cet =3D ¤t->thread.cet; + unsigned int features =3D 0; + unsigned long buf[3]; + + if (cet->shstk_enabled) + features |=3D GNU_PROPERTY_X86_FEATURE_1_SHSTK; + + buf[0] =3D (unsigned long)features; + buf[1] =3D cet->shstk_base; + buf[2] =3D cet->shstk_size; + return copy_to_user((unsigned long __user *)arg2, buf, + sizeof(buf)); +} + +static int handle_alloc_shstk(unsigned long arg2) +{ + int err =3D 0; + unsigned long arg; + unsigned long addr =3D 0; + unsigned long size =3D 0; + + if (get_user(arg, (unsigned long __user *)arg2)) + return -EFAULT; + + size =3D arg; + err =3D cet_alloc_shstk(&arg); + if (err) + return err; + + addr =3D arg; + if (put_user(addr, (unsigned long __user *)arg2)) { + vm_munmap(addr, size); + return -EFAULT; + } + + return 0; +} + +int prctl_cet(int option, unsigned long arg2) +{ + struct cet_status *cet =3D ¤t->thread.cet; + + if (!cpu_x86_cet_enabled()) + return -EINVAL; + + switch (option) { + case ARCH_X86_CET_STATUS: + return handle_get_status(arg2); + + case ARCH_X86_CET_DISABLE: + if (cet->locked) + return -EPERM; + if (arg2 & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + cet_disable_free_shstk(current); + + return 0; + + case ARCH_X86_CET_LOCK: + cet->locked =3D 1; + return 0; + + case ARCH_X86_CET_ALLOC_SHSTK: + return handle_alloc_shstk(arg2); + + default: + return -EINVAL; + } +} diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 7098618142f2..63dc88070923 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -998,7 +998,7 @@ long do_arch_prctl_common(struct task_struct *task, i= nt option, return get_cpuid_mode(); case ARCH_SET_CPUID: return set_cpuid_mode(task, cpuid_enabled); + default: + return prctl_cet(option, cpuid_enabled); } - - return -EINVAL; } --=20 2.21.0