Algorithm Search⌘K

Techniqueintro

Stack canary

Also called stack cookie, StackGuard.

Places a secret value before the return address and checks it before the function returns, to detect a linear stack overflow.

A stack canary is a secret value that the compiler places in each protected stack frame, between the local arrays and the saved return address, and checks just before the function returns. A buffer overflow that writes past the end of a local array toward the return address must write over the canary first. Unless the overflow writes the exact secret value back, the check fails and the program stops before the changed return address is used. This is the defense against stack smashing that Cowan et al. built into StackGuard in 1998, and every major C compiler now has it. The guarantee is narrow and exact: against an attacker who cannot read memory and who overflows a buffer contiguously, a uniformly random canary of bb bits lets one attempt pass with probability 2−b2^{-b}. It does not stop writes that skip the canary, it does not survive a leak of the canary, and it does nothing for data that the overflow changes before the return. A shadow stack checks the return address itself and needs no secret.

In plain words

Coal miners took a canary into the mine. If the air went bad, the bird died first, and the miners got out before it reached them.

A stack canary works the same way. The compiler puts a secret number in each function's block of stack memory, between the local arrays and the note that says where to return. An overflow of an array runs upward through memory. It hits the secret number before it can reach the note.

Just before the function returns, the program checks that the secret number is still there. If it changed, something overflowed. The program stops at once, instead of jumping to wherever the changed note points.

The trick works only while the number is secret, and only for overflows that run through it in order.

When it applies

  • The threat is a contiguous overflow of a stack buffer toward the saved return address: a copy, a string operation or a loop that writes past the end of a local array, one cell after the next.
  • The attacker cannot read the canary. There is no information leak that reveals the stack, and no way to learn the value from crashes, for example from a server that forks children with the same canary.
  • The damage happens at the return. The overflow is used through the return address or through data the epilogue protects, not through a pointer or a variable that the function uses before it returns.
  • Source or compiler control. The canary is inserted by the compiler, so the code must be rebuilt. A binary without the instrumentation gets no protection.

When the threat includes arbitrary writes, use a shadow stack or bounds checking (AddressSanitizer, SoftBound). When it includes leaks, a canary alone is not enough.

The template

The defense has three parts: a value chosen once per process, a store in the prologue of each protected function, and a check in its epilogue. The named holes are the choices that implementations make.

  1. INIT() ▷ once, at process start
  2. 1C ← ⟨canary source⟩, for example a random word from the operating system
  3. 2keep C in ⟨protected storage⟩ that the program's buffers cannot overflow into
  4. PROLOGUE(F) ▷ at entry, for each F chosen by ⟨function selection⟩
  5. 3arrange the frame by ⟨layout rule⟩: arrays next to the canary, scalars and pointers below the arrays
  6. 4write ⟨canary value⟩ (C, or C xor the return address) into the canary slot between the arrays and the saved return address
  7. EPILOGUE(F) ▷ before each return of F
  8. 5v ← the value in the canary slot
  9. 6if v ≠ ⟨canary value⟩ then ⟨failure action⟩: report and end the process, without returning
  10. 7return through the saved return address

Walk-through. Line L1 draws the secret. Line L2 keeps the reference copy away from every buffer, in a global or thread-local word, so that an overflow cannot change both copies. Line L3 is the layout rule that ProPolice added: if arrays sit directly below the canary and scalar locals and copies of pointer arguments sit below the arrays, then an overflow of an array cannot reach a scalar or a pointer before it reaches the canary. Line L4 writes the canary into the frame. Lines L5 and L6 run before the return uses the saved address. The failure action must not return and must not trust anything in the damaged frame; it reports and ends the process.

The guarantee, as a statement in a model. Model the frame as a sequence of cells, with the canary cell between the last array cell and the saved return address. Let the attacker write a contiguous run of cells that starts inside an array of the frame and goes toward higher addresses, choosing every value without knowledge of CC. If CC is uniform on 2b2^b values and independent of the attacker's choices, then any run that reaches the return address also writes the canary cell, with some value vv chosen independently of CC, and Pr⁡[v=C]=2−b\Pr[v = C] = 2^{-b}. So the probability that one such overflow changes the return address and passes line L6 is at most 2−b2^{-b}. Each assumption matters: a non-contiguous write need not touch the canary cell, and an attacker who knows CC can write it back.

Cost. Each protected call does one load and one store in the prologue and one load, one compare and one branch in the epilogue: O(1)O(1) extra operations per call in the word RAM model, and one word of stack per frame. The real overhead is a measurement and depends on how many functions the selection rule protects; Dang, Maniatis and Wagner (2015) compare it with the cost of shadow stacks.

