| Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame^] | 1 | =========================================== |
| 2 | Control Flow Integrity Design Documentation |
| 3 | =========================================== |
| 4 | |
| 5 | This page documents the design of the :doc:`ControlFlowIntegrity` schemes |
| 6 | supported by Clang. |
| 7 | |
| 8 | Forward-Edge CFI for Virtual Calls |
| 9 | ---------------------------------- |
| 10 | |
| 11 | This scheme works by allocating, for each static type used to make a virtual |
| 12 | call, a region of read-only storage in the object file holding a bit vector |
| 13 | that maps onto to the region of storage used for those virtual tables. Each |
| 14 | set bit in the bit vector corresponds to the `address point`_ for a virtual |
| 15 | table compatible with the static type for which the bit vector is being built. |
| 16 | |
| 17 | For example, consider the following three C++ classes: |
| 18 | |
| 19 | .. code-block:: c++ |
| 20 | |
| 21 | struct A { |
| 22 | virtual void f(); |
| 23 | }; |
| 24 | |
| 25 | struct B : A { |
| 26 | virtual void f(); |
| 27 | }; |
| 28 | |
| 29 | struct C : A { |
| 30 | virtual void f(); |
| 31 | }; |
| 32 | |
| 33 | The scheme will cause the virtual tables for A, B and C to be laid out |
| 34 | consecutively: |
| 35 | |
| 36 | .. csv-table:: Virtual Table Layout for A, B, C |
| 37 | :header: 0, 1, 2, 3, 4, 5, 6, 7, 8 |
| 38 | |
| 39 | A::offset-to-top, &A::rtti, &A::f, B::offset-to-top, &B::rtti, &B::f, C::offset-to-top, &C::rtti, &C::f |
| 40 | |
| 41 | The bit vector for static types A, B and C will look like this: |
| 42 | |
| 43 | .. csv-table:: Bit Vectors for A, B, C |
| 44 | :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8 |
| 45 | |
| 46 | A, 0, 0, 1, 0, 0, 1, 0, 0, 1 |
| 47 | B, 0, 0, 0, 0, 0, 1, 0, 0, 0 |
| 48 | C, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
| 49 | |
| 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 | |
| 54 | The compiler relies on co-operation from the linker in order to assemble |
| 55 | the bit vector for the whole program. It currently does this using LLVM's |
| 56 | `bit sets`_ mechanism together with link-time optimization. |
| 57 | |
| 58 | .. _address point: https://mentorembedded.github.io/cxx-abi/abi.html#vtable-general |
| 59 | .. _bit sets: http://llvm.org/docs/BitSets.html |