Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp6396938ybh; Wed, 7 Aug 2019 23:20:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqzbAJztl4ZDDZqt+51Nw4q2F8DqlsQVHa+/Jny+FUnZ9aeFWGmEMwfXd+C5BE0ltrkTUO8s X-Received: by 2002:a63:1f03:: with SMTP id f3mr11018197pgf.249.1565245238839; Wed, 07 Aug 2019 23:20:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245238; cv=none; d=google.com; s=arc-20160816; b=QAopm3xrCH6lU0HhICyZ8XyuDixk2y0YGx1Z5y0mWbZTP6BP6zRotF6tMeKInvaKHj vOK7wUF6CEwtZpiuCubXqsmbEytUCB84KAnrJ1ks3YXSPEnPO6WUrIZ6fB7TrBUEXaV2 7WrFgHb0e2jkhhKl5vTaqK4dzWSXJN4QdSdTvKBB/pmKo1op+dqJrdaGr2MpivG3LJWx Apg84w0Jg+B9NmYEjiNWYT8RuexAoSfvQip7Ss+8k8P5d3HOPuNTCJEqhPWJN4v6O2QD oumPp7KXhNr+lG6dC6+/X6/wGGCLWqJrlj8wTWUOQzw7tOdQjnjXzE2ZOktQXaAHsh5t hLTA== 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=zeV7cRDg//NBM0c2n9mDOnVPZ/BNPft3Bx8vY7oRip8=; b=kIOsOYg8zPdp5kMotSquqc+FSk6HgqsmsrE6XN+qZIy2zt2DLKS24KobyM1Lrra5Q3 kHjBvbdY79cWfPo4claMKvFLH9lb8LX+2pxVs5I65qkctN6lDx/r8cnqPCt2gRcDKS8t EXC9y4nT7u3K2eGhtZg45ZJyaQTACz4NzVQyScJ9r/9OKTOR/SNmGb2snuGcWSu7rXFz lz9ogUWNT4JG72iSsNxqkTJ8Z/fo12RxA8E/b4r6oR512E/O1Cpj9CCikDttHSfehGSi yJvuOZnA0KfT0JyooRuDnTTwudBgTkojygmruN1GN+urLXYlAH+0odSEC3t4bIZwZs2N /7rw== 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 f2si53386999pgl.74.2019.08.07.23.20.23; Wed, 07 Aug 2019 23:20:38 -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 S1731009AbfHHGTQ (ORCPT + 99 others); Thu, 8 Aug 2019 02:19:16 -0400 Received: from relay7-d.mail.gandi.net ([217.70.183.200]:48059 "EHLO relay7-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725796AbfHHGTP (ORCPT ); Thu, 8 Aug 2019 02:19:15 -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 relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 3FDAC20003; Thu, 8 Aug 2019 06:19:07 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , 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 v6 01/14] mm, fs: Move randomize_stack_top from fs to mm Date: Thu, 8 Aug 2019 02:17:43 -0400 Message-Id: <20190808061756.19712-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-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 Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- 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 d4e11b2e04f6..cec3b4146440 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -670,26 +670,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 0334ca97c584..ae0e5d241eb8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2351,6 +2351,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 e6351a80f248..15a4fb0f5473 100644 --- a/mm/util.c +++ b/mm/util.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include @@ -293,6 +295,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