From patchwork Thu Jan 19 21:23:04 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: 13108804 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 50335C6379F for ; Thu, 19 Jan 2023 21:24:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C562C280003; Thu, 19 Jan 2023 16:24:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4178280001; Thu, 19 Jan 2023 16:24:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96C8E280003; Thu, 19 Jan 2023 16:24:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7CB0B280001 for ; Thu, 19 Jan 2023 16:24:09 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 58DE414030C for ; Thu, 19 Jan 2023 21:24:09 +0000 (UTC) X-FDA: 80372826618.26.665260A Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 35696180011 for ; Thu, 19 Jan 2023 21:24:07 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=C9MIzJq8; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163447; 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=oh2+crOjRcnFhQrw+0Ej8TqzGPwj3bIwotS5/bXBRpE=; b=h+i3UwTSM+kMOhGpdJPECIT7u3OlwpLX58Q4OWd06rZ/rOitRhHH1rxcPdWc0JO/Sq3Vuf fuYvOiAocnvBm+nlU4l3Tc7qgTFn5iNt4AaLiXt1uKZKfWxuhOord6/pKdSg2nkgHc0Xak JFYZdVu7PjQ1FsBEIHPpAOfezJZwats= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=C9MIzJq8; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163447; a=rsa-sha256; cv=none; b=VQ0+aD49gPwfwEbK1C1J+qpESiJ5gOmP+Jgww0VB+Jca63P4uwe6rg9KeCGxQeC72HNmQ3 dgfactecxDIXqqF2Z7azOqfYOTKLWvjsGE2+bX4bo9Ore+36NJMxv3WD/5WOT8WpNIoAvp XR2R/Ng8E2cmrrt6EQNa8jR8UTwXjtY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163447; x=1705699447; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=0FS3biIis1HBrs8s6jBReO51I+7M0YdUM2Pqpl2DgjA=; b=C9MIzJq8lpOW9pnyuadKQVsYkHo88yrHfsr8s3e3FPfUx8iLpwrsRhMO j/KjDKJPJ60p1waZYrH2Hk7zCVtyDdvaO4g0CwanDqQyiPa4e4KUiek6B uvV4z/iasigGDs4lUgeGtDPM7QDG8GAEB1OT8MbypQUiUgD0CK/Z9ypXH SHiOhd2/uaWElseXGc0onvWDw/RhO5MuvnelhP1HhyRFemfzv47DkGJix EW77g7qJplLNFlC+MBENWgJniBrxZd5KkGWG+xpoCyA+83ajxjWymTpxm yTEcChh3S3xE+tnDLYSfPx6fTROxPk1dOYgl1/67w5YCZBrheLCUjoelP g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119809" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119809" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:06 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139130" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139130" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:04 -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 Cc: rick.p.edgecombe@intel.com Subject: [PATCH v5 26/39] x86: Introduce userspace API for shadow stack Date: Thu, 19 Jan 2023 13:23:04 -0800 Message-Id: <20230119212317.8324-27-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 35696180011 X-Stat-Signature: notgu1nm5ayntbw1zokuprjitf1dm7m9 X-Rspam-User: X-HE-Tag: 1674163447-919978 X-HE-Meta: U2FsdGVkX1+8dUuSlY98Dxb1iutyyOYl9Ymo5OAtQ00LLe0E3cuseLxRwubCy1VnKa+Gl6g0adclnZgIK6htmZXud6l119ohJ1XOpAmT/gfqkO7nH2UCqA84WKPl8G4baWB4vxeRFFp+Bdn3IlH7mCxx0yawd8wSFIWSBMVCqRlrR1eelndafomgu/f0EypbXT0VkSWetahuPhRz+jz3wstER5BwY1qXmsI4C8JWLUnKkVkDReFk6tBQHhq4n7OIesD+Nq/+vWuV0kxcwuX44fNQ90K0NvM9hPTLzV7ewHtqKz+4nyZp2dAlopwXtBQn69Mz5bPBaXIcMBaxpLGiBn45IiPP+f0ClSIhMydNtA6uXQ9ADx2O7zM7tUh1sV7gTs7CcN9fyz6O2Aw1BFfSWuG8WxuCiGtx+8JVnVwRU+Io/gAAJWq6Q1WzpFWOzxjv6bH6JBZsv06fcPvd/hme5WKLyLtC5Tv9bSqwozBJ+/ioX5qrMQuOhrPsCvq5zlSUSoxwlUYLIM6haR/2iLefR/N1PFVHpizukSXHOPbCnqvgCDA7tpOvHH9fI5Y38UNGMcRxhfdv5dLZP+ycOFumxr7BgEUzHwE4Bw3/JjSiJqymwtjKwYvPl4n6QkE3+lLk7vPyr/obiBM/FbkcaMwNgDLYHWAGRAoD2eKVOpIjAB/nQ1AM4loHCOcRfxBodufC6r5OLEig7qRIAcAZRCxDzqdxCSXU/DJiuDy4fgnJ0Mz+gykUNTpuVw4dXQneZCEXWOeghVo7pcxWvXIyn5+zsrOUhavBN94E6VuAynO2PwkXcMxCrOwQbrRkkh9s4YWRXPWbXf+lH6+VRPaBt4K5t35MaxGXSfHzT5FDd0FsY3ktv393+INl2beNAj7ho1Ow4zcq84ZcVqZLaio2bioPkuK/AWk38tDemw8D4YSaKjHxZJ7oQ09RPdXbaEv5ey+YCukpuiH+t5LKTVlqXIs UvzeZcfL PF9hq1te3FdNb80Pytt7oSbzqaQ/XAy+Su7iQ9EaHB7w50ZxQTYoOFwnHR3yYQifu7wvq4bjPmdb/Pj5UB9FfYg19K2LCDiNlyMY4gyPHgqm+ggfH9HFLwudR4MAXXHszACo+GvJSxvAyJ0m33+gXuVjs44zeCxN3Ue8vx1+EuFFaJHGukZu3M22S/wX9gpMmJKb7ZJjTNJxL1jsSLkEkfYdLthm3v0bHaEfyYqak/lU+bvg= 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 Signed-off-by: Kirill A. Shutemov [tweaked with feedback from tglx] Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- 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 4e35c66edeb7..e0734f417273 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..58f9ee675be0 --- /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 features) { 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; +}