<div dir="ltr"><div><span id="gmail-docs-internal-guid-2bbf4331-2597-2564-0280-7349706e966e">The proposed mitigation for variant 2 of CVE-2017-5715, “branch target injection”, is to send all indirect branches through an instruction sequence known as a retpoline. Because the purpose of a retpoline is to prevent attacker-controlled speculation, we also end up losing the benefits of benign speculation, which can lead to a measurable loss of performance.<br><br>We can regain some of those benefits if we know that the set of possible branch targets is fixed (this is sometimes known to be the case when using whole-program devirtualization or CFI -- see <a href="https://clang.llvm.org/docs/LTOVisibility.html">https://clang.llvm.org/docs/LTOVisibility.html</a>). In that case, we can construct a so-called “branch funnel” that selects one of the possible targets by performing a binary search on an address associated with the indirect branch (for virtual calls, this is the address of the vtable, and for indirect calls via a function pointer, this is the function pointer itself), eventually directly branching to the selected target. As a result, the processor is free to speculatively execute the virtual call, but it can only speculatively branch to addresses of valid implementations of the virtual function, as opposed to arbitrary addresses.<br><br>For example, suppose that we have the following class hierarchy, which is known to be closed:<br><br><font face="monospace, monospace">struct Base { virtual void f() = 0; };<br>struct A : Base { virtual void f(); };<br>struct B : Base { virtual void f(); };<br>struct C : Base { virtual void f(); };<br></font><br>We can lay out the vtables for the derived classes in the order A, B, C, and produce an instruction sequence that directs execution to one of the targets A::f, B::f and C::f depending on the vtable address. In x86_64 assembly, a branch funnel would look like this:<br><br><font face="monospace, monospace">lea B::vtable+16(%rip), %r11<br>cmp %r11, %r10<br>jb A::f<br>je B::f<br>jmp C::f<br></font><br>A caller performs a virtual call by loading the vtable address into register r10, setting up the other registers for the virtual call and directly calling the branch funnel as if it were a regular function. Because the branch funnel enforces control flow integrity by itself, we can also avoid emitting CFI checks at call sites that use branch funnels when CFI is enabled.<br><br>To control the layout of vtables and function pointers, we can extend existing mechanisms for controlling layout that are used to implement CFI (see <a href="https://clang.llvm.org/docs/ControlFlowIntegrityDesign.html">https://clang.llvm.org/docs/ControlFlowIntegrityDesign.html</a>) so that they are also used whenever a branch funnel needs to be created.<br><br>The compiler will only use branch funnels when both the retpoline mitigation (-mretpoline) and whole-program devirtualization (-fwhole-program-vtables) features are enabled (the former is on the assumption that in general a regular indirect call will be less expensive than a branch funnel, and the latter provides the necessary guarantee that the type hierarchy is closed). Even when retpolines are enabled, there is still a cost associated with executing a branch funnel that needs to be balanced against the cost of a regular CFI check and retpoline, so branch funnels are only used when there are <=10 targets (this number has not been tuned yet). Because the implementation uses some of the same mechanisms that are used to implement CFI and whole-program devirtualization, it requires LTO (it is compatible with both full LTO and ThinLTO).<br><br>To measure the performance impact of branch funnels, I ran a selection of Chrome benchmark suites on Chrome binaries built with CFI, CFI + retpoline and CFI + retpoline + branch funnels, and measured the median impact over all benchmarks in each suite. The numbers are presented below. I should preface these numbers by saying that these are largely microbenchmarks, so the impact of retpoline on its own is unlikely to be characteristic of real workloads. The numbers to focus on should be the impact of retpoline + branch funnels relative to the impact of retpoline, where there is a median 5.7% regression as compared to the median 8% regression associated with retpoline.</span></div><div><span><br><div dir="ltr" style="margin-left:0pt"><table style="border:none;border-collapse:collapse;width:468pt"><colgroup><col width="*"><col width="*"><col width="*"></colgroup><tbody><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Benchmark suite</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">CFI + retpoline impact</span></p><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">(relative to CFI)</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">CFI + retpoline + BF impact</span></p><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">(relative to CFI)</span></p></td></tr><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">blink_perf.bindings</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">0.9% improvement</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">9.8% improvement</span></p></td></tr><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">blink_perf.dom</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">20.4% regression</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">17.5% regression</span></p></td></tr><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">blink_perf.layout</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">17.4% regression</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><font color="#000000" face="Arial"><span style="font-size:14.6667px;white-space:pre-wrap">14.3% regression</span></font></p></td></tr><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">blink_perf.parser</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">3.8% regression</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">5.7% regression</span></p></td></tr><tr style="height:0pt"><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">blink_perf.svg</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">8.0% regression</span></p></td><td style="border-width:1pt;border-style:solid;border-color:rgb(0,0,0);vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">5.4% regression</span></p></td></tr></tbody></table></div><h1 dir="ltr" style="line-height:1.38;margin-top:20pt;margin-bottom:6pt"><span style="font-size:20pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Future work</span></h1>Implementation of branch funnels for architectures other than x86_64.<br><br>Implementation of branch funnels for indirect calls via a function pointer (currently only implemented for virtual calls). This will probably require an implementation of whole-program “devirtualization” for indirect calls.<br><br>Use profile data to order the comparisons in the branch funnel by frequency, to minimise the number of comparisons required for frequent virtual calls.<div><br></div><div>Thanks,</div></span></div>-- <br><div class="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</div>