From patchwork Sun Mar 19 00:15: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: 71684 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp531525wrt; Sat, 18 Mar 2023 18:32:41 -0700 (PDT) X-Google-Smtp-Source: AK7set9SjPyAMfXc2YZ5gEcEfRVBHOV09n4m24LwVQFV63Q4CB1pNuCMxDG/+81pdLsapM8anU1Q X-Received: by 2002:a17:90a:ea05:b0:23f:6b62:803c with SMTP id w5-20020a17090aea0500b0023f6b62803cmr6051299pjy.40.1679189561199; Sat, 18 Mar 2023 18:32:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679189561; cv=none; d=google.com; s=arc-20160816; b=EbnAwGYj0pjKLf9gRiwk/ECZrtlc51ccHd/YEcuA1MVNSGUmSMUfvnfaNrvO59QNKS xQ92pifKOl0/TcW3WnRoHFi7sHpRJDeSeGBnm83dUBBStBK9sopZ00aF2uRoBJH3DCCF xnQU67TY8qHWItY973bKQFevAkJSYmQoXUZi5zu+A3V7/9SgxcpJvJUpSxsXsOqWIxCL 3IFnTJljNN6OPYrTMFrm+VhLzjDMw8ibMcgiRyPTTxxtPo2IJcXk+zYUEk/xXRICMIko a7rkqbgsSwpXPIEDOzbYU0ssI7fbT/ZwCFWyinn1Tzdh0VMNnXxTFpy4zwdTOK9V2IWN q99g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=mwokoOpGillR2xIBR8l2lN8VfHeotneOlmH/z0FhJq4=; b=M70Us8qgS9VMmdSPamaNCG4mhqGcQxCKi03oVCgE5j+3DoJo7R1eB8TMsbPdrZ8QfQ kMUC9SciH/EScNVceT/1jtyLBdyN6s20egaqDMz3vpuoIWoFnFxAjdtrylhbBiTYvnf3 TSbR2l5FJ2ycaBBlmdT+UpLNDakU0ohgpmqn55Yo/2VXw5XizsGBtu92fC3cUEshRZus NytWa4YpBXakeRmzFQmwCLEOpK5MQc/hMvpLH+oNvq4/zY3eJz/femMd6vRpo5xwtmDc LAZ4AVia3bnibgOA3LHejj1CBiZCKcwgLluBYZbpDr33+vWJdeVI4BljMUX+sxpKR2sl AGFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lb1Od6fh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h23-20020a17090a9c1700b0023b3a60296fsi11832835pjp.71.2023.03.18.18.32.29; Sat, 18 Mar 2023 18:32:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lb1Od6fh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230273AbjCSAVG (ORCPT + 99 others); Sat, 18 Mar 2023 20:21:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjCSAUV (ORCPT ); Sat, 18 Mar 2023 20:20:21 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D692F2B2AB; Sat, 18 Mar 2023 17:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679185124; x=1710721124; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=m2G03CSTQRSNX4YXn65CryKHCN03SDYmmfcTPTkRd8E=; b=lb1Od6fhtL2DLrCK9c+USmxyHjHNckFz4EimAijArw9/WJ4flEY+hAmJ kbYbFTMuLrtNqrzf2WSuECwPndQ2Bgu3mgBdz1XeQIBaxNpMG7LQB6hUt FooqfXx+xvBZrpV+7HMhwsUcAHUu+nvPlm/62HQotWwKhesIKLeErd8HW 4e08tSW0xQJGruLVQWp9BiZzyTjIvccASPsCqwjZmtIxI33V02uiZzlZY q6TouUe16LxaRiVVjW4GsdtXWaA1B/RHSWAJ3cIa7zV0/g7yefoiuXv0A oArJ2d6q3fBoiNHeeH1qYB6qPKhno2qljRDQRV9OamBVNMw9UWcd1P9cO g==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338491282" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338491282" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749672886" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749672886" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:34 -0700 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, szabolcs.nagy@arm.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v8 25/40] x86/mm: Introduce MAP_ABOVE4G Date: Sat, 18 Mar 2023 17:15:20 -0700 Message-Id: <20230319001535.23210-26-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760757873081540711?= X-GMAIL-MSGID: =?utf-8?q?1760757873081540711?= 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 mapped 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 its 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 Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v8: - Update commit log verbiage (Boris) - Use SZ_4G (Boris) 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..c783aeb37dce 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 = SZ_4G; + 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)