Classic examples

  • StackGuard (1998). Cowan et al. put a canary word next to the return address in GCC-compiled code. They offered a terminator canary, made of bytes that end strings and lines (zero, carriage return, line feed and end of file), so that a string copy cannot write it back, and a random canary chosen at program start. It is the first general defense against stack smashing.
  • ProPolice and the GCC stack protector. Etoh's ProPolice added the layout rule of line L3. GCC adopted it as -fstack-protector, with variants that protect only functions with character arrays, a wider set of functions, or all functions. Some C libraries set one byte of the random canary to zero, so the canary is both random and a terminator; then a ww-bit canary has w−8w - 8 random bits.
  • Microsoft /GS. The Visual C++ compiler inserts a security cookie, combined with the frame address, and also orders locals. On platforms with exception handler records on the stack, an overflow could change a handler and raise an exception before the epilogue runs, which led to separate handler validation; see the variations of stack smashing.
  • The comparison with shadow stacks. A shadow stack checks the return address by value, with no secret, and catches non-contiguous writes too. Dang, Maniatis and Wagner (2015) measured why compilers kept the cheaper canary as the default for so long.

Variations

  • XOR canary. Store C⊕aC \oplus a where aa is the return address, and check it against the current return address in the epilogue. A write that changes aa but skips the canary slot now fails the check too, because the stored value no longer matches. An attacker who knows CC can still compute the right value.
  • Per-frame or per-call canaries. Derive the canary from CC and the frame, so that a leaked canary of one frame does not give the canary of another. The cost is a few more operations per call.
  • Renewing the canary in child processes. A server that forks children with the same canary lets an attacker learn about the value from which requests crash a child. Choosing a fresh canary in each child removes that channel; then the probability bound of the template holds for each attempt again.
  • Heap canaries and guard values. Allocators put check values in chunk headers to detect heap overflows on free. The idea is the same; the check point is the allocator, not the return.
  • Memory tagging. Memory tagging gives each allocation a small tag and checks it on every access, which catches the overflow at the first wrong cell rather than at the return. With 4-bit random tags, a mismatch is caught with probability 15/1615/16.

Pitfalls

  • A canary that is not secret. A fixed or predictable value, a value printed by a format string error, or a value read through any memory leak makes the check pass for the attacker. The bound 2−b2^{-b} needs a uniform value that the attacker cannot see.
  • Non-contiguous writes. An out-of-bounds index, a write through a corrupted pointer, or a format string write can change the return address without touching the canary slot. A plain canary does not see it.
  • Damage before the return. The check runs only in the epilogue. If the overflow changes a local function pointer, a pointer argument, or a decision variable that the function uses before it returns, the harm is done first. The layout rule of line L3 reduces this, but it cannot reorder fields inside a structure.
  • Functions left out. A selection rule that protects only functions with character arrays leaves functions with other arrays or with arrays inside structures unprotected.
  • A failure action that trusts the frame. The handler must not return, unwind with the damaged frame, or call code reached through data on the damaged stack.
  • Too few random bits. On a 32-bit system with a zero byte, a canary has 24 random bits, and one blind attempt passes with probability 2−24≈5.96×10−82^{-24} \approx 5.96 \times 10^{-8}. That is small per attempt, but a process that is restarted without a fresh canary gives the attacker many attempts.

Exercises

  1. In the cell model of the template, the frame from low to high addresses is: an array of 8 cells, the canary, a saved frame pointer, the return address. List the write runs [i,j][i, j] (start and end cell, with 0≤i≤70 \le i \le 7) that change the return address. Show that each of them also writes the canary cell.
  2. Prove the bound of the template: if CC is uniform on {0,…,2b−1}\{0, \ldots, 2^b - 1\} and the value vv written into the canary cell is independent of CC, then Pr⁡[v=C]=2−b\Pr[v = C] = 2^{-b}, whatever the distribution of vv.
  3. For an XOR canary C⊕aC \oplus a, show that a write that changes the return address from aa to a′≠aa' \ne a and leaves the canary slot unchanged always fails the check. What does an attacker who knows CC need to write?
  4. A canary has 24 random bits. If every child of a server has the same canary, a blind attacker who never repeats a whole guess needs (224+1)/2(2^{24} + 1)/2 attempts on average. If every child gets a fresh uniform canary, each attempt passes with probability 2−242^{-24} independently; give the expected number of attempts with the geometric distribution. Explain why renewal gains only a factor of about 2 against blind whole guesses, and why it gains much more against an attacker who learns the canary in parts from which attempts crash.

References

  1. Crispin Cowan, Calton Pu, Dave Maier, Heather Hinton, Jonathan Walpole, Peat Bakke, Steve Beattie, Aaron Grier, Perry Wagle and Qian Zhang, StackGuard, automatic adaptive detection and prevention of buffer-overflow attacks, 7th USENIX Security Symposium, 1998, pages 63-78, the canary word next to the return address, the terminator and random canaries, and the measured cost
  2. Laszlo Szekeres, Mathias Payer, Tao Wei and Dawn Song, SoK, eternal war in memory, 2013 IEEE Symposium on Security and Privacy, pages 48-62, the section on stack cookies among the defenses against control-flow hijacking, and what they leave open
  3. Thurston H. Y. Dang, Petros Maniatis and David Wagner, The performance cost of shadow stacks and stack canaries, ASIA CCS 2015, pages 555-566, the comparison of canaries with shadow stacks in cost and in the attacks each one stops

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises