From patchwork Tue Jun 13 00:10:52 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: 13277741 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 AEFC0C7EE43 for ; Tue, 13 Jun 2023 00:13:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6E80F8E001B; Mon, 12 Jun 2023 20:12:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66D178E000B; Mon, 12 Jun 2023 20:12:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4996F8E001B; Mon, 12 Jun 2023 20:12:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 31CD68E000B for ; Mon, 12 Jun 2023 20:12:34 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0E8DEAF4CD for ; Tue, 13 Jun 2023 00:12:34 +0000 (UTC) X-FDA: 80895798228.09.6D8E69D Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf08.hostedemail.com (Postfix) with ESMTP id BEA42160013 for ; Tue, 13 Jun 2023 00:12:31 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YVS8qSma; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf08.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686615152; a=rsa-sha256; cv=none; b=rkAcqttigrgIDm4gGDE2mS3xyZ68W/vtj5i8xIrYX2EWeg6ULoFHR78e+XBKPbgJw1mHzg NWWuf5QQ70P/0zBzWyj03XOfdiENE/uts0ONX2Gw/1xqTvC3GxY0YYVuPblHiAKTLH+Cow 0rbnCslOpqYTy01Co0OJvln6hSqJMao= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YVS8qSma; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf08.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 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=1686615152; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=izcKFpB20xlWfjQ0sAp89VgdYkvyy6AEcctJjTNYnrk=; b=MFl4ZVY4q79VhgFhjO0haL29pcW0/cTI3SW1XAzO/PlTT7VLUSDspRlTMaqdm7EytuHJdV Ho9Og9LpXzUxHfa/hj914qmUKCLHlH6iIomigNHOx4qYn/SMQNPpS5NscefWgVP8e1YlOt se5nKbwuFJXFgIZS2m+tImLX3vbU2+4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686615152; x=1718151152; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FpsnqDmskVtuVWeAVhUxFxlMXuwPJqlBrZTKKFCDs5Y=; b=YVS8qSmarnjno/wo29UmoxAb1YWIq92197YX+U7bXS6vDpvHS5xcOKZ4 bsmXIMMVgSYyL3yU+w/3ttAtinLnFW7xQTuNGrbFODVlpBW2gMMAUV3BI m48zHu0YDBQiE5CmbGJ873los2Yb81Jm/2c2YiV+cJSJaJshSSIUEGkvx 2xiokL1EKTWjDJPM8ZA8v4fU9YFT5Y3lFryT+q405r/z8jJa8XhCv+6px 2rSVBdmPQua/ZwQVvsPPFEJOhWT/0X0Ep01yYjRWskbbYi2JObLdBswG9 aZ204UvZDEFUWYdvf/T6vD65yqKwRols/zxRgaD36ENCV/a2Rm8viVamz A==; X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="361557280" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="361557280" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="835671078" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="835671078" Received: from almeisch-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.amr.corp.intel.com) ([10.209.42.242]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:29 -0700 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, szabolcs.nagy@arm.com, torvalds@linux-foundation.org, broonie@kernel.org Cc: rick.p.edgecombe@intel.com, Pengfei Xu Subject: [PATCH v9 26/42] x86: Introduce userspace API for shadow stack Date: Mon, 12 Jun 2023 17:10:52 -0700 Message-Id: <20230613001108.3040476-27-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> References: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BEA42160013 X-Stat-Signature: 83zy3e5froy99uqkcn8pm9zxee47c3f7 X-HE-Tag: 1686615151-472965 X-HE-Meta: U2FsdGVkX1/e4U6YY8WmPcUVijzt5RKitrtVUCWDa9Pshi16d/VoZd4HWssRzbXm9rQ9i/j/WLnd2REAFPfFmp5SOARQS0seSOHZkDroEn33yUuPc0Xk4oRUG510+lKyEThb2vBjh//XA0Jw+Dmvwaar9FcjXBTmyouFRfURYX0/UZSoSW25iGJhz5Of09dLwZOreIDZQtOZLFeQf5w3u1OhH5MnMBbelO/H2Vc2//NKMlL7d7MWODGLH+zIIMQG5rby0YBTEyHBhxO5po4bwqbA5nlqQnlAWcb/GzwgOwHbyQfJMPWBBqzEdHajKxGn0S4UmkrX0khOG+6PsDGHciFqjF3sstMw0e/QxQfDt4jvCW7ZIkZF+rwywr+EUpTKWe6EolhFHPt2RbBMylAJDEwzrWBxz8blahoqpSMNlV75uTJI3PvLsMsSuD4but2mLwKbSNCmBM0XZXfHvVSL0sb/RnBRlE2KwOHiO86gS4iE+hHVj0MbX7rNjgYjdsqCv115z8CqO0t6/r6DqH0qNwVpNjOI3KeqeveyGbLyY2r+Aor952TsWPct501LkRQANkh0aRcysCZ1o9t1bez2BxJ5fll0b6tuu45wy9/pJ2cGRpY4BTwCr8BdtUrXSuSEg36QxFehKd25OPlILcww+zS9THmo/YqmJciJiRZ21QMjIqLTTuxDG0Fck3o2AdlP5eoUFp72kO1bPuOmtp9Zn3VUVxckfcZnN67/Q/reVf3tP4crjA/4MdYJ8cw0q2oobzn1OiqlgkEwfWGYH/A5+8zPrIMANAxzLpGXL27cP3tWsbEZHhJoeG3D6+HxLmYwYMtDuiHW2ds1v5TU6nJD18kdmOD/iXTd3UMME2Q45VyS2/47f59tmxTq4A4cE2+iekWU5iph4uvfKpX8JeGHDzdO5d3odlVc3mcrFQV/sA3zvpcLJyTyska3eLNd259akys7etKdXTAKrDq0/Lq 147XjroO wDcoxhjO+MkuzJED5fhF83l6Nhr6cVoNpOUehQGysycNG5dxymD3wQoq0TlB1hD6lOsi2qmIhrX3yLtLucDa3IYU7UEMW9r2hfOwNh5pyfs5FuxOnfEM7QKfIum4TH/Un2eRsIi7fOygkJ5osomYQuFF5fn085YDxYEL57upGtFiBMXaRAZZ2cP2w2tNXIqqlvSTISuK6Y+XXq/5o25T3+IJB/mMT//pB6R1GClqzPMVOm5NWrFhdZvIhfpfCuYWH7x7W6avZ//VMV/OF++bfY1wPK8taHv+MLK6ywV65WQf+vX+HGwVz4T4fIRlSnzQsnSNbDCACjOFvRRz3l9VVfAgVRnzsoBSpBKdvRn7cJSDILhLROLhvBi0dpQ== 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: Add three new arch_prctl() handles: - ARCH_SHSTK_ENABLE/DISABLE enables or disables the specified feature. Returns 0 on success or a negative value on error. - ARCH_SHSTK_LOCK prevents future disabling or enabling of the specified feature. Returns 0 on success or a negative value on error. The features are handled per-thread and inherited over fork(2)/clone(2), but reset on exec(). Co-developed-by: Kirill A. Shutemov Signed-off-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- 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 | 6 +++++ arch/x86/kernel/shstk.c | 44 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+) 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 a1e4fa58b357..407d5551b6a7 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 e8d7ebbca1a4..1cd44ecc9ce0 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -23,9 +23,15 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +/* Don't use 0x3001-0x3004 because of old glibcs */ + #define ARCH_GET_UNTAG_MASK 0x4001 #define ARCH_ENABLE_TAGGED_ADDR 0x4002 #define ARCH_GET_MAX_TAG_BITS 0x4003 #define ARCH_FORCE_TAGGED_SVA 0x4004 +#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 abee0564b750..6b6bf47652ee 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -147,6 +147,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 3d181c16a2f6..0f89aa0186d1 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -515,6 +515,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); @@ -894,6 +896,10 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) else return put_user(LAM_U57_BITS, (unsigned long __user *)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; +}