Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1689387pxb; Fri, 20 Aug 2021 11:23:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxjw3Wv+/Yx1rYylESaUuvTmZAb1qfOhJCPeyuHwnUH0FnCdwP2UtPcazrgQ+CO957o/aQ X-Received: by 2002:a92:b301:: with SMTP id p1mr9584278ilh.10.1629483811555; Fri, 20 Aug 2021 11:23:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483811; cv=none; d=google.com; s=arc-20160816; b=r+P+uTGqA2FJcNDbr3Cv+GEq4cWpfSq0igWb7kwZt6WiSC0G61KddEbLwTOr/u3dB8 ovuBaAax4Tlrra5F0Rvj+GuMTBj0XowDDU6JyeuItq5V7Q01eVQg7A3e1Ro0oI1IDdTF 6V4gqoKdMA2IG6QySoK2ZOMnaz5R2Um4CJcZwHJqPOAnxgDciT/b1WrV7UxWn/C3+S7l di8rtnOSWiZD6fso3aZs/D+CadCXLOCwC5hiqaTWd9Y+Vq4C+RYVTDUvgq6pjTWk+4B/ akvNxyhPvCnsWiAsLiHmzgK28P9DT40yT5VP8zxY0nDQzd/vZWg+wsvmpHvGlvvCyXri Rtlg== 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; bh=RfIXjsfu9zrP15A0sfImlJnYzRpj0evs/KFmYvdNWzU=; b=FTWNegXPSD6cy/3p1kwP0CrX7GeIRdnJXgRR+xW/uh6ACVwlmbFcX2jFh6/AHe3ZuD kFYJZ2FUktZrrw73p+1BQqn7Rq3+R9NoGudzlkjCKaPyQmx15sFCqN8fdLEm34OLhdq+ 1xb5DzuSA+ON+K0exR8uXk2EnPvlDdR3egPMP4oMQ39OfoAeNFs7wk9RwjRFIsSNu+lh 0O6d1m5HswXMSMULCmWc6zas9Gd+bpN/jDCyynkEIA8bHF38R/8TEapkxV9GdZq1BhNa CxZaAQanPlZ3Hxf1UAVluZYoXEdGjJrpP9KYd0RaFxVbxtro6O3kGc3rADXiJphiS5uG 6fOA== 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 q3si7374285ils.46.2021.08.20.11.23.19; Fri, 20 Aug 2021 11:23:31 -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 S237066AbhHTSW7 (ORCPT + 99 others); Fri, 20 Aug 2021 14:22:59 -0400 Received: from mga05.intel.com ([192.55.52.43]:22429 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238130AbhHTSWD (ORCPT ); Fri, 20 Aug 2021 14:22:03 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407843" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407843" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:52 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074785" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:52 -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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu Subject: [PATCH v29 29/32] x86/cet/shstk: Add arch_prctl functions for shadow stack Date: Fri, 20 Aug 2021 11:11:58 -0700 Message-Id: <20210820181201.31490-30-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-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 9e064845e497..39e826b5cabd 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -151,6 +151,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 bade6a594d63..7d8ccebdcab1 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -1006,14 +1006,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