From patchwork Wed Apr 17 05:22:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904471 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 678931515 for ; Wed, 17 Apr 2019 05:24:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52B88284DC for ; Wed, 17 Apr 2019 05:24:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4612928A11; Wed, 17 Apr 2019 05:24:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2FBE284DC for ; Wed, 17 Apr 2019 05:24:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 222946B0008; Wed, 17 Apr 2019 01:24:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1AA056B0266; Wed, 17 Apr 2019 01:24:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C10D6B0269; Wed, 17 Apr 2019 01:24:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id AD3D76B0008 for ; Wed, 17 Apr 2019 01:24:01 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id o3so5261305edr.6 for ; Tue, 16 Apr 2019 22:24:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=88jJnvkgiwUXyeaRCl/DLtIxcpQckhQBrkTZmSxg5g0=; b=uY0YqtefnJFAEqFcuIt3GrJmFN3ZsG3Hl7mDjJrGZEN3p+kBUwREdnzpdJIDjXrXgR 9nZNcOBTSRLtDyQr66U8kGAUliDxHkJM00qJREaH+Td7nhSEqwkdxYZKTTLB8V+QnlRv wl/kGF+2Mdrp3whMcLG/d79SpAmS9LJJnuKGKL59d09Sk0UuyJtj/kFaBNLjM6n9j6LI QK1k5Mt1ElKOS14QUET+4R1QiNWhzQa2zP2jvGT4B/uvBQWyufiWwLuSuayykkKdczsQ 0UEAcSMM6Pz6vWRqVDuWTZkRky3FJz8uNsp0d21l5aeM7HYQPWCDcNN5YRS8kuAHfEUa TbRQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXfIcGR9GY9idkFg5oEO2FPDlqTi0dj1B/devXCvWrIOJz4QI5U xjZ2htk7spmcIYnbcBaT2+zJC1hgVnYc9YXPlnmETI7iusc2/tH6NeutXE0w6xNJZ0a+L+YKxGQ wByXwdm9bEGrnPaQ8FZq2Cw8rU+IRDU6kS/1E7hyg3mJqK6RJgR5M0qZLaSQMQcs= X-Received: by 2002:a17:906:7010:: with SMTP id n16mr28788195ejj.271.1555478641197; Tue, 16 Apr 2019 22:24:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRaOxhKa8m46Hp7hXc+Ey3y7YIxQU9bXCcg9JS3NszAQ4sN85RZlXWs7+6Dwo2ASSrYhRn X-Received: by 2002:a17:906:7010:: with SMTP id n16mr28788164ejj.271.1555478640206; Tue, 16 Apr 2019 22:24:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478640; cv=none; d=google.com; s=arc-20160816; b=glkgAdcooE9WrdoI1Nyv3eOOu6S5oDg22nw65tXNrs4ajTmkKd0IMHOlTx9ayG64fr P33O7Q6W1X1bcYXrcEq03mWoqb+nvL0be5cq25UA6G2sFXw8cYW/DO/j5hVhIpFNoLhH O1TbTgNdzy6LtHIvRs5bXV+Ygt0E682+hj85nxcstSPoQweKj4HCRwC2cNGm55DZ+YG+ G29rXKEqygNI0rm5jmkQu8+8jikoRcvGC0X+qQuo4ZtLdd3TlGpXyG914dawAOcZG4SG 3NS663Bbyc/GUTWHnhDILSRiGl72DU3KriMK4ylpjDQz4bAOXOX4HyS1pL2pxGy3qpST jmKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=88jJnvkgiwUXyeaRCl/DLtIxcpQckhQBrkTZmSxg5g0=; b=o9o4/fONYDG2UEuMIlNPfrg8if972yg4AXPH6HRkf9H3nuI+sOU715KETU2D5eEdMe pSUq2n5xDHHWtfSH8SNzdhV3iZBNIF63TnyoV41R97JDsL+VMcUUpw9RCpggVHrEqk1V s0O916/fsy7G5K5GF5O+i8RfeK033hynLHRpylNZAgrpmVL9gO9sJFQI3jXjanBCfEWi rU+AxfbHGgbuUhgE57MzHdOZPvq/lFseMCFnh56tKLOWzG9TFuAcLFdwY2qwvW2PcqLq ouOWCWPjjK5ynoFkZtSr8yBHd8HPSFKQFhzs7zrbdDeU1reRQi3xhw4pKvR6H2ytINFQ Vt/g== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id jr18si3396102ejb.22.2019.04.16.22.24.00 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:24:00 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 0C4B6FF802; Wed, 17 Apr 2019 05:23:55 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 01/11] mm, fs: Move randomize_stack_top from fs to mm Date: Wed, 17 Apr 2019 01:22:37 -0400 Message-Id: <20190417052247.17809-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP This preparatory commit moves this function so that further introduction of generic topdown mmap layout is contained only in mm/util.c. Signed-off-by: Alexandre Ghiti Reviewed-by: Christoph Hellwig Acked-by: Kees Cook --- fs/binfmt_elf.c | 20 -------------------- include/linux/mm.h | 2 ++ mm/util.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 7d09d125f148..045f3b29d264 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -662,26 +662,6 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, * libraries. There is no binary dependent code anywhere else. */ -#ifndef STACK_RND_MASK -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ -#endif - -static unsigned long randomize_stack_top(unsigned long stack_top) -{ - unsigned long random_variable = 0; - - if (current->flags & PF_RANDOMIZE) { - random_variable = get_random_long(); - random_variable &= STACK_RND_MASK; - random_variable <<= PAGE_SHIFT; - } -#ifdef CONFIG_STACK_GROWSUP - return PAGE_ALIGN(stack_top) + random_variable; -#else - return PAGE_ALIGN(stack_top) - random_variable; -#endif -} - static int load_elf_binary(struct linux_binprm *bprm) { struct file *interpreter = NULL; /* to shut gcc up */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 76769749b5a5..087824a5059f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2312,6 +2312,8 @@ extern int install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, struct page **pages); +unsigned long randomize_stack_top(unsigned long stack_top); + extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); extern unsigned long mmap_region(struct file *file, unsigned long addr, diff --git a/mm/util.c b/mm/util.c index d559bde497a9..a54afb9b4faa 100644 --- a/mm/util.c +++ b/mm/util.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include @@ -291,6 +293,26 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); } +#ifndef STACK_RND_MASK +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ +#endif + +unsigned long randomize_stack_top(unsigned long stack_top) +{ + unsigned long random_variable = 0; + + if (current->flags & PF_RANDOMIZE) { + random_variable = get_random_long(); + random_variable &= STACK_RND_MASK; + random_variable <<= PAGE_SHIFT; + } +#ifdef CONFIG_STACK_GROWSUP + return PAGE_ALIGN(stack_top) + random_variable; +#else + return PAGE_ALIGN(stack_top) - random_variable; +#endif +} + #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { From patchwork Wed Apr 17 05:22:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904483 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 28FC917E6 for ; Wed, 17 Apr 2019 05:25:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1ADC528521 for ; Wed, 17 Apr 2019 05:25:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E33D2859A; Wed, 17 Apr 2019 05:25:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEA0828521 for ; Wed, 17 Apr 2019 05:25:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC85E6B0008; Wed, 17 Apr 2019 01:25:06 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E511A6B0266; Wed, 17 Apr 2019 01:25:06 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1AB76B0269; Wed, 17 Apr 2019 01:25:06 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 7EA116B0008 for ; Wed, 17 Apr 2019 01:25:06 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id u16so2254054edq.18 for ; Tue, 16 Apr 2019 22:25:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=UDJJoD0qMDqsEKa5fIO7G8qbJZY7SvtJV8LUclcf8SQ=; b=P81WpQajIivj1mIEwqj18ekZXFOhMNKg87EdoxRCBRXyS6tOqv4I3DdBqLj/vR0Rx0 hTEwsi3re+7qhxpPb0c5LrZAUjZ1exFx2Ie06NRU3WbB/0/AwCAf2Ajz0eamXQ+vVnKJ uiFG2kKJvSLtmkpvmf6PdtaDx1PqLIsaCtsn/yrdI0wIz6HkPBnnZPZHRtTwgVIVsSgf krZEDrjBAHaIgfe7Yrhxv9UC/7isVAI52B6ILLsluAGXnksV1UQySTj99C/tKO3MH0r7 JK5r7DN4QPQzPH4uVmqqtK8ttzoyDkGYZcIZAixfZfyYhbFs45WnurXNlHVN5OICzovs BltQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXOqs515qilXemhvtafS6jTjL9sLXCuX0Hn4sByItbIfF04lBvx omUIKVIGJJJ0wHvooTYQaFTXGilMd0hHPtsq4ccxLIdiSEWA7L2wKRJpanqrgd5lUaG3LNch9aK CWtkP0LgDOH8DkKWs8jeDbPzBNJZKUHmU3F1/TEXYDZx5PAFBeO+miwDA6T3okH0= X-Received: by 2002:a17:906:6408:: with SMTP id d8mr2530221ejm.185.1555478705947; Tue, 16 Apr 2019 22:25:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfv02vOETR069SY4BYmR/MPl3ISoPu1IU7G59wGQ5SfDZugR/Uvz6O4E/kWJtvJEzfml+q X-Received: by 2002:a17:906:6408:: with SMTP id d8mr2530183ejm.185.1555478705123; Tue, 16 Apr 2019 22:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478705; cv=none; d=google.com; s=arc-20160816; b=Eg1Sqn8yF5MJEvH5Zm6+WajMFoBFkApER6JRT/+IUxgPOZBi/JSnaZk4+gVDvb71Dm nanM1iMmIZOcQ85cxkkbihXTOLnIKs/Bmdmqr2CQdtjZEzCe0a89xabYAfCJXxNGR3ao WUGb7Vwb5qmFyNhSFzabndiwipNWGNjCyXxJl74B4uyNFpnBm9MqvBXLgCO9MTkwIFuf lJuSnjg0NjX7tVt+KMWbdlfNvh+F7jrM1UCROQ5L/CTFVc+mnkMgvgokQjhoW2/STn1k qO1jr1tpdkQ1EIXJ991RLFmxjJWsy2lW+9BuD0B8wL8wDN6zyHsbZ1g9NX7TuT+QrY7z Thgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=UDJJoD0qMDqsEKa5fIO7G8qbJZY7SvtJV8LUclcf8SQ=; b=YRAVXIwjFLnfcAwobhqqM4ueA8xWrAdmgmL1TA4famJL6VbSmm+YguO/HcJ+pfvCWp +sf7NjOLFVTxZoxHRukzievATw6KaaAYgdFLRQfYGqhap4ii4Xg67yxXiVOAX8beMcfS Ug/QT01f4lt9w3kCRbknhxQDh9SLuakwpne6NkLyqxP3V/zGD84SORPT0M+gc/ApJpUf nzk7JFAE40r/mXUFu1XRUk0wZRpvxcDd8xDJiKdmFc/ypeJtkdo39g1n5ZwRl9zFJf4o 4Xo3zfuI+Cgu1tcyUOETjZ5X+uqfI3fBIm3jII7sMi8XNYVemfLpPZCJqIKHBmIVEYgH rP/g== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id e9si3077855eje.110.2019.04.16.22.25.04 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:25:05 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 61127FF805; Wed, 17 Apr 2019 05:25:00 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 02/11] arm64: Make use of is_compat_task instead of hardcoding this test Date: Wed, 17 Apr 2019 01:22:38 -0400 Message-Id: <20190417052247.17809-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP Each architecture has its own way to determine if a task is a compat task, by using is_compat_task in arch_mmap_rnd, it allows more genericity and then it prepares its moving to mm/. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Christoph Hellwig --- arch/arm64/mm/mmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index 842c8a5fcd53..ed4f9915f2b8 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -54,7 +54,7 @@ unsigned long arch_mmap_rnd(void) unsigned long rnd; #ifdef CONFIG_COMPAT - if (test_thread_flag(TIF_32BIT)) + if (is_compat_task()) rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); else #endif From patchwork Wed Apr 17 05:22:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904493 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 78FF417E6 for ; Wed, 17 Apr 2019 05:26:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B2922871C for ; Wed, 17 Apr 2019 05:26:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E7C02873A; Wed, 17 Apr 2019 05:26:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12CB728A01 for ; Wed, 17 Apr 2019 05:26:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DD1A6B0008; Wed, 17 Apr 2019 01:26:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 464C56B0266; Wed, 17 Apr 2019 01:26:12 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32F226B0269; Wed, 17 Apr 2019 01:26:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id D791D6B0008 for ; Wed, 17 Apr 2019 01:26:11 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id o3so5263403edr.6 for ; Tue, 16 Apr 2019 22:26:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=jI/ny0pZOck4kyabjqWPla9k4gZ6U+crF7GP5DfyIb8=; b=JDcLfDwlv5Eh5NSO+h4B0OHPoxKhNdHY0YngWKVrOE/9KjNIsUhpFL9MKEzF9VoVvs NFYUCg6fjM7Zd3q/bun+WMT09toBBuTX4AsacMOkKc0QdaVyNBJMSL2ALmAVW9zjllMt a6HMVa+BNhxOULw8wj78kLMEOC6By3dBCe5J6yrVtmDkchMJNW/rOK7gJLUqwfLMlD0v UVoNiO8DUBwVtl5vyJRioW7uPYcAjTvn4ID6sxpwO5dPzlzgk4nDufAq+utjdyFWE6QN tz5oGeiVQjwPDSlFxkXF4vVPqAigIfXlHjaZub+nUmqYmZoRCd+IAzrBIU2dKvDzhBx1 140w== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAWdSZ6mN+D9WEM41HE+X8vzvUeRPM0Vp7O1kI3IlU3kVTxoKun/ mHALVSeVj9Z3TcsXZKCEl/YkRqZxKLL0U1s4wlhlSVMd9mIfteVCTY7+uRMnCPIgwrV4NixWuND KT4cL4LN6XGp7kmbbqJXHfDzz++J5kreGFgSnnFRs2QznpDc1MAEv50ztsRITh90= X-Received: by 2002:a17:906:5a09:: with SMTP id p9mr47271458ejq.46.1555478771408; Tue, 16 Apr 2019 22:26:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqzM5hC5G46DqlVTUy8wDxDMpnHB19JmlvjaRLx7QY5/7C96//ZeAc4RNDzQN7GVUIBya6RN X-Received: by 2002:a17:906:5a09:: with SMTP id p9mr47271411ejq.46.1555478770363; Tue, 16 Apr 2019 22:26:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478770; cv=none; d=google.com; s=arc-20160816; b=lFG3opUq/jGXyosgGZNdMVw+3j5YSJcJLXKRT3I2d0BMoonlGQUOgl9KRuB8bTNers Qwr3ZnJ/X6b+2hjbMt9dflg5cydVzY2BzLbXrWftzTEGjkTKf0O1ZgPYKLvtkHPY+tij CYLQstW57w3Klt2iLoAGJC+l1VrksyLDcTrv9Snlbe58jXNK29XFSoPM/d+P9wPoubAj mMrXa9nvJwSAmEaGRahPpP5OkSxc0oFAzJfM/QKypZMd+8g25abtb/KNMAlQLgQjCiP2 cyMQC0LNYyWnHjXzl4M6Ah6nJdoJarrmJNPDq1frrD13hawiPmx6RWfohnfDTrIec89Q Lo9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=jI/ny0pZOck4kyabjqWPla9k4gZ6U+crF7GP5DfyIb8=; b=NlLUQXPOrawLaNOcaXllL4I1LtP6LCXbTTBteKCGQLPc8S7K5a9ViDcQgD6vWqdrVV vhza/PUj0dwL3SXqpyRDPKYygB/Bt2v/1TFi0QYlrqqw1MmzWKXOcwpontBnaSqkbp9D VEwd/7qy97gdB15Am7BPQ/P7ACRaNca/oV4i52u1MI/gns4A3BmhpbXnEDnp8mgMbF79 K43OeMsbmu2LiQ0DVcSEHQ9cdEB7+h/s1zW6pjbUDBlyLWip5qU+9XAKtJ4jZp3r826r mbDLm39EqtVfDA26SHXU+VgzkOm9y6Krc6+JTTFf9fKCUtqnFwqKzwYbfESC9efdV7fp 0duw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net. [217.70.183.200]) by mx.google.com with ESMTPS id q40si3597444edd.219.2019.04.16.22.26.10 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:26:10 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.200; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 442A720003; Wed, 17 Apr 2019 05:26:05 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 03/11] arm64: Consider stack randomization for mmap base only when necessary Date: Wed, 17 Apr 2019 01:22:39 -0400 Message-Id: <20190417052247.17809-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.001919, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Do not offset mmap base address because of stack randomization if current task does not want randomization. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Christoph Hellwig --- arch/arm64/mm/mmap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index ed4f9915f2b8..ac89686c4af8 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -65,7 +65,11 @@ unsigned long arch_mmap_rnd(void) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = (STACK_RND_MASK << PAGE_SHIFT) + stack_guard_gap; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); /* Values close to RLIM_INFINITY can overflow. */ if (gap + pad > gap) From patchwork Wed Apr 17 05:22:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904503 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C085E1515 for ; Wed, 17 Apr 2019 05:27:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B20222871C for ; Wed, 17 Apr 2019 05:27:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A558B28A00; Wed, 17 Apr 2019 05:27:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E10072873A for ; Wed, 17 Apr 2019 05:27:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D0416B0008; Wed, 17 Apr 2019 01:27:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 157646B0266; Wed, 17 Apr 2019 01:27:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 021B86B0269; Wed, 17 Apr 2019 01:27:20 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id A20A76B0008 for ; Wed, 17 Apr 2019 01:27:20 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id f7so12164132edi.3 for ; Tue, 16 Apr 2019 22:27:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Z2e1rGEtwWVqovdS4SDd9d80d5Pbnb5D//WW90hZdwQ=; b=B4UO0MSbvR2c9LBGjwUrvcwjbj+srwgcOIZEHZEnVHnQa5hdbqeDfKehuKt9M/is5H CXVw8q1BxEZOt1IVj4YuCq0uhCmRMZSs0W+tQWRedKiC0qdlx5Lk4cS3196ZL3fQC88t cbKp/mAt7YPd5IeioDa8QaEik0xY1l2yolrwfuVkwStZ4JLgBR1GvA7eYKdN+Pnk0sa9 bPwEJuPJxaLGBhdTWRUgoAT53hnAGST8AL4cWYNY1D1C5qHqxE6ZSk0mWlp0gE6/ZNTq SWQVhBHJs2zMv8gG0i8fJprmQf3ztQDhDV7AlgggWNwBPn0iIS5soJB5CJmVRRPPQhhJ hdoA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXfS7hRjxpnjSA0zy+tw7hdCtuushKoQTvxCTy3yvrZT6W5cpid YmNrjBbMKS8wBGJSJXHJqNKdD7dJ+2ON1yyiNK9vlsQBdXWIbBFvWaEeejuBjsvuPN8sZeHfcLW tEB6HtrUPtTwH+fnGa79z/rKzkrUTfQm22fzY8GJgs75V+00GQvwCNKrWO6F3PQg= X-Received: by 2002:a50:b3eb:: with SMTP id t40mr4971906edd.147.1555478840146; Tue, 16 Apr 2019 22:27:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqy5e4LZC4zRou/MK3G4oSv/H9r2BcGSaSAnUPTqaEwbd5YXKYFxoynDVEK3Fe6D/sk8MQ5g X-Received: by 2002:a50:b3eb:: with SMTP id t40mr4971854edd.147.1555478839016; Tue, 16 Apr 2019 22:27:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478839; cv=none; d=google.com; s=arc-20160816; b=SAl3uzQwdTDb+s6+vF6mMdWbyIbOHvTsV2GCJJMfulOWpb5BXJ1X1REAA3g6n1vZVD j0daIPp9jimVlcZvtMGwGouei4c2Sc6zeOf2Z60ETdgF/Zg4b2DwPk1ohGDNaVI1lwNv MOrr6dtKh3aIEphUHGCkDTXMD1ak5qNPt/AIEe9Hi+tgwM2ZNMmvTHoZW3NQ+0dCuaC7 UvayHWFXpDfkItuuuvBBrBcLJIsfyAFKKeHHv79+GTHkg8UTlEvRdLiZhxFtkgJRrdY2 TClGmBzwKXdeIlMOU6hEp/aemmfW4fgYFq022cTBP7HIf4GF3XCnuPyVMK9ZXts5ByFG iSDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Z2e1rGEtwWVqovdS4SDd9d80d5Pbnb5D//WW90hZdwQ=; b=TLIPBXDZOOI1Ja803+Wfv0EGMx0AhENZS2z8CLkDxz4XahjsiH1t7kpMMwJQ+wyo62 BnMYahkN2JCWrDC4ujb+AVeWy1kVM7wYW/zLRCZfd1IUn5EdqoPQnJZyNY16crXK2Dtd G8sCpK0Z+PNpSOPZ3LcZfqdOiGgaTawI5rHxka6fTBJrImDVUVIHHjiLW9LIavVOhZFU 1xVdlMlY+/MoL5S7y5O6RxyhkTiNb8fGL98rZVp8fIfSUqhW7+7MLkfM8GvjWUW4oXPi /Rfg8VX3Lb7jwpvIQd2Q/XdldMO0CR/yKAow5zjfyO368ZKOHMQ9vufta+HoiGLvtAga bU/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net. [217.70.183.193]) by mx.google.com with ESMTPS id y22si133168edc.31.2019.04.16.22.27.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:27:19 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.193; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id A4426240005; Wed, 17 Apr 2019 05:27:10 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Christoph Hellwig Subject: [PATCH v3 04/11] arm64, mm: Move generic mmap layout functions to mm Date: Wed, 17 Apr 2019 01:22:40 -0400 Message-Id: <20190417052247.17809-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP arm64 handles top-down mmap layout in a way that can be easily reused by other architectures, so make it available in mm. It then introduces a new config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT that can be set by other architectures to benefit from those functions. Note that this new config depends on MMU being enabled, if selected without MMU support, a warning will be thrown. Suggested-by: Christoph Hellwig Signed-off-by: Alexandre Ghiti Reviewed-by: Christoph Hellwig --- arch/Kconfig | 8 ++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 2 - arch/arm64/mm/mmap.c | 76 ------------------------------ kernel/sysctl.c | 6 ++- mm/util.c | 74 ++++++++++++++++++++++++++++- 6 files changed, 86 insertions(+), 81 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 33687dddd86a..7c8965c64590 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES and vice-versa 32-bit applications to call 64-bit mmap(). Required for applications doing different bitness syscalls. +# This allows to use a set of generic functions to determine mmap base +# address by giving priority to top-down scheme only if the process +# is not in legacy mode (compat task, unlimited stack size or +# sysctl_legacy_va_layout). +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT + bool + depends on MMU + config HAVE_COPY_THREAD_TLS bool help diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7e34b9eba5de..670719a26b45 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -66,6 +66,7 @@ config ARM64 select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG select ARCH_SUPPORTS_NUMA_BALANCING select ARCH_WANT_COMPAT_IPC_PARSE_VERSION + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS select ARCH_HAS_UBSAN_SANITIZE_ALL select ARM_AMBA diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 5d9ce62bdebd..4de2a2fd605a 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) "nop") : : "p" (ptr)); } -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index ac89686c4af8..c74224421216 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -31,82 +31,6 @@ #include -/* - * Leave enough space between the mmap area and the stack to honour ulimit in - * the face of randomisation. - */ -#define MIN_GAP (SZ_128M) -#define MAX_GAP (STACK_TOP/6*5) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (is_compat_task()) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - return rnd << PAGE_SHIFT; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - -/* - * This function, called very early during the creation of a new process VM - * image, sets up which VM layout function to use: - */ -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - /* - * Fall back to the standard layout if the personality bit is set, or - * if the expected stack growth is unlimited: - */ - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This might go * away in the future. diff --git a/kernel/sysctl.c b/kernel/sysctl.c index e5da394d1ca3..eb3414e78986 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; extern struct ctl_table firmware_config_table[]; #endif -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) int sysctl_legacy_va_layout; #endif @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { .proc_handler = proc_dointvec, .extra1 = &zero, }, -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) { .procname = "legacy_va_layout", .data = &sysctl_legacy_va_layout, diff --git a/mm/util.c b/mm/util.c index a54afb9b4faa..5c3393d32ed1 100644 --- a/mm/util.c +++ b/mm/util.c @@ -15,7 +15,12 @@ #include #include #include +#include +#include #include +#include +#include +#include #include @@ -313,7 +318,74 @@ unsigned long randomize_stack_top(unsigned long stack_top) #endif } -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE +unsigned long arch_mmap_rnd(void) +{ + unsigned long rnd; + +#ifdef CONFIG_COMPAT + if (is_compat_task()) + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); + else +#endif /* CONFIG_COMPAT */ + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); + + return rnd << PAGE_SHIFT; +} +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ + +static int mmap_is_legacy(struct rlimit *rlim_stack) +{ + if (current->personality & ADDR_COMPAT_LAYOUT) + return 1; + + if (rlim_stack->rlim_cur == RLIM_INFINITY) + return 1; + + return sysctl_legacy_va_layout; +} + +#define MIN_GAP (SZ_128M) +#define MAX_GAP (STACK_TOP / 6 * 5) + +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) +{ + unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; + + if (gap < MIN_GAP) + gap = MIN_GAP; + else if (gap > MAX_GAP) + gap = MAX_GAP; + + return PAGE_ALIGN(STACK_TOP - gap - rnd); +} + +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) +{ + unsigned long random_factor = 0UL; + + if (current->flags & PF_RANDOMIZE) + random_factor = arch_mmap_rnd(); + + if (mmap_is_legacy(rlim_stack)) { + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; + mm->get_unmapped_area = arch_get_unmapped_area; + } else { + mm->mmap_base = mmap_base(random_factor, rlim_stack); + mm->get_unmapped_area = arch_get_unmapped_area_topdown; + } +} +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { mm->mmap_base = TASK_UNMAPPED_BASE; From patchwork Wed Apr 17 05:22:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904519 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB62613B5 for ; Wed, 17 Apr 2019 05:28:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B7D932871C for ; Wed, 17 Apr 2019 05:28:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC2A428A00; Wed, 17 Apr 2019 05:28:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C98F2871C for ; Wed, 17 Apr 2019 05:28:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 529D76B0008; Wed, 17 Apr 2019 01:28:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4FF7F6B0266; Wed, 17 Apr 2019 01:28:26 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 417876B0269; Wed, 17 Apr 2019 01:28:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id E1BF96B0008 for ; Wed, 17 Apr 2019 01:28:25 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id q17so996679eda.13 for ; Tue, 16 Apr 2019 22:28:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=akQS/yG8sPhImfKfbdP4IgmJO6rV8gx2SpQpdbx2UzQ=; b=KxOYJet5btkbe2bYNd/1RKbQkwq+kG5hR0DI7UC27umpOwAyES7Vay9LDdbH4hMUNH C8fACSZ+XXmRmy3ZXEyouAWfVsXCraM55MRBSDTMA9hVs5D+4xTTD6YsBO88gXr2MgtX w3QvpPkEQwmjLjMkimylXez4ykVw1Ca+/rxPvYFosyNf+TaOG9dUV6mlplnxa/k/qIVY sCGpNjuph72h1WvaEqOj0a4iLWxM9m+opfOy4ESsdl03cKmovJ24glJvnxXSFWo+fnbZ 2ejYq3P3Hm0/VI7zLsN1xWt2u90L49xJK80SbaLZY7/c7+wqwRyOXdNy8K1OpkEtK7c5 aNiw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAV3qRKhNXVWdgwMYawkCQOGTGZaU/T3WEt6FWtS8kq0KqhsWjtD p4sV0na5jkmDlAxL2YaSvPAJe5KkGnC+lSOxBpofabpHCJC8dAj4ZvPZsByVXTU2ie+8mJ9lz/I LLVjSUf55QZoxAhe6xjrl8D5vrRmvdh+L30OnfIJz6+PaUA1yCQWnBLnk+mvPOZA= X-Received: by 2002:a17:906:ddb:: with SMTP id p27mr47118845eji.183.1555478905421; Tue, 16 Apr 2019 22:28:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/Ife5bTAxn65rlNwJ8nJyX5c86fbRqxflgicdf/EJ8ehr2As5odRuqLEw8D3HcouRPIJk X-Received: by 2002:a17:906:ddb:: with SMTP id p27mr47118812eji.183.1555478904559; Tue, 16 Apr 2019 22:28:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478904; cv=none; d=google.com; s=arc-20160816; b=M9bKwEeVsD765kULstuvkuGCxDufWJYnV+wm32/xwIybZOzQoFCyWRChhTr2VzUZXW zck/hLc2ym8nwDRyOWfXrzhgls3AqUTccEL1UFS0XNqfsLohKOJs1k3gi3Vdbk3/IucP Pe0Ea/h2Ky427pwBut5i/P//43Qbb9CnU5NoHDLOHMWpSQtGlGOfBHqDDUzXI/elRfAY d+OOFSC5f2BGGxfxduz6QZqx+JWYAQ9dVeSCLY5PC6UsZGOeh5uQpG8hNVa/QcqsbiYJ CG/N1RZA0bTviPANJkKYu6rh46bGGpNMBUKOx+OrV/teuGtsSjF7KRYifWne2WRHPHTw +KgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=akQS/yG8sPhImfKfbdP4IgmJO6rV8gx2SpQpdbx2UzQ=; b=tm9xValkBLlnVFLgUBSKltoBIVPhdl1ClLpCwSMFmW2u4kuu4FbMv+hAYnRfFRYtf8 HFcvZk+fHMr0mYHyhvU0Zx3YXDlkQk10LYaVO4Q01TOSHLFGwp1PERZ4UtYztGXqrrYp Ny1t0NAvvCpe7brbsPoAa+XM0htFt90xGI5Mx22GsnImBeT3ZQfc0/Pq6CU2bWEVyaDF p+u7aYWGri+mjz0nehgeDXzhJRFytkkPRX/5nH5TkXKC1pJ0fVa/2tcImxhSvmUAhLAo 4ZR51AzX2wdiH0etyNK2HmX6EwldTP+F1YjGK6dOLgdlKeosleBoDy41mUwB3mMJVOmt vEFA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id m2si6630031ejr.38.2019.04.16.22.28.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:28:24 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 3A2E6200008; Wed, 17 Apr 2019 05:28:18 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 05/11] arm: Properly account for stack randomization and stack guard gap Date: Wed, 17 Apr 2019 01:22:41 -0400 Message-Id: <20190417052247.17809-6-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP This commit takes care of stack randomization and stack guard gap when computing mmap base address and checks if the task asked for randomization. This fixes the problem uncovered and not fixed for arm here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/arm/mm/mmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index f866870db749..bff3d00bda5b 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -18,8 +18,9 @@ (((pgoff)<> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) { @@ -35,6 +36,15 @@ static int mmap_is_legacy(struct rlimit *rlim_stack) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; if (gap < MIN_GAP) gap = MIN_GAP; From patchwork Wed Apr 17 05:22:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904525 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 088071515 for ; Wed, 17 Apr 2019 05:29:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E48452871C for ; Wed, 17 Apr 2019 05:29:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D402828A00; Wed, 17 Apr 2019 05:29:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76CC82871C for ; Wed, 17 Apr 2019 05:29:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1C9E6B0008; Wed, 17 Apr 2019 01:29:30 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AF22D6B0266; Wed, 17 Apr 2019 01:29:30 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A09A26B0269; Wed, 17 Apr 2019 01:29:30 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 515CA6B0008 for ; Wed, 17 Apr 2019 01:29:30 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id y7so11976247eds.7 for ; Tue, 16 Apr 2019 22:29:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=qLIfa0rcn3uuBSuGdyQd2indykfbUvCgupWpuxIE69U=; b=r6ZL0rJwAkv/6sqNvjoNBtvgGMhHHeXC2+bt0faUWc7MyQkK87SL0qn6PyLyJnmb+x euyINaI/cS14MlZVO2jxp4Njt1RMu1R7/LQSZU4+XuaQe7EQXOVUc7B4jBHfc79t8aJp feDr9N/zl7/CN0U7tv6i+FjyxqVDwSw/UbP/Git6W9G8w6wdFvZQ3OXg4zrVds0tjLif UNx4eY2ObwJQNnjJIrHL7OxPyJ8TSV4yMIGnx1NrsYp7HCjP8I9DjHzarFk9jsOTvdDL My0tuGwUpWZmcerMNrZimeyidzoYRde01VvKb1jlCf0kwcxO+09IMKqNB8pYoCpvld6i 1thQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAX+mkXd8fpfApPg98i7DSTZ2+o8NSY7L2C+SnS6aOoRLGXKsNvt caFNVz7z1qS+r9CY50xB20bwDikPGBdo8K1LsK7xaXPXMeHaT8juwDLUPJE3RYNimaUL0H8WOoE pek+vyO7/oVnNOgNoHX9PhMQxASkYd+kcwK3GCYO06+KEIWDvwTbJNbtoF0j5omM= X-Received: by 2002:a17:906:b756:: with SMTP id fx22mr39746624ejb.192.1555478969850; Tue, 16 Apr 2019 22:29:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqxGqhVpboCJfqfJfIDYs5iB7GfqYlmKgbWcLMrWw2N7ryAXJqLErb8DoAJxoAxPFu+XKjQK X-Received: by 2002:a17:906:b756:: with SMTP id fx22mr39746585ejb.192.1555478968722; Tue, 16 Apr 2019 22:29:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478968; cv=none; d=google.com; s=arc-20160816; b=v/YrMrd0IF5x9YAY9rv83Ko0hLkAefsr7PYX3+fLz4b1NPK+Tx8rkmeGkkxhfW8aWU GGqJxlBa8EbNoJag54tM+FoQpoAIfiYOMso9KXs3oSiZxNe7EIBBhqZ2N2H9enosJdDh v+VhxzC1CpntOIjzddWApJGMCfuHh5aqsJ/szj7OxL3fNal8FctmqX7y0wEymXfF/S4Q 0EZTO8sQCn4nJa2SJJwigrHQvfUmPtiNbkOoY5XfHx+tPRswN17uLKqqzR7nNgnWZXup OIxmEwPEClaiR/YAb5tfhGT5oz5vO0zwVBm4HmywhBpa+Qd/SrZI6iMNhONNN/GEDJnm rDng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=qLIfa0rcn3uuBSuGdyQd2indykfbUvCgupWpuxIE69U=; b=FdGCxJICe/vk2D9Ow4dcmumum92ko+pxYM5rVR8Zoc2FPZt7gC5m/v+S5LQ8V9kQZ/ ZyNofQQrjLuqXGfmLutbOlDKZ3sGduaL6Zgolymaws2ceR+UPp0bHok39WXA7WMdvrvH xcY18wSWFbBvQZK1MVg8KxAX6Ojhfnm95NqDSgW1/WE+UaHCnEa2drOWom6aZtTNsaOQ mkDhUGOP0FvTW6OYf339z/vZZFnnTg8hRvK5V2aXYmboN1o+1GDpSNaMU7QHTRIT4kLx N/ct1BH20YcScKjyvbsn370zDm1Irz9Yq0+Elz8kVbom3qoGD8oDxox29ZhxtWi0Cgaf RZ1Q== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net. [217.70.183.193]) by mx.google.com with ESMTPS id os14si3342628ejb.155.2019.04.16.22.29.28 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:29:28 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.193; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.193 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id AAD82240004; Wed, 17 Apr 2019 05:29:24 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 06/11] arm: Use STACK_TOP when computing mmap base address Date: Wed, 17 Apr 2019 01:22:42 -0400 Message-Id: <20190417052247.17809-7-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP mmap base address must be computed wrt stack top address, using TASK_SIZE is wrong since STACK_TOP and TASK_SIZE are not equivalent. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/arm/mm/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index bff3d00bda5b..0b94b674aa91 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -19,7 +19,7 @@ /* gap between mmap and stack */ #define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MAX_GAP ((STACK_TOP)/6*5) #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) @@ -51,7 +51,7 @@ static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) else if (gap > MAX_GAP) gap = MAX_GAP; - return PAGE_ALIGN(TASK_SIZE - gap - rnd); + return PAGE_ALIGN(STACK_TOP - gap - rnd); } /* From patchwork Wed Apr 17 05:22:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904537 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C63E613B5 for ; Wed, 17 Apr 2019 05:30:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6E0628915 for ; Wed, 17 Apr 2019 05:30:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AB16E28A00; Wed, 17 Apr 2019 05:30:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3792128915 for ; Wed, 17 Apr 2019 05:30:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D4CC6B0008; Wed, 17 Apr 2019 01:30:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 685006B0266; Wed, 17 Apr 2019 01:30:35 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59B7B6B0269; Wed, 17 Apr 2019 01:30:35 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 0B2F16B0008 for ; Wed, 17 Apr 2019 01:30:35 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id p26so3816467edy.19 for ; Tue, 16 Apr 2019 22:30:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=of8M16skONcMMW8lly/0Bgnzg2NbL2wb3TkAt3jYEls=; b=kApP9VGg/3Fh0HH7Xo+VQXcC6Acke8IT8ub+jpd3edah2BbB6+h+EH7z5/phRmxtJC sRYkkyfqsMwUJOojJY6nmD3GDI7cr8UGuqintTViwMLhIa9nUkN4OwtHC2O6259i0pYC Kb1H7JT36zL/DfOMdG74/bujXby7UV7bbYDoGUarvUZCFHLfiPjxZ13SilnLd1D/v/Ll 5Fx4ddhTRE5fNbzHEpB3BpFw2PwrW2TNF2iBs2f/0gfVXWjd/p2dDFKJD3hA3Gnyu9tb 60TaTDWld8Zb9wnemOprZVheYF+wQEEctaU3CLfXQkLFyADdJuTAqp4l8Wb9LAzfjj53 cS9Q== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAVElyeOB6pRXbgnLP5IT0+R8ZtnOpK8h487+jd+359VXB2tBD8j uUUL4ollsX0Bnrsgkmt6RmLuQICO6eNcgQKb/ytAe4dN685e8/PEON8G2ob+IVeq9WF3gT3nt4D k0+rfb+RYkM54esCm/y9wwenTd3HxVo5MvWN9cX4rzY+Wl7YJCR/PEAm0b3GCwVM= X-Received: by 2002:a50:eb42:: with SMTP id z2mr10390828edp.56.1555479034559; Tue, 16 Apr 2019 22:30:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwkrIgDuLJ+PgVkDhOnsOzWks/W6PCtCLK4SxNgARbEJq0ooeCX4y6QSVc+cg76UuF6DQrh X-Received: by 2002:a50:eb42:: with SMTP id z2mr10390757edp.56.1555479033175; Tue, 16 Apr 2019 22:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479033; cv=none; d=google.com; s=arc-20160816; b=0bILkwkg4jVJ4W2QmCsUwaA/B/f9EaP94Pul3j98YKHeSIKK8CcE2m3zZLwcEjAwve ukKVjJb85PzaFtj1qQ7g2p5/Re91JpgfPFYm9mRnDmSiQjZzODCSUiwGWrJjgfHio+5Y 3X1jdjv2xJkB8ahPPE3g0TlUI7vDXGcqX9ENgMyd+yu5ekZ/LnWpXPYRWWpDnG020d7X Vqf7kbrNGY9u+mO6CXr8DPaGX0W/tie1lAN1SjzMT3UAcjdo7Y4Nk2jlkutsDfyZaUtZ K3pY3nPoTDzvSaDdjcc2aHiOCvzC1TaiKEYtrD18eTt1dJol5p3FchlQ7VnIjI/1CqEA RWKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=of8M16skONcMMW8lly/0Bgnzg2NbL2wb3TkAt3jYEls=; b=rdrCOCSSdN7BY7VNDuc/33N038l1T1OoQ3ZVbpCG2hpsrMZPpryRNWe1+u1W2Yvu4m wt2chLV7OesETkZFhrReA0YOhRV6UIL3B9Ku4CGdDuyyhSsFrtZemDzAIPZGj5xziMut nOaQU26hCoM2qquoFGh6EZxqeKT/njVXIZmNH+j4R/ivm8EqZ+fy+6fIwWa4Mhr9v0pp TcH2gW1FgPaIMg68crmkG7YEYQSu9qU8YLJ2Eq6svf3OTjjsz5xUlo4BiD5AG+u+5KoG 72Yp/blWyUcCveDcmOh0mTMnrLfJk+0ibwC2WeGMAnn1lwZqx9KSVJo01Q1EuWC/2KEG LvGg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id s9si7217196ejz.181.2019.04.16.22.30.32 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:30:33 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id D715120000A; Wed, 17 Apr 2019 05:30:28 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 07/11] arm: Use generic mmap top-down layout Date: Wed, 17 Apr 2019 01:22:43 -0400 Message-Id: <20190417052247.17809-8-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP arm uses a top-down mmap layout by default that exactly fits the generic functions, so get rid of arch specific code and use the generic version by selecting ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/arm/Kconfig | 1 + arch/arm/include/asm/processor.h | 2 -- arch/arm/mm/mmap.c | 62 -------------------------------- 3 files changed, 1 insertion(+), 64 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 850b4805e2d1..f8f603da181f 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -28,6 +28,7 @@ config ARM select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_IPC_PARSE_VERSION select BUILDTIME_EXTABLE_SORT if MMU select CLONE_BACKWARDS diff --git a/arch/arm/include/asm/processor.h b/arch/arm/include/asm/processor.h index 57fe73ea0f72..944ef1fb1237 100644 --- a/arch/arm/include/asm/processor.h +++ b/arch/arm/include/asm/processor.h @@ -143,8 +143,6 @@ static inline void prefetchw(const void *ptr) #endif #endif -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif #endif /* __ASM_ARM_PROCESSOR_H */ diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index 0b94b674aa91..b8d912ac9e61 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -17,43 +17,6 @@ ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ (((pgoff)<> (PAGE_SHIFT - 12)) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - /* * We need to ensure that shared mappings are correctly aligned to * avoid aliasing issues with VIPT caches. We need to ensure that @@ -181,31 +144,6 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, return addr; } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This * might go away in the future. From patchwork Wed Apr 17 05:22:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DF07213B5 for ; Wed, 17 Apr 2019 05:31:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C7B042892E for ; Wed, 17 Apr 2019 05:31:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B646728915; Wed, 17 Apr 2019 05:31:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4890E28915 for ; Wed, 17 Apr 2019 05:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80D5E6B0008; Wed, 17 Apr 2019 01:31:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7BC416B0266; Wed, 17 Apr 2019 01:31:39 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D2806B0269; Wed, 17 Apr 2019 01:31:39 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 21E486B0008 for ; Wed, 17 Apr 2019 01:31:39 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id e22so10505005edd.9 for ; Tue, 16 Apr 2019 22:31:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=3oOhKgf5++Zcd2NhUlCVUeA8BDKs7cw/2jMalEBWqIE=; b=JyxarhDBPQlimUyiNgAK7/JdC2uXxrJFb2GD9Yh/K7aqPXO7mV9jYDqUoS2Ka1Avqg J7z+FIfEqj3pKdJD6d89wl5U45tJWj2RK4Gtt0xrzWD4Uj4JGD/J87pqD0N86pJ/vSQp NnzNzdxDLV/vIxcZDyXImEHmNMuSSlSKolCucUB14E9XHoFwcQe1ZsoP1MeIw4BaqSJp 7YkyKR2MJnfcoNMwAhDxwqwxgBWfPyAQSyPSC7CuvHmLW1lqqqlGpTaCA1mFTrl2qNRK 3bgu0AAKu4VzPTkXGnIEiHNwiQVqtb3drZA2MJomaVtLkZKvQtkFC4w28nzEEuJ31WGW TXLQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAX4juyPqmm9nMLTT9TuHdd0GH/0LrUgrX8Nb+Cxa+A21367sNwe zXMyG2908ubLhy+RARGwG3MBiJk/lhkofhFCtStov92WCqPMcL/0NLmJZmfSuSmcNROYv00L9sg Rm0w9CP5zqyYFWdsoY+bSo4njmml/IOERMVhcsr1IQkq5v9MI/DUM/t++CcGchCE= X-Received: by 2002:a50:b646:: with SMTP id c6mr53686590ede.150.1555479098616; Tue, 16 Apr 2019 22:31:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqzXlykVJBKY8sdHHFZRkjtuurjgt2A6cqcbbwVmaCpuW00R62E5YT/eRchNBR99Xy3qDal7 X-Received: by 2002:a50:b646:: with SMTP id c6mr53686551ede.150.1555479097804; Tue, 16 Apr 2019 22:31:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479097; cv=none; d=google.com; s=arc-20160816; b=WStiVbRQQU/tkO9b9pvNt8EOUIWcESLBvD6acGq3aT7nsLDv01nLm24OZmhZ+vjb/J aH4jSEUn4+DXXgKY5OMDtlnA5Wh/tZYfy8Qqrs1Enpuoy8UArKht3H+gXyKgZ3e9Neh7 XtCD/PnS4mUxGthV6zme1COBJwU+8FZT/ShIytYQWcIZEZOBOPIHcuNdVN/js16mYuNh FeL8BH2yG6aqZDpaInRADpwzOJs8JCB+XL7Wj9mpZOMzmFWjKCkZCIOuLwvxXLQAuySR 39yld9beziqLyKuu8HCFAaDxc8lhJ7S9YmYI7QnK7zd5iVDl97m5Gca1F0QsafvD2LUE MrUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=3oOhKgf5++Zcd2NhUlCVUeA8BDKs7cw/2jMalEBWqIE=; b=ifKb+jJ8zm4dwnsjCPP0JP1BLhXSjof9G1bBZknbHxQ9v7cu8XM3AjVFeRaeGUuba9 O4vLtfNhgESwR3X2zCQZvfoFG8cCflcQYYt7SolA/sx7gPyG93z/owPIYaF3JJ+PxUh1 GJdy1JMccood5w/QJH7EP09sqPBKIPTbYI2uLDXSWtpBb261HVaixCV2QiGn6qx0kzXA OmhzlfFXPbjuxeymuxUTHgWmrIyegJnm2ifA9uGQq4H9dRFJGraBuTaEQ3tOTvMXz+wh loQv+LDBdUUTPKFO6xSpneFLLd1erxgQmje6qfzJl0hpTDEd37yJ/eyd64kt6Sn0BPG2 jN3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id r5si1311983edy.227.2019.04.16.22.31.37 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:31:37 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 4BE7CFF804; Wed, 17 Apr 2019 05:31:33 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 08/11] mips: Properly account for stack randomization and stack guard gap Date: Wed, 17 Apr 2019 01:22:44 -0400 Message-Id: <20190417052247.17809-9-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP This commit takes care of stack randomization and stack guard gap when computing mmap base address and checks if the task asked for randomization. This fixes the problem uncovered and not fixed for mips here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/mips/mm/mmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index 2f616ebeb7e0..3ff82c6f7e24 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -21,8 +21,9 @@ unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); /* gap between mmap and stack */ -#define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MIN_GAP (128*1024*1024UL) +#define MAX_GAP ((TASK_SIZE)/6*5) +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) { @@ -38,6 +39,15 @@ static int mmap_is_legacy(struct rlimit *rlim_stack) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; if (gap < MIN_GAP) gap = MIN_GAP; From patchwork Wed Apr 17 05:22:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904559 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 516B613B5 for ; Wed, 17 Apr 2019 05:32:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 43A2228915 for ; Wed, 17 Apr 2019 05:32:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 37C0B28A00; Wed, 17 Apr 2019 05:32:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E0E2F28915 for ; Wed, 17 Apr 2019 05:32:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28E6D6B0008; Wed, 17 Apr 2019 01:32:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 265756B0266; Wed, 17 Apr 2019 01:32:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17B476B0269; Wed, 17 Apr 2019 01:32:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id C2BE16B0008 for ; Wed, 17 Apr 2019 01:32:43 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id h10so4056454edn.22 for ; Tue, 16 Apr 2019 22:32:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=6D+bml0GAKMUKHe87npe+2LGj2+9T7+GwcJmjpz0elM=; b=avxJDEKVgoviBboW74xj9KkOFcMlXOlHOlsYHQexHEkyp4XLEOPCKCMnBQY2secwCg SYvDTc9VMqxt6qLttkfqpnmaXIeC/Wxi/XjqgVWPE3rUFRh4JgxOdSjgn/E2TWuuTyJ7 /u6Po7upVG4U+ZRJJgOj/99X2/+B6T1J+Dz6bbHBYHgG/4wodmFx8gyK+6Wckwg8qxE9 7FMuPAWyBhOu/k9uxqMASrfJkhVUAYjaL1LCttKISOmabCliAS0cO3FTCT4dQhYS8Qfd sSEi1+LBetdxXa5WzCKC7A2hUO8wIVJcmb4fgKLHnjNiGpo5tvtBglwbpuXZjUF3dWjY r02Q== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUnyhA/xzJIh71YqK+7V5+BXIJLCzfNeED5wD6m+mazLIWRy7Gr fKxwgdMt4zWR55q9QmW3OA9pG8GJ+Ws9whZXRwna0KV91jRhI9NAD0Dk4+KV/pf5VTNEFepG6Y4 yF7wjd2i16Pd/Ofx0RcirQqMTFfxhcACgGEQeTkwidfGiXsO3Rpo2S9rGaEaTKXo= X-Received: by 2002:a50:c44d:: with SMTP id w13mr25701070edf.50.1555479163323; Tue, 16 Apr 2019 22:32:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqzHjs7/Zoo8NiOu9FhNS2OJVaUQSjg6RW+5oq9zeY2IEwu6KiInKs5TGYndUesL8as/tn69 X-Received: by 2002:a50:c44d:: with SMTP id w13mr25701007edf.50.1555479162318; Tue, 16 Apr 2019 22:32:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479162; cv=none; d=google.com; s=arc-20160816; b=L2/vFXMum486HcFfBADhIboNcmGL5gxoibZ/HE15s4mLIysFfGPTTFmdv14+sz/x9v JeeftW5ThfYcRJBz0oDKxFZJhkr+Pp1ut0PxlooS9mIu16iLnrFLilNOpVTHu+r8UNY5 sW373Y6bf4rcn7mcxbOBfHcTzVaw5rpR4gwqScwf55Q3ymlQZl7chdq4hEZQiyDzbnD5 NG6aOYE7WE623nr5SCRfjv9QZ5rCCObwf0anuzCs8FDK+fNvxcpfHOn6b/O2Fh8u2Yz8 vkyxuusB26oOWgS6HCCC4QKTH9TnUY3BWfj0YCCWsSyxmtmbSWd/nzPJKlDRXFFIp3TD DK0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=6D+bml0GAKMUKHe87npe+2LGj2+9T7+GwcJmjpz0elM=; b=gk791zp6J//I2i6vg5GSt2NXn2SAEvqSiIzFM+H0yZkIrekRib6yI+vBOVrE/zPgSZ 4cj/fCgPlZlHuG/m5iCaWkTk9jbwo5fe23ri6SSdHxYL9iDyagPWFsQiX6aicJtyRaKd biIt5T0q32DhYKjN1yH5BABuXeTko/Z7lSaf4jg9Wwfu6xctolAlNCrFVObLEqjNeU5w 8nKo3nANLi9h5E3OhkWXb9ps2wwQNUqPXNzpOvZMKP2QWVtsRczWVFApMhf+3NIRLSLl sJKj073MAymKTOl2MJjh4J+82qn1x4ljlPzzNBQ844e3lKihxeGoBVZG+iCbH/9Ot3AN 0j0g== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id d6si5581786edo.288.2019.04.16.22.32.42 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:32:42 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id C815DFF80B; Wed, 17 Apr 2019 05:32:37 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 09/11] mips: Use STACK_TOP when computing mmap base address Date: Wed, 17 Apr 2019 01:22:45 -0400 Message-Id: <20190417052247.17809-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP mmap base address must be computed wrt stack top address, using TASK_SIZE is wrong since STACK_TOP and TASK_SIZE are not equivalent. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/mips/mm/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index 3ff82c6f7e24..ffbe69f3a7d9 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -22,7 +22,7 @@ EXPORT_SYMBOL(shm_align_mask); /* gap between mmap and stack */ #define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MAX_GAP ((STACK_TOP)/6*5) #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) @@ -54,7 +54,7 @@ static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) else if (gap > MAX_GAP) gap = MAX_GAP; - return PAGE_ALIGN(TASK_SIZE - gap - rnd); + return PAGE_ALIGN(STACK_TOP - gap - rnd); } #define COLOUR_ALIGN(addr, pgoff) \ From patchwork Wed Apr 17 05:22:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904571 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E4EFC17E6 for ; Wed, 17 Apr 2019 05:33:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D51F42892E for ; Wed, 17 Apr 2019 05:33:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C8A8E28A26; Wed, 17 Apr 2019 05:33:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F2BE2892E for ; Wed, 17 Apr 2019 05:33:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EBFC6B0008; Wed, 17 Apr 2019 01:33:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5A2E46B0266; Wed, 17 Apr 2019 01:33:51 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4653F6B0269; Wed, 17 Apr 2019 01:33:51 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id E518F6B0008 for ; Wed, 17 Apr 2019 01:33:50 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id f42so6888463edd.0 for ; Tue, 16 Apr 2019 22:33:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ofjbywuwMy7Hm0IDjqbFaAHELAPZEgqhyfFsdeTc4hk=; b=kq29CcfYePuCa/ZQKzSO/rZrimOauFhsJd6QgAvWEKw6CY+sJPelJ5pAQSSDnfnX7q 9tAwbRPqYPuM6gnMbtIp1GlWSCUnVnAjhPrl6sUS52cEMIIktHJnJ47sn75LKB353IYV vQ2+Y1O3w7AFnWv1cWIh/tHH9dso4p92NLl2kFbqnDUjco07yuOqHwpLx3fNIjObY2Yl IUkKjHA8ais/K6XlS03kuZhbZNah1O0O4kjBi19L4DoKyWbBC1wPJ089RfnseYapfq0s bfiQULyNp/iuk+nhEHsUZnh4og6iAkeZSEsNdrXG5tQbBtPlsoHlGUjXvWBnlItvfzVo fIMA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXfWROXX5V/iXlEsOVtGqDcz3urciQcBRz1re3AnrFr5XslR7kd v3uXSM/ZmM0dsBuYFiHOJWyMV4yTcMGRUBkvxEjg54Hh+RWp6zgcqgyN1xNfcW0vCJxNKMztdJv YSd3KsAkPNCGEv9R/vXaOLdpGs8YZ2eOZVA6zPFxDRTXt31koeqelzZaK8Mzxn1I= X-Received: by 2002:a17:906:6c0d:: with SMTP id j13mr46618982ejr.249.1555479230427; Tue, 16 Apr 2019 22:33:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqy/Nw7WZ8RwmACzwCPnI7PUpXOnBnTkVgyNy5WzbQ30DWDjlVxQ47WDvYyc9FJ02OV0sP8P X-Received: by 2002:a17:906:6c0d:: with SMTP id j13mr46618938ejr.249.1555479229388; Tue, 16 Apr 2019 22:33:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479229; cv=none; d=google.com; s=arc-20160816; b=RR5psYDKmRNYYYU1caRJsEzZb5bZ7eEYSARh3sjeXN6IhwGISWDTdrM/79bPxJOjv7 Qa8Bf+7A4A3oWIpBJHkOa05qvrKlzr5m2jduKIUJSlFfSZM/b1d2AWEW/tRTrsQOK/Uw vUAg0sKMtPjrY7b+0NhqKonS2x5LZzWHTQ012BLgDz3z+fuIx4cLaN8SFdZ8eFpeqyCr fOfN6oSWW8A+sSwuW0v1/CQxjtLs+KpXIEM4sPZ/71iFyADWBWf2tHXZXXFDwAhirY8t h+b2/e9oP+vHT9bAT+j6RXwNMZ1s9AclvLUh5GDt/7G2JjG2o7bmaVYAgUB9kFtQ49rL dOxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=ofjbywuwMy7Hm0IDjqbFaAHELAPZEgqhyfFsdeTc4hk=; b=04XkzONSp5eKOcuJmfUIx8nyV9SwE74A62X4FmsEGvnbi8e028Uo3WsJ53+HgzkUPl KbIv6Y+i4fscrbM2wgPvVN/MAJUUtOrvTx8qRDjXj5WWCRZWZuIcpRIXYV1HNjGxTzEw FskHC5+MgfMUvLdvFZOnxIQRxUh+CA/BMGrhmDT3u5qbvdMJKq7jEHMulESw8GpZG/dW yoL6V23QPQR1yyKQE2zlT6lRhV8zrBaU3r3GOm2pygzzX9fMLlN3OrBEgdfYNWiR5/W2 IC+5tn/CQAjzcpyWFiDTggH7I1wvPn4I5eNW1rjcwgWfPkKNHvYGhBzOiGrm/GwESele MpnQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net. [217.70.183.197]) by mx.google.com with ESMTPS id q23si2369413eda.221.2019.04.16.22.33.49 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:33:49 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.197; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 7D8861C0002; Wed, 17 Apr 2019 05:33:42 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 10/11] mips: Use generic mmap top-down layout Date: Wed, 17 Apr 2019 01:22:46 -0400 Message-Id: <20190417052247.17809-11-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP mips uses a top-down layout by default that fits the generic functions. At the same time, this commit allows to fix problem uncovered and not fixed for mips here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/mips/Kconfig | 1 + arch/mips/include/asm/processor.h | 5 --- arch/mips/mm/mmap.c | 67 ------------------------------- 3 files changed, 1 insertion(+), 72 deletions(-) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index 4a5f5b0ee9a9..ec2f07561e4d 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -14,6 +14,7 @@ config MIPS select ARCH_USE_CMPXCHG_LOCKREF if 64BIT select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_IPC_PARSE_VERSION select BUILDTIME_EXTABLE_SORT select CLONE_BACKWARDS diff --git a/arch/mips/include/asm/processor.h b/arch/mips/include/asm/processor.h index aca909bd7841..fba18d4a9190 100644 --- a/arch/mips/include/asm/processor.h +++ b/arch/mips/include/asm/processor.h @@ -29,11 +29,6 @@ extern unsigned int vced_count, vcei_count; -/* - * MIPS does have an arch_pick_mmap_layout() - */ -#define HAVE_ARCH_PICK_MMAP_LAYOUT 1 - #ifdef CONFIG_32BIT #ifdef CONFIG_KVM_GUEST /* User space process size is limited to 1GB in KVM Guest Mode */ diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index ffbe69f3a7d9..61e65a69bb09 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -20,43 +20,6 @@ unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); -/* gap between mmap and stack */ -#define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((STACK_TOP)/6*5) -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - #define COLOUR_ALIGN(addr, pgoff) \ ((((addr) + shm_align_mask) & ~shm_align_mask) + \ (((pgoff) << PAGE_SHIFT) & shm_align_mask)) @@ -154,36 +117,6 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp, addr0, len, pgoff, flags, DOWN); } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (TASK_IS_32BIT_ADDR) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif /* CONFIG_COMPAT */ - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - static inline unsigned long brk_rnd(void) { unsigned long rnd = get_random_long(); From patchwork Wed Apr 17 05:22:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10904581 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A743C17E0 for ; Wed, 17 Apr 2019 05:34:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 995CB2863C for ; Wed, 17 Apr 2019 05:34:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CD2028747; Wed, 17 Apr 2019 05:34:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C00882863C for ; Wed, 17 Apr 2019 05:34:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06B5F6B0008; Wed, 17 Apr 2019 01:34:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 01A3C6B0266; Wed, 17 Apr 2019 01:34:55 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4CA96B0269; Wed, 17 Apr 2019 01:34:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 918496B0008 for ; Wed, 17 Apr 2019 01:34:55 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id d2so11802129edo.23 for ; Tue, 16 Apr 2019 22:34:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=iSlMSAbalTlRLkI2S8vtmFwy6Fa3L9aABoAOjXi7jg4=; b=gb8Bszkj0TO85ZUs+1Lq4TqJeljQZbvZxkO3TFT5T+6mUu6oy3lm6AVdZckviY7i0Y 2x7zsg4A2TNvr9xjfi2ayTffRFv/Bj5l+Q842MxEBTb2ez6hZCUIQUKcZGOwZLaWm61h 8jLZaBlBHekcSC2igvAkVqKEltwa5aG3/m0/I7ue5axLFyt+bup9dNpFnDGDJCVszFP7 sJSepnk4xHQW9NLkhVdO6BzWlJhC8Wf0RaSENawfxO3MX4D6oJezGtMi3aaz+qYcDryS VGnY+gcUaATcNNHhosbfVmIzjLX7XkzhRoHYHwNf2tU2jnARgwUFEau3KJoQpgwR53z9 LwUA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUAQSQOB3DZ1GwUrETH5EsK5QPC4jUg7DQK3AgXGrgAy5NNcZf1 cnXDVzKRPTQ+nxYUJ9INetJa7hU5Gnc1jHP9bB+0PlRQNTbwc+67HM2TdjYw9Gcl4PSn1FGLFaS QJFmV2zzdBAhQGX0zlHE5w4XjhbJJhpup2bjnScK5JKpH75Ao+mhZcE/uMIGKFDw= X-Received: by 2002:a17:906:a841:: with SMTP id dx1mr44036418ejb.99.1555479295092; Tue, 16 Apr 2019 22:34:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqwWnGX+TkSvQMtL0qS9kn3+lisD5tINniCPZ8okPae3m2NosiBxgo8Nq9hNe0ncLkCrakT8 X-Received: by 2002:a17:906:a841:: with SMTP id dx1mr44036389ejb.99.1555479294120; Tue, 16 Apr 2019 22:34:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479294; cv=none; d=google.com; s=arc-20160816; b=Axxu4wEDHAMiQXA3yQq3mwJ+bWGExXl/CwgO2tSfUFufCKmmIfNw/Bw9AHm5N2GC3c B6s7Xhgbo0NIBirm2B6z8UjRi4fzBJEttLrQg1YT1V18FGJryXdYASm4pkPJ08bifc+e H3f79jLz0zBi/esJhY945mJEdop1HrcGq/9C7lJjKq9YP2uAJ82lIXk3UAhzh6S692pe b109XcEX+Q/KimxjVbOiU8WXOupz1rv64HSWuNEQoECSqioviIOAS92EDhajQcGPxqbH II3c5PYUzssL5fYoLyqjmz705MXyt4W+aLFJVVUYZ7H32MEM7XEB1SdsTUe4V4WEMg2N 3meA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=iSlMSAbalTlRLkI2S8vtmFwy6Fa3L9aABoAOjXi7jg4=; b=Y4jvS1I2qJmB/J6r0rhvO16uxYuhXuCcZNS6selT5ZO9CYg9wfYxuPsoryNLkKcv85 FxOXwL8CROOhM6a33gkuLPa4gy0VAgJn+WQ7mDkdR4XGZ6bpL5ixhHqoXPeiLlyQZkkp Sbu0Mcm7/jZRFMQdtQnW9XaNy07ee5M/812sBtJhqL/5yNXLdxnoTF9KOKvLHGtfVhHL FcAEY5SgoJK95qnvSpx2LGc/BgyyO6CKknK/oOsOZ+y9iNmF6/uQddG58RWwwFU86MhQ ci8ORVrq1Erdl5Xn+HfvTxLtLjY+FQTJqPNonfFdMrMghPvePvT9/f4zlZphB+Qu7hHu 5pcw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net. [217.70.183.197]) by mx.google.com with ESMTPS id k29si1605602edd.337.2019.04.16.22.34.53 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 22:34:54 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.197; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 8DE831C0008; Wed, 17 Apr 2019 05:34:49 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 11/11] riscv: Make mmap allocation top-down by default Date: Wed, 17 Apr 2019 01:22:47 -0400 Message-Id: <20190417052247.17809-12-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP In order to avoid wasting user address space by using bottom-up mmap allocation scheme, prefer top-down scheme when possible. Before: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 00018000-00039000 rw-p 00000000 00:00 0 [heap] 1555556000-155556d000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 155556d000-155556e000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 155556e000-155556f000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 155556f000-1555570000 rw-p 00000000 00:00 0 1555570000-1555572000 r-xp 00000000 00:00 0 [vdso] 1555574000-1555576000 rw-p 00000000 00:00 0 1555576000-1555674000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 1555674000-1555678000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 1555678000-155567a000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 155567a000-15556a0000 rw-p 00000000 00:00 0 3fffb90000-3fffbb1000 rw-p 00000000 00:00 0 [stack] After: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 00018000-00039000 rw-p 00000000 00:00 0 [heap] 3ff7eb6000-3ff7ed8000 rw-p 00000000 00:00 0 3ff7ed8000-3ff7fd6000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 3ff7fd6000-3ff7fda000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 3ff7fda000-3ff7fdc000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 3ff7fdc000-3ff7fe2000 rw-p 00000000 00:00 0 3ff7fe4000-3ff7fe6000 r-xp 00000000 00:00 0 [vdso] 3ff7fe6000-3ff7ffd000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 3ff7ffd000-3ff7ffe000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 3ff7ffe000-3ff7fff000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 3ff7fff000-3ff8000000 rw-p 00000000 00:00 0 3fff888000-3fff8a9000 rw-p 00000000 00:00 0 [stack] Signed-off-by: Alexandre Ghiti Reviewed-by: Christoph Hellwig Reviewed-by: Kees Cook Reviewed-by: Christoph Hellwig --- arch/riscv/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index eb56c82d8aa1..f5897e0dbc1c 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -49,6 +49,17 @@ config RISCV select GENERIC_IRQ_MULTI_HANDLER select ARCH_HAS_PTE_SPECIAL select HAVE_EBPF_JIT if 64BIT + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU + select HAVE_ARCH_MMAP_RND_BITS + +config ARCH_MMAP_RND_BITS_MIN + default 18 + +# max bits determined by the following formula: +# VA_BITS - PAGE_SHIFT - 3 +config ARCH_MMAP_RND_BITS_MAX + default 33 if 64BIT # SV48 based + default 18 config MMU def_bool y