Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3856298yba; Tue, 16 Apr 2019 22:25:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqyt9/Q1jJsDQJk28T3wgqMxpz07dghSHEW9PwTdqKkpJmyYRHzOR3y0AnStycIckv5P8blP X-Received: by 2002:a17:902:2965:: with SMTP id g92mr70504518plb.267.1555478724371; Tue, 16 Apr 2019 22:25:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555478724; cv=none; d=google.com; s=arc-20160816; b=acodHhrE6LcaBrwWmMzhz9kelydEoxuKK4d6dJrVt0jb0LwyuSHcMxA7Pw6i7yhiwM Urfodz1F/JCEdZ1ByXwri6XAb1Ecqrjg8PW8xKc9imWybPdGZZIGBMw51q0IHkUue33W 1x+CdxqbIA3nTgRJKb16or5RwGxZzK+9c89u/nwUOsD8SRp0Nahzfx6/3Jt3qmWvr+wi mpkpov79NVvLh5pMUVU0ODFG0CI/TqgoU5JrDjTAc1pG3uccTnYZsqwFJ5+liANOZ95Z zoHWrZ89U1x8jCdw7mjVdm1v8zZ+kW/nhWEwiRq4tG3mn2HYL99fiTxLUVtW8Emv2iD9 iodg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=88jJnvkgiwUXyeaRCl/DLtIxcpQckhQBrkTZmSxg5g0=; b=lPbzObI6yfYyrgBxFV2EWqhIL71YM5TuYNrCoUPj/sg/K+2Z/8Al3yv2VxcoEsRgIj Cgjn/NZLPzVCWl2xUuLBikp6antnrcVZ+cwtGMataP+Nj1vFrsJdMag7iVQUUrpaWdUz xUf6oEULH4WVzaHmGtsp++XIdEDE3KOEtykKQ8orjIftKD+PtXYcsYc6gNrW0uSzeQHL OIc491pt0VHOounelle2Rnicn4d+ustJ/h8wfTIq1k1l+NWgdnFKNOZ59fj34T8GWxCH i66gNsO+CrtN1jUkeqgwL++szzxgLV1RuUIeQq+n1SUfvZYTi/V6UU64LUA1K8dd5jJO fNSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e15si39107906pfj.60.2019.04.16.22.25.09; Tue, 16 Apr 2019 22:25:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729343AbfDQFYC (ORCPT + 99 others); Wed, 17 Apr 2019 01:24:02 -0400 Received: from relay9-d.mail.gandi.net ([217.70.183.199]:54621 "EHLO relay9-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725767AbfDQFYC (ORCPT ); Wed, 17 Apr 2019 01:24:02 -0400 X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 0C4B6FF802; Wed, 17 Apr 2019 05:23:55 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v3 01/11] mm, fs: Move randomize_stack_top from fs to mm Date: Wed, 17 Apr 2019 01:22:37 -0400 Message-Id: <20190417052247.17809-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-1-alex@ghiti.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This preparatory commit moves this function so that further introduction of generic topdown mmap layout is contained only in mm/util.c. Signed-off-by: Alexandre Ghiti Reviewed-by: Christoph Hellwig --- fs/binfmt_elf.c | 20 -------------------- include/linux/mm.h | 2 ++ mm/util.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 7d09d125f148..045f3b29d264 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -662,26 +662,6 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, * libraries. There is no binary dependent code anywhere else. */ -#ifndef STACK_RND_MASK -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ -#endif - -static unsigned long randomize_stack_top(unsigned long stack_top) -{ - unsigned long random_variable = 0; - - if (current->flags & PF_RANDOMIZE) { - random_variable = get_random_long(); - random_variable &= STACK_RND_MASK; - random_variable <<= PAGE_SHIFT; - } -#ifdef CONFIG_STACK_GROWSUP - return PAGE_ALIGN(stack_top) + random_variable; -#else - return PAGE_ALIGN(stack_top) - random_variable; -#endif -} - static int load_elf_binary(struct linux_binprm *bprm) { struct file *interpreter = NULL; /* to shut gcc up */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 76769749b5a5..087824a5059f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2312,6 +2312,8 @@ extern int install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, struct page **pages); +unsigned long randomize_stack_top(unsigned long stack_top); + extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); extern unsigned long mmap_region(struct file *file, unsigned long addr, diff --git a/mm/util.c b/mm/util.c index d559bde497a9..a54afb9b4faa 100644 --- a/mm/util.c +++ b/mm/util.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include @@ -291,6 +293,26 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); } +#ifndef STACK_RND_MASK +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ +#endif + +unsigned long randomize_stack_top(unsigned long stack_top) +{ + unsigned long random_variable = 0; + + if (current->flags & PF_RANDOMIZE) { + random_variable = get_random_long(); + random_variable &= STACK_RND_MASK; + random_variable <<= PAGE_SHIFT; + } +#ifdef CONFIG_STACK_GROWSUP + return PAGE_ALIGN(stack_top) + random_variable; +#else + return PAGE_ALIGN(stack_top) - random_variable; +#endif +} + #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { -- 2.20.1