Great questions! Let’s address each of them in detail:
1. How can I try to read kernel memory if I am in userland?
In a properly functioning system, userland processes should not be able to directly read kernel memory. The processor enforces this through mechanisms like privilege levels and memory segmentation. However, the Meltdown attack exploits a microarchitectural behavior that bypasses these protections.
Here’s how it works:
- Out-of-Order Execution: Modern processors execute instructions "out of order" to improve performance. This means that instructions are executed as soon as their dependencies are resolved, regardless of their position in the program.
- Speculative Execution: Processors often speculate on the outcome of branches (e.g.,
if statements) and execute instructions ahead of time. If the speculation is incorrect, the results are discarded, but the side effects (like cache state changes) may persist.
In the context of Meltdown:
- The attacker crafts a piece of code that tries to access kernel memory. For example:
if (user_controlled_value < threshold) {
access_kernel_memory(); // This should be illegal
}
- The processor speculatively executes
access_kernel_memory() even if user_controlled_value is not less than threshold. This is because the processor doesn't know the result of the comparison immediately and speculates to save time.
- The speculative access to kernel memory brings the data into the cache, even though the result of the instruction will later be discarded.
Since the cache is shared between userland and kernel, the attacker can now observe the effects of this speculative access using the cache side channel.
2. Even if I know the address is in cache, how does it let me get the info out?
Knowing that an address is in the cache is only the first step. The attacker still needs to extract the actual data from the kernel memory. Here’s how the Flush+Reload covert channel is used to achieve this:
Flush+Reload Explained:
- Flush: The attacker removes (flushes) a specific cache line from the cache using instructions like
clflush. This ensures the data is not already in the cache.
- Reload: The attacker measures the time it takes to access the memory address again. If the access is fast, the data is in the cache (because the speculative execution brought it there). If it’s slow, the data was not in the cache.