CWE-562: Return of Stack Variable Address

BaseDraft

A function returns the address of a stack variable, which will cause unintended program behavior, typically in the form of a crash.

View on MITRE
Back to CWE Lookup

Extended Description

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.

Technical Details

Structure
Simple

Applicable To

Languages
CC++
Platforms

Frequently Asked Questions

What is CWE-562: Return of Stack Variable Address?+

CWE-562: Return of Stack Variable Address is a Common Weakness Enumeration (CWE) entry maintained by MITRE. 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.

What are the security consequences of Return of Stack Variable Address?+

If exploited, CWE-562 (Return of Stack Variable Address) it can compromise Availability, Integrity and Confidentiality, leading to outcomes such as Read Memory, Modify Memory, Execute Unauthorized Code or Commands and DoS: Crash, Exit, or Restart.

How do you prevent or mitigate Return of Stack Variable Address?+

Recommended mitigations for CWE-562 include: Use static analysis tools to spot return of the address of a stack variable.

Which programming languages are affected by Return of Stack Variable Address?+

CWE-562 commonly affects C and C++. Note that weaknesses are often language-agnostic patterns, so secure coding practices apply broadly.

What is the difference between a CWE and a CVE?+

A CWE (Common Weakness Enumeration) like CWE-562 describes a category of software weakness — the underlying flaw type. A CVE (Common Vulnerabilities and Exposures) identifies a specific, real-world vulnerability in a particular product. In short, a CWE is the kind of mistake, and a CVE is an instance of that mistake being found in software.

Learn More