Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4692576ybi; Tue, 30 Jul 2019 06:33:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwMZbTb333+fXlYEeU1KtJUxUTlL9/+STtosi0q25ZRw9yBWZdQgh3bUJtFIoFXmZ8P0ce4 X-Received: by 2002:a17:902:ac87:: with SMTP id h7mr119126158plr.36.1564493590831; Tue, 30 Jul 2019 06:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564493590; cv=none; d=google.com; s=arc-20160816; b=qTQRqoZPjCgYrcf2d5epv1IshsgtXq2wY+ftafLNV8WwtwV1qHADhJveMELDukrpOM BYlOTiWXRE2VzJqckMAc2hXhUNGooRNaG5Yv28eF1/7x2NkQLpRLFIfglI9rAefFdvxL gHfZkMZxxnKZPz+FU2/eq/grDmaW6TRQ9kXzlTE/4rVy+Kfi+Qph43tz9tw5EwY3n8/j B6cJrNE1WIW0Ux6K64vAdddgsiYTkwwDuMxr+oRBymPI5PFe32BYQeV5Z3DcamcBir5p rB7eiWZmU5b+DxCjQu+7pMgJTehJbAkwRxz9Sb26W9OCsstchOlOLLbr70oMGKUZkGSh J5vw== 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=mIrkUjObwaeukH78H/mj8oQicOip3Xxq77sucpNE7ZlLJGCY5J3BNFit8AB4EOy0Vh znVpidjknxd52lzSUoLnrI2Nya2H4h3XJL+upgHzp2g7x0ofEr0JXGU13cykr28dPtQu +7acKGIY93/pbRT+hPufgGkz4LzgrmlZFcyc3n/GkJwwAl8kTcpgbEIvf3adGo4pbO/H 6pcPjt05YYRPIgRaB2WHDXw79cuHqpxpfEGH+JzEsqazWKM5FSejkmMU0XcKrgYGvSzG v1drUm5Ff+7UBzoMCjTeZ2jSYD8LtfIMRh9k3/vWWUk4NqjO5b67XOyKab+Z9ZiHrqqD SfDw== 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 130si31348213pgg.18.2019.07.30.06.32.43; Tue, 30 Jul 2019 06:33:10 -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 S1729779AbfG3FxL (ORCPT + 99 others); Tue, 30 Jul 2019 01:53:11 -0400 Received: from relay8-d.mail.gandi.net ([217.70.183.201]:44581 "EHLO relay8-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728338AbfG3Fwh (ORCPT ); Tue, 30 Jul 2019 01:52:37 -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 relay8-d.mail.gandi.net (Postfix) with ESMTPSA id C4A9E1BF20E; Tue, 30 Jul 2019 05:52:31 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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 v5 01/14] mm, fs: Move randomize_stack_top from fs to mm Date: Tue, 30 Jul 2019 01:51:00 -0400 Message-Id: <20190730055113.23635-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190730055113.23635-1-alex@ghiti.fr> References: <20190730055113.23635-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