From patchwork Sat Feb 18 21:14:20 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: 13145667 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 CB54BC64EC4 for ; Sat, 18 Feb 2023 21:16:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E239D28001A; Sat, 18 Feb 2023 16:16:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D83BF280019; Sat, 18 Feb 2023 16:16:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4A1A28001A; Sat, 18 Feb 2023 16:16:25 -0500 (EST) 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 AD4C5280019 for ; Sat, 18 Feb 2023 16:16:25 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 87ED08056E for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) X-FDA: 80481671130.27.D4FE380 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 90D9A4000A for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jLl8kmDa; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754983; 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=6Uy6jrrgmncVBRF1HaAQVgU/HvAYFBOwLgZGBX8wDiA=; b=AOlhGGzwXu9djd24MVPTwBu4m0IZ7YTTvUhdpy9T8LodWg500HNn1VbDq6FvYpHuvU/8K9 JR5q0ut07EIzmHRCTg+iVbMNQpZNLi53UQ4ooglQbee4kcjKR2PCQEEIZ0xV0BoVKsg4f6 UQ7VnauXqWi4gwZKgY86rIVv/AKqvWk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jLl8kmDa; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754983; a=rsa-sha256; cv=none; b=p8o6krnJ1g9fK/X6mccHN5p+/4Fl+pQnwJXewsNtUVCpUYvpPqm0tWNd8djbryn/wCsj4W 13qvay9PhdH/2DtLB86R1xYTzAf2YYJF0QhJEfeWCR6afweQgRARYH2eAwMt09V2Mu47Mp bcqMzsVtsi1Rz58PqABwCXG/AQ+iJ8U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754983; x=1708290983; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=l9ok0JOA/L86m7W4+eWESC88bM6ZrRTo0gq2MfVIjZo=; b=jLl8kmDaq3Q8kmKJkAJ60ljwfqcedQxS1YPxJ6KbFVZDYWzUY2Bb68hc NTrN6Yy+1ZjRfG85N5McTDUZugPRYGPjnf6AV6/c5bGcBRGHPSn2UGy2a KEejudfYtC3tQnYm3AlQp4j+/AYrIuyJiMesTlQlBtGZQV1hxVilCgeVb qppndangjTvueLA1thcED+AS9FZ8ulVwkqPY7PqKxze9/Wg+KdPpvU1vl ZtOv/Qj1Awm/LB0zvtcRBDSG/+Y3dPVe2SuA+SUjGeRY2cFpJClQVWkcX SegsuCJUuueL5vujkTK7k/gskcbjBuF1Ps41bL8vWBtTCwCqiAGRhig8N Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427693" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427693" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:19 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241705" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241705" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:18 -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 v6 28/41] x86: Introduce userspace API for shadow stack Date: Sat, 18 Feb 2023 13:14:20 -0800 Message-Id: <20230218211433.26859-29-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: k97n55dijh5ewyd4kqetynrh3w7h9gd8 X-Rspamd-Queue-Id: 90D9A4000A X-HE-Tag: 1676754983-452934 X-HE-Meta: U2FsdGVkX1/2IZ0x1AgMwuRcOH3xdU1UfwF+60PceV2sDoEdgqjlDrL/xIegw4cdXO2EhyzT1QqFfI2HYn1NnSUZJbCSIvv0u7iJDNewhETp81Ey1fhW8Tny8/rlsX8jQ6DwV5TqGFifEqX9WWbzQenELp4gMTD4y7O6H72JeIF8Ptp9lrrs9/j5qNXY7MnPIcFI/vuQKMynkKcEDJNkOUlussBXEB02CShFEiYCYY7uq2o14+T3b16Hoin4odsUqp5ZHf/UIrRr9/8J/7IJm5AEvOtTjANpgdn7BVn3fkZVRTgy3WU8BZ/URUmABKIiPz/a56OXO+3vDuuskOj5WaoQ9A55Gh0wuEy7WUGvswmvGSUeu7opDHRoSLoMXukhv9uepCiVncIdRYH103O7Uqu/yETRZaGOINTyxfgMEybrCYfQ0S7mplp9lzVbVmreYAJUlL3R1TmpIcYAYURBaVwAFmLhlHEOdbosf02obhn9lpPrryAoUmS/w/XX/O3m6zKhu7GuOwJYORVfACNhCSruWcGL4aksodLsSqV5fIUCblXWR5MeYQvkofmz01wyjof+Lew+luGMazzmis6oRECp0qu/zF6wuEyLePlXNZGbaEu6g/NEh3yPftfRRrfoRsxHzqxpXvtwwNEU1KiOD84WOvqjX4wqT+h163wa33vfoWIKWSpOvfmSEdxF4QocK3Gw2eBUczsDVukNL6EPupa1rw6Yl2oOg76w4ikQVgvJ5+FFBDaE/KINL3PWOC/vTtMClFgHTy2ytOeo1yn9iWOzJhTcfYXRq3NxK5DHepd+W7mLfMi4wD1ONw/0L9h8oZojnsGyE1VeReWeDfVv46BVnsSjxparcD8YqKtp879F0I8q1enG5ju3zj0sFEeB0wpeb1cuTggkR+J0zEpebHZW/PXiJCqOLQGlCZMn3xkqI57pw5KGQnOJ6orwLa3sqbpddlqL+B3fyGnTeRn L15d2/OZ YFnSZbbA5R3Xd/DRTUDh4M5bFMZ0hOLNPK4oEWGLiPy7e11lOrAzWHTWTR7AQsi1/ICXOCyy8JNkdoVDhfYRt2ZR/Rg2tn3iP6JECjtk+bp/3bqIfiHDOyKduNH9exJfLeeIuke7Xx7r6yA9s+8LI4R5Df+YO9jg+jL90JgipFWOkU/ZJrrIpJWRKzXTfHfZVvFIcsobWcq8wde603ngz3nbHzdWuiEcUn8LBnwKgI/h8MhkHNaXgt01iuY389NW4FLwc1toHqvUc8z4gh6kSxPS6S4XBtMTmA9+7 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 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; +}