Goal Reached Thanks to every supporter — we hit 100%!

Goal: 1000 CNY · Raised: 1000 CNY

100.0%

CWE-562 (返回栈上的变量地址) — Vulnerability Class 6

6 vulnerabilities classified as CWE-562 (返回栈上的变量地址). AI Chinese analysis included.

CWE-562 represents a critical memory safety weakness where a function inadvertently returns the address of a local stack variable. Since stack memory is ephemeral and automatically reclaimed upon function exit, the returned pointer becomes a dangling reference. Attackers typically exploit this by invoking subsequent functions that overwrite the stack frame, corrupting the data the pointer references or causing immediate application crashes due to invalid memory access. This vulnerability can lead to unpredictable program behavior, denial of service, or potentially code execution if the corrupted data influences control flow. Developers prevent this by ensuring functions return values by value rather than by reference, or by allocating memory on the heap using dynamic allocation functions like malloc. Additionally, using smart pointers or returning copies of data structures ensures that the returned information remains valid and independent of the caller’s stack frame, thereby eliminating the risk of accessing freed or overwritten memory.

MITRE CWE Description
A function returns the address of a stack variable, which will cause unintended program behavior, typically in the form of a crash. Because local variables are allocated on the stack, when a program returns a pointer to a local variable, it is returning a stack address. A subsequent function call is likely to re-use this same stack address, thereby overwriting the value of the pointer, which no longer corresponds to the same variable since a function's stack frame is invalidated when it returns. At best this will cause the value of the pointer to change unexpectedly. In many cases it causes the program to crash the next time the pointer is dereferenced.
Common Consequences (1)
Availability, Integrity, ConfidentialityRead Memory, Modify Memory, Execute Unauthorized Code or Commands, DoS: Crash, Exit, or Restart
If the returned stack buffer address is dereferenced after the return, then an attacker may be able to modify or read memory, depending on how the address is used. If the address is used for reading, then the address itself may be exposed, or the contents that the address points to. If the address…
Mitigations (1)
ImplementationFix the code so that it does not return a stack address.
Examples (1)
The following function returns a stack address.
char* getName() { char name[STR_MAX]; fillInName(name); return name; }
Bad · C

Vulnerabilities classified as CWE-562 (返回栈上的变量地址) represent 6 CVEs. The CWE taxonomy describes the weakness; review individual CVEs for product-specific impact.