ASLR randomizes the addresses of the process executable code, stack, heap and libraries. This is to make the life of an attacker difficult as they cannot hardcode addresses in the code across multiple instantiations.
But how does this help with system libraries? They are never unloaded. Every process uses libc, for example, and so the address of prinf never changes. How is this helping if I am hardcoding the address of printf? This does not change unless the machine reboots.
I do have ASLR enabled --
# cat /proc/sys/kernel/randomize_va_space 2 Here is some sample code --
unsigned long getEBP ( void ) { asm("movl %ebp, %eax"); } int main(void) { int (*p)(const char*, ...) = &printf; printf("printf address = %p \n", p); (*p)("printf address = %p\n", &printf); printf ("EBP:%x\n" ,getEBP ()); } And the output across multiple runs --
# ./a.out printf address = 0x4003c0 printf address = 0x4003c0 EBP:6a71d300 # ./a.out printf address = 0x4003c0 printf address = 0x4003c0 EBP:93e5c100 See the EBP is changing as it should but not the address of printf.
What am I missing?
EDIT: Compiling with -fPIC did not help on my RHEL VM.
# ./a.out printf address = 0x3047a4f0f0 printf address = 0x3047a4f0f0 EBP:7aaac900 # ./a.out printf address = 0x3047a4f0f0 printf address = 0x3047a4f0f0 EBP:632eca20 If the libc calls are in fact randomized as the reply says below, how is it implemented? libc itself is not reloaded, so the actual address of printf is not changing, how can it be randomized?