Stack Frames, Prologues and Canaries
Function boundaries are the skeleton of a disassembly: once you know where each function starts and ends and how much stack it uses, the rest is filling in. Compilers mark both ends with recognisable sequences, the prologue and the epilogue, and between them every local variable is an offset from rbp or rsp. The same layout is what memory-corruption bugs damage: a buffer, then saved registers, then the return address. Reading frames is therefore both a navigation skill and the starting point of every memory-safety analysis. The Stack and Procedures: call & ret in the assembly course build frames by hand.
Definitions
call fpushes the address of the next instruction (the return address) and jumps tof.retpops it intorip.- Prologue:
push rbp; mov rbp, rsp; sub rsp, N. It saves the caller's frame pointer, makesrbppoint at the saved value, and reservesNbytes for locals. - Epilogue:
leave(=mov rsp, rbp; pop rbp) thenret. - Frame layout with a frame pointer, from high to low addresses: arguments passed on the stack, the return address at
[rbp+8], the savedrbpat[rbp], then locals at[rbp-8],[rbp-0x10], ... - Stack canary (
-fstack-protector-strong, on by default in Ubuntu's GCC): a random value copied fromfs:0x28(the thread control block) into the frame just below the savedrbp, checked before returning. A mismatch calls__stack_chk_fail, which aborts. - Frame-pointer omission (
-fomit-frame-pointer, on at-O1and above on x86-64): norbpframe; locals are addressed fromrsp, and debuggers unwind with the.eh_frametables instead.
A frame in the disassembly
The session compiles a toy greet with a 16-byte buffer at -O0 (frame pointer on), with and without the canary, and prints both functions.
cat > frame.c <<'SRC'
#include <stdio.h>
#include <string.h>
void greet(const char *name) {
char buf[16];
strncpy(buf, name, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';
printf("hi %s\n", buf);
}
int main(void) { greet("analyst"); return 0; }
SRC
gcc -O0 -g frame.c -o frame
gcc -O0 -fno-stack-protector frame.c -o frame-nocanary
dis() { objdump -d -M intel --no-show-raw-insn --disassemble=greet "$1" | grep -E '^\s+[0-9a-f]+:' | sed 's/^ *//'; }
echo "== with canary (Ubuntu default)"; dis frame
echo "== -fno-stack-protector"; dis frame-nocanary== with canary (Ubuntu default)
1189: endbr64
118d: push rbp
118e: mov rbp,rsp
1191: sub rsp,0x30
1195: mov QWORD PTR [rbp-0x28],rdi
1199: mov rax,QWORD PTR fs:0x28
11a2: mov QWORD PTR [rbp-0x8],rax
11a6: xor eax,eax
11a8: mov rcx,QWORD PTR [rbp-0x28]
11ac: lea rax,[rbp-0x20]
11b0: mov edx,0xf
11b5: mov rsi,rcx
11b8: mov rdi,rax
11bb: call 1070 <strncpy@plt>
11c0: mov BYTE PTR [rbp-0x11],0x0
11c4: lea rax,[rbp-0x20]
11c8: mov rsi,rax
11cb: lea rax,[rip+0xe32] # 2004 <_IO_stdin_used+0x4>
11d2: mov rdi,rax
11d5: mov eax,0x0
11da: call 1090 <printf@plt>
11df: nop
11e0: mov rax,QWORD PTR [rbp-0x8]
11e4: sub rax,QWORD PTR fs:0x28
11ed: je 11f4 <greet+0x6b>
11ef: call 1080 <__stack_chk_fail@plt>
11f4: leave
11f5: ret
== -fno-stack-protector
1169: endbr64
116d: push rbp
116e: mov rbp,rsp
1171: sub rsp,0x20
1175: mov QWORD PTR [rbp-0x18],rdi
1179: mov rcx,QWORD PTR [rbp-0x18]
117d: lea rax,[rbp-0x10]
1181: mov edx,0xf
1186: mov rsi,rcx
1189: mov rdi,rax
118c: call 1060 <strncpy@plt>
1191: mov BYTE PTR [rbp-0x1],0x0
1195: lea rax,[rbp-0x10]
1199: mov rsi,rax
119c: lea rax,[rip+0xe61] # 2004 <_IO_stdin_used+0x4>
11a3: mov rdi,rax
11a6: mov eax,0x0
11ab: call 1070 <printf@plt>
11b0: nop
11b1: leave
11b2: ret
The prologue and epilogue frame both versions. The canary version adds exactly two pieces and moves the buffer down by 16 bytes (from rbp-0x10 to rbp-0x20) to make room: after the prologue it copies fs:0x28 into [rbp-0x8], and before leave it subtracts the saved copy from the live value and calls __stack_chk_fail unless the result is zero. Arguments are spilled to the frame at -O0 (mov QWORD PTR [rbp-0x28], rdi): the debug-friendly code keeps every variable in memory.
The same frame in a debugger
gdb in batch mode stops at greet (after its prologue) and measures the frame instead of trusting the picture. It prints the distance from buf to the saved rbp and asks which function the return address points into.
cat > frame.c <<'SRC'
#include <stdio.h>
#include <string.h>
void greet(const char *name) {
char buf[16];
strncpy(buf, name, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';
printf("hi %s\n", buf);
}
int main(void) { greet("analyst"); return 0; }
SRC
gcc -O0 -g frame.c -o frame
gdb -q -batch -ex 'break greet' -ex 'run' \
-ex 'print (long)$rbp - (long)&buf' \
-ex 'print (long)$rbp - (long)$rsp' \
-ex 'info symbol *(long *)($rbp + 8)' \
-ex 'bt' ./frame 2>&1 | grep -vE '^\[|libthread|^