Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1107052ybz; Wed, 29 Apr 2020 15:11:40 -0700 (PDT) X-Google-Smtp-Source: APiQypI7qeVzdjS8ofleRMiWT0vafxosfTeq6tleikxdDT3av0hbqM2goBvfg3x/rM3tSpcIrL7s X-Received: by 2002:aa7:cdcb:: with SMTP id h11mr125194edw.264.1588198300157; Wed, 29 Apr 2020 15:11:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588198300; cv=none; d=google.com; s=arc-20160816; b=eB1HJcO/s01dGGCBlzXIYNt557jNRHHOXBRANoxsfJTJIdJZZO13gfGQ8CufwFDmIY J+gosGTVyKKstazFNqSV3tXdikMpR+c2nmdZlwpfJ1U3dpNjVqQynW/kJrbOsdaWYLR9 MGrLSDd4fb9vYkQwgdIw5jTmj6epGe18J/euHvVrs23SwzI1e0MBDHbyWJkWSHSItdbp Qm72YKmoEWg/w0A/fz3FK+iafpF6myU38XvWIGUiwUKJGaoqza6GgzkXw2DC5I7oaY/8 PTkiW6JvSp7VhXS5sCZIWhJjsI6JAs4BqGUY8r7RzI0NQewLMZNiYCbs54+aNPG0X+Kk /Q3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=rQbSdeNLoROOBG2sAjHrZlK3BspvaJcXhHyelxAnIk0=; b=arabHN84WLqcpQq6nk9VQuXMGrJ1O8B5TjFrIa0lLbiyfTB4uPukLcmvXZ3SL4MUq4 QcEzlxVphS0JyoWHSriV60kO50FSvhLSkewua6C+MFKEVuNwVb9KFG6S3VNE/O5mDY6k wdkpy7oQyDi8fJMe1H6b19enGLiraXTpw16uWS+q546UyPbtLWKmhdBilSgJk8WheJZf 3limTBXcRMBm1F9Bvi4VILbre3Z75wMCSJPvVWga0otpIpnHIi8jBww2OKO1rsBANez7 IBOuXk4UQ01JRCyQ2t4yhGbu1DbFiYAfWbXvTqsoxusmvBNVPflPH91v9b4JBW3S03vM /j/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l19si4684485ejq.122.2020.04.29.15.11.16; Wed, 29 Apr 2020 15:11:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728137AbgD2WJG (ORCPT + 99 others); Wed, 29 Apr 2020 18:09:06 -0400 Received: from mga09.intel.com ([134.134.136.24]:61306 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727085AbgD2WI7 (ORCPT ); Wed, 29 Apr 2020 18:08:59 -0400 IronPort-SDR: dHFCcmu8co/ufUWlVlI8NAR12KVXEfM66fNGRMdzER75LqV7PNdbcUCKmSvQJs0A7+sDCAamYY HgPNJ1j6WiUw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 15:08:56 -0700 IronPort-SDR: uLifCKL/hIIieGg3MZVyvjYueoOMMBb/D7dhKJ1nB7ry6OWDrd9WzlqOHBwWZSHHAoAAOo7i8T ZlRt35UzFMwQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,333,1583222400"; d="scan'208";a="276308944" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga002.jf.intel.com with ESMTP; 29 Apr 2020 15:08:55 -0700 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 , Weijiang Yang Cc: Yu-cheng Yu Subject: [PATCH v10 26/26] x86/cet/shstk: Add arch_prctl functions for shadow stack Date: Wed, 29 Apr 2020 15:07:32 -0700 Message-Id: <20200429220732.31602-27-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200429220732.31602-1-yu-cheng.yu@intel.com> References: <20200429220732.31602-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org arch_prctl(ARCH_X86_CET_STATUS, u64 *args) Get CET feature status. The parameter 'args' is a pointer to a user buffer. The kernel returns the following information: *args = shadow stack/IBT status *(args + 1) = shadow stack base address *(args + 2) = shadow stack size arch_prctl(ARCH_X86_CET_DISABLE, u64 features) Disable CET features specified in 'features'. Return -EPERM if CET is locked. arch_prctl(ARCH_X86_CET_LOCK) Lock in CET features. arch_prctl(ARCH_X86_CET_ALLOC_SHSTK, u64 *args) Allocate a new shadow stack. The parameter 'args' is a pointer to a user buffer containing the desired size to allocate. The kernel returns the allocated shadow stack address in *args. Signed-off-by: Yu-cheng Yu --- v10: - Verify CET is enabled before handling arch_prctl. - Change input parameters from unsigned long to u64, to make it clear they are 64-bit. 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 | 87 +++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 4 +- 6 files changed, 128 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 71dc92acd2f2..99e6e741d28c 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; }; #ifdef CONFIG_X86_INTEL_CET +int prctl_cet(int option, u64 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_verify_rstor_token(bool ia32, unsigned long ssp, unsigned long *new_ssp); void cet_restore_signal(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, u64 arg2) { return -EINVAL; } 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 void cet_restore_signal(struct sc_ext *sc) { return; } diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/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 +#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 e9cc2551573b..0b621e2afbdc 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -144,7 +144,7 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o -obj-$(CONFIG_X86_INTEL_CET) += cet.o +obj-$(CONFIG_X86_INTEL_CET) += cet.o cet_prctl.o ### # 64 bit specific files diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 121552047b86..c1b9b540c03e 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 long ssp, return 0; } +int cet_alloc_shstk(unsigned long *arg) +{ + unsigned long len = *arg; + unsigned long addr; + unsigned long token; + unsigned long ssp; + + addr = alloc_shstk(round_up(len, PAGE_SIZE)); + + if (IS_ERR((void *)addr)) + return PTR_ERR((void *)addr); + + /* Restore token is 8 bytes and aligned to 8 bytes */ + ssp = addr + len; + token = ssp; + + if (!in_ia32_syscall()) + token |= TOKEN_MODE_64; + ssp -= 8; + + if (write_user_shstk_64(ssp, token)) { + vm_munmap(addr, len); + return -EINVAL; + } + + *arg = 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..0139c48f2215 --- /dev/null +++ b/arch/x86/kernel/cet_prctl.c @@ -0,0 +1,87 @@ +/* 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(u64 arg2) +{ + struct cet_status *cet = ¤t->thread.cet; + u64 buf[3] = {0, 0, 0}; + + if (cet->shstk_size) { + buf[0] |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + buf[1] = (u64)cet->shstk_base; + buf[2] = (u64)cet->shstk_size; + } + + return copy_to_user((u64 __user *)arg2, buf, sizeof(buf)); +} + +static int handle_alloc_shstk(u64 arg2) +{ + int err = 0; + unsigned long arg; + unsigned long addr = 0; + unsigned long size = 0; + + if (get_user(arg, (unsigned long __user *)arg2)) + return -EFAULT; + + size = arg; + err = cet_alloc_shstk(&arg); + if (err) + return err; + + addr = arg; + if (put_user((u64)addr, (u64 __user *)arg2)) { + vm_munmap(addr, size); + return -EFAULT; + } + + return 0; +} + +int prctl_cet(int option, u64 arg2) +{ + struct cet_status *cet; + + if (!IS_ENABLED(CONFIG_X86_INTEL_CET)) + return -EINVAL; + + if (option == ARCH_X86_CET_STATUS) + return handle_get_status(arg2); + + if (!static_cpu_has(X86_FEATURE_SHSTK)) + return -EINVAL; + + cet = ¤t->thread.cet; + + switch (option) { + 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 = 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 ef1c2b8086a2..de6773dd6a16 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -996,7 +996,7 @@ long do_arch_prctl_common(struct task_struct *task, int 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; } -- 2.21.0