HIGH CWE-787 memory-safety

CWE-787 in APIs

What is CWE-787?

CWE-787, known as Out-of-bounds Write, is a critical software weakness where a program writes data past the end or before the beginning of the intended buffer. This occurs when an application calculates an index or offset that references a memory location outside the bounds of the allocated buffer, then attempts to write data to that location.

The official CWE description states: "The software writes data past the end, or before the beginning, of the intended buffer." This weakness can lead to memory corruption, data integrity issues, and potentially allow attackers to execute arbitrary code by overwriting critical memory structures.

Out-of-bounds writes are particularly dangerous because they often result in memory corruption that can be exploited to alter program execution flow, crash applications, or create security vulnerabilities. The severity stems from the fact that these writes can overwrite adjacent memory structures, function pointers, or return addresses, giving attackers control over the program's behavior.

CWE-787 in API Contexts

In API environments, CWE-787 manifests in several critical ways that differ from traditional application vulnerabilities. APIs often handle dynamic data structures, user-provided parameters, and complex request payloads that can create opportunities for out-of-bounds writes.

Array Index Manipulation: Many APIs accept array indices as parameters. If an API endpoint accepts a user-supplied index without proper bounds checking and uses it to write to an internal array, an attacker could specify an index beyond the array's allocated size. For example, an API that allows updating user preferences by index could be exploited if the index validation is insufficient.

Buffer Overflow in Serialization: APIs frequently serialize and deserialize data structures. If an API deserializes a user-provided object and writes properties to a fixed-size buffer without verifying the object's structure, an attacker could craft a payload that causes out-of-bounds writes during the deserialization process.

Memory Management in Language Runtimes: Even in managed languages like Java or Python, APIs can be vulnerable when interfacing with native libraries or when using unsafe operations. For instance, an API that uses JNI (Java Native Interface) to call C/C++ code might pass unvalidated array indices, leading to out-of-bounds writes in the native layer.

GraphQL and NoSQL APIs: These APIs often allow complex queries with dynamic field selection. If an API processes a GraphQL query and writes results to a buffer based on the requested fields without proper bounds checking, an attacker could craft queries that cause out-of-bounds writes in the response generation logic.

Detection

Detecting CWE-787 requires a combination of static analysis, dynamic testing, and runtime monitoring. For API developers, several approaches can help identify potential out-of-bounds write vulnerabilities.

Static Analysis Tools: Code analysis tools can identify patterns that commonly lead to out-of-bounds writes. Look for array accesses without bounds checking, pointer arithmetic without validation, and unsafe memory operations. Tools like Coverity, SonarQube, and commercial static analyzers can flag suspicious code patterns.

Dynamic Testing with Fuzzing: Fuzz testing tools send malformed or unexpected inputs to your API endpoints to trigger crashes or unusual behavior. Tools like OWASP ZAP, Burp Suite, or specialized fuzzers can help identify endpoints that don't properly validate array indices or buffer sizes.

Runtime Memory Analysis: Memory analysis tools can detect when a program writes outside allocated memory regions. AddressSanitizer (ASan) and similar tools can identify out-of-bounds writes during testing by instrumenting the code to check memory accesses.

middleBrick API Security Scanning: middleBrick performs automated security scanning of API endpoints to detect various vulnerabilities, including those that could lead to out-of-bounds conditions. The scanner tests unauthenticated attack surfaces and identifies risky patterns in API responses and behaviors. middleBrick's comprehensive scanning includes:

  • Input validation testing to identify parameters that could be manipulated
  • Property authorization checks to ensure proper access controls
  • Data exposure analysis to detect sensitive information leakage
  • Rate limiting verification to prevent abuse

By scanning your API with middleBrick, you can identify potential CWE-787 vulnerabilities before they're exploited in production. The scanner provides actionable findings with severity levels and remediation guidance.

Remediation

Fixing CWE-787 vulnerabilities requires implementing proper bounds checking and validation throughout your API code. Here are specific remediation strategies with code examples:

Bounds Checking for Array Access: Always validate array indices before use. Never trust user-supplied indices.

// Vulnerable code - no bounds checking
public void updatePreference(int index, String value) {
    preferences[index] = value; // Potential out-of-bounds write
}

// Secure implementation
public void updatePreference(int index, String value) {
    if (index < 0 || index >= preferences.length) {
        throw new IndexOutOfBoundsException("Index out of range: " + index);
    }
    preferences[index] = value;
}

Safe Buffer Management: When working with buffers, always track the current size and never write beyond allocated space.

// Vulnerable - potential buffer overflow
public void appendData(byte[] data) {
    System.arraycopy(data, 0, buffer, offset, data.length);
    offset += data.length; // No check if offset exceeds buffer capacity
}

// Secure implementation
public void appendData(byte[] data) {
    if (offset + data.length > buffer.length) {
        throw new BufferOverflowException("Insufficient buffer space");
    }
    System.arraycopy(data, 0, buffer, offset, data.length);
    offset += data.length;
}

Input Validation for API Parameters: Validate all user-supplied parameters, especially those used as indices or sizes.

@PostMapping("/api/items/{index}")
public ResponseEntity<Item> getItem(@PathVariable int index) {
    // Validate index before using it
    if (index < 0 || index >= MAX_ALLOWED_INDEX) {
        return ResponseEntity.badRequest()
            .body(null);
    }
    
    Item item = itemRepository.findById(index);
    if (item == null) {
        return ResponseEntity.notFound().build();
    }
    return ResponseEntity.ok(item);
}

Safe Deserialization: When deserializing data structures, validate their integrity and size before processing.

// Vulnerable deserialization
public void processUserData(byte[] userData) throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(userData));
    User user = (User) ois.readObject();
    // Process user data without validation
}

// Secure deserialization with validation
public void processUserData(byte[] userData) throws IOException, ClassNotFoundException {
    if (userData == null || userData.length == 0) {
        throw new IllegalArgumentException("Empty user data");
    }
    
    try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(userData))) {
        User user = (User) ois.readObject();
        validateUserStructure(user); // Custom validation
        processValidatedUser(user);
    }
}

Using Safe Libraries and Frameworks: Prefer libraries and frameworks that provide built-in bounds checking and safe memory operations. Modern languages and frameworks often include protections against common out-of-bounds write patterns.

Defense in Depth: Implement multiple layers of protection, including input sanitization, output encoding, and runtime memory protection. Use containerization and sandboxing to limit the impact of any potential exploitation.

Frequently Asked Questions

How can I test my API for CWE-787 vulnerabilities?
Test your API using a combination of static analysis tools to review code patterns, dynamic testing with fuzzers to send malformed inputs, and runtime memory analysis tools like AddressSanitizer. Additionally, use automated API security scanners like middleBrick to identify potential vulnerabilities in your API endpoints. middleBrick can scan your API's unauthenticated attack surface and provide findings with severity levels and remediation guidance.
Are managed languages like Java or Python immune to CWE-787?
No, managed languages are not immune to CWE-787. While they provide memory safety for most operations, vulnerabilities can still occur through unsafe operations, native library interfaces (JNI), custom memory management, or when using certain libraries that perform unsafe operations. APIs in managed languages can also be vulnerable when they accept user-supplied indices or sizes and use them without proper validation. Always validate inputs and use safe coding practices regardless of the programming language.