# 释放后使用

释放后使用（use-after-free，UAF）是一种内存安全缺陷，程序在内存已被释放之后仍通过指针访问该内存，通常是通过一个仍持有旧地址的悬垂指针。它在通用缺陷枚举中被归类为 CWE-416，属于时间性内存安全违规，也就是说，它关乎内存的生命周期，而非内存的边界<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup><sup> • </sup><sup>[2](https://www.usenix.org/events/sec10/tech/full_papers/security10_proceedings.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 分类 | CWE-416，释放后使用；是过期指针解引用的子类，与双重释放（CWE-415）同级<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup> |
| 后果 | 数据损坏、因无效内存块数据导致的崩溃、读取已释放内存造成的信息泄露，以及当已释放内存被重新分配并填入攻击者控制的内容时导致的任意代码执行<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup> |
| 普遍程度 | 根据 BUDAlloc 论文引用的研究，在 CWE 列表中前 25 个最普遍且影响最大的漏洞中排名第七<sup>[3](https://www.usenix.org/system/files/usenixsecurity24-ahn.pdf)</sup> |
| 利用条件 | 攻击者必须在悬垂指针被解引用之前，让已释放对象的内存被受控数据重新占用<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup> |
| 分配器防御 | 一次性分配从不复用虚拟地址，因此已释放对象的内存无法被重新占用；FFmalloc 以中等开销阻止了所有测试过的 UAF 攻击<sup>[4](https://www.usenix.org/conference/usenixsecurity21/presentation/wickman)</sup> |
| 已部署的缓解措施 | Chrome 的 MiraclePtr（BackupRefPtr）有效缓解了特权进程中 57% 的释放后使用漏洞<sup>[5](https://security.googleblog.com/2024/10/safer-with-google-advancing-memory.html)</sup> |
| 语言层面的修复 | 在内存安全语言中，除非覆盖默认语言设置，释放后使用缺陷不可能发生<sup>[6](https://www.govinfo.gov/content/pkg/GOVPUB-D-PURL-gpo248056/pdf/GOVPUB-D-PURL-gpo248056.pdf)</sup> |

## 缺陷如何产生

该缺陷需要两个在时间上分隔的事件。首先，程序用 `free` 或 `delete` 释放一个堆对象，而指向它的某个指针仍然存活；那个存活的指针就是悬垂指针。其次，程序解引用该悬垂指针。在对象所有权在组件之间转移的大型代码库中，追踪哪个组件可能仍在使用某个指针十分困难，这就是 UAF 往往难以发现的原因<sup>[7](https://security.googleblog.com/2022/05/retrofitting-temporal-memory-safety-on-c.html?hl=en_GB)</sup>。

一个相关的变体是释放后使用指针所指向的内存已经被重新分配给了一个新对象；这些同样是漏洞利用的入口<sup>[8](https://www.tdcommons.org/dpubs_series/3932/)</sup>。双重释放，即对同一资源调用两次 `free`，在 CWE 的结构化描述中被视为一种释放后使用，其中两次使用都是释放调用<sup>[9](https://cwe.mitre.org/documents/structured_descriptions/index.html)</sup>。

## 利用

释放后使用的后果取决于内存分配器的实现以及错误访问之后运行的代码<sup>[10](https://www.usenix.org/system/files/sec21-wickman.pdf)</sup>。CWE 条目列出了四种影响模式<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup>：

- **内存修改。** 如果该区域已被重新分配并正在别处合法使用，对已释放内存的写入可能损坏有效数据。
- **拒绝服务。** 如果在已释放数据被使用之后发生内存块合并，当无效数据被解释为分配器元数据时，进程可能崩溃。
- **信息泄露。** 对已释放内存的读取有时会泄露敏感信息，而不是导致崩溃。
- **代码执行。** 如果在合并之前将攻击者控制的数据放入重新分配的内存中，就可能产生任意地址写入原语。当新分配持有 C++ 对象时，散布在堆数据中的函数指针可被覆盖为指向 shellcode 的地址，从而实现任意代码执行。

因此，利用方法就是：释放一个对象，整理堆使同一内存的新分配包含攻击者选定的值，然后触发悬垂使用。

## 为何该缺陷类别意义重大

UAF 缺陷是内存不安全语言中最常被报告和利用的安全问题之一<sup>[10](https://www.usenix.org/system/files/sec21-wickman.pdf)</sup>。在 Chrome 中，大多数高危安全缺陷都是 UAF 问题<sup>[7](https://security.googleblog.com/2022/05/retrofitting-temporal-memory-safety-on-c.html?hl=en_GB)</sup>，对 100 个近期 Chrome 高危缺陷的抽样显示，浏览器进程中的释放后使用缺陷是该进程中最大的类别<sup>[12](https://security.googleblog.com/2022/09/use-after-freedom-miracleptr.html?m=1)</sup>。在操作系统内核中，复杂的控制路径和长生命周期的数据结构使严格的时间性内存安全难以强制执行，UAF 缺陷仍然是内核被攻陷的主要根源<sup>[13](https://www.preprints.org/manuscript/202602.0445)</sup>。

## 防御措施

**语言选择。** 最彻底的修复在语言层面。内存安全语言默认防止该缺陷类别：在其中，除非覆盖默认语言设置，释放后使用缺陷不可能发生<sup>[6](https://www.govinfo.gov/content/pkg/GOVPUB-D-PURL-gpo248056/pdf/GOVPUB-D-PURL-gpo248056.pdf)</sup>。Go、Java 和 Python 等语言的垃圾回收运行时管理堆内存，只有当没有存活引用能到达某块内存时才将其释放<sup>[14](https://doi.org/10.1145/3795888)</sup>。谷歌报告称，随着 Android 开发转向内存安全语言，六年间内存安全漏洞占 Android 全部漏洞的比例从 76% 降至 24%<sup>[15](https://security.googleblog.com/2024/09/eliminating-memory-safety-vulnerabilities-Android.html)</sup>。谷歌的结论是，不存在将 C++ 演进为具有严格时间性安全保证的语言的现实路径，并正在考虑将其 C++ 代码逐步过渡到内存安全语言<sup>[16](https://research.google/pubs/secure-by-design-googles-perspective-on-memory-safety/)</sup>。

**分配器重新设计。** 由于利用需要重新占用已释放内存，分配器可以打破攻击链。一次性分配（OTA）从不为后续分配复用虚拟地址，因此每个请求获得不同的内存块；这并不消除缺陷，但使其无法被利用，悬垂访问会命中未映射内存，这也使得检测成为可能<sup>[3](https://www.usenix.org/system/files/usenixsecurity24-ahn.pdf)</sup>。FFmalloc 是一种实用的一次性分配实现，使用批量页管理以及碰撞指针与固定大小分箱分配的混合方式，以中等开销阻止了所有测试过的释放后使用攻击<sup>[4](https://www.usenix.org/conference/usenixsecurity21/presentation/wickman)</sup>。BUDAlloc 与内核协同设计用户级分配器，将虚拟地址和物理地址管理分离；在 [SPEC CPU](https://www.edgepedia.org/zh/spec-cpu) 2017 上，其性能比 DangZero 提升 15%，内存开销比 FFmalloc 降低 61%<sup>[3](https://www.usenix.org/system/files/usenixsecurity24-ahn.pdf)</sup>。更早的设计走了其他路线：Cling 限制复用，使地址空间仅在相同类型的对象之间复用<sup>[2](https://www.usenix.org/events/sec10/tech/full_papers/security10_proceedings.pdf)</sup>；FreeGuard 针对堆溢出、过度读取、释放后使用以及双重和无效释放<sup>[17](https://arxiv.org/abs/1709.02746)</sup>。

**智能指针与引用计数。** Chrome 的 MiraclePtr 将原始指针重写为智能指针类型 `raw_ptr<T>`，由 BackupRefPtr 算法支撑。PartitionAlloc 为分配划出隐藏的[引用计数](https://www.edgepedia.org/zh/reference-counting)；当计数大于零时调用 `free`，内存会被隔离并投毒而非释放，只有当计数归零时才会被复用<sup>[12](https://security.googleblog.com/2022/09/use-after-freedom-miracleptr.html?m=1)</sup>。谷歌在 Chrome 中重写了超过 15,000 个原始指针，并在 Chrome 102 中为 Windows 和 Android 的浏览器进程启用了 BackupRefPtr，估计可为约 50% 的释放后使用问题提供防利用保护<sup>[12](https://security.googleblog.com/2022/09/use-after-freedom-miracleptr.html?m=1)</sup>；到 2024 年，该公司报告 MiraclePtr 已有效缓解特权进程中 57% 的释放后使用漏洞，并与在野利用的减少相关联<sup>[5](https://security.googleblog.com/2024/10/safer-with-google-advancing-memory.html)</sup>。引用计数一般而言在某些情况下有效，但并非普遍适用于 C/C++ 应用，且带来运行时检查开销<sup>[3](https://www.usenix.org/system/files/usenixsecurity24-ahn.pdf)</sup>。

**指针卫生与检测工具。** CWE 的缓解指导建议选择具有自动内存管理的语言，并作为纵深防御，在释放时将指针设为 NULL；NULL 解引用仍可能崩溃，但降低或消除了代码执行的风险<sup>[1](https://cwe.mitre.org/data/definitions/416.html)</sup>。静态分析可以追踪对象生命周期并检测可能无效的解引用<sup>[18](https://arxiv.org/abs/2410.23764)</sup>，尽管静态方法在循环处理、内存表示优化以及构造覆盖所有执行路径的测试输入方面面临局限<sup>[19](https://www.sciencedirect.com/science/article/pii/S0167404822004400)</sup>。动态工具在特定执行中捕获缺陷：[AddressSanitizer](https://www.edgepedia.org/zh/addresssanitizer) 报告堆释放后使用错误，附带分配和释放栈，例如识别出位于一个由 `operator delete[]` 释放的 400 字节区域内的 4 字节读取<sup>[20](https://clang.llvm.org/docs/AddressSanitizer.html)</sup>。Chrome 还在生产环境中使用 GWP-ASan 采样检测和轻量级 UAF 检测<sup>[21](https://security.googleblog.com/2024/10/evaluating-mitigations-vulnerabilities.html)</sup>。

## 政策与行业响应

由于 UAF 及相关缺陷在 C 和 C++ 代码中持续存在，各国政府已将内存安全列为采购和政策问题。CISA 和 FBI 的联合指导将内存安全漏洞列入危险的产品安全不良实践，并建议制造商使用内存安全语言或借助能够防止此类漏洞的硬件功能来构建产品，并发布内存安全路线图<sup>[22](https://www.cisa.gov/sites/default/files/2024-10/joint-guidance-product-security-bad-practices-508c.pdf)</sup>。一份关于内存安全语言的美国政府报告指出，消除缓冲区溢出漏洞也有助于减少其他内存安全问题，包括释放后使用<sup>[23](https://www.cisa.gov/sites/default/files/2025-02/secure-by-design-alert-eliminating-buffer-overflow-vulnerabilities-508c.pdf)</sup>。Android 的文档将包括释放后使用在内的内存安全缺陷视为其代码库中最常见的问题，占高危安全漏洞的 60% 以上<sup>[24](https://source.android.com/docs/security/test/memory-safety?hl=en)</sup>。

## References

1. [CWE-416: Use After Free, MITRE](https://cwe.mitre.org/data/definitions/416.html)
2. [Cling: A Memory Allocator to Mitigate Dangling Pointers, USENIX Security 2010 proceedings](https://www.usenix.org/events/sec10/tech/full_papers/security10_proceedings.pdf)
3. [BUDAlloc: Defeating Use-After-Free Bugs by Decoupling Virtual Address Management from Kernel, USENIX Security 2024](https://www.usenix.org/system/files/usenixsecurity24-ahn.pdf)
4. [Preventing Use-After-Free Attacks with Fast Forward Allocation, USENIX Security 2021](https://www.usenix.org/conference/usenixsecurity21/presentation/wickman)
5. [Safer with Google: Advancing Memory Safety, Google Security Blog](https://security.googleblog.com/2024/10/safer-with-google-advancing-memory.html)
6. [Memory Safe Languages: Reducing Vulnerabilities in Modern Software Development, NSA (via govinfo)](https://www.govinfo.gov/content/pkg/GOVPUB-D-PURL-gpo248056/pdf/GOVPUB-D-PURL-gpo248056.pdf)
7. [Retrofitting Temporal Memory Safety on C++, Google Security Blog](https://security.googleblog.com/2022/05/retrofitting-temporal-memory-safety-on-c.html?hl=en_GB)
8. [Automatic Sanitization of Use-After-Free Pointers, TD Commons](https://www.tdcommons.org/dpubs_series/3932/)
9. [Structured CWE Descriptions, MITRE](https://cwe.mitre.org/documents/structured_descriptions/index.html)
10. [Preventing Use-After-Free Attacks with Fast Forward Allocation (paper PDF), USENIX Security 2021](https://www.usenix.org/system/files/sec21-wickman.pdf)
11. [Google released a critical Chrome update today, Laptop Mag](https://www.laptopmag.com/software/browsers-search-engines/google-chrome-june-5-2025-update)
12. [Use-after-freedom: MiraclePtr, Google Security Blog](https://security.googleblog.com/2022/09/use-after-freedom-miracleptr.html?m=1)
13. [Lifetime-Aware Kernel Object Isolation Using Temporal Protection Windows, Preprints.org](https://www.preprints.org/manuscript/202602.0445)
14. [Safe Coding, Communications of the ACM](https://doi.org/10.1145/3795888)
15. [Eliminating Memory Safety Vulnerabilities at the Source, Google Security Blog](https://security.googleblog.com/2024/09/eliminating-memory-safety-vulnerabilities-Android.html)
16. [Secure by Design: Google's Perspective on Memory Safety, Google Research](https://research.google/pubs/secure-by-design-googles-perspective-on-memory-safety/)
17. [FreeGuard: A Faster Secure Heap Allocator, arXiv](https://arxiv.org/abs/1709.02746)
18. [Static Analysis Framework for Detecting Use-After-Free Bugs in C++, arXiv](https://arxiv.org/abs/2410.23764)
19. [UAF-GUARD: Defending the use-after-free exploits via fine-grained memory permission management, Computers & Security](https://www.sciencedirect.com/science/article/pii/S0167404822004400)
20. [AddressSanitizer, Clang documentation](https://clang.llvm.org/docs/AddressSanitizer.html)
21. [Evaluating Mitigations & Vulnerabilities in Chrome, Google Security Blog](https://security.googleblog.com/2024/10/evaluating-mitigations-vulnerabilities.html)
22. [Product Security Bad Practices, CISA and FBI joint guidance](https://www.cisa.gov/sites/default/files/2024-10/joint-guidance-product-security-bad-practices-508c.pdf)
23. [Secure by Design Alert: Eliminating Buffer Overflow Vulnerabilities, CISA](https://www.cisa.gov/sites/default/files/2025-02/secure-by-design-alert-eliminating-buffer-overflow-vulnerabilities-508c.pdf)
24. [Memory safety, Android Open Source Project](https://source.android.com/docs/security/test/memory-safety?hl=en)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/use-after-free

English original: https://www.edgepedia.org/use-after-free
