A stack canary is a security mechanism used in computer programming to detect buffer overflows. It involves placing a small, known value, called a "canary," just before the stack return pointer. If a buffer overflow occurs, this value is altered, indicating a potential attack.
When a function returns, the program checks the canary value. If it has changed, the program can take protective actions, such as terminating the process. This helps prevent unauthorized access and maintains the integrity of the program, making it harder for attackers to exploit vulnerabilities in the stack, such as those related to buffer overflows.