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. :^)
This commit is contained in:
Andreas Kling 2021-02-14 11:47:25 +01:00
parent 4188373020
commit e47bffdc8c

View file

@ -90,7 +90,10 @@ static bool validate_stack_size(const Vector<String>& arguments, const Vector<St
static KResultOr<FlatPtr> make_userspace_stack_for_main_thread(Region& region, Vector<String> arguments, Vector<String> environment, Vector<ELF::AuxiliaryValue> 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<u32>() % 4096, 16);
auto push_on_new_stack = [&new_esp](u32 value) {
new_esp -= 4;