From patchwork Mon Feb 27 22:29:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13154259 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 AB1E9C64ED9 for ; Mon, 27 Feb 2023 22:32:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D0306B009F; Mon, 27 Feb 2023 17:31:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 75A5F6B00A0; Mon, 27 Feb 2023 17:31:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55F206B00A1; Mon, 27 Feb 2023 17:31:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 432FD6B009F for ; Mon, 27 Feb 2023 17:31:56 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1BC5C16057A for ; Mon, 27 Feb 2023 22:31:56 +0000 (UTC) X-FDA: 80514520632.21.110A048 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf24.hostedemail.com (Postfix) with ESMTP id BD01C180017 for ; Mon, 27 Feb 2023 22:31:53 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eYbM6e4P; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537114; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=1i/FzPbqzxTXG8dJ1u/9sE/uM7j0WUeqnnBeybnxIC8=; b=dz2yorgqmMdNKALXRjCoyqHDQfK5sykmJGailR2kuJ7p6DHFClbcrd3ScfgGPbPE04I5MM JlC54L3kWCQSPR9EvF2K+2KilvPIJ7FwncZiXmZRwqE2ape+AFYf6CM98yFi86L0uj9O+R eYMeHYNn9xuVAgt/bnM3EQDNS1zlZ4I= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eYbM6e4P; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537114; a=rsa-sha256; cv=none; b=2Ln6Ge9zY9NwJtgD0z+7yT9IYUMgTM2NPZcz3dppSn8sesztjiV22qLEaWqjli8QO8zxJw 48iipXCQuMgShYK0q/dH/4L2wKgmdGGPraolWE5tNeJVuj9C5A3kQs9qGoyCDVQ4ws2xVD w4klK8iO66rrdAV6ia9SuprazfYgSE0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537114; x=1709073114; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=u1FQlCSURy2KLxFeOQDLZBSKCZJ7Cv/holb6byWhLUc=; b=eYbM6e4PdRl+e+8bTbp0YbDs4mVVKSD/jUfcgXI5uah3KJkopdTA/MyV 2tfSwupBJukxe7Nh5ZQWIuWcYtXS6e7wZv0Q7W02XqeOUQb2/Urgt4/Ej k24pS92lGDjimL/9r/XlLOEy5qt14prmcKoBv1TFlVRf1JT5wKl0l0z0k MpOYRV/XFzF0Zl1KWff2Fl2efnVnNZXdPSnGtyE8hSpgtHJdXLevP3kLm 0FvoIdwzGw6RJCtb7OjT+4VAV1ppRhrHKpceatWClQjWMp18Qr90W5naS bYDOiUUX1HFXoXj0l8+JQ/q+jTpbh/UoOp74dXUyL1OPSF1qhTMkgzDh+ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657674" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657674" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:28 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024703" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024703" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:27 -0800 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 Cc: rick.p.edgecombe@intel.com Subject: [PATCH v7 28/41] x86: Introduce userspace API for shadow stack Date: Mon, 27 Feb 2023 14:29:44 -0800 Message-Id: <20230227222957.24501-29-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: BD01C180017 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: y79kux4yd7hhqdijmtmdns64g83k34k8 X-HE-Tag: 1677537113-215512 X-HE-Meta: U2FsdGVkX192bv2G0LxdCspJsYAlZV7UOVH1EDWmAn2WuzNx4OJnXO5t5+nfV/lhnApXRUT/zAasdoPKt1kpCdX7VRQoHch7zKJ9K5tMXbANorAQjA4TqxCUM4dOBK6aB++44XAjz6vguG4qaaTt2EuTQeYhSYwHAFVauOeYbbA2RyfJlzyMbCz8TGvuTUo00HMcgDN5XRSk7/f6fBSVcPSQsf1fi4J88hsRs1TS5TyFuPZPh5P9RsWDYRcaKdB2dPWg064Pua0DCsFQISzrBgYOFqiD+DQsOkvfoyQaTZpYugGEvtz9BvX4TRo52U3u0kRZzik6ciCS3KnpO5LR27AFER9jZCcLPI4uAufko6L/gnQ2Gb5ed0mDQIPNF2tGC4iTKreZD3vvul7ByLouSGeA+2IAEkzau/9G6fNY6O+++5ciJPdnJb2Ira0x4FB6zWnjumLj9+QwNeo6bSaZCTp82WHSUoAwdqMALVt3DtHNGc7QheDUwMbRyiqYYNQHel57cUy0b9J5OvDVQT8wM9h8fJ1U6pQrfh3dFy9Xf1jvcfb/eAQDiTQBN1vFwVBMSV/luYkZDZhm9/AYUs5o7GTcpdLLoRi0JJKeToyfSEpDpxeB/36NXkM3zFSB4tHpaMzkx+wGSfsX0Kd87w6tAgAsILkzvnmaCqCjUdib1Zc3YKbVP58DFP1k7jibBfhC2WrcbVQKSNzer9/fp7GmwGabBKwZam/u6GpGDsXdfTPhnih7Z8CF582GV3tXy9efc1sMtKYY5Ugol9798h59RHjhN4Uz+e9A8HPVVUtmDJx9rLG7SacaUx/9GLhzPM9WVtgVzxWd357OnMBeC++EZYwnAOlRH9EcAZdGLU9ON9pTgqDTOSH/xoNw4l6LM5zO4jwkmYn7MdVa8dyZjcIHShZQQ60dGPeDdDHsUWtjwbrxHr9cGl5AJ306s7NHkSLG3wnreFwfjO5Va6c+Q8c hZSz0IJK hKdcHhJXAyPZRpDOWfCGtt3cL6GA2e/GT1cos1ytvX6/BRCniB6kwh6W6fjApVRzynfUD13Gf+cnrgkCzq/0TXQFJoFjklAP/5pzi71dp2Rs/PvcXojon7h8iRvdgBDqR62gHOY8OSF+N1j7wHzkJvFjfKaJYVEnnJerFTcWshArXfrzCNwUTFmFQX6iaUh3/DI7AqXAQFt7A67uioRd9fioCoaYbxkoBmKVzqEaDb2AM7g0gVoi4r8+gtNvdAFTdkpFD9Ns0RYwT7TfrlUHhHdW0BeywCTW3YkcRq6QI4E0EZYc2bhnFLsYTvBvAXCM09d3HisEE1RJ2fG/AxJiC2mmEk2eVHBzeH6g1 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: From: "Kirill A. Shutemov" Add three new arch_prctl() handles: - ARCH_SHSTK_ENABLE/DISABLE enables or disables the specified feature. Returns 0 on success or an error. - ARCH_SHSTK_LOCK prevents future disabling or enabling of the specified feature. Returns 0 on success or an error The features are handled per-thread and inherited over fork(2)/clone(2), but reset on exec(). This is preparation patch. It does not implement any features. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook Signed-off-by: Kirill A. Shutemov [tweaked with feedback from tglx] Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe --- v4: - Remove references to CET and replace with shadow stack (Peterz) v3: - Move shstk.c Makefile changes earlier (Kees) - Add #ifdef around features_locked and features (Kees) - Encapsulate features reset earlier in reset_thread_features() so features and features_locked are not referenced in code that would be compiled !CONFIG_X86_USER_SHADOW_STACK. (Kees) - Fix typo in commit log (Kees) - Switch arch_prctl() numbers to avoid conflict with LAM v2: - Only allow one enable/disable per call (tglx) - Return error code like a normal arch_prctl() (Alexander Potapenko) - Make CET only (tglx) --- arch/x86/include/asm/processor.h | 6 +++++ arch/x86/include/asm/shstk.h | 21 +++++++++++++++ arch/x86/include/uapi/asm/prctl.h | 6 +++++ arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/process_64.c | 7 ++++- arch/x86/kernel/shstk.c | 44 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/shstk.h create mode 100644 arch/x86/kernel/shstk.c diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 8d73004e4cac..bd16e012b3e9 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -28,6 +28,7 @@ struct vm86; #include #include #include +#include #include #include @@ -475,6 +476,11 @@ struct thread_struct { */ u32 pkru; +#ifdef CONFIG_X86_USER_SHADOW_STACK + unsigned long features; + unsigned long features_locked; +#endif + /* Floating point and extended processor state */ struct fpu fpu; /* diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h new file mode 100644 index 000000000000..ec753809f074 --- /dev/null +++ b/arch/x86/include/asm/shstk.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SHSTK_H +#define _ASM_X86_SHSTK_H + +#ifndef __ASSEMBLY__ +#include + +struct task_struct; + +#ifdef CONFIG_X86_USER_SHADOW_STACK +long shstk_prctl(struct task_struct *task, int option, unsigned long features); +void reset_thread_features(void); +#else +static inline long shstk_prctl(struct task_struct *task, int option, + unsigned long arg2) { return -EINVAL; } +static inline void reset_thread_features(void) {} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_SHSTK_H */ diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 500b96e71f18..b2b3b7200b2d 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -20,4 +20,10 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +/* Don't use 0x3001-0x3004 because of old glibcs */ + +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_DISABLE 0x5002 +#define ARCH_SHSTK_LOCK 0x5003 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 92446f1dedd7..b366641703e3 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -146,6 +146,8 @@ obj-$(CONFIG_CALL_THUNKS) += callthunks.o obj-$(CONFIG_X86_CET) += cet.o +obj-$(CONFIG_X86_USER_SHADOW_STACK) += shstk.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 4e34b3b68ebd..71094c8a305f 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -514,6 +514,8 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, load_gs_index(__USER_DS); } + reset_thread_features(); + loadsegment(fs, 0); loadsegment(es, _ds); loadsegment(ds, _ds); @@ -830,7 +832,10 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_MAP_VDSO_64: return prctl_map_vdso(&vdso_image_64, arg2); #endif - + case ARCH_SHSTK_ENABLE: + case ARCH_SHSTK_DISABLE: + case ARCH_SHSTK_LOCK: + return shstk_prctl(task, option, arg2); default: ret = -EINVAL; break; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c new file mode 100644 index 000000000000..41ed6552e0a5 --- /dev/null +++ b/arch/x86/kernel/shstk.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * shstk.c - Intel shadow stack support + * + * Copyright (c) 2021, Intel Corporation. + * Yu-cheng Yu + */ + +#include +#include +#include + +void reset_thread_features(void) +{ + current->thread.features = 0; + current->thread.features_locked = 0; +} + +long shstk_prctl(struct task_struct *task, int option, unsigned long features) +{ + if (option == ARCH_SHSTK_LOCK) { + task->thread.features_locked |= features; + return 0; + } + + /* Don't allow via ptrace */ + if (task != current) + return -EINVAL; + + /* Do not allow to change locked features */ + if (features & task->thread.features_locked) + return -EPERM; + + /* Only support enabling/disabling one feature at a time. */ + if (hweight_long(features) > 1) + return -EINVAL; + + if (option == ARCH_SHSTK_DISABLE) { + return -EINVAL; + } + + /* Handle ARCH_SHSTK_ENABLE */ + return -EINVAL; +}