From patchwork Mon Mar 20 16:39:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 72282 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1321664wrt; Mon, 20 Mar 2023 09:53:01 -0700 (PDT) X-Google-Smtp-Source: AK7set9Fh4andDBGH3ejhYKWs9dMH9rZWP8xxcrgktadYCEeJXezKJgrbD51Gwn07JsAgKZXjXmk X-Received: by 2002:a05:6a21:3385:b0:cb:af96:ace7 with SMTP id yy5-20020a056a21338500b000cbaf96ace7mr23666380pzb.46.1679331180689; Mon, 20 Mar 2023 09:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679331180; cv=none; d=google.com; s=arc-20160816; b=mMs4cGdG8ssyqULY7Twm7hhUOnpefoTH8mKSoLXBvsgcyBUwd5D/nfnSR3vDlTSJB6 JQ9Ka0kat9NjesqECj54prPtYSvtWGLmz6VZ2tewX9Q+Sy5lbG0xdSMUBYWrOiXJm8iA aI4uY9B31XOqQ0NGQeAI8lalH7xtIYPwbIe3WAZsXNRsIGB+LaycBAuhgKNnotDAmFp4 k7qKgbt4emev11pw4j+vXZjcryWwJHndyODroBR9xbP0GxauYIgICIDdU9L93UyK8pmG dAk8gM1tljz65fzkdunXIjxtmwNhKc9aUIdwN1VhaJ4yIW+Lm11lkQq8JJIUc8UeVV6F E6+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=/puiErv2H1f0Jz8/0toZjSkfrJUKiVeqM0pytddkQ3w=; b=fGVYRMwGw6te26EqXJP4UPWdoB26MLpJuVjf4Xd15qFnuSNrfKJJfKfRXaPXyJmIqz GMIV0XTQuiGRCDg7x6LV/8CveEF05hy6S3qgvZnreoU311ncyV7zjXlEWbRYoh008UEF i/hyFH8FpW/K556tbi9LXcAKdIAs870HCJLVJ7xWRHMtn6I+ahqhD9lisJWbQGFHVmpj Ds6EBKdS0BOPQHZPiWg+hgmOX78hTCNJR1BWsHhdtLGJfzIpgnAA0fOeLNucMg8yruzj QXt4xPlALlMqKjx0naIb5GBnaOTWsy/Vbx1EZpmDcmLSU+unsgtzLEI2xSw59egL2tPJ QpEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=GyKsf0P2; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s13-20020a056a0008cd00b0062578d18ba1si11074537pfu.54.2023.03.20.09.52.48; Mon, 20 Mar 2023 09:53:00 -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=@linutronix.de header.s=2020 header.b=GyKsf0P2; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232412AbjCTQrP (ORCPT + 99 others); Mon, 20 Mar 2023 12:47:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232331AbjCTQp5 (ORCPT ); Mon, 20 Mar 2023 12:45:57 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6982433441; Mon, 20 Mar 2023 09:40:34 -0700 (PDT) Date: Mon, 20 Mar 2023 16:39:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1679330364; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/puiErv2H1f0Jz8/0toZjSkfrJUKiVeqM0pytddkQ3w=; b=GyKsf0P23JCvwLNCknuuWXLPKLNS7dXv7qX8Rw79VEhVOHQJLfPEeaiH0NhK6ujAqQbjRx PHWh4NwdJm0eGNi9E0V6cm7/FZ9uv6xGBIzjJp2PDEwOP6tGlIDKQByPFtaxYXouOGXHET 8LI855v418S4s6YOLxrl4k/3fA/a7dkQ+i7tOwXAOfQkp0Z4TDr58hPn9wEAZc0r3ZdF0Q 9taQCQjdVaCvJ6+zqODWU2/Pi0m8tZoIEnzcfLYsWqzHxSV/xuPAKahBg1wND6udIDNjRq G4ettrK3MfmFI2pMDSwYIJdHKfXlgZy5vMle2YJlyipvbjH6VY9m/oU9a3SArQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1679330364; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/puiErv2H1f0Jz8/0toZjSkfrJUKiVeqM0pytddkQ3w=; b=bw3KWaHRL0ATI1gD5fCeJU/9whenQtd/fM/xCbasvEQ6LHfvWwwbD09riBqQm1IBJY+hQS T6P6RI+WLABAphCw== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86/mm: Introduce MAP_ABOVE4G Cc: Rick Edgecombe , Dave Hansen , "Borislav Petkov (AMD)" , Kees Cook , "Mike Rapoport (IBM)" , Pengfei Xu , John Allen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <167933036450.5837.16902241878929444984.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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?1760906372201888522?= X-GMAIL-MSGID: =?utf-8?q?1760906372201888522?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 5dde71115fff3946e53a2c8e04802016b8f0f9b1 Gitweb: https://git.kernel.org/tip/5dde71115fff3946e53a2c8e04802016b8f0f9b1 Author: Rick Edgecombe AuthorDate: Sat, 18 Mar 2023 17:15:20 -07:00 Committer: Dave Hansen CommitterDate: Mon, 20 Mar 2023 09:01:10 -07:00 x86/mm: Introduce MAP_ABOVE4G 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 Signed-off-by: Dave Hansen Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Link: https://lore.kernel.org/all/20230319001535.23210-26-rick.p.edgecombe%40intel.com --- 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 775dbd3..5a0256e 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 8cc653f..c783aeb 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -193,7 +193,11 @@ get_unmapped_area: 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 cee1e4b..40d9441 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)