Since I know that the stack is a FILO segment (First In Last Out,) when creating this program (shown below,) I think the auth_flag vairable is stored after the password_buffer variable:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_authentication(char *password) {
int auth_flag = 0;
char password_buffer[16];
...
}
int main(int argc, char *argv[]) {
....
}
When I check the places of auth_flag and password_buffer in gdb, I indeed see that auth_flag is after password_buffer by 28 bytes:
(gdb) x/s password_buffer
0x7fffffffe3f0: "\001"
(gdb) x/xw &auth_flag
0x7fffffffe40c: 0x00000000
(gdb) # now let's print how many bytes is auth_flag away from password_buffer
(gdb) print 0x7fffffffe40c - 0x7fffffffe3f0
$3 = 28
(gdb) # so auth_flag is 28 bytes after password_buffer
After reversing the variables' declaration, I expect to have password_buffer stored after auth_flag:
int check_authentication(char *password) {
char password_buffer[16];
int auth_flag = 0;
...
}
However, this is not what happens, since experimenting with gdb produced the same results. How is this possible? Shouldn't auth_flag be before password_buffer?