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.
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.
#include <stdio.h>
#include <string.h>
void copy(const char *s)
{
char buf[16];
strcpy(buf, s);
puts(buf);
}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_fail1
0The 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.