From patchwork Thu Jan 19 21:23:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 542BDC6379F for ; Thu, 19 Jan 2023 21:24:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB8EE900002; Thu, 19 Jan 2023 16:24:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4329280001; Thu, 19 Jan 2023 16:24:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AB23900009; Thu, 19 Jan 2023 16:24:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6B2FC900002 for ; Thu, 19 Jan 2023 16:24:06 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4D18EC0445 for ; Thu, 19 Jan 2023 21:24:06 +0000 (UTC) X-FDA: 80372826492.17.3A79686 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 255CD180017 for ; Thu, 19 Jan 2023 21:24:03 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jplBSbsF; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674163444; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=kAPK2G43HTRXIoPjAhCuhjvThw+Gk9Azt2qL3QWNxr4=; b=NIZ1iMnDtpV7nMY1hAWywBIyLz2wHCufOh8MsnYTuE9GZYRyxMxF40CDleYQ7X7XeCnG8T VQca7Zv6itNB01CtJGwVXI+BZl1Gt30l+di7G1RogKcTJSNqxg6KO3vU9i5+6BkjlI2T6c vlnxtdWfec8vghytLqHDyKGtChtVpeQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jplBSbsF; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163444; a=rsa-sha256; cv=none; b=tJjRFMjT4PjTB8pKaOb03hztjvfxwYYdStclszacFFKtAmOdwvdHQ2mu/hytWCamriMJ2z EcWHXU0ic2cYNrHpswVZi2r2n4P5oUCiQWU8xF7Kp+kbvXWkoxdOuwE8tnPTVOVsnMzzrI QYzQ+/WZoMsiUNDWX6YZUNeVPZAKdDI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163444; x=1705699444; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=DRhmUKmYGXqIeL9sLHmMRoFoAPvudeDqkB4y2AZ6uIw=; b=jplBSbsFN17ZrNjKegWNPES0U0eUbu/mpYhvf89JoHaADeSk8QvhcO1V nE+Q4ChowIWC2zZVmQ/mGGdwMrxe7OaoA69i9oxWbgC4Y4cICGCtDhrey 2d0J+encA3958D92ly3gzPbnLhXYj6+FHRPd/u8xRik9BEKJXbKDeJ1iH VmYLN0xHbuchN/tJZMF41E843VM/qVUj1ooQTgg76sa+8eGSJlpcCxUZl ZlsLRi2T8iiqnGp5oIdBvNoCx8IhBDYjIEVlHGnYdFOowehNC+mfku7Ee J5gz1LrM+xs+t8k20ym9OTB4wfXEFbYLmAtdYeNic2lVd6lJ9IG/8bzKK w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119747" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119747" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:03 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139117" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139117" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:01 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v5 24/39] x86/mm: Introduce MAP_ABOVE4G Date: Thu, 19 Jan 2023 13:23:02 -0800 Message-Id: <20230119212317.8324-25-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 255CD180017 X-Stat-Signature: rfmx67dpox4ct57dyer3dsdt6e1fty9x X-Rspam-User: X-HE-Tag: 1674163443-380783 X-HE-Meta: U2FsdGVkX18S7PYGuauNbLC0TQH7EHZYaheCzPTZtvlwkPCuSwYc21+smVjfP5o0HYiNBANs/ZI+V0xsr+0BIA+gbaI2YWr8gDrie3+1WG7vUeDhMiobpwRM1zP4U1Y/QKDNF3RaxhkHIOFJyPTyrgP7pN7w0Qy03mY6pC+ewIhMld3oUfjPv5PnHjvFfNd/dvbt33RAkBNOUme3w1NC2Hz/XlFXW6cjafokiF5aafko/nSLfgQAUuTS//NBLXnxbfyyINEmJHOehzChTEIsnPlSIF8yLn4lWBDfaYBpkdKOgMvujeKK2TxGaRvd/AWbAlVWS2t+Wwy7oi1JF+aU6h2MGJfNPsdLmr2Xzjkofv7i5Zt7IbdT+bPf7Kacd/W/oh8/CIVauBCf6vV/cngzog67Orsq3HMXbaV+shXS12Mjfu3fkCr2e5db8+yClzsnR+gDy6caAmpV6GADCxRkJXC6/Mh2ncP53n3/iPaQ5HHKglalEfiJEP0DpXsCCI89RroOd4Mmiou2BdytREnkEPfbVVwO7jw+G83lWdfEsp0dKcaLV+gSkwHaqV/+7tXpceLpmPPMYMV1zu9UPjWFY08pJZiInCGaB6Lgd+dSply6C9TAMpD3vFTQHjMZYhwk1IV3Kvfj6a3csr+2Xg41So8+AyCE0qN4dALCY8VgPZqebEdajzJQIm5mCXkqiTEWiUdJmo4/t2aTZW2nTH15MAVFK68tOymAiNDtsHNMD/LPNlA1CT5ky3qq2jU6byeFcxgVbj8CzINkhEu7zB2+Y4lAojVavl58GYmj69spcp6kqOJQqcX4VymL8zNPuC16Gzhkj3QEcAynfCZTGf7FKYZ0G31z2uiN68kud/C51VUIhRyf2x5q0q2sjX5aJPD80re9MJvP6xTPVF+BajsH2Q8HtQ2bTdagULQ7a4QGFwumblVA+3amU2JSgQ8nNbZjL2ATfqlRluTZJVND5vI JYj439k7 qb4HfsT9Wo4AHRR9r8lBQ01um5s3v7hW3OeIbIwisYx9L+Nrt0Hj88I1Tf46GAP4qefSEMUyRIuaI45yRluypUl/HwWgtGI0VbWfS367mg9HHti8JB/UfBJ3Eh/TSjPJdHjmoZlSToWnAUYL4StxLiMOCc1r/ORKZTV1NwUp2SODkwRebLjzUHOo3ht5MRa3k70oC4B7kjERcxOBk4mXfSTAHv3imELLqkUCsQbck6di7SWh0EX5uVme6bF8Z9qEts54J X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which require some core mm changes to function properly. One of the properties is that the shadow stack pointer (SSP), which is a CPU register that points to the shadow stack like the stack pointer points to the stack, can't be pointing outside of the 32 bit address space when the CPU is executing in 32 bit mode. It is desirable to prevent executing in 32 bit mode when shadow stack is enabled because the kernel can't easily support 32 bit signals. On x86 it is possible to transition to 32 bit mode without any special interaction with the kernel, by doing a "far call" to a 32 bit segment. So the shadow stack implementation can use this address space behavior as a feature, by enforcing that shadow stack memory is always crated outside of the 32 bit address space. This way userspace will trigger a general protection fault which will in turn trigger a segfault if it tries to transition to 32 bit mode with shadow stack enabled. This provides a clean error generating border for the user if they try attempt to do 32 bit mode shadow stack, rather than leave the kernel in a half working state for userspace to be surprised by. So to allow future shadow stack enabling patches to map shadow stacks out of the 32 bit address space, introduce MAP_ABOVE4G. The behavior is pretty much like MAP_32BIT, except that it has the opposite address range. The are a few differences though. If both MAP_32BIT and MAP_ABOVE4G are provided, the kernel will use the MAP_ABOVE4G behavior. Like MAP_32BIT, MAP_ABOVE4G is ignored in a 32 bit syscall. Since the default search behavior is top down, the normal kaslr base can be used for MAP_ABOVE4G. This is unlike MAP_32BIT which has to add it's own randomization in the bottom up case. For MAP_32BIT, only the bottom up search path is used. For MAP_ABOVE4G both are potentially valid, so both are used. In the bottomup search path, the default behavior is already consistent with MAP_ABOVE4G since mmap base should be above 4GB. Without MAP_ABOVE4G, the shadow stack will already normally be above 4GB. So without introducing MAP_ABOVE4G, trying to transition to 32 bit mode with shadow stack enabled would usually segfault anyway. This is already pretty decent guard rails. But the addition of MAP_ABOVE4G is some small complexity spent to make it make it more complete. Signed-off-by: Rick Edgecombe --- v5: - New patch arch/x86/include/uapi/asm/mman.h | 1 + arch/x86/kernel/sys_x86_64.c | 6 +++++- include/linux/mman.h | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h index 775dbd3aff73..5a0256e73f1e 100644 --- a/arch/x86/include/uapi/asm/mman.h +++ b/arch/x86/include/uapi/asm/mman.h @@ -3,6 +3,7 @@ #define _ASM_X86_MMAN_H #define MAP_32BIT 0x40 /* only give out 32bit addresses */ +#define MAP_ABOVE4G 0x80 /* only map above 4GB */ #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS #define arch_calc_vm_prot_bits(prot, key) ( \ diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 8cc653ffdccd..06378b5682c1 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -193,7 +193,11 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, info.flags = VM_UNMAPPED_AREA_TOPDOWN; info.length = len; - info.low_limit = PAGE_SIZE; + if (!in_32bit_syscall() && (flags & MAP_ABOVE4G)) + info.low_limit = 0x100000000; + else + info.low_limit = PAGE_SIZE; + info.high_limit = get_mmap_base(0); /* diff --git a/include/linux/mman.h b/include/linux/mman.h index 58b3abd457a3..32156daa985a 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -15,6 +15,9 @@ #ifndef MAP_32BIT #define MAP_32BIT 0 #endif +#ifndef MAP_ABOVE4G +#define MAP_ABOVE4G 0 +#endif #ifndef MAP_HUGE_2MB #define MAP_HUGE_2MB 0 #endif @@ -50,6 +53,7 @@ | MAP_STACK \ | MAP_HUGETLB \ | MAP_32BIT \ + | MAP_ABOVE4G \ | MAP_HUGE_2MB \ | MAP_HUGE_1GB)