ControlFlowIntegrity.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 be
  22. 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. Experimental support for
  25. :ref:`cross-DSO control flow integrity <cfi-cross-dso>` exists that
  26. does not have these requirements. This cross-DSO support has unstable
  27. ABI at this time.
  28. .. _gold plugin: http://llvm.org/docs/GoldPlugin.html
  29. .. _cfi-schemes:
  30. Available schemes
  31. =================
  32. Available schemes are:
  33. - ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks
  34. <cfi-strictness>`.
  35. - ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong
  36. dynamic type.
  37. - ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another
  38. unrelated type to the wrong dynamic type.
  39. - ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of
  40. the wrong dynamic type.
  41. - ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the
  42. wrong dynamic type.
  43. - ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic
  44. type.
  45. You can use ``-fsanitize=cfi`` to enable all the schemes and use
  46. ``-fno-sanitize`` flag to narrow down the set of schemes as desired.
  47. For example, you can build your program with
  48. ``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall``
  49. to use all schemes except for non-virtual member function call and indirect call
  50. checking.
  51. Remember that you have to provide ``-flto`` if at least one CFI scheme is
  52. enabled.
  53. Trapping and Diagnostics
  54. ========================
  55. By default, CFI will abort the program immediately upon detecting a control
  56. flow integrity violation. You can use the :ref:`-fno-sanitize-trap=
  57. <controlling-code-generation>` flag to cause CFI to print a diagnostic
  58. similar to the one below before the program aborts.
  59. .. code-block:: console
  60. bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50)
  61. 0x000000425a50: note: vtable is of type 'A'
  62. 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
  63. ^
  64. If diagnostics are enabled, you can also configure CFI to continue program
  65. execution instead of aborting by using the :ref:`-fsanitize-recover=
  66. <controlling-code-generation>` flag.
  67. Forward-Edge CFI for Virtual Calls
  68. ==================================
  69. This scheme checks that virtual calls take place using a vptr of the correct
  70. dynamic type; that is, the dynamic type of the called object must be a
  71. derived class of the static type of the object used to make the call.
  72. This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``.
  73. For this scheme to work, all translation units containing the definition
  74. of a virtual member function (whether inline or not), other than members
  75. of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
  76. ``-fsanitize=cfi-vcall`` enabled and be statically linked into the program.
  77. Performance
  78. -----------
  79. A performance overhead of less than 1% has been measured by running the
  80. Dromaeo benchmark suite against an instrumented version of the Chromium
  81. web browser. Another good performance benchmark for this mechanism is the
  82. virtual-call-heavy SPEC 2006 xalancbmk.
  83. Note that this scheme has not yet been optimized for binary size; an increase
  84. of up to 15% has been observed for Chromium.
  85. Bad Cast Checking
  86. =================
  87. This scheme checks that pointer casts are made to an object of the correct
  88. dynamic type; that is, the dynamic type of the object must be a derived class
  89. of the pointee type of the cast. The checks are currently only introduced
  90. where the class being casted to is a polymorphic class.
  91. Bad casts are not in themselves control flow integrity violations, but they
  92. can also create security vulnerabilities, and the implementation uses many
  93. of the same mechanisms.
  94. There are two types of bad cast that may be forbidden: bad casts
  95. from a base class to a derived class (which can be checked with
  96. ``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of
  97. type ``void*`` or another unrelated type (which can be checked with
  98. ``-fsanitize=cfi-unrelated-cast``).
  99. The difference between these two types of casts is that the first is defined
  100. by the C++ standard to produce an undefined value, while the second is not
  101. in itself undefined behavior (it is well defined to cast the pointer back
  102. to its original type) unless the object is uninitialized and the cast is a
  103. ``static_cast`` (see C++14 [basic.life]p5).
  104. If a program as a matter of policy forbids the second type of cast, that
  105. restriction can normally be enforced. However it may in some cases be necessary
  106. for a function to perform a forbidden cast to conform with an external API
  107. (e.g. the ``allocate`` member function of a standard library allocator). Such
  108. functions may be :ref:`blacklisted <cfi-blacklist>`.
  109. For this scheme to work, all translation units containing the definition
  110. of a virtual member function (whether inline or not), other than members
  111. of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
  112. ``-fsanitize=cfi-derived-cast`` or ``-fsanitize=cfi-unrelated-cast`` enabled
  113. and be statically linked into the program.
  114. Non-Virtual Member Function Call Checking
  115. =========================================
  116. This scheme checks that non-virtual calls take place using an object of
  117. the correct dynamic type; that is, the dynamic type of the called object
  118. must be a derived class of the static type of the object used to make the
  119. call. The checks are currently only introduced where the object is of a
  120. polymorphic class type. This CFI scheme can be enabled on its own using
  121. ``-fsanitize=cfi-nvcall``.
  122. For this scheme to work, all translation units containing the definition
  123. of a virtual member function (whether inline or not), other than members
  124. of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
  125. ``-fsanitize=cfi-nvcall`` enabled and be statically linked into the program.
  126. .. _cfi-strictness:
  127. Strictness
  128. ----------
  129. If a class has a single non-virtual base and does not introduce or override
  130. virtual member functions or fields other than an implicitly defined virtual
  131. destructor, it will have the same layout and virtual function semantics as
  132. its base. By default, casts to such classes are checked as if they were made
  133. to the least derived such class.
  134. Casting an instance of a base class to such a derived class is technically
  135. undefined behavior, but it is a relatively common hack for introducing
  136. member functions on class instances with specific properties that works under
  137. most compilers and should not have security implications, so we allow it by
  138. default. It can be disabled with ``-fsanitize=cfi-cast-strict``.
  139. Indirect Function Call Checking
  140. ===============================
  141. This scheme checks that function calls take place using a function of the
  142. correct dynamic type; that is, the dynamic type of the function must match
  143. the static type used at the call. This CFI scheme can be enabled on its own
  144. using ``-fsanitize=cfi-icall``.
  145. For this scheme to work, each indirect function call in the program, other
  146. than calls in :ref:`blacklisted <cfi-blacklist>` functions, must call a
  147. function which was either compiled with ``-fsanitize=cfi-icall`` enabled,
  148. or whose address was taken by a function in a translation unit compiled with
  149. ``-fsanitize=cfi-icall``.
  150. If a function in a translation unit compiled with ``-fsanitize=cfi-icall``
  151. takes the address of a function not compiled with ``-fsanitize=cfi-icall``,
  152. that address may differ from the address taken by a function in a translation
  153. unit not compiled with ``-fsanitize=cfi-icall``. This is technically a
  154. violation of the C and C++ standards, but it should not affect most programs.
  155. Each translation unit compiled with ``-fsanitize=cfi-icall`` must be
  156. statically linked into the program or shared library, and calls across
  157. shared library boundaries are handled as if the callee was not compiled with
  158. ``-fsanitize=cfi-icall``.
  159. This scheme is currently only supported on the x86 and x86_64 architectures.
  160. ``-fsanitize=cfi-icall`` and ``-fsanitize=function``
  161. ----------------------------------------------------
  162. This tool is similar to ``-fsanitize=function`` in that both tools check
  163. the types of function calls. However, the two tools occupy different points
  164. on the design space; ``-fsanitize=function`` is a developer tool designed
  165. to find bugs in local development builds, whereas ``-fsanitize=cfi-icall``
  166. is a security hardening mechanism designed to be deployed in release builds.
  167. ``-fsanitize=function`` has a higher space and time overhead due to a more
  168. complex type check at indirect call sites, as well as a need for run-time
  169. type information (RTTI), which may make it unsuitable for deployment. Because
  170. of the need for RTTI, ``-fsanitize=function`` can only be used with C++
  171. programs, whereas ``-fsanitize=cfi-icall`` can protect both C and C++ programs.
  172. On the other hand, ``-fsanitize=function`` conforms more closely with the C++
  173. standard and user expectations around interaction with shared libraries;
  174. the identity of function pointers is maintained, and calls across shared
  175. library boundaries are no different from calls within a single program or
  176. shared library.
  177. .. _cfi-blacklist:
  178. Blacklist
  179. =========
  180. A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain
  181. source files, functions and types using the ``src``, ``fun`` and ``type``
  182. entity types.
  183. In addition, if a type has a ``uuid`` attribute and the blacklist contains
  184. the type entry ``attr:uuid``, CFI checks are suppressed for that type. This
  185. allows all COM types to be easily blacklisted, which is useful as COM types
  186. are typically defined outside of the linked program.
  187. .. code-block:: bash
  188. # Suppress checking for code in a file.
  189. src:bad_file.cpp
  190. src:bad_header.h
  191. # Ignore all functions with names containing MyFooBar.
  192. fun:*MyFooBar*
  193. # Ignore all types in the standard library.
  194. type:std::*
  195. # Ignore all types with a uuid attribute.
  196. type:attr:uuid
  197. .. _cfi-cross-dso:
  198. Shared library support
  199. ======================
  200. Use **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control
  201. flow integrity mode, which allows all CFI schemes listed above to
  202. apply across DSO boundaries. As in the regular CFI, each DSO must be
  203. built with ``-flto``.
  204. Design
  205. ======
  206. Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`.
  207. Publications
  208. ============
  209. `Control-Flow Integrity: Principles, Implementations, and Applications <http://research.microsoft.com/pubs/64250/ccs05.pdf>`_.
  210. Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti.
  211. `Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_.
  212. Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway,
  213. Úlfar Erlingsson, Luis Lozano, Geoff Pike.