ControlFlowIntegrity.rst 13 KB

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