Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1371426lql; Tue, 12 Mar 2024 15:31:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrICdaMLWIjy9aYqeB10GFUnX80bIypI0Zluvcf8+WkrRzB2zAaBdM7MIPno9W44ioszDyp3s/VPd7zGkPLftoL9s5iWtQ+lYrWGhcIg== X-Google-Smtp-Source: AGHT+IHYqFzNt9yJcqyIWl0OnKrBJlop3NkUt4V/hbhkAAtjsMs5jFeWfxwn0C/oNv6CPcCVHMuK X-Received: by 2002:a17:902:ce02:b0:1dd:878d:9dca with SMTP id k2-20020a170902ce0200b001dd878d9dcamr1943014plg.48.1710282679666; Tue, 12 Mar 2024 15:31:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710282679; cv=pass; d=google.com; s=arc-20160816; b=nq78xJIgPimasMUTsLEZLSi9HhAwfmKy1g5u6lLd4QENdDXwQs7dwRhpq2W0ZuYGDO R4yuqPm+OJVXQ8TCBHX/pSAgBU12kUe0BOnfEfF09owKb64ELBcOdRyx25PEutpPDuaU iOOXLJAVODZ5Wh/a4baFxRMJcPKYAjIUB1r3WTOiNwlZrfJIgwJucddxq3wPpovzGjp5 oGgAcy16me7/Cj3e2mJe0gQ++Vr1qm3MzF7FSIAJK03vuz18362z3TOPGxklTu8vj7K9 lkY7xA+nyE7fWKpB1UgRlnhXCJc3FC942QjcKraeZx5JySB2GqhKMCPq7laGo/u/vylq G8ww== 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=LhppU4CMZhZm5EP9rRLemF2K+Rv21AF3TMMjKMbUW+w=; fh=S2XPkYdkY4mz/9jp8Mu2zVT/9ysgbR+pf0cj1KIjn6o=; b=JTh51041brspwzNvXTI8ztxKAg9JMDHHxypb3ojZOFm8v472jYIRN9woKp+Mx/v7xt oJNub6jpSQJS2lI5Fv1BcFM8KnOppIU3XQKQsndESF5LioAHdFxuX8msyEt2m+LjNsVw s8h8vCHvBOIUTCnSCoXYNVfNWNDCxkCkTCfQNqpMLlGNVmJxISssQcOE30yXzvDPAqsb OXZCnj6oarvhvY6yh6P1W/9xcz1zL4JEbUN304SXwnt0In9dgEOGgVgWdedaBcRKfnC0 n+RK/siyCqJFI/MMtWmPHf1n8uDQ3Fv4vsZJqaPkfXnKE39XzEnSIBfQfGwC1XOmWdw7 ZJaQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CwiEAiAQ; 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-100896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100896-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id sj11-20020a17090b2d8b00b0029bb65b4cb7si175193pjb.61.2024.03.12.15.31.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 15:31:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CwiEAiAQ; 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-100896-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100896-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5B4B0288EE2 for ; Tue, 12 Mar 2024 22:31:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C67A7145B2C; Tue, 12 Mar 2024 22:29:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CwiEAiAQ" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (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 2437F145640 for ; Tue, 12 Mar 2024 22:29:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710282552; cv=none; b=guA7oj2MdunL1wW5MTjYD/oWir+rCfqaTNZAd8/g0qE5ApoThYYDlGujmMent42WDR7PW6V1LvvOlZ5/MJr3MZOzvbxfouXVsD690f2pzYaSN5tQG3uBChd01ShCW7jUoDJT3ZCLm39AJbogfDiYg6E25Nbf5Oom3pTAM7BVclI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710282552; c=relaxed/simple; bh=TSd5WZe/p2AZRhCNpPLqqksEWjeny0UmWfCVRjzSme8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=CUp9dCoauY4FRo1+B10rvDaj3qidYapuv3aERZ38SEV83YI+6YiMvfcBVIEjCbAiayvQUQKYGNJ3MU/fltpz7nbaMHJyTOpH2NVXVwPhI5HAf/5XU0DXYhrlQOEp7t3VdluRIZE3qtkUE/i0rOfarXT2G1EKMLby+SoApH3wuFs= 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=CwiEAiAQ; arc=none smtp.client-ip=192.198.163.15 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=1710282551; x=1741818551; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TSd5WZe/p2AZRhCNpPLqqksEWjeny0UmWfCVRjzSme8=; b=CwiEAiAQxiaaJgzmACJGOaxq1ebSWc5WxWBMGXe+DlDRHvjVdF8Gzpti Mkot/mlUV7LV0qGejLkSCuAKMsb5YnKWckjHDEAtrihVzjE2yjBt6HfDC apcaMzguMx+ZO0VJsEhtcXMFCOYG7DzNezR8mDAICZjcmp9rvSKk9T55I vkTkneF0MvJ9l3FUJK6gz7LnzxrMPd9fkXzg8dbzN80R9kIUU6MyJfYAR r7mE8AVDmMEG0+A3cWdUG7qNNURCTpMcQw2C553SO5WqJicdaKEICuPOz vz0QKGWpdZc3xcxUoxgBBwA0Jn9tBYEWJQcAeP6GMxmXMi1AR5haahScH g==; X-IronPort-AV: E=McAfee;i="6600,9927,11011"; a="5192088" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="5192088" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 15:29:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16356883" Received: from gargayus-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.255.231.196]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 15:29:04 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, 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, christophe.leroy@csgroup.eu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, rick.p.edgecombe@intel.com Subject: [PATCH v3 11/12] x86/mm: Care about shadow stack guard gap during placement Date: Tue, 12 Mar 2024 15:28:42 -0700 Message-Id: <20240312222843.2505560-12-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240312222843.2505560-1-rick.p.edgecombe@intel.com> References: <20240312222843.2505560-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 d6fbc4dd08ef..964cb435710e 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; +} + 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) @@ -148,6 +156,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(); @@ -197,6 +206,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