blob: b043d24a142faf04d755f7fd0895019e9e5405cf [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 Samsonov907880e2015-06-19 19:57:46 +000023As currently implemented, CFI relies on link-time optimization (LTO); so it is
24required to specify ``-flto``, and the linker used must support LTO, for example
Peter Collingbournea4ccff32015-02-20 20:30:56 +000025via the `gold plugin`_. To allow the checks to be implemented efficiently,
26the program must be structured such that certain object files are compiled
27with CFI enabled, and are statically linked into the program. This may
28preclude the use of shared libraries in some cases.
29
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000030Clang currently implements forward-edge CFI for member function calls and
31bad cast checking. More schemes are under development.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000032
33.. _gold plugin: http://llvm.org/docs/GoldPlugin.html
34
35Forward-Edge CFI for Virtual Calls
36----------------------------------
37
38This scheme checks that virtual calls take place using a vptr of the correct
39dynamic type; that is, the dynamic type of the called object must be a
40derived class of the static type of the object used to make the call.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000041This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000042
43For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +000044of a virtual member function (whether inline or not), other than members
45of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
46``-fsanitize=cfi-vcall`` enabled and be statically linked into the program.
Peter Collingbournea4ccff32015-02-20 20:30:56 +000047
48Performance
49~~~~~~~~~~~
50
51A performance overhead of less than 1% has been measured by running the
52Dromaeo benchmark suite against an instrumented version of the Chromium
53web browser. Another good performance benchmark for this mechanism is the
54virtual-call-heavy SPEC 2006 xalancbmk.
55
56Note that this scheme has not yet been optimized for binary size; an increase
57of up to 15% has been observed for Chromium.
58
Peter Collingbourned2926c92015-03-14 02:42:25 +000059Bad Cast Checking
60-----------------
61
62This scheme checks that pointer casts are made to an object of the correct
63dynamic type; that is, the dynamic type of the object must be a derived class
64of the pointee type of the cast. The checks are currently only introduced
65where the class being casted to is a polymorphic class.
66
67Bad casts are not in themselves control flow integrity violations, but they
68can also create security vulnerabilities, and the implementation uses many
69of the same mechanisms.
70
71There are two types of bad cast that may be forbidden: bad casts
72from a base class to a derived class (which can be checked with
73``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of
74type ``void*`` or another unrelated type (which can be checked with
75``-fsanitize=cfi-unrelated-cast``).
76
77The difference between these two types of casts is that the first is defined
78by the C++ standard to produce an undefined value, while the second is not
79in itself undefined behavior (it is well defined to cast the pointer back
80to its original type).
81
82If a program as a matter of policy forbids the second type of cast, that
83restriction can normally be enforced. However it may in some cases be necessary
84for a function to perform a forbidden cast to conform with an external API
85(e.g. the ``allocate`` member function of a standard library allocator). Such
Peter Collingbourne6fccf952015-07-15 12:15:56 +000086functions may be :ref:`blacklisted <cfi-blacklist>`.
Peter Collingbourned2926c92015-03-14 02:42:25 +000087
88For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +000089of a virtual member function (whether inline or not), other than members
90of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
Peter Collingbourned2926c92015-03-14 02:42:25 +000091``-fsanitize=cfi-derived-cast`` or ``-fsanitize=cfi-unrelated-cast`` enabled
Peter Collingbourne6fccf952015-07-15 12:15:56 +000092and be statically linked into the program.
Peter Collingbourned2926c92015-03-14 02:42:25 +000093
Peter Collingbourne1a7488a2015-04-02 00:23:30 +000094Non-Virtual Member Function Call Checking
95-----------------------------------------
96
97This scheme checks that non-virtual calls take place using an object of
98the correct dynamic type; that is, the dynamic type of the called object
99must be a derived class of the static type of the object used to make the
100call. The checks are currently only introduced where the object is of a
101polymorphic class type. This CFI scheme can be enabled on its own using
102``-fsanitize=cfi-nvcall``.
103
104For this scheme to work, all translation units containing the definition
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000105of a virtual member function (whether inline or not), other than members
106of :ref:`blacklisted <cfi-blacklist>` types, must be compiled with
107``-fsanitize=cfi-nvcall`` enabled and be statically linked into the program.
Peter Collingbourne1a7488a2015-04-02 00:23:30 +0000108
Peter Collingbourned2926c92015-03-14 02:42:25 +0000109.. _cfi-strictness:
110
111Strictness
112~~~~~~~~~~
113
114If a class has a single non-virtual base and does not introduce or override
115virtual member functions or fields other than an implicitly defined virtual
116destructor, it will have the same layout and virtual function semantics as
117its base. By default, casts to such classes are checked as if they were made
118to the least derived such class.
119
120Casting an instance of a base class to such a derived class is technically
121undefined behavior, but it is a relatively common hack for introducing
122member functions on class instances with specific properties that works under
123most compilers and should not have security implications, so we allow it by
124default. It can be disabled with ``-fsanitize=cfi-cast-strict``.
125
Peter Collingbourne6fccf952015-07-15 12:15:56 +0000126.. _cfi-blacklist:
127
128Blacklist
129---------
130
131A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain
132source files, functions and types using the ``src``, ``fun`` and ``type``
133entity types.
134
135In addition, if a type has a ``uuid`` attribute and the blacklist contains
136the type entry ``attr:uuid``, CFI checks are suppressed for that type. This
137allows all COM types to be easily blacklisted, which is useful as COM types
138are typically defined outside of the linked program.
139
140.. code-block:: bash
141
142 # Suppress checking for code in a file.
143 src:bad_file.cpp
144 src:bad_header.h
145 # Ignore all functions with names containing MyFooBar.
146 fun:*MyFooBar*
147 # Ignore all types in the standard library.
148 type:std::*
149 # Ignore all types with a uuid attribute.
150 type:attr:uuid
151
Peter Collingbournea4ccff32015-02-20 20:30:56 +0000152Design
153------
154
155Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`.
156
157Publications
158------------
159
160`Control-Flow Integrity: Principles, Implementations, and Applications <http://research.microsoft.com/pubs/64250/ccs05.pdf>`_.
161Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti.
162
163`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <http://www.pcc.me.uk/~peter/acad/usenix14.pdf>`_.
164Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway,
165Úlfar Erlingsson, Luis Lozano, Geoff Pike.