| 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 |
| 23 |
``-fsanitize=cfi``. You can also enable a subset of available |
| 24 |
:ref:`schemes <cfi-schemes>`. As currently implemented, all schemes |
| 25 |
except for ``kcfi`` rely on link-time optimization (LTO); so it is |
| 26 |
required to specify ``-flto`` or ``-flto=thin``, and the linker used |
| 27 |
must support LTO, for example via the `gold plugin`_. |
| 28 |
|
| 29 |
To allow the checks to be implemented efficiently, the program must |
| 30 |
be structured such that certain object files are compiled with CFI |
| 31 |
enabled, and are statically linked into the program. This may preclude |
| 32 |
the use of shared libraries in some cases. |
| 33 |
|
| 34 |
The compiler will only produce CFI checks for a class if it can infer hidden |
| 35 |
LTO visibility for that class. LTO visibility is a property of a class that |
| 36 |
is inferred from flags and attributes. For more details, see the documentation |
| 37 |
for :doc:`LTO visibility <LTOVisibility>`. |
| 38 |
|
| 39 |
The ``-fsanitize=cfi-{vcall,nvcall,derived-cast,unrelated-cast}`` flags |
| 40 |
require that a ``-fvisibility=`` flag also be specified. This is because the |
| 41 |
default visibility setting is ``-fvisibility=default``, which would disable |
| 42 |
CFI checks for classes without visibility attributes. Most users will want |
| 43 |
to specify ``-fvisibility=hidden``, which enables CFI checks for such classes. |
| 44 |
|
| 45 |
When using ``-fsanitize=cfi*`` with ``-flto=thin``, it is recommended |
| 46 |
to reduce link times by passing `-funique-source-file-names |
| 47 |
<UsersManual.html#cmdoption-f-no-unique-source-file-names>`_, provided |
| 48 |
that your program is compatible with it. |
| 49 |
|
| 50 |
Experimental support for :ref:`cross-DSO control flow integrity |
| 51 |
<cfi-cross-dso>` exists that does not require classes to have hidden LTO |
| 52 |
visibility. This cross-DSO support has unstable ABI at this time. |
| 53 |
|
| 54 |
.. _gold plugin: https://llvm.org/docs/GoldPlugin.html |
| 55 |
|
| 56 |
.. _cfi-schemes: |
| 57 |
|
| 58 |
Available schemes |
| 59 |
================= |
| 60 |
|
| 61 |
Available schemes are: |
| 62 |
|
| 63 |
- ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks |
| 64 |
<cfi-strictness>`. |
| 65 |
- ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong |
| 66 |
dynamic type. |
| 67 |
- ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another |
| 68 |
unrelated type to the wrong dynamic type. |
| 69 |
- ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of |
| 70 |
the wrong dynamic type. |
| 71 |
- ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the |
| 72 |
wrong dynamic type. |
| 73 |
- ``-fsanitize=cfi-icall``: Indirect call of a function with wrong dynamic |
| 74 |
type. |
| 75 |
- ``-fsanitize=cfi-mfcall``: Indirect call via a member function pointer with |
| 76 |
wrong dynamic type. |
| 77 |
|
| 78 |
You can use ``-fsanitize=cfi`` to enable all the schemes and use |
| 79 |
``-fno-sanitize`` flag to narrow down the set of schemes as desired. |
| 80 |
For example, you can build your program with |
| 81 |
``-fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-icall`` |
| 82 |
to use all schemes except for non-virtual member function call and indirect call |
| 83 |
checking. |
| 84 |
|
| 85 |
Remember that you have to provide ``-flto`` or ``-flto=thin`` if at |
| 86 |
least one CFI scheme is enabled. |
| 87 |
|
| 88 |
Trapping and Diagnostics |
| 89 |
======================== |
| 90 |
|
| 91 |
By default, CFI will abort the program immediately upon detecting a control |
| 92 |
flow integrity violation. You can use the :ref:`-fno-sanitize-trap= |
| 93 |
<controlling-code-generation>` flag to cause CFI to print a diagnostic |
| 94 |
similar to the one below before the program aborts. |
| 95 |
|
| 96 |
.. code-block:: console |
| 97 |
|
| 98 |
bad-cast.cpp:109:7: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast (vtable address 0x000000425a50) |
| 99 |
0x000000425a50: note: vtable is of type 'A' |
| 100 |
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 |
| 101 |
^ |
| 102 |
|
| 103 |
If diagnostics are enabled, you can also configure CFI to continue program |
| 104 |
execution instead of aborting by using the :ref:`-fsanitize-recover= |
| 105 |
<controlling-code-generation>` flag. |
| 106 |
|
| 107 |
Forward-Edge CFI for Virtual Calls |
| 108 |
================================== |
| 109 |
|
| 110 |
This scheme checks that virtual calls take place using a vptr of the correct |
| 111 |
dynamic type; that is, the dynamic type of the called object must be a |
| 112 |
derived class of the static type of the object used to make the call. |
| 113 |
This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``. |
| 114 |
|
| 115 |
For this scheme to work, all translation units containing the definition |
| 116 |
of a virtual member function (whether inline or not), other than members |
| 117 |
of :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO |
| 118 |
visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` |
| 119 |
enabled and be statically linked into the program. |
| 120 |
|
| 121 |
Performance |
| 122 |
----------- |
| 123 |
|
| 124 |
A performance overhead of less than 1% has been measured by running the |
| 125 |
Dromaeo benchmark suite against an instrumented version of the Chromium |
| 126 |
web browser. Another good performance benchmark for this mechanism is the |
| 127 |
virtual-call-heavy SPEC 2006 xalancbmk. |
| 128 |
|
| 129 |
Note that this scheme has not yet been optimized for binary size; an increase |
| 130 |
of up to 15% has been observed for Chromium. |
| 131 |
|
| 132 |
Bad Cast Checking |
| 133 |
================= |
| 134 |
|
| 135 |
This scheme checks that pointer casts are made to an object of the correct |
| 136 |
dynamic type; that is, the dynamic type of the object must be a derived class |
| 137 |
of the pointee type of the cast. The checks are currently only introduced |
| 138 |
where the class being cast to is a polymorphic class. |
| 139 |
|
| 140 |
Bad casts are not in themselves control flow integrity violations, but they |
| 141 |
can also create security vulnerabilities, and the implementation uses many |
| 142 |
of the same mechanisms. |
| 143 |
|
| 144 |
There are two types of bad cast that may be forbidden: bad casts |
| 145 |
from a base class to a derived class (which can be checked with |
| 146 |
``-fsanitize=cfi-derived-cast``), and bad casts from a pointer of |
| 147 |
type ``void*`` or another unrelated type (which can be checked with |
| 148 |
``-fsanitize=cfi-unrelated-cast``). |
| 149 |
|
| 150 |
The difference between these two types of casts is that the first is defined |
| 151 |
by the C++ standard to produce an undefined value, while the second is not |
| 152 |
in itself undefined behavior (it is well defined to cast the pointer back |
| 153 |
to its original type) unless the object is uninitialized and the cast is a |
| 154 |
``static_cast`` (see C++14 [basic.life]p5). |
| 155 |
|
| 156 |
If a program as a matter of policy forbids the second type of cast, that |
| 157 |
restriction can normally be enforced. However it may in some cases be necessary |
| 158 |
for a function to perform a forbidden cast to conform with an external API |
| 159 |
(e.g. the ``allocate`` member function of a standard library allocator). Such |
| 160 |
functions may be :ref:`ignored <cfi-ignorelist>`. |
| 161 |
|
| 162 |
For this scheme to work, all translation units containing the definition |
| 163 |
of a virtual member function (whether inline or not), other than members |
| 164 |
of :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO |
| 165 |
visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` |
| 166 |
enabled and be statically linked into the program. |
| 167 |
|
| 168 |
Non-Virtual Member Function Call Checking |
| 169 |
========================================= |
| 170 |
|
| 171 |
This scheme checks that non-virtual calls take place using an object of |
| 172 |
the correct dynamic type; that is, the dynamic type of the called object |
| 173 |
must be a derived class of the static type of the object used to make the |
| 174 |
call. The checks are currently only introduced where the object is of a |
| 175 |
polymorphic class type. This CFI scheme can be enabled on its own using |
| 176 |
``-fsanitize=cfi-nvcall``. |
| 177 |
|
| 178 |
For this scheme to work, all translation units containing the definition |
| 179 |
of a virtual member function (whether inline or not), other than members |
| 180 |
of :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO |
| 181 |
visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` |
| 182 |
enabled and be statically linked into the program. |
| 183 |
|
| 184 |
.. _cfi-strictness: |
| 185 |
|
| 186 |
Strictness |
| 187 |
---------- |
| 188 |
|
| 189 |
If a class has a single non-virtual base and does not introduce or override |
| 190 |
virtual member functions or fields other than an implicitly defined virtual |
| 191 |
destructor, it will have the same layout and virtual function semantics as |
| 192 |
its base. By default, casts to such classes are checked as if they were made |
| 193 |
to the least derived such class. |
| 194 |
|
| 195 |
Casting an instance of a base class to such a derived class is technically |
| 196 |
undefined behavior, but it is a relatively common hack for introducing |
| 197 |
member functions on class instances with specific properties that works under |
| 198 |
most compilers and should not have security implications, so we allow it by |
| 199 |
default. It can be disabled with ``-fsanitize=cfi-cast-strict``. |
| 200 |
|
| 201 |
Indirect Function Call Checking |
| 202 |
=============================== |
| 203 |
|
| 204 |
This scheme checks that function calls take place using a function of the |
| 205 |
correct dynamic type; that is, the dynamic type of the function must match |
| 206 |
the static type used at the call. This CFI scheme can be enabled on its own |
| 207 |
using ``-fsanitize=cfi-icall``. |
| 208 |
|
| 209 |
For this scheme to work, each indirect function call in the program, other |
| 210 |
than calls in :ref:`ignored <cfi-ignorelist>` functions, must call a |
| 211 |
function which was either compiled with ``-fsanitize=cfi-icall`` enabled, |
| 212 |
or whose address was taken by a function in a translation unit compiled with |
| 213 |
``-fsanitize=cfi-icall``. |
| 214 |
|
| 215 |
If a function in a translation unit compiled with ``-fsanitize=cfi-icall`` |
| 216 |
takes the address of a function not compiled with ``-fsanitize=cfi-icall``, |
| 217 |
that address may differ from the address taken by a function in a translation |
| 218 |
unit not compiled with ``-fsanitize=cfi-icall``. This is technically a |
| 219 |
violation of the C and C++ standards, but it should not affect most programs. |
| 220 |
|
| 221 |
Each translation unit compiled with ``-fsanitize=cfi-icall`` must be |
| 222 |
statically linked into the program or shared library, and calls across |
| 223 |
shared library boundaries are handled as if the callee was not compiled with |
| 224 |
``-fsanitize=cfi-icall``. |
| 225 |
|
| 226 |
This scheme is currently supported on a limited set of targets: x86, |
| 227 |
x86_64, arm, arch64 and wasm. |
| 228 |
|
| 229 |
``-fsanitize-cfi-icall-generalize-pointers`` |
| 230 |
-------------------------------------------- |
| 231 |
|
| 232 |
Mismatched pointer types are a common cause of cfi-icall check failures. |
| 233 |
Translation units compiled with the ``-fsanitize-cfi-icall-generalize-pointers`` |
| 234 |
flag relax pointer type checking for call sites in that translation unit, |
| 235 |
applied across all functions compiled with ``-fsanitize=cfi-icall``. |
| 236 |
|
| 237 |
Specifically, pointers in return and argument types are treated as equivalent as |
| 238 |
long as the qualifiers for the type they point to match. For example, ``char*``, |
| 239 |
``char**``, and ``int*`` are considered equivalent types. However, ``char*`` and |
| 240 |
``const char*`` are considered separate types. |
| 241 |
|
| 242 |
``-fsanitize-cfi-icall-generalize-pointers`` is not compatible with |
| 243 |
``-fsanitize-cfi-cross-dso``. |
| 244 |
|
| 245 |
.. _cfi-icall-experimental-normalize-integers: |
| 246 |
|
| 247 |
``-fsanitize-cfi-icall-experimental-normalize-integers`` |
| 248 |
-------------------------------------------------------- |
| 249 |
|
| 250 |
This option enables normalizing integer types as vendor extended types for |
| 251 |
cross-language LLVM CFI/KCFI support with other languages that can't represent |
| 252 |
and encode C/C++ integer types. |
| 253 |
|
| 254 |
Specifically, integer types are encoded as their defined representations (e.g., |
| 255 |
8-bit signed integer, 16-bit signed integer, 32-bit signed integer, ...) for |
| 256 |
compatibility with languages that define explicitly-sized integer types (e.g., |
| 257 |
i8, i16, i32, ..., in Rust). |
| 258 |
|
| 259 |
``-fsanitize-cfi-icall-experimental-normalize-integers`` is compatible with |
| 260 |
``-fsanitize-cfi-icall-generalize-pointers``. |
| 261 |
|
| 262 |
This option is currently experimental. |
| 263 |
|
| 264 |
.. _cfi-canonical-jump-tables: |
| 265 |
|
| 266 |
``-fsanitize-cfi-canonical-jump-tables`` |
| 267 |
---------------------------------------- |
| 268 |
|
| 269 |
The default behavior of Clang's indirect function call checker will replace |
| 270 |
the address of each CFI-checked function in the output file's symbol table |
| 271 |
with the address of a jump table entry which will pass CFI checks. We refer |
| 272 |
to this as making the jump table `canonical`. This property allows code that |
| 273 |
was not compiled with ``-fsanitize=cfi-icall`` to take a CFI-valid address |
| 274 |
of a function, but it comes with a couple of caveats that are especially |
| 275 |
relevant for users of cross-DSO CFI: |
| 276 |
|
| 277 |
- There is a performance and code size overhead associated with each |
| 278 |
exported function, because each such function must have an associated |
| 279 |
jump table entry, which must be emitted even in the common case where the |
| 280 |
function is never address-taken anywhere in the program, and must be used |
| 281 |
even for direct calls between DSOs, in addition to the PLT overhead. |
| 282 |
|
| 283 |
- There is no good way to take a CFI-valid address of a function written in |
| 284 |
assembly or a language not supported by Clang. The reason is that the code |
| 285 |
generator would need to insert a jump table in order to form a CFI-valid |
| 286 |
address for assembly functions, but there is no way in general for the |
| 287 |
code generator to determine the language of the function. This may be |
| 288 |
possible with LTO in the intra-DSO case, but in the cross-DSO case the only |
| 289 |
information available is the function declaration. One possible solution |
| 290 |
is to add a C wrapper for each assembly function, but these wrappers can |
| 291 |
present a significant maintenance burden for heavy users of assembly in |
| 292 |
addition to adding runtime overhead. |
| 293 |
|
| 294 |
For these reasons, we provide the option of making the jump table non-canonical |
| 295 |
with the flag ``-fno-sanitize-cfi-canonical-jump-tables``. When the jump |
| 296 |
table is made non-canonical, symbol table entries point directly to the |
| 297 |
function body. Any instances of a function's address being taken in C will |
| 298 |
be replaced with a jump table address. |
| 299 |
|
| 300 |
This scheme does have its own caveats, however. It does end up breaking |
| 301 |
function address equality more aggressively than the default behavior, |
| 302 |
especially in cross-DSO mode which normally preserves function address |
| 303 |
equality entirely. |
| 304 |
|
| 305 |
Furthermore, it is occasionally necessary for code not compiled with |
| 306 |
``-fsanitize=cfi-icall`` to take a function address that is valid |
| 307 |
for CFI. For example, this is necessary when a function's address |
| 308 |
is taken by assembly code and then called by CFI-checking C code. The |
| 309 |
``__attribute__((cfi_canonical_jump_table))`` attribute may be used to make |
| 310 |
the jump table entry of a specific function canonical so that the external |
| 311 |
code will end up taking an address for the function that will pass CFI checks. |
| 312 |
|
| 313 |
``-fsanitize=cfi-icall`` and ``-fsanitize=function`` |
| 314 |
---------------------------------------------------- |
| 315 |
|
| 316 |
This tool is similar to ``-fsanitize=function`` in that both tools check |
| 317 |
the types of function calls. However, the two tools occupy different points |
| 318 |
on the design space; ``-fsanitize=function`` is a developer tool designed |
| 319 |
to find bugs in local development builds, whereas ``-fsanitize=cfi-icall`` |
| 320 |
is a security hardening mechanism designed to be deployed in release builds. |
| 321 |
|
| 322 |
``-fsanitize=function`` has a higher space and time overhead due to a more |
| 323 |
complex type check at indirect call sites, which may make it unsuitable for |
| 324 |
deployment. |
| 325 |
|
| 326 |
On the other hand, ``-fsanitize=function`` conforms more closely with the C++ |
| 327 |
standard and user expectations around interaction with shared libraries; |
| 328 |
the identity of function pointers is maintained, and calls across shared |
| 329 |
library boundaries are no different from calls within a single program or |
| 330 |
shared library. |
| 331 |
|
| 332 |
.. _kcfi: |
| 333 |
|
| 334 |
``-fsanitize=kcfi`` |
| 335 |
------------------- |
| 336 |
|
| 337 |
This is an alternative indirect call control-flow integrity scheme designed |
| 338 |
for low-level system software, such as operating system kernels. Unlike |
| 339 |
``-fsanitize=cfi-icall``, it doesn't require ``-flto``, won't result in |
| 340 |
function pointers being replaced with jump table references, and never breaks |
| 341 |
cross-DSO function address equality. These properties make KCFI easier to |
| 342 |
adopt in low-level software. KCFI is limited to checking only function |
| 343 |
pointers, and isn't compatible with executable-only memory. |
| 344 |
|
| 345 |
``-fsanitize-kcfi-arity`` |
| 346 |
----------------------------- |
| 347 |
|
| 348 |
For supported targets, this feature extends kCFI by telling the compiler to |
| 349 |
record information about each indirect-callable function's arity (i.e., the |
| 350 |
number of arguments passed in registers) into the binary. Some kernel CFI |
| 351 |
techniques, such as FineIBT, may be able to use this information to provide |
| 352 |
enhanced security. |
| 353 |
|
| 354 |
Member Function Pointer Call Checking |
| 355 |
===================================== |
| 356 |
|
| 357 |
This scheme checks that indirect calls via a member function pointer |
| 358 |
take place using an object of the correct dynamic type. Specifically, we |
| 359 |
check that the dynamic type of the member function referenced by the member |
| 360 |
function pointer matches the "function pointer" part of the member function |
| 361 |
pointer, and that the member function's class type is related to the base |
| 362 |
type of the member function. This CFI scheme can be enabled on its own using |
| 363 |
``-fsanitize=cfi-mfcall``. |
| 364 |
|
| 365 |
The compiler will only emit a full CFI check if the member function pointer's |
| 366 |
base type is complete. This is because the complete definition of the base |
| 367 |
type contains information that is necessary to correctly compile the CFI |
| 368 |
check. To ensure that the compiler always emits a full CFI check, it is |
| 369 |
recommended to also pass the flag ``-fcomplete-member-pointers``, which |
| 370 |
enables a non-conforming language extension that requires member pointer |
| 371 |
base types to be complete if they may be used for a call. |
| 372 |
|
| 373 |
For this scheme to work, all translation units containing the definition |
| 374 |
of a virtual member function (whether inline or not), other than members |
| 375 |
of :ref:`ignored <cfi-ignorelist>` types or types with public :doc:`LTO |
| 376 |
visibility <LTOVisibility>`, must be compiled with ``-flto`` or ``-flto=thin`` |
| 377 |
enabled and be statically linked into the program. |
| 378 |
|
| 379 |
This scheme is currently not compatible with cross-DSO CFI or the |
| 380 |
Microsoft ABI. |
| 381 |
|
| 382 |
.. _cfi-ignorelist: |
| 383 |
|
| 384 |
Ignorelist |
| 385 |
========== |
| 386 |
|
| 387 |
A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain |
| 388 |
source files, functions and types using the ``src``, ``fun`` and ``type`` |
| 389 |
entity types. Specific CFI modes can be be specified using ``[section]`` |
| 390 |
headers. |
| 391 |
|
| 392 |
.. code-block:: bash |
| 393 |
|
| 394 |
# Suppress all CFI checking for code in a file. |
| 395 |
src:bad_file.cpp |
| 396 |
src:bad_header.h |
| 397 |
# Ignore all functions with names containing MyFooBar. |
| 398 |
fun:*MyFooBar* |
| 399 |
# Ignore all types in the standard library. |
| 400 |
type:std::* |
| 401 |
# Disable only unrelated cast checks for this function |
| 402 |
[cfi-unrelated-cast] |
| 403 |
fun:*UnrelatedCast* |
| 404 |
# Disable CFI call checks for this function without affecting cast checks |
| 405 |
[cfi-vcall|cfi-nvcall|cfi-icall] |
| 406 |
fun:*BadCall* |
| 407 |
|
| 408 |
|
| 409 |
.. _cfi-cross-dso: |
| 410 |
|
| 411 |
Shared library support |
| 412 |
====================== |
| 413 |
|
| 414 |
Use **-f[no-]sanitize-cfi-cross-dso** to enable the cross-DSO control |
| 415 |
flow integrity mode, which allows all CFI schemes listed above to |
| 416 |
apply across DSO boundaries. As in the regular CFI, each DSO must be |
| 417 |
built with ``-flto`` or ``-flto=thin``. |
| 418 |
|
| 419 |
Normally, CFI checks will only be performed for classes that have hidden LTO |
| 420 |
visibility. With this flag enabled, the compiler will emit cross-DSO CFI |
| 421 |
checks for all classes, except for those which appear in the CFI ignorelist |
| 422 |
or which use a ``no_sanitize`` attribute. |
| 423 |
|
| 424 |
Design |
| 425 |
====== |
| 426 |
|
| 427 |
Please refer to the :doc:`design document<ControlFlowIntegrityDesign>`. |
| 428 |
|
| 429 |
Publications |
| 430 |
============ |
| 431 |
|
| 432 |
`Control-Flow Integrity: Principles, Implementations, and Applications <https://research.microsoft.com/pubs/64250/ccs05.pdf>`_. |
| 433 |
Martin Abadi, Mihai Budiu, Úlfar Erlingsson, Jay Ligatti. |
| 434 |
|
| 435 |
`Enforcing Forward-Edge Control-Flow Integrity in GCC & LLVM <https://www.usenix.org/system/files/conference/usenixsecurity14/sec14-paper-tice.pdf>`_. |
| 436 |
Caroline Tice, Tom Roeder, Peter Collingbourne, Stephen Checkoway, |
| 437 |
Úlfar Erlingsson, Luis Lozano, Geoff Pike. |