From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1498BC10F0B for ; Thu, 18 Apr 2019 05:25:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B0CE821479 for ; Thu, 18 Apr 2019 05:25:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vjhji0Ma" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731310AbfDRFZh (ORCPT ); Thu, 18 Apr 2019 01:25:37 -0400 Received: from mail-vs1-f65.google.com ([209.85.217.65]:46581 "EHLO mail-vs1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725836AbfDRFZh (ORCPT ); Thu, 18 Apr 2019 01:25:37 -0400 Received: by mail-vs1-f65.google.com with SMTP id e2so483169vsc.13 for ; Wed, 17 Apr 2019 22:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=Vjhji0MaAMge7bXj40KL9Z4UP08iRb0l6wKXsWdD6szF6TtGik3LdUi7v4kUyBltqk uXEWf/Xmd5Jb5TvOqTmT1keAiZsZEJTLu6CtA2f7onItxqZpkLFTaTGgeIj8SvarRqS5 gJmbPP8YdEdnm/uN4CBbf+bZjMNpdKMLmHrrM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=RcyIORBkfqeml33xbi8fewXCER2H5Dq8nHaW9vkeqV6MjtvUTUOouxIc2DTEfpRkP/ pRMlV61HtFE7riDUWi5eAphhbLqbkGQYey2rwMbKR9zH4TP6gawyKQ5Z7a4xyYPL9xK0 elfijUegMj+K+doSzNa8Kcu4WJzOxyC1O0okthu4tZAPyX/4xdYFaCd/V41nr3jdMSz9 gj1lWYvBXsM4i13QXtpaGyPeu5+1lKG5yEn7xAtGrTvuzfkmB7v24w3SEauqzSvfJlL/ H2s/7f/rHi2oubHZOpyzyaWDA3ry8xyTmOcbDwkTTL/xQGOUJ8Mon7Kebe8WWti1x4Ju HV/A== X-Gm-Message-State: APjAAAXeKFzVz8oeztujL0c3D5h+g2iauHQrm+9jLLBnMm+VwcMkmloN FfonyA52AMkLp/p+zFCs402JR4PaMDI= X-Google-Smtp-Source: APXvYqz+YD5ULmxxdzi4n1xh5xH5Kakzy52pTViE23/MSaYKLuqY9BcakrKcL+aINGNZOiR5YPI59Q== X-Received: by 2002:a67:84d1:: with SMTP id g200mr50735011vsd.69.1555565135536; Wed, 17 Apr 2019 22:25:35 -0700 (PDT) Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com. [209.85.217.47]) by smtp.gmail.com with ESMTPSA id 203sm234855vkt.52.2019.04.17.22.25.35 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:25:35 -0700 (PDT) Received: by mail-vs1-f47.google.com with SMTP id g127so503730vsd.6 for ; Wed, 17 Apr 2019 22:25:35 -0700 (PDT) X-Received: by 2002:a67:f849:: with SMTP id b9mr28803360vsp.188.1555564664290; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-5-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-5-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:17:32 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 04/11] arm64, mm: Move generic mmap layout functions to mm To: Alexandre Ghiti Cc: Andrew Morton , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , LKML , linux-arm-kernel , linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, "linux-fsdevel@vger.kernel.org" , Linux-MM , Christoph Hellwig Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ( On Wed, Apr 17, 2019 at 12:27 AM Alexandre Ghiti wrote: > > 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 > --- > arch/Kconfig | 8 ++++ > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/processor.h | 2 - > arch/arm64/mm/mmap.c | 76 ------------------------------ > kernel/sysctl.c | 6 ++- > mm/util.c | 74 ++++++++++++++++++++++++++++- > 6 files changed, 86 insertions(+), 81 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 33687dddd86a..7c8965c64590 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES > and vice-versa 32-bit applications to call 64-bit mmap(). > Required for applications doing different bitness syscalls. > > +# This allows to use a set of generic functions to determine mmap base > +# address by giving priority to top-down scheme only if the process > +# is not in legacy mode (compat task, unlimited stack size or > +# sysctl_legacy_va_layout). > +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > + bool > + depends on MMU I'd prefer the comment were moved to the help text. I would include any details about what the arch still needs to define. For example right now, I think STACK_RND_MASK is still needed. (Though I think a common one could be added for this series too...) > + > config HAVE_COPY_THREAD_TLS > bool > help > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 7e34b9eba5de..670719a26b45 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -66,6 +66,7 @@ config ARM64 > select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG > select ARCH_SUPPORTS_NUMA_BALANCING > select ARCH_WANT_COMPAT_IPC_PARSE_VERSION > + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > select ARCH_WANT_FRAME_POINTERS > select ARCH_HAS_UBSAN_SANITIZE_ALL > select ARM_AMBA > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index 5d9ce62bdebd..4de2a2fd605a 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) > "nop") : : "p" (ptr)); > } > > -#define HAVE_ARCH_PICK_MMAP_LAYOUT > - > #endif > > extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ > diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c > index ac89686c4af8..c74224421216 100644 > --- a/arch/arm64/mm/mmap.c > +++ b/arch/arm64/mm/mmap.c > @@ -31,82 +31,6 @@ > > #include > > -/* > - * Leave enough space between the mmap area and the stack to honour ulimit in > - * the face of randomisation. > - */ This comment goes missing in the move... > -#define MIN_GAP (SZ_128M) > -#define MAX_GAP (STACK_TOP/6*5) > - > -static int mmap_is_legacy(struct rlimit *rlim_stack) > -{ > - if (current->personality & ADDR_COMPAT_LAYOUT) > - return 1; > - > - if (rlim_stack->rlim_cur == RLIM_INFINITY) > - return 1; > - > - return sysctl_legacy_va_layout; > -} > - > -unsigned long arch_mmap_rnd(void) > -{ > - unsigned long rnd; > - > -#ifdef CONFIG_COMPAT > - if (is_compat_task()) > - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > - else > -#endif > - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > - return rnd << PAGE_SHIFT; > -} > - > -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > -{ > - unsigned long gap = rlim_stack->rlim_cur; > - unsigned long pad = stack_guard_gap; > - > - /* Account for stack randomization if necessary */ > - if (current->flags & PF_RANDOMIZE) > - pad += (STACK_RND_MASK << PAGE_SHIFT); > - > - /* Values close to RLIM_INFINITY can overflow. */ > - if (gap + pad > gap) > - gap += pad; > - > - if (gap < MIN_GAP) > - gap = MIN_GAP; > - else if (gap > MAX_GAP) > - gap = MAX_GAP; > - > - return PAGE_ALIGN(STACK_TOP - gap - rnd); > -} > - > -/* > - * This function, called very early during the creation of a new process VM > - * image, sets up which VM layout function to use: > - */ > -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > -{ > - unsigned long random_factor = 0UL; > - > - if (current->flags & PF_RANDOMIZE) > - random_factor = arch_mmap_rnd(); > - > - /* > - * Fall back to the standard layout if the personality bit is set, or > - * if the expected stack growth is unlimited: > - */ > - if (mmap_is_legacy(rlim_stack)) { > - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > - mm->get_unmapped_area = arch_get_unmapped_area; > - } else { > - mm->mmap_base = mmap_base(random_factor, rlim_stack); > - mm->get_unmapped_area = arch_get_unmapped_area_topdown; > - } > -} > - > /* > * You really shouldn't be using read() or write() on /dev/mem. This might go > * away in the future. > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index e5da394d1ca3..eb3414e78986 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; > extern struct ctl_table firmware_config_table[]; > #endif > > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > int sysctl_legacy_va_layout; > #endif > > @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { > .proc_handler = proc_dointvec, > .extra1 = &zero, > }, > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > { > .procname = "legacy_va_layout", > .data = &sysctl_legacy_va_layout, > diff --git a/mm/util.c b/mm/util.c > index a54afb9b4faa..5c3393d32ed1 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -15,7 +15,12 @@ > #include > #include > #include > +#include > +#include > #include > +#include > +#include > +#include > > #include > > @@ -313,7 +318,74 @@ unsigned long randomize_stack_top(unsigned long stack_top) > #endif > } > > -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE I think CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT should select CONFIG_ARCH_HAS_ELF_RANDOMIZE. It would mean moving arch_randomize_brk() into this patch set too. For arm64 and arm, this is totally fine: they have identical logic. On MIPS this would mean bumping the randomization up: arm64 uses SZ_32M for 32-bit and SZ_1G for 64-bit. MIPS is 8M and 256M respectively. I don't see anything that indicates this would be a problem. *cross fingers* It looks like x86 would need bumping too: it uses 32M on both 32-bit and 64-bit. STACK_RND_MASK is the same though. > +unsigned long arch_mmap_rnd(void) > +{ > + unsigned long rnd; > + > +#ifdef CONFIG_COMPAT > + if (is_compat_task()) > + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > + else > +#endif /* CONFIG_COMPAT */ The ifdefs on is_compat_task() are not needed: is_compat_task() returns 0 in the !CONFIG_COMPAT case. > + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > + > + return rnd << PAGE_SHIFT; > +} > +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ > + > +static int mmap_is_legacy(struct rlimit *rlim_stack) > +{ > + if (current->personality & ADDR_COMPAT_LAYOUT) > + return 1; > + > + if (rlim_stack->rlim_cur == RLIM_INFINITY) > + return 1; > + > + return sysctl_legacy_va_layout; > +} > + > +#define MIN_GAP (SZ_128M) > +#define MAX_GAP (STACK_TOP / 6 * 5) > + > +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > +{ > + unsigned long gap = rlim_stack->rlim_cur; > + unsigned long pad = stack_guard_gap; > + > + /* Account for stack randomization if necessary */ > + if (current->flags & PF_RANDOMIZE) > + pad += (STACK_RND_MASK << PAGE_SHIFT); > + > + /* Values close to RLIM_INFINITY can overflow. */ > + if (gap + pad > gap) > + gap += pad; > + > + if (gap < MIN_GAP) > + gap = MIN_GAP; > + else if (gap > MAX_GAP) > + gap = MAX_GAP; > + > + return PAGE_ALIGN(STACK_TOP - gap - rnd); > +} > + > +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > +{ > + unsigned long random_factor = 0UL; > + > + if (current->flags & PF_RANDOMIZE) > + random_factor = arch_mmap_rnd(); > + > + if (mmap_is_legacy(rlim_stack)) { > + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > + mm->get_unmapped_area = arch_get_unmapped_area; > + } else { > + mm->mmap_base = mmap_base(random_factor, rlim_stack); > + mm->get_unmapped_area = arch_get_unmapped_area_topdown; > + } > +} > +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > { > mm->mmap_base = TASK_UNMAPPED_BASE; > -- > 2.20.1 > -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D303C10F0B for ; Thu, 18 Apr 2019 05:18:02 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E4E72183E for ; Thu, 18 Apr 2019 05:18:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="siosCj5e"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vjhji0Ma" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E4E72183E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+infradead-linux-riscv=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=S6sYCzcvSELBYUJ2TYEFFA2iqQMoWgykcX4KL38JuTo=; b=siosCj5eCW/dFV zg/OzFSOBT1woqyxDv6KWsIhEjX0MBxDNdFaV9+H9SecKapeuR663DrvlyGufvIU50LbB5upELS/p B5+H7sDJY34ILCt4Nn264Fq4R4q6Ng6K0zLmPxqadtMvHMcRfmW/6vs3r5SvKim88e0SNrqEQi9EX Sip38KTuoU6iW2RCYDxhLCX1X349eZdVg+Ue7eS6QIde9tNDq1HBQrLMeavdK5+n2RX465rj6tcdR mSeIUIfcQvthY1QkPN5LnzG0ooJvFsdVYZ92t9EcxX120zJxAgvmZMRY/hczI0SK97ZtfP81i4fu6 fj/c5stdLbYUVHHzu4gg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGzQs-0003xm-Fh; Thu, 18 Apr 2019 05:17:58 +0000 Received: from mail-vs1-xe41.google.com ([2607:f8b0:4864:20::e41]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGzQj-0003qo-Qs for linux-riscv@lists.infradead.org; Thu, 18 Apr 2019 05:17:51 +0000 Received: by mail-vs1-xe41.google.com with SMTP id f22so492125vso.7 for ; Wed, 17 Apr 2019 22:17:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=Vjhji0MaAMge7bXj40KL9Z4UP08iRb0l6wKXsWdD6szF6TtGik3LdUi7v4kUyBltqk uXEWf/Xmd5Jb5TvOqTmT1keAiZsZEJTLu6CtA2f7onItxqZpkLFTaTGgeIj8SvarRqS5 gJmbPP8YdEdnm/uN4CBbf+bZjMNpdKMLmHrrM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=Oezem89H1n9ZB/CnjwK+0Qv6Z3qw10xQVETbT1cLA64y4oNDwMW0o/3EkwCCbTnbjU BtCp2BvJX88/N4Fb8iuEzQGovubeWHfqbdcvowVXczE3wLt4ajJX+28d5/0wY39lEreY ULzIN3mRXztlqEvdO4UUWJkeD9FGuWNblVZQLyXnGq/+tMpCHYyJrNQUvwDgzuApvOlP eCbm5JFViQlYXI/2eOOmktFn8++Lwb/7QTsLSEOdIvCb14ur4oBRDM6SnPcPOVl5OW7z MZrQcfKPBL1IYkW7uslVSYn2oDUpcZd42jHcXl3Uz1X3LJ2+d9nV4flbJ1QKyLY2kcbF 6jwA== X-Gm-Message-State: APjAAAXFKDql3/zibzFUuhy2+LO3DfpWVBHr7o5ZD4e5kDVwzmg5Aard MTmOjgtL9Qg+j8dbQudm0PVy6DKJ5QY= X-Google-Smtp-Source: APXvYqwMi8C5BPUJl5EuE6+3MBK96+qlIAtJ5toYiqmS1XerjqyBRJqnFZ9SLpdEqhpUXkqy3CZJTw== X-Received: by 2002:a67:fa95:: with SMTP id f21mr50537227vsq.180.1555564668209; Wed, 17 Apr 2019 22:17:48 -0700 (PDT) Received: from mail-vs1-f46.google.com (mail-vs1-f46.google.com. [209.85.217.46]) by smtp.gmail.com with ESMTPSA id x80sm297072vkd.22.2019.04.17.22.17.44 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:17:45 -0700 (PDT) Received: by mail-vs1-f46.google.com with SMTP id o10so477534vsp.12 for ; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) X-Received: by 2002:a67:f849:: with SMTP id b9mr28803360vsp.188.1555564664290; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-5-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-5-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:17:32 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 04/11] arm64, mm: Move generic mmap layout functions to mm To: Alexandre Ghiti X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190417_221749_868029_0B7359C5 X-CRM114-Status: GOOD ( 28.82 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Albert Ou , Kees Cook , Catalin Marinas , Palmer Dabbelt , Will Deacon , Russell King , Ralf Baechle , LKML , Christoph Hellwig , Linux-MM , Paul Burton , linux-riscv@lists.infradead.org, Alexander Viro , James Hogan , "linux-fsdevel@vger.kernel.org" , Andrew Morton , linux-mips@vger.kernel.org, Christoph Hellwig , linux-arm-kernel , Luis Chamberlain Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+infradead-linux-riscv=archiver.kernel.org@lists.infradead.org ( On Wed, Apr 17, 2019 at 12:27 AM Alexandre Ghiti wrote: > > 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 > --- > arch/Kconfig | 8 ++++ > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/processor.h | 2 - > arch/arm64/mm/mmap.c | 76 ------------------------------ > kernel/sysctl.c | 6 ++- > mm/util.c | 74 ++++++++++++++++++++++++++++- > 6 files changed, 86 insertions(+), 81 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 33687dddd86a..7c8965c64590 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES > and vice-versa 32-bit applications to call 64-bit mmap(). > Required for applications doing different bitness syscalls. > > +# This allows to use a set of generic functions to determine mmap base > +# address by giving priority to top-down scheme only if the process > +# is not in legacy mode (compat task, unlimited stack size or > +# sysctl_legacy_va_layout). > +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > + bool > + depends on MMU I'd prefer the comment were moved to the help text. I would include any details about what the arch still needs to define. For example right now, I think STACK_RND_MASK is still needed. (Though I think a common one could be added for this series too...) > + > config HAVE_COPY_THREAD_TLS > bool > help > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 7e34b9eba5de..670719a26b45 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -66,6 +66,7 @@ config ARM64 > select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG > select ARCH_SUPPORTS_NUMA_BALANCING > select ARCH_WANT_COMPAT_IPC_PARSE_VERSION > + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > select ARCH_WANT_FRAME_POINTERS > select ARCH_HAS_UBSAN_SANITIZE_ALL > select ARM_AMBA > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index 5d9ce62bdebd..4de2a2fd605a 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) > "nop") : : "p" (ptr)); > } > > -#define HAVE_ARCH_PICK_MMAP_LAYOUT > - > #endif > > extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ > diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c > index ac89686c4af8..c74224421216 100644 > --- a/arch/arm64/mm/mmap.c > +++ b/arch/arm64/mm/mmap.c > @@ -31,82 +31,6 @@ > > #include > > -/* > - * Leave enough space between the mmap area and the stack to honour ulimit in > - * the face of randomisation. > - */ This comment goes missing in the move... > -#define MIN_GAP (SZ_128M) > -#define MAX_GAP (STACK_TOP/6*5) > - > -static int mmap_is_legacy(struct rlimit *rlim_stack) > -{ > - if (current->personality & ADDR_COMPAT_LAYOUT) > - return 1; > - > - if (rlim_stack->rlim_cur == RLIM_INFINITY) > - return 1; > - > - return sysctl_legacy_va_layout; > -} > - > -unsigned long arch_mmap_rnd(void) > -{ > - unsigned long rnd; > - > -#ifdef CONFIG_COMPAT > - if (is_compat_task()) > - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > - else > -#endif > - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > - return rnd << PAGE_SHIFT; > -} > - > -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > -{ > - unsigned long gap = rlim_stack->rlim_cur; > - unsigned long pad = stack_guard_gap; > - > - /* Account for stack randomization if necessary */ > - if (current->flags & PF_RANDOMIZE) > - pad += (STACK_RND_MASK << PAGE_SHIFT); > - > - /* Values close to RLIM_INFINITY can overflow. */ > - if (gap + pad > gap) > - gap += pad; > - > - if (gap < MIN_GAP) > - gap = MIN_GAP; > - else if (gap > MAX_GAP) > - gap = MAX_GAP; > - > - return PAGE_ALIGN(STACK_TOP - gap - rnd); > -} > - > -/* > - * This function, called very early during the creation of a new process VM > - * image, sets up which VM layout function to use: > - */ > -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > -{ > - unsigned long random_factor = 0UL; > - > - if (current->flags & PF_RANDOMIZE) > - random_factor = arch_mmap_rnd(); > - > - /* > - * Fall back to the standard layout if the personality bit is set, or > - * if the expected stack growth is unlimited: > - */ > - if (mmap_is_legacy(rlim_stack)) { > - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > - mm->get_unmapped_area = arch_get_unmapped_area; > - } else { > - mm->mmap_base = mmap_base(random_factor, rlim_stack); > - mm->get_unmapped_area = arch_get_unmapped_area_topdown; > - } > -} > - > /* > * You really shouldn't be using read() or write() on /dev/mem. This might go > * away in the future. > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index e5da394d1ca3..eb3414e78986 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; > extern struct ctl_table firmware_config_table[]; > #endif > > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > int sysctl_legacy_va_layout; > #endif > > @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { > .proc_handler = proc_dointvec, > .extra1 = &zero, > }, > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > { > .procname = "legacy_va_layout", > .data = &sysctl_legacy_va_layout, > diff --git a/mm/util.c b/mm/util.c > index a54afb9b4faa..5c3393d32ed1 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -15,7 +15,12 @@ > #include > #include > #include > +#include > +#include > #include > +#include > +#include > +#include > > #include > > @@ -313,7 +318,74 @@ unsigned long randomize_stack_top(unsigned long stack_top) > #endif > } > > -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE I think CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT should select CONFIG_ARCH_HAS_ELF_RANDOMIZE. It would mean moving arch_randomize_brk() into this patch set too. For arm64 and arm, this is totally fine: they have identical logic. On MIPS this would mean bumping the randomization up: arm64 uses SZ_32M for 32-bit and SZ_1G for 64-bit. MIPS is 8M and 256M respectively. I don't see anything that indicates this would be a problem. *cross fingers* It looks like x86 would need bumping too: it uses 32M on both 32-bit and 64-bit. STACK_RND_MASK is the same though. > +unsigned long arch_mmap_rnd(void) > +{ > + unsigned long rnd; > + > +#ifdef CONFIG_COMPAT > + if (is_compat_task()) > + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > + else > +#endif /* CONFIG_COMPAT */ The ifdefs on is_compat_task() are not needed: is_compat_task() returns 0 in the !CONFIG_COMPAT case. > + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > + > + return rnd << PAGE_SHIFT; > +} > +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ > + > +static int mmap_is_legacy(struct rlimit *rlim_stack) > +{ > + if (current->personality & ADDR_COMPAT_LAYOUT) > + return 1; > + > + if (rlim_stack->rlim_cur == RLIM_INFINITY) > + return 1; > + > + return sysctl_legacy_va_layout; > +} > + > +#define MIN_GAP (SZ_128M) > +#define MAX_GAP (STACK_TOP / 6 * 5) > + > +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > +{ > + unsigned long gap = rlim_stack->rlim_cur; > + unsigned long pad = stack_guard_gap; > + > + /* Account for stack randomization if necessary */ > + if (current->flags & PF_RANDOMIZE) > + pad += (STACK_RND_MASK << PAGE_SHIFT); > + > + /* Values close to RLIM_INFINITY can overflow. */ > + if (gap + pad > gap) > + gap += pad; > + > + if (gap < MIN_GAP) > + gap = MIN_GAP; > + else if (gap > MAX_GAP) > + gap = MAX_GAP; > + > + return PAGE_ALIGN(STACK_TOP - gap - rnd); > +} > + > +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > +{ > + unsigned long random_factor = 0UL; > + > + if (current->flags & PF_RANDOMIZE) > + random_factor = arch_mmap_rnd(); > + > + if (mmap_is_legacy(rlim_stack)) { > + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > + mm->get_unmapped_area = arch_get_unmapped_area; > + } else { > + mm->mmap_base = mmap_base(random_factor, rlim_stack); > + mm->get_unmapped_area = arch_get_unmapped_area_topdown; > + } > +} > +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > { > mm->mmap_base = TASK_UNMAPPED_BASE; > -- > 2.20.1 > -- Kees Cook _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECCEFC10F0B for ; Thu, 18 Apr 2019 05:23:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6DEEA21479 for ; Thu, 18 Apr 2019 05:23:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vjhji0Ma" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6DEEA21479 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D2AA56B0005; Thu, 18 Apr 2019 01:23:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDB106B0006; Thu, 18 Apr 2019 01:23:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BED436B0007; Thu, 18 Apr 2019 01:23:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from mail-vs1-f71.google.com (mail-vs1-f71.google.com [209.85.217.71]) by kanga.kvack.org (Postfix) with ESMTP id 999166B0005 for ; Thu, 18 Apr 2019 01:23:10 -0400 (EDT) Received: by mail-vs1-f71.google.com with SMTP id c20so181513vse.1 for ; Wed, 17 Apr 2019 22:23:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:mime-version:references :in-reply-to:from:date:message-id:subject:to:cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=seXG+2SgeTnKv3MLPYs1lfDaxaKs8OvjM/njBH0AJKih4LR272/R7JrtWDfhtrGHTx 7UbukcJdRUTEQnxs1RUCB1Rl+QkGC9fHuqzMuPcvLfodiFofNPfmgMOTQaFKCeczbOhv rr7WVuD4UocBUswKeXuI8B3g3GlVXnaC7LnnNsR+hKyHag98pmaUqssnO8V6/zo9jOLA YD5NvbFHhLwvxm4oVLe1UFqLJFX0FPCArYiGIaoV2QieaObwr7emB/iN9Jjhgm8gVNUX N6f19iUJvNPYSsGXWOLPXhamLxe39Qb83/A0DiqADtPRWffZCUILW5eQMjZ6hZQftDkW tGYQ== X-Gm-Message-State: APjAAAUYllnr+0OYugEHxvrverNPkUKkKvdmziDqtRr0oORyYyFEc+x3 n7iWXN5v/Lp40UmLsJkG5gDoaxVGK8Oey3Z2vyEmqEoBYQG7o4pIHzWbaI4n/4eeBT0JtreXeCD X3miN/uNAAJKpKuWjFinxAixKgyl/9AILTx9q34F/1rz1ivDLZpc4dJhBDKqPEtpR7g== X-Received: by 2002:a67:f105:: with SMTP id n5mr25209219vsk.181.1555564990167; Wed, 17 Apr 2019 22:23:10 -0700 (PDT) X-Received: by 2002:a67:f105:: with SMTP id n5mr25209191vsk.181.1555564989158; Wed, 17 Apr 2019 22:23:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555564989; cv=none; d=google.com; s=arc-20160816; b=KgogpIXwjZRktU9raaEkbQ6/2Ue3ifG/T3jUH+iI6BMVrs2mrb9GZngb7nlawoK6ZI u/KqljbvDkdgvlzhcVX56QlCj+cZsHArE7++YMnkMxBK7MD/UBDdLPYm4jbghiBGJziU W2uvroeYTATHMwvSlwmiN+aBbEDTDdIc+bghjI+zkOv1MQpLoayJx2aklSSpuziOj1jA Rj1vurjAYb0k088DMLXFcITV9M/NPgkHILP0wtrhlLQHpzcX7MXzWlAKUP8RpVHprBMy ipri23SDPmcfdPMAOwT5LMlaiuIL6elwWmaOfHiwCaGIApiVusZyPyC9VX2Wn1R8Ud63 jVhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=vPl9P5F7F+g00xqy/jl6dM08kr1cc+2G+zblH1sOFAQWsOJgOMkCZ8dd6XY7OjE9SH 7oNWP6kEoTZwGAA5k3xX65AO28YmPIaY/gSkin0sRrpJxiVlyAjWoqZpJx1m5L/s9nMb VI63XsnyONYVxGayxpODdiYoIA5Jt58Hfc2e++0CS0w8JAcEMkF4dd1cQdyV+A3Rv2EZ tMx+gN0kL9NnRQwB9iPYmkj7Ux0JzqxDNUTBlCZXp0F+xuVGnWy0iLRonItsyXlecWWi vtXpiVuCS5Ey/JN5QpY5WidGRaRQEBAmVMvAk6vU9EVocvPr11bPaL5GBjY5RfFD2cj2 OADw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Vjhji0Ma; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id l28sor372693vsj.105.2019.04.17.22.23.09 for (Google Transport Security); Wed, 17 Apr 2019 22:23:09 -0700 (PDT) Received-SPF: pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Vjhji0Ma; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=Vjhji0MaAMge7bXj40KL9Z4UP08iRb0l6wKXsWdD6szF6TtGik3LdUi7v4kUyBltqk uXEWf/Xmd5Jb5TvOqTmT1keAiZsZEJTLu6CtA2f7onItxqZpkLFTaTGgeIj8SvarRqS5 gJmbPP8YdEdnm/uN4CBbf+bZjMNpdKMLmHrrM= X-Google-Smtp-Source: APXvYqzHkV8qhc6wJLwDE0FKlzfgeBrsMfQdk+u+1Lo+jch8KnUsBkoADHXUy7Qd8SxApS91mJfy2A== X-Received: by 2002:a67:bb15:: with SMTP id m21mr55215955vsn.192.1555564988539; Wed, 17 Apr 2019 22:23:08 -0700 (PDT) Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com. [209.85.217.48]) by smtp.gmail.com with ESMTPSA id y1sm756223uai.0.2019.04.17.22.23.08 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:23:08 -0700 (PDT) Received: by mail-vs1-f48.google.com with SMTP id s2so503359vsi.5 for ; Wed, 17 Apr 2019 22:23:08 -0700 (PDT) X-Received: by 2002:a67:f849:: with SMTP id b9mr28803360vsp.188.1555564664290; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-5-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-5-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:17:32 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 04/11] arm64, mm: Move generic mmap layout functions to mm To: Alexandre Ghiti Cc: Andrew Morton , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , LKML , linux-arm-kernel , linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, "linux-fsdevel@vger.kernel.org" , Linux-MM , Christoph Hellwig Content-Type: text/plain; charset="UTF-8" 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: ( On Wed, Apr 17, 2019 at 12:27 AM Alexandre Ghiti wrote: > > 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 > --- > arch/Kconfig | 8 ++++ > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/processor.h | 2 - > arch/arm64/mm/mmap.c | 76 ------------------------------ > kernel/sysctl.c | 6 ++- > mm/util.c | 74 ++++++++++++++++++++++++++++- > 6 files changed, 86 insertions(+), 81 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 33687dddd86a..7c8965c64590 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES > and vice-versa 32-bit applications to call 64-bit mmap(). > Required for applications doing different bitness syscalls. > > +# This allows to use a set of generic functions to determine mmap base > +# address by giving priority to top-down scheme only if the process > +# is not in legacy mode (compat task, unlimited stack size or > +# sysctl_legacy_va_layout). > +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > + bool > + depends on MMU I'd prefer the comment were moved to the help text. I would include any details about what the arch still needs to define. For example right now, I think STACK_RND_MASK is still needed. (Though I think a common one could be added for this series too...) > + > config HAVE_COPY_THREAD_TLS > bool > help > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 7e34b9eba5de..670719a26b45 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -66,6 +66,7 @@ config ARM64 > select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG > select ARCH_SUPPORTS_NUMA_BALANCING > select ARCH_WANT_COMPAT_IPC_PARSE_VERSION > + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > select ARCH_WANT_FRAME_POINTERS > select ARCH_HAS_UBSAN_SANITIZE_ALL > select ARM_AMBA > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index 5d9ce62bdebd..4de2a2fd605a 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) > "nop") : : "p" (ptr)); > } > > -#define HAVE_ARCH_PICK_MMAP_LAYOUT > - > #endif > > extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ > diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c > index ac89686c4af8..c74224421216 100644 > --- a/arch/arm64/mm/mmap.c > +++ b/arch/arm64/mm/mmap.c > @@ -31,82 +31,6 @@ > > #include > > -/* > - * Leave enough space between the mmap area and the stack to honour ulimit in > - * the face of randomisation. > - */ This comment goes missing in the move... > -#define MIN_GAP (SZ_128M) > -#define MAX_GAP (STACK_TOP/6*5) > - > -static int mmap_is_legacy(struct rlimit *rlim_stack) > -{ > - if (current->personality & ADDR_COMPAT_LAYOUT) > - return 1; > - > - if (rlim_stack->rlim_cur == RLIM_INFINITY) > - return 1; > - > - return sysctl_legacy_va_layout; > -} > - > -unsigned long arch_mmap_rnd(void) > -{ > - unsigned long rnd; > - > -#ifdef CONFIG_COMPAT > - if (is_compat_task()) > - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > - else > -#endif > - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > - return rnd << PAGE_SHIFT; > -} > - > -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > -{ > - unsigned long gap = rlim_stack->rlim_cur; > - unsigned long pad = stack_guard_gap; > - > - /* Account for stack randomization if necessary */ > - if (current->flags & PF_RANDOMIZE) > - pad += (STACK_RND_MASK << PAGE_SHIFT); > - > - /* Values close to RLIM_INFINITY can overflow. */ > - if (gap + pad > gap) > - gap += pad; > - > - if (gap < MIN_GAP) > - gap = MIN_GAP; > - else if (gap > MAX_GAP) > - gap = MAX_GAP; > - > - return PAGE_ALIGN(STACK_TOP - gap - rnd); > -} > - > -/* > - * This function, called very early during the creation of a new process VM > - * image, sets up which VM layout function to use: > - */ > -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > -{ > - unsigned long random_factor = 0UL; > - > - if (current->flags & PF_RANDOMIZE) > - random_factor = arch_mmap_rnd(); > - > - /* > - * Fall back to the standard layout if the personality bit is set, or > - * if the expected stack growth is unlimited: > - */ > - if (mmap_is_legacy(rlim_stack)) { > - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > - mm->get_unmapped_area = arch_get_unmapped_area; > - } else { > - mm->mmap_base = mmap_base(random_factor, rlim_stack); > - mm->get_unmapped_area = arch_get_unmapped_area_topdown; > - } > -} > - > /* > * You really shouldn't be using read() or write() on /dev/mem. This might go > * away in the future. > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index e5da394d1ca3..eb3414e78986 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; > extern struct ctl_table firmware_config_table[]; > #endif > > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > int sysctl_legacy_va_layout; > #endif > > @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { > .proc_handler = proc_dointvec, > .extra1 = &zero, > }, > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > { > .procname = "legacy_va_layout", > .data = &sysctl_legacy_va_layout, > diff --git a/mm/util.c b/mm/util.c > index a54afb9b4faa..5c3393d32ed1 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -15,7 +15,12 @@ > #include > #include > #include > +#include > +#include > #include > +#include > +#include > +#include > > #include > > @@ -313,7 +318,74 @@ unsigned long randomize_stack_top(unsigned long stack_top) > #endif > } > > -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE I think CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT should select CONFIG_ARCH_HAS_ELF_RANDOMIZE. It would mean moving arch_randomize_brk() into this patch set too. For arm64 and arm, this is totally fine: they have identical logic. On MIPS this would mean bumping the randomization up: arm64 uses SZ_32M for 32-bit and SZ_1G for 64-bit. MIPS is 8M and 256M respectively. I don't see anything that indicates this would be a problem. *cross fingers* It looks like x86 would need bumping too: it uses 32M on both 32-bit and 64-bit. STACK_RND_MASK is the same though. > +unsigned long arch_mmap_rnd(void) > +{ > + unsigned long rnd; > + > +#ifdef CONFIG_COMPAT > + if (is_compat_task()) > + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > + else > +#endif /* CONFIG_COMPAT */ The ifdefs on is_compat_task() are not needed: is_compat_task() returns 0 in the !CONFIG_COMPAT case. > + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > + > + return rnd << PAGE_SHIFT; > +} > +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ > + > +static int mmap_is_legacy(struct rlimit *rlim_stack) > +{ > + if (current->personality & ADDR_COMPAT_LAYOUT) > + return 1; > + > + if (rlim_stack->rlim_cur == RLIM_INFINITY) > + return 1; > + > + return sysctl_legacy_va_layout; > +} > + > +#define MIN_GAP (SZ_128M) > +#define MAX_GAP (STACK_TOP / 6 * 5) > + > +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > +{ > + unsigned long gap = rlim_stack->rlim_cur; > + unsigned long pad = stack_guard_gap; > + > + /* Account for stack randomization if necessary */ > + if (current->flags & PF_RANDOMIZE) > + pad += (STACK_RND_MASK << PAGE_SHIFT); > + > + /* Values close to RLIM_INFINITY can overflow. */ > + if (gap + pad > gap) > + gap += pad; > + > + if (gap < MIN_GAP) > + gap = MIN_GAP; > + else if (gap > MAX_GAP) > + gap = MAX_GAP; > + > + return PAGE_ALIGN(STACK_TOP - gap - rnd); > +} > + > +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > +{ > + unsigned long random_factor = 0UL; > + > + if (current->flags & PF_RANDOMIZE) > + random_factor = arch_mmap_rnd(); > + > + if (mmap_is_legacy(rlim_stack)) { > + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > + mm->get_unmapped_area = arch_get_unmapped_area; > + } else { > + mm->mmap_base = mmap_base(random_factor, rlim_stack); > + mm->get_unmapped_area = arch_get_unmapped_area_topdown; > + } > +} > +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > { > mm->mmap_base = TASK_UNMAPPED_BASE; > -- > 2.20.1 > -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4FEBC10F0E for ; Thu, 18 Apr 2019 05:18:01 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A71B42183E for ; Thu, 18 Apr 2019 05:18:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="MKu4K104"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vjhji0Ma" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A71B42183E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ebNFsJp8V3Wb4P4XwixjBJ95PpSZn6hTdTyKt9LzCaU=; b=MKu4K104oKTZ8U EKY37oH3rjAqUC4nqDH+k0vlhDBrOh7wcEGZU0C1pqSkZoIpDDZSQjp77ZR65kDTJ7eWJyKSL0Ay8 5qmfIXKt0ddtHEIIbF735ZZzKOVfYYoGMh6vJfs0dPsS+VdBncH03GK1K2dIRExc7omc62MBn9UlG StsghTM2FeEGXCVqYkyQmeUy0NK7CabIBgpxfDcs9s6f7UUKK3Ec2jHB4phPBC5LBa1C1ECMcEYu5 2DP0cpR4geks0Nta7mSqnnDkuLLrFLa+mK9566ShNZba78vw7CYYHg7vXOxt2QByCXHUN2OC1bmVO jII9LjwevTQyJ9Kt3P3A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGzQn-0003rM-9a; Thu, 18 Apr 2019 05:17:53 +0000 Received: from mail-vs1-xe43.google.com ([2607:f8b0:4864:20::e43]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGzQj-0003qY-2h for linux-arm-kernel@lists.infradead.org; Thu, 18 Apr 2019 05:17:51 +0000 Received: by mail-vs1-xe43.google.com with SMTP id j184so480848vsd.11 for ; Wed, 17 Apr 2019 22:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=Vjhji0MaAMge7bXj40KL9Z4UP08iRb0l6wKXsWdD6szF6TtGik3LdUi7v4kUyBltqk uXEWf/Xmd5Jb5TvOqTmT1keAiZsZEJTLu6CtA2f7onItxqZpkLFTaTGgeIj8SvarRqS5 gJmbPP8YdEdnm/uN4CBbf+bZjMNpdKMLmHrrM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jq72mIAW+DpXXKzdmdlCRpkn/sofWg7pTQf2H9PeRYA=; b=sJyp5CLuFreprCnP8wZrzlCdBA6p8OSSrpYOSsORyIntNlhSw3JLNfwtVgbsAmh+QN vnKuEhJqldLAixjZAmN+xSsf6epO86suhhfxxYqZyQdnOVrRXdkBxPj3VcS+bgCIC60n b6micK2uzFJOkG3m1EhGZcc27qjT2jeNm/Lz4Dt5xnnDV/FNb88P/MqOJnk8G80RiG0H 2JgMZYlyuSo33HEyrvFLjqVkF8IqPQfKRBPhIe7lIFSAJ2tBaEpCrPw7khBqCxDl2EB6 c2Ctic2HOjPiIcgpA+42Ms+/OYszGnLFqLL/HgdksXZR3yas3065xTj8Rr5QHrbs5wGD p73A== X-Gm-Message-State: APjAAAUGRc91l5vF3YXcq1rtDpoaIkZlfPq3oV7qvFA5SAVJmny63eBg XuV8BozVpAHXS1AMSz1XH12UbeSK4ts= X-Google-Smtp-Source: APXvYqwwy0DLwi+NnINU7obYfyXnG9wakDlGro2fTbSmuMwp++9pVsE6Vda42iDentQN/BrNcEcJew== X-Received: by 2002:a67:e28d:: with SMTP id g13mr43832628vsf.121.1555564666646; Wed, 17 Apr 2019 22:17:46 -0700 (PDT) Received: from mail-vs1-f41.google.com (mail-vs1-f41.google.com. [209.85.217.41]) by smtp.gmail.com with ESMTPSA id s195sm334323vka.42.2019.04.17.22.17.44 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:17:45 -0700 (PDT) Received: by mail-vs1-f41.google.com with SMTP id s2so497960vsi.5 for ; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) X-Received: by 2002:a67:f849:: with SMTP id b9mr28803360vsp.188.1555564664290; Wed, 17 Apr 2019 22:17:44 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-5-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-5-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:17:32 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 04/11] arm64, mm: Move generic mmap layout functions to mm To: Alexandre Ghiti X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190417_221749_152224_6376C12B X-CRM114-Status: GOOD ( 30.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Albert Ou , Kees Cook , Catalin Marinas , Palmer Dabbelt , Will Deacon , Russell King , Ralf Baechle , LKML , Christoph Hellwig , Linux-MM , Paul Burton , linux-riscv@lists.infradead.org, Alexander Viro , James Hogan , "linux-fsdevel@vger.kernel.org" , Andrew Morton , linux-mips@vger.kernel.org, Christoph Hellwig , linux-arm-kernel , Luis Chamberlain Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org ( On Wed, Apr 17, 2019 at 12:27 AM Alexandre Ghiti wrote: > > 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 > --- > arch/Kconfig | 8 ++++ > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/processor.h | 2 - > arch/arm64/mm/mmap.c | 76 ------------------------------ > kernel/sysctl.c | 6 ++- > mm/util.c | 74 ++++++++++++++++++++++++++++- > 6 files changed, 86 insertions(+), 81 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 33687dddd86a..7c8965c64590 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES > and vice-versa 32-bit applications to call 64-bit mmap(). > Required for applications doing different bitness syscalls. > > +# This allows to use a set of generic functions to determine mmap base > +# address by giving priority to top-down scheme only if the process > +# is not in legacy mode (compat task, unlimited stack size or > +# sysctl_legacy_va_layout). > +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > + bool > + depends on MMU I'd prefer the comment were moved to the help text. I would include any details about what the arch still needs to define. For example right now, I think STACK_RND_MASK is still needed. (Though I think a common one could be added for this series too...) > + > config HAVE_COPY_THREAD_TLS > bool > help > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 7e34b9eba5de..670719a26b45 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -66,6 +66,7 @@ config ARM64 > select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG > select ARCH_SUPPORTS_NUMA_BALANCING > select ARCH_WANT_COMPAT_IPC_PARSE_VERSION > + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > select ARCH_WANT_FRAME_POINTERS > select ARCH_HAS_UBSAN_SANITIZE_ALL > select ARM_AMBA > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index 5d9ce62bdebd..4de2a2fd605a 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) > "nop") : : "p" (ptr)); > } > > -#define HAVE_ARCH_PICK_MMAP_LAYOUT > - > #endif > > extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ > diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c > index ac89686c4af8..c74224421216 100644 > --- a/arch/arm64/mm/mmap.c > +++ b/arch/arm64/mm/mmap.c > @@ -31,82 +31,6 @@ > > #include > > -/* > - * Leave enough space between the mmap area and the stack to honour ulimit in > - * the face of randomisation. > - */ This comment goes missing in the move... > -#define MIN_GAP (SZ_128M) > -#define MAX_GAP (STACK_TOP/6*5) > - > -static int mmap_is_legacy(struct rlimit *rlim_stack) > -{ > - if (current->personality & ADDR_COMPAT_LAYOUT) > - return 1; > - > - if (rlim_stack->rlim_cur == RLIM_INFINITY) > - return 1; > - > - return sysctl_legacy_va_layout; > -} > - > -unsigned long arch_mmap_rnd(void) > -{ > - unsigned long rnd; > - > -#ifdef CONFIG_COMPAT > - if (is_compat_task()) > - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > - else > -#endif > - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > - return rnd << PAGE_SHIFT; > -} > - > -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > -{ > - unsigned long gap = rlim_stack->rlim_cur; > - unsigned long pad = stack_guard_gap; > - > - /* Account for stack randomization if necessary */ > - if (current->flags & PF_RANDOMIZE) > - pad += (STACK_RND_MASK << PAGE_SHIFT); > - > - /* Values close to RLIM_INFINITY can overflow. */ > - if (gap + pad > gap) > - gap += pad; > - > - if (gap < MIN_GAP) > - gap = MIN_GAP; > - else if (gap > MAX_GAP) > - gap = MAX_GAP; > - > - return PAGE_ALIGN(STACK_TOP - gap - rnd); > -} > - > -/* > - * This function, called very early during the creation of a new process VM > - * image, sets up which VM layout function to use: > - */ > -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > -{ > - unsigned long random_factor = 0UL; > - > - if (current->flags & PF_RANDOMIZE) > - random_factor = arch_mmap_rnd(); > - > - /* > - * Fall back to the standard layout if the personality bit is set, or > - * if the expected stack growth is unlimited: > - */ > - if (mmap_is_legacy(rlim_stack)) { > - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > - mm->get_unmapped_area = arch_get_unmapped_area; > - } else { > - mm->mmap_base = mmap_base(random_factor, rlim_stack); > - mm->get_unmapped_area = arch_get_unmapped_area_topdown; > - } > -} > - > /* > * You really shouldn't be using read() or write() on /dev/mem. This might go > * away in the future. > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index e5da394d1ca3..eb3414e78986 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; > extern struct ctl_table firmware_config_table[]; > #endif > > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > int sysctl_legacy_va_layout; > #endif > > @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { > .proc_handler = proc_dointvec, > .extra1 = &zero, > }, > -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT > +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ > + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) > { > .procname = "legacy_va_layout", > .data = &sysctl_legacy_va_layout, > diff --git a/mm/util.c b/mm/util.c > index a54afb9b4faa..5c3393d32ed1 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -15,7 +15,12 @@ > #include > #include > #include > +#include > +#include > #include > +#include > +#include > +#include > > #include > > @@ -313,7 +318,74 @@ unsigned long randomize_stack_top(unsigned long stack_top) > #endif > } > > -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE I think CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT should select CONFIG_ARCH_HAS_ELF_RANDOMIZE. It would mean moving arch_randomize_brk() into this patch set too. For arm64 and arm, this is totally fine: they have identical logic. On MIPS this would mean bumping the randomization up: arm64 uses SZ_32M for 32-bit and SZ_1G for 64-bit. MIPS is 8M and 256M respectively. I don't see anything that indicates this would be a problem. *cross fingers* It looks like x86 would need bumping too: it uses 32M on both 32-bit and 64-bit. STACK_RND_MASK is the same though. > +unsigned long arch_mmap_rnd(void) > +{ > + unsigned long rnd; > + > +#ifdef CONFIG_COMPAT > + if (is_compat_task()) > + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); > + else > +#endif /* CONFIG_COMPAT */ The ifdefs on is_compat_task() are not needed: is_compat_task() returns 0 in the !CONFIG_COMPAT case. > + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); > + > + return rnd << PAGE_SHIFT; > +} > +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ > + > +static int mmap_is_legacy(struct rlimit *rlim_stack) > +{ > + if (current->personality & ADDR_COMPAT_LAYOUT) > + return 1; > + > + if (rlim_stack->rlim_cur == RLIM_INFINITY) > + return 1; > + > + return sysctl_legacy_va_layout; > +} > + > +#define MIN_GAP (SZ_128M) > +#define MAX_GAP (STACK_TOP / 6 * 5) > + > +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > +{ > + unsigned long gap = rlim_stack->rlim_cur; > + unsigned long pad = stack_guard_gap; > + > + /* Account for stack randomization if necessary */ > + if (current->flags & PF_RANDOMIZE) > + pad += (STACK_RND_MASK << PAGE_SHIFT); > + > + /* Values close to RLIM_INFINITY can overflow. */ > + if (gap + pad > gap) > + gap += pad; > + > + if (gap < MIN_GAP) > + gap = MIN_GAP; > + else if (gap > MAX_GAP) > + gap = MAX_GAP; > + > + return PAGE_ALIGN(STACK_TOP - gap - rnd); > +} > + > +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > +{ > + unsigned long random_factor = 0UL; > + > + if (current->flags & PF_RANDOMIZE) > + random_factor = arch_mmap_rnd(); > + > + if (mmap_is_legacy(rlim_stack)) { > + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; > + mm->get_unmapped_area = arch_get_unmapped_area; > + } else { > + mm->mmap_base = mmap_base(random_factor, rlim_stack); > + mm->get_unmapped_area = arch_get_unmapped_area_topdown; > + } > +} > +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) > void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) > { > mm->mmap_base = TASK_UNMAPPED_BASE; > -- > 2.20.1 > -- Kees Cook _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel