Weakness · Medium
CWE-401: Missing Release of Memory after Effective Lifetime
What it is
The product does not sufficiently track and release allocated memory after it has been used, making the memory unavailable for reallocation and reuse.
- Severity (our rating)
- Medium
- Likelihood of exploit
- Medium
- Introduced during
- Implementation
- Exploited in the wild
- 1 known exploited CVE
What an attacker can do
- DoS: Crash, Exit, or Restart (Availability)
- DoS: Instability (Availability)
- DoS: Resource Consumption (CPU) (Availability)
- DoS: Resource Consumption (Memory) (Availability)
- Reduce Performance (Other)
How to prevent it
Implementation
- Choose a language or tool that provides automatic memory management, or makes manual memory management less error-prone. For example, glibc in Linux provides protection against free of invalid pointers. When using Xcode to target OS X or iOS, enable automatic reference counting (ARC). To help correctly and consistently manage memory when programming in C++, consider using a smart pointer class such as std::auto_ptr (defined by ISO/IEC ISO/IEC 14882:2003), std::shared_ptr and std::unique_ptr (specified by an upcoming revision of the C++ standard, informally referred to as C++ 1x), or equivalent solutions such as Boost.
Architecture and Design
- Use an abstraction library to abstract away risky APIs. Not a complete solution.
- The Boehm-Demers-Weiser Garbage Collector or valgrind can be used to detect leaks in code.
How to find it
- 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.)
Exploited in the wild: 1 CVE
Vulnerabilities caused by CWE-401 that attackers have used, from CISA's Known Exploited Vulnerabilities catalog, newest first.
H2 Security
Found it in your code?
We review where it appears, fix it with your team, and check it was not already used against you.
Talk to our teamT Academy
Stop writing it in the first place
Learn to build software and systems without weaknesses like this one, with practice on real hosts in your browser, every objective checked on the machine.
Start freeSource: MITRE Common Weakness Enumeration, CWE version 4.19.1. CWE is a trademark of The MITRE Corporation.