blob: 12b4610f8a282adbcf8d3772d3d60df48a33100f [file] [log] [blame]
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001======================
2Control Flow Integrity
3======================
4
5.. toctree::
6 :hidden:
7
8 ControlFlowIntegrityDesign
9
10.. contents::
11 :local:
12
13Introduction
14============
15
16Clang includes an implementation of a number of control flow integrity (CFI)
17schemes, which are designed to abort the program upon detecting certain forms
18of undefined behavior that can potentially allow attackers to subvert the
19program's control flow. These schemes have been optimized for performance,
20allowing developers to enable them in release builds.
21
22To enable Clang's available CFI schemes, use the flag ``-fsanitize=cfi``.
Alexey Samsonov9eda6402015-12-04 21:30:58 +000023You can also enable a subset of available :ref:`schemes <cfi-schemes>`.
24As currently implemented, all schemes rely on link-time optimization (LTO);
25so it is required to specify ``-flto``, and the linker used must support LTO,
26for example via the `gold plugin`_.
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +000027
Peter Collingbourne3afb2662016-04-28 17:09:37 +000028To allow the checks to be implemented efficiently, the program must
29be structured such that certain object files are compiled with CFI
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +000030enabled, and are statically linked into the program. This may preclude
Peter Collingbourne3afb2662016-04-28 17:09:37 +000031the use of shared libraries in some cases.
32
33The compiler will only produce CFI checks for a class if it can infer hidden
34LTO visibility for that class. LTO visibility is a property of a class that
35is inferred from flags and attributes. For more details, see the documentation
36for :doc:`LTO visibility <LTOVisibility>`.
37
38The ``-fsanitize=cfi-{vcall,nvcall,derived-cast,unrelated-cast}`` flags
39require that a ``-fvisibility=`` flag also be specified. This is because the
40default visibility setting is ``-fvisibility=default``, which would disable
41CFI checks for classes without visibility attributes. Most users will want
42to specify ``-fvisibility=hidden``, which enables CFI checks for such classes.
43
44Experimental support for :ref:`cross-DSO control flow integrity
45<cfi-cross-dso>` exists that does not require classes to have hidden LTO
46visibility. This cross-DSO support has unstable ABI at this time.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000047
Peter Collingbournea4ccff32015-02-20 20:30:56 +000048.. _gold plugin: http://llvm.org/docs/GoldPlugin.html
49
Alexey Samsonov9eda6402015-12-04 21:30:58 +000050.. _cfi-schemes:
51
52Available schemes
53=================
54
55Available schemes are:
56
57 - ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks
58 <cfi-strictness>`.
59 - ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong
60 dynamic type.
61 - ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another
62 unrelated type to the wrong dynamic type.
63 - ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of
64 the wrong dynamic type.
65 - ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the
66 wrong dynamic type.
67 - ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic
68 type.
69
70You can use ``-fsanitize=cfi`` to enable all the schemes and use
71``-fno-sanitize`` flag to narrow down the set of schemes as desired.
72For example, you can build your program with
73``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall``
74to use all schemes except for non-virtual member function call and indirect call
75checking.
76
77Remember that you have to provide ``-flto`` if at least one CFI scheme is
78enabled.
79
Peter Collingbourne93bb8622015-12-11 23:54:18 +000080Trapping and Diagnostics
81========================
82
83By default, CFI will abort the program immediately upon detecting a control
84flow integrity violation. You can use the :ref:`-fno-sanitize-trap=
85<controlling-code-generation>` flag to cause CFI to print a diagnostic
86similar to the one below before the program aborts.
87
88.. code-block:: console
89
90 bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50)
91 0x000000425a50: note: vtable is of type 'A'
92 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
93 ^
94
95If diagnostics are enabled, you can also configure CFI to continue program
96execution instead of aborting by using the :ref:`-fsanitize-recover=
97<controlling-code-generation>` flag.
Alexey Samsonov9eda6402015-12-04 21:30:58 +000098
Peter Collingbournea4ccff32015-02-20 20:30:56 +000099Forward-Edge CFI for Virtual Calls
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000100==================================
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000101
102This scheme checks that virtual calls take place using a vptr of the correct
103dynamic type; that is, the dynamic type of the called object must be a
104derived class of the static type of the object used to make the call.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000105This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``.
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000106
107For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000108of a virtual member function (whether inline or not), other than members
109of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
110``-fsanitize=cfi-vcall`` enabled and be statically linked into the program.
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000111
112Performance
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000113-----------
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000114
115A performance overhead of less than 1% has been measured by running the
116Dromaeo benchmark suite against an instrumented version of the Chromium
117web browser. Another good performance benchmark for this mechanism is the
118virtual-call-heavy SPEC 2006 xalancbmk.
119
120Note that this scheme has not yet been optimized for binary size; an increase
121of up to 15% has been observed for Chromium.
122
Peter Collingbourned2926c92015-03-14 02:42:25 +0000123Bad Cast Checking
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000124=================
Peter Collingbourned2926c92015-03-14 02:42:25 +0000125
126This scheme checks that pointer casts are made to an object of the correct
127dynamic type; that is, the dynamic type of the object must be a derived class
128of the pointee type of the cast. The checks are currently only introduced
129where the class being casted to is a polymorphic class.
130
131Bad casts are not in themselves control flow integrity violations, but they
132can also create security vulnerabilities, and the implementation uses many
133of the same mechanisms.
134
135There are two types of bad cast that may be forbidden: bad casts
136from a base class to a derived class (which can be checked with
137``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of
138type ``void*`` or another unrelated type (which can be checked with
139``-fsanitize=cfi-unrelated-cast``).
140
141The difference between these two types of casts is that the first is defined
142by the C++ standard to produce an undefined value, while the second is not
143in itself undefined behavior (it is well defined to cast the pointer back
Peter Collingbournec8620df2016-02-01 18:55:50 +0000144to its original type) unless the object is uninitialized and the cast is a
145``static_cast`` (see C++14 [basic.life]p5).
Peter Collingbourned2926c92015-03-14 02:42:25 +0000146
147If a program as a matter of policy forbids the second type of cast, that
148restriction can normally be enforced. However it may in some cases be necessary
149for a function to perform a forbidden cast to conform with an external API
150(e.g. the ``allocate`` member function of a standard library allocator). Such
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000151functions may be :ref:`blacklisted <cfi-blacklist>`.
Peter Collingbourned2926c92015-03-14 02:42:25 +0000152
153For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000154of a virtual member function (whether inline or not), other than members
155of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
Peter Collingbourned2926c92015-03-14 02:42:25 +0000156``-fsanitize=cfi-derived-cast`` or ``-fsanitize=cfi-unrelated-cast`` enabled
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000157and be statically linked into the program.
Peter Collingbourned2926c92015-03-14 02:42:25 +0000158
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000159Non-Virtual Member Function Call Checking
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000160=========================================
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000161
162This scheme checks that non-virtual calls take place using an object of
163the correct dynamic type; that is, the dynamic type of the called object
164must be a derived class of the static type of the object used to make the
165call. The checks are currently only introduced where the object is of a
166polymorphic class type. This CFI scheme can be enabled on its own using
167``-fsanitize=cfi-nvcall``.
168
169For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000170of a virtual member function (whether inline or not), other than members
171of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
172``-fsanitize=cfi-nvcall`` enabled and be statically linked into the program.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000173
Peter Collingbourned2926c92015-03-14 02:42:25 +0000174.. _cfi-strictness:
175
176Strictness
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000177----------
Peter Collingbourned2926c92015-03-14 02:42:25 +0000178
179If a class has a single non-virtual base and does not introduce or override
180virtual member functions or fields other than an implicitly defined virtual
181destructor, it will have the same layout and virtual function semantics as
182its base. By default, casts to such classes are checked as if they were made
183to the least derived such class.
184
185Casting an instance of a base class to such a derived class is technically
186undefined behavior, but it is a relatively common hack for introducing
187member functions on class instances with specific properties that works under
188most compilers and should not have security implications, so we allow it by
189default. It can be disabled with ``-fsanitize=cfi-cast-strict``.
190
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +0000191Indirect Function Call Checking
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000192===============================
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +0000193
194This scheme checks that function calls take place using a function of the
195correct dynamic type; that is, the dynamic type of the function must match
196the static type used at the call. This CFI scheme can be enabled on its own
197using ``-fsanitize=cfi-icall``.
198
199For this scheme to work, each indirect function call in the program, other
200than calls in :ref:`blacklisted <cfi-blacklist>` functions, must call a
201function which was either compiled with ``-fsanitize=cfi-icall`` enabled,
202or whose address was taken by a function in a translation unit compiled with
203``-fsanitize=cfi-icall``.
204
205If a function in a translation unit compiled with ``-fsanitize=cfi-icall``
206takes the address of a function not compiled with ``-fsanitize=cfi-icall``,
207that address may differ from the address taken by a function in a translation
208unit not compiled with ``-fsanitize=cfi-icall``. This is technically a
209violation of the C and C++ standards, but it should not affect most programs.
210
211Each translation unit compiled with ``-fsanitize=cfi-icall`` must be
212statically linked into the program or shared library, and calls across
213shared library boundaries are handled as if the callee was not compiled with
214``-fsanitize=cfi-icall``.
215
216This scheme is currently only supported on the x86 and x86_64 architectures.
217
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +0000218``-fsanitize-cfi-icall-generalize-pointers``
219--------------------------------------------
220
221Mismatched pointer types are a common cause of cfi-icall check failures.
222Translation units compiled with the ``-fsanitize-cfi-icall-generalize-pointers``
223flag relax pointer type checking for call sites in that translation unit,
224applied across all functions compiled with ``-fsanitize=cfi-icall``.
225
226Specifically, pointers in return and argument types are treated as equivalent as
227long as the qualifiers for the type they point to match. For example, ``char*``
228``char**`, and ``int*`` are considered equivalent types. However, ``char*`` and
229``const char*`` are considered separate types.
230
231``-fsanitize-cfi-icall-generalize-pointers`` is not compatible with
232``-fsanitize-cfi-cross-dso``.
233
234
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +0000235``-fsanitize=cfi-icall`` and ``-fsanitize=function``
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000236----------------------------------------------------
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +0000237
238This tool is similar to ``-fsanitize=function`` in that both tools check
239the types of function calls. However, the two tools occupy different points
240on the design space; ``-fsanitize=function`` is a developer tool designed
241to find bugs in local development builds, whereas ``-fsanitize=cfi-icall``
242is a security hardening mechanism designed to be deployed in release builds.
243
244``-fsanitize=function`` has a higher space and time overhead due to a more
245complex type check at indirect call sites, as well as a need for run-time
246type information (RTTI), which may make it unsuitable for deployment. Because
247of the need for RTTI, ``-fsanitize=function`` can only be used with C++
248programs, whereas ``-fsanitize=cfi-icall`` can protect both C and C++ programs.
249
250On the other hand, ``-fsanitize=function`` conforms more closely with the C++
251standard and user expectations around interaction with shared libraries;
252the identity of function pointers is maintained, and calls across shared
253library boundaries are no different from calls within a single program or
254shared library.
255
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000256.. _cfi-blacklist:
257
258Blacklist
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000259=========
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000260
261A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain
262source files, functions and types using the ``src``, ``fun`` and ``type``
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +0000263entity types. Specific CFI modes can be be specified using ``[section]``
264headers.
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000265
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000266.. code-block:: bash
267
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +0000268 # Suppress all CFI checking for code in a file.
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000269 src:bad_file.cpp
270 src:bad_header.h
271 # Ignore all functions with names containing MyFooBar.
272 fun:*MyFooBar*
273 # Ignore all types in the standard library.
274 type:std::*
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +0000275 # Disable only unrelated cast checks for this function
276 [cfi-unrelated-cast]
277 fun:*UnrelatedCast*
278 # Disable CFI call checks for this function without affecting cast checks
279 [cfi-vcall|cfi-nvcall|cfi-icall]
280 fun:*BadCall*
281
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000282
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +0000283.. _cfi-cross-dso:
284
285Shared library support
286======================
287
288Use **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control
289flow integrity mode, which allows all CFI schemes listed above to
290apply across DSO boundaries. As in the regular CFI, each DSO must be
291built with ``-flto``.
292
Peter Collingbourne3afb2662016-04-28 17:09:37 +0000293Normally, CFI checks will only be performed for classes that have hidden LTO
294visibility. With this flag enabled, the compiler will emit cross-DSO CFI
295checks for all classes, except for those which appear in the CFI blacklist
296or which use a ``no_sanitize`` attribute.
297
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000298Design
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000299======
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000300
301Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`.
302
303Publications
Alexey Samsonov9eda6402015-12-04 21:30:58 +0000304============
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000305
306`Control-Flow Integrity: Principles, Implementations, and Applications <http://research.microsoft.com/pubs/64250/ccs05.pdf>`_.
307Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti.
308
309`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_.
310Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway,
311Úlfar Erlingsson, Luis Lozano, Geoff Pike.