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