Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1465976pxj; Fri, 21 May 2021 15:17:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYq9pP8XRarwyyET8xmWw4ZtWXverh/BK/qAUB8bwZhQFyq03e9pkzNH7dNZL5rSJJfEvJ X-Received: by 2002:a17:907:94c3:: with SMTP id dn3mr11955599ejc.26.1621635438244; Fri, 21 May 2021 15:17:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621635438; cv=none; d=google.com; s=arc-20160816; b=gDbFmHOb3E5agn1XQbsmMXNOFHDxj41YbfQS+k7x6p3maIO72UyXsJTpGOuccdTlle hnnQUs8z9Ngjs708YUlhD5PD9jiNSzKtmec5vAG1Zmxny/UvqXwxQudn4GffDoOc+Gvy Zf8OOaJrkeX6tGPsp5SSW1fASP2yCNc5Nak/jltVO5QHJVHf7jXfDUjFGbx2b0TlDcwT ETvwtlkCsnU1Gu+7p/liE7ejH/uUYLL8nk4Y/zC6sGBPudTXSuMTC/PqRBftufzjUXs8 otFbLDJuWlPBMa84uoW7HJ6pLlY2UD0IT0QvgYHKMpbMDc9GHY+RkODtXM6jvKJ5YOx6 bQ+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=lgm32sIcYLCdwONVavNoPORH6l+lqjpVz7LDRw8lJrU=; b=wOk16AAfjOrLtMcFxtwNyB4lH0R5VE9bD8kPrPkDSMQwEqjD+lydG2C2qoFixfxfpn hwjQys7wOJ+VP4D6FZFQn0WOB9zD0C7XvqLmJZBcwNMoHLMBSIZ6a4ut433wHwQ6DDMK oQ7aymXkJBSjhAE2GQC6nbr6DSUMB6W5JyMKH1F/3v26ASXx37aH3piWKUbKgnr7CPGo vWZRCSa5MmFaxg/P0Erl/cyGDDbrxQUlTp4c6TzktFls56IhwsZEFAMklDByuzRJRmOY z19nA1/9ZK/kI9I0Hdzn9PWMf+GkIE7umKzgTiD8DngrWgWM85MlruE6CE7YQw9AvtKU qaqA== 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 x10si6400359eji.476.2021.05.21.15.16.54; Fri, 21 May 2021 15:17:18 -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 S230435AbhEUWRJ (ORCPT + 99 others); Fri, 21 May 2021 18:17:09 -0400 Received: from mga09.intel.com ([134.134.136.24]:55681 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbhEUWQN (ORCPT ); Fri, 21 May 2021 18:16:13 -0400 IronPort-SDR: 3BEgX9McpAfickL0EZf3mHQKJbGPKuUTdmxjv/VCqiP9FAkqj4r05Pd6/rtXWkVhTwKdB63q7V w7Juuvuo4QbQ== X-IronPort-AV: E=McAfee;i="6200,9189,9991"; a="201618786" X-IronPort-AV: E=Sophos;i="5.82,319,1613462400"; d="scan'208";a="201618786" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2021 15:13:23 -0700 IronPort-SDR: K6AzeOaBXw6o2B0W4IVNFfcn0e1ZFZqulGTulQFN/BCKw9st0wR5M/umm28cMI9JiucurWsxV5 yOnn1fqmjdGA== X-IronPort-AV: E=Sophos;i="5.82,319,1613462400"; d="scan'208";a="441116248" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2021 15:13:22 -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 , Pengfei Xu , Haitao Huang Cc: Yu-cheng Yu Subject: [PATCH v27 28/31] x86/cet/shstk: Add arch_prctl functions for shadow stack Date: Fri, 21 May 2021 15:12:08 -0700 Message-Id: <20210521221211.29077-29-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210521221211.29077-1-yu-cheng.yu@intel.com> References: <20210521221211.29077-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 32-bit binaries use the same interface, but only lower 32-bits of each item. arch_prctl(ARCH_X86_CET_DISABLE, unsigned int features) Disable CET features specified in 'features'. Return -EPERM if CET is locked. arch_prctl(ARCH_X86_CET_LOCK) Lock in CET features. Also change do_arch_prctl_common()'s parameter 'cpuid_enabled' to 'arg2', as it is now also passed to prctl_cet(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook --- arch/x86/include/asm/cet.h | 7 ++++ arch/x86/include/uapi/asm/prctl.h | 4 +++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cet_prctl.c | 60 +++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 6 ++-- 5 files changed, 75 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 2f7940d68ce3..c76a85fbd59f 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -13,6 +13,7 @@ struct task_struct; struct thread_shstk { u64 base; u64 size; + u64 locked:1; }; #ifdef CONFIG_X86_SHADOW_STACK @@ -41,6 +42,12 @@ static inline int setup_signal_shadow_stack(int ia32, void __user *restorer) { r static inline int restore_signal_shadow_stack(void) { return 0; } #endif +#ifdef CONFIG_X86_SHADOW_STACK +int prctl_cet(int option, u64 arg2); +#else +static inline int prctl_cet(int option, u64 arg2) { return -EINVAL; } +#endif + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_CET_H */ diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 5a6aac9fa41f..9245bf629120 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -14,4 +14,8 @@ #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 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index b0e599102277..2741c91104ac 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -150,6 +150,7 @@ obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev.o obj-$(CONFIG_X86_SHADOW_STACK) += shstk.o +obj-$(CONFIG_X86_SHADOW_STACK) += shstk.o cet_prctl.o ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/cet_prctl.c b/arch/x86/kernel/cet_prctl.c new file mode 100644 index 000000000000..b426d200e070 --- /dev/null +++ b/arch/x86/kernel/cet_prctl.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* See Documentation/x86/intel_cet.rst. */ + +static int cet_copy_status_to_user(struct thread_shstk *shstk, u64 __user *ubuf) +{ + u64 buf[3] = {}; + + if (shstk->size) { + buf[0] |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + buf[1] = shstk->base; + buf[2] = shstk->size; + } + + return copy_to_user(ubuf, buf, sizeof(buf)); +} + +int prctl_cet(int option, u64 arg2) +{ + struct thread_shstk *shstk; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) + return -ENOTSUPP; + + shstk = ¤t->thread.shstk; + + if (option == ARCH_X86_CET_STATUS) + return cet_copy_status_to_user(shstk, (u64 __user *)arg2); + + switch (option) { + case ARCH_X86_CET_DISABLE: + if (shstk->locked) + return -EPERM; + + if (arg2 & ~GNU_PROPERTY_X86_FEATURE_1_VALID) + return -EINVAL; + if (arg2 & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + shstk_disable(); + return 0; + + case ARCH_X86_CET_LOCK: + if (arg2) + return -EINVAL; + shstk->locked = 1; + return 0; + + default: + return -ENOSYS; + } +} diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 7a583a28ddb2..821cf5221f4f 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -990,14 +990,14 @@ unsigned long get_wchan(struct task_struct *p) } long do_arch_prctl_common(struct task_struct *task, int option, - unsigned long cpuid_enabled) + unsigned long arg2) { switch (option) { case ARCH_GET_CPUID: return get_cpuid_mode(); case ARCH_SET_CPUID: - return set_cpuid_mode(task, cpuid_enabled); + return set_cpuid_mode(task, arg2); } - return -EINVAL; + return prctl_cet(option, arg2); } -- 2.21.0