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)