Base · Medium

CWE-123: Write-what-where Condition

Any condition where the attacker has the ability to write an arbitrary value to an arbitrary location, often as the result of a buffer overflow.

CWE-123 · Base Level ·2 CVEs ·2 Mitigations

Description

Any condition where the attacker has the ability to write an arbitrary value to an arbitrary location, often as the result of a buffer overflow.

Potential Impact

Integrity, Confidentiality, Availability, Access Control

Modify Memory, Execute Unauthorized Code or Commands, Gain Privileges or Assume Identity, DoS: Crash, Exit, or Restart, Bypass Protection Mechanism

Integrity, Availability

DoS: Crash, Exit, or Restart, Modify Memory

Access Control, Other

Bypass Protection Mechanism, Other

Demonstrative Examples

The classic example of a write-what-where condition occurs when the accounting information for memory allocations is overwritten in a particular fashion. Here is an example of potentially vulnerable code:
Bad
#define BUFSIZE 256int main(int argc, char **argv) {char *buf1 = (char *) malloc(BUFSIZE);char *buf2 = (char *) malloc(BUFSIZE);strcpy(buf1, argv[1]);free(buf2);}
Vulnerability in this case is dependent on memory layout. The call to strcpy() can be used to write past the end of buf1, and, with a typical layout, can overwrite the accounting information that the system keeps for buf2 when it is allocated. Note that if the allocation header for buf2 can be overwritten, buf2 itself can be overwritten as well.
The allocation header will generally keep a linked list of memory "chunks". Particularly, there may be a "previous" chunk and a "next" chunk. Here, the previous chunk for buf2 will probably be buf1, and the next chunk may be null. When the free() occurs, most memory allocators will rewrite the linked list using data from buf2. Particularly, the "next" chunk for buf1 will be updated and the "previous" chunk for any subsequent chunk will be updated. The attacker can insert a memory address for the "next" chunk and a value to write into that memory address for the "previous" chunk.
This could be used to overwrite a function pointer that gets dereferenced later, replacing it with a memory address that the attacker has legitimate access to, where they have placed malicious code, resulting in arbitrary code execution.

Mitigations & Prevention

Architecture and Design

Use a language that provides appropriate memory abstractions.

Operation

Use OS-level preventative functionality integrated after the fact. Not a complete solution.

Detection Methods

  • 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 sea
  • Automated Dynamic Analysis Moderate — Use tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ [REF-1518].

Real-World CVE Examples

CVE IDDescription
CVE-2019-19911Chain: Python library does not limit the resources used to process images that specify a very large number of bands (CWE-1284), leading to excessive memory consumption (CWE-789) or an integer overflow
CVE-2022-0545Chain: 3D renderer has an integer overflow (CWE-190) leading to write-what-where condition (CWE-123) using a crafted image.

Taxonomy Mappings

  • CLASP: — Write-what-where condition
  • CERT C Secure Coding: ARR30-C — Do not form or use out-of-bounds pointers or array subscripts
  • CERT C Secure Coding: ARR38-C — Guarantee that library functions do not form invalid pointers
  • CERT C Secure Coding: STR31-C — Guarantee that storage for strings has sufficient space for character data and the null terminator
  • CERT C Secure Coding: STR32-C — Do not pass a non-null-terminated character sequence to a library function that expects a string
  • Software Fault Patterns: SFP8 — Faulty Buffer Access

Frequently Asked Questions

What is CWE-123?

CWE-123 (Write-what-where Condition) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. Any condition where the attacker has the ability to write an arbitrary value to an arbitrary location, often as the result of a buffer overflow.

How can CWE-123 be exploited?

Attackers can exploit CWE-123 (Write-what-where Condition) to modify memory, execute unauthorized code or commands, gain privileges or assume identity, dos: crash, exit, or restart, bypass protection mechanism. This weakness is typically introduced during the Implementation phase of software development.

How do I prevent CWE-123?

Key mitigations include: Use a language that provides appropriate memory abstractions.

What is the severity of CWE-123?

CWE-123 is classified as a Base-level weakness (Medium abstraction). It has been observed in 2 real-world CVEs.