1e5dd7070Spatrick=========================================== 2e5dd7070SpatrickControl Flow Integrity Design Documentation 3e5dd7070Spatrick=========================================== 4e5dd7070Spatrick 5e5dd7070SpatrickThis page documents the design of the :doc:`ControlFlowIntegrity` schemes 6e5dd7070Spatricksupported by Clang. 7e5dd7070Spatrick 8e5dd7070SpatrickForward-Edge CFI for Virtual Calls 9e5dd7070Spatrick================================== 10e5dd7070Spatrick 11e5dd7070SpatrickThis scheme works by allocating, for each static type used to make a virtual 12e5dd7070Spatrickcall, a region of read-only storage in the object file holding a bit vector 13e5dd7070Spatrickthat maps onto to the region of storage used for those virtual tables. Each 14e5dd7070Spatrickset bit in the bit vector corresponds to the `address point`_ for a virtual 15e5dd7070Spatricktable compatible with the static type for which the bit vector is being built. 16e5dd7070Spatrick 17e5dd7070SpatrickFor example, consider the following three C++ classes: 18e5dd7070Spatrick 19e5dd7070Spatrick.. code-block:: c++ 20e5dd7070Spatrick 21e5dd7070Spatrick struct A { 22e5dd7070Spatrick virtual void f1(); 23e5dd7070Spatrick virtual void f2(); 24e5dd7070Spatrick virtual void f3(); 25e5dd7070Spatrick }; 26e5dd7070Spatrick 27e5dd7070Spatrick struct B : A { 28e5dd7070Spatrick virtual void f1(); 29e5dd7070Spatrick virtual void f2(); 30e5dd7070Spatrick virtual void f3(); 31e5dd7070Spatrick }; 32e5dd7070Spatrick 33e5dd7070Spatrick struct C : A { 34e5dd7070Spatrick virtual void f1(); 35e5dd7070Spatrick virtual void f2(); 36e5dd7070Spatrick virtual void f3(); 37e5dd7070Spatrick }; 38e5dd7070Spatrick 39e5dd7070SpatrickThe scheme will cause the virtual tables for A, B and C to be laid out 40e5dd7070Spatrickconsecutively: 41e5dd7070Spatrick 42e5dd7070Spatrick.. csv-table:: Virtual Table Layout for A, B, C 43e5dd7070Spatrick :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 44e5dd7070Spatrick 45e5dd7070Spatrick A::offset-to-top, &A::rtti, &A::f1, &A::f2, &A::f3, B::offset-to-top, &B::rtti, &B::f1, &B::f2, &B::f3, C::offset-to-top, &C::rtti, &C::f1, &C::f2, &C::f3 46e5dd7070Spatrick 47e5dd7070SpatrickThe bit vector for static types A, B and C will look like this: 48e5dd7070Spatrick 49e5dd7070Spatrick.. csv-table:: Bit Vectors for A, B, C 50e5dd7070Spatrick :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 51e5dd7070Spatrick 52e5dd7070Spatrick A, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 53e5dd7070Spatrick B, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 54e5dd7070Spatrick C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 55e5dd7070Spatrick 56e5dd7070SpatrickBit vectors are represented in the object file as byte arrays. By loading 57e5dd7070Spatrickfrom indexed offsets into the byte array and applying a mask, a program can 58e5dd7070Spatricktest bits from the bit set with a relatively short instruction sequence. Bit 59e5dd7070Spatrickvectors may overlap so long as they use different bits. For the full details, 60e5dd7070Spatricksee the `ByteArrayBuilder`_ class. 61e5dd7070Spatrick 62e5dd7070SpatrickIn this case, assuming A is laid out at offset 0 in bit 0, B at offset 0 in 63e5dd7070Spatrickbit 1 and C at offset 0 in bit 2, the byte array would look like this: 64e5dd7070Spatrick 65e5dd7070Spatrick.. code-block:: c++ 66e5dd7070Spatrick 67e5dd7070Spatrick char bits[] = { 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 0 }; 68e5dd7070Spatrick 69e5dd7070SpatrickTo emit a virtual call, the compiler will assemble code that checks that 70e5dd7070Spatrickthe object's virtual table pointer is in-bounds and aligned and that the 71e5dd7070Spatrickrelevant bit is set in the bit vector. 72e5dd7070Spatrick 73e5dd7070SpatrickFor example on x86 a typical virtual call may look like this: 74e5dd7070Spatrick 75e5dd7070Spatrick.. code-block:: none 76e5dd7070Spatrick 77e5dd7070Spatrick ca7fbb: 48 8b 0f mov (%rdi),%rcx 78e5dd7070Spatrick ca7fbe: 48 8d 15 c3 42 fb 07 lea 0x7fb42c3(%rip),%rdx 79e5dd7070Spatrick ca7fc5: 48 89 c8 mov %rcx,%rax 80e5dd7070Spatrick ca7fc8: 48 29 d0 sub %rdx,%rax 81e5dd7070Spatrick ca7fcb: 48 c1 c0 3d rol $0x3d,%rax 82e5dd7070Spatrick ca7fcf: 48 3d 7f 01 00 00 cmp $0x17f,%rax 83e5dd7070Spatrick ca7fd5: 0f 87 36 05 00 00 ja ca8511 84e5dd7070Spatrick ca7fdb: 48 8d 15 c0 0b f7 06 lea 0x6f70bc0(%rip),%rdx 85e5dd7070Spatrick ca7fe2: f6 04 10 10 testb $0x10,(%rax,%rdx,1) 86e5dd7070Spatrick ca7fe6: 0f 84 25 05 00 00 je ca8511 87e5dd7070Spatrick ca7fec: ff 91 98 00 00 00 callq *0x98(%rcx) 88e5dd7070Spatrick [...] 89e5dd7070Spatrick ca8511: 0f 0b ud2 90e5dd7070Spatrick 91e5dd7070SpatrickThe compiler relies on co-operation from the linker in order to assemble 92e5dd7070Spatrickthe bit vectors for the whole program. It currently does this using LLVM's 93e5dd7070Spatrick`type metadata`_ mechanism together with link-time optimization. 94e5dd7070Spatrick 95e5dd7070Spatrick.. _address point: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general 96e5dd7070Spatrick.. _type metadata: https://llvm.org/docs/TypeMetadata.html 97e5dd7070Spatrick.. _ByteArrayBuilder: https://llvm.org/docs/doxygen/html/structllvm_1_1ByteArrayBuilder.html 98e5dd7070Spatrick 99e5dd7070SpatrickOptimizations 100e5dd7070Spatrick------------- 101e5dd7070Spatrick 102e5dd7070SpatrickThe scheme as described above is the fully general variant of the scheme. 103e5dd7070SpatrickMost of the time we are able to apply one or more of the following 104e5dd7070Spatrickoptimizations to improve binary size or performance. 105e5dd7070Spatrick 106e5dd7070SpatrickIn fact, if you try the above example with the current version of the 107e5dd7070Spatrickcompiler, you will probably find that it will not use the described virtual 108e5dd7070Spatricktable layout or machine instructions. Some of the optimizations we are about 109e5dd7070Spatrickto introduce cause the compiler to use a different layout or a different 110e5dd7070Spatricksequence of machine instructions. 111e5dd7070Spatrick 112e5dd7070SpatrickStripping Leading/Trailing Zeros in Bit Vectors 113e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 114e5dd7070Spatrick 115e5dd7070SpatrickIf a bit vector contains leading or trailing zeros, we can strip them from 116e5dd7070Spatrickthe vector. The compiler will emit code to check if the pointer is in range 117e5dd7070Spatrickof the region covered by ones, and perform the bit vector check using a 118e5dd7070Spatricktruncated version of the bit vector. For example, the bit vectors for our 119e5dd7070Spatrickexample class hierarchy will be emitted like this: 120e5dd7070Spatrick 121e5dd7070Spatrick.. csv-table:: Bit Vectors for A, B, C 122e5dd7070Spatrick :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 123e5dd7070Spatrick 124e5dd7070Spatrick A, , , 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, , 125e5dd7070Spatrick B, , , , , , , , 1, , , , , , , 126e5dd7070Spatrick C, , , , , , , , , , , , , 1, , 127e5dd7070Spatrick 128e5dd7070SpatrickShort Inline Bit Vectors 129e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~ 130e5dd7070Spatrick 131e5dd7070SpatrickIf the vector is sufficiently short, we can represent it as an inline constant 132e5dd7070Spatrickon x86. This saves us a few instructions when reading the correct element 133e5dd7070Spatrickof the bit vector. 134e5dd7070Spatrick 135e5dd7070SpatrickIf the bit vector fits in 32 bits, the code looks like this: 136e5dd7070Spatrick 137e5dd7070Spatrick.. code-block:: none 138e5dd7070Spatrick 139e5dd7070Spatrick dc2: 48 8b 03 mov (%rbx),%rax 140e5dd7070Spatrick dc5: 48 8d 15 14 1e 00 00 lea 0x1e14(%rip),%rdx 141e5dd7070Spatrick dcc: 48 89 c1 mov %rax,%rcx 142e5dd7070Spatrick dcf: 48 29 d1 sub %rdx,%rcx 143e5dd7070Spatrick dd2: 48 c1 c1 3d rol $0x3d,%rcx 144e5dd7070Spatrick dd6: 48 83 f9 03 cmp $0x3,%rcx 145e5dd7070Spatrick dda: 77 2f ja e0b <main+0x9b> 146e5dd7070Spatrick ddc: ba 09 00 00 00 mov $0x9,%edx 147e5dd7070Spatrick de1: 0f a3 ca bt %ecx,%edx 148e5dd7070Spatrick de4: 73 25 jae e0b <main+0x9b> 149e5dd7070Spatrick de6: 48 89 df mov %rbx,%rdi 150e5dd7070Spatrick de9: ff 10 callq *(%rax) 151e5dd7070Spatrick [...] 152e5dd7070Spatrick e0b: 0f 0b ud2 153e5dd7070Spatrick 154e5dd7070SpatrickOr if the bit vector fits in 64 bits: 155e5dd7070Spatrick 156e5dd7070Spatrick.. code-block:: none 157e5dd7070Spatrick 158e5dd7070Spatrick 11a6: 48 8b 03 mov (%rbx),%rax 159e5dd7070Spatrick 11a9: 48 8d 15 d0 28 00 00 lea 0x28d0(%rip),%rdx 160e5dd7070Spatrick 11b0: 48 89 c1 mov %rax,%rcx 161e5dd7070Spatrick 11b3: 48 29 d1 sub %rdx,%rcx 162e5dd7070Spatrick 11b6: 48 c1 c1 3d rol $0x3d,%rcx 163e5dd7070Spatrick 11ba: 48 83 f9 2a cmp $0x2a,%rcx 164e5dd7070Spatrick 11be: 77 35 ja 11f5 <main+0xb5> 165e5dd7070Spatrick 11c0: 48 ba 09 00 00 00 00 movabs $0x40000000009,%rdx 166e5dd7070Spatrick 11c7: 04 00 00 167e5dd7070Spatrick 11ca: 48 0f a3 ca bt %rcx,%rdx 168e5dd7070Spatrick 11ce: 73 25 jae 11f5 <main+0xb5> 169e5dd7070Spatrick 11d0: 48 89 df mov %rbx,%rdi 170e5dd7070Spatrick 11d3: ff 10 callq *(%rax) 171e5dd7070Spatrick [...] 172e5dd7070Spatrick 11f5: 0f 0b ud2 173e5dd7070Spatrick 174e5dd7070SpatrickIf the bit vector consists of a single bit, there is only one possible 175e5dd7070Spatrickvirtual table, and the check can consist of a single equality comparison: 176e5dd7070Spatrick 177e5dd7070Spatrick.. code-block:: none 178e5dd7070Spatrick 179e5dd7070Spatrick 9a2: 48 8b 03 mov (%rbx),%rax 180e5dd7070Spatrick 9a5: 48 8d 0d a4 13 00 00 lea 0x13a4(%rip),%rcx 181e5dd7070Spatrick 9ac: 48 39 c8 cmp %rcx,%rax 182e5dd7070Spatrick 9af: 75 25 jne 9d6 <main+0x86> 183e5dd7070Spatrick 9b1: 48 89 df mov %rbx,%rdi 184e5dd7070Spatrick 9b4: ff 10 callq *(%rax) 185e5dd7070Spatrick [...] 186e5dd7070Spatrick 9d6: 0f 0b ud2 187e5dd7070Spatrick 188e5dd7070SpatrickVirtual Table Layout 189e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~ 190e5dd7070Spatrick 191e5dd7070SpatrickThe compiler lays out classes of disjoint hierarchies in separate regions 192e5dd7070Spatrickof the object file. At worst, bit vectors in disjoint hierarchies only 193e5dd7070Spatrickneed to cover their disjoint hierarchy. But the closer that classes in 194e5dd7070Spatricksub-hierarchies are laid out to each other, the smaller the bit vectors for 195e5dd7070Spatrickthose sub-hierarchies need to be (see "Stripping Leading/Trailing Zeros in Bit 196e5dd7070SpatrickVectors" above). The `GlobalLayoutBuilder`_ class is responsible for laying 197e5dd7070Spatrickout the globals efficiently to minimize the sizes of the underlying bitsets. 198e5dd7070Spatrick 199*a9ac8606Spatrick.. _GlobalLayoutBuilder: https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h 200e5dd7070Spatrick 201e5dd7070SpatrickAlignment 202e5dd7070Spatrick~~~~~~~~~ 203e5dd7070Spatrick 204e5dd7070SpatrickIf all gaps between address points in a particular bit vector are multiples 205e5dd7070Spatrickof powers of 2, the compiler can compress the bit vector by strengthening 206e5dd7070Spatrickthe alignment requirements of the virtual table pointer. For example, given 207e5dd7070Spatrickthis class hierarchy: 208e5dd7070Spatrick 209e5dd7070Spatrick.. code-block:: c++ 210e5dd7070Spatrick 211e5dd7070Spatrick struct A { 212e5dd7070Spatrick virtual void f1(); 213e5dd7070Spatrick virtual void f2(); 214e5dd7070Spatrick }; 215e5dd7070Spatrick 216e5dd7070Spatrick struct B : A { 217e5dd7070Spatrick virtual void f1(); 218e5dd7070Spatrick virtual void f2(); 219e5dd7070Spatrick virtual void f3(); 220e5dd7070Spatrick virtual void f4(); 221e5dd7070Spatrick virtual void f5(); 222e5dd7070Spatrick virtual void f6(); 223e5dd7070Spatrick }; 224e5dd7070Spatrick 225e5dd7070Spatrick struct C : A { 226e5dd7070Spatrick virtual void f1(); 227e5dd7070Spatrick virtual void f2(); 228e5dd7070Spatrick }; 229e5dd7070Spatrick 230e5dd7070SpatrickThe virtual tables will be laid out like this: 231e5dd7070Spatrick 232e5dd7070Spatrick.. csv-table:: Virtual Table Layout for A, B, C 233e5dd7070Spatrick :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 234e5dd7070Spatrick 235e5dd7070Spatrick A::offset-to-top, &A::rtti, &A::f1, &A::f2, B::offset-to-top, &B::rtti, &B::f1, &B::f2, &B::f3, &B::f4, &B::f5, &B::f6, C::offset-to-top, &C::rtti, &C::f1, &C::f2 236e5dd7070Spatrick 237e5dd7070SpatrickNotice that each address point for A is separated by 4 words. This lets us 238e5dd7070Spatrickemit a compressed bit vector for A that looks like this: 239e5dd7070Spatrick 240e5dd7070Spatrick.. csv-table:: 241e5dd7070Spatrick :header: 2, 6, 10, 14 242e5dd7070Spatrick 243e5dd7070Spatrick 1, 1, 0, 1 244e5dd7070Spatrick 245e5dd7070SpatrickAt call sites, the compiler will strengthen the alignment requirements by 246e5dd7070Spatrickusing a different rotate count. For example, on a 64-bit machine where the 247e5dd7070Spatrickaddress points are 4-word aligned (as in A from our example), the ``rol`` 248e5dd7070Spatrickinstruction may look like this: 249e5dd7070Spatrick 250e5dd7070Spatrick.. code-block:: none 251e5dd7070Spatrick 252e5dd7070Spatrick dd2: 48 c1 c1 3b rol $0x3b,%rcx 253e5dd7070Spatrick 254e5dd7070SpatrickPadding to Powers of 2 255e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~ 256e5dd7070Spatrick 257e5dd7070SpatrickOf course, this alignment scheme works best if the address points are 258e5dd7070Spatrickin fact aligned correctly. To make this more likely to happen, we insert 259e5dd7070Spatrickpadding between virtual tables that in many cases aligns address points to 260e5dd7070Spatricka power of 2. Specifically, our padding aligns virtual tables to the next 261e5dd7070Spatrickhighest power of 2 bytes; because address points for specific base classes 262e5dd7070Spatricknormally appear at fixed offsets within the virtual table, this normally 263e5dd7070Spatrickhas the effect of aligning the address points as well. 264e5dd7070Spatrick 265e5dd7070SpatrickThis scheme introduces tradeoffs between decreased space overhead for 266e5dd7070Spatrickinstructions and bit vectors and increased overhead in the form of padding. We 267e5dd7070Spatricktherefore limit the amount of padding so that we align to no more than 128 268e5dd7070Spatrickbytes. This number was found experimentally to provide a good tradeoff. 269e5dd7070Spatrick 270e5dd7070SpatrickEliminating Bit Vector Checks for All-Ones Bit Vectors 271e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 272e5dd7070Spatrick 273e5dd7070SpatrickIf the bit vector is all ones, the bit vector check is redundant; we simply 274e5dd7070Spatrickneed to check that the address is in range and well aligned. This is more 275e5dd7070Spatricklikely to occur if the virtual tables are padded. 276e5dd7070Spatrick 277e5dd7070SpatrickForward-Edge CFI for Virtual Calls by Interleaving Virtual Tables 278e5dd7070Spatrick----------------------------------------------------------------- 279e5dd7070Spatrick 280e5dd7070SpatrickDimitar et. al. proposed a novel approach that interleaves virtual tables in [1]_. 281e5dd7070SpatrickThis approach is more efficient in terms of space because padding and bit vectors are no longer needed. 282e5dd7070SpatrickAt the same time, it is also more efficient in terms of performance because in the interleaved layout 283e5dd7070Spatrickaddress points of the virtual tables are consecutive, thus the validity check of a virtual 284e5dd7070Spatrickvtable pointer is always a range check. 285e5dd7070Spatrick 286e5dd7070SpatrickAt a high level, the interleaving scheme consists of three steps: 1) split virtual table groups into 287e5dd7070Spatrickseparate virtual tables, 2) order virtual tables by a pre-order traversal of the class hierarchy 288e5dd7070Spatrickand 3) interleave virtual tables. 289e5dd7070Spatrick 290e5dd7070SpatrickThe interleaving scheme implemented in LLVM is inspired by [1]_ but has its own 291e5dd7070Spatrickenhancements (more in `Interleave virtual tables`_). 292e5dd7070Spatrick 293e5dd7070Spatrick.. [1] `Protecting C++ Dynamic Dispatch Through VTable Interleaving <https://cseweb.ucsd.edu/~lerner/papers/ivtbl-ndss16.pdf>`_. Dimitar Bounov, Rami Gökhan Kıcı, Sorin Lerner. 294e5dd7070Spatrick 295e5dd7070SpatrickSplit virtual table groups into separate virtual tables 296e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 297e5dd7070Spatrick 298e5dd7070SpatrickThe Itanium C++ ABI glues multiple individual virtual tables for a class into a combined virtual table (virtual table group). 299e5dd7070SpatrickThe interleaving scheme, however, can only work with individual virtual tables so it must split the combined virtual tables first. 300e5dd7070SpatrickIn comparison, the old scheme does not require the splitting but it is more efficient when the combined virtual tables have been split. 301e5dd7070SpatrickThe `GlobalSplit`_ pass is responsible for splitting combined virtual tables into individual ones. 302e5dd7070Spatrick 303*a9ac8606Spatrick.. _GlobalSplit: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/IPO/GlobalSplit.cpp 304e5dd7070Spatrick 305e5dd7070SpatrickOrder virtual tables by a pre-order traversal of the class hierarchy 306e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 307e5dd7070Spatrick 308e5dd7070SpatrickThis step is common to both the old scheme described above and the interleaving scheme. 309e5dd7070SpatrickFor the interleaving scheme, since the combined virtual tables have been split in the previous step, 310e5dd7070Spatrickthis step ensures that for any class all the compatible virtual tables will appear consecutively. 311e5dd7070SpatrickFor the old scheme, the same property may not hold since it may work on combined virtual tables. 312e5dd7070Spatrick 313e5dd7070SpatrickFor example, consider the following four C++ classes: 314e5dd7070Spatrick 315e5dd7070Spatrick.. code-block:: c++ 316e5dd7070Spatrick 317e5dd7070Spatrick struct A { 318e5dd7070Spatrick virtual void f1(); 319e5dd7070Spatrick }; 320e5dd7070Spatrick 321e5dd7070Spatrick struct B : A { 322e5dd7070Spatrick virtual void f1(); 323e5dd7070Spatrick virtual void f2(); 324e5dd7070Spatrick }; 325e5dd7070Spatrick 326e5dd7070Spatrick struct C : A { 327e5dd7070Spatrick virtual void f1(); 328e5dd7070Spatrick virtual void f3(); 329e5dd7070Spatrick }; 330e5dd7070Spatrick 331e5dd7070Spatrick struct D : B { 332e5dd7070Spatrick virtual void f1(); 333e5dd7070Spatrick virtual void f2(); 334e5dd7070Spatrick }; 335e5dd7070Spatrick 336e5dd7070SpatrickThis step will arrange the virtual tables for A, B, C, and D in the order of *vtable-of-A, vtable-of-B, vtable-of-D, vtable-of-C*. 337e5dd7070Spatrick 338e5dd7070SpatrickInterleave virtual tables 339e5dd7070Spatrick~~~~~~~~~~~~~~~~~~~~~~~~~ 340e5dd7070Spatrick 341e5dd7070SpatrickThis step is where the interleaving scheme deviates from the old scheme. Instead of laying out 342e5dd7070Spatrickwhole virtual tables in the previously computed order, the interleaving scheme lays out table 343e5dd7070Spatrickentries of the virtual tables strategically to ensure the following properties: 344e5dd7070Spatrick 345e5dd7070Spatrick(1) offset-to-top and RTTI fields layout property 346e5dd7070Spatrick 347e5dd7070SpatrickThe Itanium C++ ABI specifies that offset-to-top and RTTI fields appear at the offsets behind the 348e5dd7070Spatrickaddress point. Note that libraries like libcxxabi do assume this property. 349e5dd7070Spatrick 350e5dd7070Spatrick(2) virtual function entry layout property 351e5dd7070Spatrick 352e5dd7070SpatrickFor each virtual function the distance between an virtual table entry for this function and the corresponding 353e5dd7070Spatrickaddress point is always the same. This property ensures that dynamic dispatch still works with the interleaving layout. 354e5dd7070Spatrick 355e5dd7070SpatrickNote that the interleaving scheme in the CFI implementation guarantees both properties above whereas the original scheme proposed 356e5dd7070Spatrickin [1]_ only guarantees the second property. 357e5dd7070Spatrick 358e5dd7070SpatrickTo illustrate how the interleaving algorithm works, let us continue with the running example. 359e5dd7070SpatrickThe algorithm first separates all the virtual table entries into two work lists. To do so, 360e5dd7070Spatrickit starts by allocating two work lists, one initialized with all the offset-to-top entries of virtual tables in the order 361e5dd7070Spatrickcomputed in the last step, one initialized with all the RTTI entries in the same order. 362e5dd7070Spatrick 363e5dd7070Spatrick.. csv-table:: Work list 1 Layout 364e5dd7070Spatrick :header: 0, 1, 2, 3 365e5dd7070Spatrick 366e5dd7070Spatrick A::offset-to-top, B::offset-to-top, D::offset-to-top, C::offset-to-top 367e5dd7070Spatrick 368e5dd7070Spatrick 369e5dd7070Spatrick.. csv-table:: Work list 2 layout 370e5dd7070Spatrick :header: 0, 1, 2, 3, 371e5dd7070Spatrick 372e5dd7070Spatrick &A::rtti, &B::rtti, &D::rtti, &C::rtti 373e5dd7070Spatrick 374e5dd7070SpatrickThen for each virtual function the algorithm goes through all the virtual tables in the previously computed order 375e5dd7070Spatrickto collect all the related entries into a virtual function list. 376e5dd7070SpatrickAfter this step, there are the following virtual function lists: 377e5dd7070Spatrick 378e5dd7070Spatrick.. csv-table:: f1 list 379e5dd7070Spatrick :header: 0, 1, 2, 3 380e5dd7070Spatrick 381e5dd7070Spatrick &A::f1, &B::f1, &D::f1, &C::f1 382e5dd7070Spatrick 383e5dd7070Spatrick 384e5dd7070Spatrick.. csv-table:: f2 list 385e5dd7070Spatrick :header: 0, 1 386e5dd7070Spatrick 387e5dd7070Spatrick &B::f2, &D::f2 388e5dd7070Spatrick 389e5dd7070Spatrick 390e5dd7070Spatrick.. csv-table:: f3 list 391e5dd7070Spatrick :header: 0 392e5dd7070Spatrick 393e5dd7070Spatrick &C::f3 394e5dd7070Spatrick 395e5dd7070SpatrickNext, the algorithm picks the longest remaining virtual function list and appends the whole list to the shortest work list 396e5dd7070Spatrickuntil no function lists are left, and pads the shorter work list so that they are of the same length. 397e5dd7070SpatrickIn the example, f1 list will be first added to work list 1, then f2 list will be added 398e5dd7070Spatrickto work list 2, and finally f3 list will be added to the work list 2. Since work list 1 now has one more entry than 399e5dd7070Spatrickwork list 2, a padding entry is added to the latter. After this step, the two work lists look like: 400e5dd7070Spatrick 401e5dd7070Spatrick.. csv-table:: Work list 1 Layout 402e5dd7070Spatrick :header: 0, 1, 2, 3, 4, 5, 6, 7 403e5dd7070Spatrick 404e5dd7070Spatrick A::offset-to-top, B::offset-to-top, D::offset-to-top, C::offset-to-top, &A::f1, &B::f1, &D::f1, &C::f1 405e5dd7070Spatrick 406e5dd7070Spatrick 407e5dd7070Spatrick.. csv-table:: Work list 2 layout 408e5dd7070Spatrick :header: 0, 1, 2, 3, 4, 5, 6, 7 409e5dd7070Spatrick 410e5dd7070Spatrick &A::rtti, &B::rtti, &D::rtti, &C::rtti, &B::f2, &D::f2, &C::f3, padding 411e5dd7070Spatrick 412e5dd7070SpatrickFinally, the algorithm merges the two work lists into the interleaved layout by alternatingly 413e5dd7070Spatrickmoving the head of each list to the final layout. After this step, the final interleaved layout looks like: 414e5dd7070Spatrick 415e5dd7070Spatrick.. csv-table:: Interleaved layout 416e5dd7070Spatrick :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 417e5dd7070Spatrick 418e5dd7070Spatrick A::offset-to-top, &A::rtti, B::offset-to-top, &B::rtti, D::offset-to-top, &D::rtti, C::offset-to-top, &C::rtti, &A::f1, &B::f2, &B::f1, &D::f2, &D::f1, &C::f3, &C::f1, padding 419e5dd7070Spatrick 420e5dd7070SpatrickIn the above interleaved layout, each virtual table's offset-to-top and RTTI are always adjacent, which shows that the layout has the first property. 421e5dd7070SpatrickFor the second property, let us look at f2 as an example. In the interleaved layout, 422e5dd7070Spatrickthere are two entries for f2: B::f2 and D::f2. The distance between &B::f2 423e5dd7070Spatrickand its address point D::offset-to-top (the entry immediately after &B::rtti) is 5 entry-length, so is the distance between &D::f2 and C::offset-to-top (the entry immediately after &D::rtti). 424e5dd7070Spatrick 425e5dd7070SpatrickForward-Edge CFI for Indirect Function Calls 426e5dd7070Spatrick============================================ 427e5dd7070Spatrick 428e5dd7070SpatrickUnder forward-edge CFI for indirect function calls, each unique function 429e5dd7070Spatricktype has its own bit vector, and at each call site we need to check that the 430e5dd7070Spatrickfunction pointer is a member of the function type's bit vector. This scheme 431e5dd7070Spatrickworks in a similar way to forward-edge CFI for virtual calls, the distinction 432e5dd7070Spatrickbeing that we need to build bit vectors of function entry points rather than 433e5dd7070Spatrickof virtual tables. 434e5dd7070Spatrick 435e5dd7070SpatrickUnlike when re-arranging global variables, we cannot re-arrange functions 436e5dd7070Spatrickin a particular order and base our calculations on the layout of the 437e5dd7070Spatrickfunctions' entry points, as we have no idea how large a particular function 438e5dd7070Spatrickwill end up being (the function sizes could even depend on how we arrange 439e5dd7070Spatrickthe functions). Instead, we build a jump table, which is a block of code 440e5dd7070Spatrickconsisting of one branch instruction for each of the functions in the bit 441e5dd7070Spatrickset that branches to the target function, and redirect any taken function 442e5dd7070Spatrickaddresses to the corresponding jump table entry. In this way, the distance 443e5dd7070Spatrickbetween function entry points is predictable and controllable. In the object 444e5dd7070Spatrickfile's symbol table, the symbols for the target functions also refer to the 445e5dd7070Spatrickjump table entries, so that addresses taken outside the module will pass 446e5dd7070Spatrickany verification done inside the module. 447e5dd7070Spatrick 448e5dd7070SpatrickIn more concrete terms, suppose we have three functions ``f``, ``g``, 449e5dd7070Spatrick``h`` which are all of the same type, and a function foo that returns their 450e5dd7070Spatrickaddresses: 451e5dd7070Spatrick 452e5dd7070Spatrick.. code-block:: none 453e5dd7070Spatrick 454e5dd7070Spatrick f: 455e5dd7070Spatrick mov 0, %eax 456e5dd7070Spatrick ret 457e5dd7070Spatrick 458e5dd7070Spatrick g: 459e5dd7070Spatrick mov 1, %eax 460e5dd7070Spatrick ret 461e5dd7070Spatrick 462e5dd7070Spatrick h: 463e5dd7070Spatrick mov 2, %eax 464e5dd7070Spatrick ret 465e5dd7070Spatrick 466e5dd7070Spatrick foo: 467e5dd7070Spatrick mov f, %eax 468e5dd7070Spatrick mov g, %edx 469e5dd7070Spatrick mov h, %ecx 470e5dd7070Spatrick ret 471e5dd7070Spatrick 472e5dd7070SpatrickOur jump table will (conceptually) look like this: 473e5dd7070Spatrick 474e5dd7070Spatrick.. code-block:: none 475e5dd7070Spatrick 476e5dd7070Spatrick f: 477e5dd7070Spatrick jmp .Ltmp0 ; 5 bytes 478e5dd7070Spatrick int3 ; 1 byte 479e5dd7070Spatrick int3 ; 1 byte 480e5dd7070Spatrick int3 ; 1 byte 481e5dd7070Spatrick 482e5dd7070Spatrick g: 483e5dd7070Spatrick jmp .Ltmp1 ; 5 bytes 484e5dd7070Spatrick int3 ; 1 byte 485e5dd7070Spatrick int3 ; 1 byte 486e5dd7070Spatrick int3 ; 1 byte 487e5dd7070Spatrick 488e5dd7070Spatrick h: 489e5dd7070Spatrick jmp .Ltmp2 ; 5 bytes 490e5dd7070Spatrick int3 ; 1 byte 491e5dd7070Spatrick int3 ; 1 byte 492e5dd7070Spatrick int3 ; 1 byte 493e5dd7070Spatrick 494e5dd7070Spatrick .Ltmp0: 495e5dd7070Spatrick mov 0, %eax 496e5dd7070Spatrick ret 497e5dd7070Spatrick 498e5dd7070Spatrick .Ltmp1: 499e5dd7070Spatrick mov 1, %eax 500e5dd7070Spatrick ret 501e5dd7070Spatrick 502e5dd7070Spatrick .Ltmp2: 503e5dd7070Spatrick mov 2, %eax 504e5dd7070Spatrick ret 505e5dd7070Spatrick 506e5dd7070Spatrick foo: 507e5dd7070Spatrick mov f, %eax 508e5dd7070Spatrick mov g, %edx 509e5dd7070Spatrick mov h, %ecx 510e5dd7070Spatrick ret 511e5dd7070Spatrick 512e5dd7070SpatrickBecause the addresses of ``f``, ``g``, ``h`` are evenly spaced at a power of 513e5dd7070Spatrick2, and function types do not overlap (unlike class types with base classes), 514e5dd7070Spatrickwe can normally apply the `Alignment`_ and `Eliminating Bit Vector Checks 515e5dd7070Spatrickfor All-Ones Bit Vectors`_ optimizations thus simplifying the check at each 516e5dd7070Spatrickcall site to a range and alignment check. 517e5dd7070Spatrick 518e5dd7070SpatrickShared library support 519e5dd7070Spatrick====================== 520e5dd7070Spatrick 521e5dd7070Spatrick**EXPERIMENTAL** 522e5dd7070Spatrick 523e5dd7070SpatrickThe basic CFI mode described above assumes that the application is a 524e5dd7070Spatrickmonolithic binary; at least that all possible virtual/indirect call 525e5dd7070Spatricktargets and the entire class hierarchy are known at link time. The 526e5dd7070Spatrickcross-DSO mode, enabled with **-f[no-]sanitize-cfi-cross-dso** relaxes 527e5dd7070Spatrickthis requirement by allowing virtual and indirect calls to cross the 528e5dd7070SpatrickDSO boundary. 529e5dd7070Spatrick 530e5dd7070SpatrickAssuming the following setup: the binary consists of several 531e5dd7070Spatrickinstrumented and several uninstrumented DSOs. Some of them may be 532e5dd7070Spatrickdlopen-ed/dlclose-d periodically, even frequently. 533e5dd7070Spatrick 534e5dd7070Spatrick - Calls made from uninstrumented DSOs are not checked and just work. 535e5dd7070Spatrick - Calls inside any instrumented DSO are fully protected. 536e5dd7070Spatrick - Calls between different instrumented DSOs are also protected, with 537e5dd7070Spatrick a performance penalty (in addition to the monolithic CFI 538e5dd7070Spatrick overhead). 539e5dd7070Spatrick - Calls from an instrumented DSO to an uninstrumented one are 540e5dd7070Spatrick unchecked and just work, with performance penalty. 541e5dd7070Spatrick - Calls from an instrumented DSO outside of any known DSO are 542e5dd7070Spatrick detected as CFI violations. 543e5dd7070Spatrick 544e5dd7070SpatrickIn the monolithic scheme a call site is instrumented as 545e5dd7070Spatrick 546e5dd7070Spatrick.. code-block:: none 547e5dd7070Spatrick 548e5dd7070Spatrick if (!InlinedFastCheck(f)) 549e5dd7070Spatrick abort(); 550e5dd7070Spatrick call *f 551e5dd7070Spatrick 552e5dd7070SpatrickIn the cross-DSO scheme it becomes 553e5dd7070Spatrick 554e5dd7070Spatrick.. code-block:: none 555e5dd7070Spatrick 556e5dd7070Spatrick if (!InlinedFastCheck(f)) 557e5dd7070Spatrick __cfi_slowpath(CallSiteTypeId, f); 558e5dd7070Spatrick call *f 559e5dd7070Spatrick 560e5dd7070SpatrickCallSiteTypeId 561e5dd7070Spatrick-------------- 562e5dd7070Spatrick 563e5dd7070Spatrick``CallSiteTypeId`` is a stable process-wide identifier of the 564e5dd7070Spatrickcall-site type. For a virtual call site, the type in question is the class 565e5dd7070Spatricktype; for an indirect function call it is the function signature. The 566e5dd7070Spatrickmapping from a type to an identifier is an ABI detail. In the current, 567e5dd7070Spatrickexperimental, implementation the identifier of type T is calculated as 568e5dd7070Spatrickfollows: 569e5dd7070Spatrick 570e5dd7070Spatrick - Obtain the mangled name for "typeinfo name for T". 571e5dd7070Spatrick - Calculate MD5 hash of the name as a string. 572e5dd7070Spatrick - Reinterpret the first 8 bytes of the hash as a little-endian 573e5dd7070Spatrick 64-bit integer. 574e5dd7070Spatrick 575e5dd7070SpatrickIt is possible, but unlikely, that collisions in the 576e5dd7070Spatrick``CallSiteTypeId`` hashing will result in weaker CFI checks that would 577e5dd7070Spatrickstill be conservatively correct. 578e5dd7070Spatrick 579e5dd7070SpatrickCFI_Check 580e5dd7070Spatrick--------- 581e5dd7070Spatrick 582e5dd7070SpatrickIn the general case, only the target DSO knows whether the call to 583e5dd7070Spatrickfunction ``f`` with type ``CallSiteTypeId`` is valid or not. To 584e5dd7070Spatrickexport this information, every DSO implements 585e5dd7070Spatrick 586e5dd7070Spatrick.. code-block:: none 587e5dd7070Spatrick 588e5dd7070Spatrick void __cfi_check(uint64 CallSiteTypeId, void *TargetAddr, void *DiagData) 589e5dd7070Spatrick 590e5dd7070SpatrickThis function provides external modules with access to CFI checks for 591e5dd7070Spatrickthe targets inside this DSO. For each known ``CallSiteTypeId``, this 592e5dd7070Spatrickfunction performs an ``llvm.type.test`` with the corresponding type 593e5dd7070Spatrickidentifier. It reports an error if the type is unknown, or if the 594e5dd7070Spatrickcheck fails. Depending on the values of compiler flags 595e5dd7070Spatrick``-fsanitize-trap`` and ``-fsanitize-recover``, this function may 596e5dd7070Spatrickprint an error, abort and/or return to the caller. ``DiagData`` is an 597e5dd7070Spatrickopaque pointer to the diagnostic information about the error, or 598e5dd7070Spatrick``null`` if the caller does not provide this information. 599e5dd7070Spatrick 600e5dd7070SpatrickThe basic implementation is a large switch statement over all values 601e5dd7070Spatrickof CallSiteTypeId supported by this DSO, and each case is similar to 602e5dd7070Spatrickthe InlinedFastCheck() in the basic CFI mode. 603e5dd7070Spatrick 604e5dd7070SpatrickCFI Shadow 605e5dd7070Spatrick---------- 606e5dd7070Spatrick 607e5dd7070SpatrickTo route CFI checks to the target DSO's __cfi_check function, a 608e5dd7070Spatrickmapping from possible virtual / indirect call targets to the 609e5dd7070Spatrickcorresponding __cfi_check functions is maintained. This mapping is 610e5dd7070Spatrickimplemented as a sparse array of 2 bytes for every possible page (4096 611e5dd7070Spatrickbytes) of memory. The table is kept readonly most of the time. 612e5dd7070Spatrick 613e5dd7070SpatrickThere are 3 types of shadow values: 614e5dd7070Spatrick 615e5dd7070Spatrick - Address in a CFI-instrumented DSO. 616e5dd7070Spatrick - Unchecked address (a “trusted” non-instrumented DSO). Encoded as 617e5dd7070Spatrick value 0xFFFF. 618e5dd7070Spatrick - Invalid address (everything else). Encoded as value 0. 619e5dd7070Spatrick 620e5dd7070SpatrickFor a CFI-instrumented DSO, a shadow value encodes the address of the 621e5dd7070Spatrick__cfi_check function for all call targets in the corresponding memory 622e5dd7070Spatrickpage. If Addr is the target address, and V is the shadow value, then 623e5dd7070Spatrickthe address of __cfi_check is calculated as 624e5dd7070Spatrick 625e5dd7070Spatrick.. code-block:: none 626e5dd7070Spatrick 627e5dd7070Spatrick __cfi_check = AlignUpTo(Addr, 4096) - (V + 1) * 4096 628e5dd7070Spatrick 629e5dd7070SpatrickThis works as long as __cfi_check is aligned by 4096 bytes and located 630e5dd7070Spatrickbelow any call targets in its DSO, but not more than 256MB apart from 631e5dd7070Spatrickthem. 632e5dd7070Spatrick 633e5dd7070SpatrickCFI_SlowPath 634e5dd7070Spatrick------------ 635e5dd7070Spatrick 636e5dd7070SpatrickThe slow path check is implemented in a runtime support library as 637e5dd7070Spatrick 638e5dd7070Spatrick.. code-block:: none 639e5dd7070Spatrick 640e5dd7070Spatrick void __cfi_slowpath(uint64 CallSiteTypeId, void *TargetAddr) 641e5dd7070Spatrick void __cfi_slowpath_diag(uint64 CallSiteTypeId, void *TargetAddr, void *DiagData) 642e5dd7070Spatrick 643e5dd7070SpatrickThese functions loads a shadow value for ``TargetAddr``, finds the 644e5dd7070Spatrickaddress of ``__cfi_check`` as described above and calls 645e5dd7070Spatrickthat. ``DiagData`` is an opaque pointer to diagnostic data which is 646e5dd7070Spatrickpassed verbatim to ``__cfi_check``, and ``__cfi_slowpath`` passes 647e5dd7070Spatrick``nullptr`` instead. 648e5dd7070Spatrick 649e5dd7070SpatrickCompiler-RT library contains reference implementations of slowpath 650e5dd7070Spatrickfunctions, but they have unresolvable issues with correctness and 651e5dd7070Spatrickperformance in the handling of dlopen(). It is recommended that 652e5dd7070Spatrickplatforms provide their own implementations, usually as part of libc 653e5dd7070Spatrickor libdl. 654e5dd7070Spatrick 655e5dd7070SpatrickPosition-independent executable requirement 656e5dd7070Spatrick------------------------------------------- 657e5dd7070Spatrick 658e5dd7070SpatrickCross-DSO CFI mode requires that the main executable is built as PIE. 659e5dd7070SpatrickIn non-PIE executables the address of an external function (taken from 660e5dd7070Spatrickthe main executable) is the address of that function’s PLT record in 661e5dd7070Spatrickthe main executable. This would break the CFI checks. 662e5dd7070Spatrick 663e5dd7070SpatrickBackward-edge CFI for return statements (RCFI) 664e5dd7070Spatrick============================================== 665e5dd7070Spatrick 666e5dd7070SpatrickThis section is a proposal. As of March 2017 it is not implemented. 667e5dd7070Spatrick 668e5dd7070SpatrickBackward-edge control flow (`RET` instructions) can be hijacked 669e5dd7070Spatrickvia overwriting the return address (`RA`) on stack. 670e5dd7070SpatrickVarious mitigation techniques (e.g. `SafeStack`_, `RFG`_, `Intel CET`_) 671e5dd7070Spatricktry to detect or prevent `RA` corruption on stack. 672e5dd7070Spatrick 673e5dd7070SpatrickRCFI enforces the expected control flow in several different ways described below. 674e5dd7070SpatrickRCFI heavily relies on LTO. 675e5dd7070Spatrick 676e5dd7070SpatrickLeaf Functions 677e5dd7070Spatrick-------------- 678e5dd7070SpatrickIf `f()` is a leaf function (i.e. it has no calls 679e5dd7070Spatrickexcept maybe no-return calls) it can be called using a special calling convention 680e5dd7070Spatrickthat stores `RA` in a dedicated register `R` before the `CALL` instruction. 681e5dd7070Spatrick`f()` does not spill `R` and does not use the `RET` instruction, 682e5dd7070Spatrickinstead it uses the value in `R` to `JMP` to `RA`. 683e5dd7070Spatrick 684e5dd7070SpatrickThis flavour of CFI is *precise*, i.e. the function is guaranteed to return 685e5dd7070Spatrickto the point exactly following the call. 686e5dd7070Spatrick 687e5dd7070SpatrickAn alternative approach is to 688e5dd7070Spatrickcopy `RA` from stack to `R` in the first instruction of `f()`, 689e5dd7070Spatrickthen `JMP` to `R`. 690e5dd7070SpatrickThis approach is simpler to implement (does not require changing the caller) 691e5dd7070Spatrickbut weaker (there is a small window when `RA` is actually stored on stack). 692e5dd7070Spatrick 693e5dd7070Spatrick 694e5dd7070SpatrickFunctions called once 695e5dd7070Spatrick--------------------- 696e5dd7070SpatrickSuppose `f()` is called in just one place in the program 697e5dd7070Spatrick(assuming we can verify this in LTO mode). 698e5dd7070SpatrickIn this case we can replace the `RET` instruction with a `JMP` instruction 699e5dd7070Spatrickwith the immediate constant for `RA`. 700e5dd7070SpatrickThis will *precisely* enforce the return control flow no matter what is stored on stack. 701e5dd7070Spatrick 702e5dd7070SpatrickAnother variant is to compare `RA` on stack with the known constant and abort 703e5dd7070Spatrickif they don't match; then `JMP` to the known constant address. 704e5dd7070Spatrick 705e5dd7070SpatrickFunctions called in a small number of call sites 706e5dd7070Spatrick------------------------------------------------ 707e5dd7070SpatrickWe may extend the above approach to cases where `f()` 708e5dd7070Spatrickis called more than once (but still a small number of times). 709e5dd7070SpatrickWith LTO we know all possible values of `RA` and we check them 710e5dd7070Spatrickone-by-one (or using binary search) against the value on stack. 711e5dd7070SpatrickIf the match is found, we `JMP` to the known constant address, otherwise abort. 712e5dd7070Spatrick 713e5dd7070SpatrickThis protection is *near-precise*, i.e. it guarantees that the control flow will 714e5dd7070Spatrickbe transferred to one of the valid return addresses for this function, 715e5dd7070Spatrickbut not necessary to the point of the most recent `CALL`. 716e5dd7070Spatrick 717e5dd7070SpatrickGeneral case 718e5dd7070Spatrick------------ 719e5dd7070SpatrickFor functions called multiple times a *return jump table* is constructed 720e5dd7070Spatrickin the same manner as jump tables for indirect function calls (see above). 721*a9ac8606SpatrickThe correct jump table entry (or its index) is passed by `CALL` to `f()` 722e5dd7070Spatrick(as an extra argument) and then spilled to stack. 723e5dd7070SpatrickThe `RET` instruction is replaced with a load of the jump table entry, 724e5dd7070Spatrickjump table range check, and `JMP` to the jump table entry. 725e5dd7070Spatrick 726e5dd7070SpatrickThis protection is also *near-precise*. 727e5dd7070Spatrick 728e5dd7070SpatrickReturns from functions called indirectly 729e5dd7070Spatrick---------------------------------------- 730e5dd7070Spatrick 731e5dd7070SpatrickIf a function is called indirectly, the return jump table is constructed for the 732e5dd7070Spatrickequivalence class of functions instead of a single function. 733e5dd7070Spatrick 734e5dd7070SpatrickCross-DSO calls 735e5dd7070Spatrick--------------- 736e5dd7070SpatrickConsider two instrumented DSOs, `A` and `B`. `A` defines `f()` and `B` calls it. 737e5dd7070Spatrick 738e5dd7070SpatrickThis case will be handled similarly to the cross-DSO scheme using the slow path callback. 739e5dd7070Spatrick 740e5dd7070SpatrickNon-goals 741e5dd7070Spatrick--------- 742e5dd7070Spatrick 743e5dd7070SpatrickRCFI does not protect `RET` instructions: 744e5dd7070Spatrick * in non-instrumented DSOs, 745e5dd7070Spatrick * in instrumented DSOs for functions that are called from non-instrumented DSOs, 746e5dd7070Spatrick * embedded into other instructions (e.g. `0f4fc3 cmovg %ebx,%eax`). 747e5dd7070Spatrick 748e5dd7070Spatrick.. _SafeStack: https://clang.llvm.org/docs/SafeStack.html 749e5dd7070Spatrick.. _RFG: https://xlab.tencent.com/en/2016/11/02/return-flow-guard 750e5dd7070Spatrick.. _Intel CET: https://software.intel.com/en-us/blogs/2016/06/09/intel-release-new-technology-specifications-protect-rop-attacks 751e5dd7070Spatrick 752e5dd7070SpatrickHardware support 753e5dd7070Spatrick================ 754e5dd7070Spatrick 755e5dd7070SpatrickWe believe that the above design can be efficiently implemented in hardware. 756e5dd7070SpatrickA single new instruction added to an ISA would allow to perform the forward-edge CFI check 757e5dd7070Spatrickwith fewer bytes per check (smaller code size overhead) and potentially more 758e5dd7070Spatrickefficiently. The current software-only instrumentation requires at least 759e5dd7070Spatrick32-bytes per check (on x86_64). 760e5dd7070SpatrickA hardware instruction may probably be less than ~ 12 bytes. 761e5dd7070SpatrickSuch instruction would check that the argument pointer is in-bounds, 762e5dd7070Spatrickand is properly aligned, and if the checks fail it will either trap (in monolithic scheme) 763e5dd7070Spatrickor call the slow path function (cross-DSO scheme). 764e5dd7070SpatrickThe bit vector lookup is probably too complex for a hardware implementation. 765e5dd7070Spatrick 766e5dd7070Spatrick.. code-block:: none 767e5dd7070Spatrick 768e5dd7070Spatrick // This instruction checks that 'Ptr' 769e5dd7070Spatrick // * is aligned by (1 << kAlignment) and 770e5dd7070Spatrick // * is inside [kRangeBeg, kRangeBeg+(kRangeSize<<kAlignment)) 771e5dd7070Spatrick // and if the check fails it jumps to the given target (slow path). 772e5dd7070Spatrick // 773e5dd7070Spatrick // 'Ptr' is a register, pointing to the virtual function table 774e5dd7070Spatrick // or to the function which we need to check. We may require an explicit 775e5dd7070Spatrick // fixed register to be used. 776e5dd7070Spatrick // 'kAlignment' is a 4-bit constant. 777e5dd7070Spatrick // 'kRangeSize' is a ~20-bit constant. 778e5dd7070Spatrick // 'kRangeBeg' is a PC-relative constant (~28 bits) 779e5dd7070Spatrick // pointing to the beginning of the allowed range for 'Ptr'. 780e5dd7070Spatrick // 'kFailedCheckTarget': is a PC-relative constant (~28 bits) 781e5dd7070Spatrick // representing the target to branch to when the check fails. 782e5dd7070Spatrick // If kFailedCheckTarget==0, the process will trap 783e5dd7070Spatrick // (monolithic binary scheme). 784e5dd7070Spatrick // Otherwise it will jump to a handler that implements `CFI_SlowPath` 785e5dd7070Spatrick // (cross-DSO scheme). 786e5dd7070Spatrick CFI_Check(Ptr, kAlignment, kRangeSize, kRangeBeg, kFailedCheckTarget) { 787e5dd7070Spatrick if (Ptr < kRangeBeg || 788e5dd7070Spatrick Ptr >= kRangeBeg + (kRangeSize << kAlignment) || 789e5dd7070Spatrick Ptr & ((1 << kAlignment) - 1)) 790e5dd7070Spatrick Jump(kFailedCheckTarget); 791e5dd7070Spatrick } 792e5dd7070Spatrick 793e5dd7070SpatrickAn alternative and more compact encoding would not use `kFailedCheckTarget`, 794e5dd7070Spatrickand will trap on check failure instead. 795e5dd7070SpatrickThis will allow us to fit the instruction into **8-9 bytes**. 796e5dd7070SpatrickThe cross-DSO checks will be performed by a trap handler and 797e5dd7070Spatrickperformance-critical ones will have to be black-listed and checked using the 798e5dd7070Spatricksoftware-only scheme. 799e5dd7070Spatrick 800e5dd7070SpatrickNote that such hardware extension would be complementary to checks 801e5dd7070Spatrickat the callee side, such as e.g. **Intel ENDBRANCH**. 802e5dd7070SpatrickMoreover, CFI would have two benefits over ENDBRANCH: a) precision and b) 803e5dd7070Spatrickability to protect against invalid casts between polymorphic types. 804