What matters in AI.

Subscribe

Learn / AI security

Definition · AI security

Use-after-free vulnerability

A use-after-free vulnerability is a memory bug. Software frees an area of memory but keeps a pointer to it, and then uses that pointer again. The memory can hold new data at that time. An attacker who controls the new data can crash the software, read secrets or execute code. MITRE gives this bug the number CWE-416.

Last reviewed

Key points

  • In a use-after-free (UAF), code frees an area of memory. A remaining pointer to that area stays in use.
  • The result can be bad data, a crash, a leak of memory contents, or code execution (MITRE CWE-416).
  • CWE-416 is number 7 in the 2025 CWE Top 25, from 39,080 CVE records published June 2024 to June 2025.
  • AI software has this bug too. CVE-2026-43631 is a UAF in llama-server of llama.cpp, with a CVSS 4.0 score of 9.2.
  • Memory-safe languages, quarantine of freed memory and AddressSanitizer decrease the risk. Each one has a limit.

How a use-after-free happens

A use-after-free has four steps. MITRE shows this pattern in CWE-416.

  1. The code gets an area of memory and keeps a pointer to it.
  2. One part of the code frees the area. A second pointer to it stays in use.
  3. The memory allocator gives the same area to new data.
  4. The second pointer reads or writes the area. It touches the new data.
Two threads use one object. Thread A frees it. Thread B then uses its pointer to the same memory.
Two threads use one object. Thread A frees it. Thread B then uses its pointer to the same memory.

The CWE-416 entry and the examples on this page show these causes:

  • Race conditions. One thread frees memory that a different thread continues to use.
  • Bad reference counts. The code frees an object while a different part holds it.
  • Close or unload. A connection closes, or a model unloads, while a task continues.

What an attacker gets from a use-after-free

MITRE says the freed memory “may be allocated again” to other data. CWE-416 gives four results:

Result Effect
Bad data Correct data changes with no write to it
Crash Denial of service
Memory leak Secrets from freed memory
Code execution Control of the software

Two sets of numbers show how frequent the bug is:

  • Number 7 in the 2025 CWE Top 25 (MITRE). The data was 39,080 CVE records published June 2024 to June 2025. The CISA KEV catalog of exploited bugs has 14 CVEs of this type.
  • Half of the memory safety bugs in Chromium. Around 70% of its high severity security bugs are memory safety bugs, and “half of those are use-after-free bugs”. The data is 912 high or critical bugs since 2015 (Chromium Projects).

Use-after-free bugs in AI software

AI software has this bug too.

Cyera researchers found the cause. When llama-server unloads a model, a worker thread of an open request keeps pointers into freed memory. At their June 2026 check, the two server UAFs had no patch. For local models, see security risks of running LLMs locally.

AI models also find these bugs. In May 2025, Sean Heelan gave OpenAI o3 selected parts of ksmbd, the SMB server of the Linux kernel. The larger test had about 9k to 12k lines of command handler and connection code. He used only the API, with “no scaffolding, no agentic frameworks, no tool use”. o3 found CVE-2025-37899, a use-after-free in the handler for the SMB logoff command.

The result has limits, from the count of Heelan:

  • On a known test bug, o3 found it in 8 of 100 runs and gave 28 incorrect reports.
  • In the larger test, which found the new bug, the signal to noise ratio was about 1:50.
  • Heelan wrote that “o3 is not infallible.”

4 controls against use-after-free bugs

No single control stops all use-after-free bugs. Each control below has a cost or a limit.

Control What it does Limit
Memory-safe language The Rust compiler guarantees “references will never be dangling” Only for code in that language
Pointer set to NULL after free Defense in depth (MITRE) A NULL dereference can crash the software. Less useful with complex data
Quarantine of freed memory Chromium BackupRefPtr holds and poisons freed memory while a stale raw_ptr points to it Stale pointer use stays undefined behavior
AddressSanitizer Detects use-after-free in code built with it Typical slowdown of 2x

Questions and answers

What is the difference between a use-after-free and a dangling pointer?

A dangling pointer is the cause. MITRE says it is "a pointer that no longer points to valid memory, often after it has been freed". A use-after-free is the bug that occurs when code uses that pointer. MITRE gives "dangling pointer" as a different name for CWE-416.

Can a memory-safe language stop use-after-free bugs?

MITRE gives a language with automatic memory management as a mitigation for CWE-416. The Rust compiler guarantees that references do not dangle. These controls apply to code in that language. A part of the same software in C or C++ does not get this protection.

Sources

  1. CWE-416: Use After FreeMITRE
  2. 2025 CWE Top 25 Most Dangerous Software WeaknessesMITRE, Dec 2025
  3. 2025 CWE Top 25 MethodologyMITRE, Dec 2025
  4. Memory safetyThe Chromium Projects
  5. raw_ptr<T> (aka MiraclePtr, aka BackupRefPtr, aka BRP)The Chromium Projects
  6. The Rust Programming Language, References and BorrowingThe Rust Project
  7. AddressSanitizerLLVM Project
  8. CVE-2026-43631CVE Program (assigned by VulnCheck), 6 Aug 2026
  9. Breaking Local AI Runtimes: 10 vulnerabilities in the Engine Behind Your Open-Source ModelsCyera, 7 Aug 2026
  10. How I used o3 to find CVE-2025-37899, a remote zeroday vulnerability in the Linux kernel's SMB implementationSean Heelan, 22 May 2025
  11. CVE-2025-37899CVE Program (assigned by Linux), 20 May 2025