TExcelTAD
Home › Reverse Engineering & Binary Analysis › Stack Frames, Prologues and Canaries

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 f pushes the address of the next instruction (the return address) and jumps to f. ret pops it into rip.
  • Prologue: push rbp; mov rbp, rsp; sub rsp, N. It saves the caller's frame pointer, makes rbp point at the saved value, and reserves N bytes for locals.
  • Epilogue: leave (= mov rsp, rbp; pop rbp) then ret.
  • Frame layout with a frame pointer, from high to low addresses: arguments passed on the stack, the return address at [rbp+8], the saved rbp at [rbp], then locals at [rbp-8], [rbp-0x10], ...
  • Stack canary (-fstack-protector-strong, on by default in Ubuntu's GCC): a random value copied from fs:0x28 (the thread control block) into the frame just below the saved rbp, checked before returning. A mismatch calls __stack_chk_fail, which aborts.
  • Frame-pointer omission (-fomit-frame-pointer, on at -O1 and above on x86-64): no rbp frame; locals are addressed from rsp, and debuggers unwind with the .eh_frame tables instead.
caller's frame return address saved rbp canary char buf[16] (+ padding) rbp+8rbprbp-8rbp-0x20rsp a write pastbuf goes up higher addresses at the top; the stack grows down

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.

bash · try it yourself
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.

bash · try it yourself
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|^
  

Breakpoint 1 at 0x1199: file frame.c, line 3.
Breakpoint 1, greet (name=0x5e049824e00b "analyst") at frame.c:3
3	void greet(const char *name) {
$1 = 32
$2 = 48
main + 23 in section .text of /home/learner/frame
#0  greet (name=0x5e049824e00b "analyst") at frame.c:3
#1  0x00005e049824d20d in main () at frame.c:9

buf starts 32 bytes (0x20) below rbp, matching lea rax, [rbp-0x20] in the listing, and the whole frame below rbp is 48 bytes, the sub rsp, 0x30. The canary at rbp-8, the saved rbp and the return address at rbp+8 all sit above the buffer. The return address points into main (main + 23), just after its call greet; bt shows the same address as frame #1. The breakpoint itself landed at 0x1199, the canary load, because gdb places a breakpoint on a function after the prologue. This is the layout the Memory Safety course visualises when a write runs past buf.

The stack-frame lab draws the -fno-stack-protector frame with the offsets a disassembler shows: buf at rbp-0x10, the saved rbp at rbp+0x00 and the return address at rbp+0x08. Writing 32 bytes into the 16-byte buffer overwrites both saved values, and the first byte of the return address changes at the 25th byte. Switch the canary on and the lab puts it right above the buffer, at rbp-0x08, with buf moved down to rbp-0x18; gcc's real build moved it to rbp-0x20, because it also keeps 8 bytes of padding.

offsetbytes, address →contents (rows grow ↑)rbp+0x60rbp+0x58rbp+0x50rbp+0x48rbp+0x40rbp+0x38rbp+0x30rbp+0x28rbp+0x20rbp+0x18rbp+0x10caller's frame (main)rbp+0x08AAAAAAAAret → 0x4141414141414141rbp+0x00AAAAAAAAsaved rbp: 8 bytes = 0x41rbp−0x08AAAAAAAArbp−0x10AAAAAAAAchar buf[16]void greet(int fd) {char buf[16];read(fd, buf, 32);}✓ copy input✓ no canary check✓ leave (pop rbp)✓ retnot caughtret pops a returnaddress made of 0x41bytes: control flow ishijacked (an attackerpicks that value)abstract model: placeholder values, only 'A' (0x41) bytes are written

How frame damage is detected

  • The canary check turns most linear stack overflows into a clean abort (*** stack smashing detected ***) instead of a hijacked return.
  • AddressSanitizer in test builds reports the exact overflowing write with the variable's name and frame offset (see C Buffers).
  • For an analyst: a function whose prologue reserves a buffer and then passes its address together with a length taken from input to memcpy, read or recv is the first place to look for an overflow. A function without the canary pair while others have it is worth noting too (-fstack-protector-strong only protects functions with arrays or address-taken locals).

How it is prevented

  • Bounded copies with correct lengths (snprintf, strncpy plus explicit termination as above, or a safer string API).
  • Keep the defaults: canaries (-fstack-protector-strong), NX stacks, PIE and ASLR (Memory Protection); on new hardware, shadow stacks (Intel CET, -fcf-protection) protect the return address itself.
  • Audit shipped binaries with checksec in CI.

Pitfalls

  • Off-by-one offsets: the return address is at [rbp+8], not [rbp]; the first stack argument (System V) is at [rbp+0x10].
  • No rbp frame at -O2: locals are [rsp+n] and n changes every time the function pushes or reserves; track rsp line by line.
  • The canary isn't in every function: only those with arrays or address-taken locals under -strong.
  • endbr64 at the top of a function is not part of the prologue's logic.

Practice

1. A function starts with push rbp; mov rbp, rsp; sub rsp, 0x40. How many bytes of locals, and where is the return address relative to rbp?

0x40 = 64 bytes of locals (some may be padding for alignment); the return address is at [rbp+8].

2. Which two instructions show that a function has a stack canary?

A load from fs:0x28 stored into the frame after the prologue, and a compare (or sub) with fs:0x28 followed by a call to __stack_chk_fail before the epilogue.

3. Why does bt still work for an -O2 binary without frame pointers?

gdb unwinds with the call-frame information in .eh_frame, which the compiler emits for every function.

Test yourself

With a frame pointer, where is the return address?

  1. [rbp]
  2. [rbp+8]
  3. [rbp-8]
  4. [rsp-8]

Where does a stack canary's reference value come from on x86-64 Linux?

  1. A constant in .rodata
  2. fs:0x28, the thread control block
  3. The return address
  4. The environment

What does leave do?

  1. Returns from the function
  2. mov rsp, rbp then pop rbp
  3. Calls __stack_chk_fail
  4. Clears the canary

In the gdb session, $rbp - &buf was 0x20 (32). How far is the return address above the start of buf?

  1. 8 bytes
  2. 32 bytes
  3. 40 bytes
  4. 16 bytes

In the lab's frame without a canary, buf[16] is at rbp-0x10. How many bytes must a write into buf reach before the first byte of the return address changes?

  1. 17
  2. 24
  3. 25
  4. 41