# 控制流完整性

**控制流完整性**（Control-flow integrity，CFI）是一种计算机安全技术，通过监视程序的分支行为来检测运行时攻击，确保软件执行沿预先确定的控制流图（control-flow graph，CFG）的路径进行<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/1706.07257)</sup>。该技术由马丁·阿巴迪（Martín Abadi）及其同事在2005年CCS会议上提出，用于防御控制流劫持攻击——攻击者利用内存破坏漏洞重定向程序的执行流<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup><sup> • </sup><sup>[3](https://arxiv.org/abs/2508.15386)</sup>。如今，CFI已集成到多个生产编译器中，并以硬件形式集成到主流处理器中<sup>[4](https://arxiv.org/abs/1602.04056)</sup>。

| 关键事实 | 详情 |
|---|---|
| 原始定义 | 执行必须沿预先确定的控制流图的路径进行；通过轻量级静态验证和插入运行时检查的机器码重写来实施（马丁·阿巴迪等，CCS 2005）<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup> |
| 威胁模型 | 即使攻击者完全控制正在运行的程序的整个数据内存，保护仍然有效<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup> |
| 粒度 | 粗粒度策略使用少量标签，速度快但允许原始程序之外的控制流；细粒度策略利用CFG区分每个间接分支的合法目标<sup>[5](https://www.sciencedirect.com/science/article/abs/pii/S0167404824001019)</sup><sup> • </sup><sup>[6](https://ics.uci.edu/~perl/publication/losing_control/)</sup> |
| 编译器部署 | Clang的`-fsanitize=cfi`方案（虚调用、间接调用、错误类型转换）需要链接时优化；Chrome在Linux x86-64上从M54起启用虚调用CFI，从M68起启用间接调用CFI<sup>[7](https://clang.llvm.org/docs/ControlFlowIntegrity.html)</sup><sup> • </sup><sup>[8](https://www.chromium.org/developers/testing/control-flow-integrity/)</sup> |
| Chrome中的开销 | CPU开销低于1%，观察到二进制文件大小增加最多15%<sup>[8](https://www.chromium.org/developers/testing/control-flow-integrity/)</sup> |
| 硬件实施 | Intel CET提供影子栈和间接分支跟踪；Arm提供指针认证（PAC）和分支目标识别（BTI）<sup>[9](https://www.kernel.org/doc/html/next/x86/shstk.html)</sup><sup> • </sup><sup>[10](https://developer.arm.com/community/arm-community-blogs/b/operating-systems-blog/posts/control-flow-integrity)</sup> |
| 已知局限 | 在六个被测二进制文件中的五个里，完全精确的静态CFI被控制流弯曲（Control-Flow Bending）攻破；栈操纵可绕过32位和64位x86的CFI检查<sup>[11](https://www.usenix.org/conference/usenixsecurity15/technical-sessions/presentation/carlini)</sup><sup> • </sup><sup>[6](https://ics.uci.edu/~perl/publication/losing_control/)</sup> |

## 目的与威胁模型

控制流劫持攻击使攻击者能够控制程序的执行、执行任意恶意代码并实现图灵完备的操作；正因如此，在软件攻击中它们被认为是最危险的<sup>[12](https://link.springer.com/article/10.1186/s42400-021-00098-2)</sup>。典型路径是缓冲区溢出破坏函数指针，使下一次间接调用跳转到攻击者选定的地址<sup>[13](https://learn.microsoft.com/en-us/windows/win32/secbp/control-flow-guard)</sup>。面向返回的编程（ROP）和面向调用/跳转的编程（COP/JOP）复用已有的代码片段（称为小工具），通过被劫持的返回和间接分支将它们串联起来<sup>[14](https://gcc.sourceware.org/onlinedocs/gcc/Instrumentation-Options.html)</sup>。

原始CFI设计假设存在强大的对手：即使攻击者完全控制正在运行的程序的整个数据内存，包括攻击者控制同一地址空间中的模块或线程，保护仍然有效<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup>。该策略刻意保持简单，以便能够被形式化定义和分析，并且设计为以低开销适用于现有代码<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup>。

## 实施方式

CFI的实施结合了静态分析与运行时检查。控制流图可以通过源代码分析、二进制分析或执行剖析来推导；原始实现使用静态二进制分析和机器码重写，在软件中插入运行时检查以验证每次控制转移都保持在CFG之内<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup>。在常见的基于标签的表述中，间接分支目标被静态分配标签并在运行时检查<sup>[15](https://www.ll.mit.edu/r-d/publications/control-jujutsu-weaknesses-fine-grained-control-flow-integrity)</sup>。

控制转移分为前向边（间接调用和跳转，包括C++虚调用和类型转换）和后向边（函数返回）。Clang实现了前向边方案，如`cfi-vcall`（检查虚调用使用的vptr是否为正确的动态类型）和`cfi-icall`（检查间接调用的目标是否为匹配类型的函数）；检查失败时默认中止程序<sup>[7](https://clang.llvm.org/docs/ControlFlowIntegrity.html)</sup>。后向边保护通常采用影子栈——一个存放返回地址的辅助栈，应用程序无法直接修改；返回时比较两份副本，不匹配则触发故障<sup>[9](https://www.kernel.org/doc/html/next/x86/shstk.html)</sup>。

## 粒度及其局限

**粗粒度CFI**将许多合法目标归入少量标签以保持低开销。例如，微软的控制流保护（Control Flow Guard）仅检查间接调用目标是否为程序中某处的合法调用目标，使用Windows内核维护的位图；检查失败则终止程序<sup>[13](https://learn.microsoft.com/en-us/windows/win32/secbp/control-flow-guard)</sup>。研究早已表明这种宽松性可被利用：对手可以针对粗粒度策略构建图灵完备的代码复用攻击，因为这些策略允许不属于原始程序的控制流<sup>[6](https://ics.uci.edu/~perl/publication/losing_control/)</sup>。允许的目标越多，攻击者可利用的小工具就越多<sup>[5](https://www.sciencedirect.com/science/article/abs/pii/S0167404824001019)</sup>。

**细粒度CFI**根据CFG区分每个间接分支的合法目标<sup>[5](https://www.sciencedirect.com/science/article/abs/pii/S0167404824001019)</sup>。长期以来人们认为它更安全，但攻击表明并非如此。控制流弯曲攻击（卡尔里尼、巴雷西、帕耶尔、瓦格纳和格罗斯，USENIX Security 2015）推广了非控制数据攻击：攻击者利用内存破坏漏洞将执行弯曲到允许的路径上，仅使用对标准库的调用即可在内存上实现图灵完备计算。针对完全精确的静态CFI——即不破坏功能的最严格策略——在六个真实二进制文件中的五个里仍可实现强大的攻击；添加影子栈后在六个案例中的三个里消除了任意代码执行<sup>[11](https://www.usenix.org/conference/usenixsecurity15/technical-sessions/presentation/carlini)</sup>。

2015年的另外两项结果进一步收窄了细粒度CFI的保证。[麻省理工学院林肯实验室](https://www.edgepedia.org/zh/mit-lincoln-laboratory)安全弹性系统研究员哈梅德·奥赫拉维（Hamed Okhravi）及同事在“Control jujutsu”中表明，即使拥有无限标签数量的细粒度CFI加上影子栈，对于Apache和Nginx等代码库的攻击仍然无效——这些代码库的编码实践即使在强静态分析器下也会在预期CFG中产生灵活性<sup>[15](https://www.ll.mit.edu/r-d/publications/control-jujutsu-weaknesses-fine-grained-control-flow-integrity)</sup>。[帕多瓦大学](https://www.edgepedia.org/zh/university-of-padua)计算机科学教授毛罗·孔蒂（Mauro Conti）及同事表明，基于堆的漏洞可以控制栈内容，包括用于验证控制流转移的安全关键值；他们演示了对Windows 32位CFI实现以及两个主流开源编译器中的细粒度IFCC和VTV实现的攻击，32位和64位x86的CFI检查均被证明易受栈操纵攻击<sup>[6](https://ics.uci.edu/~perl/publication/losing_control/)</sup>。

一项使用LLVM-CFI分析框架对静态CFI保护进行的调查评估了八种最先进防御在[Google Chrome](https://www.edgepedia.org/zh/google-chrome)和Apache Httpd等真实程序上的表现，对策略进行了相互排名，并披露了八种严格策略下受保护的调用目标，为类似COOP的代码复用攻击铺平了道路<sup>[16](https://arxiv.org/abs/1910.01485)</sup>。

## 编译器与平台部署

CFI在2010年代中期从研究走向生产编译器<sup>[4](https://arxiv.org/abs/1602.04056)</sup>。

**Clang/LLVM。** `-fsanitize=cfi`标志启用针对虚调用、非虚调用、派生及无关类型转换、间接调用和成员函数指针调用的方案。除`kcfi`外所有方案都需要链接时优化，且类需要隐藏的LTO可见性才能生成检查；独立的`kcfi`方案面向内核等底层系统软件，不需要LTO，且从不破坏跨DSO的函数地址相等性<sup>[7](https://clang.llvm.org/docs/ControlFlowIntegrity.html)</sup>。在Chromium上测得的虚调用方案开销在Dromaeo基准测试中低于1%，观察到二进制文件大小增加最多15%<sup>[7](https://clang.llvm.org/docs/ControlFlowIntegrity.html)</sup>。

**Chrome。** 谷歌在Linux x86-64上的官方Chrome中部署了Clang CFI：从M54起启用虚调用CFI，从M68起启用间接调用CFI，并通过持续运行的机器人保持浏览器无错误类型转换<sup>[8](https://www.chromium.org/developers/testing/control-flow-integrity/)</sup>。

**微软。** 控制流保护可通过`/guard:cf`选项在[Visual Studio](https://www.edgepedia.org/zh/visual-studio)中使用，运行于[Windows 10](https://www.edgepedia.org/zh/windows-10)、[Windows 11](https://www.edgepedia.org/zh/windows-11)以及Windows Server 2019及更高版本；编译器插入轻量级检查并识别合法的间接调用目标，Windows内核通过位图进行验证<sup>[13](https://learn.microsoft.com/en-us/windows/win32/secbp/control-flow-guard)</sup>。Windows还使用硬件强制栈保护来保护返回地址；在内核模式下，每个内核栈都有一个影子栈，返回地址不匹配会触发蓝屏。内核模式功能默认关闭，需要Intel CET硬件（第11代Intel Core移动版及更新）或AMD影子栈（Zen 3及更新），以及基于虚拟化的安全和虚拟机监控程序保护的代码完整性<sup>[17](https://learn.microsoft.com/en-us/windows-server/security/kernel-mode-hardware-stack-protection)</sup>。

**GCC。** `-fcf-protection=[full|branch|return|none|check]`选项在x86 GNU/Linux目标上使用Intel CET对间接分支和返回进行插桩，旨在防御ROP和COP/JOP<sup>[14](https://gcc.sourceware.org/onlinedocs/gcc/Instrumentation-Options.html)</sup>。

## 硬件实施

硬件支持将检查移入处理器。鲁汶大学的鲁安·德·克莱尔克（Ruan de Clercq）和英格丽德·费尔鲍韦德（Ingrid Verbauwhede）在2017年的一项调查中分析了21种近期基于硬件的CFI架构的安全策略，发现许多不适合广泛采用，因为它们依赖于难以获得的精确控制流模型，或实施的策略安全性有限<sup>[2](https://arxiv.org/abs/1706.07257)</sup>。

**Intel CET**包含两个x86特性：影子栈和间接分支跟踪（IBT）。执行CALL指令时，处理器将返回地址同时压入普通栈和影子栈；返回时比较两者，不匹配则引发控制保护故障。IBT验证间接CALL/JMP目标以编译器生成的`ENDBR`操作码开头<sup>[9](https://www.kernel.org/doc/html/next/x86/shstk.html)</sup>。Linux内核通过`X86_USER_SHADOW_STACK`配置选项暴露用户空间影子栈；在64位内核中，仅支持用户空间影子栈和内核IBT<sup>[9](https://www.kernel.org/doc/html/next/x86/shstk.html)</sup>。

**Arm PAC和BTI。** Arm的指针认证对指针进行签名和[认证](https://www.edgepedia.org/zh/authentication)，分支目标识别用着陆垫标记合法的间接分支目标。Arm和谷歌在Android开源项目（包括其构建系统、工具链、BoringSSL和Linux内核5.10）中全面启用了两者，并在2020年发布的AOSP 12中进行了验证。指针认证在M101中为Chromium中的所有C++代码启用，在M102中为V8生成的代码启用；BTI在M105中成为默认设置。使用原生代码的Android应用可通过使用`-mbranch-protection=standard`编译来采用该保护<sup>[10](https://developer.arm.com/community/arm-community-blogs/b/operating-systems-blog/posts/control-flow-integrity)</sup>。

## 实际有效性

部署经验和后续评估对原始保证提出了限定。CFI实施阻止了原始论文中检查的大多数经典攻击，包括基于栈的缓冲区溢出、基于堆的“跳转到libc”攻击和指针诡计攻击，但它并非万能药：停留在允许CFG范围内的漏洞利用（如某些参数解析攻击）无法被阻止<sup>[1](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)</sup>。

2025年的一项分类研究将LLVM的前向边CFI变体映射到来自“十大已知被利用漏洞”列表的四个高影响漏洞类别，为每个类别选择一个代表性CVE；LLVM的CFI在其中两个案例中阻止了利用，在另外两个中失败，既说明了其潜力也说明了其当前局限<sup>[3](https://arxiv.org/abs/2508.15386)</sup>。在大型C和C++代码库中部署CFI仍然困难，原因包括可见性不匹配、类型不一致和意外行为故障<sup>[18](https://arxiv.org/abs/2512.22701)</sup>。

## References

1. [Martín Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti (2005). Control-Flow Integrity. CCS 2005.](https://www.cs.columbia.edu/~suman/secure_sw_devel/p340-abadi.pdf)
2. [Ruan de Clercq, Ingrid Verbauwhede (2017). A survey of Hardware-based Control Flow Integrity (CFI). arXiv:1706.07257.](https://arxiv.org/abs/1706.07257)
3. [Sabine Houy, Bruno Kreyssig, Timothee Riom, Alexandre Bartel, Patrick McDaniel (2025). A Practical Guideline and Taxonomy to LLVM's Control Flow Integrity. arXiv:2508.15386.](https://arxiv.org/abs/2508.15386)
4. [Control-Flow Integrity: Precision, Security, and Performance. arXiv:1602.04056.](https://arxiv.org/abs/1602.04056)
5. [SCFI: Efficient forward fine-grained control flow integrity based on coarse-grained ISA extensions. Computers & Security.](https://www.sciencedirect.com/science/article/abs/pii/S0167404824001019)
6. [Mauro Conti et al. (2015). Losing control: On the effectiveness of control-flow integrity under stack attacks. ACM CCS 2015.](https://ics.uci.edu/~perl/publication/losing_control/)
7. [Control Flow Integrity, Clang documentation.](https://clang.llvm.org/docs/ControlFlowIntegrity.html)
8. [Control Flow Integrity, The Chromium Projects.](https://www.chromium.org/developers/testing/control-flow-integrity/)
9. [Control-flow Enforcement Technology (CET) Shadow Stack, Linux Kernel documentation.](https://www.kernel.org/doc/html/next/x86/shstk.html)
10. [Control Flow Integrity, Arm Community blog.](https://developer.arm.com/community/arm-community-blogs/b/operating-systems-blog/posts/control-flow-integrity)
11. [Nicholas Carlini et al. (2015). Control-Flow Bending: On the Effectiveness of Control-Flow Integrity. USENIX Security 2015.](https://www.usenix.org/conference/usenixsecurity15/technical-sessions/presentation/carlini)
12. [GPT conjecture: understanding the trade-offs between granularity, performance and timeliness in control-flow integrity. Cybersecurity (Springer Nature).](https://link.springer.com/article/10.1186/s42400-021-00098-2)
13. [Control Flow Guard for platform security, Microsoft Learn.](https://learn.microsoft.com/en-us/windows/win32/secbp/control-flow-guard)
14. [Instrumentation Options, Using the GNU Compiler Collection (GCC).](https://gcc.sourceware.org/onlinedocs/gcc/Instrumentation-Options.html)
15. [Hamed Okhravi et al. (2015). Control jujutsu: on the weaknesses of fine-grained control flow integrity. ACM CCS 2015, MIT Lincoln Laboratory.](https://www.ll.mit.edu/r-d/publications/control-jujutsu-weaknesses-fine-grained-control-flow-integrity)
16. [Analyzing Control Flow Integrity with LLVM-CFI. arXiv:1910.01485.](https://arxiv.org/abs/1910.01485)
17. [Kernel Mode Hardware-enforced Stack Protection, Microsoft Learn.](https://learn.microsoft.com/en-us/windows-server/security/kernel-mode-hardware-stack-protection)
18. [CFIghter: Automated Control-Flow Integrity Enablement and Evaluation for Legacy C/C++ Systems. arXiv:2512.22701.](https://arxiv.org/abs/2512.22701)

---
*Initially written Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/control-flow-integrity

English original: https://www.edgepedia.org/control-flow-integrity
