From patchwork Thu Feb 15 23:13:27 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: 13559259 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 B73DEC48BC4 for ; Thu, 15 Feb 2024 23:14:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D00026B0087; Thu, 15 Feb 2024 18:14:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C460A6B008C; Thu, 15 Feb 2024 18:14:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EC2A6B0088; Thu, 15 Feb 2024 18:14:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 73F7F6B0087 for ; Thu, 15 Feb 2024 18:14:44 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4A71F1606E4 for ; Thu, 15 Feb 2024 23:14:44 +0000 (UTC) X-FDA: 81795594888.20.02D2996 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) by imf06.hostedemail.com (Postfix) with ESMTP id 3D26C180019 for ; Thu, 15 Feb 2024 23:14:42 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="YM/GTh8e"; spf=pass (imf06.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=1708038882; 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=iXPoKvTibJTs5Gz/12bFcL7AnHyYRAgKArZxxbAmZoY=; b=TLk0tMsn/uFDFT6hKBm3uGFOmD9//Ht25ngyWTQmvGsIkUy1fQJfD0Q8ESE6y5wR0AG8Sa RL95H+OufBWcB6kVSqCVT6mNqPFJUrfPvzoRNHVI3TDq6stXTzP8AXpMMos+AoefHtDG8L 6fPgZepyLrKK6PJOkHQ8+kCAWUuPln4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708038882; a=rsa-sha256; cv=none; b=UIXFMH1hE9JtWyLGxsfmHrBvcf6jy/91DotQuMAnh67SSZjwAh1pH6QRrsW5N2TRYtQazI kp/83sauWhBwahEczApa2m8NAsWQyjQvJQYfpPdPE38CnrFbKipAuoC3AnozkEUqt5wNx/ rhf12fMvYOUgVAGii+HBNh3FASk8lNw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="YM/GTh8e"; spf=pass (imf06.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=1708038882; x=1739574882; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=unKAy/CGTalz/NFNrhf3RgWUr6Aw4RHxK63+GyNg8+4=; b=YM/GTh8eK6mmi7U9ztodoNavv2KsLhrv/Sw40BG7ed/5Y/ArNFlMiQdt AhW1OAnRzD+XSqg6dVq8RnXL+IipxMwuQwAYURyYBG3zf3jgjuhj+wvz0 MuUNearUX/GMA0y0QSBUp5mi4/0IreoGqW5jWJo8zQKvGcPCF3FqI0dNT kvCkrS8Kc/CJNaqxDwqNCMpB9+W69FXpZHVlmGLY9gigJThblFJTyIta3 ZHd4h9FL3YPa2Z1P+9ktN5/M+8KUwLGNv98cGw5JgsXby+lBzJDubLQne 5xoie9YIb97VNby7QW8BuQtfaNuQLrq0Y2aHHCu3dqiZKtGWLzYYfPz/+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="2066335" X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="2066335" 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:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="912250194" X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="912250194" 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:39 -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 3/8] mm: Use get_unmapped_area_vmflags() Date: Thu, 15 Feb 2024 15:13:27 -0800 Message-Id: <20240215231332.1556787-4-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: 3D26C180019 X-Rspam-User: X-Stat-Signature: qon844hi5zdbudfb856jhqkjby5n1939 X-Rspamd-Server: rspam03 X-HE-Tag: 1708038882-313299 X-HE-Meta: U2FsdGVkX1+L3uJffOXEWZot9pKoKS8YZ4HD2UFuK3TtEovEFCnvZnbVQTB3Jan4Vbx7tPOnReiLCtWp6+gI9WoOZtwiFmD0LQHn6E6XvWNY455+R+2JDdgKZLr/A1xsjdEOK3B8mHLceCueamei6Du0cmFEAnXL/KCkXUr8VWAfIFt98NVXSY7CXAZauq9cvubwwmBZZWgoWWaw7oh+8LI9nNYwv3psfB+5FE9FYMpa/b0U5geWi2k+qb7MDWheNUnTsHK0h5tSPgxFWORwvwLBrz/G0dJtX4VJUwI2tnMn2le9dIyrjaLqm6SU2IaEFU2TVoclJb+iMStmaz4QSvTVfByN6W3iYaLdM1IkJfPRANtaBAEL/Eta98Qa8HbMsIycNKcawKUIcB1cgqeIrKaYj0VAXXHXbvWc69jmZfbzLJM1TLFu71XocorAErew1NfUP+byUJETWPt6c1OVkEi7nqnd9+Ifmvm0BihrY7dvlKKOxkS8z64kNrTCUqjsL8frIwFZ5X1OKOGQmSBNbzDCesQH/V/hZyDCvaSGy8IYBUmQ8bcQV6/pzBxICNNtHDidAKQy8J62U6bi8EwG41O3mpmnPMp7I3BuJyCokQPJFIw3F8inTMsaeQ3FlcuLZ7hjjLoQSbGYMf7Kgf1h+plLEW8tTsRKfCttdfIR6wg5wFJb2e/rFpvJAHGTFJHbi3vHAqo5ax4XMOrf2bDnAvmGNgrb12+5lyWWXN/QnwKFbHAgM/1XyRwmOL14iod7/gSdl+2HFQCqJQBQwvQvQQK2v1E+9+SQisAJYliFClXtrJOAy6SZeoVft5kdK94nvGJzmPmLC/CB8RHl+edEJYOCb7jBYh55NF2g0V69fPrStkAwzA678loled8Tqg/FEihk1BqfwcL6jZe7DiuZJ3e82iB9jjiBxjxQi/TMEPrMEssmiNrtyvA1NBBc+8ISHC+2w7B2YUPrIpj2RtO sjEv+a5c Flwh+lpBBRY70fjttMnHWPSYV7vmv1iNyjECSlsxKmaWCndl3ZRsSw85h5GO+kcUNAFwntl8ne+GNdOXdt0sukKSaZalQhlSNAORLVym5mUuL2NJEIN4xKXbKGV2HEGROQczo7PM2jglwz0hDZVMp4ewFe5kzREsujix9A9YAWsec5lVlpzvnffqDEG7AMFJU3XqSas6+7RnH9R2Z726fM6WaC5RiwXPH5NG719hR3fMaeg/4pjWXTkjRqg== 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. Use mm_get_unmapped_area_vmflags() in the do_mmap() so future changes can cause shadow stack mappings to be placed with a guard gap. Also use the THP variant that takes vm_flags, such that THP shadow stack can get the same treatment. Adjust the vm_flags calculation to happen earlier so that the vm_flags can be passed into __get_unmapped_area(). Signed-off-by: Rick Edgecombe --- include/linux/mm.h | 2 ++ mm/mmap.c | 38 ++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index da5219b48d52..9addf16dbf18 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3352,6 +3352,8 @@ unsigned long randomize_stack_top(unsigned long stack_top); unsigned long randomize_page(unsigned long start, unsigned long range); extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); +extern unsigned long __get_unmapped_area(struct file *, unsigned long, unsigned long, + unsigned long, unsigned long, vm_flags_t); extern unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, diff --git a/mm/mmap.c b/mm/mmap.c index 2021bc040e81..2723f26f7c62 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1249,18 +1249,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; - /* Obtain the address to map to. we verify (or select) it and ensure - * that it represents a valid section of the address space. - */ - addr = get_unmapped_area(file, addr, len, pgoff, flags); - if (IS_ERR_VALUE(addr)) - return addr; - - if (flags & MAP_FIXED_NOREPLACE) { - if (find_vma_intersection(mm, addr, addr + len)) - return -EEXIST; - } - if (prot == PROT_EXEC) { pkey = execute_only_pkey(mm); if (pkey < 0) @@ -1274,6 +1262,18 @@ unsigned long do_mmap(struct file *file, unsigned long addr, vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; + /* Obtain the address to map to. we verify (or select) it and ensure + * that it represents a valid section of the address space. + */ + addr = __get_unmapped_area(file, addr, len, pgoff, flags, vm_flags); + if (IS_ERR_VALUE(addr)) + return addr; + + if (flags & MAP_FIXED_NOREPLACE) { + if (find_vma_intersection(mm, addr, addr + len)) + return -EEXIST; + } + if (flags & MAP_LOCKED) if (!can_do_mlock()) return -EPERM; @@ -1831,8 +1831,8 @@ unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *fi } unsigned long -get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, - unsigned long pgoff, unsigned long flags) +__get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long) @@ -1865,8 +1865,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (get_area) addr = get_area(file, addr, len, pgoff, flags); else - addr = mm_get_unmapped_area(current->mm, file, addr, len, - pgoff, flags); + addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len, + pgoff, flags, vm_flags); if (IS_ERR_VALUE(addr)) return addr; @@ -1879,6 +1879,12 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, return error ? error : addr; } +unsigned long +get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags) +{ + return __get_unmapped_area(file, addr, len, pgoff, flags, 0); +} EXPORT_SYMBOL(get_unmapped_area); unsigned long