A stack-based buffer overflow occurs when a program writes more data to a buffer located on the call stack than it can hold, corrupting adjacent memory and potentially hijacking the program's execution flow.
This vulnerability happens when functions like `strcpy`, `gets`, or `scanf` are used without proper bounds checking. Since the buffer is allocated as a local variable within a function, the overflow overwrites other critical data on the stack, such as saved register values, function parameters, and most importantly, the return address. By carefully crafting the excess data, an attacker can redirect execution to malicious code they've injected or to existing code that benefits their attack, leading to a complete compromise. To prevent this, developers should always use secure, length-limited alternatives (like `strncpy` or `snprintf`) and perform explicit bounds checking before any write operation. Enabling compiler protections like stack canaries, address space layout randomization (ASLR), and non-executable stacks can also mitigate exploitation, but the primary defense is writing safe, validated code that never trusts unchecked input.
Impact: Modify MemoryDoS: Crash, Exit, or RestartDoS: Resource Consumption (CPU)DoS: Resource Consumption (Memory)
Buffer overflows generally lead to crashes. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop.
Impact: Modify MemoryExecute Unauthorized Code or CommandsBypass Protection Mechanism
Buffer overflows often can be used to execute arbitrary code, which is usually outside the scope of a program's implicit security policy.
Impact: Modify MemoryExecute Unauthorized Code or CommandsBypass Protection MechanismOther
When the consequence is arbitrary code execution, this can often be used to subvert any other security service.
Strategy: Environment Hardening
Effectiveness: Defense in Depth
Strategy: Environment Hardening
Effectiveness: Defense in Depth
cc
/*routine that ensures user_supplied_addr is in the right format for conversion /
cHigh