8

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.

Hubert Kario
  • 21,314
  • 3
  • 24
  • 44
stcatz
  • 341
  • 1
  • 3
  • 8

3 Answers3

6

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)

stcatz
  • 341
  • 1
  • 3
  • 8
3

"`-fstack-protector' emits extra code to check for buffer overflows, such as stack smashing attacks. This is done by adding a guard variable to functions with vulnerable objects. This includes functions that call alloca, and functions with buffers larger than 8 bytes. The guards are initialized when a function is entered and then checked when the function exits. If a guard check fails, an error message is printed and the program exits"

GCC Options That Control Optimization

GCC extension for protecting applications from stack-smashing attacks

Smashing The Stack For Fun And Profit

I Hope this will give some clue..

RP.
  • 717
  • 2
  • 13
  • 29
  • 8
    Please note that `-fstack-protector` and `-fstack-check` are different options. (for fellow googlers) – Offirmo Apr 17 '13 at 16:47
3

-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.

rurban
  • 4,025
  • 24
  • 27