ControlFlowIntegrityDesign.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. `type metadata`_ mechanism together with link-time optimization.
  71. .. _address point: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
  72. .. _type metadata: https://llvm.org/docs/TypeMetadata.html
  73. .. _ByteArrayBuilder: https://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: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
  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.
  211. Forward-Edge CFI for Virtual Calls by Interleaving Virtual Tables
  212. -----------------------------------------------------------------
  213. Dimitar et. al. proposed a novel approach that interleaves virtual tables in [1]_.
  214. This approach is more efficient in terms of space because padding and bit vectors are no longer needed.
  215. At the same time, it is also more efficient in terms of performance because in the interleaved layout
  216. address points of the virtual tables are consecutive, thus the validity check of a virtual
  217. vtable pointer is always a range check.
  218. At a high level, the interleaving scheme consists of three steps: 1) split virtual table groups into
  219. separate virtual tables, 2) order virtual tables by a pre-order traversal of the class hierarchy
  220. and 3) interleave virtual tables.
  221. The interleaving scheme implemented in LLVM is inspired by [1]_ but has its own
  222. enhancements (more in `Interleave virtual tables`_).
  223. .. [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.
  224. Split virtual table groups into separate virtual tables
  225. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. The Itanium C++ ABI glues multiple individual virtual tables for a class into a combined virtual table (virtual table group).
  227. The interleaving scheme, however, can only work with individual virtual tables so it must split the combined virtual tables first.
  228. In comparison, the old scheme does not require the splitting but it is more efficient when the combined virtual tables have been split.
  229. The `GlobalSplit`_ pass is responsible for splitting combined virtual tables into individual ones.
  230. .. _GlobalSplit: https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/IPO/GlobalSplit.cpp
  231. Order virtual tables by a pre-order traversal of the class hierarchy
  232. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  233. This step is common to both the old scheme described above and the interleaving scheme.
  234. For the interleaving scheme, since the combined virtual tables have been split in the previous step,
  235. this step ensures that for any class all the compatible virtual tables will appear consecutively.
  236. For the old scheme, the same property may not hold since it may work on combined virtual tables.
  237. For example, consider the following four C++ classes:
  238. .. code-block:: c++
  239. struct A {
  240. virtual void f1();
  241. };
  242. struct B : A {
  243. virtual void f1();
  244. virtual void f2();
  245. };
  246. struct C : A {
  247. virtual void f1();
  248. virtual void f3();
  249. };
  250. struct D : B {
  251. virtual void f1();
  252. virtual void f2();
  253. };
  254. This 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*.
  255. Interleave virtual tables
  256. ~~~~~~~~~~~~~~~~~~~~~~~~~
  257. This step is where the interleaving scheme deviates from the old scheme. Instead of laying out
  258. whole virtual tables in the previously computed order, the interleaving scheme lays out table
  259. entries of the virtual tables strategically to ensure the following properties:
  260. (1) offset-to-top and RTTI fields layout property
  261. The Itanium C++ ABI specifies that offset-to-top and RTTI fields appear at the offsets behind the
  262. address point. Note that libraries like libcxxabi do assume this property.
  263. (2) virtual function entry layout property
  264. For each virtual function the distance between an virtual table entry for this function and the corresponding
  265. address point is always the same. This property ensures that dynamic dispatch still works with the interleaving layout.
  266. Note that the interleaving scheme in the CFI implementation guarantees both properties above whereas the original scheme proposed
  267. in [1]_ only guarantees the second property.
  268. To illustrate how the interleaving algorithm works, let us continue with the running example.
  269. The algorithm first separates all the virtual table entries into two work lists. To do so,
  270. it starts by allocating two work lists, one initialized with all the offset-to-top entries of virtual tables in the order
  271. computed in the last step, one initialized with all the RTTI entries in the same order.
  272. .. csv-table:: Work list 1 Layout
  273. :header: 0, 1, 2, 3
  274. A::offset-to-top, B::offset-to-top, D::offset-to-top, C::offset-to-top
  275. .. csv-table:: Work list 2 layout
  276. :header: 0, 1, 2, 3,
  277. &A::rtti, &B::rtti, &D::rtti, &C::rtti
  278. Then for each virtual function the algorithm goes through all the virtual tables in the previously computed order
  279. to collect all the related entries into a virtual function list.
  280. After this step, there are the following virtual function lists:
  281. .. csv-table:: f1 list
  282. :header: 0, 1, 2, 3
  283. &A::f1, &B::f1, &D::f1, &C::f1
  284. .. csv-table:: f2 list
  285. :header: 0, 1
  286. &B::f2, &D::f2
  287. .. csv-table:: f3 list
  288. :header: 0
  289. &C::f3
  290. Next, the algorithm picks the longest remaining virtual function list and appends the whole list to the shortest work list
  291. until no function lists are left, and pads the shorter work list so that they are of the same length.
  292. In the example, f1 list will be first added to work list 1, then f2 list will be added
  293. to 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
  294. work list 2, a padding entry is added to the latter. After this step, the two work lists look like:
  295. .. csv-table:: Work list 1 Layout
  296. :header: 0, 1, 2, 3, 4, 5, 6, 7
  297. A::offset-to-top, B::offset-to-top, D::offset-to-top, C::offset-to-top, &A::f1, &B::f1, &D::f1, &C::f1
  298. .. csv-table:: Work list 2 layout
  299. :header: 0, 1, 2, 3, 4, 5, 6, 7
  300. &A::rtti, &B::rtti, &D::rtti, &C::rtti, &B::f2, &D::f2, &C::f3, padding
  301. Finally, the algorithm merges the two work lists into the interleaved layout by alternatingly
  302. moving the head of each list to the final layout. After this step, the final interleaved layout looks like:
  303. .. csv-table:: Interleaved layout
  304. :header: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
  305. 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
  306. In 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.
  307. For the second property, let us look at f2 as an example. In the interleaved layout,
  308. there are two entries for f2: B::f2 and D::f2. The distance between &B::f2
  309. and 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).
  310. Forward-Edge CFI for Indirect Function Calls
  311. ============================================
  312. Under forward-edge CFI for indirect function calls, each unique function
  313. type has its own bit vector, and at each call site we need to check that the
  314. function pointer is a member of the function type's bit vector. This scheme
  315. works in a similar way to forward-edge CFI for virtual calls, the distinction
  316. being that we need to build bit vectors of function entry points rather than
  317. of virtual tables.
  318. Unlike when re-arranging global variables, we cannot re-arrange functions
  319. in a particular order and base our calculations on the layout of the
  320. functions' entry points, as we have no idea how large a particular function
  321. will end up being (the function sizes could even depend on how we arrange
  322. the functions). Instead, we build a jump table, which is a block of code
  323. consisting of one branch instruction for each of the functions in the bit
  324. set that branches to the target function, and redirect any taken function
  325. addresses to the corresponding jump table entry. In this way, the distance
  326. between function entry points is predictable and controllable. In the object
  327. file's symbol table, the symbols for the target functions also refer to the
  328. jump table entries, so that addresses taken outside the module will pass
  329. any verification done inside the module.
  330. In more concrete terms, suppose we have three functions ``f``, ``g``,
  331. ``h`` which are all of the same type, and a function foo that returns their
  332. addresses:
  333. .. code-block:: none
  334. f:
  335. mov 0, %eax
  336. ret
  337. g:
  338. mov 1, %eax
  339. ret
  340. h:
  341. mov 2, %eax
  342. ret
  343. foo:
  344. mov f, %eax
  345. mov g, %edx
  346. mov h, %ecx
  347. ret
  348. Our jump table will (conceptually) look like this:
  349. .. code-block:: none
  350. f:
  351. jmp .Ltmp0 ; 5 bytes
  352. int3 ; 1 byte
  353. int3 ; 1 byte
  354. int3 ; 1 byte
  355. g:
  356. jmp .Ltmp1 ; 5 bytes
  357. int3 ; 1 byte
  358. int3 ; 1 byte
  359. int3 ; 1 byte
  360. h:
  361. jmp .Ltmp2 ; 5 bytes
  362. int3 ; 1 byte
  363. int3 ; 1 byte
  364. int3 ; 1 byte
  365. .Ltmp0:
  366. mov 0, %eax
  367. ret
  368. .Ltmp1:
  369. mov 1, %eax
  370. ret
  371. .Ltmp2:
  372. mov 2, %eax
  373. ret
  374. foo:
  375. mov f, %eax
  376. mov g, %edx
  377. mov h, %ecx
  378. ret
  379. Because the addresses of ``f``, ``g``, ``h`` are evenly spaced at a power of
  380. 2, and function types do not overlap (unlike class types with base classes),
  381. we can normally apply the `Alignment`_ and `Eliminating Bit Vector Checks
  382. for All-Ones Bit Vectors`_ optimizations thus simplifying the check at each
  383. call site to a range and alignment check.
  384. Shared library support
  385. ======================
  386. **EXPERIMENTAL**
  387. The basic CFI mode described above assumes that the application is a
  388. monolithic binary; at least that all possible virtual/indirect call
  389. targets and the entire class hierarchy are known at link time. The
  390. cross-DSO mode, enabled with **-f[no-]sanitize-cfi-cross-dso** relaxes
  391. this requirement by allowing virtual and indirect calls to cross the
  392. DSO boundary.
  393. Assuming the following setup: the binary consists of several
  394. instrumented and several uninstrumented DSOs. Some of them may be
  395. dlopen-ed/dlclose-d periodically, even frequently.
  396. - Calls made from uninstrumented DSOs are not checked and just work.
  397. - Calls inside any instrumented DSO are fully protected.
  398. - Calls between different instrumented DSOs are also protected, with
  399. a performance penalty (in addition to the monolithic CFI
  400. overhead).
  401. - Calls from an instrumented DSO to an uninstrumented one are
  402. unchecked and just work, with performance penalty.
  403. - Calls from an instrumented DSO outside of any known DSO are
  404. detected as CFI violations.
  405. In the monolithic scheme a call site is instrumented as
  406. .. code-block:: none
  407. if (!InlinedFastCheck(f))
  408. abort();
  409. call *f
  410. In the cross-DSO scheme it becomes
  411. .. code-block:: none
  412. if (!InlinedFastCheck(f))
  413. __cfi_slowpath(CallSiteTypeId, f);
  414. call *f
  415. CallSiteTypeId
  416. --------------
  417. ``CallSiteTypeId`` is a stable process-wide identifier of the
  418. call-site type. For a virtual call site, the type in question is the class
  419. type; for an indirect function call it is the function signature. The
  420. mapping from a type to an identifier is an ABI detail. In the current,
  421. experimental, implementation the identifier of type T is calculated as
  422. follows:
  423. - Obtain the mangled name for "typeinfo name for T".
  424. - Calculate MD5 hash of the name as a string.
  425. - Reinterpret the first 8 bytes of the hash as a little-endian
  426. 64-bit integer.
  427. It is possible, but unlikely, that collisions in the
  428. ``CallSiteTypeId`` hashing will result in weaker CFI checks that would
  429. still be conservatively correct.
  430. CFI_Check
  431. ---------
  432. In the general case, only the target DSO knows whether the call to
  433. function ``f`` with type ``CallSiteTypeId`` is valid or not. To
  434. export this information, every DSO implements
  435. .. code-block:: none
  436. void __cfi_check(uint64 CallSiteTypeId, void *TargetAddr, void *DiagData)
  437. This function provides external modules with access to CFI checks for
  438. the targets inside this DSO. For each known ``CallSiteTypeId``, this
  439. function performs an ``llvm.type.test`` with the corresponding type
  440. identifier. It reports an error if the type is unknown, or if the
  441. check fails. Depending on the values of compiler flags
  442. ``-fsanitize-trap`` and ``-fsanitize-recover``, this function may
  443. print an error, abort and/or return to the caller. ``DiagData`` is an
  444. opaque pointer to the diagnostic information about the error, or
  445. ``null`` if the caller does not provide this information.
  446. The basic implementation is a large switch statement over all values
  447. of CallSiteTypeId supported by this DSO, and each case is similar to
  448. the InlinedFastCheck() in the basic CFI mode.
  449. CFI Shadow
  450. ----------
  451. To route CFI checks to the target DSO's __cfi_check function, a
  452. mapping from possible virtual / indirect call targets to the
  453. corresponding __cfi_check functions is maintained. This mapping is
  454. implemented as a sparse array of 2 bytes for every possible page (4096
  455. bytes) of memory. The table is kept readonly most of the time.
  456. There are 3 types of shadow values:
  457. - Address in a CFI-instrumented DSO.
  458. - Unchecked address (a “trusted” non-instrumented DSO). Encoded as
  459. value 0xFFFF.
  460. - Invalid address (everything else). Encoded as value 0.
  461. For a CFI-instrumented DSO, a shadow value encodes the address of the
  462. __cfi_check function for all call targets in the corresponding memory
  463. page. If Addr is the target address, and V is the shadow value, then
  464. the address of __cfi_check is calculated as
  465. .. code-block:: none
  466. __cfi_check = AlignUpTo(Addr, 4096) - (V + 1) * 4096
  467. This works as long as __cfi_check is aligned by 4096 bytes and located
  468. below any call targets in its DSO, but not more than 256MB apart from
  469. them.
  470. CFI_SlowPath
  471. ------------
  472. The slow path check is implemented in a runtime support library as
  473. .. code-block:: none
  474. void __cfi_slowpath(uint64 CallSiteTypeId, void *TargetAddr)
  475. void __cfi_slowpath_diag(uint64 CallSiteTypeId, void *TargetAddr, void *DiagData)
  476. These functions loads a shadow value for ``TargetAddr``, finds the
  477. address of ``__cfi_check`` as described above and calls
  478. that. ``DiagData`` is an opaque pointer to diagnostic data which is
  479. passed verbatim to ``__cfi_check``, and ``__cfi_slowpath`` passes
  480. ``nullptr`` instead.
  481. Compiler-RT library contains reference implementations of slowpath
  482. functions, but they have unresolvable issues with correctness and
  483. performance in the handling of dlopen(). It is recommended that
  484. platforms provide their own implementations, usually as part of libc
  485. or libdl.
  486. Position-independent executable requirement
  487. -------------------------------------------
  488. Cross-DSO CFI mode requires that the main executable is built as PIE.
  489. In non-PIE executables the address of an external function (taken from
  490. the main executable) is the address of that function’s PLT record in
  491. the main executable. This would break the CFI checks.
  492. Backward-edge CFI for return statements (RCFI)
  493. ==============================================
  494. This section is a proposal. As of March 2017 it is not implemented.
  495. Backward-edge control flow (`RET` instructions) can be hijacked
  496. via overwriting the return address (`RA`) on stack.
  497. Various mitigation techniques (e.g. `SafeStack`_, `RFG`_, `Intel CET`_)
  498. try to detect or prevent `RA` corruption on stack.
  499. RCFI enforces the expected control flow in several different ways described below.
  500. RCFI heavily relies on LTO.
  501. Leaf Functions
  502. --------------
  503. If `f()` is a leaf function (i.e. it has no calls
  504. except maybe no-return calls) it can be called using a special calling convention
  505. that stores `RA` in a dedicated register `R` before the `CALL` instruction.
  506. `f()` does not spill `R` and does not use the `RET` instruction,
  507. instead it uses the value in `R` to `JMP` to `RA`.
  508. This flavour of CFI is *precise*, i.e. the function is guaranteed to return
  509. to the point exactly following the call.
  510. An alternative approach is to
  511. copy `RA` from stack to `R` in the first instruction of `f()`,
  512. then `JMP` to `R`.
  513. This approach is simpler to implement (does not require changing the caller)
  514. but weaker (there is a small window when `RA` is actually stored on stack).
  515. Functions called once
  516. ---------------------
  517. Suppose `f()` is called in just one place in the program
  518. (assuming we can verify this in LTO mode).
  519. In this case we can replace the `RET` instruction with a `JMP` instruction
  520. with the immediate constant for `RA`.
  521. This will *precisely* enforce the return control flow no matter what is stored on stack.
  522. Another variant is to compare `RA` on stack with the known constant and abort
  523. if they don't match; then `JMP` to the known constant address.
  524. Functions called in a small number of call sites
  525. ------------------------------------------------
  526. We may extend the above approach to cases where `f()`
  527. is called more than once (but still a small number of times).
  528. With LTO we know all possible values of `RA` and we check them
  529. one-by-one (or using binary search) against the value on stack.
  530. If the match is found, we `JMP` to the known constant address, otherwise abort.
  531. This protection is *near-precise*, i.e. it guarantees that the control flow will
  532. be transferred to one of the valid return addresses for this function,
  533. but not necessary to the point of the most recent `CALL`.
  534. General case
  535. ------------
  536. For functions called multiple times a *return jump table* is constructed
  537. in the same manner as jump tables for indirect function calls (see above).
  538. The correct jump table entry (or it's index) is passed by `CALL` to `f()`
  539. (as an extra argument) and then spilled to stack.
  540. The `RET` instruction is replaced with a load of the jump table entry,
  541. jump table range check, and `JMP` to the jump table entry.
  542. This protection is also *near-precise*.
  543. Returns from functions called indirectly
  544. ----------------------------------------
  545. If a function is called indirectly, the return jump table is constructed for the
  546. equivalence class of functions instead of a single function.
  547. Cross-DSO calls
  548. ---------------
  549. Consider two instrumented DSOs, `A` and `B`. `A` defines `f()` and `B` calls it.
  550. This case will be handled similarly to the cross-DSO scheme using the slow path callback.
  551. Non-goals
  552. ---------
  553. RCFI does not protect `RET` instructions:
  554. * in non-instrumented DSOs,
  555. * in instrumented DSOs for functions that are called from non-instrumented DSOs,
  556. * embedded into other instructions (e.g. `0f4fc3 cmovg %ebx,%eax`).
  557. .. _SafeStack: https://clang.llvm.org/docs/SafeStack.html
  558. .. _RFG: https://xlab.tencent.com/en/2016/11/02/return-flow-guard
  559. .. _Intel CET: https://software.intel.com/en-us/blogs/2016/06/09/intel-release-new-technology-specifications-protect-rop-attacks
  560. Hardware support
  561. ================
  562. We believe that the above design can be efficiently implemented in hardware.
  563. A single new instruction added to an ISA would allow to perform the forward-edge CFI check
  564. with fewer bytes per check (smaller code size overhead) and potentially more
  565. efficiently. The current software-only instrumentation requires at least
  566. 32-bytes per check (on x86_64).
  567. A hardware instruction may probably be less than ~ 12 bytes.
  568. Such instruction would check that the argument pointer is in-bounds,
  569. and is properly aligned, and if the checks fail it will either trap (in monolithic scheme)
  570. or call the slow path function (cross-DSO scheme).
  571. The bit vector lookup is probably too complex for a hardware implementation.
  572. .. code-block:: none
  573. // This instruction checks that 'Ptr'
  574. // * is aligned by (1 << kAlignment) and
  575. // * is inside [kRangeBeg, kRangeBeg+(kRangeSize<<kAlignment))
  576. // and if the check fails it jumps to the given target (slow path).
  577. //
  578. // 'Ptr' is a register, pointing to the virtual function table
  579. // or to the function which we need to check. We may require an explicit
  580. // fixed register to be used.
  581. // 'kAlignment' is a 4-bit constant.
  582. // 'kRangeSize' is a ~20-bit constant.
  583. // 'kRangeBeg' is a PC-relative constant (~28 bits)
  584. // pointing to the beginning of the allowed range for 'Ptr'.
  585. // 'kFailedCheckTarget': is a PC-relative constant (~28 bits)
  586. // representing the target to branch to when the check fails.
  587. // If kFailedCheckTarget==0, the process will trap
  588. // (monolithic binary scheme).
  589. // Otherwise it will jump to a handler that implements `CFI_SlowPath`
  590. // (cross-DSO scheme).
  591. CFI_Check(Ptr, kAlignment, kRangeSize, kRangeBeg, kFailedCheckTarget) {
  592. if (Ptr < kRangeBeg ||
  593. Ptr >= kRangeBeg + (kRangeSize << kAlignment) ||
  594. Ptr & ((1 << kAlignment) - 1))
  595. Jump(kFailedCheckTarget);
  596. }
  597. An alternative and more compact encoding would not use `kFailedCheckTarget`,
  598. and will trap on check failure instead.
  599. This will allow us to fit the instruction into **8-9 bytes**.
  600. The cross-DSO checks will be performed by a trap handler and
  601. performance-critical ones will have to be black-listed and checked using the
  602. software-only scheme.
  603. Note that such hardware extension would be complementary to checks
  604. at the callee side, such as e.g. **Intel ENDBRANCH**.
  605. Moreover, CFI would have two benefits over ENDBRANCH: a) precision and b)
  606. ability to protect against invalid casts between polymorphic types.