Setting
A sensor gateway is a small device that collects readings from sensors and passes them on. In this invented example the gateway also accepts a small decision program, delivered through a phone, from a publisher, the one party authorized to change the gateway’s rules. The gateway’s own software, the host, runs the decision program, the guest, inside a virtual machine §2.3 (VM): a part of the host that carries out the guest’s instructions one at a time and can refuse any of them.
Two stacks are involved. A stack is a region of memory that holds one frame for every function call still in progress. A frame holds that call’s local variables, the register values it must restore and the address to return to, and it is released when the call returns. The host’s code runs on a native stack, which the processor’s own call and return instructions use. It belongs to the host thread that runs the VM, a thread being an independently scheduled sequence of execution with a stack of its own. The VM gives the guest a separate guest stack §2.25 of 512 bytes for the guest’s local data and checks every guest access to it. Nothing checks the host’s own accesses while the host runs.
A developer reports two bugs in host code and calls both of them an overflow. In the first, a function copies 9 bytes into a local array §14.5 of 8 bytes. In the second, a function that handles one level of nesting in a received message calls itself for the next level. Each call’s frame costs 40 bytes, 160 bytes of native stack are free when the first call starts, and a message arrives nested 5 levels deep. The question is whether the two bugs are the same kind of bug.
Short answer
No. A buffer overflow §4.4 is an access outside the bounds of one object, such as writing index 8 of an array with indices 0 to 7; the object can live on a stack, on the heap or in static memory. A stack overflow §4.10, also called stack exhaustion, is running out of a stack: the frames of the calls in progress need more bytes in total than the stack holds, although every single access may stay inside its own object. The two have different causes and different defenses.
Follow-up questions
First answer
No. They are two different failures, and a defense against one does not defend against the other.
A buffer overflow is a read or write outside the bounds of one object; a buffer is a block of memory reserved for one piece of data. It is the error of a single access: the program computes a position past the end, or before the start, of the object it means to use. The object can be anywhere: a local array on a stack, a block on the heap (memory that the program reserves and releases on request while it runs), or a global variable. When the object sits on a stack, the bug is called a stack buffer overflow, and that phrase is the usual source of the confusion.
A stack overflow, also called stack exhaustion, is a failure of total use. A stack is a region of memory that holds one frame, the local variables and saved values of one call, for every function call still in progress. When calls nest too deeply or frames are too large, the next frame needs memory beyond the end of the region. Every function may access only its own frame, without a single out-of-bounds access, and still the frames together do not fit.
The damage can look alike: in both cases memory beyond a boundary is overwritten, which may hold another variable, a saved return address, another thread’s stack or heap data, unless a guard detects the access first. The causes differ, and so do the defenses: a buffer overflow is prevented by checking each access against its object’s bounds, a stack overflow by bounding the depth of calls and the size of frames and by guarding the end of the stack.
A system that runs a downloaded program, the guest, inside a virtual machine (VM) usually has two stacks with separate budgets: a guest stack that the VM provides and checks, and the native stack on which the host software, including the VM itself, runs. Either kind of overflow can happen on either stack, and each case needs its own argument.
Give separate examples
A buffer overflow without stack exhaustion. A function declares a local array name of 8 bytes, whose valid indices are 0 to 7. It copies into the array a text of 8 characters followed by the zero byte that marks the end of a text string in the C language, 9 bytes in all. The ninth byte goes to index 8, one byte past the end. The function’s frame is small and the stack has hundreds of free bytes, so no stack runs out. The extra byte overwrites whatever the compiler placed after the array: another local variable, a saved register or part of the saved return address. The same copy into an 8-byte block obtained from the heap is a heap buffer overflow, and no stack is involved at all.
Stack exhaustion without a buffer overflow. A function parse_level handles one level of nesting in a received message and calls itself for the next level. Suppose each call’s frame costs 40 bytes and 160 bytes of the stack are free when the first call starts. A message nested 5 levels deep needs bytes. The first 4 frames use bytes and fill the free space exactly, so the fifth frame lies entirely beyond the end of the stack, reaching bytes past it. Every access inside every call stays within its own variables; only the total is too large. The sender chose the nesting depth, so the sender decided whether the stack ran out.
The 40-byte frame is an invented figure. A real frame’s size is fixed by the compiled code. It includes the local variables; the saved registers; padding required by the native application binary interface §12.47 (ABI), the set of rules that all compiled code for a given processor follows; and space for the arguments of further calls.
Does a bounded guest stack solve both?
No. A bounded guest stack solves both problems for the downloaded program’s own storage and neither for the software that runs it. Call the downloaded program the guest and the device’s own software the host. A virtual machine (VM) inside the host gives the guest a stack region of fixed size, say 512 bytes, and checks every guest access to it, either before the run with a verifier, a checker that examines the whole program in advance, or at each access with a runtime bounds check §2.27. If those checks are complete and correct, two things follow.
A guest buffer overflow cannot leave the region. A guest that writes past the end of one of its own arrays can damage only its own other data in the 512 bytes, never host memory.
Guest stack exhaustion becomes a refusal. If the guest can call functions of its own, the VM places each call’s frame inside the region, or counts the depth of calls, and refuses a call that would not fit.
The host’s native stack lies outside all of this. The VM is itself host code, so it runs on the native stack. Every helper function §2.19 the guest calls, a host routine offered to the guest, adds its own native frame. A VM that carries out each guest call by calling itself recursively turns guest call depth into native stack depth. An interrupt §6.11, a hardware signal that makes the processor suspend the current code and run a handler, can arrive in the middle of a run. Depending on the processor and its configuration, the registers saved for the interrupt and the handler’s own frames go onto the interrupted stack, onto a separate interrupt stack, or partly onto each, and every stack involved needs its own bound. An Arm Cortex-M processor, for example, automatically saves 8 registers, 32 bytes, onto the stack that was in use when the interrupt arrived. Finally, a helper with a bug can overflow a buffer on the heap or on the native stack, and the guest stack’s bound says nothing about host objects.
A complete argument therefore has three separate parts: the VM’s checks for guest accesses, a bound or a guard for native stack depth, and the correctness of host code for host buffers.
What does a static stack bound require?
A static stack bound is a number of bytes, computed from the compiled program without running it, that one thread’s stack use can never exceed; deriving it requires a complete call graph, the frame size of every function as compiled, and limits on recursion and interrupt nesting. A call graph is a directed graph with one node per function and an edge from function to function whenever can call . Label each node with its function’s frame size. The bound for a thread is the largest sum of frame sizes along any path that starts at the thread’s entry function.
Example: main_loop (frame 32 bytes) calls either log_reading (64 bytes) or run_vm (96 bytes), and run_vm calls helper_copy (24 bytes). The paths cost bytes and bytes, so the thread needs at most 152 bytes. If an interrupt can arrive on this stack and costs 32 bytes of saved registers plus a 40-byte handler frame, add bytes, for bytes. A 256-byte stack then leaves bytes of margin.
The bound is sound, meaning never below the true worst case, only if all of the following hold.
Every call is an edge. This includes calls through function pointers, which a tool cannot always resolve and which must then be listed by hand, and calls that the compiler inserts itself, for example to library routines for division or for copying memory.
Frame sizes come from the shipped binary. Many compilers can report each function’s frame size for the code they produced, including saved registers and alignment §4.22 padding. The sizes of the local variables in the source omit these, and a change of compiler, options or target processor invalidates the numbers.
Recursion is bounded. A cycle in the graph means that a function can call itself, directly or through others. A finite bound then exists only with a proven limit on the depth. A recursive parser with 40-byte frames costs bytes only because the depth is at most 5; a sixth level adds 40 bytes, and an unlimited depth leaves no bound at all.
Stack allocation of run-time size is bounded, such as a local array whose length is computed while the program runs.
Every asynchronous entry is counted. Each interrupt handler §6.12 is a separate entry with its own paths. Add the worst handler path to the bound of every stack the interrupt can arrive on. When interrupts of different priorities can nest, add the worst path of each priority level, since one handler of each level can be active at once.
Nothing moves the stack pointer behind the analysis, such as hand-written assembly code.
Because a missed edge or an omitted frame makes the computed number too small, systems pair the bound with a guard that detects an error at run time: a protected region placed beyond the end of the stack, or a stack-limit register, which some processors provide, that raises a fault when the stack pointer passes a set address.
What if a stack canary catches the write?
Then a stack buffer overflow has already happened and has been detected, and only part of the damage is known. A stack canary is a value that compiler-inserted protection places in a function’s frame between the local arrays and the saved return address. Code added at the end of the function compares the canary with a reference copy just before returning and, on a mismatch, calls a failure routine instead of returning. The name recalls the canaries that miners carried underground as an early warning of gas.
Detection, not prevention. The write ran past the array and reached the canary, so everything between the two has already been overwritten.
The check comes late. The comparison runs at return. Between the overflow and the return, the function may already have used an overwritten local variable, such as a length or a function pointer, so harm can come before detection.
One shape is caught. A contiguous overrun that passes through the canary is detected. A write to a computed index that skips over the canary, an overrun that changes only other local variables, a heap buffer overflow and stack exhaustion are not detected by it. Stack exhaustion needs a separate guard at the end of the whole stack.
The value must be unpredictable. If an attacker knows the canary, because it is fixed in the firmware or has leaked, the overrun can write the correct value back over it.
The failure routine decides the outcome. The frame is corrupt, so the function cannot safely continue. On a microcontroller §6.1 the usual response is to record the fault and restart. That turns a possible takeover of the device into a restart, which is better, but an attacker who can trigger the overflow again can keep the device restarting, a denial of service §1.28.
A canary that fired is evidence that the detection worked in that run. It is not evidence that no other overflow happened, and the bug that overflowed still has to be fixed.
Related entries: VM stack (§2.25), Buffer overflow (§4.4), Stack overflow (§4.10).