Hi, everybody. I'm going to be giving a talk about entitled virtuanoid breaking out of KVM, Linux kernel virtual machine. So what exactly is this KVM thing and why do I care about it? KVM is sort of the new hotness for virtualization on Linux. It's a virtualization system that was developed sort of from scratch to be the official entry supported by upstream, working friendly with the Linux community, virtualization solution that sort of started later than Zen and everyone else but has ramping up speed and sort of become the officially blessed platform for a lot of distributions to do virtualization. So it's an exciting new platform that I think is going to be seeing a lot of attention in the security space soon. So I decided to take a look at it and come up with some conclusions and share my results here. So who am I and why am I talking with you? I'm going to be talking about you. My day job actually has absolutely nothing to do with this. I'm a kernel engineer at a company called Ksplice but it does mean I spend a lot of time staring at low level software systems and so in my spare time I tend to do a lot of low level security stuff including this work on KVM. All right. So structure of this talk. We're going to start by taking a high level look at KVM at the Linux virtual machine. We'll look at what the different pieces are and how they fit together and then we'll go back and focus in on each of them from an attack surface perspective. From an attacker trying to break out of a KVM virtual machine. What's interesting about each of the components? How are they exposed to the attacker? What kind of things should we be thinking about and looking for? And then we'll run into a deep technical dive into the guts of actually writing a breakout exploit against KVM. Learning in the process a bunch about some useful features of the KVM architecture that come in handy and that we can exploit in creative ways. We'll start by just looking at the bug that my exploit centers around and then we'll talk about the actual exploit that I wrote. Then I'll discuss some conclusions that I think are sort of implied by my work or suggested and some future directions that I hope to proceed in or that I would love to see other people do work in. And then we'll demo the exploit because no talk on exploitation is complete. It's been reliable so far. All right. So KVM. How does it work? There's three main components to the KVM virtual machine. There's KVM.KO, the core kernel module. Then there's a pair of kernel modules for supporting Intel and AMD hardware support. And then there's the user space driver program, QMU KVM. Looking at the function of each of those and where they sit, KVM.KO is the core of the kernel modules, the kernel side support for KVM. It's sort of responsible for emulating and tracking the virtual CPU and memory management unit, the core of the virtual machine using the X86, AMDs and Intel's hardware virtualization extensions. It emulates a small number of devices and input output operations in kernel directly for efficiency. But for the most part, it doesn't deal with emulating hardware. And then it also provides a large interface for user space driver program, like QMU KVM, which we'll talk about in a second, to communicate with it, to allocate new virtual machines, allocate new virtual CPUs on those machines, set up the emulated physical memory and all of that. And one of my favorite bits of trivia about KVM.KO is despite the fact that we're using hardware virtualization extensions here, which means that for the most part, the virtual machine is just executed directly by the processor in a virtual machine non-root context, as Intel calls it, KVM still contains an entire X86 emulator in the kernel module that's used for handling certain rare traps. And just sort of from a code complexity and attack service perspective, it's an interesting thing to know. KVM Intel and KVM AMD are the other half, really less than half of the kernel component here. They just provide the glue code for communicating with the hardware virtualization extensions. You know, more or less you take those four chapters or whatever from the Intel manuals and implement the software half of that and you get these. And they're relatively small. They're one C file, 4,000 lines or so each, which compared to any of the other components is tiny. And then finally, we come to the user space component, QMU KVM, which is the direct user interface and user driver for the four KVM VMs. It's based on the classic QMU emulator, which I'm sure that almost all of you have heard of and probably even used, because it's amazingly useful and handy. And in addition to providing the interface and driver loop, QMU KVM implements basically all of the virtual devices that your VM talks to, because it's not just a virtual machine. It's a virtual machine. It's a virtual machine, much like a real machine, is not just a CPU talking to a block of memory. In order for it to be useful, there needs to be peripheral devices, PCI buses that, you know, hang off all kinds of devices, your Ethernet card, your display, a serial console, whatever, a timer device of some sort. All of that, all of that lives in QMU KVM. It's responsible for emulating those devices. And devices are complicated, it turns out. And this, as a result, contains a huge quantity of code. It's an order of magnitude more code than KVM.KO, even if you only consider the devices that are actually in use by a typical VM. If you consider, you know, all of the possible devices, it's even bigger. There's currently a project under way by the upstream Linux community to potentially replace this user space component with a separate developed more or less from scratch KVM binary that would be maintained in the Linux kernel tree. But that work is relatively new. It's not more than a year or two old. And it's going to be some years before it's stable enough that people are seriously using it and then some more years before distributions are actually shipping it and, you know, lots of people are using it in production. So if we're thinking about KVM, we're thinking about attacking or defending KVM for the near future QMU KVM is what we need to be talking about. All right. So those are the three things that we're talking about. Now we can go back and take a look at each one with an eye for attack surface. KVM.KO for an attacker is a very tempting target because it runs in the kernel mode on the host in ring zero. If I successfully find and exploit a bug here, I have ultimate privileges on the host with no further exploitation or privilege escalation needed. However, despite being a tempting target, it's also a bit of a tough target because there's not very much code there compared to this user space component. And what code there is is much of that is dedicated to interfacing with the user space component and so is not directly available for attack by a guest. That x86 emulator that I mentioned is definitely an interesting target because there's a lot of code there that is rarely exercised because the emulator is only used in some edge cases and that's sort of whenever I'm auditing a system, lots of subtle code that's rarely used is sort of the first place I'm going to go. So I'm going to go ahead and show you some of the places that I look for bugs. There have been a number of interesting bugs there that allowed privilege escalation within a guest because of bugs in the x86 emulator. So there is a bit of a track record of bugs. Those aren't as interesting as breakout bugs, but again, it's a hint that this is an interesting place to look. It's unfortunately not the focus of this talk, but it's something that I want to highlight because I think future research should take a close look here. And then in addition to within the guest, just if we're talking about KVM.KO, we should keep in mind that there's also the possibility of privilege escalation in the host because unprivileged users can communicate with this privileged kernel module. Just another thing to note if we're looking at the whole scope of KVM exploitation. KVM Intel and KVM AMD.KO, the kernel modules that interface with the hardware, are not super interesting targets because there isn't a ton of code there. It's mostly sort of straight line code that just translates between the kernel's data structures that represent the VM and how the hardware wants to model those and just bridges back and forth. But there's a lot of subtlety and complexity in interacting with these hardware components. And so there's potentially scope for some interesting bugs where the KVM is using the hardware support slightly incorrectly or in an unusual way that allows for interesting attacks. But it's not the first place I'd look. And then finally we come again to QMU KVM, which as you can probably guess by now is the easiest place to look for targets here. Hundreds of thousands of lines of code, emulating device code, so the devices are talking directly to the guest via emulated memory mapped IO or IO ports. So they're parsing control structures that the guest is exporting and communicating these strange arcane hardware protocols. Lots of interesting code. And much of this code comes straight from QMU, which is mostly written by one guy, Fabrice Ballard, who is an absolute genius, brilliant programmer, but just one guy spewing out code over years with no one auditing it, bugs are going to happen. The one unfortunate thing about this as attackers is that it is often sandboxed using SELinux or app armor or some other technology. And so if we do successfully break out from KVM into QMU KVM, we're going to have a lot of problems. And so we're going to have a lot of problems. We'll probably need another privilege escalation attack to get full privileges on the host. Fortunately, we're all running Linux, which as any Slashdot reader knows has no bugs whatsoever, so we should be safe. All right. So that's the structure of KVM and taking a bit of a look at the attack surface. Now we're going to dive in on our bug, which is in fact, and the bug that I used, which is in fact a bug in QMU KVM. So we're going to dive in on our bug, which is in fact a bug in QMU KVM in that user space driver program. I've got here the text from the Red Hat security advisory. The bug is CVE 2011 1751. All the major distributions have hatched it by now, so you can go look it up. Red Hat describes it as we found that the PIIX4 power management emulation layer did not properly check for hot plug eligibility. So what does that mean? First off, what is PIIX4? PIIX4 was an actual physical chip that was used to control the power of the device. It was the south bridge in circa 2000 Intel chipsets. And since it's a south bridge, what that means is that it talks to, it, most of the physical devices in computers of that era hung off of the south bridge architecturally. And so the PCI bus, the support, the real time clock, all of that stuff, the host, the CPU communicates with through the south bridge. And this is the default south bridge chip that QMU emulates. So we're going to take a look at that. So we're going to take a look at this. It supports PCI bus and PCI hot plug. It supports in physical hardware, what this would mean is a hot plug device, the chip and the PCI, you know, instructs the PCI bus to electronically disconnect it so that you can pull it out and be safe. In a virtual machine, what this just means is that we disconnect the, you know, virtual mappings for those I.O. ports and free the memory backing that device. It's expected that, you know, the destructor function for a virtual device performs this successful unplugging of everything. But not every device in QMU was implemented under the expectation that it might be hot plugged. And so many of these destructor functions are, some of these destructor functions are no ops or, you know, insufficiently clean up state. And that's supposed to be okay because you're not supposed to be able to hot plug these devices. And so it's okay if, you know, they don't successfully clean themselves up. But it turns out that, again, as the advisory says, it insufficiently cleans up state. And that's why we have sufficient checks for hot plug eligibility. The PIX4 PCI hot plug path, if you handed it a device, an identifier on the PCI bus to hot plug, it would just blindly go and hot plug it without actually checking the flag that says this is a hot pluggable device. In particular, a tempting target is the emulated ISA bridge. So who here actually remembers, you know, physical ISA cards and ISA buses? All right. Most of us. So, you know, we have a lot of those. We don't have those anymore, at least, you know, outside of, you know, hobbyist, you know, archaic hardware shows. But it turns out that your south bridge, even in your modern Intel chip sets, has an emulated ISA bus with emulated with virtual ISA devices hanging off of it. And QMU faithfully emulates this behavior and has a bunch of virtual ISA devices hanging off of a PCI ISA bridge. And we can just unplug that and all of those ISA devices, which include such things as the real time clock that QMU emulates. And we can just unplug that and all of those ISA devices, which include such things as the real time clock that QMU emulates. And we can just unplug that and all of those ISA devices real time clock, it turns out, is not expecting to be unplugged. In particular, it leaves around it's a real time clock, so it has timer events that it uses to keep track of the real time. It leaves those timers hanging around on QMU's run loop. And so we can just sort of look a little bit at what this means in code. The real time clock is emulated by this struct RTC state at the bottom of the screen. And we want to know what that means. So, for example, if you have a virtual machine And it has this second timer that it just schedules to get fired every second so that it can update the time. And if we look up above at how a QMU timer works, it just has an expire time that says when this timer should fire. And then it has a callback and an opaque pointer that is passed to that callback. And so once a second, the QMU timer fires and calls this RTC update function on that RTC state struct. But if we free it, it frees the RTC state structure, but it doesn't free or unregister actually either of the timers it used. I've only shown the one that's relevant for this exploit. And so we're left with dangling pointers. That opaque pointer there is actually used as a pointer to the RTC state. And so we have a dangling pointer to a free object. And so within a second later, with the next second tick, we call this RTC update second function. And on a freed object. And we have a classic use after free bug here. As those of you who are exploit developers probably know, that's almost the most beautiful case you could hope for today for an exploitable bug is a use after free like that. And just to show how easy this is to reproduce, that's the reproducer. Compile that program, run it as root in a Linux machine on a vulnerable version of KVM, and KVM will seg fault. And you know, all this does is IOPL just gets privileges to be able to write out IOPORTS. And you write a single value to a single IOPORT and boom. In fact, this bug was found by a fuzzer that just literally wrote random values out random IOPORTS. And eventually it stumbled on this value and I get a signal 11. And I went and debugged it and wrote this talk. All right. So. That's the bug. So we have this beautiful use after free. But so what does it take to in the KVM environment go from a use after free like this to a working exploit? We'll talk about this process in three stages. First off, how do we go from this use after free to controlling the instruction pointer? How do we get KVM to jump to some address that we specify? And then once we have that. How do we leverage that into executing arbitrary shellcode inside the guest? And then once we have. And then I will have talked about those two assuming that I can guess addresses in the QMU KVM process. I.e. that there's no address space randomization in the process that I can predict addresses. And then we'll talk a little bit about what we have to do to get rid of that assumption. To work on a process with a randomized address space like KVM is going to be. In the wild on any real deployment. So for getting RIP control. The high level to do is sort of fairly straightforward. If you look at how the code works. We're going to need to create a fake QMU timer object that we control. And inject that at a known address. Then we'll trigger the eject. We'll dump the ISA bridge. And then we will force an allocation. Into the space previously occupied by the RTC state structure. That points that second timer field at our timer. Then on the next second boundary. RTC update second will run on our fake RTC state. And it will reschedule the second timer to run one second later. But we've hijacked that pointer to point at our timer. And so one second later our timer will run. Which includes the hour callback method. That it will jump. Into and it will jump to code that we control. So there's three steps here. I've already shown you how to do number two. How to eject the ISA bridge. And sort of actually trigger the vulnerability. Which leaves two things left to talk about. One. How do we construct fake devices in QMU KVM's address. Fake structures in QMU KVM's address space. How do we get objects that A. Appear in that address space somewhere. So that we can write pointers to them. And B. Appear at a known address. Or find out that address. So that we can predict that address. And write that second timer pointer to point at our thing. So this is one case where exploiting a virtual machine. Is actually sort of in some ways. Has a unique advantage over many other types of exploits. Is that that process turns out to be incredibly simple. The way that QMU KVM handles the emulated RAM for the guest. Is that it's just a big. M-Map region in QMU KVM's address space. That is the physical RAM of the guest. So there is no injection portion. Like I talked about here actually at all. We just literally allocate an object. However the hell we want. Statically. M-Map it. Allocate it on the stack. Whatever we feel like. Find the physical address of that. Find the physical address in the guest. Which we can do in a couple of ways. We can do in kernel mode. By walking the page tables explicitly. Or the kernel actually it turns out. Exports a proc file. That will just give us this information. But you know. Clearly we can find that from the guest. And then we just add that to the base address. Of the M-Map in the host process. And we found that object. For now. We're going to assume. That we know the base address of that M-Map. Because this base address is predictable. Assuming no ASLR. And again. We'll talk about how to get by that assumption later. So that's step one. Is injecting data at a controlled address. Actually totally easy. When you're attacking a virtual machine. And this is true. I believe in basically actually all current virtual machines. Do this or something similar. So this is not totally unique to KVM. Although this is the first exploit. I'm aware of that's talked about this technique. Second technique is forcing allocations. Inside the QMU KVM process. We need to get QMU KVM. To do a malloc. And then populate it with data that we control. Of an appropriate size. That it will with high probability. Get allocated into the space. That destruct RTC state. Used to occupy. To do that. I'm going to use a feature of the QMU KVM. Network stack that I'll talk about. QMU KVM. Inherits from QMU. A user mode networking stack. That implements an entire. Virtual LAN. Inside the QMU KVM process. This is used. As a way of allowing you. To get network access to the guest. Without having to mess around with bridge devices. And ton tap devices. Or other such madness on the host. And also without requiring privilege. On the part of the QMU KVM process. It's the default networking set up. So it's a reasonable thing to attack. Although in production environments. It's probably more common that we'll see. Bridge networks or something else. And so we'd have to modify this step there. But the same fundamental principles. Of most of the attack would apply. So this virtual network stack. You know emulates a DHCP server. DNS server and gateway NAT. All plugged into this virtual LAN. The way that this virtual LAN handles packet delivery. Is that normally packets are handled. Synchronously. When you inject a packet onto the virtual LAN. It just looks up the recipient. And you know calls there. Deliver packet callback method. Synchronously. And so there's no buffering. No queuing. But in order to prevent recursion. If a virtual device. Or virtual hosts. Deliver mechanism. Then injects a second packet. In response to the first packet. That packet is queued. Using malloc. With a small header appended. And then delivered later. Once we return to the main run loop. So if we can find a device. On the virtual LAN. That responds to packets. From the user. By synchronously generating a second packet. Whose contents are. All or almost all controlled. By the contents of the first packet. Then that will generate a malloc. With controlled contents. Can anyone think of a network service. That has that property? Ping. ICMP ping echo packets. So the way we're actually going to force allocations. Inside the guest. Is by pinging the virtual gateway. And it will reflect the ICMP packets. Back at us. With the payload of length we control. And contents we control. Which will force mallocs. And let us exploit this use after free. And so now. Putting the pieces together. We allocate a fake QMU timer. In our address space. With a callback method. Which is whatever address we want KVM to jump to. We'll calculate its address in the host. Using the arithmetic I showed earlier. We'll do that. Eject dance. And then we'll ping the emulated gateway. As fast as we can. With ICMP packets that are. Have pointers to our fake timer. In the host. And with extremely high probability. Basically one. One of those will end up allocated. Into the space previously occupied. By the RTC state. We have the ability. To get the QMU to jump. To an address we control. All right. So that's part one. We have RIP control. We can get QMU to jump to any address we want. But we still have to deal with. Non-executable pages. Or an injection shell code somehow. We're not quite there yet. So what are our options to do next? We'll start with the classics. We can set EIP equals 41414141. AAAA. That clearly demonstrates it's possible. And, you know, we're done. That's boring. I could disable NX. Non-executable pages in my BIOS. Or in my kernel. Blatch shell code wherever the hell I want. That's also boring. Getting more interesting. We could use a technique called return oriented programming. To chain together bits of QMU's own code. To do the standard. MMAP or MProtect. To allocate executable shell code. And then jump into the shell code that way. That's a technique used in most exploits these days. It would be a perfectly fine strategy here. But I have a slightly different strategy. That I happen to like. That I stumbled upon here. That I think has some cleaner properties. And was easier to develop than doing a rough payload. So we'll talk about that. So let's take another look. At that QMU timer structure. That we're faking. So we focused on the callback. And opaque members before. But now let's look at this expire time. And this next pointer. And so the way that these timers are implemented. Is they're stored in a sorted linked list. Threaded through by that next pointer. And every time the QMU main loop wakes up. It runs these timers. Which just walks these timers. By this linked list. By following that next pointer. As long as that expire time. Is before the current time. And so we control that next pointer. Because we control this entire time. So what we can do. Is construct multiple timers. And chain them together. By that next pointer. So that we can cause QMU KBM. To execute multiple functions. In a row. And perform sort of limited strings of computation. In the host. Rather than just doing one jump. And we're going to point these functions at existing functions. Inside the host. That we're going to string together in an unexpected way. So this is sort of. Halfway between. Traditional return to libc attacks. And ROP attacks. With you know. A slightly unorthodox method. Of doing the dispatching. So now one feature. We notice here. Is that we have a large number of one argument function calls. We want to do more argument function calls. So to do that. We're going to dive a little bit into. The detail of the AMD64 calling convention. How it works. So on AMD64. We pass arguments to functions. In registers. Starting with RDI, RSI, RDX, etc. So RDX is the first argument. That's going to get the opaque member. Of our fake timers. RSI we don't directly control. However. Every compiled version of QMU run timers. That I've encountered. Leaves RSI untouched. It doesn't clobber RSI. So suppose that we found a function. That looked like this hypothetical set RSI function. It sets the RSI register. Based on the RDI register. Which is the first argument register. So back. If we go back to our chain of function calls. We set F1. To set RSI. And we pass it some argument. So then we're going to populate RSI. With that first argument. That argument to the first timer. RSI is going to get populated with that. And then we call the second function. We now control both arguments. So we can get to two arguments this way. The same trick doesn't work with RDX. The third argument register. Because QMU run timers. Does clobber that. So we can't count on it being preserved. Across different timers. Timer calls. So. We'll talk about what we do there in a second. So this function. Happens to be the set RSI. Gadget that I chose. What this actually does in QMU. Is relatively unimportant. Because it takes a value. As its first argument. And then it calls a function. With that value as the second argument. Which means that it moves it into RSI. And so as long as we choose a value of adder. That makes IO port write. A no op or other or you know. Mostly harmless operation. This has the effect of populating RSI. From RDI. So that's that. So now to get to a three argument. M protect call. Because M protect. Calls to set things executable. We need to call with an address. A length and a protection value. That we want to contain at least prot exec. A little bit of searching. For useful patterns with some grep. Finds us this interesting function. IO port read L thunk. Again what this is actually supposed to do. Totally irrelevant. But it takes two arguments. Which again we control. And then it follows some function pointers. To call it. So we control those function pointers. With a size that we control. With a third argument. That just conveniently happens to be equal. To prot exec. So it will set that memory executable. All right. So. To put the steps together. We need to allocate a fake. IO range op struct. Which is that operation structure. We need to set the read op to M protect. We need to allocate an IO range object. That needs to be page aligned. Because we're going to call M protect on it. And it has ops pointing at fake ops. And base is set up such that that adder. Minus IO port base computation. Yields a useful length. And then we're going to copy our shell code. Into that same page. Immediately following the IO range. And then we'll do this timer chain. We'll call CPU out L. To set the second argument register to zero. Because that happens to be a value. That makes IO port read. Which. Is the IO port right up here. Harmless. And then we do this IO port read L thunk. Which through this hilarious chain of indirection. Will result in an M protect. And then we'll just jump right into that fake IO port. Plus one. Which is where we stuck our shell code. All right. And so that works. Turn off ASLR. Code up all the offsets right. And you've got code execution. So one question. Before we jump into getting by ASLR. I want to ask you a couple of questions. Return oriented programming is a standard well understood technique. That is the standard way to write these exploits. Why did I do this more creative thing? There's a couple of reasons. One is that this mechanism makes continuing execution. In the. Getting the QMU KVM process to continue executing. Dead simple. Because we're not corrupting the stack. We're not really smashing any state. We're just sort of hijacking the legitimate functionality of the run loop. And so after all of this returns. Everything is actually. Except for you know the things that we freed as a result of our exploit. Everything is in a sane state. And we can just continue executing. Another thing that I like about this technique. Is that. I've shown you virtually no assembly on the previous few slides. I've referred to one detail. But there's very little dependence on. How these functions actually got compiled. And ROP. Cares very deeply about how the functions got compiled. Because you're chaining together strings of assembly. When you're executing. When you're exploiting a program under Linux. You have to deal with the fact. That every Linux vendor has their own build of the program. With a slightly different source version. A slightly different GCC version. A slightly different set of flags. And so if you want to have to try to find ROP gadgets. Across every single one. It's a fair amount of work. And there aren't yet great tools. For doing this in a completely automated fashion on Linux. Whereas here. I just need to look at the source. And then I just need to grab the symbol table. From all of those different versions. Of these functions that exist in the C code. And so are more or less preserved. Across compilation by different versions. And then finally. The cop out answer. Is that I'm just personally not that good at ROP. I don't know great tools for doing it on Linux. And so I decided to try something different. All right. So we've got code execution. But I've been assuming no ASLR. So let's get rid of that restriction. So we need two addresses. There's fundamentally two addresses. That we've been using. One is the base address. Of the QMU KVM binary. If we assume we're attacking a known binary. We know the layout of all of the functions in memory. And so we know their offsets. From the executable base. But we need to know where that executable is loaded. And secondly. We need to know the address of that physical memory mapping. Inside QMU KVM. In order to. In order to get the address. Of fake objects we're injecting. So there's a couple of answers here. The classic answer is. Find a sufficiently. Sufficiently powerful information leak. That lets us leak the contents of pointers. Or the contents of memory. And back solve or back derive. Somehow these addresses from leaked information. That's a classic way of doing this. I didn't end up going that way. I decided to do. Again something else. That I think is a little more informative and interesting. The second option is. We can take advantage of the fact. That every major distribution. Still compiles KVM. Not as a position independent executable. If you're not familiar. With implementation. Of address space randomization. What this ends up meaning. Is that the QMU KVM actual core elf binary. Is loaded at a fixed address. Every time. It's always loaded at the same address in memory. And so addresses in the binary itself. Are not subject to randomization. And we can assume that we know them. So we can just assume that we know. For a given binary all of those code addresses. And that means. The only thing we have left to find. Is the FISMEM base address. To do that we're going to use. Yet another obscure feature of QMU KVM. That comes in handy here. There's this FWCFG. For firmware configuration. Subsystem in QMU KVM. It's sort of a virtual device. It emulates two IO ports. You're not supposed to know or care about it. Because it's used by QMU's bios. To communicate with the emulated hardware. But it's just listening on IO. There's no reason other software. That's not the bios can't talk to it. It's purpose is to export. Data tables that the bios needs. Such as the E820 map. That describes the layout of physical memory. The ACPE tables that describe. How to interface with the emulated ACPE hardware. Etc. It also has support for the bios. Feeding information back. To the emulated hardware. This is a little odd in that. As of the versions that I checked. This support for writable tables. In QMU KVM. There's actually no place that it exports tables. That the bios is expected to write. But the infrastructure is there. Not only is it there. It actually lets the bios or any software. Write to any of these exporting tables. And conveniently. Several of these tables are backed. By statically allocated buffers. Inside the host. Which means again assuming no PIE. They're loaded as part of the elf binary. At fixed addresses. Which means that we can write to them. And get nearly 500 bytes. Of writable data. At a fixed static predictable address. Even under ASLR. So that's enough. That we can inject our fake timers. Our fake structures into that space. Rather than into. The backing memory map. There's one complication. Which is that mprotect. As I mentioned. Needs a page aligned address. And so a 500 byte region. Is not likely to land. If it's allocated. If it's at a random offset. Which it basically is. So what we actually need to do. Or what I did at least. Is we construct a different set of fake timer chains. Using the same techniques. That does basically a read for. It reads four bytes. At an address we choose. From the guest. And then it writes it to a space. Or from the host process. And it writes it to a space that's visible to the guest. And so we use that read for. We build up our own information leak. And we chase pointers. Using those reads. To derive the value of that. Physical memory base map. Once we found the address of that mapping. We proceed exactly as before. There's a little bit. Of complexity here. Is that now this means. We actually need to do that read for. Then do computation based on the results of that read. And then execute more gadgets in the host. So rather than ending our timer chains. With next equals null. We actually end them with another timer. That calls that RTC update second function again. Which remember it takes. It follows pointers to find a timer. And then schedules that timer. At one second in the future. So we execute this chain. We do the read for. And then one second from now. A timer that we control. Will again get executed. But until then. In this context. The guest can do arithmetic. Do computation. Glue bits together based on that read. And then one second later. Will jump back into another set of gadgets. So we use this to chain. To chain multiple timer chains. One after another. With guest execution in between. And so that then. Is what it takes to. Or at least the steps that I took. And you know the broad steps. What it takes to make this exploit. And so. We're going to work against ASLR. Bypassing non-executable pages. And work completely. On a stock install of KVM. From a vendor. And we'll demo that in just a moment. So. Before we get to the demo. So that I can end the talk on the high note. With the demo. We'll talk about a couple of conclusions. That I think are. Thoughts that are raised by this work. One thing I want to emphasize. That I hope this work emphasizes. Is that virtual machine breakouts. Aren't magical at all. I think there's often a tendency. Virtual machines are kind of magic honestly. They do this weird thing. Where you get multiple computers in one computer. And they do that by. Doing lots of crazy low level. Hardware and software tricks. To make it possible. And so people tend to assume. That attacking virtual machines. Must be at least as complicated. As writing one in the first place. Which is probably a pretty strong security layer. But that's just not true. All of the usual software protections. ASLR, NX, etc. Apply to virtual machines. But that's about it. Virtual machine breakouts. Typically are memory corruption bugs. Like any other bugs. And frankly today. I would prefer to be attacking a virtual machine. Than a modern web browser. Because the state of mitigating exploits. And defending against exploits in sandboxing. Is much more advanced there. So virtual machines. Don't think of them as magic security boxes. They're just as vulnerable as anything else. And of course. As you might expect. The fake devices are the weak spot. And just sort of to drive this point home. And to give some context. We'll take a brief look at some past. Virtual machine breakouts. In 2008. Invisible things lab. Put out this paper. Adventures with a certain zen vulnerability. Where they demonstrated a zen breakout. And what they were exploiting. Was an integer overflow. In the para virtual frame buffer. And they did much the same standard tricks. That I did. Ret to lib C. Or return oriented programming. To copy a buffer in. And then jump into it. Immunity at black hat 2009. Presented cloud burst. A breakout exploit for VM ware. Again missing bounds checking. In the virtual SVGA device. Get memory corruption. To give a sense of where. Virtual machines can get interesting. And just earlier this year. Invisible things lab. Again put out another attack against zen. But this one based on a bug. In the way that zen uses. Intel's VTD. Which is the IO MMU. And hardware IO protection technology. It turns out that if you. Turns out that there was a subtle bug. In the way that interrupts were handled. That actually allowed a guest to escape. And so that's an example. That might be possible. With the KVM AMD and KVM Intel modules. Is that these things are complicated. And there probably are subtle attacks. That involve the hardware interaction. But once again the actual primitive. That they got out of that attack. Was memory corruption in the host. And so from there they proceeded. In exactly the same set of techniques. That everyone else uses. For exploiting memory corruption bugs. And so virtual machines are interesting. But they're not that much more interesting. They're not that much more secure. Than anything else. So that's some conclusions. Some further work. That I want to see done. And that I'm going to be pursuing. Is in hardening KVM. The first take away. Is that you absolutely should be. Sandboxing QMU KVM. If you're running it in a production environment. Fortunately if you're running it. At least under Ubuntu or Red Hat. Via libvert. They are already sandboxing it. Using SELinux and AppArmor. I haven't yet. But I want to take a look at those sandboxes. And see how hard they are. But at least. This is work that people do realize needs to be done. But if any of you are deploying KVM. Make sure. KVM in a remotely possibly sensitive context. Make sure that you're using a technology. That gives you that sandboxing. I think another no brainer. Is I'd like to see the distributions. Start building QMU KVM as PIE. As full position independent. The traditional reason not to do this. Is performance. Because it costs an extra register. To build as PIE. But virtually no one is running KVM on 32 bit platforms anymore. And AMB 64 has enough registers. That the performance impact. Is really negligible. I haven't done benchmarks on KVM. It's also on my to do list. But I think that's an obvious thing. That it will improve the state of the world. Then we can try some crazier ideas. We can try. To make things a little bit harder. The standard technique. XOR encoding key function pointers. XORing them with some constant value. That's stored at some address. And then on XORing them when you get them out. So that if a memory corruption bug tries to clobber them. They'll just basically end up with a random address. I think we'll need to do a little more thought. To figure out which things are likely to be targets. And might be worth applying this technique to. But it's a standard technique. That's used to harden applications. Against memory corruption bugs. And I think it's time that we start asking the question. Of whether it's appropriate here. And we can try doing even crazier things. Like protecting that guest memory region. By lazily mapping or protecting regions of it. Or sandboxing individual devices. So that they run in a thread. That somehow only has a view. Into that specific piece of memory. And this is a topic that. I've seen some discussion on the KVM lists. But that I think. Hopefully this work demonstrates. Does have potential value. And of course. I want to see more people looking at QMU KVM. And auditing it and fuzzing it. And fixing bugs. Some future research directions. From the offensive side. I want to go after KVM.KO. I'd like to see someone else look into it. Because I think that there probably are bugs there. And we should understand those bugs. And we should fix them. Another interesting question. That I don't know the answer to. And I want to know the answer to. Is how hard. Given that I know that I'm running in a guest. On QMU KVM. Can I fingerprint the exact QMU KVM version. Comparing the behavior. Or possibly even more subtle. Timing attacks. Against the memory layout or something. Because I've been assuming that I know. Which QMU KVM binary I'm running against. In the wild. You can usually assume that you're running against. A distros binary. But that's still a range of binaries. So how well can we fingerprint. Which will give us an understanding of. How weaponizable these attacks really. How general such exploits can be made. And then another question is. What do information leaks. In QMU KVM look like? Assuming that we get things built. As fully randomized. We're going to start wanting to find information leaks. To extract addresses. In order to write these kinds of exploits. Is there sort of a whole class. Of information leaks that we haven't realized yet. Or whole classes where. The standard idioms that QMU uses. For communicating with devices. Have a tendency to leak addresses. In a way that we hadn't realized. That's analogous to Dan Rosenberg's work. On the Linux kernel. Where he demonstrated that. As small as a four byte leak. Of uninitialized memory. Can reliably be used. To deduce kernel addresses. With a bunch of cleverness. So that's sort of my immediate checklist. Of future work on KVM. And I also hope to. Improve my fuzzer. And point it at other virtual machines. Because I haven't had a chance to do that. But for KVM. This is my checklist. And if you're interested in any of these. I encourage you to talk to me. And share thoughts. Whatever you have. All right. So finally. After all of that talking at you. It's time to demo my exploit. In progress. In action. So. Oh. Well. That text is not relevant. I just ran a shell script that launched a KVM VM. And I hope you can read this. It's not super relevant. But so I'm booting KVM. Just into an Ubuntu kernel. Just to show that this is a working KVM virtual machine. You know. We're in a VM. Proc CPU info shows. That we're running a QMU CPU. And the way. The way that I've implemented this exploit. Is that. Because we are hot plugging the ISA bus. There are a lot of virtual devices that go away. And if anyone tries to use those. There's a high risk of the machine seg faulting. Which is not useful. If we want to exploit it. So I've actually packaged my exploit. Into an init RD. That just contains. A statically compiled version of my exploit. And a minimal kernel. That doesn't use any of my exploit. And I've put it in grub. So we can boot straight into the exploit. And greatly increase the reliability. Of this attack. So we'll reboot the VM now. And from the grub prompt. We'll select my exploit. Doing things. Chasing pointers. And we pop up a calculator in the host. And obviously as I'm sure most of you are aware. The calculator is the standard. Yes I've exploited it demo. But that could be arbitrary code. And all right. As you can see here. The guest kernel is very upset. That it's real time clock has gone away. But it's valiantly stumbling forward. All right. So that is the end of this talk. The goons have told me. That I shouldn't bother heading to the QA room. Because there's no one after me. So if you have questions. Just head up here and we can chat.