Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the gcc option -fstack-check exactly work?

Tags:

stack

gcc

My program crashed when I added the option -fstack-check and -fstack-protector. __stack_chk_fail is called in the back trace.

So how could I know where the problem is ? What does -fstack-check really check ? The information about gcc seems too huge to find out the answer.

like image 393
stcatz Avatar asked Mar 03 '10 09:03

stcatz


People also ask

What does the GCC option do?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.

What exactly happens when you compile ac program How does it work?

c is called the source file which keeps the code of the program. Now, when we compile the file, the C compiler looks for errors. If the C compiler reports no error, then it stores the file as a . obj file of the same name, called the object file.

How does the C compilation compiler work?

It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. The c compilation process converts the source code taken as input into the object code or machine code.

How does the compiler work?

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.


2 Answers

After checked the assembly program. I think -fstack-check, will add code write 0 to an offset of the stack pointer, so to test if the program visit a violation address, the program went crash if it does. e.g. mov $0x0,-0x928(%esp)

like image 58
stcatz Avatar answered Jan 31 '23 00:01

stcatz


-fstack-check: If two feature macros STACK_CHECK_BUILTIN and STACK_CHECK_STATIC_BUILTIN are left at the default 0, it just inserts a NULL byte every 4kb (page) when the stack grows. By default only one, but when the stack can grow more than one page, which is the most dangerous case, every 4KB. linux >2.6 only has only one small page gap between the stack and the heap, which can lead to stack-gap attacks, known since 2005. See What exception is raised in C by GCC -fstack-check option for assembly. It is enabled in gcc at least since 2.95.3, in clang since 3.6.

__stack_chk_fail is the inserted -fstack-protector code which verifies an inserted stack canary value which might be overwritten by a simple stack overflow, e.g. by recursion.

like image 34
rurban Avatar answered Jan 31 '23 02:01

rurban