It depends a lot on your architecture and compiler flags, so itβs impossible to point out one thing and say βit should be thisβ here. However, I can give you some pointers that may help you.
First, consider the border of the stack. You may have heard of the -mpreferred-stack-border = X flag for GCC. If not, this basically tells your compiler to have your stack values ββbe 2 ^ X bytes each. Then your compiler will try to optimize your program so that these values ββmatch the stack as closely as possible. On the other hand, a GCC modifier, such as __packed__, will cause the compiler to try to set the data on the stack as accurately as possible.
There is also a stack protector. Basically, GCC pushes dummy values ββonto the stack, which ensures that buffer overflows can do no harm except segfaulting your program (which is not fun, but better than an attacker that controls the instruction pointer). You can easily try this: grab any latest version of GCC and let the user overflow the buffer. You will notice that the program exits with a message in the line "stack detection, termination". Try compiling your program with -fno-stack-protector, and the allocated local memory on the stack is likely to be less.
source share