A buffer underwrite, also known as buffer underflow, happens when a program writes data to a memory location before the official start of a buffer.
This vulnerability occurs when a program miscalculates a write position, placing it outside the allocated buffer's bounds in the 'backward' direction. Common causes include decrementing a pointer or index too many times, performing incorrect pointer arithmetic that results in a negative offset, or directly using a negative index to access an array. The result is a write operation to an unintended, and often critical, memory location. Writing to memory before a buffer can corrupt other program data, function pointers, or critical control structures. This can lead to system crashes, unpredictable behavior, or, if exploited, allow an attacker to hijack the program's execution flow. Developers should rigorously validate all pointer arithmetic and array indices, ensuring they remain within the valid, zero-based range of the allocated buffer.
Impact: Modify MemoryDoS: Crash, Exit, or Restart
Out of bounds memory access will very likely result in the corruption of relevant memory, and perhaps instructions, possibly leading to a crash.
Impact: Execute Unauthorized Code or CommandsModify MemoryBypass Protection MechanismOther
If the corrupted memory can be effectively controlled, it may be possible to execute arbitrary code. If the corrupted memory is data rather than instructions, the system will continue to function with improper changes, possibly in violation of an implicit or explicit policy. The consequences would only be limited by how the affected data is used, such as an adjacent memory location that is used to specify whether the user has special privileges.
Impact: Bypass Protection MechanismOther
When the consequence is arbitrary code execution, this can often be used to subvert any other security service.
c
// copy input string to a temporary string* char message[length+1]; int index; for (index = 0; index < length; index++) { ``` message[index] = strMessage[index]; } message[index] = '\0';
c
// return string without trailing whitespace* retMessage = message; return retMessage;}
cMedium