This vulnerability occurs when a program uses the size of the source data buffer to control reading or writing to a smaller destination buffer, potentially accessing memory outside the destination's allocated bounds.
This flaw typically happens during copy or manipulation operations like `memcpy`, `strncpy`, or custom loops. The developer incorrectly uses the length or size of the *source* buffer (which might be large or untrusted) as the limit for the operation, instead of using the smaller, safe capacity of the *destination* buffer. This mismatch sets the stage for a buffer overflow. When executed, this can corrupt adjacent memory, crash the application, or allow an attacker to inject and run malicious code. To prevent it, always explicitly validate that the destination buffer is large enough for the operation, and use its specific size limit—not the source's size—as the guard for any data transfer.
Impact: Modify MemoryDoS: Crash, Exit, or RestartDoS: Resource Consumption (CPU)
Buffer overflows generally lead to crashes. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop.
Impact: Read MemoryModify MemoryExecute Unauthorized Code or Commands
Buffer overflows often can be used to execute arbitrary code, which is usually outside the scope of a program's implicit security policy.
Impact: Bypass Protection Mechanism
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
Strategy: Environment Hardening
Effectiveness: Defense in Depth
ccc
// saves the file name to a log file* int outputFilenameToLog(char *filename, int length) { ``` int success;
cc
// copy filename to buffer* strncpy(buf, filename, sizeof(buf)-1); ...