The Makefile
This Makefile produces three assembly files from the c function, one without stack protection, one compiled with -fstack-protector
and one compiled with -fstack-protector-strong
:
all: no-protection.S stack-protector.S stack-protector-strong.S
no-protection.S: func.c
gcc -S -masm=intel $< -o $@
stack-protector.S: func.c
gcc -S -masm=intel $< -o $@ -fstack-protector
stack-protector-strong.S: func.c
gcc -S -masm=intel $< -o $@ -fstack-protector-strong
Produced difference
When compiled with -fstack-protector
or -fstack-protector-strong
, the compiler inserted the following code before the call to g
:
sub rsp, 32
mov rax, QWORD PTR fs:40
mov QWORD PTR -8[rbp], rax
xor eax, eax
lea rax, -18[rbp]
and this code after the call:
mov rax, QWORD PTR -8[rbp]
sub rax, QWORD PTR fs:40
je .L2
call __stack_chk_fail@PLT
L2:
I was unable to detect a difference between compiling with -fstack-protector
and -fstack-protector-strong
.