Evaluating a pointer—including dereferencing the pointer, using it as an operand of an arithmetic operation, type casting it, and using it as the right-hand side of an assignment—into memory that has been deallocated by a memory management function is undefined behavior. Pointers to memory that has been deallocated are called dangling pointers. Accessing a dangling pointer can result in exploitable vulnerabilities.
It is at the memory manager's discretion when to reallocate or recycle the freed memory. When memory is freed, all pointers into it become invalid, and its contents might either be returned to the operating system, making the freed space inaccessible, or remain intact and accessible. As a result, the data at the freed location can appear to be valid but change unexpectedly. Consequently, memory must not be written to or read from once it is freed.
Noncompliant Code Example (new
and delete
)
In this noncompliant code example, s
is dereferenced after it has been deallocated. If this access results in a write-after-free, the vulnerability can be exploited to run arbitrary code with the permissions of the vulnerable process. Typically, dynamic memory allocations and deallocations are far removed, making it difficult to recognize and diagnose such problems.
#include <new> struct S { void f(); }; void f() noexcept(false) { S *s = new S; // ... delete s; // ... s->f(); }
The function f()
is marked noexcept(false)
to comply with MEM52-CPP. Detect and handle memory allocation errors.
Compliant Solution (new
and delete
)
In this compliant solution, the dynamically allocated memory is not deallocated until it is no longer required:
#include <new> struct S { void f(); }; void f() noexcept(false) { S *s = new S; // ... s->f(); delete s; }
Compliant Solution (Automatic Storage Duration)
When possible, use automatic storage duration instead of dynamic storage duration. Since s
is not required to live beyond the scope of f()
, this compliant solution uses automatic storage duration to limit the lifetime of s
to the scope of f()
:
struct S { void f(); }; void f() { S s; // ... s.f(); }
Noncompliant Code Example (std::unique_ptr
)
In the following noncompliant code example, the dynamically allocated memory managed by the buff
object is accessed after it has been implicitly deallocated by the object's destructor:
#include <iostream> #include <memory> #include <cstring> int main(int argc, const char *argv[]) { const char *s = ""; if (argc > 1) { enum { BUFFER_SIZE = 32 }; try { std::unique_ptr<char[]> buff(new char[BUFFER_SIZE]); // ... s = std::strncpy(buff.get(), argv[1], BUFFER_SIZE - 1); } catch (std::bad_alloc &) { // Handle error } } std::cout << s << std::endl; }
Compliant Solution (std::unique_ptr
)
In this compliant solution, the lifetime of the buff
object extends past the point at which the memory managed by the object is accessed:
#include <iostream> #include <memory> #include <cstring> int main(int argc, const char *argv[]) { std::unique_ptr<char[]> buff; const char *s = ""; if (argc > 1) { enum { BUFFER_SIZE = 32 }; try { buff.reset(new char[BUFFER_SIZE]); // ... s = std::strncpy(buff.get(), argv[1], BUFFER_SIZE - 1); } catch (std::bad_alloc &) { // Handle error } } std::cout << s << std::endl; }
Compliant Solution
In this compliant solution, a variable with automatic storage duration of type std::string
is used in place of the std::unique_ptr<char[]>
, which reduces the complexity and increases the security of the solution:
#include <iostream> #include <string> int main(int argc, const char *argv[]) { std::string str; if (argc > 1) { str = argv[1]; } std::cout << str << std::endl; }
Noncompliant Code Example (std::string::c_str()
)
In this noncompliant code example, std::string::c_str()
is being called on a temporary std::string
object. The resulting pointer will point to released memory once the std::string
object is destroyed at the end of the assignment expression, resulting in undefined behavior when accessing elements of that pointer.
#include <string> std::string someStringReturningFunction(); void displayString(const char *); void f() { const char *str = someStringReturningFunction().c_str(); displayString(str); /* Undefined behavior */ }
Compliant solution (std::string::c_str()
)
In this compliant solution, a local copy of the string returned by someStringReturningFunction()
is made to ensure that string str
will be valid when the call to displayString
is made:
#include <string> std::string someStringReturningFunction(); void displayString(const char *s); void f() { std::string str = someStringReturningFunction(); const char *str = str.c_str(); displayString(str); /* ok */ }
Noncompliant Code Example
In this noncompliant code example, an attempt is made to allocate zero bytes of memory through a call to operator new()
. If this request succeeds, operator new()
is required to return a non-null pointer value. However, according to the C++ Standard, [basic.stc.dynamic.allocation], paragraph 2 [ISO/IEC 14882-2014], attempting to indirect through such a pointer results in undefined behavior.
#include <new> void f() noexcept(false) { unsigned char *ptr = static_cast<unsigned char *>(::operator new(0)); *ptr = 0; // ... ::operator delete(ptr); }
Compliant Solution
The compliant solution depends on programmer intent. If the programmer intends to allocate a single unsigned char
object, the compliant solution is to use new
instead of a direct call to operator new()
, as this compliant solution demonstrates:
void f() noexcept(false) { unsigned char *ptr = new unsigned char; *ptr = 0; // ... delete ptr; }
If the programmer intends to allocate zero bytes of memory (perhaps in order to obtain a unique pointer value that cannot be reused by any other pointer in the program until it is properly released), then instead of attempting to dereference the resulting pointer, the compliant solution is to declare ptr
as a void *
, which cannot be indirected through in a conforming implementation.
#include <new> void f() noexcept(false) { void *ptr = ::operator new(0); // ... ::operator delete(ptr); }
Noncompliant Code Example
In this noncompliant code example, the B *
pointer value stored by a std::shared_ptr
object is cast to the D *
pointer type with dynamic_cast
in an attempt to obtain a std::shared_ptr
of the polymorphic derived type. However, this eventually results in undefined behavior as the same pointer is stored in two different std::shared_ptr
objects. When g()
exits, the pointer stored in Derived
is freed by the default deleter. Any further use of Poly
results in accessing freed memory. When f()
exits, the same pointer stored in Poly
is destroyed, resulting in a double-free vulnerability.
#include <memory> struct B { virtual ~B() = default; // Polymorphic object // ... }; struct D : B {}; void g(std::shared_ptr<D> Derived); void f() { std::shared_ptr<B> Poly(new D); // ... g(std::shared_ptr<D>(dynamic_cast<D *>(Poly.get()))); // Any use of Poly will now result in accessing freed memory. }
Compliant Solution
In this compliant solution, the dynamic_cast
is replaced with a call to std::dynamic_pointer_cast()
, which returns a std::shared_ptr
of the polymorphic type with the valid shared pointer value. When g()
exits, the reference count to the underlying pointer is decremented by the destruction of Derived
, but because of the reference held by Poly
(within f()
) the stored pointer value is still valid after g()
returns.
#include <memory> struct B { virtual ~B() = default; // Polymorphic object // ... }; struct D : B {}; void g(std::shared_ptr<D> Derived); void f() { std::shared_ptr<B> Poly(new D); // ... g(std::dynamic_pointer_cast<D, B>(Poly)); // Poly is still referring to a valid pointer value. }
Risk Assessment
Reading previously dynamically allocated memory after it has been deallocated can lead to abnormal program termination and denial-of-service attacks. Writing memory that has been deallocated can lead to the execution of arbitrary code with the permissions of the vulnerable process.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MEM50-CPP | High | Likely | Medium | P18 | L1 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
|
|
| |
v7.5.0 | USE_AFTER_FREE | Can detect the specific instances where memory is deallocated more than once or read/written to the target of a freed pointer | |
5.0 | Double Free |
| |
2024.3 | UFM.DEREF.MIGHT |
| |
9.7.1
| 51 D | Fully implemented | |
5.0
|
|
|
Related Vulnerabilities
VU#623332 describes a double-free vulnerability in the MIT Kerberos 5 function krb5_recvauth().
Search for other vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ Coding Standard | MEM52-CPP. Detect and handle memory allocation errors |
SEI CERT C Coding Standard | MEM30-C. Do not access freed memory |
MITRE CWE |
Bibliography
[ISO/IEC 14882-2014] | Subclause 3.7.4.1, "Allocation Functions" Subclause 3.7.4.2, "Deallocation Functions" |
[Seacord 2013b] | Chapter 4, "Dynamic Memory Management" |