linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Adjust brk randomness
@ 2024-02-17  6:25 Kees Cook
  2024-02-17  6:25 ` [PATCH 1/2] x86: Increase brk randomness entropy on x86_64 Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Kees Cook @ 2024-02-17  6:25 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Kees Cook, Geert Uytterhoeven, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, Peter Zijlstra,
	Qi Zheng, Alexandre Ghiti, Al Viro, Christian Brauner, Jan Kara,
	Eric Biederman, Christophe Leroy, Josh Poimboeuf,
	Kirill A. Shutemov, Rick Edgecombe, Brian Gerst, Bjorn Helgaas,
	Tony Battersby, linux-kernel, y0un9n132, x86, linux-fsdevel,
	linux-mm, linux-hardening

Hi,

It was recently pointed out[1] that x86_64 brk entropy was not great,
and that on all architectures the brk can (when the random offset is 0)
be immediately adjacent to .bss, leaving no gap that could stop linear
overflows from the .bss. Address both issues.

-Kees

Link: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com [1]

Kees Cook (2):
  x86: Increase brk randomness entropy on x86_64
  binfmt_elf: Leave a gap between .bss and brk

 arch/x86/kernel/process.c | 5 ++++-
 fs/binfmt_elf.c           | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] x86: Increase brk randomness entropy on x86_64
  2024-02-17  6:25 [PATCH 0/2] Adjust brk randomness Kees Cook
@ 2024-02-17  6:25 ` Kees Cook
  2024-02-26 10:39   ` Jiri Kosina
  2024-02-17  6:25 ` [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk Kees Cook
  2024-02-18  0:25 ` [PATCH 0/2] Adjust brk randomness H. Peter Anvin
  2 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2024-02-17  6:25 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Kees Cook, y0un9n132, Geert Uytterhoeven, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin,
	Peter Zijlstra, Qi Zheng, Alexandre Ghiti, x86, Al Viro,
	Christian Brauner, Jan Kara, Eric Biederman, Christophe Leroy,
	Josh Poimboeuf, Kirill A. Shutemov, Rick Edgecombe, Brian Gerst,
	Bjorn Helgaas, Tony Battersby, linux-kernel, linux-fsdevel,
	linux-mm, linux-hardening

In commit c1d171a00294 ("x86: randomize brk"), arch_randomize_brk() was
defined to use a 32MB range (13 bits of entropy), but was never increased
when moving to 64-bit. The default arch_randomize_brk() uses 32MB for
32-bit tasks, and 1GB (18 bits of entropy) for 64-bit tasks. Update
x86_64 to match the entropy used by arm64 and other 64-bit architectures.

Reported-by: y0un9n132@gmail.com
Closes: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com/
Signed-off-by: Kees Cook <keescook@chromium.org>
---
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: x86@kernel.org
---
 arch/x86/kernel/process.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index ab49ade31b0d..45a9d496fe2a 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -1030,7 +1030,10 @@ unsigned long arch_align_stack(unsigned long sp)
 
 unsigned long arch_randomize_brk(struct mm_struct *mm)
 {
-	return randomize_page(mm->brk, 0x02000000);
+	if (mmap_is_ia32())
+		return randomize_page(mm->brk, SZ_32M);
+
+	return randomize_page(mm->brk, SZ_1G);
 }
 
 /*
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk
  2024-02-17  6:25 [PATCH 0/2] Adjust brk randomness Kees Cook
  2024-02-17  6:25 ` [PATCH 1/2] x86: Increase brk randomness entropy on x86_64 Kees Cook
@ 2024-02-17  6:25 ` Kees Cook
  2024-04-24 19:20   ` (subset) " Kees Cook
  2024-02-18  0:25 ` [PATCH 0/2] Adjust brk randomness H. Peter Anvin
  2 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2024-02-17  6:25 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Kees Cook, y0un9n132, Geert Uytterhoeven, Alexander Viro,
	Christian Brauner, Jan Kara, Eric Biederman, Christophe Leroy,
	linux-fsdevel, linux-mm, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, Peter Zijlstra,
	Qi Zheng, Alexandre Ghiti, Josh Poimboeuf, Kirill A. Shutemov,
	Rick Edgecombe, Brian Gerst, Bjorn Helgaas, Tony Battersby,
	linux-kernel, x86, linux-hardening

Currently the brk starts its randomization immediately after .bss,
which means there is a chance that when the random offset is 0, linear
overflows from .bss can reach into the brk area. Leave at least a single
page gap between .bss and brk (when it has not already been explicitly
relocated into the mmap range).

Reported-by: y0un9n132@gmail.com
Closes: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com/
Signed-off-by: Kees Cook <keescook@chromium.org>
---
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-mm@kvack.org
---
 fs/binfmt_elf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 5397b552fbeb..7862962f7a85 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1262,6 +1262,9 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
 		    elf_ex->e_type == ET_DYN && !interpreter) {
 			mm->brk = mm->start_brk = ELF_ET_DYN_BASE;
+		} else {
+			/* Otherwise leave a gap between .bss and brk. */
+			mm->brk = mm->start_brk = mm->brk + PAGE_SIZE;
 		}
 
 		mm->brk = mm->start_brk = arch_randomize_brk(mm);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] Adjust brk randomness
  2024-02-17  6:25 [PATCH 0/2] Adjust brk randomness Kees Cook
  2024-02-17  6:25 ` [PATCH 1/2] x86: Increase brk randomness entropy on x86_64 Kees Cook
  2024-02-17  6:25 ` [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk Kees Cook
@ 2024-02-18  0:25 ` H. Peter Anvin
  2024-02-18  1:25   ` Kees Cook
  2 siblings, 1 reply; 7+ messages in thread
From: H. Peter Anvin @ 2024-02-18  0:25 UTC (permalink / raw)
  To: Kees Cook, Jiri Kosina
  Cc: Geert Uytterhoeven, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, Peter Zijlstra, Qi Zheng,
	Alexandre Ghiti, Al Viro, Christian Brauner, Jan Kara,
	Eric Biederman, Christophe Leroy, Josh Poimboeuf,
	Kirill A. Shutemov, Rick Edgecombe, Brian Gerst, Bjorn Helgaas,
	Tony Battersby, linux-kernel, y0un9n132, x86, linux-fsdevel,
	linux-mm, linux-hardening

On February 16, 2024 10:25:42 PM PST, Kees Cook <keescook@chromium.org> wrote:
>Hi,
>
>It was recently pointed out[1] that x86_64 brk entropy was not great,
>and that on all architectures the brk can (when the random offset is 0)
>be immediately adjacent to .bss, leaving no gap that could stop linear
>overflows from the .bss. Address both issues.
>
>-Kees
>
>Link: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com [1]
>
>Kees Cook (2):
>  x86: Increase brk randomness entropy on x86_64
>  binfmt_elf: Leave a gap between .bss and brk
>
> arch/x86/kernel/process.c | 5 ++++-
> fs/binfmt_elf.c           | 3 +++
> 2 files changed, 7 insertions(+), 1 deletion(-)
>

Why do we even have the brk, or perhaps more importantly, why do we use it? Is there any reason whatsoever why glibc uses brk instead of mmap to her heap memory?

I thought the base of the brk wasn't even known to userspace other than in the form of the image end...

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] Adjust brk randomness
  2024-02-18  0:25 ` [PATCH 0/2] Adjust brk randomness H. Peter Anvin
@ 2024-02-18  1:25   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2024-02-18  1:25 UTC (permalink / raw)
  To: H. Peter Anvin
  Cc: Jiri Kosina, Geert Uytterhoeven, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, Peter Zijlstra, Qi Zheng,
	Alexandre Ghiti, Al Viro, Christian Brauner, Jan Kara,
	Eric Biederman, Christophe Leroy, Josh Poimboeuf,
	Kirill A. Shutemov, Rick Edgecombe, Brian Gerst, Bjorn Helgaas,
	Tony Battersby, linux-kernel, y0un9n132, x86, linux-fsdevel,
	linux-mm, linux-hardening

On Sat, Feb 17, 2024 at 04:25:33PM -0800, H. Peter Anvin wrote:
> On February 16, 2024 10:25:42 PM PST, Kees Cook <keescook@chromium.org> wrote:
> >Hi,
> >
> >It was recently pointed out[1] that x86_64 brk entropy was not great,
> >and that on all architectures the brk can (when the random offset is 0)
> >be immediately adjacent to .bss, leaving no gap that could stop linear
> >overflows from the .bss. Address both issues.
> >
> >-Kees
> >
> >Link: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com [1]
> >
> >Kees Cook (2):
> >  x86: Increase brk randomness entropy on x86_64
> >  binfmt_elf: Leave a gap between .bss and brk
> >
> > arch/x86/kernel/process.c | 5 ++++-
> > fs/binfmt_elf.c           | 3 +++
> > 2 files changed, 7 insertions(+), 1 deletion(-)
> >
> 
> Why do we even have the brk, or perhaps more importantly, why do we use it? Is there any reason whatsoever why glibc uses brk instead of mmap to her heap memory?
> 
> I thought the base of the brk wasn't even known to userspace other than in the form of the image end...

AFAIK, it's part of ELF ABI, and the loader uses it only for very early
allocations. e.g. it's what shows up as "[heap]" in /proc/$pid/maps.
It's also available to any program that wants it still (see "man brk").
I don't think glibc has plans to redirect it.

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86: Increase brk randomness entropy on x86_64
  2024-02-17  6:25 ` [PATCH 1/2] x86: Increase brk randomness entropy on x86_64 Kees Cook
@ 2024-02-26 10:39   ` Jiri Kosina
  0 siblings, 0 replies; 7+ messages in thread
From: Jiri Kosina @ 2024-02-26 10:39 UTC (permalink / raw)
  To: Kees Cook
  Cc: y0un9n132, Geert Uytterhoeven, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, Peter Zijlstra,
	Qi Zheng, Alexandre Ghiti, x86, Al Viro, Christian Brauner,
	Jan Kara, Eric Biederman, Christophe Leroy, Josh Poimboeuf,
	Kirill A. Shutemov, Rick Edgecombe, Brian Gerst, Bjorn Helgaas,
	Tony Battersby, linux-kernel, linux-fsdevel, linux-mm,
	linux-hardening

On Fri, 16 Feb 2024, Kees Cook wrote:

> In commit c1d171a00294 ("x86: randomize brk"), arch_randomize_brk() was
> defined to use a 32MB range (13 bits of entropy), but was never increased
> when moving to 64-bit. The default arch_randomize_brk() uses 32MB for
> 32-bit tasks, and 1GB (18 bits of entropy) for 64-bit tasks. Update
> x86_64 to match the entropy used by arm64 and other 64-bit architectures.
> 
> Reported-by: y0un9n132@gmail.com
> Closes: https://lore.kernel.org/linux-hardening/CA+2EKTVLvc8hDZc+2Yhwmus=dzOUG5E4gV7ayCbu0MPJTZzWkw@mail.gmail.com/
> Signed-off-by: Kees Cook <keescook@chromium.org>

Wow, this is a pretty aged code indeed.

	Acked-by: Jiri Kosina <jkosina@suse.com>

Thanks,

-- 
Jiri Kosina
SUSE Labs


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (subset) [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk
  2024-02-17  6:25 ` [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk Kees Cook
@ 2024-04-24 19:20   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2024-04-24 19:20 UTC (permalink / raw)
  To: Jiri Kosina, Kees Cook
  Cc: y0un9n132, Geert Uytterhoeven, Alexander Viro, Christian Brauner,
	Jan Kara, Eric Biederman, Christophe Leroy, linux-fsdevel,
	linux-mm, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, Peter Zijlstra, Qi Zheng,
	Alexandre Ghiti, Josh Poimboeuf, Kirill A. Shutemov,
	Rick Edgecombe, Brian Gerst, Bjorn Helgaas, Tony Battersby,
	linux-kernel, x86, linux-hardening

On Fri, 16 Feb 2024 22:25:44 -0800, Kees Cook wrote:
> Currently the brk starts its randomization immediately after .bss,
> which means there is a chance that when the random offset is 0, linear
> overflows from .bss can reach into the brk area. Leave at least a single
> page gap between .bss and brk (when it has not already been explicitly
> relocated into the mmap range).
> 
> 
> [...]

Patch 1/2 was already applied via x86 tip, so I'll grab this one for the execve/binfmt tree.

Applied to for-next/execve.

[2/2] binfmt_elf: Leave a gap between .bss and brk
      https://git.kernel.org/kees/c/2a5eb9995528

Take care,

-- 
Kees Cook


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-24 19:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-17  6:25 [PATCH 0/2] Adjust brk randomness Kees Cook
2024-02-17  6:25 ` [PATCH 1/2] x86: Increase brk randomness entropy on x86_64 Kees Cook
2024-02-26 10:39   ` Jiri Kosina
2024-02-17  6:25 ` [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk Kees Cook
2024-04-24 19:20   ` (subset) " Kees Cook
2024-02-18  0:25 ` [PATCH 0/2] Adjust brk randomness H. Peter Anvin
2024-02-18  1:25   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).