GCC stack-protector: choosing -fstack-protector, -fstack-protector-strong, or -fstack-protector-all
Pick the right GCC stack canary flag: compare -fstack-protector, -fstack-protector-strong, and -fstack-protector-all with verification steps.
11 Nov 2025, 10:50 UTC

Decision and constraints
You are compiling C or C++ code with GCC and want stack canaries to terminate a program when a stack buffer overflow corrupts the saved return address. GCC offers three related flags, and the choice affects which functions get instrumented, how large the binary grows, and whether the build links at all.
Useful takeaway: for most general-purpose Linux builds, start with -fstack-protector-strong. Reserve -fstack-protector-all for cases where you have measured and accepted the size and speed cost, and avoid -fstack-protector unless you specifically need its narrow coverage. Always confirm your installed GCC’s default before adding flags.
Assumptions: GCC 4.9 or later, which introduced -fstack-protector-strong, and a target C library that provides __stack_chk_fail.
Compare the supported options
| Flag | Functions instrumented | Typical cost | When to consider it |
|---|---|---|---|
-fstack-protector | Only functions with local character arrays | Lowest overhead and code-size impact | Legacy or size-constrained builds where narrow coverage is acceptable |
-fstack-protector-strong | Functions with local character arrays, address-taken locals, arrays, and register variables | Moderate; broader coverage than the base flag | General-purpose applications and libraries; often the distribution default |
-fstack-protector-all | Every non-inline function | Highest binary size and instruction overhead | High-security builds where maximum coverage justifies measured cost |
A stack canary is a value placed between local variables and the saved return address. Before the function returns, GCC checks the canary; if it changed, the program calls __stack_chk_fail and terminates. The canary is typically read from thread-local storage or a dedicated register.
Trade-offs and compatibility
- Coverage versus cost.
-fstack-protector-allinstruments leaf functions and simple accessors that cannot realistically overflow a local buffer, adding code size and branch instructions for little benefit. - Runtime dependency. Instrumented code references
__stack_chk_fail. Minimal or embedded C libraries may not provide it, producing link errors unless you disable protection with-fno-stack-protector. - Inconsistent flags. Compiling translation units with different protector levels can create duplicate or missing
__stack_chk_failreferences. Use the same flag across all objects in a program or library. - Per-function escape hatch. Where the target supports it,
__attribute__((no_stack_protector))excludes an individual function from instrumentation.
Check your GCC default first
Run these from a normal user shell; no root is required. Replace gcc with your cross-compiler prefix if you are targeting embedded hardware.
gcc -Q -fstack-protector-strong --help=target
Expected check: the option is accepted and reported as active. If GCC rejects it, your toolchain predates GCC 4.9 or the flag is unsupported for that target.
gcc -dumpspecs | grep stack_protector
Expected check: distribution specs often enable -fstack-protector-strong by default. If the output is empty, protection is likely off unless you add it explicitly.
Risk: adding a flag that is already the default is harmless, but adding -fstack-protector-all without measuring can bloat performance-sensitive binaries.
Concrete implementation: compile and verify
Create a test file canary_check.c:
#include <stdio.h>
#include <string.h>
void vulnerable(const char *input) {
char buffer[64];
strcpy(buffer, input); /* intentional overflow target for testing only */
printf("ok\n");
}
int main(void) {
vulnerable("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
return 0;
}
Compile three objects from the same source:
gcc -c -O2 -fstack-protector canary_check.c -o base.o gcc -c -O2 -fstack-protector-strong canary_check.c -o strong.o gcc -c -O2 -fstack-protector-all canary_check.c -o all.o
Inspect each object with nm. Look for __stack_chk_fail references:
nm base.o | grep __stack_chk_fail nm strong.o | grep __stack_chk_fail nm all.o | grep __stack_chk_fail
Expected check: a reference such as U __stack_chk_fail indicates the object calls the canary failure routine; absence means that object was not instrumented to call it. Compare the three objects to see how coverage expands from base to strong to all.
Link and run a runtime check only in a disposable environment:
gcc -O2 -fstack-protector-strong canary_check.c -o canary_test ./canary_test
Expected check: if the overflow overwrites the canary, the process terminates under __stack_chk_fail rather than returning normally. Do not run this against production inputs; it is deliberately corrupting memory.
Limitations
- Stack canaries mitigate stack-buffer overflows; they do not prevent heap corruption, use-after-free, or information leaks.
-fstack-protector-allincreases binary size and instruction count; measure before applying it to performance-sensitive or size-constrained code.- Bare-metal or minimal C library targets may lack
__stack_chk_fail, causing link failures.
Rollback
Because these flags change compiled binaries, rollback means recompiling without the flag or explicitly disabling it:
gcc -O2 -fno-stack-protector canary_check.c -o canary_test
Then redeploy the previously built artifacts or rebuild the affected packages with the prior flag set.
Practical verification checklist
- Confirm GCC version and target support for
-fstack-protector-strong. - Check
gcc -dumpspecsto see whether your distribution already enables protection. - Compile with the chosen flag and use
nmto confirm__stack_chk_failreferences appear where expected. - Link against your target C library and resolve any missing
__stack_chk_failerrors before deployment. - Measure binary size and, if performance matters, benchmark representative workloads before choosing
-fstack-protector-all.
Treat runtime overflow results as indicative, not proof of complete hardening; verify on your actual toolchain and target libc.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.