CWE-126 Variant Draft

Buffer Over-read

This vulnerability occurs when a program reads data from a memory buffer using an index or pointer that points beyond the buffer's allocated boundary, accessing unintended memory locations.

Definition

What is CWE-126?

This vulnerability occurs when a program reads data from a memory buffer using an index or pointer that points beyond the buffer's allocated boundary, accessing unintended memory locations.
Buffer over-reads happen when software fails to properly validate that a read operation stays within the bounds of a buffer. This can lead to the exposure of sensitive information stored in adjacent memory, cause crashes, or create unexpected program behavior. It's a common pitfall when using low-level languages like C/C++ with functions that don't inherently check boundaries, or when manual bounds checking logic contains errors. Detecting these flaws manually across a large codebase is challenging. While SAST tools can identify the risky patterns, Plexicus uses AI to not only find them but also generate specific, context-aware fixes—transforming a security finding into a ready-to-apply code suggestion. This automates the remediation step, helping developers secure their applications faster and more consistently.
Vulnerability Diagram CWE-126
Buffer Over-read "Hello\0" Hello\0 heap memory (secrets, ptrs) A B C D E F G H I J ... read length larger than string Length-trusting reads expose data past the real terminator.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-126

  • Text editor has out-of-bounds read past end of line while indenting C code

  • Chain: "Heartbleed" bug receives an inconsistent length parameter (CWE-130) enabling an out-of-bounds read (CWE-126), returning memory that could include private cryptographic keys and other sensitive data.

  • Chain: product does not handle when an input string is not NULL terminated, leading to buffer over-read or heap-based buffer overflow.

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 1

    In the following C/C++ example the method processMessageFromSocket() will get a message from a socket, placed into a buffer, and will parse the contents of the buffer into a structure that contains the message length and the message body. A for loop is used to copy the message body into a local character string which will be passed to another method for processing.

  2. 2

    However, the message length variable (msgLength) from the structure is used as the condition for ending the for loop without validating that msgLength accurately reflects the actual length of the message body (CWE-606). If msgLength indicates a length that is longer than the size of a message body (CWE-130), then this can result in a buffer over-read by reading past the end of the buffer (CWE-126).

  3. 3

    The following C/C++ example demonstrates a buffer over-read due to a missing NULL terminator. The main method of a pattern matching utility that looks for a specific pattern within a specific file uses the string strncopy() method to copy the command line user input file name and pattern to the Filename and Pattern character arrays respectively.

  4. 4

    However, the code do not take into account that strncpy() will not add a NULL terminator when the source buffer is equal in length of longer than that provide size attribute. Therefore if a user enters a filename or pattern that are the same size as (or larger than) their respective character arrays, a NULL terminator will not be added (CWE-170) which leads to the printf() read beyond the expected end of the Filename and Pattern buffers.

  5. 5

    To fix this problem, be sure to subtract 1 from the sizeof() call to allow room for the null byte to be added.

Verwundbares Codebeispiel

Vulnerable C

In the following C/C++ example the method processMessageFromSocket() will get a message from a socket, placed into a buffer, and will parse the contents of the buffer into a structure that contains the message length and the message body. A for loop is used to copy the message body into a local character string which will be passed to another method for processing.

Verwundbar C
int processMessageFromSocket(int socket) {
  		int success;
  		char buffer[BUFFER_SIZE];
  		char message[MESSAGE_SIZE];
```
// get message from socket and store into buffer* 
  		
  		
  		 *//Ignoring possibliity that buffer > BUFFER_SIZE* 
  		if (getMessage(socket, buffer, BUFFER_SIZE) > 0) {
  		```
```
// place contents of the buffer into message structure* 
  				ExMessage *msg = recastBuffer(buffer);
  				
  				
  				 *// copy message body into string for processing* 
  				int index;
  				for (index = 0; index < msg->msgLength; index++) {
  				```
  					message[index] = msg->msgBody[index];
  				}
  				message[index] = '\0';
```
// process message* 
  				success = processMessage(message);}
  		return success;}
Sicheres Codebeispiel

Secure C

To fix this problem, be sure to subtract 1 from the sizeof() call to allow room for the null byte to be added.

Sicher C
```
/* copy filename parameter to variable, no off-by-one overflow */* 
  strncpy(Filename, argv[2], sizeof(Filename)-1);
  Filename[255]='\0';
  
  
   */* copy pattern parameter to variable, no off-by-one overflow */* 
  strncpy(Pattern, argv[3], sizeof(Pattern)-1);Pattern[31]='\0';
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-126

  • 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-126

Automated Static Analysis High

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Plexicus Auto-Fix

Plexicus erkennt CWE-126 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-126?

This vulnerability occurs when a program reads data from a memory buffer using an index or pointer that points beyond the buffer's allocated boundary, accessing unintended memory locations.

Wie gravierend ist CWE-126?

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-126 betroffen?

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

Wie kann ich CWE-126 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-126?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-126 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-126?

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

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.