Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp173110yba; Wed, 17 Apr 2019 22:22:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqyb1DHL+7THZOe96/05MMwJbkSGiGVGcuWjysLPDDamM+knHvpt1SPP/JA5+e/x0XL8Zx0o X-Received: by 2002:aa7:83d1:: with SMTP id j17mr95209868pfn.78.1555564934576; Wed, 17 Apr 2019 22:22:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555564934; cv=none; d=google.com; s=arc-20160816; b=elSRfTf4pcL2LcuItktt4Bjj2Mje5VhcKyYXDfT9CRRX6IwbY4nlgayAmnplnTJmfm CBmbIrwmrvLkx58koe+ke/Ze0GA9ImREp9HGu8pOyzUEAkJhLd/SKTNDllEoLXj1qUqh NCcETquaIhh/ZjphFRoPDoNK3XUTOlg3/ETADevlFxBVCpsLwQPq2yEF9uoxp7PKBy7C Ws5wE3PcGshoRxhdCvg5Fd3rN0aGq9D7bfcioYJsfv0bhQS7IwUi4X+J+HBcmeDyv06T Plj4f7Zzzkhae6+kour4GsJjRCGUMB26zlaPiev2sVGIOrSwF7UgznIDeq7Gs3ZjwDfa c8XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=IcA/uYI8DV5Uk3TdhA5fHXy0czTBgFSx6RCoxfz+//4=; b=jxLwY0XGHA/JuilkUcIV/wO1U7UEvlQtIUxxtRfEFowR5tYrDG+WUlmVqfpcloBlmf Vp+Lya4ATYY0O+V7JR8WKNijITN1RSsZiL3NVviH8/Q9YTJvvVM+WbqxwBT0da6fBJOu M/FzK12dDhze6Mbo0U+Kl2MQFXx1d3oLDIq7+gfYRJuKr5GxqCBKJVbv3EhLraY1YIB5 H87RpIoQzDzElI/24a3hg7/ZHtyEvryakP5sGkgl2LhfxZ7uHkVFbuH3mrj/ng2ATA3R HIfF5cR1hmKUZhyWQ8RFXseXG9wyHv4dg1xTvbWlvd0SxqxB+t+/IfsDs2h7c5aQMOed UpXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZOmVLE2k; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i68si1147279plb.194.2019.04.17.22.21.58; Wed, 17 Apr 2019 22:22:14 -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; dkim=pass header.i=@chromium.org header.s=google header.b=ZOmVLE2k; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726873AbfDRFVB (ORCPT + 99 others); Thu, 18 Apr 2019 01:21:01 -0400 Received: from mail-vs1-f66.google.com ([209.85.217.66]:37045 "EHLO mail-vs1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725710AbfDRFVA (ORCPT ); Thu, 18 Apr 2019 01:21:00 -0400 Received: by mail-vs1-f66.google.com with SMTP id w13so502173vsc.4 for ; Wed, 17 Apr 2019 22:21:00 -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=IcA/uYI8DV5Uk3TdhA5fHXy0czTBgFSx6RCoxfz+//4=; b=ZOmVLE2kWQEfxAwJtqNT7fptm5m89yjN9pJ4hwhI4Z8f49vkIG1SsNzihNlPHXzko1 XXEV3xLUvXrMTYVGVNWYLrrCC0hIAZCueqmowWDl/0KTMFCsqx1U+m/G2nteU8eiooyN 6sDfWAdWlsvCPZQuCGxF8OqTfh2ZEPtoetyBA= 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=IcA/uYI8DV5Uk3TdhA5fHXy0czTBgFSx6RCoxfz+//4=; b=JIsxHNFx2nRdd1fLmH+0c0VO+J83syvQf1CMsLBjkGQG+aqR/pobTSl8SJCbyNW8gk YtvNMWxcojl/viMKu7y9MxUov5QCHib5sASbD5f7u6sV4X5V56mHCCii4LA1UH1xdiOG 2i1rUda3qWS45zZwCclrdK6E+gDh1fw4kpznBYmO19PA/BLOv6/K4zBga4QZJ0Y8snbZ x201mhlVXkAjYCt3LwWg+yNPAxOfowVqz5XsQhPtWpnrjTEh1U5LULAv4lOMJfotlLD2 F6cfD59zQAbWu0YmhbEMT7fmsYaNSll2igsazsFnKqUJEMDiZI4Ab6XQrEGUHvFgpp25 ld1Q== X-Gm-Message-State: APjAAAWg4Hc0Vo4ciY6QKQb8JnAJNCkGsSxTA2C6ma50bz3csu08pLkM t/mgp/T7a3thm0OwErZpsL1giWrZhWk= X-Received: by 2002:a67:3016:: with SMTP id w22mr19378971vsw.15.1555564858961; Wed, 17 Apr 2019 22:20:58 -0700 (PDT) Received: from mail-ua1-f42.google.com (mail-ua1-f42.google.com. [209.85.222.42]) by smtp.gmail.com with ESMTPSA id j93sm249973uad.6.2019.04.17.22.20.56 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:20:56 -0700 (PDT) Received: by mail-ua1-f42.google.com with SMTP id v7so358943uak.13 for ; Wed, 17 Apr 2019 22:20:56 -0700 (PDT) X-Received: by 2002:a9f:3fce:: with SMTP id m14mr49582984uaj.96.1555564855818; Wed, 17 Apr 2019 22:20:55 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-2-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-2-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:20:44 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 01/11] mm, fs: Move randomize_stack_top from fs 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 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:24 AM Alexandre Ghiti wrote: > > 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 Oh right, here's the generic one... this should probably just copy arm64's version instead. Then x86 can be tweaked (it uses mmap_is_ia32() instead of is_compat_task() by default, but has a weird override..) Regardless, yes, this is a direct code move: Acked-by: Kees Cook -Kees > + > +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 > -- Kees Cook