Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3059452lqp; Mon, 25 Mar 2024 19:19:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWMGAcWyF8Ey0HtapHTgHetFONz4r5lo0GVxGhhW2ZDzohy2sVEP4qSw7ILGp/RnAG+0QBJ252kL0XZEk9kbNNshxQECvzzzjvoHja25w== X-Google-Smtp-Source: AGHT+IGC2jUL+KKE+2vjYAmPWPJ2Zh1leydTuHnJ6rlx6Fg5H5OuJ6Ug1y5C+AmbzeYh+J7cBCq+ X-Received: by 2002:a05:622a:113:b0:431:3d2c:de18 with SMTP id u19-20020a05622a011300b004313d2cde18mr1732970qtw.5.1711419575580; Mon, 25 Mar 2024 19:19:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711419575; cv=pass; d=google.com; s=arc-20160816; b=BP0Ii7qE3bq5DUCMMwg4W7S38K3aoivzGwbIUfA2JzI+jOUGG5+T84gty4iaWPca03 SgMIr4EtPdYANyXEuecg6j2/aaMLSVYqe91JNIGSP5WoWLIE8yVdRVVvy1C48/1FH8Tn mHS1IP+nncKALjy1HZMpu0VVbf03aPDlJosLe3bvCSOkJ/M1n5aAd2q5al4KI7pgEPwu kHC02wTvVSfHiqYEOKS2eB9l7AwcbNPbDtmwQRwld/KvfBXo1nrGZGfqewM3iI9M+0/A 7kjpTdohtrCOunHTg15odUriiTGCn0tc5LUQY2vQbvy6/JTHazYziLJL7gk2VJeiZbZa p6Ag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dO6RPFI99vr7uz3j2jX2hgFYSAqsHgrD7fWTjzdlt20=; fh=VdawlRosHP6/TRt7Q+d5dFfk99BjWPbqwi77dk1QfXM=; b=LoJWkXRP8Q47qfkw6e9uWFHlXW+ajaznU0OWIUWYfgpSk4qMjyl+EUCOxjfo4CxtuK EriIlwyNtW+JWcef/cAy66UPu+zc9UOk62T5RB9F6/QumLe1NIUTXWxiK+Z21Ui5XlAp Ihss9XRr0WUtttJD7u1wqCUZQ3Q7aiPDa7G+4imtKLQtWHzvbUAW51joFSSZOybBOsd+ BHU1YntmDwKTTjfg2oSK7vVUiVs44Y7pNDFcwtBdAICyPsh2tJmeghf9SS/LCI2Ss95Q 2xCszWIZ6H0+L/kYwHx2Qzx9qzwoLv4an/FoqMXlQcf2UtxY3wSCmdlWkU0B+evXycEf BeUQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mYEdhCIL; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-118292-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118292-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id e19-20020ac85993000000b004314707949dsi5422257qte.591.2024.03.25.19.19.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 19:19:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118292-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mYEdhCIL; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-118292-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118292-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4E16E1C346E2 for ; Tue, 26 Mar 2024 02:19:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F006612AAD8; Tue, 26 Mar 2024 02:17:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mYEdhCIL" 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 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: 8bit 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 | 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, 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; +} + 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) @@ -141,6 +149,7 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned l info.low_limit = begin; info.high_limit = end; 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(); @@ -190,6 +199,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 -- 2.34.1