Kernel bridge is melting down – Meltdown Vulnerability with POC on Ubuntu/Kali(Intel)

Hello folks ! I know it has been a long while since I wrote on this weblog. But the start of this year was much enthusiastic in terms of newly discovered vulnerabilities. Yes I am talking about Spectre and Meltdown
So today we will discuss about Meltdown. 

What the heck it is !

Spectre and Meltdown are the different variants of the same fundamental underlying vulnerability that affects nearly every computer chip manufactured in the last 20 years. If exploited, it allows attackers to get access to data previously considered completely protected. These vulnerabilities involve a malicious program gaining access to unauthorized data, and by exploiting two important techniques used to speed up computer chips, called speculative execution and caching.


When I first heard about this vulnerability the first thing came into my mind was how it is possible to have unprivileged access as we already have “Page Tables” in place. But as it still happened. lets see how.

So when a software running on a Core requires memory it starts a so called “load” command. The load command is then processed in multiple stages until the data is found and returned or an error occurred. Below is the simple representation of the memory subsystem.

 

How this sub-system works

Software including operating system uses virtual addressing to start a load(we can say it as memory read in simple terms). After the first stage of processing L1 cache is split between a data and an instruction cache. The L1 Cache is a so called VIPT(Virtually Indexed, Physically Tagged) cache.

This means the data can be looked up by directly using the virtual address of the load request.  This along with central position in the core makes the L1 incredibly fast. If the requested data was not found in the L1 cache the load must be passed down the cache hierarchy. This is the point where the page tables come into play. The page tables are used to translate the virtual address into a physical address. This is essentially how paging is enabled on x64.

It is during this translation that privileges are checked. Once we have a physical address, the CPU can query the L2 cache and L3 cache in turn. Both are PITP caches (Physically Indexed, Physically Tagged) thus requiring the translation in the page tables before the lookup can be done. If no data was in the caches then the CPU will ask the memory controller to fetch the data in main memory.

The latency of a data load in the L1 cache is around 5 clock cycles whereas a load from main memory is typically around 200 clock cycles. With the security check in the page tables and the L1 located before those we see already at this point that the because “page table” argument is too simple. That said – the intel manuals software developer’s manuals states that the security settings is copied along the data into the L1 cache.

Now for simplifying these things we will first discuss about speculative execution and caching. 

Speculative Execution

As the name suggests it works like a future predictor. It involves a mechanism/chip to predict the future of the code and execute something in order to work faster. It will try to find out the multiple logical branches and will start working on the math of the logic regardless of the time or decision of execution.

As for example If X is true, compute function K; if X is false, compute function M”, the chip can start computing both functions K and M in parallel, before it even knows whether X is true or false. Once it knows whether X is true or false, it already has a head start on what comes after, which speeds up processing overall. Sometimes, if a chip learns that a program makes use of the same function frequently, it might use idle time to compute that function even when it hasn’t been asked to, just so it has what it thinks the answer will be on hand.

Caching

As we are aware of the it takes some time to CPU for fetching the data from RAM so there is a memory storage called CPU Cache which can be accessed quickly enough. Sometimes data is stored for pretty long time just to ensure the speedy process for frequently executing commands.

Now the actual problem starts when these functionalities were used to play with the protected memory.

In order to access protected memory, a process needs to undergo a privilege check, which determines whether or not it’s allowed to see that data. What if the privilege check is taking too much time. In that case, when the CPU is waiting for the result of the privilege check it also starts working with the data even if it doesn’t have access to it. Well ! Still it can be considered safe because it will not display the result unless it has the privilege.

Interestingly, once it fails the privilege check, it will store the result in CPU cache in case it is required later. If we try to access the data it will be get rejected more quickly than normal confirming that there is some data at that address. It will easy to find out the data if the address is known, which can be called as Side-channel Attack. 

Let’s test it out

I have tried out on few test machines in my personal lab, will go through one by one.

Type the below command in the terminal to download the script form git hub and it will show the below results.

Now we need to compile the program before executing it with the help of “make” command.

Now, run the script

Oops !! My ubuntu VM seems to be vulnerable to it. When I tried the same thing on my Kali machine, it gave me some relief.

 

Below is the execution of meltdown exploit in real life scenario provided by Michael Schwarz.

I definitely want to do on some test websites but couldn’t find any. If you know some let me know in the comment.

References:

  1. https://en.wikipedia.org/wiki/Meltdown_(security_vulnerability)
  2. https://github.com/paboldin/meltdown-exploit
  3. https://meltdownattack.com/

I will keep adding the content in this series with more details specially talking about Spectre.

Warning :

This post is only for knowledge purposes. Do not run it on any productive systems. Do not run it on any system that might be used by another person or entity.

Leave a Reply

Your email address will not be published. Required fields are marked *