CWEweakness

CWE-562 Return of Stack Variable Address

Official page ↗
Abstraction
Variant
Structure
Simple
Status
Draft
Languages
C, C++
CWE version
5.0

Description

A function returns the address of a stack variable.

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.

Common consequences

• 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

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

Detection methods

• Fuzzing: Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues. • Automated Static Analysis: 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.) • Automated Dynamic Analysis: 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].

Observed examples

• CVE-2024-33045: product returns stack variable address, leading to memory corruption

Related

Observed example · CVE (1)

  • CVE-2024-33045 Memory corruption when BTFM client sends new messages over Slimbus to ADSP. vulnerability

Can precede · CWE (2)

  • CWE-672 Operation on a Resource after Expiration or Release weakness
  • CWE-825 Expired Pointer Dereference weakness

Child of · CWE (1)

  • CWE-758 Reliance on Undefined, Unspecified, or Implementation-Defined Behavior weakness

Referenced by CVE · weakness (8)

  • CVE-2026-26399 A stack-use-after-return issue exists in the Arduino_Core_STM32 library prior to version 1.7.0. vulnerability
  • CVE-2026-34553 iccDEV provides a set of libraries and tools for working with ICC color management profiles. vulnerability
  • CVE-2026-3591 A use-after-return vulnerability exists in the `named` server when handling DNS queries signed with SIG(0). vulnerability
  • CVE-2024-33045 Memory corruption when BTFM client sends new messages over Slimbus to ADSP. vulnerability
  • CVE-2024-4418 A race condition leading to a stack use-after-free flaw was found in libvirt. vulnerability
  • CVE-2020-21686 A stack-use-after-scope issue discovered in expand_mmac_params function in preproc.c in nasm before 2.15.04 allows remote attackers to… vulnerability
  • CVE-2022-41837 An out-of-bounds write vulnerability exists in the OpenImageIO::add_exif_item_to_spec functionality of OpenImageIO Project OpenImageIO… vulnerability
  • CVE-2021-21798 An exploitable return of stack variable address vulnerability exists in the JavaScript implementation of Nitro Pro PDF. vulnerability

Referenced by CWE · has member (1)

  • CWE-1340 CISQ Data Protection Measures view