Adjust brk randomness
Subsystems (4)
cover Adjust brk randomness
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
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(-)
Signature · 1 line
2.34.1
PATCH 1/2 x86: Increase brk randomness entropy on x86_64
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.
---
Cc: Jiri Kosina <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Qi Zheng <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: [email protected]
--- arch/x86/kernel/process.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/process.c +4 -1
@@ -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 ); }/*
Signature · 1 line
2.34.1
Re: [PATCH 1/2] x86: Increase brk randomness entropy on x86_64
Quoted 9 lines · Kees Cook
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 defaultarch_randomize_brk()uses 32MB for 32-bit tasks, and 1GB (18 bits of entropy) for 64-bit tasks. Updatex86_64to match the entropy used by arm64 and other 64-bit architectures.
Wow, this is a pretty aged code indeed.
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
[tip: x86/core] x86: Increase brk randomness entropy for 64-bit systems
In reply to Kees CookThe following commit has been merged into the x86/core branch of tip:
The following commit has been merged into the x86/core branch of tip:
Commit-ID: 44c76825d6eefee9eb7ce06c38e1a6632ac7eb7d Gitweb: https://git.kernel.org/tip/44c76825d6eefee9eb7ce06c38e1a6632ac7eb7d Author: Kees Cook <[email protected]> AuthorDate: Fri, 16 Feb 2024 22:25:43 -08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Tue, 27 Feb 2024 00:23:55 +01:00
x86: Increase brk randomness entropy for 64-bit systems
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.
--- arch/x86/kernel/process.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/process.c +4 -1
@@ -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 ); }/*
PATCH 2/2 binfmt_elf: Leave a gap between .bss and brk
In reply to Kees CookCurrently 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).
---
Cc: Jiri Kosina <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: Christophe Leroy <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- fs/binfmt_elf.c | 3 +++ 1 file changed, 3 insertions(+)
fs/binfmt_elf.c +3 -0
@@ -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);
Signature · 1 line
2.34.1
Kees Cook
AppliedRe: (subset) [PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk
Quoted 8 lines · Kees Cook
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,
Signature · 1 line
Kees Cook
H. Peter Anvin
reviewerQuoted 11 lines · Kees Cook
Hi,
It was recently pointed out[1] that
x86_64brk 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
Quoted 8 lines
Kees Cook (2):
x86: Increase brk randomness entropy onx86_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...
Quoted 16 lines · H. Peter Anvin
Quoted 15 lines · Kees Cook
Hi,
It was recently pointed out[1] that
x86_64brk 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
Kees Cook (2):
x86: Increase brk randomness entropy onx86_64
binfmt_elf: Leave a gap between .bss and brk
Quoted 8 lines
Quoted 4 lines
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.
Signature · 1 line
Kees Cook