blob: b583fc0debcd5bc8b4113ec786d6805de3783cd8 [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``.
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +000023As currently implemented, all of Clang's CFI schemes (``cfi-vcall``,
24``cfi-derived-cast``, ``cfi-unrelated-cast``, ``cfi-nvcall``, ``cfi-icall``)
25rely on link-time optimization (LTO); so it is required to specify
26``-flto``, and the linker used must support LTO, for example via the `gold
27plugin`_. To allow the checks to be implemented efficiently, the program must
28be structured such that certain object files are compiled with CFI enabled,
29and are statically linked into the program. This may preclude the use of
30shared libraries in some cases.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000031
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000032Clang currently implements forward-edge CFI for member function calls and
33bad cast checking. More schemes are under development.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000034
35.. _gold plugin: http://llvm.org/docs/GoldPlugin.html
36
37Forward-Edge CFI for Virtual Calls
38----------------------------------
39
40This scheme checks that virtual calls take place using a vptr of the correct
41dynamic type; that is, the dynamic type of the called object must be a
42derived class of the static type of the object used to make the call.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000043This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000044
45For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +000046of a virtual member function (whether inline or not), other than members
47of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
48``-fsanitize=cfi-vcall`` enabled and be statically linked into the program.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000049
50Performance
51~~~~~~~~~~~
52
53A performance overhead of less than 1% has been measured by running the
54Dromaeo benchmark suite against an instrumented version of the Chromium
55web browser. Another good performance benchmark for this mechanism is the
56virtual-call-heavy SPEC 2006 xalancbmk.
57
58Note that this scheme has not yet been optimized for binary size; an increase
59of up to 15% has been observed for Chromium.
60
Peter Collingbourned2926c92015-03-14 02:42:25 +000061Bad Cast Checking
62-----------------
63
64This scheme checks that pointer casts are made to an object of the correct
65dynamic type; that is, the dynamic type of the object must be a derived class
66of the pointee type of the cast. The checks are currently only introduced
67where the class being casted to is a polymorphic class.
68
69Bad casts are not in themselves control flow integrity violations, but they
70can also create security vulnerabilities, and the implementation uses many
71of the same mechanisms.
72
73There are two types of bad cast that may be forbidden: bad casts
74from a base class to a derived class (which can be checked with
75``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of
76type ``void*`` or another unrelated type (which can be checked with
77``-fsanitize=cfi-unrelated-cast``).
78
79The difference between these two types of casts is that the first is defined
80by the C++ standard to produce an undefined value, while the second is not
81in itself undefined behavior (it is well defined to cast the pointer back
82to its original type).
83
84If a program as a matter of policy forbids the second type of cast, that
85restriction can normally be enforced. However it may in some cases be necessary
86for a function to perform a forbidden cast to conform with an external API
87(e.g. the ``allocate`` member function of a standard library allocator). Such
Peter Collingbourne6fccf952015-07-15 12:15:56 +000088functions may be :ref:`blacklisted <cfi-blacklist>`.
Peter Collingbourned2926c92015-03-14 02:42:25 +000089
90For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +000091of a virtual member function (whether inline or not), other than members
92of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
Peter Collingbourned2926c92015-03-14 02:42:25 +000093``-fsanitize=cfi-derived-cast`` or ``-fsanitize=cfi-unrelated-cast`` enabled
Peter Collingbourne6fccf952015-07-15 12:15:56 +000094and be statically linked into the program.
Peter Collingbourned2926c92015-03-14 02:42:25 +000095
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000096Non-Virtual Member Function Call Checking
97-----------------------------------------
98
99This scheme checks that non-virtual calls take place using an object of
100the correct dynamic type; that is, the dynamic type of the called object
101must be a derived class of the static type of the object used to make the
102call. The checks are currently only introduced where the object is of a
103polymorphic class type. This CFI scheme can be enabled on its own using
104``-fsanitize=cfi-nvcall``.
105
106For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000107of a virtual member function (whether inline or not), other than members
108of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
109``-fsanitize=cfi-nvcall`` enabled and be statically linked into the program.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000110
Peter Collingbourned2926c92015-03-14 02:42:25 +0000111.. _cfi-strictness:
112
113Strictness
114~~~~~~~~~~
115
116If a class has a single non-virtual base and does not introduce or override
117virtual member functions or fields other than an implicitly defined virtual
118destructor, it will have the same layout and virtual function semantics as
119its base. By default, casts to such classes are checked as if they were made
120to the least derived such class.
121
122Casting an instance of a base class to such a derived class is technically
123undefined behavior, but it is a relatively common hack for introducing
124member functions on class instances with specific properties that works under
125most compilers and should not have security implications, so we allow it by
126default. It can be disabled with ``-fsanitize=cfi-cast-strict``.
127
Peter Collingbourne2c7f7e32015-09-10 02:17:40 +0000128Indirect Function Call Checking
129-------------------------------
130
131This scheme checks that function calls take place using a function of the
132correct dynamic type; that is, the dynamic type of the function must match
133the static type used at the call. This CFI scheme can be enabled on its own
134using ``-fsanitize=cfi-icall``.
135
136For this scheme to work, each indirect function call in the program, other
137than calls in :ref:`blacklisted <cfi-blacklist>` functions, must call a
138function which was either compiled with ``-fsanitize=cfi-icall`` enabled,
139or whose address was taken by a function in a translation unit compiled with
140``-fsanitize=cfi-icall``.
141
142If a function in a translation unit compiled with ``-fsanitize=cfi-icall``
143takes the address of a function not compiled with ``-fsanitize=cfi-icall``,
144that address may differ from the address taken by a function in a translation
145unit not compiled with ``-fsanitize=cfi-icall``. This is technically a
146violation of the C and C++ standards, but it should not affect most programs.
147
148Each translation unit compiled with ``-fsanitize=cfi-icall`` must be
149statically linked into the program or shared library, and calls across
150shared library boundaries are handled as if the callee was not compiled with
151``-fsanitize=cfi-icall``.
152
153This scheme is currently only supported on the x86 and x86_64 architectures.
154
155``-fsanitize=cfi-icall`` and ``-fsanitize=function``
156~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157
158This tool is similar to ``-fsanitize=function`` in that both tools check
159the types of function calls. However, the two tools occupy different points
160on the design space; ``-fsanitize=function`` is a developer tool designed
161to find bugs in local development builds, whereas ``-fsanitize=cfi-icall``
162is a security hardening mechanism designed to be deployed in release builds.
163
164``-fsanitize=function`` has a higher space and time overhead due to a more
165complex type check at indirect call sites, as well as a need for run-time
166type information (RTTI), which may make it unsuitable for deployment. Because
167of the need for RTTI, ``-fsanitize=function`` can only be used with C++
168programs, whereas ``-fsanitize=cfi-icall`` can protect both C and C++ programs.
169
170On the other hand, ``-fsanitize=function`` conforms more closely with the C++
171standard and user expectations around interaction with shared libraries;
172the identity of function pointers is maintained, and calls across shared
173library boundaries are no different from calls within a single program or
174shared library.
175
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000176.. _cfi-blacklist:
177
178Blacklist
179---------
180
181A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain
182source files, functions and types using the ``src``, ``fun`` and ``type``
183entity types.
184
185In addition, if a type has a ``uuid`` attribute and the blacklist contains
186the type entry ``attr:uuid``, CFI checks are suppressed for that type. This
187allows all COM types to be easily blacklisted, which is useful as COM types
188are typically defined outside of the linked program.
189
190.. code-block:: bash
191
192 # Suppress checking for code in a file.
193 src:bad_file.cpp
194 src:bad_header.h
195 # Ignore all functions with names containing MyFooBar.
196 fun:*MyFooBar*
197 # Ignore all types in the standard library.
198 type:std::*
199 # Ignore all types with a uuid attribute.
200 type:attr:uuid
201
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000202Design
203------
204
205Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`.
206
207Publications
208------------
209
210`Control-Flow Integrity: Principles, Implementations, and Applications <http://research.microsoft.com/pubs/64250/ccs05.pdf>`_.
211Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti.
212
213`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_.
214Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway,
215Úlfar Erlingsson, Luis Lozano, Geoff Pike.