1a4ccff32SPeter Collingbourne====================== 2a4ccff32SPeter CollingbourneControl Flow Integrity 3a4ccff32SPeter Collingbourne====================== 4a4ccff32SPeter Collingbourne 5a4ccff32SPeter Collingbourne.. toctree:: 6a4ccff32SPeter Collingbourne :hidden: 7a4ccff32SPeter Collingbourne 8a4ccff32SPeter Collingbourne ControlFlowIntegrityDesign 9a4ccff32SPeter Collingbourne 10a4ccff32SPeter Collingbourne.. contents:: 11a4ccff32SPeter Collingbourne :local: 12a4ccff32SPeter Collingbourne 13a4ccff32SPeter CollingbourneIntroduction 14a4ccff32SPeter Collingbourne============ 15a4ccff32SPeter Collingbourne 16a4ccff32SPeter CollingbourneClang includes an implementation of a number of control flow integrity (CFI) 17a4ccff32SPeter Collingbourneschemes, which are designed to abort the program upon detecting certain forms 18a4ccff32SPeter Collingbourneof undefined behavior that can potentially allow attackers to subvert the 19a4ccff32SPeter Collingbourneprogram's control flow. These schemes have been optimized for performance, 20a4ccff32SPeter Collingbourneallowing developers to enable them in release builds. 21a4ccff32SPeter Collingbourne 22a4ccff32SPeter CollingbourneTo enable Clang's available CFI schemes, use the flag ``-fsanitize=cfi``. 239eda6404SAlexey SamsonovYou can also enable a subset of available :ref:`schemes <cfi-schemes>`. 249eda6404SAlexey SamsonovAs currently implemented, all schemes rely on link-time optimization (LTO); 259eda6404SAlexey Samsonovso it is required to specify ``-flto``, and the linker used must support LTO, 269eda6404SAlexey Samsonovfor example via the `gold plugin`_. 27fd6f92d5SEvgeniy Stepanov 283afb2668SPeter CollingbourneTo allow the checks to be implemented efficiently, the program must 293afb2668SPeter Collingbournebe structured such that certain object files are compiled with CFI 30fd6f92d5SEvgeniy Stepanovenabled, and are statically linked into the program. This may preclude 313afb2668SPeter Collingbournethe use of shared libraries in some cases. 323afb2668SPeter Collingbourne 333afb2668SPeter CollingbourneThe compiler will only produce CFI checks for a class if it can infer hidden 343afb2668SPeter CollingbourneLTO visibility for that class. LTO visibility is a property of a class that 353afb2668SPeter Collingbourneis inferred from flags and attributes. For more details, see the documentation 363afb2668SPeter Collingbournefor :doc:`LTO visibility <LTOVisibility>`. 373afb2668SPeter Collingbourne 383afb2668SPeter CollingbourneThe ``-fsanitize=cfi-{vcall,nvcall,derived-cast,unrelated-cast}`` flags 393afb2668SPeter Collingbournerequire that a ``-fvisibility=`` flag also be specified. This is because the 403afb2668SPeter Collingbournedefault visibility setting is ``-fvisibility=default``, which would disable 413afb2668SPeter CollingbourneCFI checks for classes without visibility attributes. Most users will want 423afb2668SPeter Collingbourneto specify ``-fvisibility=hidden``, which enables CFI checks for such classes. 433afb2668SPeter Collingbourne 443afb2668SPeter CollingbourneExperimental support for :ref:`cross-DSO control flow integrity 453afb2668SPeter Collingbourne<cfi-cross-dso>` exists that does not require classes to have hidden LTO 463afb2668SPeter Collingbournevisibility. This cross-DSO support has unstable ABI at this time. 47a4ccff32SPeter Collingbourne 48bc5c3f57SSylvestre Ledru.. _gold plugin: https://llvm.org/docs/GoldPlugin.html 49a4ccff32SPeter Collingbourne 509eda6404SAlexey Samsonov.. _cfi-schemes: 519eda6404SAlexey Samsonov 529eda6404SAlexey SamsonovAvailable schemes 539eda6404SAlexey Samsonov================= 549eda6404SAlexey Samsonov 559eda6404SAlexey SamsonovAvailable schemes are: 569eda6404SAlexey Samsonov 579eda6404SAlexey Samsonov - ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks 589eda6404SAlexey Samsonov <cfi-strictness>`. 599eda6404SAlexey Samsonov - ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong 609eda6404SAlexey Samsonov dynamic type. 619eda6404SAlexey Samsonov - ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another 629eda6404SAlexey Samsonov unrelated type to the wrong dynamic type. 639eda6404SAlexey Samsonov - ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of 649eda6404SAlexey Samsonov the wrong dynamic type. 659eda6404SAlexey Samsonov - ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the 669eda6404SAlexey Samsonov wrong dynamic type. 679eda6404SAlexey Samsonov - ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic 689eda6404SAlexey Samsonov type. 69e44acadfSPeter Collingbourne - ``-fsanitize=cfi-mfcall``: Indirect call via a member function pointer with 70e44acadfSPeter Collingbourne wrong dynamic type. 719eda6404SAlexey Samsonov 729eda6404SAlexey SamsonovYou can use ``-fsanitize=cfi`` to enable all the schemes and use 739eda6404SAlexey Samsonov``-fno-sanitize`` flag to narrow down the set of schemes as desired. 749eda6404SAlexey SamsonovFor example, you can build your program with 759eda6404SAlexey Samsonov``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall`` 769eda6404SAlexey Samsonovto use all schemes except for non-virtual member function call and indirect call 779eda6404SAlexey Samsonovchecking. 789eda6404SAlexey Samsonov 7966cf68edSEvgenii StepanovRemember that you have to provide ``-flto`` or ``-flto=thin`` if at 8066cf68edSEvgenii Stepanovleast one CFI scheme is enabled. 819eda6404SAlexey Samsonov 8293bb862fSPeter CollingbourneTrapping and Diagnostics 8393bb862fSPeter Collingbourne======================== 8493bb862fSPeter Collingbourne 8593bb862fSPeter CollingbourneBy default, CFI will abort the program immediately upon detecting a control 8693bb862fSPeter Collingbourneflow integrity violation. You can use the :ref:`-fno-sanitize-trap= 8793bb862fSPeter Collingbourne<controlling-code-generation>` flag to cause CFI to print a diagnostic 8893bb862fSPeter Collingbournesimilar to the one below before the program aborts. 8993bb862fSPeter Collingbourne 9093bb862fSPeter Collingbourne.. code-block:: console 9193bb862fSPeter Collingbourne 9293bb862fSPeter Collingbourne bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50) 9393bb862fSPeter Collingbourne 0x000000425a50: note: vtable is of type 'A' 9493bb862fSPeter Collingbourne 00 00 00 00 f0 f1 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 5a 42 00 9593bb862fSPeter Collingbourne ^ 9693bb862fSPeter Collingbourne 9793bb862fSPeter CollingbourneIf diagnostics are enabled, you can also configure CFI to continue program 9893bb862fSPeter Collingbourneexecution instead of aborting by using the :ref:`-fsanitize-recover= 9993bb862fSPeter Collingbourne<controlling-code-generation>` flag. 1009eda6404SAlexey Samsonov 101a4ccff32SPeter CollingbourneForward-Edge CFI for Virtual Calls 1029eda6404SAlexey Samsonov================================== 103a4ccff32SPeter Collingbourne 104a4ccff32SPeter CollingbourneThis scheme checks that virtual calls take place using a vptr of the correct 105a4ccff32SPeter Collingbournedynamic type; that is, the dynamic type of the called object must be a 106a4ccff32SPeter Collingbournederived class of the static type of the object used to make the call. 1071a7488afSPeter CollingbourneThis CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``. 108a4ccff32SPeter Collingbourne 109b8b248cfSPeter CollingbourneFor this scheme to work, all translation units containing the definition 110b8b248cfSPeter Collingbourneof a virtual member function (whether inline or not), other than members 111d7ec48d7SNico Weberof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 112b8b248cfSPeter Collingbournevisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 113b8b248cfSPeter Collingbourneenabled and be statically linked into the program. 114a4ccff32SPeter Collingbourne 115a4ccff32SPeter CollingbournePerformance 1169eda6404SAlexey Samsonov----------- 117a4ccff32SPeter Collingbourne 118a4ccff32SPeter CollingbourneA performance overhead of less than 1% has been measured by running the 119a4ccff32SPeter CollingbourneDromaeo benchmark suite against an instrumented version of the Chromium 120a4ccff32SPeter Collingbourneweb browser. Another good performance benchmark for this mechanism is the 121a4ccff32SPeter Collingbournevirtual-call-heavy SPEC 2006 xalancbmk. 122a4ccff32SPeter Collingbourne 123a4ccff32SPeter CollingbourneNote that this scheme has not yet been optimized for binary size; an increase 124a4ccff32SPeter Collingbourneof up to 15% has been observed for Chromium. 125a4ccff32SPeter Collingbourne 126d2926c91SPeter CollingbourneBad Cast Checking 1279eda6404SAlexey Samsonov================= 128d2926c91SPeter Collingbourne 129d2926c91SPeter CollingbourneThis scheme checks that pointer casts are made to an object of the correct 130d2926c91SPeter Collingbournedynamic type; that is, the dynamic type of the object must be a derived class 131d2926c91SPeter Collingbourneof the pointee type of the cast. The checks are currently only introduced 132d2926c91SPeter Collingbournewhere the class being casted to is a polymorphic class. 133d2926c91SPeter Collingbourne 134d2926c91SPeter CollingbourneBad casts are not in themselves control flow integrity violations, but they 135d2926c91SPeter Collingbournecan also create security vulnerabilities, and the implementation uses many 136d2926c91SPeter Collingbourneof the same mechanisms. 137d2926c91SPeter Collingbourne 138d2926c91SPeter CollingbourneThere are two types of bad cast that may be forbidden: bad casts 139d2926c91SPeter Collingbournefrom a base class to a derived class (which can be checked with 140d2926c91SPeter Collingbourne``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of 141d2926c91SPeter Collingbournetype ``void*`` or another unrelated type (which can be checked with 142d2926c91SPeter Collingbourne``-fsanitize=cfi-unrelated-cast``). 143d2926c91SPeter Collingbourne 144d2926c91SPeter CollingbourneThe difference between these two types of casts is that the first is defined 145d2926c91SPeter Collingbourneby the C++ standard to produce an undefined value, while the second is not 146d2926c91SPeter Collingbournein itself undefined behavior (it is well defined to cast the pointer back 147c8620dfdSPeter Collingbourneto its original type) unless the object is uninitialized and the cast is a 148c8620dfdSPeter Collingbourne``static_cast`` (see C++14 [basic.life]p5). 149d2926c91SPeter Collingbourne 150d2926c91SPeter CollingbourneIf a program as a matter of policy forbids the second type of cast, that 151d2926c91SPeter Collingbournerestriction can normally be enforced. However it may in some cases be necessary 152d2926c91SPeter Collingbournefor a function to perform a forbidden cast to conform with an external API 153d2926c91SPeter Collingbourne(e.g. the ``allocate`` member function of a standard library allocator). Such 154d7ec48d7SNico Weberfunctions may be :ref:`ignored <cfi-ignorelist>`. 155d2926c91SPeter Collingbourne 156d2926c91SPeter CollingbourneFor this scheme to work, all translation units containing the definition 1576fccf95aSPeter Collingbourneof a virtual member function (whether inline or not), other than members 158d7ec48d7SNico Weberof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 159282ad770SPeter Collingbournevisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 160282ad770SPeter Collingbourneenabled and be statically linked into the program. 161d2926c91SPeter Collingbourne 1621a7488afSPeter CollingbourneNon-Virtual Member Function Call Checking 1639eda6404SAlexey Samsonov========================================= 1641a7488afSPeter Collingbourne 1651a7488afSPeter CollingbourneThis scheme checks that non-virtual calls take place using an object of 1661a7488afSPeter Collingbournethe correct dynamic type; that is, the dynamic type of the called object 1671a7488afSPeter Collingbournemust be a derived class of the static type of the object used to make the 1681a7488afSPeter Collingbournecall. The checks are currently only introduced where the object is of a 1691a7488afSPeter Collingbournepolymorphic class type. This CFI scheme can be enabled on its own using 1701a7488afSPeter Collingbourne``-fsanitize=cfi-nvcall``. 1711a7488afSPeter Collingbourne 1721a7488afSPeter CollingbourneFor this scheme to work, all translation units containing the definition 1736fccf95aSPeter Collingbourneof a virtual member function (whether inline or not), other than members 174d7ec48d7SNico Weberof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 175282ad770SPeter Collingbournevisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 176282ad770SPeter Collingbourneenabled and be statically linked into the program. 1771a7488afSPeter Collingbourne 178d2926c91SPeter Collingbourne.. _cfi-strictness: 179d2926c91SPeter Collingbourne 180d2926c91SPeter CollingbourneStrictness 1819eda6404SAlexey Samsonov---------- 182d2926c91SPeter Collingbourne 183d2926c91SPeter CollingbourneIf a class has a single non-virtual base and does not introduce or override 184d2926c91SPeter Collingbournevirtual member functions or fields other than an implicitly defined virtual 185d2926c91SPeter Collingbournedestructor, it will have the same layout and virtual function semantics as 186d2926c91SPeter Collingbourneits base. By default, casts to such classes are checked as if they were made 187d2926c91SPeter Collingbourneto the least derived such class. 188d2926c91SPeter Collingbourne 189d2926c91SPeter CollingbourneCasting an instance of a base class to such a derived class is technically 190d2926c91SPeter Collingbourneundefined behavior, but it is a relatively common hack for introducing 191d2926c91SPeter Collingbournemember functions on class instances with specific properties that works under 192d2926c91SPeter Collingbournemost compilers and should not have security implications, so we allow it by 193d2926c91SPeter Collingbournedefault. It can be disabled with ``-fsanitize=cfi-cast-strict``. 194d2926c91SPeter Collingbourne 1952c7f7e31SPeter CollingbourneIndirect Function Call Checking 1969eda6404SAlexey Samsonov=============================== 1972c7f7e31SPeter Collingbourne 1982c7f7e31SPeter CollingbourneThis scheme checks that function calls take place using a function of the 1992c7f7e31SPeter Collingbournecorrect dynamic type; that is, the dynamic type of the function must match 2002c7f7e31SPeter Collingbournethe static type used at the call. This CFI scheme can be enabled on its own 2012c7f7e31SPeter Collingbourneusing ``-fsanitize=cfi-icall``. 2022c7f7e31SPeter Collingbourne 2032c7f7e31SPeter CollingbourneFor this scheme to work, each indirect function call in the program, other 204d7ec48d7SNico Weberthan calls in :ref:`ignored <cfi-ignorelist>` functions, must call a 2052c7f7e31SPeter Collingbournefunction which was either compiled with ``-fsanitize=cfi-icall`` enabled, 2062c7f7e31SPeter Collingbourneor whose address was taken by a function in a translation unit compiled with 2072c7f7e31SPeter Collingbourne``-fsanitize=cfi-icall``. 2082c7f7e31SPeter Collingbourne 2092c7f7e31SPeter CollingbourneIf a function in a translation unit compiled with ``-fsanitize=cfi-icall`` 2102c7f7e31SPeter Collingbournetakes the address of a function not compiled with ``-fsanitize=cfi-icall``, 2112c7f7e31SPeter Collingbournethat address may differ from the address taken by a function in a translation 2122c7f7e31SPeter Collingbourneunit not compiled with ``-fsanitize=cfi-icall``. This is technically a 2132c7f7e31SPeter Collingbourneviolation of the C and C++ standards, but it should not affect most programs. 2142c7f7e31SPeter Collingbourne 2152c7f7e31SPeter CollingbourneEach translation unit compiled with ``-fsanitize=cfi-icall`` must be 2162c7f7e31SPeter Collingbournestatically linked into the program or shared library, and calls across 2172c7f7e31SPeter Collingbourneshared library boundaries are handled as if the callee was not compiled with 2182c7f7e31SPeter Collingbourne``-fsanitize=cfi-icall``. 2192c7f7e31SPeter Collingbourne 22066cf68edSEvgenii StepanovThis scheme is currently supported on a limited set of targets: x86, 22166cf68edSEvgenii Stepanovx86_64, arm, arch64 and wasm. 2222c7f7e31SPeter Collingbourne 223634c601fSVlad Tsyrklevich``-fsanitize-cfi-icall-generalize-pointers`` 224634c601fSVlad Tsyrklevich-------------------------------------------- 225634c601fSVlad Tsyrklevich 226634c601fSVlad TsyrklevichMismatched pointer types are a common cause of cfi-icall check failures. 227634c601fSVlad TsyrklevichTranslation units compiled with the ``-fsanitize-cfi-icall-generalize-pointers`` 228634c601fSVlad Tsyrklevichflag relax pointer type checking for call sites in that translation unit, 229634c601fSVlad Tsyrklevichapplied across all functions compiled with ``-fsanitize=cfi-icall``. 230634c601fSVlad Tsyrklevich 231634c601fSVlad TsyrklevichSpecifically, pointers in return and argument types are treated as equivalent as 2328b74db9cSVlad Tsyrklevichlong as the qualifiers for the type they point to match. For example, ``char*``, 2338b74db9cSVlad Tsyrklevich``char**``, and ``int*`` are considered equivalent types. However, ``char*`` and 234634c601fSVlad Tsyrklevich``const char*`` are considered separate types. 235634c601fSVlad Tsyrklevich 236634c601fSVlad Tsyrklevich``-fsanitize-cfi-icall-generalize-pointers`` is not compatible with 237634c601fSVlad Tsyrklevich``-fsanitize-cfi-cross-dso``. 238634c601fSVlad Tsyrklevich 23971c7313fSRamon de C Valle.. _cfi-icall-experimental-normalize-integers: 24071c7313fSRamon de C Valle 24171c7313fSRamon de C Valle``-fsanitize-cfi-icall-experimental-normalize-integers`` 24271c7313fSRamon de C Valle-------------------------------------------------------- 24371c7313fSRamon de C Valle 24471c7313fSRamon de C ValleThis option enables normalizing integer types as vendor extended types for 24571c7313fSRamon de C Vallecross-language LLVM CFI/KCFI support with other languages that can't represent 24671c7313fSRamon de C Valleand encode C/C++ integer types. 24771c7313fSRamon de C Valle 24871c7313fSRamon de C ValleSpecifically, integer types are encoded as their defined representations (e.g., 24971c7313fSRamon de C Valle8-bit signed integer, 16-bit signed integer, 32-bit signed integer, ...) for 25071c7313fSRamon de C Vallecompatibility with languages that define explicitly-sized integer types (e.g., 25171c7313fSRamon de C Vallei8, i16, i32, ..., in Rust). 25271c7313fSRamon de C Valle 25371c7313fSRamon de C Valle``-fsanitize-cfi-icall-experimental-normalize-integers`` is compatible with 25471c7313fSRamon de C Valle``-fsanitize-cfi-icall-generalize-pointers``. 25571c7313fSRamon de C Valle 25671c7313fSRamon de C ValleThis option is currently experimental. 25771c7313fSRamon de C Valle 2580e497d15SPeter Collingbourne.. _cfi-canonical-jump-tables: 2590e497d15SPeter Collingbourne 2600e497d15SPeter Collingbourne``-fsanitize-cfi-canonical-jump-tables`` 2610e497d15SPeter Collingbourne---------------------------------------- 2620e497d15SPeter Collingbourne 2630e497d15SPeter CollingbourneThe default behavior of Clang's indirect function call checker will replace 2640e497d15SPeter Collingbournethe address of each CFI-checked function in the output file's symbol table 2650e497d15SPeter Collingbournewith the address of a jump table entry which will pass CFI checks. We refer 2660e497d15SPeter Collingbourneto this as making the jump table `canonical`. This property allows code that 2670e497d15SPeter Collingbournewas not compiled with ``-fsanitize=cfi-icall`` to take a CFI-valid address 2680e497d15SPeter Collingbourneof a function, but it comes with a couple of caveats that are especially 2690e497d15SPeter Collingbournerelevant for users of cross-DSO CFI: 2700e497d15SPeter Collingbourne 2710e497d15SPeter Collingbourne- There is a performance and code size overhead associated with each 2720e497d15SPeter Collingbourne exported function, because each such function must have an associated 2730e497d15SPeter Collingbourne jump table entry, which must be emitted even in the common case where the 2740e497d15SPeter Collingbourne function is never address-taken anywhere in the program, and must be used 2750e497d15SPeter Collingbourne even for direct calls between DSOs, in addition to the PLT overhead. 2760e497d15SPeter Collingbourne 2770e497d15SPeter Collingbourne- There is no good way to take a CFI-valid address of a function written in 2780e497d15SPeter Collingbourne assembly or a language not supported by Clang. The reason is that the code 2790e497d15SPeter Collingbourne generator would need to insert a jump table in order to form a CFI-valid 2800e497d15SPeter Collingbourne address for assembly functions, but there is no way in general for the 2810e497d15SPeter Collingbourne code generator to determine the language of the function. This may be 2820e497d15SPeter Collingbourne possible with LTO in the intra-DSO case, but in the cross-DSO case the only 2830e497d15SPeter Collingbourne information available is the function declaration. One possible solution 2840e497d15SPeter Collingbourne is to add a C wrapper for each assembly function, but these wrappers can 2850e497d15SPeter Collingbourne present a significant maintenance burden for heavy users of assembly in 2860e497d15SPeter Collingbourne addition to adding runtime overhead. 2870e497d15SPeter Collingbourne 2880e497d15SPeter CollingbourneFor these reasons, we provide the option of making the jump table non-canonical 2890e497d15SPeter Collingbournewith the flag ``-fno-sanitize-cfi-canonical-jump-tables``. When the jump 2900e497d15SPeter Collingbournetable is made non-canonical, symbol table entries point directly to the 2910e497d15SPeter Collingbournefunction body. Any instances of a function's address being taken in C will 2920e497d15SPeter Collingbournebe replaced with a jump table address. 2930e497d15SPeter Collingbourne 2940e497d15SPeter CollingbourneThis scheme does have its own caveats, however. It does end up breaking 2950e497d15SPeter Collingbournefunction address equality more aggressively than the default behavior, 2960e497d15SPeter Collingbourneespecially in cross-DSO mode which normally preserves function address 2970e497d15SPeter Collingbourneequality entirely. 2980e497d15SPeter Collingbourne 2990e497d15SPeter CollingbourneFurthermore, it is occasionally necessary for code not compiled with 3000e497d15SPeter Collingbourne``-fsanitize=cfi-icall`` to take a function address that is valid 3010e497d15SPeter Collingbournefor CFI. For example, this is necessary when a function's address 3020e497d15SPeter Collingbourneis taken by assembly code and then called by CFI-checking C code. The 3030e497d15SPeter Collingbourne``__attribute__((cfi_canonical_jump_table))`` attribute may be used to make 3040e497d15SPeter Collingbournethe jump table entry of a specific function canonical so that the external 30531443f8eSKazu Hiratacode will end up taking an address for the function that will pass CFI checks. 306634c601fSVlad Tsyrklevich 3072c7f7e31SPeter Collingbourne``-fsanitize=cfi-icall`` and ``-fsanitize=function`` 3089eda6404SAlexey Samsonov---------------------------------------------------- 3092c7f7e31SPeter Collingbourne 3102c7f7e31SPeter CollingbourneThis tool is similar to ``-fsanitize=function`` in that both tools check 3112c7f7e31SPeter Collingbournethe types of function calls. However, the two tools occupy different points 3122c7f7e31SPeter Collingbourneon the design space; ``-fsanitize=function`` is a developer tool designed 3132c7f7e31SPeter Collingbourneto find bugs in local development builds, whereas ``-fsanitize=cfi-icall`` 3142c7f7e31SPeter Collingbourneis a security hardening mechanism designed to be deployed in release builds. 3152c7f7e31SPeter Collingbourne 3162c7f7e31SPeter Collingbourne``-fsanitize=function`` has a higher space and time overhead due to a more 317*279a4d0dSFangrui Songcomplex type check at indirect call sites, which may make it unsuitable for 318*279a4d0dSFangrui Songdeployment. 3192c7f7e31SPeter Collingbourne 3202c7f7e31SPeter CollingbourneOn the other hand, ``-fsanitize=function`` conforms more closely with the C++ 3212c7f7e31SPeter Collingbournestandard and user expectations around interaction with shared libraries; 3222c7f7e31SPeter Collingbournethe identity of function pointers is maintained, and calls across shared 3232c7f7e31SPeter Collingbournelibrary boundaries are no different from calls within a single program or 3242c7f7e31SPeter Collingbourneshared library. 3252c7f7e31SPeter Collingbourne 326cff5bef9SSami Tolvanen.. _kcfi: 327cff5bef9SSami Tolvanen 328cff5bef9SSami Tolvanen``-fsanitize=kcfi`` 329cff5bef9SSami Tolvanen------------------- 330cff5bef9SSami Tolvanen 331cff5bef9SSami TolvanenThis is an alternative indirect call control-flow integrity scheme designed 332cff5bef9SSami Tolvanenfor low-level system software, such as operating system kernels. Unlike 333cff5bef9SSami Tolvanen``-fsanitize=cfi-icall``, it doesn't require ``-flto``, won't result in 334cff5bef9SSami Tolvanenfunction pointers being replaced with jump table references, and never breaks 335cff5bef9SSami Tolvanencross-DSO function address equality. These properties make KCFI easier to 336cff5bef9SSami Tolvanenadopt in low-level software. KCFI is limited to checking only function 337cff5bef9SSami Tolvanenpointers, and isn't compatible with executable-only memory. 338cff5bef9SSami Tolvanen 339e44acadfSPeter CollingbourneMember Function Pointer Call Checking 340e44acadfSPeter Collingbourne===================================== 341e44acadfSPeter Collingbourne 342e44acadfSPeter CollingbourneThis scheme checks that indirect calls via a member function pointer 343e44acadfSPeter Collingbournetake place using an object of the correct dynamic type. Specifically, we 344e44acadfSPeter Collingbournecheck that the dynamic type of the member function referenced by the member 345e44acadfSPeter Collingbournefunction pointer matches the "function pointer" part of the member function 346e44acadfSPeter Collingbournepointer, and that the member function's class type is related to the base 347e44acadfSPeter Collingbournetype of the member function. This CFI scheme can be enabled on its own using 348e44acadfSPeter Collingbourne``-fsanitize=cfi-mfcall``. 349e44acadfSPeter Collingbourne 350e44acadfSPeter CollingbourneThe compiler will only emit a full CFI check if the member function pointer's 351e44acadfSPeter Collingbournebase type is complete. This is because the complete definition of the base 352e44acadfSPeter Collingbournetype contains information that is necessary to correctly compile the CFI 353e44acadfSPeter Collingbournecheck. To ensure that the compiler always emits a full CFI check, it is 354e44acadfSPeter Collingbournerecommended to also pass the flag ``-fcomplete-member-pointers``, which 355e44acadfSPeter Collingbourneenables a non-conforming language extension that requires member pointer 356e44acadfSPeter Collingbournebase types to be complete if they may be used for a call. 357e44acadfSPeter Collingbourne 358e44acadfSPeter CollingbourneFor this scheme to work, all translation units containing the definition 359e44acadfSPeter Collingbourneof a virtual member function (whether inline or not), other than members 360d7ec48d7SNico Weberof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 361e44acadfSPeter Collingbournevisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 362e44acadfSPeter Collingbourneenabled and be statically linked into the program. 363e44acadfSPeter Collingbourne 364e44acadfSPeter CollingbourneThis scheme is currently not compatible with cross-DSO CFI or the 365e44acadfSPeter CollingbourneMicrosoft ABI. 366e44acadfSPeter Collingbourne 367d7ec48d7SNico Weber.. _cfi-ignorelist: 3686fccf95aSPeter Collingbourne 369d7ec48d7SNico WeberIgnorelist 370d7ec48d7SNico Weber========== 3716fccf95aSPeter Collingbourne 3726fccf95aSPeter CollingbourneA :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain 3736fccf95aSPeter Collingbournesource files, functions and types using the ``src``, ``fun`` and ``type`` 3742eccdab3SVlad Tsyrklevichentity types. Specific CFI modes can be be specified using ``[section]`` 3752eccdab3SVlad Tsyrklevichheaders. 3766fccf95aSPeter Collingbourne 3776fccf95aSPeter Collingbourne.. code-block:: bash 3786fccf95aSPeter Collingbourne 3792eccdab3SVlad Tsyrklevich # Suppress all CFI checking for code in a file. 3806fccf95aSPeter Collingbourne src:bad_file.cpp 3816fccf95aSPeter Collingbourne src:bad_header.h 3826fccf95aSPeter Collingbourne # Ignore all functions with names containing MyFooBar. 3836fccf95aSPeter Collingbourne fun:*MyFooBar* 3846fccf95aSPeter Collingbourne # Ignore all types in the standard library. 3856fccf95aSPeter Collingbourne type:std::* 3862eccdab3SVlad Tsyrklevich # Disable only unrelated cast checks for this function 3872eccdab3SVlad Tsyrklevich [cfi-unrelated-cast] 3882eccdab3SVlad Tsyrklevich fun:*UnrelatedCast* 3892eccdab3SVlad Tsyrklevich # Disable CFI call checks for this function without affecting cast checks 3902eccdab3SVlad Tsyrklevich [cfi-vcall|cfi-nvcall|cfi-icall] 3912eccdab3SVlad Tsyrklevich fun:*BadCall* 3922eccdab3SVlad Tsyrklevich 3936fccf95aSPeter Collingbourne 394fd6f92d5SEvgeniy Stepanov.. _cfi-cross-dso: 395fd6f92d5SEvgeniy Stepanov 396fd6f92d5SEvgeniy StepanovShared library support 397fd6f92d5SEvgeniy Stepanov====================== 398fd6f92d5SEvgeniy Stepanov 399fd6f92d5SEvgeniy StepanovUse **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control 400fd6f92d5SEvgeniy Stepanovflow integrity mode, which allows all CFI schemes listed above to 401fd6f92d5SEvgeniy Stepanovapply across DSO boundaries. As in the regular CFI, each DSO must be 40266cf68edSEvgenii Stepanovbuilt with ``-flto`` or ``-flto=thin``. 403fd6f92d5SEvgeniy Stepanov 4043afb2668SPeter CollingbourneNormally, CFI checks will only be performed for classes that have hidden LTO 4053afb2668SPeter Collingbournevisibility. With this flag enabled, the compiler will emit cross-DSO CFI 406d7ec48d7SNico Weberchecks for all classes, except for those which appear in the CFI ignorelist 4073afb2668SPeter Collingbourneor which use a ``no_sanitize`` attribute. 4083afb2668SPeter Collingbourne 409a4ccff32SPeter CollingbourneDesign 4109eda6404SAlexey Samsonov====== 411a4ccff32SPeter Collingbourne 412a4ccff32SPeter CollingbournePlease refer to the :doc:`design document<ControlFlowIntegrityDesign>`. 413a4ccff32SPeter Collingbourne 414a4ccff32SPeter CollingbournePublications 4159eda6404SAlexey Samsonov============ 416a4ccff32SPeter Collingbourne 417adcb3f52SEugene Zelenko`Control-Flow Integrity: Principles, Implementations, and Applications <https://research.microsoft.com/pubs/64250/ccs05.pdf>`_. 418a4ccff32SPeter CollingbourneMartin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti. 419a4ccff32SPeter Collingbourne 420a4ccff32SPeter Collingbourne`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_. 421a4ccff32SPeter CollingbourneCaroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway, 422a4ccff32SPeter CollingbourneÚlfar Erlingsson, Luis Lozano, Geoff Pike. 423