From mboxrd@z Thu Jan 1 00:00:00 1970 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 8AAABC7EE2E for ; Mon, 27 Feb 2023 22:32:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 256BA6B009C; Mon, 27 Feb 2023 17:31:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CB9E6B009D; Mon, 27 Feb 2023 17:31:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEA106B009E; Mon, 27 Feb 2023 17:31:53 -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 CA8446B009C for ; Mon, 27 Feb 2023 17:31:53 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9091AAACE2 for ; Mon, 27 Feb 2023 22:31:53 +0000 (UTC) X-FDA: 80514520506.15.2026C81 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf24.hostedemail.com (Postfix) with ESMTP id 6BF27180017 for ; Mon, 27 Feb 2023 22:31:51 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Iq0+aTfB; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537111; 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=veDLluZZGIZBNK/wTadMCXK8+klKEMevtWOXQT8RUT0=; b=fyiSQi8CZrw1OhuwunLwzH0zDH5uBEAsAeaQ9Oo+tvp8ffn0uSQxlBTL+AqxXS73UarIzv Pm6gEgTi5kwrX5P08qqxBQfhT/C34R6irIV3mAls5qqsdNMQaqpJPjEX9bA0ymIR5lPprQ E75eCVyO/ft5pBsLeK92a8Ewg5KNy1U= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Iq0+aTfB; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537111; a=rsa-sha256; cv=none; b=VplEpVnEfqt/fRWknqsrzLpXBSNAGrLkhrnaa/tQUWrdEFQ37Pfp7himq9qrNWrXAQBcb0 TUcyl70Do58/T+pGQQRm1y4w+Mx+puoTGgIVF/8tfTzJRWxwBI+vq5lSw7sEhh45q5raTx sl1M/P359WWaSf/CKAtkRjdgXrz7dNM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537111; x=1709073111; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=FzRSuSkDyk6//OI0YkurvPKbmt+RZMfvZ31TH5a40rI=; b=Iq0+aTfBZY8f0yN+5xgGCYd2pMoXHdCHWiMvuWGo7gmHC188Uh6vkxd2 80JLk1woMT8f4NfUwnKvWluWSdBvdR1im4MUpACEadIsV67mr33zjeXQb WnW3zOregDU9+p+R7CuZqmD3e5KWILQFO1vk+1a6MLS/zplqRijSYROdf Qr72B6m2rwKc0ZjP4kbmV0o+BYCaJrP01pJrhu+OdAWhQb/LBv2VvOJnK 41ZfhFS90aVLTaCoVOuFGP3zXgbT2sCgoQRowKgrB/aAcbqI+u+LeQPOn NfZbW3t6DlL2Sv+9P1h7uiABDmEv0L3rk2hWlrlQyNXJfpmtrGmvSWMSW A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657599" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657599" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:26 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024675" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024675" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31: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 v7 25/41] x86/mm: Introduce MAP_ABOVE4G Date: Mon, 27 Feb 2023 14:29:41 -0800 Message-Id: <20230227222957.24501-26-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: 6BF27180017 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: nmqg4b64oenxqk81e5nsdpdzn34gbws3 X-HE-Tag: 1677537111-358105 X-HE-Meta: U2FsdGVkX1/Xax84XrmqTjOY+RHEXae6oK35BEsZz+aM6iX4+14MyYABzBPCaAdtzIZ0GnNRS0gZKRqpQLDDJ9IOT9Rn7uyVZmWrsBwmW/WOolW0ApGDAdu7xqqh1O9CwZcg/E8IM2zvb4cz+MTaJPcWknH48Jez8BTps5nTamRfM15DP7yw1KnN+gZXWLFyaoepn0K860kaM8f5v9ZrVtQbNPCEtUBtJ7ZU9k4xsZJEj65uT6ceQeIWfs69TCZ54wQMDq4bZtou+C7LvV3K561tTTLKtDjqKI2EOH71QZ5iiVqS0NrwFHOzUlctmWp65vxy76gCaPY66IM1rVNTpuXeCQmOSYpoUMRuT0nxivLGjEiOIzPC1vUYHmeLBT8fSzOnH0HLumoklWcuWBikUjPhPhK6ydVsSzPOyQsEPKOissUuMKuzEMQxtkD4SljDbPdaligjDsLxHaR1OXhXqdt7rq4go+lahNKAqeRp5FDhiM6ekWMAkm67ZxcFQ5ceqI6zrrxIqu7BChUtEVKwBDdNsJICyUtgG2gtgpMmyz/PGaXUZ8aEqSNpSdJT7pv621F4dr8RcaEJFFwKDke48rv1eWfU57Hnp7S35e3MucNUWf17L3vZ5cAfMDC21/hsR34Fro5GCS6A3QemNqvFIykMIbiNQqPdBkPGOItmwlO7NaWY/8aoFXSZMIvgq4u48nWOZeoNok/eGRUcBOS7njhu0+7uP3zt6naAhjOs+wyCTi15Rl52xN3XuC7t/UiHOQqzl2dLErpoLT38iT01ZchlZKiyuSLF1IIZ9xaGNYVzqNuYb9AJwZX4uGHpB9nHZBwxzl9CWPwNARGbXbAz1PmNqobIooTRUIN9bahcDyY6/Y2h8fxdRrca+NEQeZUgQZYtRK8fejhA71x5mwexKc039gDsovbtFzXIo4CmzLw11h+HiRNebIvKFghePLInNsAPHlo+wOcJJ3xvh0M m3qB+UBr xJIBEGLjT9hL/LniZuiPA9rBURSkJhug6uCSRLFxUrVlxPRJ8bt6OkCQ1z7y8wSTaZ7Us4YBwzEYz8ox2V8FTGOhSvfD+zpMLqNjWzYkFAWMK0ngYloIsHYjXAayStmBW9jSTcP8YdHp4viwSLfhX2RkbeJDM2ugSvcYn2wIvW9Ei0LQ7dB9d+C9YdKXLx2au84lPYDYsLHM5KrEdF0K2XW+3uo6P/ScqNN7Xz8KLYphfuDZH1fVKMi/aZGs/THi/gcmGLjX3oPFZYw7mTptzMYSpvDY6q8GZXRR7H6e2AFeH1pizW/9JYt2IwpFbM84vsiEusR94hy1SQlsYW07rKB5SEef08gCSZvtk5gMwBR1ZjzD8NvDdT7UvAfdZWO1T2stmif/IJs8h304= 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 Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook 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 cee1e4b566d8..40d94411d492 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) -- 2.17.1