Search code examples
cgccstack-overflowstack-smash

How does gcc's -fstack-protector option prevent stack smashing?


I've been running into a stack-smashing issue and I'm having difficulty finding the cause. The stack smashing error only happens occasionally, and only at the very end of the program's execution. It also stops happening completely when I compile it using the 'fstack-protector' option with gcc. I'm wondering if using the 'fstack-protector' option is an actual solution or if I'm just hiding the problem? I'd post the code but it's 3000 lines long and I'm not sure which part of the code is responsible.


Solution

  • This option does not prevent stack smashing, but rather detects it and halts the program.

    From the gcc man page:

    -fstack-protector

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

    You still have an overflow problem, but the addition of the guard variables is apparently masking the issue. If you run your program under valgrind it should be able to detect what's happening.