Skip to content
systemcoding.dev
Security

Enable strong stack canaries with -fstack-protector-strong

Compile with GCC 14 and -fstack-protector-strong to put canaries in every function with a local array or address-taken local, and confirm it.

1 min read
Summarize in AI

This is the canary flag most distributions pass by default, and it is worth checking that your own build really has it. -fstack-protector-strong puts a random value between the locals and the return address, and every protected function compares it before returning. Plain -fstack-protector only covers functions with character arrays of 8 bytes or more. The strong variant also covers any function with a local array or a local whose address is taken, which is where most overflows start.

vuln.cc
#include <stdio.h>
#include <string.h>

void copy(const char *s)
{
    char buf[16];
    strcpy(buf, s);
    puts(buf);
}
Shell
gcc -O2 -S -fstack-protector-strong vuln.c -o - | grep -c stack_chk_fail
gcc -O2 -S -fno-stack-protector vuln.c -o - | grep -c stack_chk_fail
Text
1
0

The count is the number of __stack_chk_fail calls in the generated assembly, which is the function the epilogue jumps to when the canary no longer matches. The check costs a load from thread-local storage and a compare per call, and it does not stop an overflow that overwrites other locals or leaks the canary first. See the GCC instrumentation options for -fstack-protector-all when you want every function covered.

Also inToolchains
Taggedcgcc
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.