Stack Canary Detection
Place a canary value between a buffer and return address. A simulated overflow corrupts the canary, and the check detects the corruption before the function returns.
A stack canary is a known value placed between a function's local buffer and its saved return address. Before the function returns, it checks whether the canary still holds the expected value. If a buffer overflow has occurred, the canary will have been overwritten (since it sits between the buffer and the return address). Detecting the changed canary lets the program abort before executing a corrupted return address. This is one of the most widely deployed exploit mitigations.
Simulator
Walkthrough Guide
- 1
Set up the canary and return address
Do this: Step through the setup: canary (value 7) at address 203, return address (value 9) at address 204
Watch for: Address 203 = 7 (canary), address 204 = 9 (return address)
The memory layout: buffer at 200-202, canary at 203, return address at 204. Any overflow that reaches the return address must first pass through the canary. The canary value (7) is known to the function so it can be verified later.
- 2
Overflow the buffer (4 writes into 3 slots)
Do this: Step through the fill loop
Watch for: Values 5, 6, 7, 8 written to addresses 200, 201, 202, 203. The canary at address 203 changes from 7 to 8
The loop writes 4 values but the buffer only has 3 slots. The 4th write lands on address 203 — the canary. The canary value changes from 7 (expected) to 8 (overwritten).
- 3
Check the canary
Do this: Step through LOAD R4, [R3] / MOV R5, #7 / CMP R4, R5 / JZ safe
Watch for: R4 = 8, R5 = 7. CMP shows they are not equal. JZ does not jump
The function loads the canary and compares it to the expected value. 8 != 7, so the canary check fails. The program takes the 'corruption detected' path instead of returning normally.
- 4
Corruption detected — abort
Do this: Step through MOV R6, #1 and HALT
Watch for: R6 = 1 (corruption flag set). The program aborted safely
The program sets R6 = 1 to indicate corruption was detected and halts without ever using the return address. The return address at address 204 survived this time, but a longer overflow would corrupt it too, and the canary would still catch the overflow first. The exploit was prevented.
Objectives
- •See canary value 7 placed at address 203
- •Watch the overflow write 4 values starting at address 200
- •See address 203 (canary) get overwritten by the 4th write (value 8)
- •Verify R6 = 1 - corruption detected, program aborted before using return address
Hints
- The canary at address 203 should be 7, but the overflow writes 8 there. The CMP catches this mismatch.