1e5dd7070Spatrick====================== 2e5dd7070SpatrickControl Flow Integrity 3e5dd7070Spatrick====================== 4e5dd7070Spatrick 5e5dd7070Spatrick.. toctree:: 6e5dd7070Spatrick :hidden: 7e5dd7070Spatrick 8e5dd7070Spatrick ControlFlowIntegrityDesign 9e5dd7070Spatrick 10e5dd7070Spatrick.. contents:: 11e5dd7070Spatrick :local: 12e5dd7070Spatrick 13e5dd7070SpatrickIntroduction 14e5dd7070Spatrick============ 15e5dd7070Spatrick 16e5dd7070SpatrickClang includes an implementation of a number of control flow integrity (CFI) 17e5dd7070Spatrickschemes, which are designed to abort the program upon detecting certain forms 18e5dd7070Spatrickof undefined behavior that can potentially allow attackers to subvert the 19e5dd7070Spatrickprogram's control flow. These schemes have been optimized for performance, 20e5dd7070Spatrickallowing developers to enable them in release builds. 21e5dd7070Spatrick 22e5dd7070SpatrickTo enable Clang's available CFI schemes, use the flag ``-fsanitize=cfi``. 23e5dd7070SpatrickYou can also enable a subset of available :ref:`schemes <cfi-schemes>`. 24e5dd7070SpatrickAs currently implemented, all schemes rely on link-time optimization (LTO); 25e5dd7070Spatrickso it is required to specify ``-flto``, and the linker used must support LTO, 26e5dd7070Spatrickfor example via the `gold plugin`_. 27e5dd7070Spatrick 28e5dd7070SpatrickTo allow the checks to be implemented efficiently, the program must 29e5dd7070Spatrickbe structured such that certain object files are compiled with CFI 30e5dd7070Spatrickenabled, and are statically linked into the program. This may preclude 31e5dd7070Spatrickthe use of shared libraries in some cases. 32e5dd7070Spatrick 33e5dd7070SpatrickThe compiler will only produce CFI checks for a class if it can infer hidden 34e5dd7070SpatrickLTO visibility for that class. LTO visibility is a property of a class that 35e5dd7070Spatrickis inferred from flags and attributes. For more details, see the documentation 36e5dd7070Spatrickfor :doc:`LTO visibility <LTOVisibility>`. 37e5dd7070Spatrick 38e5dd7070SpatrickThe ``-fsanitize=cfi-{vcall,nvcall,derived-cast,unrelated-cast}`` flags 39e5dd7070Spatrickrequire that a ``-fvisibility=`` flag also be specified. This is because the 40e5dd7070Spatrickdefault visibility setting is ``-fvisibility=default``, which would disable 41e5dd7070SpatrickCFI checks for classes without visibility attributes. Most users will want 42e5dd7070Spatrickto specify ``-fvisibility=hidden``, which enables CFI checks for such classes. 43e5dd7070Spatrick 44e5dd7070SpatrickExperimental support for :ref:`cross-DSO control flow integrity 45e5dd7070Spatrick<cfi-cross-dso>` exists that does not require classes to have hidden LTO 46e5dd7070Spatrickvisibility. This cross-DSO support has unstable ABI at this time. 47e5dd7070Spatrick 48e5dd7070Spatrick.. _gold plugin: https://llvm.org/docs/GoldPlugin.html 49e5dd7070Spatrick 50e5dd7070Spatrick.. _cfi-schemes: 51e5dd7070Spatrick 52e5dd7070SpatrickAvailable schemes 53e5dd7070Spatrick================= 54e5dd7070Spatrick 55e5dd7070SpatrickAvailable schemes are: 56e5dd7070Spatrick 57e5dd7070Spatrick - ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks 58e5dd7070Spatrick <cfi-strictness>`. 59e5dd7070Spatrick - ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong 60e5dd7070Spatrick dynamic type. 61e5dd7070Spatrick - ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another 62e5dd7070Spatrick unrelated type to the wrong dynamic type. 63e5dd7070Spatrick - ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of 64e5dd7070Spatrick the wrong dynamic type. 65e5dd7070Spatrick - ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the 66e5dd7070Spatrick wrong dynamic type. 67e5dd7070Spatrick - ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic 68e5dd7070Spatrick type. 69e5dd7070Spatrick - ``-fsanitize=cfi-mfcall``: Indirect call via a member function pointer with 70e5dd7070Spatrick wrong dynamic type. 71e5dd7070Spatrick 72e5dd7070SpatrickYou can use ``-fsanitize=cfi`` to enable all the schemes and use 73e5dd7070Spatrick``-fno-sanitize`` flag to narrow down the set of schemes as desired. 74e5dd7070SpatrickFor example, you can build your program with 75e5dd7070Spatrick``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall`` 76e5dd7070Spatrickto use all schemes except for non-virtual member function call and indirect call 77e5dd7070Spatrickchecking. 78e5dd7070Spatrick 79a9ac8606SpatrickRemember that you have to provide ``-flto`` or ``-flto=thin`` if at 80a9ac8606Spatrickleast one CFI scheme is enabled. 81e5dd7070Spatrick 82e5dd7070SpatrickTrapping and Diagnostics 83e5dd7070Spatrick======================== 84e5dd7070Spatrick 85e5dd7070SpatrickBy default, CFI will abort the program immediately upon detecting a control 86e5dd7070Spatrickflow integrity violation. You can use the :ref:`-fno-sanitize-trap= 87e5dd7070Spatrick<controlling-code-generation>` flag to cause CFI to print a diagnostic 88e5dd7070Spatricksimilar to the one below before the program aborts. 89e5dd7070Spatrick 90e5dd7070Spatrick.. code-block:: console 91e5dd7070Spatrick 92e5dd7070Spatrick bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50) 93e5dd7070Spatrick 0x000000425a50: note: vtable is of type 'A' 94e5dd7070Spatrick 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 95e5dd7070Spatrick ^ 96e5dd7070Spatrick 97e5dd7070SpatrickIf diagnostics are enabled, you can also configure CFI to continue program 98e5dd7070Spatrickexecution instead of aborting by using the :ref:`-fsanitize-recover= 99e5dd7070Spatrick<controlling-code-generation>` flag. 100e5dd7070Spatrick 101e5dd7070SpatrickForward-Edge CFI for Virtual Calls 102e5dd7070Spatrick================================== 103e5dd7070Spatrick 104e5dd7070SpatrickThis scheme checks that virtual calls take place using a vptr of the correct 105e5dd7070Spatrickdynamic type; that is, the dynamic type of the called object must be a 106e5dd7070Spatrickderived class of the static type of the object used to make the call. 107e5dd7070SpatrickThis CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``. 108e5dd7070Spatrick 109e5dd7070SpatrickFor this scheme to work, all translation units containing the definition 110e5dd7070Spatrickof a virtual member function (whether inline or not), other than members 111a9ac8606Spatrickof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 112e5dd7070Spatrickvisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 113e5dd7070Spatrickenabled and be statically linked into the program. 114e5dd7070Spatrick 115e5dd7070SpatrickPerformance 116e5dd7070Spatrick----------- 117e5dd7070Spatrick 118e5dd7070SpatrickA performance overhead of less than 1% has been measured by running the 119e5dd7070SpatrickDromaeo benchmark suite against an instrumented version of the Chromium 120e5dd7070Spatrickweb browser. Another good performance benchmark for this mechanism is the 121e5dd7070Spatrickvirtual-call-heavy SPEC 2006 xalancbmk. 122e5dd7070Spatrick 123e5dd7070SpatrickNote that this scheme has not yet been optimized for binary size; an increase 124e5dd7070Spatrickof up to 15% has been observed for Chromium. 125e5dd7070Spatrick 126e5dd7070SpatrickBad Cast Checking 127e5dd7070Spatrick================= 128e5dd7070Spatrick 129e5dd7070SpatrickThis scheme checks that pointer casts are made to an object of the correct 130e5dd7070Spatrickdynamic type; that is, the dynamic type of the object must be a derived class 131e5dd7070Spatrickof the pointee type of the cast. The checks are currently only introduced 132e5dd7070Spatrickwhere the class being casted to is a polymorphic class. 133e5dd7070Spatrick 134e5dd7070SpatrickBad casts are not in themselves control flow integrity violations, but they 135e5dd7070Spatrickcan also create security vulnerabilities, and the implementation uses many 136e5dd7070Spatrickof the same mechanisms. 137e5dd7070Spatrick 138e5dd7070SpatrickThere are two types of bad cast that may be forbidden: bad casts 139e5dd7070Spatrickfrom a base class to a derived class (which can be checked with 140e5dd7070Spatrick``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of 141e5dd7070Spatricktype ``void*`` or another unrelated type (which can be checked with 142e5dd7070Spatrick``-fsanitize=cfi-unrelated-cast``). 143e5dd7070Spatrick 144e5dd7070SpatrickThe difference between these two types of casts is that the first is defined 145e5dd7070Spatrickby the C++ standard to produce an undefined value, while the second is not 146e5dd7070Spatrickin itself undefined behavior (it is well defined to cast the pointer back 147e5dd7070Spatrickto its original type) unless the object is uninitialized and the cast is a 148e5dd7070Spatrick``static_cast`` (see C++14 [basic.life]p5). 149e5dd7070Spatrick 150e5dd7070SpatrickIf a program as a matter of policy forbids the second type of cast, that 151e5dd7070Spatrickrestriction can normally be enforced. However it may in some cases be necessary 152e5dd7070Spatrickfor a function to perform a forbidden cast to conform with an external API 153e5dd7070Spatrick(e.g. the ``allocate`` member function of a standard library allocator). Such 154a9ac8606Spatrickfunctions may be :ref:`ignored <cfi-ignorelist>`. 155e5dd7070Spatrick 156e5dd7070SpatrickFor this scheme to work, all translation units containing the definition 157e5dd7070Spatrickof a virtual member function (whether inline or not), other than members 158a9ac8606Spatrickof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 159e5dd7070Spatrickvisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 160e5dd7070Spatrickenabled and be statically linked into the program. 161e5dd7070Spatrick 162e5dd7070SpatrickNon-Virtual Member Function Call Checking 163e5dd7070Spatrick========================================= 164e5dd7070Spatrick 165e5dd7070SpatrickThis scheme checks that non-virtual calls take place using an object of 166e5dd7070Spatrickthe correct dynamic type; that is, the dynamic type of the called object 167e5dd7070Spatrickmust be a derived class of the static type of the object used to make the 168e5dd7070Spatrickcall. The checks are currently only introduced where the object is of a 169e5dd7070Spatrickpolymorphic class type. This CFI scheme can be enabled on its own using 170e5dd7070Spatrick``-fsanitize=cfi-nvcall``. 171e5dd7070Spatrick 172e5dd7070SpatrickFor this scheme to work, all translation units containing the definition 173e5dd7070Spatrickof a virtual member function (whether inline or not), other than members 174a9ac8606Spatrickof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 175e5dd7070Spatrickvisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 176e5dd7070Spatrickenabled and be statically linked into the program. 177e5dd7070Spatrick 178e5dd7070Spatrick.. _cfi-strictness: 179e5dd7070Spatrick 180e5dd7070SpatrickStrictness 181e5dd7070Spatrick---------- 182e5dd7070Spatrick 183e5dd7070SpatrickIf a class has a single non-virtual base and does not introduce or override 184e5dd7070Spatrickvirtual member functions or fields other than an implicitly defined virtual 185e5dd7070Spatrickdestructor, it will have the same layout and virtual function semantics as 186e5dd7070Spatrickits base. By default, casts to such classes are checked as if they were made 187e5dd7070Spatrickto the least derived such class. 188e5dd7070Spatrick 189e5dd7070SpatrickCasting an instance of a base class to such a derived class is technically 190e5dd7070Spatrickundefined behavior, but it is a relatively common hack for introducing 191e5dd7070Spatrickmember functions on class instances with specific properties that works under 192e5dd7070Spatrickmost compilers and should not have security implications, so we allow it by 193e5dd7070Spatrickdefault. It can be disabled with ``-fsanitize=cfi-cast-strict``. 194e5dd7070Spatrick 195e5dd7070SpatrickIndirect Function Call Checking 196e5dd7070Spatrick=============================== 197e5dd7070Spatrick 198e5dd7070SpatrickThis scheme checks that function calls take place using a function of the 199e5dd7070Spatrickcorrect dynamic type; that is, the dynamic type of the function must match 200e5dd7070Spatrickthe static type used at the call. This CFI scheme can be enabled on its own 201e5dd7070Spatrickusing ``-fsanitize=cfi-icall``. 202e5dd7070Spatrick 203e5dd7070SpatrickFor this scheme to work, each indirect function call in the program, other 204a9ac8606Spatrickthan calls in :ref:`ignored <cfi-ignorelist>` functions, must call a 205e5dd7070Spatrickfunction which was either compiled with ``-fsanitize=cfi-icall`` enabled, 206e5dd7070Spatrickor whose address was taken by a function in a translation unit compiled with 207e5dd7070Spatrick``-fsanitize=cfi-icall``. 208e5dd7070Spatrick 209e5dd7070SpatrickIf a function in a translation unit compiled with ``-fsanitize=cfi-icall`` 210e5dd7070Spatricktakes the address of a function not compiled with ``-fsanitize=cfi-icall``, 211e5dd7070Spatrickthat address may differ from the address taken by a function in a translation 212e5dd7070Spatrickunit not compiled with ``-fsanitize=cfi-icall``. This is technically a 213e5dd7070Spatrickviolation of the C and C++ standards, but it should not affect most programs. 214e5dd7070Spatrick 215e5dd7070SpatrickEach translation unit compiled with ``-fsanitize=cfi-icall`` must be 216e5dd7070Spatrickstatically linked into the program or shared library, and calls across 217e5dd7070Spatrickshared library boundaries are handled as if the callee was not compiled with 218e5dd7070Spatrick``-fsanitize=cfi-icall``. 219e5dd7070Spatrick 220a9ac8606SpatrickThis scheme is currently supported on a limited set of targets: x86, 221a9ac8606Spatrickx86_64, arm, arch64 and wasm. 222e5dd7070Spatrick 223e5dd7070Spatrick``-fsanitize-cfi-icall-generalize-pointers`` 224e5dd7070Spatrick-------------------------------------------- 225e5dd7070Spatrick 226e5dd7070SpatrickMismatched pointer types are a common cause of cfi-icall check failures. 227e5dd7070SpatrickTranslation units compiled with the ``-fsanitize-cfi-icall-generalize-pointers`` 228e5dd7070Spatrickflag relax pointer type checking for call sites in that translation unit, 229e5dd7070Spatrickapplied across all functions compiled with ``-fsanitize=cfi-icall``. 230e5dd7070Spatrick 231e5dd7070SpatrickSpecifically, pointers in return and argument types are treated as equivalent as 232e5dd7070Spatricklong as the qualifiers for the type they point to match. For example, ``char*``, 233e5dd7070Spatrick``char**``, and ``int*`` are considered equivalent types. However, ``char*`` and 234e5dd7070Spatrick``const char*`` are considered separate types. 235e5dd7070Spatrick 236e5dd7070Spatrick``-fsanitize-cfi-icall-generalize-pointers`` is not compatible with 237e5dd7070Spatrick``-fsanitize-cfi-cross-dso``. 238e5dd7070Spatrick 239e5dd7070Spatrick.. _cfi-canonical-jump-tables: 240e5dd7070Spatrick 241e5dd7070Spatrick``-fsanitize-cfi-canonical-jump-tables`` 242e5dd7070Spatrick---------------------------------------- 243e5dd7070Spatrick 244e5dd7070SpatrickThe default behavior of Clang's indirect function call checker will replace 245e5dd7070Spatrickthe address of each CFI-checked function in the output file's symbol table 246e5dd7070Spatrickwith the address of a jump table entry which will pass CFI checks. We refer 247e5dd7070Spatrickto this as making the jump table `canonical`. This property allows code that 248e5dd7070Spatrickwas not compiled with ``-fsanitize=cfi-icall`` to take a CFI-valid address 249e5dd7070Spatrickof a function, but it comes with a couple of caveats that are especially 250e5dd7070Spatrickrelevant for users of cross-DSO CFI: 251e5dd7070Spatrick 252e5dd7070Spatrick- There is a performance and code size overhead associated with each 253e5dd7070Spatrick exported function, because each such function must have an associated 254e5dd7070Spatrick jump table entry, which must be emitted even in the common case where the 255e5dd7070Spatrick function is never address-taken anywhere in the program, and must be used 256e5dd7070Spatrick even for direct calls between DSOs, in addition to the PLT overhead. 257e5dd7070Spatrick 258e5dd7070Spatrick- There is no good way to take a CFI-valid address of a function written in 259e5dd7070Spatrick assembly or a language not supported by Clang. The reason is that the code 260e5dd7070Spatrick generator would need to insert a jump table in order to form a CFI-valid 261e5dd7070Spatrick address for assembly functions, but there is no way in general for the 262e5dd7070Spatrick code generator to determine the language of the function. This may be 263e5dd7070Spatrick possible with LTO in the intra-DSO case, but in the cross-DSO case the only 264e5dd7070Spatrick information available is the function declaration. One possible solution 265e5dd7070Spatrick is to add a C wrapper for each assembly function, but these wrappers can 266e5dd7070Spatrick present a significant maintenance burden for heavy users of assembly in 267e5dd7070Spatrick addition to adding runtime overhead. 268e5dd7070Spatrick 269e5dd7070SpatrickFor these reasons, we provide the option of making the jump table non-canonical 270e5dd7070Spatrickwith the flag ``-fno-sanitize-cfi-canonical-jump-tables``. When the jump 271e5dd7070Spatricktable is made non-canonical, symbol table entries point directly to the 272e5dd7070Spatrickfunction body. Any instances of a function's address being taken in C will 273e5dd7070Spatrickbe replaced with a jump table address. 274e5dd7070Spatrick 275e5dd7070SpatrickThis scheme does have its own caveats, however. It does end up breaking 276e5dd7070Spatrickfunction address equality more aggressively than the default behavior, 277e5dd7070Spatrickespecially in cross-DSO mode which normally preserves function address 278e5dd7070Spatrickequality entirely. 279e5dd7070Spatrick 280e5dd7070SpatrickFurthermore, it is occasionally necessary for code not compiled with 281e5dd7070Spatrick``-fsanitize=cfi-icall`` to take a function address that is valid 282e5dd7070Spatrickfor CFI. For example, this is necessary when a function's address 283e5dd7070Spatrickis taken by assembly code and then called by CFI-checking C code. The 284e5dd7070Spatrick``__attribute__((cfi_canonical_jump_table))`` attribute may be used to make 285e5dd7070Spatrickthe jump table entry of a specific function canonical so that the external 286a9ac8606Spatrickcode will end up taking an address for the function that will pass CFI checks. 287e5dd7070Spatrick 288e5dd7070Spatrick``-fsanitize=cfi-icall`` and ``-fsanitize=function`` 289e5dd7070Spatrick---------------------------------------------------- 290e5dd7070Spatrick 291e5dd7070SpatrickThis tool is similar to ``-fsanitize=function`` in that both tools check 292e5dd7070Spatrickthe types of function calls. However, the two tools occupy different points 293e5dd7070Spatrickon the design space; ``-fsanitize=function`` is a developer tool designed 294e5dd7070Spatrickto find bugs in local development builds, whereas ``-fsanitize=cfi-icall`` 295e5dd7070Spatrickis a security hardening mechanism designed to be deployed in release builds. 296e5dd7070Spatrick 297e5dd7070Spatrick``-fsanitize=function`` has a higher space and time overhead due to a more 298e5dd7070Spatrickcomplex type check at indirect call sites, as well as a need for run-time 299e5dd7070Spatricktype information (RTTI), which may make it unsuitable for deployment. Because 300e5dd7070Spatrickof the need for RTTI, ``-fsanitize=function`` can only be used with C++ 301e5dd7070Spatrickprograms, whereas ``-fsanitize=cfi-icall`` can protect both C and C++ programs. 302e5dd7070Spatrick 303e5dd7070SpatrickOn the other hand, ``-fsanitize=function`` conforms more closely with the C++ 304e5dd7070Spatrickstandard and user expectations around interaction with shared libraries; 305e5dd7070Spatrickthe identity of function pointers is maintained, and calls across shared 306e5dd7070Spatricklibrary boundaries are no different from calls within a single program or 307e5dd7070Spatrickshared library. 308*12c85518Srobert 309*12c85518Srobert.. _kcfi: 310*12c85518Srobert 311*12c85518Srobert``-fsanitize=kcfi`` 312*12c85518Srobert------------------- 313*12c85518Srobert 314*12c85518SrobertThis is an alternative indirect call control-flow integrity scheme designed 315*12c85518Srobertfor low-level system software, such as operating system kernels. Unlike 316*12c85518Srobert``-fsanitize=cfi-icall``, it doesn't require ``-flto``, won't result in 317*12c85518Srobertfunction pointers being replaced with jump table references, and never breaks 318*12c85518Srobertcross-DSO function address equality. These properties make KCFI easier to 319*12c85518Srobertadopt in low-level software. KCFI is limited to checking only function 320*12c85518Srobertpointers, and isn't compatible with executable-only memory. 321e5dd7070Spatrick 322e5dd7070SpatrickMember Function Pointer Call Checking 323e5dd7070Spatrick===================================== 324e5dd7070Spatrick 325e5dd7070SpatrickThis scheme checks that indirect calls via a member function pointer 326e5dd7070Spatricktake place using an object of the correct dynamic type. Specifically, we 327e5dd7070Spatrickcheck that the dynamic type of the member function referenced by the member 328e5dd7070Spatrickfunction pointer matches the "function pointer" part of the member function 329e5dd7070Spatrickpointer, and that the member function's class type is related to the base 330e5dd7070Spatricktype of the member function. This CFI scheme can be enabled on its own using 331e5dd7070Spatrick``-fsanitize=cfi-mfcall``. 332e5dd7070Spatrick 333e5dd7070SpatrickThe compiler will only emit a full CFI check if the member function pointer's 334e5dd7070Spatrickbase type is complete. This is because the complete definition of the base 335e5dd7070Spatricktype contains information that is necessary to correctly compile the CFI 336e5dd7070Spatrickcheck. To ensure that the compiler always emits a full CFI check, it is 337e5dd7070Spatrickrecommended to also pass the flag ``-fcomplete-member-pointers``, which 338e5dd7070Spatrickenables a non-conforming language extension that requires member pointer 339e5dd7070Spatrickbase types to be complete if they may be used for a call. 340e5dd7070Spatrick 341e5dd7070SpatrickFor this scheme to work, all translation units containing the definition 342e5dd7070Spatrickof a virtual member function (whether inline or not), other than members 343a9ac8606Spatrickof :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO 344e5dd7070Spatrickvisibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` 345e5dd7070Spatrickenabled and be statically linked into the program. 346e5dd7070Spatrick 347e5dd7070SpatrickThis scheme is currently not compatible with cross-DSO CFI or the 348e5dd7070SpatrickMicrosoft ABI. 349e5dd7070Spatrick 350a9ac8606Spatrick.. _cfi-ignorelist: 351e5dd7070Spatrick 352a9ac8606SpatrickIgnorelist 353a9ac8606Spatrick========== 354e5dd7070Spatrick 355e5dd7070SpatrickA :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain 356e5dd7070Spatricksource files, functions and types using the ``src``, ``fun`` and ``type`` 357e5dd7070Spatrickentity types. Specific CFI modes can be be specified using ``[section]`` 358e5dd7070Spatrickheaders. 359e5dd7070Spatrick 360e5dd7070Spatrick.. code-block:: bash 361e5dd7070Spatrick 362e5dd7070Spatrick # Suppress all CFI checking for code in a file. 363e5dd7070Spatrick src:bad_file.cpp 364e5dd7070Spatrick src:bad_header.h 365e5dd7070Spatrick # Ignore all functions with names containing MyFooBar. 366e5dd7070Spatrick fun:*MyFooBar* 367e5dd7070Spatrick # Ignore all types in the standard library. 368e5dd7070Spatrick type:std::* 369e5dd7070Spatrick # Disable only unrelated cast checks for this function 370e5dd7070Spatrick [cfi-unrelated-cast] 371e5dd7070Spatrick fun:*UnrelatedCast* 372e5dd7070Spatrick # Disable CFI call checks for this function without affecting cast checks 373e5dd7070Spatrick [cfi-vcall|cfi-nvcall|cfi-icall] 374e5dd7070Spatrick fun:*BadCall* 375e5dd7070Spatrick 376e5dd7070Spatrick 377e5dd7070Spatrick.. _cfi-cross-dso: 378e5dd7070Spatrick 379e5dd7070SpatrickShared library support 380e5dd7070Spatrick====================== 381e5dd7070Spatrick 382e5dd7070SpatrickUse **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control 383e5dd7070Spatrickflow integrity mode, which allows all CFI schemes listed above to 384e5dd7070Spatrickapply across DSO boundaries. As in the regular CFI, each DSO must be 385a9ac8606Spatrickbuilt with ``-flto`` or ``-flto=thin``. 386e5dd7070Spatrick 387e5dd7070SpatrickNormally, CFI checks will only be performed for classes that have hidden LTO 388e5dd7070Spatrickvisibility. With this flag enabled, the compiler will emit cross-DSO CFI 389a9ac8606Spatrickchecks for all classes, except for those which appear in the CFI ignorelist 390e5dd7070Spatrickor which use a ``no_sanitize`` attribute. 391e5dd7070Spatrick 392e5dd7070SpatrickDesign 393e5dd7070Spatrick====== 394e5dd7070Spatrick 395e5dd7070SpatrickPlease refer to the :doc:`design document<ControlFlowIntegrityDesign>`. 396e5dd7070Spatrick 397e5dd7070SpatrickPublications 398e5dd7070Spatrick============ 399e5dd7070Spatrick 400e5dd7070Spatrick`Control-Flow Integrity: Principles, Implementations, and Applications <https://research.microsoft.com/pubs/64250/ccs05.pdf>`_. 401e5dd7070SpatrickMartin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti. 402e5dd7070Spatrick 403e5dd7070Spatrick`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_. 404e5dd7070SpatrickCaroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway, 405e5dd7070SpatrickÚlfar Erlingsson, Luis Lozano, Geoff Pike. 406