ControlFlowIntegrity.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. ======================
  2. Control Flow Integrity
  3. ======================
  4. .. toctree::
  5. :hidden:
  6. ControlFlowIntegrityDesign
  7. .. contents::
  8. :local:
  9. Introduction
  10. ============
  11. Clang includes an implementation of a number of control flow integrity (CFI)
  12. schemes, which are designed to abort the program upon detecting certain forms
  13. of undefined behavior that can potentially allow attackers to subvert the
  14. program's control flow. These schemes have been optimized for performance,
  15. allowing developers to enable them in release builds.
  16. To enable Clang's available CFI schemes, use the flag ``-fsanitize=cfi``.
  17. You can also enable a subset of available :ref:`schemes <cfi-schemes>`.
  18. As currently implemented, all schemes rely on link-time optimization (LTO);
  19. so it is required to specify ``-flto``, and the linker used must support LTO,
  20. for example via the `gold plugin`_.
  21. To allow the checks to be implemented efficiently, the program must
  22. be structured such that certain object files are compiled with CFI
  23. enabled, and are statically linked into the program. This may preclude
  24. the use of shared libraries in some cases.
  25. The compiler will only produce CFI checks for a class if it can infer hidden
  26. LTO visibility for that class. LTO visibility is a property of a class that
  27. is inferred from flags and attributes. For more details, see the documentation
  28. for :doc:`LTO visibility <LTOVisibility>`.
  29. The ``-fsanitize=cfi-{vcall,nvcall,derived-cast,unrelated-cast}`` flags
  30. require that a ``-fvisibility=`` flag also be specified. This is because the
  31. default visibility setting is ``-fvisibility=default``, which would disable
  32. CFI checks for classes without visibility attributes. Most users will want
  33. to specify ``-fvisibility=hidden``, which enables CFI checks for such classes.
  34. Experimental support for :ref:`cross-DSO control flow integrity
  35. <cfi-cross-dso>` exists that does not require classes to have hidden LTO
  36. visibility. This cross-DSO support has unstable ABI at this time.
  37. .. _gold plugin: https://llvm.org/docs/GoldPlugin.html
  38. .. _cfi-schemes:
  39. Available schemes
  40. =================
  41. Available schemes are:
  42. - ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks
  43. <cfi-strictness>`.
  44. - ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong
  45. dynamic type.
  46. - ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another
  47. unrelated type to the wrong dynamic type.
  48. - ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of
  49. the wrong dynamic type.
  50. - ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the
  51. wrong dynamic type.
  52. - ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic
  53. type.
  54. - ``-fsanitize=cfi-mfcall``: Indirect call via a member function pointer with
  55. wrong dynamic type.
  56. You can use ``-fsanitize=cfi`` to enable all the schemes and use
  57. ``-fno-sanitize`` flag to narrow down the set of schemes as desired.
  58. For example, you can build your program with
  59. ``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall``
  60. to use all schemes except for non-virtual member function call and indirect call
  61. checking.
  62. Remember that you have to provide ``-flto`` if at least one CFI scheme is
  63. enabled.
  64. Trapping and Diagnostics
  65. ========================
  66. By default, CFI will abort the program immediately upon detecting a control
  67. flow integrity violation. You can use the :ref:`-fno-sanitize-trap=
  68. <controlling-code-generation>` flag to cause CFI to print a diagnostic
  69. similar to the one below before the program aborts.
  70. .. code-block:: console
  71. bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50)
  72. 0x000000425a50: note: vtable is of type 'A'
  73. 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
  74. ^
  75. If diagnostics are enabled, you can also configure CFI to continue program
  76. execution instead of aborting by using the :ref:`-fsanitize-recover=
  77. <controlling-code-generation>` flag.
  78. Forward-Edge CFI for Virtual Calls
  79. ==================================
  80. This scheme checks that virtual calls take place using a vptr of the correct
  81. dynamic type; that is, the dynamic type of the called object must be a
  82. derived class of the static type of the object used to make the call.
  83. This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``.
  84. For this scheme to work, all translation units containing the definition
  85. of a virtual member function (whether inline or not), other than members
  86. of :ref:`blacklisted <cfi-blacklist>` types or types with public :doc:`LTO
  87. visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin``
  88. enabled and be statically linked into the program.
  89. Performance
  90. -----------
  91. A performance overhead of less than 1% has been measured by running the
  92. Dromaeo benchmark suite against an instrumented version of the Chromium
  93. web browser. Another good performance benchmark for this mechanism is the
  94. virtual-call-heavy SPEC 2006 xalancbmk.
  95. Note that this scheme has not yet been optimized for binary size; an increase
  96. of up to 15% has been observed for Chromium.
  97. Bad Cast Checking
  98. =================
  99. This scheme checks that pointer casts are made to an object of the correct
  100. dynamic type; that is, the dynamic type of the object must be a derived class
  101. of the pointee type of the cast. The checks are currently only introduced
  102. where the class being casted to is a polymorphic class.
  103. Bad casts are not in themselves control flow integrity violations, but they
  104. can also create security vulnerabilities, and the implementation uses many
  105. of the same mechanisms.
  106. There are two types of bad cast that may be forbidden: bad casts
  107. from a base class to a derived class (which can be checked with
  108. ``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of
  109. type ``void*`` or another unrelated type (which can be checked with
  110. ``-fsanitize=cfi-unrelated-cast``).
  111. The difference between these two types of casts is that the first is defined
  112. by the C++ standard to produce an undefined value, while the second is not
  113. in itself undefined behavior (it is well defined to cast the pointer back
  114. to its original type) unless the object is uninitialized and the cast is a
  115. ``static_cast`` (see C++14 [basic.life]p5).
  116. If a program as a matter of policy forbids the second type of cast, that
  117. restriction can normally be enforced. However it may in some cases be necessary
  118. for a function to perform a forbidden cast to conform with an external API
  119. (e.g. the ``allocate`` member function of a standard library allocator). Such
  120. functions may be :ref:`blacklisted <cfi-blacklist>`.
  121. For this scheme to work, all translation units containing the definition
  122. of a virtual member function (whether inline or not), other than members
  123. of :ref:`blacklisted <cfi-blacklist>` types or types with public :doc:`LTO
  124. visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin``
  125. enabled and be statically linked into the program.
  126. Non-Virtual Member Function Call Checking
  127. =========================================
  128. This scheme checks that non-virtual calls take place using an object of
  129. the correct dynamic type; that is, the dynamic type of the called object
  130. must be a derived class of the static type of the object used to make the
  131. call. The checks are currently only introduced where the object is of a
  132. polymorphic class type. This CFI scheme can be enabled on its own using
  133. ``-fsanitize=cfi-nvcall``.
  134. For this scheme to work, all translation units containing the definition
  135. of a virtual member function (whether inline or not), other than members
  136. of :ref:`blacklisted <cfi-blacklist>` types or types with public :doc:`LTO
  137. visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin``
  138. enabled and be statically linked into the program.
  139. .. _cfi-strictness:
  140. Strictness
  141. ----------
  142. If a class has a single non-virtual base and does not introduce or override
  143. virtual member functions or fields other than an implicitly defined virtual
  144. destructor, it will have the same layout and virtual function semantics as
  145. its base. By default, casts to such classes are checked as if they were made
  146. to the least derived such class.
  147. Casting an instance of a base class to such a derived class is technically
  148. undefined behavior, but it is a relatively common hack for introducing
  149. member functions on class instances with specific properties that works under
  150. most compilers and should not have security implications, so we allow it by
  151. default. It can be disabled with ``-fsanitize=cfi-cast-strict``.
  152. Indirect Function Call Checking
  153. ===============================
  154. This scheme checks that function calls take place using a function of the
  155. correct dynamic type; that is, the dynamic type of the function must match
  156. the static type used at the call. This CFI scheme can be enabled on its own
  157. using ``-fsanitize=cfi-icall``.
  158. For this scheme to work, each indirect function call in the program, other
  159. than calls in :ref:`blacklisted <cfi-blacklist>` functions, must call a
  160. function which was either compiled with ``-fsanitize=cfi-icall`` enabled,
  161. or whose address was taken by a function in a translation unit compiled with
  162. ``-fsanitize=cfi-icall``.
  163. If a function in a translation unit compiled with ``-fsanitize=cfi-icall``
  164. takes the address of a function not compiled with ``-fsanitize=cfi-icall``,
  165. that address may differ from the address taken by a function in a translation
  166. unit not compiled with ``-fsanitize=cfi-icall``. This is technically a
  167. violation of the C and C++ standards, but it should not affect most programs.
  168. Each translation unit compiled with ``-fsanitize=cfi-icall`` must be
  169. statically linked into the program or shared library, and calls across
  170. shared library boundaries are handled as if the callee was not compiled with
  171. ``-fsanitize=cfi-icall``.
  172. This scheme is currently only supported on the x86 and x86_64 architectures.
  173. ``-fsanitize-cfi-icall-generalize-pointers``
  174. --------------------------------------------
  175. Mismatched pointer types are a common cause of cfi-icall check failures.
  176. Translation units compiled with the ``-fsanitize-cfi-icall-generalize-pointers``
  177. flag relax pointer type checking for call sites in that translation unit,
  178. applied across all functions compiled with ``-fsanitize=cfi-icall``.
  179. Specifically, pointers in return and argument types are treated as equivalent as
  180. long as the qualifiers for the type they point to match. For example, ``char*``,
  181. ``char**``, and ``int*`` are considered equivalent types. However, ``char*`` and
  182. ``const char*`` are considered separate types.
  183. ``-fsanitize-cfi-icall-generalize-pointers`` is not compatible with
  184. ``-fsanitize-cfi-cross-dso``.
  185. .. _cfi-canonical-jump-tables:
  186. ``-fsanitize-cfi-canonical-jump-tables``
  187. ----------------------------------------
  188. The default behavior of Clang's indirect function call checker will replace
  189. the address of each CFI-checked function in the output file's symbol table
  190. with the address of a jump table entry which will pass CFI checks. We refer
  191. to this as making the jump table `canonical`. This property allows code that
  192. was not compiled with ``-fsanitize=cfi-icall`` to take a CFI-valid address
  193. of a function, but it comes with a couple of caveats that are especially
  194. relevant for users of cross-DSO CFI:
  195. - There is a performance and code size overhead associated with each
  196. exported function, because each such function must have an associated
  197. jump table entry, which must be emitted even in the common case where the
  198. function is never address-taken anywhere in the program, and must be used
  199. even for direct calls between DSOs, in addition to the PLT overhead.
  200. - There is no good way to take a CFI-valid address of a function written in
  201. assembly or a language not supported by Clang. The reason is that the code
  202. generator would need to insert a jump table in order to form a CFI-valid
  203. address for assembly functions, but there is no way in general for the
  204. code generator to determine the language of the function. This may be
  205. possible with LTO in the intra-DSO case, but in the cross-DSO case the only
  206. information available is the function declaration. One possible solution
  207. is to add a C wrapper for each assembly function, but these wrappers can
  208. present a significant maintenance burden for heavy users of assembly in
  209. addition to adding runtime overhead.
  210. For these reasons, we provide the option of making the jump table non-canonical
  211. with the flag ``-fno-sanitize-cfi-canonical-jump-tables``. When the jump
  212. table is made non-canonical, symbol table entries point directly to the
  213. function body. Any instances of a function's address being taken in C will
  214. be replaced with a jump table address.
  215. This scheme does have its own caveats, however. It does end up breaking
  216. function address equality more aggressively than the default behavior,
  217. especially in cross-DSO mode which normally preserves function address
  218. equality entirely.
  219. Furthermore, it is occasionally necessary for code not compiled with
  220. ``-fsanitize=cfi-icall`` to take a function address that is valid
  221. for CFI. For example, this is necessary when a function's address
  222. is taken by assembly code and then called by CFI-checking C code. The
  223. ``__attribute__((cfi_canonical_jump_table))`` attribute may be used to make
  224. the jump table entry of a specific function canonical so that the external
  225. code will end up taking a address for the function that will pass CFI checks.
  226. ``-fsanitize=cfi-icall`` and ``-fsanitize=function``
  227. ----------------------------------------------------
  228. This tool is similar to ``-fsanitize=function`` in that both tools check
  229. the types of function calls. However, the two tools occupy different points
  230. on the design space; ``-fsanitize=function`` is a developer tool designed
  231. to find bugs in local development builds, whereas ``-fsanitize=cfi-icall``
  232. is a security hardening mechanism designed to be deployed in release builds.
  233. ``-fsanitize=function`` has a higher space and time overhead due to a more
  234. complex type check at indirect call sites, as well as a need for run-time
  235. type information (RTTI), which may make it unsuitable for deployment. Because
  236. of the need for RTTI, ``-fsanitize=function`` can only be used with C++
  237. programs, whereas ``-fsanitize=cfi-icall`` can protect both C and C++ programs.
  238. On the other hand, ``-fsanitize=function`` conforms more closely with the C++
  239. standard and user expectations around interaction with shared libraries;
  240. the identity of function pointers is maintained, and calls across shared
  241. library boundaries are no different from calls within a single program or
  242. shared library.
  243. Member Function Pointer Call Checking
  244. =====================================
  245. This scheme checks that indirect calls via a member function pointer
  246. take place using an object of the correct dynamic type. Specifically, we
  247. check that the dynamic type of the member function referenced by the member
  248. function pointer matches the "function pointer" part of the member function
  249. pointer, and that the member function's class type is related to the base
  250. type of the member function. This CFI scheme can be enabled on its own using
  251. ``-fsanitize=cfi-mfcall``.
  252. The compiler will only emit a full CFI check if the member function pointer's
  253. base type is complete. This is because the complete definition of the base
  254. type contains information that is necessary to correctly compile the CFI
  255. check. To ensure that the compiler always emits a full CFI check, it is
  256. recommended to also pass the flag ``-fcomplete-member-pointers``, which
  257. enables a non-conforming language extension that requires member pointer
  258. base types to be complete if they may be used for a call.
  259. For this scheme to work, all translation units containing the definition
  260. of a virtual member function (whether inline or not), other than members
  261. of :ref:`blacklisted <cfi-blacklist>` types or types with public :doc:`LTO
  262. visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin``
  263. enabled and be statically linked into the program.
  264. This scheme is currently not compatible with cross-DSO CFI or the
  265. Microsoft ABI.
  266. .. _cfi-blacklist:
  267. Blacklist
  268. =========
  269. A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain
  270. source files, functions and types using the ``src``, ``fun`` and ``type``
  271. entity types. Specific CFI modes can be be specified using ``[section]``
  272. headers.
  273. .. code-block:: bash
  274. # Suppress all CFI checking for code in a file.
  275. src:bad_file.cpp
  276. src:bad_header.h
  277. # Ignore all functions with names containing MyFooBar.
  278. fun:*MyFooBar*
  279. # Ignore all types in the standard library.
  280. type:std::*
  281. # Disable only unrelated cast checks for this function
  282. [cfi-unrelated-cast]
  283. fun:*UnrelatedCast*
  284. # Disable CFI call checks for this function without affecting cast checks
  285. [cfi-vcall|cfi-nvcall|cfi-icall]
  286. fun:*BadCall*
  287. .. _cfi-cross-dso:
  288. Shared library support
  289. ======================
  290. Use **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control
  291. flow integrity mode, which allows all CFI schemes listed above to
  292. apply across DSO boundaries. As in the regular CFI, each DSO must be
  293. built with ``-flto``.
  294. Normally, CFI checks will only be performed for classes that have hidden LTO
  295. visibility. With this flag enabled, the compiler will emit cross-DSO CFI
  296. checks for all classes, except for those which appear in the CFI blacklist
  297. or which use a ``no_sanitize`` attribute.
  298. Design
  299. ======
  300. Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`.
  301. Publications
  302. ============
  303. `Control-Flow Integrity: Principles, Implementations, and Applications <https://research.microsoft.com/pubs/64250/ccs05.pdf>`_.
  304. Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti.
  305. `Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_.
  306. Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway,
  307. Úlfar Erlingsson, Luis Lozano, Geoff Pike.