BaseDraft

CWE-562Return of Stack Variable Address

Category: other

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 / Confidentiality — Read 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 is used for writing, this can lead to a crash and possibly code execution.

Potential mitigations· 1

  • [Implementation]Fix the code so that it does not return a stack address.

References

  1. https://cwe.mitre.org/data/definitions/562.html

Related by meaning· 6

Nearest entities by semantic similarity across the cs-graph corpus.

CWE
Return of Pointer Value Outside of Expected Range
CWE
Stack-based Buffer Overflow
CWE
Returning a Mutable Object to an Untrusted Caller
CWE
Use of sizeof() on a Pointer Type
CWE
Use of Uninitialized Variable
CWE
Failure to Handle Missing Parameter
Sourced from MITRE CWE 4.20. Curated for EU compliance use cases by Adam Lundqvist, Founder at SQUR.