From nobody Sun Feb 8 22:48:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91CEC1292E5 for ; Tue, 26 Mar 2024 02:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419447; cv=none; b=oxWqmt3bo/QKe+oFgc4LENI8X5aJ1tyO02Pf4KCqCli7wpwrhCHiffZyss7o/STh+vG5MUMt1F59iEywjWYLy9WPl0OVIiGO+vbLQJa3nYM1xveYN7AocM5YHFINiVSLFva+yic/Th2kdQdHUIRrE7joG/KIMcbkISFd1vPpufA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419447; c=relaxed/simple; bh=mZPCbr0rDs8wcMO8G+Ur+xnvhS6uTOIeQkjfiLGhRhI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WwwQuK/hW+wwbVmd4JYA9LNj0lw3H70TwUXP9qu7lQ1oapzGm/xkWFVZjdN8rQSCfrVgcqUm3a3etThh6WBjVnNPOM7hdJTzZd9lATCN0tKlwlULwAM3tCjESPae5dqnbcWPekphIl96TlQBk292UyVirWNM3biz/yIwpnc6fHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mYEdhCIL; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mYEdhCIL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419445; x=1742955445; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mZPCbr0rDs8wcMO8G+Ur+xnvhS6uTOIeQkjfiLGhRhI=; b=mYEdhCILPXAa8FIFejmi7DaWvmj1GpnnOvbatn9a8Jlk0Do4CEhdzrO5 C7e7CKvAOGBgqOT02QCk329IdT1AH4mLsapDiLekWorSZDNPf+B/d1WlC WKP1MD5ZShkc+1ZIRXu1hc/seFO3k9/BGHcppIgayMaoGRpK4MdilLz8B 8e+X2vMgpqOxP/axGFVkNBk/TCUqxdyVgqnV7ENHKo7Fv2c1zuvoveWKW /HkZHngShbT5ZTesDDHSZfbUevKnrqjjoo4rZx0F/cJWcK/HzcQNOSety rJ1HeJtfrLw2tBO43yW0qR3Ns/HhaGy9GyT5i/ryzC+YgEfGEGeR/pIKA A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564378" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564378" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489917" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 13/14] x86/mm: Care about shadow stack guard gap during placement Date: Mon, 25 Mar 2024 19:16:55 -0700 Message-Id: <20240326021656.202649-14-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 75966afb6251..01d7cd85ef97 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -112,6 +112,14 @@ static void find_start_end(unsigned long addr, unsigne= d long flags, *end =3D task_size_64bit(addr > DEFAULT_MAP_WINDOW); } =20 +static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) +{ + if (vm_flags & VM_SHADOW_STACK) + return PAGE_SIZE; + + return 0; +} + unsigned long arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsi= gned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) @@ -141,6 +149,7 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsig= ned long addr, unsigned l info.low_limit =3D begin; info.high_limit =3D end; info.align_offset =3D pgoff << PAGE_SHIFT; + info.start_gap =3D stack_guard_placement(vm_flags); if (filp) { info.align_mask =3D get_align_mask(); info.align_offset +=3D get_align_bits(); @@ -190,6 +199,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *fil= p, unsigned long addr0, info.low_limit =3D PAGE_SIZE; =20 info.high_limit =3D get_mmap_base(0); + info.start_gap =3D stack_guard_placement(vm_flags); =20 /* * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area --=20 2.34.1