Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5672139img; Wed, 27 Mar 2019 12:54:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxIejTrLPwKMIEKL1b8yilc2asCWOUHiYquxEQjQ3hZQeJayjw8ZijSFUTq35xuJnUJcY93 X-Received: by 2002:a65:4108:: with SMTP id w8mr36005935pgp.236.1553716484915; Wed, 27 Mar 2019 12:54:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553716484; cv=none; d=google.com; s=arc-20160816; b=QXkHg5pDdvwuL6HQL7yPxKoPKnN+JGHRQjQfAeb1x/BZ4pjP7cQthWnmY6Rv9nG9kU P51vtgJuy7I8sK4hfkBBpBPpdUWlAP0X8xJr87HIEuUKiKp6dRA0NOimTDU8lCyiOhkr MFNz6+voIQ73amb1SJtd0beJjHiKOfe0hUs7ruaMEBQZbhNg05p/2EC43a6Osx2D0Nk5 EYTyP5UTbK+ppKIP6T8OGbefpV9CA4SWxUPJ/fnyTpt9B0zpbt68KE+6DgQSYJs8f1gh 5h9cnZkCDZJl+X/EpXKTKgq46C1WRCJBzU10XeWEgavx0nb8F2vg7RK8B9ybsbht8UoO O52A== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ZtHr4JPeCg91ihWiUcGr2uK2mi+PMwRZ8iP/7/zC0gg=; b=dJdL/4qL8GSsuyV+nL9rXKM14x8HmHQtQv0I1aROGTrGRX5GCke64okdHf8SaYRb+w dZ8w0kf9r8QbN8jjmC+fS1+HAYMuy8Ax7iSiEs2kKJIndWaj9QSlLBGSt40h4WhMAHsl RPBI9YfTQ+iHRjMW3SfH6nh/kpUhTGJ7Go5LzYd9cnpaKg6BCUt7fdPBG9qAoMDYZROY 0Yeu6/UsNm+2o6CSjqJhPTQ/pul3tCCEDAM9BlMaXXgk4ZX+Z3Hkhwc0vGEglE8nhUHk ZtlTd21ZeqdBWgvxrFgjxlZmQ2yvrbHp4tBSf2NaVF2I/3rdov5+abJOHRQdpQhK04OM gEjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WrOZnux8; 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 k16si19108279pfj.174.2019.03.27.12.54.29; Wed, 27 Mar 2019 12:54:44 -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=WrOZnux8; 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 S1730255AbfC0Tvt (ORCPT + 99 others); Wed, 27 Mar 2019 15:51:49 -0400 Received: from mail-ua1-f66.google.com ([209.85.222.66]:46657 "EHLO mail-ua1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729381AbfC0Tvt (ORCPT ); Wed, 27 Mar 2019 15:51:49 -0400 Received: by mail-ua1-f66.google.com with SMTP id v7so50258uak.13 for ; Wed, 27 Mar 2019 12:51: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:content-transfer-encoding; bh=ZtHr4JPeCg91ihWiUcGr2uK2mi+PMwRZ8iP/7/zC0gg=; b=WrOZnux8a7iPl9MuKH530IQzJaH1K953croGMy3TVkHKgwYAASFW8a6gyJhh88vuCP QQgfdjESH3+TE+Q4GfAxA9PD15t41Wp0Sd9YiIrLMwZs0Ih9oPskKpTuebCDSju/Tzek cRSkh/LIKTbK92iB4uctkeDRBoBP1wXBLza3I= 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:content-transfer-encoding; bh=ZtHr4JPeCg91ihWiUcGr2uK2mi+PMwRZ8iP/7/zC0gg=; b=P6zsUPZCWxqa57EsfszMwf+U9DjyMf1/gMVna6tvncrvsdSrEr0EliQUkoI4PVh92o Hvy74LRTnlAwpiw41mD5omsNoGdKtBrs/ZvAySilk8FQgc00GGUO3zKWzO8nZ3fFqP45 Ozn44Ru9RoIM26Ncrh6dy9l4KM6sMdpZilCKctvnCvaixrBr+ncxQzY6Xe3oqEQ1L/e7 7IV0htaEhbsvi81VvyRTD4FXEO1S/3wymhewx/Hht1VyIKkkLHUl5pR3pmBJDGTgb7AA 7ghBHaZbZ8tnXyfE74cOVEbJOYpTVt82DA8Gya04jLqT2L2JgEh56nRnHM8nQ2WEwtj9 YDoA== X-Gm-Message-State: APjAAAU/CcHven5X8wBcEzASgYXvbx5PrNOZBfsnEXo2MGGMwvVcy/nD mM/zs3lySSrNdWee15KYUd+Osn/9bjI= X-Received: by 2002:ab0:702a:: with SMTP id u10mr6226491ual.5.1553716307314; Wed, 27 Mar 2019 12:51:47 -0700 (PDT) Received: from mail-vs1-f44.google.com (mail-vs1-f44.google.com. [209.85.217.44]) by smtp.gmail.com with ESMTPSA id d188sm4996777vka.48.2019.03.27.12.51.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Mar 2019 12:51:46 -0700 (PDT) Received: by mail-vs1-f44.google.com with SMTP id w26so9645788vsk.13 for ; Wed, 27 Mar 2019 12:51:45 -0700 (PDT) X-Received: by 2002:a67:6983:: with SMTP id e125mr22627980vsc.222.1553716305244; Wed, 27 Mar 2019 12:51:45 -0700 (PDT) MIME-Version: 1.0 References: <20190312173248.13490-1-alisaidi@amazon.com> <20190312173248.13490-3-alisaidi@amazon.com> In-Reply-To: From: Kees Cook Date: Wed, 27 Mar 2019 12:51:33 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] x86/mmap: handle worst-case heap randomization in mmap_base To: Ali Saidi , Michal Hocko , Matthew Wilcox , Jann Horn Cc: LKML , linux-arm-kernel , X86 ML , "H. Peter Anvin" , Andrew Morton , Borislav Petkov , Ingo Molnar , Thomas Gleixner , Peter Zijlstra , Andy Lutomirski , Dave Hansen , Will Deacon , Catalin Marinas , David Woodhouse , Anthony Liguori Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 13, 2019 at 3:58 PM Kees Cook wrote: > > On Tue, Mar 12, 2019 at 10:33 AM Ali Saidi wrote: > > > > Increase mmap_base by the worst-case brk randomization so that > > the stack and heap remain apart. > > > > In Linux 4.13 a change was committed that special cased the kernel ELF > > loader when the loader is invoked directly (eab09532d400; binfmt_elf: u= se > > ELF_ET_DYN_BASE only for PIE). Generally, the loader isn=E2=80=99t invo= ked > > directly and this issue is limited to cases where it is, (e.g to set a > > non-inheritable LD_LIBRARY_PATH, testing new versions of the loader). I= n > > those rare cases, the loader doesn't take into account the amount of br= k > > randomization that will be applied by arch_randomize_brk(). This can > > lead to the stack and heap being arbitrarily close to each other. > > In the case of using the loader directly, brk (so helpfully identified > as "[heap]") is allocated with the _loader_ not the binary. For > example, with ASLR entirely disabled, you can see this more clearly: > > $ /bin/cat /proc/self/maps > 555555554000-55555555c000 r-xp 00000000 fd:02 34603015 > /bin/cat > 55555575b000-55555575c000 r--p 00007000 fd:02 34603015 > /bin/cat > 55555575c000-55555575d000 rw-p 00008000 fd:02 34603015 > /bin/cat > 55555575d000-55555577e000 rw-p 00000000 00:00 0 = [heap] > ... > 7ffff7ff7000-7ffff7ffa000 r--p 00000000 00:00 0 = [vvar] > 7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 = [vdso] > 7ffff7ffc000-7ffff7ffd000 r--p 00027000 fd:02 49287483 > /lib/x86_64-linux-gnu/ld-2.27.so > 7ffff7ffd000-7ffff7ffe000 rw-p 00028000 fd:02 49287483 > /lib/x86_64-linux-gnu/ld-2.27.so > 7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 > 7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 = [stack] > > $ /lib/x86_64-linux-gnu/ld-2.27.so /bin/cat /proc/self/maps > ... > 7ffff7bcc000-7ffff7bd4000 r-xp 00000000 fd:02 34603015 > /bin/cat > 7ffff7bd4000-7ffff7dd3000 ---p 00008000 fd:02 34603015 > /bin/cat > 7ffff7dd3000-7ffff7dd4000 r--p 00007000 fd:02 34603015 > /bin/cat > 7ffff7dd4000-7ffff7dd5000 rw-p 00008000 fd:02 34603015 > /bin/cat > 7ffff7dd5000-7ffff7dfc000 r-xp 00000000 fd:02 49287483 > /lib/x86_64-linux-gnu/ld-2.27.so > 7ffff7fb2000-7ffff7fd6000 rw-p 00000000 00:00 0 > 7ffff7ff7000-7ffff7ffa000 r--p 00000000 00:00 0 = [vvar] > 7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 = [vdso] > 7ffff7ffc000-7ffff7ffd000 r--p 00027000 fd:02 49287483 > /lib/x86_64-linux-gnu/ld-2.27.so > 7ffff7ffd000-7ffff7ffe000 rw-p 00028000 fd:02 49287483 > /lib/x86_64-linux-gnu/ld-2.27.so > 7ffff7ffe000-7ffff8020000 rw-p 00000000 00:00 0 = [heap] > 7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 = [stack] > > So I think changing this globally isn't the right solution (normally > brk is between text and mmap). Adjusting the mmap base padding means > we lose even more memory space. Perhaps it would be better if brk > allocation would be placed before the mmap region (i.e. use > ELF_ET_DYN_BASE). This seems to work for me: > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > index 7d09d125f148..cdaa33f4a3ef 100644 > --- a/fs/binfmt_elf.c > +++ b/fs/binfmt_elf.c > @@ -1131,6 +1131,15 @@ static int load_elf_binary(struct linux_binprm *bp= rm) > current->mm->end_data =3D end_data; > current->mm->start_stack =3D bprm->p; > > + /* > + * When executing a loader directly (ET_DYN without Interp), move > + * the brk area out of the mmap region (since it grows up, and ma= y > + * collide early with the stack growing down), and into the unuse= d > + * ELF_ET_DYN_BASE region. > + */ > + if (!elf_interpreter) > + current->mm->brk =3D current->mm->start_brk =3D ELF_ET_DY= N_BASE; > + > if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) = { > current->mm->brk =3D current->mm->start_brk =3D > arch_randomize_brk(current->mm); > > $ /lib/x86_64-linux-gnu/ld-2.27.so /bin/cat /proc/self/maps > 555556de3000-555556e04000 rw-p 00000000 00:00 0 = [heap] > 7f8467da9000-7f8467f90000 r-xp 00000000 fd:01 399295 > /lib/x86_64-linux-gnu/libc-2.27.so > ... > 7f846819a000-7f84681a2000 r-xp 00000000 fd:01 263229 > /bin/cat > ... > 7f84685cb000-7f84685cc000 rw-p 00028000 fd:01 399286 > /lib/x86_64-linux-gnu/ld-2.27.so > 7f84685cc000-7f84685cd000 rw-p 00000000 00:00 0 > 7ffce68f8000-7ffce6919000 rw-p 00000000 00:00 0 = [stack] > 7ffce69f0000-7ffce69f3000 r--p 00000000 00:00 0 = [vvar] > 7ffce69f3000-7ffce69f4000 r-xp 00000000 00:00 0 = [vdso] > > Does anyone see problems with this? (Note that ET_EXEC base is > 0x400000, so no collision there...) Adding some more people to CC... what do people think about this moving of the brk to ELF_ET_DYN_BASE in this corner-case? Anything that worked before should still work (i.e. the ultimately-launched binary already had the brk very far from its text, so this should be no different from a COMPAT_BRK standpoint). The only risk I see here is that if someone started to suddenly depend on the entire memory space above the mmap region being available when launching binaries via a direct loader execs... which seems highly unlikely, I'd hope: this would mean a binary would not work when execed normally. --=20 Kees Cook