Meaning
Data verification value placed at the end of a memory buffer to identify unauthorized overwrites. Software developers use a stack canary pattern as a defensive mechanism to detect buffer overflows before they cause a system crash. The value is usually a unique or random number that is unlikely to appear by chance in normal data.
If a function writes beyond its allocated stack space, it will overwrite this value, signaling that a memory violation has occurred.
Detection Logic
Functional routine within the operating system that verifies the integrity of the guard value. The system checks the stack canary pattern at specific intervals, such as during a context switch or before a function returns. If the value in memory does not match the expected constant, the detection logic triggers an error handler.
This check provides a reliable way to catch silent data corruption that might otherwise lead to unpredictable behavior in the application.
Failure Logic
Consequence of detecting a modified guard value within the execution stack. When the stack canary pattern is found to be incorrect, the system usually enters a safe state by halting the current thread or resetting the microcontroller. This immediate response prevents the corrupted data from being used in critical calculations.
It also stops an attacker from using a stack overflow to redirect the program flow to a malicious instruction.
Implementation Burden
Technical trade-off between increased security and the consumption of processor resources. Using a stack canary pattern requires extra memory for the guard values and extra clock cycles to perform the checks. While the overhead for a single check is small, the cumulative effect can be significant in a system with hundreds of active functions.
An architect must decide which parts of the code require this protection based on the level of risk and the available performance margin. Some compilers automate this process by inserting the canary and the check code into every function that uses a local buffer. This automation reduces the chance of human error but increases the size of the final binary.
For highly constrained systems, the pattern might only be placed on the most vulnerable stacks or checked only during the development and testing phases.