From patchwork Sat Feb 18 21:13:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145641 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 AEDB7C61DA4 for ; Sat, 18 Feb 2023 21:16:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A29DC6B0073; Sat, 18 Feb 2023 16:16:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FF916B0074; Sat, 18 Feb 2023 16:16:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8787E6B0075; Sat, 18 Feb 2023 16:16:03 -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 79BC06B0073 for ; Sat, 18 Feb 2023 16:16:03 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 56B72140222 for ; Sat, 18 Feb 2023 21:16:03 +0000 (UTC) X-FDA: 80481670206.20.CF82DBB Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 5ED1218000B for ; Sat, 18 Feb 2023 21:16:01 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NoiThyso; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754961; 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=JQwRNHaHkmY2KQo1mXW/rHmQcKtW0pujni7BobXekj0=; b=1Bhyiz4doljBnQXpzgm8k/mHOfgKAeIfFAvyPFYk7phKGM5F+wwZ88goBZ0OebR52oWyuO cl4J12/VWxJYMgjHMBtDaQMssc9tpmKEG/xaT+QFctERFHLY5zjbivw10HxuHl7zd5OK/s r0DNhtPxKVjbW085tZCWVR5sOPAsvZs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NoiThyso; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754961; a=rsa-sha256; cv=none; b=jMCqWD+5KGyH6z/N7NbkjZBbRzmy0hB6lmQYxu4VrUSx/lAkhhoTL7vhNj+dqSo7bMjDY0 T0NNGs2tJyNqWs9TF2B43M1P5xb8di+OFNHojAOamn87pw9XcCQV0tfgzq0yy31Bl497bu g4pvMPgp2oUW6fPg+6xsE/52eYtSSjE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754961; x=1708290961; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=edN58pjbqU2kcMkGqoWutD5goF7QxjbUm3FJJChmazQ=; b=NoiThysoNsOyGk/TTZ/jNWkpAnhV4cNOxAd7pNLFn2Ln2fNLi4Xa/RAn bDXv+PC1OBam5/xFwvEBUhnGGjLpN1lOy+HAeJNIRmRSavPmvEclq/cL9 5Lq7S3x2lUozgWP8IwTBl4d960dI2VP05i3G3JInyEbI52odsbEEL8yAz ToXfUyGHmab0AvZ/PfEbFFMWV7E7g8zbZR+u9/Ol1S6yTavkIMJbm3m94 nG/QGJ9UzU0ADkGMjQRVWNR/VqK3kzBh0W8ZkiSuhtFlrxCmMNYYZWkKP sVa7i5cuNCWlE3Sgd5wpfFym6VxFWlL/oJWdeLVF7DEOSoaeJeAl32BbU A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427101" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427101" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15:57 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241571" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241571" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15:56 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 01/41] Documentation/x86: Add CET shadow stack description Date: Sat, 18 Feb 2023 13:13:53 -0800 Message-Id: <20230218211433.26859-2-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 5ED1218000B X-Stat-Signature: 87d9i4amp34o7nsisswxj8hpnxtg5jcx X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754961-9769 X-HE-Meta: U2FsdGVkX18np5Y/ekA6EcFs2HPoha6mtPpbzya7IhVzaXBOkwDuhlMEFZPDCXwwtLmYGnSn2rvoN2eYEp7jGy7EZazqNQH7wxXsSZQd0CXqIYMvB5rXUvCgzFqpMtsgMs4uEaftoegtn4UWvsFoj+sMmVehsyj0X7TpQ/lU6OG0AnCGzQesE69E7bUxUDFu+KYZYPqhUZ8kYrynJPU32Djy1qam4vUtEkRfZp7wKylTQJSjKjfmAEaGrhXTfs4iqeQ8ysnQTDe4njuYANGQ9YR+iQhPps93vwF47CDYzC7SZGnnDQmUvebBwtrkjcAm+rkbZ/7iu9+zuv8UcOTpZ1GO7EVNUHKUk6GQqzP3ZFADtiqOFd2wC+3wYOZgzP5fi8sQ5x6DaZcN+2eMV7nG05DbsqlKMJvg/64fKij/9vY1cKLLaZqOBwFcPcuUBqo7rWtk/ca8v31fIPxyFVBU6zm6jZ4qhbwpzXfvUkvCO6K4fnjp45PhLQFK/vmqfqBpd+cElXNKuAFl108BTNrVeNq+Jh9F7vhe11lSBrUikKc1jJwsKemuqWa172rCZzVOgrknDIoBAvvo+rGdMJLBgUy/P88sJYr654sJ9dLZHXpNrnMNRHTQcjo23UbJdNd16rIKeWKtuSmFHgnDdU6fofp9UtXi2ZGrHYFtQqRUFGWqE1jgN95ALrunfnPMdDvv1lhUbkjgkslDc8Pg0lNngwbV7VRgq0f3fHXUbpfSeJ1rGPKWiAKRXQWV8sFQ/8q1KoFFpvWQCqVXngYEKq21zqWXo4MHPOIknEcSt03nus61iDveJdZVYgUV2NQWOizI/RQTvKCPmopvsF5KzdsM4XwKz9+sHYIeDWAOQz6HkTmz1EAa/ObHrPa9V/C+o0of+xjIwFiYw4Bw3mBD42bP0w15ReJinNWxKDWNzIA4ziPhBw9xKu99RY5yQXMKbRGc2LSj1uZ4UPhG1ugXfvT 19uKOBlE /dQAr9bUFcC54wbrLQvamGkauYrdEBDgUJBh2pyUf4wyV3YaDyaqv+7pZmiZYw2tNOGnBtpmz/SzqXIH2gwUTXSMbUK02AwD4FPYLT+FOOXltiVMutUU9PZbaWxqtBGERgsoLxkzVJ7iYb1RqnisRIFCaX/MDYKwwtx4rrsn1OaV+odDPI+ZBnVIKVwV4nOPK7eqh4wVRuTvvAkIm/qCs7dcfNh26T48pzOn0L8kx30DpvLkJEmEB2VfmDUSN9FqA2rf/KCBLPWA+YwPDYCJwnhojhRtbct2LgJy1 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: 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 Sat Feb 18 21:13:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145640 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 AC2C3C6379F for ; Sat, 18 Feb 2023 21:16:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6351E6B0074; Sat, 18 Feb 2023 16:16:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 609646B0075; Sat, 18 Feb 2023 16:16:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 398AD6B0078; Sat, 18 Feb 2023 16:16:05 -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 218176B0074 for ; Sat, 18 Feb 2023 16:16:05 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D46B816053D for ; Sat, 18 Feb 2023 21:16:04 +0000 (UTC) X-FDA: 80481670248.06.F94E119 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 965EBA001F for ; Sat, 18 Feb 2023 21:16:02 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hm+c9wwq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754963; 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=sD7IX5vgBO4fM0jG6hc8nWvqQVD7JOaDFzg2SU6zIdM=; b=HJGIzAbmX78Afx+SIIbHhk/FqasOH7Fexa8CNdY5Qnc6Hkvm2WVAEMpT6r9oazrQJ/eAM7 rd1Jgh1ADpK5rISClWll5QqQ3VR+DPeEdib/JEMJ7FVFaF4lmYT7Cx9oH46Wmig715atZB VwiMfRSm2oHk7yhVZgZZypDekguKoEc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hm+c9wwq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754963; a=rsa-sha256; cv=none; b=5dNdxdpLFYv3rDwCWbBI+1iOkzc6itFZZBPFcEbqGoj4qrQDZ+WA8ygy+nwbvD+Ue4EQRO Bi6K9SopbLL8s30NISPQH4Rhd9jGP46THQQI/ixudoSfCz3TAgZEARB/pM4UDlUlIgdfJ+ A953OYth9H2SqknsqK/3oGasWCXbAAI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754962; x=1708290962; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=4iLM/QQV8kQnF+xZq53uEjXr+ZUWXLeX39BIgsKBw78=; b=hm+c9wwqgXUVEQOboElX0ToCTBC8BZcc2PddQLk6TkVQ8bSR+I01W/Pr AVTOhkalxO5bH4RlcuOc+ZA3a+OELFgwCTxHEi5it/lzPzBnh016GkjWL gX/Ss6rno3RcafVrWsQH2izNLA7wRSApljW+Z044LjkopIXHy1KnNzc+T f0pZ941WhffdguonZ4YJZRiP3Buep3J/iyOo53HHzMkLa9t2hUzaZPKLe 4Z/Zl3N8p/jdAPu92yGBP3N0H2TGROZwJZ7tEPFE2PSNwym3DiA2e1fIM ifutOUIvDLnrxKXiGpVMQwlBQ8Mu2lOP+mW6McwhAvXfL+gNUbF0am/kp g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427122" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427122" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15:58 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241574" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241574" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 02/41] x86/shstk: Add Kconfig option for shadow stack Date: Sat, 18 Feb 2023 13:13:54 -0800 Message-Id: <20230218211433.26859-3-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 965EBA001F X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: oy1nam7qbrhj1uu6e3mjwqwbfrgz5kee X-HE-Tag: 1676754962-405559 X-HE-Meta: U2FsdGVkX19zck0DPiyGbLxeGz/VNzSDzk7cTijBDFTEa6RKsyk5VzH7TP472UjOcdLwohnsUkl+6Lgcmpcvhh2o99FOgW0z18Gd3xmhSaWBNzdqJgBVUaU7/vID/uvL2AgaDQODlSdUWg2UP2ysnQOLWcO/oPjIAfoz/Ax4KbEMDBFvA8jKd8ru7gGZZthVXH2rfqvaceKHLQfN6peth2RFt247Au/C97AmflhYUvOQDdVopKIzgQBGvoVhlblRUhaWKuKn5tLThm1xSq2lMr41AEwiBOq405Ivbe2hy/038p59gbZXSBSxycRrOc/tyu6Ur6AwPbBMz5JhYtvVTHOt40IhRafD4TxAu2wC5FVC0sEaNaRguc4bRElU429Kbct7OTfGLWyj4DTseuEgP6bZdoD9wMuhx+G/qjqSezRMyLoo1ZEn+Qa6CTVLSqlT3py9pkLXzLE1P9dlmnniSeeTDmI1Yze6dGrFK1dTy9mGv9gngem+Ak+16aFkM70pIbypYQrqCr6ZIjgCKw2Olynyz0z4J3qFbfXQWtoON3mCM5kJBuVxE9Cuwr5acxq/FFXyC0Aj6O+bneOQNuT9BJhUGmeT6T/M1gKFGIbQ82GflGtL4n/sTK3I9q4pt/PyqYaNG0B8lG9BOnt/3eXYijXHAXp8ymPuewh99kCkFVCuMnN+azDtCEih7PUQjXWpasXubFsdBChdBSKanqheVkt1C+oYcswc7aLju6qm0zUOBS8Cz1dXPjZ9RslsWtL5vUffpZTaiPm9HlgPNVlrocgJbr+7WxWPWgv0bCCQRraEd8zMc4NdPqmFmFNzVd1lyqoOhBPfs/G/BZCII/7Tz31DRqwLsrZdyjll9MJCcplreQ7ya9Ajn5rAYdaDSLi3MpU6QohuxjvbpNIQQDyrQNHy2wF07zOArxnfAxs+tLaVbI1tzIaB8ZVReSJW7P8r0X4ReuTPZeOsd4goKiO 57WeurbQ lfDS0GJ8L4AWL4kscN6OeIkEU0wWIw252UnKAeKZ3xL/pda7jn2qQ5mN+aN1GXy6Q1poqsPxgMpLpJqFWgZpaQ3P2JT+iHFC96M47Q4Px5lhPbvj9tHq1Lrubzc1rD57FW1A6TfqdjQhGJIbNAhBI++CDSfiIeSFYI2cJftCC0k1770AjVkYicWjelk5YCqhiaJDVdtecdJTR6bWY8JlONZuKPPkZweADu05qakKr8d1mRULrfuRviDScUq2s/OV/10mYf4kV2bVoqlDgRw/FUrnPvQui0JanrAtITSOm7sUoAgb/wmNefZlqUA== 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: 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 a825bf031f49..f03791b73f9f 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 Sat Feb 18 21:13:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145643 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 C4909C636CC for ; Sat, 18 Feb 2023 21:16:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B3E86B0075; Sat, 18 Feb 2023 16:16:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96527280002; Sat, 18 Feb 2023 16:16:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82BF06B0078; Sat, 18 Feb 2023 16:16:06 -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 62BEB280002 for ; Sat, 18 Feb 2023 16:16:06 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2F69EAAD6E for ; Sat, 18 Feb 2023 21:16:06 +0000 (UTC) X-FDA: 80481670332.05.8FEE1DB Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 2D80040024 for ; Sat, 18 Feb 2023 21:16:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QoR9Uns6; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754963; 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=sdaF8qREkYkB5csxYfyAu821rzAR90Mq4vm6siSgLrI=; b=osGwO5YM48bKe9RnpWknYFVcYqUcvuy88mI4QzLA4Ruzwg8mbABeyHKWVo+kPzw0fz/eT9 0L0Xn697Ee3qmfOfobWAzFUC2jbW9VsM8TT76C5+PXsBn5oZnqm0bOkP37ITvm4TkCS4Ft 4LvF6f+7LDjNq3hg2TQDvCah3MrbwGw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QoR9Uns6; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754963; a=rsa-sha256; cv=none; b=JYBGdDaRl9aUk69u8jGnH+p5kVsmR7tIS4JE9H+4xCxozj3dPmILYZotyd6zJlTYql5kMe aRHD9Qu3R0WU2W691bOOzp/HY4NnEdrGh4AXzVf1+RlqWyaT6zA1HuoK7TFZDhJyfS1vxp 5PgDXNlkh3208nBTzG231XU7bb7ZWHY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754963; x=1708290963; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=taTLN9jgTVYTLY9Eitvg4bLvIT9+A18uF00hT2wXoJs=; b=QoR9Uns6+z59pdUqv/+4WzK2POjyoZYSbYZSEIzMXYVIgVdxKgfr4nLa 0ATaaH5/E/E5Pwo+tBlBtc5CqZwqJg5vSLFibe1ImEI9rX2YTVJ7yTLJB TPW6R+fcJ6AoTRin10+QiB8QKe5RFjbSaxDkPZ6brSXSYK2zmLKWrL3MD MYTsPxR6AT5ySAtLvsKhZ9XJ1+NsQfkHhYVa1j/VAA/tskjwcJQ5Ucllp Q0V++1QdXldAoNrwHQ/10M1/6JSm7mS78zs7e/fkRnETJAPtIBGwUJV1e 5YzI5MKSqjEOdOkQyLKcZT69v1e+jiDiJNdsSlmFBbc9WOyReLIzOKeRx Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427144" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427144" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15:59 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241580" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241580" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 03/41] x86/cpufeatures: Add CPU feature flags for shadow stacks Date: Sat, 18 Feb 2023 13:13:55 -0800 Message-Id: <20230218211433.26859-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: docqf8ao54ni41uaq46c6iyxwqi1riw5 X-Rspamd-Queue-Id: 2D80040024 X-HE-Tag: 1676754962-681410 X-HE-Meta: U2FsdGVkX1+ljC4L44nR5xbMcw5uPhss+EIIoRRuFRUyzcdzaibLyuvdQjykOLBx+Udx8pfy6qzAqs44iGcWLyxaXZuNmxbAo+scoj/eC8wBHXy+a+RdeH0EfHx87ctBn3UNOPTrRUi8hz0SYqHuEIPDncF19o8y5oBw8vE0YDlEXdhOmkgKeemvT7takK7PUlQTnqVTItH9iNOCK274p64oK/Mix8kjqf9EFfGLlWWNcnHHrJEjWhYw7f3C2tVhA0cIksyQICI8NMjcUV0L6AVygvJ+aw3et8mPMbQdZmjOWvXnMJjE0LLJY/h38EyjplCxGe2/itOj1zixwpG8ShxWMkuTSpV6Nly8SxxQnvfSDbmsCdrsJ1KuguxNQkKCxVwCyZJtDc1PEneuN6ih4HqWD3HpLSYvUJ14PTdpOfK2v9MXNypqVv+TkgDGvdDhmR7ncFaArY4cyIQVyZHeZMr9k7QO24HwZciw9h3MUWOckLlMUxc12z2WnNFbJsupf8lRjtYqrofTkbBy9fr6nqdexzb/D0dQbAk5Me8c408mPzXYGXqozuYyxktMu1uZ84LLNG2oYyiIcdQn8qoilnmQdactMmZPvUBHbmherb2dyTtftUyRZ7wF0wiRFJFponInWu0YzGIovfia/nydpC2S5u+zPFvRS+rgs5OPj3SXL2amuVFQqsCH+u4wR/H+gn/xOpa1TTbO3JVSOlg+3swpYxTV71bohlJdLowe2nltOB3w/G+B6Sg3w5y5+188tDdRztYeELd65mtGPqeWqw1XcAFZyZdLlkIsl801CPrDGvt+Ze5WhMsKxIW/mPZLXVpvqATUoCPz0BAhZG8be2kwrEjIoPjofsSPR2yklMaKTDkiCmgIcTpH0lgnJHR9vYbT5ZVZjrSyXZyH0NaBaueHtogzY1vgFLqZSmOyABFkCLhF9aWcrke0AS8H9vTc9EgtwUg9Z5beGdfOZ57 TDaUoA6L dlCbp1yB0fCqEdbSqGM9ZpEmeeG6+glSkakuUBG4rVtsLB9f5pcjW2OS2W18bNYiKLMHzfHB8LNV3uhYW3EBNaAvhy5vn9sQs1txL8xNmLOUEaO7e+vglIzQBbjcxkJF7K4S9yXXsr5v9hqMcep75pHzgVOGMl4/z2JIcZOJKXAHtLBY1f/HFn6euR8v/KOji2mWYmEI2eyn3SdAIOOq4wAdh31QczIcF1E0s0Hfa+zJgLpOHprLgkCa/tTQxh5fdrJInhVCzGSK85lJlrmssncXuiw6zhhWZD+07 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: 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 fdb8e09234ba..af4178e0d76a 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -309,6 +309,7 @@ #define X86_FEATURE_MSR_TSX_CTRL (11*32+20) /* "" MSR IA32_TSX_CTRL (Intel) implemented */ #define X86_FEATURE_SMBA (11*32+21) /* "" Slow Memory Bandwidth Allocation */ #define X86_FEATURE_BMEC (11*32+22) /* "" Bandwidth Monitoring Event Configuration */ +#define X86_FEATURE_USER_SHSTK (11*32+23) /* 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 */ @@ -375,6 +376,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 f6748c8bd647..e462c1d3800a 100644 --- a/arch/x86/kernel/cpu/cpuid-deps.c +++ b/arch/x86/kernel/cpu/cpuid-deps.c @@ -81,6 +81,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 Sat Feb 18 21:13:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145642 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 E0617C64ED8 for ; Sat, 18 Feb 2023 21:16:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54A15280001; Sat, 18 Feb 2023 16:16:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F8306B0078; Sat, 18 Feb 2023 16:16:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A2F5280001; Sat, 18 Feb 2023 16:16:06 -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 2D32A6B0075 for ; Sat, 18 Feb 2023 16:16:06 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A3E03AAD71 for ; Sat, 18 Feb 2023 21:16:05 +0000 (UTC) X-FDA: 80481670290.28.940AEB2 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 7BB5318000B for ; Sat, 18 Feb 2023 21:16:03 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lcK4Gt4K; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754964; 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=rj6t87CMrj6ldVU2qcRajQjKqhDovVUzliCKYD1NMbk=; b=avkN3NshRD8vaCBvp2b3zUdF29P+1cELwgmnlExeoptaHRKQH7mXwx2dNdH5dNuKrBCXFq SD8Uh1C+em2MAvxmfd0aMBOE5t+slH2d1P+jg7zLwNartCdcdf4k0szBuhSQUxnj46zXrh 5VAj901G6EfCcOKlrIBea2sXGPQADWc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lcK4Gt4K; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754964; a=rsa-sha256; cv=none; b=RpxRs/hYJlVH4cGIgl1wHogNokQaKOjwcEAopjwfjK8ytC3kKzuIX1Y1jD8UdCaev7jfkD 15VbUNTAy8ZUtynVqPWSRgML2o4q5BNDRdO11bSrcVuxS0AHK/grmz5MncbcWcJdk5cGwX f4Pv0ERxfGIxK9Tl0cnf+d/KFS0RuzI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754963; x=1708290963; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=P0vRMRHavNRGpizZkq3RL8rMxSsVzWcbrEXvoEvUKSg=; b=lcK4Gt4KNR75XiiKK4mv0TwvsKnQXH8l9gUk/uRARYHw2F4/hoKgPnUl zIfDDgOXyYpconISNDd5Pt8vmLrBdGD9LuNot5ULCzjkUB92wDLDDWpzz cimRvcJ9hJYFgB2wkEoX7lXdBr3jxErNTvQrl9UXozSCWDdUqPfNp4MWt 6k2VCt2dk3Ii2IlOMGiemgJic9I234/og+TWBcZnS4FWlDzKCLvn10MtU JhHgQCK02Rf6sppgHuAAnnkzAoSPH8n7QWplIv0J+3l6ZD8/DG2E95Sre GXPDKy+aAIDHfRsehlsIqRdfjFbwFEQJ83UoRPjH+bN8EuaIQTgUzq5rX Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427167" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427167" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241585" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241585" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:15:59 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 04/41] x86/cpufeatures: Enable CET CR4 bit for shadow stack Date: Sat, 18 Feb 2023 13:13:56 -0800 Message-Id: <20230218211433.26859-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 7BB5318000B X-Stat-Signature: qyhsbqotd1eefoh4emky461toea4h1md X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754963-7609 X-HE-Meta: U2FsdGVkX183V+zqw59Nq3fdpuloXmHTZbWRARtVLpKrqrgyYGCqmgK7uZxEdb11qASjlEmsjHK3LMs3TPzapv36zRWHU5FhZU7gzi9weKqLOaROOGigoYB8l+4OF6aMA3CFIGidj14ouSFpcs6CBLAD+cBg6xxJ4qAHMwYqDKs73dL6NuPoH8iTTEcO5A2KicpwKsV8HPv3TxEweTJzTNZFn6/P3hPEp2qfSyMSx9hkUug2+UfJ9GexjKtUbTpR3DLcIwLXkCKb5tEZN0m/TPRPeGgkZ3RRYsI3a8fQeZQb9bq85VUiF53kxRIdD3VElM1F+w5r1ikfthiurrsURicuLhsJZtJVnZhQ3KCNBhwMFsnXZ++kaHRT/gvmr1wmmWD/OtMsCRWzsE2ihpNMDvMopQWpxx1BEu4fJdMNN4tL21VljrHXPOX4lT7AtClQ0jpqTeVnM+CW813cR225/VLpvuFYEd3OmG8dvinZpK/nmPpqsnf6pqud1wUROeDxEjSTm5oIpVrLMY5MXqLQTda2ZNz+jX3c5GEnwromKxT0MPB9HnWiHTCp/xUiA/dL76NIOYPysg0PuHOA+bQm3vNUd8MyMoWP8p14VhGeAOjAQVOSki5jra84lsrFllxn+jS9vOBeHsFzHkAo327V9sL5ii0YIyzeNpzr1htLQI3FEtVGIePiFSGNNs4YseTYhNDJEzUlXXpw3ezNyy56LDAt3kcBAYxQXaKxAZSdZvnFfphZU1CNCwhs4ka7qj1K9UG0s5hdiIrwnzX5POmqXKprRq8waTUpsBJGK30dohIzvZpZOupGFgktxuz9i2bxsjc9u+kbkixzHD18r+9I48m+C4r8C9iP/doRMAP8feteSPYL7b0a+sLU1nDXOzhJkZrAPsraymcCtEuEIHvDLnNy67iXbk7JYKCq1EX3oHm1f6kvSu7TEZmrp039oHwIwj5zGwpNTAeMAysQevW 8VgdGia0 Uxx21EE7KKIXjuBZ7vKNOxmLLJmlODAUhj5TDkEIzHuHXoTyrPlo25fQCM3iiSVIHzLj54DHNhi4XS/QM0O56YjbSc10x/BgSZ8VarSy3cgWXK948XB/4cPZXy0+l/GOkU/VbpCHiGk6DULt4vCBbh+t5mmnbJoT1slfr6VORzp4xDKfGX0wp3/jr7/tyvfY93YVhAFkMQA33GDNvkm5RMGxTZis4FtcmcLSOS9WNH2/sHNd0epJGpPByWsIOKM2DdG+80x4nrqeN193VWDJo1PanIlEeP8GzdRul 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v5: - Remove #ifdeffery (Boris) v4: - Add back dedicated command line disable: "nousershstk" (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 38646f1b5f14..30c524cd8cad 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 Sat Feb 18 21:13:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145644 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 9B2E5C64EC4 for ; Sat, 18 Feb 2023 21:16:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FA28280003; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 481CF280002; Sat, 18 Feb 2023 16:16:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FDA9280003; Sat, 18 Feb 2023 16:16:07 -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 1D233280002 for ; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DC11EA01E9 for ; Sat, 18 Feb 2023 21:16:06 +0000 (UTC) X-FDA: 80481670332.14.434EB3D Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id D1BA7A0006 for ; Sat, 18 Feb 2023 21:16:04 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L9elGjzZ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754965; 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=GNnpf5k0kJ4uatG0hpzQ829Pk8hsupCBv0Rp85ze4k0=; b=aCMN5AYbAX5KA83i3LNQTX3+UOFj683gSa4/7sFOAOa8yB4oG3+sHvW2TJWV4PUymlQGjC TV+jY7WHJCGkfBMoMWlF3oWqSUt/6qaTCkenKv3iRdlBKEHoEYkAGGQW7Tos5RheBEzb5p PH00/hY9NODiN+YBdG3Toq83nysCtDw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L9elGjzZ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754965; a=rsa-sha256; cv=none; b=K9+ncDkkHtPfn0Wsz28wiDnOEGFt+/tH+4cllgSxcEkleu1az50pqR1NhVNu0JCcNSahQF 5NY5IrI5xhczjCd4DZ6ZwUfQNE2QJp5OqncLhbjtMX4oXwhGUAo2UEDqAy1kJFpkpahDJD k8FCaTeaMSMzDOI4X+W279Yv8tMDt7c= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754965; x=1708290965; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=5VUE7h8Acx3iz1NYmWp0nvOV1gNofXGCRfQNxNEPRuQ=; b=L9elGjzZKQ/OtcGGX5mn4siwkqVP9wJKbdA+59A04DO3iY5BUjXH8XZg ucQ0OtRJJvfCrZfjnehrkayX6K4KgjasiGHQjGdL18zBUD7s2NtzOgO7C EW0UWcNo+Qo9t/F+QKUfq0aK46ZDtr0pmmkAFDLBJw0W6rSyfFozQrdgb rJPJAq9GtyYxHfj5KoAjn+2EwbGwyDIrVte6TYuTTbRZXHPq5ISVdyHKS tJeams6OKZEvfaucLYQT+ndZvfrVfWAol6Yzg+PTygzDeNX/JI4lwCbMl jBNF9+z9DjTbVKApQkAUwgcHuuot/B/V7z0X05SilBzvQ4i+bh/NtBWMC g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427190" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427190" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:01 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241589" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241589" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 05/41] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states Date: Sat, 18 Feb 2023 13:13:57 -0800 Message-Id: <20230218211433.26859-6-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: D1BA7A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: cc69urtjydcxno9pampc7ehnn3ryme1h X-HE-Tag: 1676754964-193497 X-HE-Meta: U2FsdGVkX1/azzczaXoRAMkjhHU0/KuNDZKJLtn4GnpbYNoie0N5ZqRvKcpeoHZev1TNtxm/2+s4iw1wmyEDvOxv3B8Fyf5p8mQw9dgetjxF74CHtZauc/bF2zx3xYquOqIOkkQ60YrzocKryB05pjJB9zCzKU8ye+gi9YPkKRXE/zQN8hv90NQHIhpjkH6RAh2Ngc1HGc+SjZ3pflliDH3aK+j/DGIsW/uU2C9pTwqv6CouqYFEJd/TudBXMWhVlTwGLZZlzEhFd9kt7CbH/+Jgvk9Dz5wTKMMRVUXbnHTbnliPquGFF6mDGd1AzTafACsacSUDBQ/UGkCr4EMtWoYKONbtpZkSoJYHn+Q4dW0rl+AhI/xAsLYMFPxxp7yHVWPDBSeJN2a8RDQ5TyLyj+g7/wvtgmZ1HX3doJ/6klTOqqLKFfbrv/ZSK9sUNB2UPSOqNQcS5ZPZQWrCAcD5UBBZyExBuMnxHKP2DuU8UDYEiVGKVuy4uSpx0C4LCnkm3tUWXT+U0drCr6W4Kye3rqD53gvhAyGb661NkBWRWJoKQC6ZrU/g+FoJm2ptsimOr31T3Ph26dDpfZ03Ll8R85k6eq7BYSQQoqDBddMUB3/ha6+JpPXJh3WfZVyv7tGuQHgjlppVSMfXgKUtt1dIo57lrfZ9x4ylPgk/H37zijL3CfvaGN4wAPgIV11DlWlqA4VoerMCtq0JQEP485MOm4NERRxioNmxPPojjbMw11+sK1wXmQrHjmhA1xKv028Qs+QOJBMOyxtmH+sdIcuJZkGb+zu+ZvwDEfDFV3tzRCewl5cNrd7G7Kv0lzjPC/PUmjqXmYMqu3gKkOQX+YKOTzSxitP4ezlAqO9m0bAHCFwp1oxywAmvdCKnmMnPGCMUQeZTCxu6oZ/ANkLpa9tLNQEbt1PAP5ZsgHbQAANXSJhqbukpvgiiZ31wRimUAcCxuaQqFEWl+euy0iVrPfk 4qYkbuZE lqUti+biRM0IEtfnQpMDFg/cDkmS+V09lZJlxumwjPDte2bDvSh32/etC5MM13llcZx30BlSMB1STRvMrnBtqkw07B4SudOFbUMEKcOa6OpQH95v2fexhB+X4uiHY0Nhu0nEyvR+pWhkpvvXJ/bGD0n2KeAetKtiPJbXwEFB2Izv2zbfPj6qrTbBsnEVtuWmAN0BiqjV5343fuRdfW3rDUsftRWGZgWNpgS1fRVqp0M4q+PK4hCaOZ6pur/OxABZto69ThxyzwLlzMYuit5C+HrVKaIlPBwFPXul/ 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: 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 7f6d858ff47a..eb810074f1e7 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 Sat Feb 18 21:13:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145645 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 6012CC678D5 for ; Sat, 18 Feb 2023 21:16:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B135A280002; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A9D0C280004; Sat, 18 Feb 2023 16:16:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EE8A280002; Sat, 18 Feb 2023 16:16:07 -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 6EE71280004 for ; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4A131AAD71 for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) X-FDA: 80481670374.11.8DD960D Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 6155E4000A for ; Sat, 18 Feb 2023 21:16:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XE1WC2ce; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754965; 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=1qF79w3tBeVewLR/G4DWxS5EV45VnTvhj/Fuk26jDzE=; b=AH7z8Pqp8qCUHiO901g2sZzAePaAdirMJk9/vzcyC4y2Sq7Tkd2CBdfXRRIJyRmiptRvbq D1wYTkOLScvirPZ5iRlVuB2E5aM4HMzvGEDa8Q5LqpSs7vOT5urxi5PuVyOaFGbMz4e01X mcsLLXcnzrGsQImxaPWkHmJaFn7rcj0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XE1WC2ce; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754965; a=rsa-sha256; cv=none; b=Qi0BLIJBBBj/w0sRLm7qgRw87HAEz6m69SfuxMKHJO0ce6OuYtpAQU7+FeCGbOp3Pc+8aX ziZuiUyhrEETc08Y2pXLMAZ5jJMo00XRjfXM7e34KsWtF53Nvf39p4CF9e3Jq5YR/E14PF +Y7Eba00ntZ95XORFf6tfMbZaRkI0Yw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754965; x=1708290965; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=7jVlFLlhK4ICVGezzTiai5C8Zlo8EPl2MrkiyOokM44=; b=XE1WC2ce4e9PML4/uY9JnyglLzzzUJDXV+ZHO5EgA+IP6/1yt3cs+fDG B/qquk8R49JSWFfiqd4nPBS2yrWayG/AtcsgDnXRb87gbcQ3N2Mgvyhv0 elFZ2ddYxbPMnat+EjukYxnxpiq4bz7PTb/YJghmPj5LuRazFPaGcODCW 7qOIdtMoKFq/H1w1DsDeddcCd7qcr8q5xbUNJ63BkniEk6ucfv9xJJ4Kh xLets9rirtyS3zlrNpl0+OZmiSJCp4d+tTsjgdX8UtfSgGC8MJ0atixBJ 56AA+SR8BX/XpXlvJLcGEeGanO5cEKK/DtKH8ZNbEoBGCuR8boYm1bm8Z w==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427211" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427211" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:01 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241594" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241594" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 06/41] x86/fpu: Add helper for modifying xstate Date: Sat, 18 Feb 2023 13:13:58 -0800 Message-Id: <20230218211433.26859-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: c4poe1dkmt6heo84umh4twtyan4m9kho X-Rspamd-Queue-Id: 6155E4000A X-HE-Tag: 1676754965-577915 X-HE-Meta: U2FsdGVkX18KDSrD2TM/IVCwpS2XgqwCt3o0o9lJU2CQ8k3pW+trVgFZSs+hQi/5ViC26XylCxmpqx2A5a/rybfyCWCaFN9yGFzOT4auEeo2bReBF+ndNlYFMsueH5jUYvh6wOCcCPgydtqnLp9JJTp3LsD/qesdUUjFHk3nxkq7Y1YNE01hMMYNkfO/kUQDTqNpH1j+yrTUkGuU0HjSFStWgiN1BIf3Eiy1NxJuxsEHYPpRYKQ8PGvDMscwdQpi2uO5C0yEHqHxw9+K8/A5kB+9uxkkhH2MUn9JRRrxOIxDXJCoSb+9hY1TFogvgAHPc7H1seA1OIhVGqOtKd21+StgIVpHlnHVsW9kgzuZjEqMqYqEr/Yt2P9/uC7eRtDapGGQSfnoqLtt0eHzC30M0+ZXCPhXp0Y+KDdwh3nxySd8qEivnk8KfysWcGWNghn2DV4Mk43KxiFUbm+Eb4I36BV07z2qjbHiRhTaqeg6aFkCfrjafjhcPCm9v5zrp0A+VTqdF6jj0vmMri3z99OxDidsIuxmLZdnZ8GGFlkxaoRGowNZ9uO0xVDZRlf8ErT/luNr75XOdGGxBkx9IFP6v56r2dBlVC0oJ2vSdWcJdBdlEdD1N6uMqzmrRlRT8oAkVU8pvof7mZVzU9TPDYMcQhwLYX4I2ZZQ02SgfjxQ6naZYjTSY+C70JdF00SoxGht+dQsxc/qaXiNFVHlxkt8sCJi0qx/iFx25qgaARixY2lugf8Av2QDTBstj/0lAm4jXRWei6AjwBgtizfhfHff4Eh2eqiHl+PgUIMBNOC8pBH1NOFsExLPoWrcNdlxkg/89d2BSjZwDnlcc/fsnU7XjHKdP0dZZdzXhYhMlZpDKQJCwcpi/KUKpNbR0Wq5jVzu5fMQA7Af3YQCq1yC24+Fl5hdAi0CvpvvUq8sgF1rsDWc+r04Vi7yrWZY91g1V3QvVKJ0aitWSAHUCPs+RXs J/5Gv3yt fVfZgWONkvBrSfDZdCNpzxwkV5UHPC0NtY1/mRNOo9F7g2/uCSYUTI52xNrp5ECLx40iCaPJDtClMtN1TR/Z32mAjGR5yOpgR1c7wFtMhgwM5PTn8w1rijzcHdJI4Xl+N6YmFyuez9ID478/6V/4O45ATubIfxkk1/08W975Vzehww87/DXLc36SUvCFZDrXWaqvILz3q4YgTmjrTYaNK3vzUaRPJsbQci7TDw6EXevZqI7VpjTi3LBISxb5AdcZrL2K+9EsCjEnmMb6mCPiz6JdKglWJN4ivF3WY 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 Reviewed-by: Kees Cook Suggested-by: Thomas Gleixner Signed-off-by: Rick Edgecombe --- v6: - Drop "but appear to work" (Boris) 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 --- 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 caf33486dc5e..f851558b673f 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. + * 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 Sat Feb 18 21:13:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145646 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 0658AC6FA9D for ; Sat, 18 Feb 2023 21:16:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C16C280005; Sat, 18 Feb 2023 16:16:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 321B6280004; Sat, 18 Feb 2023 16:16:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1749B280005; Sat, 18 Feb 2023 16:16:08 -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 035BB280004 for ; Sat, 18 Feb 2023 16:16:08 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C50371C5F88 for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) X-FDA: 80481670374.05.C62E042 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id D3445180007 for ; Sat, 18 Feb 2023 21:16:05 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SNm3Gp7f; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754966; 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=rkA83GGmNHLrIYmiZJYoU8PC+1rjmj46jPgBfejrGnc=; b=716kMbCGrKVVaYXHAl4P5bcu4ldeFCFgnEctCJmlujFs0142UDJE7t3LL+WaGeoCNVBja+ Rm2ZL8/mt/jet5b+h3bg9A8eMKAwYXz30OCsjkABepqU61hPLM2CfkN03wo/B3mxIxIsCN MQxcnxkbqvHQwmRSNrBN/F6wK4RhqmY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SNm3Gp7f; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754966; a=rsa-sha256; cv=none; b=1Yqtl04v4d84LPx/CQq6fWKmTHtqxrQIVib7sZWI6JEJZrIdJHCo09hOFFJdscpPSCFzyv 66QAKMk88k4eM3m1WstXwrmWtqOb+2xJz+sSowtjiGRKYLi6yMoBXqhOKgt/1ac9QB1F9M u2wCchq2B0+49SngEB6ElRUq5VWcFlQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754966; x=1708290966; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=I2MatxMVauhJIHVRqc02oKKO+hrjMuY4waDrBBWGcyY=; b=SNm3Gp7fULaPBudLEhjazim347iec2mfmTq4X+j34EBtza40xjfYQh3R Lw9vfTqpHqW3TzS29HIGGmKf9K7nK0SqkDfMzPxJI550ojPj5BXrzgyf0 bc2qu3gslFVbUu0LOnDdmTuGcq/9NlML6nCOJzRtSdHW5+RAdo09ozSed SaVGpWgzpKZsAaPHotU8zHSEjMVcY49mVcu5wg3a3GIWoYu+oVylo6a3N 5NFpJFQHrZcb1DJHOUyHpUPBIysZO9rNPvDYXMJ3I1f4je2D2LvpcIoy1 oxwZExXSyQX8ndyoRersyLpa952onQxpCE7LUKngDVvndSheNBxautyii w==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427234" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427234" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:02 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241598" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241598" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 07/41] x86: Move control protection handler to separate file Date: Sat, 18 Feb 2023 13:13:59 -0800 Message-Id: <20230218211433.26859-8-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: D3445180007 X-Stat-Signature: 3bu4ta6miwcwkr7mh95f1f5ocs3ey6mk X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754965-725518 X-HE-Meta: U2FsdGVkX1+g19WytvP1tP01V4HE3eWNKQVit+DYYvEHr5jdtR6L6iC7dUZ/GPUMH5nkptD9Rn4ZcBPi2b5EAt5w3hce151jg8KOXckOvPEdFenrAGs8KxEHer0JMpi+jyFNBU6a3JAscRgVQGteNUjF39B4bd7CSey7bM0NUCtXK1e1k7J1ADIXVwBmLkg7EJXYhqfC9R2V/n2SkP3HazKm9LdXI8fCIh0i5Ry8r+Y2+KXmRr8/DADi+NTQLlmW7pT4Tf7uUFFIMruwza7GaNLx7rIHyZFq11SITJ15FLj2MzvHtGQvmU1pntL30cyFCESKke20jjzl1OoM7iquf41rUm6jVF3CZkFPj1H4TZ+35wNdfvZj4GETYeazGDbsjaqoGeC9trLYER8qIVQdWjsujGmtMkWlgdgOUQtiLvSbhfstkob2BoTEVj5M7VU3OIYrdelpZqpOTJ9Z+NqDZDDnT7ec9cXt/2Ywy8R5fOJOQOYJJ0dUDqQylSykjDoUkcAqwU2rRv68A3a5DryRfhQKb1hAjGsZEK77GeDxD6PSO0ZmOn9Yng8FbORlTbO8ruX8PQvUDO5QDSjd/3QlYDp5X+HX5Zz0z7GAbVxhXsRd92j6F58gfhBNn7ThpHydyMS6mSDqDtK4lcFwrtAqLkTc1cm+qsUXh2byTUmu/xp50a4vAA9w53MqvST9MuctQT9dewcJqPr6lafJNpbEmpJW+QZjRoMvbBjbgfqq4PAccefHrts3Iuee2HohUqv6gmw8SGVrK3xCHd1gFX3wDwIcv7n5inGQT1Ql8TaNXwHHSxPGQYQ3dySpUgb3AaUhI99+QKDbCxckGFlwz/Q9yRJTBsmAwy5kPkwqYJBxkNy3rRI81WBPYq5qt5sUaYLffUkNLGFEy1fWyaE7IMH3K6Bz0VUX6lsIKmoGSef6WA5GNYGXasDq54PKGBsN9fOxdqrSdm9TR3z/3WXuAvd qBQ1T8cJ AIAeGgHl9LluRyPRz8o0IQa1hqPMAkwX97Dci8nGDV7VHOTp9QMvM0eM3PambsbM0RIrAvi8ae9ATbUauE5RJCN3QZtEN5a723XJIv5Mw9xbhCf/gQXZUvE0iaocuJnWHdRbvrdt4O9WAZShYhWG504bKmvYmqO1x+EMdCDR0IUoHdCn7u/zFLs5reel13ONjW7uX2V/xhJjN0MP/+d1X0PeXuC3veOGvLkvXXlgV0K/pbzcuEQS6uq0eUZvG8rwak8KFuzyQUwTEUgE= 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: Today the control protection handler is defined in traps.c and used only for the kernel IBT feature. To reduce ifdeffery, move it to it's own file. In future patches, functionality will be added to make this handler also handle user shadow stack faults. So name the file cet.c. No functional change. Tested-by: Pengfei Xu Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- v6: - Split move to cet.c and shadow stack enhancements to fault handler to separate files. (Kees) --- arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/cet.c | 76 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/traps.c | 75 --------------------------------------- 3 files changed, 78 insertions(+), 75 deletions(-) create mode 100644 arch/x86/kernel/cet.c 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..7ad22b705b64 --- /dev/null +++ b/arch/x86/kernel/cet.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +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); diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index d317dc3d06a3..cc223e60aba2 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -213,81 +213,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 From patchwork Sat Feb 18 21:14:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145647 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 90ED3C64EC4 for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF918280006; Sat, 18 Feb 2023 16:16:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DAB05280004; Sat, 18 Feb 2023 16:16:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC3C3280006; Sat, 18 Feb 2023 16:16:09 -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 9C91E280004 for ; Sat, 18 Feb 2023 16:16:09 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6B605140338 for ; Sat, 18 Feb 2023 21:16:09 +0000 (UTC) X-FDA: 80481670458.20.9CB9969 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 35060A000D for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Xtjq9sxY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754967; 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=ujWF0NLtqX0C/SG+LYbOhJ7ijCm1wRNF99a5IOvejX4=; b=DzbuKSEpSTa2HcSPn/VAwFldDuo0r4r9I2Sx2aCWdQgcXvQLHMME+LTgyeAU1hAnHGAq3I +dy0drcAF5o3+o3+/y25vjNDTfO2N3HtTjP56zxJlADw7eRhpi1KdK2OptLwdshoCbtBmJ +Le1FJYda/V6jNGQGa4IYErh9hYzl5k= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Xtjq9sxY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754967; a=rsa-sha256; cv=none; b=EuEF0wKhmvfpFRu21Dx2z0skpeorpcyFTn6NewRIC8wn/nxrDrpxzMg9B+jJNR1gqwptvi enX/IY61LJlgqcev9vca+1OTaHdSWt4F7rn9vAdrAtNnrAPOA/WadrPnWC7QTJXnJ1kHLo NIS7HLymnSM0KL+oh5Jt2kXXscddnZ4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754967; x=1708290967; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=KoQX/4PD3ISvhAKhthzkea3Z0acPwwrfS0/ghplF4mw=; b=Xtjq9sxYjgCyHrZ+n2WgRWyfKzkrSeS9JGFspFr5LzKwmBmc8foZYqnU vyq+tO3Cp8PmRAHGPMBBP2Jsv3kBSSMc0tJLPNhzm4A/wdjp9AybunIjp nfsPMy5Fuypb+sNSWngTE1+THypnegaEXNhL2ojrLu5YcXffoZhl0uOKD Ynr25AVlhS7FrDNNF3vsthCJxEayKeMs5KsrS15vMXfs815RYS0E9mh0k e0nrijmZvzEXQy9jhij5fdSTvuLhTzBbRscUxmJkUu3RKZy4y8KbnVXDa 657x6z6Udrs27AxgB6KeeCT6QoU4YP/6qsCktAPHhkUjWlmaKuLsvfTDK g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427256" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427256" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:03 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241603" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241603" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:02 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Michael Kerrisk Subject: [PATCH v6 08/41] x86/shstk: Add user control-protection fault handler Date: Sat, 18 Feb 2023 13:14:00 -0800 Message-Id: <20230218211433.26859-9-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 35060A000D X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 9bfgu9uqhbjunkpekcq7zhb8gw4ua687 X-HE-Tag: 1676754967-856475 X-HE-Meta: U2FsdGVkX18jR5WlYi3usq8HQNpGoGOc79Ia+TciLXvLZy8P98hgX/fFhYYFw6Rud+4NjcvgCuZ1D5VrmQx3MOO2TjIrwdE+XQDbtf1Fz4yfyjHvBPhOaLvP3ZRlH+bCQTVV4eiN/dT5ki5yrFSCzg6O8A0/T8Zau69H7vNEbgqdWUsFmClInrrmGTf+bLctBi3YCneasCJHjE2vd2eXU2kHkxahFfT2wmnCiO4aCGbmht5PA7taZUkTzDMx0YuSRk9owe+DgKeAICWzPoPtUmE9Zsnp4vn4IhIYY+1YrFxkZI1GclUVaOeWLdvhODKKG5bYhY9R3IecpCph7wmIbnEmpHX5WhXI3B8zc4NKVnQ4o1CHAvHE/K8Z1su4K9Q7ESYktChvSPThbhSjsxOwDZ9xQbvHcJz3AAmgi2J29budGeC1BmRQNwZBKOkuRKVMVA8t+jAvmhEjORGWYitkNeyqLJ+x0ebMBsqJSJRL1LlOTK22P18b74sa9F6ihsWyZ9qf8MbfvZquGGzD+IEsBaw0dULiLWbkcZwL1K2sgSkTqdh1KXSH/kgiAv1o8tjbuycVXbljCgd62OYRgUZ5vnM/g4H50t0CeK/ZU4DGof8RDurmDWncI83tHj6+DTMKj6RU0CrO18tNKQtM84LfVP38bZioeN+jQoOo4Qv4qKl74jcl6aukwo1AxQ4APT9SJLC3+hbRhCzmrXU3lDYBFFOoHiDpGqios/LW6IQOgBOPiKAaOewFPoJq4XRHnipInDmYoFbzPe+o1pt5RRMdZZ10lNTZLk1HI+wihkvPM87tR9KwBxrpApYSr0FmrbC4a7XFUy7cVfPjup07eyScrnGDWqpBYdRTh9ZiiM/pauvSEppCprz8c17L8aYO2QlSOwweuIJEedU60UVU1Am93n8r0wZTzlQRZ8bNOdDP4MVSolSk3rkXvT4lMgFWtMCX1bFgUZLZ+7s55hxmcqw GKUofkCM RLKO1I56+Iqz2j0Xgny0ijP+BVJuxXbxZlzNL0GHMZdO8Lh2/1XxWLVz3baB1vNQpJG8Y3kuq6TncaTQp3c+OZfCxKFBlXFPdfo64f+Hwgtt6UsvA2E3UFoaka7eID6/ytaAzo1mMDXh/pZg2yhYr+sqiDL3M5R/vqSASq5NLK0XcfeEMy68gh3iY91owpe3qcgH5UfZWhYiF2vz25kqKuASaV60/twH8NWiO+R7fvFwgThVY4kjCK9WQm6MhDmQWhIX6595czIebgbInBDugO8xoxo5zJC2HTFV3fG5lOYkMILd7tshjhUNxIA== 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Cc: Michael Kerrisk --- v6: - Split into separate patches (Kees) - Change to "x86/shstk" in commit log (Boris) 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. --- 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/cet.c | 94 +++++++++++++++++++++--- 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 | 12 --- arch/x86/xen/enlighten_pv.c | 2 +- arch/x86/xen/xen-asm.S | 2 +- include/uapi/asm-generic/siginfo.h | 3 +- 16 files changed, 117 insertions(+), 34 deletions(-) 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/cet.c b/arch/x86/kernel/cet.c index 7ad22b705b64..33d7d119be26 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -4,10 +4,6 @@ #include #include -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, @@ -20,15 +16,80 @@ enum cp_error_code { CP_ENCL = 1 << 15, }; -DEFINE_IDTENTRY_ERRORCODE(exc_control_protection) +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) { - if (!cpu_feature_enabled(X86_FEATURE_IBT)) { - pr_err("Unexpected #CP\n"); - BUG(); + 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"); } - if (WARN_ON_ONCE(user_mode(regs) || (error_code & CP_EC) != CP_ENDBR)) + 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; @@ -74,3 +135,18 @@ static int __init ibt_setup(char *str) } __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 cc223e60aba2..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) 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 Sat Feb 18 21:14:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145649 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 356B3C61DA4 for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E70B6280008; Sat, 18 Feb 2023 16:16:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD0D8280004; Sat, 18 Feb 2023 16:16:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C21F1280008; Sat, 18 Feb 2023 16:16:10 -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 B2483280004 for ; Sat, 18 Feb 2023 16:16:10 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7F846A01E9 for ; Sat, 18 Feb 2023 21:16:10 +0000 (UTC) X-FDA: 80481670500.05.4C23794 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id A1FEC40024 for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=juAsqDcB; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754967; 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=DmmT6dkaMq6VPb+Vq+6I+ElYXeodGHO11MDhoMSj6m8=; b=EeLTl/5L0820zSZgyLg2LfoHnL3To+5QJJe3kSBtMJWKzKoPkW8q7DAGK1B8hEeCL0mznZ NDWJUtpY420fYZj/IF6V4jgQHo/c2tR9F1NMXHsMoIoYoHJBM/tZv2aigpncrCet7mOXyC yAwoHKVplmwWKcXykj6BbNsUg4PKTD8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=juAsqDcB; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754967; a=rsa-sha256; cv=none; b=8I7y1kpQfF+GvPMx3DKHil+mOCotxwyLj914iapAIV8rSJV5qWytta5AdnsU0+uaVNXthR hsmJyOefchA8Ykc0/UBVnXDVSrWZ/M0GnBMramx3YlQtO9V4lxRKCif5qzrHE3kRoPM+/K mTeBUrfIko6gg1iLpO2At7dInSJIZ5A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754967; x=1708290967; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=n3X6PvKLUTwSn8Fp1OjRbulohHVicRUFLge5hfismaI=; b=juAsqDcBCqOcSxIWmwU4RGufRAHqndoYh0gqx+RKvcB9RWwv2EUflzS9 3bTSr2/rG0C0pTTkvSiHdkYjBQonVONfWvLHqY5CZgWk8mF0P4LEtDbAO v6T913tyQ2XpzXlu5kWucNCpPXgCd0HOuX2y6dzSmPm6zRsaqY2tRhqdj VMxukJViKy3Mcpj1YI4h+HLfw7mx0mk9dFj37W3/dEZTwcryRwzQHyIPR 7NbkQQBc5IrZuu31AjGhWXkmHNqm2GTMrxpd+1oyRrhOEJ1qBoH4Nie31 McxfdZaN3c9p9sHNYQV+TRyBjviI0tlXoE9FDIZ3ApRc7nBp5D2sGUN8T g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427278" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427278" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:04 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241611" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241611" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Christoph Hellwig Subject: [PATCH v6 09/41] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Date: Sat, 18 Feb 2023 13:14:01 -0800 Message-Id: <20230218211433.26859-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: bhxfrki77drrs551juhee5nr5nnojpue X-Rspamd-Queue-Id: A1FEC40024 X-HE-Tag: 1676754967-936709 X-HE-Meta: U2FsdGVkX18CTDkjCRysGlOf1DcfLKvMqvOZxUytm3Md0h14NYpS4KB/sLWcp5ENI3X0X6B/iFxqTuVDHsckBnx4gig9rB5TIiHVOT86H2ShJPvBHZpqDPZI86bxrHW3OBTuXqdrkCmVRX2zX2dErGE7YREFvdWWuVd623BfQV2Dnb5PPMq2PTdFEalPd+Pw0V7BnWRqVdnL4OaXhvKBjTWBKho/5SmNT0c4PIHjmzlH/ctxGIHdYiKE/h3LUZgS8UQkTMCGBEtzQ3ugQmIEpvcNfVSD7pKo+JUVKugLNYJIYvFLqtdRglq4WDXxWmKdBpJ+TU4bfbqy8fwDQymarLoxPg88k+DSXc0BhTw6ihR79qx5VxELWo1aijDyFY0a5ctuPDn6qdkIAl9NSwqj/IOBcdRN1/53f9fY5lucj+E3FeJGpQqJh4KHDQo2pAVHzu/76AZF7QPqnMZu5MqOA+X3h0TpElB2G4qDRcR0mUoWHDvwjzjleVMCXcPMUDUHG+IBT62ld8UkfJTIkV6aioMF3Q0w9B0jMR39bP5g3ln4tI3UiTOZ2VF6eDOAOx2fR07/k0eFWKwpOX3gQKJfBZv1AUkj3x6y8rJDDH2ICcnvZCP7Vy4TYdr9xyJ123iHeT7fSnIwTIlfqR+CC3NEkukIDCWIev+tkT49j0nBk3rPKNdv9qEfPNoPAn4X0TrZ3Q0qO8HaOg5z+X6l526l9x8i1/BBeE0t4W2aWYbhD406Td3LobIvO2gURgYyQ4piJHyYh1RQHaXd98VKn21VHKwfVEfVfM2WNbU7KmP+1NB8JbRBG5O87wwT8c6t0i1o0ebGEZplF2wBUe/yogNq48BcWMYZrnJOLMPkxpnTP3qnG4HaeSZybBwxNeDVpj3ukrXs5fvP8SH+MzKtuDDdpl2a7XnMSkR+oA8BeYoWAl0jjSEaYMxnW4/PBOy07twtEusXSIvRfFUDg8a4G57 Y5XN9Obe NRu+z5qWKSCim/Oc89kb/TxGiv3MFyLY3vwtbCLo1y4gyG78fknc3HuUUWs1on/z0GmldJTjXuGfk1Vy6r4OrbMK5H9imISeYHBpGm6pTUDfbU8qKZpSamMtVZQUsNWCoZbP+al54oS3qKlsk+W9TrgNZLmE7/8W94HeDp7zoRTJ8p81venZ5twnzwBONjm0Y7mPPy0p9Cir/j6SYrWfTfWfnOgXzJ5n6IHwX/Oi7pVRojF3/yf2qdLrnOkDRdTWiklPlPVaqVRbdvQVBgvYmrpH/MAzwIwFnOfvdJLPHOl+6ybW/JQbPn8BFoB+NOW5JgWRkkSS0YsdBfbJ/vnkiDTQmN7OFtIQhqp5gc3IBPC7TK45kvA0SHbVcGHdcvYb+Fz8o9Dv2vh+596JRyTYHSEnGYLZ+Dqy5P1lY6Q+6a027B07AapDRjMTIwpopYjfhq/thsJz3W+m2h6CsZB5LZdplpiT7lw9slJl6f/k7evUYF5D3/7wYWNV5HqvjFBqZ+j5WqCTV/1C1aPwZ7HKPsJ74vkk40n89FEuxyebb2duxoX89IsEy1iRLwQ== 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 Reviewed-by: Kees Cook 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 --- v6: - Also remove dirty from newly added set_memory_rox() v5: - Spelling and grammer 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 | 4 ++-- 2 files changed, 5 insertions(+), 5 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..1b5c0dc9f32b 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2073,12 +2073,12 @@ 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) { - pgprot_t clr = __pgprot(_PAGE_RW); + pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY); if (__supported_pte_mask & _PAGE_NX) clr.pgprot |= _PAGE_NX; From patchwork Sat Feb 18 21:14:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145648 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 DE9B2C64ED8 for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60BDE280007; Sat, 18 Feb 2023 16:16:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 56D6E280004; Sat, 18 Feb 2023 16:16:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3BFEE280007; Sat, 18 Feb 2023 16:16:10 -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 20E49280004 for ; Sat, 18 Feb 2023 16:16:10 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E950A160498 for ; Sat, 18 Feb 2023 21:16:09 +0000 (UTC) X-FDA: 80481670458.10.63685D1 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id E52F5180007 for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="byzbw/l0"; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754968; 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=YxW9MJRFAPgDiRmr9UkkwMi91Qa7hNQXFFYWjlAnpjc=; b=vth7OgsDftFI++2NQoqniVqrVahQLS4gGmmAYcvC0kvNzlPdTuyqTHG6uGbYs5tCiLBL0a JjF+Ltl/h5yyx3zBtP+HNoG6L31X4kyn3l2sTaOTQi74yAqWSz0ebSCzT6YpBR/cHkY8eq Y9tLIDhk2BOTeSrKBHCwMGQ/cv+hjFg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="byzbw/l0"; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754968; a=rsa-sha256; cv=none; b=Q9G1gimUdLTcUS+jZCOrlKol8r4695WQscoyfd+9Ds63IkCiTTWV/uLKGp+j7Pedk6GPYV b2Mzf24pAkosDZAV/JyDQ9RLP8ZsFGTPF7B+2H7tKTVv322h1n5aCYReNPIhqWoq/wxnEW rQ3rYTeCzJgxwlxxWp5jHyXuQK4guy4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754968; x=1708290968; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=WNPU9K/uO1/sECVvbc1vvDXvcVrYVW+CWqRcIxYNITs=; b=byzbw/l0nDj40VMR6a0M73aj+1lj0+mmB5zVh1/D/eY3bchUkCEquLrh BxFq25CI14FclWsOEccBzCXra1HNU58UZgF84qam7uoR7/OtfsEiVr7sv 0Xi6jrk4OHrGWWmPtxjHBlCWAy1bcw5d+7bT+2Q8PPpnfVNKB+MmjZAIO ml4VJhNW0hK54G6yhxJCTAlJyBqpovoN85DZjhBg54VQcwfEzElm3eAac ow6x4pPIwxbUDrWlwSiiDI3esYSyWvgsZW1/InJ6BbPoFKjcvPbyZEdvI W2ihys6zxJUUsdEVf9dhsYou4kIAaVwUquWhtWcqAgf9vka4n8FTIb+Hw A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427301" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427301" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:05 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241617" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241617" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 10/41] x86/mm: Move pmd_write(), pud_write() up in the file Date: Sat, 18 Feb 2023 13:14:02 -0800 Message-Id: <20230218211433.26859-11-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: E52F5180007 X-Stat-Signature: e34ob4d9xg1mrpqdxrdkqyrwa18nffuy X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754967-774732 X-HE-Meta: U2FsdGVkX1/SkW3eY06MX7xic2crxOIggrKOTDzAlVkgVC32a8LKLws3jAQUsCh4aVAgOvgioloeISucnVycfGV+tHN6ZOne2lafzPJHkl3xfPI/7aQ+1A7hv4VnTFb2ADcz5d4M9cRZAVuSDEknD5PEPl9JDfyrb9IOujmRsZv5ATOu4L4jFzQ0Hj1wTDYwp7BML9VICoBqDeT8Ql8yZHZ+/+0XQWqwMKI90lAdpzM1nZEaazgAjgVl6twdAN/3FZryWiM2aFthVbe7UHuLcUWJxlnjgh/0z+TSvk86fwh9L0O4fSf4Wd1vbpQF2TFCvwnbfMmPFhOF7cuRHFjC8Yp8Pe1Oc32FOc8oOQa3x55I+jDz42o0BGyQfS+sD4QZ6dD0s5WB/NkIpx1rkIwajPfEwlX5WJrcHiMR5TJ150CddJgpapxNjupccDbaS6XZmoV/QSpJnL+nVKs3G1Ta0BJlm0tW6UcnEFp/LqiL0edPKf992qHYrdz3Wmx+4n+EsZtHMSrz4JZtE5LuSugKS0uMn7awob54+r6fw709dUIzJAtxzfiNZItDSeuooRJRqo0FQXIEaFHxMmkxvAZDbrbA776NGy8nHjmpwnfw3VxHTd4lPCUnj7YBCYy2bxFIm0JIFbx54G/ceQwKwL2Lgf51NR6c1xd2KDdc68LWuBvtPz+wQnyOwxCfH1UN4+V0lnc7p+yAX9ArGAoXilqKzVIbLit2tCoQ/uQPqdZshQBW96LBBwLLKog9lWRzXPM8vM3JWvlKVcBFpn6j70GnnVPA3WzGTi3QqTpnzCvHVGwNeML0bs2P7FL5VyjhHMg3/2q7i9ojJeURMRwu/WJ9yZ+/zHeexUNfbotbQesAM6saq5T4GanvDLEYzvxtK0M17nXYa1J1T0v2zcfwstxd7uBUP8I38f2q2kh1N2HjWg5lSgYSqc87pmymCdIASF8z1hW07UZoOXzwObKY0b8 +ZDlzB8L Yt8Vf6UI1IGuAVTp2M+YAHRX65skQHCD9Q4/siA4gwdh2iJnEzw7EKSVYe3n57K/k6117UJ/ibqfKli8ykLA+rFrA5bNS051Sy740J69Dq0A4ostWfpTDGTaSv9/yXRa0O+SFZZYSwe/t2Z34DNjj2zc+TayEBmWjLOoJS7aIqywSE0bdBK7/c/+iGQXawPBPq+mnLn4H1kJa/Ud0rXaDzNIw0iIRAo1IcAZRNxijzBchWF61zCi9N7rR30Bf1zmE1QisteFkDKpDh/4ze7kvQ7Spw49egp7sMGZccMwTb+d7TriCWka+aKrLam624gc1yj1dmJVmPdA+deI= 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_SAVED_DIRTY, 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 Sat Feb 18 21:14:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145650 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 88F68C636CC for ; Sat, 18 Feb 2023 21:16:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45D7E280009; Sat, 18 Feb 2023 16:16:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E581280004; Sat, 18 Feb 2023 16:16:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 210E4280009; Sat, 18 Feb 2023 16:16: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 1298A280004 for ; Sat, 18 Feb 2023 16:16:12 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D0120A02DF for ; Sat, 18 Feb 2023 21:16:11 +0000 (UTC) X-FDA: 80481670542.25.44FEACD Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id A6592A000D for ; Sat, 18 Feb 2023 21:16:09 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=oCHqOc50; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754970; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nWDBkTSU3J8LACwiki+UNFLjNBB7IM2dgQ2DeDf+8OE=; b=LGVZWjA8s9Yna7tkoBQNJf+jmoMlipXEulNsBEhqz3jMQqEgKTyBFqLb4xNxiZ6RXYutSZ 9xNbVqs/yXHcIzfO0BCdxBZnQqlAmsXOKqlDeSNkE0STSAxJ/+FAdcsEg/iu8IbxHBj1fG sn5oq/KMbquTH9i/fxYpBh24dDS+yaU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=oCHqOc50; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754970; a=rsa-sha256; cv=none; b=h+yCQCt8/a7XWYxImaL470vSuKQNmp1jTJN42ymtFq2CGHzOpghZ9Ygp2lrdKI+hHsjBhh LZsldnLOBqORnFl6jnqp+aAewccnptz081aXLqr7wYVt9h3L6dZ3yhC37vpeAZwxoCKb1v NoDWCoz1tYEoDrDuYAacQtBjhBZfXC0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754969; x=1708290969; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YUfVMdqaIDo5eYQ52v6BzSjW4FAE7LTxtjQ5YsNs8EI=; b=oCHqOc50WlYD1298STFES/CcxE+8pDYYWOhXFlpSGrvgNIrFYThiPagq UQX99F1R34EwNZ2GNJ9lDvFXSkjdRtQzDDkLhLeLtMTrPNaR5ypVo0Mh+ pbftLVtAG770+Y1tuqoBrag8X9feDXj0qYKcqQ5Cfflk+TtBc24gzmXhn SpDH6bcA5vQ9EzCaRHp18uB2bKtQFJRO3jHWOcLmzByphDFI+rmi4mO/Y wBjPfc3Tiz0oGCKkCFM24TAiDe8cAtNVL6TDbCEkQOmf//11dB/1bFJcH 7wuSP1dG5S2+eHh+Ue4qdLncEzxNW4LUASinjNAtwp5vJc/aFwzLTou4R Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427322" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427322" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:05 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241624" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241624" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, linux-arm-kernel@lists.infradead.org, linux-s390@vger.kernel.org, xen-devel@lists.xenproject.org Subject: [PATCH v6 11/41] mm: Introduce pte_mkwrite_kernel() Date: Sat, 18 Feb 2023 13:14:03 -0800 Message-Id: <20230218211433.26859-12-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A6592A000D X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: bajmqmz1pb65ta5wkt3g3whtgeq7af1q X-HE-Tag: 1676754969-899045 X-HE-Meta: U2FsdGVkX19SYnAl/JxTx8+6WAMFfyiUuaOeBEjeDLBFa5dXe26+qicfiQKa6c8lfHlra5JQ5agg0g6v9AN0T8t4GN41E36E8HVgywUc+uuZksqcFLTWtmpYvy8dJJJejkHOkwhibEdvRQg9VzhhoYDae0oxbK0P3/3DhcuV3Ww0achJxT18y2FSLrbOcTFRwSwOcXZswixZ3CVOk34EzdVkK3/I7IHKInAnv33d8D+imyxf0Y0VGYtKLBIO05rmQ9oCfI7w7KaEtE4YoNNbBQcr7XXgHwGYZ2RWTRwQQfEfUoBynrqV/ssgqG+mPCUVvjCype21Sok+BtGv1/l5qOq+jZ+UGqIO9O/34NyuvcXj0pGf1/oJgNcGow88XCi1JL5yn80iJKvls00PlJJFNgL2diojNnPM08og523VyUxoxL0uLzWy80w/FUdwUXky1WmF/tw7hD2kTbRnzdMTupQ2HVj040S6rRoVc67wPfD+bSjM5skTS3xIn/d3GJjXFbWdbdJZ3uo0GIiRqL2M3AmGfPinPKjZZzJyqtSFKoMFb33C/+CmcZ2gubAWuFXjVpAJ95o4dCt+BItlCZ+07ZENmDG6uuDxHWMcZ1lwsclnbP/+9FSbsgfxF1Gkj56zWVBMqHxw9uWK+V4NDXHr65MDMiTfpHXBSiDTbJ7M0JMQCzhYfmC+73EoXr9GS18yeb3sEV0vXfqqzhMO15NkjSdfyYcmKZ3YpKoiz/RER02V+zHqrXobvivpimobypQJYW88zAtkeTCCN2AXeSxDE0q7kCTKepvqasmJLTTC7rRzqyMqzYHFNUcZ+MUgTlBnqvMGc7GANz31VQnajC2NSpD1dbaUCDiv0toLOYWjFhLuglFMqIho/svnVQB4rMr7FwBx17dk7JeOHnsk8gzV03y3b9uVeaDlIg9YhPeuZSXGAjAoz3SIYQxHHbXRCusyJ+gil25At4yscx7fRZt VoA0aiEK tkjQK0bZcN6cqPwc9nsLcne6YaNDPRQytH5SJ3tN7ziROZ+cjf35XlFKdnBJ4SMUO4wiZGfV/K/TYrZv19uMemjrNgAAISzpsWr0H9Ng2hsSCm6YaN4lYpn3bKkZs0hS3g7K1v0fBvD6P89fa+zt0mbheaHLAj1YUH8xtFlF4W4lW6I4jdONlelwotGBKKegOq8fu2ZXulmNn6FuQtAAmFQc7wKweAMDE/WTyA7a47mhjOcSSV3RNGHGpn2eCk+Wwz4Dpgg05VSkBQ8AMnTcmMyj3qKHzKYNL6bvFdSQWlK5n6CKv56ommUKpi45CICdSAKjIKPBzo9hylZPIpkfk7//Wx6CkBpOJelkONRAxr26dCRWhASZ7qbG7VnmuYq4Q+lBbGrZUya4G9TciNA+aTsxfpM7bYBAUUuw4ndbfTb+FeBPTqGLdEpCnc7jAOqODB/2dUkH0GFHT7U0W9uDTvAzYGfNAS21Z/YqdHVJY/4o3mbKx2B89FrXOJkvm0odFdTJiSAT9C13KnaMFl0O34dIosV4zESbUVrzPAG/5Ibq/G9Km0/u92g1Aeg== 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 of these changes is to allow for pte_mkwrite() to create different types of writable memory (the existing conventionally writable type and also the new shadow stack type). Future patches will convert pte_mkwrite() to take a VMA in order to facilitate this, however there are places in the kernel where pte_mkwrite() is called outside of the context of a VMA. These are for kernel memory. So create a new variant called pte_mkwrite_kernel() and switch the kernel users over to it. Have pte_mkwrite() and pte_mkwrite_kernel() be the same for now. Future patches will introduce changes to make pte_mkwrite() take a VMA. Only do this for architectures that need it because they call pte_mkwrite() in arch code without an associated VMA. Since it will only currently be used in arch code, so do not include it in arch_pgtable_helpers.rst. Cc: linux-doc@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-s390@vger.kernel.org Cc: xen-devel@lists.xenproject.org Cc: linux-arch@vger.kernel.org Cc: linux-mm@kvack.org Tested-by: Pengfei Xu Suggested-by: David Hildenbrand Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: David Hildenbrand Acked-by: Deepak Gupta --- Hi Non-x86 Arch’s, x86 has a feature that allows for the creation of a special type of writable memory (shadow stack) that is only writable in limited specific ways. Previously, changes were proposed to core MM code to teach it to decide when to create normally writable memory or the special shadow stack writable memory, but David Hildenbrand suggested[0] to change pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be moved into x86 code. Since pXX_mkwrite() is defined in every arch, it requires some tree-wide changes. So that is why you are seeing some patches out of a big x86 series pop up in your arch mailing list. There is no functional change. After this refactor, the shadow stack series goes on to use the arch helpers to push shadow stack memory details inside arch/x86. Testing was just 0-day build testing. Hopefully that is enough context. Thanks! [0] https://lore.kernel.org/lkml/0e29a2d0-08d8-bcd6-ff26-4bea0e4037b0@redhat.com/#t v6: - New patch --- arch/arm64/include/asm/pgtable.h | 7 ++++++- arch/arm64/mm/trans_pgd.c | 4 ++-- arch/s390/include/asm/pgtable.h | 7 ++++++- arch/s390/mm/pageattr.c | 2 +- arch/x86/include/asm/pgtable.h | 7 ++++++- arch/x86/xen/mmu_pv.c | 2 +- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 65e78999c75d..ed555f947697 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -180,13 +180,18 @@ static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) return pmd; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite_kernel(pte_t pte) { pte = set_pte_bit(pte, __pgprot(PTE_WRITE)); pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); return pte; } +static inline pte_t pte_mkwrite(pte_t pte) +{ + return pte_mkwrite_kernel(pte); +} + static inline pte_t pte_mkclean(pte_t pte) { pte = clear_pte_bit(pte, __pgprot(PTE_DIRTY)); diff --git a/arch/arm64/mm/trans_pgd.c b/arch/arm64/mm/trans_pgd.c index 4ea2eefbc053..5c07e68d80ea 100644 --- a/arch/arm64/mm/trans_pgd.c +++ b/arch/arm64/mm/trans_pgd.c @@ -40,7 +40,7 @@ static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) * read only (code, rodata). Clear the RDONLY bit from * the temporary mappings we use during restore. */ - set_pte(dst_ptep, pte_mkwrite(pte)); + set_pte(dst_ptep, pte_mkwrite_kernel(pte)); } else if (debug_pagealloc_enabled() && !pte_none(pte)) { /* * debug_pagealloc will removed the PTE_VALID bit if @@ -53,7 +53,7 @@ static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) */ BUG_ON(!pfn_valid(pte_pfn(pte))); - set_pte(dst_ptep, pte_mkpresent(pte_mkwrite(pte))); + set_pte(dst_ptep, pte_mkpresent(pte_mkwrite_kernel(pte))); } } diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h index b26cbf1c533c..29522418b5f4 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -991,7 +991,7 @@ static inline pte_t pte_wrprotect(pte_t pte) return set_pte_bit(pte, __pgprot(_PAGE_PROTECT)); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite_kernel(pte_t pte) { pte = set_pte_bit(pte, __pgprot(_PAGE_WRITE)); if (pte_val(pte) & _PAGE_DIRTY) @@ -999,6 +999,11 @@ static inline pte_t pte_mkwrite(pte_t pte) return pte; } +static inline pte_t pte_mkwrite(pte_t pte) +{ + return pte_mkwrite_kernel(pte); +} + static inline pte_t pte_mkclean(pte_t pte) { pte = clear_pte_bit(pte, __pgprot(_PAGE_DIRTY)); diff --git a/arch/s390/mm/pageattr.c b/arch/s390/mm/pageattr.c index 85195c18b2e8..4ee5fe5caa23 100644 --- a/arch/s390/mm/pageattr.c +++ b/arch/s390/mm/pageattr.c @@ -96,7 +96,7 @@ static int walk_pte_level(pmd_t *pmdp, unsigned long addr, unsigned long end, if (flags & SET_MEMORY_RO) new = pte_wrprotect(new); else if (flags & SET_MEMORY_RW) - new = pte_mkwrite(pte_mkdirty(new)); + new = pte_mkwrite_kernel(pte_mkdirty(new)); if (flags & SET_MEMORY_NX) new = set_pte_bit(new, __pgprot(_PAGE_NOEXEC)); else if (flags & SET_MEMORY_X) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index b39f16c0d507..4f9fddcff2b9 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -364,11 +364,16 @@ static inline pte_t pte_mkyoung(pte_t pte) return pte_set_flags(pte, _PAGE_ACCESSED); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite_kernel(pte_t pte) { return pte_set_flags(pte, _PAGE_RW); } +static inline pte_t pte_mkwrite(pte_t pte) +{ + return pte_mkwrite_kernel(pte); +} + static inline pte_t pte_mkhuge(pte_t pte) { return pte_set_flags(pte, _PAGE_PSE); diff --git a/arch/x86/xen/mmu_pv.c b/arch/x86/xen/mmu_pv.c index ee29fb558f2e..a23f04243c19 100644 --- a/arch/x86/xen/mmu_pv.c +++ b/arch/x86/xen/mmu_pv.c @@ -150,7 +150,7 @@ void make_lowmem_page_readwrite(void *vaddr) if (pte == NULL) return; /* vaddr missing */ - ptev = pte_mkwrite(*pte); + ptev = pte_mkwrite_kernel(*pte); if (HYPERVISOR_update_va_mapping(address, ptev, 0)) BUG(); From patchwork Sat Feb 18 21:14:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145651 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 6286EC6379F for ; Sat, 18 Feb 2023 21:16:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC8B128000A; Sat, 18 Feb 2023 16:16:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91589280004; Sat, 18 Feb 2023 16:16:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 768DC28000A; Sat, 18 Feb 2023 16:16:13 -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 5FC37280004 for ; Sat, 18 Feb 2023 16:16:13 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3B60AC04B6 for ; Sat, 18 Feb 2023 21:16:12 +0000 (UTC) X-FDA: 80481670584.21.5DF7597 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 1EA04180012 for ; Sat, 18 Feb 2023 21:16:09 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UI+eSoVW; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754970; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4QlSWC10bkPzU1QfsUF22qHU54f9BAvAV7P+dWDdidM=; b=Uf9rqNreyxkJtmeP2NnEB0sEppptu5uM7XmltwF3W2yS8hAJXWaUYHjiKTpOgRgxa3ehrX W5hQRiOod0JCvS7qfVBpLlujldqhA0e4sZm6aw7y6h3x2IqGSbNN7dGKQ1C0U1O1fYjQFh /kRFOypcJ7RakQcMidrueZ1NuznlW94= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UI+eSoVW; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754970; a=rsa-sha256; cv=none; b=Na0zDwdezugtt87ILXcq3LhNljRmZ8ILEMtzHqG/kpnV7kKvxM7zFKlsCdW4zzSTCHdkNV kls+UFYMfIIsv/PBc7kn+Gc9Roy59WeiHS5O9YSzL3GPNB89Q4A6+r5+8RBsP0OoS8ELMg q7tXO5AOEUUUvPiR7m+UjHUYW0nJnrc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754970; x=1708290970; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qGE9eRR2bKBJP4DHmA8A12HCtfd+hwPFDkFpADpJ44g=; b=UI+eSoVWIWeuc7rKyjnoumf5ObRX1bC+xwb2AVwfWntx+cpqncQzAAjs wfx6C4gqMK2NrzWiV8ackXY2dTKeKTMkefzMT6lbxwca9+wOo1CPsS24Y ikJO2u9JW0a/LmotsR6RHegzxBmGdOVEfYJSKzM3FnVPQV0eoWmvN6jfl 2kEeFqdWlFvHqbfs70zi2S2o2PvUxgSvsZ+Dj13gls1Dr8k8cg95A4VcQ n4sWgT0MoCTTE3bgjn+qfsb3LsbMbagy48aoktnXhOVh7EhV2W0FOgczO lPHlZtEswTYTJNDEfNLuZuDNpfFQ/CFKlJbWaS7oIoga87zP16434Yhf+ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427345" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427345" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:06 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241629" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241629" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:05 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, linux-s390@vger.kernel.org Subject: [PATCH v6 12/41] s390/mm: Introduce pmd_mkwrite_kernel() Date: Sat, 18 Feb 2023 13:14:04 -0800 Message-Id: <20230218211433.26859-13-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1EA04180012 X-Stat-Signature: ajbbwx16sqzks1rxapkeaq5yx4ewom18 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754969-134507 X-HE-Meta: U2FsdGVkX19DT1S2C1ZR1dXku2lwJLOWrIzbhEOyCHGWtj9zGnyAmWanoO+wI7AT5o22msFxQKFxbyszaH4Kkhlt3kmfPfnFmTREJgrmqPs1WNr0v3DTDg2xB5y19XP9KnckZ8S5IooZH5xvGylPLl9nAduWnldXZ71kK0M0Kfd17Mc9rvVhAQ3qpmiJ5tUtPuPimeFmaW1pPaMFSMBYnxwAkEP5s3IgxcJHBffGstJHv/U5qGxAaBi1gREyigxH5MlliIzkfu1INi2kvGn7trywePYi6ftKcpFbxdfLgcmZRf2URFAFKeBlBDvfKOf/ecd2F8OSl4/5ULaXLc/O+0MhpkLS+3Nu6aoR9dDZtRPUtXG+2+eRXQyawnh1RhhD3KYru8WZ6Dp2mOOlQNgdgZ/rvGyAjQmArIRGv+SADHgw0uxAC53sx/c7Wuh1qaTNFi1cxa93pbFwfz3F6qHfnlwTWM3WdwLuP38CGEiaoFwUEK7FiMOMXHrMjcy4IB2ecp5mKwc642LMt9elX6c1YPtA0hBzDvVGDL2D76JJ1PfVBs7bg8g547oyyo+yszp+F1upjXD35bBXyS4KIhjcZbv2JIOOl/EMjHX9C2lERO30U3PxEwutsK3qWqrSVZWNvDBlYwtCnytSmFRWJKu5IsvVT7cz84MmRYF9IaTicUF7Eum885QNHNHptx51hHkJyEBxgkg44vTnfTig1btUxbvVfqo45UM6m/pew/HsLSOm2rVZrckcfM0lfRhT5qQT6TpS1vfJMB3THop0D3fZQ+MBsRz+e3MbPdHp3vxtyHobj0TYRvTLTWmdqDAZBFV1QObgjXu/z+5TqK7nO1FYy6j1x85Wwy09wR4TGqi+dt1XKNn6Py3peI+re5hf4iDMCORf46UQBJvqOTfeC7PchtVz3EwBTWmtvFyQe0vKKf40Jm9Gg+Uc4B8iC+9GRy/XJ5m+lH1Rcmq8l8BZzPl r3bhzwlf c2Ukp2QXzSPBe/aFoHdjF2z+hi1lmxRAabokrS+WEN6GzsmDW7uk/wQW0WnWR/haYrZk/gmkaaCIe6hlCzpO3u8zVALQN71tVcjg4jRwddAnGifDfpdu+5nSqADRW0DxNlzoQeDgoJuVTnmwkWiwn/wMkIK4LBf7gtahSobLHUp31ept7M3RZvV8sglaB9qGP7y7NiyExrMDfWBsjbtvBGwlJo0XD8v0d/+PN7rtpYzyTlyNN9JHqxa2VmWGfwHjhrEhKMkplIYEcZxZQJYyqfiv409OYlZnCBdP4OE9UeZQKoBkjEOfu3/BYbaDjEIRUlV3++fLk3k6mkir6tsrtGkEyqm8GCLGUY/MWBme6chDWm4xf/Zk5fNcmTxVhePZ3A1JquDel7X7XOuDV+QvafqgX6IeQ4HrMpWNhJCxkHR4whRIXQ5jX3u1UQuRgRg5nefgU 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 of these changes is to allow for pmd_mkwrite() to create different types of writable memory (the existing conventionally writable type and also the new shadow stack type). Future patches will convert pmd_mkwrite() to take a VMA in order to facilitate this, however there are places in the kernel where pmd_mkwrite() is called outside of the context of a VMA. These are for kernel memory. So create a new variant called pmd_mkwrite_kernel() and switch the kernel users over to it. Have pmd_mkwrite() and pmd_mkwrite_kernel() be the same for now. Future patches will introduce changes to make pmd_mkwrite() take a VMA. Only do this for architectures that need it because they call pmd_mkwrite() in arch code without an associated VMA. Since it will only currently be used in arch code, so do not include it in arch_pgtable_helpers.rst. Cc: linux-kernel@vger.kernel.org Cc: linux-s390@vger.kernel.org Cc: linux-arch@vger.kernel.org Cc: linux-mm@kvack.org Tested-by: Pengfei Xu Suggested-by: David Hildenbrand Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: David Hildenbrand Acked-by: Heiko Carstens --- Hi Non-x86 Arch’s, x86 has a feature that allows for the creation of a special type of writable memory (shadow stack) that is only writable in limited specific ways. Previously, changes were proposed to core MM code to teach it to decide when to create normally writable memory or the special shadow stack writable memory, but David Hildenbrand suggested[0] to change pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be moved into x86 code. Since pXX_mkwrite() is defined in every arch, it requires some tree-wide changes. So that is why you are seeing some patches out of a big x86 series pop up in your arch mailing list. There is no functional change. After this refactor, the shadow stack series goes on to use the arch helpers to push shadow stack memory details inside arch/x86. Testing was just 0-day build testing. Hopefully that is enough context. Thanks! [0] https://lore.kernel.org/lkml/0e29a2d0-08d8-bcd6-ff26-4bea0e4037b0@redhat.com/#t v6: - New patch --- arch/s390/include/asm/pgtable.h | 7 ++++++- arch/s390/mm/pageattr.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h index 29522418b5f4..c48a447d1432 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -1425,7 +1425,7 @@ static inline pmd_t pmd_wrprotect(pmd_t pmd) return set_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_PROTECT)); } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite_kernel(pmd_t pmd) { pmd = set_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_WRITE)); if (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) @@ -1433,6 +1433,11 @@ static inline pmd_t pmd_mkwrite(pmd_t pmd) return pmd; } +static inline pmd_t pmd_mkwrite(pmd_t pmd) +{ + return pmd_mkwrite_kernel(pmd); +} + static inline pmd_t pmd_mkclean(pmd_t pmd) { pmd = clear_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_DIRTY)); diff --git a/arch/s390/mm/pageattr.c b/arch/s390/mm/pageattr.c index 4ee5fe5caa23..7b6967dfacd0 100644 --- a/arch/s390/mm/pageattr.c +++ b/arch/s390/mm/pageattr.c @@ -146,7 +146,7 @@ static void modify_pmd_page(pmd_t *pmdp, unsigned long addr, if (flags & SET_MEMORY_RO) new = pmd_wrprotect(new); else if (flags & SET_MEMORY_RW) - new = pmd_mkwrite(pmd_mkdirty(new)); + new = pmd_mkwrite_kernel(pmd_mkdirty(new)); if (flags & SET_MEMORY_NX) new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_NOEXEC)); else if (flags & SET_MEMORY_X) From patchwork Sat Feb 18 21:14:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145653 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 5104BC6379F for ; Sat, 18 Feb 2023 21:16:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8093B280004; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DDFF28000B; Sat, 18 Feb 2023 16:16:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3BFFA280004; Sat, 18 Feb 2023 16:16:14 -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 17BDB28000B for ; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E56FA1601CD for ; Sat, 18 Feb 2023 21:16:13 +0000 (UTC) X-FDA: 80481670626.01.55989E8 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id A8CB540002 for ; Sat, 18 Feb 2023 21:16:10 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=R9tru15p; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754971; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vJx+mbBC98p/6zPz4/hW4XjK/dxx6LflqrfkofZ1x+c=; b=wNLfz3V0jPXbboOKj4B/RvvX/0bhilu5vqCPLsySHtsTebrd5LqszsPKPJugopM72Q79Sp RCDy5ZuFzi9MECYBj6LFLK2PbL4dzw1SoWNk4XufCaLI/+uqBcEzyK0b6cZi4PIL7kkmZ+ EO1E8Z2gPJoZ6DPnIvA1MUWZbSHp+tE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=R9tru15p; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754971; a=rsa-sha256; cv=none; b=6sN3g+KiDbrBSjREM3aE0gP9t3ooOUiqyqB+4x0dA3Jhcgs5Im3PmwJjApYBMRhUP2YI/Z tHikUUE9Ij19E90+h7pnklFN1mVSmrNwI5EIYecc1yqqzF0nGGKwMnk0wmzgKzhWcH/UwC LCbYLGN6DdKO9l0dwkMMN9CsTq5vzsc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754970; x=1708290970; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rxj9czymX7gUlnvqos5jD1E4th0P80n6KyIsd1wZY38=; b=R9tru15pOQN3cGfGgcTU3jZPf8jGB1WzM17iRnO80pShzjJ34V6bhEya bkNBqlJ+jxhjqks61On9pMy6dYMD3Kcn1/Qc3q6tEzXYa6xv2tQQhGbfr 7MJFM44j0btTpWM03m9ySXcxLTTMyJc4BfIAtoFC5078IfbTGJ5f/nSCw NwDxYT/Q2czMDWgZbESUD9kQe6oObepuSjh9Vi8u11Vyi6yLkBTuRU+zP Bf9vpPvuGiCatVlc0iRjF2fAzGLydu3b3K4RxUV1KqbGRznfd6cXvRFxA THYk1US0spYAYbWMBhMC5AWxY7ykRfOStnv6h2l+Wwd7oKjrBv7syq03i A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427367" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427367" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:07 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241634" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241634" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, linux-alpha@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-ia64@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, Michal Simek , Dinh Nguyen , linux-mips@vger.kernel.org, openrisc@lists.librecores.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, xen-devel@lists.xenproject.org Subject: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA Date: Sat, 18 Feb 2023 13:14:05 -0800 Message-Id: <20230218211433.26859-14-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 577h7fg7p3z4chtwss7jqtrrwn75q4nw X-Rspamd-Queue-Id: A8CB540002 X-HE-Tag: 1676754970-994981 X-HE-Meta: U2FsdGVkX1/ly91eWYfygCQ9Nj+er7JNFsaX2r916rmvrlaA+Nuw5poaqQM9eLnq/o/xPcTKNDKUEsoJ5mfxGCCqie/xa/Gax2zR3/4q31JzqNPvbB1ja0xnAPPAaA2ysJndk5qR14ib6HAR8sIqvyNumjbELTIf7X/fwWgEfSaGwifzKi+aJPImz+h3tSJNgx4E2bH01egeK6ekOkMxMCfTPhwoyyDdBGmiZK8ZgkMZl2/B8M0v+cbBo6UIOicQLn4jwp5WvxlnerKbVg6ua2SoZDWTzJ1FRrabEyFqBdzsqwHY2lVk3/7G1l6jdADSIhXQ+BlrMCuOQ3v+wtyjJ1msgeSVW7990MlcxGSOY5/1i95/bzZJSXizSxHPcxQzrs8UqF1EAx6DtLsdm3exO6nf31M1PKmiAskxCcsmOsDcpt3QTEyoQfjDDg/ge0YOxoaKnIookubyjxivJHRr2OWDULNgTVOWc8IOXHJgH9GawvDjNQj5HPfgebW9+mJN4nIM3DgpUMabbWWGyfocRRBB5dMiRLuvao3tphV/vMYnmsqQBA+uC7znOC6kQQvOsPgUX0OI2tw7hY5ptqJrMSkqO6/k0aPhiBV+S6G4ZnaEz1NEtT4cXiOyUALhTsvr5oNB+q06buNWW0eLtyAnZZt+4YNHiiBebe9+uFMni6/KtkptBC41No+QM/sTBzmNd4+4dEMmTwmu4OKjwYl5EiGHRg3MheoXIn+uZpo8eON3dD32Xp9sap3jkg1etRkE2hqFB+CGTRWNeA3bZSlC9ABmNEbqD/dMKpcPCnWB+0Y5cBQzIJTid52FJTaSZ8P4hsg38EXCGCmxMac1JQKLHIzf99b/JFYPhEt4Rm2FvkKz/GDbhC/3RPE9rjFbIiJ8YaxwijCPerrhm1R+C2+DSoAupu0ddz0sg7GDALzRW5pbNLWM6gmNBuMro7y+eFZwf0H0nD8wfHXgfDWhU5c VAwx+MXh I2D9BdCxrg4ALoVU6vhAa4Y+OIaf/WD5Xil5/i63VOGy583ljvBAWl9CJPKD4173P1Xhu5rpwGupYd63prqnCSqepO71idPKIJxNpKME+7zlIFFLIM0brbDNfE0TbYqT3XwidSeQzAb1LO2IkgPa9PPL+QqKPf1MTje7GJ50Brrkg9Hou+naUGtbciO2fDiHsQs1yS/UY3bpLFS38qHw0TlXwBDYt/afdvkxJa1bMXP91Gaor6ppY7W8L3QTiKtwDgjVM2xzlHtjFyItfhIfJtKe7II++iTb6xtb10txoJKlV7dFn8l9Ub0aBNz/i7l7urztki/gJFSnVW1NYVRN4NU8PIv/oWFvlLgPcBbOKjNF7ACQLZZqbRhuS1iBARZJLSZESgxGm12XH6hyTZy3KI1jdBAG8mOqCE9tmbKrPJBhSZsxo/9D5ufVvKZI0uB7QJDtw9VetJcB6KaU+57Vt6MYD0BZ7ep34lF41ynprq3pJMMPYP1S5b/KRFqvr8Kha2y90uN610T/pXCyKini+DbBxxU+IAUaiQWvdvBIosF/N0kX7ldKJ0qJuqiBxwcyw7ojMECcJ3hDQQD0Z6rwMmLw3Li7MeSQ8Om1a7BjX1AmE39kgsHE5r6LWoefYjxMv4yZxhdvKHoGAwplkIvqxYMQZFQO+NGoK0ALLGBEWKcuzYBacTS9y4D+OlJR1cU0PrJGW3Gan8KU69VYNXhnLK78OVlx3q9FHMEMAJJoAMO7z8lJMFxNJHp9oP7iZwoHHVdckvDyqdcdQ5W13SuZ9zjfP4nDbmnPLOO7kflCgf2J0aJs= 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 of these unusual properties is that shadow stack memory is writable, but only in limited ways. These limits are applied via a specific PTE bit combination. Nevertheless, the memory is writable, and core mm code will need to apply the writable permissions in the typical paths that call pte_mkwrite(). In addition to VM_WRITE, the shadow stack VMA's will have a flag denoting that they are special shadow stack flavor of writable memory. So make pte_mkwrite() take a VMA, so that the x86 implementation of it can know to create regular writable memory or shadow stack memory. Apply the same changes for pmd_mkwrite() and huge_pte_mkwrite(). No functional change. Cc: linux-doc@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-alpha@vger.kernel.org Cc: linux-snps-arc@lists.infradead.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-csky@vger.kernel.org Cc: linux-hexagon@vger.kernel.org Cc: linux-ia64@vger.kernel.org Cc: loongarch@lists.linux.dev Cc: linux-m68k@lists.linux-m68k.org Cc: Michal Simek Cc: Dinh Nguyen Cc: linux-mips@vger.kernel.org Cc: openrisc@lists.librecores.org Cc: linux-parisc@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-riscv@lists.infradead.org Cc: linux-s390@vger.kernel.org Cc: linux-sh@vger.kernel.org Cc: sparclinux@vger.kernel.org Cc: linux-um@lists.infradead.org Cc: xen-devel@lists.xenproject.org Cc: linux-arch@vger.kernel.org Cc: linux-mm@kvack.org Tested-by: Pengfei Xu Suggested-by: David Hildenbrand Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: Michael Ellerman (powerpc) Acked-by: David Hildenbrand Acked-by: Deepak Gupta --- Hi Non-x86 Arch’s, x86 has a feature that allows for the creation of a special type of writable memory (shadow stack) that is only writable in limited specific ways. Previously, changes were proposed to core MM code to teach it to decide when to create normally writable memory or the special shadow stack writable memory, but David Hildenbrand suggested[0] to change pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be moved into x86 code. Since pXX_mkwrite() is defined in every arch, it requires some tree-wide changes. So that is why you are seeing some patches out of a big x86 series pop up in your arch mailing list. There is no functional change. After this refactor, the shadow stack series goes on to use the arch helpers to push shadow stack memory details inside arch/x86. Testing was just 0-day build testing. Hopefully that is enough context. Thanks! [0] https://lore.kernel.org/lkml/0e29a2d0-08d8-bcd6-ff26-4bea0e4037b0@redhat.com/#t v6: - New patch --- Documentation/mm/arch_pgtable_helpers.rst | 9 ++++++--- arch/alpha/include/asm/pgtable.h | 6 +++++- arch/arc/include/asm/hugepage.h | 2 +- arch/arc/include/asm/pgtable-bits-arcv2.h | 7 ++++++- arch/arm/include/asm/pgtable-3level.h | 7 ++++++- arch/arm/include/asm/pgtable.h | 2 +- arch/arm64/include/asm/pgtable.h | 4 ++-- arch/csky/include/asm/pgtable.h | 2 +- arch/hexagon/include/asm/pgtable.h | 2 +- arch/ia64/include/asm/pgtable.h | 2 +- arch/loongarch/include/asm/pgtable.h | 4 ++-- arch/m68k/include/asm/mcf_pgtable.h | 2 +- arch/m68k/include/asm/motorola_pgtable.h | 6 +++++- arch/m68k/include/asm/sun3_pgtable.h | 6 +++++- arch/microblaze/include/asm/pgtable.h | 2 +- arch/mips/include/asm/pgtable.h | 6 +++--- arch/nios2/include/asm/pgtable.h | 2 +- arch/openrisc/include/asm/pgtable.h | 2 +- arch/parisc/include/asm/pgtable.h | 6 +++++- arch/powerpc/include/asm/book3s/32/pgtable.h | 2 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 ++-- arch/powerpc/include/asm/nohash/32/pgtable.h | 2 +- arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +- arch/powerpc/include/asm/nohash/64/pgtable.h | 2 +- arch/riscv/include/asm/pgtable.h | 6 +++--- arch/s390/include/asm/hugetlb.h | 4 ++-- arch/s390/include/asm/pgtable.h | 4 ++-- arch/sh/include/asm/pgtable_32.h | 10 ++++++++-- arch/sparc/include/asm/pgtable_32.h | 2 +- arch/sparc/include/asm/pgtable_64.h | 6 +++--- arch/um/include/asm/pgtable.h | 2 +- arch/x86/include/asm/pgtable.h | 6 ++++-- arch/xtensa/include/asm/pgtable.h | 2 +- include/asm-generic/hugetlb.h | 4 ++-- include/linux/mm.h | 2 +- mm/debug_vm_pgtable.c | 16 ++++++++-------- mm/huge_memory.c | 6 +++--- mm/hugetlb.c | 4 ++-- mm/memory.c | 4 ++-- mm/migrate_device.c | 2 +- mm/mprotect.c | 2 +- mm/userfaultfd.c | 2 +- 42 files changed, 106 insertions(+), 69 deletions(-) diff --git a/Documentation/mm/arch_pgtable_helpers.rst b/Documentation/mm/arch_pgtable_helpers.rst index fd2a19df884e..f119d16915d4 100644 --- a/Documentation/mm/arch_pgtable_helpers.rst +++ b/Documentation/mm/arch_pgtable_helpers.rst @@ -48,7 +48,8 @@ PTE Page Table Helpers +---------------------------+--------------------------------------------------+ | pte_mkclean | Creates a clean PTE | +---------------------------+--------------------------------------------------+ -| pte_mkwrite | Creates a writable PTE | +| pte_mkwrite | Creates a writable PTE of the type specified by | +| | the VMA. | +---------------------------+--------------------------------------------------+ | pte_wrprotect | Creates a write protected PTE | +---------------------------+--------------------------------------------------+ @@ -120,7 +121,8 @@ PMD Page Table Helpers +---------------------------+--------------------------------------------------+ | pmd_mkclean | Creates a clean PMD | +---------------------------+--------------------------------------------------+ -| pmd_mkwrite | Creates a writable PMD | +| pmd_mkwrite | Creates a writable PMD of the type specified by | +| | the VMA. | +---------------------------+--------------------------------------------------+ | pmd_wrprotect | Creates a write protected PMD | +---------------------------+--------------------------------------------------+ @@ -224,7 +226,8 @@ HugeTLB Page Table Helpers +---------------------------+--------------------------------------------------+ | huge_pte_mkdirty | Creates a dirty HugeTLB | +---------------------------+--------------------------------------------------+ -| huge_pte_mkwrite | Creates a writable HugeTLB | +| huge_pte_mkwrite | Creates a writable HugeTLB of the type specified | +| | by the VMA. | +---------------------------+--------------------------------------------------+ | huge_pte_wrprotect | Creates a write protected HugeTLB | +---------------------------+--------------------------------------------------+ diff --git a/arch/alpha/include/asm/pgtable.h b/arch/alpha/include/asm/pgtable.h index 9e45f6735d5d..39da4baa4f2d 100644 --- a/arch/alpha/include/asm/pgtable.h +++ b/arch/alpha/include/asm/pgtable.h @@ -253,9 +253,13 @@ extern inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; extern inline pte_t pte_wrprotect(pte_t pte) { pte_val(pte) |= _PAGE_FOW; return pte; } extern inline pte_t pte_mkclean(pte_t pte) { pte_val(pte) &= ~(__DIRTY_BITS); return pte; } extern inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~(__ACCESS_BITS); return pte; } -extern inline pte_t pte_mkwrite(pte_t pte) { pte_val(pte) &= ~_PAGE_FOW; return pte; } extern inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= __DIRTY_BITS; return pte; } extern inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= __ACCESS_BITS; return pte; } +extern inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + pte_val(pte) &= ~_PAGE_FOW; + return pte; +} /* * The smp_rmb() in the following functions are required to order the load of diff --git a/arch/arc/include/asm/hugepage.h b/arch/arc/include/asm/hugepage.h index 5001b796fb8d..223a96967188 100644 --- a/arch/arc/include/asm/hugepage.h +++ b/arch/arc/include/asm/hugepage.h @@ -21,7 +21,7 @@ static inline pmd_t pte_pmd(pte_t pte) } #define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd))) -#define pmd_mkwrite(pmd) pte_pmd(pte_mkwrite(pmd_pte(pmd))) +#define pmd_mkwrite(pmd, vma) pte_pmd(pte_mkwrite(pmd_pte(pmd), (vma))) #define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd))) #define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd))) #define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) diff --git a/arch/arc/include/asm/pgtable-bits-arcv2.h b/arch/arc/include/asm/pgtable-bits-arcv2.h index 515e82db519f..cbbbcbd0a780 100644 --- a/arch/arc/include/asm/pgtable-bits-arcv2.h +++ b/arch/arc/include/asm/pgtable-bits-arcv2.h @@ -84,7 +84,6 @@ PTE_BIT_FUNC(mknotpresent, &= ~(_PAGE_PRESENT)); PTE_BIT_FUNC(wrprotect, &= ~(_PAGE_WRITE)); -PTE_BIT_FUNC(mkwrite, |= (_PAGE_WRITE)); PTE_BIT_FUNC(mkclean, &= ~(_PAGE_DIRTY)); PTE_BIT_FUNC(mkdirty, |= (_PAGE_DIRTY)); PTE_BIT_FUNC(mkold, &= ~(_PAGE_ACCESSED)); @@ -92,6 +91,12 @@ PTE_BIT_FUNC(mkyoung, |= (_PAGE_ACCESSED)); PTE_BIT_FUNC(mkspecial, |= (_PAGE_SPECIAL)); PTE_BIT_FUNC(mkhuge, |= (_PAGE_HW_SZ)); +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + pte_val(pte) |= (_PAGE_WRITE); + return pte; +} + static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) { return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot)); diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h index eabe72ff7381..c25024455445 100644 --- a/arch/arm/include/asm/pgtable-3level.h +++ b/arch/arm/include/asm/pgtable-3level.h @@ -199,11 +199,16 @@ static inline pmd_t pmd_##fn(pmd_t pmd) { pmd_val(pmd) op; return pmd; } PMD_BIT_FUNC(wrprotect, |= L_PMD_SECT_RDONLY); PMD_BIT_FUNC(mkold, &= ~PMD_SECT_AF); -PMD_BIT_FUNC(mkwrite, &= ~L_PMD_SECT_RDONLY); PMD_BIT_FUNC(mkdirty, |= L_PMD_SECT_DIRTY); PMD_BIT_FUNC(mkclean, &= ~L_PMD_SECT_DIRTY); PMD_BIT_FUNC(mkyoung, |= PMD_SECT_AF); +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + pmd_val(pmd) |= L_PMD_SECT_RDONLY; + return pmd; +} + #define pmd_mkhuge(pmd) (__pmd(pmd_val(pmd) & ~PMD_TABLE_BIT)) #define pmd_pfn(pmd) (((pmd_val(pmd) & PMD_MASK) & PHYS_MASK) >> PAGE_SHIFT) diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h index f049072b2e85..dcefac6b1edc 100644 --- a/arch/arm/include/asm/pgtable.h +++ b/arch/arm/include/asm/pgtable.h @@ -227,7 +227,7 @@ static inline pte_t pte_wrprotect(pte_t pte) return set_pte_bit(pte, __pgprot(L_PTE_RDONLY)); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return clear_pte_bit(pte, __pgprot(L_PTE_RDONLY)); } diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index ed555f947697..67748142eb8c 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -187,7 +187,7 @@ static inline pte_t pte_mkwrite_kernel(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return pte_mkwrite_kernel(pte); } @@ -489,7 +489,7 @@ static inline int pmd_trans_huge(pmd_t pmd) #define pmd_cont(pmd) pte_cont(pmd_pte(pmd)) #define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd))) #define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd))) -#define pmd_mkwrite(pmd) pte_pmd(pte_mkwrite(pmd_pte(pmd))) +#define pmd_mkwrite(pmd, vma) pte_pmd(pte_mkwrite(pmd_pte(pmd), (vma))) #define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd))) #define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd))) #define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) diff --git a/arch/csky/include/asm/pgtable.h b/arch/csky/include/asm/pgtable.h index 77bc6caff2d2..559bed7c9cd0 100644 --- a/arch/csky/include/asm/pgtable.h +++ b/arch/csky/include/asm/pgtable.h @@ -176,7 +176,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; if (pte_val(pte) & _PAGE_MODIFIED) diff --git a/arch/hexagon/include/asm/pgtable.h b/arch/hexagon/include/asm/pgtable.h index f7048c18b6f9..8a317f41e04f 100644 --- a/arch/hexagon/include/asm/pgtable.h +++ b/arch/hexagon/include/asm/pgtable.h @@ -297,7 +297,7 @@ static inline pte_t pte_wrprotect(pte_t pte) } /* pte_mkwrite - mark page as writable */ -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; return pte; diff --git a/arch/ia64/include/asm/pgtable.h b/arch/ia64/include/asm/pgtable.h index 01517a5e6778..70724658ddee 100644 --- a/arch/ia64/include/asm/pgtable.h +++ b/arch/ia64/include/asm/pgtable.h @@ -265,7 +265,7 @@ ia64_phys_addr_valid (unsigned long addr) * access rights: */ #define pte_wrprotect(pte) (__pte(pte_val(pte) & ~_PAGE_AR_RW)) -#define pte_mkwrite(pte) (__pte(pte_val(pte) | _PAGE_AR_RW)) +#define pte_mkwrite(pte, vma) (__pte(pte_val(pte) | _PAGE_AR_RW)) #define pte_mkold(pte) (__pte(pte_val(pte) & ~_PAGE_A)) #define pte_mkyoung(pte) (__pte(pte_val(pte) | _PAGE_A)) #define pte_mkclean(pte) (__pte(pte_val(pte) & ~_PAGE_D)) diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h index 7a34e900d8c1..8cc4292357aa 100644 --- a/arch/loongarch/include/asm/pgtable.h +++ b/arch/loongarch/include/asm/pgtable.h @@ -360,7 +360,7 @@ static inline pte_t pte_mkdirty(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; if (pte_val(pte) & _PAGE_MODIFIED) @@ -460,7 +460,7 @@ static inline int pmd_write(pmd_t pmd) return !!(pmd_val(pmd) & _PAGE_WRITE); } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { pmd_val(pmd) |= _PAGE_WRITE; if (pmd_val(pmd) & _PAGE_MODIFIED) diff --git a/arch/m68k/include/asm/mcf_pgtable.h b/arch/m68k/include/asm/mcf_pgtable.h index b619b22823f8..ea1531b60645 100644 --- a/arch/m68k/include/asm/mcf_pgtable.h +++ b/arch/m68k/include/asm/mcf_pgtable.h @@ -208,7 +208,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= CF_PAGE_WRITABLE; return pte; diff --git a/arch/m68k/include/asm/motorola_pgtable.h b/arch/m68k/include/asm/motorola_pgtable.h index 7ac3d64c6b33..58daa34902a0 100644 --- a/arch/m68k/include/asm/motorola_pgtable.h +++ b/arch/m68k/include/asm/motorola_pgtable.h @@ -152,7 +152,6 @@ static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; static inline pte_t pte_wrprotect(pte_t pte) { pte_val(pte) |= _PAGE_RONLY; return pte; } static inline pte_t pte_mkclean(pte_t pte) { pte_val(pte) &= ~_PAGE_DIRTY; return pte; } static inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; } -static inline pte_t pte_mkwrite(pte_t pte) { pte_val(pte) &= ~_PAGE_RONLY; return pte; } static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= _PAGE_DIRTY; return pte; } static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= _PAGE_ACCESSED; return pte; } static inline pte_t pte_mknocache(pte_t pte) @@ -165,6 +164,11 @@ static inline pte_t pte_mkcache(pte_t pte) pte_val(pte) = (pte_val(pte) & _CACHEMASK040) | m68k_supervisor_cachemode; return pte; } +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + pte_val(pte) &= ~_PAGE_RONLY; + return pte; +} #define swapper_pg_dir kernel_pg_dir extern pgd_t kernel_pg_dir[128]; diff --git a/arch/m68k/include/asm/sun3_pgtable.h b/arch/m68k/include/asm/sun3_pgtable.h index 90d57e537eb1..89ce30bff5a1 100644 --- a/arch/m68k/include/asm/sun3_pgtable.h +++ b/arch/m68k/include/asm/sun3_pgtable.h @@ -140,10 +140,14 @@ static inline int pte_young(pte_t pte) { return pte_val(pte) & SUN3_PAGE_ACCESS static inline pte_t pte_wrprotect(pte_t pte) { pte_val(pte) &= ~SUN3_PAGE_WRITEABLE; return pte; } static inline pte_t pte_mkclean(pte_t pte) { pte_val(pte) &= ~SUN3_PAGE_MODIFIED; return pte; } static inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~SUN3_PAGE_ACCESSED; return pte; } -static inline pte_t pte_mkwrite(pte_t pte) { pte_val(pte) |= SUN3_PAGE_WRITEABLE; return pte; } static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= SUN3_PAGE_MODIFIED; return pte; } static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= SUN3_PAGE_ACCESSED; return pte; } static inline pte_t pte_mknocache(pte_t pte) { pte_val(pte) |= SUN3_PAGE_NOCACHE; return pte; } +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + pte_val(pte) |= SUN3_PAGE_WRITEABLE; + return pte; +} // use this version when caches work... //static inline pte_t pte_mkcache(pte_t pte) { pte_val(pte) &= SUN3_PAGE_NOCACHE; return pte; } // until then, use: diff --git a/arch/microblaze/include/asm/pgtable.h b/arch/microblaze/include/asm/pgtable.h index 42f5988e998b..7a696c2e326c 100644 --- a/arch/microblaze/include/asm/pgtable.h +++ b/arch/microblaze/include/asm/pgtable.h @@ -263,7 +263,7 @@ static inline pte_t pte_mkread(pte_t pte) \ { pte_val(pte) |= _PAGE_USER; return pte; } static inline pte_t pte_mkexec(pte_t pte) \ { pte_val(pte) |= _PAGE_USER | _PAGE_EXEC; return pte; } -static inline pte_t pte_mkwrite(pte_t pte) \ +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) \ { pte_val(pte) |= _PAGE_RW; return pte; } static inline pte_t pte_mkdirty(pte_t pte) \ { pte_val(pte) |= _PAGE_DIRTY; return pte; } diff --git a/arch/mips/include/asm/pgtable.h b/arch/mips/include/asm/pgtable.h index a68c0b01d8cd..e92232739bb8 100644 --- a/arch/mips/include/asm/pgtable.h +++ b/arch/mips/include/asm/pgtable.h @@ -309,7 +309,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte.pte_low |= _PAGE_WRITE; if (pte.pte_low & _PAGE_MODIFIED) { @@ -364,7 +364,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; if (pte_val(pte) & _PAGE_MODIFIED) @@ -591,7 +591,7 @@ static inline pmd_t pmd_wrprotect(pmd_t pmd) return pmd; } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { pmd_val(pmd) |= _PAGE_WRITE; if (pmd_val(pmd) & _PAGE_MODIFIED) diff --git a/arch/nios2/include/asm/pgtable.h b/arch/nios2/include/asm/pgtable.h index ab793bc517f5..20cbf857099a 100644 --- a/arch/nios2/include/asm/pgtable.h +++ b/arch/nios2/include/asm/pgtable.h @@ -129,7 +129,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; return pte; diff --git a/arch/openrisc/include/asm/pgtable.h b/arch/openrisc/include/asm/pgtable.h index 6477c17b3062..7d1d0a3936eb 100644 --- a/arch/openrisc/include/asm/pgtable.h +++ b/arch/openrisc/include/asm/pgtable.h @@ -247,7 +247,7 @@ static inline pte_t pte_mkold(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITE; return pte; diff --git a/arch/parisc/include/asm/pgtable.h b/arch/parisc/include/asm/pgtable.h index ea357430aafe..9e4344a976a7 100644 --- a/arch/parisc/include/asm/pgtable.h +++ b/arch/parisc/include/asm/pgtable.h @@ -328,8 +328,12 @@ static inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~_PAGE_ACCESSED; retu static inline pte_t pte_wrprotect(pte_t pte) { pte_val(pte) &= ~_PAGE_WRITE; return pte; } static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= _PAGE_DIRTY; return pte; } static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= _PAGE_ACCESSED; return pte; } -static inline pte_t pte_mkwrite(pte_t pte) { pte_val(pte) |= _PAGE_WRITE; return pte; } static inline pte_t pte_mkspecial(pte_t pte) { pte_val(pte) |= _PAGE_SPECIAL; return pte; } +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + pte_val(pte) |= _PAGE_WRITE; + return pte; +} /* * Huge pte definitions. diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h b/arch/powerpc/include/asm/book3s/32/pgtable.h index 75823f39e042..fc4b3a55482b 100644 --- a/arch/powerpc/include/asm/book3s/32/pgtable.h +++ b/arch/powerpc/include/asm/book3s/32/pgtable.h @@ -471,7 +471,7 @@ static inline pte_t pte_mkpte(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) | _PAGE_RW); } diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h index cb4c67bf45d7..4bfa37cbf5d0 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -600,7 +600,7 @@ static inline pte_t pte_mkexec(pte_t pte) return __pte_raw(pte_raw(pte) | cpu_to_be64(_PAGE_EXEC)); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { /* * write implies read, hence set both @@ -1072,7 +1072,7 @@ static inline pte_t *pmdp_ptep(pmd_t *pmd) #define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd))) #define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd))) #define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) -#define pmd_mkwrite(pmd) pte_pmd(pte_mkwrite(pmd_pte(pmd))) +#define pmd_mkwrite(pmd, vma) pte_pmd(pte_mkwrite(pmd_pte(pmd), (vma))) #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY #define pmd_soft_dirty(pmd) pte_soft_dirty(pmd_pte(pmd)) diff --git a/arch/powerpc/include/asm/nohash/32/pgtable.h b/arch/powerpc/include/asm/nohash/32/pgtable.h index 70edad44dff6..20ce92108f6c 100644 --- a/arch/powerpc/include/asm/nohash/32/pgtable.h +++ b/arch/powerpc/include/asm/nohash/32/pgtable.h @@ -171,7 +171,7 @@ void unmap_kernel_page(unsigned long va); do { pte_update(mm, addr, ptep, ~0, 0, 0); } while (0) #ifndef pte_mkwrite -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) | _PAGE_RW); } diff --git a/arch/powerpc/include/asm/nohash/32/pte-8xx.h b/arch/powerpc/include/asm/nohash/32/pte-8xx.h index 1a89ebdc3acc..f32450eb270a 100644 --- a/arch/powerpc/include/asm/nohash/32/pte-8xx.h +++ b/arch/powerpc/include/asm/nohash/32/pte-8xx.h @@ -101,7 +101,7 @@ static inline int pte_write(pte_t pte) #define pte_write pte_write -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) & ~_PAGE_RO); } diff --git a/arch/powerpc/include/asm/nohash/64/pgtable.h b/arch/powerpc/include/asm/nohash/64/pgtable.h index 879e9a6e5a87..526868817df0 100644 --- a/arch/powerpc/include/asm/nohash/64/pgtable.h +++ b/arch/powerpc/include/asm/nohash/64/pgtable.h @@ -85,7 +85,7 @@ #ifndef __ASSEMBLY__ /* pte_clear moved to later in this file */ -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) | _PAGE_RW); } diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 3e01f4f3ab08..7eb7dd558319 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -338,7 +338,7 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) | _PAGE_WRITE); } @@ -624,9 +624,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { - return pte_pmd(pte_mkwrite(pmd_pte(pmd))); + return pte_pmd(pte_mkwrite(pmd_pte(pmd), vma)); } static inline pmd_t pmd_wrprotect(pmd_t pmd) diff --git a/arch/s390/include/asm/hugetlb.h b/arch/s390/include/asm/hugetlb.h index ccdbccfde148..558f7eef9c4d 100644 --- a/arch/s390/include/asm/hugetlb.h +++ b/arch/s390/include/asm/hugetlb.h @@ -102,9 +102,9 @@ static inline int huge_pte_dirty(pte_t pte) return pte_dirty(pte); } -static inline pte_t huge_pte_mkwrite(pte_t pte) +static inline pte_t huge_pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { - return pte_mkwrite(pte); + return pte_mkwrite(pte, vma); } static inline pte_t huge_pte_mkdirty(pte_t pte) diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h index c48a447d1432..edf290429050 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -999,7 +999,7 @@ static inline pte_t pte_mkwrite_kernel(pte_t pte) return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return pte_mkwrite_kernel(pte); } @@ -1433,7 +1433,7 @@ static inline pmd_t pmd_mkwrite_kernel(pmd_t pmd) return pmd; } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { return pmd_mkwrite_kernel(pmd); } diff --git a/arch/sh/include/asm/pgtable_32.h b/arch/sh/include/asm/pgtable_32.h index d0240decacca..3ddf7e13c6aa 100644 --- a/arch/sh/include/asm/pgtable_32.h +++ b/arch/sh/include/asm/pgtable_32.h @@ -351,6 +351,12 @@ static inline void set_pte(pte_t *ptep, pte_t pte) #define PTE_BIT_FUNC(h,fn,op) \ static inline pte_t pte_##fn(pte_t pte) { pte.pte_##h op; return pte; } +#define PTE_BIT_FUNC_VMA(h,fn,op) \ +static inline pte_t pte_##fn(pte_t pte, struct vm_area_struct *vma) \ +{ \ + pte.pte_##h op; \ + return pte; \ +} #ifdef CONFIG_X2TLB /* @@ -359,11 +365,11 @@ static inline pte_t pte_##fn(pte_t pte) { pte.pte_##h op; return pte; } * kernel permissions), we attempt to couple them a bit more sanely here. */ PTE_BIT_FUNC(high, wrprotect, &= ~(_PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE)); -PTE_BIT_FUNC(high, mkwrite, |= _PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE); +PTE_BIT_FUNC_VMA(high, mkwrite, |= _PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE); PTE_BIT_FUNC(high, mkhuge, |= _PAGE_SZHUGE); #else PTE_BIT_FUNC(low, wrprotect, &= ~_PAGE_RW); -PTE_BIT_FUNC(low, mkwrite, |= _PAGE_RW); +PTE_BIT_FUNC_VMA(low, mkwrite, |= _PAGE_RW); PTE_BIT_FUNC(low, mkhuge, |= _PAGE_SZHUGE); #endif diff --git a/arch/sparc/include/asm/pgtable_32.h b/arch/sparc/include/asm/pgtable_32.h index 5acc05b572e6..ba829881c904 100644 --- a/arch/sparc/include/asm/pgtable_32.h +++ b/arch/sparc/include/asm/pgtable_32.h @@ -241,7 +241,7 @@ static inline pte_t pte_mkold(pte_t pte) return __pte(pte_val(pte) & ~SRMMU_REF); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return __pte(pte_val(pte) | SRMMU_WRITE); } diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h index 3bc9736bddb1..71df58562138 100644 --- a/arch/sparc/include/asm/pgtable_64.h +++ b/arch/sparc/include/asm/pgtable_64.h @@ -463,7 +463,7 @@ static inline pte_t pte_mkclean(pte_t pte) return __pte(val); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { unsigned long val = pte_val(pte), mask; @@ -753,11 +753,11 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return __pmd(pte_val(pte)); } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { pte_t pte = __pte(pmd_val(pmd)); - pte = pte_mkwrite(pte); + pte = pte_mkwrite(pte, vma); return __pmd(pte_val(pte)); } diff --git a/arch/um/include/asm/pgtable.h b/arch/um/include/asm/pgtable.h index 4e3052f2671a..0f9554ab6b6f 100644 --- a/arch/um/include/asm/pgtable.h +++ b/arch/um/include/asm/pgtable.h @@ -204,7 +204,7 @@ static inline pte_t pte_mkyoung(pte_t pte) return(pte); } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { if (unlikely(pte_get_bits(pte, _PAGE_RW))) return pte; diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 4f9fddcff2b9..2b423d697490 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -369,7 +369,9 @@ static inline pte_t pte_mkwrite_kernel(pte_t pte) return pte_set_flags(pte, _PAGE_RW); } -static inline pte_t pte_mkwrite(pte_t pte) +struct vm_area_struct; + +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { return pte_mkwrite_kernel(pte); } @@ -470,7 +472,7 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pmd_set_flags(pmd, _PAGE_ACCESSED); } -static inline pmd_t pmd_mkwrite(pmd_t pmd) +static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { return pmd_set_flags(pmd, _PAGE_RW); } diff --git a/arch/xtensa/include/asm/pgtable.h b/arch/xtensa/include/asm/pgtable.h index 5b5484d707b2..13f05b0d38a3 100644 --- a/arch/xtensa/include/asm/pgtable.h +++ b/arch/xtensa/include/asm/pgtable.h @@ -258,7 +258,7 @@ static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= _PAGE_DIRTY; return pte; } static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= _PAGE_ACCESSED; return pte; } -static inline pte_t pte_mkwrite(pte_t pte) +static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { pte_val(pte) |= _PAGE_WRITABLE; return pte; } #define pgprot_noncached(prot) \ diff --git a/include/asm-generic/hugetlb.h b/include/asm-generic/hugetlb.h index a57d667addd2..45e8c4a98cf4 100644 --- a/include/asm-generic/hugetlb.h +++ b/include/asm-generic/hugetlb.h @@ -20,9 +20,9 @@ static inline unsigned long huge_pte_dirty(pte_t pte) return pte_dirty(pte); } -static inline pte_t huge_pte_mkwrite(pte_t pte) +static inline pte_t huge_pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { - return pte_mkwrite(pte); + return pte_mkwrite(pte, vma); } static inline pte_t huge_pte_mkdirty(pte_t pte) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7afc86d50442..4650e2580d60 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1102,7 +1102,7 @@ void free_compound_page(struct page *page); 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); + pte = pte_mkwrite(pte, vma); return pte; } diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c index c631ade3f1d2..ee7d6fc36128 100644 --- a/mm/debug_vm_pgtable.c +++ b/mm/debug_vm_pgtable.c @@ -107,10 +107,10 @@ static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) WARN_ON(!pte_same(pte, pte)); WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); - WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); + WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte), args->vma))); WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); - WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); + WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma)))); WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); } @@ -151,7 +151,7 @@ static void __init pte_advanced_tests(struct pgtable_debug_args *args) pte = pte_mkclean(pte); set_pte_at(args->mm, args->vaddr, args->ptep, pte); flush_dcache_page(page); - pte = pte_mkwrite(pte); + pte = pte_mkwrite(pte, args->vma); pte = pte_mkdirty(pte); ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1); pte = ptep_get(args->ptep); @@ -197,10 +197,10 @@ static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) WARN_ON(!pmd_same(pmd, pmd)); WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); - WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); + WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd), args->vma))); WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); - WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); + WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd, args->vma)))); WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd)))); WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd)))); /* @@ -251,7 +251,7 @@ static void __init pmd_advanced_tests(struct pgtable_debug_args *args) pmd = pmd_mkclean(pmd); set_pmd_at(args->mm, vaddr, args->pmdp, pmd); flush_dcache_page(page); - pmd = pmd_mkwrite(pmd); + pmd = pmd_mkwrite(pmd, args->vma); pmd = pmd_mkdirty(pmd); pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1); pmd = READ_ONCE(*args->pmdp); @@ -903,8 +903,8 @@ static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) pte = mk_huge_pte(page, args->page_prot); WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); - WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); - WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); + WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte), args->vma))); + WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte, args->vma)))); #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot); diff --git a/mm/huge_memory.c b/mm/huge_memory.c index abe6cfd92ffa..a216129e6a7c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -554,7 +554,7 @@ __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); + pmd = pmd_mkwrite(pmd, vma); return pmd; } @@ -1587,7 +1587,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) pmd = pmd_modify(oldpmd, vma->vm_page_prot); pmd = pmd_mkyoung(pmd); if (writable) - pmd = pmd_mkwrite(pmd); + pmd = pmd_mkwrite(pmd, vma); set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd); update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); spin_unlock(vmf->ptl); @@ -1935,7 +1935,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, /* See change_pte_range(). */ if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) && can_change_pmd_writable(vma, addr, entry)) - entry = pmd_mkwrite(entry); + entry = pmd_mkwrite(entry, vma); ret = HPAGE_PMD_NR; set_pmd_at(mm, addr, pmd, entry); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bdbfeb6fb393..0f5cf9a3cdb7 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4898,7 +4898,7 @@ static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, if (writable) { entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page, - vma->vm_page_prot))); + vma->vm_page_prot)), vma); } else { entry = huge_pte_wrprotect(mk_huge_pte(page, vma->vm_page_prot)); @@ -4914,7 +4914,7 @@ static void set_huge_ptep_writable(struct vm_area_struct *vma, { pte_t entry; - entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep))); + entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)), vma); if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) update_mmu_cache(vma, address, ptep); } diff --git a/mm/memory.c b/mm/memory.c index 3e836fecd035..6ad031d5cfb0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4089,7 +4089,7 @@ 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) - entry = pte_mkwrite(pte_mkdirty(entry)); + entry = pte_mkwrite(pte_mkdirty(entry), vma); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); @@ -4777,7 +4777,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_modify(old_pte, vma->vm_page_prot); pte = pte_mkyoung(pte); if (writable) - pte = pte_mkwrite(pte); + pte = pte_mkwrite(pte, vma); ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 721b2365dbca..0d2c97f6d561 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -646,7 +646,7 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, } entry = mk_pte(page, vma->vm_page_prot); if (vma->vm_flags & VM_WRITE) - entry = pte_mkwrite(pte_mkdirty(entry)); + entry = pte_mkwrite(pte_mkdirty(entry), vma); } ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); diff --git a/mm/mprotect.c b/mm/mprotect.c index 61cf60015a8b..2a70d16ec556 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -200,7 +200,7 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pte_write(ptent) && can_change_pte_writable(vma, addr, ptent)) - ptent = pte_mkwrite(ptent); + ptent = pte_mkwrite(ptent, vma); ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent); if (pte_needs_flush(oldpte, ptent)) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 0499907b6f1a..c3bcc6fcf27b 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -85,7 +85,7 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, } if (writable) - _dst_pte = pte_mkwrite(_dst_pte); + _dst_pte = pte_mkwrite(_dst_pte, dst_vma); else /* * We need this to make sure write bit removed; as mk_pte() From patchwork Sat Feb 18 21:14:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145652 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 CC0EBC636CC for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54A9528000C; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A9EB28000B; Sat, 18 Feb 2023 16:16:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AD8B28000C; Sat, 18 Feb 2023 16:16:14 -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 13AED280004 for ; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D59291A03AC for ; Sat, 18 Feb 2023 21:16:13 +0000 (UTC) X-FDA: 80481670626.02.5AA834D Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id F021AA0006 for ; Sat, 18 Feb 2023 21:16:11 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SkZbYE1r; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754972; 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=sD6wa/GcnZfGOz/UJDkgNM+mvb/TjXmdVSD23fgaRkM=; b=8YYOYFHG93tga8Dz3XWRmFYt8ZK6+RvS3xUkvXxnLwsv4sxnJ5b9cp5iNXQEnCfAQQDpGl Qg1eLLb7VuTE7tusUV2WyBsxeNWp6GF9gIr9QoYezwBjEltQt7S6LcX5K0iM1beHMnMtRX yxyxusWfhGEcXRaRp/W6M/vLrgiQ8K4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SkZbYE1r; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754972; a=rsa-sha256; cv=none; b=ZqfLX9nWrmK/tq5GnpVpr7s10SPqcAGMKaX/Hkn+87dcBVCpHnYEVAJGSYLNLuUu7hPoBe hAoGPcRRDCWxZlL8kvuCmOK/jECaSBVSqdDJI0SEuJPTLMO1TBYBOkQduw4+12tNHY+0NS qB+Uj1inrWFis/V1DOHUQxESUFOrfZs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754972; x=1708290972; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=s+JIElE36GvkH9//5+z8uWdNPHcLIVQVGfIRMsbLR5I=; b=SkZbYE1rCNw7B28q9HO2/Nx77cYSL1wn+Sf56AJN3iPJ33ezir0MJ0vH jKdRA7xJRX1MuTi05k8Pmm2zP1yffO/fP1a4fwCr08qGUdA1+Lcjb+4yo 25lE71SqHGFBY0rVLEuOWFrAFExrxuIYqNgssBGhsebv1i45s4wRO5xJW 4zsoYUhYiin7sfStMebEjFz5usTEYBL6blrulSW/N5eifj8hqnxWWyQjo qXtqldWRIOf5FVxAzCkynrFBhzbpK+40b6ggD7jtusQTGB6iEHmAz66SE oxxWtXwsToRdshgeG9NfdYgxmzD3EcChrv0yq59+wzz36aLKCfXFjPnLc g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427390" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427390" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:08 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241639" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241639" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:07 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 14/41] x86/mm: Introduce _PAGE_SAVED_DIRTY Date: Sat, 18 Feb 2023 13:14:06 -0800 Message-Id: <20230218211433.26859-15-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: F021AA0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: mj8kpwjbdw3g7wf5n4w3tewuk1hu5rwf X-HE-Tag: 1676754971-691653 X-HE-Meta: U2FsdGVkX19SzKdVgKK4uctYKdmS1VKCqCXgLDe8+9u+AP9Art1HlqyzRZ5D81Rx0IFiycRQntBM8lvFCZw90sCegGHm07e6zhI15MhfdNqCbxWdWev0jGUeKK2T8ngF4O4ygrZZyd5fYb9UVrmQ4nV0sgfxhTr2qyzgse7+OG3/7AZU4oKYMcRyR0pwYGJZVx20HoL/EMn7UviFAj/PXo+Mos4fUT9gWDcjzj+nDpmWJBdW4BqGWXoU/yRUajP4PhjCH+m51W3RztfrMdAiSQiVUQ1EP4AoBnH66c5NzIMQXLvJAtJqzD1wpcD9TwbAIgrk7EjOdyXePkgwDPI5COJ6wRr+Rp2H4+dTfALDIifur5bFoWxzz+goCmYld5/X1TDgjVuCJD6UYzfoAA9jCCeki1RUXA8ra30bpzxbWSvGNItE7S6WeSha03W/ztd/3qduVv3t/HGUX2JNTe4XA/kJQZyVp0xS03IschDoFJTYxpOx2E37lQv8sPkPYhvToLCnMvnhZ/m/gKhICgo8/vxTXLAI1b62zj0/Xhbz9aV8tFmxcpqGCXlcrA/K6P/+SawQt1x9TGpZ/SjVJwL3WEwrVN4IzeWN6DptZA7R0p9DRctCyqk9R37YCwrAIGGaNCvD5uD+nAijwF/uU+0C6Nq4GxYu+DPjp6KclV4pOcAWpnEzkqupb05XddUmY7/CbIxUl+aWDmRUiB4PSF6G4mfyfqS943GuLGflmA8XzNcCjCt2qA9ZX53na/I0/8huf0yMvURDbqtit51M/1B6+ciJ33AwPljT/7or9dYQFcXrFFSugp760SlxRmMkLEML3KxZSPTvbNk7Z4yjslVgirMk8R7T93o0hBot9etBWeDwNoJmJ97GTQ8DvrOZCUzsAYeRAivrkaKTFQQTwX3aoeW3JVe0NOzNKEfj91AZ7eZ1q/SHRiyBE2bcTOpfsfxNjhm4GhVHuv89dQOmCBZ SipoVXrc FxzobeswTdZ/ERJqvLpkgsOuE1/jV1RvVnb4016j6xiT/eHegeo9i3qwH2ZTjM0f4NoTaXyGJ1x7cbk09s5TDeRUQXbIpbj6em90zkXVHLOv4CNTTeQqfew7lwBaF4LZvKgx8DO2I1xj6fgWGl6KDSF6JgysMCctUauwPSLaezFGxX1f04n95hAb+RcBS41VgbJ+nRMQWSdmSr6Gqf++umXJOb4IaMjW4aXnKYoN+5U2NmobtuPWxo0/SwUDTJHyGEK8TvTDJ8JwzsyEyXyHv4fFcfgdUGDqbMDtkDB1erapbZqJFx5jEio1rmyKxay3cdmIz2eHbLiMZd9k= 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_SAVED_DIRTY in place of the hardware _PAGE_DIRTY. In other words, whenever Linux needs to create Write=0,Dirty=1, it instead creates Write=0,SavedDirty=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,SavedDirty=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 SavedDirty 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 SavedDirty=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,SavedDirty=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_SAVED_DIRTY. No space is consumed in 32-bit kernels because shadow stacks are not enabled there. Implement only the infrastructure for _PAGE_SAVED_DIRTY. Changes to start creating _PAGE_SAVED_DIRTY PTEs will follow once other pieces are in place. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe --- v6: - Rename _PAGE_COW to _PAGE_SAVED_DIRTY (David Hildenbrand) - Add _PAGE_SAVED_DIRTY to _PAGE_CHG_MASK 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) --- arch/x86/include/asm/pgtable.h | 79 ++++++++++++++++++++++++++++ arch/x86/include/asm/pgtable_types.h | 65 ++++++++++++++++++++--- arch/x86/include/asm/tlbflush.h | 3 +- 3 files changed, 138 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 2b423d697490..110e552eb602 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -301,6 +301,45 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear) return native_make_pte(v & ~clear); } +/* + * COW and other write protection operations can result in Dirty=1,Write=0 + * PTEs. But in the case of X86_FEATURE_USER_SHSTK, the software SavedDirty 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 dirty, write-protected memory, a + * software bit is used _PAGE_BIT_SAVED_DIRTY. The following functions + * pte_mksaveddirty() and pte_clear_saveddirty() take a conventional dirty, + * write-protected PTE (Write=0,Dirty=1) and transition it to the shadow stack + * compatible version. (Write=0,SavedDirty=1). + */ +static inline pte_t pte_mksaveddirty(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_SAVED_DIRTY); +} + +static inline pte_t pte_clear_saveddirty(pte_t pte) +{ + /* + * _PAGE_SAVED_DIRTY is unnecessary on !X86_FEATURE_USER_SHSTK kernels, + * since the HW dirty bit can be used without creating shadow stack + * memory. See the _PAGE_SAVED_DIRTY 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_SAVED_DIRTY); +} + #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP static inline int pte_uffd_wp(pte_t pte) { @@ -420,6 +459,26 @@ static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear) return native_make_pmd(v & ~clear); } +/* See comments above pte_mksaveddirty() */ +static inline pmd_t pmd_mksaveddirty(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_SAVED_DIRTY); +} + +/* See comments above pte_mksaveddirty() */ +static inline pmd_t pmd_clear_saveddirty(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_SAVED_DIRTY); +} + #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP static inline int pmd_uffd_wp(pmd_t pmd) { @@ -491,6 +550,26 @@ static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear) return native_make_pud(v & ~clear); } +/* See comments above pte_mksaveddirty() */ +static inline pud_t pud_mksaveddirty(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_SAVED_DIRTY); +} + +/* See comments above pte_mksaveddirty() */ +static inline pud_t pud_clear_saveddirty(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_SAVED_DIRTY); +} + 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..3b420b6c0584 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 Saved Dirty bit page. + */ +#ifdef CONFIG_X86_USER_SHADOW_STACK +#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW5 /* copy-on-write */ +#else +#define _PAGE_BIT_SAVED_DIRTY 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_SAVED_DIRTY is a software-only bit used to separate copy-on-write + * PTEs from shadow stack PTEs: + * + * (Write=0,SavedDirty=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 SavedDirty=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,SavedDirty=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_SAVED_DIRTY (_AT(pteval_t, 1) << _PAGE_BIT_SAVED_DIRTY) +#else +#define _PAGE_SAVED_DIRTY (_AT(pteval_t, 0)) +#endif + +#define _PAGE_DIRTY_BITS (_PAGE_DIRTY | _PAGE_SAVED_DIRTY) + #define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE) /* @@ -125,9 +169,9 @@ * instance, and is *not* included in this mask since * pte_modify() does modify it. */ -#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \ - _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY | \ - _PAGE_SOFT_DIRTY | _PAGE_DEVMAP | _PAGE_ENC | \ +#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \ + _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY_BITS | \ + _PAGE_SOFT_DIRTY | _PAGE_DEVMAP | _PAGE_ENC | \ _PAGE_UFFD_WP) #define _HPAGE_CHG_MASK (_PAGE_CHG_MASK | _PAGE_PSE) @@ -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..6c5ef14060a8 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_SAVED_DIRTY; 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 Sat Feb 18 21:14:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145654 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 71C87C64EC4 for ; Sat, 18 Feb 2023 21:16:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C249228000B; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BFDC928000D; Sat, 18 Feb 2023 16:16:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A33A328000B; Sat, 18 Feb 2023 16:16: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 91F4B28000D for ; Sat, 18 Feb 2023 16:16:14 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 65225A022D for ; Sat, 18 Feb 2023 21:16:14 +0000 (UTC) X-FDA: 80481670668.26.72A18A0 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 7A79A180013 for ; Sat, 18 Feb 2023 21:16:12 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=APyaY+hv; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754972; 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=SpY8RNKvPEU8/scbKuOf3L3LO6d2Aql0f3gk0afkH+Q=; b=YYLxlUwPUL/LqgazsZRki87h+5VEYuaZhVWR5DIrRNA6I6cgKy+8xx3n7tYtQ48mRivklu Y2veUk3Sx4etQCrwEpYxKKYIB9YBSt9715TAZZYVX+v5ejueu1ZvoIeqr6eTUGmJX/zL96 5DMa2W+jg9l4IRWWKxr+9afaOxMH3iU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=APyaY+hv; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754972; a=rsa-sha256; cv=none; b=5di4hhAd4Rend6FE/7+Gq+LWOJJ3hYnpFDjAWN2rbIXhvjlSz3Qy4UO+iPQCHMkyTkhoZw mxHP75XBDkGdSbwLf8zWN0yAHq5c/j8Vh3wqThV1aJdh8mz+u5H64TQ92NT2BbtygIeeIo sw4Nd4nTdGamMJXn/cSvC8Z8B8jA0O4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754972; x=1708290972; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=64CpYc54+5BlAgu2DYKyZjSWnQK6dNYfq1G22ifMybk=; b=APyaY+hvX+mhXzXiGMIQ3/ebF/aOER7PGlWhPRGns8dmWUJNxq+fdw2+ 3Fo5DtwsQLA0GPDVAeVX28h7C2RWDfGFj9T2Rn+kXABc9DsJQYQ7GNGCO imLCl7zVYl1YU1KzALrddsulxBjL1qeu9gV0ulqWENR4d/kyntcMRUlcm INY6czPAFv3RG9D2BVV/AQ23PTqUmpj6qRVpsMqMit1o5UNebu5K9xZEQ ISrjCUE9V8zQddVdmhuvhbC4em3MkrebW9t/toWn10AAIjqdu50un7ECF HNTOkbx8HrOu+qnfzq39vBBVtzaWGIqpxdl+yh9UIOJ6oWrBPNreBohJA g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427413" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427413" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:09 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241643" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241643" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 15/41] x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY Date: Sat, 18 Feb 2023 13:14:07 -0800 Message-Id: <20230218211433.26859-16-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 7A79A180013 X-Stat-Signature: nwbyj5a3ytky8ptxc7k7sd8t9f9rmfco X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754972-562217 X-HE-Meta: U2FsdGVkX1+zjeAs00qFnXQUCfxbL+NqYqKt/MSUMbk17SZ7VXvVjqA1mIrTjR31kbwMCk8YxSSUcgWlwzgW7zaDRiiW2jqO+6g87Cja4rr+5+XJebEJQ+QhFllwdY6oNICZGpmq0RaXgUz8J4kM6I+7a2WK9N1DFUMvdq4P2ZKC9lqFRbgRxswlZbiyUHgd+6PfBp3hfKMRD7hCpd0MB3PaVX7Bm8cdwnLr9fNfmdBtaNMJNyNqCXbJvmnf6tRcPoCWhHwpMX2BjNvQUnCpBqi0x4sMEj4B49Xnj0CL/i933S390gq8efNGjSmCjxguMlk1Si3LoZYncUbfm0C1M4QHyGfXxKI0mk/ivx18CfcMozXTiKLHb19n1CMgutKcMfjWozt1RX1oDvIITaKsL2/itTIWsWImgJJ8Pr/vDJIWXj7jvgGAHM/Ii7byVd5RpULal5BqDRVmlpo5qQYQFBkEEYZTcBUkqia5HaaV7jhYK9uHV305ipZCKTnBSKCvT6UtMgka4qP3ct5t8tTDx9iNUAotQiJ35Pe+rlxxPsR2nbkhsLDUAkkf10kQNqRlTU6upGU8vYSE669LEUlaNQM5ZFwOn7uuOR2eAep41Q5JsGHigVu2CnwTYhUYiPZsqmyyi2McFRPXnb95ZcZ84L68UjU5uqrIB8oKmwYTuwrNeLJ+dQ8uaa2vTQgjs9sZKfvgusJV079yOfy6fWMAKTswlmxrGGHGwiZ79bzcArUdQ26K4TmBLY4+mFs/PVW2DusEtknUxfv8Lsomwsfadv8N76vV8VoI+r3dnr3DLV8vYQH22jSzy3qLr4t4yg7m+i5Ta+nwJfB5EYJZsLU9sL6Rv/1cHA0l0+hwQXO5gqXmPOIWJecRVk9DqaMvvLppIXnQLaVglXj9Eb65aR+SGbpfgTPaZSu09IWjhItWeGgeuxaYqsMt06TiMK+M/aNMGHR4HQVLTgn2X3v77+A hbsu3HqD nJVszC4LyNdtAGdwzOxiJNbD2+83wmF0nCo2zuyz0XeguvVKIg4ojmZqdpqv1oNChRbi/dZHFgF5cGcKHWhkoV7qKhmWMzOG/4UHtuSrsxvG8qXBT45FdCgg2zIqi5G3SOw2OZz2ylwI+8ApiakweVr0J/uEZjgQciLES5zyVBmGUtedHGcV9xCmnwhnoIOiljNos/amZYuFd/CQz7pwUDKAhA2bytIqFyBI5VzHYjEOBuxXYh9ivmrGq+RE/Yg2ChgnKLzYeqxMrx7FdPKY89LUIS5R1eTk4laOIOBIltN7zUYwEH1fi0Rx0BlMKtKVSDwLEprECn0xa+/I= 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,SavedDirty=1. When a PTE goes from Write=1,Dirty=1 to Write=0,SavedDirty=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_SAVED_DIRTY 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe --- v6: - Fix comment and log to update for _PAGE_COW being replaced with _PAGE_SAVED_DIRTY. 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) --- arch/x86/include/asm/pgtable.h | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 110e552eb602..e5f00c077039 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1192,6 +1192,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); } @@ -1244,6 +1261,24 @@ 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 + /* + * 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)) { + 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 Sat Feb 18 21:14:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145656 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 28E24C636CC for ; Sat, 18 Feb 2023 21:16:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B898328000E; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B39C928000F; Sat, 18 Feb 2023 16:16:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 881D628000E; Sat, 18 Feb 2023 16:16:17 -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 72E4428000D for ; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3F6961601CD for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) X-FDA: 80481670794.08.19902DE Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 2112DA0006 for ; Sat, 18 Feb 2023 21:16:13 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RmT2JBFK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754975; 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=UwlF8pj2HbCR2lOHoYAg5ltmgGgpDeMatG0biscucn8=; b=PE36Z/T7833dFjIrAfl6xepLVn+rlsHU7HOiQXBE+D9fZ2znFZxuCml95L125xM5byAB+z B73nUY/Y1DKAs/emzwGS1GcxWu0mFVOitj/4osde1DUt8qt+FNoQg0eIpbYrRr8iDTkrHj ZsF4SOXxJJRUc10imO0uMwscGyMCnis= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RmT2JBFK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754975; a=rsa-sha256; cv=none; b=APf9AMofIcll+1vLe/59qXD05+yk0e1PrVLIRZT4QiwkZjzaKOcNHZy5ouxjIHbPECLcDS GQrA5NpEphDSq7hcx57y8E12LSPkooZjnHQz87/eJfIBR5r+oeBpl8W6D+tCD+1lhyThvu i2eKYtl3sdY13N1Bm5PIygKzX5R6sUs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754975; x=1708290975; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=jwVGyMm+E1e39ZdaEUcwexkNuji+vNPhFwZ9/yjeQdc=; b=RmT2JBFKTJ0Ysh8S6/aabu3LaYsTihY+1yLv54Hgbrtu8X1HjADksffn MKrCjltXlYbjPBfMruvkxKaAC8xtnHFjnqQeAWW+5DuCE3GccWzA2mzBh P3IhjVO8Bpfm3wTqCVxnmN6dtQkPfGmI3vyezp7NAi6r7k9W/vV0/TELJ +sMQiXwn0t7/YhiokyTPIolzqQqBfEYHlevJeBcCHlcBWWWci7ejypF3f dJXkwRrRnV67WG+rkiBPZ06dAwo5i6xYr/xBXc/AUJ11oAOATQGKbEqCf vwHs6joALdFDqUB0AFRUwxAaBXQ5DSGjqyKKNutI2g7n8cL+O6PDeac+O g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427440" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427440" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:10 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241648" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241648" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 16/41] x86/mm: Start actually marking _PAGE_SAVED_DIRTY Date: Sat, 18 Feb 2023 13:14:08 -0800 Message-Id: <20230218211433.26859-17-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 2112DA0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 7byzyb4t85gczwg31hb5876dtrjwz7q4 X-HE-Tag: 1676754973-785080 X-HE-Meta: U2FsdGVkX19c+a1NBIUlgMu7idr6T+6JLyFdzRQlcLHKC8Eei9N02s1Sf4T/Zk90VVhXPcLu25URgC4+8bNus4diijHx5sxA+wmPeOsgZ11bKxT3eX8l+BNyv/UtzSfBY69gaYqVrP7jqSo0b9KedqVGpK9XcDc4bfZf4GrioGCbMKXJWHUwcOEdJxdKRNP8cXr6L5KgYeWSCdtqY9T6FGsI6aMWPwoaM4PVEyofeE9gJ4oq6X3vnKA/t47ypfHqdX34iqTf8nGuW3C7UaC3Rdc6uu/6kGsCMLXxyJAYAnekLM9D7BTXD2c2PcQcHP+5H1rG47DSdfTzhfq/ZDfDnGE+qdxeQaEt+617BvBvNWN3H6FheGHzdSM8k6IBIOPrbgy+IXXsybehmhH+5hSu8qJqA2tHhbrnz7r1lS4xPt7p3y7Q8LrJ8j8JxpzC3ndupQuEHYxaANREiQs95Oo0XlfgVis+BTfx96RSTjQ9a5R74y8ovjBv5s/Fj0S34U3uPCLbymSV194bWuaYBg4F6M/AcrwbWHReWm1ZACY1hOUT9g7GOoaPdam+KaYvqtNhbPRbAffd1EaVYTEIyveWXlVpVOGF+UCAJroE6McuxCMNVMdkfvocGXAGDpPd8mWbGYtRmE2nJMvkCt2xLLHUz3QdzYORCvWhmL+sfqMZe4U7Rp4XC5Et+zs7VHgqDJdl1b/0XXfW7e8oEtwCke9gyUuKKlMCni9ErYD38d0R5qQOJNfKNccbaLp0fqdiq6bthJRANr5bbDcXfZrqRE7Hk7/mNPOh3L7RMqJt5xY7M1PphgxTCZmW6AlMrJi68XvedgQ9T35/5HBdrEevGSB6hk0+Zbl8zSkx8OTx+OQscSG3Fa7dqWdsqWpontCmMlmX1gY7AGqMCqr1imkePLq5MoRgoeSoBfe2E0WlDanBk5bW5YKDSh0wgFcYlerUcjTWWKu2jiBiFFDZ4SpcKb3 3G0myvAx 7YyP70lKHJtBC5CVtyRtnpLkSwFmHE3qmTzeERH61+AB872YaCuglHgcjpnOOdUPXE2f7hN5ueULDCj5lzrAImhd4sdww2RtCgMqZebqbdeHs94k//bLwN0pmbeZ6vF7+Z2l0/Y7V7Q9p2A/b/AeMKMcdAp/IS0Zoq0xvKpahS9ftr6IlaIn9zy0Ip6p1wSYsjopQVerIckOtTpS3grehWggFX5mhh/Vjp2ANSZK71EDaQ8lGpyuHyUz30Ljff4lXaJVNADiM+53br9kDvyQJL4qblfXY5Rl8Jea5XO8wRCa2lx4VhZw8UEQUrIXFCsdaupKP0h/I1TfEfHQ= 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_SAVED_DIRTY 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. For pte_modify() this is a bit trickier. It 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. Modify it to also move _PAGE_DIRTY to _PAGE_SAVED_DIRTY. To avoid creating Write=0,Dirty=1 PTEs, pte_modify() needs to avoid: 1. Marking Write=0 PTEs Dirty=1 2. Marking Dirty=1 PTEs Write=0 The first case cannot happen as the existing behavior of pte_modify() is to filter out any Dirty bit passed in newprot. Handle the second case by shifting _PAGE_DIRTY=1 to _PAGE_SAVED_DIRTY=1 if the PTE was write protected by the pte_modify() call. Apply the same changes to pmd_modify(). 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 --- v6: - Rename _PAGE_COW to _PAGE_SAVED_DIRTY (David Hildenbrand) - Open code _PAGE_SAVED_DIRTY part in pte_modify() (Boris) - Change the logic so the open coded part is not too ugly - Merge pte_modify() patch with this one because of the above v4: - Break part patch for better bisectability --- arch/x86/include/asm/pgtable.h | 168 ++++++++++++++++++++++++++++----- 1 file changed, 145 insertions(+), 23 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index e5f00c077039..292a3b75d7fa 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 @@ -375,7 +400,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) @@ -385,7 +410,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_mksaveddirty(pte); + return pte; } static inline pte_t pte_mkexec(pte_t pte) @@ -395,7 +429,19 @@ static inline pte_t pte_mkexec(pte_t pte) static inline pte_t pte_mkdirty(pte_t pte) { - return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); + 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_SAVED_DIRTY; + + return pte_set_flags(pte, dirty | _PAGE_SOFT_DIRTY); +} + +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + /* pte_clear_saveddirty() also sets Dirty=1 */ + return pte_clear_saveddirty(pte); } static inline pte_t pte_mkyoung(pte_t pte) @@ -412,7 +458,12 @@ struct vm_area_struct; static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { - return pte_mkwrite_kernel(pte); + pte = pte_mkwrite_kernel(pte); + + if (pte_dirty(pte)) + pte = pte_clear_saveddirty(pte); + + return pte; } static inline pte_t pte_mkhuge(pte_t pte) @@ -503,17 +554,36 @@ 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_mksaveddirty(pmd); + return pmd; } static inline pmd_t pmd_mkdirty(pmd_t pmd) { - return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); + 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_SAVED_DIRTY; + + return pmd_set_flags(pmd, dirty | _PAGE_SOFT_DIRTY); +} + +static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd) +{ + return pmd_clear_saveddirty(pmd); } static inline pmd_t pmd_mkdevmap(pmd_t pmd) @@ -533,7 +603,12 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { - return pmd_set_flags(pmd, _PAGE_RW); + pmd = pmd_set_flags(pmd, _PAGE_RW); + + if (pmd_dirty(pmd)) + pmd = pmd_clear_saveddirty(pmd); + + return pmd; } static inline pud_t pud_set_flags(pud_t pud, pudval_t set) @@ -577,17 +652,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_mksaveddirty(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_SAVED_DIRTY; + + return pud_set_flags(pud, dirty | _PAGE_SOFT_DIRTY); } static inline pud_t pud_mkdevmap(pud_t pud) @@ -607,7 +697,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_saveddirty(pud); + return pud; } #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY @@ -724,6 +818,8 @@ 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 val = pte_val(pte), oldval = val; + bool wr_protected; + pte_t pte_result; /* * Chop off the NX bit (if present), and add the NX portion of @@ -732,17 +828,43 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) val &= _PAGE_CHG_MASK; val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK; val = flip_protnone_guard(oldval, val, PTE_PFN_MASK); - return __pte(val); + + pte_result = __pte(val); + + /* + * Do the saveddirty fixup if the PTE was just write protected and + * it's dirty. + */ + wr_protected = (oldval & _PAGE_RW) && !(val & _PAGE_RW); + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && wr_protected && + (val & _PAGE_DIRTY)) + pte_result = pte_mksaveddirty(pte_result); + + return pte_result; } static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) { pmdval_t val = pmd_val(pmd), oldval = val; + bool wr_protected; + pmd_t pmd_result; - val &= _HPAGE_CHG_MASK; + val &= (_HPAGE_CHG_MASK & ~_PAGE_DIRTY); val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK; val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK); - return __pmd(val); + + pmd_result = __pmd(val); + + /* + * Do the saveddirty fixup if the PMD was just write protected and + * it's dirty. + */ + wr_protected = (oldval & _PAGE_RW) && !(val & _PAGE_RW); + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && wr_protected && + (val & _PAGE_DIRTY)) + pmd_result = pmd_mksaveddirty(pmd_result); + + return pmd_result; } /* From patchwork Sat Feb 18 21:14:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145655 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 8C65AC64ED9 for ; Sat, 18 Feb 2023 21:16:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88D10280010; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DEA328000F; Sat, 18 Feb 2023 16:16:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67D7D28000E; Sat, 18 Feb 2023 16:16:17 -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 555DA28000D for ; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 18882140338 for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) X-FDA: 80481670794.06.3A408EF Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 2B8BB40009 for ; Sat, 18 Feb 2023 21:16:13 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bc50K8GD; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754975; 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=1K7hWixcu0DRP0TOxVYSNvsFGJbVf7pw+XfQxdSR7lc=; b=5aTb4PPGUGXc8gANuds7EIjeWNQn0oNibNyZhSjAVYkKsFjUlDNCeB6o5BHrpY+FX/0oql oY/lXahj3FZ9QAnlgCR/M5x2i4yQZ8//xh1+bFB0w/MMqwtTlEKeTD/AqQbGndWKKZyOVs UyZz98IQXIzl2WFIAVIb0/B/X6OFEcE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Bc50K8GD; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754975; a=rsa-sha256; cv=none; b=2AxzjJ/wyPirl0vcUR7aH3Qrm8DDeLA/jZL2DlYwIfFiMZcaZJpbN5BRZScj9Y5ikmBzYH dtM63YOVb4hGoyLfwz+TQ05/EMu9YC2rpIxW7jQJFc0f9FMPGDdktafqd6KcrOcn7GhqI2 znzVi9ERbwwLN4aUdvl6Rc6R2XV7P6g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754975; x=1708290975; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=o/D1WjgTvHa+RZFY0t2cOsdLZJ4ujw/CAZDldCJEJW8=; b=Bc50K8GDOo5yzkytzLEAWoLidTInIOtKzb4ComyvRtviZKJhPm+ejKQF rK3rwFNtndn3g/+nqr6vmmL/8LLgv/W72o+x5H+INqR9MYfAjEMMIs/d8 3/upAP05Iriqn89qgIbTfufQ1NW7EtZB8A/OlId1YEMC2bmzNY+fHtjB9 XZ/FaUGx8mIRR6Um/0lCJRjeVHm+RjRuPdBRhZJqCFoapeLxTs9qYnw82 7nu0mtEElC3EyjpL/yGQQsaCIB3aRrUi2EX2BjJ1NvpBgJPNgcU1rdca7 TxhfPxuJ/Nt8SCx7Ao8bVUkmLSXO6sfpInKuWFy0R3BE1sGl2qNnMC2Pu A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427464" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427464" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:11 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241653" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241653" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:10 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Peter Xu Subject: [PATCH v6 17/41] mm: Move VM_UFFD_MINOR_BIT from 37 to 38 Date: Sat, 18 Feb 2023 13:14:09 -0800 Message-Id: <20230218211433.26859-18-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: wi4koqc6n14pdota8oieffg6uoyfs7eg X-Rspamd-Queue-Id: 2B8BB40009 X-HE-Tag: 1676754973-577970 X-HE-Meta: U2FsdGVkX18qkC9Gm7Y3v8qAT47Eq4cAzkeY9INyl4EDvs5aTPV1Kb1hgHhhyHkDALc7QHSr1/DUSqvgPLz+GTP36E3CjKOF3tfKsK/YFPTi7DIUfAtGwB17ycANwkyd1R5CPsuFH+RhUuo83fu4kTy0L1oqlnwQAGBl1XezlAHYkpFbCuav+TpVCuqS8QqstuePI4goq4cFUI7urUQkYGmBwQIDF/lF9uok5/aX7OtmGZHtfsL8fQPN8d63Woj2OE2IRSdKE8joKyMQnCvP/Sj3toNtF2tue5442+0PjXp6MFxSkcvqa+BXxd+yqfoyReon4WcUM/OV0z1Wo2iLN+Ozn/jcVLXkvV/ylIVT4LGQQZ93X/9vN3p5pPHMT4McYRyTyGOUG4ToQd0/5JvTUrS3jqTdjgedHB1+wUOsm8JrxxeS109cqugEO4zihiLu23PdUHc0h8zlS0RrYhoHm8cTSuBqIdfLbBT+W75B6qe9ie5QhMu/+TaauwEZytBZxGUNCWQsEhx2QqhA1WbZKiwrKoPAlLC2cPrL37KJ/9TzKoQRaBEFxWFJZfiVSJuusAsRfDn1EOvYIhoKR0++CAc4xFrmO+gTa0m8u823B2iwSYSQamAZ8jOwgHUZBh53F7A+sZOqCOVlMpwFf0tux0XkqY/WcIhkTsygyL/+PNh6vCo2LmIp5tzC+AEMGisldjLxj5LzVEISs0SD8/0nVaWPN4I8xTUW/bQpc2sRzbtleWjvEH9vTKsMZ/g6Az7VF9vxPgOD88iACWPYyT/bB1vx5dOAtRSvBBMLHpZ4AaIWnceHRmaxWJRgUAD6+LH76b5THT0SiKinP9uhDg3eBYTCCp8wS28E6xmQ24ST5xgGtpCt/8eUJG8Gokc5OQK83Jdcd9yeSt+smhBZPKNA54L9IWlKwLTUpX69ph+NpPk0H3DhkVIeCabAmB9ikugO6mgPdCFJ+wFXmodGIy8 yn4jZSC9 Bj/ieDo5ZApOtuRNvgHe/LgAuRvrY3xr8XbqrnmbbM7zUPHvLgAHrrOZhf12Ebd4GtJWdLQhtkWgfikH4Fuc1gOtu+ru3nHDnquJZasaQgJACpUQ2zQrcefiSxWunu4qvFSDRE+4IWE0+JxFBUpydyWhctWrXCyrbHPK3hKDv2WxQ5+lXWUa8zOwTu4qSJz0sN47zgcoLkoDAMDfRuRgWZzExV+lwlWNyxFNry0npcCb3PMhClJeFNzuVKzKMT4MBdV5x9nbwGI/pZJ2atJnPQ9jKymzO0eurhXOu4VZUnmqf7njcOBODL3Fb7EdULw1C3R4uUL+FyUnPbF+C0mmeCFebMvT4Co6etwCajIRtduFcuPguIgEbfQ/5qlq9jwk4SG8hg9vX5arK4iw2pR4oVrhVcQ== 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 4650e2580d60..e6f1789c8e69 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 Sat Feb 18 21:14:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145657 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 48C84C64ED9 for ; Sat, 18 Feb 2023 21:16:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBF1828000F; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D841028000D; Sat, 18 Feb 2023 16:16:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C18D0280011; Sat, 18 Feb 2023 16:16:17 -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 AB24F28000D for ; Sat, 18 Feb 2023 16:16:17 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8223FAAD8B for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) X-FDA: 80481670794.03.33E9D8C Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 84272180004 for ; Sat, 18 Feb 2023 21:16:15 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=n3sEmWyd; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754975; 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=hZVQOPh5gsC4utlFQ0Ct33FH6ZCu3J+LIAtvRyiL4EQ=; b=nYwpeNIskFsRll/3gtSHWXbGWPVMV4NPVJrhOF39c8ZoRxqFkz2J5h6VcJJXUdG3Q2uzZ6 tA/iQtkW0dHf3ePef04zzPephb0kAErnxCEQ5indv+F0Sblzhsh1Ax7dIR3gdun40fnFEP drpRGlmI0uLXTEkZIcC7bC7EUfOCnb8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=n3sEmWyd; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754975; a=rsa-sha256; cv=none; b=sukGcFSpo4z4uvSMr2vayY84MEk3quoKz9pjUj28PY50jdW0WFqh7+nhklckROrBOKPR3O k60g6DTox/1orXoXpbR1BXSu1mfl59Pfl/qMR6VbEX8YyclHIcRasY2gangWPekYex7Q7Q 5jaOGn3t/B1aSAP3qqro6GHvrsxOrkA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754975; x=1708290975; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=tPsImO1U6ZzFWnTQpJpFZiSIEfl+QZcqM1Ze+IYBtwc=; b=n3sEmWydaK6VZhhGULwuc/bc1qbXgbYBXiPewvkBF/9dUWqT5/vLF1Mx c0TN4HDxdaz+W0yATGz0GjXpRRRHQ68zAfGPOBBvJ8LKmBswHg0QYNRGN zkFg3Mt5pyhf2/yes8Bbyvw0kZdMQ93+Adnm9EKiWBYfFtPB2nf940ocR DvGkAzVaRN/aW1Fsmz7WNkOay6dZbRd36ggitgZdFcWXK9BRtpXPeQ99T ojMEeg5yEmOKIjUAbywC51epHpMoGa0n+Sko8+bQ2/88Yf4KiTDfurj47 TpzwTAd2WZlhSqklg3d3jHrXw3oETED8OOwiZ9hw9ys3h2Ks4f1SfSoaR Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427485" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427485" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:11 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241659" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241659" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:10 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 18/41] mm: Introduce VM_SHADOW_STACK for shadow stack memory Date: Sat, 18 Feb 2023 13:14:10 -0800 Message-Id: <20230218211433.26859-19-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 84272180004 X-Stat-Signature: rp1su1hyx6b6w8cghsxdju6ywk9a13qy X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754975-827273 X-HE-Meta: U2FsdGVkX1/BfOFFS4KKYmeuH4nqPOcTHHhFjh8zKQQrGRk7gSVn4KmtntUSw57zm/5ThzYXHrLUGzaqZoGL+KZ4e7+lmJNkXz5w82yC/4S7lml/xfrz6wXdactawv+Q4NNJ/CcTc4nKFNkGaXn+ap2mpZ63fhpZA5ws/v7ayVDPzk3RFeG4yKiOO3XpotXm08V/p0w/xy6V8ZZqA8OckV3Jf6VjnF5Vj6/dTIntOSfwcs52vGL8sybrqVoMVGdk0+H3xVtKLgxBKGqEQWOUiAK/lK4JAue4iHpPAlGzKloIrsBbm1nsoqLS3Vl1T8mMKLXPWcm//+9bBQ4Xzik6a80QDY6RYT6JeJKljW5VQSQ8Pb0Sn7eSQBLJz9EDgpnAcFhiCbfzKp+rt+0fViRL5i2AV2VJU5MMuRzplT1/VN1AAejNuEXAcDMiWD69Jqp2c317trgj95/hNCZNfFc8IwK7g4QJoPW4mJhHJ+/IsnFdayQ8oYXbJrwlErbB2wnaJXQZZ58W9K/FoAv2ylb3EmTaOIUr7UR6+Fc4JsU10GoUgej41x8ZJRHHKtWEIa2T/Qhvr4cQrYA53YrITk46nkeEQ7qort2hVhMDOBf/4RwghVMN+VMI3HZZ2F8cTkRqMdKCncuHRnrNuLtLMQ55mvtNeB0zv75WeC7GqJrn6icZ+SoLXrY2MNUEjOXSH/k75sCYWgcn/Pt+iwCxgL7a9tMqAgxgm3CakDf6O3OyswaWq4hp3z9+CEfbs7/lxZpIj0h6lkgt6JaEUTkfTseVTSeMCVX9NwUzWh/0snVVq/zwZdZKOajWv+Fyt+Iq/2fd0fRFy+Ybqbym7DRU77GEMNdEE8BvJ3qMWqeu/lbh1EkBeuk5RfbKpuN6e4xyiEMjVeCCVLqtzgNDoUgH3Xy0R7jmA5oGvYjWxzSA4sZ2QfRwZastHT9XVJpyrj9gqtWks+yg4Eurn2k4ZVIEzF3 GVeJCxVJ +7CMcC7XenaYbdF5NXDlwTFyDhKbf6M+F7KkzeHg/UZtrIu2gmcEVhod6hbOGc1l6leyfRgXrg5EttXPZs7GF9JsEAAJu69IwDpxltI1UOavZd/3DJOuATwxF90lEbbIgLHx6dzLw2LZHbPfsRLU7JmoSug04y+fWfSfzMDPtHyoXkSNKtXvRsEMX5EMAmxnqOo11TosxhsljnLBH52QCJuyNC1po2BrkO7WM8awE0P4S0yr7pHQds01OIauTSLWiWDncLL1PsktYNkcc4VYAlMEKaz1elMGCwS28ugTbi7gUTXqAC0O0Fxqvxm5pg2SJswOc2QJ+IookkvU= 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 Acked-by: David Hildenbrand --- v6: - Add comment about VM_SHADOW_STACK not being allowed with VM_SHARED (David Hildenbrand) 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 af1c49ae11b1..9e2cefe47749 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 e6f1789c8e69..76e0a09aeffe 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 /* Should not be set with VM_SHARED */ +#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 Sat Feb 18 21:14:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145658 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 35A8EC6FA9D for ; Sat, 18 Feb 2023 21:16:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B664B280011; Sat, 18 Feb 2023 16:16:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AEC5228000D; Sat, 18 Feb 2023 16:16:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EE2F280011; Sat, 18 Feb 2023 16:16:19 -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 7BB4128000D for ; Sat, 18 Feb 2023 16:16:19 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4D7E8A0421 for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) X-FDA: 80481670878.17.2DC86D4 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 54BD740009 for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ih8vhIxj; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754977; 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=/UpplPI4+cYRkVpg9K9b7HSmsSFR2XVceQoo1OLvz9A=; b=srujo7ivs5q4xA/gos+sWHIJBMS6lVWu+PIczK/6wcjpg6LMcggBt6h2LZw+xHI4CwHURN 1maHcM9kqo4Ffrr07042PqReWPKDgjiJrLLA9TsQzsRNMONEGotjNYWsAwsEJdQTj2VYou mL4m8WTyWPBHt61hubreJH8qmwMreDk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ih8vhIxj; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754977; a=rsa-sha256; cv=none; b=WpVNPnaOuODrI1KkbJ1bFSwWPV+buNlZQyScq6xAHaYFnQsO21GaH9q2EsOQ5C/GMGiIdm HCLIq6GggkSeJclX5VMJuZQxU5Yi1ookFyjVnIM2+R7L06eh093wrhVls+BZhp76QcKu+g i45Q9ET+7Te9JNHdjqR8oNdg++dOQMs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754977; x=1708290977; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=jMpoJVpnciJcstvlDtXofQ3AOsuP5WXYYIAV42M/rF8=; b=Ih8vhIxjck2wkf6Hv9tfaizDLWQ9K8BkEcpM1fZINBvgoBCROCWmWpvA 7/xxWhMa4DzJwBP0Mvsm6Gma5EfvAwWuG0BMd3t2mfyVBj/kIY0X2QYSX /XOEr39fjw6fN3gBgYsKcf3hxsctIvu8NLI5X7PkbuCjU2XqJ5USDzsw3 fBomcM81k8x/3CtmWHaphIRq6630uwOr1jj/a12kMQt5UdityDLvmJJV3 n4h3enewcyy5SyG2zDbRKgB9bJ291NZXvLZyZQv3gEC7KrbIFFcVzuYaL rLTfid6NiYcXc2uuXDj7epahKvwt+1v2MFoGdblGTNtdFf8DlUFyxDFh2 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427507" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427507" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241664" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241664" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 19/41] x86/mm: Check shadow stack page fault errors Date: Sat, 18 Feb 2023 13:14:11 -0800 Message-Id: <20230218211433.26859-20-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: y8strop3oy8ddnkmbq79tctfqx3cbfqq X-Rspamd-Queue-Id: 54BD740009 X-HE-Tag: 1676754977-837130 X-HE-Meta: U2FsdGVkX1/Jr3v0vx8nzMd91Q9H6NnTTlkytZupsnb+Eq7e3vjzzUqd3J768bREyEZoG9Y12O0spbIMm7cQ4jXBCAgiQHcrWbbjZ+g5BTDCQP1wwpkbGwVBwecrV7Z12IDWR9Yvj+Z3B1OAO+Iqi+KAGNcSlkFNMOIqsx0oNmFH48BnGVwupQeCgV37zkKcqgNI02jsyLsvOOilQoDxk0VEcWiMU+AALyJnqAKg+wCCN324p2M1cNtyuiVND0hdTDdeZnQzVl2yIujeV5RNlIAbnj7Gqq20NJzWEvR4E+VqiPoVe9C97sNbUJPFvV6EKlQcvu3r7c9jw+xrpuc0InxV/QaMR65kaWl76Ml7hkDaPRl738eE5yrJDruMDIwgbXfNBuemhNb3FQ4wGYTpUefgB/AotmZP27aYhrHaoB6KmJZWuaap2ajxLJZNJ5Lz7eh2F8fNOYzFGsr6/eMTb1XZxiv4bxLZ4ZnHbRraIA0pTxdw7oYstpYzBEaXBvJffj2fIkjudXP2VSJIE8m0Z87Eh7HtTd17+uMzyqyVfliRDlzNxE+9a/I/uhEy1D3iDoKFP48nxSndq7viK18/FOBRpmc40U1kweuONVI1rzJtJVYdPVwcMXSWypyHHT5QSHi+uUQAxgVHr5osbgk/c6c2eDgmLNlJ1DQ2N5XwYv1rztvD6PD8ex7aa6+5teGJxQW37J3ZYazC3vTwm6q7WOppkBYiRNDoO5rq6TH8Ms4q68YpUob7hAWXgV4MuePnO0jSLfw4OGUezdQNlqciWAIhBqujU8bvaA1+Vu9L0tw7WCYH9Le4wObzr1rdF7xiwaLysTObhL7nCHuQaX+O2VfHgL/DOhmZN0yC/eMvTCvZ25YI7aPYJJphyjOJKFO5wOE7OCC9njHtQ5ptzNG29H8kCUeoCvB6ePjDhXB1l9+7PSP8Ynu4vlg6Z2/UtRb0zj6DctNRhUBrZBAcoRW fdcCShyF oB6lPmnQ+36ADwjGkNZ7FdiIozs47p0XFoHpLJt3LM1M9Twak9zlPCupBvarGrq2TKDz/GL5rIGc/wBYpP1btKambJNiOHLebC3dkpdRmJg5oatXVajsR+lfRsPxaNBojC+RVEU/LhKkEkVTPjQlqREgkGiZuVopo0ra/5oQiRe5pGp0AyMutg5Hsxt7lfX1RFrjKk9IKMN4dOCCpJaxLCWEIEpQhK8jg79b45OCfX85ZtvTtuiRq/ZW5/zYHJ7tmZFsC6yQZ6P4KId1tHpcumGlNNVCs5j8xqFgk 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe --- v6: - Update comment due to rename of Cow bit to SavedDirty 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) --- 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..42885d8e2036 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,SavedDirty=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 Sat Feb 18 21:14:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145659 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 D2C15C636CC for ; Sat, 18 Feb 2023 21:16:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E961528000D; Sat, 18 Feb 2023 16:16:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DDDC6280013; Sat, 18 Feb 2023 16:16:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4DA3280012; Sat, 18 Feb 2023 16:16:19 -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 90DA928000D for ; Sat, 18 Feb 2023 16:16:19 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 71B71A063B for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) X-FDA: 80481670878.17.F9ACFA8 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 7DBD6A0006 for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Cr0Lxczn; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754977; 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=9UaPp7NFG0LEcPHMvr1nfm8BlHPzTXSOfJPjX78pISI=; b=lqZ29Lyt8Vkosbha55BiwdLYtpEv1wHEx94Mcu2F3LAwq32MOow65ENeXqf0pap6w2+wo+ OqvOTuFe40u2XKr71s4IeojlrKfjdA8LKGiDmX+63Ir95l+TFrXb+Nehics21KROax39gx MccOdC1A2IXSqcqN5DWRpQg4Gaop1Mw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Cr0Lxczn; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754977; a=rsa-sha256; cv=none; b=34u4Fi9DL7iMahA5jnHPImOCAZBQUEy0SGdJv+VJlNZU5vrdCPUP1RsSVy42VNShf9dPSN /Bgpcf5dsARKunlI2wgQd/o+x9Tv/FeSHMnf+dviwaToItshl07+KNq0WSAZ8h1BwSH4pd gpN0T2H9d8W6xB9Rv0P48VrTOSPX+NU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754977; x=1708290977; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=FRX2Zip/lmlZmsFz5KdG4yfHn5/JjBj845luOn8deCo=; b=Cr0Lxcznu9OfXxmvCmfiBQBIH7N+tqBJJHfFJyQQfHw/DhkriH23kVN/ pr5IGThypBvgA8gY/PjsEFti8pcOmb4GMAZdPLp1MFwmY1U6LkuB74g4a temFSBbuAjnKg1cLX8bo0hsvf85V2+zaas4bIclIqEFLx3bB0IhTABCDt ODQdLnc9dZyVoq1ePAQ5jhNnfLwrsHFWYBQsGOWMIbW1KUMt0nDIJh6yL aPZodVDhcc9r0V1J2A95Qiy2MzzB2d9zxIyNtXNH0ogKZPfQFcGyyDFa9 /BCBe2HLcQTbeoU7TjmSUUCn8HIwx37OE0Hbkq+1MLmvAsHz84oJv/D/I Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427530" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427530" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:13 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241668" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241668" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 20/41] x86/mm: Teach pte_mkwrite() about stack memory Date: Sat, 18 Feb 2023 13:14:12 -0800 Message-Id: <20230218211433.26859-21-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 7DBD6A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: mpz1b8ndbjgr6umym4o93iwgkiuh1o8z X-HE-Tag: 1676754977-938553 X-HE-Meta: U2FsdGVkX19Y5T7EAIDtmvFe4Fh8CiKyyVwiD4OkLoK7r96zBXdmT1g9oJijJzz5I1a+sxwtuv0B20aUm7VBmMRxsCNXekFvVHeZwvAXDPU3t3lCby3KAMVVITOcqqNZ8ODO0ofjyX678ga0F35+c9lBuNkytEPOFyZS8jp5+X+LfXC03oVRtNG7C7IjTuOj7HVwhqM87VFYDY1LNtWAcr76cCRqz+GzJ8gSAo0R1Mt+nACvLvHPVtk8Vek/eBDqoQdP9Si8J7XSO3o+7qse83XPi4PRmlhvVuCAin8iZV/UMVwdIUE3Tl5Dj0NNmN1ZZ0MIgOnNSB4N4GH4VYPwFabFy1h3DcbumzHk2wa2HQWqv574rXedYouL1Bh64q2ESXlK7nO9tvXsZEsaMFe501MDnCZruJQiLyp1vi1zha0snMdL5U6wVwieu9KeKQ8aOv5a0MJ/DkcIeFLpvq97gF609p+uhS4qVSfPDI0Q+Av8mOqoPXbvynz3cZhuvxAyspvexxv/sBNLIERcneIN5SbBR7USeJJoQco1iNjwF77fHJ0X4o46ZPZfAgA04+vrE6YINtABc2Qe2syC7B7E7BILF703DxHW0ogylhN3tDoPsncUjA/4IDR8r4LmAgV3sTb6Du7l5gFeIZ4mevWIBLfEChsSPd6VMWky55US57E9y6YqX3zz8Ml8Gro2OFsZzO9SW3ItpLJWCxTVa5/qa2bGQLxE3PbJ5WBvUqiF/ewFNpfboqXdGoZScd3KGdOQ63fcr1yIGgTp0ux5/QSou3TxWRMdE7wR9Bx4+ehoNcZa82E5KV4O60IK4gKj8vqHD1cR/3bvd22PV4W8AvfVMg0htIHnhbufrfT2ccpdAUlFYJwS4VtjUs2q20UTf9eV/BJOysYn/Ttgr6a8d7e+N84nuO3l+yHGEm6YRFpQbLI62wR9Tpt4ClqhnvDEUXOJuOQuA8qNICw1neLee5R 55Y2Ub9Q H9OXz44M8ioRjITXFo3a0gtylTu2Va5s5gn0bLPdLUmORkboXF74ZqsLgfumWBiJw3SHZtelhE2+4gtvoRkeYS059dwEVu7ekgqy268+kF2Z9fm05x/F+OOVFKC6C+5zCYynU5HaTo0f1ci12b5b6oeQAikY6LRt/SDBZ8p/B/LSJLnhtW9IBTcWGpyG6bCtTNCD1nxVRPR42rw/7KQcKY4YfUxIfa5JElLpn7usQgudbgzM7yGjlbIW5ew== 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 a VMA has the VM_SHADOW_STACK flag, it is shadow stack memory. So when it is made writable with pte_mkwrite(), it should create shadow stack memory, not conventionally writable memory. Now that pte_mkwrite() takes a VMA, and places where shadow stack memory might be created pass one, pte_mkwrite() can know when it should do this. So make pte_mkwrite() create shadow stack memory when the VMA has the VM_SHADOW_STACK flag. Do the same thing for pmd_mkwrite(). This requires referencing VM_SHADOW_STACK in these functions, which are currently defined in pgtable.h, however mm.h (where VM_SHADOW_STACK is located) can't be pulled in without causing problems for files that reference pgtable.h. So also move pte/pmd_mkwrite() into pgtable.c, where they can safely reference VM_SHADOW_STACK. Tested-by: Pengfei Xu Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: Deepak Gupta --- v6: - New patch --- arch/x86/include/asm/pgtable.h | 20 ++------------------ arch/x86/mm/pgtable.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 292a3b75d7fa..6b7106457bfb 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -456,15 +456,7 @@ static inline pte_t pte_mkwrite_kernel(pte_t pte) struct vm_area_struct; -static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) -{ - pte = pte_mkwrite_kernel(pte); - - if (pte_dirty(pte)) - pte = pte_clear_saveddirty(pte); - - return pte; -} +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); static inline pte_t pte_mkhuge(pte_t pte) { @@ -601,15 +593,7 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pmd_set_flags(pmd, _PAGE_ACCESSED); } -static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) -{ - pmd = pmd_set_flags(pmd, _PAGE_RW); - - if (pmd_dirty(pmd)) - pmd = pmd_clear_saveddirty(pmd); - - return pmd; -} +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); static inline pud_t pud_set_flags(pud_t pud, pudval_t set) { diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e4f499eb0f29..98856bcc8102 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -880,3 +880,29 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) #endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + pte = pte_mkwrite_kernel(pte); + + if (pte_dirty(pte)) + pte = pte_clear_saveddirty(pte); + + return pte; +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_set_flags(pmd, _PAGE_RW); + + if (pmd_dirty(pmd)) + pmd = pmd_clear_saveddirty(pmd); + + return pmd; +} From patchwork Sat Feb 18 21:14:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145660 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 B9333C6379F for ; Sat, 18 Feb 2023 21:16:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D54C280013; Sat, 18 Feb 2023 16:16:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E4E2280012; Sat, 18 Feb 2023 16:16:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19D2E280014; Sat, 18 Feb 2023 16:16:20 -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 F3FBC280012 for ; Sat, 18 Feb 2023 16:16:19 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CBAB44067B for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) X-FDA: 80481670878.16.E4E7D22 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id BC31E180012 for ; Sat, 18 Feb 2023 21:16:17 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IQACktDc; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754978; 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=iK2VGgQBI/+OGXrqkyy4L52pNUPaFihWRDsp8yLUZlA=; b=v1yRIiZfNbhAj2FZhvxLjmCzA0ukAxwglPg+g7vK58EEP+7q/6563jbPM7nyTMBuv6qYUv uOG32ZvXowillVsfTvs1XucpNFDOJGPQGUaiWog9Vv5ijxgb61fcgcvllyLdq/XKBFvfi/ SDmhbXzD5bIASxFOsFsFui6zoP9YAv0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IQACktDc; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754978; a=rsa-sha256; cv=none; b=ZL3d7ghQwhry2sWGM+Zj39TlYuT23tfdKDMjkgIFd/UoR4Wf8YHcXZ1HSnPAEQImkqRwVL 6xA9z1TmZBERLWLLmA5J1MBRpClNXTqfsgz41IXrCrBX7e56WWxCS1fc+9w3biw8IXOLJW wyIvp/H/uiVkX2GPSaR1kn0jV4ryI5c= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754977; x=1708290977; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=WCxUl60+C8EdhSm8/KOSywQ4hWAvwLQmO3bE5jU9eJA=; b=IQACktDcYQFAG0AV195lLo8YEDk6gtdP5pyyZokGzmdUa8X0TQM9igLO iaM4ztOobamm62vDODsA16Icv9MYl54WaL9ethPybazGfk6KEvuRIutoW GjCtCr1rYxDSM+UM5LA3roZt9jSf/BBTdSWawXSDagCuuvAkXhoMO/TEg B1C7tc6O7X6YydXziHvbHM6ngGe+RfiEVxloM2xkJy08/0/FSw8/ZEwl2 uCSjSwB/CQWg3Kcet3hOAgrRDrMyB3gnq0kPWOJKGP823cuzds4hndjwe ccwn3C/E73MH6geP3kGE/IvFIaSeBbhF8siWQuv9EK550wKB89NMhrYSM w==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427551" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427551" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:15 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241671" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241671" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:13 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 21/41] mm: Add guard pages around a shadow stack. Date: Sat, 18 Feb 2023 13:14:13 -0800 Message-Id: <20230218211433.26859-22-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: BC31E180012 X-Stat-Signature: k7mzztuxg3twmg477bxz935x85m5f6w8 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754977-554852 X-HE-Meta: U2FsdGVkX1+HKuNu/u87p60kanDLZvQ6v/LooHv2qvDU8RLJ78Ivk0rF50ltCyh3suPnBWW/xCKqSF7gvrlVo7BM5NU5s7nnLmcn12HdQGLbVU+mVwnkTzodsFQfmIkqCoVVBSlxUBrGsNIfxHTTSVlqI9a3xhjflCPIEv+OoP4qvhFBqGJRlp92tjlqm5QdMZiwHHKF2xKQIXLJ6mgbiJD5hpUX4ybtvckDZ8iWTOY5Pt8H6sHoVLvYPncY9TSz9lRgTMwKpH8eX0l4uF/ootma8ob5ujGmUTricaOvEguExslmKtwdFdxMI1I3T2QxMjtZS9lBF3aW86SDZC+MNZDnFWMNqvJ7Wt056wBvY50ZcnTmmjNfuLXUcNd7M69qkAKN/fwootdQnkqPPTD0SQSXo6fU2uRE9qcpzUvIlkGjBp/YM3UPoR3QUBNXrBzQO5BrgQKPwbdblf4jLaTcXxksW0JVyHyDv1gkB+2cGx7+0KipfNTsnGsSpJ9hIelclrN+nB/kUIvVI3pQAMtzlPrYLWf9NyUHjtBV46h65pDZhsw18kAAX2+kPkcF/uVNCnVD74F+p/D2BjLDbUW+JYW0qoOUJ0HzXDoNXhBkyC8t1x6+ND15ADg00geSTa/HY69Xbz0EnI40sRdqR4G/7H7Veae3cFooMu95VCEOAhiGkiLXfdYXRNovWjbKpFuUd6KumyG4jxSvpBvnlgbuYqhRUWWZejLnQwCGLc30xn/56yt4rmbiyWhZevY7FE1W/BLceqGy0K+9jSI2XdJxX+7f7QD9RqQCJ+w26jUk2hUb+xjk+gy4f4NbU7CojJTPFcYSfeD7pzVNWxD58b35o1dJzEKdq+3fjWHc5+tf3qKPl3RRU+xXcPktpnLdtv9kaLAh1pRM2Zwyloy4TKgxsUQoLwSOSuTuXWrr0gZl0lZi45ska9vagZdv5sn8cY6n6v8kF/zZP2PuniO680K shS5fq6R m1SmDIj6IB20pjrXTG5/a1FNrkoHx2xLD8xlK7kNkYfTR892UWJZH0yMFvjnrtci959HrJT+3cimFiF6En1MCeukm1CvraWyN5OjCWwlIdAogmt52sPtd41wTM6QCV2dLoldsOcsxRPZqKKvlvOCbvM6UGibpyluWJZBVC2lG7/R9xCucBPFBGO4IUfulRHg8g/wG29RAvF1OTGBgYuThWqF+ASaQp3ANAjMP3ZcPuWMDfQW8zsV+5eDdWYqnlVCBcz12klwlE3/t7by+/SMLmZKE+Jg7qvOtOjfO 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 Reviewed-by: Kees Cook 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 76e0a09aeffe..a41577c5bf3e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2980,15 +2980,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 Sat Feb 18 21:14:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145661 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 A66CDC636CC for ; Sat, 18 Feb 2023 21:16:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E250B280014; Sat, 18 Feb 2023 16:16:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D82AB280016; Sat, 18 Feb 2023 16:16:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B13D9280014; Sat, 18 Feb 2023 16:16:21 -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 96B2A280012 for ; Sat, 18 Feb 2023 16:16:21 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6819F1601CD for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) X-FDA: 80481670962.02.1124A7C Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 790A540009 for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GzJHc8ol; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754979; 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=QrUmx8EKept3iSq/eqbQaHrnHicPTWm8RRXXurJvnBo=; b=3ceAFFPLUlxymLAqadWOWunsk/mOEReyoGwoydmL8J1faK4l7kgoVSZv4g2nAM7+/173Mc /0EqcxrBHNYGISHKljbDf5sf5AN3vPiwT/9Ts5idA6Mk2952qdm3xIqmfIZNdwelu1nuc/ r3bKKfwbp3xKdRz2ABUUQmGM0j0RQrg= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GzJHc8ol; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754979; a=rsa-sha256; cv=none; b=KoEN5q+ntwya+mwEnbcy3ZsKly67kmwmV3jaKu7D0Dv7gP/jm2icgasqvIfpFhmzDdUGcb p43ntfFXYbPjf7Er0cTTzkTC/Pskfjj58FHUBd4kg6vg6lpC829f53j0SrHhoc7F9pG5KF Wh2tenArUeJFZIl7rUQA66NMIbGQO3U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754979; x=1708290979; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=h1Ina/ZMuUJfFdTiWzdv2FLDw2HpwpjMUTBFM0iRihw=; b=GzJHc8ol1abHPAvqMUIhaQAXpOCREi3Vl9ACuA4hCA4bMwIP0d+U3Fnv ngJo0u5PJIy7ujPolzF1pcdZN69T0PhuY78aQDrJCnFZrLUYNlH1z3plv LzUiDAUmA7EQno7EPQR7SLJkzHYLihnLJKhakqEMCLGTVcqwts1bZSOqD qR472E7zFinHeyLH9hbJQYN0fFdtidwupNR/0GdG3QN3eJRoHES7OEpTT QykE7OzbKNQ44fRiEQwBmcrNG+Xzh7VaIsENbSV9v01arQKmrpVGowwN8 FpW8GAFt4Ph/UgeRisttmDpLslOWFumnpeqGlIOR1TX+/58MPBIVV/uOT Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427563" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427563" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:15 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241677" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241677" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 22/41] mm/mmap: Add shadow stack pages to memory accounting Date: Sat, 18 Feb 2023 13:14:14 -0800 Message-Id: <20230218211433.26859-23-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: ekqqqmbdock8iwk7m3ubq5fyik6f7byk X-Rspamd-Queue-Id: 790A540009 X-HE-Tag: 1676754979-951542 X-HE-Meta: U2FsdGVkX1/VQokxKEX5o67EhunvxEQYXLUTwtRJqKAJFPHMtAUK6M529UgQMPgsy0acXcD4Z2cp1xNpLM9Er/8pYJJCePNpk5F/dnF+I+IBawjmTZoPbjsx/KC3BcqMfOnQPcW7EFcoQcimD20euioAy0qKEM4Qn3hQDsJIHSKraN/BA2t9AVjErYeO425jcjxyx3+K00zutK9Dg0RWyHN+CMCFsCWeITDDMASCp4349i35ZKg8CmV0L/ETcM1XnpZVbvnjbYz5MxrjKyaJX6aS8HxP5N9qiii6uRoU8xY4rpN5CiP+KRevhstSPsbfgZCnADXd2W/2CU1qqr/gcwW4TFPpx9UzYZgDszVFFuDtNc6PwpAd3kKtEh6qdy3AR2kWdmAYdy56I8aOVj6DLRiNBQDmZ0lnlVhRs8BjoJ5P2LvN0wgsDHASEayoXtecav3wUpBTVWV36c7qzKqF+VHOjF865/KeYLUSqf8KtNYOvYFHWhUvFmidkm6x4nJzz2AV8rMB78NC8jQQ4iXLN6lSrsHf8uM6Ea7hj0qvCpc7WJsAVn8HfDnfN6m1WRp7t8FgFqXs6dU/FIGBkBsxNX9Zq0Us/hFBw4CiH8/auecjWvg8ZHX7siog8ZU1SepHpZGoalRENhcNk5UgTsUKEB8LVhe5jG25sNLM4O+PKhCGsSi8d90EJMPkqPmw0daDz+sThRvVGK+tC9uyKp/+VG1UBAzgdXsEkMXRrcItg58BDfbQ/IJkP5x/z5b46UuJ0g+E5rMdS6y2dXxfIe7Ya7AFb06JjdoAWBw/wtON8CmjcgqPqJ6hGSE8ph69eP+zswfOpWYuYYTm9Uo6LqIJRpHRGbDulcYxG/LZIPeatzAgJEAysaENDiqYPB7vNLMsOCz1n1pWdAMN8cjr//azO0E1SE1s+UltX8Q8CJ4XeDdJ6LTw/iRFKlfMt/u8tC/dLA8sa8i7dznXFaE44I1 ea9C+YUk pzGFkPLclG5EoexBXaUZaVlmpiTJBmzgUPDlRxbi1T9nOJyysp7YQDBVw6w6kdLDIdPuI/6UGznFEqmiT8X+KvRxqYSDt4gtDVMgAlwf45e1tzOri/OSZ68D/PCOOXVq5lng9RSlckYc2Jg7ojRa5AVCMAKYI7JX5A8luR+duY15Pw21B4VEA1ltKD389nsxKwKx5MYzjynI+aY92/iGmPM3q5MvMXjZ2G6q2rDakioiDjnYuCGuH8OPZHmxVJWCR9sxkakHdl0yf5nk5rY6tZIzRGI27nRSX5hyv 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 Sat Feb 18 21:14:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145662 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 5B77AC64EC4 for ; Sat, 18 Feb 2023 21:16:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D5C2280012; Sat, 18 Feb 2023 16:16:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23863280016; Sat, 18 Feb 2023 16:16:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E21C0280012; Sat, 18 Feb 2023 16:16:21 -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 BF15B280012 for ; Sat, 18 Feb 2023 16:16:21 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9AAEDC0345 for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) X-FDA: 80481670962.01.D7D5169 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 97E61A0006 for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kXXQF5hv; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754979; 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=usK09sGT9EyD0WZXI5DqW7zN0VjIXAZLAMOKcXZAWZ0=; b=SvDov6YyF/PQvgm1oBYTHfUdyMrI2GxmDKJkmhygrbb+Pn4A77bc6WZZrhP7rsDVLE/2lX qaajnb9W1NgQoGG9f15Tyih+M+QbbEKIZ4ycQzVPz1NgOkGXzUci0hM+qoL6Xjg2+bpXWr wbxscAPCalD4sKj8gKPYJ3QkGuCfgyk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kXXQF5hv; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754979; a=rsa-sha256; cv=none; b=JSrnKmQ4ujEfTdiiyDGqUJrCSM7Qeajk6VHWriYPDR3G7Q/jeKOwVobWJJWWkB3rBuixF4 QmyFkItsSB+sObEzp0fJ3cSKQ2PH5tFkz/upFHp67SV45sbwq+RauC/Z0Bq1NTIwdv6p+Y JCgbQAR5XnTUnJOEBYtEkBGz9SKjd4U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754979; x=1708290979; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=M6irf6FiLm+DEdVV2c/4paQ/4mYnKshDCbvMphRfAuU=; b=kXXQF5hvRUhziiSl85LE9MjBtfKE7cFp84bT8z+/FlZeZ1KEI4CDhQkQ 0QV+VuGVxI0t8DDKhcvsRvZm/D4nSsg49Ym5xGggOWGcIhyXjm4r/ogbI COWF9mOkjJ2WoK5chcmx3SH1uqHVAXNH1dk4g8jBCuBqSbbz775fMFT5i SqymoVsEPRyIryE2jjFj6Kxy8FnZXfD/nZlyObIoIezusjfKqvRg2APlI cit/3cRKkW7JdCHNMndTs0ebKl1XZdg4dyR1aTv+S+ui/1GGwsxpsr/85 jS1Lq8MAMlqUW58yDT0q9M8LxIzvHHDTPYAldGuH3X103y9B4ZFhgvSLm Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427593" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427593" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241680" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241680" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 23/41] mm: Re-introduce vm_flags to do_mmap() Date: Sat, 18 Feb 2023 13:14:15 -0800 Message-Id: <20230218211433.26859-24-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 97E61A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: oqa414j1x6dmupcdm1tu14jipmwfepe4 X-HE-Tag: 1676754979-798795 X-HE-Meta: U2FsdGVkX19vy35ETopkaIdk2a7WFPr/qRZyC/8CQiFW/sQYd/4RGc/HFH/hdCtsopyiDUzn0Zs62qQOLFtamVK996dQNU/kLOATSX5NZHam+FQ64MUzS766fAvBbUAkIPqP3DXSxQwbQOc3EZN/0kYi2paIRQFeX78J4tjfkgEV4bj2af7wj9hqdalqf25wku0qupdpw0MiEkiGrKq/KtNbMQk4u/jjNurFfNCGkszVaWxbM8g5EhXWaCuJ54WJnFHu6sjIPHqjdQMDmD+PdVgr67rKxLRFNM491FOUR0wmlaxaDIOIGXBUAtQf7F+jnGhdTzE3LJtwfrq5Itr1vUHBNg3PaC7UzaWBeeZhnvvQJYL4s3cqLtupikYCJUmWjKtwrbnWGJl32yueuCVi+PehONauSdivzP7E1RQERKV35FmXtV0v9hS/QH1QIntkSh61qKIrqdXJdwlQbwsYG22c7it6eNxlBg4a68xHI3hHdLgkAcMOT8a+XbgSXTWq3PMy0AkZ5HqyxMg7cePZ5oRXAixi7n8feuHAf+ozIsDrMD54FqyU3rYpLy36xFlMz5U6AvYcOZ93Id0RwMhwSSpKSOWWR1ASJ3PoPuAdTiip36le+DTfYgHMhYmra+O0aiUZMztGHK/pUsanVOOs1plqKd/raiVJ8OWemh0/DcF9Wr/yq63mnfxQCARpX4F3W307NDS6pg4Y/42s/8+jL+akY2C3OXG01z4kFBFwhUv84uroGHIJAYKJWYk5cuYBdcSh+h1G84JHB9Os4OWIAVeqgN0KRyqwHo4LLC6RiYDOmGMJFRAAQMxOrDM837v1JDieGdadgSwvJnJQudi53HZR3NIASwysEe6t3rnZaeVr+cl3LMsj5lZJ4rJ3MNskgUeVlh3SZB6DJZB9XHiqfeaFej6jJqFAJiCQLuTapKjDcoRwLi5W8pDUnOOX/GV8MywuKf6N7J8XZofZaiS Y8GRoeAf xfHqqucRXQHuN1IqB98W52O7FNubW0Xcj33+klNyWt/tgajFLBWn8BWYrFVAShgsDOf3uYAy6WAqbvy3lNdoz0tlkk4yqDiooMgLfayXde9CCtJKYm1lntrlX3ONcHsvvHucuqTId3PhDpFo5xdG/+lL7+5nDFaGq3yYcmL8kE4VMHVW6YlYK2NlOjzs7J4SkVsgi6U1HuO0bFuD877zSFubMiMT05bJPpbtLOdlzP/gaaKy2YbITg2SqjF674x0Cg7cYSyp7Pm1RV2r6kYIaKDMS2nW/R0M2mDy3sSGJctX8XeyvBE6CmPLYCZfq2xPggX5UJX/KLRlSzNAy2BUDh/kYS/alJEaXAB6uNWSa+jLqhid9bJkorYFdBdp3mAA30fGZH6BHH3ZZRfea7gn9+qQFGmBDBkWLJ8Tm+IcWqeGmfS+fPYweQFAT+zj4sxD6iNW8f8W2iDOpvszs5KHnvtu5KtBdMJrl9cD2CLxMjbFrVNVTsID6ekN2GA== 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 a41577c5bf3e..6d6ffd7563bf 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2890,7 +2890,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 Sat Feb 18 21:14:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145663 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 EFBCEC636CC for ; Sat, 18 Feb 2023 21:16:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5825C280015; Sat, 18 Feb 2023 16:16:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E26A280016; Sat, 18 Feb 2023 16:16:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0259F280015; Sat, 18 Feb 2023 16:16:21 -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 CDF4A280015 for ; Sat, 18 Feb 2023 16:16:21 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B41571601CD for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) X-FDA: 80481670962.27.A933075 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id D9292180007 for ; Sat, 18 Feb 2023 21:16:19 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cqKAMcsN; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754980; 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=W34Awsk7ObJ3JdP2fcXDLIDAtaSphGEmGZ3iOjCGOHY=; b=ReV1R9zFyBh2dU4IjVGQDdoKvVHvqriKorujg3jXBu1g7XJeDHuk6M4zhZzVJ8RdPj5Vd7 bIpoJ7nG/Zep1Xrn0NWtKa6+UKRjBoc+sCR0MKehg8plHlJ8V+yjRO05MYCzMEuYrfUGEW D+wabDg62BdMFWMLMdvunqJUtspb1Jw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cqKAMcsN; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754980; a=rsa-sha256; cv=none; b=6nxn9FbnMFi+JE6DWWHvVIoT4rKmnzmalxUQLn3LxDjKNK9Fm7FvSR7uxlRmO8v1mL5JjZ qb3de78W+moGSUyKFSd1bjorcBbN4XN/ri7WBxzkQU4FZl/kOoMEz9pIP2vGvZNSYoQd2J nUJHGaKKPi+HNltQlEYfvM4/s7pv+eU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754980; x=1708290980; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=ECFpkrFmLdwFWEoO9V0RJS5m7s8m94UVjRDo+XGZ7qk=; b=cqKAMcsN0dx1D7XgrCoSGK+qi/JdEbMwVtIsL1ALyoSW/26V8nGuvRmM 283ZJtc8l7tLZSOa+DKcXZdlKGEKkhUgmSaD7CXA5HrlECiIzerTcSURp 9wM9HnOWYgenIGHJeKKkArKzKPFa+KDrJrUGZKE2Lizrj2588fgz7LhTv t+jhfXt4RPRY8C+F52v8hZExNwjXFgX3oPt/YFg3/A2IdOIlWMfpXsOo2 OoDUyiHXs4uufBVnDIZHImdRsh+K7DfquMZuVYB2Ke1UOE37SFrhrxAj+ GW01LA0OjzZ42QuTJSSr6pUivWEY3IXYxZddxezuuRg/qt3rXeP5GEBxk A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427612" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427612" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241685" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241685" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:15 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 24/41] mm: Don't allow write GUPs to shadow stack memory Date: Sat, 18 Feb 2023 13:14:16 -0800 Message-Id: <20230218211433.26859-25-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: D9292180007 X-Stat-Signature: y5g8orpf5efs1p1nceeq8bjhdqjokr6m X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754979-135475 X-HE-Meta: U2FsdGVkX19ErAQ3sUR5PmFD9HF/LAzKUnZzs+WQQ0JkWdvCeqqs0JSLzfatBv0917ayW+iQa2q88KQztNcSB/A98fB3RRuohtV64+UiF7TUMBMmFpgQzGFqgdwRhOQ8AArHM8NTmprrZx8MZAW51ykW480MT3+mXigJvpukA2Qfr2zVPrw7lG/+9w5u3UmsJUIM7WxKu1g5OYSZFIHWzBFHwKognm30qX7OEmi8QmYTcR8YN7LlC9A1XEafF9T38S+r1ysfVOdLUnXN0rbBa9mjSt5hnlNKtuVoss3tvcGMKFqtkfgz8bvneggpd0WkvSviokgn3d08k5dMBeyoZA+45T5W/k+qn7Q52PjetGOxpWDkpKyRLSJHkbtkXO+7DFkthzTaNwd+NbfD/IipD/dogYzV2foArSxuUX+dBX0U7fsCJJmA2cyuev5N8uMUVpUUkTv+itoZnoFCFG4qzuqX8/n9dQcJ6utYKJkVQTW6gtYV1srX/9jIDrqmsn4/mmNtXWS7y/DXb5hJQttm8n090YQ+jnzfWezfX9/F/z7bX6dz4/IE5rqrxasT0nwcDMUJY0d//Mh/F3F6Fhwho1vE0ASh5CgMEnetZ+S6tFu2SisdaMeaAW4QdlBHhDDWRjyYMJ/i+HgVUVJtVAtUlEKln3nAB6KfB62vAkfYg5Ri6WPQ/lq0wfOrHzOd8Px2m3LQf1nDDWaqM56zzaLW86l4DTJzy9JRjVFX4ZKBL8rlSrR/6pv4pKSQht8mzpSk8h8LS/rVY+ktCf5qRjEG8PYkkwtmhOjLUepPogfwx5ndrDP4WEPd1/0eUGYVWEzI3VeTYqizT8JXIdu3CHPRVLQbtPoK57mTyVLjE4rnMWjENAVVTwigThheYpXvOSOISGEaNuA1e7suwn6jEhBUG2XpRp9yvUyGxLGH1eSvMaPGnwPwvllurx66zYF9Ph2BUZZy3oO7LtBAPWKJoVe ILvlegUv b/yCe/FrskhDfJk1yES1SHYgjlkQCF3iO8FNPbmRP5XMsv8CywoPstbsE+QRIzELocg+kAFd8avY9kv1tawVd0LyaUFEgoz6XXQkUxoe+drhdAwYwd3LUc/N7gx49i3jd8e/N7uqYR93r4yeE8oGeSuxzCbNRK99YZeCstNO5Lg6aqp12sIRdAzwl1STK0rgR4i1v4xGb9mt0qaNBHWzaaqo+Mc1tECVDkGqxzEKjJZ1Dtup6jYaulMX8/n3BWzAT38ioXOi5AgODzYIUXt1Ad+iE9C+uFV79udCL 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 Acked-by: David Hildenbrand --- 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 6b7106457bfb..20d0df494269 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1641,6 +1641,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 Sat Feb 18 21:14:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145664 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 59532C6379F for ; Sat, 18 Feb 2023 21:16:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE890280017; Sat, 18 Feb 2023 16:16:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C4ADE280016; Sat, 18 Feb 2023 16:16:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AED55280017; Sat, 18 Feb 2023 16:16:23 -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 9DCA1280016 for ; Sat, 18 Feb 2023 16:16:23 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 74F5FAAD75 for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) X-FDA: 80481671046.14.DFCC197 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 82E5E40009 for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MmMKuaWX; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754981; 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=pidlwIvk+BMVdKhrS1IiKMNaI8P2tBU6sMJkaLSBIFk=; b=JinnYdADbfzfYZ49MJ0MsHA6LWua/3qpZYFsJl09/6zF3cRoJyQsaWUMZzApvR/ChpSfXi jqyhorQ0a6SBbtU52t06n36PGT3dPMn8y5T7O7f06+5TBYn1losBuEvk4n66f7zCrnvboN xJ3MEqFFp/1r7RONMJwSS3mf3SVassw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MmMKuaWX; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754981; a=rsa-sha256; cv=none; b=MGdD9Mh7TAfrxxII7Hq1gdMQr96svfnI0PJ8UArKK2rjPSz2XEAHcUSpHRMAuidbPKW50Z +LB1uGDGo953NoEauJ9v/hEtN0xQomD4s5Csj1DLThlhdkb2LVmlV8qkZYV6pBNLdo7NGE TBNXMrEb7YBJ/gS9V4Zf4YuTS2j8qo0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754981; x=1708290981; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Siy80TX29l+RDBjhgBwEUhEMPmCeRxNDGp+kmyr8mZA=; b=MmMKuaWX0cSrJNR/CeidXoYsrvnRAkxQxe5P7JfTqdaBD4VQZH2lqI9s x6OoztgxK9X4DqOM7+Ru12dd9/NBg+6MLGqvcMXstwZJukgNTnUEDU/38 hMk2lQj9Z8D0Zz3WOPTO9/KpjvW/mr7hHmpyrYKE+y0+VgvdEzpcGD1eM 4jwFW8ak2W1O2yofKdHF9/OEjCn9VQsgcGuhUeFAjQmy3qfISe8dld+cp r+l1up8R8KrAqSlTSLqZQOFlHTN4xcd0e0nYQcXD2/5Y5zje9Rwn1XyvJ CYKdwW2MhNxLbALe4Q8+A9rgmksr58ExNYgF5yaSU0uuGPX2BEYRKg2uD w==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427627" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427627" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241690" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241690" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 25/41] x86/mm: Introduce MAP_ABOVE4G Date: Sat, 18 Feb 2023 13:14:17 -0800 Message-Id: <20230218211433.26859-26-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: gu8x5th99tbgimpa84nbscfz6nptckmp X-Rspamd-Queue-Id: 82E5E40009 X-HE-Tag: 1676754981-311644 X-HE-Meta: U2FsdGVkX1/goXy8RR/5ZSndArejgu3lAcZoh6nUYDN2ayeo0dUwghpObm0sCjhJLfeaz4f8PiNh6KIt3wGvoQi4ZBUYVSJA6TGalPO15eZQCcQbK12yf2ohS4pdZN4QVzbYQq1ZdFz0ztM0kwcdGENu8bUjDwcNZNH3rRkjxy7qsGgSmgAgSgf1reSLOLM5V+HZCPTPcRvWYFZBsUfn9hTL3RNONijd65Id3m1R3BAesx8o09zZr2yyc+H1TyeIxbyhi8LfBqfmXFwnaw85+W4jf1McI3KPrXr179rPAXIJffSq3B3LVxdsr/88x0u1mqxHOa5UGPap7yr8GBMM2T5609eIANPSv8z7yZ5+Qabr5mTB1iKh/m9K/pIORtitTeEOTK6rjc/Q0eE1NFoV6Jt+lXky3RYXkpTS4rpR/mGYgtDXQSYsv2ik7vFjKuJjTP1FMzsHm719teW7cpjyxLfPqZ08Ei5gfaActSE4yhNIJokRYZgUPZm0QeqqdmZPPmCa8gjEIre3GiKjkOKosWayTF4/n6VtetjS+n+96xsv8Qxcukd1oxJZAYtsGbP1R47g5u+Lwccc0yPu+eX3sqJEeuCECJ4IOL7wDi1wh+S/nn2tXnDK1yLMCoNKdF6ffY5nDh7DGQ8WYwTSt8V+hfVM0C9wCFAlyPxeSzauv9yv7vuIzvikzXcbu8BEFXSyiyhSpxrHn8fyibf19ikXd5PW2DJfUVgZY/BDu9kIn/+R1G8pxxbYPdWOFewAPl8MerS4B7eLd9mis7t2gHvuEBrSc9TihiAb66Q1+I0aeaFCovCxPLWHqq7n9i8dePdKny/zq0sGhDthEgYPuOgyHU+ILTZXc07eH0YKwP2r/Wy+RkdHvTn42JVXn/QTZDf6uV6cT2ddHaADBdg/IQGuc21g7d+bWKY/BoAOS7fNvNX/tcZFGb5//PkcFMvZas6G1ZnATP/h7a74bzF9hZn vPWqsCj5 hyr6DiPx1POCnOqNj7uEmUcnTbywHdLuKvD+j+2CyN/dg44ZkiwNKRh8KwYJzXm9cTX2mBU8CDH17gqgj471TUNUdN59KGsFdb2y6uNxECfUYlQ5xd9rILlZrniYlhcUrX72/mR8NBzUliaA1OAEW6mJPv0cfadUOIFf79s2/7+GmlMYGDnhPW26sqGItThoItuVd7rvTUjUSDfCeFf2AdcEBJ6cQ9df4KgfAayqt2Oy+6dh8yKNYa7sMn7BmyyvxBLGu 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. Tested-by: Pengfei Xu Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- 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 Sat Feb 18 21:14:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145665 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 42994C636CC for ; Sat, 18 Feb 2023 21:16:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 520E6280018; Sat, 18 Feb 2023 16:16:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F841280019; Sat, 18 Feb 2023 16:16:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17583280018; Sat, 18 Feb 2023 16:16:24 -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 F2875280016 for ; Sat, 18 Feb 2023 16:16:23 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B038E1204CA for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) X-FDA: 80481671046.13.5C05A14 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id C084BA0006 for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fEIGT8bF; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754982; 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=XSFlVqZd719tgVIR5uAh62h+RJ0ve8ohUB9lJ1MzRl4=; b=rUgSYRBLWj6rYO9lrL52tchXFIcFQSO5O9Xr4KI+P44QFYJhZiNg96eSGJ+Ea0Jal5p1dG oRfTQff8frs96kc7ii1e91UcokdBEsaZuvlcG5BY1nMlWfwB2cHUfkKerVrmW+mU6BWBRU c5i1UZFrD4rFoAPyzba6BXIBgSXwLr0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fEIGT8bF; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754982; a=rsa-sha256; cv=none; b=VMWobHuWvdtoP9ZaJ5iOxuBvaskCyUs1hGa19YzsJ/FIdC4JRx6ULnMq+0kChAf+CjL6E5 ZUFy7KSw9mJyirbTBYZqWBHDQE9zoE4zBRMhIcbt+kUomH/VhgVWPD3WvdA3iBZDtUHWd+ WgPjddp5Ek57yIFuSdLfazPaDFgUDMQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754981; x=1708290981; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=QFt1nd3j6Yjl0jrlYnYinrJNdu94hS5cjLAeUme4VXo=; b=fEIGT8bFLo5n0viSZ9ndlDFr6O+2myt12JxAYjcv2mO0sK7K8kk7d0iQ oKSv/0qupv3WOz8l12b+yNbkEzTLJxGoWOvtnh5+jCB4+H89IJXbyRBlj uJGOPMwIASEifmeRAN/M4l6tfyniBxHHPRLuSA6va9yBR+450G8rmDG/Y w6aturd4F2xaDEwC6TJJ2ZoBX29HzWi8U359AeqRnusklDC5HeTHQmWVQ td0I2Ge6fItie/AF39zOrJrNTXuzvvIq9GvUPkFuKDFtruGcVehLHqLd2 img8AOfVW3jby8s/j/J0CfIRrHV76LwDWza2QlGv3Y/3vfmOYNXCbpW/T Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427648" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427648" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241695" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241695" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 26/41] mm: Warn on shadow stack memory in wrong vma Date: Sat, 18 Feb 2023 13:14:18 -0800 Message-Id: <20230218211433.26859-27-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: C084BA0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: t7nwfxd67gsuwxc47wbpohzuce7pi1q6 X-HE-Tag: 1676754981-502750 X-HE-Meta: U2FsdGVkX1+nlkOPavksfiR/RjcCYS/EHN5GPeE8Xok60BQZclLmjbUFlOaCCWQ+WSMSb7NkXyY+66f6RqKMxGYaLUJKWtdnjfS+KJZLopYDtpWeB4GubhplSHZxG7ATvVkcCklrcwyATi9MD5We15RXSwJQLNFExs4IXTwAC6Jnjzfj+z0dQtxoIJfuFy65hefOU/GuXO6iO49q4176/efcllCGS3hO8EWLAXX96EM/tV3znYWH2aDESBBrCe7VvcMPU/PEpPyYc6K/tEqCd3RuQJ0nFubRg+9V6nUyqGIA6BNiw2n13JH8xvrDssUe3awGitsOSY+kSVXQ29bt3B0xCCg7yia2rgf83mH8kaRs4tVLcFyn0rnYvC8GhEb8BsvCG5z1xj3P1EGDpFsuuP56Cew8YrQX6gUjZusOc+wAqCqwYe+t2cE03UKtPMc/LThXzHOwLAyau1qHyM8iJKLfWhdvkUkb8kQiD1s+2kF4x2K3P/dZUfFkWJ4mtU8x45izNixf5PHRm9MvbLsYXcsc4ARMjj5q8gENr0LXZEwqJNAZkWfr5/pDJdhDYIJ/Y4FtcxpJhdy89MhM4H1AFAGQRWtHj8oRsqOb6GBQDqu0h3EvbwbvIuPNcW/5z2NUGU5PbCgXWm38Fixn0hJSseqBT/Rco8mSjQZmnesiTwhOZqgLYlp1K+q0x3fn1DyRJN4F/MlddzkalOkHEESi6vP1//7hdWGoLo+FesD9TKi4xEmsTqrcNZr+TBH6ElGhfewzXaeasJEDRCq08lM4CLpyBNnkbZP7yg8XdtrikZsusx8DAuGbReO96W35SeeK92RQ4T6w0568wAG+AlwFXzZyv+SMyY9Wzl2DEIrKp5ZZAwKRJv+U6owSD6jdXasW3jG589CBiJtBc/AlxnI17spzDVh4FgzfQl7fpUJnV9Lck//Um96lKqH/n2fBuaZ6+qaSry5WcoUOykyu0j1 Xvds2tSB 70meCSXitlJoB9PQADHdchXJuZTVZ0L9bJfKQQOweYCM5ccn+5PFQIoXJfmHufgvHRFV/ZPTKjNFdxxdGBvlLKjGNzBaaE0ZeE1vmMhYSEq1FlbWT9MdHGZeLbockXGWNAfH8gUG2GOYoaj0XCmEUa84X/C/5btk/9Ycj7lXf5RyT5Q4lc5oyzt+AJMYRYwPr4YppkJq0sYTJ6+a0zvwatBxa1bbMa7ws/HPIOHk3+8dT10HvXuvkN5cCOsahqT0eJuzQEJcGsy8pg16wJh1UlhJoKX4lW3MLgBYj 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 two arch breakouts arch_check_zapped_pte/pmd(). This will allow shadow stack specific code to be kept in arch/x86. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- v6: - Add arch breakout to remove shstk from core MM code. v5: - Fix typo in commit log v3: - New patch --- arch/x86/include/asm/pgtable.h | 6 ++++++ arch/x86/mm/pgtable.c | 12 ++++++++++++ include/linux/pgtable.h | 14 ++++++++++++++ mm/huge_memory.c | 1 + mm/memory.c | 1 + 5 files changed, 34 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 20d0df494269..f3dc16fc4389 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1687,6 +1687,12 @@ static inline bool arch_has_hw_pte_young(void) return true; } +#define arch_check_zapped_pte arch_check_zapped_pte +void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte); + +#define arch_check_zapped_pmd arch_check_zapped_pmd +void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd); + #ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young static inline bool arch_has_hw_nonleaf_pmd_young(void) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 98856bcc8102..afab0bc7862b 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -906,3 +906,15 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) return pmd; } + +void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte) +{ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pte_shstk(pte)); +} + +void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd) +{ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pmd_shstk(pmd)); +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1159b25b0542..22787c86c8f2 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -291,6 +291,20 @@ static inline bool arch_has_hw_pte_young(void) } #endif +#ifndef arch_check_zapped_pte +static inline void arch_check_zapped_pte(struct vm_area_struct *vma, + pte_t pte) +{ +} +#endif + +#ifndef arch_check_zapped_pmd +static inline void arch_check_zapped_pmd(struct vm_area_struct *vma, + pmd_t pmd) +{ +} +#endif + #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a216129e6a7c..842925f7fa9e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1696,6 +1696,7 @@ 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); + arch_check_zapped_pmd(vma, 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 6ad031d5cfb0..29e8f043b603 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1377,6 +1377,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, continue; ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + arch_check_zapped_pte(vma, ptent); tlb_remove_tlb_entry(tlb, pte, addr); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); From patchwork Sat Feb 18 21:14:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145666 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 A22DBC64ED8 for ; Sat, 18 Feb 2023 21:16:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83058280016; Sat, 18 Feb 2023 16:16:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7452528001A; Sat, 18 Feb 2023 16:16:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52096280016; Sat, 18 Feb 2023 16:16:24 -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 21172280016 for ; Sat, 18 Feb 2023 16:16:24 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E5AB1C0345 for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) X-FDA: 80481671046.09.E633AAC Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id E8339180007 for ; Sat, 18 Feb 2023 21:16:21 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CfovthwK; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754982; 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=Yrz+d/eFxmaL9MnO2KDDZpe/U0WyP+SMDwz8S4U7Yyk=; b=PqT8jHdZ/Qe2+5qWRGZQhQDc2wnQSuStVsbYAVLoOQwvCIggJDdcpkxybKTyjUnw+Hr6Zz p7dhkkeSXsC9TWAP3m7LcW+FvZK4F69LqWZgUCbGJYeW8KqbLGVbHWiCsE4N0vdccu/XCy DMDKfyFPWwwLh2pV75aa/R08nIEX0wE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CfovthwK; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754982; a=rsa-sha256; cv=none; b=NRooq2t+7lyBWn6z3y5BtYaXItl18dmY6Wy/Nsy5ydbstpBTrLL2d7Gn33sy6CR/TImXG4 2Se7LU+zsEoKTp6+6DBVDHjpd6ZVQX9G6+zTaWfBS467AAELQxvl0V97SdKq7y6v52PPlz 5goBVNlI+6O4YqR90FNImDaeARFbf4A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754982; x=1708290982; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=gHMCyuAknB7ri+34Rr5iU3FsNUK61YpioXkhorFA7UU=; b=CfovthwKPoGMAj0BKTcyKsl1AHh0ziLFaZ2psf0jKBFyKE9qFBtCG9zM xqUpHMnBup0YfGGvLlH2DKZ8OsuoKdj0a17Hfpb76B/llKa9F3iJ1Mw35 ugFHPPvniD2an7wNaD6ElJAA7JS9zCjEUeiBLy8UkzDl4IiOdvvZoBfra I7VfWBgh27goaMTpdY1NGFyI2J7B6QQ88G/yeMrwsA5zJOpMZ+bKUBbYR tVQQ6/1lgHGLshpeeBay6l/ESwp7Ff91UKYmVLJ4RVq0tTyL+kordK25L yqOEKb5X7744fdjG6y7wRJ6EiIpNl+b74FWO3l85TyDXnO2X1FSWmvkAA Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427672" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427672" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:18 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241699" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241699" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 27/41] x86/mm: Warn if create Write=0,Dirty=1 with raw prot Date: Sat, 18 Feb 2023 13:14:19 -0800 Message-Id: <20230218211433.26859-28-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: E8339180007 X-Stat-Signature: wt6oeyt3ffzmzaystno79ifz4syn7wh8 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754981-341520 X-HE-Meta: U2FsdGVkX19A1XzAKjqJmFAnL2Xd86wcUq8WKDgJ/T+XrqI77RAgV58BJDhDPtZEQZWuXPKETiLSAIW1+WWJbyna+0LSKblhDeN5fYs/m3ivZqIBc54xugsC5QNg5zIOxMZnXqhHAsrjnDkt2ViHvMzOK+VLMDeLUqrTpiokfeIkm/0UY29xJVcNqYeb2jmuLDm0eDNzIHFsddO5KUDUC8lnHSKvvxq/N4bRUDLkKlB8gA6YDjsomiN6vcSDggnb6obC5tdvoH8CB3yo5tH3/FYv9l2fkvSz891KLUnUTN+ycKl0hGhpiJyiJiBWvdtzGJ4ApQWV0EMrSOxfZq2oBJu/+4PB5zgfDYnfg1Wr4LAmGRo+ce4IJLLzB1BBFaMOk4tNYSBi+EiGXyuwWSnZ++d7hKKcl0g5C+GEJJv8LmYzwdK8GcWqs42GGLVaiiOtgSqzS7caP8wCwGZu5kPG2fn12OWrA5G4tOVk0f28ROH0PQG5lYfKwGsyOfK34IUE11KMCklEnqimIMMRaWapwS4rwgHT9nKRkGzyjRBB6J7Z0eFIkyE7jHEHurV6rQJEct8N+/AupE+21pez1vz8R9kuMPHR/Bm5YqnDM8UzAyYnNJ3o3qsC5hCn8WSi6i6w8HYbTUwjrfmIYYSpZm/tBXnQ7EO1RKHUtgQ+qnP9p5zsDF5YDvnz/SRoDAVn68FyHpXeOgbsOesq7O79z9iwQa1Qd88b9wurTtjHxruHjNkkldncZPvrLb5gVffM3PUPGB0Py0r8eY4qIKKyjChLcWIRn4dA/Xq9QYguURLPI10owLPV+yqmk1EUF4zKSYGRd3yx+rB2ue+OcwGbWQd6OGtasJKAEvrDbO55+Uh6JKgt4eCmHePiuopI/Y/9kKVGIYDerPUrJtxRSUhse3jAug+necL2ZMq8I5Y44mq44D5j+ybld++SoJDeqLQMuywHfdnRSFPmLkjEicYz7nU ugS89Xml apESETbuBi7XxARLhW8Ol/QTMwrQg8P1ZhXtDT+7KrlZUEpbcBkVIUrNWlh6taGhGo6QHuwzvI6twRIEOMNhpFRFpN+OhCUJUbNeuyGMnVrfKDsEJdtX0Kc+VAsMROIPcKcfXAIJvacxszA4xVqvQfkzXQ389cWksJYcj4wPQJyx4TGMmMPNoUCUpKn/PMDNOudjDV/gvsQWjmvA= 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 user shadow stack is use, Write=0,Dirty=1 is treated by the CPU as shadow stack memory. So for shadow stack memory this bit combination is valid, but when Dirty=1,Write=1 (conventionally writable) memory is being write protected, the kernel has been taught to transition the Dirty=1 bit to SavedDirty=1, to avoid inadvertently creating shadow stack memory. It does this inside pte_wrprotect() because it knows the PTE is not intended to be a writable shadow stack entry, it is supposed to be write protected. However, when a PTE is created by a raw prot using mk_pte(), mk_pte() can't know whether to adjust Dirty=1 to SavedDirty=1. It can't distinguish between the caller intending to create a shadow stack PTE or needing the SavedDirty shift. The kernel has been updated to not do this, and so Write=0,Dirty=1 memory should only be created by the pte_mkfoo() helpers. Add a warning to make sure no new mk_pte() start doing this. Tested-by: Pengfei Xu Signed-off-by: Rick Edgecombe --- v6: - New patch (Note, this has already been a useful warning, it caught the newly added set_memory_rox() doing this) --- arch/x86/include/asm/pgtable.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index f3dc16fc4389..db8fe5511c74 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1032,7 +1032,15 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd) * (Currently stuck as a macro because of indirect forward reference * to linux/mm.h:page_to_nid()) */ -#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot)) +#define mk_pte(page, pgprot) \ +({ \ + pgprot_t __pgprot = pgprot; \ + \ + WARN_ON_ONCE(cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && \ + (pgprot_val(__pgprot) & (_PAGE_DIRTY | _PAGE_RW)) == \ + _PAGE_DIRTY); \ + pfn_pte(page_to_pfn(page), __pgprot); \ +}) static inline int pmd_bad(pmd_t pmd) { From patchwork Sat Feb 18 21:14:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB54BC64EC4 for ; Sat, 18 Feb 2023 21:16:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E239D28001A; Sat, 18 Feb 2023 16:16:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D83BF280019; Sat, 18 Feb 2023 16:16:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4A1A28001A; Sat, 18 Feb 2023 16:16:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id AD4C5280019 for ; Sat, 18 Feb 2023 16:16:25 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 87ED08056E for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) X-FDA: 80481671130.27.D4FE380 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 90D9A4000A for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jLl8kmDa; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754983; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=6Uy6jrrgmncVBRF1HaAQVgU/HvAYFBOwLgZGBX8wDiA=; b=AOlhGGzwXu9djd24MVPTwBu4m0IZ7YTTvUhdpy9T8LodWg500HNn1VbDq6FvYpHuvU/8K9 JR5q0ut07EIzmHRCTg+iVbMNQpZNLi53UQ4ooglQbee4kcjKR2PCQEEIZ0xV0BoVKsg4f6 UQ7VnauXqWi4gwZKgY86rIVv/AKqvWk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jLl8kmDa; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754983; a=rsa-sha256; cv=none; b=p8o6krnJ1g9fK/X6mccHN5p+/4Fl+pQnwJXewsNtUVCpUYvpPqm0tWNd8djbryn/wCsj4W 13qvay9PhdH/2DtLB86R1xYTzAf2YYJF0QhJEfeWCR6afweQgRARYH2eAwMt09V2Mu47Mp bcqMzsVtsi1Rz58PqABwCXG/AQ+iJ8U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754983; x=1708290983; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=l9ok0JOA/L86m7W4+eWESC88bM6ZrRTo0gq2MfVIjZo=; b=jLl8kmDaq3Q8kmKJkAJ60ljwfqcedQxS1YPxJ6KbFVZDYWzUY2Bb68hc NTrN6Yy+1ZjRfG85N5McTDUZugPRYGPjnf6AV6/c5bGcBRGHPSn2UGy2a KEejudfYtC3tQnYm3AlQp4j+/AYrIuyJiMesTlQlBtGZQV1hxVilCgeVb qppndangjTvueLA1thcED+AS9FZ8ulVwkqPY7PqKxze9/Wg+KdPpvU1vl ZtOv/Qj1Awm/LB0zvtcRBDSG/+Y3dPVe2SuA+SUjGeRY2cFpJClQVWkcX SegsuCJUuueL5vujkTK7k/gskcbjBuF1Ps41bL8vWBtTCwCqiAGRhig8N Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427693" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427693" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:19 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241705" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241705" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:18 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 28/41] x86: Introduce userspace API for shadow stack Date: Sat, 18 Feb 2023 13:14:20 -0800 Message-Id: <20230218211433.26859-29-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: k97n55dijh5ewyd4kqetynrh3w7h9gd8 X-Rspamd-Queue-Id: 90D9A4000A X-HE-Tag: 1676754983-452934 X-HE-Meta: U2FsdGVkX1/2IZ0x1AgMwuRcOH3xdU1UfwF+60PceV2sDoEdgqjlDrL/xIegw4cdXO2EhyzT1QqFfI2HYn1NnSUZJbCSIvv0u7iJDNewhETp81Ey1fhW8Tny8/rlsX8jQ6DwV5TqGFifEqX9WWbzQenELp4gMTD4y7O6H72JeIF8Ptp9lrrs9/j5qNXY7MnPIcFI/vuQKMynkKcEDJNkOUlussBXEB02CShFEiYCYY7uq2o14+T3b16Hoin4odsUqp5ZHf/UIrRr9/8J/7IJm5AEvOtTjANpgdn7BVn3fkZVRTgy3WU8BZ/URUmABKIiPz/a56OXO+3vDuuskOj5WaoQ9A55Gh0wuEy7WUGvswmvGSUeu7opDHRoSLoMXukhv9uepCiVncIdRYH103O7Uqu/yETRZaGOINTyxfgMEybrCYfQ0S7mplp9lzVbVmreYAJUlL3R1TmpIcYAYURBaVwAFmLhlHEOdbosf02obhn9lpPrryAoUmS/w/XX/O3m6zKhu7GuOwJYORVfACNhCSruWcGL4aksodLsSqV5fIUCblXWR5MeYQvkofmz01wyjof+Lew+luGMazzmis6oRECp0qu/zF6wuEyLePlXNZGbaEu6g/NEh3yPftfRRrfoRsxHzqxpXvtwwNEU1KiOD84WOvqjX4wqT+h163wa33vfoWIKWSpOvfmSEdxF4QocK3Gw2eBUczsDVukNL6EPupa1rw6Yl2oOg76w4ikQVgvJ5+FFBDaE/KINL3PWOC/vTtMClFgHTy2ytOeo1yn9iWOzJhTcfYXRq3NxK5DHepd+W7mLfMi4wD1ONw/0L9h8oZojnsGyE1VeReWeDfVv46BVnsSjxparcD8YqKtp879F0I8q1enG5ju3zj0sFEeB0wpeb1cuTggkR+J0zEpebHZW/PXiJCqOLQGlCZMn3xkqI57pw5KGQnOJ6orwLa3sqbpddlqL+B3fyGnTeRn L15d2/OZ YFnSZbbA5R3Xd/DRTUDh4M5bFMZ0hOLNPK4oEWGLiPy7e11lOrAzWHTWTR7AQsi1/ICXOCyy8JNkdoVDhfYRt2ZR/Rg2tn3iP6JECjtk+bp/3bqIfiHDOyKduNH9exJfLeeIuke7Xx7r6yA9s+8LI4R5Df+YO9jg+jL90JgipFWOkU/ZJrrIpJWRKzXTfHfZVvFIcsobWcq8wde603ngz3nbHzdWuiEcUn8LBnwKgI/h8MhkHNaXgt01iuY389NW4FLwc1toHqvUc8z4gh6kSxPS6S4XBtMTmA9+7 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Kirill A. Shutemov" Add three new arch_prctl() handles: - ARCH_SHSTK_ENABLE/DISABLE enables or disables the specified feature. Returns 0 on success or an error. - ARCH_SHSTK_LOCK prevents future disabling or enabling of the specified feature. Returns 0 on success or an error The features are handled per-thread and inherited over fork(2)/clone(2), but reset on exec(). This is preparation patch. It does not implement any features. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Signed-off-by: Kirill A. Shutemov [tweaked with feedback from tglx] Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe --- v4: - Remove references to CET and replace with shadow stack (Peterz) v3: - Move shstk.c Makefile changes earlier (Kees) - Add #ifdef around features_locked and features (Kees) - Encapsulate features reset earlier in reset_thread_features() so features and features_locked are not referenced in code that would be compiled !CONFIG_X86_USER_SHADOW_STACK. (Kees) - Fix typo in commit log (Kees) - Switch arch_prctl() numbers to avoid conflict with LAM v2: - Only allow one enable/disable per call (tglx) - Return error code like a normal arch_prctl() (Alexander Potapenko) - Make CET only (tglx) --- arch/x86/include/asm/processor.h | 6 +++++ arch/x86/include/asm/shstk.h | 21 +++++++++++++++ arch/x86/include/uapi/asm/prctl.h | 6 +++++ arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/process_64.c | 7 ++++- arch/x86/kernel/shstk.c | 44 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/shstk.h create mode 100644 arch/x86/kernel/shstk.c diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 8d73004e4cac..bd16e012b3e9 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -28,6 +28,7 @@ struct vm86; #include #include #include +#include #include #include @@ -475,6 +476,11 @@ struct thread_struct { */ u32 pkru; +#ifdef CONFIG_X86_USER_SHADOW_STACK + unsigned long features; + unsigned long features_locked; +#endif + /* Floating point and extended processor state */ struct fpu fpu; /* diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h new file mode 100644 index 000000000000..ec753809f074 --- /dev/null +++ b/arch/x86/include/asm/shstk.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SHSTK_H +#define _ASM_X86_SHSTK_H + +#ifndef __ASSEMBLY__ +#include + +struct task_struct; + +#ifdef CONFIG_X86_USER_SHADOW_STACK +long shstk_prctl(struct task_struct *task, int option, unsigned long features); +void reset_thread_features(void); +#else +static inline long shstk_prctl(struct task_struct *task, int option, + unsigned long arg2) { return -EINVAL; } +static inline void reset_thread_features(void) {} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_SHSTK_H */ diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 500b96e71f18..b2b3b7200b2d 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -20,4 +20,10 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +/* Don't use 0x3001-0x3004 because of old glibcs */ + +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_DISABLE 0x5002 +#define ARCH_SHSTK_LOCK 0x5003 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 92446f1dedd7..b366641703e3 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -146,6 +146,8 @@ obj-$(CONFIG_CALL_THUNKS) += callthunks.o obj-$(CONFIG_X86_CET) += cet.o +obj-$(CONFIG_X86_USER_SHADOW_STACK) += shstk.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 4e34b3b68ebd..71094c8a305f 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -514,6 +514,8 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, load_gs_index(__USER_DS); } + reset_thread_features(); + loadsegment(fs, 0); loadsegment(es, _ds); loadsegment(ds, _ds); @@ -830,7 +832,10 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_MAP_VDSO_64: return prctl_map_vdso(&vdso_image_64, arg2); #endif - + case ARCH_SHSTK_ENABLE: + case ARCH_SHSTK_DISABLE: + case ARCH_SHSTK_LOCK: + return shstk_prctl(task, option, arg2); default: ret = -EINVAL; break; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c new file mode 100644 index 000000000000..41ed6552e0a5 --- /dev/null +++ b/arch/x86/kernel/shstk.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * shstk.c - Intel shadow stack support + * + * Copyright (c) 2021, Intel Corporation. + * Yu-cheng Yu + */ + +#include +#include +#include + +void reset_thread_features(void) +{ + current->thread.features = 0; + current->thread.features_locked = 0; +} + +long shstk_prctl(struct task_struct *task, int option, unsigned long features) +{ + if (option == ARCH_SHSTK_LOCK) { + task->thread.features_locked |= features; + return 0; + } + + /* Don't allow via ptrace */ + if (task != current) + return -EINVAL; + + /* Do not allow to change locked features */ + if (features & task->thread.features_locked) + return -EPERM; + + /* Only support enabling/disabling one feature at a time. */ + if (hweight_long(features) > 1) + return -EINVAL; + + if (option == ARCH_SHSTK_DISABLE) { + return -EINVAL; + } + + /* Handle ARCH_SHSTK_ENABLE */ + return -EINVAL; +} From patchwork Sat Feb 18 21:14:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145668 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 2927CC6379F for ; Sat, 18 Feb 2023 21:16:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45D1F28001B; Sat, 18 Feb 2023 16:16:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 436DD280019; Sat, 18 Feb 2023 16:16:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D9B428001C; Sat, 18 Feb 2023 16:16:26 -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 EED2428001B for ; Sat, 18 Feb 2023 16:16:25 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C48FAAAD71 for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) X-FDA: 80481671130.20.6C2DE42 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id DE2B5A0006 for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Av3BINPN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754984; 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=iNUnkPacdMKPP0JY+JfK1r5b2aug/1vNrSgdx7qF/YQ=; b=sO4fKytjPMoW8QGPaVO1l9646k7NZo7Q1Cx1s+cy6X7xN2LMutE9WTc1xazxky+85DSWUh w3x4f3Qqw1vh3aer2eDcxwG0zAsJTO1mYdvUU25ULy/dMnydnqyJueyWWzImj39RYe+ega pyrWC+MFqkEtHBYhEPFoYxtkynFDte0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Av3BINPN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754984; a=rsa-sha256; cv=none; b=JmODL7WV4m1+h8e2g0e+zhlUMZCjmfLFVnu7/eLvSyvpacfpqPX39mMgtr/Wq8TGAAGJ5t ZXPJudEtsrnrizvyqztV/Ybu8dtsWT+k+zw5SMJfJaZKvllyqaejBSzpJE7XSYz3zJcslP BTzu+KaXUkEIDxLNho762WKL3NnWKoI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754984; x=1708290984; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Fx30aEuXtnObd4pLstduxowlzqHzXgwWfGXPPH6LT+k=; b=Av3BINPNbCvB9yBGUThKyb3e/QHy8n+jraITNweDGauRVLrz9gxJv9qU w5IQZJap+nd4jknpIhaiUIqWh0kr3lOM8izqdNkX8OwZK2Bg5UrCr4JpY j4xFZDjsqHvB3p6IWcjB+gabwYQ71P8xCNHD16zxRmKw6InJ20fNIHbA1 GYpyYgNpO7GzssOGEFg5WGcWsIm97chTHmwBlMfLq4QjEEagg5t4ptMLD nGEch7xgbjU6Vko+OW80Undu+kA0UgPcqkuBB7k2cEVS1RxeqtwawbB6B AEwSl24qBjQdUzt6sbkfDLXlc+oigGvuhFB5euWN8d/Hm0/wCaSb0NY2i g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427719" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427719" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:20 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241712" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241712" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 29/41] x86/shstk: Add user-mode shadow stack support Date: Sat, 18 Feb 2023 13:14:21 -0800 Message-Id: <20230218211433.26859-30-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: DE2B5A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 1kesn378p7bkxpyrb9zw7mugisfnjyko X-HE-Tag: 1676754983-258399 X-HE-Meta: U2FsdGVkX1+5NPvZ5+IGbp4K/LPl9uGDI3Ahbn47lbJYlJxT+YpIUlfD6aCKMnjj2roLn0zrCspevwFf6bESHpzdNWo9svFTioEokrRr1cIJ49Mwm0dSJhAhTp/QLhZDW5RHPWS1mf3a7+lVOjAd3J13a/eSASju1FqipJGs4IWuEJdKwAqnVXy+P+sT/Sfr0xEhjuUzCTucjg2AwfKr3cV/OriW4mSvE00TJhgFtGmIGFDqZy3S6MrNhAyQYabCDA/5QYfCUew/oCb4dt2IX8PqndNl+3J/7n32P72uqIvQSl05AEpbmKjPA2S/4SrtaAxIeey9Ja1mCup62Bkbxukbu0iL5/m0ka1tvWMvzg26WLD/q6TTDPr82eij/KR1lm+vAZR0tyoGEfXwDskFR7EMLgqtiVfXvRmzDkC0t/9HF8jZdkadtuAft0lbqnzW9bFx92XddW0i+DXBhdIDFLO0R3rxFaUXgcWZBCyz97sasr93/osdC+5gwG+kKBI/fp8nmi+VKTd3G+aei53znDVv9f5qR4ynXRe9xRHjaPOjBy6yAFf1eDjvJImB4bcdeGosu1cXFD2DbZCHzxUxTmd5ge/N89mLgrrmnJfyg3PFsa+LEbmps37PkiBne6LscnHZ3/LL+3+JGeWTqzpyB4DUbwccGIcSzUT4qvs8GGdHNt1mhqYB1tTRctYhqFHyWzVLqY02iy1B7lFspycKY0v89/VwgDfdcuBcyLYWhxKzwdbZSfkSurQNoTANfB/+G9AGh8vl2atme8AdvWPYlg66xfXo6FulrOU4e2JfCII+BKzHlZ/iSMLWaoDei3SwKw4qvCNfq9vBKPKz/O6iO4Ra1/N/8Bn5ZeGomW9TB48hYKuMfMjqoYg+ibtF60nHkrKo5gMxzwjG8j03NnqPFegio5uftRczKrm5owpXfi3Im1mLWxJ2haH+j3kBJlAHHccPMabkFjPgz61jGTT qEjI8FQO ns5gb7gqfIRxBvjzTDVu71Z/qkFIqutjeZZw7m9kDGUtST9klFPqdQrPbHz2T9khxoo7yBfR4iJHHUNVkCnxPcXM3rdrGKpvuD8v1KJy0OccFAW5j3bkZ56X29v1XoYONC7JyR32YnOU5zuZLSETsFj50+y6z0uzCw1j8Xdb5sZwUEYsGxvI80IbEzWJNib9ceKIQld0CHSVdPH+uQEG+gMOSlTYFqpsf00ziAh5ECnYLVFI/cevsLA2Pt1qVrKMCXnrDQf6BDk2LXqAXx35r7BjWFFSOYt7Iu+kk 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 Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: 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 unnecessary 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 | 145 ++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index bd16e012b3e9..ff98cd6d5af2 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 ec753809f074..2b1f7c9b9995 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 arg2) { 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..3cb85224d856 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -8,14 +8,159 @@ #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 Sat Feb 18 21:14:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145669 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 7C008C636CC for ; Sat, 18 Feb 2023 21:16:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9667028001C; Sat, 18 Feb 2023 16:16:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91768280019; Sat, 18 Feb 2023 16:16:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CB0628001D; Sat, 18 Feb 2023 16:16: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 4927F28001C for ; Sat, 18 Feb 2023 16:16:26 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1FECE1603E7 for ; Sat, 18 Feb 2023 21:16:26 +0000 (UTC) X-FDA: 80481671172.21.456F512 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 265AA180004 for ; Sat, 18 Feb 2023 21:16:23 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HbsMbndS; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754984; 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=4FdzmviWhVlzkDCVV34p0Mqrc18vMCq1OcpDoSZBEok=; b=PJt7v9AUGPRXLyflWIMFWkQql7Ctd8J3uSc7VwDF4L7hXUWUSowWkjF0Jj6/JxzwzosTTV xVnF1+bes/ZrBau9wRJ+BhGTLXn6pCRb4XYiUbshHFgYhFUnViIIpJ2VBvb3vZ3HDccgxo Drj0HW4H4btuMfPbLM7eok7ZOnS0O6c= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HbsMbndS; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754984; a=rsa-sha256; cv=none; b=VV0PHKpb4nFyMLQ6YKim4kaLb8hxq7IOABTY00zwCz+Z1JYsIaCyLxA6EeMLKtKyZfu/R6 Ga4Z8OwpY3gC93Q93cV0Cfxkli+dCjIrY47RKgP+lmkSkyCK4KqwVCH+4a7U0EspitExYf /lxEE0RlQQrFC8PMyard6LtYoj19fS4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754984; x=1708290984; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=OsbwFYkuuguYFFkAeyX8/HXEtDCnfV4hcbsK+U1ySjY=; b=HbsMbndSBrUuek2C8Y0fzawAxDre403T+P8AbfbK57FKbgfUcyI8N1v3 WvMWpkra2nOUtnYCU6tRzDNdmdOig9MaAmAE6LspG+umNmWMzwIwPu9C0 IHE8bfglNivQHJY5MlEoeT+5KGOWLT4z1e3kIhjOCBzf5u/6ya8KJ7kgZ SxL6xCLqbF3G0YVDhzdT80MBKit5cxnsXIwVplm1Ff2MR3/NYaCEAKgvH tnjlQ7NWfLXuVjZ1hGWazlH2LJTgAUOffbJUzaCtY9kxUK6uOaLdeDUbS u6Xwnc8N4urMDH/NPcLGBLQx/ay09fYEh29MrzXdAZXNcKSciR6tXf1+H Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427740" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427740" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:21 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241717" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241717" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 30/41] x86/shstk: Handle thread shadow stack Date: Sat, 18 Feb 2023 13:14:22 -0800 Message-Id: <20230218211433.26859-31-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 265AA180004 X-Stat-Signature: gko97oi4j835c1c7o9ny38sbxzr1xj6h X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754983-160180 X-HE-Meta: U2FsdGVkX1/gPl/XtXZUdWrOq86s8HfrwbVkMG168yb0AWwBZJZejMgCBX825jgpaN/s25nBiIZv9x2F7NKusXIRs3YBg/t9DOHh2CZzjlKhvIQF8W9wVl94wcUwlOOwjnRneq8mmIQuyeHFrXRuaf8DOoT4WA2BXY0vJmdOlTvMQAG0KqvLrv4XSJqLRr/YmwxJGd+IQo6nQXNPK1QUIJzEGYh8P3FM+ewl5RliB9tl09YmR+UzF8KgUPRLdWOa5rV3ntkva+GIQl1Cc+LhKKAV7OZQ2xg0OIgNcYKjXe86bhG0D8aPTQ5ujK8rDVYgKPwYr8lu2seUZtX9b2CNeS9uQ4wiYegYB4xxSjOdzkrtmCuP/37Fbpik32JVdGLhTa0nhbk2JeVQtxB6yZz414DFMFG7+CA6PvhJYI9i2E2vMmr6LXE2szE+Tgnp8UsWh8VfmmRhLqskMN8I8RVYL7Hq41vMzQ7Y3m4PbQ9BomwyrLGolq4IBkA0I/el2pzmbx+ykxVNnf8FGjJgLovw8ii4HmeOmEWltcomPJT+JgvLRFH0ccKDISWwTNvpzP0/sRGCeWY29kVpsVIGO7ucS3hQYvIg1KNx4tP/xu8Bcc62nBrw0Gr9uz/EGF145zt+Bct5TapTUxFbfm0ctOg/aHX8pXOhGzjtePVEcxDzqege3UEuifKDvbGDG04JCsCy5zSyOFdug0Kt0iRsWHIUBmUFp0qRDLrghOIP4rtVQWF2B0r041zvC9PPXHzvkTcJ/4VC98BK+0K9CC1KyWqrytIaqRgbB3rii4v48Xrd3rpNUDbBEZ9ODrtJVm52WJ8MJ9pTU7u64IbKTwH35ky/TashcdUTwFIIX4V4EetJqFDp4OXBFH6KkcB1hghGuWzyrTq7YgJlxE9bsvjmX2iPGriaDW0UvbQTctArQP6jOU3V+WhFKLe3G6r9oIhFvzHbwQdcER8wZbwfFvAVb3F dqL8HzOU GspjysLNUli0bypxt3gmDilcbXg3DDjkLm4Ti7si5Tg/pzmFk0UI1SQRRqDXMi9PzSJvH/FJo2MLetLXgf8vLuadE0SY8UkKF9HH9dm2SQRBYWWDQcC2bOIoT1uU+fGoohP4pSeBm+/w0hCNwlpfRodduUteDejN1uycWeWmRWa7yoERctoXEiCb7yVkuF1ZZaPECqWMaxtA6QNMmx7KqpAY9/jmxAb/QTV+7DM0KFpwfZwiUGEe9bJ49xvn1AanFO9Khak94Qnl4H4xGRL2JOWl66eC5qncXp9Gb 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 centralized 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 | 43 ++++++++++++++++++++++++++++-- 6 files changed, 109 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/fpu/sched.h b/arch/x86/include/asm/fpu/sched.h index c2d6cd78ed0c..3c2903bbb456 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 2b1f7c9b9995..1399f4df098b 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 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, + 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 f851558b673f..bc3de4aeb661 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 3cb85224d856..1d30295e0066 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,39 @@ 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 +167,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 Sat Feb 18 21:14:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145670 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 E95CFC61DA4 for ; Sat, 18 Feb 2023 21:16:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3E867280020; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3731228001E; Sat, 18 Feb 2023 16:16:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C63628001D; Sat, 18 Feb 2023 16:16: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 0616D280019 for ; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D5E44120352 for ; Sat, 18 Feb 2023 21:16:27 +0000 (UTC) X-FDA: 80481671214.30.B7C8218 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id EC5D74000A for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GcdArNy0; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754986; 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=PzXH/uJcd68ZZoNWbbCBuIigR/v2lSDbJ+EW86dLo/w=; b=VPcygDitT8CfDMPLqS+iNYRG67ri14Qvb7qAn5zCKc+z4tIIhEoA+3kzFx2JCQwDayfliG Uq0+70PwB4vKRzTs/JraqMwNV11xgIB6MRnhr33pjZa8nGmkjBzPXpGVEMUPvv6P1vndCm MYJtEykeF4uiWxJCQITVdwVTHT4ejA8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GcdArNy0; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754986; a=rsa-sha256; cv=none; b=CjDJB3YwtDDX8gDwGkM3eceXrLMg9mmk2Q2xLA5+CYDnlTLGS1twK5knq8/ty99DB7DY9u LXsf6BE/6Y41CrXZhy3D2wA5UblDyWuFoD7PodxiQGG533Ec42mvYiH5mJ5d4kGytRr9cV uBqbavKEc+B8tXfxMucPw3Zpfrlh3sE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754986; x=1708290986; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=qEjm0xHlFad5ywdqKhdVQEfywUJWji/1KowdSbu4Qj8=; b=GcdArNy0+fUq9Oojx4l3mZT7XTdd+bz4a1EbYQIv4xhWbolCabb49nWA 4cYtk5+UeIY/L1nRsRFETCEnKbHGKQU+rvskA79TTrmb5z/XuS1df65X/ f0fmTbEHlI3NekfvEAnJ6xq2sfgQK55H5fkxrgaDrnabwu+VRQp7Qqoei UkhccTV9i/ghYlqlXd5YJG/vUAl4Oblz/jqnddD0bBJ81spiG00HwJdQx Z/6U+X5iT2ehYBDasZwLyyjPO7Qeg/wUvVK3bixNsKWrr8nxM0VrD3MTB UNGRmemzlpmTfRFYzgQdAUmpMcNYXYI0iDR53ycGfUxXJxBC2o8U5OxS/ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427763" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427763" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:22 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241723" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241723" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:21 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 31/41] x86/shstk: Introduce routines modifying shstk Date: Sat, 18 Feb 2023 13:14:23 -0800 Message-Id: <20230218211433.26859-32-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: zfcro57neruobx53bgpft9xobxet76zt X-Rspamd-Queue-Id: EC5D74000A X-HE-Tag: 1676754985-840425 X-HE-Meta: U2FsdGVkX180q/cH/ojfDRvlyUe//VcvORXd6Uy9LaHtJZ4kexdVi6yoQAH11iiu6rqq3lPJKKPy3NS5IiseXhs/ffRVj1pRzlucFvykLkFnJiHF93o4mtzMBai04Q++46Pp9HIfcmkqgjL+qAGPqDbtS8jph9ZABMou8VVgs+/n9ryjF5KWn7Yv+1Go/JrmnpSiA1YjPq8bIURhv6SuWyH18McbbWMd2Ftkaws+rRFknnpKiw42MDgB5dvx6s6FpIACjkvEP+JQIjFklvO1T7HHRiotrGG2uZrTXaspierkgN4JzL5FxVeKgAs+iST8jA4TUqwkqJF+WoxWXlPVZTgZgdvvojAJRi6I9iRN5+5i9R3HOe1xh/trIwezaLrAwMAWAKtyyL5/3XITCJdb06nujXIpPXIjMDlcb9+uIaxAsYTxVj8pERKRQRQ//DVP2LIGzsV58SJ9wixiZHAb8iM/lY2FM6Ri5t7R01yNudCPlSZ33j2H2cvJOAmgcC/fKPnqD0euEX0if4//Ua3Ll4moKcS2QvnIjtSY0majwD331+/7Hph14/19Ol6DfoILvWQX+SBfjuz+iYDUHILWDuPAnlLLFDOLE2rVxLbKkn6ZIPHdvVS4esXkMWx4ZZU4m1PWhqKxHZlbV/T2TN2dIetre5BnyxwIQaZfu6mjWymzK8b4yk55BLZcUW8zfuTc3w/prxTi38oZXbyNPy0Zwl4RUACyQCvf7p7MhCXHdSv43gLfcT/hWN5K86f2m+m4vMErSvlqTJrhrTV7G7cVrTR386FR2m7YJqkYShODw+yBpqu9486EGS7UgdQLlBy6o7PzGCSq6MYeOMrl3JC5HzalwzxEpYDiCUOoxy27K3jkrjMGmh5y5EdztrPpBwhqipZGp7Kn7X7wP8IQdmvqZFgeL2aOYxM1dhrOKUvX9Seg2SZb56JyNzYyRcvDsYqkSjP6nZCMO8m9xWEqNL8 txCgw1Qj OBgZfj3KN7WnmfCQu7Ne8xvQ84N9qzKnSDzcq5JkFrwpJ6ZogU/YaZSLw2aa+13BOSQVFBt+EHDCv/JmQJ9epwpnqJw3rrEZbNxLrS3n7avmg5kDGaAjhELjALr3YeL+MCwzpmcq2cp1a5p2fmhJqPu0Bh4MbHgL1a+9QwoSpSRbiR+AAHCRnrvU/JeAJgkwzxjaK9h7cu02+WZ9WIHYMsvxpTEIaSDjK1ssGZHt+i7gqarEBAvIoiDKwmM9mLuNks+bcUJZdPcVl8nrSIIt2Euv7RwrC9A6j4Rel 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 Reviewed-by: Kees Cook 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 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 1d30295e0066..13c02747386f 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; @@ -159,6 +190,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 Sat Feb 18 21:14:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145671 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 62766C64EC4 for ; Sat, 18 Feb 2023 21:16:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7466628001D; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F152280019; Sat, 18 Feb 2023 16:16:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E53528001D; Sat, 18 Feb 2023 16:16:28 -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 1E1B6280019 for ; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EF415C0119 for ; Sat, 18 Feb 2023 21:16:27 +0000 (UTC) X-FDA: 80481671214.05.68986B3 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 14800A0006 for ; Sat, 18 Feb 2023 21:16:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WVIKlV8y; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754986; 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=x2X6jm5AWL0uwXSwQo3TYAY/Az0Bw0TKIIE1nbqjcyo=; b=FcbIOZMlII8jtY0ffQI76INjUgstqpJIbF5fyhLyu7taKxPZ7CmO8SD+p4gng20aeGy9P5 Q5ltoxXHd6n+q47xy/amif5YgLBUZ+95gq40DFnGAJOK0b0ouhuh61aChXE66y1156rI0F SCAsMmfUgBWIm2ZUH+hw/87dgJjUNjQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WVIKlV8y; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754986; a=rsa-sha256; cv=none; b=cNiE5uNvryWZeB8b0rsYIljLGwzyNBIhaO5cKm3pzYe/AGCaQFD6UB7CcW1xWXj3TKlgPH O30M3p0g5+Zdmv/ep1BLs8F/BafAddJucUUaS1UaBnF0p1kBPPUYapw1HBFkKEij8ES4hY p3XgXK3vMMHjFhfluDvCkbAh12Icbug= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754986; x=1708290986; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Ya96I/DHdCSLqRTBep1neQnE1dJy17ENvxuDYSoNu7M=; b=WVIKlV8ywtI3j1v6JnLp2Egf6jXA76B4TVVv0/4GwD4ZdGCye0zcDywx W6iGZzeFyhmPJ8DLdOz/xE/CEdCKIcf5llkg21qjEU9H6d9w27T35+s2n DZZnXZ3+XvgcxfqWAxx/mVEvgh+ZhcLa+19bEpuRSx8EWxFRpkntRXeqt BGRKVR1GBrN+cqMGzfQh/PjLWGbXNraC4UOGVzh3Xg3NbTDykoCp2a9eh Vu7oaDShSj6Zqb9Ka2BbAoU1R0YKiPZi5xXk3x1SSnMbHJ1ta31cS6T+9 ue0RPnB/SqOqGn+8UZRnyxnBwfxXUvxckbJp1TJgDpt1tS6VM9R3iKnlL A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427785" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427785" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241727" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241727" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 32/41] x86/shstk: Handle signals for shadow stack Date: Sat, 18 Feb 2023 13:14:24 -0800 Message-Id: <20230218211433.26859-33-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 14800A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: cukcfsxmar4btzktj3gmfg1anmc3jxgk X-HE-Tag: 1676754985-66805 X-HE-Meta: U2FsdGVkX1/7ISufFLvmEkor8Lt2InGEwmTK5uk8cgekuj+vAFwzapcw0yBOwgJO0peaP6TfcFPpYfwXhD+UPA/McX1fuoHsVHI0wTrKo9w/sH8KLKXbVlvfa4o7KjtAjIvuLrhZ5Qp7gir/9y8AstDjcFk4y9dJCZs994PGVfLTsoA1RFZBClSaM5c2KDEPumgTve8cxKtGqSWj9iUUYbzT0UAQIRJun5QKTspvNjfS6Tij0vw0TjDUljgt2s5J9kDAtcJ9vMCiii1MSIQVrHF+zgNiUk2oLJa4JVm9SsL+xO/WhwZe++UjfXnBGXZzL/aKaB4S8aqZdtIxe3v+c8P+sm9cuts4irvDbCMAqaF27rOJWPA4fY9GglPull5HfxoN4pa5kY+tvCjcpMgXaMGGYPKZK/XENXLgosE3CDiphAZnOOjrifp22b3TIHg/7qo7Vm4jAjMmABWUF2DkeHp4nTPW11PU4FutF5vmm8ap4mH0ot3kO/syXFksZX1HJWZaQ5jq9z87BlKtrL8cE0ei1c16AeJd7xQWXa/3zyx6enKGbx77Z2Ln64663ElYXedTZKr2q36Dl24+jERdIXkQzJuNBhQAnY6EfYeJfGNek6QDG08DRV8iqxABpX9yUmzoTLVx8oMjc5VRUlb0KPKh3u/Vae94FqeFgYlS9ySt7luNscWdjP6cYsiuyg3Bzg6jV32kEVofstUb+eFxoE+5WXQ32HYY8EP9qGe0H8ow6ENHDLdImNtf8j+vS9lq4RbAnKuRAF3XFNM6opmGRuSaloXzM/0DI+Gj0JvOdVJmEfBRFupnh2xqnNgzsoe800oAI+5L2Vfhr4g2sEoFvOX/e7gF57eiF3Gyjrun7xJzv+0/pP4ySTzWeKWp5QQ7F7/wGkBEcR5ge40L2OIFP8bjjx55pxrDqbHoLQujoFRH/Um8AB/1rxCcL+Ig2BR+M3xNPs4+SMCeqv06gB+ ncbdnm3n AOJAleeBP2PWwsmwdEieJKmvWwrIQRb4DH+pIi+xhTeD7TBjaWO7Ih9+nSP3gudev4gYlhHJUGFeo0SG+AHwN2ghM+uyBst8LkqqzhaAOmN1x456iTqamUZ55O1DMxYvjn6Xe8VU+o80BRoc7ChmBHIhjOC/s0+DmLEfJITqQJhlXFAhY7OB2Gea3gc+Nd36oVkmTbN8EfhNS1srOubUd15UCOts2PUpQFkp1lBBVAjFtD0OfMcYdzMRAzUTzscdSdxa+bmPvgKKdZGuTQQwedKHpGuttkqoFXqgJYZ9UIUL33qQ3G+sdJmVLTbBORJXuq/sCFq9m/iOpuYTxf3iWGLSCbN8AKOP41Ryn/ijz87Y5HZ0dk2SOhtXVwZaTMQsXv2QB3MDBzy2xmosxP1EEP8WVOL4lDUniCnt/Y1Tl7jb00ovThsS+2sJGJBdLW1R5Jtj6XffZR3JRSBY= 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 1399f4df098b..acee68d30a07 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 arg2) { 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 13c02747386f..40f0a55762a9 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -232,6 +232,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 Sat Feb 18 21:14:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145672 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 DDC05C61DA4 for ; Sat, 18 Feb 2023 21:16:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E962528001E; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E45E9280019; Sat, 18 Feb 2023 16:16:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C970528001E; Sat, 18 Feb 2023 16:16:28 -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 B85D1280019 for ; Sat, 18 Feb 2023 16:16:28 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 99648A0425 for ; Sat, 18 Feb 2023 21:16:28 +0000 (UTC) X-FDA: 80481671256.25.6BC7CB3 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id A9326180012 for ; Sat, 18 Feb 2023 21:16:26 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DWz3FURB; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754986; 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=sPjgtO7gjiSisrQloQhJv/VllXOHJMFZ+fwjEtKpkI4=; b=BlZK2HCU+MGOoGCVxh+N1wveqgFcpKiwWAQaYLRay/NsgO/gvUhBTzA1N7qKcfEb11ius0 FtXbAElNqRrxc8YHwze4DNJD682rg30ZSFs0GBawr3w3zNJCneGIxQ3p0EtdXm6jcfrmS9 0TLIp/Z+vvrcyNpGMFgwztuvw0d18Ng= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DWz3FURB; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754987; a=rsa-sha256; cv=none; b=P4jcbu4DYw9I50wtZhYxwr7+fw64RNQFBkWR8SZ7FNrTwkEOk5NH98Oxh7A0TVQHfcgG6T 8eTTMHExErUqLRqAef5eNtpKWTH987S/o4+UNDB7ZRRfyDi9AFergLqXfyzK8A7LfZLECY +vP6Z/ZvaYn4olkPn51B2RIkFQx2Fmg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754986; x=1708290986; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=id0+LKgwN1/MC0Jmt1pcpgkgYdQwQEyfmLNN2VWh7u4=; b=DWz3FURBjtdmeJzaUX+pYNjF0qfsyELeNd3M6506O4hvt60/7vhIhrBu waMPrPBbP2JhNaGY7o5vixC71i2FiWUb+ZpeGKOaO7mvv9SoWATxkNy4E iqgx22Xzg6KOniHjV0w3fEGpZaJRPhF+VbLwnypqRjgPsOKUHQXs4LTAi MPqiczo2Pff2pYEG0b95ADZZGonWnl65Lv1GI9zlWdOqC3IAbYERjKGmX jsYHeKcjmX4y+FcSyNfip+jHy/zRP3JlpkTKE7m85wU/mC08sx/9cAYex A3tPtd2AT6p60Bv8ZJdZ5gnUTKOePTWxcwQlCd72+g1P+vp800ADKrSQo g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427808" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427808" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:24 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241732" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241732" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 33/41] x86/shstk: Introduce map_shadow_stack syscall Date: Sat, 18 Feb 2023 13:14:25 -0800 Message-Id: <20230218211433.26859-34-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: A9326180012 X-Stat-Signature: c1ue4ppahi79bw6w5cnq1rpy81mh773r X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754986-797599 X-HE-Meta: U2FsdGVkX1/w7qhawnv2DL+svxjG1z0pojfCsP7sYgYIvuE2d/u8e3sDCu0I3nTDQf2Q/I5MfVQ8gjJSBrLdY8NFjsxfH0Wn4d+/sS+Oy8YSANxyLINy9bNBVNRrKGJNuIEcZujtQiqGGAFgJdGuK26N9Dzl0OM+iN5EoTi6uXVsjSeTWkeDmHze17xt1KpE+Mv9+SK59l0YLeWoizEYCenv1mGCpoNKoyNueCqVaD2hc68kYMEvBG7Ue37wKAIl5aWbzrRCvMTIjd1YBJeck4KcAKZFSp+bbq1BwvKKi+PmpDOj9x5nvqRdbpV89mtMNJ8DyaCl3h+mlZN6PRCV/o/i3rJDlnweTkz72I/eLcUBpwEPfBHDVUFKhcR7n5y6irzVtJzeypkL6lVlcFvzCdp9j4x/qX1A2EdGyRClmgAt42NX8ts0WvqCEOPtyN2SLVA6/nikW/GFRdnHEbCsaNjhB5tPo6eq0ph0XquZP/N0dQ7YSwPGiwX7pxnGlzXZXN5WrX2KHzZ9kCe7XvGkweOpWxW4iMjMr5gGPQdDSwdNemEs/1c0WXrqwFLZ00L5jl9Kzpn8Yr+h51QK1anvMMwJTSWAgy+Oap5ni2Q2ouXiYY2rLKevV3B4e3jBMrD7sBrPdhNOF34AVVlQGmfj/FNS/eCspXyc31mNmQjofsh/pn+aRL0KT+B0Q7cr7Cm7XazWf3QAZO6B8u7puYSuTdmTC8lnlApNolLvDxI7U8nzozPkwtQFVEJRgrtRhyJrQ2yGYCn0QeLZ4cBJldgU9q92OCnkmnECnSWd00kjr/9GfSHhiavRhMGxjO+XyyRwp6dK/38YuZXBYi7ov+4N+Gwz1fQYGzHu+oeFcjXe5aU9MG/xzVq8no/RULx7TX8JjHD8cTOhgi5l64aCU1M7R9CHUoXJGDTG6eOn+5mX3a21wky5zSR/8lhGhYGYWCHgxrVP0D7TqlCVJcv+8Pb KusxlKWA TozHpD9aIp/8BFNhMA7eVrSFoZK/yZy4/CeitPRqF5ZMEfGbC7Zq82rfSKkHX4OWFs7vYqOnbL99f3atq0UkQ0F2ECEdjD4IrdiS4ekmn8/DlZCcXECsPmpe+c9eCbPNKiKwpsbVk6LVi8SyyBabDwByKgTzU/IR8034KaqOKcu21iUGuQic3aYyj8Z+jmY4eSuUiX1x0LX4/ODet4I43yTtKENapTFvJ3yhbY65fD0SLxPmnLvWFkMHZZc514QXWo1vhcyjwyGVnMqEG/SSPKpA+1j6kxx/0SjuLPusLfvrEa8fzQP41dh5ivlnw6tz1/ahs/mcXNdfDmSrDWLLnmryZnw== 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 Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- 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 40f0a55762a9..0a3decab70ee 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); @@ -178,7 +191,7 @@ int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, return 0; 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); @@ -371,6 +384,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 Sat Feb 18 21:14:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145673 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 4CDA6C636CC for ; Sat, 18 Feb 2023 21:17:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B338280021; Sat, 18 Feb 2023 16:16:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 98A70280022; Sat, 18 Feb 2023 16:16:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80407280021; Sat, 18 Feb 2023 16:16:30 -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 713BD280019 for ; Sat, 18 Feb 2023 16:16:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 48043AAD75 for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) X-FDA: 80481671340.14.2361DD0 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 5786440009 for ; Sat, 18 Feb 2023 21:16:28 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=g3CJyg7J; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754988; 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=riAGnKk0Xm3+kCwcX8Y629XRcWsNr1jeCHfuvNkZ9dw=; b=PC+r53/OuOB17V2rLV3bJaTHIW2gxpYwUH3d4EQA8ft5fSs9z7n5WoNWo2leYaLiENzQkP YFzSzT05IScUpq5jKG2uhzsm9qxeZjVNkZB74Obmurk7HaNwAlYIHY0GW1ZilxxuaeJlkg siz/wUTihYbOePnm/lBgwdZXEa3WcyQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=g3CJyg7J; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754988; a=rsa-sha256; cv=none; b=weFt2IUw8aE7Ld/odPYlumWfat9k9IWSXSHKhA6pJqXOCPiJY0LwDOjUaX6YKJfSz374AD 8/APv6wY7837woTOakOWpOjeS/Ixtv1E5MZhyw53N6DKx+xeK8MRiWum+1c0pDHcS8wT+T cQmn4tWKzLKEZdLQ+YdfJkh1qd8O/S8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754988; x=1708290988; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=qXZkRuAhvszhsNxk9zQmBL4ipRAOhXZVRz3O0rxB2RI=; b=g3CJyg7JjNZDH0mBwgMb4RT1gv14ZVq1lq4LbtmMVxkJA3sOsujaE25P 7asp/2cnPPcJTVJGXMGqmBiDhEgTBkr03DoFEARKQgSTzx3CP8jrUnlZq 7KR2UXdUQvFq4Hcffqdu2/TwG74Sn2wNVs6Ym3YX4puqLubSmYnNbup7x b7iv4DkOAhPm076/7h8YRHV+ooRmd7kbUv06CnNLq12tlz+3f8w41fPs1 90wcDX4qcYPcf5soxbnnSItju9WY/MXi6kYUQFlaBK0Urki6rxyQZ68m4 bbIFpRMMzOwenOmI0MK9a6imAX5TRd9SuRF+NvwShMQYo/l56D1D2+Lv6 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427831" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427831" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:25 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241735" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241735" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 34/41] x86/shstk: Support WRSS for userspace Date: Sat, 18 Feb 2023 13:14:26 -0800 Message-Id: <20230218211433.26859-35-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: kyeqiq7uw6e9fwwb4qip7p8d4xuf5xgb X-Rspamd-Queue-Id: 5786440009 X-HE-Tag: 1676754988-407731 X-HE-Meta: U2FsdGVkX18IjgSgVrJeKjq1Nh27xfFPlPqp2ZPeLvJLhszIfofUpu91damCAsY+8DwMUIwM+2Bf6Y6P0RZqeHK2SYvovZXorweH/UEX0i/vup6Xbvj7vf32G5fqWGDVXKMOrUi9WNWDB6IsPo33qNR7AhtL8vXJILwMA7HRqpig9mGHvut3a6yTlO/cNu/WqABA1cu0/9NcxcFAFKhqzZDLvFA7XfVOfVWqCyufuqfox8PNXkyit7Gw2+oYx2SanrkC/ARM/fxffhcgMN5PskX3kqiIWjhY3tXXiZzZ87ZsWZPz2T03T6jcGJ/x8RBEnZwZ1jOIl1hX2Z738Lvx8cF5bN/Pl87L2HGP5w5Fcc+xYz6udj6iGk+Zep7pbC4kxxibvtGVHJjTNKne/ExiNyRdCYJ4UDYvEdDyJUeExRncAbJunDjXDkmnT0NIrSUZV2go9q9HeQ9NHyd1kMH5bgE0eZQwbgInaZjBmkUGtCtri+aty7E/B8IvNeBACIXWVPWtiDHE2LLPmOUNGUc8KoprzGNTx4Zi5TIEbYqNEi6BLuftQCj5KblZuAuxqADBolHCcTT4Ygf0Tj1s/2zhiuH4MOdWWELxkK6ePiEuj6/fxb16PXxHYFosPkEsfuNKQOQnNgO1dnwg62pNNNmHLJ79kEtGayqmPVI74El/qCzwiyBnNzaWd9+t3oRve3PQClXmkqrXPIst8CZYHoV+kZ1wpxaQu/5/udQUWHarRzcpP098J+eo5ShF7dJt8YkJ+wG2Fl+YQoyrxIlsMQm6nae3szF6CESdNw7WcnKfBE6NrKe8UUrh1HfDb4O41GXjNwQCH+oK4YNgbTqva7ts6k+LzTkRNTQOz8skKNmotfFq2VDcu6uIP4II/PLBChe/sI44zQQlMoTTugqOlT2cryTNj0WoFhEOUg36qeO17AR2MqmWKq+4XaUkImh18rwIz3+vwxMTyVJJdjBl3kW wPDWxzC1 FWEYIkbkU02IP9i0Ef0rhbln4VXgX4IDVMO1WnZwsL4a/Y580Wxv+JAkXNVs1Czr571Gu0BofFczs07D77QR26REzh5aQC9EZr1Dm7gqNphz7Z5BXebkFs8TKj4bCUeUj1xBPoEY/IfJJYQLdC9Saw5hQIEi2JbIyXYxqFIbemU4CH/Z6C/KzZjDKrsFsOsSVtAH36EbW/2YvX0vyx09sW0tg24dDiEfLcEQlzBdwLn8ZdrFZSLy7FHK5kAyIUzhmHQMH/W9sbrPGmr/CybrC0itiMVSo9NyXaT96 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 Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- v6: - Make set_clr_bits_msrl() avoid side affects in 'msr' 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 | 32 ++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 65ec1965cd28..2d3b35c957ad 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, __msr = msr; \ + \ + 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 0a3decab70ee..009cb3fa0ae5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -363,6 +363,36 @@ 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) { if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) @@ -379,7 +409,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 Sat Feb 18 21:14:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145674 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 B4BCBC6379F for ; Sat, 18 Feb 2023 21:17:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8512280022; Sat, 18 Feb 2023 16:16:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CE63C280019; Sat, 18 Feb 2023 16:16:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8F92280023; Sat, 18 Feb 2023 16:16:30 -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 89DD2280019 for ; Sat, 18 Feb 2023 16:16:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 663441C5CEC for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) X-FDA: 80481671340.14.AAF7128 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 6BDCBA0006 for ; Sat, 18 Feb 2023 21:16:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="cduPV+/J"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754988; 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=9GGr4I/3LFReBSYqcfJoP1HVRW9+H5YST8fr63qrVPw=; b=vebUHlv3NjopViO5FwRKN+nSDS2+X9wicO8jcXoOwrRfQEaIHAx1eKfsoCiwfn4sK4ve90 1N7mUiyFDImCg9D2eyniXIDSP/ymSM2vQwUbMplxvN6ayMYSJMHzZpq4HPEp/oRk9XCpOa xibJiYzSOMOfAKLYF9QvlVJhfhuvZW8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="cduPV+/J"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754988; a=rsa-sha256; cv=none; b=r0CfF+EccHX2yKBKI4Y47LZyUCC/6fZ+MLJ+qh+12TPK0MwcBltzGknIXP2U76Cb2YrZlr vkftdD9YcYmjWUp/2dTm3xVnpK/6qnS5g7VL/ksi373Hqex51oABOEGxTlsWICtASWZXbq FQprfe0EXwB1RUhjkgYwSIkVjdMv9M0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754988; x=1708290988; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=rLqhRE2d5TzXO0jkrIimoFvYNyWSmS33HBbaA6qKIFg=; b=cduPV+/JoDbfGVKZuDZrXwkzlgP3AOpYm+S5Y6qFoaagN280FZtbpe2e lfvZgEcLPS04429pyo4tqYBQqbXe7pXq/uP5Idft0WQPjwf7OZjNqU2mN Qg7ZGZuj+IfP2QbzBCZ/cnTK8ASaTWV0qBdU4V+rFvgiAIQFnlRhUsdWq hZZhqMHPixEupOyzsL02vBcRXKpy/OMrP40ygJhZmKVQXLFvVZtrhiOIb h5Fw4/+pqq4xVY0WgxyO16Ht9Im3PGzm1DTSOrIAX2sHISmN+4iTv9UkK BRrxcuY/F3FezdHtoO0kwbB82QhlNo0QygNqkzLH3zPR4PJDTuQWGHoa1 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427850" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427850" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:25 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241739" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241739" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 35/41] x86: Expose thread features in /proc/$PID/status Date: Sat, 18 Feb 2023 13:14:27 -0800 Message-Id: <20230218211433.26859-36-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 6BDCBA0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: aai8ykhh8uykxih1uo7ks4c6swk9xej6 X-HE-Tag: 1676754988-604025 X-HE-Meta: U2FsdGVkX1/wcDg5cogOn5MjiJVkRB4DuYPFF3lpwK5HJ3ERqcH+5Ji6kMbcNSD+Ww5vJfm/PUurejyDduRvA7K911hXL6SHycByWYkE40RexwCRoYa9V3fN5GGtfG9OY99EQPqaqTe903mYnXFsB4rXpKuVqSzNStzbH4vlhsacNRh8M0HP5Cp7s1JyMidf0EmMmT49qpYLi1zPyBk+jzfCMgSP6dJ8Xgju83oX7ojFTxj28xI46iu8u2TEF18gYc/8XlXfQTYEHXtTL4+BH4ZcQoIUB762CKX5q6rCi9qQgGEuYzZ5QtI4Q9YHKi39mhgI9ODTT7yOKz5UU8U8u7g47eWz5gtCmla/mDwi9oz/cmbTwwFs1D02NYEgIZDbTxdtWvp4q+Jk+G3t1KEfROtxJdnO+6occ6BBkwjULnsvZWiFcTsGqYkwbkEQ+IO9VCRzRx/YXLLfxL/ChJ1cTrIEDGA6YfpjgbB12mN3DFbDXgvPdj7PUHxbiTaYN1zNCCq5DfxceKnj1TUtiA/2I8v914ZfMWr7F1G+jluFS6rJutCRZez7kGW7fzR7+b9axmz3dM/gLXFa4aB7vuSOsfzhyiX7tsoFwlYQCSOL3OQkBsFcNH2npQPsGTwctt9azqm7cI1odanA24o551V44KZrBU36cdma8ZYnoN/bhhJVaoLH0VjFPvdiLYIrKMdoXbl8a6AeOa3T5GCalnnjaWSFclhGAH/pzwsJybPCaFCzo+pUIM3SN7xgoE27KXVDgkhSfqehKzUApq5lcefBnqtMD6whVkZiZ1S3D/UDxiGRtWQFr3r034VvLlwE6MSVEliG+ul0F3JfMV9LUGB8cquWGkqtpD9MIG687YTwXx1wEWWlP8dUKHsKsIehAnBlAS90uyxZuhkas3kKiscghWbBNjMx5od25sgdIvD2WC9K1Jne7jfl94CMitMQZpa0+f0e9Md9sMoXKpngTEE OxEAtLg6 KPyAJIiX2AcWCEoLV7bJwdcgmakIlMdPgIkmC2Vp7SkTPoukXEUDTb/U3gpn2sC8i7iHWFSC/wxs89s/Q93iqPgQV5kZJFSCytOy1s1NBRq981/3Jje1mlvnW3HChDV5yqeboFZxd8wSpN+lNaJI6zrBnj2VGDcw2GbxC5npeYVjQLorzsQaSPRfHgZBbcP/+gMO+1vcuZxOcHIYL0rZ8l7x2oL3EFLIzrcdBRnbAjdM/HG93B8UjPQt9dV7Qx4ThydfTa3xprwKfFsgludFMUYc2JRK13E3qu/l4QjXGLgyZ+GYSHMdNyqm4kZydEh4aE6Tylez+K3SUklU= 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 Sat Feb 18 21:14:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145675 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 15715C677F1 for ; Sat, 18 Feb 2023 21:17:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5960A280023; Sat, 18 Feb 2023 16:16:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 56BCB280019; Sat, 18 Feb 2023 16:16:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C0B1280023; Sat, 18 Feb 2023 16:16:31 -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 2480D280019 for ; Sat, 18 Feb 2023 16:16:31 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0313A406F0 for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) X-FDA: 80481671382.12.266BDC6 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id EED32180004 for ; Sat, 18 Feb 2023 21:16:28 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k0vC8x+n; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754989; 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=DReRSgvy6bwQAJ5021aO9D5VPOmaKRsAP7JXRlEBPpk=; b=o8j0GDWRZxHXkg44YSdLkaFBvWb98V8uJYCv5pWq1sFPQOOWL3eGmBvh352dukTRY9nLD0 IqamZNSTKFbquoPUHPdUL6+tUiNmZjmjB2zzSxnPyZ2Rwq14VqfLXuDIluf9laf+1MUQMX sg4IdPz71pT6RSg2n3QJ/KtNh/VoFwY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k0vC8x+n; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754989; a=rsa-sha256; cv=none; b=mb9d/1uwLyPxuj/IYgLYw4UK0Mj8freaq7DsdIc/Di+/+kUToG87xm4roxNFkxHQDqKxbs slDvZoUxeivrJHwXg2LjoRCLKVHDfk4mWVL1Sxitk+Bm/ELmi2Arom3H07SaLp8ChG9xt7 u6eQH4VwFvlZ0HkSSjzYeUVWy7Jd/KY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754989; x=1708290989; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=LBXMAmJiTfFh9q2evgIt9WapwuhYib2Yu77ryjdrcH4=; b=k0vC8x+nqf3FSH/UI5HJo4M3Jdd91vlF+JE5pTUaJO9uaA6RXrp/a9zo O/scgu/a7U+3+I9xnzazn7KzseyN45wy9aKFoUSOU4l7EZqMdjsR9Vnqi z1CZyhB0IyNQtcUoViD4xKoh12zdZpbDiPPd88YRqdg3CX/z1/Zy5e8yf wfkx0nBjMcgx8dLDPWfw2miD2/A1qSsJTEr+1HAsqQFmt5hh318Pvgs0f H0ms0XnJ5LS6bzVH4SwfTvL20ncMm64V13kiwaqHFm9ALIv69p0/f4JXu QQt4wgsG4L8ckPCCbe2RCIaIOEUzfwr7qSssZc0iuMCVZoVdSgQzY4pJ8 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427871" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427871" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:26 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241745" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241745" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 36/41] x86/shstk: Wire in shadow stack interface Date: Sat, 18 Feb 2023 13:14:28 -0800 Message-Id: <20230218211433.26859-37-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: EED32180004 X-Stat-Signature: fjd1anwxf8wu6xigi13aspwa95y8p96h X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754988-488089 X-HE-Meta: U2FsdGVkX1/7/xSIXlcYF4F+Jm25sibEi6eoh1noSdiOKK+iHs5cbMEU27ThikFh5+k4Uia5H8RZPhm09msDHV5Wgdu3Q39aeMo5C6Ztxk93QMplIgBVhO7YYprJc0BHs6sH+mWa5GTXjImFVeMAKT7uQGxj8l+H+6XGFhE4BOi6r/urE4DK32vKk8dYmor+zFLlV21vkL0ssoj4NZLFgU9qs5/R+cFSvbjb/DHawVCiEo6JSnmRHivtZOoo/vGt46oPTGYKC/EqT7umlSubnyhJpp2m6GlK1ez/GohfDou0Q0B5NcS6KnS2r+FQxWGLc813eK+TsOvxgV9aAuknaw/Qb5aLK1XO760HYUbiHcNh6FpG+yBQeWytpPwGRUthLJ4YWfYREzj6mDrVEBDED1AEThnHBEeftAGw0E6WcofvzJ7+m7ozMKPFkBHvpgoJNXypzCqjFbC3XWke3qNwn/PHCGyys2bzOoGoD3XdoltdGdYNvYHFHvLy/vfNM4nYTWj4VB4PD31HYBqUzQAmXEXqU7dO4/KSmrE+rIZMzHLI6gewNYBSpVvk3HcJ1Lpatu5InzCEHU0YBtk96yMNioWOAAnEWHbx2qdzAw3nxjs30mwOX21SnfDss24VOc8JZymGWE6jKyexvcoTW0e7ao8nGqtnT98tT317vEcteZsxh/2DVBJQoFRERHuSLATXn9BpF2Xqb2+8NSgPwUB1ssPBgAQ0jyn7otKTZ2Am33QE00WIi4std5Ri9seM3mKsLc7RYcWWwVbWzWJHqkdv5ZFxoCC0QgyHmiZr4rtMzEHMw2q8su3l48rNdlstR7Fj43SzxxGR7aV/yMwdl+hG8LdP97x1CYDP8cqE1/yKa/TLDCd1EfbJZWkA6ZpeY/bpumcyzIvdqj+m3enKILfQ6F6YYxtEWv8yh8Fw7R3ov9htuW03np2kiGgv9rNwlQG41KU/qG9SsPGuxWhOH7o u2qQQDX1 41kfYABrvQDKYFNTQD1R3sWQaAW+N6InsFnoSCoEn3Emaig202gRzE3qND0W9uighHs2yXOosRmHYnwUp6KztzVhtTrZxzmb9CqGkaMtZ3gHZ3Xye8U7atM7Qa9eintsVn1cQCGftnwpmHRSssmBnwcK8QlO+epcTEWajmU2e/hQ1tFmEL5pkoCL+UhkBku4TO62fmQTAF2b2ykZbDf3T6a8WEP3WhDS0gO0yxtNOPCF8l/zJgPfXCq9u9qMoNws0jx0OVJmAfxsFS1kUU62wEkdGbsVFL6wI8pSc 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 009cb3fa0ae5..2faf9b45ac72 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -464,9 +464,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 Sat Feb 18 21:14:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145677 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 AC5AAC64ED9 for ; Sat, 18 Feb 2023 21:17:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 188A9280019; Sat, 18 Feb 2023 16:16:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 10943280026; Sat, 18 Feb 2023 16:16:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1043280019; Sat, 18 Feb 2023 16:16:32 -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 AA380280024 for ; Sat, 18 Feb 2023 16:16:32 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8C8CEA063B for ; Sat, 18 Feb 2023 21:16:32 +0000 (UTC) X-FDA: 80481671424.23.4E5A301 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 7817140010 for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="bk/fdW/N"; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754990; 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=ZkoDqDFXvJpY+tcKRNY9zMhNd7nvXfNDjd2KMtCqxIY=; b=GVOAbhMMAW8WrgwjUznlGzQd0I4GpdP0QDeqViuy5HDQ9qhdd1F8JpmHyK2i7pu2bgbPkY PuuzV6mVrqKyAccTfTm4XpRzCo+MSkKqCOdYo1bLL98vzZV3v5PsI3YH4ouOKUBaEmCR9x CzUuiVn5Jdmu3eJhWGn2TFJNjpylksk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="bk/fdW/N"; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754990; a=rsa-sha256; cv=none; b=y/sa0dQQieOETjDmudo9dcmT8V7koZZH0aBu2pAVZNQZMFnzd6NCFFIOfvfPmyh55ckXS4 F0tgY0j3q4t7qF/NOA/vI8hEWmBKKDaAZtVEJ9tLiLxTVbMq+xyksOnj3g+lTpiha0lLkR pjMRpa+BiexOrOTB4zpnvq7faTRFmPk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754990; x=1708290990; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=wOs5WGLz5VpZmdqpMgeC7szY9LNTPcbC4TNXJybBNLg=; b=bk/fdW/NLZikE3Z+GIVuX0o1HA5CeRCc/cxpld5van5mPzj75BgcU7+R Y7rCbZXM2IXjXOnO+vKTR3X3UlS0dgSSmdzBeaoEU4nHAoMxPjH+dqaSe QJ5/yjWoiadSO9F8DwWAkyHHSDHp43/aoSuELFDKUh+g8Ba5SkzdAPgwA 2SbTz+A+toh5sEY8sDdbEpWlU4YfaSeQqK6bnXH1Yw5fNEjTa1vHQybYn um3uapokexc9pxcFvXm9JLxZYSLRzTRHyK9l1ISyW6TIEoyU0WFgDGqc1 lrh48R7PFyZlhemBqH9uoLi3uL7o7ej47DrP7RSAEdSJT3cOw7LdeSNmG g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427895" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427895" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241750" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241750" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 37/41] selftests/x86: Add shadow stack test Date: Sat, 18 Feb 2023 13:14:29 -0800 Message-Id: <20230218211433.26859-38-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 1wer9ttkh9qujoohhewxjs5hbpq1uksq X-Rspamd-Queue-Id: 7817140010 X-HE-Tag: 1676754990-306169 X-HE-Meta: U2FsdGVkX1/LW4NimHic/Qv39X1Ov4x+ZJV1av2EavaLdOTusfrCAfrYKZkoWDAPlRfEkZ0vJ6tfuWuNu+PLSWzVqNxIA/jXjStenamsYE+I4gp05XKoRIvpDy9nFD9EqL7aq/aea518qe07XfnpQNsQ3yKXI18/ZgJxbvhfUU7wJSY4EMGrohlHkX8JtSOXb103CCheU4/jd7El+Hk1gK08a8X9YiVJvhYW31UJJgWgI/tzaC4E0/H3w/JasDxAe9HT0XQ7ihjLLqh6DTr/AbJNxIeYSDBdMy02jpfbggdMu4G0AsIgcnaH4moqv7duOz9eVLRhV88gKLdLZOhTjWqrtZB+0dliyZvJqEb5kppWRrJ6U9/MGfMXCy5rGzzeC5lR6yBSIkFs0De9d7vRevIiEE3BYiy+Mr0dURczZJS8U0u5cwW5c3cVjZPkzEQ42UaEc7xoWY8rxaDeD9O35VcGNTEcYJnKd+oq3aI6lkhWsgLJEwX0r+GmtT9nBebVINa3J3FpMLrxWzx2KfRtvbqYakLo0WDv275KaTqFVIY3g4lXhDTaulXffjGvkiejYlFaubUot8CMKC+xXxgIFn2O8sCX6i+CIN++J9nssOO9E69biMpiXJJnGVnkW5ZoFwdz+CPj9Q2UNU33/lkbUCGOLksXRUKWkRJ1MBXukYWduKDW/W4HdHBt+jiWM2KMAfqaNdrSot31jz3wXI3TJa2YFUBV0q5UEAamdL0oeI7EUH4twak/VL/Ah/VbtPqER8EMnToSSxDms7ufCPfsSv0JSwQ6fYwaYZQ6FIX0edyTRDthk1EkuUPdHk4OaLlRPOLNGWKWk+pxbmfuZgf7Wo1BTZmXIsaFNbxzW5C/Q1aATw3GwctoEgqjeHrmvpCrCWvL2pxBhyJGcdrinuwsV+/uX3QsxrZCBzbdtIVtuq30ySs1TkWjG1IVvY47LqRIQVwWLCXAZhd4QYSE+Mp FTlvUVNm BEtaEUajBDrgOgW4AcQF6tePrfM4OxwZ6yEATZvNNhBiU2MckkR0afBqtyf8LUTTAEBQ4CU2I/v0Zl8DriwSeAbMoEdrmtCHnag3+UxNacR9doO/Y+RuqLPLEqwlO6T4XltUzxV+xnUUNVZ6jB19rsFpEzQmZNwaTG8//wRvwqiSbg8q8onR+Av69GXBTvmpAO68Atp7wFxxUMbU4gGZD3FN+fRF3ZpdFNN7OK7GMmpVV0rY= 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 Reviewed-by: Kees Cook --- v6: - Tweak mprotect test - Code style tweaks 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. --- tools/testing/selftests/x86/Makefile | 4 +- .../testing/selftests/x86/test_shadow_stack.c | 676 ++++++++++++++++++ 2 files changed, 678 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..71de3527c67a --- /dev/null +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -0,0 +1,676 @@ +// 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) + 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; + } + + /* + * The shadow stack was reset above to resolve the fault, make the new one + * read-only. + */ + if (mprotect(shstk_ptr, SS_SIZE, PROT_READ) < 0) { + printf("[FAIL]\tmprotect(PROT_READ) failed\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 wrss 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 Sat Feb 18 21:14:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145676 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 55929C636CC for ; Sat, 18 Feb 2023 21:17:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBCEC280025; Sat, 18 Feb 2023 16:16:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DF9DE280024; Sat, 18 Feb 2023 16:16:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEBB8280025; Sat, 18 Feb 2023 16:16:32 -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 9DEB0280019 for ; Sat, 18 Feb 2023 16:16:32 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 811FFA02DF for ; Sat, 18 Feb 2023 21:16:32 +0000 (UTC) X-FDA: 80481671424.23.0ADB730 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 85B23A0006 for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q+vf45Ln; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754990; 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=JBsixjohsb7aDhNhJq/TrQJH3+e+COprL58KvKEzftQ=; b=bIo6YlQlK9MukxAIH4JSS3u+06lK/2M9iiF9rw/QKqxZIis6rARRka8RcsDpjJdVc9EZY/ pFwrGbdYKIR/IWuISYzhSYT5ULmeuvTMbEYLsl7cy946CJY8902rCgaIuyl5fN2m9KuWzW 0geEs6E8GXaoBous7+JuCs8K3rgEF6U= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q+vf45Ln; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754990; a=rsa-sha256; cv=none; b=CMqz6Quynqw0WV2kK7bXgd8uN2NuDy+FGCipniKFo+75o7z+5EQ2xyPU4p5//oVy9EzERl 86VBmJ9K8iKjE2dZHTavgfrK5OaWyMHf7lyyYVsJg4tA5laMuGuiFZSpFeFLu5t7Ba6IEu F2V1ZoQBTiZIkcHzFzu20RdDposwHB0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754990; x=1708290990; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=YxwDoMp5u+7K/cKvKjbEHedNxVeyXtrjkBbg4BbPtvQ=; b=Q+vf45LndY6+WFnKci4be8n28cUK9vpbcyBBhlJAeuTqvZzDF4zaeI9a KZM/wqf2I15vrqEWqEQIp5PrxNdkuAompr290npTiSfXyXZRLLLnyzr0P jhxfXEPV+reHNPv/P9SH318Tuuuc8nvHwjMY8tjNtQqHzcguZchRjeYYZ sOCB/i02M7UOrqd4sMJML7Mgr8CYZrf5kuxx/T1Gw+rOOqld4v5KzTQ0I dUvqmg+N1tp8RZ8vLkM/m7jRWE72BsRUwqv75+MiUEb5QJX5kGy3lCcao XSEnJ9+B90nmKB2reVNpYBCxgnW1s33sW+78Me5pusAs/UwOgIWIc5hmH g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427916" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427916" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:28 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241755" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241755" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:27 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 38/41] x86/fpu: Add helper for initing features Date: Sat, 18 Feb 2023 13:14:30 -0800 Message-Id: <20230218211433.26859-39-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 85B23A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: nnqjsdh9ya8rdgxnspwbjfi6zig1onbb X-HE-Tag: 1676754990-383503 X-HE-Meta: U2FsdGVkX1+EpwDG1L7fUUzatQZfP/jxCMxH+B2fmwUeWn+LjE70YJEKxkwcpCw0pJjQYBIbQbizOG/y3rgTDQLXa3yH0ExCAfbD28KlLYGXi+VIjw4QBTPPl6YPl4nV+GKPjTyt+t2x7r+a8xNmpnky9LeeyNB+mBirsrpnqxqP14e81Mt7m91Zg41fjmCdE+BH2TcdU+nGz0QrqP1cay3bjCiIw6blmqijg+u2AZT/P0F6wymx6cRVEi+TyMkQLqKIcGEKaWFu4W5Ysl2QHG4hCW9niVmLWb/ByZnl18rXgjGOQm1LEa9TbTvzIpWnk60tV57ymWm72uWWeEhAPxAOgKVvZ1+ErpafpPtEGXs1O7EGwZRNQTueHaSs4MbBb559v29HdI88QxoG5hcqonDGYMJ/uarTZVwgwENLvHV/5mzmDKacjsGAN/lms94DWNMW18XcKzbItzznlGv28/1iTYrQKsuzylZwfkTE+Q0HOE8uhbeiFYefQ4YuexgDFO62XOeOcohacqTYkM60iC2z/PvXVegUf+zFF14h8XMKVN+JxfUZCWVW7T76hQQUEZXU1hvD2zDscgBCNpYK6pISPJpbXyudUqt37yrf0xOuQ2lcOdYm8QtcktHecr9kktpZpho3dbGfq1QA58LYzKrqtJYyGh4SKT95exm5WSguyH+c1pwdzlZUJq4+psGRpxqJWjLzUn9ox/TgwmdxvGLnAMZEZEruIGMofdPWjDN2YGTpJ0uhoGWy7mEk5e2k+/MrxIr//+LNXXEW+uiYfV3TGlvUV9lxhUuMl6FbgxMCuTeMAsEGgh32sELjqjqR8ZrZFCsQZiwYKSHquZn7vxF2myal+oZjS1pI11+M/HPKSNWA04UVkcZIFQd1AIL/VLEQplyIGpDYrvSRHOJWL/wF6qURjL49rm222l2Cj7Un1UyURI+4PO8Dw6GjApWMr5CzBESH9pJUZhmt0Xm NphN9eN/ zmkrx3by9sBqHOL/oT0c8fVO4RKZLDTChiLx1hLObr/lgKTlxbigZEmxWpv7CiAS35vj9ez3+sTqtor8gJar8nRZ8AKpLPNylGDwvyH4zPBqDYB3SlTyKj2oLBUUlo5cUKoP0yicmZWsBqnH86rxfZlwXIPhfB/RmIdVzVTmDzBOliTje4LUgi/awKNUK5ldaMWIH+P1ptToamLPdnTyo5+IV9nPRkX5y9/x9SbxsBhFyfv8= 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 Reviewed-by: Kees Cook --- 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 Sat Feb 18 21:14:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145678 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 27F88C6379F for ; Sat, 18 Feb 2023 21:17:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFFF5280026; Sat, 18 Feb 2023 16:16:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8697280024; Sat, 18 Feb 2023 16:16:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A25D0280026; Sat, 18 Feb 2023 16:16:33 -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 8E409280024 for ; Sat, 18 Feb 2023 16:16:33 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 644898058F for ; Sat, 18 Feb 2023 21:16:33 +0000 (UTC) X-FDA: 80481671466.18.A63C8ED Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 3C72218000C for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DWblILRh; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754991; 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=08JkBkEO+Bx6wzCH5LqHBY8IMmstQr/gUoMXAKSYvwY=; b=1ifVeIFttissRks2ZB9DH4ZDysY5rI683KnQXWoXLuxGYOTyaVFYyZ0MFNzeaC6YNYukpd kMExEL+mi6OU7UChe42NhItSORYkpnDC7QXLT9xqwwpJ22h1PwVohavYgQ7/fUgjTS/Cxo tAWBBTWQkLl/4/z3Q/f1AZa/Ck8hEwo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DWblILRh; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754991; a=rsa-sha256; cv=none; b=Azm5Fj7xgufqrCreZjpPg+bGoLN0ZQSjJuDCcCYDMamPjLCCANHseKs7ZYJJ/oX74uovbe nQhxRl0N1QgR1UyArzM7sR9+Wx14Hj0JNgSFfW/Wg4gGfn90I52rbE/vZ6DZ4KNXX+8xc0 loEuLCbboL7ZYBvFRutLX5CcRbXYINU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754991; x=1708290991; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=xhLWZB/OZUOSeVyrlZfHv/Uy56frfc4dDYUp5qyh3D4=; b=DWblILRhqccTArLgMyIjbV3EtMMZncZEpEAkNvJC4qsAFpnT+xTVazKx oCVaLZxmXsjY3YojlvkOIKwh4L19Vupt4Cm4QHHj90AI8AssaiRaTeWmb DYr2/CJ+Ou2RT4uWFoCir9Tr1tIhh1shpRYKakIygmvrH39vF9YrWmgQK MmHX+9cfb+HAarldbZ8kNky+2Jch1+Nfkn5CvPXU1yW8Nvn1om60c7ITK K8xs6BQ3Ow1In1ZmIgVtjJayD1dlx94zv3qnSVWuDKGO9mZ9lEDhjO9a9 Lz4VzFDOhlWoyRU8nw74mVMBLV/QnI6rxpVOjbKdc0RpFDNHhzcht7eKf g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427937" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427937" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241760" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241760" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:28 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 39/41] x86: Add PTRACE interface for shadow stack Date: Sat, 18 Feb 2023 13:14:31 -0800 Message-Id: <20230218211433.26859-40-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 3C72218000C X-Stat-Signature: wscds18t1i7r8u4aoafmd4odcp1q5pau X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676754990-658039 X-HE-Meta: U2FsdGVkX1/XSYsyQzK4eGMZDfD5KlNerFFbiMkwHyCfpF3oxhSZYOLqwneMXGqfeQx1R5DhY8zqV23qavwmXeed3vMFgNBxginVRi/T51Cv9JTHxqk3WG4v9rBixsAjE/gN2Y+H5lIztIx4UlAMOmZO/hPpIXoVPD3bl7XcgYag1toXx8wQna4ST00/BWX2yR6uMG3fWjdb64ZfRwRsH0G0wNByAhOJ5ksis2q3WeXzy2XBaihr/LIlaMK7e9qPMFjhE0GiWsVhCdMWxAvKOimjodjYufFfPz1uVxg9rBvhKbZjTAAFq10cKM6KtikxErg8KzeyQrKP1osEBNi3BH/O0MY5aYaD7O0R4n0vsUBHzUO5z6PJe57gGANnDMEWfZDqXluGDYiD4PcQ7tKeOgygyDjfVT2/2d7NfmB/AXlkf7kJa8Nwx7oeR7tUPJ00N/QwwFZPPn/rIOsUeRUWsK//ofM1efcCDoyB1e+47ZTGXJBaM9tQ2adSMppZP4t+BP9CwRzKyRqdpEChURNvybMq/yczSS8fl1R3V+KgqGwnl4w6AF9CmiB+cxPSFPXNchAOaSDUxLGaIFuzQQ/b4zhpUQBr9+enDzXCQ3k4y2vg8XnTbI5mAo867sPt+F1CJngMOETBCoU7qGCIzABM31+9GHJpu2pTrvliftojepPx7+km/4h2mpLMWoUHmgWQ3d8NC6ab58ZqOoqo/okWBIUQn3+E1GaevtF6MUsS443ioLG62OqzN/LAE0NihhQrsYdrhS1xT64nBs1Dp8IYxm2O0G3lPNsBFBjhXXkGkG1N+TcMsiuUf0FAi3PL2tjve/5XH4gUlkdKDzLdciyIbGIOAcGZxWj/h7wDsKEeDSvK1tCQfa6Sk3VslXJlkPMyM+5wTH3IBdIfQa91XlvhwXI3uir03rLLqzU6OZg4tnqkC1AaHzB2DHNduYt4MFHUdZLcAIjv4HwkL8fdTKV nHhcGJif 7eBjcGE8JBgtKHhhtmJw85XNgyFaXE4VeyTtEu1fmIT/JDqjwJlAqL2yhia4HwWVaFKhsucMcbNV2bo7+oJTxalPYW2u6lYk6HTFigviTYuu42rdp/3B2s5SYmHS9nKWxBJTkhLWXZhs+0GGoC8bbEVpuDV3LGMUYBa/EmWS/GQB4sWMKgcO41Hw6bINQoEUHh/d7UIMXaO5fkBrznnpm9VLXaPOQdO6CaH1kca0mVw/FfzqpiC7vZnklkLi67PAdfCLvcdkht+QkR7b/8kccrjnpcSYSBQ1xMzGz 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 Reviewed-by: Kees Cook Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Signed-off-by: Yu-cheng Yu --- 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 | 86 +++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 12 +++++ include/uapi/linux/elf.h | 2 + 4 files changed, 104 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..c806952d9496 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,91 @@ 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 Sat Feb 18 21:14:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145679 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 833ADC636CC for ; Sat, 18 Feb 2023 21:17:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0AD9280028; Sat, 18 Feb 2023 16:16:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9348280024; Sat, 18 Feb 2023 16:16:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D344B280028; Sat, 18 Feb 2023 16:16:34 -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 C01DB280027 for ; Sat, 18 Feb 2023 16:16:34 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9991B1A026D for ; Sat, 18 Feb 2023 21:16:34 +0000 (UTC) X-FDA: 80481671508.06.99A6CBC Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 8EAD040009 for ; Sat, 18 Feb 2023 21:16:32 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IZri8SJx; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754992; 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=Sn4n7crmTxrbn/OIeUWT1Azd349YYKdcy9ub8HpHkYM=; b=YV2XQpHe6KNltqtJ2a6RdFHEOHrPi9Y33Z5GpjaBEjP739L9fE3PJHaQcfIMYzvRbmcE55 704pFh/DteMszGY0om/uwDEI51M2O9sqUwW1q9YVNnFjOfms0dycSx942+nwXymQLTvrKU rjtbPMe8G4g3El/t0tiu4icA5oJdufM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IZri8SJx; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754992; a=rsa-sha256; cv=none; b=4AX/jBBL/2FipRHGZ11XP7EcsbFeXK4QXPs+7ROCMxmsRNtDbtZ/c1AUTyT1fn1+9P5Erf YWG16PXYHPIlX387tglz/qZ231vp4OJJvaTuloOMTbpWvr21mwvJINA2exhzWXkqpGUX4N YxC6DUacmBLD86XMyenv3oW2TCn2J0c= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754992; x=1708290992; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=PfOj46FKnOWR1PiqFiy1gLNlGQ5AfxHeduPbiAD9rSI=; b=IZri8SJxpcesIFXQ6MFFJCOnypJDXJ4/2EMluxarXMH8kOzi0SSi8Nzg eXT7Wu9cs0QMRkHvJfoCrrJMb84+aLNLBXoAlJIwgtIp8UBMm2W/YyIwS yVCenx/o1iGySWf1N6HtIGDZYFOCRRKWDBYJnB7XIWOoQdM4AGSlb3Bm7 wNmxTqUgxyliKmPsjuTPW8lXGNm7w2fhqVcy+Og/UfddPJTjjsfr7GRR4 I4hA7j5IOvDlw8KOC1+5HzACv+RmKl8wxrv6kouq06Ngyxfd4/zE4BAJJ +TJSkYl/ySZIZRSjsgkvoHJ+UOy+S6xy7E2vlSi3XjAnodc9OkBrW5BWm Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427960" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427960" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:30 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241764" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241764" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Mike Rapoport Subject: [PATCH v6 40/41] x86/shstk: Add ARCH_SHSTK_UNLOCK Date: Sat, 18 Feb 2023 13:14:32 -0800 Message-Id: <20230218211433.26859-41-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: gtydbb9rnmtbg5ryo55z8x3dxcr7nia3 X-Rspamd-Queue-Id: 8EAD040009 X-HE-Tag: 1676754992-9105 X-HE-Meta: U2FsdGVkX1/BBfV1I26mL9TaJWAsIK7+wQCM/aKf1A6ijeFrEMiYJSED3MvjZk04JoQkcuBreEqzaEIy2lGVgrntzvaTZ9fvDdW68Pnjf4Re7LuPaSqCnVmDWIUfzqVCPyobi/3/6Fzrr39EgYVuyqa/BJOLHvvb59Vk6STyGhBHgXmBm8m+BzPbtKU6x0bQpscEyoHjGMJoAwQZf1Wulke9paUkyoVvWfsF5XTKPJb4GRolqUJDEs2wNn7xOekSN7gr7iqjUmqF5iNDOkL0qcRHQIvRZRXBIA2mteVfQWMVWJjaQCGK4xYZeQXodKxUyL+N32eKkrjzDjzP+E5KBcq0SEnhu80D5PEDk9l+45orskHlg6uWRxberpU+pumqo6+SCnLIc0PtXT2TlUYFGH2ha9aDoEfy32zYBq8a6ohWLNMAj48kqT3uvgYzZl/k+nBwI0lG+Hki0ti3rSY6kH8TcLZseDM8Z9zHee2RFck4bIjjEEuNITHs10CDXDwZsCrXxy38DjHogNO0FxeyD4Ffvs4dtQwlUPzEytOUQq+k9rG9VKfVyF4dPSyLr/3fyRQef33gHrNDi6ctBJ+T1XG5G1hRMGbPsi/Pc0yvuvp6buX7Nn7Pbnob5BkPjbbRZ83uUmiq2tBj75QlEdgivSiOmZX0TnPSihnxPiGe9l2mq1d06bfZMfGFcebdc2Dfy4dmx8THayVoF3bF0/76O9zkQ9FjXYd1n0gk2VO2krsdn4jiaRQ6Go1TW4+O23pDreIsWsCUmB6NA8Vl/g5FNEmuB/sSC4SxtuFdoGwS0WAF0rxJTjgAtze6U78RDKBz6+GWl7JPCDOnUEfLMJz/e4VsH2kQuJG3/DFFB03xDzay9DIj2aEKAJJBxe7DTvLOuFXYejerPAuGpwd62uZlbw5nyNkDmDQ3aOauDvw1+QvJEPY5yANeFhdwymY2oPy/A4il4LfUhmVOiAKw0pF s4gtlnIS Jy8aTc6l/3IU8RCoZGEhZ620dkd7SywApJX0RUa7U1EAUu4M3+aN1F0L6rMp6carWN4+ejGwrEYu6jMrHhhTvqLUUON9J7To/+PPtbAWWsdFdYMI1eqaJRZX8I90f6iRHyFNmXRpKuKcVyPjjdqVYLzOibmof5d4Nf8W2WXV94QoRKgp8wQSkNXBFobwaWZY80hDedAVCQAuRgkW/jHHhQH4BVzymuBJTaU07qMXvFMf9Is7FfzvZMWax1QdK/B+c6Znh3lOcywnVJIobD/41ivMRNdAtqJDn7webHtpwzyd2C2Ptkmn4mQKXiURTNqzhgiUr+qpl8s1DE6fZlbXhA0GtS1PR01Ta5ph4ieeUF38YJ+Q= 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 2faf9b45ac72..3197ff824809 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -451,9 +451,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 Sat Feb 18 21:14:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13145680 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 E75FBC64EC4 for ; Sat, 18 Feb 2023 21:17:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DDBF280024; Sat, 18 Feb 2023 16:16:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19C77280029; Sat, 18 Feb 2023 16:16:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD194280027; Sat, 18 Feb 2023 16:16:34 -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 BF265280024 for ; Sat, 18 Feb 2023 16:16:34 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 94FDFAAD6E for ; Sat, 18 Feb 2023 21:16:34 +0000 (UTC) X-FDA: 80481671508.02.38787F9 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id AC639A0006 for ; Sat, 18 Feb 2023 21:16:32 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="SLNQ54+/"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 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=1676754992; 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=XB7GgEgn63ZLeSXiVo8rMaq5mTEIs0hdTlnI+zYN+qg=; b=vv986g+t4EoTpqLHtktFSsiBrFBiouEH6WwM6xKFf72cp7Rofan3eKcDKC2zwvBlHHHB+F FwIOd9qG8KlYTxqQSnbhUrv1sCBdOCRJzBVgAU+rUOjXdC5N1TxvjO9wValoLDy/NrzYkp G+7bYRcMTceco9TcCnyb1HabUuQZ7lM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="SLNQ54+/"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754992; a=rsa-sha256; cv=none; b=hLpadRUS3AmzYnt5A0BY/kONQO21Y6Pivd2NXIPKwUfO/R/DAomEMMcZF4zMcC6Kj6lfm1 aCbsPoYLuu+foVQxNgu9G7D2ZNW31efTrpICBvdiiPEWE9xQUyQY9cJysHUpCxuPaOzJeq WtLDYKqIg9smQvamZyYGL4MeD96hsfw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754992; x=1708290992; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=ubqwcNNiyY5+cprOn2ju85ofUOiPXcRDQntgWmd5we0=; b=SLNQ54+/H3wCqInKLKtjnfnqxr2QEQWcuHpwGTVGS4CZ3hTmGwqG1Htn 2KIEHyCaI1R+ThakLQKcyB6AEngNe7CpLq75Lfo48cBuK9CKjt65PEERS 0mMkEYFojq45ArwXH/teBRecVjKXhAyU9OKS1Vguj6ohT7RQJov08MJAs JspwWQk3yZNNRtpEaxIA5ehtokJoDKqlDV0BDLbtW6dnvM/amxIonQ9fP EQ//xxOF3HwdNTTtnIblqO68oJfNPkWaDaFUF+lpp/sKB6a2A1q+vbbpO x4+xQsNmLGD49IaCJsLAnhtXyv4SGk+4WSCAIWhvykiXTludOuKYgPW0q A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427983" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427983" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:30 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241768" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241768" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:30 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v6 41/41] x86/shstk: Add ARCH_SHSTK_STATUS Date: Sat, 18 Feb 2023 13:14:33 -0800 Message-Id: <20230218211433.26859-42-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: AC639A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 6d9exkb45z8jmhfmby7r7k6g4wzke1jb X-HE-Tag: 1676754992-369820 X-HE-Meta: U2FsdGVkX1+Qcvhl6m6dAbZ0Ds2ujc94xOxzoSDTwY1a1+jQ6HYXvx19uoW0RYYSWQmubQidiLu3pBW7NjschpHRNNwqrECPNnzHRGFfz0o9QYu61hYrIIPojHxGf7UVwdKGb0vkN50ZPyNH9mJUOndn8Jk3fV04uY0xIeXMQtek3sStblCOTIKNbwMQ+LH3cgF2SpfvEhBRRnv6AFxjK2WezBD9jju+y4GbNcpFL0AdC+o9gMKb0bg89s2qAKBRBik/8NaoPRdtoshmbAPcvvD+UyHSaiibE2cAU4tEvwhpW/BizRvEhcyw5zf2RdixXHDZfqk99zMOgIm9uGEXiWohaRiZTC7xq3vQANyPuVTL34ZSVJiO7SfBGN2YFb4rXiRqDXnDhra56YDzb6C3pyUOftNw4F99XlLmnU5tFB1fMupt9fpU6Tgc98vZcHASYYyFlLlaVJsiyHlWNNJ0ICOyzH/+D7NVSP6lIy0mAtdb0NeCYUxabCiBC66n67m4XRszttoP2Zts/kITuWfqXb2CArzZiq4KtF1iwsmTa5F4ISWK2kUF1XwYTVqDMeTJ+P+/u7cCc3qW6HnbJX1To6zLJlnqpCdPYxDFECC20+u2wuNrPImYb40gIk3bwvS4+zNltyaJT/AECJ+wX9hXHDx1gQHgT9WSzCEvEhRmqIXHMi97vURtn4AYL3J9zRhg4aGIt0zIjUgCukN8yaaS7EjVz3Mn9hOWWGFob5rcA8a+sZ5LefUaOMkftdYjMsvAa/xPg/YRHsWpe6UqfOACoeHFDhZkyVva1jXLIws4mq0jEosZWWbXhLEMP/HZhCvghzVrdyi7fqDBjGw8oCePIslGA9yfGnTidsVMXpF1554q6gXlEuj1CHrJbIGpkatuMgdQbIvFMl/tId4YE8GKY4OziL5tKUWJ8zfgSQi6abiL7IZJQkqgr0ugfhSuToeN0fBtWL9YlFjpX9AoZTu lniqPXle jOFLPa+fasO4UZWnNpl8eZA79egM0HXElIYdgpAA9p2sWuqa3aYNhNcsWqzcYXcOsJ5Auj+wJpQCiqn1lQKMjnSXCaDtmUUUjkobTLsmYPsKzDu3DqOmeH7PBstnnnkagMnzFuE/5UKvecV0debx8PGt1Mf6E4qxHstfqR0B/gSjw3v4eoKqkiU33HLfh3e1vmBmZmEUR8htPuqCkCNWBvU4wrsTNWFiIdwBU3llNPCjypHlzqro+1JgABJFH55TU/IE/mEa2Y2GvtLnDgoG5GRfN1N2noZTsZhq2JOQrmp5pV/x/JEZNGB30aZJ7Jihk36GHiEf7XRxkOtd+5Ki1CjMVBVuXgWiKkiYPp8DXO8d207uhBDbSGuGu7Q== 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 Reviewed-by: Kees Cook Suggested-by: Mike Rapoport Signed-off-by: Rick Edgecombe --- v5: - Fix typo in commit log v4: - New patch --- Documentation/x86/shstk.rst | 6 ++++++ arch/x86/include/asm/shstk.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 8 +++++++- 5 files changed, 16 insertions(+), 2 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 acee68d30a07..be9267897211 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, 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 3197ff824809..4069d5bbbe8c 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -444,8 +444,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;