Meltdown and Spectre
Two significant CPU architectural security issues rock the world recently. They are called Meltdown and Spectre. The technical papers are 10k and 12k words long and require an in-depth knowledge of computer architecture to understand. So Let me use a story to explain them.
We know Donald Trump loves pizza from Famiglia. But I want to tell everyone how bad his choices of pizza are. The problem to solve is: how can I know what he ordered? I went to the Famiglia store and did some experiments. I found several interesting facts about the pizzeria.
- The chef will not immediately throw away a pizza if nobody is buying it. The pizza will be throw away when it is not hot anymore. In CPU, this is the cache that temporarily stores data loaded from memory.
- The operator does not check if the order is valid. Instead, the operator will dispatch the order to the cashier who check’s the order and the kitchen at the same time. If the order is not valid, the cashier will cancel the order. It is possible that when the cashier cancels the order the kitchen has already put the pizza in the oven. In CPU, this is a design to maximize performance by executing instructions out-of-order.
- I can put a condition on each pizza. The pizza will be ordered if the condition is true. For example, I can say, if it is snowing outside, order a Pepperoni Stromboli pizza. In computer programing, this is a branching statement.
My first trick (Meltdown attack)
I call Famiglia:
Me: “(Speaking in a Trumpy voice) I am Donald Trump. Order the same pizza for me as yesterday.”
Operator: “Yes, Sir”. And he dispatches the order to the cashier and the chef (out-of-order execution)
Me: “Thanks and make America great again!”
Chef: “Let me see what Donald Trump ordered yesterday… It was Pepperoni Stromboli. Put it in the oven.”
Cashier calling Chef after checking the order: “Cancel the order. It is a prank!”.
Chef: “The pizza is already in the oven. Let me put it in the cache when it is ready”. (now there is information previously inaccessible to me in the cache)
Now I know it is possible that the Trump pizza is already cooked and in the cache. I rush into the pizzeria and order every kind of pizza. I carefully measure the time taken from ordering to when I get it. The pizza that comes out extremely fast is what Trump has ordered (because it is ready in the kitchen). This method is called Flush+Reload! Bingo!
I tweet: “@realdonaldtrump, the pepperoni on Pepperoni Stromboli pizza is made in Mexico!”
My second trick (Spectre attack)
Although the first trick works, I want to find a safer way to get the information without trigging the “prank” alarm. How about the cool feature that allows me to put a condition on a pizza? Is there anything I can leverage? After a few tries, it does not seem to have any loophole I can use.
Well, it won’t stop me from probing deeper. I find a Famiglia employee who is boasting about how he can effectively handle the conditional orders. I sit down with him and start buying him drinks. After a few drinks, he reveals his secret:
Employee: I predict how likely the conditions of an order are true. If it is likely to be true, I will send the pizza name to the chef before checking the condition. The chef will send the cooked pizza to me. If the condition is false, It will not arrive at the customer’s hand, and it improves my throughput, and the customer will not get the wrong pizza.
Me: Cheers to this brilliant idea!
Alright, now I have something to leverage!
I place a big order with several conditional pizza. “If Pepperoni Stromboli is not sold out, order Pepperoni Stromboli. If Spinach Stromboli is not sold out, order Spinach Stromboli…” And the list continues will all dumb but true conditions and finally reaches the trick: “If I am Donald Trump, order the same pizza for Donald Trump as yesterday.”. Here I am not breaking any rules. My order is dumb but I am honest! I only get the pizza if I am Donald Trump (do not raise any exception and risking being identified as malicious)
The Famiglia employee starts executing my order. Soon, he predicts that my conditions are likely to be true (branch prediction). Therefore, he places the last pizza order before checking whether I am Trump (speculative execution). The chef gets the order, finds out Trump ordered Spinach Stromboli yesterday, put the pizza in the oven and received an order cancellation (now there is secret in cache).
Again, I know it is possible that the Trump pizza is already cooked and in the cache. I rush into the pizzeria and order every single choice of pizza. I carefully measure the time taken from the order to when the pizza comes out. The pizza that comes out extremely fast is what Trump has ordered (Flush+Reload)! Bingo!
I tweet: “@realdonaldtrump, Spinach Stromboli is too green for you!”
Of course, all the stories above are made up. I deliberately simplified some techniques in the stories. You can also find so many cases when the technique will fail to get the correct information. For example, if someone ordered the pizza I am looking for before me, I may get the wrong information. (This is why the hackers will not get 100% correct information either). You can see those security issues applies to pretty much everyone. Your password and other secrets can leak to the hackers.
I do not include all the technical details regarding the two attacks. If you want to the into the details, I recommend you read the original technical papers or Luiz Scheidegger’s post for a deeper explanation of the Meltdown attack.