From patchwork Thu Feb 15 23:13:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13559262 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 665FFC4829E for ; Thu, 15 Feb 2024 23:14:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 519EA6B008A; Thu, 15 Feb 2024 18:14:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 461156B00A2; Thu, 15 Feb 2024 18:14:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 015FE6B0093; Thu, 15 Feb 2024 18:14:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B957B6B008C for ; Thu, 15 Feb 2024 18:14:46 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7020280899 for ; Thu, 15 Feb 2024 23:14:46 +0000 (UTC) X-FDA: 81795594972.26.52BA8DF Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) by imf09.hostedemail.com (Postfix) with ESMTP id 926CF140010 for ; Thu, 15 Feb 2024 23:14:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PVh08EVe; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.21 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=1708038884; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZVnx71WeH6HH6EqY5Lraa48e1cboxzcmNmmFJj9hDh4=; b=kpYQIx2i66Ofxa2wYLQ9WRwAnyV+5xlHD9GwAMldD6pMeiYgiifnuM/pOgF/B7T/Q7tofB 5PpDwbrkHo3dD9bmzqgaYK2gJ2FgFkjhRegQe/dYHRbREL/LOpnJbS+GLXmS3MigNH319c 8IvFRjVuh4CjZrKzkNQTrIQNs6yszME= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708038884; a=rsa-sha256; cv=none; b=345685U1hXUAX+VPVi3+KqgsnHCHclwx7uVhStbfh1n5nXgElNkvVGzLdp06ksK0vMZXWB qtTsLGfjJd9xL7+KMr+sDO7TiiabweeUlcw5AMPOkRK6eRbqp7Nqf4TDjVm1a7igoszc0s sKKS58zwwYrDzrQLtme3rUF6mrpcLJ4= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PVh08EVe; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.21 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708038885; x=1739574885; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fQLzGTXW5Y/EBT0eWnQYJ+GT+r/+mVoQa4wR7CvVXl4=; b=PVh08EVezN8JnrhzLkbq0lM+KeuoNBnEybXTIw8rqwcf5mwgy9AVdhxE vyRjbcM8wg1LqUif4GKWl6o3aQyK25+ji/te3cLD/K8kbvVe+eA4l25yf g4gVQQZeHdXVY5FINTevT6rbZAXB/Gi7sIPhoVQZGJUnVG/xKRpTsGDGq +GP5+HtTCHH0mzmEped4jGp3lcpD2Ye+Icd2mbsf+A45ed+UzZ9HbOhrY CxEzJFfOfzM3bVzrcYVUkGHGxcQDJ0b3Nzrpgh1YQpFWklZdOuKfH7156 7MHa+aHKiioT6vPPN+n+TQaKy9m57yl/FQqbPxD0rX149sCtWFpXiy34u w==; X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="2066389" X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="2066389" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2024 15:14:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="912250207" X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="912250207" Received: from yshin-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.209.95.133]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2024 15:14:41 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [RFC PATCH 7/8] x86/mm: Care about shadow stack guard gap during placement Date: Thu, 15 Feb 2024 15:13:31 -0800 Message-Id: <20240215231332.1556787-8-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240215231332.1556787-1-rick.p.edgecombe@intel.com> References: <20240215231332.1556787-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 926CF140010 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: qmmnhungxcmpdny61bw3u5y47wik67he X-HE-Tag: 1708038884-986920 X-HE-Meta: U2FsdGVkX18t6KulNokAkc3CmcT8mEDO10U0wn15gxTkI/bt+V/fqzEnL0tj3unYTVgGM2CNqTYn3aOwHZkl0kvvU+B6MfHRilyZkbKaou6RK3ryc0mmwaJOnbFeYnaQW0fVibgRJBwtvb3YvzlZ591vgD1yAxDvl+575/H+WuVPGCUjR4xwz9tknqmXG84vNMkbKeH1R/bqPmYSkUqosrFQ2TPlwZh6wl8DnlI3b/Y8Xm0eZVA/zvdXfXXjyjSNoZqdJc2zPPnROmZmhbG8JTo80VODhbKVzlpHb63fe12B0Cas4r/mOc+TEVn9yg+qDlBOkocyOA0tcHljlhvXzMYg20aWzymsOMp0cYnrgWIm4+1jVIRJt0PjQwwB1Cl09Jq1qvafr/10TaH0VfxN3ZSUPQUF9Em5pnoomyh9kS8ipLQJc91CbzC/RhQpEDXEHf5OuCvUvo8WGg8fgIhAmTfkLr3I0IbQ4TP1UUQ6Qie4ZzLNfnRjV4kMgwMA8Jz8buKTlEogmGkorj6fck8G3fknC2yS+G053pJqYnhcwSHSA4xR4MwDPMBz+oeAHh77Cq2BzdPNG0GJ0is8k91v/613kk2NKsWqOy52Eu66CwUibwliNa4g6UAjWjtdpoxSbr9D16vq1ArY7Q5N8/l2w5i7r4luWvXR9RTvC4wcGgJP2OtJobyjc73o/D4WoChzxQkdSTxIOS2whF1TVKtSCLBBXf3s9rWKPUF/0vsDP/WzSqb8x7MHfEzzsSPNGSNaY18Bbyb/i7LcAfVdn6UMi/tqABsd14a5viSOcDM+e5ysSB1wsyMJOJxNT3w4lnoZ2FAUzEr25kfljBajFm9PmBZrugnrB4Tm 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Now that the vm_flags is passed into the arch get_unmapped_area()'s, and vm_unmapped_area() is ready to consider it, have VM_SHADOW_STACK's get guard gap consideration for scenario 2. Signed-off-by: Rick Edgecombe --- arch/x86/kernel/sys_x86_64.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index f92780cf9662..3b78fdc235fc 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -119,6 +119,14 @@ static void find_start_end(unsigned long addr, unsigned long flags, *end = task_size_64bit(addr > DEFAULT_MAP_WINDOW); } +static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) +{ + if (vm_flags & VM_SHADOW_STACK) + return PAGE_SIZE; + + return 0; +} + extern unsigned long arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) @@ -144,12 +152,13 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned l return addr; } - info.flags = 0; + info.flags = VM_UNMAPPED_START_GAP_SET; info.length = len; info.low_limit = begin; info.high_limit = end; info.align_mask = 0; info.align_offset = pgoff << PAGE_SHIFT; + info.start_gap = stack_guard_placement(vm_flags); if (filp) { info.align_mask = get_align_mask(); info.align_offset += get_align_bits(); @@ -191,7 +200,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, } get_unmapped_area: - info.flags = VM_UNMAPPED_AREA_TOPDOWN; + info.flags = VM_UNMAPPED_AREA_TOPDOWN | VM_UNMAPPED_START_GAP_SET; info.length = len; if (!in_32bit_syscall() && (flags & MAP_ABOVE4G)) info.low_limit = SZ_4G; @@ -199,6 +208,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, info.low_limit = PAGE_SIZE; info.high_limit = get_mmap_base(0); + info.start_gap = stack_guard_placement(vm_flags); /* * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area