94 lines · plain
1==============================================2Control Flow Verification Tool Design Document3==============================================4 5.. contents::6 :local:7 8Objective9=========10 11This document provides an overview of an external tool to verify the protection12mechanisms implemented by Clang's *Control Flow Integrity* (CFI) schemes13(``-fsanitize=cfi``). This tool, given a binary or DSO, should infer whether14indirect control flow operations are protected by CFI, and should output these15results in a human-readable form.16 17This tool should also be added as part of Clang's continuous integration testing18framework, where modifications to the compiler ensure that CFI protection19schemes are still present in the final binary.20 21Location22========23 24This tool will be present as a part of the LLVM toolchain, and will reside in25the ``/llvm/tools/llvm-cfi-verify`` directory, relative to the LLVM trunk. It will26be tested in two methods:27 28- Unit tests to validate code sections, present in29 ``/llvm/unittests/tools/llvm-cfi-verify``.30- Integration tests, present in ``/llvm/tools/clang/test/LLVMCFIVerify``. These31 integration tests are part of clang as part of a continuous integration32 framework, ensuring updates to the compiler that reduce CFI coverage on33 indirect control flow instructions are identified.34 35Background36==========37 38This tool will continuously validate that CFI directives are properly39implemented around all indirect control flows by analysing the output machine40code. The analysis of machine code is important as it ensures that any bugs41present in the linker or compiler do not subvert CFI protections in the final42shipped binary.43 44Unprotected indirect control flow instructions will be flagged for manual45review. These unexpected control flows may not have been accounted for in46the compiler implementation of CFI (e.g., indirect jumps to facilitate switch47statements may not be fully protected).48 49It may be possible in the future to extend this tool to flag unnecessary CFI50directives (e.g., CFI directives around a static call to a non-polymorphic base51type). This type of directive has no security implications, but may present52performance impacts.53 54Design Ideas55============56 57This tool will disassemble binaries and DSO's from their machine code format and58analyse the disassembled machine code. The tool will inspect virtual calls and59indirect function calls. This tool will also inspect indirect jumps, as inlined60functions and jump tables should also be subject to CFI protections. Non-virtual61calls (``-fsanitize=cfi-nvcall``) and cast checks (``-fsanitize=cfi-*cast*``)62are not implemented due to a lack of information provided by the bytecode.63 64The tool would operate by searching for indirect control flow instructions in65the disassembly. A control flow graph would be generated from a small buffer of66the instructions surrounding the 'target' control flow instruction. If the67target instruction is branched-to, the fallthrough of the branch should be the68CFI trap (on x86, this is a ``ud2`` instruction). If the target instruction is69the fallthrough (i.e., immediately succeeds) of a conditional jump, the70conditional jump target should be the CFI trap. If an indirect control flow71instruction does not conform to one of these formats, the target will be noted72as being CFI-unprotected.73 74Note that in the second case outlined above (where the target instruction is the75fallthrough of a conditional jump), if the target represents a vcall that takes76arguments, these arguments may be pushed to the stack after the branch but77before the target instruction. In these cases, a secondary 'spill graph' in78constructed, to ensure the register argument used by the indirect jump/call is79not spilled from the stack at any point in the interim. If there are no80spills that affect the target register, the target is marked as CFI-protected.81 82Other Design Notes83~~~~~~~~~~~~~~~~~~84 85Only machine code sections that are marked as executable will be subject to this86analysis. Non-executable sections do not require analysis as any execution87present in these sections has already violated the control flow integrity.88 89Suitable extensions may be made at a later date to include analysis for indirect90control flow operations across DSO boundaries. Currently, these CFI features are91only experimental with an unstable ABI, making them unsuitable for analysis.92 93The tool currently only supports the x86, x86_64, and AArch64 architectures.94