From patchwork Sun May 26 13:47:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10961549 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 0E06C112C for ; Sun, 26 May 2019 13:49:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECD1B28A5D for ; Sun, 26 May 2019 13:49:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE20B28A89; Sun, 26 May 2019 13:49:20 +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 B39A828A5D for ; Sun, 26 May 2019 13:49:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2FDC6B0003; Sun, 26 May 2019 09:49:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BDF226B0005; Sun, 26 May 2019 09:49:18 -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 AA7706B0007; Sun, 26 May 2019 09:49:18 -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 5CD7C6B0003 for ; Sun, 26 May 2019 09:49:18 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id c26so23232667eda.15 for ; Sun, 26 May 2019 06:49:18 -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=SIdeHf7UhNLwsGYhllhi9P4YFI2Wqufk36v6ndrJAYI=; b=eCQVg6mWY835Gh4frAGDaPzIFZoz9EJHbVPrpz3sQHF2yjepZqxP3oCOSw5E1bscD9 UEbefk830tLf7FSdeFsxCLdci9eJmr06N7kJoqMvjHDuZOyht5r0OBZHSV/y/m79+ACq j/7/ILtPRHvrgiGQzdCJgwpp3Xjj82aPMuSLxivAOp8fYOy17J8pfHpQvAShynpqLu2Z 1OgB8nJLFvlFC8h/Cw+w0l4cFrmKtf5E391/EIX233jlSQxSL32FH6p0hGHymUN45t2n mhUT0HENcX7R+4fj0dAQ3RYisAU9h0FihH1BQ4MFZ2ZS/9rBB0cTRrBhe+mLghQvZ8dd dXVg== 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: APjAAAVXRH1L88P7L8Rg8sSuG0hwIHEICLq1sSld0UaNTg6Ww2bETfes R8n4iDl3LT/hjrlPP2pRc31JGOUF+sgOwWqw4Ebz8dJTeb5/bR2m4aJnotD9X1QPHhjVwaFs9hi BrgnuOa0x5S6kpZD2XYl6vhl7ArL6C/MsFEhXMwPjqa5vtXFklp7GkxRGC28ORmI= X-Received: by 2002:aa7:c596:: with SMTP id g22mr117761017edq.32.1558878557776; Sun, 26 May 2019 06:49:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqwK1dpRpU/bum5j3Gm4HlYOjpIe4v1u+c9qzIibg5G631ycXr73nZUSvEDJNU3nuFgjqq4J X-Received: by 2002:aa7:c596:: with SMTP id g22mr117760947edq.32.1558878556575; Sun, 26 May 2019 06:49:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878556; cv=none; d=google.com; s=arc-20160816; b=dPjrFVOnu0TVgzt+MR/H35ff6cK6mEXhg4tCBkIV7wi4/WJpp/XjJSNZ4WVzv9570F 592YTlxa2UurLBtLJ9yv+DTYg2V9S+k4HY3H3wuROwhlVb62AsyYPTUhvKeGtAjxhfXb cb8fRH/2TKwH8vQh5zx/XQhEDedia5e5AtXiq1VusmQtMrYCRVcSkr0YpGGx+D9c8D3I OrbxSyQ0TTI1TJQ2tTCTaIYMnsPUlzPyh1Sly7MgkmNtTFqTGsHppeqOZn1VNpmbi5pC DlPdognLV1iFsc83ARscZoCwHDonFcasRkZMrBQcpWBgQXAMlHiMIOh/tYcxPEL7MylD BysQ== 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=SIdeHf7UhNLwsGYhllhi9P4YFI2Wqufk36v6ndrJAYI=; b=Ox9RwbOy46id7AJszcE6fRIRCfFiVN4mTqpYwoiG6bKlrRSHT43d9McyXQbTCBnB2b dvssA0U5KSc84vqlLbATQmcM6XacD8VhWG2W+TW+NPgpcZrONc2B9y8Ky4nwjNbrbOEl wdyGx2fZ34NlL4o9nATxicge+hx1wGyw3ETykH/uTJMWuLMXG1UTJwlZ5zR7LJZDaBKD e2ob/NgyFAUynrkU5GGoYsa+b7ZoASW0qtHRfJMDqTp0NP373BZSY8uWN2qqtQfD3DC9 iXpv75HXMk3JKS6HGmtK1p0Xbe3mPZIRi9I3h2sPrHUSZK5C8uatEewXQASsSK5sg9nQ w6sw== 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 g28si5203223eda.439.2019.05.26.06.49.16 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:49:16 -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 16C8C20002; Sun, 26 May 2019 13:48:58 +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 v4 01/14] mm, fs: Move randomize_stack_top from fs to mm Date: Sun, 26 May 2019 09:47:33 -0400 Message-Id: <20190526134746.9315-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 Acked-by: Kees Cook Reviewed-by: Christoph Hellwig --- 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 fa9e99a962e0..d4d2fe109ee9 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -669,26 +669,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 0e8834ac32b7..446ec32c62b8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2368,6 +2368,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 e2e4f8c3fa12..dab33b896146 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 Sun May 26 13:47:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10961559 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 B72116C5 for ; Sun, 26 May 2019 13:50:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3944288A6 for ; Sun, 26 May 2019 13:50:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96D0628927; Sun, 26 May 2019 13:50: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 0221F28917 for ; Sun, 26 May 2019 13:50:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 243446B0003; Sun, 26 May 2019 09:50:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1F3126B0005; Sun, 26 May 2019 09:50: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 0BBC56B0007; Sun, 26 May 2019 09:50:26 -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 B3B656B0003 for ; Sun, 26 May 2019 09:50:25 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id z5so23315940edz.3 for ; Sun, 26 May 2019 06:50: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=dEuG6Wupa95dC9izLwtsSW8dwaH2/9q+MhpkifLzt/c=; b=bGYHfjfDa9hTuqfjtecU388aaZNa5i8yQ/rJnYAVnl90n6z8p7nVmQv36tpCkHVBYv uqgR+pmUki9tYpTWO1EFTryNjs3NBwhyIrqosdQX558cma4sbl9x3iC84rp5yKz+ZIDn DxPdZYJAsMQ7xxqSjOvToweZxVVVPGvwRq9sSIWsyyg6hW9KXgMybdNPXTtfIvjzCMKk m6ln7nCwfYo30b3KsdhScTgxYGDmzSCr/JB76CXKqeDUtrwyCHjpBcC9Z5eHlpHr/38W s2W0xgaEsmviADnmtzyFRxsFDCcR21Gz+DOQ/M0r6+hqxcB5MbZugv0dpN3AfjNSyTDW 2kMw== 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: APjAAAUPIbHRy+seJeTMBF6yk9zZKwYR7RlCtK4Jvm3nj09YyuydSH5A X5l+IMWDjZtHP6x4kk6P00PZL3NK7eD28HZg+AnthDWjS80HC9FOxrzUHvizBu+uPOwbgFOG8e6 RZFWltVvFaH3XRdD2To7TTRlb8DFn5kgwcjTwlNZd7Ge9TNARQi1UbUKHluvGmAY= X-Received: by 2002:aa7:d444:: with SMTP id q4mr12272138edr.302.1558878625212; Sun, 26 May 2019 06:50:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyka+Fdy2IN0+/iiJRqMYAana05bXvb7o9HnTwcJrxv6Xw0/vNWvxxh2flWlsDz12wVndHL X-Received: by 2002:aa7:d444:: with SMTP id q4mr12272066edr.302.1558878624217; Sun, 26 May 2019 06:50:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878624; cv=none; d=google.com; s=arc-20160816; b=hq6huGCwYV60FF+zcgJwDmbTPw/d1oDjgTYM67amdkPVkTceEwb8pS0Xf8kn0MhZb0 xwie8YRNP+4byTPXUTaaQSD3AD9RzU1LTEGNwLt26Vb+suI0trisqUjN2LmKV5oWkPn4 UzXR4pmW4iqtUUN5DqngC8oIi9uPKKIoSuScOk/Nt9MBjQ0igkEmUGfKKe9h67zIOdPh 864w39ypZavZbylQXHng0ywYZ60aWarMEmD/AzcdbJtZJ1eJefyme8obTMYCqH1m9X5y ZReKO3uG5W/aWrbfm0se7rh/FNmGWST3vs/Al5YipIYV0+gFpdmTW/IkVNGUsdyGgMlK FrPw== 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=dEuG6Wupa95dC9izLwtsSW8dwaH2/9q+MhpkifLzt/c=; b=YYF3Z/vl5XFirJduBuXAhP8nRtmvOZRAJQu8S6nibAdUueKx8BEEYpHxX6csaB8AH0 k4nI9aBrpaCPuoQkCmDcB2m2S/QQo6+JwrFJcmaum+QddMtoC1igNxVpFOawulm4YKKQ /bssT8aNQSHV2wI9dWd0RZm/y0+rEYJGTQd6xuThkj5GVr0edoWUfh7SHO4VgRQCkyNl c0ZVajmKIGIy8tVkik4Rk01dopKag6bT33R5PHjybeHBEhyzkBx/xLshh8N4fI+ZosXS n/GE6W0F1Ln35XJgC2DYKYCSCLdn5OMCcPtQndvi4Gbu98uBbaXJWk+7TkUCEoXfs9fa XP4w== 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 a18si4593831eda.350.2019.05.26.06.50.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:50:24 -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 7A7651C0009; Sun, 26 May 2019 13:50:16 +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 v4 02/14] arm64: Make use of is_compat_task instead of hardcoding this test Date: Sun, 26 May 2019 09:47:34 -0400 Message-Id: <20190526134746.9315-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000123, 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 Acked-by: Catalin Marinas --- 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 Sun May 26 13:47:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10961569 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 83AE676 for ; Sun, 26 May 2019 13:51:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CD0E28A71 for ; Sun, 26 May 2019 13:51:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D01D28A89; Sun, 26 May 2019 13:51:33 +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 2994728A71 for ; Sun, 26 May 2019 13:51:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 528826B0003; Sun, 26 May 2019 09:51:31 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4B2B96B0005; Sun, 26 May 2019 09:51:31 -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 32BBC6B0007; Sun, 26 May 2019 09:51:31 -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 D7D6E6B0003 for ; Sun, 26 May 2019 09:51:30 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id g36so23301560edg.8 for ; Sun, 26 May 2019 06:51: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=VLvm0D1nmVy1ERihwkodjsjVqa9oiCwNeZg629ShJFQ=; b=oBu3mQWRwV3tYgCqzWXtb05PZNmjfvwuyhGvV+nQMQH917r24Fd5SBmPnSxNErOO4z 6Pv6EdgxD76vtT/WBX4JfUP7BIyOrkKt5zVdGO/+NsqAA25RX+Aff9Q2elFTE8Wgoo1S fv2ZvZN1Cn3OhBWq3R+s8L97xnOm27FKEgjGhi7mudJvPfW3Ba8yPQFreMp/qn+w1qUy CrDrw5GTEpDCuAmZBGgCjP8TB5EScgIa6+RMnVf5rFekxnEEo1dJutAGRIaC6Eu/OD+V icjsESL6u8tFdC7bIozZqMeTFNQiHS+BFWf2rBacD9tLBtTgxiQ4+ZRel7gW8rKrF0mi b8BA== 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: APjAAAUZqUjtOEahfmDy+EOLQUZXEd0xY3XET3JxT0+iAgpdTPVXIFsl wEF4vIBiWpWDxHbOKKv2ZrJ5R9EsdDpu+76ok8N4rnLyAkpC79QHLyoRlP2bM/ctiKKL16eS6+P UVK0AHVhBAt+w1OpuMSP9a1ymaocDYLEE0Vxa17BnpkMesjDYaZCbcMzy0nxzK9A= X-Received: by 2002:a50:893d:: with SMTP id e58mr112981017ede.244.1558878690393; Sun, 26 May 2019 06:51:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqxtxq8crbF51jOUOwNCUzwtcbjoQBF7wRSPZH1ibmZ0g7fEOIqrdYOKh33fUpxADdncZxZT X-Received: by 2002:a50:893d:: with SMTP id e58mr112980963ede.244.1558878689493; Sun, 26 May 2019 06:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878689; cv=none; d=google.com; s=arc-20160816; b=I7kbKHpeDF7f9vftc0VjRhGXzQCWyQwPcucsELMckzxK8hbt53fpMGURQGJxeVhK0x apJnedEfL5lTZW8BBlus4tkqe3/R82OWixCmdFf+FPv0GMPTGSCmfJhjLTaDjOKX5sNl vxPGYqye/VE1Iug8uW3MQUJmS2n+soE3ACfUQlcrINo5PUq8/+MuDeTFLBuGYb4VRlnQ jAIOpJGfQuJ7NcTw8C4xy4NS6246c2/Cqx3U/OhoetQKq/FaK5vfW/ZMA2iuUYTSobb0 3mVMb+5D5HOvWcjbvL0gmRhfK22rA+1ocWQ7lsuMu9eyS+y9CHfk18n3kEzLlZxThWHL g5Yg== 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=VLvm0D1nmVy1ERihwkodjsjVqa9oiCwNeZg629ShJFQ=; b=WPjhhHtbQ9ZbpwKbhkwuZ6CZGLI5QH8C+kt1AFAGtFdB2RdBs8jApe2akRVS7Ln3pO ExWuz5FO7411pynxCqFIB9cySWv4ofLqfG+GAAp9YASc6EHTd//5xPekLM/qqQ6G7lhU DGGYlDMkdep/wzmVIuX3ykWW3QxES/WSN5WIIfcngi+uRJXj4WuCvIXw3MGpjw12fuHC hhd+4F0B0Yx0IZZ6rDZXMU5n0KzbxJjRqCL6V5vxeVPvGZ+gG83iOhTvVez+oGqc6KZw RmmOcgmt1cl6x8RZRVDwZtSl3DObYQddJJccIDN4+5uPOq/Ja+XlIDkEHOlVdgb/VzvB tpwA== 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 25si6722409edz.155.2019.05.26.06.51.29 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:51:29 -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 56DEF1C000B; Sun, 26 May 2019 13:51: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 v4 03/14] arm64: Consider stack randomization for mmap base only when necessary Date: Sun, 26 May 2019 09:47:35 -0400 Message-Id: <20190526134746.9315-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 Do not offset mmap base address because of stack randomization if current task does not want randomization. Note that x86 already implements this behaviour. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Acked-by: Catalin Marinas --- 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 Sun May 26 13:47:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10961579 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 D244B1395 for ; Sun, 26 May 2019 13:52:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFE3828A71 for ; Sun, 26 May 2019 13:52:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B40AE28A89; Sun, 26 May 2019 13:52: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=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 DB8B328A71 for ; Sun, 26 May 2019 13:52:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F12C06B0003; Sun, 26 May 2019 09:52:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E9D3C6B0005; Sun, 26 May 2019 09:52:43 -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 D166B6B0007; Sun, 26 May 2019 09:52:43 -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 7B8F36B0003 for ; Sun, 26 May 2019 09:52:43 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id n52so23352860edd.2 for ; Sun, 26 May 2019 06:52: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=b+xOaBeJmpHXKoDN8EnKbqTJvOQ3kpPBpnu5wSAlIDQ=; b=Z9I3PpNBBFD7H+680BBG9QMidnfOEQgqwb8mHMx2DNSTQ++gIxZJ9YU0kRdg9kCE6N Axzvaw+pfj/j+BACBBwJuExszDeMNEumwtlNVxZfjm6TN7QfBsGUPt+0S5sWEKLZDDSc OrmK5Sg4oVzJdDcgUYgksxgcAig+dX/loubEhpIzDwZY7MCaPqtf3OwopoI0/kVXe0Vk pjWZ+Umw8rsbIVp7zAWrMHbam47GheK09rO0xMz4rGwDwRb6gHO145gjThE4wKdb7IhN nNaHhFdYV/EWYOHAXY1WPludyMa8D/JbCtp//aWhItwHgWMmDFc714MyG7FOsYChp6U8 wBWw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAW2rRw/sA9FU8kKT8uP7ec+Q2DFJkCx7rQFsut3vhnCPUXGbxr7 FpjGkuRYalJxn8WIRSmlYOMlCoOpo+J1sExf8i9+B3nOHjrzgq6ue/0+ilupLwzZ9OggtjM9QWe yQWwyKgR6d84Oi3sN4sPaEy/spNVXWkdesYE0y6QhMJVBUU8fW1IB1I2dmabkmWE= X-Received: by 2002:a50:91cc:: with SMTP id h12mr116227375eda.3.1558878762981; Sun, 26 May 2019 06:52:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwAKyORHQNhW5KbxkOfz539PTB5T8+JpvVlUyZQnLmEqO7qzw6zYOGizrRd/cdBCE+bG5j6 X-Received: by 2002:a50:91cc:: with SMTP id h12mr116227273eda.3.1558878761303; Sun, 26 May 2019 06:52:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878761; cv=none; d=google.com; s=arc-20160816; b=zIHit5jhAaywVB2T5on1c/2cRTm41ZVci2zP+bbsnJgRxD931cB199OkCZMqeokcpe 9mKhVCgZP2/U4GoyaU3T/23v09PUrFJ+4oRoXkjJkHtquj9h5QWRfEYqcD5O6bFr6VKi yC4ZMquqBzOET7OWjwjMm36mpm2nx8F9VIgUxWs0DNIStXv57NHo1wv96L7ill4FzRTZ VyAORlUX9r8PiCD0ZQ6QZTQeIcSukgCBP/3vLckQAdjVBo2WP1kvz4ObVIs2BseWnEhL xHHQloG+7SUwkS139K3fgIS/idmf0pTF6+TO51ZnIz8bMyV9ZaBE81DAlBPFIJAtorvh Uj1w== 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=b+xOaBeJmpHXKoDN8EnKbqTJvOQ3kpPBpnu5wSAlIDQ=; b=YyttT8YXXtT3eOZwY0JWjFuBVQTffPDWk4xKnD51PcLkvZw4IXMQrkRe0V9HIqC9Xh H0H/0wducbeErdTteAaKg+Z+2LIYEs+AAPQ4VNG+GHJSPGGtObQHc0wY4BexI7FQIzjQ Nvp/HFyLM7318OouZ9rDEIX1ZerZqH9A8Pcdt+eY+fKhzeMe+CEOJLN7W51/w9LU0raG Ph2glt4Y0rLzcgx7FNP8ZqHo1fIOvFUporzpfhp1KXTrzW2Ly84iDpBd79S+0j8rtIhy ZMjweFvhB0rymlzoy+EkPx6+N51SK1KNGOfz3hvdCHvCnafVPQwfHRPxYjHuIY/YD4cj cqCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id n11si138565edv.199.2019.05.26.06.52.41 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:52:41 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 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 relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 968C260004; Sun, 26 May 2019 13:52:29 +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 v4 04/14] arm64, mm: Move generic mmap layout functions to mm Date: Sun, 26 May 2019 09:47:36 -0400 Message-Id: <20190526134746.9315-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 Acked-by: Kees Cook Acked-by: Catalin Marinas --- arch/Kconfig | 10 ++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 2 - arch/arm64/mm/mmap.c | 76 ----------------------------- kernel/sysctl.c | 6 ++- mm/util.c | 78 +++++++++++++++++++++++++++++- 6 files changed, 92 insertions(+), 81 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index c47b328eada0..df3ab04270fa 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -701,6 +701,16 @@ 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). +# Architecture that selects this option can provide its own version of: +# - STACK_RND_MASK +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 4780eb7af842..3d754c19c11e 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -69,6 +69,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 fcd0e691b1ea..3bd818edf319 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -282,8 +282,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 943c89178e3d..aebd03cc4b65 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -271,7 +271,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 @@ -1566,7 +1567,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 dab33b896146..717f5d75c16e 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,78 @@ 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_HAVE_ARCH_MMAP_RND_COMPAT_BITS + if (is_compat_task()) + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); + else +#endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */ + 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; +} + +/* + * 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 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 Sun May 26 13:47: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: 10961585 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 E73EA112C for ; Sun, 26 May 2019 13:53:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D3D6328A71 for ; Sun, 26 May 2019 13:53:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C4EDE28A89; Sun, 26 May 2019 13:53:50 +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 4AC2B28A71 for ; Sun, 26 May 2019 13:53:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D4B26B0003; Sun, 26 May 2019 09:53:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 685316B0005; Sun, 26 May 2019 09:53:49 -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 59AC46B0007; Sun, 26 May 2019 09:53:49 -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 0AF096B0003 for ; Sun, 26 May 2019 09:53:49 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id n23so23335370edv.9 for ; Sun, 26 May 2019 06:53:49 -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=VTQC7kI6uLCH8Kj/P0fJoMTcPRoRnmnCtakF65iLIOc=; b=TZeZtAIEV0XdnDIBNhizXWen7RQWGtQp0AQbMCPqezdvu5nomFwXXbDGAXiAHtTh+y mAFleCmu7ghrT15eKRJ4DtNvI/M4M/ibQOTkb+w0tb0Z6N2HnPpte/o1Susy3QWYlKwI YOkjloZ7NStzTsMKze3cIFDVvqR4ABakfjNYba/wNeN96CEPdJDlamrz/Wa/om49+FqP wx15QNfEoJpYvr8ErOy9S5C/dpxffxonf40ERozoDQHw6ekI9tljvK6ksNeYribNJ5eC PbMh65XGqNr05FcQ7TDvTsBH5DkOxd4Wt2Yt1zvavfWuxMkVTRKVWW1SKuCoqPAfUqE8 oZXw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXax/qlZRhif9P2KOtv0P6DDPz1SVHH/30AeOCcA31+BIjDWMeC XjChr4UY8s/uX05HBZ22rX/lSqbjzA+Q+J+NiNU2UiYOvRcmVxYpWPLb17z1bkk9L/3xuplbSwm 0mG2fqR0m4apUkybIpzjojYQ2fe9oAg3mQn9VeeRbC5qjQjQuTqJudOlmLjfDGzk= X-Received: by 2002:a50:f5d4:: with SMTP id x20mr116150460edm.88.1558878828554; Sun, 26 May 2019 06:53:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqwnRYVl7BdQ+Pt3avWk7DBivEpWgtrw5KGanPZjy+P6yg4YkCgWtdKTpfDmz4d0ph64UUOC X-Received: by 2002:a50:f5d4:: with SMTP id x20mr116150384edm.88.1558878827320; Sun, 26 May 2019 06:53:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878827; cv=none; d=google.com; s=arc-20160816; b=nHUp5iW+XxXz1fpYLgFOyl57IVL9LHSmWtj9CJjD0/tkECMXZh983A5EcKN2qZ0Uq0 06o15VM/pgJC2kAp1vFHwJ9P2sLxR0kijLSmvE9oG7bXanh1DRsJgVVp7HRIUKn6qGvf 8c13PfuWiYMLt2qebyXMyEllkw4R87MwqZzvyd5PBU8TABXxBoxIDNbd9T6/CBlnn9ca 1osIG4eob7EGHEV2OsE+bPEzfm4LP+9w9XZA6F/HNmucMqCMmzXf0XOg4rjMS7FPYvHl RcSFOwfi64wskJdKoxT+bc1QrsuR7E+jD71FeKtSnpq2TF+XVDCWdMPwLqMtNp/bokC+ M9cg== 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=VTQC7kI6uLCH8Kj/P0fJoMTcPRoRnmnCtakF65iLIOc=; b=uvZjHOe1/0ZhdKnfbkHpr58rmZry+PvwJgd1b+rPY+qj2CjJ8kh6mDxNJhsTqsAslR /4MiDwbwKugXRkK72XUMyt1s9m281vhgjufk6JKY80EpVTCC2cLUQ07Byt9Fl/KHXSK8 F+tnT77jXlynpswEl/weIIupdzu6z9qbYOi7ygBrTNLrG4HmVROQXJkcq0fck40fZSR7 wNZg29AsnriqccMEsIbXBc7OYl39Xf3add+9B4PxSx9tmHCFQJC+eMjBxfilrRsgCNKr RJ9Yq5btzaHbvQaJgzDaaH8QzNcpBeFLHE1DXLCCnkHOp/G6vjpzOdd50lswdzUgAf6X oDyQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay11.mail.gandi.net (relay11.mail.gandi.net. [217.70.178.231]) by mx.google.com with ESMTPS id d19si1122278ejt.19.2019.05.26.06.53.47 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:53:47 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.231; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 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 relay11.mail.gandi.net (Postfix) with ESMTPSA id EAC53100002; Sun, 26 May 2019 13:53:41 +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 v4 05/14] arm64, mm: Make randomization selected by generic topdown mmap layout Date: Sun, 26 May 2019 09:47:37 -0400 Message-Id: <20190526134746.9315-6-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 commits selects ARCH_HAS_ELF_RANDOMIZE when an arch uses the generic topdown mmap layout functions so that this security feature is on by default. Note that this commit also removes the possibility for arm64 to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Acked-by: Catalin Marinas --- arch/Kconfig | 1 + arch/arm64/Kconfig | 1 - arch/arm64/kernel/process.c | 8 -------- mm/util.c | 11 +++++++++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index df3ab04270fa..3732654446cc 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -710,6 +710,7 @@ config HAVE_ARCH_COMPAT_MMAP_BASES config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT bool depends on MMU + select ARCH_HAS_ELF_RANDOMIZE config HAVE_COPY_THREAD_TLS bool diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 3d754c19c11e..403bd3fffdbc 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -15,7 +15,6 @@ config ARM64 select ARCH_HAS_DMA_MMAP_PGPROT select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FAST_MULTIPLIER select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 3767fb21a5b8..3f85f8f2d665 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -535,14 +535,6 @@ unsigned long arch_align_stack(unsigned long sp) return sp & ~0xf; } -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - if (is_compat_task()) - return randomize_page(mm->brk, SZ_32M); - else - return randomize_page(mm->brk, SZ_1G); -} - /* * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. */ diff --git a/mm/util.c b/mm/util.c index 717f5d75c16e..8a38126edc74 100644 --- a/mm/util.c +++ b/mm/util.c @@ -319,7 +319,15 @@ unsigned long randomize_stack_top(unsigned long stack_top) } #ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT -#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE +unsigned long arch_randomize_brk(struct mm_struct *mm) +{ + /* Is the current task 32bit ? */ + if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task()) + return randomize_page(mm->brk, SZ_32M); + + return randomize_page(mm->brk, SZ_1G); +} + unsigned long arch_mmap_rnd(void) { unsigned long rnd; @@ -333,7 +341,6 @@ unsigned long arch_mmap_rnd(void) return rnd << PAGE_SHIFT; } -#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ static int mmap_is_legacy(struct rlimit *rlim_stack) { From patchwork Sun May 26 13:47: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: 10961595 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 DF15713AD for ; Sun, 26 May 2019 13:54:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CCA9728A86 for ; Sun, 26 May 2019 13:54:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC96328A71; Sun, 26 May 2019 13:54:59 +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 8A36428A71 for ; Sun, 26 May 2019 13:54:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8DF96B0003; Sun, 26 May 2019 09:54:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B3DBF6B0005; Sun, 26 May 2019 09:54:57 -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 A079F6B0007; Sun, 26 May 2019 09:54:57 -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 528806B0003 for ; Sun, 26 May 2019 09:54:57 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id t58so23220231edb.22 for ; Sun, 26 May 2019 06:54:57 -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=eym0BMHdbArtu9uPr+EyfIeUu2p0S0z5kNpGctDciqY=; b=p2mVaAbkCJdNDryRBGu2Smy/mmhvFRAgoMdWjGBPgehxNTF8hb8bHB+TE6LKrcoRAv lj1DU3fcY6ZH8pdXqUiJytmDQjILrmn4mcMnxsvCnJkC0DgDXezyNSUEtPFuAJAv3Yub kn1A5H/aU/uCbpBA+bRadVUL2tLz7wEezSKY0jpZpYRSdtwlWrmY7kQ5mWewYQEgmzWI DkebcggzVQ2YR+7MjQPdBYa8CST2JSZ1b+u97ejs4S/KUxsh9PZ1MCES9z6TLu+4xoGZ dCrBvA3/9phz69K3/d22XWcjsFqZzvaXeDSkU7quSuXQl8bQ3nevaHaE5ShRiMZbHs29 E6ZA== 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: APjAAAWluRgiF3iFL+af8/RR0/4Psjx+bBPHoo4UJa2tzzoKw45njAGT p6XVjcOeL8hSYIySMzqhpznBAps6jY1JNODXP1E3rlAkGRWwJllkkh2TbjwyKS6Wi82PpF6bjYO RADxgiITWUhubqU9Z82DNNyIuRWxapU5l9OrY1PF8HmohBBiILR/G5V0V6JytzVk= X-Received: by 2002:a17:906:5e16:: with SMTP id n22mr50139894eju.28.1558878896848; Sun, 26 May 2019 06:54:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwmaVqmzSaPhWNn15CXD5hY6nomd8yTC+1Jz8sk6Fcnd5067eE2Nnjkej1/QvxoQiN/O81R X-Received: by 2002:a17:906:5e16:: with SMTP id n22mr50139851eju.28.1558878895934; Sun, 26 May 2019 06:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878895; cv=none; d=google.com; s=arc-20160816; b=IiSgnse8hwPLIpXbD5PJSGjxztbDtUQOcOLm/J9EhDgN2zz68doyAklEGkBYDZQE0M 44LAhmMJIA22xIztx6fYD1kD4n92vc21Ov431nNHFYS1tk9B/uKGbhuuz30mO3VJ8JWR Rdms4A38Bx8DyNx8/J0YfFC10VdCmtz90Km3u/jwZNUWCqyMMVlhKgTU/e50j5VyvfZ/ 9Eya8zL9eNwKPgtXTgdupUhrjHgEeqE8esHPWfVsz2CNWVhp0tCH1R1wjtUWHntrzR3N RcZwtzOshfcc9OvYDyzM0a6Aqs20clIvsoGcKcACvabvU9PXLCsObBF/BW+nmKXtoCS7 bRxg== 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=eym0BMHdbArtu9uPr+EyfIeUu2p0S0z5kNpGctDciqY=; b=COjGHwaHNcTmGQBAFNeiqEpOfcpzh463DrR5FJB4ReeRDQLcZqWdwuVMFM58QkcwTg ZmiDNM7Hji7WEU2sVdMFgAFHMR+8frK9UgyliHRX4SsgBBq9evjY+xLAoDmxpO0cNmBt L/8DvX1g4YeFRwDJ6pYzdJ1a85D0lvSRfDeWICVS+eFNTrSsIo+kCyO0Qq3OxIS78XHr 28D7ZVN1DrZ9mEp0VBz2+6zM+g3CFhaQgey1T4ZBsDKA6xTGivYbJ4uPPhgsD1lYYHi+ 1TSWIk5JL+yKK9fb0ywQXdZLlhhh4Jbcf6bJI9qgbQtcEr8477Np3FfkAyIfD3TE0qG+ bdng== 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 k55si438782ede.289.2019.05.26.06.54.55 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:54:55 -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 3D31BFF805; Sun, 26 May 2019 13:54:47 +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 v4 06/14] arm: Properly account for stack randomization and stack guard gap Date: Sun, 26 May 2019 09:47:38 -0400 Message-Id: <20190526134746.9315-7-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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://lkml.kernel.org/r/20170622200033.25714-1-riel@redhat.com 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 Sun May 26 13:47: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: 10961607 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 EEF7E112C for ; Sun, 26 May 2019 13:56:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEA7728A7A for ; Sun, 26 May 2019 13:56:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D1F1728A96; Sun, 26 May 2019 13:56:03 +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 8642E28A7A for ; Sun, 26 May 2019 13:56:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8FE66B0003; Sun, 26 May 2019 09:56:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B19606B0005; Sun, 26 May 2019 09:56: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 A2ED16B0007; Sun, 26 May 2019 09:56:02 -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 527F56B0003 for ; Sun, 26 May 2019 09:56:02 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id p14so23365631edc.4 for ; Sun, 26 May 2019 06:56:02 -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=iEUnru/pgX+pWRywt03LhpHMFzmbntBrlVCKRjubFbE=; b=IjfeJMwQYEp3RWrvYAnZT370xyX6ppoKGbshnWMFPFYLWZuP+dARXxmq09ZNd9ypro rNrHHYMb7EnV6kiqZ3OY9P5GPVkjGQkuD8vdyY+yjy/KtCSw2jOF8wvhxWZYIop3Zsiq z7iCnzWYLIqyb2iIuHhmWCXhLbJKR5jzk38BHDjvcXWdtar7pE/E/Tmu/UVAtfeV4F6x VZbeAiZ8Iohs6n4UxhnrbaqObFBIqDXx99nev8ylXk/Qz11VINvFypcnDF12ryLBiSp1 WusnvF6TMrVocjp6OE6o0qcnyf5X6X2EI765AgSxIsl4cJNp/gRpPmr00nM0O6IP2hPx 4+Og== 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: APjAAAVkk8U5lUBBFlOWo0hRxPOsi5g6nxhTno4IZ/32RbQfmmss7s/O Ga4uTYW+WyG9/kkrROeY1iO6FwVOC5jfYe6oiN8pV8C6HRzVojKGl+PoUHYRAW+zidxM2Muc9wn pbgBttK3p8cN4TyatYkFuPRfSG7YznO6V6ikiEC4uuM7DqoZvNljJMuPr4T/4dxU= X-Received: by 2002:a50:8903:: with SMTP id e3mr115062577ede.11.1558878961847; Sun, 26 May 2019 06:56:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqxonfJgA+N31R8QJBXKZByObpVWMBNYdRNy7doZXrl/H5A+oyXNGa3/A8ZUT2fDKlCPdQ+2 X-Received: by 2002:a50:8903:: with SMTP id e3mr115062507ede.11.1558878960700; Sun, 26 May 2019 06:56:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558878960; cv=none; d=google.com; s=arc-20160816; b=lRdPCrG3Gh67b3DcAbetSfhXzt8tYuKDV6cro54Qs1I57Fpejz5sLXWtjd+Y+83LeO OMKuSEMzI43tCVPUyLznOd6tt8XbNX0oQVKie7MbD5NgRwesZqrmOpsNtlMspii7DS2Q j7fzI5C3xWGO5cTfIlBDSOTd8d7bwGlPgoWSQAhg3JghJq4UlmHqgJSZkIifkn9Nh9a8 8DA7nqXgT/0aVpzVrtZRT2V8sCc3ofBh5K9TUXK5dw3jw/sHuSF5lXFlXsqNgkChExKY KtP5WFlj4L2Lft9etHGFyg209BWDH3nnFkRhuxLDfx6YdoxSdyszY/Am9cRRg3zgcvEF CU8w== 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=iEUnru/pgX+pWRywt03LhpHMFzmbntBrlVCKRjubFbE=; b=J5fxwq1FPdXZX2vAwrrCoYk5i92qpqZIz0t3L5YTbFckFNat7SJuBJsM3PMjK1TD4k vWeuKCoLHzC/gVQ6KVX5beUPcmwPmIAsm0kwzJfaF6aQ238Htrh5si97piIJE+gWFTdq zxp4iYWqPg4h6MppL3VLFabQh0n1CnFX+936OX13EJ5+cwYg8u80hzAPA9nQTYBe4boI BGNCRnUwS4id1XbUuMfrO2dyl4hU9izds9qnGKM2rtpyeJ1H5ojIJVsYNu9MIs6sRbFZ y3uUANO34KoS7oBNNnhBvRQI8kUEWJJhAFWvNePbijume1hMjgLqINwRKA4yJdBs/4NB kPbg== 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 j49si3377361ede.377.2019.05.26.06.56.00 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:56: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 0B511FF804; Sun, 26 May 2019 13:55: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 v4 07/14] arm: Use STACK_TOP when computing mmap base address Date: Sun, 26 May 2019 09:47:39 -0400 Message-Id: <20190526134746.9315-8-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 Sun May 26 13:47: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: 10961615 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 7DE6E13AD for ; Sun, 26 May 2019 13:57:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6804D28A86 for ; Sun, 26 May 2019 13:57:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 55B2828A7A; Sun, 26 May 2019 13:57:10 +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 BAD1E28A7A for ; Sun, 26 May 2019 13:57:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E99FA6B0003; Sun, 26 May 2019 09:57:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E49EA6B0005; Sun, 26 May 2019 09:57:08 -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 D12B66B0007; Sun, 26 May 2019 09:57:08 -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 840B86B0003 for ; Sun, 26 May 2019 09:57:08 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id d15so23333776edm.7 for ; Sun, 26 May 2019 06:57:08 -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=k4gAy3Z0GohLZRHeMFIbFeMU/TQlVJcVgn+DpFBm95g=; b=LUAZ7Vwt2ksiFYbpw5W4CTO9PBTWqrmj9TowN188ne6B65dnMcSci0Ez8T2ExpF7Uf Fd/Va8evgssmVu17lNoPeva7CaKUYu7Olg1D+RHdfy2k09YLqBXuIEIE0EbjIm4qC6+U MaNrU9kUyvW+Z4JiCDG0Ae4RaZgVoDXry1w8RKAFzBKwexQUOyfepxxjgqdesk/XuwlB QelbhgjTEfr0ekySrJ4MKsagca7kB/PY/1cjeF3YS3r1zU3RWtp6CwwzFiRBrv/O2tuV rJ5jZqAVGkOHU/Y9sDZj14I3/pdmXWIpH7plDXanQq4RGQYSOQxsuD+zVl+nw3/CffL8 FDVQ== 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: APjAAAU0IjeogDDeGufm61A3BKR9RAtMR4gz2TZE6wCSYl8gyZ+mKTCC ouz237ZUgUaNS6hiHOkZG0woWP/jvSfFlRHkBQ9/nO/JClFLOyr3BsBFqM5GCm5A41qv531nOhc akuowEZ5OV8zAgA82/1aNHxYOowIeshnRwojrElWqKy2WN7wxLZvLdONOtj37F4g= X-Received: by 2002:a50:91cc:: with SMTP id h12mr116243475eda.3.1558879028028; Sun, 26 May 2019 06:57:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqxgApBfPLUTy/7iECdVsQI/wtru241tgH6PA+5SlVLKsuWtxBGARbZPwYj0P56dIjf6TKIx X-Received: by 2002:a50:91cc:: with SMTP id h12mr116243384eda.3.1558879026676; Sun, 26 May 2019 06:57:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879026; cv=none; d=google.com; s=arc-20160816; b=oqIiJ7P8iORu8a/2Gs6lohkC2PFa5sHWgbTHr8bujGnlt+nzFW8hEaAMLyKVX1POuI M17fzDbbKTBNnlClp5Jp8ALhUTV6jkX0rzgtkNxB2+7+UM8jE1CRx5yf3vjuJMvfcmx4 aQPvTvfZypS/+p3GFkc8GYspZGrAjTJ4tdiY+Ij+rGaeIzny0BL5bIb8l/y3W9BTr4U7 XVIA+MsqaJuaguHp1uGDG3gmJe+OpA0uOQ6zobhSWMPbjOStb9NtV8N4HRVP5sjXgNSe 64Q0QX31NhV6rxcJiQgpJeY9aHZxCly/fac0LSm0BUJhZaK53rfWoEXUIfQ6TmKJiDu3 Ak1A== 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=k4gAy3Z0GohLZRHeMFIbFeMU/TQlVJcVgn+DpFBm95g=; b=Lze2yAnXfM9O9fkNgAc7bSkEu9HFHad1F0jdE0jCKfDXJ9TarekENhX9eIK9+H6Wwr OfSB1IH02nIUootr7EPkyNsUrjyvaRZGoyRDGL2NQ50zulLfTfFcy+fSvjEkrKsxanBZ QEj0NOjaDQEFHcI+nJ7/7YJJUhmEMnzKjQ8aavQyRVlQ4X1EA0ray8RmBYp1vv1poqG1 ZZV3Zdt9h9XAP+Qm35lJ0JNqLyTVJKsv7Y0/vZnDTnb3zBmTbUuPtRV/QfXOBAmwXMPR FqR9UYN7l9gbGJUA6WZUGrQY4bzLIspyf+unMwA3rfhxKzYIPyeP4Z48ed2S2khUaB3p hGpQ== 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 b26si6110929edw.334.2019.05.26.06.57.06 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:57:06 -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 572EE1C0003; Sun, 26 May 2019 13:57: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 v4 08/14] arm: Use generic mmap top-down layout and brk randomization Date: Sun, 26 May 2019 09:47:40 -0400 Message-Id: <20190526134746.9315-9-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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. As ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT selects ARCH_HAS_ELF_RANDOMIZE, use the generic version of arch_randomize_brk since it also fits. Note that this commit also removes the possibility for arm to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook --- arch/arm/Kconfig | 2 +- arch/arm/include/asm/processor.h | 2 -- arch/arm/kernel/process.c | 5 --- arch/arm/mm/mmap.c | 62 -------------------------------- 4 files changed, 1 insertion(+), 70 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 8869742a85df..27687a8c9fb5 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -6,7 +6,6 @@ config ARM select ARCH_CLOCKSOURCE_DATA select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DEVMEM_IS_ALLOWED - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_KEEPINITRD select ARCH_HAS_KCOV @@ -29,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 5d06f75ffad4..95b7688341c5 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/kernel/process.c b/arch/arm/kernel/process.c index 72cc0862a30e..19a765db5f7f 100644 --- a/arch/arm/kernel/process.c +++ b/arch/arm/kernel/process.c @@ -322,11 +322,6 @@ unsigned long get_wchan(struct task_struct *p) return 0; } -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - return randomize_page(mm->brk, 0x02000000); -} - #ifdef CONFIG_MMU #ifdef CONFIG_KUSER_HELPERS /* 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 Sun May 26 13:47: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: 10961625 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 ACEAC112C for ; Sun, 26 May 2019 13:58:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 99E2528A7A for ; Sun, 26 May 2019 13:58:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8978528A96; Sun, 26 May 2019 13:58:14 +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 0DCF928A7A for ; Sun, 26 May 2019 13:58:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EAE36B0003; Sun, 26 May 2019 09:58:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 39C136B0005; Sun, 26 May 2019 09:58:13 -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 2B1896B0007; Sun, 26 May 2019 09:58:13 -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 CFA246B0003 for ; Sun, 26 May 2019 09:58:12 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id f41so23329742ede.1 for ; Sun, 26 May 2019 06:58:12 -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=ZDWhH5lHKvu6n/JfphSGR0tadyAKjYAsbuB4tHrJEFI=; b=ewQ+3rsda5zYOIDem+W+OpN99T0PyLFmXhkgszRdwdWBGK2znrm50HDj51fXVG6InM q/9dXz7mZbqWnM7OMyBzIngGEc7YcC6ygKTo4IoaTqQQ5GEOBsRaGRF75Xzpd6VNCVyC OaNyd5m8MdNQAr4b0qooNzP399qX3Iao1/UcoM3r9s8WMgbjBXt5MTOTbhS8ohUDtSQA 1Hoy+4YW+r/isG48KIVwzqZ3tNI9mX4ZcGlNgIZpB76pF+PSviuLHricYefl3Ax8/5WO W7V0QEtDbaqUtgABDd5+JbXalN+SHqxuy7o94dgeWTu3k33YQRzDMiDZSR7Jb42q9VPm 7Csw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXPfpCBf+OvZy0bqjutA3Ph3S/JwW/KLIoYayW9gf0Chh2BYMBc GhWvwT6D5S9DFwlU67nu9BFWNEk153MQvlGj9iRdSOF6SuAehGwN78cPF/xLcl6n3tWCbxudnGL WBfyPGiNpXNk/nf1hYAO6GMi+6uq0978ZQVqLQM9nZ7TAxsLzxAZ9XIThPU8K1cY= X-Received: by 2002:a05:6402:1612:: with SMTP id f18mr115004474edv.295.1558879092302; Sun, 26 May 2019 06:58:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwnDpOR/aewUxoQ7Wmclw4fLN3UgunI4A3GqkAw9rK2cEGVLRuimm/En7DtUqBI9qqhtbN X-Received: by 2002:a05:6402:1612:: with SMTP id f18mr115004424edv.295.1558879091509; Sun, 26 May 2019 06:58:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879091; cv=none; d=google.com; s=arc-20160816; b=cuE/M12RKnh2bngjDUl4mtzW5cp+mHCfvjkOV0G3XsckN3ebf95Z6UMATt3ixSgItD L9EiuqFGcoAK/aJMHkoZy+/56LujD/3AOR3q5boagKdL2b5Wjx8klM08ZeZpXps1ZxKl kxok2CvYB+UcuLVwlBMJrsNwHHAFeWgB6b8hVu2G4r+OEAxwWkd8o7yuU8VFc+W4aSX/ ON+8J4TsKA9gVzilD+g6UAqpo/xZmXCWOVgIa73j9aE8f/mHNJ4VvEpkQoLqRm4Em4y2 7lFNWj76AyfA8aseTlWN1rV8BZ/D/ZqqAfkgnlix3QDJqKB1HU6pCAIVYwx/hUeOjOnO aAhQ== 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=ZDWhH5lHKvu6n/JfphSGR0tadyAKjYAsbuB4tHrJEFI=; b=wAaf9J8G1vrJE94T9hcjhNqh/JdZhfAUKGJ6VKjLtr8+wlSMyPtJgDY/hhPxef5dit bZlCAMfRsvzgR9kLpg4waixFWc4UzCqoseBndOf/PTBIJUZkMlNdxX11lv+Sc1RVi/Kg WPZ0mKuDc2LQ3Z/+E0x6foPV0ICz6o1LeIpLx5LZuLKYH2hdTM/wNIcua8ftDAt3WsvJ cFohjRJT3ziCZYpikzRf1KZtphjTDhQUoZH8kSQrbFT6XxhsGdKOZxZkPs95KF/N488q b3Bi+H3R1WP/17LqyANUbLupEUkH01n2W7um37vN55qAckzMgUvFc+KLNxsVho/tmOGm 5gtA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id o52si4668122edc.421.2019.05.26.06.58.11 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:58:11 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 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 relay3-d.mail.gandi.net (Postfix) with ESMTPSA id D80B960006; Sun, 26 May 2019 13:58:06 +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 v4 09/14] mips: Properly account for stack randomization and stack guard gap Date: Sun, 26 May 2019 09:47:41 -0400 Message-Id: <20190526134746.9315-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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://lkml.kernel.org/r/20170622200033.25714-1-riel@redhat.com Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Acked-by: Paul Burton --- 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 Sun May 26 13:47: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: 10961637 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 491EF112C for ; Sun, 26 May 2019 13:59:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 382A328A7A for ; Sun, 26 May 2019 13:59:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AE2F28A96; Sun, 26 May 2019 13:59:19 +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 CBA6728A7A for ; Sun, 26 May 2019 13:59:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E725E6B0005; Sun, 26 May 2019 09:59:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E22366B0007; Sun, 26 May 2019 09:59:17 -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 D38EF6B0008; Sun, 26 May 2019 09:59:17 -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 83DA76B0005 for ; Sun, 26 May 2019 09:59:17 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id t58so23234135edb.22 for ; Sun, 26 May 2019 06:59:17 -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=Q87r3fNmNCP3Biyq3RF0DkHapd6qlpuqLXAsIpA43rw=; b=JM8B2Z5sCt6uktATMgtysjY7kfjtQ2M9k+PmfYFZeXOYkPwciWZbeGh3Cfr4mUQzuG YyubRFvG722LjPgD6MVvPcuNnPrRd/D3phhOOW4gTWUk6fkrPigYc9MMgN69cvm2DROO JtpDEphVTcwiEJS+MIWs1X71i6cgy6NsosK9zcshqhM5ylEEO6UX6YmHtt7FvPESQWrN OfWpGtFIuTlBHnx2/WZOYQseUirrUhno3xMiKBy/7x2l84sg0Ou8Q1jZDzKIOsg71CwW kBvb2SM8k02WWIQT4r/bdf2GQ2DxKrJjnrkOmrxJvgws6kEQHaZWZkxfaxkpG89B0m6C H6aw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXHqat63brWPJ1aXFvRSqOSDKx1FwJyeifi9QwXBqhZKLmcgKHk EEo6ZgPh52Lf8v1qUL7Wl2zHPRgTDNSvsAwF3VR/TT1lLdAKKIb2djquniQTsKuqPI06SD5e2YX Sl82JTML89plI0GbmH69+7J/aDBDFWKZyCEF/k3N1VvN7qzAI7QThP56AMTpjp2w= X-Received: by 2002:a50:9435:: with SMTP id p50mr117824098eda.40.1558879157058; Sun, 26 May 2019 06:59:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqzuvguqB86LazKYwuKUUnSc7CMFbsHKFMrijGXATMNu514Ez3IVBBCS4VneYPmE4TOxlVgm X-Received: by 2002:a50:9435:: with SMTP id p50mr117824028eda.40.1558879156008; Sun, 26 May 2019 06:59:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879156; cv=none; d=google.com; s=arc-20160816; b=eojFf3BOpQUKfb5eyNAF7f2jGSwhjBzPlBGftOEl7F+aSpazrucCgI2jvwBaVAI5jk iu24y8tpcq+QcAuTigXNCoG4COZEKWTmANBwFschCGItexsqqhThBs07xsNuw/zJ4e7C 210uK9tYEiLTN4dKTCNsVn4aVPohfJzR9yOjid/R4bWJHZIs2oEdbm8btGp6joYErq+v 0wVNbteMISPrwr3ejkQ2R/xva/TcnexitqEUZwaMwq+C/qmpumua+tkv3JOjURzdjSvF ZmfVTeKCcz8Al+JoGO853sGiEZtSEjnkRIgt1vMFJ/c5reI24iwDLAI1xiO1ToVIwPLQ C+GQ== 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=Q87r3fNmNCP3Biyq3RF0DkHapd6qlpuqLXAsIpA43rw=; b=ZOvzo+U09q8aF/C/xYucIA2290JUR+VY2kksz3lawWarXT5+5mocwU4jZ+Hcq4umXH 4LFXzeuD9z0ZNCknChp0aTStokrNafG2CvMAUmcoqKi1dnhPmjoHBoLMMdUjVKxM5rfu f1ZG7okMD7Bn6ETjja2nTFUZ3L03KfPY7k+8avxM+NBYTLDGTtz8/5xUmj4t3UwNHd3Q myEksZ7rdibJIyCCy+jU+K6EzjI5Kp2fO4nUWrNba3cRqK9skcZPe7wxnv/RQI8YP2p+ JUXngr3HJIBNADfCunfnHJCpKZAngnEk5DM7VXov0EksdbS0FuXFWOsNgmhXZJiR3M+e /eyQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id t57si1466371eda.339.2019.05.26.06.59.15 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 06:59:15 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 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 relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 974FB60003; Sun, 26 May 2019 13:59:11 +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 v4 10/14] mips: Use STACK_TOP when computing mmap base address Date: Sun, 26 May 2019 09:47:42 -0400 Message-Id: <20190526134746.9315-11-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 Acked-by: Paul Burton --- 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 Sun May 26 13:47: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: 10961647 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 AE80113AD for ; Sun, 26 May 2019 14:00:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FD00288E0 for ; Sun, 26 May 2019 14:00:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92FE8288F8; Sun, 26 May 2019 14:00:23 +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 464CA288E0 for ; Sun, 26 May 2019 14:00:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9286B0003; Sun, 26 May 2019 10:00:22 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7435C6B0005; Sun, 26 May 2019 10:00:22 -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 5E4FA6B0007; Sun, 26 May 2019 10:00:22 -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 074166B0003 for ; Sun, 26 May 2019 10:00:22 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id d15so23344443edm.7 for ; Sun, 26 May 2019 07:00:21 -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=B1xCAfwoo2VsupRz9LufscYM0z2KLMPO+UsoTLhSc0Y=; b=OKTCUCanGaxAbTa8vfMpdDl0erG7v98bgcw/KudUlw9U2tk234Jxg0kH3TJVMSvgyn 3zFkdo1gJdCqFz+bc+lBu0f7AWGKBuqN8bBWVEeKO+xmFsIodEBmmX0lpzMsVHO6NCDN O7gg2QV29sRHoMMqx8tZf7jjGyXU4xQ5WATuCXnpSfaDqi7fMZ4V17UTfxwtX7Qa1BUE uxp+NMiPwRfA5uapL9nhJfuR9rM+bau4BlZbA6coZvOnkVUVYWnWKJuzv0e6+ouzdhzM cURc/0mKfq0NHzu8+RAiSoewgnx+8goE9j0jc6YwraN+0Vg2GNO2SD/VY1gm254fwMIi PwFQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAV7gz2ygRMueSdbNgzcGkWJwQW95sbC/9YVK0b0NTmhCGFMj4gq T5xNAUf2UHKGnFd904ZP+eIucwsik4+Q5WiXE68zj5aHkW9NTDKJ/kX0jUZlwdl2NxehRzPc+nr BfJqN8i3xOoqvAKQPNnSMJqDhXrMdY+O7YbZ9XVxekf6SEQU0tVR/dXu3pRMhqZU= X-Received: by 2002:a17:906:f112:: with SMTP id gv18mr9676295ejb.308.1558879221527; Sun, 26 May 2019 07:00:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqwt2SxknWjOgi6bDSPCzmDv2JolZnVTrIrvVudDkivx3pZwR2tA0P+yAFRpvNdU27QV4/LQ X-Received: by 2002:a17:906:f112:: with SMTP id gv18mr9676211ejb.308.1558879220534; Sun, 26 May 2019 07:00:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879220; cv=none; d=google.com; s=arc-20160816; b=YqSBE9gn2hhRSFWOg8OCMboqDil0eq/XmRdmSEJTEAanqh9PlE4lNL4PD1nkE4+142 evHG51W79IF+UKbtQbTluzIO4SmhVA+gxSiJl1A95CdvN46nZ853ZHc9B6smlYovi18r rUs2rEZ89eMfHswGzE1ZVlapSgeh+eLwu4QE92sO2/yz0xiWwga+xtQ8DcAoU1HFUsi5 2ZDNWgsOedUhfiU9/B9b0+kSExTchYDcTd6To+OJ+GCNovWL1T/ZfjFxH6jkZOYuak2k xJog1q7xQp682KEtUC5Wp9gEjMOg5Pdu/jpM6p5U50lN4LzQSdZNiRZ8T54odc/U9w56 9oDA== 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=B1xCAfwoo2VsupRz9LufscYM0z2KLMPO+UsoTLhSc0Y=; b=ibhmOBXP5bk9826z47eUR2YSVWc8ItAFffyRLXw5PXKge8cnWneY0lDdzNcZUw24u6 7Zn8NWcQ1w4AGgIyl8dpSQpFCfzHC9xQeFwypXTtqk5dZdvMqRUHzVcJameIlYn/KKqt si7G1Do1NwL4ljYjK66+QKFjVIHduSe4yIjfMXQtNCZHk42n+PGzK8lpXhPQWff2ppct 1RQ6PQhA3jb3QmX9MOaX0rPTOF+ZUmpUunJKqsUr9ezQ9/1UtKzlpVVXlUlbo/BCtmJ1 IooiBV7WyhSarxYrqhlFmQ2PDkutI1NrWQHV2fnV8TcydGguMHROfFTwMN+gsxQITQ0G PEAg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id t22si897694ejj.240.2019.05.26.07.00.20 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 07:00:20 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 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 relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 1A2206000A; Sun, 26 May 2019 14:00:15 +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 v4 11/14] mips: Adjust brk randomization offset to fit generic version Date: Sun, 26 May 2019 09:47:43 -0400 Message-Id: <20190526134746.9315-12-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, 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 simply bumps up to 32MB and 1GB the random offset of brk, compared to 8MB and 256MB, for 32bit and 64bit respectively. Suggested-by: Kees Cook Signed-off-by: Alexandre Ghiti Reviewed-by: Kees Cook --- arch/mips/mm/mmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index ffbe69f3a7d9..c052565b76fb 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -16,6 +16,7 @@ #include #include #include +#include unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); @@ -189,11 +190,11 @@ static inline unsigned long brk_rnd(void) unsigned long rnd = get_random_long(); rnd = rnd << PAGE_SHIFT; - /* 8MB for 32bit, 256MB for 64bit */ + /* 32MB for 32bit, 1GB for 64bit */ if (TASK_IS_32BIT_ADDR) - rnd = rnd & 0x7ffffful; + rnd = rnd & SZ_32M; else - rnd = rnd & 0xffffffful; + rnd = rnd & SZ_1G; return rnd; } From patchwork Sun May 26 13:47: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: 10961665 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 74A7D13AD for ; Sun, 26 May 2019 14:02:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6418F28A86 for ; Sun, 26 May 2019 14:02:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 579F028A96; Sun, 26 May 2019 14:02:33 +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 C6E8528A86 for ; Sun, 26 May 2019 14:02:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E96076B0003; Sun, 26 May 2019 10:02:31 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E477D6B0005; Sun, 26 May 2019 10:02:31 -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 CE76C6B0007; Sun, 26 May 2019 10:02:31 -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 7B8806B0003 for ; Sun, 26 May 2019 10:02:31 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id c26so23275379eda.15 for ; Sun, 26 May 2019 07:02:31 -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=6LZGvTAD62mMbL2kcAn9cI6Q+B9t74AkWV+jhj/3vo4=; b=Z2DDp5uNDd1QittCqyXxjMwYnC5biIQnezNXByNQkMjn4qTeag1/MC8cbAj1Jk3po6 XsRDLKN5CtOZObUD/Hq8XNgh4bFqG+HRU0fN32sCm5sPqcp19TOEBon/eDonf+3DTDJ0 bvogxxL5oiDLdf1i+43Z6KSNGm3cIiH0G9Pex73AUCI0K5QkWAmDPYI91zeAuV33lIbR ApFWhX4nOBz8NJ1fWtLZ92tj9xV1sPFFWasv2sJBwuW7pWYud2KQQllDIUCscXgdViI2 C7NkyGloTK56WqkqLkErZoyVEBbCaiFNqZd5eeBp35vfR/pLgDrKTa1rjO2Zdi/GRIZX poeA== 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: APjAAAV9ahiFrcSTCe0CTAFjzanhVUOohxzUzumqahBD02TEie6wtlpx x840t4mlyiMp/eYRnY3j3hsd7qs8Bi8eRNTeH+CL4WAvKfjtKYYHiCXyr5Har/pDRndOFWyrXBo BRf+Gd7GDZr4ER2X78wxo3ynlMR6fPT0JGbQwlSaeYhdpyLgW+9pmc0j8NJ1GL7c= X-Received: by 2002:a50:95b0:: with SMTP id w45mr117807737eda.221.1558879350968; Sun, 26 May 2019 07:02:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqwBYNN/l43JYJc4sIkedVu7nJ7luaij9vL5D0tESW5f2u74f8DG7lPu5yUOOpzYowdDRrwd X-Received: by 2002:a50:95b0:: with SMTP id w45mr117807577eda.221.1558879349658; Sun, 26 May 2019 07:02:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879349; cv=none; d=google.com; s=arc-20160816; b=TRbBZD5EhElWz7kTU4hRrlKuxsn0043k1JD6yiRKd1YpfdE8cPM9/oSDFhWjx0Fygs AVWSgUvAb2yAfBTySzz3LCpNXL3ijTHy9yDP93YKtTUbiDVTCwb8PXfcIEsp4r5JDRlT f9RxO4gZmk/MZBii6FGp9jYYnaWjc16SzIAlxocStdFbD7ADm77ATWFxbquM2+nYr7Wo Z3YUOAatXT5nvjWND0QADkQ8Ar2cNeLJ2PStI9fjILTgzWXyf+v2Hp9KKqwJN1g5cPiF 8XmAcyJA7uuyMxY7vlh+I0p+q1d0fbTj7kY0c5OI/wwSy6PenzImPkKU6bb4pR9AqFwu enhA== 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=6LZGvTAD62mMbL2kcAn9cI6Q+B9t74AkWV+jhj/3vo4=; b=Q9NShEL+lm3YBEVpd/vtbY8mHXgYLz6xRaDpFpHFjkx37IT8ocYDrgSkhtQnSs5gPL 3qVQnWbgBoVCqUs+JN981tgEqvId0ZrY6rcLgwK0VR7hYj+OwdTBjFq+r6qTKKP0SBlG 2cy0eVOybTiYnzB62KwiESpAezR0cZuSnAdI4L4bUxabrwsv9EPrWv2G5ppd2xUT00Cn eMVNzsaFGQh/A2aGUHiZvurj5F+AX/6nCzIzo5nTa3pQXpb3bxNSLHZnVZlGeNAufh+x Ci1wSSxYCgUK4tx8ObWHccYTa0rm5NKDFHTRwaZG2t2rNFzJn9WaXu+HGL/unpWfW0Hc P/xA== 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 v16si6164ejb.309.2019.05.26.07.02.29 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 07:02:29 -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 46E5C20006; Sun, 26 May 2019 14:02:25 +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 v4 13/14] mips: Use generic mmap top-down layout and brk randomization Date: Sun, 26 May 2019 09:47:45 -0400 Message-Id: <20190526134746.9315-14-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 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. As ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT selects ARCH_HAS_ELF_RANDOMIZE, use the generic version of arch_randomize_brk since it also fits. Note that this commit also removes the possibility for mips to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Signed-off-by: Alexandre Ghiti Reviewed-by: Kees Cook --- arch/mips/Kconfig | 2 +- arch/mips/include/asm/processor.h | 5 -- arch/mips/mm/mmap.c | 96 ------------------------------- 3 files changed, 1 insertion(+), 102 deletions(-) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index 70d3200476bf..da15b02bbe23 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -5,7 +5,6 @@ config MIPS select ARCH_32BIT_OFF_T if !64BIT select ARCH_BINFMT_ELF_STATE if MIPS_FP_SUPPORT select ARCH_CLOCKSOURCE_DATA - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_SUPPORTS_UPROBES @@ -13,6 +12,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 900670ea8531..c2effe535484 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -16,49 +16,10 @@ #include #include #include -#include -#include 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)) @@ -156,63 +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(); - - rnd = rnd << PAGE_SHIFT; - /* 32MB for 32bit, 1GB for 64bit */ - if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task()) - rnd = rnd & SZ_32M; - else - rnd = rnd & SZ_1G; - - return rnd; -} - -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - unsigned long base = mm->brk; - unsigned long ret; - - ret = PAGE_ALIGN(base + brk_rnd()); - - if (ret < mm->brk) - return mm->brk; - - return ret; -} - int __virt_addr_valid(const volatile void *kaddr) { return pfn_valid(PFN_DOWN(virt_to_phys(kaddr))); From patchwork Sun May 26 13:47: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: 10961673 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 A542A1575 for ; Sun, 26 May 2019 14:03:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FEA128A86 for ; Sun, 26 May 2019 14:03:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7EA8228A96; Sun, 26 May 2019 14:03:37 +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 CA9AF28A86 for ; Sun, 26 May 2019 14:03:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F33A46B0003; Sun, 26 May 2019 10:03:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EE1266B0005; Sun, 26 May 2019 10:03: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 DA9CD6B0007; Sun, 26 May 2019 10:03: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 8965C6B0003 for ; Sun, 26 May 2019 10:03:35 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id p14so23391701edc.4 for ; Sun, 26 May 2019 07:03: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=eylvikXdybEsbZ5NWad3DwvJBhDEOYlVAp2XXhmXkJo=; b=XperZ8UIZCb9+gwW8o4qpYYfeWO6rBJ2TaOD6Muz73t1fK4cw9j9GqsvjO9yITLa9T Db0omikJH4XYGEXH5ZyYr7jLhLEsC+TVOEaeWk5GZvLJdeTyCcMo7Da127Ft+Yp8EtwW DQtpbLXJocaVGxv4RTGGl93ipHyQ3R+b3r3podfIbIZlE1zvInZSysuctbX1tzPF/fx+ O90WX+OklA+qeKiKiHZgtRQBlcqnMFf++f6A7BbcwOga7jg0InPvDzeL7AmP+NmrrDTP nX+Btjr3zkF8QBmkAJ3xLfzbIMQ6fUQjcdZP+JlrLsSJjegAnVrO4xWECDy9JBo/4CyR u9Dw== 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: APjAAAUdM0pfms4Mbi7OwnNlKER4tfGNiAOOVOE6GdJHFLh75AR+vwAJ SUfr7Z5oC1Z1zwD1lq8FxZESB2KawgQOiju4Kc38DuM5ASIJ0s8Rpcmkjz6ddCq0wikqtrmssF1 JWglR+QixsoYiJdfDeheTCRvfiOfy469zaaql6b2TlxXrAsfM29ERGXuf3h0zjCo= X-Received: by 2002:a17:906:3713:: with SMTP id d19mr84123985ejc.194.1558879415039; Sun, 26 May 2019 07:03:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqxD5mMeRyYELeCoPIiXuiBy51qfCroI7GvnkJAJCVLpSk4lOp+hjEsKR96LPyJjKxEBC2Ys X-Received: by 2002:a17:906:3713:: with SMTP id d19mr84123897ejc.194.1558879414134; Sun, 26 May 2019 07:03:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879414; cv=none; d=google.com; s=arc-20160816; b=MX4JrTFCaSGg15WwD/tNSrF8taiAZiAcxHnppBK03cRcrBNmMq5I0pgI1rd/NDmtC4 1S8CcX7NGDJVLKDuq/61b6MmxzGCGPlYXBprA2gVytNg83EvvNT30rPrSeI9yFTuPItu z42mgQ34fcOcOEeNhI2n6X8PKuBVKDP1uslONBzFvpKPrXdhjJUe5YmeorpeBKTeFf1D tEkPDDxUj6uhAi+KRALMx0CHnreYSuo57d+uMoANHkmzjelIEP7Cmqg7gI0OwLWt/ggy KyJS2evUs61Y4xR1avCOZ+li8wXIYI78u52Z4KctcFGLnbNNqVySUx3gTzRAPqcPpbMy GS/Q== 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=eylvikXdybEsbZ5NWad3DwvJBhDEOYlVAp2XXhmXkJo=; b=RE4ebF1+dwD3gn0fpYmmFGMA4Eus1lUJHK3ASs6mvzlxj9y0M5FdVApXrTuAdOhPNs a5bONm4WX1K/k9Ckw1VwijQlZYsC2jvhvDmiA6BAs18FLwrxQNtxhgAm+Q+Idx5aS6X1 nuVhokuoYIf8j/x0wiW67ZAeQrRXdMVXEpwd543EiQUak02zoG32SAY+/52Cfq6RCAO+ re36383ZHFFCkeGaUjLUdWZMC3rxOkwxscFk8HoKwR4chon2YSW3EEk0PUBVQtWZqxaG Kl6TXABjEcxaaA8p3UNbz4AfUJ3ZqAAqhV47ZREKtqh76l0xzpKVtfLA8H7z/+b3LvBd pEqQ== 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 h11si2883692edh.278.2019.05.26.07.03.33 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 May 2019 07:03:34 -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 CA53E1C0008; Sun, 26 May 2019 14:03:29 +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 v4 14/14] riscv: Make mmap allocation top-down by default Date: Sun, 26 May 2019 09:47:46 -0400 Message-Id: <20190526134746.9315-15-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-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 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 2de81000-2dea2000 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 --- arch/riscv/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index ee32c66e1af3..8c067ebd3ae0 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -49,6 +49,17 @@ config RISCV select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_MMIOWB 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