From patchwork Thu Jan 19 21:22:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108777 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 C1C3FC46467 for ; Thu, 19 Jan 2023 21:23:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 550EF6B0073; Thu, 19 Jan 2023 16:23:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4645C6B0078; Thu, 19 Jan 2023 16:23:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06D826B007B; Thu, 19 Jan 2023 16:23:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EB7AA6B0073 for ; Thu, 19 Jan 2023 16:23:29 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C373712092B for ; Thu, 19 Jan 2023 21:23:29 +0000 (UTC) X-FDA: 80372824938.16.E9D3766 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf21.hostedemail.com (Postfix) with ESMTP id C6DFA1C0019 for ; Thu, 19 Jan 2023 21:23:27 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q5hIwRSQ; spf=pass (imf21.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=1674163408; 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=GnbG2zT0q0TQKJhci012/C8hz/SCKJl3hl5drYijqMc=; b=zjqnCmg23Wcqfw9ecDQWkaHmt4YiwKQ4RkVRBr49ZQyyL84hD0tRcGKhB0dz0a3sTBpzPQ Zue7lAtEHmW7hFwonUw2y7dh4EokzEn30YwnIgNkncqlOG0y1vZqacqf1S90XRfa5jvkOY UK2H83bKbDW2ma9U0zrXiubUcyLO4xM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q5hIwRSQ; spf=pass (imf21.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=1674163408; a=rsa-sha256; cv=none; b=m13+VnX4oXUWX5q053fzxq7No3YXukLL/8NWAeSh7mfquB3lHH0hwm5gk1SuaHmbWEroGK 3R9aRna//H55pBl06kJgQeiACsDlCNAZBCTgIkNgKOFywkQ9oEzYNZw1MJ+9wxD2hKr1eJ J5oJ/5bAo+pxjsk6Eatxmf8deG2uPJI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163407; x=1705699407; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=uhuMbHp6Kbqx+B8M1wEyR7U+E0nR3IzT2DHoy+pPYUg=; b=Q5hIwRSQrbeaTw2ZNKYjvDPKzgWyCWaaMjqtz76ZJ/Jr8hJB5PYj0PGo pM2FXcJhD4N4ZUofgbQ6pHAjjLqsYy99ka1gQrfG6+QPfQp6FOl19eIQN 76QTmZRv2VpSE49q9PEVuB4M2pKj1oHAhsgqZQ+fxn77SP+TbGGsZvLC5 R/x9o9yu6B/ycMb062rBqpdFtzqdVOv+lTgwgdgL8fONWXLUFZCyZw8IS Ts33pnZ0PE8YPS93cuMPAsO7rdYPxE278XS0fB5Ln6vAlmOQiQM/TnEw0 DLOtkq8NEp5REEklTGaQiJX1AKw/hJbULd6xBjVqoQiWqauvc2p3qDJeQ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119171" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119171" 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:23:26 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989138986" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989138986" 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:23:24 -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, Yu-cheng Yu Subject: [PATCH v5 01/39] Documentation/x86: Add CET shadow stack description Date: Thu, 19 Jan 2023 13:22:39 -0800 Message-Id: <20230119212317.8324-2-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: rspam07 X-Rspamd-Queue-Id: C6DFA1C0019 X-Rspam-User: X-Stat-Signature: orqbpbpiwgdah15gccxdk448aqwj5gim X-HE-Tag: 1674163407-681816 X-HE-Meta: U2FsdGVkX1/6+VIS5+472rOJkYGuHgxg+oCAb3lpE/HYP99N70KdS9It3HVneBYsORsV8OjiRx68y73KWF6Mri7/q426d7ti/8BZwjZsn33ctz5pQvI6KoXffkyEreq6xziVE8VTKEK05nY26fJbrLVEsu2R9xrmbUccVhKpdWrzm78NTyRHYPkY/5uJITfXK411Eq7yeU84ZwQ3oCdzu0sl4j5/BHdMqLrPB55rdNmUvJACSJlxFwgO3MDXOtYfJ1kuIa1lIs95UqlJYODgtGiUqqSGtVOqQXyg++kuXE7BnSF6Z0nzNTiD59VSp0pAfZyTpE8XRrWMgvKw9216iM9F5Ht/MIuZwa8v670JJISilP3JyI0YUk1eXYlP8jbuDJzv0eWDkGtrC73/iDrEWfO0g5pSXkwmPbJpH+MufAENIc3lSDGZA3DabI17SnK3rmlrWtm4M4z56iM6IvsPBxWSeNT5KmvaL8a/5JTGQlMadQXs+bHsLTATsEUcm/RNnt5Cx6emBa8JXCJItzPFdG1NEWjLN41Br2C9Md6CXgo0q6P5SEn7r/kyNuI2myjfn9gDlwaH/j8satcJezukdNh07nPRNaUmlYgnk+zKBSfpe6UtGK3thgSjwhXgtgIoxQolz48j3+OnRxL6nCfYnoZHa8UgZrkczPteOPGPVCa5AaFtIHThzYo7vnmG/dzaqFNubVk6mzFv5GFsDCIIhzkytap4+mz0y4cgjkjnbgrvsX1fhPxXyU1G/Na29b5ToorD17OICMhCUcrXyHVng9TencsmrE+5XcQ633OF/2DLGn4bMfwZ2H+ctbT7xNR0k0jqj0YHmp5TTMyCZzWLelGni41qHpXfQFz6Gdo8BEIPkTdVbdjA4GrRBvlmybHI9U23/EypYgvDSLx09nMUxMd0nHHkDVSEvlx10pgeRVzpNR8IBdFJxt6fbhcPg50YD9k3b1xOH3MnecyPgie DnAcTZBk AYs1uQYN/S4hnvlfSOty6LyCC/mRfxX/6PEtXPZs51NVW2y5Ro1/+yyAMX/e9n6TFiwPI54ls/d+4A6laFieH7Xa4se5+kPAwchdFc8mcK3fNrk3WHoPWb5ULaB1M7398BUWG7Ru0yB7qlvXdLStA3MTVmi6fOjAfzfyK3wNRDuTcRPaHMan3NqWXoNVWP0T8m5Vn9TDX1MGezMGxNxmE8SONmaHfqiiuH6UtITft26WtnAEjne0SJBMA8O3JQbx0VuPNevIFehaP4IhI30+UqWqGg50CEUy643I5 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: Yu-cheng Yu Introduce a new document on Control-flow Enforcement Technology (CET). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Literal format tweaks (Bagas Sanjaya) - Update EOPNOTSUPP text due to unification after comment from (Kees) - Update 32 bit signal support with new behavior - Remove capitalization on shadow stack (Boris) - Fix typo v4: - Drop clearcpuid piece (Boris) - Add some info about 32 bit v3: - Clarify kernel IBT is supported by the kernel. (Kees, Andrew Cooper) - Clarify which arch_prctl's can take multiple bits. (Kees) - Describe ASLR characteristics of thread shadow stacks. (Kees) - Add exec section. (Andrew Cooper) - Fix some capitalization (Bagas Sanjaya) - Update new location of enablement status proc. - Add info about new user_shstk software capability. - Add more info about what the kernel pushes to the shadow stack on signal. v2: - Updated to new arch_prctl() API - Add bit about new proc status Documentation/x86/index.rst | 1 + Documentation/x86/shstk.rst | 166 ++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Documentation/x86/shstk.rst diff --git a/Documentation/x86/index.rst b/Documentation/x86/index.rst index c73d133fd37c..8ac64d7de4dc 100644 --- a/Documentation/x86/index.rst +++ b/Documentation/x86/index.rst @@ -22,6 +22,7 @@ x86-specific Documentation mtrr pat intel-hfi + shstk iommu intel_txt amd-memory-encryption diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst new file mode 100644 index 000000000000..f2e6f323cf68 --- /dev/null +++ b/Documentation/x86/shstk.rst @@ -0,0 +1,166 @@ +.. SPDX-License-Identifier: GPL-2.0 + +====================================================== +Control-flow Enforcement Technology (CET) Shadow Stack +====================================================== + +CET Background +============== + +Control-flow Enforcement Technology (CET) is term referring to several +related x86 processor features that provides protection against control +flow hijacking attacks. The HW feature itself can be set up to protect +both applications and the kernel. + +CET introduces shadow stack and indirect branch tracking (IBT). Shadow stack +is a secondary stack allocated from memory and cannot be directly modified by +applications. When executing a CALL instruction, the processor pushes the +return address to both the normal stack and the shadow stack. Upon +function return, the processor pops the shadow stack copy and compares it +to the normal stack copy. If the two differ, the processor raises a +control-protection fault. IBT verifies indirect CALL/JMP targets are intended +as marked by the compiler with 'ENDBR' opcodes. Not all CPU's have both Shadow +Stack and Indirect Branch Tracking. Today in the 64-bit kernel, only userspace +shadow stack and kernel IBT are supported. + +Requirements to use Shadow Stack +================================ + +To use userspace shadow stack you need HW that supports it, a kernel +configured with it and userspace libraries compiled with it. + +The kernel Kconfig option is X86_USER_SHADOW_STACK, and it can be disabled +with the kernel parameter: nousershstk. + +To build a user shadow stack enabled kernel, Binutils v2.29 or LLVM v6 or later +are required. + +At run time, /proc/cpuinfo shows CET features if the processor supports +CET. "user_shstk" means that userspace shadow stack is supported on the current +kernel and HW. + +Application Enabling +==================== + +An application's CET capability is marked in its ELF note and can be verified +from readelf/llvm-readelf output:: + + readelf -n | grep -a SHSTK + properties: x86 feature: SHSTK + +The kernel does not process these applications markers directly. Applications +or loaders must enable CET features using the interface described in section 4. +Typically this would be done in dynamic loader or static runtime objects, as is +the case in GLIBC. + +Enabling arch_prctl()'s +======================= + +Elf features should be enabled by the loader using the below arch_prctl's. They +are only supported in 64 bit user applications. + +arch_prctl(ARCH_SHSTK_ENABLE, unsigned long feature) + Enable a single feature specified in 'feature'. Can only operate on + one feature at a time. + +arch_prctl(ARCH_SHSTK_DISABLE, unsigned long feature) + Disable a single feature specified in 'feature'. Can only operate on + one feature at a time. + +arch_prctl(ARCH_SHSTK_LOCK, unsigned long features) + Lock in features at their current enabled or disabled status. 'features' + is a mask of all features to lock. All bits set are processed, unset bits + are ignored. The mask is ORed with the existing value. So any feature bits + set here cannot be enabled or disabled afterwards. + +The return values are as follows. On success, return 0. On error, errno can +be:: + + -EPERM if any of the passed feature are locked. + -ENOTSUPP if the feature is not supported by the hardware or + kernel. + -EINVAL arguments (non existing feature, etc) + +The feature's bits supported are:: + + ARCH_SHSTK_SHSTK - Shadow stack + ARCH_SHSTK_WRSS - WRSS + +Currently shadow stack and WRSS are supported via this interface. WRSS +can only be enabled with shadow stack, and is automatically disabled +if shadow stack is disabled. + +Proc Status +=========== +To check if an application is actually running with shadow stack, the +user can read the /proc/$PID/status. It will report "wrss" or "shstk" +depending on what is enabled. The lines look like this:: + + x86_Thread_features: shstk wrss + x86_Thread_features_locked: shstk wrss + +Implementation of the Shadow Stack +================================== + +Shadow Stack Size +----------------- + +A task's shadow stack is allocated from memory to a fixed size of +MIN(RLIMIT_STACK, 4 GB). In other words, the shadow stack is allocated to +the maximum size of the normal stack, but capped to 4 GB. However, +a compat-mode application's address space is smaller, each of its thread's +shadow stack size is MIN(1/4 RLIMIT_STACK, 4 GB). + +Signal +------ + +By default, the main program and its signal handlers use the same shadow +stack. Because the shadow stack stores only return addresses, a large +shadow stack covers the condition that both the program stack and the +signal alternate stack run out. + +When a signal happens, the old pre-signal state is pushed on the stack. When +shadow stack is enabled, the shadow stack specific state is pushed onto the +shadow stack. Today this is only the old SSP (shadow stack pointer), pushed +in a special format with bit 63 set. On sigreturn this old SSP token is +verified and restored by the kernel. The kernel will also push the normal +restorer address to the shadow stack to help userspace avoid a shadow stack +violation on the sigreturn path that goes through the restorer. + +So the shadow stack signal frame format is as follows:: + + |1...old SSP| - Pointer to old pre-signal ssp in sigframe token format + (bit 63 set to 1) + | ...| - Other state may be added in the future + + +32 bit ABI signals are not supported in shadow stack processes. Linux prevents +32 bit execution while shadow stack is enabled by the allocating shadow stack's +outside of the 32 bit address space. When execution enters 32 bit mode, either +via far call or returning to userspace, a #GP is generated by the hardware +which, will be delivered to the process as a segfault. When transitioning to +userspace the register's state will be as if the userspace ip being returned to +caused the segfault. + +Fork +---- + +The shadow stack's vma has VM_SHADOW_STACK flag set; its PTEs are required +to be read-only and dirty. When a shadow stack PTE is not RO and dirty, a +shadow access triggers a page fault with the shadow stack access bit set +in the page fault error code. + +When a task forks a child, its shadow stack PTEs are copied and both the +parent's and the child's shadow stack PTEs are cleared of the dirty bit. +Upon the next shadow stack access, the resulting shadow stack page fault +is handled by page copy/re-use. + +When a pthread child is created, the kernel allocates a new shadow stack +for the new thread. New shadow stack's behave like mmap() with respect to +ASLR behavior. + +Exec +---- + +On exec, shadow stack features are disabled by the kernel. At which point, +userspace can choose to re-enable, or lock them. From patchwork Thu Jan 19 21:22:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108778 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 42EA3C6379F for ; Thu, 19 Jan 2023 21:23:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 550BA6B0078; Thu, 19 Jan 2023 16:23:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B2136B007B; Thu, 19 Jan 2023 16:23:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 306046B007D; Thu, 19 Jan 2023 16:23:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 10C346B0078 for ; Thu, 19 Jan 2023 16:23:31 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id DEC7040A2C for ; Thu, 19 Jan 2023 21:23:30 +0000 (UTC) X-FDA: 80372824980.11.89A468A Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf29.hostedemail.com (Postfix) with ESMTP id 0681C12000A for ; Thu, 19 Jan 2023 21:23:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PB7jOnoy; spf=pass (imf29.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=1674163409; a=rsa-sha256; cv=none; b=iRK/emMrUSLwh1FBGZuK5cnhYUoLBDwpDwPnZFKq6thEMNn3KH3lGTNkzTIzP6PB76E4gX UZjEBqvSCzk+A55Rh80B3fkiTVPQdZ7MCA3a1OWJLGFAyhZiNwsDgcagC5wNLJA06xT4WO a+ArnSsCQE+ZAg2asIaZRj29YpsXQQs= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PB7jOnoy; spf=pass (imf29.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=1674163409; 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=3U7z+h3YDF8Sba0ajewGuAx6MSy1V++Xb+AytMN0PuA=; b=jeHy+m2VWadzMj1XF3vzi1Dpm82Tim6hYfpPW9LzCBiD5engME7ZXxJ36XB8XU6+j9Z8Z3 s//oOtsqdLLyZATRrq5itpWxv+S4F0/gzgJMDQKP8B6XXczVdg+9xGSqpYucwsv5zmjeLt fy5/6ieH6oD0+swDPS6ZL+ROskLjmWM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163409; x=1705699409; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=qg2fCiCJOKwg0sz79Qyr0Ky5ojS7yp2AsnYDlj2uU6w=; b=PB7jOnoyfC7YPNuEmTTG9NcAS/zT9eg/VD25mEVtXzqoz5FCLr6jpyXS 44t7m0+ozTojCRra7v8j3/tv+1+VuEXHIlU3GGEJpwFkGIZAuy6dlOsOz NntunsOu8DwKCCb4n9r8XGlgic5jwuYzHFMYAYzcNEqsfsEfp3QCz+dwa cLJVTgBYhHWULlP+AjfZzofggY1Mud6feMebfyDMqekkeMpo5cCJhlHiC 8Os9MFpQ3EF7m3HLbSuCA/uiren7YQpAtpBckDx5wIzsE5oXltVa8I/JI AMKtRg340OXROVLvAUiYNiLfK72ISkxij2rVbpswFGl3Zws5AwmJnahsn Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119198" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119198" 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:23:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989138990" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989138990" 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:23:26 -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, Yu-cheng Yu Subject: [PATCH v5 02/39] x86/shstk: Add Kconfig option for shadow stack Date: Thu, 19 Jan 2023 13:22:40 -0800 Message-Id: <20230119212317.8324-3-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-Rspam-User: X-Rspamd-Queue-Id: 0681C12000A X-Rspamd-Server: rspam01 X-Stat-Signature: szrizaqwcxe3djnyoncdadrjojty9sje X-HE-Tag: 1674163408-43147 X-HE-Meta: U2FsdGVkX1/bBPILc6vO6sx7iCf2RAEjPqFl6F5DEolqQggi607UyySDQYVqGHM3G5GPuaGTj9K1jgSghR1DS2yzJiT6OtIF8UWlPNeHSu4LyTXYaZJHDfKwmqzfIJZjA+F/16YByWWFoeJn3pDr9TIX9sUaKqTvEgwUyAzW4AR6MZcT6EbHGRG7Y4FSBkaYwtxY4pGouyuaz1DJSeIQUNVRRF4pe252C/nQvJTKljFzeNpMtOloaMcr5HXNO+d+sMcvf8N13t/s6BQ+kGrDNNtyPklgOwMzkd1cMtpFN9/h+07jiQeW4FJyajSrzrgokzy+7Xtxt/NtGW9AsLBHgwR0EVv080B3GrD2t5QsP1X4rFSdgCgqghej3A3IxEEPnnoQP2tkNE9ydfOAkJ7rmqTOifEUKFvvF6iFy7gPDB6KJu4HObwZdotqX8P4zJtcjfzbhG6yD9NtpmOC2NP+bAzGqAUIYGrsQyceDVqbR01cKOea9Mi6kbUyu35toSbyx1IwKg6khGuqVfdndREah9o3g47Y/zzGD96DvVA1TsXb0rCVmefIeaM/2Irr15yBMbDGkAbzmDH7+6YzUzOf1CnGiKZjx/nsmi6sgbet3PM26uzXs1kCV5x58J7hQSVcZkX+ayWF30aBagiWO0p2uF5p/S54D+cJ4QqV36t8s3LCDp+tRD3i8V/U/3ZlCLzAtAJZcvtExWRIIQYOlm5ulYfZVXV77njCZCnCHlFdOnU3uXRIzTia2DbWjHwqM2Dg8axw94Z4UZrnem+GOS3uon6Ot3rXK+zC6ODFehuAMqHg1BGZNyRM2gyjU2fK2xeonncKqjiofmgAybxYnMuuBxRCE+ia921k1Rb6FF6JoK1yUeMLWkGy+wtf4ZWYN82B5suTPF+0uNXm83X8T/2YbF0aoswibFjYSGaz618UowmLecydX+27+qKDQHGmM8MS0jdCNdZ/xkOb9dgoW5M ulTbTiBM SlKm++qx9aQ37cyhHJ1pe3MzG7Jlg2gfmvBiQFbG+FmJpIKLf2As3zvNuCysINHTD7IapzjaSONQub8ERgM6zwL1dmDRNVtepy03E2eDnYlaXw30ax2vrH1imxI3VsPjr4NqIyOm1X0WcuYhmRKl4KkOi0MibphEYy356Hy1VaUz7o7yW1KOwdpef2csr9gXo/cVXvBBVqvTh9Lwasv1duS51v7ne4pduRXnZVF4vutZ6KKKJNm/0oogNuSxMuRlxlQk18ecB+5u+QY1k/eGkAy7HY/HcLCIRm+S96WGoVtcmvf0d0ec2CpS+Fw== 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: Yu-cheng Yu Shadow stack provides protection for applications against function return address corruption. It is active when the processor supports it, the kernel has CONFIG_X86_SHADOW_STACK enabled, and the application is built for the feature. This is only implemented for the 64-bit kernel. When it is enabled, legacy non-shadow stack applications continue to work, but without protection. Since there is another feature that utilizes CET (Kernel IBT) that will share implementation with shadow stacks, create CONFIG_CET to signify that at least one CET feature is configured. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Remove capitalization of shadow stack (Boris) v3: - Add X86_CET (Kees) - Add back WRUSS dependency (Kees) - Fix verbiage (Dave) - Change from promt to bool (Kirill) - Add more to commit log v2: - Remove already wrong kernel size increase info (tlgx) - Change prompt to remove "Intel" (tglx) - Update line about what CPUs are supported (Dave) Yu-cheng v25: - Remove X86_CET and use X86_SHADOW_STACK directly. arch/x86/Kconfig | 24 ++++++++++++++++++++++++ arch/x86/Kconfig.assembler | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 3604074a878b..d0037181bc15 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1851,6 +1851,11 @@ config CC_HAS_IBT (CC_IS_CLANG && CLANG_VERSION >= 140000)) && \ $(as-instr,endbr64) +config X86_CET + def_bool n + help + CET features configured (Shadow stack or IBT) + config X86_KERNEL_IBT prompt "Indirect Branch Tracking" def_bool y @@ -1858,6 +1863,7 @@ config X86_KERNEL_IBT # https://github.com/llvm/llvm-project/commit/9d7001eba9c4cb311e03cd8cdc231f9e579f2d0f depends on !LD_IS_LLD || LLD_VERSION >= 140000 select OBJTOOL + select X86_CET help Build the kernel with support for Indirect Branch Tracking, a hardware support course-grain forward-edge Control Flow Integrity @@ -1952,6 +1958,24 @@ config X86_SGX If unsure, say N. +config X86_USER_SHADOW_STACK + bool "X86 userspace shadow stack" + depends on AS_WRUSS + depends on X86_64 + select ARCH_USES_HIGH_VMA_FLAGS + select X86_CET + help + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Applications must be enabled to use it, and old userspace does not + get protection "for free". + + CPUs supporting shadow stacks were first released in 2020. + + See Documentation/x86/shstk.rst for more information. + + If unsure, say N. + config EFI bool "EFI runtime service support" depends on ACPI diff --git a/arch/x86/Kconfig.assembler b/arch/x86/Kconfig.assembler index 26b8c08e2fc4..00c79dd93651 100644 --- a/arch/x86/Kconfig.assembler +++ b/arch/x86/Kconfig.assembler @@ -19,3 +19,8 @@ config AS_TPAUSE def_bool $(as-instr,tpause %ecx) help Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7 + +config AS_WRUSS + def_bool $(as-instr,wrussq %rax$(comma)(%rbx)) + help + Supported by binutils >= 2.31 and LLVM integrated assembler From patchwork Thu Jan 19 21:22:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108779 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 CB30BC004D4 for ; Thu, 19 Jan 2023 21:23:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 725256B007B; Thu, 19 Jan 2023 16:23:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D6A66B007D; Thu, 19 Jan 2023 16:23:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B5396B007E; Thu, 19 Jan 2023 16:23:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 35FB46B007B for ; Thu, 19 Jan 2023 16:23:33 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1022E1A03B3 for ; Thu, 19 Jan 2023 21:23:33 +0000 (UTC) X-FDA: 80372825106.25.F8B348B Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id E4B1640007 for ; Thu, 19 Jan 2023 21:23:30 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TMJlcKO3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163411; 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=4jbP/vvwC2xDxOm16xmDL1x5rE+wMcxLrGHW3HbSuJY=; b=PBtvGtWRcyHLZcXnNXBGBrpooJb+MQjYmNJvBcFVyCe5ZonYZFIQ5yGHhqCkJ7I//xHv6S wGBDP+aBZPfA4Enw1s8M9rP0Lk74mUMC1JVczMQfCAduQW8JXQAfXlBDUTNFp95GQRujOw /2AH4W0DCI2/hr0to7bogJSkj8ZG+LM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TMJlcKO3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163411; a=rsa-sha256; cv=none; b=VMCU7HD4TLhH7uI3ATdqkZ1G1dgR4MNTLb48h96hDbKMFwcF8DT2Hj2kX70DiEwlpcicx6 v9jA8RHCzS8RN2fuKpqZSmAe80BiwQA4jPIwJ6cZ6QygfGlAmgXwtlEN06FFiMnA8q2zaK f34uE3zEGFjuti2UU9PTUIr3klo6ahk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163411; x=1705699411; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=d2PWSkL27px2D+va1Qwpdf/EaJ+P/H11jw+dYhLJV08=; b=TMJlcKO3SdmCH4hZt9oF9u4A87BhWF4R423f3VLo46W6UaUNBruq7EgM 5rDRdYItKcHody5diviITJRQDMG2kAi7rlWGyHCptuPXvRWul1g9DKfU6 xzmNDKgw63WP2GQr72q5AeJ/7+NcilYfrzKx3dzDunB/I4Om5fSbt/tpD 3JdhbZolKOSNGRUkobTfsbXfNIOf/CyVNdsA+/z+yBX6vHbUcZQmQkT+C o5HI9ZpS70igXwv3I71ZscfefYJACXXM2CI+rL4wAZxpHZghFhUA9B7xH 1pPoQ9M/a30ahUZHIiAGufTB6s8POXGCy+7aG1LxWcVV/FsFh8YWrl0VR A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119221" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119221" 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:23:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989138996" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989138996" 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:23: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 Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v5 03/39] x86/cpufeatures: Add CPU feature flags for shadow stacks Date: Thu, 19 Jan 2023 13:22:41 -0800 Message-Id: <20230119212317.8324-4-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E4B1640007 X-Stat-Signature: cchs13antgk44q6odfxbp6jzdoqghctp X-HE-Tag: 1674163410-274722 X-HE-Meta: U2FsdGVkX18lJOvID/L7YpQg7SKsGNqLSELSb4hGmvIFaGooeOYdA0k4LnmPMu0dmV1HM9uLrwUXjDVLheTil1CD6qCV4yS1x8ZFLtsr/azAQyBj5miTb7QwHS47RQVeZ/KpW7+t3biIxdfntGBDzfZQGTGWnIpIbiKh6lHAXcX1x03Kxnu5dlZ8fyMQX6SIO7p3rF7UUVSm2yAtU24M4VNWheKAKSUhaVxB2/9YFRHMKCOsgEMbqB5e6OmTawSwXQVXZt3YVC9fF8Le1ukPaq6ca8tUwDUMOAHYC/puSTOAFfYy5OTZEILVEtfe7mqt3HSK+O16DfHk2I5Iw5k1PIcQbd5Y4LidqcA7ANttEH179r6aZ2OU96s++/mC+HIngg4+fTPVQdmxFUooHZN3bsuMbibLq5VA8vzrTRDQEMybnqsD0cQRKr1Zyr7BKkeFhreFPYuOJ28YG20uB6AN3QoWK2pexpuX+kBfhjPk0iozTj8IF8NBSl7AiGGLCbLcZIh8Li6Ttc5Azbi15JK21z7+sIu3ocp51E60rQxHwAkIHo5jFfAlTDtZGryh5CDwu4XtxMpZT3kiZqkahrIFZloTPhB2YRupYq4WsOszHZhpuFDdR9bCwDowkCoCtOR23xNRftqPlgI3jDZi7j+ERj61ZzVtP8BznpYz+sCIi4pQRFbRxm1lRCqE8Fra3Kn8pqkowcztzzWICyz4ha4kQJ90ZZqNZzMKBrmM/auU70K8pxH7yLSwEWOoaiW0TPAnfoI/CjCD2TBeRReYBgv2Azh2KrOiBvw8zWNFZR2tIVueyDgf31FIDV3B4cNodZ2UK1qqxQWArvOhca9CrslGxHEqm/fGDm7CprjOxBtu5YB+ZirSn9ak2z+ZkJc1d8qSqT9i6pU2QyDt+ig99igTTk3BgYPourMcAEF6scQHTBcXHgxaqh7okdO6Uei4ZGUtg8gNtD/NR/QQhmVpzC0 2drIjUbR BiM7dUyiCuPFFCKeNo+Rz4pQp3xEvEDtuWObxwmoUv9FF7YsWMrYTNfpbR4fX6kxbOU4oPp9eHHeokjc605nsVr/M0Xw8GtMybXcsDzvCIxUzLIgApzqar+KfBXcqHxcLmyAmElAp3RXLw54xTy6BU92seA4y1VKCA4az+ODp82P+tXSut13uFTZqKzaQSMEUzR5BwrFtLuY5HZiBNlalH3XK7YUPvfkBku1fW9ps8Nh0mEMTB00VTDWvIh5SrEsR7L2+9/eBBIk1FIeo1EwXdmS8VWmnMQ6XosPx 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: Yu-cheng Yu The Control-Flow Enforcement Technology contains two related features, one of which is Shadow Stacks. Future patches will utilize this feature for shadow stack support in KVM, so add a CPU feature flags for Shadow Stacks (CPUID.(EAX=7,ECX=0):ECX[bit 7]). To protect shadow stack state from malicious modification, the registers are only accessible in supervisor mode. This implementation context-switches the registers with XSAVES. Make X86_FEATURE_SHSTK depend on XSAVES. The shadow stack feature, enumerated by the CPUID bit described above, encompasses both supervisor and userspace support for shadow stack. In near future patches, only userspace shadow stack will be enabled. In expectation of future supervisor shadow stack support, create a software CPU capability to enumerate kernel utilization of userspace shadow stack support. This user shadow stack bit should depend on the HW "shstk" capability and that logic will be implemented in future patches. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Drop "shstk" from cpuinfo (Boris) - Remove capitalization on shadow stack (Boris) v3: - Add user specific shadow stack cpu cap (Andrew Cooper) - Drop reviewed-bys from Boris and Kees due to the above change. v2: - Remove IBT reference in commit log (Kees) - Describe xsaves dependency using text from (Dave) v1: - Remove IBT, can be added in a follow on IBT series. arch/x86/include/asm/cpufeatures.h | 2 ++ arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/kernel/cpu/cpuid-deps.c | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 7b319acda31a..a8551b6c8041 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -307,6 +307,7 @@ #define X86_FEATURE_SGX_EDECCSSA (11*32+18) /* "" SGX EDECCSSA user leaf function */ #define X86_FEATURE_CALL_DEPTH (11*32+19) /* "" Call depth tracking for RSB stuffing */ #define X86_FEATURE_MSR_TSX_CTRL (11*32+20) /* "" MSR IA32_TSX_CTRL (Intel) implemented */ +#define X86_FEATURE_USER_SHSTK (11*32+21) /* Shadow stack support for user mode applications */ /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4) /* AVX VNNI instructions */ @@ -373,6 +374,7 @@ #define X86_FEATURE_OSPKE (16*32+ 4) /* OS Protection Keys Enable */ #define X86_FEATURE_WAITPKG (16*32+ 5) /* UMONITOR/UMWAIT/TPAUSE Instructions */ #define X86_FEATURE_AVX512_VBMI2 (16*32+ 6) /* Additional AVX512 Vector Bit Manipulation Instructions */ +#define X86_FEATURE_SHSTK (16*32+ 7) /* "" Shadow stack */ #define X86_FEATURE_GFNI (16*32+ 8) /* Galois Field New Instructions */ #define X86_FEATURE_VAES (16*32+ 9) /* Vector AES */ #define X86_FEATURE_VPCLMULQDQ (16*32+10) /* Carry-Less Multiplication Double Quadword */ diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 5dfa4fb76f4b..505f78ddca82 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -99,6 +99,12 @@ # define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31)) #endif +#ifdef CONFIG_X86_USER_SHADOW_STACK +#define DISABLE_USER_SHSTK 0 +#else +#define DISABLE_USER_SHSTK (1 << (X86_FEATURE_USER_SHSTK & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -114,7 +120,7 @@ #define DISABLED_MASK9 (DISABLE_SGX) #define DISABLED_MASK10 0 #define DISABLED_MASK11 (DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \ - DISABLE_CALL_DEPTH_TRACKING) + DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK) #define DISABLED_MASK12 0 #define DISABLED_MASK13 0 #define DISABLED_MASK14 0 diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c index d95221117129..c3e4e5246df9 100644 --- a/arch/x86/kernel/cpu/cpuid-deps.c +++ b/arch/x86/kernel/cpu/cpuid-deps.c @@ -79,6 +79,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, + { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, {} }; From patchwork Thu Jan 19 21:22:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108780 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 3A5EEC46467 for ; Thu, 19 Jan 2023 21:23:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5784C6B007D; Thu, 19 Jan 2023 16:23:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B2976B007E; Thu, 19 Jan 2023 16:23:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2433C6B0080; Thu, 19 Jan 2023 16:23:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0DAB76B007D for ; Thu, 19 Jan 2023 16:23:34 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CBEDE807E9 for ; Thu, 19 Jan 2023 21:23:33 +0000 (UTC) X-FDA: 80372825106.19.2B478FD Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf29.hostedemail.com (Postfix) with ESMTP id D5C4A120008 for ; Thu, 19 Jan 2023 21:23:31 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Rux9FGGx; spf=pass (imf29.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=1674163412; a=rsa-sha256; cv=none; b=WSEmvyXri3UBlyjroUjXeI8PlDokuu6tmxpUtHzZgrZMljOPSIHqzeb+EfCcVzKVZ4CFBu ae8/kE48sonW014Fs8/s1Ly14hc5k9cWDQk6mKz/A7jKEJp31joQWsM0W1aiAFkzO2KPPw /1DWZiMdUEFk9NjVa6ldL6hNyevYkOo= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Rux9FGGx; spf=pass (imf29.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=1674163412; 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=vxp0lIQdGnMd31RbCo3vuJaZKtAwgMarQuDGlKx2nuY=; b=SkQYxLC6v6pkhP4+YcsMUWduDmybCTEDI/yKEhi0KdvwoLFqdX2P5CYa3bAaGZy7dt4oxT 1uKI80ffRNd0thtzuEFJHi7qCTZ/j8VHJN33Sx4ysHxe6evUqN8jtiC0l+txVU+/9JLllQ q0BfOaTg/396aOwSyrI4i668WAv9TuI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163411; x=1705699411; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=d7P5HLFSxYXgzr8kgfmXPsFqpuUUbCRpcWe/3LmTQFM=; b=Rux9FGGx+4Ascy/Gj71YzZ16od31T3djf10JESqSWbscq+mF+ot2RdNB bKCIQ/PJvTO3vU8u3Vn+zYLMvl5nLZVe1OnYmchaJZPbo4G5KhzfYFuch Abi2UDV9GNUPxWRg8hBmWiWdO731kbipFT7KUvvgqn+m5uAjfq5xBemeY FmxKiG5F0VjJqFadMGu2YAwSSmt1NJZ/nTDF/ROz5vlmEMVDC8eyQg13G DbMoJTkPbPfTUFkbkwFQ/9f+B43XiLCxNFQTONLq4MAHHsuANkkXTYpH8 f0hBznpL65ntHsXVGnN/tyrpm0+SGN2euORp3pYHx77zYjEPGpJaFW2un w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119247" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119247" 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:23:31 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139002" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139002" 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:23:29 -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, Yu-cheng Yu Subject: [PATCH v5 04/39] x86/cpufeatures: Enable CET CR4 bit for shadow stack Date: Thu, 19 Jan 2023 13:22:42 -0800 Message-Id: <20230119212317.8324-5-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-Rspam-User: X-Rspamd-Queue-Id: D5C4A120008 X-Rspamd-Server: rspam01 X-Stat-Signature: fzir9hterp5fyujsgw6c4b7rwhtbx7rn X-HE-Tag: 1674163411-249626 X-HE-Meta: U2FsdGVkX1+r9uT23vBufA/xdrr1WTcmquffuSZunOngseOtEG5lK4LBnaAn12sYpXzmRGDv0fExZcuhJl90Q2cDsv/uDU0FD3qRKJY9wk+sAZ4tKhaj6iT+aWiKj0jOSRnzcn74J/y5XgUl03Hq7C8j1O8qMI7Nn1nEM9h4XckarlMQIO+ru5VWQG9I6S8zfLoVWj6OzqCnOgVi+TbJYoZBxBbnGBZXR4+8w6rA5jWw3autTZZjxmU/Vi87RTx5hp3TI1DHpbLr0/Yc1hKEZDMObHqpgsloCq9dlONOxI+TrKaWuGlV9PKjbA+v0Ovj7P7mYOHYZyLOCfb0SNXrZap1AEva6uobuiTH/GvmoP+w2ed9Ho0q2xwaryCTSzBuqzVdwtzLYvi4xXlNzmVEpR995ytfRDGUVLAuCw6tS3DqSM2MXd7WKEWpYQOyItBBJ/vZ23OBm0KBxj3lKek4GKZZzaHfRzxIa8ReUQq7N1qgqZk4g2h5fo92jsQU+K6PTGkCUtUhca0XvGEtHf4hrEeVgTEMbv98fPZ0TTzX6qXc5Y2/QaquczMHLpe3fNklyeEAD4VV1ZKp/sWSczE7gFh3rjytITbfIIEwqhZQzyxspU+WisawSt/m31X942Ij7myyt/4SkSlGinF/a94GlOMUIux9rClb0ZsuqkiLraKJx9Hc+o3EkEOolZ2D0nwflRBoqG2VxI8j+j+mcduC29u/CWpz/JJXhrPyrzxEiKQWAqEHLOkNZ//MkpLBszejP+u22PsGhYuQN28IrEvsoN44k+wlTXeY1cz/E999Q1E9yFsh6sBaupQI5vua5tju5EbfWUm7rWZRe2vHZEVQrrMw5mMmwNbxn5FSn8OdXgJcrncNGuDebRvjwiaCX2TeLYHVeIXcZwni+UZ81XyIxDGpQ5F0H7lvWU3uaKa9qqjC4+M49c6wGPK0EK2edrhcP3v4SwicdvR/oxIwblQ qniNy8EE 05+frYUwzn2e81RLKW9JMJAiYaidrM63hpbWhx4vyVhShdhC/qzpPStSxK4t9snLzTiUyOdHRnL7pzLseILlpQTJjQQSEWyiA1L1X6E0D/3He8ld0AhN9TlNK5ja8dxqnP57zLwVtnNOO/JFyAPxbR7ElMTfjyLh5zRORnHM4hCyM7rYbhFWxGuUOcuSXyaGmDNMIyoZ4ek9h3akOqLp1hvla0e5b/uEm6J/oFxt+ZfllPzpBL44zqC0mY+RwhocaYQ3vj3juRx0ymrXfjG4niQC/eDAZ8kSyAt// 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: Yu-cheng Yu Setting CR4.CET is a prerequisite for utilizing any CET features, most of which also require setting MSRs. Kernel IBT already enables the CET CR4 bit when it detects IBT HW support and is configured with kernel IBT. However, future patches that enable userspace shadow stack support will need the bit set as well. So change the logic to enable it in either case. Clear MSR_IA32_U_CET in cet_disable() so that it can't live to see userspace in a new kexec-ed kernel that has CR4.CET set from kernel IBT. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Remove #ifdeffery (Boris) v4: - Add back dedicated command line disable: "nousershtk" (Boris) v3: - Remove stay new line (Boris) - Simplify commit log (Andrew Cooper) v2: - In the shadow stack case, go back to only setting CR4.CET if the kernel is compiled with user shadow stack support. - Clear MSR_IA32_U_CET as well. (PeterZ) arch/x86/kernel/cpu/common.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index cec654e674ff..80507a5ba0ca 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -599,27 +599,43 @@ __noendbr void ibt_restore(u64 save) static __always_inline void setup_cet(struct cpuinfo_x86 *c) { - u64 msr = CET_ENDBR_EN; + bool user_shstk, kernel_ibt; - if (!HAS_KERNEL_IBT || - !cpu_feature_enabled(X86_FEATURE_IBT)) + if (!IS_ENABLED(CONFIG_X86_CET)) return; - wrmsrl(MSR_IA32_S_CET, msr); + kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT); + user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) && + IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK); + + if (!kernel_ibt && !user_shstk) + return; + + if (user_shstk) + set_cpu_cap(c, X86_FEATURE_USER_SHSTK); + + if (kernel_ibt) + wrmsrl(MSR_IA32_S_CET, CET_ENDBR_EN); + else + wrmsrl(MSR_IA32_S_CET, 0); + cr4_set_bits(X86_CR4_CET); - if (!ibt_selftest()) { + if (kernel_ibt && !ibt_selftest()) { pr_err("IBT selftest: Failed!\n"); wrmsrl(MSR_IA32_S_CET, 0); setup_clear_cpu_cap(X86_FEATURE_IBT); - return; } } __noendbr void cet_disable(void) { - if (cpu_feature_enabled(X86_FEATURE_IBT)) - wrmsrl(MSR_IA32_S_CET, 0); + if (!(cpu_feature_enabled(X86_FEATURE_IBT) || + cpu_feature_enabled(X86_FEATURE_SHSTK))) + return; + + wrmsrl(MSR_IA32_S_CET, 0); + wrmsrl(MSR_IA32_U_CET, 0); } /* @@ -1476,6 +1492,9 @@ static void __init cpu_parse_early_param(void) if (cmdline_find_option_bool(boot_command_line, "noxsaves")) setup_clear_cpu_cap(X86_FEATURE_XSAVES); + if (cmdline_find_option_bool(boot_command_line, "nousershstk")) + setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK); + arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg)); if (arglen <= 0) return; From patchwork Thu Jan 19 21:22:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108783 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 0D13CC6379F for ; Thu, 19 Jan 2023 21:23:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0C106B007E; Thu, 19 Jan 2023 16:23:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB566B0080; Thu, 19 Jan 2023 16:23:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B0048E0001; Thu, 19 Jan 2023 16:23:36 -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 4BE4D6B007E for ; Thu, 19 Jan 2023 16:23:36 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9425940DEE for ; Thu, 19 Jan 2023 21:23:35 +0000 (UTC) X-FDA: 80372825190.07.87892EA Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 72CC34001C for ; Thu, 19 Jan 2023 21:23:33 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FTxtjZBZ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163413; 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=V27cKRY2KCv5vyRkPBcMUXcbgd5W/TlMLH+92/zt3E0=; b=eaL/RNNNVPSVLV6PT83fRf2s9zTQsI/F49wkNHpo37tuY945OjjmmI6k+WKQPUYaKFvnrb u1ZmFhsGrxZ3jdMtwzkXgOaej2LpoYAmMyKfwK39a1JS61zlOoslyb6Tcd4pYVjdIGz/r6 ZB+bgIby/OoLsfnp6C+yBejjDzepK10= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FTxtjZBZ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163413; a=rsa-sha256; cv=none; b=A0eXgZkcu6jV8ZsOxqjV3b8ii6blg1GJ9nT2iuxtRVZF9qYWQysN7oNu2PoNyX+fXmIEHW 277UutePzQzFsZuAofbwUcrdNn5qQTGYWnxOYxVIBD1dQS/gL4fyfRWDYSEdBiVhaEWmXP 2djwntn5QdkVfCL48+xi+lk8edwJ83g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163413; x=1705699413; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=oiH2FubPSeETMvfMbKf2yNJtMKC2vkuWNh3uopEbpLc=; b=FTxtjZBZJHy0cbTUjd11yyoKeoW71Xa0ouAMxI9qtwuO8+y89IJI8nAW ngNPTkELYj4aeisAh1l848K9if0sOKiUPMG6iOK0iGWZj7G9UNQwUGRnj 6pWJSp3zxRH4pXhZmR9/IGp47drU4AwMcKi5OmEF9MG3+89H44SSHod14 Oh1GLi0UHm1oX237L6tiiOyFGhh7WGjYltt9BAiBuLTPx3K1aycHgqAY/ ulWZu2cxcrd949s0SNUyV+MgOD4fQr7g4cn34dJbIeqviWteZFnHcwPGX qOgAvkbHe/2SshlAbaa0myArg8GEtZ/YxXwolpojj/38DdI0Z65+zpFfZ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119269" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119269" 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:23:32 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139006" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139006" 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:23:31 -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, Yu-cheng Yu Subject: [PATCH v5 05/39] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states Date: Thu, 19 Jan 2023 13:22:43 -0800 Message-Id: <20230119212317.8324-6-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 72CC34001C X-Stat-Signature: 1edb4g8y85nsnr5f9gm5ukxtx83zckyh X-HE-Tag: 1674163413-596503 X-HE-Meta: U2FsdGVkX19o9n32CPWufOx1838ln/P7Lu9IB2BjFXjnkEOmTTS1IzCRTUDQJ9seCbvZt3bToWg3neE1Iax9rKBEUl1ZYTT91rqpV9D+oPdBnTzHRvTZnKtRDWPEKjgWUvvgGLkj6OFvOLbpHZZS135JMbxKE+UKAJ5Nz1HLQ1kX/Ep8en6lwSjgk9FOEtwApAKzGppM7HPGkEcpVBG8eWNRJR3OSbK3Zaf2h691UE101XMpV11zkYsSPE/SbZof1KaKlYW/9Zz835lZVSI/WL0/N/Vr4+oPoXSsVmNYvvbtnV/7Q0YGnYqZUNxC54NSkOssnIrJ3c50w4jpnShGjbfkfkPMBrmREqXgPiXLyOTozIZX3//mUqCG8O+n8TdLIy9wIrBwxA7UIYrRSm3z4oI6eGNCCwXk3Gp6mDqOW+/OSnUxesDbIY0HewOi1un64Mf8JQFGJvWbjsh4qunv27ub+4dGDq6jGZfGpzjiv3DiG2k9okZwoMdBs8Qls/gLUYEdYarD6TD7rBn08Lpfns6VmB1hM+l+D5yk8Y6Z++ICbrpags0J+R9HNQlJs8ylnB6cgEZOPZ4wOZ9bOGfKrC/DxgAvF+8uKZLfAWWN2Bg34WjY4ksUspnCLpkB7MMN10S6nO0dDoznALaIX4VyrceWJ4z0n5VGma/3KMIbmTUAtB9dB9VQYFS8XqfEo9T2VaYgXCb57xuZ+oLLp6Fcf0FFE3y6o8196aUwl0kW81AkulRK7H7EuXTpS4kQ3ZeHobdzxP6lNZ5ZPp6KrxwIKvBC+gspRN4ygd2kJG2Z86rouLS22Q4I614g9G0KDF+Aq08W+7RaQ6sQmhUf59RP+ui02S5vi/bXUXr9nDrkM9BhbJVNEgxm/bvCPwOerwrF3Ci+FTetI3uVzhypxCNAvvzhxcXV6CxUYU0EkJnvOGcI7Lh1S9Pz6bYu8UjvPQ+pQgD2r9RRYP8uG/QfQ7V w2UsxHBz ar53EI4IXwdH3JWS0sDvtzvPsTtS2RxM2WI0P4r4ZsoXdbiyF2Nm3/vbSlIqIXZ5nHxXzS3gJf89He0jEMKNS1Ln407eQorSTifbVwG/skN8Egw/+G0xWc8/Z4icKwcVOqQVXUDOtuMNtrFofLYw+aKpJcEyqovlErN06puIo0KCKa6tNDsvtpisc6p5iPGD28fE4gcSvhk+CbDvQN9R6U/+w8iWiD1IMoLNkZjL6FD37l9sknLioB5+lQMd7YYiAgNa8yeoCarDWKOH1y8Wq0f7aMjZ3tSyF8T1I 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: Yu-cheng Yu Shadow stack register state can be managed with XSAVE. The registers can logically be separated into two groups: * Registers controlling user-mode operation * Registers controlling kernel-mode operation The architecture has two new XSAVE state components: one for each group of those groups of registers. This lets an OS manage them separately if it chooses. Future patches for host userspace and KVM guests will only utilize the user-mode registers, so only configure XSAVE to save user-mode registers. This state will add 16 bytes to the xsave buffer size. Future patches will use the user-mode XSAVE area to save guest user-mode CET state. However, VMCS includes new fields for guest CET supervisor states. KVM can use these to save and restore guest supervisor state, so host supervisor XSAVE support is not required. Adding this exacerbates the already unwieldy if statement in check_xstate_against_struct() that handles warning about un-implemented xfeatures. So refactor these check's by having XCHECK_SZ() set a bool when it actually check's the xfeature. This ends up exceeding 80 chars, but was better on balance than other options explored. Pass the bool as pointer to make it clear that XCHECK_SZ() can change the variable. While configuring user-mode XSAVE, clarify kernel-mode registers are not managed by XSAVE by defining the xfeature in XFEATURE_MASK_SUPERVISOR_UNSUPPORTED, like is done for XFEATURE_MASK_PT. This serves more of a documentation as code purpose, and functionally, only enables a few safety checks. Both XSAVE state components are supervisor states, even the state controlling user-mode operation. This is a departure from earlier features like protection keys where the PKRU state is a normal user (non-supervisor) state. Having the user state be supervisor-managed ensures there is no direct, unprivileged access to it, making it harder for an attacker to subvert CET. To facilitate this privileged access, define the two user-mode CET MSRs, and the bits defined in those MSRs relevant to future shadow stack enablement patches. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Move comments from end of lines in cet_user_state struct (Boris) v3: - Add missing "is" in commit log (Boris) - Change to case statement for struct size checking (Boris) - Adjust commas on xfeature_names (Kees, Boris) v2: - Change name to XFEATURE_CET_KERNEL_UNUSED (peterz) KVM refresh: - Reword commit log using some verbiage posted by Dave Hansen - Remove unlikely to be used supervisor cet xsave struct - Clarify that supervisor cet state is not saved by xsave - Remove unused supervisor MSRs arch/x86/include/asm/fpu/types.h | 16 +++++- arch/x86/include/asm/fpu/xstate.h | 6 ++- arch/x86/kernel/fpu/xstate.c | 90 +++++++++++++++---------------- 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/arch/x86/include/asm/fpu/types.h b/arch/x86/include/asm/fpu/types.h index eb7cd1139d97..26abde698fc0 100644 --- a/arch/x86/include/asm/fpu/types.h +++ b/arch/x86/include/asm/fpu/types.h @@ -115,8 +115,8 @@ enum xfeature { XFEATURE_PT_UNIMPLEMENTED_SO_FAR, XFEATURE_PKRU, XFEATURE_PASID, - XFEATURE_RSRVD_COMP_11, - XFEATURE_RSRVD_COMP_12, + XFEATURE_CET_USER, + XFEATURE_CET_KERNEL_UNUSED, XFEATURE_RSRVD_COMP_13, XFEATURE_RSRVD_COMP_14, XFEATURE_LBR, @@ -138,6 +138,8 @@ enum xfeature { #define XFEATURE_MASK_PT (1 << XFEATURE_PT_UNIMPLEMENTED_SO_FAR) #define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU) #define XFEATURE_MASK_PASID (1 << XFEATURE_PASID) +#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) +#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL_UNUSED) #define XFEATURE_MASK_LBR (1 << XFEATURE_LBR) #define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG) #define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA) @@ -252,6 +254,16 @@ struct pkru_state { u32 pad; } __packed; +/* + * State component 11 is Control-flow Enforcement user states + */ +struct cet_user_state { + /* user control-flow settings */ + u64 user_cet; + /* user shadow stack pointer */ + u64 user_ssp; +}; + /* * State component 15: Architectural LBR configuration state. * The size of Arch LBR state depends on the number of LBRs (lbr_depth). diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h index cd3dd170e23a..d4427b88ee12 100644 --- a/arch/x86/include/asm/fpu/xstate.h +++ b/arch/x86/include/asm/fpu/xstate.h @@ -50,7 +50,8 @@ #define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA /* All currently supported supervisor features */ -#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID) +#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \ + XFEATURE_MASK_CET_USER) /* * A supervisor state component may not always contain valuable information, @@ -77,7 +78,8 @@ * Unsupported supervisor features. When a supervisor feature in this mask is * supported in the future, move it to the supported supervisor feature mask. */ -#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT) +#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT | \ + XFEATURE_MASK_CET_KERNEL) /* All supervisor states including supported and unsupported states. */ #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \ diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 714166cc25f2..13a80521dd51 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -39,26 +39,26 @@ */ static const char *xfeature_names[] = { - "x87 floating point registers" , - "SSE registers" , - "AVX registers" , - "MPX bounds registers" , - "MPX CSR" , - "AVX-512 opmask" , - "AVX-512 Hi256" , - "AVX-512 ZMM_Hi256" , - "Processor Trace (unused)" , + "x87 floating point registers", + "SSE registers", + "AVX registers", + "MPX bounds registers", + "MPX CSR", + "AVX-512 opmask", + "AVX-512 Hi256", + "AVX-512 ZMM_Hi256", + "Processor Trace (unused)", "Protection Keys User registers", "PASID state", - "unknown xstate feature" , - "unknown xstate feature" , - "unknown xstate feature" , - "unknown xstate feature" , - "unknown xstate feature" , - "unknown xstate feature" , - "AMX Tile config" , - "AMX Tile data" , - "unknown xstate feature" , + "Control-flow User registers", + "Control-flow Kernel registers (unused)", + "unknown xstate feature", + "unknown xstate feature", + "unknown xstate feature", + "unknown xstate feature", + "AMX Tile config", + "AMX Tile data", + "unknown xstate feature", }; static unsigned short xsave_cpuid_features[] __initdata = { @@ -73,6 +73,7 @@ static unsigned short xsave_cpuid_features[] __initdata = { [XFEATURE_PT_UNIMPLEMENTED_SO_FAR] = X86_FEATURE_INTEL_PT, [XFEATURE_PKRU] = X86_FEATURE_PKU, [XFEATURE_PASID] = X86_FEATURE_ENQCMD, + [XFEATURE_CET_USER] = X86_FEATURE_SHSTK, [XFEATURE_XTILE_CFG] = X86_FEATURE_AMX_TILE, [XFEATURE_XTILE_DATA] = X86_FEATURE_AMX_TILE, }; @@ -276,6 +277,7 @@ static void __init print_xstate_features(void) print_xstate_feature(XFEATURE_MASK_Hi16_ZMM); print_xstate_feature(XFEATURE_MASK_PKRU); print_xstate_feature(XFEATURE_MASK_PASID); + print_xstate_feature(XFEATURE_MASK_CET_USER); print_xstate_feature(XFEATURE_MASK_XTILE_CFG); print_xstate_feature(XFEATURE_MASK_XTILE_DATA); } @@ -344,6 +346,7 @@ static __init void os_xrstor_booting(struct xregs_state *xstate) XFEATURE_MASK_BNDREGS | \ XFEATURE_MASK_BNDCSR | \ XFEATURE_MASK_PASID | \ + XFEATURE_MASK_CET_USER | \ XFEATURE_MASK_XTILE) /* @@ -446,14 +449,15 @@ static void __init __xstate_dump_leaves(void) } \ } while (0) -#define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \ - if ((nr == nr_macro) && \ - WARN_ONCE(sz != sizeof(__struct), \ - "%s: struct is %zu bytes, cpu state %d bytes\n", \ - __stringify(nr_macro), sizeof(__struct), sz)) { \ +#define XCHECK_SZ(sz, nr, __struct) ({ \ + if (WARN_ONCE(sz != sizeof(__struct), \ + "[%s]: struct is %zu bytes, cpu state %d bytes\n", \ + xfeature_names[nr], sizeof(__struct), sz)) { \ __xstate_dump_leaves(); \ } \ -} while (0) + true; \ +}) + /** * check_xtile_data_against_struct - Check tile data state size. @@ -527,36 +531,28 @@ static bool __init check_xstate_against_struct(int nr) * Ask the CPU for the size of the state. */ int sz = xfeature_size(nr); + /* * Match each CPU state with the corresponding software * structure. */ - XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct); - XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state); - XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state); - XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state); - XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state); - XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state); - XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state); - XCHECK_SZ(sz, nr, XFEATURE_PASID, struct ia32_pasid_state); - XCHECK_SZ(sz, nr, XFEATURE_XTILE_CFG, struct xtile_cfg); - - /* The tile data size varies between implementations. */ - if (nr == XFEATURE_XTILE_DATA) - check_xtile_data_against_struct(sz); - - /* - * Make *SURE* to add any feature numbers in below if - * there are "holes" in the xsave state component - * numbers. - */ - if ((nr < XFEATURE_YMM) || - (nr >= XFEATURE_MAX) || - (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR) || - ((nr >= XFEATURE_RSRVD_COMP_11) && (nr <= XFEATURE_RSRVD_COMP_16))) { + switch (nr) { + case XFEATURE_YMM: return XCHECK_SZ(sz, nr, struct ymmh_struct); + case XFEATURE_BNDREGS: return XCHECK_SZ(sz, nr, struct mpx_bndreg_state); + case XFEATURE_BNDCSR: return XCHECK_SZ(sz, nr, struct mpx_bndcsr_state); + case XFEATURE_OPMASK: return XCHECK_SZ(sz, nr, struct avx_512_opmask_state); + case XFEATURE_ZMM_Hi256: return XCHECK_SZ(sz, nr, struct avx_512_zmm_uppers_state); + case XFEATURE_Hi16_ZMM: return XCHECK_SZ(sz, nr, struct avx_512_hi16_state); + case XFEATURE_PKRU: return XCHECK_SZ(sz, nr, struct pkru_state); + case XFEATURE_PASID: return XCHECK_SZ(sz, nr, struct ia32_pasid_state); + case XFEATURE_XTILE_CFG: return XCHECK_SZ(sz, nr, struct xtile_cfg); + case XFEATURE_CET_USER: return XCHECK_SZ(sz, nr, struct cet_user_state); + case XFEATURE_XTILE_DATA: check_xtile_data_against_struct(sz); return true; + default: XSTATE_WARN_ON(1, "No structure for xstate: %d\n", nr); return false; } + return true; } From patchwork Thu Jan 19 21:22:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108784 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 92C17C004D4 for ; Thu, 19 Jan 2023 21:23:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7741C6B0080; Thu, 19 Jan 2023 16:23:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D6846B0081; Thu, 19 Jan 2023 16:23:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 418316B0088; Thu, 19 Jan 2023 16:23:37 -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 2C23A6B0080 for ; Thu, 19 Jan 2023 16:23:37 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E7234AAAF5 for ; Thu, 19 Jan 2023 21:23:36 +0000 (UTC) X-FDA: 80372825232.30.F50B3B4 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf29.hostedemail.com (Postfix) with ESMTP id 0010512000F for ; Thu, 19 Jan 2023 21:23:34 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XGv2svSQ; spf=pass (imf29.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=1674163415; a=rsa-sha256; cv=none; b=Rm3PmV5WLECHNsZImIBRF13BK2ITNQzwSGzNohboS8cGbev8HrG4rUQJX+feSSnPTdADh4 T864peQVxi/eGxHbi6qG7bykWDY+hVpRJ6EQy3T7H2y4i1RqUpKXJAci7j0v4HuCiB//U8 SuYqTnK0PsdaA/0RHAgtiCuOdFoQEZQ= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XGv2svSQ; spf=pass (imf29.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=1674163415; 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=/I2rA7g9gaeqAh7utVe0HX92Ho3+6Ko6l1A1i1JyDLw=; b=2f7k7K/drJPfCytRdBnzFm40vZYtbT5Ivi51VSATjaQ8SWwaa4SdtPa0hbX4i9L8ZMPSGd Bf5LobmpZo+1FcvX7MW/IyEKjeWowZ/q4EW93Vt6Ebsczquy681P2H0c8bNU/Y1hiTv7Hd EBjkLuWLXtMHGWfStgfXGJfNeoKd+bI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163415; x=1705699415; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=AD5tXA8hUee5SZ1Pd+Ll3tDzu/Wr8phq95vUnNoKkT8=; b=XGv2svSQ5sue7SbqSGaNPyyXdqh5nPX3klWE98bZ036cCkB/klMX/ZY1 NLzd6idsNWewSJAmvOQGlYDPIzW89pHt7STUnK+EHSbIwWxKcOO+zyO4j L4YJz1iktDYoVnz7m8dJgKuRLjpfu1qhVk0GUkSp0NP7dMa+u8ahbFib6 K4zThSQrldVI78cOBciJPCWjdF+r92HF1RbppetF6ndaIrUwFxJ2vlr8a NjwZJJkkIzgRpMvpg1+gqtkQQo/hvNDbPoyF0GUNThQT3XhYvXoa/ywLd cSpR2L56Maiw9pg6QPAOklJvLGtsfO6T6PWZxF4iSS0vb5wPHHz0lkizf A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119295" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119295" 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:23:34 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139011" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139011" 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:23:32 -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 06/39] x86/fpu: Add helper for modifying xstate Date: Thu, 19 Jan 2023 13:22:44 -0800 Message-Id: <20230119212317.8324-7-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-Rspam-User: X-Rspamd-Queue-Id: 0010512000F X-Rspamd-Server: rspam01 X-Stat-Signature: 4wuf66ia56sa5a1nstg7t84a1mccmz4q X-HE-Tag: 1674163414-389894 X-HE-Meta: U2FsdGVkX181PWi47RDXV/MtHZp+4bVf0Gd8nLdSsBmkjvBIQoq2XkXKk5XKFlVBG7UHLxjsALG7ogxpDzEu1Cd8R4e3NXed3zSDqa3nh8dAPxkpDiq/PYNsRk9ENy7escPBpZ9stqCWQOnfeVBqtD9GMvQXzlhkppWhE8gVl2VZ4zT+V6A8mWJNDjKGJPSKHr5/jlfNQBWh4HofqTz/c2GiwPhGKr8uCPSBN5GhCCVWWOU9UxCU74gXk2ouB9Nm+fjGXNnr9EEjkKdCrRxZ0sS5DimMiXvo1knuscL0DY11Q8vEdCzh0AWJWi5tZ5b+rSVbkwut5LZxnnPAZMmzTLIKi6sxnbq39HoDzzJn+POAN9qxyLZZ5lbsU1/JI00vT5fxSC3XsituB/sWxIEwiM2Hq+8YmNlHTLo3BmS8M061v9/KLox30LybWgXlHZ2NOcFd8wit11GgGCzpH4ugJvx9qVWT1Ubzqor+ncQjBaJRBx1dl3WLjoCMbIwUPz92KrZZKh1hrfsVauZ81pln21temyr7lyCRxl33MuIv+NuMy/DJmiYAtGfY8msgyf5qYAtnJxTikkk5mQxgf0jV40oOUngmgvfzBNrEs0aluQh9pFZx3ZkF8FIFq5K4ozslUJShAe14mfJhB/RWBkh2+PNUrHD5LxhpLzb2+VSn9+J5FjQXM0EE4ZKUdPo7ipfeAyyBBgmcU7aERCA2kkeMpdgnoBBkH4I6URYfPI5Y2h74T7GLQAzx+djDLfIz8rtEv6t6le/U7eL2HUOGmStCVhEYH9jI1bKEyCTPR/tbuxalOctWS9Q76bDPY5CpcbWUkhAOeekbvaBEBT4gso0n+A+arixBP0c2jyWfYZMYlFNcGEDjE0WMaxvVEJhVtemqng9WxWug5AM/RJbdsp4Gb5IcvG5hbAlTnkshYESFQ2BYq6mbySWM4VzpTvvvgbf6ggvX3JOShWdoEE5kmvt 25UMx+ua sXVa16vYfC4YvLp9vAliXrpWf4fQmqBJPrBC56KHOmIi+2v30btm+XAjRrEEmB7bpqHPNvpZaDnAvgflZ90nopss9d4dJxDupxzSXZrvNY+YucHX+e8bn+LYnLjMK3W6PCD8TFmhAHawS+TwwxBrNt+K+nNj8bdGZwvZeO6ELHJag9tAcgsUkvfnssS10K7dZi+k2SOQ8jdzB/ECv40Ng8Hg4+NifCQq5cT9vNzCdIUbgACs= 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: Just like user xfeatures, supervisor xfeatures can be active in the registers or present in the task FPU buffer. If the registers are active, the registers can be modified directly. If the registers are not active, the modification must be performed on the task FPU buffer. When the state is not active, the kernel could perform modifications directly to the buffer. But in order for it to do that, it needs to know where in the buffer the specific state it wants to modify is located. Doing this is not robust against optimizations that compact the FPU buffer, as each access would require computing where in the buffer it is. The easiest way to modify supervisor xfeature data is to force restore the registers and write directly to the MSRs. Often times this is just fine anyway as the registers need to be restored before returning to userspace. Do this for now, leaving buffer writing optimizations for the future. Add a new function fpregs_lock_and_load() that can simultaneously call fpregs_lock() and do this restore. Also perform some extra sanity checks in this function since this will be used in non-fpu focused code. Tested-by: Pengfei Xu Tested-by: John Allen Suggested-by: Thomas Gleixner Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix spelling error (Boris) - Don't export fpregs_lock_and_load() (Boris) v3: - Rename to fpregs_lock_and_load() to match the unlocking fpregs_unlock(). (Kees) - Elaborate in comment about helper. (Dave) v2: - Drop optimization of writing directly the buffer, and change API accordingly. - fpregs_lock_and_load() suggested by tglx - Some commit log verbiage from dhansen v1: - New patch. arch/x86/include/asm/fpu/api.h | 9 +++++++++ arch/x86/kernel/fpu/core.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/arch/x86/include/asm/fpu/api.h b/arch/x86/include/asm/fpu/api.h index 503a577814b2..aadc6893dcaa 100644 --- a/arch/x86/include/asm/fpu/api.h +++ b/arch/x86/include/asm/fpu/api.h @@ -82,6 +82,15 @@ static inline void fpregs_unlock(void) preempt_enable(); } +/* + * FPU state gets lazily restored before returning to userspace. So when in the + * kernel, the valid FPU state may be kept in the buffer. This function will force + * restore all the fpu state to the registers early if needed, and lock them from + * being automatically saved/restored. Then FPU state can be modified safely in the + * registers, before unlocking with fpregs_unlock(). + */ +void fpregs_lock_and_load(void); + #ifdef CONFIG_X86_DEBUG_FPU extern void fpregs_assert_state_consistent(void); #else diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index dccce58201b7..7317bfd5ea36 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -753,6 +753,24 @@ void switch_fpu_return(void) } EXPORT_SYMBOL_GPL(switch_fpu_return); +void fpregs_lock_and_load(void) +{ + /* + * fpregs_lock() only disables preemption (mostly). So modifying state + * in an interrupt could screw up some in progress fpregs operation, + * but appear to work. Warn about it. + */ + WARN_ON_ONCE(!irq_fpu_usable()); + WARN_ON_ONCE(current->flags & PF_KTHREAD); + + fpregs_lock(); + + fpregs_assert_state_consistent(); + + if (test_thread_flag(TIF_NEED_FPU_LOAD)) + fpregs_restore_userregs(); +} + #ifdef CONFIG_X86_DEBUG_FPU /* * If current FPU state according to its tracking (loaded FPU context on this From patchwork Thu Jan 19 21:22:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108785 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 F2B55C004D4 for ; Thu, 19 Jan 2023 21:23:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 906516B008A; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85D808E0001; Thu, 19 Jan 2023 16:23:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 686836B0089; Thu, 19 Jan 2023 16:23:41 -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 5420C6B0081 for ; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 21520A064D for ; Thu, 19 Jan 2023 21:23:41 +0000 (UTC) X-FDA: 80372825442.29.E7906ED Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf06.hostedemail.com (Postfix) with ESMTP id 09A0E180011 for ; Thu, 19 Jan 2023 21:23:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DR3KDRKX; spf=pass (imf06.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=1674163418; 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=Zfu5uYQB7EldEQLqi+9Lt3YRP95ugqetP6chtNBuX1o=; b=UlLb+EtKH4rfvaL0wzFuP+hbPGxeDdrXMgeymqMvoiKuCKIV63j0uLFZMJlrrCqBMweE/0 UbyMbCwFxLEK9ZSVU9Bl2OdZp5U2IyGq4OrZa98dvXgGcypWY3q9N5KOa69nqsMCnqyYdm Gc9J6WBysw/QAofTdkQFDWtea/y6+4g= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DR3KDRKX; spf=pass (imf06.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=1674163418; a=rsa-sha256; cv=none; b=ppSCdWuZ7amjdCUSkJPO0q1ytp45BtBD8IGdOtLsU871i6UPzwDmMaOQjHoy7L8As0e6lw rmqW1jRKI3++S+KDl0owrsG37VSoGmszm3sqd0slCIcfA+6wMng1aqYNc1JXDgkQE5/3nW r8SppFSvALaquEwAk0nDQTd3Ibz5sGA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163418; x=1705699418; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=PzST9NKmNucCPEHUcN1NhrjLFoRDDlEf869nc/warJQ=; b=DR3KDRKXIbCDN+sf+IBWE14MWJSz4e25EvFVAK9/XQ5j3htiSoIX1Lqy RpmGhHx40hnR3+BNMAPPEHLI4aXB0lHfXbhie4n8D9M0aPlMc14x0K6v+ O57IbQYyhWQKTXWUJIY/TYuyfdOJQ3w8+i6kBQO9Cy3taEuzyTi2KYLLF XZGbAQsFxBpOIlVoaR3+BKpYI9eor2H7tkm/lt5S1IdagO0LXjVEHI6ow 7CQIUM1c9rVwHi2oWzdWWR+rdw68PXIqSMoTgsOonJmMvOhbcY3lI35bV /k96h1DVUGeTOnWfrE4nbCC3h4inGlqGMdFI4SV3hD8ZgJAdp0ajc5G2t Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119317" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119317" 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:23:36 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139017" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139017" 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:23:34 -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, Yu-cheng Yu , Michael Kerrisk Subject: [PATCH v5 07/39] x86: Add user control-protection fault handler Date: Thu, 19 Jan 2023 13:22:45 -0800 Message-Id: <20230119212317.8324-8-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 09A0E180011 X-Stat-Signature: k8xkgazkxxe1gp98r9xby1kik638jymc X-HE-Tag: 1674163417-48491 X-HE-Meta: U2FsdGVkX19snnuElezlGHYHnivejuv/SEdiZPZ1j+ZL1k/607f0BQCknELHQ7e27GpvJROyZYNT2l6LwT2xZSYdjt9Z+0Klx+C0ChgUr5ijIBhHVoeFf+G1kYXQNAekcVXdmI+xgAzmrOPFG/kvOqx2a3GEJGNKUTHZVQ/SF24NbXX2rljf4psg/kvEIcY1fuBQaSJGIK1wl3xdhcOvbq8ByhzEVtW8Cnj1VK8HRmsXHIbavOyGWBGc/7iZF1P2mhCSCkrtmzd9cljYvYN0pk25/QdPqg9REhCkTXqFlCI+pmj7EK30AIlo3LSf6e5E43Cmvcjlcizqc0iMEFZMP461YmKY7pTVvS4FQkSkmU7WHbq8bc32B44ZCgiCgL0DXMQl9Qs02xwviIsfSgEszxSLRNlClCSH3NOrUHfqukv7d4x4s67R0KlIqvSA+TMPJDzxT4T3VJN3RaWd153FvoR8Hw4N83JTyDBry3RtVlJ/ugVx6eigBD0VZ9/TywOj7EXxl3DcKkHMKDfX19989ooYhWiNuQq+VygPwKJP0QDaYqB1EbOocQjaBCYQjSfYkgVgIbiPywdISASq5o39FEDCiXU8RYJYflufglsnWG9BZKuIxY3TRJSFdsZQYMLM50kab9t8c1VLF4Mj1bSm7jia3SXExnCYnmee4Cw2v9dW2LD94HVVynF8nx2LJYFpyE+oWTS9FuLMYIdRyesEZLShPJn+VQhBmFZIciYJ927cLot+7A0HMBEsLirC+wqqX85k+f6b4nBP+IzpekGKQ7+vhyv6/8AP8PaFQJkje3oPx5Hmh7qBOzz2NI/lZYneYog3LLlHeRuAkkj16CMxB/aKfJ26BKlHuxtDS7okROgOy+k+svOpcdz2gVCGSidf/SOpoprPvYmC+VJ1CiA449AY3y8rGAKRTIszfS3Xb23zlSeCAiyTwTKnsALt24EHkVKVY/RM+LWU6CXwnni 8FLgOE74 VnRxVNrcLu0wNUiLyehs7HpfWt3qjEPyKg4wa4K8kG6NH1kLpMmps2SXOKt5Ydm1rrfKLbu+yny2ODFOs5mMdegjQTVt+SLsWEh/me9poc8mwipknjIpmq7YRFBBnqYVCSJpiioBkxvil1T9x6ZCCwR0Va2yYf4yzCEjctL59z81jFgxQaGnTXtiiEFWijeU0Mwgtv3U8EyTHjXtAD2YE4t5uNvc/CGLZLQrw9m3DoXDmeq7ODCuj9wfkA6a57KcPS/bobIyt5e4U+E/W3bmueLYHn8+gtJkzmpz7feqIQ1+oFs2IKxVwyCmwUw== 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: Yu-cheng Yu A control-protection fault is triggered when a control-flow transfer attempt violates Shadow Stack or Indirect Branch Tracking constraints. For example, the return address for a RET instruction differs from the copy on the shadow stack. There already exists a control-protection fault handler for handling kernel IBT faults. Refactor this fault handler into separate user and kernel handlers, like the page fault handler. Add a control-protection handler for usermode. To avoid ifdeffery, put them both in a new file cet.c, which is compiled in the case of either of the two CET features supported in the kernel: kernel IBT or user mode shadow stack. Move some static inline functions from traps.c into a header so they can be used in cet.c. Opportunistically fix a comment in the kernel IBT part of the fault handler that is on the end of the line instead of preceding it. Keep the same behavior for the kernel side of the fault handler, except for converting a BUG to a WARN in the case of a #CP happening when the feature is missing. This unifies the behavior with the new shadow stack code, and also prevents the kernel from crashing under this situation which is potentially recoverable. The control-protection fault handler works in a similar way as the general protection fault handler. It provides the si_code SEGV_CPERR to the signal handler. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Cc: Michael Kerrisk Reviewed-by: Kees Cook --- v5: - Move to separate file to advoid ifdeffery (Boris) - Improvements to commit log (Boris) - Rename control_protection_err (Boris) - Move comment from end of line in IBT fault handler (Boris) v3: - Shorten user/kernel #CP handler function names (peterz) - Restore CP_ENDBR check to kernel handler (peterz) - Utilize CONFIG_X86_CET (Kees) - Unify "unexpected" warnings (Andrew Cooper) - Use 2d array for error code chars (Andrew Cooper) - Add comment about why to read SSP MSR before enabling interrupts v2: - Integrate with kernel IBT fault handler - Update printed messages. (Dave) - Remove array_index_nospec() usage. (Dave) - Remove IBT messages. (Dave) - Add enclave error code bit processing it case it can get triggered somehow. - Add extra "unknown" in control_protection_err. v1: - Update static asserts for NSIGSEGV arch/arm/kernel/signal.c | 2 +- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/signal32.c | 2 +- arch/sparc/kernel/signal32.c | 2 +- arch/sparc/kernel/signal_64.c | 2 +- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/idtentry.h | 2 +- arch/x86/include/asm/traps.h | 12 ++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/cet.c | 152 +++++++++++++++++++++++ arch/x86/kernel/idt.c | 2 +- arch/x86/kernel/signal_32.c | 2 +- arch/x86/kernel/signal_64.c | 2 +- arch/x86/kernel/traps.c | 87 ------------- arch/x86/xen/enlighten_pv.c | 2 +- arch/x86/xen/xen-asm.S | 2 +- include/uapi/asm-generic/siginfo.h | 3 +- 17 files changed, 186 insertions(+), 100 deletions(-) create mode 100644 arch/x86/kernel/cet.c diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c index e07f359254c3..9a3c9de5ac5e 100644 --- a/arch/arm/kernel/signal.c +++ b/arch/arm/kernel/signal.c @@ -681,7 +681,7 @@ asmlinkage void do_rseq_syscall(struct pt_regs *regs) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c index be279fd48248..4bced22213d5 100644 --- a/arch/arm64/kernel/signal.c +++ b/arch/arm64/kernel/signal.c @@ -1176,7 +1176,7 @@ void __init minsigstksz_setup(void) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/arm64/kernel/signal32.c b/arch/arm64/kernel/signal32.c index 4700f8522d27..bbd542704730 100644 --- a/arch/arm64/kernel/signal32.c +++ b/arch/arm64/kernel/signal32.c @@ -460,7 +460,7 @@ void compat_setup_restart_syscall(struct pt_regs *regs) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/sparc/kernel/signal32.c b/arch/sparc/kernel/signal32.c index dad38960d1a8..82da8a2d769d 100644 --- a/arch/sparc/kernel/signal32.c +++ b/arch/sparc/kernel/signal32.c @@ -751,7 +751,7 @@ asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/sparc/kernel/signal_64.c b/arch/sparc/kernel/signal_64.c index 570e43e6fda5..b4e410976e0d 100644 --- a/arch/sparc/kernel/signal_64.c +++ b/arch/sparc/kernel/signal_64.c @@ -562,7 +562,7 @@ void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 505f78ddca82..652e366b68a0 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -105,6 +105,12 @@ #define DISABLE_USER_SHSTK (1 << (X86_FEATURE_USER_SHSTK & 31)) #endif +#ifdef CONFIG_X86_KERNEL_IBT +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1 << (X86_FEATURE_IBT & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -128,7 +134,7 @@ #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ DISABLE_ENQCMD) #define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT) #define DISABLED_MASK19 0 #define DISABLED_MASK20 0 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21) diff --git a/arch/x86/include/asm/idtentry.h b/arch/x86/include/asm/idtentry.h index 72184b0b2219..69e26f48d027 100644 --- a/arch/x86/include/asm/idtentry.h +++ b/arch/x86/include/asm/idtentry.h @@ -618,7 +618,7 @@ DECLARE_IDTENTRY_RAW_ERRORCODE(X86_TRAP_DF, xenpv_exc_double_fault); #endif /* #CP */ -#ifdef CONFIG_X86_KERNEL_IBT +#ifdef CONFIG_X86_CET DECLARE_IDTENTRY_ERRORCODE(X86_TRAP_CP, exc_control_protection); #endif diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h index 47ecfff2c83d..75e0dabf0c45 100644 --- a/arch/x86/include/asm/traps.h +++ b/arch/x86/include/asm/traps.h @@ -47,4 +47,16 @@ void __noreturn handle_stack_overflow(struct pt_regs *regs, struct stack_info *info); #endif +static inline void cond_local_irq_enable(struct pt_regs *regs) +{ + if (regs->flags & X86_EFLAGS_IF) + local_irq_enable(); +} + +static inline void cond_local_irq_disable(struct pt_regs *regs) +{ + if (regs->flags & X86_EFLAGS_IF) + local_irq_disable(); +} + #endif /* _ASM_X86_TRAPS_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index dd61752f4c96..92446f1dedd7 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -144,6 +144,8 @@ obj-$(CONFIG_CFI_CLANG) += cfi.o obj-$(CONFIG_CALL_THUNKS) += callthunks.o +obj-$(CONFIG_X86_CET) += cet.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c new file mode 100644 index 000000000000..33d7d119be26 --- /dev/null +++ b/arch/x86/kernel/cet.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +enum cp_error_code { + CP_EC = (1 << 15) - 1, + + CP_RET = 1, + CP_IRET = 2, + CP_ENDBR = 3, + CP_RSTRORSSP = 4, + CP_SETSSBSY = 5, + + CP_ENCL = 1 << 15, +}; + +static const char cp_err[][10] = { + [0] = "unknown", + [1] = "near ret", + [2] = "far/iret", + [3] = "endbranch", + [4] = "rstorssp", + [5] = "setssbsy", +}; + +static const char *cp_err_string(unsigned long error_code) +{ + unsigned int cpec = error_code & CP_EC; + + if (cpec >= ARRAY_SIZE(cp_err)) + cpec = 0; + return cp_err[cpec]; +} + +static void do_unexpected_cp(struct pt_regs *regs, unsigned long error_code) +{ + WARN_ONCE(1, "Unexpected %s #CP, error_code: %s\n", + user_mode(regs) ? "user mode" : "kernel mode", + cp_err_string(error_code)); +} + +static DEFINE_RATELIMIT_STATE(cpf_rate, DEFAULT_RATELIMIT_INTERVAL, + DEFAULT_RATELIMIT_BURST); + +static void do_user_cp_fault(struct pt_regs *regs, unsigned long error_code) +{ + struct task_struct *tsk; + unsigned long ssp; + + /* + * An exception was just taken from userspace. Since interrupts are disabled + * here, no scheduling should have messed with the registers yet and they + * will be whatever is live in userspace. So read the SSP before enabling + * interrupts so locking the fpregs to do it later is not required. + */ + rdmsrl(MSR_IA32_PL3_SSP, ssp); + + cond_local_irq_enable(regs); + + tsk = current; + tsk->thread.error_code = error_code; + tsk->thread.trap_nr = X86_TRAP_CP; + + /* Ratelimit to prevent log spamming. */ + if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) && + __ratelimit(&cpf_rate)) { + pr_emerg("%s[%d] control protection ip:%lx sp:%lx ssp:%lx error:%lx(%s)%s", + tsk->comm, task_pid_nr(tsk), + regs->ip, regs->sp, ssp, error_code, + cp_err_string(error_code), + error_code & CP_ENCL ? " in enclave" : ""); + print_vma_addr(KERN_CONT " in ", regs->ip); + pr_cont("\n"); + } + + force_sig_fault(SIGSEGV, SEGV_CPERR, (void __user *)0); + cond_local_irq_disable(regs); +} + +static __ro_after_init bool ibt_fatal = true; + +/* code label defined in asm below */ +extern void ibt_selftest_ip(void); + +static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code) +{ + if ((error_code & CP_EC) != CP_ENDBR) { + do_unexpected_cp(regs, error_code); + return; + } + + if (unlikely(regs->ip == (unsigned long)&ibt_selftest_ip)) { + regs->ax = 0; + return; + } + + pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs)); + if (!ibt_fatal) { + printk(KERN_DEFAULT CUT_HERE); + __warn(__FILE__, __LINE__, (void *)regs->ip, TAINT_WARN, regs, NULL); + return; + } + BUG(); +} + +/* Must be noinline to ensure uniqueness of ibt_selftest_ip. */ +noinline bool ibt_selftest(void) +{ + unsigned long ret; + + asm (" lea ibt_selftest_ip(%%rip), %%rax\n\t" + ANNOTATE_RETPOLINE_SAFE + " jmp *%%rax\n\t" + "ibt_selftest_ip:\n\t" + UNWIND_HINT_FUNC + ANNOTATE_NOENDBR + " nop\n\t" + + : "=a" (ret) : : "memory"); + + return !ret; +} + +static int __init ibt_setup(char *str) +{ + if (!strcmp(str, "off")) + setup_clear_cpu_cap(X86_FEATURE_IBT); + + if (!strcmp(str, "warn")) + ibt_fatal = false; + + return 1; +} + +__setup("ibt=", ibt_setup); + +DEFINE_IDTENTRY_ERRORCODE(exc_control_protection) +{ + if (user_mode(regs)) { + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + do_user_cp_fault(regs, error_code); + else + do_unexpected_cp(regs, error_code); + } else { + if (cpu_feature_enabled(X86_FEATURE_IBT)) + do_kernel_cp_fault(regs, error_code); + else + do_unexpected_cp(regs, error_code); + } +} diff --git a/arch/x86/kernel/idt.c b/arch/x86/kernel/idt.c index a58c6bc1cd68..5074b8420359 100644 --- a/arch/x86/kernel/idt.c +++ b/arch/x86/kernel/idt.c @@ -107,7 +107,7 @@ static const __initconst struct idt_data def_idts[] = { ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE), #endif -#ifdef CONFIG_X86_KERNEL_IBT +#ifdef CONFIG_X86_CET INTG(X86_TRAP_CP, asm_exc_control_protection), #endif diff --git a/arch/x86/kernel/signal_32.c b/arch/x86/kernel/signal_32.c index 9027fc088f97..c12624bc82a3 100644 --- a/arch/x86/kernel/signal_32.c +++ b/arch/x86/kernel/signal_32.c @@ -402,7 +402,7 @@ int ia32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/x86/kernel/signal_64.c b/arch/x86/kernel/signal_64.c index 13a1e6083837..0e808c72bf7e 100644 --- a/arch/x86/kernel/signal_64.c +++ b/arch/x86/kernel/signal_64.c @@ -403,7 +403,7 @@ void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact) */ static_assert(NSIGILL == 11); static_assert(NSIGFPE == 15); -static_assert(NSIGSEGV == 9); +static_assert(NSIGSEGV == 10); static_assert(NSIGBUS == 5); static_assert(NSIGTRAP == 6); static_assert(NSIGCHLD == 6); diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index d317dc3d06a3..18fb9d620824 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -77,18 +77,6 @@ DECLARE_BITMAP(system_vectors, NR_VECTORS); -static inline void cond_local_irq_enable(struct pt_regs *regs) -{ - if (regs->flags & X86_EFLAGS_IF) - local_irq_enable(); -} - -static inline void cond_local_irq_disable(struct pt_regs *regs) -{ - if (regs->flags & X86_EFLAGS_IF) - local_irq_disable(); -} - __always_inline int is_valid_bugaddr(unsigned long addr) { if (addr < TASK_SIZE_MAX) @@ -213,81 +201,6 @@ DEFINE_IDTENTRY(exc_overflow) do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL); } -#ifdef CONFIG_X86_KERNEL_IBT - -static __ro_after_init bool ibt_fatal = true; - -extern void ibt_selftest_ip(void); /* code label defined in asm below */ - -enum cp_error_code { - CP_EC = (1 << 15) - 1, - - CP_RET = 1, - CP_IRET = 2, - CP_ENDBR = 3, - CP_RSTRORSSP = 4, - CP_SETSSBSY = 5, - - CP_ENCL = 1 << 15, -}; - -DEFINE_IDTENTRY_ERRORCODE(exc_control_protection) -{ - if (!cpu_feature_enabled(X86_FEATURE_IBT)) { - pr_err("Unexpected #CP\n"); - BUG(); - } - - if (WARN_ON_ONCE(user_mode(regs) || (error_code & CP_EC) != CP_ENDBR)) - return; - - if (unlikely(regs->ip == (unsigned long)&ibt_selftest_ip)) { - regs->ax = 0; - return; - } - - pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs)); - if (!ibt_fatal) { - printk(KERN_DEFAULT CUT_HERE); - __warn(__FILE__, __LINE__, (void *)regs->ip, TAINT_WARN, regs, NULL); - return; - } - BUG(); -} - -/* Must be noinline to ensure uniqueness of ibt_selftest_ip. */ -noinline bool ibt_selftest(void) -{ - unsigned long ret; - - asm (" lea ibt_selftest_ip(%%rip), %%rax\n\t" - ANNOTATE_RETPOLINE_SAFE - " jmp *%%rax\n\t" - "ibt_selftest_ip:\n\t" - UNWIND_HINT_FUNC - ANNOTATE_NOENDBR - " nop\n\t" - - : "=a" (ret) : : "memory"); - - return !ret; -} - -static int __init ibt_setup(char *str) -{ - if (!strcmp(str, "off")) - setup_clear_cpu_cap(X86_FEATURE_IBT); - - if (!strcmp(str, "warn")) - ibt_fatal = false; - - return 1; -} - -__setup("ibt=", ibt_setup); - -#endif /* CONFIG_X86_KERNEL_IBT */ - #ifdef CONFIG_X86_F00F_BUG void handle_invalid_op(struct pt_regs *regs) #else diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index bb59cc6ddb2d..9c29cd5393cc 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -640,7 +640,7 @@ static struct trap_array_entry trap_array[] = { TRAP_ENTRY(exc_coprocessor_error, false ), TRAP_ENTRY(exc_alignment_check, false ), TRAP_ENTRY(exc_simd_coprocessor_error, false ), -#ifdef CONFIG_X86_KERNEL_IBT +#ifdef CONFIG_X86_CET TRAP_ENTRY(exc_control_protection, false ), #endif }; diff --git a/arch/x86/xen/xen-asm.S b/arch/x86/xen/xen-asm.S index 4a184f6e4e4d..7cdcb4ce6976 100644 --- a/arch/x86/xen/xen-asm.S +++ b/arch/x86/xen/xen-asm.S @@ -148,7 +148,7 @@ xen_pv_trap asm_exc_page_fault xen_pv_trap asm_exc_spurious_interrupt_bug xen_pv_trap asm_exc_coprocessor_error xen_pv_trap asm_exc_alignment_check -#ifdef CONFIG_X86_KERNEL_IBT +#ifdef CONFIG_X86_CET xen_pv_trap asm_exc_control_protection #endif #ifdef CONFIG_X86_MCE diff --git a/include/uapi/asm-generic/siginfo.h b/include/uapi/asm-generic/siginfo.h index ffbe4cec9f32..0f52d0ac47c5 100644 --- a/include/uapi/asm-generic/siginfo.h +++ b/include/uapi/asm-generic/siginfo.h @@ -242,7 +242,8 @@ typedef struct siginfo { #define SEGV_ADIPERR 7 /* Precise MCD exception */ #define SEGV_MTEAERR 8 /* Asynchronous ARM MTE error */ #define SEGV_MTESERR 9 /* Synchronous ARM MTE exception */ -#define NSIGSEGV 9 +#define SEGV_CPERR 10 /* Control protection fault */ +#define NSIGSEGV 10 /* * SIGBUS si_codes From patchwork Thu Jan 19 21:22:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108786 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 15118C46467 for ; Thu, 19 Jan 2023 21:23:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCBFC6B0081; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C08D56B0088; Thu, 19 Jan 2023 16:23:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F7E66B0089; Thu, 19 Jan 2023 16:23:41 -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 76D276B0081 for ; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4F31F40679 for ; Thu, 19 Jan 2023 21:23:41 +0000 (UTC) X-FDA: 80372825442.28.0A89FC4 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf30.hostedemail.com (Postfix) with ESMTP id 756AD80011 for ; Thu, 19 Jan 2023 21:23:39 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PuSAsgH0; spf=pass (imf30.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=1674163419; 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=BVe42IZ3bUVWxOXb5SeOdBeUgISVBwg/EiJcXbj5PIQ=; b=ol17rK7oEdBDs9Y0J+b6rholmKRdWOyCvI4DwV+Z03Mg9YE+8+ej/oVJfDntcj7Y3zhCan 7sumQBBmHV5ZD378DsugRwn26i7Oh/piGDnXPlPpuaX+mL39R+BMm/f7aF4FwWHjWerGGs Auq1FKT3KxsSpk8+6rKCH7cwmN9ZNzQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PuSAsgH0; spf=pass (imf30.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=1674163419; a=rsa-sha256; cv=none; b=YAWzdVRAWF2dxb7cc8U2YMZLqf8pLKebMlyIqnoDHy9rgry7gxfuBllZL/LkC3tanzpJax KFsoG/C1LyRlUJNnm7dhZxwEcH9cmRu/VwsrkTF0bTtpArhZf8JzGg/yky4Rh/Y+JLCu/5 UhqXbIrBy2Y1Yk45VsQN78+1pR7ks0U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163419; x=1705699419; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=3Wysy2UxXgn7XrxNNcTO6OHZIBa9HpH74QVE6RG86iA=; b=PuSAsgH0VqC1CMnyfFWiC4MIRN2J2yRDl/F1KMsx1zgh08mXUFiYbisQ qqdxPDl6EuQJiVs98L1yGG0qgviB2AeGNl1GDK3YOzWjK1Tqio8rFY5G1 lXeuaTcPoNK+dKrHxfXznOOObYZvIN9AiQrKNOQrZ8VUnuZ09sv9m6dd1 gQ+UIILUOq5K4sj47wWZT9rr6DgQFV4kx7pVgFPjKpM12txE9AdRL0Ocn wq9MaWCr49KbvsEaqaZgFv4XNmhTLxcwbNUHRHamSwm2Vevbon0tMalPs 4OByCwrbm2RBXOcipBMDbqkDx18Mxu/eGtNFbRlDJbjE9MVPrB/1Akb0E g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119350" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119350" 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:23:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139021" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139021" 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:23:36 -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, Yu-cheng Yu , Christoph Hellwig Subject: [PATCH v5 08/39] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Date: Thu, 19 Jan 2023 13:22:46 -0800 Message-Id: <20230119212317.8324-9-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-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 5ue4jidotwikwugabeiboz9ou5bi7mes X-Rspamd-Queue-Id: 756AD80011 X-HE-Tag: 1674163419-451789 X-HE-Meta: U2FsdGVkX1+7E1nPy1CTZUkpv6FBEUszBI5W2TrZrPPsnUYhlxX2rEjjv3My7wOw6ZDljmuVyl6cCEAW0MhH+SHtP+Nd5xmKcaJByH3B88DTt2v5xzBRnJ/z/NdAotg//5+JI6eqP/9L52mibdmyTvVLZcjGXuR0kw4/gpEoO4nx2qqeQTNKtp3rKYsBy7cJ4y7Q8FBMxXNp36HGojdt8zDTyKV7C/Y/WLz2xeebDj1BkewKEloSL13C0CMif2LxsvCKtV86pzjpCtaKDzdOOAql4oDv7THnOn+Hoc9fKuvkUaF9c36Hta9PRWUPKi52HlZkah+MIgaVYe8vajatDz+pTbUfqdOMvlsipEJXUZJ0QR6Fxdc6MA74EIGwg512dFExaQMStuhvKerLeourk7llfD2SSjy0gmZ4Ugg86SJWp1spS3ebz6J7M6lUpJPdb33ldcrsRamrkVhIFhumP/D6jgUFrWDb+n4zKtn+RZcWJrGWmDSdWR75qdLuGTr8/Ir3aAznN+abF0h2967nxQNhAjEFgZIWPJnjdtAhyiGNd2rNTthudSVjXAiJ1DvDcNUKJzR7DKCrPem4zqrJT2tPgsR0FItjJpEkNWZBKTp/Oi0p0BV00VEd0UsossGBPfe9GbIiYTUhX1WuNLSXnc04uckQE47tlkae4tKr3SCrqrVx9VhtAndjBy/epZyMwLy8k8hu4VAOkBWbgGBDqoeuhIpd5NFYdNyA3PrLtTT8/SycmsIPpxEcX4/iXm8eOQ374NjAEGIq/0ZwnXo/JBAbiwbWVqpWm5R/EXxnPpM10nt+xNFMlLb2KC4M1d2PaSTnkGdDnmuKnW2rUkhEaMBDiv8mZL2+Ac+9kpQk4NCC2frAWyrhY8XSa3n6MZwOUHyDND8aV1q8UQOMXGoI5ElV1V6OEbCoHw4HKodirIrSTHkJLnBtwg6y9TQeZJSNyKiRQoo0ViJpoN30WdJ /Ej//8+T ar6MjIvm2ExbTB9qJ8QxN+gAZg7uWELKXUbGmvqurw8CmpKP+wGUwj1C9PSS7OjNUKjh0BYBwKOKS6JtnnCvUWDg98A2c8Kcq+iJFYdQT8dVRHxdlkH3PlAqPwaCj8CMavBAN6+QVWFt3Nu8NbLzr6xYi5+No9oOGpjQcxIm4akvOCFzuzz3oReOCydYs1CvX/lmKzaUo4m/X34vL0H/EcqS78PNlx2ugVUwtmCM43CGtMq34sMowKo98mH7hOkk3ZhOH/d37UBoR3yUDkPSTdHrbDeFhKltQSJFCD9oa0f8T7IOqJ+haTun1zPiJJ+eRuSVwYe+FTXnQPiANtl8G1Qhrxala1Ps+lzVfeaaSA3sm0UVdDWdKOLEwkg5j2gEk47mhstQkB87Qnd5Nhf1HHvCRRIOTCV5tZ5L8vJKBIYmlpmOrbkPE9T9lBHx3bKfWzOmOTETWiwlU7VmcO69HAV8oBAOBVbteXOc9LwiTUFbhsCbIN9blteu9S5bT/XsdkhHnNGTs5zGY7cP7BsksScLyv0LB8Wl/M6qpjaIL29pvy5NATmFHDCADXQ== 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: Yu-cheng Yu New processors that support Shadow Stack regard Write=0,Dirty=1 PTEs as shadow stack pages. In normal cases, it can be helpful to create Write=1 PTEs as also Dirty=1 if HW dirty tracking is not needed, because if the Dirty bit is not already set the CPU has to set Dirty=1 when the memory gets written to. This creates additional work for the CPU. So traditional wisdom was to simply set the Dirty bit whenever you didn't care about it. However, it was never really very helpful for read-only kernel memory. When CR4.CET=1 and IA32_S_CET.SH_STK_EN=1, some instructions can write to such supervisor memory. The kernel does not set IA32_S_CET.SH_STK_EN, so avoiding kernel Write=0,Dirty=1 memory is not strictly needed for any functional reason. But having Write=0,Dirty=1 kernel memory doesn't have any functional benefit either, so to reduce ambiguity between shadow stack and regular Write=0 pages, remove Dirty=1 from any kernel Write=0 PTEs. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: "H. Peter Anvin" Cc: Kees Cook Cc: Thomas Gleixner Cc: Dave Hansen Cc: Christoph Hellwig Cc: Andy Lutomirski Cc: Ingo Molnar Cc: Borislav Petkov Cc: Peter Zijlstra Reviewed-by: Kees Cook --- v5: - Spelling and grammar in commit log (Boris) v3: - Update commit log (Andrew Cooper, Peterz) v2: - Normalize PTE bit descriptions between patches arch/x86/include/asm/pgtable_types.h | 6 +++--- arch/x86/mm/pat/set_memory.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 447d4bee25c4..0646ad00178b 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -192,10 +192,10 @@ enum page_cache_mode { #define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC) #define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0) #define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC) -#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX|___D| 0|___G) -#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0|___D| 0|___G) +#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX| 0| 0|___G) +#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0| 0| 0|___G) #define __PAGE_KERNEL_NOCACHE (__PP|__RW| 0|___A|__NX|___D| 0|___G| __NC) -#define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX|___D| 0|___G) +#define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX| 0| 0|___G) #define __PAGE_KERNEL_LARGE (__PP|__RW| 0|___A|__NX|___D|_PSE|___G) #define __PAGE_KERNEL_LARGE_EXEC (__PP|__RW| 0|___A| 0|___D|_PSE|___G) #define __PAGE_KERNEL_WP (__PP|__RW| 0|___A|__NX|___D| 0|___G| __WP) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 356758b7d4b4..d41706ad29db 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2073,7 +2073,7 @@ int set_memory_nx(unsigned long addr, int numpages) int set_memory_ro(unsigned long addr, int numpages) { - return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0); + return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0); } int set_memory_rox(unsigned long addr, int numpages) From patchwork Thu Jan 19 21:22:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108787 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 D3265C6379F for ; Thu, 19 Jan 2023 21:23:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 664476B0088; Thu, 19 Jan 2023 16:23:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EDE76B0089; Thu, 19 Jan 2023 16:23:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A2496B008C; Thu, 19 Jan 2023 16:23:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1E74F6B0088 for ; Thu, 19 Jan 2023 16:23:43 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D89FD14030C for ; Thu, 19 Jan 2023 21:23:42 +0000 (UTC) X-FDA: 80372825484.21.0D7A960 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id DC42A4000C for ; Thu, 19 Jan 2023 21:23:40 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aU5e2BFy; spf=pass (imf27.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=1674163421; 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=SLtGihyn/x9FzqL6Gl9syvqGmcYAMPQwgzBEC4XQqQQ=; b=dxZJ/DiSo8ER7LpZT9c+5xhxfM1979HDJrhoVNIWZ53qx5yfMa2/evqWT9WCTG04LxtG58 BikilH/7O9TOyOp616bwIn2EraY7UUp/nUjolq/hvlf6jC2Ri99s6WAv+ntUecY8PysSCg muWkVsBkCZh5H70ryrd2VIvw0QtDhg0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aU5e2BFy; spf=pass (imf27.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=1674163421; a=rsa-sha256; cv=none; b=oReSlylAtEZpPS9oQKW1rrvXZGV5bBF1kv3J8GxWKoK6WlnMHS8wUddCCgwfZ/Crtt1LTi ATIe3Xkld01yIodqVPjnNTkpkLiMVqUmfaOCJq/FZpY8p+LqRn/YQ72oyDEHqLE349Wtb5 wMk70tbe+FQW4+nRDv9I2GalpxPGX7M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163420; x=1705699420; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=tmt4cSd6A0kDS/7f4GwHJfynyFRJ89yUY8hbbxiN2tQ=; b=aU5e2BFy++df7SsTXbVEvf5MTtW4IUqnNQbTRmYH3+MJC4whmbsf5XRY f/vEHApwx890V/cqieS0sIsmffL48evLk/+Ngfs2hZgZlbMdyg2VnhRgQ HC94PLpXXpVrgdLp3G0I3021ilEZ/HzuPltlrxyHLXCv/pAJd/bPX/57l ofx34EEUkrH+mTT9N0qtP3xBJEn+sGqg5jtLcB5WZ6biyl+FEKgpmDYoS 7y9atj9RR1WRYe5KhR3a9qTlVGPQBf+kAQ0a/w8z2eJ0404xiaZNvdp4Z d/LZKmDvKyjLbhy6ss3PVrj8yEEyIP1ty4hInLMKW6Ijvh+yK0B6qHSwV A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119367" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119367" 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:23:39 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139027" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139027" 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:23:37 -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, Yu-cheng Yu Subject: [PATCH v5 09/39] x86/mm: Move pmd_write(), pud_write() up in the file Date: Thu, 19 Jan 2023 13:22:47 -0800 Message-Id: <20230119212317.8324-10-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: DC42A4000C X-Stat-Signature: c6nnx6dh5fh74e3sok4toc5r6e4qgq7c X-Rspam-User: X-HE-Tag: 1674163420-6456 X-HE-Meta: U2FsdGVkX18poT+oKD0JHkOTNh7xUc7PcXE/raR8rGmIbN9eWCyMHlP5PXF4mnEyxP2b4sPQhxbrnKcB/Mx+7i74WfqEWlvQohwUBA9jhJMLc/u78qYFkNiHtoiKjxawdEx4z2I+4H2Y6ZObu8D6Oo35/l7AJcOCZEXGqWLFn9VeR+WB+J4XCuLZ7i+ZoTvqbiLPw6XQFMk1VP5RQ7MZV6ZP0NNWUSq3w6oAkudcv5fx6wfzMk5zL9NcIodcHDvkvkcqyw1ca8c3muzoMKIbp7WLK0cfmNMi/oUZBVtr9rCm9crOKnPm51c4ONB42qjYquOEStkI44JinjDJJoc+OTy/Aor3ScPVzCQLLZdrn8AVU+3KjY3dTqwfDmBGpkEDAyhHOi5oWh8a5EdvXpZXJlrv/8Z/Jh40KtFYYkwKBnPU1BqAJeTWzj/UKJyxgikkv8H28DiuvpmcaX8gJiOMjuc4hwLeuXyb1vCresGQmPn7cBrITZondmvEBgn8+ZT0jsUzfXyYKFZNQDMtbngwKMSKu23Bt+fg3AQ6ZXHQwoHvY31ADXaE71wjguZvgQkQ6p7/a4gyGZrCOT8jjrdRfUnMoxnEsr57IgwW/MCffNc7g8gN/BbATSHy82bpotm6DjNYEgwIYI6bS0EHHoBYPiDGhotvnnaRNJw++QtwtAyWjsf13sJzAgpHskwDeJc/oq0h3DNtpZa3NPRSvOhq/mQyPslaVjZ6csx9lp+iz57LmCBVYzIGximlBqOAt2OijJCR/QWLbDMtbT9lXyYwqwQybnr3qvuY5VnuE9Fcsd/mRO3bXcKw1xUXNdTgx9IM9v6+cWAoXAFJfpmD1IccnCveSsn3OT+UzgL4xtHQ63nBewEmnBd2jSPPvlTAiuya2BPVXNKAM739UMtxsikeO39XOPwo2W0hRpj7wf3tVZhTlpeJhZB3XJcDT0oqGG+1mPbyRqPmLwGeHmFQWYb rrte6Dm3 TgsoabDHK0mybjN1mMQOe9SbY/YKN1zC2FaywBRx6fpFUIjdymh5/gjtGH2lUPvc0JI8HNU936x/9pQbkWPM8QVTCu73cb59ZkRuoPaCxxOE/AtKi1NGy8xW4HZL5imQt9B0O+Pb95pKyqFCYb6ox3yTmODA/VJBVR2bI118l7N4slTjExfwPMdFTUMDDb3k0kSCauczoMUKYRxQ3nquClJGYqfn8YoZJUj3aIaJtHFRtrgvxK1HhU1rzvvoMHpvXf9H0Dd1Khnmquy43ZAYjxq1uiDSniobmnjPz/jzhV8rjcP0xXTtZMkzv9h5LYuxf0Ng1MLCBCmN5X90= 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: Yu-cheng Yu To prepare the introduction of _PAGE_COW, move pmd_write() and pud_write() up in the file, so that they can be used by other helpers below. No functional changes. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- arch/x86/include/asm/pgtable.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 0564edd24ffb..b39f16c0d507 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -160,6 +160,18 @@ static inline int pte_write(pte_t pte) return pte_flags(pte) & _PAGE_RW; } +#define pmd_write pmd_write +static inline int pmd_write(pmd_t pmd) +{ + return pmd_flags(pmd) & _PAGE_RW; +} + +#define pud_write pud_write +static inline int pud_write(pud_t pud) +{ + return pud_flags(pud) & _PAGE_RW; +} + static inline int pte_huge(pte_t pte) { return pte_flags(pte) & _PAGE_PSE; @@ -1120,12 +1132,6 @@ extern int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp); -#define pmd_write pmd_write -static inline int pmd_write(pmd_t pmd) -{ - return pmd_flags(pmd) & _PAGE_RW; -} - #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp) @@ -1155,12 +1161,6 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); } -#define pud_write pud_write -static inline int pud_write(pud_t pud) -{ - return pud_flags(pud) & _PAGE_RW; -} - #ifndef pmdp_establish #define pmdp_establish pmdp_establish static inline pmd_t pmdp_establish(struct vm_area_struct *vma, From patchwork Thu Jan 19 21:22:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108788 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 6B006C46467 for ; Thu, 19 Jan 2023 21:23:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9208F6B0089; Thu, 19 Jan 2023 16:23:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AB3A6B0092; Thu, 19 Jan 2023 16:23:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 661128E0001; Thu, 19 Jan 2023 16:23:45 -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 46B356B0089 for ; Thu, 19 Jan 2023 16:23:45 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1D4F31C6599 for ; Thu, 19 Jan 2023 21:23:45 +0000 (UTC) X-FDA: 80372825610.01.0AB33E8 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 13D6C180015 for ; Thu, 19 Jan 2023 21:23:42 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=a5KeCgk8; 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=1674163423; 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=gg1qCyi+VRVbGSEkEF0teOFXegG4SQhXxychn9nztMQ=; b=6ooTj1v2aGHvFlt1G5xHDfJUvwoTj5wauMzBeMmncnTXdASCR/f4o7aIJxKAmuK2v8Bs/S 3yYRAtp3KBwZyM6ZltwhuYtN92aVBIcPvk5/LNAMl3fxjY2KmGXlSklxWmhCuav+Z5E9l4 VZ/mrx4MM6bsF+RfXJNlVELiZOxok4Y= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=a5KeCgk8; 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=1674163423; a=rsa-sha256; cv=none; b=59w8PTxCzemovuvSBMCHiwAs/vD04QpeDtTO2K3VQgSqJmT00uaxqNwXx2aUWLMszYFWeJ YL1horcZRBVBfXLXNGIXLsiRn8CocyYwtjadXcsE9ZbegMH2Y4WGpklmasBlZ5rDvdrnAq fNAEsMYZE+uNAlTiLTll9rkRERQBOGk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163423; x=1705699423; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=FFlwLGFrc3HasCEVoFiq4z4jSgXcdwaUjJhlr8Fvg+4=; b=a5KeCgk88/cPaxrNiWpSwQDseKDTtP0A1jZgMRxCulmXblcedB2RuuQC ILu5UuAnQ9HNAfVVJB9AWMf6t+rcTfKcmKNk/fEUHegCQAx58wST/CBf6 h2qC4TAIoHtH4+JMQllwdWYpcHaBW6gMBOeW0lwF1yxndhysIvkIhXROE 5hsf5EGKqsOF4a4Tt/jI26lMLoiCQkRhI5OWSgr00FrrLnDsBi2GnyAi2 9gCHQC4/VOzXfLf/vehuNSNSdnVkm54upzZhxiAn6w1oz++gJh+jglqmj zki+6VP+zqc8zc4Nc8rlaOMWtdTCqPWtM3UGKFwr3Q4qKYyIyZJx3UoSb w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119393" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119393" 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:23:41 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139032" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139032" 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:23:39 -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, Yu-cheng Yu Subject: [PATCH v5 10/39] x86/mm: Introduce _PAGE_COW Date: Thu, 19 Jan 2023 13:22:48 -0800 Message-Id: <20230119212317.8324-11-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: 13D6C180015 X-Stat-Signature: jw45xbn3kswmjaszt1zxpukzyysw9wng X-Rspam-User: X-HE-Tag: 1674163422-985029 X-HE-Meta: U2FsdGVkX19YwrAKOD3fLeFxL4dSUH5sHSTWtxMiR1ZLWRI/FPJI/LsJK7mWs/1StOVgwCqyOCNpql+O3q/x3Lu0cu+fFffw4A/bcj6fAEJXymnkpozEhse0T5245EMDiYv6mxIB/uZYXrClzPed1JTn0txvu9pBIeF95b4DwYjHXDwmVtxAbrDFYWQm/lvRfj3DGIqnBH5onooaRzCaPR+/L52YPoEqBtRZBsOF7hLwic+ClI/JtWzx0mHeNFN7DvoX2ValiXXG7zK/btBrO2TEvqs89VXsjwRIkVRDIMTPl/cMlaaFOAC/gJjS4IXnZRV5B/yQTOkpHvK0Q09waYF8DPl3cevAHnDHASfNbAX1YeZWS7gIkPKRkYyvY8uomdOdiufx9fOmG1gsGd7MkZ9KaxwtHdVcn/UMrvsWCVtkre36wOQaIruoQzE99ObSM4yaqNo9o+x+lhCs10GNUeND7JA3DsGwInW/emZHe/e3kYAHvFf6JxdgUjY4TeUbPt4/IhcIaHuFuqFQuklApSpHLmssW5ZIH1PZBJE+EimPVvn2gkPzQhgDoH+Ji2s8wJFrnf2nT8ODCc+xoF2cYao+t9kZdnTOfj8Vyg2O4DsF97igIjkZpTN9W3rCwr/iJs6C9QcG72bAZ7cwzrc9/shO9Tl1ZM121Va38bY3SZE9nITg99xVYf054jqErC8d6jNOSEUbryiQ0Oef/RWy8UgzbwT/DoKFJyRy9flNygSDjdfSOMcY//w+eVaBG+GcAxhcUWbWa2OO8uWRwCvM+SZPqgk/WtftdPendmrqoOIMtpglsxRVeJhj2HNseaACNkGxV9HQvQ5DGbB3F9qUIhk08xEMsaVVE6yFnyanOytrOPaNOifJLHJMA26aeqUe6RzVzks04m43ziVIaOLbvkPQuFzIMNRtrDyhRhHURbfGOpD9X7XKkmGkNvvOrc6gDWFrt2la+jSVGS8OCO4 LDvkAhKG rJi7a4I6fDVfxpZ7VPsvHSdtL05EB8PQU+UMuVmmOLnlAsvBlhZlqcGBv+7oj5IKdO8FFiorx7iwgXMs7jSFXZW0EifWTb5JQnHEImLGpUJKVd4DUz4amDc1NaIPL3sySb/CpI65nLz8uehb7djrYjv3A6aSMLvc269wKdO7OjxGMlK7hK3POSClv/4jkTEbwVPAmK06e/Im7rU0qhaL46g3onyhDLIQv1LusUDIyYD6ecYANkcaPPkmmXyzaWLcJ1V+4VwITKEB11Zyz6qlMcaFWdg== 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: Some OSes have a greater dependence on software available bits in PTEs than Linux. That left the hardware architects looking for a way to represent a new memory type (shadow stack) within the existing bits. They chose to repurpose a lightly-used state: Write=0,Dirty=1. So in order to support shadow stack memory, Linux should avoid creating memory with this PTE bit combination unless it intends for it to be shadow stack. The reason it's lightly used is that Dirty=1 is normally set by HW _before_ a write. A write with a Write=0 PTE would typically only generate a fault, not set Dirty=1. Hardware can (rarely) both set Dirty=1 *and* generate the fault, resulting in a Write=0,Dirty=1 PTE. Hardware which supports shadow stacks will no longer exhibit this oddity. So that leaves Write=0,Dirty=1 PTEs created in software. To achieve this, in places where Linux normally creates Write=0,Dirty=1, it can use the software-defined _PAGE_COW in place of the hardware _PAGE_DIRTY. In other words, whenever Linux needs to create Write=0,Dirty=1, it instead creates Write=0,Cow=1 except for shadow stack, which is Write=0,Dirty=1. Further differentiated by VMA flags, these PTE bit combinations would be set as follows for various types of memory: (Write=0,Cow=1,Dirty=0): - A modified, copy-on-write (COW) page. Previously when a typical anonymous writable mapping was made COW via fork(), the kernel would mark it Write=0,Dirty=1. Now it will instead use the Cow bit. This happens in copy_present_pte(). - A R/O page that has been COW'ed. The user page is in a R/O VMA, and get_user_pages(FOLL_FORCE) needs a writable copy. The page fault handler creates a copy of the page and sets the new copy's PTE as Write=0 and Cow=1. - A shared shadow stack PTE. When a shadow stack page is being shared among processes (this happens at fork()), its PTE is made Dirty=0, so the next shadow stack access causes a fault, and the page is duplicated and Dirty=1 is set again. This is the COW equivalent for shadow stack pages, even though it's copy-on-access rather than copy-on-write. (Write=0,Cow=0,Dirty=1): - A shadow stack PTE. - A Cow PTE created when a processor without shadow stack support set Dirty=1. There are six bits left available to software in the 64-bit PTE after consuming a bit for _PAGE_COW. No space is consumed in 32-bit kernels because shadow stacks are not enabled there. Implement only the infrastructure for _PAGE_COW. Changes to start creating _PAGE_COW PTEs will follow once other pieces are in place. Tested-by: Pengfei Xu Tested-by: John Allen Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix log, comments and whitespace (Boris) - Remove capitalization on shadow stack (Boris) v4: - Teach pte_flags_need_flush() about _PAGE_COW bit - Break apart patch for better bisectability v3: - Add comment around _PAGE_TABLE in response to comment from (Andrew Cooper) - Check for PSE in pmd_shstk (Andrew Cooper) - Get to the point quicker in commit log (Andrew Cooper) - Clarify and reorder commit log for why the PTE bit examples have multiple entries. Apply same changes for comment. (peterz) - Fix comment that implied dirty bit for COW was a specific x86 thing (peterz) - Fix swapping of Write/Dirty (PeterZ) v2: - Update commit log with comments (Dave Hansen) - Add comments in code to explain pte modification code better (Dave) - Clarify info on the meaning of various Write,Cow,Dirty combinations arch/x86/include/asm/pgtable.h | 78 ++++++++++++++++++++++++++++ arch/x86/include/asm/pgtable_types.h | 59 +++++++++++++++++++-- arch/x86/include/asm/tlbflush.h | 3 +- 3 files changed, 134 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index b39f16c0d507..6d2f612c04b5 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -301,6 +301,44 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear) return native_make_pte(v & ~clear); } +/* + * Normally COW memory can result in Dirty=1,Write=0 PTEs. But in the case + * of X86_FEATURE_USER_SHSTK, the software COW bit is used, since the + * Dirty=1,Write=0 will result in the memory being treated as shadow stack + * by the HW. So when creating COW memory, a software bit is used + * _PAGE_BIT_COW. The following functions pte_mkcow() and pte_clear_cow() + * take a PTE marked conventionally COW (Dirty=1) and transition it to the + * shadow stack compatible version of COW (Cow=1). + */ +static inline pte_t pte_mkcow(pte_t pte) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pte; + + pte = pte_clear_flags(pte, _PAGE_DIRTY); + return pte_set_flags(pte, _PAGE_COW); +} + +static inline pte_t pte_clear_cow(pte_t pte) +{ + /* + * _PAGE_COW is unnecessary on !X86_FEATURE_USER_SHSTK kernels, since + * the HW dirty bit can be used without creating shadow stack memory. + * See the _PAGE_COW definition for more details. + */ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pte; + + /* + * PTE is getting copied-on-write, so it will be dirtied + * if writable, or made shadow stack if shadow stack and + * being copied on access. Set the dirty bit for both + * cases. + */ + pte = pte_set_flags(pte, _PAGE_DIRTY); + return pte_clear_flags(pte, _PAGE_COW); +} + #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP static inline int pte_uffd_wp(pte_t pte) { @@ -413,6 +451,26 @@ static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear) return native_make_pmd(v & ~clear); } +/* See comments above pte_mkcow() */ +static inline pmd_t pmd_mkcow(pmd_t pmd) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pmd; + + pmd = pmd_clear_flags(pmd, _PAGE_DIRTY); + return pmd_set_flags(pmd, _PAGE_COW); +} + +/* See comments above pte_mkcow() */ +static inline pmd_t pmd_clear_cow(pmd_t pmd) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pmd; + + pmd = pmd_set_flags(pmd, _PAGE_DIRTY); + return pmd_clear_flags(pmd, _PAGE_COW); +} + #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP static inline int pmd_uffd_wp(pmd_t pmd) { @@ -484,6 +542,26 @@ static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear) return native_make_pud(v & ~clear); } +/* See comments above pte_mkcow() */ +static inline pud_t pud_mkcow(pud_t pud) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pud; + + pud = pud_clear_flags(pud, _PAGE_DIRTY); + return pud_set_flags(pud, _PAGE_COW); +} + +/* See comments above pte_mkcow() */ +static inline pud_t pud_clear_cow(pud_t pud) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return pud; + + pud = pud_set_flags(pud, _PAGE_DIRTY); + return pud_clear_flags(pud, _PAGE_COW); +} + static inline pud_t pud_mkold(pud_t pud) { return pud_clear_flags(pud, _PAGE_ACCESSED); diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 0646ad00178b..5c3f942865d9 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -21,7 +21,8 @@ #define _PAGE_BIT_SOFTW2 10 /* " */ #define _PAGE_BIT_SOFTW3 11 /* " */ #define _PAGE_BIT_PAT_LARGE 12 /* On 2MB or 1GB pages */ -#define _PAGE_BIT_SOFTW4 58 /* available for programmer */ +#define _PAGE_BIT_SOFTW4 57 /* available for programmer */ +#define _PAGE_BIT_SOFTW5 58 /* available for programmer */ #define _PAGE_BIT_PKEY_BIT0 59 /* Protection Keys, bit 1/4 */ #define _PAGE_BIT_PKEY_BIT1 60 /* Protection Keys, bit 2/4 */ #define _PAGE_BIT_PKEY_BIT2 61 /* Protection Keys, bit 3/4 */ @@ -34,6 +35,15 @@ #define _PAGE_BIT_SOFT_DIRTY _PAGE_BIT_SOFTW3 /* software dirty tracking */ #define _PAGE_BIT_DEVMAP _PAGE_BIT_SOFTW4 +/* + * Indicates a copy-on-write page. + */ +#ifdef CONFIG_X86_USER_SHADOW_STACK +#define _PAGE_BIT_COW _PAGE_BIT_SOFTW5 /* copy-on-write */ +#else +#define _PAGE_BIT_COW 0 +#endif + /* If _PAGE_BIT_PRESENT is clear, we use these: */ /* - if the user mapped it with PROT_NONE; pte_present gives true */ #define _PAGE_BIT_PROTNONE _PAGE_BIT_GLOBAL @@ -117,6 +127,40 @@ #define _PAGE_SOFTW4 (_AT(pteval_t, 0)) #endif +/* + * The hardware requires shadow stack to be read-only and Dirty. + * _PAGE_COW is a software-only bit used to separate copy-on-write PTEs + * from shadow stack PTEs: + * + * (Write=0,Cow=1,Dirty=0): + * - A modified, copy-on-write (COW) page. Previously when a typical + * anonymous writable mapping was made COW via fork(), the kernel would + * mark it Write=0,Dirty=1. Now it will instead use the Cow bit. This + * happens in copy_present_pte(). + * - A R/O page that has been COW'ed. The user page is in a R/O VMA, + * and get_user_pages(FOLL_FORCE) needs a writable copy. The page fault + * handler creates a copy of the page and sets the new copy's PTE as + * Write=0 and Cow=1. + * - A shared shadow stack PTE. When a shadow stack page is being shared + * among processes (this happens at fork()), its PTE is made Dirty=0, so + * the next shadow stack access causes a fault, and the page is + * duplicated and Dirty=1 is set again. This is the COW equivalent for + * shadow stack pages, even though it's copy-on-access rather than + * copy-on-write. + * + * (Write=0,Cow=0,Dirty=1): + * - A shadow stack PTE. + * - A Cow PTE created when a processor without shadow stack support set + * Dirty=1. + */ +#ifdef CONFIG_X86_USER_SHADOW_STACK +#define _PAGE_COW (_AT(pteval_t, 1) << _PAGE_BIT_COW) +#else +#define _PAGE_COW (_AT(pteval_t, 0)) +#endif + +#define _PAGE_DIRTY_BITS (_PAGE_DIRTY | _PAGE_COW) + #define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE) /* @@ -186,12 +230,17 @@ enum page_cache_mode { #define PAGE_READONLY __pg(__PP| 0|_USR|___A|__NX| 0| 0| 0) #define PAGE_READONLY_EXEC __pg(__PP| 0|_USR|___A| 0| 0| 0| 0) -#define __PAGE_KERNEL (__PP|__RW| 0|___A|__NX|___D| 0|___G) -#define __PAGE_KERNEL_EXEC (__PP|__RW| 0|___A| 0|___D| 0|___G) -#define _KERNPG_TABLE_NOENC (__PP|__RW| 0|___A| 0|___D| 0| 0) -#define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC) +/* + * Page tables needs to have Write=1 in order for any lower PTEs to be + * writable. This includes shadow stack memory (Write=0, Dirty=1) + */ #define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0) #define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC) +#define _KERNPG_TABLE_NOENC (__PP|__RW| 0|___A| 0|___D| 0| 0) +#define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC) + +#define __PAGE_KERNEL (__PP|__RW| 0|___A|__NX|___D| 0|___G) +#define __PAGE_KERNEL_EXEC (__PP|__RW| 0|___A| 0|___D| 0|___G) #define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX| 0| 0|___G) #define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0| 0| 0|___G) #define __PAGE_KERNEL_NOCACHE (__PP|__RW| 0|___A|__NX|___D| 0|___G| __NC) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index cda3118f3b27..9429da70d689 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -273,7 +273,8 @@ static inline bool pte_flags_need_flush(unsigned long oldflags, const pteval_t flush_on_clear = _PAGE_DIRTY | _PAGE_PRESENT | _PAGE_ACCESSED; const pteval_t software_flags = _PAGE_SOFTW1 | _PAGE_SOFTW2 | - _PAGE_SOFTW3 | _PAGE_SOFTW4; + _PAGE_SOFTW3 | _PAGE_SOFTW4 | + _PAGE_COW; const pteval_t flush_on_change = _PAGE_RW | _PAGE_USER | _PAGE_PWT | _PAGE_PCD | _PAGE_PSE | _PAGE_GLOBAL | _PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PKEY_BIT0 | _PAGE_PKEY_BIT1 | From patchwork Thu Jan 19 21:22:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108789 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 1DBE5C6379F for ; Thu, 19 Jan 2023 21:23:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2F1F6B008C; Thu, 19 Jan 2023 16:23:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8FAB6B0092; Thu, 19 Jan 2023 16:23:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE838E0001; Thu, 19 Jan 2023 16:23:45 -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 7CCE96B008C for ; Thu, 19 Jan 2023 16:23:45 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 48634A0275 for ; Thu, 19 Jan 2023 21:23:45 +0000 (UTC) X-FDA: 80372825610.01.C808D22 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 5102A4000F for ; Thu, 19 Jan 2023 21:23:43 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bz10HKRK; spf=pass (imf27.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=1674163423; 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=raQIy6Fe766NUbCocQ4XfRzcFOdeBSgY+sYH+kVPZqU=; b=FYntVvP38uh1+0s2VBA7bn/5Vi1O2gh+FBh0UPymBlE+0x85H49MCOyp2ntcO1Zk27psSM q7/XSoYLk8NUOtuceopCYOgacocsWapOTwYTk7FM/r/6JFtrOIVfZ/OI4txstzpnv0VtYU wh3tjFs793/YyWA4Iqc59QAdQzTxyAQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bz10HKRK; spf=pass (imf27.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=1674163423; a=rsa-sha256; cv=none; b=pp5vTAncP9tKPFriu3yVYAyGQrGFTcaF9Q6DeTpBXtgUiwqdrTz6ZWuXSjVMAlfssaP7T6 NX7na3nFTE+2uUCZLGwcF1DfhY1gGPxjl0EA0OeEuYyLzNr5mbal7cQj4mC7p5ozgwxg0k dRAF8MYIKQ3o29FibCyw7ESa5kkK2kk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163423; x=1705699423; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=0xRZR4lTYh6NOOM79Ggd51EdLXNXPAUjGASZ+kAeGKM=; b=Bz10HKRK3VK0rFUJYz0cfitDqNY8XsMYtlnr3IlhCQiHMlBz9p8DgWRT al4Dmie3KrJRd5/tHAeG9ixgj8qHd501GFnW8Iz1Qql56pdzb6rj823do xKOiw+fFojXy8iBSOT88WBjcP/sfwpC+Dz15yGccazR0JgbgGjW+e1oEj VVB3ZWjacorrawVHVl4MB5R70yix7A+jfsjzQxmasgWlqtAB+N0+StKdF KzxDDVC8vS0aSPWKFkig90fOfLiBSU5s2zmheJ/nhQYaTzNuGI53SNuoX b+Mnx06i4bK1AMHW7uifyFPjkNUrgZ5BffaNXu+YUnqFnZtw78kV3jAeS g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119413" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119413" 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:23:42 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139038" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139038" 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:23:41 -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, Yu-cheng Yu Subject: [PATCH v5 11/39] x86/mm: Update pte_modify for _PAGE_COW Date: Thu, 19 Jan 2023 13:22:49 -0800 Message-Id: <20230119212317.8324-12-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: 5102A4000F X-Stat-Signature: n15dc68h5soxty3hkzs18eqzh6bcuru8 X-Rspam-User: X-HE-Tag: 1674163423-476256 X-HE-Meta: U2FsdGVkX19AX/OBOwRkgh6bhFJMBRaSkOLyhcvY1S/eAIqycPPf38JfDiWUIWy0fLFIuOcpCVAvITwjJVNhW96Tu0wec+NAivY0w1is5roPo6qW6GOTzuAw37kfaRXgWK1iUMbSN9ijTvA0UeLEF91HzNnP9uWTwK1q67ovBg3JBbno2MP7HjHDhZnoO7cRHDcqCgscWymWEoqHu0vOd6WjguwceYPNYLCLFum0UMonzL6PiiJfLBIHzYA5zSGnohZJUFxzMoYKvDuw1Xjnqz0uHrepKCMVDts1pRqEYsYXJJ0xh6YcJmhf2HJWJ6orwsKaZdD27V6Ht8flhhNsPX+HdXzqg7hnxeK4oR48sTAvdhSiUUGBUMFU+hOUVZ7UnBD4EKoYfya31j2XElN5nEbzFfQjmDqQG5QU8YCrs1NjURGCoQIBxfHr3Q9m0DpHJlgXPcM6Q/kLo1c3UUYnSbDu+1ApQ2LSYd0fHOCELFYHdP2xyB3qaOt2F9Kx2KkjGhUacrWeZhB9k+n30feB5r/wC7qulux9fdGBZD8Y8SrUj9jDAfXTbtmwTEGDRpMvgFwVS3JrUD5Pg7PVMidm0uY5zW6TdfCuhJUMzxRMM8jEBAGferJVrnS2I4kNKyK2IA4+gwbUS/84y5jXPxUkS7fxe2lRc27KcIL93bSGgdUDiKsHmIbzXbMAPmvMj4yGb1/ShUmBUjiVydma3JTBaI1oAxaZZDjrruuWpDmyTCc6YM1H4PTsSGA0iT6mwnj5UnwICPuczVfy+AW+2+Skqq+5AKHUMYFbstvga3d+H3xkChy5js2jmu+JEiYvioZy8XeyNhP6OJk4ibr6OgXfngDZBDM5fbd92Qsed0mrtNKp8zKOc1MgJv6fSxjo+mh/ui3qAmqS5BWLaGR1vbOgMR5Ttcy5vgJ2BZwDuQTXeehRjtlvjsQVI2tbjV1fCdFV5tLKuN3wRsNfohq7SjY 5YYF44Bg ztR26fQ5QwNUTaJIBv/ly/Tf0OwNX6TLYAGnRGKAEtzr34CksEgO5smJFviheGEB5SttIs71mi42jqwc53MPKAVtaP9QPCNIiF7k5lPr/T9kleWMpmyJpBV5Ops7mpAdDhXnOOxV1QQldQdA7eXfK5drYN7hNdw9gV8lByWhf7QLs1+5XCqRxLxZFxl9dQZ3+TK5cr5u14/LJru80PRjy8iqyjICCWqxPGoC3Mji7yxarlTka+MFBwEeZG0gpFRnhxccFMYzwYN+EZP9YW1AOxrXJKw== 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: Yu-cheng Yu The Write=0,Dirty=1 PTE has been used to indicate copy-on-write pages. However, newer x86 processors also regard a Write=0,Dirty=1 PTE as a shadow stack page. In order to separate the two, the software-defined _PAGE_DIRTY is changed to _PAGE_COW for the copy-on-write case, and pte_*() are updated to do this. pte_modify() takes a "raw" pgprot_t which was not necessarily created with any of the existing PTE bit helpers. That means that it can return a pte_t with Write=0,Dirty=1, a shadow stack PTE, when it did not intend to create one. However pte_modify() changes a PTE to 'newprot', but it doesn't use the pte_*(). Modify it to also move _PAGE_DIRTY to _PAGE_COW. Do this by using the pte_mkdirty() helper. Since pte_mkdirty() also sets the soft dirty bit, extract a helper that optionally doesn't set _PAGE_SOFT_DIRTY. This helper will allow future logic for deciding when to move _PAGE_DIRTY to _PAGE_COW can live in one place. Apply the same changes to pmd_modify(). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix pte_modify() again, to not lose _PAGE_DIRTY, but still not set _PAGE_SOFT_DIRTY as was fixed in v4. v4: - Fix an issue in soft-dirty test, where pte_modify() would detect _PAGE_COW in pte_dirty() and set the soft dirty bit in pte_mkdirty(). v2: - Update commit log with text and suggestions from (Dave Hansen) - Drop fixup_dirty_pte() in favor of clearing the HW dirty bit along with the _PAGE_CHG_MASK masking, then calling pte_mkdirty() (Dave Hansen) arch/x86/include/asm/pgtable.h | 64 +++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 6d2f612c04b5..7942eff2af50 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -392,9 +392,19 @@ static inline pte_t pte_mkexec(pte_t pte) return pte_clear_flags(pte, _PAGE_NX); } +static inline pte_t __pte_mkdirty(pte_t pte, bool soft) +{ + pteval_t dirty = _PAGE_DIRTY; + + if (soft) + dirty |= _PAGE_SOFT_DIRTY; + + return pte_set_flags(pte, dirty); +} + static inline pte_t pte_mkdirty(pte_t pte) { - return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); + return __pte_mkdirty(pte, true); } static inline pte_t pte_mkyoung(pte_t pte) @@ -503,9 +513,19 @@ static inline pmd_t pmd_wrprotect(pmd_t pmd) return pmd_clear_flags(pmd, _PAGE_RW); } +static inline pmd_t __pmd_mkdirty(pmd_t pmd, bool soft) +{ + pmdval_t dirty = _PAGE_DIRTY; + + if (soft) + dirty |= _PAGE_SOFT_DIRTY; + + return pmd_set_flags(pmd, dirty); +} + static inline pmd_t pmd_mkdirty(pmd_t pmd) { - return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); + return __pmd_mkdirty(pmd, true); } static inline pmd_t pmd_mkdevmap(pmd_t pmd) @@ -715,26 +735,54 @@ static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask); static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) { + pteval_t _page_chg_mask_no_dirty = _PAGE_CHG_MASK & ~_PAGE_DIRTY; pteval_t val = pte_val(pte), oldval = val; + pte_t pte_result; /* * Chop off the NX bit (if present), and add the NX portion of * the newprot (if present): */ - val &= _PAGE_CHG_MASK; - val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK; + val &= _page_chg_mask_no_dirty; + val |= check_pgprot(newprot) & ~_page_chg_mask_no_dirty; val = flip_protnone_guard(oldval, val, PTE_PFN_MASK); - return __pte(val); + + pte_result = __pte(val); + + /* + * Dirty bit is not preserved above so it can be done + * in a special way for the shadow stack case, where it + * may need to set _PAGE_COW. __pte_mkdirty() will do this in + * the case of shadow stack. + */ + if (pte_dirty(pte)) + pte_result = __pte_mkdirty(pte_result, false); + + return pte_result; } static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) { + pteval_t _hpage_chg_mask_no_dirty = _HPAGE_CHG_MASK & ~_PAGE_DIRTY; pmdval_t val = pmd_val(pmd), oldval = val; + pmd_t pmd_result; - val &= _HPAGE_CHG_MASK; - val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK; + val &= _hpage_chg_mask_no_dirty; + val |= check_pgprot(newprot) & ~_hpage_chg_mask_no_dirty; val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK); - return __pmd(val); + + pmd_result = __pmd(val); + + /* + * Dirty bit is not preserved above so it can be done + * in a special way for the shadow stack case, where it + * may need to set _PAGE_COW. __pmd_mkdirty() will do this in + * the case of shadow stack. + */ + if (pmd_dirty(pmd)) + pmd_result = __pmd_mkdirty(pmd_result, false); + + return pmd_result; } /* From patchwork Thu Jan 19 21:22:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108790 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 6D995C46467 for ; Thu, 19 Jan 2023 21:23:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 995DC6B0092; Thu, 19 Jan 2023 16:23:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91F5A6B0093; Thu, 19 Jan 2023 16:23:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74B2C6B0095; Thu, 19 Jan 2023 16:23:47 -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 5B0A16B0092 for ; Thu, 19 Jan 2023 16:23:47 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2F7F01A0869 for ; Thu, 19 Jan 2023 21:23:47 +0000 (UTC) X-FDA: 80372825694.21.A43A55D Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 3437F18000D for ; Thu, 19 Jan 2023 21:23:45 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="b98X5/zX"; 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=1674163425; 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=qRKDNK2oMQO5oOmk+unpG3nEq+k+7RxaZmTu69DQeXE=; b=holrM65BxBSEgImQdaBHThVxEFaXECZqcIwew/2xlTNvx+kUtlY7/Kc0cWNspdS8+R66Uu Etqc0u7vhyOtPPgZ+UZDXaaXwh2bELKYFI6VDOUuchOoQGWNyXm+FtGmtFurSy0UWxVgOx pfpKGP9+0/uLPagesWFLDsQadhddhA4= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="b98X5/zX"; 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=1674163425; a=rsa-sha256; cv=none; b=vff9BboXkOBO3flX1ukIMJPhr/tZro/7cTY1hHgoO/yZ1T7+Sxs2ZOtK8frsJvrlBdsP5D nAENgPvpAGFU7jCJ5JHyRwCNIOqKvjWa9NlA0QebsSxU7Nz9Yuqzocpgdu7Sh6MPGbsF5C hDSzC2POR14F1e+EvG0DsO98nW9bQo8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163425; x=1705699425; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=BvuOTjZc5NHm9ltMy7F/33kG5ApCivtjPVo72HaZTJE=; b=b98X5/zXpgsQ4ZoSooa98Obqwk47qS8izwohTigwJkbEDSG56iFyO5g/ T0Ip9aEkihdsgUfSJDHIjpYJNwa8EzfFNYbxJVWIPBmTIlv1BsG9Bh9HR RVv7z3vL3jxd/45E9X/CyyZ7bDqwm95oD0aK35tn5hxGFyagRB7ah5w3U /D9lTj1ZybhB4Q0for2xJspeRyiSb56N7mOyxqkx2fv+MfM9P+qCKzZiM tjX4mIXNE6rmka20OiKhlvs7qOkifo4QZ3GULVUOMffy7Frqq6UmZWNRp GecanX1A5DRIO2teQP54Sj0Zr+1JxTiPJ7IPN0lw5oA1rVB5ISd+TN556 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119436" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119436" 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:23:44 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139043" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139043" 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:23:42 -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, Yu-cheng Yu Subject: [PATCH v5 12/39] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW Date: Thu, 19 Jan 2023 13:22:50 -0800 Message-Id: <20230119212317.8324-13-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: 3437F18000D X-Stat-Signature: 8bn8xubrdwxbpij1gyrfc64mjae6nixm X-Rspam-User: X-HE-Tag: 1674163425-815341 X-HE-Meta: U2FsdGVkX18E4i+B1wL+C5DFuzkunH520lx/pVKq0j5owxEOONHRU7OnKM9Xa5g1pUmFx50TfacEEFZWjtpRPPV5levAZN+zM718QRqsIoybzbXtSQQGdCbTzJ+7pm9S82AnTOEMhICG0tGm129sRlLwLwf3hvJU/oZRuQMr6SWZPxBSKhlDcnc8gSbwS972OK0pYsrAXbRStlHCVnqDa7Oe/vO0afuVq1+pu0Wj2g6MOJv/0mMsWSOheMGk5lpbNd1BkoXcRZ90Oj6EhH0oQL4luAAT4g7FC4LTtgDVE7oBx9j187gUf6NHgaz96J47DXrwMfJ+kkl/8Oqk3PziWHlj7lJN4x+R4wqcHaG6gyYn0cL1ujKcJm92KlD72tBJMU+USHODnA7U2seNRHeRTl9aiXYz7VStViH1CyUR3RC23IEn0vzIXYU4+hU14rOQd+TglsKTBMngV68roDZ9h+mA/wR0oyk7EknHwysXOo1gS+vLuUWJSWCZNTsGw15QTcniH69aSoG2NcSzO8H4fk3o1NNYC/OszCyRt7lGmRQ98UXRmSLU40f3sD43qwc3D9e7Eje4rU5xHIqmZ/zTMP+Khrsd6XNCv7Sp4iasRdnQMvy9lr/ygKlxBGJJKW2PgyLr5YPPV43fwPphnjzUChgvFhWq1s3F7wbx7OpDhThhp0iRcXZXBfQj1ThPNaZDzJcQE/G93H1PEtrd3+L7lpyVWusfl81Qd9eotrUfcvV38NLc6cHnqN4TRqRnR+i91d2rlEL692ktGck23lcnfA67+UR/yiaiAim52c9wmHMs5Tbo9w2/IGps2l+dOIWiRPYscE07KKcNxuYU/J/Oh2rcWzW4mrlB6+w2N3Esi5r0ch4KHc8wjvKxLOIJhJ9npYmI/1Q6yrh6gCsqVdifcXzD2+N05q6w7pJkKxRfdSBpFIcT3f2M3QSuEWhF/Vhj05QGVOqkFQA/PIP3oma aCwDlcAI wxJBdMlSwgWo2atn1tDo4hufsGDrpUoM48G/0cOE31Nisqq4UnDH4X2ly7yGaWs3rdwXNDcDQxInUU36pwugzNpskkaHeHiutdepjG44TVm8hk0xtvJ/9oFtOx1kyH6/HaqsiZWqMlomD8WB/1b21bSduWKNkJDka6qDg2ol3TMxMDozlE/kIltWa73bzkZNvLnPm8VJS/usvBAOX70fFek5vGiWibpLNYFjA38EvBbKcvr0bD28nmpaPJxCpySFJQzNgmHQQ9n5Z+4MuI2a4icdjPQ== 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: Yu-cheng Yu When shadow stack is in use, Write=0,Dirty=1 PTE are preserved for shadow stack. Copy-on-write PTEs then have Write=0,Cow=1. When a PTE goes from Write=1,Dirty=1 to Write=0,Cow=1, it could become a transient shadow stack PTE in two cases: 1. Some processors can start a write but end up seeing a Write=0 PTE by the time they get to the Dirty bit, creating a transient shadow stack PTE. However, this will not occur on processors supporting shadow stack, and a TLB flush is not necessary. 2. When _PAGE_DIRTY is replaced with _PAGE_COW non-atomically, a transient shadow stack PTE can be created as a result. Thus, prevent that with cmpxchg. In the case of pmdp_set_wrprotect(), for nopmd configs the ->pmd operated on does not exist and the logic would need to be different. Although the extra functionality will normally be optimized out when user shadow stacks are not configured, also exclude it in the preprocessor stage so that it will still compile. User shadow stack is not supported there by Linux anyway. Leave the cpu_feature_enabled() check so that the functionality also gets disabled based on runtime detection of the feature. Similarly, compile it out in ptep_set_wrprotect() due to a clang warning on i386. Like above, the code path should get optimized out on i386 since shadow stack is not supported on 32 bit kernels, but this makes the compiler happy. Dave Hansen, Jann Horn, Andy Lutomirski, and Peter Zijlstra provided many insights to the issue. Jann Horn provided the cmpxchg solution. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Commit log verbiage and formatting (Boris) - Remove capitalization on shadow stack (Boris) - Fix i386 warning on recent clang v3: - Remove unnecessary #ifdef (Dave Hansen) v2: - Compile out some code due to clang build error - Clarify commit log (dhansen) - Normalize PTE bit descriptions between patches (dhansen) - Update comment with text from (dhansen) Yu-cheng v30: - Replace (pmdval_t) cast with CONFIG_PGTABLE_LEVELES > 2 (Borislav Petkov). arch/x86/include/asm/pgtable.h | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 7942eff2af50..c5047eb5f406 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1232,6 +1232,23 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { +#ifdef CONFIG_X86_USER_SHADOW_STACK + /* + * Avoid accidentally creating shadow stack PTEs + * (Write=0,Dirty=1). Use cmpxchg() to prevent races with + * the hardware setting Dirty=1. + */ + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) { + pte_t old_pte, new_pte; + + old_pte = READ_ONCE(*ptep); + do { + new_pte = pte_wrprotect(old_pte); + } while (!try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte)); + + return; + } +#endif clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); } @@ -1284,6 +1301,26 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp) { +#ifdef CONFIG_X86_USER_SHADOW_STACK + /* + * If shadow stack is enabled, pmd_wrprotect() moves _PAGE_DIRTY + * to _PAGE_COW (see comments at pmd_wrprotect()). + * When a thread reads a RW=1, Dirty=0 PMD and before changing it + * to RW=0, Dirty=0, another thread could have written to the page + * and the PMD is RW=1, Dirty=1 now. + */ + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) { + pmd_t old_pmd, new_pmd; + + old_pmd = READ_ONCE(*pmdp); + do { + new_pmd = pmd_wrprotect(old_pmd); + } while (!try_cmpxchg(&pmdp->pmd, &old_pmd.pmd, new_pmd.pmd)); + + return; + } +#endif + clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); } From patchwork Thu Jan 19 21:22:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108791 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 0FF67C6379F for ; Thu, 19 Jan 2023 21:23:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 502066B0095; Thu, 19 Jan 2023 16:23:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 48CD36B0096; Thu, 19 Jan 2023 16:23:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DDE98E0001; Thu, 19 Jan 2023 16:23:50 -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 0EB156B0095 for ; Thu, 19 Jan 2023 16:23:50 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CA11C1C658F for ; Thu, 19 Jan 2023 21:23:48 +0000 (UTC) X-FDA: 80372825736.07.C6EBB78 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id C4E5440010 for ; Thu, 19 Jan 2023 21:23:46 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Mp57EqQ3; spf=pass (imf27.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=1674163427; 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=e2Tm+WaYXmy3ls6tYjrnsqh0ZVC1D7WOAXojsWXtuCw=; b=EQU/sILmBNH2ufBqggnSRXZM7b2+3K9GBsr2SfEPOV5ekeJLb8P38Xh16O7rFX2dOEWnxq 2zPVKM8Jyq8HP7nH3TPWbyyDOkDbXcqwn2715toQbotjQt7ZrOtJdIV1KhipDJ6dh5nkrh oZ7RHC0GwkKKKFkYHUwXUvZ0w5cbgRM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Mp57EqQ3; spf=pass (imf27.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=1674163427; a=rsa-sha256; cv=none; b=B2Td3z97oP7EQzXo43VQAf9KirvGTZxRThtX+72lN8hQMWkVU/Nqj64NUikpTNi6m36iYK M9kkNFuDbRGOBZO8VyUY8CnNpDq/2+phnv2MOebY+939E9UIgBFkPnmGVs17JJl3wZ3hTi xwxICx1Es0UA9v+HAmnEpipK+6fUef4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163426; x=1705699426; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Wk+41wU25mzgjYiTB7ECzyixSrgPZ+gatfisdIuQPw8=; b=Mp57EqQ3XMKXxtPVUtv6VrtCaZLXAzBy17S2un5uD63lPx/t6FjTksBW MSn7pUefla5qvJbu08j8gl70mjOGbFVKWoTWPTVKNN00AN8fcweJGIqo4 XFoK5+hovj9nHyV0x+KpN4/jzMOzw4Q3NsfXfP0UbZG7SPb197ivqNH9h wTzM16szX18V9tGn/29w7M1ZgE5B4QKyLItHcM1yN6pKAbaxcJH7z/69p m4qmXWMhx7Tq2fcdta+Qr+tzdqHonx1/zRCnGX2tW9oVVMgobFG+qq9cq rUVRyCRc02zT/bKvh0IJ0N8QWMWSQ4l7diQpyXx/jmDlFsrPinTV+QR8w A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119459" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119459" 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:23:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139047" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139047" 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:23:44 -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, Yu-cheng Yu Subject: [PATCH v5 13/39] x86/mm: Start actually marking _PAGE_COW Date: Thu, 19 Jan 2023 13:22:51 -0800 Message-Id: <20230119212317.8324-14-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: C4E5440010 X-Stat-Signature: 6oyjuya4e1t9hoysf4q4bwdhqokrox4y X-Rspam-User: X-HE-Tag: 1674163426-979918 X-HE-Meta: U2FsdGVkX1/JBjhROK1UhUlNXdRNhgiWDOHZuiCe994GBsqtE6HfbDyBCb/hjWhHo8Gxfs/VOTUQcmM/mL10mAfGCcdbLMVaIaTQGBoWen6B+BWAERXBsBOMaGNC9VjM1QKruMvM+3wvIMEKTNNnXA4/MJrPUSJDZW3eAGBQyK2JVGmG54zy41XFzL9WA5RcY54lAcCrfgMaCVQRCyR7mwdAuhCZ5Joxhi5GS1htcUzv7sq9+eveci0abX8N3XaHmzJAWlHPXQ5na4hVRkwjnRgn2UqOTL8TRx8uLxq+cJou3mnCGfOXGbvklaUU9Ht/ZcCYMoMaq75x6g1PMn3os3rubiKW+Q14lq0YcMMIzyO9EJZ7puE8TyvPdZYWAvjoAPM64c4of3EK1g9c6Aaxq7zJAQme/nRXXv0aVKwctL37MS1EGQrfrrTWFZosIRwDTkU0Vn8KElRpvI1zfMhjD/UcnbJ1vRwBdfha3SRppPZqFDcLWF9NExDJ6UiiHwIcNAzF0nKlB23J8UUXJFq01xn1v42L1gzvoLBVQQxWJLPivx+Rz8kEdFDgWoHypocTI4ibTVjESHPRDkynqlRMzdoN6kKMPgzaFcNKKG0EHA5CsxA3PeCXJhPxKGdnv+SeXmJYFgJIntmkymHgriZFaTxgFc2f9h06s0LfALUYCW/aZiyT+0sfGLabjrLyU1LYwH/II1HHN7BMP7Cm7u0PdAg6fFWQ+yFm1/tLKhFxeJG6x4c7I74iV9OF6IMkR/dxHufKKwwIzGxz9c5U58oC4I98j6via/6AxKroBMCBsSqQILrgq2/jj+MLNq8HkEh6JRZqLJxM2enwE37PWEJ41jftyhRUmusGH5ROKt3q4GCQlwNf0zFig7sliiWWWRhfaJ0vT8mVMOpXMn3bGhb8/NXWt4PJC1l0XObfFRpZ7rHI9XtRr7KZLq+X75LZ4PaM4x0koCy0whsv8+pwtQ/ Bs7Xcsoy Q3ccwaOn+ZvozXx5R/oePKvDN9n7DmThwiLMYEzJefA38Dyy6XQBxJzl+Zk57vmfr97pnvQKUr4ZK6XK7mwaE58XvlbJe8mhY+zEv8Yi2v0xPkunKyacReMx50zcF754qKJqq4r1cO9jO/Kx5kFZF1fDOnFqQe+O1Sj/KkJ38++pdMQrK7NiMoGxMqpU9vB1NbvHihUW0xURFrcI5//sSxOtxCXAowKjzpmrfZTn6uJRr/HUwGqfwVtCbxU01DhYvjxZkFe7vbqKoXExEZh+/rDwtlChBNfJLXHzcD62HOHvxfCGgw30DYsFCr8Bz0DS2Ye+N76vtKpDu54+KA3QPCE+BWw== 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: The recently introduced _PAGE_COW should be used instead of the HW Dirty bit whenever a PTE is Write=0, in order to not inadvertently create shadow stack PTEs. Update pte_mk*() helpers to do this, and apply the same changes to pmd and pud. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe --- v4: - Break part patch for better bisectability arch/x86/include/asm/pgtable.h | 125 ++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index c5047eb5f406..e96558abc8ec 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -124,9 +124,17 @@ extern pmdval_t early_pmd_flags; * The following only work if pte_present() is true. * Undefined behaviour if not.. */ -static inline int pte_dirty(pte_t pte) +static inline bool pte_dirty(pte_t pte) { - return pte_flags(pte) & _PAGE_DIRTY; + return pte_flags(pte) & _PAGE_DIRTY_BITS; +} + +static inline bool pte_shstk(pte_t pte) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return false; + + return (pte_flags(pte) & (_PAGE_RW | _PAGE_DIRTY)) == _PAGE_DIRTY; } static inline int pte_young(pte_t pte) @@ -134,9 +142,18 @@ static inline int pte_young(pte_t pte) return pte_flags(pte) & _PAGE_ACCESSED; } -static inline int pmd_dirty(pmd_t pmd) +static inline bool pmd_dirty(pmd_t pmd) { - return pmd_flags(pmd) & _PAGE_DIRTY; + return pmd_flags(pmd) & _PAGE_DIRTY_BITS; +} + +static inline bool pmd_shstk(pmd_t pmd) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return false; + + return (pmd_flags(pmd) & (_PAGE_RW | _PAGE_DIRTY | _PAGE_PSE)) == + (_PAGE_DIRTY | _PAGE_PSE); } #define pmd_young pmd_young @@ -145,9 +162,9 @@ static inline int pmd_young(pmd_t pmd) return pmd_flags(pmd) & _PAGE_ACCESSED; } -static inline int pud_dirty(pud_t pud) +static inline bool pud_dirty(pud_t pud) { - return pud_flags(pud) & _PAGE_DIRTY; + return pud_flags(pud) & _PAGE_DIRTY_BITS; } static inline int pud_young(pud_t pud) @@ -157,13 +174,21 @@ static inline int pud_young(pud_t pud) static inline int pte_write(pte_t pte) { - return pte_flags(pte) & _PAGE_RW; + /* + * Shadow stack pages are logically writable, but do not have + * _PAGE_RW. Check for them separately from _PAGE_RW itself. + */ + return (pte_flags(pte) & _PAGE_RW) || pte_shstk(pte); } #define pmd_write pmd_write static inline int pmd_write(pmd_t pmd) { - return pmd_flags(pmd) & _PAGE_RW; + /* + * Shadow stack pages are logically writable, but do not have + * _PAGE_RW. Check for them separately from _PAGE_RW itself. + */ + return (pmd_flags(pmd) & _PAGE_RW) || pmd_shstk(pmd); } #define pud_write pud_write @@ -374,7 +399,7 @@ static inline pte_t pte_clear_uffd_wp(pte_t pte) static inline pte_t pte_mkclean(pte_t pte) { - return pte_clear_flags(pte, _PAGE_DIRTY); + return pte_clear_flags(pte, _PAGE_DIRTY_BITS); } static inline pte_t pte_mkold(pte_t pte) @@ -384,7 +409,16 @@ static inline pte_t pte_mkold(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { - return pte_clear_flags(pte, _PAGE_RW); + pte = pte_clear_flags(pte, _PAGE_RW); + + /* + * Blindly clearing _PAGE_RW might accidentally create + * a shadow stack PTE (Write=0,Dirty=1). Move the hardware + * dirty value to the software bit. + */ + if (pte_dirty(pte)) + pte = pte_mkcow(pte); + return pte; } static inline pte_t pte_mkexec(pte_t pte) @@ -396,6 +430,10 @@ static inline pte_t __pte_mkdirty(pte_t pte, bool soft) { pteval_t dirty = _PAGE_DIRTY; + /* Avoid creating Dirty=1,Write=0 PTEs */ + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && !pte_write(pte)) + dirty = _PAGE_COW; + if (soft) dirty |= _PAGE_SOFT_DIRTY; @@ -407,6 +445,12 @@ static inline pte_t pte_mkdirty(pte_t pte) return __pte_mkdirty(pte, true); } +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + /* pte_clear_cow() also sets Dirty=1 */ + return pte_clear_cow(pte); +} + static inline pte_t pte_mkyoung(pte_t pte) { return pte_set_flags(pte, _PAGE_ACCESSED); @@ -414,7 +458,12 @@ static inline pte_t pte_mkyoung(pte_t pte) static inline pte_t pte_mkwrite(pte_t pte) { - return pte_set_flags(pte, _PAGE_RW); + pte = pte_set_flags(pte, _PAGE_RW); + + if (pte_dirty(pte)) + pte = pte_clear_cow(pte); + + return pte; } static inline pte_t pte_mkhuge(pte_t pte) @@ -505,18 +554,30 @@ static inline pmd_t pmd_mkold(pmd_t pmd) static inline pmd_t pmd_mkclean(pmd_t pmd) { - return pmd_clear_flags(pmd, _PAGE_DIRTY); + return pmd_clear_flags(pmd, _PAGE_DIRTY_BITS); } static inline pmd_t pmd_wrprotect(pmd_t pmd) { - return pmd_clear_flags(pmd, _PAGE_RW); + pmd = pmd_clear_flags(pmd, _PAGE_RW); + /* + * Blindly clearing _PAGE_RW might accidentally create + * a shadow stack PMD (RW=0, Dirty=1). Move the hardware + * dirty value to the software bit. + */ + if (pmd_dirty(pmd)) + pmd = pmd_mkcow(pmd); + return pmd; } static inline pmd_t __pmd_mkdirty(pmd_t pmd, bool soft) { pmdval_t dirty = _PAGE_DIRTY; + /* Avoid creating (HW)Dirty=1, Write=0 PMDs */ + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && !pmd_write(pmd)) + dirty = _PAGE_COW; + if (soft) dirty |= _PAGE_SOFT_DIRTY; @@ -528,6 +589,11 @@ static inline pmd_t pmd_mkdirty(pmd_t pmd) return __pmd_mkdirty(pmd, true); } +static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd) +{ + return pmd_clear_cow(pmd); +} + static inline pmd_t pmd_mkdevmap(pmd_t pmd) { return pmd_set_flags(pmd, _PAGE_DEVMAP); @@ -545,7 +611,11 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) static inline pmd_t pmd_mkwrite(pmd_t pmd) { - return pmd_set_flags(pmd, _PAGE_RW); + pmd = pmd_set_flags(pmd, _PAGE_RW); + + if (pmd_dirty(pmd)) + pmd = pmd_clear_cow(pmd); + return pmd; } static inline pud_t pud_set_flags(pud_t pud, pudval_t set) @@ -589,17 +659,32 @@ static inline pud_t pud_mkold(pud_t pud) static inline pud_t pud_mkclean(pud_t pud) { - return pud_clear_flags(pud, _PAGE_DIRTY); + return pud_clear_flags(pud, _PAGE_DIRTY_BITS); } static inline pud_t pud_wrprotect(pud_t pud) { - return pud_clear_flags(pud, _PAGE_RW); + pud = pud_clear_flags(pud, _PAGE_RW); + + /* + * Blindly clearing _PAGE_RW might accidentally create + * a shadow stack PUD (RW=0, Dirty=1). Move the hardware + * dirty value to the software bit. + */ + if (pud_dirty(pud)) + pud = pud_mkcow(pud); + return pud; } static inline pud_t pud_mkdirty(pud_t pud) { - return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); + pudval_t dirty = _PAGE_DIRTY; + + /* Avoid creating (HW)Dirty=1, Write=0 PUDs */ + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && !pud_write(pud)) + dirty = _PAGE_COW; + + return pud_set_flags(pud, dirty | _PAGE_SOFT_DIRTY); } static inline pud_t pud_mkdevmap(pud_t pud) @@ -619,7 +704,11 @@ static inline pud_t pud_mkyoung(pud_t pud) static inline pud_t pud_mkwrite(pud_t pud) { - return pud_set_flags(pud, _PAGE_RW); + pud = pud_set_flags(pud, _PAGE_RW); + + if (pud_dirty(pud)) + pud = pud_clear_cow(pud); + return pud; } #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY From patchwork Thu Jan 19 21:22:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108792 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 CC496C004D4 for ; Thu, 19 Jan 2023 21:23:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A81CE6B0096; Thu, 19 Jan 2023 16:23:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A096F8E0001; Thu, 19 Jan 2023 16:23:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 836A26B0099; Thu, 19 Jan 2023 16:23:50 -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 5CF2B6B0098 for ; Thu, 19 Jan 2023 16:23:50 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3DE31120DF4 for ; Thu, 19 Jan 2023 21:23:50 +0000 (UTC) X-FDA: 80372825820.27.E50A314 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 2748A180011 for ; Thu, 19 Jan 2023 21:23:47 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=klHA003b; 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=1674163428; 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=Cxz0RFzia4yt8BlCv7dHJItKc83W6pPpciB1MrUhgXc=; b=nz67eAqt+wcMfL7rfUnJ8hWpmRZT1+fqEevfMvHlaMWLPwSz1nxNqf7mJBKCHTdjN6CM8Z FgFutVTEUCMNhUijwMf20WH16/DPAGE+kxwyBgomy/bbVBBac1GK8s9EHVOusICbHe5CZR Y3EFdHGi/TNUHAPr/BvihOqBY2OAtj4= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=klHA003b; 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=1674163428; a=rsa-sha256; cv=none; b=4CtBY26ds2L5gn52q9kNz0hAiWcfT8l8qRWA390f1f/VHsocDSOZG/2io21v68FfTFkjof 2eU4B2TPyGk4m+QDwgyRMyWd4O5f14Nw5/Y8UIoJpME528eUC7vW1IoQFvu8UhWQgael3G uB35PxgjK0Y7gl8MJCvHjFeXWHQG71Q= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163428; x=1705699428; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=i0EcBhWDxak7M9Yc3IVuRFqXYd9IgUPiDgl15diefTc=; b=klHA003bl1py/jOwulov68md1kQ6+1hF6rklLWfUj+ffC3cgXKMMkwNL 2OiFDpy9QySegR+/yA++HmPAsQnalLfn+ROSTuIjANmC3CWV41MapIVBB HN+CHNN4M90kKGHoDDlqarhawG1xLKOj0AKt8D1CfzXyYIsTQFlYOsmYu T3QnTg1IVvp1l7Vh8VI9V2SuUoqqQMMD8oEPBgylacfGJeXLlQPIJBILk Yd/LCb8zbSbkhxX5shJBHD0mb1GTZJJKLoLaa5wMqLblFEGfgf0HQDulN rOS+q2G7riwq8hf2MWIM5tn9YajN0pnMex6XQskLcjNzcMu2kE3fTRFID A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119480" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119480" 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:23:47 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139051" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139051" 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:23:45 -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, Yu-cheng Yu , Peter Xu Subject: [PATCH v5 14/39] mm: Move VM_UFFD_MINOR_BIT from 37 to 38 Date: Thu, 19 Jan 2023 13:22:52 -0800 Message-Id: <20230119212317.8324-15-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: 2748A180011 X-Stat-Signature: qp8rqr98xxmkcim6i8mwbczh6fm1a3xp X-Rspam-User: X-HE-Tag: 1674163427-716969 X-HE-Meta: U2FsdGVkX19zJZmqxqRtXpj62yoPK6wwupbDEpa+4LuSdcIFzMG/spgjaPxuwLMypgoNKI4ziD/QRKkeLM+Rw8T934beh7QTFKhdcS+NpKEpJMdmYJKF70DDIfqlmiJZhWR6A6f1CXLpXkQnUjflTf5zecuvyOAaG+nffArpiiksk4jhxcMkysQ0sSmQYjFekLrjAVmnmdhnbk0itmvZlhTw+8aUKkAyUpY8RFmn1Gb1QJ9/ENT8a9RMMwyA0vmhqVCzbxtdcMty3WrK4xoreE/sVY6l6+YDd/8guU64EUT99L5mSGucmmiJRXm6gbEiJbYJ/3EudtDGjGzIUGCZjs7JBOLKxRKMgoaB6EVbVaehip9wkQ02MlCoCDUj5qjZF+BBOJ9NL4w3Jp/oJs/hzlO42olI+HNouFBF5NRU0yxVMeQvObzgnV6GRKsz9DBGg+/b3alOrmiaZ8J160GwvHT4fvNE0y7YVNnmFcAJZVaEm17L4osha5yyIfJEGCqc6MotPRV+p8zY5j2Id6ZDy0qq46LHk30g13jaw/8USqyHX1p1fA+cWNDq/xyqbrc6GJd9QwLBC1IdN1wQxMRKWQBld/McoDZkTM8L2NL4oikATGuotkaoDZKa2sH4VoGb7b1grPzKJqBK2sqYUfEYSioIGqUYXY42X5lBspHElDe4Ru3lTj8pMub3Br1NGBgPezI+8AmnERA1lf/UYHfQlywq5wJdk9T6K8E5mKPpGaD9NvRbd+gsYUYP15jawuAc4cigV4b9MmUJYTf9cNMxM4vX9B/8J4WPZbTgaE8LW8VUw/RXZTy35G7DLBXI4iuJkz7HGHd5LU0cftnbxcVoLw3DB686qIVETdYu4cW4m+jmWxudfZ7Cf8aBxMe9lmwAHbYQlzSGTCPzaz0i+WJWwhYbgl4hKgLPfmaAgENozdN9trdLX9kBF7+37zjtDJi9Qec3a026dV6nQXLHnDJ KdLZAAO+ VsTbP1utbwACrdbTFT4La/gTVLzekZqCbx5/XZtJSxVLm30kR8ezHpxi8sSqXviddryX9AatMLqR1Gw0T7Jabaz4M16xS6TjyYkLTfGZpqZBd6BtpCRRUktkvH4iDNofHo+TIYAyvD/Wn7q/QrShiHDEDqAdctuWk50fyQfq8HBt/qemVTDeXBGhOprNsiuQhmC3fa+WoVas8L4YR63U+5Xtolk6bxWwRdOJinHzvlT5XQqIVjdLp1IQEwNn5wh26tD5p9IqajmZuN8wJn6uj0xrGQXhHWCqN+mtaSkXYmhD+78qVwcSa1Rr44gdY9kMy17v2bgwEVSXyJpxbxK9IDBX01VBl2vnDFRvpjVby5sA0bzY0dtFplLpang4oOCXplbo9I9ZulPfBNp5iHd9a/Z0zpA== 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: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. Future patches will introduce a new VM flag VM_SHADOW_STACK that will be VM_HIGH_ARCH_BIT_5. VM_HIGH_ARCH_BIT_1 through VM_HIGH_ARCH_BIT_4 are bits 32-36, and bit 37 is the unrelated VM_UFFD_MINOR_BIT. For the sake of order, make all VM_HIGH_ARCH_BITs stay together by moving VM_UFFD_MINOR_BIT from 37 to 38. This will allow VM_SHADOW_STACK to be introduced as 37. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Acked-by: Peter Xu Signed-off-by: Yu-cheng Yu Reviewed-by: Axel Rasmussen Signed-off-by: Rick Edgecombe Cc: Peter Xu Cc: Mike Kravetz --- include/linux/mm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7afc86d50442..82a9a4903651 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -366,7 +366,7 @@ extern unsigned int kobjsize(const void *objp); #endif #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR -# define VM_UFFD_MINOR_BIT 37 +# define VM_UFFD_MINOR_BIT 38 # define VM_UFFD_MINOR BIT(VM_UFFD_MINOR_BIT) /* UFFD minor faults */ #else /* !CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ # define VM_UFFD_MINOR VM_NONE From patchwork Thu Jan 19 21:22:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108793 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 308B5C46467 for ; Thu, 19 Jan 2023 21:23:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DB5E6B0098; Thu, 19 Jan 2023 16:23:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4646F8E0001; Thu, 19 Jan 2023 16:23:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CCA16B009A; Thu, 19 Jan 2023 16:23:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 04CBA6B0098 for ; Thu, 19 Jan 2023 16:23:52 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CE1CC1A03B3 for ; Thu, 19 Jan 2023 21:23:51 +0000 (UTC) X-FDA: 80372825862.18.573DC14 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id CF4354001B for ; Thu, 19 Jan 2023 21:23:49 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=m0PxXXnH; spf=pass (imf27.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=1674163430; 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=StQkA6ot9FCk/YmAiAeLd46+t6/Gkl8n00cyZWXpPeQ=; b=dhyJcAge5k0mskrDMGl1IN4kylqgae0xss1r+uEQ/aWoTM3jpgP7f0VZJ4NmU9xbs2sMCE HV1PxW10TN1fBV6O717bDOV/BRv6HpVgMoIqKrL/aNVqbelmYPyDyDZgrT/LJc0lLFiNRd vmF1Ec444bLf6fW4xk6guRS+I8dFxG8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=m0PxXXnH; spf=pass (imf27.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=1674163430; a=rsa-sha256; cv=none; b=NXkj58XTp+E73T9OVo0lcAuVktSPd8LI5fXdeB8wiiQxoqUwAXkuiL277tChET6HrcD92S DOs7n1fLk8HpZQ5FwE2OPvWppONJud8FEh3mxVERJbJzhzbCCsM9lD/df1chDBmqduqBkU jjveeBEu2UJW4ujPnQXkmCwpL8ntfAM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163429; x=1705699429; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=G9eEBGmU/UnEJ+400J+VwekvQK2O1I9MsdKnAZAqxQw=; b=m0PxXXnHdB2SO/3ZC825Nxe8WMtIyL7iqfoEJMAPKnTJ4BFvkUDrnIwt u1V0CZeMmq2WQ6ZJx2oZ4gtbTstPJLiQmrLSqA9jEaGQ3f+s+m52bT5Ik cQvwSG3va6uE6MV2+t9t3hprxDAb0Tg8APDUPRY90wB2MGOMnNzDsrnh5 fZcYW9xtPbvzkMAg3O6OI403wDZ/zdVtU35FSPDI77D1zPWbhEilAfgKB jrk5JU0bq2NQwcDxgzRKj67WktKsaUPudRdplhTyBC+fbS2hv4IuBzuSX 5VZH6ZCjEmg45jvyZcP6L66DMku5ovDNdH+U0ZoR5Wvvgg4RZlezo8zSU Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119501" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119501" 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:23:49 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139056" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139056" 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:23:47 -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, Yu-cheng Yu Subject: [PATCH v5 15/39] mm: Introduce VM_SHADOW_STACK for shadow stack memory Date: Thu, 19 Jan 2023 13:22:53 -0800 Message-Id: <20230119212317.8324-16-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: CF4354001B X-Stat-Signature: e3h471nwjmoqgbk6puwdyqdhzytdem5t X-Rspam-User: X-HE-Tag: 1674163429-641397 X-HE-Meta: U2FsdGVkX1+MCi9cO5oYiqZH+v7UAhnArj+CGCf7rI33KbXaTMy14iuuZ7/3pKYawijp8HcPOc6hozNO9SuWA9MemhCHNLhik4vUPKf/ZPAq1ZMN0lP4E8lABwcxOi2FaqXvCaXzdSe9hF6hK+KAVDe/g8Y62Q8Vnk3X+ABvRapd9LXRH+ZI1sf9ublJR/BLYJhSBB9AS2zqs1Hu/cWIxG9nEUizcXkFe5sGllOSXsWeiE7AzVpJwtzF4Avk67olvtNwm0UzaC6eyu1KIAu3MYeJGIjZvdY66xMTURTqZfPzs8Nfd4aGtP8yQNPzdwIs9e3Cqi1/KZTO+1yK6ORM2H3rbtSxW9Cte7lReLacdekVrWuZ3UFDBo3wLQlhW+PyuiwNk6KFWwtfBrVN+ktPaIzZrx9/XReoRt9q2m2Cdo4+Ii/mDd8nkK3yYRfSj4EpCcKpqftNmh8SBErwmrend6VX6bh8jlug0iN36qrcj2EyjJFAyiylS/+D+3agImGFsyH2nCDSkHDsFDdS0GkCxbzNiFXMWf3K398mIVa3IJHIfovGEzmsXw4zf7zWJKgipJPYjMc2s7GN9XvGKUIWMRHahJ6LsTnNTEBJW5P6jnyA9c5LxgESdOcGcayRz3KkpYO08FgnVPTdkbUmTj79hMiUCc3dLT3pc8L4TkqfbywVqz/hFHLtraw/gAOYIBcmfEiCRByrbCy8mH+xR7R3Zs0MX4oRGASQLQUHFUrUO3RPTUZvwxLCdZv9QfWU/a3pAuuiKKrGrc3PAYJ10IYLO+u7/KAkFh/E3ZhB+/Uw5sd8dLGIqR/d/UvG6uDVwV5SnXKbrvx6iH8YsV56ogZEvuvjWLPsOFNq53q7zBPJnYavqLjj08iiK6bLm4Rvb/GzXP3mM/WWvrJ91Bd8EPKkOe82NpVkcODVt2Vq/9WWwocCp/Fer8+EOM+EXKDuwY/zumzlbzXN20Z79SR7aOV tuBY39MZ /kQVoBWIxw6hPbcr0jVinL1/CPuaQi0wNmmjiDSRMCW4d/lF2JsN8CiSRsR8Sgw9ZBYMk08otvI+W3RujngjSaTm5YlOHbGAKp5pg9uCBoGZXc3mKKH25PhuRydsnDZcjg42GUEjfgrMGplAmh7yMYp2VZGhIlhDaGbfHdaIidzOujL36E0nGHcfS5sXLAs/ERC3W4JMMEjx+IQ+bDBgE7De1Htbc1hzQrwHiVyceVvJTHBaPsdMt3vQaVaZbtlsI4LEw4HlUq4bwx9JDiOjzyor1L378cHZr6rJtQMzzg1Sl87ztszNYFEyUJdd3DouvflNlds7/EI/BNdZ7u6nYjX/keA== 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: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. A shadow stack PTE must be read-only and have _PAGE_DIRTY set. However, read-only and Dirty PTEs also exist for copy-on-write (COW) pages. These two cases are handled differently for page faults. Introduce VM_SHADOW_STACK to track shadow stack VMAs. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v3: - Drop arch specific change in arch_vma_name(). The memory can show as anonymous (Kirill) - Change CONFIG_ARCH_HAS_SHADOW_STACK to CONFIG_X86_USER_SHADOW_STACK in show_smap_vma_flags() (Boris) Documentation/filesystems/proc.rst | 1 + fs/proc/task_mmu.c | 3 +++ include/linux/mm.h | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index e224b6d5b642..115843e8cce3 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -564,6 +564,7 @@ encoded manner. The codes are the following: mt arm64 MTE allocation tags are enabled um userfaultfd missing tracking uw userfaultfd wr-protect tracking + ss shadow stack page == ======================================= Note that there is no guarantee that every flag and associated mnemonic will diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index e35a0398db63..982126ffdbae 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -711,6 +711,9 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ +#ifdef CONFIG_X86_USER_SHADOW_STACK + [ilog2(VM_SHADOW_STACK)] = "ss", +#endif }; size_t i; diff --git a/include/linux/mm.h b/include/linux/mm.h index 82a9a4903651..824e730b21af 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -315,11 +315,13 @@ extern unsigned int kobjsize(const void *objp); #define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_5 37 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0) #define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1) #define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2) #define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) #define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4) +#define VM_HIGH_ARCH_5 BIT(VM_HIGH_ARCH_BIT_5) #endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #ifdef CONFIG_ARCH_HAS_PKEYS @@ -335,6 +337,12 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ +#ifdef CONFIG_X86_USER_SHADOW_STACK +# define VM_SHADOW_STACK VM_HIGH_ARCH_5 +#else +# define VM_SHADOW_STACK VM_NONE +#endif + #if defined(CONFIG_X86) # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */ #elif defined(CONFIG_PPC) From patchwork Thu Jan 19 21:22:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108794 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 B4211C6379F for ; Thu, 19 Jan 2023 21:23:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26A238E0002; Thu, 19 Jan 2023 16:23:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CF938E0001; Thu, 19 Jan 2023 16:23:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDAB88E0002; Thu, 19 Jan 2023 16:23:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C57768E0001 for ; Thu, 19 Jan 2023 16:23:53 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8E6C9120DFE for ; Thu, 19 Jan 2023 21:23:53 +0000 (UTC) X-FDA: 80372825946.08.6D36503 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 67388180019 for ; Thu, 19 Jan 2023 21:23:51 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="SlJB/YHi"; 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=1674163431; 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=LjZx3/OxatauvlHEC9jJBaxJxgdku9VSDjOhpZOLExo=; b=rxoZCBB9xJlYn0EQKZ+BSwQ1L4s/cM9kYtIdxiVnroSu/2t3NVWG4kXs3yBnqZQJunxaeT kJplU0zJWxI8oW5IZADOaqL5I7eraGysdtvnpzpM0ecs+GBRdeZpa7Y35xo63kRzKba12i rhQzP1uNGxNzNWx/XFvIoW3wNlL60So= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="SlJB/YHi"; 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=1674163431; a=rsa-sha256; cv=none; b=Czk0PcpqlrRN86+71mrsMf7UE/Ma58AjvcnN3bPx6AY9mJhWrh0UC3hOzVn0xpXe5cei1G y95cKN8eVQcdWYaM3zQQVspAJW186d3r14VDzJBSvrXoQNa04UPUVi8yuaApdBWc0Fz3Vj Ao4C64An72AoUOiw2rbjS0OMiyAPPC0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163431; x=1705699431; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=W0uo/pMYztKK7cUO1RHzIUdsd0CMMqFeZAfln1MTMss=; b=SlJB/YHitVhbEDbmCuYk4n/fO5q6q2iv0Mtbdzy6rD7PmZf+aPxNwCNO Iaqx6gwj1Waa/dJYPG1AC5Y3XhN6vl93g9WTWi2IXt8dSVxklddIn/di9 1SGMv+1oMtite9nk5TLUTPavNkkubCn6DLwOQP3LXr8VKWpNFSqN+TdPp mIYS7PCxjA8/ZXVrZJ5PnywbGHg7AxAG7i1KaXRR9j/KDGpDV2lu4y+H9 +TbQlACtV0d12OH2r8h9sM73dKVEHSF64Rl3QljNw2g9VMqx+UwkVv9ml wFdqILAoFBfKILa0/gBfmtZ6cBMMBEeuIvItcdFW39woQygtLVuapjRnd Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119528" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119528" 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:23:50 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139070" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139070" 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:23:49 -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, Yu-cheng Yu Subject: [PATCH v5 16/39] x86/mm: Check shadow stack page fault errors Date: Thu, 19 Jan 2023 13:22:54 -0800 Message-Id: <20230119212317.8324-17-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: 67388180019 X-Stat-Signature: tyhu7zo5ond7gy6yf7699k44de38pcgr X-Rspam-User: X-HE-Tag: 1674163431-936513 X-HE-Meta: U2FsdGVkX1+2ucrQqCBpgHp01+c21Vh9KU0N302t0t+df1ADKY4Lz0Y2cIlfhksC8iy/q9TbGVdZNacwO7N7SRXfSewut73/os4XJyO4AW0ugIngCsaj0gEcpYqqEBnriuAqHB53iX/owEvBi2KHcHgdCrbRfQRrtiHrcfdGTuEppenc1T1C8m36ION9G5dZAVV+v3JqUSCTcGEK8jwe5br0aoxo0G/G3id+OHRPuXhIrXHC3CMVeGc1feJT1HfX81dkSYonBkwuImOsrmJNUrfnOnMOeTT2wMsJLom0ZtxfFLaV2XkmDf5lqAcst5NS7uNbmmPO78owZ17YJEPvkiAksfrtmcXgiaWkJvy6H6RUddjEa7HppG52jUgMVb2UTQzj+eoGDOrNdYaxcMdCHOxSOC5g7PFqmbuG01tdovgBNpf+NrH8Sm8lpuSk7x7KApu6SdYBPAyEcGKsfnf9CMGZiERqKeWanSy4TnGjsXZrhYjAmdXZPkvyBMRRWVrw0Xn9CzpiCxr9gxrfurpmJq9XMWHCgWNCL0Ozpc2jdpcrBMVv3tlu7dubjaN7/C0G5HR2rB/TsEd4L9h0wKr9x63BTDYvkFBWYZTtrlQXJxpyDFZ4KQRaljsWwFftLLvLU5N1T/ZVQ7ecNRLLZihhoJWrXqpNg77ezglP09WEEy3v0UWOZhMwjHE22jJWp93YdIeSD2Jhdgx/EKzAgNb1jHvtWj6IhvXmReVy0yBXJlW9Zw7G2VYidWZ1/RAJVd8Qwpx7GrkAv85hW/GW8U+wTVkXKTVMW3a1jcpIovwt4iNThzjszs/JSRMoJv1NABZv8LCvKTdEGjQoZG0gIQfY2sK2tv+hfUTdUJUl6g5Vbtv9H0jeJnFWFpQlQDHdbRplYhVnHG9HQFYtG5J7P8SfPMonb5rWuxt6Cf8LTry5GT6pbRJuTRRzp0OG4bkP3pMelViA3468YovV33lyZ+w J2vM6cMu tsmP0lPPszuR6r1xkrkOMJtudEe1tzRZvQIPTAyIFG+2b7dmbh2KepgayAQsNDvE4s7W/EpvxNbP1+mezS53ZW6xfARH8qPMDhgVarA94ah1CoQiB4rkuwWFjamQELowxGQbTBIn3WT37YKWbBeOkMwAEiHCseR5A46ZtyvDPCCU4/olY9iZqXDr8e7OC0W64t0dJZNl9rqhXLpUWvLLGnzVcZkJ0W+Ydb+1yR1bnkN5ztdo= 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: Yu-cheng Yu The CPU performs "shadow stack accesses" when it expects to encounter shadow stack mappings. These accesses can be implicit (via CALL/RET instructions) or explicit (instructions like WRSS). Shadow stack accesses to shadow-stack mappings can result in faults in normal, valid operation just like regular accesses to regular mappings. Shadow stacks need some of the same features like delayed allocation, swap and copy-on-write. The kernel needs to use faults to implement those features. The architecture has concepts of both shadow stack reads and shadow stack writes. Any shadow stack access to non-shadow stack memory will generate a fault with the shadow stack error code bit set. This means that, unlike normal write protection, the fault handler needs to create a type of memory that can be written to (with instructions that generate shadow stack writes), even to fulfill a read access. So in the case of COW memory, the COW needs to take place even with a shadow stack read. Otherwise the page will be left (shadow stack) writable in userspace. So to trigger the appropriate behavior, set FAULT_FLAG_WRITE for shadow stack accesses, even if the access was a shadow stack read. For the purpose of making this clearer, consider the following example. If a process has a shadow stack, and forks, the shadow stack PTEs will become read-only due to COW. If the CPU in one process performs a shadow stack read access to the shadow stack, for example executing a RET and causing the CPU to read the shadow stack copy of the return address, then in order for the fault to be resolved the PTE will need to be set with shadow stack permissions. But then the memory would be changeable from userspace (from CALL, RET, WRSS, etc). So this scenario needs to trigger COW, otherwise the shared page would be changeable from both processes. Shadow stack accesses can also result in errors, such as when a shadow stack overflows, or if a shadow stack access occurs to a non-shadow-stack mapping. Also, generate the errors for invalid shadow stack accesses. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Add description of COW example (Boris) - Replace "permissioned" (Boris) - Remove capitalization of shadow stack (Boris) v4: - Further improve comment talking about FAULT_FLAG_WRITE (Peterz) v3: - Improve comment talking about using FAULT_FLAG_WRITE (Peterz) v2: - Update commit log with verbiage/feedback from Dave Hansen - Clarify reasoning for FAULT_FLAG_WRITE for all shadow stack accesses - Update comments with some verbiage from Dave Hansen arch/x86/include/asm/trap_pf.h | 2 ++ arch/x86/mm/fault.c | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..afa524325e55 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -11,6 +11,7 @@ * bit 3 == 1: use of reserved bit detected * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access + * bit 6 == 1: shadow stack access fault * bit 15 == 1: SGX MMU page-fault */ enum x86_pf_error_code { @@ -20,6 +21,7 @@ enum x86_pf_error_code { X86_PF_RSVD = 1 << 3, X86_PF_INSTR = 1 << 4, X86_PF_PK = 1 << 5, + X86_PF_SHSTK = 1 << 6, X86_PF_SGX = 1 << 15, }; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 7b0d4ab894c8..070b50c87415 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1138,8 +1138,22 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Shadow stack accesses (PF_SHSTK=1) are only permitted to + * shadow stack VMAs. All other accesses result in an error. + */ + if (error_code & X86_PF_SHSTK) { + if (unlikely(!(vma->vm_flags & VM_SHADOW_STACK))) + return 1; + if (unlikely(!(vma->vm_flags & VM_WRITE))) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ + if (unlikely(vma->vm_flags & VM_SHADOW_STACK)) + return 1; if (unlikely(!(vma->vm_flags & VM_WRITE))) return 1; return 0; @@ -1331,6 +1345,30 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * When a page becomes COW it changes from a shadow stack permission + * page (Write=0,Dirty=1) to (Write=0,Dirty=0,CoW=1), which is simply + * read-only to the CPU. When shadow stack is enabled, a RET would + * normally pop the shadow stack by reading it with a "shadow stack + * read" access. However, in the COW case the shadow stack memory does + * not have shadow stack permissions, it is read-only. So it will + * generate a fault. + * + * For conventionally writable pages, a read can be serviced with a + * read only PTE, and COW would not have to happen. But for shadow + * stack, there isn't the concept of read-only shadow stack memory. + * If it is shadow stack permission, it can be modified via CALL and + * RET instructions. So COW needs to happen before any memory can be + * mapped with shadow stack permissions. + * + * Shadow stack accesses (read or write) need to be serviced with + * shadow stack permission memory, so in the case of a shadow stack + * read access, treat it as a WRITE fault so both COW will happen and + * the write fault path will tickle maybe_mkwrite() and map the memory + * shadow stack. + */ + if (error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_INSTR) From patchwork Thu Jan 19 21:22:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108795 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 78A4CC004D4 for ; Thu, 19 Jan 2023 21:23:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE30A8E0001; Thu, 19 Jan 2023 16:23:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B415B900002; Thu, 19 Jan 2023 16:23:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91F078E0003; Thu, 19 Jan 2023 16:23:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6FD328E0001 for ; Thu, 19 Jan 2023 16:23:55 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3FDA916099F for ; Thu, 19 Jan 2023 21:23:55 +0000 (UTC) X-FDA: 80372826030.04.924A21C Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id ED8BF40010 for ; Thu, 19 Jan 2023 21:23:52 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=axh1SzDZ; spf=pass (imf27.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=1674163433; 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=F+8Tf9HdQyQPAdeGQwF3qLXqysHCxD0/3GwiYQ1Fj7g=; b=ykpnI1wLFbXe9dYrg38o9Wj1/75ePCAH2l90wy3P2AwYMBiQAU7BAOZUxalCn5Sy8KokWE T7//EXXJnkd9Sbt7eJZ6tBRATJnAqZnMdPNaaIUOGQlYkcNXr0xKvHh+8GtGuqtu9ewhIh Jaa5qP+IyTaF8eLRUd95fW7fG8ukpeo= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=axh1SzDZ; spf=pass (imf27.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=1674163433; a=rsa-sha256; cv=none; b=4IsC9b/YZWGjKi+u82ece6w+oXmZ0bp84FujuBo3Ahbcnt8fu2G2lTBq9MX2Lh5U+b86n9 T03Pb2ydI1gOUuMSXrjgQjDxgiCOi6NG3nyyMthFSkYs/+0uHmmkZhbd77yWaVWRETg8ev 1hAjfK80wWxAZf812oqDFm2ZkQ332pQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163433; x=1705699433; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=M7Xo1HXic3OeNqgtbqqoeSfgPr5lKe9tQj+QswBwq38=; b=axh1SzDZl3xgqDqDer6PEDEcdr7nIlc4mcTGYKlDwW1/fSXe6BenJrGs l+zSphlNKRbQZ4dQXvuX/3LwKjvUwWzqN5jEMrDWMmAqlhjANqmfWZcvx kyfhEzFT6KTmlILEx2r8rEdTLKKDpyXM/y/nDw51iR7Y7a7exsbEckGwa tobBj77dgmHPp+iKPGaN2K7M6XNoIKl8acl+vA1R5re4Crye20BlkUBRS V0MFPPGGklRLyhCFL1svzVYN2ZLqAabmwG2CmjPk05GheU54l0EyIq5wF +XN0m35CnmQA6pXvvTPQBIJyhfJ+KVyqyzzpFQXlD5Llq8XXppHFe8jIZ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119557" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119557" 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:23:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139076" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139076" 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:23:50 -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, Yu-cheng Yu Subject: [PATCH v5 17/39] x86/mm: Update maybe_mkwrite() for shadow stack Date: Thu, 19 Jan 2023 13:22:55 -0800 Message-Id: <20230119212317.8324-18-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: ED8BF40010 X-Stat-Signature: zitk1w1gfttqj8pnrwx33tq3wgacq66e X-Rspam-User: X-HE-Tag: 1674163432-988933 X-HE-Meta: U2FsdGVkX1/Tp2TurAlPO1LhexA9cr1UwLlQqmz2X248XYgzaVFplZ9DE0OKX43j6CUutHhpgH1R5TTi9o54rpkmM7Gtxvvzr6C6939vtRykL0IGl74MwQyU9TrCRZ0Nvrp6SYfNDPAIZaz/yLNjUNLXoZ/gjY3O9UgnoQ0NzO+QeyEnt6fbrr3sNM7F/RW7xryk0qApxBHqK/DcjQE4lqykRuUWXyUqVb/CEU6tPpO4n0Sgea7w8y74+k274/1ncbeVfA0vjw8z74CsuMRYugoEYrc4+n88UhfFmYEdJLgqYbykhoITVlncYvGkNACpa4qdnjpC8qh4gRQBb+HtjR7bx5tBWGuhpzhVsnZ3BF+IuMqy2XVR6vyi+wosm1QlfPb9G8+Xr0dDrWtmauhgUABttshDmete4iFGAeJyKIvAAZL2AhAw9/l1PH9ORYjENp65f6me5ze+wr/RIhs+oD7+nAWURjVvxS7/gm6Bkg3lEyqUBN2ZKA+JiXNtbiO76Wx0LNaCKAtB4v9XcdBFRo3lY+ItRiikXejoj8tTRGjW0jRNz/bE59c/ETZjKtQELHUIp9izBMyvietiL3FsO6r5uzxyV5cCGTDRhE9CYDzLRftVxERI7vSA1avyp0I/KKirVYHoPNGDCLCOqsfdiF/mB6dSxROzpCrBkBe8+SOsAROLpOHLjt6tnWSxwFedPIVj+5y3ij5EmFzmUnNl6Rcs1j+CmfRojlayv/ygVIEjvUmGNKciFAgbXa3DqMkSlBYO8LGGiz0/LDwg+gbcEmWYJL96cjNfyOxe7Ixw+4wbO3f2/q/VYTRS9CKEEXV6eRJ5aNms0NTKCft+fa4oOiP+HrCtKgqyGQaccRynf5e/lKMS5MQYJYoiOPjL50wUGLTdtkQUGZ4br5pDVIwF4uYYrqOzEZisJdXYqhM2N6y1luvjj9d1ZBaxOS7ANU4c3O8XS27NZC/w10fmB7q IbNtflxX fMJd2puzrbYaeUKLK5fxGuTzwkybFZKZlmm2SAZXp1QHBzdeZ/hhjl65oXqJC5eu03dPYC3ycC+1pywp43QhVcAQbFsc2PDbYqo0J1B6Mzps5DZmwJ1OcTAT9Yxpbq6VheF7LONRS+aLX/XF7tvkXfcvjpS9PPXms0MdFvXWeyXy8l5WC+5VGxUS3pt9DwOLj0gMaaGYF0cWkDcFkwt3D6YThTo4rbk/Bj8NyFa57OF/c/j4rrEstQELG2qwYpJpKLAkPskHD+h0A0LW5/07xL+VEdA2Mm5o6uV4DEWdXX3sJP3sFu7j6rDMcgScILdddHbxucvQP8Sj4XIoHb029f3u9nQ== 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: Yu-cheng Yu When serving a page fault, maybe_mkwrite() makes a PTE writable if there is a write access to it, and its vma has VM_WRITE. Shadow stack accesses to shadow stack vma's are also treated as write accesses by the fault handler. This is because setting shadow stack memory makes it writable via some instructions, so COW has to happen even for shadow stack reads. So maybe_mkwrite() should continue to set VM_WRITE vma's as normally writable, but also set VM_WRITE|VM_SHADOW_STACK vma's as shadow stack. Do this by adding a pte_mkwrite_shstk() and a cross-arch stub. Check for VM_SHADOW_STACK in maybe_mkwrite() and call pte_mkwrite_shstk() accordingly. Apply the same changes to maybe_pmd_mkwrite(). Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v3: - Remove unneeded define for maybe_mkwrite (Peterz) - Switch to cleaner version of maybe_mkwrite() (Peterz) v2: - Change to handle shadow stacks that are VM_WRITE|VM_SHADOW_STACK - Ditch arch specific maybe_mkwrite(), and make the code generic - Move do_anonymous_page() to next patch (Kirill) Yu-cheng v29: - Remove likely()'s. arch/x86/include/asm/pgtable.h | 2 ++ include/linux/mm.h | 13 ++++++++++--- include/linux/pgtable.h | 14 ++++++++++++++ mm/huge_memory.c | 10 +++++++--- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index e96558abc8ec..45b1a8f058fe 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -445,6 +445,7 @@ static inline pte_t pte_mkdirty(pte_t pte) return __pte_mkdirty(pte, true); } +#define pte_mkwrite_shstk pte_mkwrite_shstk static inline pte_t pte_mkwrite_shstk(pte_t pte) { /* pte_clear_cow() also sets Dirty=1 */ @@ -589,6 +590,7 @@ static inline pmd_t pmd_mkdirty(pmd_t pmd) return __pmd_mkdirty(pmd, true); } +#define pmd_mkwrite_shstk pmd_mkwrite_shstk static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd) { return pmd_clear_cow(pmd); diff --git a/include/linux/mm.h b/include/linux/mm.h index 824e730b21af..e15d2fc04007 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1106,12 +1106,19 @@ void free_compound_page(struct page *page); * servicing faults for write access. In the normal case, do always want * pte_mkwrite. But get_user_pages can cause write faults for mappings * that do not have writing enabled, when used by access_process_vm. + * + * If a vma is shadow stack (a type of writable memory), mark the pte shadow + * stack. */ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) { - if (likely(vma->vm_flags & VM_WRITE)) - pte = pte_mkwrite(pte); - return pte; + if (!(vma->vm_flags & VM_WRITE)) + return pte; + + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite(pte); } vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page); diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1159b25b0542..14a820a45a37 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -532,6 +532,20 @@ static inline pte_t pte_sw_mkyoung(pte_t pte) #define pte_sw_mkyoung pte_sw_mkyoung #endif +#ifndef pte_mkwrite_shstk +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + return pte; +} +#endif + +#ifndef pmd_mkwrite_shstk +static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd) +{ + return pmd; +} +#endif + #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT #ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline void pmdp_set_wrprotect(struct mm_struct *mm, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index abe6cfd92ffa..fbb8beb9265e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -553,9 +553,13 @@ __setup("transparent_hugepage=", setup_transparent_hugepage); pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { - if (likely(vma->vm_flags & VM_WRITE)) - pmd = pmd_mkwrite(pmd); - return pmd; + if (!(vma->vm_flags & VM_WRITE)) + return pmd; + + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite(pmd); } #ifdef CONFIG_MEMCG From patchwork Thu Jan 19 21:22:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108796 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 0F469C46467 for ; Thu, 19 Jan 2023 21:23:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F116900003; Thu, 19 Jan 2023 16:23:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37A4E900002; Thu, 19 Jan 2023 16:23:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CC40900003; Thu, 19 Jan 2023 16:23:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 02D19900002 for ; Thu, 19 Jan 2023 16:23:57 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CDB8C12040C for ; Thu, 19 Jan 2023 21:23:56 +0000 (UTC) X-FDA: 80372826072.16.DC2AECF Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id C9E0D180016 for ; Thu, 19 Jan 2023 21:23:54 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gGM0ifSG; 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=1674163435; 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=cL1+A7sEmRT22WV4GTGRusW0jLfRlihJHMYsiIHNplM=; b=FSTmOgYvdSwheqbS/0lNGSpmOLHKwcr7N7FzP+2FAWbM6L0+CQIZppzrjPmTu+VkJ7R3Lv 7O23qQ2CkRvA2fwUerMnqBZwJqzgYYFcqAyH+N9cVzabZRduGbKAi1Ik+v9qpVim+fPcW+ S0aILrqIGWcW4kAH6B6YBhyf/ulsX04= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gGM0ifSG; 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=1674163435; a=rsa-sha256; cv=none; b=K4ttf4YG1NRyUdW+aWXiidjDwDmTbut9msgWhQocIfTG42R31TkAnyOonnS24VB7lpHOL8 p77KEBwZLCQvPvhwZX12WRuMKARbrXtq4joYqa+D/a7LecFQ6hPphRyDfcV1fLXgIOGtCt fw2LT4x04paBU0MHpcPsQdAhL14fvj4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163434; x=1705699434; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=NJhAPcbhUYo6SN1rH0mb7F3rAzE882GPxl5rAvSEEz0=; b=gGM0ifSGeDrGrb8XwFdzdKI6mtwMt/YCTxjuVOiaArQ3OwekNnZDGH7W vwphd1Je9fcWqhIGrKMT8f54uFNgPmACduHsHg7UjiPmiMxg9LBrfPqvf g/j3defe9T+0I16qBIa+MEGVRX/tKi4btiFCY4acaa7o4BigLOxAh7USF w9DRjXFTabJ0Msh9C4dYT+QAIRI4bzftlWeEhS2fIk+L5nRvK3pjlVp0l nfoLIAcD4W/PzzWzyc80CdI0RiPWxaZ1dbyAhB8Vr9r5REPmu1auyXRNN uMwjeYI/LVk+VuL2fovVejoZJuw2fy049EdA+Q6FI67A7egC1tSSZqOKs w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119585" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119585" 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:23:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139082" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139082" 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:23:52 -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, David Hildenbrand , Yu-cheng Yu Subject: [PATCH v5 18/39] mm: Handle faultless write upgrades for shstk Date: Thu, 19 Jan 2023 13:22:56 -0800 Message-Id: <20230119212317.8324-19-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: C9E0D180016 X-Stat-Signature: 8cgg1zxhgxxroywqxa6yutxfqmak53es X-Rspam-User: X-HE-Tag: 1674163434-857141 X-HE-Meta: U2FsdGVkX19PPFHGQjr5ZEyOoumsNmwqNbrVRxwwRDbyF+55YbXiybaTI2ny1Z+1jQJjfPlnQxHYsIjvk1dvmwYnAD1APKSNMxAG79iVQ6fxObCgjVa8kugevLIpP2Z2BYbRW3IktWapj0ZGEQ3gVanMNmsnfj2BVfXS4GdDAovHUf51FFq0DqicqWzKJNFcBSVRgdvQPuLmBY6kZB7qhqHalK7wv1JXv5nS4vwTmpJnl3icfZ09WbsZzl6BtI2R//Jm8u6VB76FfGzwg5hH83vTDF4LRyWYSIW3X5jnE1hmeCW7gWd0LVQkPpwuAHriklgl/l1scpEkd3t/39ufN0ectXIOk4rfNxolKflGETkXRwvk1XEv5zT/TJbBCtJkDQxkf9IIoSveJL1x+gH8YTLIyWLvKcgYoaHEDopB+9OEH+bFtHPZWF5tq2jZGWqQ3gyIZMcP2wgZnh27XeLDU8ctzE6X4bXRYgr3fuhlpxXsFd4MCCxAfTiodS0OSw2/V4KxAl0s5V9n6S9ciobFmc+vQrnre2jyk83uH8GIEnoBQBL5toNeSCgLMQcqMyHpR8/nWm2ENraaN0jyNkX+R5R8Ej/30WS2fF9CmA5ULAXHNsQTV63+KG5TjrEXV9Z6iLP9KNiLusIKK8+4eu1VWEkUoDdjNbRqHS85LlNIkUc1K+xbPV4Ywyo+MhKPOHxvvCiqMXn0NzzkjXs95Gsg8AihmR2PipFlT6z052TrNINo1TCVlAHyrrszyzyXloNLK77vx6YuCiyRKQa8sZjXM5hYfsuVTYjYXZIqw4a4ECt/1wVnloNP4Xg8MjFDqm3wGc7wgpXn6QWcHHNGLIBf121E1bCz3Ar/4KndCijvE+N66OgsQ05VXNY5RejVDYfs2wbYHbSU6dMdlUTJPCPWIHreqkraA7JzI29zYq6fmR7hAt26yODfvo7CyxcQPQEyu676N+EB560LdXZp34r K9/5Rrei ZvQMZRFwtzD2OTgXuTVutA07UGQhAmMoaLjl3ToBn41184oseaUCd8C9rm1PFBwnlxEI84vRHzTU0EyetAk4YqKskyjmuzY+w//bHR0xkwOgLISCDoh/W5VxjXnhfGwaK/T3ZB3gesz9OIEH25zz0BxjNpgi8MiUkO9MSHeGY4/N+3utCysd6b4akDh7Z0zqDBJgphs0HP2q3UwjZROu3nwUHqBOCkaFHiQBSiyzRgwK1KOcqu2yKnzS3m60K+qv2vSNu 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: The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. Since shadow stack memory can be changed from userspace, is both VM_SHADOW_STACK and VM_WRITE. But it should not be made conventionally writable (i.e. pte_mkwrite()). So some code that calls pte_mkwrite() needs to be adjusted. One such case is when memory is made writable without an actual write fault. This happens in some mprotect operations, and also prot_numa faults. In both cases code checks whether it should be made (conventionally) writable by calling vma_wants_manual_pte_write_upgrade(). One way to fix this would be have code actually check if memory is also VM_SHADOW_STACK and in that case call pte_mkwrite_shstk(). But since most memory won't be shadow stack, just have simpler logic and skip this optimization by changing vma_wants_manual_pte_write_upgrade() to not return true for VM_SHADOW_STACK_MEMORY. This will simply handle all cases of this type. Cc: David Hildenbrand Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- v5: - Update solution after the recent removal of pte_savedwrite() v4: - Add "why" to comments in code (Peterz) Yu-cheng v25: - Move is_shadow_stack_mapping() to a separate line. Yu-cheng v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). include/linux/mm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e15d2fc04007..139a682d243b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2181,7 +2181,7 @@ static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma */ if (vma->vm_flags & VM_SHARED) return vma_wants_writenotify(vma, vma->vm_page_prot); - return !!(vma->vm_flags & VM_WRITE); + return (vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHADOW_STACK); } bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, From patchwork Thu Jan 19 21:22:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108797 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 96250C6379F for ; Thu, 19 Jan 2023 21:24:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B7DD900004; Thu, 19 Jan 2023 16:23:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 243EE900002; Thu, 19 Jan 2023 16:23:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB04900004; Thu, 19 Jan 2023 16:23:58 -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 D1F7D900002 for ; Thu, 19 Jan 2023 16:23:58 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B0949806E1 for ; Thu, 19 Jan 2023 21:23:58 +0000 (UTC) X-FDA: 80372826156.02.4420006 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 9105F40003 for ; Thu, 19 Jan 2023 21:23:56 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XSnZIMk+; spf=pass (imf27.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=1674163436; 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=1+7FX9/q+7niILOmZEQ+FO8zDLlVkBM+CikBvkIlWYA=; b=dFqR0U1pxGPDAmbIxYb9bj5lz+Hcxslj/1D56nagRHgBitm9GxJJjSKh9FGNKDo+aVreRN RrDKu/KUwxKrGjHpepFUrUID3Mz85/KsuD+MpJ5sdfFGaPSlYq9KN2/ByYcRDIVjToeq+l KB0Gy4iOHqSU2/0pc6O8NBRH5tzT2fs= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XSnZIMk+; spf=pass (imf27.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=1674163436; a=rsa-sha256; cv=none; b=BHCS9OmSejlpJaozjjoLujLvYVZcefWf7u8J/PnuthrLBsWDyqsA5r7KEBEjDFQq4SvtoG JrQnrnJyEVPasqhxfkpfOw/aBSE2Se73khyQuw/E9XcfuzdAdeFLZ78RekulbaFtg4h1pZ zhF4ybvP5i4/2BJo1VF80piMi67H9I8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163436; x=1705699436; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=mm+/ZDL+/WIl4y++bICVeaKn86uLCgMEEZawTB+3rKc=; b=XSnZIMk+XJtnpyXwzM63EH1HnBvLMLN2TNWAM7dhjX7+k2dS198Dk2Tl Fp8cIoRuDCAa4HZ6teh0jbIXbhBC4pLUBkIN1J+04QUVum4T9iZ1wnp2M Vbo2FZSnUKbULiRAnHYWW+sZALs13umuHl8PL/ldtuRkofIWBxLO/IrlP 4TSpWD5vDNyiVFJb4ZSU13+zkfA3TPNLDkWKk4WkL++rssCoQI9tO3m1/ 1GgeJ3hvKSoZEb/Ji8jy1yAbLmPSbrx+DtY5NMK5rPpngP6fgQ3GNLHNu 2OhJqIzBPEupV17knihGatO9+1NwSoaTm+FvYOxZGK7Raf7I38dOjmLVL A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119613" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119613" 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:23:55 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139087" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139087" 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:23:53 -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, Yu-cheng Yu Subject: [PATCH v5 19/39] mm: Fixup places that call pte_mkwrite() directly Date: Thu, 19 Jan 2023 13:22:57 -0800 Message-Id: <20230119212317.8324-20-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: 9105F40003 X-Stat-Signature: 1i5xqss1igmjgsu4u9f6e8xa9s74i5op X-Rspam-User: X-HE-Tag: 1674163436-324045 X-HE-Meta: U2FsdGVkX1+510ZZdMHum+IjaE6ZhpFAkYtrOtJPombrZYalEWf64Ekrg+8+iVzdosTSJTHdsKvBnlhipOH+F6yzQo4AuELZO021tlYcIyNCPMmiD0PymkDU5Cbm9M0bblaEsoXug+qR+qQlAqEqB0lTsp8T4lfSsAHuKnZXyUoEsGFDQ7wonaIEjWGIduvHQJcvi5X27+bg9CljfHsGBzN2AjM63FQn+eoWa095yMP2syg0fU/tvbEWMeeylOgKT4sLx4LwOCZuo5lJRdsjwBrwNXTjCw7/yts29sr+atr/X0jIu0JX9rvfr3UuZMn7bRd7/UvwEaJ3e1+nFiaK3lfYpls9QOehHKAtO7v1JBRZKLzNWJpcp9GAC+6SXmYyhH4DTUw/K8ZmDnITio8/MEPkOGXeZwlwGp4shLxeVryK74RrjnyB1xAkIQqNMPzQx2o3YydzIS9wu75fA47sMk6QAVtmvsp0Tb7vlMzvD81nnWJxODeCx1vaJvijwKupKPkwCBmDLzRFi5PZ8TTPGYpnBhXJ6ZVrIXdQKRDLdL6b70BFc9rZY20+0C+bFlYPuSXdJzY1XrncbsKisYPXkx45/wQEHyHA1hyeS2StOEFKjWcrefl/6UgBRsmE4JWfjfex5XT7erEgb5l5t4imoCGU7ACzMgzZDku1Z+rCAVM2lCFPkqNF9JRKXBaQ1CPk4ddSMT8x8QhJQUQj0SRNtIAviAHn/Tysj9Vmbe4rCLXr/uVKz9eN/9xUVvb/sgFic4bDIHbN98EjRt9e9l5WqEsBvp01Q3K0UexLBe67maLQl50BFRHT0AjS5NOgYSiOZrrD5ZkUv7Tud8Cf8evFhtDkKKRoogmndI8zBYSzxRaoRhrKGK0oylULVnWjD1/znzzIoEOwDQk9eaFZOZjvlCz/PsvgyU4p30Qp9H7/1qStw81gdQrEvl2s4cuSitjiaoQjunvAfKpxk+R5nOc 892bmXoU C16a0Ay6TU6/CwEzdRUGdrsoSNYpmWSZBnjKvK4nXuA+0/hMSi0QpDW5opR0rWAJC2SXIp92zdY3nZs186ixhC7YPDDufmnEMah/VvPkm7X+T9gZin5rAhOD+r+TWAjt7EAacQr6cBx2EyI5Wy3qd7SAMFQCWWCRIkt/O6/nS7J4UAhG5nyAdWjZGgZqqYyAihoAp0GqfNVszz0hyovw1VFo7Uf63p2wQgAUAMiw7yKiaRGAM+hueMXnVoXlbQDCGuQADwFMLeVI1TJQ4WN9eMsj+d0Upwcde/nFbMigc/R03oDXaIkj2QfYOR0X1lFW8qEJwlIW+LLinwynD6vfIBa1NUA== 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: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. With the introduction of shadow stack memory there are two ways a pte can be writable: regular writable memory and shadow stack memory. In past patches, maybe_mkwrite() has been updated to apply pte_mkwrite() or pte_mkwrite_shstk() depending on the VMA flag. This covers most cases where a PTE is made writable. However, there are places where pte_mkwrite() is called directly and the logic should now also create a shadow stack PTE in the case of a shadow stack VMA. - do_anonymous_page() and migrate_vma_insert_page() check VM_WRITE directly and call pte_mkwrite(). Teach it about pte_mkwrite_shstk() - When userfaultfd is creating a PTE after userspace handles the fault it calls pte_mkwrite() directly. Teach it about pte_mkwrite_shstk() To make the code cleaner, introduce is_shstk_write() which simplifies checking for VM_WRITE | VM_SHADOW_STACK together. In other cases where pte_mkwrite() is called directly, the VMA will not be VM_SHADOW_STACK, and so shadow stack memory should not be created. - In the case of pte_savedwrite(), shadow stack VMA's are excluded. - In the case of the "dirty_accountable" optimization in mprotect(), shadow stack VMA's won't be VM_SHARED, so it is not necessary. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Fix typo in commit log v3: - Restore do_anonymous_page() that accidetally moved commits (Kirill) - Open code maybe_mkwrite() cases from v2, so the behavior doesn't change to mark that non-writable PTEs dirty. (Nadav) v2: - Updated commit log with comment's from Dave Hansen - Dave also suggested (I understood) to maybe tweak vm_get_page_prot() to avoid having to call maybe_mkwrite(). After playing around with this I opted to *not* do this. Shadow stack memory memory is effectively writable, so having the default permissions be writable ended up mapping the zero page as writable and other surprises. So creating shadow stack memory needs to be done with manual logic like pte_mkwrite(). - Drop change in change_pte_range() because it couldn't actually trigger for shadow stack VMAs. - Clarify reasoning for skipped cases of pte_mkwrite(). Yu-cheng v25: - Apply same changes to do_huge_pmd_numa_page() as to do_numa_page(). arch/x86/include/asm/pgtable.h | 3 +++ arch/x86/mm/pgtable.c | 6 ++++++ include/linux/pgtable.h | 7 +++++++ mm/memory.c | 5 ++++- mm/migrate_device.c | 4 +++- mm/userfaultfd.c | 10 +++++++--- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 45b1a8f058fe..87d3068734ec 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -951,6 +951,9 @@ static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) } #endif /* CONFIG_PAGE_TABLE_ISOLATION */ +#define is_shstk_write is_shstk_write +extern bool is_shstk_write(unsigned long vm_flags); + #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e4f499eb0f29..d103945ba502 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -880,3 +880,9 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) #endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +bool is_shstk_write(unsigned long vm_flags) +{ + return (vm_flags & (VM_SHADOW_STACK | VM_WRITE)) == + (VM_SHADOW_STACK | VM_WRITE); +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 14a820a45a37..49ce1f055242 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1578,6 +1578,13 @@ static inline bool arch_has_pfn_modify_check(void) } #endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */ +#ifndef is_shstk_write +static inline bool is_shstk_write(unsigned long vm_flags) +{ + return false; +} +#endif + /* * Architecture PAGE_KERNEL_* fallbacks * diff --git a/mm/memory.c b/mm/memory.c index aad226daf41b..5e5107232a26 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4088,7 +4088,10 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) entry = mk_pte(page, vma->vm_page_prot); entry = pte_sw_mkyoung(entry); - if (vma->vm_flags & VM_WRITE) + + if (is_shstk_write(vma->vm_flags)) + entry = pte_mkwrite_shstk(pte_mkdirty(entry)); + else if (vma->vm_flags & VM_WRITE) entry = pte_mkwrite(pte_mkdirty(entry)); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 721b2365dbca..53d417683e01 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -645,7 +645,9 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, goto abort; } entry = mk_pte(page, vma->vm_page_prot); - if (vma->vm_flags & VM_WRITE) + if (is_shstk_write(vma->vm_flags)) + entry = pte_mkwrite_shstk(pte_mkdirty(entry)); + else if (vma->vm_flags & VM_WRITE) entry = pte_mkwrite(pte_mkdirty(entry)); } diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 0499907b6f1a..832f0250ca61 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -63,6 +63,7 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, int ret; pte_t _dst_pte, *dst_pte; bool writable = dst_vma->vm_flags & VM_WRITE; + bool shstk = dst_vma->vm_flags & VM_SHADOW_STACK; bool vm_shared = dst_vma->vm_flags & VM_SHARED; bool page_in_cache = page_mapping(page); spinlock_t *ptl; @@ -84,9 +85,12 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, writable = false; } - if (writable) - _dst_pte = pte_mkwrite(_dst_pte); - else + if (writable) { + if (shstk) + _dst_pte = pte_mkwrite_shstk(_dst_pte); + else + _dst_pte = pte_mkwrite(_dst_pte); + } else /* * We need this to make sure write bit removed; as mk_pte() * could return a pte with write bit set. From patchwork Thu Jan 19 21:22:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108798 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 91533C46467 for ; Thu, 19 Jan 2023 21:24:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DC19900005; Thu, 19 Jan 2023 16:24:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 43F6B900002; Thu, 19 Jan 2023 16:24:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CC55900005; Thu, 19 Jan 2023 16:24:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 02105900002 for ; Thu, 19 Jan 2023 16:24:00 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CE10F409D2 for ; Thu, 19 Jan 2023 21:23:59 +0000 (UTC) X-FDA: 80372826198.29.406BC5E Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id C7F19180010 for ; Thu, 19 Jan 2023 21:23:57 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Qd4w3FiI; 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=1674163438; 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=YWVE+Ni8aNDtLo7tgyffJpW9kI97N/rM06lnRUBkkSk=; b=yUv6edfnPpX9JHMq0Kbl7FWQCRs1IuGESW3AgaTPxG4199ekW8ZKy77w7QzApdeD/G2kvU wqv4Vh1mQVmGKrQ0A1bZDJ/1Q1u8RPxaWeI5w/pZpFMckjxgIh06z28rMeb/tWx/y1IfoM X4FjkTHQZ/2PGaLTOqJq3D9mQMlydN0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Qd4w3FiI; 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=1674163438; a=rsa-sha256; cv=none; b=6YgOP35XmwOBnPuIcU7+ukk7bIIpaZkD6A8jFTeyOkTnpMzWzeUYW9wJoma/z7o9ks9s0f Df9PKQLAqMRzrgCCbZbWrjva56zTwV5rcg1KjJ1MV8YmHihTgNfYhRwtyEvp9z7jmy2o9J sCcFDWsCZ2vCqejZMR5mR2BxaUHFGNg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163437; x=1705699437; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=+3N34QEERZNcvQNJAARc1au4ZPtrpENbiq/H3U3EHgE=; b=Qd4w3FiIkTcGjfGVzZ4mHGfPY+JXQCmc3Ab1W00CzHm/dFnWN3DuvDbz M8Np47kLoaZeHmsEexJ/CKxO45oynaFr+XI8a3r6pFPcPU5+w9eLt9cW4 M3j6JGn6VO6cCRAtrbtSCLIvR92pbcDXoACWK9OD5xLrWsGpdSad8BCzh P/0JXafqUbaOhr2KTX7eMilbAuvx+zuDHidNLfgh8AwPj0ECmjb4MB0oI wH55JpSJqaQ2UWwJ+jGiFo14Tbd6FhZ6ULTySibRHFb1g3T9FnElzhOlF YDfzR4wulfHzkcSkip56Xyqs7+F9LAJJcW5diuc3CHTI552DLwTDs+6tn w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119638" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119638" 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:23:57 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139093" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139093" 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:23:55 -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, Yu-cheng Yu Subject: [PATCH v5 20/39] mm: Add guard pages around a shadow stack. Date: Thu, 19 Jan 2023 13:22:58 -0800 Message-Id: <20230119212317.8324-21-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: C7F19180010 X-Stat-Signature: zh177x1cajtf5p7safpzhfa7rxcd4orj X-Rspam-User: X-HE-Tag: 1674163437-508952 X-HE-Meta: U2FsdGVkX19oyGSeTa1oD34bim7Y6ek5scNCGJEQ9JiFAvfaKSfvVhOrNWruiWGN704jhjpIF3S502J43TibijilajSzQSjwzWPtfYCHhU79oUrLs9QDW9WGyAm2FTLOEbusk0dtp67E5XCk8uk3vML2EYRWQTFZ1MFuoWg5Y29Yow763I0hxefh3KkrJ7w5aLndvLGmcdeCyNPlK10z8vBd3/MZsDeLRa+VjFSUMfGciJsWI9s2bq2zk6Vm3LfCkdtSpSNILiDTXqTC3c7/yvMpOThzvfbdUuHC3VcHzCwbz/pXt2ypMNALaAN9GjI4jeMxr4kzzxLcmLXlLWlqLbn+UlkvlFktTt6LDCCXr65ele6G/vS3D4X8USTmZE0pd6nGZTXgB5oBko/J0SJvtJmtzRosg/89tLM1BVL2HktuALDi6evOIsc9BNjcpZ2YC0ZVLFNhMrw3jF3PMs5Wzel4BtxgI+B0tz0tL3ZvfSbGkeZQbqNg7HbKf0YvNMurgirhZvGx6OO5MizZRQUPny33jPcA9FpdUoRxSXKBUGRjxQ60/W+GbydGZ93oOec/oB/tdIHKmDhAxQ02y8Hu+PJSlqBsoSQ6tu314DgkJijJhhoJkNGgWNj70eJTJloVAZc7P93mFWtMAPBZx3jognGNOr8G83GZgishb9gb6HPNXcrWRblWfyrGTp9FBiBRpjTMnXBt89cKKoOIlDmPI1HEm7pXGA/P+pzCN9llaBA3EVkQNRbEjbdUEBZfvgITeP5n8jbftU5eVjFrPDtOT0QbPwAOun13TNai3HZaEdIakPOlnVfpwn2TUFL4BdrmQnuAU64mFJSh2plrmjfcOMxcOg8BC1+Ibb/iJJMuat2PXjqF5bwPaYUtG33hTPRXIF+RyKrjmoMhh1JMWWV5j8YvU4D7GDcKF5npwm3JMJBcryMb4A3DDHg6N9pT/NXGYtOlm8gDREzcX+H6W5x nxuHi4I+ T+D5+zgQONJ7zaP5G1I6GJdobV2J+tV2O6cCyZLmRHBhy9DPiMVEUPpaPeNxiJPrO08pt5x9IgktPAiBt+ohtigwh5pFgd2sK8rnFImH+DWje4qWIcPKNiygFBUDuXDU2AGHX5NwZuo3QMgGtfyFyo6WvF1kwRXURsmYtkMt4vlyEOL8A4fo9ka39uwmNZzHTq8bI2M+QYXKEbjWdt4KgAzclHoIfJY7TCrZa4u7jN2FMF9ioqah7YvhnHxmkOUhMUOUWhJ5BQ2hsCJDz7dkhbxSblQmLC3wlc6DU 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: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. The architecture of shadow stack constrains the ability of userspace to move the shadow stack pointer (SSP) in order to prevent corrupting or switching to other shadow stacks. The RSTORSSP can move the ssp to different shadow stacks, but it requires a specially placed token in order to do this. However, the architecture does not prevent incrementing the stack pointer to wander onto an adjacent shadow stack. To prevent this in software, enforce guard pages at the beginning of shadow stack vmas, such that there will always be a gap between adjacent shadow stacks. Make the gap big enough so that no userspace SSP changing operations (besides RSTORSSP), can move the SSP from one stack to the next. The SSP can increment or decrement by CALL, RET and INCSSP. CALL and RET can move the SSP by a maximum of 8 bytes, at which point the shadow stack would be accessed. The INCSSP instruction can also increment the shadow stack pointer. It is the shadow stack analog of an instruction like: addq $0x80, %rsp However, there is one important difference between an ADD on %rsp and INCSSP. In addition to modifying SSP, INCSSP also reads from the memory of the first and last elements that were "popped". It can be thought of as acting like this: READ_ONCE(ssp); // read+discard top element on stack ssp += nr_to_pop * 8; // move the shadow stack READ_ONCE(ssp-8); // read+discard last popped stack element The maximum distance INCSSP can move the SSP is 2040 bytes, before it would read the memory. Therefore a single page gap will be enough to prevent any operation from shifting the SSP to an adjacent stack, since it would have to land in the gap at least once, causing a fault. This could be accomplished by using VM_GROWSDOWN, but this has a downside. The behavior would allow shadow stack's to grow, which is unneeded and adds a strange difference to how most regular stacks work. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v5: - Fix typo in commit log v4: - Drop references to 32 bit instructions - Switch to generic code to drop __weak (Peterz) v2: - Use __weak instead of #ifdef (Dave Hansen) - Only have start gap on shadow stack (Andy Luto) - Create stack_guard_start_gap() to not duplicate code in an arch version of vm_start_gap() (Dave Hansen) - Improve commit log partly with verbiage from (Dave Hansen) Yu-cheng v25: - Move SHADOW_STACK_GUARD_GAP to arch/x86/mm/mmap.c. include/linux/mm.h | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 139a682d243b..3f980d4823ad 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2987,15 +2987,36 @@ struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr) return mtree_load(&mm->mm_mt, addr); } +static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_GROWSDOWN) + return stack_guard_gap; + + /* + * Shadow stack pointer is moved by CALL, RET, and INCSSPQ. + * INCSSPQ moves shadow stack pointer up to 255 * 8 = ~2 KB + * and touches the first and the last element in the range, which + * triggers a page fault if the range is not in a shadow stack. + * Because of this, creating 4-KB guard pages around a shadow + * stack prevents these instructions from going beyond. + * + * Creation of VM_SHADOW_STACK is tightly controlled, so a vma + * can't be both VM_GROWSDOWN and VM_SHADOW_STACK + */ + if (vma->vm_flags & VM_SHADOW_STACK) + return PAGE_SIZE; + + return 0; +} + static inline unsigned long vm_start_gap(struct vm_area_struct *vma) { + unsigned long gap = stack_guard_start_gap(vma); unsigned long vm_start = vma->vm_start; - if (vma->vm_flags & VM_GROWSDOWN) { - vm_start -= stack_guard_gap; - if (vm_start > vma->vm_start) - vm_start = 0; - } + vm_start -= gap; + if (vm_start > vma->vm_start) + vm_start = 0; return vm_start; } From patchwork Thu Jan 19 21:22:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108799 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 7F124C004D4 for ; Thu, 19 Jan 2023 21:24:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF0E9900006; Thu, 19 Jan 2023 16:24:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA0FD900002; Thu, 19 Jan 2023 16:24:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA95E900006; Thu, 19 Jan 2023 16:24:01 -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 881AE900002 for ; Thu, 19 Jan 2023 16:24:01 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 677BF1209E3 for ; Thu, 19 Jan 2023 21:24:01 +0000 (UTC) X-FDA: 80372826282.19.0411C0A Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 6277F40003 for ; Thu, 19 Jan 2023 21:23:59 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=U5AkpDuH; spf=pass (imf27.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=1674163439; 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=uNNpN6cdmenOslkS6PIT2EBjNmLCOw87BxzclT8zLwY=; b=xImAAeZNM89YdVSz3QgkZZMmUEyd/TDCkGWNjBtXUGTIsFi5Z7d7AhxX45YdYmIw53G7MM HjGqabCXsDKPWJH/OzPxLThv8iLNv55mEalTSuE+DpYanxuEesUgvpyxe1NBbnjajvwh4G pvT14Nv9ngmt3664fB2eyPFC12Fzw1s= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=U5AkpDuH; spf=pass (imf27.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=1674163439; a=rsa-sha256; cv=none; b=fVDC2izhHhNLqyuOW4cfIMQi0p1LOd5FMpMEGeJs8wVM9IvXlknFR6YzOlJ9pQzVXMGsDS sUB8kAW/HAK69Zb+DTt2Udob0YamEmMz1OaLGF9w+lhldwPw9bngmf4QUHN1jo1IR4UxIW ANPvQ27NL9fEnXrkRkT7YH/8jtGldfw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163439; x=1705699439; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Sqmbg6aViWSIuyH7MnUz0YCJC/oOzGtcIb7+8J/qX00=; b=U5AkpDuHFdbgqDCWy/tse1rrmd5iw7EQBlp7sFFe5BxsrCaxcmow90XS ip5fLyvsbFofK2nU0Nj23h+IUrRq8Ygntlh3aR2Mp/pZ2x/KHMkaUjKR7 T9bm71jUco3Ai4hKptZslwZRM/eOTVjFI2qGNqatL2zDkSTYZLYw1mJOF OP7ScaMJXsW3H9lIDhJwA6nkJGHaiQCGiv7selQ5/NbFec1c87LMxWiQT eIdh1t0QuFDJUiiMoERyhA/QW/gXpBscRXY1T3oWaKLx4C9ZVJw1Fncoo /Drqx5ueOAzL3b9yWMzq1eyv2/Lx97IdmxrpP7xs/gfaaOpL1jgCSHcAt w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119664" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119664" 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:23:58 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139099" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139099" 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:23:57 -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, Yu-cheng Yu Subject: [PATCH v5 21/39] mm/mmap: Add shadow stack pages to memory accounting Date: Thu, 19 Jan 2023 13:22:59 -0800 Message-Id: <20230119212317.8324-22-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: 6277F40003 X-Stat-Signature: 17ae7sxcpuo3f6q6gtqo4dqwobyjcd88 X-Rspam-User: X-HE-Tag: 1674163439-950601 X-HE-Meta: U2FsdGVkX191T9n4zu5kdaTWQdaflwAWGa5+kO7BgE0u2mM53Ay0Ae5AtcUs/EW9DsSn+6wS3BC88WCd1bxLia/DOxGf768PcyeynGgOH1/8k23HaFHIDaBxEpTN8zGQu2QzoJ1CKKpqcQ373mZF0oUP+dIG5arKS39NXnfupzJN57UQQt1OUfIM03ZZ/awmGLZUq0Gyw4xvPJbL9yDWaXAxzY0ti5zJJ9dbRLXTshQWNXI8b5VIW5qADHk7hVomVfiNHv8w3PThcOg/yRj7rJvUrUjGZbFtnVhDwbydzHalZdxTwMBpbfflgM+vR5nEV3kSEHDLthVZzIaLVUmZDcU0V3pfwkH4Qc0EF/0N4FxpweVy1zuN5F5y4d/A0iClyreRj7T5NqP6PteM4rBpEahuv4bDAS8kPZoM9t7qW1PtC5AO7CIH+t4Y4cc/ZKMsFqemzzd4b+W/Bei3J+MypaWY3HjRgZZweVfvmCVivc0ZRf1CIQ3X9pvy4Klh5yjFWgEcg9Og/F9A8SEzneBdklDBJsNWPaizY3kqZ96NLynq1OBoQhrJ3uPpEdDL4MRnGfSCtSa4tpRZLOS564qZW0a82QQd8rqwyxGKPARahFnA4bL74GYPOxGhMj7fJ21Gs004JLm+MdflQ/tcmeHmR8SimCSjaERCkTGl/JTFyzsVbSOWdMj1qzh8Rue3+nBW5SJJkz7MQFFS2W1/wtF8rTA1+Kmr8z8y5EYvIX021i1YpTIm1WEzB85mcYPO3RTA897rhSrtIdbZwzl/1ybf30NklwHK+FLERVsckoI83d3xO5oUOMwjuJKWrM7cAjyq9o/epv22xmH7A1sGjlhrI4UXd6bsuxiBw6+LyaoJYGglH1XZdcm9uShjtisk1HsIRZcNfwUANI0Aa/OPAcsr4yPE/zbjV9t5U/8SAgQ+9iZc6mSuedQgy9TwyB99Q/W8VIihPlxtk/yanRrc4Te yWAGtRug igwFhR69WIDhd2dBq/ruULcgnbZF2998P0kAoNyVdWn9Gs7tYZWrXujwbnNZ+vBxi8GQj2QTcunnk9Y9DEdlm1lJ1MZt4kFnk63zfaIaYKjiuZV/zUAkuCCZKXAP1y993DhxTphpipJCppH8Tm4eN+kH3Zct050HHYqz93WEyhoaRukqlyyjZiUSycZiEbe5brAr4efRsAosgAb99sgV3c+1oFz7dwKmQmzJXBJ3/q5WYZbHrhbZEXPxG3s5/1swkZUzYnu/kdo7SBa2Z78GkI6U/owJyw/73E0yU 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: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. Account shadow stack pages to stack memory. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v3: - Remove unneeded VM_SHADOW_STACK check in accountable_mapping() (Kirill) v2: - Remove is_shadow_stack_mapping() and just change it to directly bitwise and VM_SHADOW_STACK. Yu-cheng v26: - Remove redundant #ifdef CONFIG_MMU. Yu-cheng v25: - Remove #ifdef CONFIG_ARCH_HAS_SHADOW_STACK for is_shadow_stack_mapping(). mm/mmap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/mmap.c b/mm/mmap.c index 425a9349e610..9f85596cce31 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3290,6 +3290,8 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) mm->exec_vm += npages; else if (is_stack_mapping(flags)) mm->stack_vm += npages; + else if (flags & VM_SHADOW_STACK) + mm->stack_vm += npages; else if (is_data_mapping(flags)) mm->data_vm += npages; } From patchwork Thu Jan 19 21:23:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108801 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 4C47EC46467 for ; Thu, 19 Jan 2023 21:24:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 77293900007; Thu, 19 Jan 2023 16:24:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FBD1900002; Thu, 19 Jan 2023 16:24:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 54E25900007; Thu, 19 Jan 2023 16:24:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2DD5E900002 for ; Thu, 19 Jan 2023 16:24:03 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 102A1120DEC for ; Thu, 19 Jan 2023 21:24:03 +0000 (UTC) X-FDA: 80372826366.11.B20B3E0 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id EFF62180005 for ; Thu, 19 Jan 2023 21:24:00 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=oIcx4WbC; 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=1674163441; 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=+CXWRCPnpy0btc6mfBj/1TwmGopDVclXbZtiYlkdzbE=; b=pK0Z0o2FzwtdXDPYUvR1A0ucBG76km6cFF/wIM8cPk5xjOStw2BOQ9240BLyoCSCKfzSNb QhvRn36v+waLM2KT8x4dDI7bnNzsVLY9Dlqt3VUt9XS6hPysPhsQvP+fRkddn5BIOCBAkF o9rJabZnu31Vb21Ru986YTFR7xV5Kh4= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=oIcx4WbC; 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=1674163441; a=rsa-sha256; cv=none; b=eshbypOHrRdM47VS7IhsWCpR25+mfEyUxG/Z/5TTYLpvwAGhodMjoO0iwoYurZVdzsjR+2 hroXENfyMq9Fu5gISWwv8V5e4Q21Cgx+CLGXTChh8WRnSvJtH9HAW40SPeVxWR2TYNe0MP WtZfiwAzlBeqQpOy0Uxi1IjMaIfkUVM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163441; x=1705699441; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=yYO9z3rz8CNjUY1wgWFKyzY7eNICGcV4jFfyGKxF5+o=; b=oIcx4WbCfndcp9TajOLmuB5iV+9WUtUQvDwBkyBnpjUejJRDz6YUWm67 QRilRgmDw+AvpX8EskNSycxy8fctCYgmrBBeVe7hFZqo+qkV2UtsRwAKa z9zZkKs25eTu5LW5CUF7vUiC8f1/MINeKTzOfwxHpoZYDadry21hFEUs8 zXxLQRYYS0u9kkvtzzuYt2XbHUcy/0DzsTtVrtQ+xNWD/7DsjUhu0gHWs F95orl77V4tRUTjmwjP/PQg7HAj32bovaSesqTcy/GBQPMXaCbEt/6sTx OL6KTIE9WovkRiMp+KW1sgb8v4Um95G0MljJRcp6y+poocUWaKL5NbuBT w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119692" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119692" 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:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139105" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139105" 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:23:58 -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, Yu-cheng Yu Subject: [PATCH v5 22/39] mm: Re-introduce vm_flags to do_mmap() Date: Thu, 19 Jan 2023 13:23:00 -0800 Message-Id: <20230119212317.8324-23-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: EFF62180005 X-Stat-Signature: quukpcw33s3yges875tioxw1y3swgyiu X-Rspam-User: X-HE-Tag: 1674163440-832151 X-HE-Meta: U2FsdGVkX18NKGxqG3WDRJT9rgIK834HQLb4H+NF0eiZAl8IQO/LXha6zvt17KJ4whrjjcKkjE+70HtowMd2aM6C1wSLOurSsIugr6l5lqxehpWwnovWA6AL2x6d8mkJt+//WAdMklmxBXyl46v+ZTzxp3UODDjAgOXvmaNRmsyllQcqmDF2k1pwsexNQuILJQPs0cCnFbs4MNh1Vo2BcBbMab4WEmtVHIkHIHjAPDer/TI1T/j2q5ZpZ41HGkgrWZWjzzSBmcgB/UTo3LD9Ra2wAIDGys+5i2gDcqLR7YVkA29ZcsFNk2N9jhwdKzJngidptV6KJXVRCEkn7HUhn3t7nZf9bqIlbZ/1KtR6xchYYkfo/rc1MpcNoSjslQ9D4Fu1FHiF7tklVLWLkYa6isJnoBXRx3XrkCap/XM1crsr+4aVahhMQoOby3Orxms/cJQg8WuaVY93ipN9FzWKZjfkHMva9X+cm/teldDHsb08pz9C/Z/lyiEEDK//TTOm1x/XemGqyTyA8IRvlRju0ydPjyt9M0+MyBTEBFR+HP4z6ea7nAwkXPB2vDtNWjq86Lv2qJeFSmY444xgA1yaOPJFlmulb5TDlZQDrED4leNK//rumnOIYi8pRSGMuzb7RIuDCdP1CA5YjJJkrBIV8dJ/3J02lsTIpmRhETqzDTx+6vF10DgqFec6RlbO0qMoeam9OFfKiZj26lJL2jwmY1/rbicx4TNa9bq3XQMPAuuxXoxLuqBU3xHqmYisI8wJG5t0YmWvq9+DtKNeoQuX4lCiXVxtFYES+VkQCUYVtMwcaBwVq0fDyQm2R/OOWkpKjzBbCoysZH5YxPKvhD5H68YVEt0f8xfSWc4nV9fxJJG8JGU7LZbXnQuACTv7dacYOk7F6wJWbHyYa0DM8nYWrbzQXrXgaOMq5O4Mq7bAq7QoUIGtjqYtcjgSKLrwtygsrMEX5sndcjZG+N0WEHe OP54rksN Rzg/oNs5XK9bZ5fuxhH8klJ3rh6nrLrwRQ7MEeFHtyb876oR97hP3W/U79uo7xPvqfj+GJQjn4xF74X7FVel7KBI8OUHvkUOK0u8VP34fPhI5wcyqhEXAo6qCysfSK7iSltAEuzYkmAJMN2F8f7mQqvqD7HkH0lxBBT7IWxYppyiLf+vaT0tam/LHbWgJ9iP1qrFBFUgUQGISMdHvRnI+YQ3uRg4n43bEuQ3oizihDJLE1FZEjKBGOWdl2nFBW3DfqmZp7yJPmoBUfAh1Vm3ThmghyO7AK0UztX6JI1svGOCjmPIUOWSo5RBonxhNKf2Zn3uZIZXmr2qwXKb1gF/GUoEPMDPTFJ1BgMaURSub7egKcWvjrUnQiO35Qi3qM/yKIbRMS9ZMvhyNeU+CoZPVMU985Byi2+Y4iwlrEW6mUp91DGNgGZ1CaiNHaDzE5WzcxsCjH+xjPY0/k8gWgpZnDrUL2HHaXk3MLmHwI5Lq1GktqgUIq1s/d4XXzQ== 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: Yu-cheng Yu There was no more caller passing vm_flags to do_mmap(), and vm_flags was removed from the function's input by: commit 45e55300f114 ("mm: remove unnecessary wrapper function do_mmap_pgoff()"). There is a new user now. Shadow stack allocation passes VM_SHADOW_STACK to do_mmap(). Thus, re-introduce vm_flags to do_mmap(). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Peter Collingbourne Reviewed-by: Kees Cook Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Cc: Andrew Morton Cc: Oleg Nesterov Cc: linux-mm@kvack.org --- fs/aio.c | 2 +- include/linux/mm.h | 3 ++- ipc/shm.c | 2 +- mm/mmap.c | 10 +++++----- mm/nommu.c | 4 ++-- mm/util.c | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index 562916d85cba..279c75ec6a05 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -554,7 +554,7 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events) ctx->mmap_base = do_mmap(ctx->aio_ring_file, 0, ctx->mmap_size, PROT_READ | PROT_WRITE, - MAP_SHARED, 0, &unused, NULL); + MAP_SHARED, 0, 0, &unused, NULL); mmap_write_unlock(mm); if (IS_ERR((void *)ctx->mmap_base)) { ctx->mmap_size = 0; diff --git a/include/linux/mm.h b/include/linux/mm.h index 3f980d4823ad..6e1796ee7e1a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2897,7 +2897,8 @@ extern unsigned long mmap_region(struct file *file, unsigned long addr, struct list_head *uf); extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, - unsigned long pgoff, unsigned long *populate, struct list_head *uf); + vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate, + struct list_head *uf); extern int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf, bool downgrade); diff --git a/ipc/shm.c b/ipc/shm.c index bd2fcc4d454e..1c5476bfec8b 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -1662,7 +1662,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, goto invalid; } - addr = do_mmap(file, addr, size, prot, flags, 0, &populate, NULL); + addr = do_mmap(file, addr, size, prot, flags, 0, 0, &populate, NULL); *raddr = addr; err = 0; if (IS_ERR_VALUE(addr)) diff --git a/mm/mmap.c b/mm/mmap.c index 9f85596cce31..350bf156fcae 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1238,11 +1238,11 @@ static inline bool file_mmap_ok(struct file *file, struct inode *inode, */ unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, - unsigned long flags, unsigned long pgoff, - unsigned long *populate, struct list_head *uf) + unsigned long flags, vm_flags_t vm_flags, + unsigned long pgoff, unsigned long *populate, + struct list_head *uf) { struct mm_struct *mm = current->mm; - vm_flags_t vm_flags; int pkey = 0; validate_mm(mm); @@ -1303,7 +1303,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, * to. we assume access permissions have been handled by the open * of the memory object, so we don't do any here. */ - vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | + vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; if (flags & MAP_LOCKED) @@ -2877,7 +2877,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, file = get_file(vma->vm_file); ret = do_mmap(vma->vm_file, start, size, - prot, flags, pgoff, &populate, NULL); + prot, flags, 0, pgoff, &populate, NULL); fput(file); out: mmap_write_unlock(mm); diff --git a/mm/nommu.c b/mm/nommu.c index 5b83938ecb67..3642a3e01265 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1042,6 +1042,7 @@ unsigned long do_mmap(struct file *file, unsigned long len, unsigned long prot, unsigned long flags, + vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate, struct list_head *uf) @@ -1049,7 +1050,6 @@ unsigned long do_mmap(struct file *file, struct vm_area_struct *vma; struct vm_region *region; struct rb_node *rb; - vm_flags_t vm_flags; unsigned long capabilities, result; int ret; MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); @@ -1069,7 +1069,7 @@ unsigned long do_mmap(struct file *file, /* we've determined that we can make the mapping, now translate what we * now know into VMA flags */ - vm_flags = determine_vm_flags(file, prot, flags, capabilities); + vm_flags |= determine_vm_flags(file, prot, flags, capabilities); /* we're going to need to record the mapping */ diff --git a/mm/util.c b/mm/util.c index b56c92fb910f..77867bf9959a 100644 --- a/mm/util.c +++ b/mm/util.c @@ -517,7 +517,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, if (!ret) { if (mmap_write_lock_killable(mm)) return -EINTR; - ret = do_mmap(file, addr, len, prot, flag, pgoff, &populate, + ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate, &uf); mmap_write_unlock(mm); userfaultfd_unmap_complete(mm, &uf); From patchwork Thu Jan 19 21:23:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108800 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 5256AC004D4 for ; Thu, 19 Jan 2023 21:24:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2448B900008; Thu, 19 Jan 2023 16:24:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1809F900002; Thu, 19 Jan 2023 16:24:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E039E900008; Thu, 19 Jan 2023 16:24:04 -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 C100E900002 for ; Thu, 19 Jan 2023 16:24:04 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9660240D5D for ; Thu, 19 Jan 2023 21:24:04 +0000 (UTC) X-FDA: 80372826408.28.4C83CD2 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 93B1440010 for ; Thu, 19 Jan 2023 21:24:02 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GBfheZir; spf=pass (imf27.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=1674163442; 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=o/4m81j8xE9qAXKmMhKAmN4IZZXNyomiS4bHPHlONRs=; b=Gk3nkPAlk7uXVGL7JfjW0Nod1okFS3fl9nu4u6UsFsptZgg67MadpURiJZpYerfm98u0Mk L2VP/fXZSnTP7+XqnHkFoZtzM0NbaWjo6iyCcssUYuyMHw1OPCyYBlagphAlaf71TXaqs+ jQ+Tk0fHZdKV79poMC5LtXoKOVjyyfk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GBfheZir; spf=pass (imf27.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=1674163442; a=rsa-sha256; cv=none; b=0chODgWaFzbpIh2ZQtles92HLGjyvWYMLeAGbgFSkztWr+cCrxl4FMiZB7qFwjD5VK4uGU pBASUVoDZHbs7NQpaPnT3aBQkUkM8tIxr3pphqegj+N66t+ARSF/7d+oMAK0+tgR39GbNi esOUDnpGtqgCRNnaV5/HSA7I8PRqvKo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163442; x=1705699442; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=yljn5XVXAdgu3Q2GcXJK+3SbP1Csl5qMglVIozskdYk=; b=GBfheZir8y7/goI+O+JqjOeRumqPxaouAJARGTShGHf4FVhFOCwG/eET BKS5AxxIyFpSl4oht1oGgOXTrddUvfFJ6UCmyZTYjabSkevTDxuyGZ5as 5+TrjCuNPS9M7qu9JVCeWyMpFytZVwufjiOb6Ln2l7oaI3oVNIBCqxTzv l6Sh5bf7+pmT5uBVpZJwVNwtm+z037z1BoR538OHlE/e/HDCzrPoivHLj WromUWWjk/CbFYgqQ7XGFu5kQqjRDurez3w3V4GgI2GyyMvR+2NGk4fou pnO021UVQcjcLKOmp5XL8ffVbcTQfMY0I0HvzJ1Rf4ALondcvNC7/ikt6 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119717" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119717" 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:01 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139111" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139111" 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:00 -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 23/39] mm: Don't allow write GUPs to shadow stack memory Date: Thu, 19 Jan 2023 13:23:01 -0800 Message-Id: <20230119212317.8324-24-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: 93B1440010 X-Stat-Signature: otti79b3o9hhegn8kexetbpi46zcgeeo X-Rspam-User: X-HE-Tag: 1674163442-694418 X-HE-Meta: U2FsdGVkX1+EGiUX0Qi8KCCjTJN3i2LR4ZevvF5qB3TYnzk20oxVLUKNl/uwPJPbM1Eu2EGrxICA9/LDaLG2qV51wXY8mpSajd/BVfFqCA4JPowAynjdqq9QYYMTzpNPQeE9j3oJ/ofLs3APARjqQnizpiUTB5MDcAejTcI+uS9As60m2M3I9G/tEMIz0XRkb4Yb25D8xHbGyD17BgHGuvHDUQDYVH9lWHh/5r3WyBGyUPqjy6yNALTLwG7hwoxVDmFsOV1dxMkmKt9VA54wXN6fH9Da84ucgVl1LJ8+5Gg2Oe4LAx1M56WAbvx0JHIV9Pyt7ywOjh/LPqgT+o1h1fM+EGV4CglykJEEQO9MFa/C/meqonYbrUR5ocx6V584ZW2USTuW71W6x7AAlpoIqziX6Bs/csP7NfmTu8+Z7Pp6UfT1C6pOUCou9NpNVcKiHd9yBOnr4PJAGNy37tZ4cJpevy0ZvlbFaOGOzvRblruecgDt+lXewSPJmtirmEim77XYuH0m6sYBMarwGbJZ1DJhmj3hSnTFlg7A2pnrSORyVZFnEU80cdPrIC+NIwdEPSLxEVRa6TcgNqQlBVG0DRn56HWij4n1vBNttxoeJqk3I3WfQASAANbJuaE1h2qVj/blfgTcaTth6y+1CO2/JWKQ9DJnydramZFhkmvbvyRAWVwhFjlLXdlRVGllKNCvtGMKuXspDF3BMnvuYD85Ei0e41c4oxk/0FoMmVHumu2vjioY/AUIMQBQ7ByaoxxVcl8y6ZeCkTGgv1w66Q0du+63f8TzwYqKj+cG8x3l35cHYHkRfGzp0aRM1+muO/tSPjXku27YV/VqG3y+gLd/zyZmUfWZkD2uAmXnI9ZrOu6ODdrKpYowFgMojvTN6qenISbiwNl47F8VHI0Qcu+IvHmGnmdSKoX+SfkGwaJRM0hx5yJBT3aaHMXLenXWRlXCBg0WnoPGMw0A2MiGO32 j7jJ1e6J HqTeBmS2DZX0xPlmBVNs/QtdkPJ+yNHfGsNKfUlFouAAN0rcZMbFf/xPSLlwJ3rwbp0G1FdkM/MenoTUINUP0S6W/RU6CPH6/SUNOJCRUBegvNfoa45Ic1tsw+p7sAjUpbB1v1xPt4hAOvoX/clvJ4otKXe5YQHt9Ay1BbVTGfh9vvWyR1rKka+cYLUp2C+6P18ia/po+4ffxE2Le7lrswl9gkxeNRwSqNEr27mXV/gM03gVTjnE51GyaeZnNpZb6SWLtPr7FOagmzdxnef7xtyN8LSEwseNgOcvj 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: The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. Shadow stack memory is writable only in very specific, controlled ways. However, since it is writable, the kernel treats it as such. As a result there remain many ways for userspace to trigger the kernel to write to shadow stack's via get_user_pages(, FOLL_WRITE) operations. To make this a little less exposed, block writable GUPs for shadow stack VMAs. Still allow FOLL_FORCE to write through shadow stack protections, as it does for read-only protections. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe --- v3: - Add comment in __pte_access_permitted() (Dave) - Remove unneeded shadow stack specific check in __pte_access_permitted() (Jann) arch/x86/include/asm/pgtable.h | 5 +++++ mm/gup.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 87d3068734ec..425ded5dd6ec 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1671,6 +1671,11 @@ static inline bool __pte_access_permitted(unsigned long pteval, bool write) { unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; + /* + * Write=0,Dirty=1 PTEs are shadow stack, which the kernel + * shouldn't generally allow access to, but since they + * are already Write=0, the below logic covers both cases. + */ if (write) need_pte_bits |= _PAGE_RW; diff --git a/mm/gup.c b/mm/gup.c index f45a3a5be53a..bfd33d9edb89 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -982,7 +982,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) return -EFAULT; if (write) { - if (!(vm_flags & VM_WRITE)) { + if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ From patchwork Thu Jan 19 21:23:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108802 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 542BDC6379F for ; Thu, 19 Jan 2023 21:24:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB8EE900002; Thu, 19 Jan 2023 16:24:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4329280001; Thu, 19 Jan 2023 16:24:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AB23900009; Thu, 19 Jan 2023 16:24:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6B2FC900002 for ; Thu, 19 Jan 2023 16:24:06 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4D18EC0445 for ; Thu, 19 Jan 2023 21:24:06 +0000 (UTC) X-FDA: 80372826492.17.3A79686 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 255CD180017 for ; Thu, 19 Jan 2023 21:24:03 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jplBSbsF; 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=1674163444; 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=kAPK2G43HTRXIoPjAhCuhjvThw+Gk9Azt2qL3QWNxr4=; b=NIZ1iMnDtpV7nMY1hAWywBIyLz2wHCufOh8MsnYTuE9GZYRyxMxF40CDleYQ7X7XeCnG8T VQca7Zv6itNB01CtJGwVXI+BZl1Gt30l+di7G1RogKcTJSNqxg6KO3vU9i5+6BkjlI2T6c vlnxtdWfec8vghytLqHDyKGtChtVpeQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jplBSbsF; 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=1674163444; a=rsa-sha256; cv=none; b=tJjRFMjT4PjTB8pKaOb03hztjvfxwYYdStclszacFFKtAmOdwvdHQ2mu/hytWCamriMJ2z EcWHXU0ic2cYNrHpswVZi2r2n4P5oUCiQWU8xF7Kp+kbvXWkoxdOuwE8tnPTVOVsnMzzrI QYzQ+/WZoMsiUNDWX6YZUNeVPZAKdDI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163444; x=1705699444; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=DRhmUKmYGXqIeL9sLHmMRoFoAPvudeDqkB4y2AZ6uIw=; b=jplBSbsFN17ZrNjKegWNPES0U0eUbu/mpYhvf89JoHaADeSk8QvhcO1V nE+Q4ChowIWC2zZVmQ/mGGdwMrxe7OaoA69i9oxWbgC4Y4cICGCtDhrey 2d0J+encA3958D92ly3gzPbnLhXYj6+FHRPd/u8xRik9BEKJXbKDeJ1iH VmYLN0xHbuchN/tJZMF41E843VM/qVUj1ooQTgg76sa+8eGSJlpcCxUZl ZlsLRi2T8iiqnGp5oIdBvNoCx8IhBDYjIEVlHGnYdFOowehNC+mfku7Ee J5gz1LrM+xs+t8k20ym9OTB4wfXEFbYLmAtdYeNic2lVd6lJ9IG/8bzKK w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119747" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119747" 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:03 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139117" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139117" 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:01 -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 24/39] x86/mm: Introduce MAP_ABOVE4G Date: Thu, 19 Jan 2023 13:23:02 -0800 Message-Id: <20230119212317.8324-25-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: 255CD180017 X-Stat-Signature: rfmx67dpox4ct57dyer3dsdt6e1fty9x X-Rspam-User: X-HE-Tag: 1674163443-380783 X-HE-Meta: U2FsdGVkX18S7PYGuauNbLC0TQH7EHZYaheCzPTZtvlwkPCuSwYc21+smVjfP5o0HYiNBANs/ZI+V0xsr+0BIA+gbaI2YWr8gDrie3+1WG7vUeDhMiobpwRM1zP4U1Y/QKDNF3RaxhkHIOFJyPTyrgP7pN7w0Qy03mY6pC+ewIhMld3oUfjPv5PnHjvFfNd/dvbt33RAkBNOUme3w1NC2Hz/XlFXW6cjafokiF5aafko/nSLfgQAUuTS//NBLXnxbfyyINEmJHOehzChTEIsnPlSIF8yLn4lWBDfaYBpkdKOgMvujeKK2TxGaRvd/AWbAlVWS2t+Wwy7oi1JF+aU6h2MGJfNPsdLmr2Xzjkofv7i5Zt7IbdT+bPf7Kacd/W/oh8/CIVauBCf6vV/cngzog67Orsq3HMXbaV+shXS12Mjfu3fkCr2e5db8+yClzsnR+gDy6caAmpV6GADCxRkJXC6/Mh2ncP53n3/iPaQ5HHKglalEfiJEP0DpXsCCI89RroOd4Mmiou2BdytREnkEPfbVVwO7jw+G83lWdfEsp0dKcaLV+gSkwHaqV/+7tXpceLpmPPMYMV1zu9UPjWFY08pJZiInCGaB6Lgd+dSply6C9TAMpD3vFTQHjMZYhwk1IV3Kvfj6a3csr+2Xg41So8+AyCE0qN4dALCY8VgPZqebEdajzJQIm5mCXkqiTEWiUdJmo4/t2aTZW2nTH15MAVFK68tOymAiNDtsHNMD/LPNlA1CT5ky3qq2jU6byeFcxgVbj8CzINkhEu7zB2+Y4lAojVavl58GYmj69spcp6kqOJQqcX4VymL8zNPuC16Gzhkj3QEcAynfCZTGf7FKYZ0G31z2uiN68kud/C51VUIhRyf2x5q0q2sjX5aJPD80re9MJvP6xTPVF+BajsH2Q8HtQ2bTdagULQ7a4QGFwumblVA+3amU2JSgQ8nNbZjL2ATfqlRluTZJVND5vI JYj439k7 qb4HfsT9Wo4AHRR9r8lBQ01um5s3v7hW3OeIbIwisYx9L+Nrt0Hj88I1Tf46GAP4qefSEMUyRIuaI45yRluypUl/HwWgtGI0VbWfS367mg9HHti8JB/UfBJ3Eh/TSjPJdHjmoZlSToWnAUYL4StxLiMOCc1r/ORKZTV1NwUp2SODkwRebLjzUHOo3ht5MRa3k70oC4B7kjERcxOBk4mXfSTAHv3imELLqkUCsQbck6di7SWh0EX5uVme6bF8Z9qEts54J 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: The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which require some core mm changes to function properly. One of the properties is that the shadow stack pointer (SSP), which is a CPU register that points to the shadow stack like the stack pointer points to the stack, can't be pointing outside of the 32 bit address space when the CPU is executing in 32 bit mode. It is desirable to prevent executing in 32 bit mode when shadow stack is enabled because the kernel can't easily support 32 bit signals. On x86 it is possible to transition to 32 bit mode without any special interaction with the kernel, by doing a "far call" to a 32 bit segment. So the shadow stack implementation can use this address space behavior as a feature, by enforcing that shadow stack memory is always crated outside of the 32 bit address space. This way userspace will trigger a general protection fault which will in turn trigger a segfault if it tries to transition to 32 bit mode with shadow stack enabled. This provides a clean error generating border for the user if they try attempt to do 32 bit mode shadow stack, rather than leave the kernel in a half working state for userspace to be surprised by. So to allow future shadow stack enabling patches to map shadow stacks out of the 32 bit address space, introduce MAP_ABOVE4G. The behavior is pretty much like MAP_32BIT, except that it has the opposite address range. The are a few differences though. If both MAP_32BIT and MAP_ABOVE4G are provided, the kernel will use the MAP_ABOVE4G behavior. Like MAP_32BIT, MAP_ABOVE4G is ignored in a 32 bit syscall. Since the default search behavior is top down, the normal kaslr base can be used for MAP_ABOVE4G. This is unlike MAP_32BIT which has to add it's own randomization in the bottom up case. For MAP_32BIT, only the bottom up search path is used. For MAP_ABOVE4G both are potentially valid, so both are used. In the bottomup search path, the default behavior is already consistent with MAP_ABOVE4G since mmap base should be above 4GB. Without MAP_ABOVE4G, the shadow stack will already normally be above 4GB. So without introducing MAP_ABOVE4G, trying to transition to 32 bit mode with shadow stack enabled would usually segfault anyway. This is already pretty decent guard rails. But the addition of MAP_ABOVE4G is some small complexity spent to make it make it more complete. Signed-off-by: Rick Edgecombe --- v5: - New patch arch/x86/include/uapi/asm/mman.h | 1 + arch/x86/kernel/sys_x86_64.c | 6 +++++- include/linux/mman.h | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h index 775dbd3aff73..5a0256e73f1e 100644 --- a/arch/x86/include/uapi/asm/mman.h +++ b/arch/x86/include/uapi/asm/mman.h @@ -3,6 +3,7 @@ #define _ASM_X86_MMAN_H #define MAP_32BIT 0x40 /* only give out 32bit addresses */ +#define MAP_ABOVE4G 0x80 /* only map above 4GB */ #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS #define arch_calc_vm_prot_bits(prot, key) ( \ diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 8cc653ffdccd..06378b5682c1 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -193,7 +193,11 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, info.flags = VM_UNMAPPED_AREA_TOPDOWN; info.length = len; - info.low_limit = PAGE_SIZE; + if (!in_32bit_syscall() && (flags & MAP_ABOVE4G)) + info.low_limit = 0x100000000; + else + info.low_limit = PAGE_SIZE; + info.high_limit = get_mmap_base(0); /* diff --git a/include/linux/mman.h b/include/linux/mman.h index 58b3abd457a3..32156daa985a 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -15,6 +15,9 @@ #ifndef MAP_32BIT #define MAP_32BIT 0 #endif +#ifndef MAP_ABOVE4G +#define MAP_ABOVE4G 0 +#endif #ifndef MAP_HUGE_2MB #define MAP_HUGE_2MB 0 #endif @@ -50,6 +53,7 @@ | MAP_STACK \ | MAP_HUGETLB \ | MAP_32BIT \ + | MAP_ABOVE4G \ | MAP_HUGE_2MB \ | MAP_HUGE_1GB) From patchwork Thu Jan 19 21:23:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108803 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 542E4C46467 for ; Thu, 19 Jan 2023 21:24:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F4C4280002; Thu, 19 Jan 2023 16:24:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A532280001; Thu, 19 Jan 2023 16:24:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1408280002; Thu, 19 Jan 2023 16:24:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D9C4F280001 for ; Thu, 19 Jan 2023 16:24:07 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C3922A033A for ; Thu, 19 Jan 2023 21:24:07 +0000 (UTC) X-FDA: 80372826534.05.7792231 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id BD0AF40007 for ; Thu, 19 Jan 2023 21:24:05 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="hSM//sq/"; spf=pass (imf27.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=1674163446; 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=XA8Iioxyvl6Xy5Wa5VtmFxF3mszcNtBcOHPIPzhymEA=; b=nmymhWdO69s1deRXSLJQIqmiSKOwaX9caw+wJ7a7mGxeNXfvclhMheLFhAk2elKXr5u+rQ V/mIflYZMTqiJUlrE9FdvBH8PdNaFteRKUUP4dqX4ti9CtR4UZFA7szQ2X7JrCMeoZFBGg hEoYjiHn5NJ5WpzhVQL1+vfQNL19geI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="hSM//sq/"; spf=pass (imf27.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=1674163446; a=rsa-sha256; cv=none; b=JcQU5il6NqNzQexrGeLqy5y1MsuvpNnFLpTB5N/OB898E++LlB8relb051H7b126qPNyXW dmFXAtH2Q8WpT/a5frktLc80cefBUN9lp7TToF1Izx/HkRSRniCGZzkqcgFBonZqCD3it1 F14hFfDn+gGlmYVfgcyzgw/HFABB7aM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163445; x=1705699445; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=7r4JBhnxBM6qBcyW6ndYNR0sg/Hfe6OTwhDLlSBw690=; b=hSM//sq/0NC/6Isd1k30Cjr+uCueMcWxGLH4qq83yhAeT8Qi3Q2VclHw DpA37VqRG4wadKd8VC7bktLhrnOfKvLaI1FyRfz6Yzqp8dIamthzP6+DN Gy3DbhEN5PPizo+OQwEMrvxWZwYWLFCZU5o5VCjOXjz41vPWsaHlMEs1z kQMENXuYAQAoGmx7kERojpP6nrOssXcvvkoBIaR8jAQFV3xIKOVInIkn+ 1lUSs+Pxeurc0PI5HOi4nDheNG1cLm7YW1wY01Z1YRTz9+wf5bqfRd+qd FEh9DjckH4tNhV6n082jUWAD0nLyN+a3hEwSY5g6XDUDK0Hy/mgfcrNwL g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119776" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119776" 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:04 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139124" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139124" 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:03 -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 25/39] mm: Warn on shadow stack memory in wrong vma Date: Thu, 19 Jan 2023 13:23:03 -0800 Message-Id: <20230119212317.8324-26-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: BD0AF40007 X-Stat-Signature: oyywbeu5widcur8pfz6nb8ykmfi6ti89 X-Rspam-User: X-HE-Tag: 1674163445-809673 X-HE-Meta: U2FsdGVkX19UMPyoGHW/HFdbLodNYrqe1tYLvdi1t900YYKXlmaWwmvPawmdeHuh1GtscH64MQeKbm6CqfJOeDfyRU9cjkkW91X6PxSRHyCq07OYpbZPLkAwK56Iwako2IMq5+qIBzZFZUV0MhAU1uR6Ng5ChEPcpC5rIAydC7mm5m+0BV5XNC+sp+xS7GIXPpC1IfiZfYxOkgm2JflRl8gpJgSCcvs4f/kTCmzfw0qFoR/R0B//6mFQMdi28F4yKxZIeHDhr3rG8wKM6H874YXL+VB+IVRB3/McXRNZW6yvP5sWQcYCbgHqSqtddpnfCdodRSxtkfk3a/RoEYHNKnFWYbwK62HxxzVbkFPxFlT3Oqch5WWAIWVFYs5u5jkcXQfW9mN0djuC5y5axOrMLP5jZ4DMxILq68PFdi2Bd+fV05PmJZzgCqAAqw9SqXPplOGPr+XwJWiKVWLdOHEkSn3gNP0Ho5gH6I3OuM+RVz4OSQ1iCiqsyY6tT+b1Kj9oMFYWMgHkZUV97bqE6HlqcfwDTBpuPzdyBPppvNkt5CKgTu4rxbxAvearTyrLsWvtvvx+RxZWC1aYhUfh/2hOpvtOYwgxVK11V+qRrFB1CS6y/PGLd+nKRF9jVu+NC8clm5nD+EEojxcm8o5Y+aZ4M0ABI6ZsNNfvNUueZLWHpKqkeEMAYnSrHE6TJMjrbElYIrCIPSDNklxYzaeb2MdffFmi+mM0zuaG/Hq6Wo0ruSHW4ijQB6HydcaILCR4IG7QcmFoZsIe+FlAPIyMiNm9T7cxQOsAxR6wMD3y2Eqw/Y50VV8IY1wAmP1G6LsWFY/dZoPHt1SR4E30qdIQSd2GkWxkvymOf9C4YuVzcOZRbQtJgaUuBNWP66C+53ximv9pRKABTpXysuGTrun6hz2GX6omIGsREHrMF75wQQPmZXhV0A2/2cc1x/+cd3cOQRTJp3uzxfSAE7GqcnIncVq Sw638ES2 8m3/pk9h0iVgpdr08JmfhWrFX9drEeT7c0Ori754i/BUHDyXZjwJ6M045Q4eEc5Ilii7jvdh0K83T3IxUFmO6COt5ZLAQvxMjZsw9xl0wy97rLXv0J2RCGVZD43lz1WzYjuUjFqglHLm75GZDpnACY+XiB0dE0TvA99FPION9CeRc06/lYIrRgmI9AHtxZbc8cM9ibneLhuKl4DgC4CBzvXlW1HbhYGNjxCzt6YuYX2Q1GR8= 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: The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. One sharp edge is that PTEs that are both Write=0 and Dirty=1 are treated as shadow by the CPU, but this combination used to be created by the kernel on x86. Previous patches have changed the kernel to now avoid creating these PTEs unless they are for shadow stack memory. In case any missed corners of the kernel are still creating PTEs like this for non-shadow stack memory, and to catch any re-introductions of the logic, warn if any shadow stack PTEs (Write=0, Dirty=1) are found in non-shadow stack VMAs when they are being zapped. This won't catch transient cases but should have decent coverage. It will be compiled out when shadow stack is not configured. In order to check if a pte is shadow stack in core mm code, add default implementations for pte_shstk() and pmd_shstk(). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix typo in commit log v3: - New patch arch/x86/include/asm/pgtable.h | 2 ++ include/linux/pgtable.h | 14 ++++++++++++++ mm/huge_memory.c | 2 ++ mm/memory.c | 2 ++ 4 files changed, 20 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 425ded5dd6ec..356f1d43e403 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -129,6 +129,7 @@ static inline bool pte_dirty(pte_t pte) return pte_flags(pte) & _PAGE_DIRTY_BITS; } +#define pte_shstk pte_shstk static inline bool pte_shstk(pte_t pte) { if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) @@ -147,6 +148,7 @@ static inline bool pmd_dirty(pmd_t pmd) return pmd_flags(pmd) & _PAGE_DIRTY_BITS; } +#define pmd_shstk pmd_shstk static inline bool pmd_shstk(pmd_t pmd) { if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 49ce1f055242..04d0bc466e43 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -539,6 +539,20 @@ static inline pte_t pte_mkwrite_shstk(pte_t pte) } #endif +#ifndef pte_shstk +static inline bool pte_shstk(pte_t pte) +{ + return false; +} +#endif + +#ifndef pmd_shstk +static inline bool pmd_shstk(pmd_t pte) +{ + return false; +} +#endif + #ifndef pmd_mkwrite_shstk static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index fbb8beb9265e..5bd71da75dec 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1700,6 +1700,8 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, */ orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd, tlb->fullmm); + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pmd_shstk(orig_pmd)); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); if (vma_is_special_huge(vma)) { if (arch_needs_pgtable_deposit()) diff --git a/mm/memory.c b/mm/memory.c index 5e5107232a26..c4cc38baffc5 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1381,6 +1381,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, continue; ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pte_shstk(ptent)); tlb_remove_tlb_entry(tlb, pte, addr); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); 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: Rick Edgecombe 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; +} From patchwork Thu Jan 19 21:23:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108805 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 4D551C004D4 for ; Thu, 19 Jan 2023 21:24:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6617B280004; Thu, 19 Jan 2023 16:24:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C326280001; Thu, 19 Jan 2023 16:24:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CC35280004; Thu, 19 Jan 2023 16:24:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 24705280001 for ; Thu, 19 Jan 2023 16:24:11 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E9C90A0275 for ; Thu, 19 Jan 2023 21:24:10 +0000 (UTC) X-FDA: 80372826660.17.7C11D82 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id DB19140014 for ; Thu, 19 Jan 2023 21:24:08 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TZuy+3QB; spf=pass (imf27.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=1674163449; 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=bs+9z9aIIF14yXerZa6rGEtRQZc7+0mOjtqR3MupuLg=; b=RDmeCse5P5BxAPu08PpDb3c2pdnOkoG8CMnPRGfhLTZVMNgjqlF3vu3VhlozGtC9rUXmtL 5b5MvXSz9MOlUw/vdxuOi7NeSeQGBYrpj73qQGvigSwQcVKXm++lsSRMi4R548Hq3bj2KC Ejpd9FNqpLTR4cHHiMDhj30h1WDEp6k= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TZuy+3QB; spf=pass (imf27.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=1674163449; a=rsa-sha256; cv=none; b=hNpzFfAoU2CNUt0qCtjGLzGKc36qXgZ+mFA3tismSTcsEYYS+yQwvHcxKjxO3fQ2fVY8iW 4QROMrliWKVNQX68NqrI4D6jAIngfVo3nTGtNEIziESUAwwE3QxL4jPCaQPqBVJSv731u9 V7VkW62C+xwW7JnpmgWrwyayzTUkVuw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163449; x=1705699449; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Ifxi0e32c84RQATdXOs3rUNHxe57SwrjYewWnLIO6BE=; b=TZuy+3QBd59FA8ZPS8UpStBkuM46jx68KuWSDcL+4PDkWKCg4Vumo7aB 3t1JxBu9rQm+xOauKJVRNW3cEs220/LA647pcvFAmKJCZMKk3zoetWLqM OiXe/Q0J6taMc+ZlOQUdFCibWqrPrIm3bSrZ9Q4JPoiTU5lDjA5/05XYQ 0pgx+5Cs6RNQ3QHnwBbxzOcZvfXZd2n2B/ZKOoEyCahs+xExGCUghfMcG UHLcHNLfi+K1ll+OmxG4ndHZsht9oF8FuzjjgKcoBgBdKOKzDpSxNeo3K 0Bv980bgjHEWr27EgsPQOo2KAO+KZRiLF7OOSXiRC05eXThBmDhmLSYOX g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119839" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119839" 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:08 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139135" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139135" 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:06 -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, Yu-cheng Yu Subject: [PATCH v5 27/39] x86/shstk: Add user-mode shadow stack support Date: Thu, 19 Jan 2023 13:23:05 -0800 Message-Id: <20230119212317.8324-28-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: DB19140014 X-Stat-Signature: h1ytforowmbf7mfs37j53rb1c68jusz7 X-Rspam-User: X-HE-Tag: 1674163448-461478 X-HE-Meta: U2FsdGVkX189KgycEYk3sZ0KomY3PUSvAibsgykC5n/geZr/inQYwy6jxIL8aXDsUDvjRcx6FAQ61R+A/aTPYbCFlB+66Gn6L9Gha/9P57X3B254DHwFZYdGwLgWioz0WrkUdSVxoFPxfNBCxnbubAyT+gLF9lJiyqNrSgk+MxNnioKF9E91CvfuOLLRkr1YSn4QPsX6D08N8XcCZ0PBJVhov4T7tHZJPC+VvLvIyMvYvqJJFzYeoYOKj/4VouJHOqX1E9wvnoD9yy7xk1ttypBjy+/GVjRWUw3AISorYlf4lAgXUaGO2HMbed+GLNXttWWAFJZSVajLIAEsVfbu+fzTA78Cqudlzk+mWRABgTqSWbpXb2N1gjMlqxo0FvPJcK58BiqteTMnaC+IzjrnMr87kqi1FGSC7zIs+2H+LYZlN6cvijX4iUfC0/IhA0NbvEOJmVbEWC04Hr5aWOTQDdbdAWxLzlM2AgKFpae10tZKoGzGZK+2ap+7GiebmpEj5jHWFWhHZEdjWXAPn931T0mnrt5d8Qng9ZxuBzQhxhuN7hEGzuQa6vyaGFje2C6gS/hGtja9ZqAPs5FqCOblTcED5HLiHxdjA7aB+a6PnUKSgFFTGY14i1njc2S44PBHZ1aYLGPB8Cp0uLVr/wtN1bHd5dvo3xNTV2dhIjkD37FMy5pfM7Wmcf4cqFX0aG42RFl43qefNdHTSO1jg6G8/QU4cV85pEaeQ1w+A2FRzB/EwVtnwLRrhS96QJehwxeUeL1ZbghOSQjI1oH3i549c59Vv2nwGJ4e71SNvEH+q64Ehx8JN1fObUyNOqm9CTV8pizhrZ7aW6im0vln3Uoqvu0ONqsHEo443TgtOQh6M1yYMiHz5zz0xj3VfT2KZ1JFtYnUvNok65FgDZXiDioeoj9dP+fEjs2A3zdZ/9FTDT2ZJyYOqIcjb0T0HE1rNAijMlFBqp+p45nyf0deQ3q xF7TBhXN 0fFr1tMNUx8M31JntP1wD0UAg8sdIEQ9tIeW0T1NZpf+KBI9FC7vcavsyu4ZEBP+90N1YBDCX4tZ2BYst0uKNkhx5a5/DaoOSw1vThn60hTp/lOlJYWPslTH3SAkFTHKI5HUq8xG2Dka5QCo+uIWXoiT1w4cX9SV6RGJvghZ2Zavs52/pfi4IdDHzzjG/90b0JzgsqJ0fS8CVZgnnDP1aMK8BcR6LZOmJ2/2CUpXiA054Lup2a5BYWrVtUJO3QmyQEkvUxALR4TjwTGTxNUXuAe9/syAinVVIbxIR 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: Yu-cheng Yu Introduce basic shadow stack enabling/disabling/allocation routines. A task's shadow stack is allocated from memory with VM_SHADOW_STACK flag and has a fixed size of min(RLIMIT_STACK, 4GB). Keep the task's shadow stack address and size in thread_struct. This will be copied when cloning new threads, but needs to be cleared during exec, so add a function to do this. Do not support IA32 emulation or x32. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Switch to EOPNOTSUPP - Use MAP_ABOVE4G - Move set_clr_bits_msrl() to patch where it is first used v4: - Just set MSR_IA32_U_CET when disabling shadow stack, since we don't have IBT yet. (Peterz) v3: - Use define for set_clr_bits_msrl() (Kees) - Make some functions static (Kees) - Change feature_foo() to features_foo() (Kees) - Centralize shadow stack size rlimit checks (Kees) - Disable x32 support v2: - Get rid of unnessary shstk->base checks - Don't support IA32 emulation arch/x86/include/asm/processor.h | 2 + arch/x86/include/asm/shstk.h | 7 ++ arch/x86/include/uapi/asm/prctl.h | 3 + arch/x86/kernel/shstk.c | 146 ++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index e0734f417273..3c257a1a0757 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -479,6 +479,8 @@ struct thread_struct { #ifdef CONFIG_X86_USER_SHADOW_STACK unsigned long features; unsigned long features_locked; + + struct thread_shstk shstk; #endif /* Floating point and extended processor state */ diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 58f9ee675be0..f40414a982e8 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -8,12 +8,19 @@ struct task_struct; #ifdef CONFIG_X86_USER_SHADOW_STACK +struct thread_shstk { + u64 base; + u64 size; +}; + long shstk_prctl(struct task_struct *task, int option, unsigned long features); void reset_thread_features(void); +void shstk_free(struct task_struct *p); #else static inline long shstk_prctl(struct task_struct *task, int option, unsigned long features) { return -EINVAL; } static inline void reset_thread_features(void) {} +static inline void shstk_free(struct task_struct *p) {} #endif /* CONFIG_X86_USER_SHADOW_STACK */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index b2b3b7200b2d..7dfd9dc00509 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -26,4 +26,7 @@ #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 +/* ARCH_SHSTK_ features bits */ +#define ARCH_SHSTK_SHSTK (1ULL << 0) + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 41ed6552e0a5..f39e5d3b9303 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -8,14 +8,160 @@ #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +static bool features_enabled(unsigned long features) +{ + return current->thread.features & features; +} + +static void features_set(unsigned long features) +{ + current->thread.features |= features; +} + +static void features_clr(unsigned long features) +{ + current->thread.features &= ~features; +} + +static unsigned long alloc_shstk(unsigned long size) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G; + struct mm_struct *mm = current->mm; + unsigned long addr, unused; + + mmap_write_lock(mm); + addr = do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL); + + mmap_write_unlock(mm); + + return addr; +} + +static unsigned long adjust_shstk_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); +} + +static void unmap_shadow_stack(u64 base, u64 size) +{ + while (1) { + int r; + + r = vm_munmap(base, size); + + /* + * vm_munmap() returns -EINTR when mmap_lock is held by + * something else, and that lock should not be held for a + * long time. Retry it for the case. + */ + if (r == -EINTR) { + cond_resched(); + continue; + } + + /* + * For all other types of vm_munmap() failure, either the + * system is out of memory or there is bug. + */ + WARN_ON_ONCE(r); + break; + } +} + +static int shstk_setup(void) +{ + struct thread_shstk *shstk = ¤t->thread.shstk; + unsigned long addr, size; + + /* Already enabled */ + if (features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + /* Also not supported for 32 bit and x32 */ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_32bit_syscall()) + return -EOPNOTSUPP; + + size = adjust_shstk_size(0); + addr = alloc_shstk(size); + if (IS_ERR_VALUE(addr)) + return PTR_ERR((void *)addr); + + fpregs_lock_and_load(); + wrmsrl(MSR_IA32_PL3_SSP, addr + size); + wrmsrl(MSR_IA32_U_CET, CET_SHSTK_EN); + fpregs_unlock(); + + shstk->base = addr; + shstk->size = size; + features_set(ARCH_SHSTK_SHSTK); + + return 0; +} + void reset_thread_features(void) { + memset(¤t->thread.shstk, 0, sizeof(struct thread_shstk)); current->thread.features = 0; current->thread.features_locked = 0; } +void shstk_free(struct task_struct *tsk) +{ + struct thread_shstk *shstk = &tsk->thread.shstk; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !features_enabled(ARCH_SHSTK_SHSTK)) + return; + + if (!tsk->mm) + return; + + unmap_shadow_stack(shstk->base, shstk->size); +} + + +static int shstk_disable(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return -EOPNOTSUPP; + + /* Already disabled? */ + if (!features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + fpregs_lock_and_load(); + /* Disable WRSS too when disabling shadow stack */ + wrmsrl(MSR_IA32_U_CET, 0); + wrmsrl(MSR_IA32_PL3_SSP, 0); + fpregs_unlock(); + + shstk_free(current); + features_clr(ARCH_SHSTK_SHSTK); + + return 0; +} + long shstk_prctl(struct task_struct *task, int option, unsigned long features) { if (option == ARCH_SHSTK_LOCK) { From patchwork Thu Jan 19 21:23:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108806 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 50950C6379F for ; Thu, 19 Jan 2023 21:24:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21BE4280005; Thu, 19 Jan 2023 16:24:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1584C280001; Thu, 19 Jan 2023 16:24:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDB20280005; Thu, 19 Jan 2023 16:24:12 -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 B63BC280001 for ; Thu, 19 Jan 2023 16:24:12 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 98BDB1C65BB for ; Thu, 19 Jan 2023 21:24:12 +0000 (UTC) X-FDA: 80372826744.08.9B84F4B Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 6E755180016 for ; Thu, 19 Jan 2023 21:24:10 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ozftb4jo; 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=1674163450; 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=LF9eWuGfmBXCMQNhJgKUQvTdsk+a5XWVtnwIrkM2vbU=; b=qCoHLWoDgohBHg6VK+GKlGuDvqtqaTRt77XUtaVsGPpzCtfKTlgwKEK9i2Xz4zyduuwbBS MghikIsAZevhXKJOHioRk7gXRsjsDW2HQbjcV/eLKUbgzHMvsWIyIcmX0XvP2Lspm5Hba4 LHKEQeHpdVBi1Z/N5gYL7zGX4g+Qewc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ozftb4jo; 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=1674163450; a=rsa-sha256; cv=none; b=3N0IYqQY56huw/JqAyU9DIzKMywHPp9r9baz5praWIVxXq22ldaS89PDKv2/o8kz+ZUd0o oXUEuVoocwhzYUmz3tA3OBhucWiPvvqtQm3wR2Fjepew9myYm/RVN4vpYVthYMgPLzP4xU dRusv4hMDCN+ZluOA8sm9sahGsFxymk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163450; x=1705699450; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=sY0pQZGQdI/cNzp/wwjwE3pcURvBjqOaRcvBZzBOV/8=; b=Ozftb4joJ5KyPLaAWs+T2pt9HItokxRGaWi/yG3a9DPBCGJJ/QpuxFzH URZ7pA5JZ7ZGOVfgwWUmHkV8SocwHQxvRX7oB5CTlnkCbiIrnxLiHByiZ sfHn58TJHkUO6Ic6XEOyrhSBj/Js5aECy3mtBDOYpvCo0ZPJl77xoZbAm RNQVR10YhBkTmadZwCtQRcbJiESYBSCrcT+AszfpgqCaLUu44T3Ff+IDn Fi2QqIXfMDRlS9j9OxGa4aO1aXt7W/oyRm2ZRKea9X5unST7Y86jhBMel 3QN9/iGNWpNKn0pyQUJrPrliz5i0bBJYmXsOOZjRhYb6LhsxWwt4L6Fqg g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119865" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119865" 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:09 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139140" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139140" 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:08 -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, Yu-cheng Yu Subject: [PATCH v5 28/39] x86/shstk: Handle thread shadow stack Date: Thu, 19 Jan 2023 13:23:06 -0800 Message-Id: <20230119212317.8324-29-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: 6E755180016 X-Stat-Signature: 7cdcbepbdn4m7qu65j7c54x9jgb4co6g X-Rspam-User: X-HE-Tag: 1674163450-702265 X-HE-Meta: U2FsdGVkX1+3u4TPMzmrm0vfAs9G9xGMvsX0BC1/Stbv9XPeAE+bZVAdZbUaXo7D4ehMEErirTIcEFqHB9S1VG6W1ybq9w/OpWF4te6+C9Prs1u3TlWixg1HBIFdmMxH/Hxw5O457/nX+4CPB/g6j7/QHQyU8q+kj1/BsZv92WDJVc4iRIHSr0TOIRJq+rbc6exfenhkrzE2HhWGB2BedZ+mbcoc1GtPm/zP9mG339/ZerE4mMG8+UID0LWGJ4MvIKPsKIu1eJp+oLqR12hMhZtvee+c7/Ss0Vm5q3pdniVVd6eRXJ753JEhcaemxSx4BFcprh54s06/54WFZLytdySuCmCaLtyPGcwD1SefcR0Vxjucf/Y/yuFpFUproN4B05kCEVeDMbZnAGM7Jzmf0hjd/CjxqLm2mFcjOYP6w0RGsIdUWc98nD4QOmkb1wD6BhVoToauX2ZveuOiavqevK0735nso+1hpa+YnmwbXdcCaIEkX6vjajcpN3UDKLDU8Rvf6qP+mtFag5MZmX7akpRZW5AF4MRZqvC06BjNnW1MykL19Hfqp1PS/p3bzs+ZxvV+MuwVMJqwGfQytipRaSH+d0+2+/Tc4INn08BeZnxiluTOyTJR5lWSnK4n0JOi2Kr74jKb+ERG+YNSeor4lB9uOSqWLBCecfU2qTW8sW5iNT1dJI9+kX2pYJ0jN3ynjWOeWNurT7n5z3Kl3V4DTRcoWj4j7ct+YpcpfNKuRKTrCB3ZGdQTk+H8QRF8HUhAa2Pb1XP3vPwk1gb4aZEaM76oJKQtuBcOqapja0ho6Go9BNd/8HQZbP2XC2avRtUjDCKeODXGC8tcJG9NyDV7TuM2D7F7/LRAEvauxyodftYmjqe5Cx+KNJWTOIXw+AphgR57OM1Uf3qtvddVMNO+iprRAD4/PaEer2LXagNC7bA3NCWf1wRaTDePiFfTHWcXvAYgaGRAL2K0F7c1qhi 4s+HKVyy jqNqpH/dXnMJIIikqbALSjo0eXXWuKKcUQjgfxY4cV9aCcPQOBk685CdoLXCzQf73mXYmVkEXKiv0wSOT6Cfh0IVcBv4RKfhk6vhNwq0M2lxvfNGhe6uXj3tTLMDtq2lqSR5QH/SHHKzDlR4LIxO68u2zixfsbT5JyjKQgWq0DFCwWnXlDLwqLw8g1M3TfzsqVe4aWsJUznwhiHj+oL4VhigaLF8JLYY1SkJxSr6BZyLkdhrU9cA6qb7KvcBLIlcm8H2jZPTcQGbH2BHSE9V+0xlVwCI1KcXC5Ij2 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: Yu-cheng Yu When a process is duplicated, but the child shares the address space with the parent, there is potential for the threads sharing a single stack to cause conflicts for each other. In the normal non-cet case this is handled in two ways. With regular CLONE_VM a new stack is provided by userspace such that the parent and child have different stacks. For vfork, the parent is suspended until the child exits. So as long as the child doesn't return from the vfork()/CLONE_VFORK calling function and sticks to a limited set of operations, the parent and child can share the same stack. For shadow stack, these scenarios present similar sharing problems. For the CLONE_VM case, the child and the parent must have separate shadow stacks. Instead of changing clone to take a shadow stack, have the kernel just allocate one and switch to it. Use stack_size passed from clone3() syscall for thread shadow stack size. A compat-mode thread shadow stack size is further reduced to 1/4. This allows more threads to run in a 32-bit address space. The clone() does not pass stack_size, which was added to clone3(). In that case, use RLIMIT_STACK size and cap to 4 GB. For shadow stack enabled vfork(), the parent and child can share the same shadow stack, like they can share a normal stack. Since the parent is suspended until the child terminates, the child will not interfere with the parent while executing as long as it doesn't return from the vfork() and overwrite up the shadow stack. The child can safely overwrite down the shadow stack, as the parent can just overwrite this later. So CET does not add any additional limitations for vfork(). Userspace implementing posix vfork() can actually prevent the child from returning from the vfork() calling function, using CET. Glibc does this by adjusting the shadow stack pointer in the child, so that the child receives a #CP if it tries to return from vfork() calling function. Free the shadow stack on thread exit by doing it in mm_release(). Skip this when exiting a vfork() child since the stack is shared in the parent. During this operation, the shadow stack pointer of the new thread needs to be updated to point to the newly allocated shadow stack. Since the ability to do this is confined to the FPU subsystem, change fpu_clone() to take the new shadow stack pointer, and update it internally inside the FPU subsystem. This part was suggested by Thomas Gleixner. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Suggested-by: Thomas Gleixner Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe --- v3: - Fix update_fpu_shstk() stub (Mike Rapoport) - Fix chunks around alloc_shstk() in wrong patch (Kees) - Fix stack_size/flags swap (Kees) - Use centalized stack size logic (Kees) v2: - Have fpu_clone() take new shadow stack pointer and update SSP in xsave buffer for new task. (tglx) v1: - Expand commit log. - Add more comments. - Switch to xsave helpers. Yu-cheng v30: - Update comments about clone()/clone3(). (Borislav Petkov) arch/x86/include/asm/fpu/sched.h | 3 +- arch/x86/include/asm/mmu_context.h | 2 ++ arch/x86/include/asm/shstk.h | 7 +++++ arch/x86/kernel/fpu/core.c | 41 +++++++++++++++++++++++++++- arch/x86/kernel/process.c | 18 +++++++++++- arch/x86/kernel/shstk.c | 44 ++++++++++++++++++++++++++++-- 6 files changed, 110 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/fpu/sched.h b/arch/x86/include/asm/fpu/sched.h index b2486b2cbc6e..54c9c2fd1907 100644 --- a/arch/x86/include/asm/fpu/sched.h +++ b/arch/x86/include/asm/fpu/sched.h @@ -11,7 +11,8 @@ extern void save_fpregs_to_fpstate(struct fpu *fpu); extern void fpu__drop(struct fpu *fpu); -extern int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal); +extern int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal, + unsigned long shstk_addr); extern void fpu_flush_thread(void); /* diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index e01aa74a6de7..9714f08d941b 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -147,6 +147,8 @@ do { \ #else #define deactivate_mm(tsk, mm) \ do { \ + if (!tsk->vfork_done) \ + shstk_free(tsk); \ load_gs_index(0); \ loadsegment(fs, 0); \ } while (0) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index f40414a982e8..172a69052770 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -15,11 +15,18 @@ struct thread_shstk { long shstk_prctl(struct task_struct *task, int option, unsigned long features); void reset_thread_features(void); +int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, + unsigned long stack_size, + unsigned long *shstk_addr); void shstk_free(struct task_struct *p); #else static inline long shstk_prctl(struct task_struct *task, int option, unsigned long features) { return -EINVAL; } static inline void reset_thread_features(void) {} +static inline int shstk_alloc_thread_stack(struct task_struct *p, + unsigned long clone_flags, + unsigned long stack_size, + unsigned long *shstk_addr) { return 0; } static inline void shstk_free(struct task_struct *p) {} #endif /* CONFIG_X86_USER_SHADOW_STACK */ diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 7317bfd5ea36..c72262479f03 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -552,8 +552,41 @@ static inline void fpu_inherit_perms(struct fpu *dst_fpu) } } +#ifdef CONFIG_X86_USER_SHADOW_STACK +static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp) +{ + struct cet_user_state *xstate; + + /* If ssp update is not needed. */ + if (!ssp) + return 0; + + xstate = get_xsave_addr(&dst->thread.fpu.fpstate->regs.xsave, + XFEATURE_CET_USER); + + /* + * If there is a non-zero ssp, then 'dst' must be configured with a shadow + * stack and the fpu state should be up to date since it was just copied + * from the parent in fpu_clone(). So there must be a valid non-init CET + * state location in the buffer. + */ + if (WARN_ON_ONCE(!xstate)) + return 1; + + xstate->user_ssp = (u64)ssp; + + return 0; +} +#else +static int update_fpu_shstk(struct task_struct *dst, unsigned long shstk_addr) +{ + return 0; +} +#endif + /* Clone current's FPU state on fork */ -int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal) +int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal, + unsigned long ssp) { struct fpu *src_fpu = ¤t->thread.fpu; struct fpu *dst_fpu = &dst->thread.fpu; @@ -613,6 +646,12 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal) if (use_xsave()) dst_fpu->fpstate->regs.xsave.header.xfeatures &= ~XFEATURE_MASK_PASID; + /* + * Update shadow stack pointer, in case it changed during clone. + */ + if (update_fpu_shstk(dst, ssp)) + return 1; + trace_x86_fpu_copy_src(src_fpu); trace_x86_fpu_copy_dst(dst_fpu); diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index e57cd31bfec4..13a0a81d70b9 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -48,6 +48,7 @@ #include #include #include +#include #include "process.h" @@ -119,6 +120,7 @@ void exit_thread(struct task_struct *tsk) free_vm86(t); + shstk_free(tsk); fpu__drop(fpu); } @@ -140,6 +142,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) struct inactive_task_frame *frame; struct fork_frame *fork_frame; struct pt_regs *childregs; + unsigned long shstk_addr = 0; int ret = 0; childregs = task_pt_regs(p); @@ -174,7 +177,13 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) frame->flags = X86_EFLAGS_FIXED; #endif - fpu_clone(p, clone_flags, args->fn); + /* Allocate a new shadow stack for pthread if needed */ + ret = shstk_alloc_thread_stack(p, clone_flags, args->stack_size, + &shstk_addr); + if (ret) + return ret; + + fpu_clone(p, clone_flags, args->fn, shstk_addr); /* Kernel thread ? */ if (unlikely(p->flags & PF_KTHREAD)) { @@ -220,6 +229,13 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP))) io_bitmap_share(p); + /* + * If copy_thread() if failing, don't leak the shadow stack possibly + * allocated in shstk_alloc_thread_stack() above. + */ + if (ret) + shstk_free(p); + return ret; } diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index f39e5d3b9303..111ea56115d2 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -47,7 +47,7 @@ static unsigned long alloc_shstk(unsigned long size) unsigned long addr, unused; mmap_write_lock(mm); - addr = do_mmap(NULL, addr, size, PROT_READ, flags, + addr = do_mmap(NULL, 0, size, PROT_READ, flags, VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL); mmap_write_unlock(mm); @@ -126,6 +126,40 @@ void reset_thread_features(void) current->thread.features_locked = 0; } +int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, + unsigned long stack_size, unsigned long *shstk_addr) +{ + struct thread_shstk *shstk = &tsk->thread.shstk; + unsigned long addr, size; + + /* + * If shadow stack is not enabled on the new thread, skip any + * switch to a new shadow stack. + */ + if (!features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + /* + * For CLONE_VM, except vfork, the child needs a separate shadow + * stack. + */ + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) + return 0; + + + size = adjust_shstk_size(stack_size); + addr = alloc_shstk(size); + if (IS_ERR_VALUE(addr)) + return PTR_ERR((void *)addr); + + shstk->base = addr; + shstk->size = size; + + *shstk_addr = addr + size; + + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk; @@ -134,7 +168,13 @@ void shstk_free(struct task_struct *tsk) !features_enabled(ARCH_SHSTK_SHSTK)) return; - if (!tsk->mm) + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. + */ + if (!tsk->mm || tsk->mm != current->mm) return; unmap_shadow_stack(shstk->base, shstk->size); From patchwork Thu Jan 19 21:23:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108807 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 36D67C004D4 for ; Thu, 19 Jan 2023 21:24:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63C5D280006; Thu, 19 Jan 2023 16:24:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C3C4280001; Thu, 19 Jan 2023 16:24:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C84B280006; Thu, 19 Jan 2023 16:24:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 20855280001 for ; Thu, 19 Jan 2023 16:24:14 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E3EC980A3E for ; Thu, 19 Jan 2023 21:24:13 +0000 (UTC) X-FDA: 80372826786.28.5125EA4 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id DC4D34001A for ; Thu, 19 Jan 2023 21:24:11 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="gr8gr1/0"; spf=pass (imf27.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=1674163452; 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=5Fw8vcaOAUH/Ghj13uHxxsRffcwnoPq1Ec7oLYAhwu4=; b=qTKVZyu5YXkjgNg2TOf9IV+nAtp33xNvtJs2/vZbwgLVYOhS/sY10DiX62pRFUcb3cG00I A3GFLN7DPAMSwAf6auWSQgDgQq/3oGvU0zVtpltmuS75gHv4zztrvN0u0nmFNrjiHfeino cTpDaXISQDR2Bc1tsYqKeEc/Umyjq4g= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="gr8gr1/0"; spf=pass (imf27.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=1674163452; a=rsa-sha256; cv=none; b=To+IEarsMjFPgzXpiw+6O+Jo6EwkklLYbN+JrExcDwk3s2/zMelO3C9YtLF5IMqM7BAXYl JrWnGqff9iqD1ZNWzrURMfmWtwNkgEynPnOABdShBBF1TlUAG8Z3VWpzmg8kyPUUVTdKnC 2XjiGwFYBMqSnHM1Uj72PMzjTVUxz2s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163451; x=1705699451; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=JqwiNktiNn9E4briGN+4X4+lTVwiARdl/uQ7DJ6w35c=; b=gr8gr1/0n+LsW//7KHpFG/gsbACNkRcISJKqDj4e2OxqoareP+8sm6p1 gZs9LprNKU+u9i2ftHvetuNWkDs9YXlZKJuLKl4jfyH82kb9eSEywH/7Q q0Kl2XQir5azrY+b2/o+Mk3XWZJ3u5fSnLy6HXbY8TyUBNk+vgOFpkGwD 1kc0ezF8vzFOaUstdKthr1gL81s4Y5A5kSgIyrDH3ZYbFwj/9s+gin4+Q f+kxkSVSPMI9CygP91bnlFErf5axAp6WrKdQykrQ3px4q1AVPKDyvqZV+ 3zkMXJFT/27fYvmHAfTaqoJBLcD9ej8PEfRtrkI5lOFdxRAtU2wJw4tDz Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119888" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119888" 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:11 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139145" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139145" 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:09 -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, Yu-cheng Yu Subject: [PATCH v5 29/39] x86/shstk: Introduce routines modifying shstk Date: Thu, 19 Jan 2023 13:23:07 -0800 Message-Id: <20230119212317.8324-30-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: DC4D34001A X-Stat-Signature: imy11835kr5omt5nc8q81p3e1aofyc7a X-Rspam-User: X-HE-Tag: 1674163451-96865 X-HE-Meta: U2FsdGVkX1/hc6ElvC6Jh2P+Xfxusr0exdhNzlwtyjquCHxHlJHi+DtdVylZXP7SuI02DJzQe9O1bloCmwD2FaliZ8JFuhAI8TOjmfcx6zluU6Pe48kL5U9lKJ9MUd228jsgKTOOZn0n10L4dNxm8TWqZv+HPP3gBU9ifb3OJjg2FWTQkz4m4CzZy1/C4FQDYQXVYxcQpsUSNCvV9wfX7J2J8l8hRFKL+yr60KCmco+7MSHKmYVO/sVA9q1EmpCKnQb4qEYhPvA9iu1FJQlfehkTZ6EGIRie8ZJp7mgehx0WVYgQ6ExwVjDttteUYm09kHRFf0RnccepLCQTsRuYM0HRxfnkIz6BZr1xbbKn/4qxhTSvZCliWoE5CLFt6ZlRjlNGjrbVhYpEX83gnF3LXzguBAg1ET5VMCTyMqbjf/yiQHse9bAZsMDDDpnoltffmhzT5vW55HO893Cka4xalwiAqVLpduyM8jpusEaqiIqP2r9eR8V68lqbFVuN8Kb7hrdoj9+4gwW5FGCxTkflNK73LURkTiZskYps1sCSh83QDjtp3wD3llCCjL5cIvJJ3UdWL2Bbf9imi4qJNQ+nuDoJYcyvAcYYynMOLjHoFjYskSALt84Hgt0m3sBPhPx4OkUwxjrxav0I3tco+HU4Bs60ZfCMytHFBhcEvO4tILdja8fFCuE8XleTewyOr2oQpR6Do5Is1JqNju7IU9/Pzd17GFY6/zW+i0+52heiUMmTvVFgFFjyt2JNJpNd1z2QYsayN1PO3X5QpH8slQdH59uyVPrKPriqk3FsVZJ3AR1L7VxCs7F3tr0hDvl3knrcYoGGxU9Wd4SKklfrT0Q5YwnpvHc/MQbM4r+8Bn6jggU7V331d7iUg7foKMDizOQNLkge+Yz5bFCl2gOtTGPWtocSnvs/TA0/oi+R1DvBXEhFBNhTKzsdireQXG9Lo4EdU9J5c2iK64c7UIx+TkX e19ppzJu 1OLb7IOS0DcL5RQIbif4U3sa8tkluxzwh+7Bp7Ya7Gf3RKXZuHoLDDEoranZjcXBq2BzPjZ/aIWcSUQZXXK/RS5P+wiAvFGHeULbJ5n/30GDdNeebYTnZTjombW5iTOnGGgQqT4vdYsScpPECbJeQJck5uE6U5C8LHwg0omseZpbpXQftnyX8fIdUddSOjcJxGGDobbFt+fFX1hHkdumWKs53xzAcO3NIN6ERPXKmBFVQln0D33VtDvyr7KpDeXgcyouuoy4Phfpu/wdXPjhpB0LreHZ2JydPV80r 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: Yu-cheng Yu Shadow stacks are normally written to via CALL/RET or specific CET instructions like RSTORSSP/SAVEPREVSSP. However during some Linux operations the kernel will need to write to directly using the ring-0 only WRUSS instruction. A shadow stack restore token marks a restore point of the shadow stack, and the address in a token must point directly above the token, which is within the same shadow stack. This is distinctively different from other pointers on the shadow stack, since those pointers point to executable code area. Introduce token setup and verify routines. Also introduce WRUSS, which is a kernel-mode instruction but writes directly to user shadow stack. In future patches that enable shadow stack to work with signals, the kernel will need something to denote the point in the stack where sigreturn may be called. This will prevent attackers calling sigreturn at arbitrary places in the stack, in order to help prevent SROP attacks. To do this, something that can only be written by the kernel needs to be placed on the shadow stack. This can be accomplished by setting bit 63 in the frame written to the shadow stack. Userspace return addresses can't have this bit set as it is in the kernel range. It is also can't be a valid restore token. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Fix typo in commit log v3: - Drop shstk_check_rstor_token() - Fail put_shstk_data() if bit 63 is set in the data (Kees) - Add comment in create_rstor_token() (Kees) - Pull in create_rstor_token() changes from future patch (Kees) v2: - Add data helpers for writing to shadow stack. v1: - Use xsave helpers. arch/x86/include/asm/special_insns.h | 13 +++++ arch/x86/kernel/shstk.c | 73 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index de48d1389936..d6cd9344f6c7 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -202,6 +202,19 @@ static inline void clwb(volatile void *__p) : [pax] "a" (p)); } +#ifdef CONFIG_X86_USER_SHADOW_STACK +static inline int write_user_shstk_64(u64 __user *addr, u64 val) +{ + asm_volatile_goto("1: wrussq %[val], (%[addr])\n" + _ASM_EXTABLE(1b, %l[fail]) + :: [addr] "r" (addr), [val] "r" (val) + :: fail); + return 0; +fail: + return -EFAULT; +} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + #define nop() asm volatile ("nop") static inline void serialize(void) diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 111ea56115d2..3e470917eb0b 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -25,6 +25,8 @@ #include #include +#define SS_FRAME_SIZE 8 + static bool features_enabled(unsigned long features) { return current->thread.features & features; @@ -40,6 +42,35 @@ static void features_clr(unsigned long features) current->thread.features &= ~features; } +/* + * Create a restore token on the shadow stack. A token is always 8-byte + * and aligned to 8. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, 8)) + return -EINVAL; + + addr = ssp - SS_FRAME_SIZE; + + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + ssp |= BIT(0); + + if (write_user_shstk_64((u64 __user *)addr, (u64)ssp)) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + static unsigned long alloc_shstk(unsigned long size) { int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G; @@ -160,6 +191,48 @@ int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, return 0; } +static unsigned long get_user_shstk_addr(void) +{ + unsigned long long ssp; + + fpregs_lock_and_load(); + + rdmsrl(MSR_IA32_PL3_SSP, ssp); + + fpregs_unlock(); + + return ssp; +} + +static int put_shstk_data(u64 __user *addr, u64 data) +{ + if (WARN_ON_ONCE(data & BIT(63))) + return -EINVAL; + + /* + * Mark the high bit so that the sigframe can't be processed as a + * return address. + */ + if (write_user_shstk_64(addr, data | BIT(63))) + return -EFAULT; + return 0; +} + +static int get_shstk_data(unsigned long *data, unsigned long __user *addr) +{ + unsigned long ldata; + + if (unlikely(get_user(ldata, addr))) + return -EFAULT; + + if (!(ldata & BIT(63))) + return -EINVAL; + + *data = ldata & ~BIT(63); + + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk; From patchwork Thu Jan 19 21:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108808 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 02CF5C46467 for ; Thu, 19 Jan 2023 21:24:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21C37280007; Thu, 19 Jan 2023 16:24:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CE4E280001; Thu, 19 Jan 2023 16:24:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E75DB280007; Thu, 19 Jan 2023 16:24:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CAC4C280001 for ; Thu, 19 Jan 2023 16:24:15 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8A410409D2 for ; Thu, 19 Jan 2023 21:24:15 +0000 (UTC) X-FDA: 80372826870.20.D7A1AFC Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 6B3B5180016 for ; Thu, 19 Jan 2023 21:24:13 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZFrDCe6m; 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=1674163453; 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=BfytEEK/+VcJ5OwhEeIY6zyWKiTF9926RCmcxKRzpw0=; b=K4hXYmOGMnY28v0r6ePPoiW+MjqqD6Gk4cs7T6gHZ2zXrWIbKo6BkQmqE1FHpf5loAYON8 M28YkvnVyy5UZkE6skPaly3gKcoZulI71Fu2uMmo/hQ5wt7EzzVk03eyu9krXHuh1Mdj/N u6bko4Hc3pznyfV1zL2IGlVVFilc/A8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZFrDCe6m; 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=1674163453; a=rsa-sha256; cv=none; b=o5Q4kYva0aR9rhxcOylXD0WoUOuBBK7jrBuQYHpJz1cBCsx/zS9rH3HFl/mzAwO2Nv6yo1 y+0ArZ41kgHqSzzNCYFBDCO7W8MmXoKZxBLyU2irk1A1vNWeislGTxvrKbWcKb8XgJhOP0 LduvHgy5DZMWy//Xj7cWr0aZ92i7GB4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163453; x=1705699453; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=9f0c+DcTpRuupUlQlGr5FM1/mXL/cB4F6Skj1zH+SWI=; b=ZFrDCe6mf7KjsQuXitYpC8kPuo//3DL4fu9bJ5IbYJf7gKKpggKBPKdQ uHPy0Nr3Oh39WhvSK2WnHYehVHAKNl7zn/AvYV/DpQ44e8nXx4fJml7pL G4exqKDhm3zpdCro2J83BriPl81rtPPt1x8teSWyBfHvfmop512/CTnQ6 5AXxyBg+JJ/MIs8Bp0fHuOr6NVn4+B/urgllyOLzWNJN14lRVKfj90Uvs xwqTMc6uJz7Zq9vkTaw5m4d8ZuIdWiSHWbhLsYKgp1J/Uf7O5KxidAcSt dVuYD2shciJp1jl1a3LxgkygUMBR8rG0u1Rzn8S2dW9Y15GMGB9h0FliH Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119909" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119909" 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:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139150" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139150" 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:11 -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, Yu-cheng Yu Subject: [PATCH v5 30/39] x86/shstk: Handle signals for shadow stack Date: Thu, 19 Jan 2023 13:23:08 -0800 Message-Id: <20230119212317.8324-31-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: 6B3B5180016 X-Stat-Signature: agy9qeeq3g9tyqyyqqcmwnq4a7rtzir5 X-Rspam-User: X-HE-Tag: 1674163453-297881 X-HE-Meta: U2FsdGVkX1+RLPiHD1aiugJ/GpPsEoCPBozGvi60asgk9FGme17lwLl1wBj+9mx4ZLnai12cktHZSdYcbOqWOFg86BgKw+KUzY4I9se/SAMjmfsQRPJc8kjZbKZOw/BUHDdl4EuHdNjFdk5o9V6RMFGLtWMaQpNAVS7Zd1wqLxEhuMJkfsvsOgvoFFW+V1b+0Nh3xWpYDbe86XQGBDNS6lS5lrndx/hLaHJYH1aQKaQ93EhSnbLf/L3zAjb+6NtH7p35TXurtzaC5VLjVjKLngM7lQJj/fticNkM8HMEoDUNCjv8K6r8OQDiciOhkAdl55nWDp879/e3YyOcFVlplQSxWuJ/Bygkzh/Nq50tMURXkOaX3Znmavv8q1WM4vw8glo3ZrzOhh8oYBVMLyBudiVbGWJCS3y8+4ItHbhfhPiBEewPMeeHv/mQeaR+Fu46ydVuQeMN6P7RAyovlUScWWEH6SHQMypfR3emNXqV9Jg39VNCqBROEGJAsve1KDpObdOwGQiCeeBKffp/IWjw+V0EtEBLrJUgoS6UgTdrF4rQ4nMLOrfXT0NRtrxEgHr92CNIM4YXr7BhuEz4rBcybev8DInty3TPVjniaZd58yCDoaK6m2BpElZ79cJXTKgEGAbehbpVYk5Z81TUBoWAtISKdJXhfVdC+DtWSP9BuCmQojar2A4DQ+gDBtfOJN0bBKN5nNs2WC5yjjkgFY2AEsjLQjYlYZKDnqqfHI2Qz2PkvCant7EhuHSWH3VRUIPfK9ry8wjDvE7XIHChBrzMeizjDpruB5tw2FUFgl75OD5VFJgs9Ysqb+VT/6JH7jKwe1LIKrDH7tngKKfGc+J2lfN+28lnWpDFA1AvdlmV8Vw91gK4Y1yZ+icbPbYEJg/wY5eHOuHlh2uwO/fGiMnu/5zRNaSkmFG5C+r7lK3JSv6aYSTA46S4wbfKQ2R2am6dGlps7Hd/siVphod+Ajs 3yO/xUur roVBzRtX5+j6zfghvRjwZhDdv9tfKkIP2i7q33uxkS0FZbqHTbjv6fj9qoeewe+BJb9eKmdx0JJGgYWucNBIuhmnOk3ednpxk7nOgM3z+VDl92jTiWazvqlFBja4UtL2sdDeYCzixFDmrkq7cUv2BBCZDqiyQ7DnohqGLBvwkpMeCWBu3Sk+wZ4+5CmEQyaeFb6GMYU/uzqpSLV4wp9xoh5n09wmy406YxNZ/R4d17e6ILgD+5KM7LKGpWMG2xalcF1vZOPm3vFgIvLjmH3LVazgKISPQK9aimGtQV4U1rHI1jRDS1rrrnilZwB4Qpyia0flxjw7ayAXp1c9RANYiafj5kLLW9pquhn3swt5Cg4iW3EoSfSbw/RffaRRl6B4z/amf0LwvEkUqhHxhyT3i2apcoPn5K/5MAcfpy4hojyDudsVAswyK64YdnwBAsp/IEvPkiDy4JeIFF+s= 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: Yu-cheng Yu When a signal is handled normally the context is pushed to the stack before handling it. For shadow stacks, since the shadow stack only track's return addresses, there isn't any state that needs to be pushed. However, there are still a few things that need to be done. These things are userspace visible and which will be kernel ABI for shadow stacks. One is to make sure the restorer address is written to shadow stack, since the signal handler (if not changing ucontext) returns to the restorer, and the restorer calls sigreturn. So add the restorer on the shadow stack before handling the signal, so there is not a conflict when the signal handler returns to the restorer. The other thing to do is to place some type of checkable token on the thread's shadow stack before handling the signal and check it during sigreturn. This is an extra layer of protection to hamper attackers calling sigreturn manually as in SROP-like attacks. For this token we can use the shadow stack data format defined earlier. Have the data pushed be the previous SSP. In the future the sigreturn might want to return back to a different stack. Storing the SSP (instead of a restore offset or something) allows for future functionality that may want to restore to a different stack. So, when handling a signal push - the SSP pointing in the shadow stack data format - the restorer address below the restore token. In sigreturn, verify SSP is stored in the data format and pop the shadow stack. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Andy Lutomirski Cc: Cyrill Gorcunov Cc: Florian Weimer Cc: H. Peter Anvin Cc: Kees Cook --- v3: - Drop shstk_setup_rstor_token() (Kees) - Drop x32 signal support, since x32 support is dropped v2: - Switch to new shstk signal format v1: - Use xsave helpers. - Expand commit log. Yu-cheng v27: - Eliminate saving shadow stack pointer to signal context. arch/x86/include/asm/shstk.h | 5 ++ arch/x86/kernel/shstk.c | 98 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/signal.c | 1 + arch/x86/kernel/signal_64.c | 6 +++ 4 files changed, 110 insertions(+) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 172a69052770..746c040f7cb6 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -6,6 +6,7 @@ #include struct task_struct; +struct ksignal; #ifdef CONFIG_X86_USER_SHADOW_STACK struct thread_shstk { @@ -19,6 +20,8 @@ int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size, unsigned long *shstk_addr); void shstk_free(struct task_struct *p); +int setup_signal_shadow_stack(struct ksignal *ksig); +int restore_signal_shadow_stack(void); #else static inline long shstk_prctl(struct task_struct *task, int option, unsigned long features) { return -EINVAL; } @@ -28,6 +31,8 @@ static inline int shstk_alloc_thread_stack(struct task_struct *p, unsigned long stack_size, unsigned long *shstk_addr) { return 0; } static inline void shstk_free(struct task_struct *p) {} +static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return 0; } +static inline int restore_signal_shadow_stack(void) { return 0; } #endif /* CONFIG_X86_USER_SHADOW_STACK */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 3e470917eb0b..56e7ca8e42cc 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -233,6 +233,104 @@ static int get_shstk_data(unsigned long *data, unsigned long __user *addr) return 0; } +static int shstk_push_sigframe(unsigned long *ssp) +{ + unsigned long target_ssp = *ssp; + + /* Token must be aligned */ + if (!IS_ALIGNED(*ssp, 8)) + return -EINVAL; + + if (!IS_ALIGNED(target_ssp, 8)) + return -EINVAL; + + *ssp -= SS_FRAME_SIZE; + if (put_shstk_data((void *__user)*ssp, target_ssp)) + return -EFAULT; + + return 0; +} + +static int shstk_pop_sigframe(unsigned long *ssp) +{ + unsigned long token_addr; + int err; + + err = get_shstk_data(&token_addr, (unsigned long __user *)*ssp); + if (unlikely(err)) + return err; + + /* Restore SSP aligned? */ + if (unlikely(!IS_ALIGNED(token_addr, 8))) + return -EINVAL; + + /* SSP in userspace? */ + if (unlikely(token_addr >= TASK_SIZE_MAX)) + return -EINVAL; + + *ssp = token_addr; + + return 0; +} + +int setup_signal_shadow_stack(struct ksignal *ksig) +{ + void __user *restorer = ksig->ka.sa.sa_restorer; + unsigned long ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + if (!restorer) + return -EINVAL; + + ssp = get_user_shstk_addr(); + if (unlikely(!ssp)) + return -EINVAL; + + err = shstk_push_sigframe(&ssp); + if (unlikely(err)) + return err; + + /* Push restorer address */ + ssp -= SS_FRAME_SIZE; + err = write_user_shstk_64((u64 __user *)ssp, (u64)restorer); + if (unlikely(err)) + return -EFAULT; + + fpregs_lock_and_load(); + wrmsrl(MSR_IA32_PL3_SSP, ssp); + fpregs_unlock(); + + return 0; +} + +int restore_signal_shadow_stack(void) +{ + unsigned long ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + ssp = get_user_shstk_addr(); + if (unlikely(!ssp)) + return -EINVAL; + + err = shstk_pop_sigframe(&ssp); + if (unlikely(err)) + return err; + + fpregs_lock_and_load(); + wrmsrl(MSR_IA32_PL3_SSP, ssp); + fpregs_unlock(); + + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk; diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 004cb30b7419..356253e85ce9 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -40,6 +40,7 @@ #include #include #include +#include static inline int is_ia32_compat_frame(struct ksignal *ksig) { diff --git a/arch/x86/kernel/signal_64.c b/arch/x86/kernel/signal_64.c index 0e808c72bf7e..cacf2ede6217 100644 --- a/arch/x86/kernel/signal_64.c +++ b/arch/x86/kernel/signal_64.c @@ -175,6 +175,9 @@ int x64_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe), &fp); uc_flags = frame_uc_flags(regs); + if (setup_signal_shadow_stack(ksig)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -260,6 +263,9 @@ SYSCALL_DEFINE0(rt_sigreturn) if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) goto badframe; + if (restore_signal_shadow_stack()) + goto badframe; + if (restore_altstack(&frame->uc.uc_stack)) goto badframe; From patchwork Thu Jan 19 21:23:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108809 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 C65CFC004D4 for ; Thu, 19 Jan 2023 21:24:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21D89280008; Thu, 19 Jan 2023 16:24:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1572D280001; Thu, 19 Jan 2023 16:24:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9CC6280008; Thu, 19 Jan 2023 16:24:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CD117280001 for ; Thu, 19 Jan 2023 16:24:17 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A848E1C65DA for ; Thu, 19 Jan 2023 21:24:17 +0000 (UTC) X-FDA: 80372826954.05.E44D5C2 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 9639C40012 for ; Thu, 19 Jan 2023 21:24:15 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QUkyR4YE; spf=pass (imf27.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=1674163456; 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=eLJHhDzZvRpEUGF1Yt556eEckzPL/rnA9Q5SOcyr52E=; b=BsWOSg/+9Rf3ssZ7teyHiH4LnRo92da5FEJVIDEe7eCnwzr2X+XJhDVuToWnQMRIxtVyGN Zb3gm1eWcF9vnQALW8UMfLtcBhpR00Ee/ZmmKz5l7h1UYNn9JQO6F4LqpN7P36PB4eSJAP qpsXPkTNNu4hdmNpmRSlQ1Zi5bl340c= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QUkyR4YE; spf=pass (imf27.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=1674163456; a=rsa-sha256; cv=none; b=UM6eYk+flYTS9nu01YZlXu7qovkpRvHgrRv/fSmc+si9C0JzLvopubBalEJMJt1kuhlUql 8xt2jpi7d5FS07OrHmK5ePGOkzkLvbYxldaBsTAHVboMTct00Mygyzl54mhrTm01xwDhbz +EU/I767Gc98uO+kM5+rsGHcJp8TByE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163455; x=1705699455; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Lpl3uucf4iqvzsR2gzdDBd0io6AfGsBhVzdSqVLnSOA=; b=QUkyR4YEbZI/+z1MjRr3UksmUh7QvXDCNtB+cqVYStJAgd2SHezCW5Ol kmZf/xQ5GNDFN2W0ZZZxBDOqEXiRC8PEJwfhbLJJA6uReC0H7OYOh/dRb 3hkRxeIQHgY3ma//qeTIUa9oqq8c2CwmN7areJiNQlCZsQhiJBkbOP0M+ CE2Ad7WocoJxF8jHTn9/hINy6zMt+RMqmF9MCl50aW8C7sqVYqFqcpU7x /fehYI837/rbr+bDkHvOjijMRh/MJVroLUu92NG1TFKUrnHHaB1NKRLGi Nf7Y6imaVNBFJiUZrGo7LzHpeV9RX2BeSCMYCa0ljtJBC0iHhmYBuZxfD w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119932" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119932" 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:14 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139157" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139157" 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:12 -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 31/39] x86/shstk: Introduce map_shadow_stack syscall Date: Thu, 19 Jan 2023 13:23:09 -0800 Message-Id: <20230119212317.8324-32-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: 9639C40012 X-Stat-Signature: ku5zzkynw4f1z5n46d3gcydp8qpny47w X-Rspam-User: X-HE-Tag: 1674163455-602105 X-HE-Meta: U2FsdGVkX18lx3bWZ/iz0ZJJfbA9PFJJ/xiCtbsFK7L5wJSj1GAwpYzY86BJngb5Ld3im6QCLblVV1Iu6i278b1xwpx2zHtk9T2UqIR19QudBYzgC2fspq2eI8cc9R71O5J/LwLgj10ZqvSL5N9bBQnhwxPR+oVAet8NNmsciM5FAfUkQSGlEGlX2v8OGfM0jbq2vIe3EeSi3sXdk/1zu8NQH17Pbl5dk5ussTqbrZL8hlltCPF2JUGnyaecboUz3Ym2kapiMwyfB3p1VY2DniIqm9TpYnu8+E8+75HuoY2kpUIDDJ1xjRFZlytTgdJvrPsaFMAvhyiKqfvJfwgv66KvFizJCjdgH59up/Kcb/H/BZXV6xpnY0JzEctODwBimJgd2+9wXlyisTg9bMChVAJKXBSGxlmhy2j+Qjg9bTwfgqogLNnd/Y12gpYiThQ93P0i1hoenP3XOBW3Xn2ckE2fimZfKwexrFkt9jB54Ke1J8pZx/hY9uIlUb/wn52UDKp6R78Dc4REE6d+Me2dtRb9b6aeC5VcvdqNzZbg20P4QHEz3ZwGhQ2dS+G8q1Csyfs20RHd7aIJ1g1tVV8rwlh9mq+nmbwMSnU/TQWgEhLqSeSMl15kdyDj61FrMtUhqZkRHmzcTbgcoO84pdGv+61ODZ2Tm/J5aXAv+D9sIwBaAnjSmI+Xv/w+vM/g4UbIaKrJ3o0+iZS6qYMVnQIC1O4yempoAt6KunmwI4d4tHcejZX7lumfJz315Oh0JaFYKrhqXsSpUGUPkZOaNEnhK1EUbcHvzr5LkNmjvuPKeZYlWJDLjlN3i3uX5BLSthudxJbncArrzoHv5S8hN4Hi41ODIXfCljArdctEAb05lsiMhRJhhX6qH1nwk6nbonHauClEwYW/aDCOrm9f0vrmByJaoFpCaJvziLUlNLfaP8uL9hy4tZH8xymD6J/tC89Za+lFibispYZ48yfqQf0 wIakRmh0 n+LsEroKv0cL8rBNBe4x3sw7SWjpRhZOyWXIpdaRAl6h5J2ggrB21nRp1RJErYcNMUefNybYlVhThtuZ0lcujq79mjwnhpb1fMnALodFTzAnkP1RtAddm7pbA6mWhbOToPhdUoZRj0Qby4Yzw4PjcagerO1jDqaIfUNJRkN6cKneVrtiFh/ltdpwOe22XAhU7iM3Segy8aNJ/2bV6Cu8Ggej3X5VjPSE92bsYczH1FiMEfxCqp1FN/YaPiMNc8fgARFRDWmRJMvQ931friWl0INZpEw== 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: When operating with shadow stacks enabled, the kernel will automatically allocate shadow stacks for new threads, however in some cases userspace will need additional shadow stacks. The main example of this is the ucontext family of functions, which require userspace allocating and pivoting to userspace managed stacks. Unlike most other user memory permissions, shadow stacks need to be provisioned with special data in order to be useful. They need to be setup with a restore token so that userspace can pivot to them via the RSTORSSP instruction. But, the security design of shadow stack's is that they should not be written to except in limited circumstances. This presents a problem for userspace, as to how userspace can provision this special data, without allowing for the shadow stack to be generally writable. Previously, a new PROT_SHADOW_STACK was attempted, which could be mprotect()ed from RW permissions after the data was provisioned. This was found to not be secure enough, as other thread's could write to the shadow stack during the writable window. The kernel can use a special instruction, WRUSS, to write directly to userspace shadow stacks. So the solution can be that memory can be mapped as shadow stack permissions from the beginning (never generally writable in userspace), and the kernel itself can write the restore token. First, a new madvise() flag was explored, which could operate on the PROT_SHADOW_STACK memory. This had a couple downsides: 1. Extra checks were needed in mprotect() to prevent writable memory from ever becoming PROT_SHADOW_STACK. 2. Extra checks/vma state were needed in the new madvise() to prevent restore tokens being written into the middle of pre-used shadow stacks. It is ideal to prevent restore tokens being added at arbitrary locations, so the check was to make sure the shadow stack had never been written to. 3. It stood out from the rest of the madvise flags, as more of direct action than a hint at future desired behavior. So rather than repurpose two existing syscalls (mmap, madvise) that don't quite fit, just implement a new map_shadow_stack syscall to allow userspace to map and setup new shadow stacks in one step. While ucontext is the primary motivator, userspace may have other unforeseen reasons to setup it's own shadow stacks using the WRSS instruction. Towards this provide a flag so that stacks can be optionally setup securely for the common case of ucontext without enabling WRSS. Or potentially have the kernel set up the shadow stack in some new way. The following example demonstrates how to create a new shadow stack with map_shadow_stack: void *shstk = map_shadow_stack(addr, stack_size, SHADOW_STACK_SET_TOKEN); Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix addr/mapped_addr (Kees) - Switch to EOPNOTSUPP (Kees suggested ENOTSUPP, but checkpatch suggests this) - Return error for addresses below 4G v3: - Change syscall common -> 64 (Kees) - Use bit shift notation instead of 0x1 for uapi header (Kees) - Call do_mmap() with MAP_FIXED_NOREPLACE (Kees) - Block unsupported flags (Kees) - Require size >= 8 to set token (Kees) v2: - Change syscall to take address like mmap() for CRIU's usage v1: - New patch (replaces PROT_SHADOW_STACK). arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/include/uapi/asm/mman.h | 3 ++ arch/x86/kernel/shstk.c | 59 ++++++++++++++++++++++---- include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 2 +- kernel/sys_ni.c | 1 + 6 files changed, 58 insertions(+), 9 deletions(-) diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index c84d12608cd2..f65c671ce3b1 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -372,6 +372,7 @@ 448 common process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node +451 64 map_shadow_stack sys_map_shadow_stack # # Due to a historical design error, certain syscalls are numbered differently diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h index 5a0256e73f1e..8148bdddbd2c 100644 --- a/arch/x86/include/uapi/asm/mman.h +++ b/arch/x86/include/uapi/asm/mman.h @@ -13,6 +13,9 @@ ((key) & 0x8 ? VM_PKEY_BIT3 : 0)) #endif +/* Flags for map_shadow_stack(2) */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ + #include #endif /* _ASM_X86_MMAN_H */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 56e7ca8e42cc..e857083b9e14 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -71,19 +72,31 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) return 0; } -static unsigned long alloc_shstk(unsigned long size) +static unsigned long alloc_shstk(unsigned long addr, unsigned long size, + unsigned long token_offset, bool set_res_tok) { int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G; struct mm_struct *mm = current->mm; - unsigned long addr, unused; + unsigned long mapped_addr, unused; - mmap_write_lock(mm); - addr = do_mmap(NULL, 0, size, PROT_READ, flags, - VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL); + if (addr) + flags |= MAP_FIXED_NOREPLACE; + mmap_write_lock(mm); + mapped_addr = do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL); mmap_write_unlock(mm); - return addr; + if (!set_res_tok || IS_ERR_VALUE(mapped_addr)) + goto out; + + if (create_rstor_token(mapped_addr + token_offset, NULL)) { + vm_munmap(mapped_addr, size); + return -EINVAL; + } + +out: + return mapped_addr; } static unsigned long adjust_shstk_size(unsigned long size) @@ -134,7 +147,7 @@ static int shstk_setup(void) return -EOPNOTSUPP; size = adjust_shstk_size(0); - addr = alloc_shstk(size); + addr = alloc_shstk(0, size, 0, false); if (IS_ERR_VALUE(addr)) return PTR_ERR((void *)addr); @@ -179,7 +192,7 @@ int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, size = adjust_shstk_size(stack_size); - addr = alloc_shstk(size); + addr = alloc_shstk(0, size, 0, false); if (IS_ERR_VALUE(addr)) return PTR_ERR((void *)addr); @@ -373,6 +386,36 @@ static int shstk_disable(void) return 0; } +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return -EOPNOTSUPP; + + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* If there isn't space for a token */ + if (set_tok && size < 8) + return -EINVAL; + + if (addr && addr <= 0xFFFFFFFF) + return -EINVAL; + + /* + * An overflow would result in attempting to write the restore token + * to the wrong location. Not catastrophic, but just return the right + * error code and block it. + */ + aligned_size = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return alloc_shstk(addr, aligned_size, size, set_tok); +} + long shstk_prctl(struct task_struct *task, int option, unsigned long features) { if (option == ARCH_SHSTK_LOCK) { diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 33a0ee3bcb2e..392dc11e3556 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1058,6 +1058,7 @@ asmlinkage long sys_memfd_secret(unsigned int flags); asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, unsigned long home_node, unsigned long flags); +asmlinkage long sys_map_shadow_stack(unsigned long addr, unsigned long size, unsigned int flags); /* * Architecture-specific system calls diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 45fa180cc56a..b12940ec5926 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -887,7 +887,7 @@ __SYSCALL(__NR_futex_waitv, sys_futex_waitv) __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node) #undef __NR_syscalls -#define __NR_syscalls 451 +#define __NR_syscalls 452 /* * 32 bit systems traditionally used different diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index 860b2dcf3ac4..cb9aebd34646 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -381,6 +381,7 @@ COND_SYSCALL(vm86old); COND_SYSCALL(modify_ldt); COND_SYSCALL(vm86); COND_SYSCALL(kexec_file_load); +COND_SYSCALL(map_shadow_stack); /* s390 */ COND_SYSCALL(s390_pci_mmio_read); From patchwork Thu Jan 19 21:23:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108810 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 A3E4CC6379F for ; Thu, 19 Jan 2023 21:24:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CE3A280009; Thu, 19 Jan 2023 16:24:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 157BD280001; Thu, 19 Jan 2023 16:24:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC389280009; Thu, 19 Jan 2023 16:24:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D6D37280001 for ; Thu, 19 Jan 2023 16:24:18 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B0CB6160E15 for ; Thu, 19 Jan 2023 21:24:18 +0000 (UTC) X-FDA: 80372826996.25.754E283 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id B09B9180016 for ; Thu, 19 Jan 2023 21:24:16 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VLWcf1GM; 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=1674163457; 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=e6Ucp8srnUL/pIV8RzS949UTzc2xSGWpmpvzoBTxJjU=; b=RM0d2MagJyiM+xe+IsKZuokpoqKSgaAmVFHlfZ7HsKNMTMFh2YYw8RM+4PR3Gi7ddpFMBH DZGkbnpkMoRIOxEImqoy1M0QlQLRt8qVc4F/Ys9Ye3MH8x1VdrsHwMDhfC83i986tn5X1a +MFBOkMyWCUOieYDlulR3zZuCHEUj/w= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VLWcf1GM; 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=1674163457; a=rsa-sha256; cv=none; b=38YZKv+OKSB14nyY+CytrmEJt2axZRU7EBoDLOBjdX4bFqw7on9E8V0mzAco8oENBkS/hE AfhVxMxbuDMFD8oJPZmfmNCMcOT7PQjuxGnrMIX4tLI9StL3UJz1ERZogEfS+kjIuR6aMV 2bV5fjYvQ6N4kVbhvjI05mYVpjdF0vQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163456; x=1705699456; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=miL5v57uRD6gfvhLMCiJeO3l14zaMekSGW7g+Pa5v08=; b=VLWcf1GMSmVIXib8mIGh/H1nPovuzJoISWtACZyvN1O9JH7tIE4LybK6 GF6YpF3jPdICOLnkUxPuJlVQttPsJxq7HSotdyt8NoZtcKJ+0ZruLadw2 fJd8nslzNhvPiC4Kyajuf6f3mHC8pPczU/1sHzaC87KhY5ihyHOcyxfhZ x1Gv7dg6CYEtt7W60Yjb4zvgkkny88vRITfc2tI0FHRrwQCCV4xvv8bS8 zmLcLz0BbNHC0SL9LTvcDhQHqDjqtbm/VCyTmWptaRAqqZ5DlLObP42Kr tIwf/RrPaabGVHSTbK/20S0aWp3Qz58u6xRZFyWn4V0wWnZm6bXAsrYjK w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119953" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119953" 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:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139164" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139164" 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:14 -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 32/39] x86/shstk: Support WRSS for userspace Date: Thu, 19 Jan 2023 13:23:10 -0800 Message-Id: <20230119212317.8324-33-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: B09B9180016 X-Stat-Signature: t87sod6rx7eqydim47b8qfj615ie4bjw X-Rspam-User: X-HE-Tag: 1674163456-249550 X-HE-Meta: U2FsdGVkX1/oAO+i8VbZMrUXUS5kPLNMPFZK6vD7AQC5RwffOChO5Dd86LPjmxjmxTOlkhyACB3C2B76sDzEznJo4IMDBiKYxUcXhzxXs+uiE7CMM9TqpzLZcTJyqfLH+MTLmkEpP/mqG2LnFcU8rXn/iYfHNIEhpNB32GKRmcwpJazLoeEY+Le8biSDQ4JRco9FOTZE6gqBJ39j9bxYEpqYyq1GO3obhsH1KXvSpomJcnOdtuqfqCj5MnLsG3xRHSGvq9QsZt8BTdlcVplgVPxngIk8r3a+Oc6Bz5bycQnGhj+RTJ99QMqhWwqvMDGNeKdSus+0sXh5zFeTDMYf9/2kCTsuFsowgqI9F62tTzqqzEW9mhsVUCtlUY955I043NaFNwrOk4MxGdzoyUD2OwLp23gtF3ptaTssKJ2uHvkVZq17c8LP9mossrd4SMSTezPVgBOJqdjAr9P3gYg4HV/hevsiln2Kpaf5I4MM3mLIAv/K9zf6QZmD1zfoKZjo8wqxWWxL08Uz/LDTvSCyeRlilqEQlB5pkGvY4FEbqeyilt8/XCYlXyw4/dtWkfXwezLLqoeJrvkOclR4r6fhBFGWTWJAsmCkbX6V3aKvk7fjYHW6iWulCRw9SnnF82cumgvd1WvEP4jQ5mm8z4pCbEwg8aLXpfIulbzzdUjYhETU6b08wJqYqM6hBzKPKM9l8uHltJEJNhRFXNai7l+ECrc0sRi4zMRwHRTMzPSN12vJF2KkzK61/ejxqob14ja6NLHtPc3JOtZyort3RzWCeBudy+htNIVFdsLhDFOc15ajpE7A8AtrcAoSXwjqQLQ1mqxaV0ETRFI/30Lo1btTV9JMUidmgBG4hMPixdFPfgGFgfBmQgyOIatSxF/JgLZ3CNsqlPyO4NdUQVgZ4sSzcJnCNEaxkllkJDf53O36X1nyYT4JT1KynlXqWP7cf8hqnBYyKEPB7RQZn4PZrZM ZtY03McF AxutgM0CYr56XdoYWH0C2vAhv3cnXRJcPBBFVZ51poNzM88nJTFdmelvCZCy2Z4U8ml82Epf1n5/6pq191LiQP0reQgKga2pxgaBoJ6IJy57RKd9htRRicCF0T6oTaRLx9Kyr0F4rDngfsCDL6CyPciQyEiMVoPHa5DyIb5B7z2n8JYgfQ0+b5nZUmmvfRUPx0uPYBN/tzRq0IzqDJ0i4x7jZ/UbUT6InRvk1i9qTFaTvXQU= 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: For the current shadow stack implementation, shadow stacks contents can't easily be provisioned with arbitrary data. This property helps apps protect themselves better, but also restricts any potential apps that may want to do exotic things at the expense of a little security. The x86 shadow stack feature introduces a new instruction, WRSS, which can be enabled to write directly to shadow stack permissioned memory from userspace. Allow it to get enabled via the prctl interface. Only enable the userspace WRSS instruction, which allows writes to userspace shadow stacks from userspace. Do not allow it to be enabled independently of shadow stack, as HW does not support using WRSS when shadow stack is disabled. From a fault handler perspective, WRSS will behave very similar to WRUSS, which is treated like a user access from a #PF err code perspective. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Switch to EOPNOTSUPP - Move set_clr_bits_msrl() to patch where it is first used - Commit log formatting v3: - Make wrss_control() static - Fix verbiage in commit log (Kees) v2: - Add some commit log verbiage from (Dave Hansen) v1: - New patch. arch/x86/include/asm/msr.h | 11 +++++++++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/shstk.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 65ec1965cd28..a4b86eb537d6 100644 --- a/arch/x86/include/asm/msr.h +++ b/arch/x86/include/asm/msr.h @@ -310,6 +310,17 @@ void msrs_free(struct msr *msrs); int msr_set_bit(u32 msr, u8 bit); int msr_clear_bit(u32 msr, u8 bit); +/* Helper that can never get accidentally un-inlined. */ +#define set_clr_bits_msrl(msr, set, clear) do { \ + u64 __val, __new_val; \ + \ + rdmsrl(msr, __val); \ + __new_val = (__val & ~(clear)) | (set); \ + \ + if (__new_val != __val) \ + wrmsrl(msr, __new_val); \ +} while (0) + #ifdef CONFIG_SMP int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 7dfd9dc00509..e31495668056 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -28,5 +28,6 @@ /* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) +#define ARCH_SHSTK_WRSS (1ULL << 1) #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index e857083b9e14..71dbb49b93cd 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -364,6 +364,35 @@ void shstk_free(struct task_struct *tsk) unmap_shadow_stack(shstk->base, shstk->size); } +static int wrss_control(bool enable) +{ + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + return -EOPNOTSUPP; + + /* + * Only enable wrss if shadow stack is enabled. If shadow stack is not + * enabled, wrss will already be disabled, so don't bother clearing it + * when disabling. + */ + if (!features_enabled(ARCH_SHSTK_SHSTK)) + return -EPERM; + + /* Already enabled/disabled? */ + if (features_enabled(ARCH_SHSTK_WRSS) == enable) + return 0; + + fpregs_lock_and_load(); + if (enable) { + set_clr_bits_msrl(MSR_IA32_U_CET, CET_WRSS_EN, 0); + features_set(ARCH_SHSTK_WRSS); + } else { + set_clr_bits_msrl(MSR_IA32_U_CET, 0, CET_WRSS_EN); + features_clr(ARCH_SHSTK_WRSS); + } + fpregs_unlock(); + + return 0; +} static int shstk_disable(void) { @@ -381,7 +410,7 @@ static int shstk_disable(void) fpregs_unlock(); shstk_free(current); - features_clr(ARCH_SHSTK_SHSTK); + features_clr(ARCH_SHSTK_SHSTK | ARCH_SHSTK_WRSS); return 0; } From patchwork Thu Jan 19 21:23:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108811 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 A61A7C004D4 for ; Thu, 19 Jan 2023 21:24:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEDA028000A; Thu, 19 Jan 2023 16:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E292E280001; Thu, 19 Jan 2023 16:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C308528000A; Thu, 19 Jan 2023 16:24:20 -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 A4981280001 for ; Thu, 19 Jan 2023 16:24:20 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6EAF91C65BC for ; Thu, 19 Jan 2023 21:24:20 +0000 (UTC) X-FDA: 80372827080.12.DBCBEE8 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 5E50F40017 for ; Thu, 19 Jan 2023 21:24:18 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WkYbOLMw; spf=pass (imf27.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=1674163458; 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=upYGst2M+pM9taclLJcrPM9etxXHvFq2ULPd7LORduM=; b=GUNApVzOkK3W5m4oXgi8XUwXcMDc17WJf2IeYevD81GuB2+bsEXSUpCZaAyj4/rQaYHJ01 0pMKTqbvyNk9sZZFDRkwDbK6aIDBg+oYhOdgbBEZ9j0juess7I95n4qdEx92WkfO43eLrB QejB67z3V0wMUqloJ8e2zW3PKW+39RU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WkYbOLMw; spf=pass (imf27.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=1674163458; a=rsa-sha256; cv=none; b=vRw0cdSM7FAYFqweqNlqC42l4wbATnBIHFGpQZfkOR0uXWTpY1UUUdDc46IejofQ6csLj2 TqLJ24mMiIBvJi1+D7zR5vS5p60R5gC9noWythhSfOavQXtikDAxeYo9luuZYgSjeFBRQQ 0pXjWTROciGzQHWOT4L2IohRpvwV6zQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163458; x=1705699458; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=vY8qQFdDU3u0a0tvehCZCS7MwIYptOrO+eN8SXLOirs=; b=WkYbOLMwdw7lrIkE6D2/e+FlWZTuhSpMQG9coYxAGzFLGcgmeaB6sS+o /BTyHtH5zV8iFj/2a0ZbPc7Hpc+SD/Ma/zB6ee5DSR8cMI60NTFUXtZRM WAuVaIHZpD+Hq0OPtMv/HAfCHGrqrwPkLJbQmzhogheAeIu8i4mrtE18g 8oZMLl7+lIg52TZgemS68g3X2NNuzKHhLmCP9zRdoxP2w1nrV6Bor8/av gxL5Ss8oV2Iy5lEYZiazF5t/PrYndtuZv3K77iyra2jVuEAt/zAfzH8EM DJumBDXfH01vfkSLXEVcMH5pnuMyjnxP9cGHPZ1JLSvg+EN7BDXzh/hxp Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119974" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119974" 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:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139169" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139169" 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:16 -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 33/39] x86: Expose thread features in /proc/$PID/status Date: Thu, 19 Jan 2023 13:23:11 -0800 Message-Id: <20230119212317.8324-34-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: 5E50F40017 X-Stat-Signature: fr4f966fnmrs7u1soe7bop3ba6ufue8n X-Rspam-User: X-HE-Tag: 1674163458-119854 X-HE-Meta: U2FsdGVkX194qJQ532dxaZeq8qX0NgRzXo747ndbSXjaBhCikrjgBsR4wHNdPPfr8LALlOpC/7wcv621ukx2PYfnPSP7DqOKcd3ikN7S1AIi9seAoShaScAs9IjiN+H8F5lEDjXX0LtdAdGljDJxzcnyqaxhSGjOfTx1vlnjhUywwoPCgfW81/9tIkq3JAgCFwZFc0YqH7HEF/mZ6f0qdo+wPm/VIGh0jHrvpCuXH3ZmifUODpU91Kw+jwvv505tHsrL/NR2rHRbks/K+JZ4zpOucmiVCKtNU2MW01f4gfMtbd5XzxqJ0j9MTCDjHvmmYA55obpSUljEi5AvlixWzAEt1cA31QxxmqKdBwG5juK3ceKsSSLKAa6AZE7tHReVnG4xKhiDNbYrnV22Y1wxiPELjiMpNmlo+/1sHwdo2eZTamkdnCi8uir+18LcJrRj3uB57W7QLWNUpNsCjyTdzpWhD/tXP3D65DmPS1vHundGDC3ZK7MDAnae3GAYhaVpAm4YGWRqZnVXN5WssRAlvhZmv4ksthfXBydpUPSJPv4xgkwcWrwGsSAr4F8+IrG+gr4uRJh+rEG16K48twyob+mB6lLTVfjpIjd9FfuTn4JGtedREmdKL0h+qUqNmNmJ5Ka8J4EibaiKOaFKkDqo0sEv0BQ0qikdCUkqunevp4FmtAi392lZrX2mKsEX5O3Hl1Rp2+NWOyD/IzggOw8XzQvwVlaZMzBKdjgVgd9efjHdIF/RrdRgZ1UJvz6lyymSq6KRo6P1e561lfoPJx0GWeevNbI19WNZsVqn5Jk39iIc1sJMg2vWjkMFjreQt6j07Cluh+LfocJtdLUynm35TECp600/WZbCMkZ7hLKvHV7pTyMzFKXBcvAkfUwV5FoQqln5gDe4+MnGYWpKrTrKZ6OpqPrQ/ruku29jtd9bXtWfdi/ABME/8K1JHo23nZl5uomOAgu8b4bTjpJfSS0 Y3LbQXbf RfSOL1s/cQp/MhvbhusPsnQ0ffFbGpaWOqAyPO3p1R4WyOqsZI9byuxj16lwAs2uiQcllPTr5rCs4nZtVXefAvFHvHuGVE8YJL5xn/qBZcH7C0EG8wGXVqmPOu8Rbjf5cj9O4IDE6M+DBzvwkdq7zAYDCkVGlMYYpBfNX9CqmHUb8vWelyiJDNyaAwxLMH+fPwpAY7XQ4ilQ0TM+97DtsHfAbi1/jKYmrQf17S0c8AgHpHQPyMzEDYfa5fv2rXLCXNoQa6X38NTmN2ZQuYxlpPVExPEoLoPIlXtmGiW/7xX61mrrq2tnI+pvmVM2ZErGew5wW/LySugOQDfQ= 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: Applications and loaders can have logic to decide whether to enable shadow stack. They usually don't report whether shadow stack has been enabled or not, so there is no way to verify whether an application actually is protected by shadow stack. Add two lines in /proc/$PID/status to report enabled and locked features. Since, this involves referring to arch specific defines in asm/prctl.h, implement an arch breakout to emit the feature lines. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Kirill A. Shutemov [Switched to CET, added to commit log] Signed-off-by: Rick Edgecombe --- v4: - Remove "CET" references v3: - Move to /proc/pid/status (Kees) v2: - New patch arch/x86/kernel/cpu/proc.c | 23 +++++++++++++++++++++++ fs/proc/array.c | 6 ++++++ include/linux/proc_fs.h | 2 ++ 3 files changed, 31 insertions(+) diff --git a/arch/x86/kernel/cpu/proc.c b/arch/x86/kernel/cpu/proc.c index 099b6f0d96bd..31c0e68f6227 100644 --- a/arch/x86/kernel/cpu/proc.c +++ b/arch/x86/kernel/cpu/proc.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "cpu.h" @@ -175,3 +177,24 @@ const struct seq_operations cpuinfo_op = { .stop = c_stop, .show = show_cpuinfo, }; + +#ifdef CONFIG_X86_USER_SHADOW_STACK +static void dump_x86_features(struct seq_file *m, unsigned long features) +{ + if (features & ARCH_SHSTK_SHSTK) + seq_puts(m, "shstk "); + if (features & ARCH_SHSTK_WRSS) + seq_puts(m, "wrss "); +} + +void arch_proc_pid_thread_features(struct seq_file *m, struct task_struct *task) +{ + seq_puts(m, "x86_Thread_features:\t"); + dump_x86_features(m, task->thread.features); + seq_putc(m, '\n'); + + seq_puts(m, "x86_Thread_features_locked:\t"); + dump_x86_features(m, task->thread.features_locked); + seq_putc(m, '\n'); +} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ diff --git a/fs/proc/array.c b/fs/proc/array.c index 49283b8103c7..7ac43ecda1c2 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -428,6 +428,11 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm) seq_printf(m, "THP_enabled:\t%d\n", thp_enabled); } +__weak void arch_proc_pid_thread_features(struct seq_file *m, + struct task_struct *task) +{ +} + int proc_pid_status(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { @@ -451,6 +456,7 @@ int proc_pid_status(struct seq_file *m, struct pid_namespace *ns, task_cpus_allowed(m, task); cpuset_task_status_allowed(m, task); task_context_switch_counts(m, task); + arch_proc_pid_thread_features(m, task); return 0; } diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 0260f5ea98fe..80ff8e533cbd 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -158,6 +158,8 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task); #endif /* CONFIG_PROC_PID_ARCH_STATUS */ +void arch_proc_pid_thread_features(struct seq_file *m, struct task_struct *task); + #else /* CONFIG_PROC_FS */ static inline void proc_root_init(void) From patchwork Thu Jan 19 21:23:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108812 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 8CF57C46467 for ; Thu, 19 Jan 2023 21:24:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FE9C28000B; Thu, 19 Jan 2023 16:24:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6891D280001; Thu, 19 Jan 2023 16:24:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B27F28000B; Thu, 19 Jan 2023 16:24:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 31A9E280001 for ; Thu, 19 Jan 2023 16:24:22 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0932A14047C for ; Thu, 19 Jan 2023 21:24:22 +0000 (UTC) X-FDA: 80372827164.28.8EA6DD4 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id E7114180019 for ; Thu, 19 Jan 2023 21:24:19 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NZgTc1jt; 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=1674163460; 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=LARyYXf5hRWem17uRVeQYirgVhi/Ae5Eh0pNi1jng38=; b=iKzW/ska0zeTKj3BIxU2DRShEySIf7Yoz5tZljHjkUNDDiu+VTAMiPTshAKbnEAczK8RH9 6Jlxg+QezbpMJyqreOh1zXPTG8e52XowLx6OUpnUr2Mwo051fi+859eodr6VmTlUTr0/zE U1j/s/2ig3pHYzR3P29NOHvqtJ3z6so= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NZgTc1jt; 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=1674163460; a=rsa-sha256; cv=none; b=6obqklqcuRFTl+VQm3JJrBydwb/5JWV60tIIys8jnbukAiobUNmlw32fXJ+yhYVcFniiKN md2CtcL06PvBwUqACRtPgN82BN2Szs5msW0D+Lv6KB6S9uKQ+HswDi8l6CVQmjlCpITpTL b0D3QKyFyilauJ6cBM/LWtlxOHzVC5w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163460; x=1705699460; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=sQY1E+dUSY4CcT1mmXrJaIQvOz4XoIsJlzVbJWGjXhw=; b=NZgTc1jt2DsiaWUvsDhRzRe12ovOKWRrQgpfkNqdvTI2Tr21JIe89XtO MJJ36Xa2zx1zeGOQbnyC0HD4fV8cVsWOILZsyHN8FK4WaJHFunaYMYQ9u Up98qvoMoAIF+I+DmZWO11SO3uI1awVKWkenCU4XZ6QqA3OjB98EqmTSb 1CVOfZr+S20a4GfzvBTuwLSQc70Bq2YtAe5JhbCQPUZwciceJ+K8fom8W lTkMGCMuMoIACYgi1iNbIDLI9hjlN1m7Xp7meOF3ADNLmprCD5BCJdPyA 2hHkTXLnuTFqk9PZfJlmu6ZwK7TGmpPG10VsopkrkXH7oaittSYKMP+Tb g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119998" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119998" 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:19 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139174" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139174" 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:17 -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 34/39] x86/shstk: Wire in shadow stack interface Date: Thu, 19 Jan 2023 13:23:12 -0800 Message-Id: <20230119212317.8324-35-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: E7114180019 X-Stat-Signature: hgnfiw7pso13x9timr8hwhwxayctopz1 X-Rspam-User: X-HE-Tag: 1674163459-960724 X-HE-Meta: U2FsdGVkX18ckPHeD+C7k+ealdHAklneaNEkfZqpA8m/pok+H9ky2JKcyqjJquwZeBLkJFKf4UuthJlDMUQqkDwe6E2potcTwsfwG5qA7OYez0leu0uOVc44nWqqLK4iR6Tsdmwz6no/PwCbfPOfLY7O/tbXt3D1Ea8EQttSMrcC07mq9pnRKdNS0gWZXIjm+3rJkthKw0m6wK0tnFjmDeD7xQsRrDPeF7ou36oMxh875liMEr+5/2LIUJw5hxRbvjx30KMjwsTqP7vWMHgzrj5f12cSHrwfY7GfimWYJbhvVmMO4JdRZWC5Qsaf3dhSrdNYKzP9/MnaHJcKMPkPKKu0XjrEWUPEbol0UNTt/B362qMGTwZEvl0zRIvRfBMFjKhri9EZdY5uIL+VzBKxZOtr86qk/ggFg4jVIvaBu2ivln41ullSF/mtYWEn9bbCRTFAgiz3mKS9Puu1jmXezQ1Y0xUPbjj2l0HQNgEwoRwqrx0mx0ex61Om/cV6oLkkpWTOtaebl0/FB8RmAJyes1ADvjuPEYHvA7SS5IMv7hxW72mwofv7y2qONAqwAJ/dyxGcNqKQf6RjP+qmVQQA5iKYjX58x72Vp0qJuAe+fLp7SQedGRiBtHWiypIQcRO3meAVLIEePdUrlNrFShVOhytqrL0kkcydXkj64xgCIDY37LTZtjbJtR7wzCeDKwdt8+xdgYdxwE7vtwmhC/yQvuVyMp1Pr7+l4SvBwabVxGnhitHzvuVoqW0sCo1GlbGawz1CdnK/x2KrZAjzuGZn53cxYWNKXLC3RkiNszb+ccfeZZqzS40YzaAFTkQ8CsXJM0ZkfkyEmgCkf/Ho5YzeMXXTH0Dq4BZFcsTP6R46JwvTyRWe+J0Yj5XsEIUEln/yeCGOEz5wftx9XrIEfOacqtW3UQ1ncwmeqaHrPBOl1V9Q5QaT+8SI3g2MKxeMdeRa0K0UMTRZB0oizRdPypz MEkJvook juS2VjHFZSEYGynqHE7sBAE/HETyMioscCfqVrFqEHVv60rXaHMWZs9m9y18CzE/DZgGQWZpsjE3eDv37X+tBIup2hZUsBJEyd+xa7z67L3ETtCNKESzH07qMa/XsykcxCH1HMzjloqYzJ4/DPlZSsxeU7TfmQmqhY/9JnhwAttb/G/dJXFg5fDtPrj88mStIqTdGZw87KmfkxRLAg+21e+h3uUgYIM+dVsXqF/nAwUIY1ecDB8EP6Jlnq3Ar4b8J0yJkPjGsEo8qlInaAZOq/nEKYSIXzqmwZHQC 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: The kernel now has the main shadow stack functionality to support applications. Wire in the WRSS and shadow stack enable/disable functions into the existing shadow stack API skeleton. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- v4: - Remove "CET" references v2: - Split from other patches arch/x86/kernel/shstk.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 71dbb49b93cd..07142e6f05f6 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -465,9 +465,17 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features) return -EINVAL; if (option == ARCH_SHSTK_DISABLE) { + if (features & ARCH_SHSTK_WRSS) + return wrss_control(false); + if (features & ARCH_SHSTK_SHSTK) + return shstk_disable(); return -EINVAL; } /* Handle ARCH_SHSTK_ENABLE */ + if (features & ARCH_SHSTK_SHSTK) + return shstk_setup(); + if (features & ARCH_SHSTK_WRSS) + return wrss_control(true); return -EINVAL; } From patchwork Thu Jan 19 21:23:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108813 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 8DBA4C6379F for ; Thu, 19 Jan 2023 21:24:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DD3A28000C; Thu, 19 Jan 2023 16:24:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 43FEE280001; Thu, 19 Jan 2023 16:24:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A5CA28000C; Thu, 19 Jan 2023 16:24:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EEA1E280001 for ; Thu, 19 Jan 2023 16:24:23 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CAABE160378 for ; Thu, 19 Jan 2023 21:24:23 +0000 (UTC) X-FDA: 80372827206.19.DBC70E2 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id A80614000C for ; Thu, 19 Jan 2023 21:24:21 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=c4BjfiOQ; spf=pass (imf27.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=1674163462; 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=TgTd1mfufOsbbmS0K83XaaAGrMI8Q2ihbQqiYwpYOW0=; b=JZwfdxBVddyCSm0KN4gZhgAFmBVIIW1rk30Q6SNwBvheB4lMF8NTde58CMfoWQjrSW+3QJ IbTuONEXP9OBchTX8yTcRituQ11Snc7kmdId2l3KPf62psuRur6HoqdQithpw6TTeM1NU2 qgf8Af62fx8aP4336BBE5YKyQ8bqkeI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=c4BjfiOQ; spf=pass (imf27.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=1674163462; a=rsa-sha256; cv=none; b=Aif/Exlei+gX5ICOKRkd3muHVjTcXZiGth0bmc+IyshM7dj4dQd2jwDYUpOtrwGC2pRDE+ 8Wo2stDtIpjVrq2X66UscMbz20aI3kEB08YaKbCIs4CCSImGfCIvm0/StwBxCluipxHHCZ 0QvfwMyQ6jHMUjIbmJn7artNw7xp7+8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163461; x=1705699461; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=+geAUGtKJ5/tBElMoeiPJqxtmy5X7bBSU640+SHskXE=; b=c4BjfiOQE0zc8x0AvQI811uEHzqBSFxk7nd0Kcp2bfIiIbAqIofyAuqi WvkJm2UYK/sDa50Ug0mEFaDxjC4fOlxeaYC2Y3haS3wTbQZ0lYK0Z8iWf LGDSfratMRQqYhtjcT1+8lKX3dqqDECtvuMdNIJ6NJ5ELzKaZGubrcu2o aWxahYkiQ7sqPUg0fzFMVtoDku4GkRKULKGJaHOOCil8QBYN9J3SAQa0e g7Tp/KGnlgyIgFoweHXbxrAKHldIuP8Na6PC8ymRTVv8RSmugASbRKk6+ HZo5k+s5F7xp9PLLwSWDXNpnNyy61k2byXVqaNrl9IQhNazY3vgJvqu9y g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120024" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120024" 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:20 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139180" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139180" 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:19 -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, Yu-cheng Yu Subject: [PATCH v5 35/39] selftests/x86: Add shadow stack test Date: Thu, 19 Jan 2023 13:23:13 -0800 Message-Id: <20230119212317.8324-36-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: A80614000C X-Stat-Signature: b1e7miaa18xmmbeygsqxzx7ag31e1o49 X-Rspam-User: X-HE-Tag: 1674163461-543 X-HE-Meta: U2FsdGVkX180yJBqP63SLPaCVU8BZfhi/4lVH5nCfSwBTpv/ZOp0mlQyrvgR6uITtyxX4CINMCt474EX8lT2E6JRBTeY8BbmOh3OVRJsoT4kDY/+YJWDiEGvzH7+zbAyxUcv5UvKETPvg+fggTdN4nA3+ZUt0e2rF9p+KVjSEkJA70fUKEpqZQGkGawJAVd/JgjC9OKKxkTaUXBo5fP3KK0+FLS6s0ujcTFUNheq2jr5PSLmGtw3/+EzUKggTZVkarFGQRsSvbeh7vuYQ51saZijhPDIXLJgE4ip9fmZvQX+GEIFYIKedjV9HuBqiuqEZwmqlIM/GORTt7sQ61IFrHhJlg+WTTqPzCvttSqskpsHwq6V5iXSD0e+J0QNi3JXosOE1ySedu7K6jOqh8m8NTT22Q3ViKIVJqK858DzY+QnrR1Fe1TCxjisjvNL+L5PcWpZE1AJ4EtDwDtSym7Zo7EPmqdhG7CmquK3fmu3BQMsnnBbqiTA4vwn5Oru02dVSE7HaeWvGSB7szM0wKWISi6Zmuj0Lpsvi8RTF6lg4hOZ3ZdjD2yN/qAjoWttX0sUYpN1Uqt98gHxsyxmez1LLEJ0xSdbZprdWov8KA3HRaRKYchBYCEvAWSEBqDsnJvBV9q+5LOx+SFXL06auKxnK0C72NmLPd26lQZe7rPA0MTHmka6gQ59YEVBWnYRbtdFR4Um6ZGTLuhEPLxvI3FVQuDI6x4WqrVfc5zthPq1wJAMS/QRMSDfdkiK/MSCh8Xzn4WMqCllpKmEchz+KEJEWvYkSDvWC7E+M0oBoQnfw1P0RvdQVMnwI6kUK8i1cbGnSv99s/Kiu6bXULop3VCNfq3YErdqPQ08e82FO4rlpMfDgLU1NuiKJGQjDMWx59CFDKAta7/D1lg2OOT0p+k8lNXA6XtvsIKdQ/CQ/26sltFIyYhUoYtnZGTmVtGF8uP2LBiWw7ToHlMbxotSMNT 2isXKyeQ Im9AAULmT2cWFgYxj6ZMod5Pb+1tEFJbfDtywDk0oBLy015r0YccPhF2KRT41huf6IMnPl9ium/sseHkMnvGQSXifjW1Ni9Go01ReueBOonFqUYQYt4L3l2lHId4OdyQhMTArxp60I/YzQY8rSACTKLgU9HEiICq27mJSAKqoNTMl99/M+n+KqaH5AOpZQumTBlAoDgf9RdZ49LRxjDy5vlFrXrJXvsWEfazy2071U+meprw= 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 a simple selftest for exercising some shadow stack behavior: - map_shadow_stack syscall and pivot - Faulting in shadow stack memory - Handling shadow stack violations - GUP of shadow stack memory - mprotect() of shadow stack memory - Userfaultfd on shadow stack memory Since this test exercises a recently added syscall manually, it needs to find the automatically created __NR_foo defines. Per the selftest documentation, KHDR_INCLUDES can be used to help the selftest Makefile's find the headers from the kernel source. This way the new selftest can be built inside the kernel source tree without installing the headers to the system. So also add KHDR_INCLUDES as described in the selftest docs, to facilitate this. Tested-by: Pengfei Xu Tested-by: John Allen Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe --- v5: - Update 32 bit signal test with new ABI and better asm v4: - Add test for 32 bit signal ABI blocking v3: - Change "+m" to "=m" in write_shstk() (Andrew Cooper) - Fix userfaultfd test with transparent huge pages by doing a MADV_DONTNEED, since the token write faults in the while stack with huge pages. v2: - Change print statements to more align with other selftests - Add more tests - Add KHDR_INCLUDES to Makefile tools/testing/selftests/x86/Makefile | 4 +- .../testing/selftests/x86/test_shadow_stack.c | 667 ++++++++++++++++++ 2 files changed, 669 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/x86/test_shadow_stack.c diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile index 0388c4d60af0..cfc8a26ad151 100644 --- a/tools/testing/selftests/x86/Makefile +++ b/tools/testing/selftests/x86/Makefile @@ -18,7 +18,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \ - corrupt_xstate_header amx + corrupt_xstate_header amx test_shadow_stack # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall @@ -34,7 +34,7 @@ BINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64)) -CFLAGS := -O2 -g -std=gnu99 -pthread -Wall +CFLAGS := -O2 -g -std=gnu99 -pthread -Wall $(KHDR_INCLUDES) # call32_from_64 in thunks.S uses absolute addresses. ifeq ($(CAN_BUILD_WITH_NOPIE),1) diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c new file mode 100644 index 000000000000..5a3b4f6d1a1d --- /dev/null +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -0,0 +1,667 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This program test's basic kernel shadow stack support. It enables shadow + * stack manual via the arch_prctl(), instead of relying on glibc. It's + * Makefile doesn't compile with shadow stack support, so it doesn't rely on + * any particular glibc. As a result it can't do any operations that require + * special glibc shadow stack support (longjmp(), swapcontext(), etc). Just + * stick to the basics and hope the compiler doesn't do anything strange. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SS_SIZE 0x200000 + +#if (__GNUC__ < 8) || (__GNUC__ == 8 && __GNUC_MINOR__ < 5) +int main(int argc, char *argv[]) +{ + printf("[SKIP]\tCompiler does not support CET.\n"); + return 0; +} +#else +void write_shstk(unsigned long *addr, unsigned long val) +{ + asm volatile("wrssq %[val], (%[addr])\n" + : "=m" (addr) + : [addr] "r" (addr), [val] "r" (val)); +} + +static inline unsigned long __attribute__((always_inline)) get_ssp(void) +{ + unsigned long ret = 0; + + asm volatile("xor %0, %0; rdsspq %0" : "=r" (ret)); + return ret; +} + +/* + * For use in inline enablement of shadow stack. + * + * The program can't return from the point where shadow stack gets enabled + * because there will be no address on the shadow stack. So it can't use + * syscall() for enablement, since it is a function. + * + * Based on code from nolibc.h. Keep a copy here because this can't pull in all + * of nolibc.h. + */ +#define ARCH_PRCTL(arg1, arg2) \ +({ \ + long _ret; \ + register long _num asm("eax") = __NR_arch_prctl; \ + register long _arg1 asm("rdi") = (long)(arg1); \ + register long _arg2 asm("rsi") = (long)(arg2); \ + \ + asm volatile ( \ + "syscall\n" \ + : "=a"(_ret) \ + : "r"(_arg1), "r"(_arg2), \ + "0"(_num) \ + : "rcx", "r11", "memory", "cc" \ + ); \ + _ret; \ +}) + +void *create_shstk(void *addr) +{ + return (void *)syscall(__NR_map_shadow_stack, addr, SS_SIZE, SHADOW_STACK_SET_TOKEN); +} + +void *create_normal_mem(void *addr) +{ + return mmap(addr, SS_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); +} + +void free_shstk(void *shstk) +{ + munmap(shstk, SS_SIZE); +} + +int reset_shstk(void *shstk) +{ + return madvise(shstk, SS_SIZE, MADV_DONTNEED); +} + +void try_shstk(unsigned long new_ssp) +{ + unsigned long ssp; + + printf("[INFO]\tnew_ssp = %lx, *new_ssp = %lx\n", + new_ssp, *((unsigned long *)new_ssp)); + + ssp = get_ssp(); + printf("[INFO]\tchanging ssp from %lx to %lx\n", ssp, new_ssp); + + asm volatile("rstorssp (%0)\n":: "r" (new_ssp)); + asm volatile("saveprevssp"); + printf("[INFO]\tssp is now %lx\n", get_ssp()); + + /* Switch back to original shadow stack */ + ssp -= 8; + asm volatile("rstorssp (%0)\n":: "r" (ssp)); + asm volatile("saveprevssp"); +} + +int test_shstk_pivot(void) +{ + void *shstk = create_shstk(0); + + if (shstk == MAP_FAILED) { + printf("[FAIL]\tError creating shadow stack: %d\n", errno); + return 1; + } + try_shstk((unsigned long)shstk + SS_SIZE - 8); + free_shstk(shstk); + + printf("[OK]\tShadow stack pivot\n"); + return 0; +} + +int test_shstk_faults(void) +{ + unsigned long *shstk = create_shstk(0); + + /* Read shadow stack, test if it's zero to not get read optimized out */ + if (*shstk != 0) + goto err; + + /* Wrss memory that was already read. */ + write_shstk(shstk, 1); + if (*shstk != 1) + goto err; + + /* Page out memory, so we can wrss it again. */ + if (reset_shstk((void *)shstk)) + goto err; + + write_shstk(shstk, 1); + if (*shstk != 1) + goto err; + + printf("[OK]\tShadow stack faults\n"); + return 0; + +err: + return 1; +} + +unsigned long saved_ssp; +unsigned long saved_ssp_val; +volatile bool segv_triggered; + +void __attribute__((noinline)) violate_ss(void) +{ + saved_ssp = get_ssp(); + saved_ssp_val = *(unsigned long *)saved_ssp; + + /* Corrupt shadow stack */ + printf("[INFO]\tCorrupting shadow stack\n"); + write_shstk((void *)saved_ssp, 0); +} + +void segv_handler(int signum, siginfo_t *si, void *uc) +{ + printf("[INFO]\tGenerated shadow stack violation successfully\n"); + + segv_triggered = true; + + /* Fix shadow stack */ + write_shstk((void *)saved_ssp, saved_ssp_val); +} + +int test_shstk_violation(void) +{ + struct sigaction sa; + + sa.sa_sigaction = segv_handler; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + sa.sa_flags = SA_SIGINFO; + + segv_triggered = false; + + /* Make sure segv_triggered is set before violate_ss() */ + asm volatile("" : : : "memory"); + + violate_ss(); + + signal(SIGSEGV, SIG_DFL); + + printf("[OK]\tShadow stack violation test\n"); + + return !segv_triggered; +} + +/* Gup test state */ +#define MAGIC_VAL 0x12345678 +bool is_shstk_access; +void *shstk_ptr; +int fd; + +void reset_test_shstk(void *addr) +{ + if (shstk_ptr != NULL) + free_shstk(shstk_ptr); + shstk_ptr = create_shstk(addr); +} + +void test_access_fix_handler(int signum, siginfo_t *si, void *uc) +{ + printf("[INFO]\tViolation from %s\n", is_shstk_access ? "shstk access" : "normal write"); + + segv_triggered = true; + + /* Fix shadow stack */ + if (is_shstk_access) { + reset_test_shstk(shstk_ptr); + return; + } + + free_shstk(shstk_ptr); + create_normal_mem(shstk_ptr); +} + +bool test_shstk_access(void *ptr) +{ + is_shstk_access = true; + segv_triggered = false; + write_shstk(ptr, MAGIC_VAL); + + asm volatile("" : : : "memory"); + + return segv_triggered; +} + +bool test_write_access(void *ptr) +{ + is_shstk_access = false; + segv_triggered = false; + *(unsigned long *)ptr = MAGIC_VAL; + + asm volatile("" : : : "memory"); + + return segv_triggered; +} + +bool gup_write(void *ptr) +{ + unsigned long val; + + lseek(fd, (unsigned long)ptr, SEEK_SET); + if (write(fd, &val, sizeof(val)) < 0) + return 1; + + return 0; +} + +bool gup_read(void *ptr) +{ + unsigned long val; + + lseek(fd, (unsigned long)ptr, SEEK_SET); + if (read(fd, &val, sizeof(val)) < 0) + return 1; + + return 0; +} + +int test_gup(void) +{ + struct sigaction sa; + int status; + pid_t pid; + + sa.sa_sigaction = test_access_fix_handler; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + sa.sa_flags = SA_SIGINFO; + + segv_triggered = false; + + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + return 1; + + reset_test_shstk(0); + if (gup_read(shstk_ptr)) + return 1; + if (test_shstk_access(shstk_ptr)) + return 1; + printf("[INFO]\tGup read -> shstk access success\n"); + + reset_test_shstk(0); + if (gup_write(shstk_ptr)) + return 1; + if (test_shstk_access(shstk_ptr)) + return 1; + printf("[INFO]\tGup write -> shstk access success\n"); + + reset_test_shstk(0); + if (gup_read(shstk_ptr)) + return 1; + if (!test_write_access(shstk_ptr)) + return 1; + printf("[INFO]\tGup read -> write access success\n"); + + reset_test_shstk(0); + if (gup_write(shstk_ptr)) + return 1; + if (!test_write_access(shstk_ptr)) + return 1; + printf("[INFO]\tGup write -> write access success\n"); + + close(fd); + + /* COW/gup test */ + reset_test_shstk(0); + pid = fork(); + if (!pid) { + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + exit(1); + + if (gup_write(shstk_ptr)) { + close(fd); + exit(1); + } + close(fd); + exit(0); + } + waitpid(pid, &status, 0); + if (WEXITSTATUS(status)) { + printf("[FAIL]\tWrite in child failed\n"); + return 1; + } + if (*(unsigned long *)shstk_ptr == MAGIC_VAL) { + printf("[FAIL]\tWrite in child wrote through to shared memory\n"); + return 1; + } + + printf("[INFO]\tCow gup write -> write access success\n"); + + free_shstk(shstk_ptr); + + signal(SIGSEGV, SIG_DFL); + + printf("[OK]\tShadow gup test\n"); + + return 0; +} + +int test_mprotect(void) +{ + struct sigaction sa; + + sa.sa_sigaction = test_access_fix_handler; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + sa.sa_flags = SA_SIGINFO; + + segv_triggered = false; + + /* mprotect a shadow stack as read only */ + reset_test_shstk(0); + if (mprotect(shstk_ptr, SS_SIZE, PROT_READ) < 0) { + printf("[FAIL]\tmprotect(PROT_READ) failed\n"); + return 1; + } + + /* try to wrss it and fail */ + if (!test_shstk_access(shstk_ptr)) { + printf("[FAIL]\tShadow stack access to read-only memory succeeded\n"); + return 1; + } + + /* then back to writable */ + if (mprotect(shstk_ptr, SS_SIZE, PROT_WRITE | PROT_READ) < 0) { + printf("[FAIL]\tmprotect(PROT_WRITE) failed\n"); + return 1; + } + + /* then pivot to it and succeed */ + if (test_shstk_access(shstk_ptr)) { + printf("[FAIL]\tShadow stack access to mprotect() writable memory failed\n"); + return 1; + } + + free_shstk(shstk_ptr); + + signal(SIGSEGV, SIG_DFL); + + printf("[OK]\tmprotect() test\n"); + + return 0; +} + +char zero[4096]; + +static void *uffd_thread(void *arg) +{ + struct uffdio_copy req; + int uffd = *(int *)arg; + struct uffd_msg msg; + + if (read(uffd, &msg, sizeof(msg)) <= 0) + return (void *)1; + + req.dst = msg.arg.pagefault.address; + req.src = (__u64)zero; + req.len = 4096; + req.mode = 0; + + if (ioctl(uffd, UFFDIO_COPY, &req)) + return (void *)1; + + return (void *)0; +} + +int test_userfaultfd(void) +{ + struct uffdio_register uffdio_register; + struct uffdio_api uffdio_api; + struct sigaction sa; + pthread_t thread; + void *res; + int uffd; + + sa.sa_sigaction = test_access_fix_handler; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + sa.sa_flags = SA_SIGINFO; + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd < 0) { + printf("[SKIP]\tUserfaultfd unavailable.\n"); + return 0; + } + + reset_test_shstk(0); + + uffdio_api.api = UFFD_API; + uffdio_api.features = 0; + if (ioctl(uffd, UFFDIO_API, &uffdio_api)) + goto err; + + uffdio_register.range.start = (__u64)shstk_ptr; + uffdio_register.range.len = 4096; + uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + goto err; + + if (pthread_create(&thread, NULL, &uffd_thread, &uffd)) + goto err; + + reset_shstk(shstk_ptr); + test_shstk_access(shstk_ptr); + + if (pthread_join(thread, &res)) + goto err; + + if (test_shstk_access(shstk_ptr)) + goto err; + + free_shstk(shstk_ptr); + + signal(SIGSEGV, SIG_DFL); + + if (!res) + printf("[OK]\tUserfaultfd test\n"); + return !!res; +err: + free_shstk(shstk_ptr); + close(uffd); + signal(SIGSEGV, SIG_DFL); + return 1; +} + +/* + * Too complicated to pull it out of the 32 bit header, but also get the + * 64 bit one needed above. Just define a copy here. + */ +#define __NR_compat_sigaction 67 + +/* + * Call 32 bit signal handler to get 32 bit signals ABI. Make sure + * to push the registers that will get clobbered. + */ +int sigaction32(int signum, const struct sigaction *restrict act, + struct sigaction *restrict oldact) +{ + register long syscall_reg asm("eax") = __NR_compat_sigaction; + register long signum_reg asm("ebx") = signum; + register long act_reg asm("ecx") = (long)act; + register long oldact_reg asm("edx") = (long)oldact; + int ret = 0; + + asm volatile ("int $0x80;" + : "=a"(ret), "=m"(oldact) + : "r"(syscall_reg), "r"(signum_reg), "r"(act_reg), + "r"(oldact_reg) + : "r8", "r9", "r10", "r11" + ); + + return ret; +} + +sigjmp_buf jmp_buffer; + +void segv_gp_handler(int signum, siginfo_t *si, void *uc) +{ + segv_triggered = true; + + /* + * To work with old glibc, this can't rely on siglongjmp working with + * shadow stack enabled, so disable shadow stack before siglongjmp(). + */ + ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK); + siglongjmp(jmp_buffer, -1); +} + +/* + * Transition to 32 bit mode and check that a #GP triggers a segfault. + */ +int test_32bit(void) +{ + struct sigaction sa; + struct sigaction *sa32; + + /* Create sigaction in 32 bit address range */ + sa32 = mmap(0, 4096, PROT_READ | PROT_WRITE, + MAP_32BIT | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + sa32->sa_flags = SA_SIGINFO; + + sa.sa_sigaction = segv_gp_handler; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + sa.sa_flags = SA_SIGINFO; + + segv_triggered = false; + + /* Make sure segv_triggered is set before triggering the #GP */ + asm volatile("" : : : "memory"); + + /* + * Set handler to somewhere in 32 bit address space + */ + sa32->sa_handler = (void *)sa32; + if (sigaction32(SIGUSR1, sa32, NULL)) + return 1; + + if (!sigsetjmp(jmp_buffer, 1)) + raise(SIGUSR1); + + if (segv_triggered) + printf("[OK]\t32 bit test\n"); + + return !segv_triggered; +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + + if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) { + printf("[SKIP]\tCould not enable Shadow stack\n"); + return 1; + } + + if (ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK)) { + ret = 1; + printf("[FAIL]\tDisabling shadow stack failed\n"); + } + + if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) { + printf("[SKIP]\tCould not re-enable Shadow stack\n"); + return 1; + } + + if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS)) { + printf("[SKIP]\tCould not enable WRSS\n"); + ret = 1; + goto out; + } + + /* Should have succeeded if here, but this is a test, so double check. */ + if (!get_ssp()) { + printf("[FAIL]\tShadow stack disabled\n"); + return 1; + } + + if (test_shstk_pivot()) { + ret = 1; + printf("[FAIL]\tShadow stack pivot\n"); + goto out; + } + + if (test_shstk_faults()) { + ret = 1; + printf("[FAIL]\tShadow stack fault test\n"); + goto out; + } + + if (test_shstk_violation()) { + ret = 1; + printf("[FAIL]\tShadow stack violation test\n"); + goto out; + } + + if (test_gup()) { + ret = 1; + printf("[FAIL]\tShadow shadow stack gup\n"); + goto out; + } + + if (test_mprotect()) { + ret = 1; + printf("[FAIL]\tShadow shadow mprotect test\n"); + goto out; + } + + if (test_userfaultfd()) { + ret = 1; + printf("[FAIL]\tUserfaultfd test\n"); + goto out; + } + + if (test_32bit()) { + ret = 1; + printf("[FAIL]\t32 bit test\n"); + } + + return ret; + +out: + /* + * Disable shadow stack before the function returns, or there will be a + * shadow stack violation. + */ + if (ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK)) { + ret = 1; + printf("[FAIL]\tDisabling shadow stack failed\n"); + } + + return ret; +} +#endif From patchwork Thu Jan 19 21:23:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108814 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 8D9B0C46467 for ; Thu, 19 Jan 2023 21:24:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63AA528000D; Thu, 19 Jan 2023 16:24:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C280280001; Thu, 19 Jan 2023 16:24:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AC2228000D; Thu, 19 Jan 2023 16:24:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 25683280001 for ; Thu, 19 Jan 2023 16:24:25 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F172D1C6571 for ; Thu, 19 Jan 2023 21:24:24 +0000 (UTC) X-FDA: 80372827248.13.81C5051 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id F0FD3180002 for ; Thu, 19 Jan 2023 21:24:22 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nG6ZywQW; 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=1674163463; 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=UIaubgF5cxlgy9UntqyzMGFahvGgA9BhdHUjWEg2kcA=; b=JM7Xd7hpIfLNh2QA4YoRD69DcZujHian9ooinQG+bu1SiFm0T+yJK9PBLHKhOspjTkbg4G 5jH3dc3CbFx34QzOoocX7yoWfZbjOo6f6VGEIHc9Qi5BTmHjBvhtmgjpTackuZldGz08+4 NKYT2Awt1vQrYFL9nb7F4OBUpadnWCU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nG6ZywQW; 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=1674163463; a=rsa-sha256; cv=none; b=BA7F3DudyG9Zyc+CKLezVhj3Sq86aoWeQXJqThKtiSm9KXa/kr4L4JN0zZ4YRhe0wfMCRt lRkRQjQzFyK0BYP/84ZLY7xEG0x79w1bBRpY8YFSeMAburNGSf5xD25tDd1OXZXjHbt7YL KIVC4fXelaoDFfZRwxN6f0QgRZGL/6I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163463; x=1705699463; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Y8HP+6SI6A4E79h0YyqSt+wn/k0j51I/AMnEXqlWOFs=; b=nG6ZywQW3X45sxUg6+EVOduvXjjCPAU3k6fs1Hr/xHTqEUvwbVVRAyTw g0E8qOySNoqUDUphLnD11AuAg4lBvOx2KJjjAFQ89iJg4L3jxdEyNHyoa kG6A7cUBG9SRWc0pvfcdYWn6s6pqUL0bSranA/TcqByZr4V7kTmcKwt45 JmZscj5lG6Q7RHhM/gONCK5LUhKaCqqOuKkcWGrTm5HRf5Nvu3dAkEt/a hkUtbYas/blQUDuxG+bSxzyPIHnXYVPMG+AMHdlieX0uUvODOtMvqJE4f uOO/Uyt0NGQOv+2mp3y04cVjnB5vDWJN1c+gJF9tIBADcaZYD2dsPmgVc Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120044" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120044" 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:22 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139185" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139185" 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:20 -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 36/39] x86/fpu: Add helper for initing features Date: Thu, 19 Jan 2023 13:23:14 -0800 Message-Id: <20230119212317.8324-37-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: F0FD3180002 X-Stat-Signature: pjmkxsq4kjp5whyhske3phj4kco8yn6n X-Rspam-User: X-HE-Tag: 1674163462-190073 X-HE-Meta: U2FsdGVkX1/aM2t4inLICzkaBInsdMKGrmta6I6mjM8NiBj9VI00SlQV/bZSME3F7307MMXTFOS2fhAzGAhPSwQou/yHnTlInJjIdGEQtKk+qPg5SQluEeL6DBqOG5iOjqIVMIMql+t9t6m4gn4PiMlaFk3Wn16G/ObOK6r8iNhV4OiOovj0tmc7mYelkqDmVeQ4WKsHR7yjCXmTn0X2d1vYFxr3XKOejYb51+oLEmhvEU59IIDJXQ1WqQk4TBtp66pdMyqsZLoWlfGeqaLjuaE+VgTqdS9H8LTiGdqSo/SDDNYGOXCebyKEU/Buv6HnwdFDRTh1nePdOWfbjnNcXrOcSaBW/nykFXYrQrkgjBSBDYqdFL9f977N6q9sFQ/cEtcfDThIoFWSjE3MGSpAS/AzH9F1dY8giZcAlqeejPPDNOkQ/af11XaWHYsowmWHyxP40Vr8k4Y9RzMgGR7EQ/oNPwTU0dccnYUFO/YSO9jyL1BzuKdNpTN43d648GIN2SvumolaA9d+mnGJvb7COP9cnn4vLxejHEaruxnKkog0y08htDOiounZOiABv7voZywshbTHsdX8l/hO1NUabw+lzcAwvjWTrOEDVh2JRDxf7L32+tncV86mVJrsBFqIfIlUoiL0KX9W8p0cRnlHpm8Pz+0zYY8WzIY1cIXNSV7/n1Pw/HaiqNMmUNrK/lBNPfwW2aNGChUqTisZ4lpvMMtgBMPNwoRD0FUsneoNovuUqH6gQ0JtnqQRiq3LgmCNNmR3vhukhkUt9CvkK+d3vCXWU4te/fd6S71oLfMoUUxK+EFEvJIynyKO91GFvfVzpLIfk19U9daAnb4nhIvRK/QIYdR308FDMFbyJoH7m/AI6XX0sugF0wff23umpt0gP6fBaP4X1ze7DcBvqJmExastYOsiFb+VfyTDfeX2nkAhpxYXUItdxSqiRohWrNANFbdT8YsitHFuRDWLwxL CP19yTom RewQO6djpKV9R2m7RXn/gLbjDs0lGCWNOQgd790b8bTT4CWJtcIEQ7VCSGN1IUy5d6Ii2FOhsqLJt1XWSbMXJ8SVO0+R1GpOx/i4MsDExWuc/Bhg1GMJ7NVEhQm78UWC22hEFuGhh5rRa2AH4ugWvlnP0hUxOZCWjDm23Z7BMpSVq9oeT+R+7F5H1RYhPWx0Px+1PvY2yQcXiFAVVZP9u+CH8igAm4v8NzBS8+CLqX6JdTaY= 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: If an xfeature is saved in a buffer, the xfeature's bit will be set in xsave->header.xfeatures. The CPU may opt to not save the xfeature if it is in it's init state. In this case the xfeature buffer address cannot be retrieved with get_xsave_addr(). Future patches will need to handle the case of writing to an xfeature that may not be saved. So provide helpers to init an xfeature in an xsave buffer. This could of course be done directly by reaching into the xsave buffer, however this would not be robust against future changes to optimize the xsave buffer by compacting it. In that case the xsave buffer would need to be re-arranged as well. So the logic properly belongs encapsulated in a helper where the logic can be unified. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe --- v2: - New patch arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++------- arch/x86/kernel/fpu/xstate.h | 6 ++++ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 13a80521dd51..3ff80be0a441 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -934,6 +934,24 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) return (void *)xsave + xfeature_get_offset(xcomp_bv, xfeature_nr); } +static int xsave_buffer_access_checks(int xfeature_nr) +{ + /* + * Do we even *have* xsave state? + */ + if (!boot_cpu_has(X86_FEATURE_XSAVE)) + return 1; + + /* + * We should not ever be requesting features that we + * have not enabled. + */ + if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr))) + return 1; + + return 0; +} + /* * Given the xsave area and a state inside, this function returns the * address of the state. @@ -954,17 +972,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) */ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) { - /* - * Do we even *have* xsave state? - */ - if (!boot_cpu_has(X86_FEATURE_XSAVE)) - return NULL; - - /* - * We should not ever be requesting features that we - * have not enabled. - */ - if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr))) + if (xsave_buffer_access_checks(xfeature_nr)) return NULL; /* @@ -984,6 +992,34 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) return __raw_xsave_addr(xsave, xfeature_nr); } +/* + * Given the xsave area and a state inside, this function + * initializes an xfeature in the buffer. + * + * get_xsave_addr() will return NULL if the feature bit is + * not present in the header. This function will make it so + * the xfeature buffer address is ready to be retrieved by + * get_xsave_addr(). + * + * Inputs: + * xstate: the thread's storage area for all FPU data + * xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP, + * XFEATURE_SSE, etc...) + * Output: + * 1 if the feature cannot be inited, 0 on success + */ +int init_xfeature(struct xregs_state *xsave, int xfeature_nr) +{ + if (xsave_buffer_access_checks(xfeature_nr)) + return 1; + + /* + * Mark the feature inited. + */ + xsave->header.xfeatures |= BIT_ULL(xfeature_nr); + return 0; +} + #ifdef CONFIG_ARCH_HAS_PKEYS /* diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index a4ecb04d8d64..dc06f63063ee 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -54,6 +54,12 @@ extern void fpu__init_cpu_xstate(void); extern void fpu__init_system_xstate(unsigned int legacy_size); extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr); +extern int init_xfeature(struct xregs_state *xsave, int xfeature_nr); + +static inline int xfeature_saved(struct xregs_state *xsave, int xfeature_nr) +{ + return xsave->header.xfeatures & BIT_ULL(xfeature_nr); +} static inline u64 xfeatures_mask_supervisor(void) { From patchwork Thu Jan 19 21:23:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108815 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 52E71C6379F for ; Thu, 19 Jan 2023 21:24:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06E3728000E; Thu, 19 Jan 2023 16:24:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F39BB280001; Thu, 19 Jan 2023 16:24:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CEF4628000E; Thu, 19 Jan 2023 16:24:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B417B280001 for ; Thu, 19 Jan 2023 16:24:26 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9657D806E1 for ; Thu, 19 Jan 2023 21:24:26 +0000 (UTC) X-FDA: 80372827332.06.47F7C07 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id 7F90840017 for ; Thu, 19 Jan 2023 21:24:24 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YJ2+WySu; spf=pass (imf27.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=1674163464; 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=dsrFj8bpNT5MvUDiRiCUjp10IycWIyaFRdvlnCTkRv8=; b=CGPX9QZRvS4Afi5m4mVYgsdHc6q8FkJR68BWo0zkSIorz7UK7bP1nnjz5M3Lep66jejvUh Qn7Wso2pz3vfBf8/pQyRzGnpxAj6U0IO7hGGPoiVaVU8rrDAQPnAChh1TQO/S0O+ncbSIk ygcqt3FbHS5oInLeRbtHuWOg4qufIcE= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YJ2+WySu; spf=pass (imf27.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=1674163464; a=rsa-sha256; cv=none; b=O+UBlXBXeHt1iiTiGe+cykfAhHCUeCZzaqnaNbEr+2HbRmSTjiAA4247gbPa4pxgg0e5pP OKAPD0a3uj64E2AWIJ0pZT1SyTIC/byAAo4H4aSn3BwHJGnOcL/U2g3+22zEyIxm2uComE 1rN3p+9M8dToXXVq+QoQhdq7LHI5sm8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163464; x=1705699464; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=0itXeYpmwui1qYObTMssZc6TAQWFWu07BF3T37hLMrE=; b=YJ2+WySusE9I72cReF8eKHRWgerieoW418QTG0Fe/VLJpmgmSb04eeGY aaU8AgRfFmuJ7PfWsL9xIOPkZUTLWYqZl703Rt4eEkjGhvstGnr46d5UW vroz/ugmFcm0Jg/DcxiQIU0o/olwOdPmhfFR6YASiBqKRReSQVaaL3CwO zjavWeC2CMs3QVGAu2tWswlm09entJJA/3WgnI3QxloGCzxykytvSn1k3 wUlEuCot5Rfy3+2b5JVmSLvXOUVGF1e3lyEpa08iJxnHKh66hRUQvKd7U TjjVYS4d5sOX8vVNfCsP+kxn9R+IKuIttrz5JP8Ntjnt0HQbZKb2G4qRP A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120065" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120065" 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:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139192" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139192" 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:22 -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, Yu-cheng Yu Subject: [PATCH v5 37/39] x86: Add PTRACE interface for shadow stack Date: Thu, 19 Jan 2023 13:23:15 -0800 Message-Id: <20230119212317.8324-38-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: 7F90840017 X-Stat-Signature: jr7s6oh8dre5qz9kxmjro58qa6o8atbo X-Rspam-User: X-HE-Tag: 1674163464-700537 X-HE-Meta: U2FsdGVkX1/dPJ9aM1tvIhkRtG3s/9WxvO25yzcYah3iQFxOgLT2Z/az8dioTivYIqrdzH5hVVChuSCujUxHeDz4x2IMAqww/Z0VnsExjqKxp0+aLHNvXjrVylUUD4HOajh9d9TNP5qY6ZDqPXGhGkxGQb1dq51SsxTQrVTWgs/8s422psxSdrg60G2jXQk8+/GKhpFwS7trW8GASb0pKy/XkcmyIP6BGf4tAbkNBTcmzDyf7twDbLf7OvFqLqt40OvhoKstH9sASgA1+cWcRI9KQ+bLCDjwuOmLESZGxDTYSiuVBu0NpeuhGArOSfYjJHPhO5zO5dzeaEq5YZhvk+8Wla30F4v9yVwRH10jeO5AAiBkvoIrwJF03E+EhU8Dv0C+ED2TYgFoVvZ1YdKLaGbuNnafIt75OAm7FO0gbOj1EK/AbqEjyR/PIta9LeBlGbF4oXG/EZFiTtuMCswFhj4OSU3quk1tYd04ri/C4fO8XqlzbsjW6Ycv33X3QFOfGaDkMUnW2yT+ALIPFiWbx96+9R5+gpfMZqip9sifhFKa7KvOQsYNlcjJg7UjSkec9N5+o3cdcrHya6IuCbj2Ta7xuayzCJnovVKR6NPzJJZJfUWGSdBVIUh5Pr7w3r7RveiuNTdvHu+YqeBqbDC+yiYXA53PBW3jwCE48RKqYG9u/yA/bxs4IjAFziNP+OssYoeub+NTe+Lydi8cCCCh1PWc83HNsFq32Ou4HKt90I8HBW3kWkk7KWYiO9vd6DdbgZbY18vy75vfIOJZ0nLaNLYAZ9zm7LvmcGq85y5zL0SviNwmezSu/Nwyv51U9hREjv37/Ugd/jOllMnti4CLXLLhe/u4XiFl1NdFy31/Bomw2GBcaX8qQsUX1WdhfiMdwRaKDhpoxA44nJ7Bl/U4fPibaJyL+cY071scDNzApex6quzAHlK3RSKC2Ce0CwqMbAiNVyL56N42tu7pzG7 TCy9useC 3gDoipM5KloXiPX092ZFxMNwERjYPFbNMW5AD4dAt7Odaz3c1CJ3+NIYfNIVIz5BWjhJxczkDUY1A8qWeTP/kmsNZ0Z0uyzAR5QHupDKw2dswoC2qs7RaW47oYKkO26XtAQHRumVVNYri9JU2T0SEMKrGlkgR4o3/Zj9MWKJ45aJdPcu5p7rd/GJg51ggshZ5K4kLAy9eixEB7C1mf5v8isWhwnZ6ubiNAs7GZy01C4YCujc= 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: Yu-cheng Yu Some applications (like GDB) would like to tweak shadow stack state via ptrace. This allows for existing functionality to continue to work for seized shadow stack applications. Provide an regset interface for manipulating the shadow stack pointer (SSP). There is already ptrace functionality for accessing xstate, but this does not include supervisor xfeatures. So there is not a completely clear place for where to put the shadow stack state. Adding it to the user xfeatures regset would complicate that code, as it currently shares logic with signals which should not have supervisor features. Don't add a general supervisor xfeature regset like the user one, because it is better to maintain flexibility for other supervisor xfeatures to define their own interface. For example, an xfeature may decide not to expose all of it's state to userspace, as is actually the case for shadow stack ptrace functionality. A lot of enum values remain to be used, so just put it in dedicated shadow stack regset. The only downside to not having a generic supervisor xfeature regset, is that apps need to be enlightened of any new supervisor xfeature exposed this way (i.e. they can't try to have generic save/restore logic). But maybe that is a good thing, because they have to think through each new xfeature instead of encountering issues when new a new supervisor xfeature was added. By adding a shadow stack regset, it also has the effect of including the shadow stack state in a core dump, which could be useful for debugging. The shadow stack specific xstate includes the SSP, and the shadow stack and WRSS enablement status. Enabling shadow stack or wrss in the kernel involves more than just flipping the bit. The kernel is made aware that it has to do extra things when cloning or handling signals. That logic is triggered off of separate feature enablement state kept in the task struct. So the flipping on HW shadow stack enforcement without notifying the kernel to change its behavior would severely limit what an application could do without crashing, and the results would depend on kernel internal implementation details. There is also no known use for controlling this state via prtace today. So only expose the SSP, which is something that userspace already has indirect control over. Tested-by: Pengfei Xu Tested-by: John Allen Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook --- v5: - Check shadow stack enablement status for tracee (rppt) - Fix typo in comment v4: - Make shadow stack only. Reduce to only supporting SSP register, and remove CET references (peterz) - Add comment to not use 0x203, because binutils already looks for it in coredumps. (Christina Schimpe) v3: - Drop dependence on thread.shstk.size, and use thread.features bits - Drop 32 bit support v2: - Check alignment on ssp. - Block IBT bits. - Handle init states instead of returning error. - Add verbose commit log justifying the design. arch/x86/include/asm/fpu/regset.h | 7 +-- arch/x86/kernel/fpu/regset.c | 87 +++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 12 +++++ include/uapi/linux/elf.h | 2 + 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/fpu/regset.h b/arch/x86/include/asm/fpu/regset.h index 4f928d6a367b..697b77e96025 100644 --- a/arch/x86/include/asm/fpu/regset.h +++ b/arch/x86/include/asm/fpu/regset.h @@ -7,11 +7,12 @@ #include -extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active; +extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active, + ssp_active; extern user_regset_get2_fn fpregs_get, xfpregs_get, fpregs_soft_get, - xstateregs_get; + xstateregs_get, ssp_get; extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, - xstateregs_set; + xstateregs_set, ssp_set; /* * xstateregs_active == regset_fpregs_active. Please refer to the comment diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 6d056b68f4ed..10c092d21809 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "context.h" #include "internal.h" @@ -174,6 +175,92 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, return ret; } + +#ifdef CONFIG_X86_USER_SHADOW_STACK +int ssp_active(struct task_struct *target, const struct user_regset *regset) +{ + if (target->thread.features & ARCH_SHSTK_SHSTK) + return regset->n; + + return 0; +} + +int ssp_get(struct task_struct *target, const struct user_regset *regset, + struct membuf to) +{ + struct fpu *fpu = &target->thread.fpu; + struct cet_user_state *cetregs; + + if (!boot_cpu_has(X86_FEATURE_USER_SHSTK)) + return -ENODEV; + + sync_fpstate(fpu); + cetregs = get_xsave_addr(&fpu->fpstate->regs.xsave, XFEATURE_CET_USER); + if (!cetregs) { + /* + * The registers are the in the init state. The init values for + * these regs are zero, so just zero the output buffer. + */ + membuf_zero(&to, sizeof(cetregs->user_ssp)); + return 0; + } + + return membuf_write(&to, (unsigned long *)&cetregs->user_ssp, + sizeof(cetregs->user_ssp)); +} + +int ssp_set(struct task_struct *target, const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + struct fpu *fpu = &target->thread.fpu; + struct xregs_state *xsave = &fpu->fpstate->regs.xsave; + struct cet_user_state *cetregs; + unsigned long user_ssp; + int r; + + if (!boot_cpu_has(X86_FEATURE_USER_SHSTK) || + !ssp_active(target, regset)) + return -ENODEV; + + r = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_ssp, 0, -1); + if (r) + return r; + + /* + * Some kernel instructions (IRET, etc) can cause exceptions in the case + * of disallowed CET register values. Just prevent invalid values. + */ + if ((user_ssp >= TASK_SIZE_MAX) || !IS_ALIGNED(user_ssp, 8)) + return -EINVAL; + + fpu_force_restore(fpu); + + /* + * Don't want to init the xfeature until the kernel will definitely + * overwrite it, otherwise if it inits and then fails out, it would + * end up initing it to random data. + */ + if (!xfeature_saved(xsave, XFEATURE_CET_USER) && + WARN_ON(init_xfeature(xsave, XFEATURE_CET_USER))) + return -ENODEV; + + cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER); + if (WARN_ON(!cetregs)) { + /* + * This shouldn't ever be NULL because it was successfully + * inited above if needed. The only scenario would be if an + * xfeature was somehow saved in a buffer, but not enabled in + * xsave. + */ + return -ENODEV; + } + + cetregs->user_ssp = user_ssp; + return 0; +} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION /* diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c index dfaa270a7cc9..095f04bdabdc 100644 --- a/arch/x86/kernel/ptrace.c +++ b/arch/x86/kernel/ptrace.c @@ -58,6 +58,7 @@ enum x86_regset_64 { REGSET64_FP, REGSET64_IOPERM, REGSET64_XSTATE, + REGSET64_SSP, }; #define REGSET_GENERAL \ @@ -1267,6 +1268,17 @@ static struct user_regset x86_64_regsets[] __ro_after_init = { .active = ioperm_active, .regset_get = ioperm_get }, +#ifdef CONFIG_X86_USER_SHADOW_STACK + [REGSET64_SSP] = { + .core_note_type = NT_X86_SHSTK, + .n = 1, + .size = sizeof(u64), + .align = sizeof(u64), + .active = ssp_active, + .regset_get = ssp_get, + .set = ssp_set + }, +#endif }; static const struct user_regset_view user_x86_64_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index 4c6a8fa5e7ed..413a15c07121 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -406,6 +406,8 @@ typedef struct elf64_shdr { #define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ #define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ #define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ +/* Old binutils treats 0x203 as a CET state */ +#define NT_X86_SHSTK 0x204 /* x86 SHSTK state */ #define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ #define NT_S390_TIMER 0x301 /* s390 timer register */ #define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ From patchwork Thu Jan 19 21:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108816 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 18C5CC678D6 for ; Thu, 19 Jan 2023 21:24:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AB3A28000F; Thu, 19 Jan 2023 16:24:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85B65280001; Thu, 19 Jan 2023 16:24:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F8E28000F; Thu, 19 Jan 2023 16:24:28 -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 4D7D0280001 for ; Thu, 19 Jan 2023 16:24:28 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 355E6A033A for ; Thu, 19 Jan 2023 21:24:28 +0000 (UTC) X-FDA: 80372827416.23.122E243 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 3843F180016 for ; Thu, 19 Jan 2023 21:24:26 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FHI692CH; 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=1674163466; 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=JxHOoebEC0ntNY7urTWVeuLnqJvKXHl/N1WxuofeGTg=; b=ZsWZtDzq2mpO0fA75viHKhKOw0Hc7Qm6MBrq74SmN2w7Bez2hOkdYWP+JRzNUnWTA1Z3FF LCyBTELt4G52KrRrg7LZIJFztIarncXJKdTnh3k94yR7cvgb1rxMi4NnBI+bQhU1wiDEhN iPYDR6+fZ8rrRDqH4I+OBcDiB3TrKyY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FHI692CH; 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=1674163466; a=rsa-sha256; cv=none; b=cmagROUQkh7pOE42Jbe+ymfnCY+k2SonFnyMr4pfETJoLkYamup0n78Q6cefiuAnHwK45l cbzmoFe1lZrpY66ruC4E59dlV4w7uYatH2FmJEdtxmyqYVXO1ksda0gld/GCxfVxH0MEFT jbOcS9VEPxns2+lN+Z0l3gmuo8AZGP8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163466; x=1705699466; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=SwMjGEgQ4NIbBGCsK+je+PfgR9ALTBIfAb8eu9YmH38=; b=FHI692CHEeFbm/zc1xCzAjQjtiY/mhwjmySAACfbqKWkU4J76GC0Hycz SRpx2bO5BXpGMq++h9DMEGhb1cA0iI3QFKF9Bwi3QMh/2uPu5eUOkBPsR rz0tayBlWGPOKb82Qux+nf4ZYKYB9Pbk5sxErJOwBpJTCawuNwxhoQSC+ o+Qe4U95ggZ9V1hXYfA1v1A7U+jJPfBZgQrBNkbCspiyavlH0dLSXIP51 DO2YRSkjunRdlvSR18L33ZWTdCf7Hyf4oygvi2NF+JBshpjYLhyr+klSB ZNA2xaJrDvtINxXHPHGrFTaFcSvlk38wjPzMdroM4gM5U/dnAY19YCNni A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120094" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120094" 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:25 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139196" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139196" 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:23 -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, Mike Rapoport Subject: [PATCH v5 38/39] x86/shstk: Add ARCH_SHSTK_UNLOCK Date: Thu, 19 Jan 2023 13:23:16 -0800 Message-Id: <20230119212317.8324-39-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: 3843F180016 X-Stat-Signature: pgc49s49u11h3bj316eb36xr4s7y1o15 X-Rspam-User: X-HE-Tag: 1674163466-162747 X-HE-Meta: U2FsdGVkX1+peVQ98X15rdzug4RhM3bXPTZIWkJbo7/9u6+S3D2Iu2aPaNANvZhku6gBNj2QOaNxYLXYSpi1FZgZqgy8ysvJRleWc26kTsYURophHcGmAUAhHo1VCmZ+lR6FThxHYUeEBn6z664rWJU6r+tudLcr8Fp8BFy+4r7MCE1nv0n6ZrroaiE58m3c31Gz2FuNyuh2VBa2+4eNZtRZ/6ij/hJJLc7tngbClYdee8La84keHsA2JYz6NxcNI+I94aitw1DzU5u6VTLPLpsrBllTSi3BjsP/ByS849/QiUHJH+oRI1ix2fE3I8yATvuTZ2ur42ayqgMXq8CS3I87ECa0EUlQOYTyxCtbLkVC4qKr3P8J+9LLx1Nu31HrQgQ4tE57UJca+ozA1FWlxYEf+KCa+8+V0o9Rx7bg1ZY7cbyCviQqH+yZjfs3exsRQ/0jITx0XiUuLa3jYJKz5Jb28iihaebuXqqx8uP2/w5UJ5PEmOp3lDUuo73V2IsRuF8571cGUnCLo//W6vFBxSxXU/kTw+SlDXS5c1VemNIEtNsnqfTXcz/QZzNG02GmRlFX9cqkufd1eYbad1peyEa0PZDp0vfRsWx4WMPn3cXFzvvjYxBWqSZRQjNlG4l/lPSkmuMB6OAXoCHmlfado5118XR4b/4m38gDY6ytGTAGuzWaUCWhtkWt/XqudludU5bL1U9d+cqYlIwMdkfcLZuhezpVZFiFa/xXANis1ZtH5O1J/Yo/0GUQL6DV2qPIs1rdOnEE1JuwGcSL9+cp82+8n08XWAWpFCky6t5UTB5YoH7eC4u6lVYXxq+E33JFA8l9XDUSMXXo0hN85XHIw7Wwy1sZ2QXOFEG0erXuW3Qef65VZxEYgA3EeghV0quJ8pptF0jQnHg8hJWGuJm4II36SCPn8WyTy0/V//lvr+fzzyC35dwetJoBkZWQEHFNGUKwEqYqMv4ZF9Thfcv K1wez+8u kOh/rtz8F+rptPtaIFCTxV5ilUKvnvPt5luErlqfcDJJK3TBmTRS23EnOrL81kx9btC2ijpLMMTy7z0bDQOmn2eZ9g1RbUn+E+p/dIkAt6Q94HS9o9kz/3wBwVp2Fv8Ql+8kN+x/d28yBFJDDPZS74JfIO7DkXBvyMCuvF+W+9JaJx+Lp+WRrkRAo9o7gRL9P01ETBw+totw+lowDFmXYbSsHbVIhlaGaC8KeM+YS6zMT1Vr4R2EWPZxg7LM9AVI0Szex8eN0254nQURzIQWKA+FDjztgvsruKNywbov/qJN66G6v1CuhpL4Mqo943UAKO6gzaWMD44aOo+OyqAFDyiTBVbtcLRO4plxY+dllPYT1n/A= 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: Mike Rapoport Userspace loaders may lock features before a CRIU restore operation has the chance to set them to whatever state is required by the process being restored. Allow a way for CRIU to unlock features. Add it as an arch_prctl() like the other shadow stack operations, but restrict it being called by the ptrace arch_pctl() interface. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Mike Rapoport [Merged into recent API changes, added commit log and docs] Signed-off-by: Rick Edgecombe --- v4: - Add to docs that it is ptrace only. - Remove "CET" references v3: - Depend on CONFIG_CHECKPOINT_RESTORE (Kees) Documentation/x86/shstk.rst | 4 ++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 9 +++++++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst index f2e6f323cf68..e8ed5fc0f7ae 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -73,6 +73,10 @@ arch_prctl(ARCH_SHSTK_LOCK, unsigned long features) are ignored. The mask is ORed with the existing value. So any feature bits set here cannot be enabled or disabled afterwards. +arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) + Unlock features. 'features' is a mask of all features to unlock. All + bits set are processed, unset bits are ignored. Only works via ptrace. + The return values are as follows. On success, return 0. On error, errno can be:: diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index e31495668056..200efbbe5809 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -25,6 +25,7 @@ #define ARCH_SHSTK_ENABLE 0x5001 #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 +#define ARCH_SHSTK_UNLOCK 0x5004 /* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 71094c8a305f..d368854fa9c4 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -835,6 +835,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_SHSTK_ENABLE: case ARCH_SHSTK_DISABLE: case ARCH_SHSTK_LOCK: + case ARCH_SHSTK_UNLOCK: return shstk_prctl(task, option, arg2); default: ret = -EINVAL; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 07142e6f05f6..a639119a21c5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -452,9 +452,14 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features) return 0; } - /* Don't allow via ptrace */ - if (task != current) + /* Only allow via ptrace */ + if (task != current) { + if (option == ARCH_SHSTK_UNLOCK && IS_ENABLED(CONFIG_CHECKPOINT_RESTORE)) { + task->thread.features_locked &= ~features; + return 0; + } return -EINVAL; + } /* Do not allow to change locked features */ if (features & task->thread.features_locked) From patchwork Thu Jan 19 21:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108817 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 9E529C46467 for ; Thu, 19 Jan 2023 21:24:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43F06280010; Thu, 19 Jan 2023 16:24:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 354C3280001; Thu, 19 Jan 2023 16:24:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180B6280010; Thu, 19 Jan 2023 16:24:30 -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 E06FB280001 for ; Thu, 19 Jan 2023 16:24:29 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A8925140988 for ; Thu, 19 Jan 2023 21:24:29 +0000 (UTC) X-FDA: 80372827458.14.2A94BA7 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id A64BB40017 for ; Thu, 19 Jan 2023 21:24:27 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hB6vCRKw; spf=pass (imf27.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=1674163468; 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=cjMBHlgASRiVZwcEv7s62E9H2RxSF9urW8wYPqkrv6A=; b=KiXoOIZBty+5jeIuvILbsd76Z8S1cMfJeXdxXcsKg4o1TGeyx8Ess9z472jheG2x/8J567 YqD+eL1wCWw7W/sm7Mg8/8O9YMMOg3Sr3BKd8U82KXM+gkJvA3NaYniJJme0KKzgA4qsbL 6J0jeqj1Ls8fIptycnuJ4Ik7gZwscdk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hB6vCRKw; spf=pass (imf27.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=1674163468; a=rsa-sha256; cv=none; b=vEopv744AZv73rDTYLEOWholKGuRCy2Qtn0NMYSlBTMJP+C1drChd8gNWol8WV1ZxD7O0u p8eC/TZEf2yYUqUAtNMzcJhl89WpoCQAoSMfRQTAFcGrVrJaincdDdstCWs+es++RoXFfi +pvrm7a5GWfQmDxs4Y0hb8zAKRp4Di0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163467; x=1705699467; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Q/roafiLg5FWGNhLb00ov8wc44ATIJKREdVQC5GYlS4=; b=hB6vCRKwHo0DaWQQU0IWD04AZrGw0MWUD2JOGBWY6+mSgEB/3e4Zxi14 fa8TchgY7D/aB0YeR9TFE7ArusPocGQl1RubLl5fSivvG8ClKGEmJUjM8 XzVg/0oCleoZgTi4OFMclI4jcBdiAJ0Gix2S+DxTKJkrSiow+a5BVOZZn eUk5nxjJtvFcV66JjclE58wzy26j7X2TqSPPwOmWiCSayIQfY2iaVR4Gu SjKzPSazjSeu8V4wm/r6f6JzD310DyJneJq+2r4NKkalP0kCkB8M/r2uo BSy71FedVs/1hsluTfhKJ9DWI+zSE1+d3kMQxvdPQHx8dOsmbqKWnHFOY A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120105" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120105" 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:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139201" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139201" 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:25 -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 39/39] x86/shstk: Add ARCH_SHSTK_STATUS Date: Thu, 19 Jan 2023 13:23:17 -0800 Message-Id: <20230119212317.8324-40-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: A64BB40017 X-Stat-Signature: 31ahfb7bkui35ezh645si359q1iq73qb X-Rspam-User: X-HE-Tag: 1674163467-457299 X-HE-Meta: U2FsdGVkX18aGIO4acF21Mdp7hgPiIauKNBEm1hjs3iK6hjOp4TW1RppuHKeV08ta03tCmXjl7jWymv/gAHnQJtW0eDvL29LrVks+ir9Shroa8+Nq3Oorl9VYWmtByopF6kNDgsoX1dRbOExEWaqgkXoRGnvsM41F+/im87EfxIgDNEBA6b5hGtaoM+9pIjpqRsi8TxSBMvWXNLOCsM3+UzJf07uLkZECpTIJ8zJN1BMsj/yF6kRSCs6pFP5EY28gi4UkLnNEsPlYHaIHyQh7R37OiIghlyvjJSK1T0a6glxgiVxsB5Gr12ARpqqBIQ1nefFaQr+pA6ytshrmyh5Aw7WdhZGqT2SYZNxFPwnZ6dE6pZAb/wmrYaQjMMhwubO4u3F2V35l+yNTurddbhrlLHVJ+frHR655CELfIpH2TJqo7ItNfQurQDnQKRCtR5ryJ022qELsuf8T4f7m3A/Bk3h+eG+kon4T2hqz+MczhoZJBWLEs1WCEjhz784KIpCy0UEBID5xjpyL2rHJwRzmVVrPqH0Lrc40MwqnTm6QYl1BMl0yDVS2vFPoX7+OPHLo2VNyb4Q8T1k9OnplpL8D2taWpBxfcMpcuXBr866abQitUaRSePbZVBexQ7kqg5+Q1OUvEGSfVwTpVG1F3XgU5dUhsN0O2lsPxyL4HA8UJaJpADG+6lVxhqbPk5B4b8gHAJXgOSaiA4KhNMN2MGZAusRBjpVd2o64qgwnMeW3YG7O/sP87tTBTfDdZTgB6/OtE7avugFWq0CsGyKF85wJFl8n0A4FTbnfVKkICpfivbGZICakzJfrSxRrqhsy4ULqUN36gpJSJ92TKRq1+VDh3oiLyYIXfO/C51XcAzDWQRnVcHGs05g/ycDuKMFByI/w8+tUdFheAwDbx8LzPqJpRmHU/0Hb4Lm2SaxL5XixnXHTfazlpne5bcp2FzpuwMLK7OJmSl7w4PuMycedab xfTS1RXh 3ScSod6nt4s39xPJV/O0Xfx+YPDwhGH4l/tUOrq5mMXVf1RC3hXm26Dfdx1PpzOvSBsaCIXgX2S28A5LPyexg2T2OfT5rFLWQq2CszkRHR+3dxYlgea8eX2yVQ8PtolLCgl1a8sdqYxrWbWAhoWYWM0H3fyCS2bwqf7MdLfiO3E2XUgpk+G5bH4HjZxyC5HYfvNkVcDbsDVg6OpwspHPGzs98B8GFWdeZsBbApeKFABkTXohiNfmVZZQtrouJyEo6tltUJvdLJLBkouoyqAhEx0coSpA65mcwPYeFH+E1+ffB0M/T/LonEpJGb34KqcHvke+G 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: CRIU and GDB need to get the current shadow stack and WRSS enablement status. This information is already available via /proc/pid/status, but this is inconvenient for CRIU because it involves parsing the text output in an area of the code where this is difficult. Provide a status arch_prctl(), ARCH_SHSTK_STATUS for retrieving the status. Have arg2 be a userspace address, and make the new arch_prctl simply copy the features out to userspace. Tested-by: Pengfei Xu Suggested-by: Mike Rapoport Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix typo in commit log v4: - New patch Documentation/x86/shstk.rst | 6 ++++++ arch/x86/include/asm/shstk.h | 4 ++-- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 8 +++++++- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst index e8ed5fc0f7ae..7f4af798794e 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -77,6 +77,11 @@ arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) Unlock features. 'features' is a mask of all features to unlock. All bits set are processed, unset bits are ignored. Only works via ptrace. +arch_prctl(ARCH_SHSTK_STATUS, unsigned long addr) + Copy the currently enabled features to the address passed in addr. The + features are described using the bits passed into the others in + 'features'. + The return values are as follows. On success, return 0. On error, errno can be:: @@ -84,6 +89,7 @@ be:: -ENOTSUPP if the feature is not supported by the hardware or kernel. -EINVAL arguments (non existing feature, etc) + -EFAULT if could not copy information back to userspace The feature's bits supported are:: diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 746c040f7cb6..73de995f55ca 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -14,7 +14,7 @@ struct thread_shstk { u64 size; }; -long shstk_prctl(struct task_struct *task, int option, unsigned long features); +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size, @@ -24,7 +24,7 @@ int setup_signal_shadow_stack(struct ksignal *ksig); int restore_signal_shadow_stack(void); #else static inline long shstk_prctl(struct task_struct *task, int option, - unsigned long features) { return -EINVAL; } + unsigned long arg2) { return -EINVAL; } static inline void reset_thread_features(void) {} static inline int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 200efbbe5809..1b85bc876c2d 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -26,6 +26,7 @@ #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 #define ARCH_SHSTK_UNLOCK 0x5004 +#define ARCH_SHSTK_STATUS 0x5005 /* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index d368854fa9c4..dde43caf196e 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -836,6 +836,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_SHSTK_DISABLE: case ARCH_SHSTK_LOCK: case ARCH_SHSTK_UNLOCK: + case ARCH_SHSTK_STATUS: return shstk_prctl(task, option, arg2); default: ret = -EINVAL; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index a639119a21c5..3b1433bd63c7 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -445,8 +445,14 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return alloc_shstk(addr, aligned_size, size, set_tok); } -long shstk_prctl(struct task_struct *task, int option, unsigned long features) +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { + unsigned long features = arg2; + + if (option == ARCH_SHSTK_STATUS) { + return put_user(task->thread.features, (unsigned long __user *)arg2); + } + if (option == ARCH_SHSTK_LOCK) { task->thread.features_locked |= features; return 0;