Debug School

Cover image for Prevent Out-of-bounds Write
Suyash Sambhare
Suyash Sambhare

Posted on

Prevent Out-of-bounds Write

Description

The product writes data past the end, or before the beginning, of the intended buffer.
Typically, this can result in corruption of data, a crash, or code execution. The product may modify an index or perform pointer arithmetic that references a memory location that is outside of the boundaries of the buffer. A subsequent write operation then produces undefined or unexpected results.
Memory Corruption: Often used to describe the consequences of writing to memory outside the bounds of a buffer, or to memory that is invalid, when the root cause is something other than a sequential copy of excessive data from a fixed starting location. This may include issues such as incorrect pointer arithmetic, accessing invalid pointers due to incomplete initialization or memory release, etc.

Example

This code attempts to save four different identification numbers into an array.

int id_sequence[3];  

/* Populate the id array. */  

id_sequence[0] = 123;  
id_sequence[1] = 234;  
id_sequence[2] = 345;  
id_sequence[3] = 456;
Enter fullscreen mode Exit fullscreen mode

Since the array is only allocated to hold three elements, the valid indices are 0 to 2; so, the assignment to id_sequence[3] is out of bounds.

Example

Here it is possible to request that memcpy move a much larger segment of memory than assumed:

int returnChunkSize(void *) {

/* if chunk info is valid, return the size of usable memory,  
else, return -1 to indicate an error */
...

}  
int main() {

...  
memcpy(destBuf, srcBuf, (returnChunkSize(destBuf)-1));  
...

}
Enter fullscreen mode Exit fullscreen mode

CVE

Potential Mitigations in Different Phases

Requirements Strategy

Language Selection: Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. Eg., many languages that perform their memory management, such as Java and Perl, are not subject to buffer overflows. Other languages, such as Ada and C#, typically provide overflow protection, but the protection can be disabled by the programmer. Be wary that a language's interface to native code may still be subject to overflows, even if the language itself is theoretically safe.

Architecture and Design Strategy

Libraries or Frameworks: Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. Examples include the Safe C String Library (SafeStr) by Messier and Viega and the Strsafe.h library from Microsoft. These libraries provide safer versions of overflow-prone string-handling functions. This is not a complete solution, since many buffer overflows are not related to strings.
Operation; Build and Compilation

Strategy

Environment Hardening: Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice, which provide various mechanisms including canary-based detection and range/index checking. D3-SFCV (Stack Frame Canary Validation) from D3FEND discusses canary-based detection in detail. This is not necessarily a complete solution, since these mechanisms only detect certain types of overflows. In addition, the result is still a denial of service, since the typical response is to exit the application.
Implementation: Consider adhering to the following rules when allocating and managing an application's memory: Double-check that the buffer is as large as specified. When using functions that accept several bytes to copy, such as strncpy(), be aware that if the destination buffer size is equal to the source buffer size, it may not NULL-terminate the string. Check buffer boundaries if accessing the buffer in a loop and make sure there is no danger of writing past the allocated space. If necessary, truncate all input strings to a reasonable length before passing them to the copy and concatenation functions.

Operation

Build and Compilation Strategy: Environment Hardening Run or compile the software using features or extensions that randomly arrange the positions of a program's executable and libraries in memory. Because this makes the addresses unpredictable, it can prevent an attacker from reliably jumping to exploitable code. Examples include Address Space Layout Randomization (ASLR) and Position-Independent Executables (PIE). Imported modules may be similarly realigned if their default memory addresses conflict with other modules, in a process known as "rebasing" (for Windows) and "prelinking" (for Linux) using randomly generated addresses. ASLR for libraries cannot be used in conjunction with prelink since it would require relocating the libraries at run-time, defeating the whole purpose of prelinking. These techniques do not provide a complete solution. For instance, exploits frequently use a bug that discloses memory addresses to maximize the reliability of code execution. It has also been shown that a side-channel attack can bypass ASLR
Operation Strategy: Environment Hardening Use a CPU and operating system that offers Data Execution Protection (using hardware NX or XD bits) or the equivalent techniques that simulate this feature in software, such as PaX. These techniques ensure that any instruction executed is exclusively at a memory address that is part of the code segment. This is not a complete solution, since buffer overflows could be used to overwrite nearby variables to modify the software's state in dangerous ways. In addition, it cannot be used in cases in which self-modifying code is required. Finally, an attack could still cause a denial of service, since the typical response is to exit the application.

Implementation

Replace unbounded copy functions with analogous functions that support length arguments, such as strcpy with strncpy. Create these if they are not available. This approach is still susceptible to calculation errors, including issues such as off-by-one errors and incorrectly calculating buffer lengths.

Detection Methods

Automated Static Analysis: This weakness can often be detected using automated static analysis tools. Many modern tools use data flow analysis or constraint-based techniques to minimize the number of false positives. Automated static analysis generally does not account for environmental considerations when reporting out-of-bounds memory operations. This can make it difficult for users to determine which warnings should be investigated first. For example, an analysis tool might report buffer overflows that originate from command line arguments in a program that is not expected to run with setuid or other special privileges. Detection techniques for buffer-related errors are more mature than for most other weakness types.

Automated Dynamic Analysis: This weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The software's operation may slow down, but it should not become unstable, crash, or generate incorrect results.

Ref: https://cwe.mitre.org/data/definitions/787.html

Top comments (0)