I think you are misunderstanding the effects of ASLR (Address Space Layout Randomization): ASLR randomly arranges the positions of different virtual memory areas of a process (executable, stack, heap, data, libraries, etc) to be different in every execution. It does not change the relative position of elements in the same virtual memory area.
Take for example the following simple program:
int main(void) {
struct {
char a[10];
char b[10];
} myvar;
printf("a: %p\n", &myvar.a);
printf("b: %p\n", &myvar.b);
return 0;
}
Here's the program's virtual memory with ASLR disabled:
0x555555554000 0x555555555000 r-xp 1000 0 /home/marco/test/test [executable segment]
0x555555754000 0x555555755000 r--p 1000 0 /home/marco/test/test [read only data]
0x555555755000 0x555555756000 rw-p 1000 1000 /home/marco/test/test [bss (global variables)]
0x7ffffffde000 0x7ffffffff000 rw-p 21000 0 [stack] <-- myvar is here
Output (ASLR disabled):
$ ./test
a: 0x7ffffffde080
b: 0x7ffffffde08a
$ ./test
a: 0x7ffffffde080
b: 0x7ffffffde08a
$ ./test
a: 0x7ffffffde080
b: 0x7ffffffde08a
And here's the same program with ASLR enabled:
0x559fefcbe000 0x559fefcbf000 r-xp 1000 0 /home/marco/test/test [executable segment]
0x559fefebe000 0x559fefebf000 r--p 1000 0 /home/marco/test/test [read only data]
0x559fefebf000 0x559fefec0000 rw-p 1000 1000 /home/marco/test/test [bss (global variables)]
0x7ffe3bb5e000 0x7ffe3bb7f000 rw-p 21000 0 [stack] <-- myvar is here
Output (ASLR enabled):
$ ./test
a: 0x7ffe3bb5e080
b: 0x7ffe3bb5e08a
$ ./test
a: 0x7ff4abdeea80
b: 0x7ff4abdeea8a
$ ./test
a: 0x7efa6b8fa080
b: 0x7efa6b8fa08a
Your variable is still going to be inside a certain contiguous block of virtual memory, and the relative position of the fields will not change at all. Contiguous arrays will still be contiguous using ASLR: they will just start at a different position in memory.
Since struct
fields are by standard contiguous in memory (and follow their declaration order), this means that buffer overflow will still be a potential problem, even when using ASLR.