ControlFlowIntegrityDesign.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ===========================================
  2. Control Flow Integrity Design Documentation
  3. ===========================================
  4. This page documents the design of the :doc:`ControlFlowIntegrity` schemes
  5. supported by Clang.
  6. Forward-Edge CFI for Virtual Calls
  7. ==================================
  8. This scheme works by allocating, for each static type used to make a virtual
  9. call, a region of read-only storage in the object file holding a bit vector
  10. that maps onto to the region of storage used for those virtual tables. Each
  11. set bit in the bit vector corresponds to the `address point`_ for a virtual
  12. table compatible with the static type for which the bit vector is being built.
  13. For example, consider the following three C++ classes:
  14. .. code-block:: c++
  15. struct A {
  16. virtual void f1();
  17. virtual void f2();
  18. virtual void f3();
  19. };
  20. struct B : A {
  21. virtual void f1();
  22. virtual void f2();
  23. virtual void f3();
  24. };
  25. struct C : A {
  26. virtual void f1();
  27. virtual void f2();
  28. virtual void f3();
  29. };
  30. The scheme will cause the virtual tables for A, B and C to be laid out
  31. consecutively:
  32. .. csv-table:: Virtual Table Layout for A, B, C
  33. :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  34. 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
  35. The bit vector for static types A, B and C will look like this:
  36. .. csv-table:: Bit Vectors for A, B, C
  37. :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  38. A, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0
  39. B, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  40. C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
  41. Bit vectors are represented in the object file as byte arrays. By loading
  42. from indexed offsets into the byte array and applying a mask, a program can
  43. test bits from the bit set with a relatively short instruction sequence. Bit
  44. vectors may overlap so long as they use different bits. For the full details,
  45. see the `ByteArrayBuilder`_ class.
  46. In this case, assuming A is laid out at offset 0 in bit 0, B at offset 0 in
  47. bit 1 and C at offset 0 in bit 2, the byte array would look like this:
  48. .. code-block:: c++
  49. char bits[] = { 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 0 };
  50. To emit a virtual call, the compiler will assemble code that checks that
  51. the object's virtual table pointer is in-bounds and aligned and that the
  52. relevant bit is set in the bit vector.
  53. For example on x86 a typical virtual call may look like this:
  54. .. code-block:: none
  55. ca7fbb: 48 8b 0f mov (%rdi),%rcx
  56. ca7fbe: 48 8d 15 c3 42 fb 07 lea 0x7fb42c3(%rip),%rdx
  57. ca7fc5: 48 89 c8 mov %rcx,%rax
  58. ca7fc8: 48 29 d0 sub %rdx,%rax
  59. ca7fcb: 48 c1 c0 3d rol $0x3d,%rax
  60. ca7fcf: 48 3d 7f 01 00 00 cmp $0x17f,%rax
  61. ca7fd5: 0f 87 36 05 00 00 ja ca8511
  62. ca7fdb: 48 8d 15 c0 0b f7 06 lea 0x6f70bc0(%rip),%rdx
  63. ca7fe2: f6 04 10 10 testb $0x10,(%rax,%rdx,1)
  64. ca7fe6: 0f 84 25 05 00 00 je ca8511
  65. ca7fec: ff 91 98 00 00 00 callq *0x98(%rcx)
  66. [...]
  67. ca8511: 0f 0b ud2
  68. The compiler relies on co-operation from the linker in order to assemble
  69. the bit vectors for the whole program. It currently does this using LLVM's
  70. `bit sets`_ mechanism together with link-time optimization.
  71. .. _address point: https://mentorembedded.github.io/cxx-abi/abi.html#vtable-general
  72. .. _bit sets: http://llvm.org/docs/BitSets.html
  73. .. _ByteArrayBuilder: http://llvm.org/docs/doxygen/html/structllvm_1_1ByteArrayBuilder.html
  74. Optimizations
  75. -------------
  76. The scheme as described above is the fully general variant of the scheme.
  77. Most of the time we are able to apply one or more of the following
  78. optimizations to improve binary size or performance.
  79. In fact, if you try the above example with the current version of the
  80. compiler, you will probably find that it will not use the described virtual
  81. table layout or machine instructions. Some of the optimizations we are about
  82. to introduce cause the compiler to use a different layout or a different
  83. sequence of machine instructions.
  84. Stripping Leading/Trailing Zeros in Bit Vectors
  85. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. If a bit vector contains leading or trailing zeros, we can strip them from
  87. the vector. The compiler will emit code to check if the pointer is in range
  88. of the region covered by ones, and perform the bit vector check using a
  89. truncated version of the bit vector. For example, the bit vectors for our
  90. example class hierarchy will be emitted like this:
  91. .. csv-table:: Bit Vectors for A, B, C
  92. :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  93. A, , , 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, ,
  94. B, , , , , , , , 1, , , , , , ,
  95. C, , , , , , , , , , , , , 1, ,
  96. Short Inline Bit Vectors
  97. ~~~~~~~~~~~~~~~~~~~~~~~~
  98. If the vector is sufficiently short, we can represent it as an inline constant
  99. on x86. This saves us a few instructions when reading the correct element
  100. of the bit vector.
  101. If the bit vector fits in 32 bits, the code looks like this:
  102. .. code-block:: none
  103. dc2: 48 8b 03 mov (%rbx),%rax
  104. dc5: 48 8d 15 14 1e 00 00 lea 0x1e14(%rip),%rdx
  105. dcc: 48 89 c1 mov %rax,%rcx
  106. dcf: 48 29 d1 sub %rdx,%rcx
  107. dd2: 48 c1 c1 3d rol $0x3d,%rcx
  108. dd6: 48 83 f9 03 cmp $0x3,%rcx
  109. dda: 77 2f ja e0b <main+0x9b>
  110. ddc: ba 09 00 00 00 mov $0x9,%edx
  111. de1: 0f a3 ca bt %ecx,%edx
  112. de4: 73 25 jae e0b <main+0x9b>
  113. de6: 48 89 df mov %rbx,%rdi
  114. de9: ff 10 callq *(%rax)
  115. [...]
  116. e0b: 0f 0b ud2
  117. Or if the bit vector fits in 64 bits:
  118. .. code-block:: none
  119. 11a6: 48 8b 03 mov (%rbx),%rax
  120. 11a9: 48 8d 15 d0 28 00 00 lea 0x28d0(%rip),%rdx
  121. 11b0: 48 89 c1 mov %rax,%rcx
  122. 11b3: 48 29 d1 sub %rdx,%rcx
  123. 11b6: 48 c1 c1 3d rol $0x3d,%rcx
  124. 11ba: 48 83 f9 2a cmp $0x2a,%rcx
  125. 11be: 77 35 ja 11f5 <main+0xb5>
  126. 11c0: 48 ba 09 00 00 00 00 movabs $0x40000000009,%rdx
  127. 11c7: 04 00 00
  128. 11ca: 48 0f a3 ca bt %rcx,%rdx
  129. 11ce: 73 25 jae 11f5 <main+0xb5>
  130. 11d0: 48 89 df mov %rbx,%rdi
  131. 11d3: ff 10 callq *(%rax)
  132. [...]
  133. 11f5: 0f 0b ud2
  134. If the bit vector consists of a single bit, there is only one possible
  135. virtual table, and the check can consist of a single equality comparison:
  136. .. code-block:: none
  137. 9a2: 48 8b 03 mov (%rbx),%rax
  138. 9a5: 48 8d 0d a4 13 00 00 lea 0x13a4(%rip),%rcx
  139. 9ac: 48 39 c8 cmp %rcx,%rax
  140. 9af: 75 25 jne 9d6 <main+0x86>
  141. 9b1: 48 89 df mov %rbx,%rdi
  142. 9b4: ff 10 callq *(%rax)
  143. [...]
  144. 9d6: 0f 0b ud2
  145. Virtual Table Layout
  146. ~~~~~~~~~~~~~~~~~~~~
  147. The compiler lays out classes of disjoint hierarchies in separate regions
  148. of the object file. At worst, bit vectors in disjoint hierarchies only
  149. need to cover their disjoint hierarchy. But the closer that classes in
  150. sub-hierarchies are laid out to each other, the smaller the bit vectors for
  151. those sub-hierarchies need to be (see "Stripping Leading/Trailing Zeros in Bit
  152. Vectors" above). The `GlobalLayoutBuilder`_ class is responsible for laying
  153. out the globals efficiently to minimize the sizes of the underlying bitsets.
  154. .. _GlobalLayoutBuilder: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/LowerBitSets.h?view=markup
  155. Alignment
  156. ~~~~~~~~~
  157. If all gaps between address points in a particular bit vector are multiples
  158. of powers of 2, the compiler can compress the bit vector by strengthening
  159. the alignment requirements of the virtual table pointer. For example, given
  160. this class hierarchy:
  161. .. code-block:: c++
  162. struct A {
  163. virtual void f1();
  164. virtual void f2();
  165. };
  166. struct B : A {
  167. virtual void f1();
  168. virtual void f2();
  169. virtual void f3();
  170. virtual void f4();
  171. virtual void f5();
  172. virtual void f6();
  173. };
  174. struct C : A {
  175. virtual void f1();
  176. virtual void f2();
  177. };
  178. The virtual tables will be laid out like this:
  179. .. csv-table:: Virtual Table Layout for A, B, C
  180. :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
  181. 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
  182. Notice that each address point for A is separated by 4 words. This lets us
  183. emit a compressed bit vector for A that looks like this:
  184. .. csv-table::
  185. :header: 2, 6, 10, 14
  186. 1, 1, 0, 1
  187. At call sites, the compiler will strengthen the alignment requirements by
  188. using a different rotate count. For example, on a 64-bit machine where the
  189. address points are 4-word aligned (as in A from our example), the ``rol``
  190. instruction may look like this:
  191. .. code-block:: none
  192. dd2: 48 c1 c1 3b rol $0x3b,%rcx
  193. Padding to Powers of 2
  194. ~~~~~~~~~~~~~~~~~~~~~~
  195. Of course, this alignment scheme works best if the address points are
  196. in fact aligned correctly. To make this more likely to happen, we insert
  197. padding between virtual tables that in many cases aligns address points to
  198. a power of 2. Specifically, our padding aligns virtual tables to the next
  199. highest power of 2 bytes; because address points for specific base classes
  200. normally appear at fixed offsets within the virtual table, this normally
  201. has the effect of aligning the address points as well.
  202. This scheme introduces tradeoffs between decreased space overhead for
  203. instructions and bit vectors and increased overhead in the form of padding. We
  204. therefore limit the amount of padding so that we align to no more than 128
  205. bytes. This number was found experimentally to provide a good tradeoff.
  206. Eliminating Bit Vector Checks for All-Ones Bit Vectors
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. If the bit vector is all ones, the bit vector check is redundant; we simply
  209. need to check that the address is in range and well aligned. This is more
  210. likely to occur if the virtual tables are padded.