CWE-786 Base Incomplete

Access of Memory Location Before Start of Buffer

This vulnerability occurs when software attempts to read from or write to a memory location positioned before the official start of a buffer.

Definition

What is CWE-786?

This vulnerability occurs when software attempts to read from or write to a memory location positioned before the official start of a buffer.
This flaw, often called a 'buffer under-read' or 'under-write,' happens when a pointer or index is incorrectly positioned. Common triggers include decrementing a pointer beyond the buffer's first element, performing pointer arithmetic that steps back too far, or directly using a negative index value to access array-like structures. Accessing memory before a buffer's valid range can lead to unpredictable behavior. It may read sensitive data from unrelated parts of memory, corrupt critical program state, or cause an immediate crash, creating opportunities for denial-of-service or information disclosure attacks.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-786

  • Unchecked length of SSLv2 challenge value leads to buffer underflow.

  • Buffer underflow from a small size value with a large buffer (length parameter inconsistency, CWE-130)

  • Buffer underflow from an all-whitespace string, which causes a counter to be decremented before the buffer while looking for a non-whitespace character.

  • Buffer underflow resultant from encoded data that triggers an integer overflow.

  • Product sets an incorrect buffer size limit, leading to "off-by-two" buffer underflow.

  • Negative value is used in a memcpy() operation, leading to buffer underflow.

  • Buffer underflow due to mishandled special characters

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 1

    In the following C/C++ example, a utility function is used to trim trailing whitespace from a character string. The function copies the input string to a local character string and uses a while statement to remove the trailing whitespace by moving backward through the string and overwriting whitespace with a NUL character.

  2. 2

    However, this function can cause a buffer underwrite if the input character string contains all whitespace. On some systems the while statement will move backwards past the beginning of a character string and will call the isspace() function on an address outside of the bounds of the local buffer.

  3. 3

    The following example asks a user for an offset into an array to select an item.

  4. 4

    The programmer allows the user to specify which element in the list to select, however an attacker can provide an out-of-bounds offset, resulting in a buffer over-read (CWE-126).

  5. 5

    The following is an example of code that may result in a buffer underwrite. This code is attempting to replace the substring "Replace Me" in destBuf with the string stored in srcBuf. It does so by using the function strstr(), which returns a pointer to the found substring in destBuf. Using pointer arithmetic, the starting index of the substring is found.

Verwundbares Codebeispiel

Vulnerable C

In the following C/C++ example, a utility function is used to trim trailing whitespace from a character string. The function copies the input string to a local character string and uses a while statement to remove the trailing whitespace by moving backward through the string and overwriting whitespace with a NUL character.

Verwundbar C
char* trimTrailingWhitespace(char *strMessage, int length) {
  		char *retMessage;
  		char *message = malloc(sizeof(char)*(length+1));
```
// 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';
```
// trim trailing whitespace* 
  		int len = index-1;
  		while (isspace(message[len])) {
  		```
  			message[len] = '\0';
  			len--;
  		}
```
// return string without trailing whitespace* 
  		retMessage = message;
  		return retMessage;}
Sicheres Codebeispiel

Secure pseudo

Sicher pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
  const safe = validateAndEscape(input);
  return executeWithGuards(safe);
}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Präventions-Checkliste

How to prevent CWE-786

  • Architecture Use safe-by-default frameworks and APIs that prevent the unsafe pattern from being expressible.
  • Implementation Validate input at trust boundaries; use allowlists, not denylists.
  • Implementation Apply the principle of least privilege to credentials, file paths, and runtime permissions.
  • Testing Cover this weakness in CI: SAST rules + targeted unit tests for the data flow.
  • Operation Monitor logs for the runtime signals listed in the next section.
Erkennungssignale

How to detect CWE-786

Fuzzing High

Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

Plexicus Auto-Fix

Plexicus erkennt CWE-786 automatisch und öffnet in unter 60 Sekunden einen Fix-PR.

Codex Remedium scannt jeden Commit, identifiziert genau diese Schwachstelle und liefert einen reviewer-ready Pull Request mit dem Patch. Keine Tickets. Keine Hand-offs.

Häufig gestellte Fragen

Frequently asked questions

Was ist CWE-786?

This vulnerability occurs when software attempts to read from or write to a memory location positioned before the official start of a buffer.

Wie gravierend ist CWE-786?

MITRE hat für diese Schwachstelle keine Exploit-Wahrscheinlichkeit veröffentlicht. Behandle sie als mittlere Auswirkung, bis dein Threat Model anderes belegt.

Welche Sprachen oder Plattformen sind von CWE-786 betroffen?

MITRE lists the following affected platforms: C, C++.

Wie kann ich CWE-786 verhindern?

Use safe-by-default frameworks, validate untrusted input at trust boundaries, and apply the principle of least privilege. Cover the data-flow signature in CI with SAST.

Wie erkennt und behebt Plexicus CWE-786?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-786 bei jedem Commit. Bei einem Treffer öffnet unser Codex-Remedium-Agent einen Fix-PR mit korrigiertem Code, Tests und einer einzeiligen Zusammenfassung für den Reviewer.

Wo erfahre ich mehr über CWE-786?

MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/786.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.

Verwandte Schwachstellen

Weaknesses related to CWE-786

CWE-119 Parent

Improper Restriction of Operations within the Bounds of a Memory Buffer

This vulnerability occurs when software accesses a memory buffer but reads from or writes to a location outside its allocated boundary.…

CWE-120 Sibling

Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')

This vulnerability occurs when a program copies data from one memory location to another without first verifying that the source data will…

CWE-123 Sibling

Write-what-where Condition

A write-what-where condition occurs when an attacker can control both the data written and the exact memory location where it's written,…

CWE-125 Sibling

Out-of-bounds Read

An out-of-bounds read occurs when software accesses memory outside the boundaries of a buffer, array, or similar data structure, reading…

CWE-130 Sibling

Improper Handling of Length Parameter Inconsistency

This vulnerability occurs when a program reads a structured data packet or message but fails to properly validate that the declared length…

CWE-466 Sibling

Return of Pointer Value Outside of Expected Range

This vulnerability occurs when a function returns a memory pointer that points outside the expected buffer range, potentially exposing…

CWE-787 Sibling

Out-of-bounds Write

This vulnerability occurs when software incorrectly writes data outside the boundaries of its allocated memory buffer, either beyond the…

CWE-788 Sibling

Access of Memory Location After End of Buffer

This vulnerability occurs when software attempts to read from or write to a memory buffer using an index or pointer that points past the…

CWE-805 Sibling

Buffer Access with Incorrect Length Value

This vulnerability occurs when software reads from or writes to a buffer using a loop or sequential operation, but mistakenly calculates…

Bereit, wenn du es bist

Schluss mit dem Bezahlen pro Entwickler.
Schließ den Kreislauf.

Plexicus ist die KI-native ASPM, die scannt, filtert, fixt, pentestet und erklärt — autonom. Unbegrenzte Entwickler, unbegrenzte Repos, Fair-Use-KI-Aktionen. Echter kostenloser Tarif, €269/mo jährlich, wenn du bereit bist.