自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
核心控制流程完整性
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
控制流程完整性 (CFI) 是一種安全機制,可禁止修改已編譯二進位檔的原始控制流程圖,大幅降低執行這類攻擊的難度。
在 Android 9 中,我們在更多元件和核心中啟用了 LLVM 的 CFI 實作功能。系統 CFI 預設為開啟,但您需要啟用核心 CFI。
LLVM 的 CFI 需要使用連結時間最佳化 (LTO) 進行編譯。LTO 會保留物件檔案的 LLVM 位元碼表示法,直到連結時間為止,讓編譯器能更妥善地判斷可執行哪些最佳化作業。啟用 LTO 可縮減最終二進位檔的大小並提升效能,但會增加編譯時間。在 Android 上進行測試時,LTO 和 CFI 的組合會對程式碼大小和效能造成微不足道的額外負擔;在少數情況下,兩者都會有所改善。
如要進一步瞭解 CFI 的技術細節,以及如何處理其他前向控制檢查,請參閱 LLVM 設計文件。
實作
kCFI 修補程式已納入所有支援的 Android 核心版本。CONFIG_CFI_CLANG
選項會啟用 kCFI,並預設在 GKI 中設定。
疑難排解
啟用後,請處理可能存在於其驅動程式中的任何類型不相符錯誤。透過不相容的函式指標進行間接函式呼叫會觸發 CFI。偵測到 CFI 失敗時,核心會列印警告,其中包含呼叫的函式和導致失敗的堆疊追蹤。如要修正這個問題,請確保函式指標一律與所呼叫的函式具有相同類型。
如要協助偵錯 CFI 失敗,請啟用 CONFIG_CFI_PERMISSIVE
,這樣系統就會顯示警告,而不會導致核心發生恐慌。請勿在正式上線環境中使用寬鬆模式。
驗證
目前沒有專門針對 CFI 進行的 CTS 測試。請改為確保啟用或停用 CFI 時,CTS 測試都能通過,以驗證 CFI 不會影響裝置。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Kernel control flow integrity\n\n[Control flow\nintegrity](https://clang.llvm.org/docs/ControlFlowIntegrity.html) (CFI) is a security mechanism that disallows changes to the\noriginal control flow graph of a compiled binary, making it significantly harder\nto perform such attacks.\n\n\nIn Android 9, we enabled LLVM's implementation of CFI in more components and\nalso in the kernel. [System CFI](/docs/security/test/cfi) is on by\ndefault, but you need to enable kernel CFI.\n\n\nLLVM's CFI requires compiling with [Link-Time Optimization\n(LTO)](https://llvm.org/docs/LinkTimeOptimization.html). LTO preserves the LLVM bitcode representation of object files until\nlink-time, which allows the compiler to better reason about what optimizations\ncan be performed. Enabling LTO reduces the size of the final binary and improves\nperformance, but increases compile time. In testing on Android, the combination\nof LTO and CFI results in negligible overhead to code size and performance; in a\nfew cases both improved.\n\n\nFor more technical details about CFI and how other forward-control checks are\nhandled, see the [LLVM design\ndocumentation](https://clang.llvm.org/docs/ControlFlowIntegrityDesign.html).\n\nImplementation\n--------------\n\n\nThe kCFI patches are in all supported Android kernel versions. The `CONFIG_CFI_CLANG `\noption enables kCFI and is set by default in GKI.\n\n### Troubleshooting\n\n\nAfter enabling, work through any type mismatch errors that may exist with their\ndrivers. An indirect function call through an incompatible function pointer\ntrips CFI. When a CFI failure is detected, the kernel prints out a warning that\nincludes both the function that was called and the stacktrace that led to the\nfailure. Correct this by ensuring function pointers always have the same type as\nthe function that's called.\n\n\nTo assist in debugging CFI failures, enable `CONFIG_CFI_PERMISSIVE`,\nwhich prints out a warning instead of causing a kernel panic. Permissive mode\nmust not be used in production.\n\nValidation\n----------\n\n\nCurrently, there are no CTS test specifically for CFI. Instead, make sure that\nCTS tests pass with and without CFI enabled to verify that CFI isn't impacting\nthe device."]]