added 1 changeset to branch 'refs/remotes/pdziepak-github/aslr' old head: 0000000000000000000000000000000000000000 new head: fe9b18223abc0c92e2bbf3d80df8dfaf22e680a8 overview: https://github.com/pdziepak/Haiku/compare/fe9b182 ---------------------------------------------------------------------------- fe9b182: arch: randomize initial user stack pointer Inside the page randomization of initial user stack pointer is not only a part of ASLR implementation but also a performance improvement that helps eliminating aligned 64 kB data access. Minimal user stack size is increased to 8 kB in order to ensure that regardless of initial stack pointer value there is still enough space on stack. [ Pawel Dziepak <pdziepak@xxxxxxxxxxx> ] ---------------------------------------------------------------------------- Commit: fe9b18223abc0c92e2bbf3d80df8dfaf22e680a8 Author: Pawel Dziepak <pdziepak@xxxxxxxxxxx> Date: Mon Feb 25 02:26:49 2013 UTC ---------------------------------------------------------------------------- 3 files changed, 21 insertions(+), 1 deletion(-) headers/private/system/thread_defs.h | 2 +- src/system/kernel/arch/x86/32/thread.cpp | 10 ++++++++++ src/system/kernel/arch/x86/64/thread.cpp | 10 ++++++++++ ---------------------------------------------------------------------------- diff --git a/headers/private/system/thread_defs.h b/headers/private/system/thread_defs.h index 2d55937..3d7a3c1 100644 --- a/headers/private/system/thread_defs.h +++ b/headers/private/system/thread_defs.h @@ -15,7 +15,7 @@ #define USER_STACK_GUARD_SIZE (4 * B_PAGE_SIZE) // 16 kB #define USER_MAIN_THREAD_STACK_SIZE (16 * 1024 * 1024) // 16 MB #define USER_STACK_SIZE (256 * 1024) // 256 kB -#define MIN_USER_STACK_SIZE (4 * 1024) // 4 KB +#define MIN_USER_STACK_SIZE (8 * 1024) // 8 kB #define MAX_USER_STACK_SIZE (16 * 1024 * 1024) // 16 MB diff --git a/src/system/kernel/arch/x86/32/thread.cpp b/src/system/kernel/arch/x86/32/thread.cpp index 5878aac..9e13a7e 100644 --- a/src/system/kernel/arch/x86/32/thread.cpp +++ b/src/system/kernel/arch/x86/32/thread.cpp @@ -200,6 +200,14 @@ arch_thread_dump_info(void *info) } +static addr_t +arch_randomize_stack_pointer(addr_t value) +{ + value -= rand() & (B_PAGE_SIZE - 1); + return value & ~0xful; +} + + /*! Sets up initial thread context and enters user space */ status_t @@ -214,6 +222,8 @@ arch_thread_enter_userspace(Thread* thread, addr_t entry, void* args1, TRACE(("arch_thread_enter_userspace: entry 0x%lx, args %p %p, " "ustack_top 0x%lx\n", entry, args1, args2, stackTop)); + stackTop = arch_randomize_stack_pointer(stackTop); + // copy the little stub that calls exit_thread() when the thread entry // function returns, as well as the arguments of the entry function stackTop -= codeSize; diff --git a/src/system/kernel/arch/x86/64/thread.cpp b/src/system/kernel/arch/x86/64/thread.cpp index 0379777..8c5c2fa 100644 --- a/src/system/kernel/arch/x86/64/thread.cpp +++ b/src/system/kernel/arch/x86/64/thread.cpp @@ -197,6 +197,14 @@ arch_thread_dump_info(void* info) } +static addr_t +arch_randomize_stack_pointer(addr_t value) +{ + value -= rand() & (B_PAGE_SIZE - 1); + return value & ~0xful; +} + + /*! Sets up initial thread context and enters user space */ status_t @@ -208,6 +216,8 @@ arch_thread_enter_userspace(Thread* thread, addr_t entry, void* args1, TRACE("arch_thread_enter_userspace: entry %#lx, args %p %p, " "stackTop %#lx\n", entry, args1, args2, stackTop); + stackTop = arch_randomize_stack_pointer(stackTop); + // Copy the little stub that calls exit_thread() when the thread entry // function returns. // TODO: This will become a problem later if we want to support execute