Führe statische Analyse (SAST) auf der Codebasis aus und suche im Datenfluss nach dem unsicheren Muster.
Buffer Underwrite ('Buffer Underflow')
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.
What is CWE-124?
Real-world CVEs caused by CWE-124
-
buffer underwrite in firmware verification routine allows code execution via a crafted firmware image
-
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
Angreiferpfad Schritt für Schritt
- 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
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
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.
- 4
In the case where the substring is not found in destBuf, strstr() will return NULL, causing the pointer arithmetic to be undefined, potentially setting the value of idx to a negative number. If idx is negative, this will result in a buffer underwrite of destBuf.
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.
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;} Secure pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
const safe = validateAndEscape(input);
return executeWithGuards(safe);
} How to prevent CWE-124
- Requirements Choose a language that is not susceptible to these issues.
- Implementation All calculated values that are used as index or for pointer arithmetic should be validated to ensure that they are within an expected range.
How to detect CWE-124
Führe dynamische Application-Security-Tests gegen den Live-Endpoint aus.
Beobachte Runtime-Logs auf ungewöhnliche Exception-Traces, fehlerhafte Eingaben oder Versuche, Autorisierung zu umgehen.
Code Review: Markiere jeden neuen Code, der Eingaben von dieser Oberfläche ohne validierte Framework-Helper verarbeitet.
Plexicus erkennt CWE-124 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.
Frequently asked questions
Was ist CWE-124?
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.
Wie gravierend ist CWE-124?
MITRE stuft die Exploit-Wahrscheinlichkeit als mittel ein — eine Ausnutzung ist realistisch, erfordert aber meist bestimmte Bedingungen.
Welche Sprachen oder Plattformen sind von CWE-124 betroffen?
MITRE lists the following affected platforms: C, C++.
Wie kann ich CWE-124 verhindern?
Choose a language that is not susceptible to these issues. All calculated values that are used as index or for pointer arithmetic should be validated to ensure that they are within an expected range.
Wie erkennt und behebt Plexicus CWE-124?
Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-124 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-124?
MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/124.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.
Weaknesses related to CWE-124
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 Under-read
A buffer under-read occurs when a program attempts to read data from a memory location positioned before the start of an allocated buffer.
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.