I am trying to understand the following C program and the layout of the stack. The ultimate goal is to understand the buffer overflow exploit.
#include <unistd.h>
void Test()
{
char buff[4];
printf("Some input: ");
gets(buff);
puts(buff);
}
int main(int argc, char *argv[ ])
{
Test();
return 0;
}
I used -g -fno-stack-protector -O0 to compile the program.
In gdb when I see the assembly of the Test function it looks like this
(gdb) disass Test
Dump of assembler code for function Test:
0x0804847d <+0>: push %ebp
0x0804847e <+1>: mov %esp,%ebp
**0x08048480 <+3>: sub $0x28,%esp**
=> 0x08048483 <+6>: movl $0x8048550,(%esp)
0x0804848a <+13>: call 0x8048330 <printf@plt>
0x0804848f <+18>: lea -0xc(%ebp),%eax
0x08048492 <+21>: mov %eax,(%esp)
0x08048495 <+24>: call 0x8048340 <gets@plt>
0x0804849a <+29>: lea -0xc(%ebp),%eax
0x0804849d <+32>: mov %eax,(%esp)
0x080484a0 <+35>: call 0x8048350 <puts@plt>
0x080484a5 <+40>: leave
0x080484a6 <+41>: ret
End of assembler dump.
I noticed that in assembly the compiler has reserved around 40 bytes of data. This is based on the **0x08048480 <+3>: sub $0x28,%esp**
statement above.
Question: Since the buffer allocated is only 4 byte why is the compiler reserving 40 bytes? What are the other bytes for?
PS:
Please notice that I am disabling all optimizations in compiler and using -fno-stack-protector as an argument. Also the Test function above has no arguments.
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)