From e47bffdc8c1b69614d24d135f2ce26dcd8e35ca1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 14 Feb 2021 11:47:25 +0100 Subject: [PATCH] Kernel: Add some bits of randomness to the userspace stack pointer This patch adds a random offset between 0 and 4096 to the initial stack pointer in new processes. Since the stack has to be 16-byte aligned, the bottom bits can't be randomized. Yet another thing to make things less predictable. :^) --- Kernel/Syscalls/execve.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 033c8c1f0e..7301e853e8 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -90,7 +90,10 @@ static bool validate_stack_size(const Vector& arguments, const Vector make_userspace_stack_for_main_thread(Region& region, Vector arguments, Vector environment, Vector auxiliary_values) { - FlatPtr new_esp = region.vaddr().offset(Thread::default_userspace_stack_size).get(); + FlatPtr new_esp = region.range().end().get(); + + // Add some bits of randomness to the user stack pointer. + new_esp -= round_up_to_power_of_two(get_fast_random() % 4096, 16); auto push_on_new_stack = [&new_esp](u32 value) { new_esp -= 4;