From patchwork Thu Jan 19 21:22:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108777 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1C3FC46467 for ; Thu, 19 Jan 2023 21:23:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 550EF6B0073; Thu, 19 Jan 2023 16:23:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4645C6B0078; Thu, 19 Jan 2023 16:23:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06D826B007B; Thu, 19 Jan 2023 16:23:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EB7AA6B0073 for ; Thu, 19 Jan 2023 16:23:29 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C373712092B for ; Thu, 19 Jan 2023 21:23:29 +0000 (UTC) X-FDA: 80372824938.16.E9D3766 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf21.hostedemail.com (Postfix) with ESMTP id C6DFA1C0019 for ; Thu, 19 Jan 2023 21:23:27 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q5hIwRSQ; spf=pass (imf21.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674163408; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=GnbG2zT0q0TQKJhci012/C8hz/SCKJl3hl5drYijqMc=; b=zjqnCmg23Wcqfw9ecDQWkaHmt4YiwKQ4RkVRBr49ZQyyL84hD0tRcGKhB0dz0a3sTBpzPQ Zue7lAtEHmW7hFwonUw2y7dh4EokzEn30YwnIgNkncqlOG0y1vZqacqf1S90XRfa5jvkOY UK2H83bKbDW2ma9U0zrXiubUcyLO4xM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q5hIwRSQ; spf=pass (imf21.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163408; a=rsa-sha256; cv=none; b=m13+VnX4oXUWX5q053fzxq7No3YXukLL/8NWAeSh7mfquB3lHH0hwm5gk1SuaHmbWEroGK 3R9aRna//H55pBl06kJgQeiACsDlCNAZBCTgIkNgKOFywkQ9oEzYNZw1MJ+9wxD2hKr1eJ J5oJ/5bAo+pxjsk6Eatxmf8deG2uPJI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163407; x=1705699407; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=uhuMbHp6Kbqx+B8M1wEyR7U+E0nR3IzT2DHoy+pPYUg=; b=Q5hIwRSQrbeaTw2ZNKYjvDPKzgWyCWaaMjqtz76ZJ/Jr8hJB5PYj0PGo pM2FXcJhD4N4ZUofgbQ6pHAjjLqsYy99ka1gQrfG6+QPfQp6FOl19eIQN 76QTmZRv2VpSE49q9PEVuB4M2pKj1oHAhsgqZQ+fxn77SP+TbGGsZvLC5 R/x9o9yu6B/ycMb062rBqpdFtzqdVOv+lTgwgdgL8fONWXLUFZCyZw8IS Ts33pnZ0PE8YPS93cuMPAsO7rdYPxE278XS0fB5Ln6vAlmOQiQM/TnEw0 DLOtkq8NEp5REEklTGaQiJX1AKw/hJbULd6xBjVqoQiWqauvc2p3qDJeQ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119171" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119171" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:26 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989138986" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989138986" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:24 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v5 01/39] Documentation/x86: Add CET shadow stack description Date: Thu, 19 Jan 2023 13:22:39 -0800 Message-Id: <20230119212317.8324-2-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C6DFA1C0019 X-Rspam-User: X-Stat-Signature: orqbpbpiwgdah15gccxdk448aqwj5gim X-HE-Tag: 1674163407-681816 X-HE-Meta: U2FsdGVkX1/6+VIS5+472rOJkYGuHgxg+oCAb3lpE/HYP99N70KdS9It3HVneBYsORsV8OjiRx68y73KWF6Mri7/q426d7ti/8BZwjZsn33ctz5pQvI6KoXffkyEreq6xziVE8VTKEK05nY26fJbrLVEsu2R9xrmbUccVhKpdWrzm78NTyRHYPkY/5uJITfXK411Eq7yeU84ZwQ3oCdzu0sl4j5/BHdMqLrPB55rdNmUvJACSJlxFwgO3MDXOtYfJ1kuIa1lIs95UqlJYODgtGiUqqSGtVOqQXyg++kuXE7BnSF6Z0nzNTiD59VSp0pAfZyTpE8XRrWMgvKw9216iM9F5Ht/MIuZwa8v670JJISilP3JyI0YUk1eXYlP8jbuDJzv0eWDkGtrC73/iDrEWfO0g5pSXkwmPbJpH+MufAENIc3lSDGZA3DabI17SnK3rmlrWtm4M4z56iM6IvsPBxWSeNT5KmvaL8a/5JTGQlMadQXs+bHsLTATsEUcm/RNnt5Cx6emBa8JXCJItzPFdG1NEWjLN41Br2C9Md6CXgo0q6P5SEn7r/kyNuI2myjfn9gDlwaH/j8satcJezukdNh07nPRNaUmlYgnk+zKBSfpe6UtGK3thgSjwhXgtgIoxQolz48j3+OnRxL6nCfYnoZHa8UgZrkczPteOPGPVCa5AaFtIHThzYo7vnmG/dzaqFNubVk6mzFv5GFsDCIIhzkytap4+mz0y4cgjkjnbgrvsX1fhPxXyU1G/Na29b5ToorD17OICMhCUcrXyHVng9TencsmrE+5XcQ633OF/2DLGn4bMfwZ2H+ctbT7xNR0k0jqj0YHmp5TTMyCZzWLelGni41qHpXfQFz6Gdo8BEIPkTdVbdjA4GrRBvlmybHI9U23/EypYgvDSLx09nMUxMd0nHHkDVSEvlx10pgeRVzpNR8IBdFJxt6fbhcPg50YD9k3b1xOH3MnecyPgie DnAcTZBk AYs1uQYN/S4hnvlfSOty6LyCC/mRfxX/6PEtXPZs51NVW2y5Ro1/+yyAMX/e9n6TFiwPI54ls/d+4A6laFieH7Xa4se5+kPAwchdFc8mcK3fNrk3WHoPWb5ULaB1M7398BUWG7Ru0yB7qlvXdLStA3MTVmi6fOjAfzfyK3wNRDuTcRPaHMan3NqWXoNVWP0T8m5Vn9TDX1MGezMGxNxmE8SONmaHfqiiuH6UtITft26WtnAEjne0SJBMA8O3JQbx0VuPNevIFehaP4IhI30+UqWqGg50CEUy643I5 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Yu-cheng Yu Introduce a new document on Control-flow Enforcement Technology (CET). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook Reviewed-by: Kees Cook --- v5: - Literal format tweaks (Bagas Sanjaya) - Update EOPNOTSUPP text due to unification after comment from (Kees) - Update 32 bit signal support with new behavior - Remove capitalization on shadow stack (Boris) - Fix typo v4: - Drop clearcpuid piece (Boris) - Add some info about 32 bit v3: - Clarify kernel IBT is supported by the kernel. (Kees, Andrew Cooper) - Clarify which arch_prctl's can take multiple bits. (Kees) - Describe ASLR characteristics of thread shadow stacks. (Kees) - Add exec section. (Andrew Cooper) - Fix some capitalization (Bagas Sanjaya) - Update new location of enablement status proc. - Add info about new user_shstk software capability. - Add more info about what the kernel pushes to the shadow stack on signal. v2: - Updated to new arch_prctl() API - Add bit about new proc status Documentation/x86/index.rst | 1 + Documentation/x86/shstk.rst | 166 ++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Documentation/x86/shstk.rst diff --git a/Documentation/x86/index.rst b/Documentation/x86/index.rst index c73d133fd37c..8ac64d7de4dc 100644 --- a/Documentation/x86/index.rst +++ b/Documentation/x86/index.rst @@ -22,6 +22,7 @@ x86-specific Documentation mtrr pat intel-hfi + shstk iommu intel_txt amd-memory-encryption diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst new file mode 100644 index 000000000000..f2e6f323cf68 --- /dev/null +++ b/Documentation/x86/shstk.rst @@ -0,0 +1,166 @@ +.. SPDX-License-Identifier: GPL-2.0 + +====================================================== +Control-flow Enforcement Technology (CET) Shadow Stack +====================================================== + +CET Background +============== + +Control-flow Enforcement Technology (CET) is term referring to several +related x86 processor features that provides protection against control +flow hijacking attacks. The HW feature itself can be set up to protect +both applications and the kernel. + +CET introduces shadow stack and indirect branch tracking (IBT). Shadow stack +is a secondary stack allocated from memory and cannot be directly modified by +applications. When executing a CALL instruction, the processor pushes the +return address to both the normal stack and the shadow stack. Upon +function return, the processor pops the shadow stack copy and compares it +to the normal stack copy. If the two differ, the processor raises a +control-protection fault. IBT verifies indirect CALL/JMP targets are intended +as marked by the compiler with 'ENDBR' opcodes. Not all CPU's have both Shadow +Stack and Indirect Branch Tracking. Today in the 64-bit kernel, only userspace +shadow stack and kernel IBT are supported. + +Requirements to use Shadow Stack +================================ + +To use userspace shadow stack you need HW that supports it, a kernel +configured with it and userspace libraries compiled with it. + +The kernel Kconfig option is X86_USER_SHADOW_STACK, and it can be disabled +with the kernel parameter: nousershstk. + +To build a user shadow stack enabled kernel, Binutils v2.29 or LLVM v6 or later +are required. + +At run time, /proc/cpuinfo shows CET features if the processor supports +CET. "user_shstk" means that userspace shadow stack is supported on the current +kernel and HW. + +Application Enabling +==================== + +An application's CET capability is marked in its ELF note and can be verified +from readelf/llvm-readelf output:: + + readelf -n | grep -a SHSTK + properties: x86 feature: SHSTK + +The kernel does not process these applications markers directly. Applications +or loaders must enable CET features using the interface described in section 4. +Typically this would be done in dynamic loader or static runtime objects, as is +the case in GLIBC. + +Enabling arch_prctl()'s +======================= + +Elf features should be enabled by the loader using the below arch_prctl's. They +are only supported in 64 bit user applications. + +arch_prctl(ARCH_SHSTK_ENABLE, unsigned long feature) + Enable a single feature specified in 'feature'. Can only operate on + one feature at a time. + +arch_prctl(ARCH_SHSTK_DISABLE, unsigned long feature) + Disable a single feature specified in 'feature'. Can only operate on + one feature at a time. + +arch_prctl(ARCH_SHSTK_LOCK, unsigned long features) + Lock in features at their current enabled or disabled status. 'features' + is a mask of all features to lock. All bits set are processed, unset bits + are ignored. The mask is ORed with the existing value. So any feature bits + set here cannot be enabled or disabled afterwards. + +The return values are as follows. On success, return 0. On error, errno can +be:: + + -EPERM if any of the passed feature are locked. + -ENOTSUPP if the feature is not supported by the hardware or + kernel. + -EINVAL arguments (non existing feature, etc) + +The feature's bits supported are:: + + ARCH_SHSTK_SHSTK - Shadow stack + ARCH_SHSTK_WRSS - WRSS + +Currently shadow stack and WRSS are supported via this interface. WRSS +can only be enabled with shadow stack, and is automatically disabled +if shadow stack is disabled. + +Proc Status +=========== +To check if an application is actually running with shadow stack, the +user can read the /proc/$PID/status. It will report "wrss" or "shstk" +depending on what is enabled. The lines look like this:: + + x86_Thread_features: shstk wrss + x86_Thread_features_locked: shstk wrss + +Implementation of the Shadow Stack +================================== + +Shadow Stack Size +----------------- + +A task's shadow stack is allocated from memory to a fixed size of +MIN(RLIMIT_STACK, 4 GB). In other words, the shadow stack is allocated to +the maximum size of the normal stack, but capped to 4 GB. However, +a compat-mode application's address space is smaller, each of its thread's +shadow stack size is MIN(1/4 RLIMIT_STACK, 4 GB). + +Signal +------ + +By default, the main program and its signal handlers use the same shadow +stack. Because the shadow stack stores only return addresses, a large +shadow stack covers the condition that both the program stack and the +signal alternate stack run out. + +When a signal happens, the old pre-signal state is pushed on the stack. When +shadow stack is enabled, the shadow stack specific state is pushed onto the +shadow stack. Today this is only the old SSP (shadow stack pointer), pushed +in a special format with bit 63 set. On sigreturn this old SSP token is +verified and restored by the kernel. The kernel will also push the normal +restorer address to the shadow stack to help userspace avoid a shadow stack +violation on the sigreturn path that goes through the restorer. + +So the shadow stack signal frame format is as follows:: + + |1...old SSP| - Pointer to old pre-signal ssp in sigframe token format + (bit 63 set to 1) + | ...| - Other state may be added in the future + + +32 bit ABI signals are not supported in shadow stack processes. Linux prevents +32 bit execution while shadow stack is enabled by the allocating shadow stack's +outside of the 32 bit address space. When execution enters 32 bit mode, either +via far call or returning to userspace, a #GP is generated by the hardware +which, will be delivered to the process as a segfault. When transitioning to +userspace the register's state will be as if the userspace ip being returned to +caused the segfault. + +Fork +---- + +The shadow stack's vma has VM_SHADOW_STACK flag set; its PTEs are required +to be read-only and dirty. When a shadow stack PTE is not RO and dirty, a +shadow access triggers a page fault with the shadow stack access bit set +in the page fault error code. + +When a task forks a child, its shadow stack PTEs are copied and both the +parent's and the child's shadow stack PTEs are cleared of the dirty bit. +Upon the next shadow stack access, the resulting shadow stack page fault +is handled by page copy/re-use. + +When a pthread child is created, the kernel allocates a new shadow stack +for the new thread. New shadow stack's behave like mmap() with respect to +ASLR behavior. + +Exec +---- + +On exec, shadow stack features are disabled by the kernel. At which point, +userspace can choose to re-enable, or lock them.