1 00:00:00,000 --> 00:00:05,840 Hi, everybody. I'm going to be giving a talk about entitled 2 00:00:05,840 --> 00:00:09,280 virtuanoid breaking out of KVM, Linux kernel virtual machine. 3 00:00:10,160 --> 00:00:14,240 So what exactly is this KVM thing and why do I care about 4 00:00:14,240 --> 00:00:18,800 it? KVM is sort of the new hotness for virtualization on 5 00:00:18,800 --> 00:00:22,600 Linux. It's a virtualization system that was developed sort 6 00:00:22,600 --> 00:00:27,960 of from scratch to be the official entry supported by 7 00:00:27,960 --> 00:00:30,700 upstream, working friendly with the Linux community, 8 00:00:31,840 --> 00:00:35,780 virtualization solution that sort of started later than Zen and 9 00:00:35,780 --> 00:00:38,820 everyone else but has ramping up speed and sort of become the 10 00:00:38,820 --> 00:00:43,200 officially blessed platform for a lot of distributions to do 11 00:00:43,200 --> 00:00:47,080 virtualization. So it's an exciting new platform that I 12 00:00:47,080 --> 00:00:49,080 think is going to be seeing a lot of attention in the security 13 00:00:49,080 --> 00:00:52,380 space soon. So I decided to take a look at it and come up 14 00:00:52,380 --> 00:00:56,620 with some conclusions and share my results here. So who am I and 15 00:00:56,620 --> 00:00:57,940 why am I talking with you? I'm going to be talking about 16 00:00:57,960 --> 00:01:01,260 you. My day job actually has absolutely nothing to do with 17 00:01:01,260 --> 00:01:04,860 this. I'm a kernel engineer at a company called Ksplice but it 18 00:01:04,860 --> 00:01:08,400 does mean I spend a lot of time staring at low level software 19 00:01:08,400 --> 00:01:11,620 systems and so in my spare time I tend to do a lot of low level 20 00:01:11,620 --> 00:01:17,720 security stuff including this work on KVM. All right. So 21 00:01:17,720 --> 00:01:23,220 structure of this talk. We're going to start by taking a high 22 00:01:23,220 --> 00:01:27,900 level look at KVM at the Linux virtual machine. We'll look at 23 00:01:27,960 --> 00:01:30,960 what the different pieces are and how they fit together and then 24 00:01:30,960 --> 00:01:34,340 we'll go back and focus in on each of them from an attack surface 25 00:01:34,340 --> 00:01:38,260 perspective. From an attacker trying to break out of a KVM 26 00:01:38,260 --> 00:01:41,340 virtual machine. What's interesting about each of the 27 00:01:41,340 --> 00:01:44,500 components? How are they exposed to the attacker? What kind of 28 00:01:44,500 --> 00:01:47,900 things should we be thinking about and looking for? And then 29 00:01:47,900 --> 00:01:53,140 we'll run into a deep technical dive into the guts of actually 30 00:01:53,140 --> 00:01:57,760 writing a breakout exploit against KVM. Learning in the process a 31 00:01:57,760 --> 00:02:01,260 bunch about some useful features of the KVM architecture that come 32 00:02:01,260 --> 00:02:04,400 in handy and that we can exploit in creative ways. We'll start by 33 00:02:04,400 --> 00:02:07,800 just looking at the bug that my exploit centers around and then 34 00:02:07,800 --> 00:02:13,360 we'll talk about the actual exploit that I wrote. Then I'll 35 00:02:13,360 --> 00:02:16,700 discuss some conclusions that I think are sort of implied by my 36 00:02:16,700 --> 00:02:20,540 work or suggested and some future directions that I hope to proceed 37 00:02:20,540 --> 00:02:24,320 in or that I would love to see other people do work in. And then 38 00:02:24,320 --> 00:02:27,580 we'll demo the exploit because no talk on exploitation is 39 00:02:27,580 --> 00:02:27,740 complete. 40 00:02:31,500 --> 00:02:36,240 It's been reliable so far. All right. So KVM. How does it work? 41 00:02:37,240 --> 00:02:42,360 There's three main components to the KVM virtual machine. There's 42 00:02:43,740 --> 00:02:48,640 KVM.KO, the core kernel module. Then there's a pair of kernel 43 00:02:48,640 --> 00:02:52,860 modules for supporting Intel and AMD hardware support. And then 44 00:02:52,860 --> 00:02:57,740 there's the user space driver program, QMU KVM. Looking at 45 00:02:57,740 --> 00:03:02,180 the function of each of those and where they sit, KVM.KO is the 46 00:03:02,180 --> 00:03:07,060 core of the kernel modules, the kernel side support for KVM. It's 47 00:03:07,300 --> 00:03:11,220 sort of responsible for emulating and tracking the virtual CPU and 48 00:03:11,220 --> 00:03:16,000 memory management unit, the core of the virtual machine using the 49 00:03:16,000 --> 00:03:20,840 X86, AMDs and Intel's hardware virtualization extensions. It 50 00:03:20,840 --> 00:03:26,580 emulates a small number of devices and input output operations in 51 00:03:26,580 --> 00:03:30,200 kernel directly for efficiency. But for the most part, it doesn't 52 00:03:30,200 --> 00:03:34,500 deal with emulating hardware. And then it also provides a large 53 00:03:34,500 --> 00:03:38,960 interface for user space driver program, like QMU KVM, which we'll 54 00:03:38,960 --> 00:03:42,280 talk about in a second, to communicate with it, to allocate new virtual 55 00:03:42,280 --> 00:03:46,380 machines, allocate new virtual CPUs on those machines, set up the 56 00:03:46,380 --> 00:03:50,200 emulated physical memory and all of that. And one of my favorite bits 57 00:03:50,200 --> 00:03:55,860 of trivia about KVM.KO is despite the fact that we're using hardware 58 00:03:55,860 --> 00:04:01,200 virtualization extensions here, which means that for the most part, the 59 00:04:01,200 --> 00:04:04,920 virtual machine is just executed directly by the processor in a virtual 60 00:04:05,560 --> 00:04:09,920 machine non-root context, as Intel calls it, KVM still contains an 61 00:04:09,920 --> 00:04:13,960 entire X86 emulator in the kernel module that's used for handling 62 00:04:13,960 --> 00:04:18,940 certain rare traps. And just sort of from a code complexity and attack 63 00:04:18,940 --> 00:04:25,840 service perspective, it's an interesting thing to know. KVM Intel and KVM 64 00:04:25,860 --> 00:04:30,860 AMD are the other half, really less than half of the kernel component 65 00:04:30,860 --> 00:04:36,280 here. They just provide the glue code for communicating with the 66 00:04:36,280 --> 00:04:40,040 hardware virtualization extensions. You know, more or less you take those 67 00:04:40,040 --> 00:04:44,280 four chapters or whatever from the Intel manuals and implement the 68 00:04:44,280 --> 00:04:47,960 software half of that and you get these. And they're relatively small. 69 00:04:48,160 --> 00:04:53,020 They're one C file, 4,000 lines or so each, which compared to any of the 70 00:04:53,020 --> 00:04:53,940 other components is tiny. 71 00:04:55,860 --> 00:05:01,160 And then finally, we come to the user space component, QMU KVM, which is 72 00:05:01,160 --> 00:05:08,260 the direct user interface and user driver for the four KVM VMs. It's based 73 00:05:08,260 --> 00:05:12,740 on the classic QMU emulator, which I'm sure that almost all of you have 74 00:05:12,740 --> 00:05:16,140 heard of and probably even used, because it's amazingly useful and handy. 75 00:05:16,840 --> 00:05:21,260 And in addition to providing the interface and driver loop, QMU KVM 76 00:05:21,260 --> 00:05:25,340 implements basically all of the virtual devices that your VM talks to, 77 00:05:25,400 --> 00:05:25,840 because it's not just a virtual machine. It's a virtual machine. It's a virtual 78 00:05:25,860 --> 00:05:30,880 machine, much like a real machine, is not just a CPU talking to a block of 79 00:05:30,880 --> 00:05:34,020 memory. In order for it to be useful, there needs to be peripheral devices, 80 00:05:34,260 --> 00:05:39,260 PCI buses that, you know, hang off all kinds of devices, your Ethernet card, 81 00:05:39,520 --> 00:05:43,900 your display, a serial console, whatever, a timer device of some sort. 82 00:05:44,000 --> 00:05:48,880 All of that, all of that lives in QMU KVM. It's responsible for emulating 83 00:05:48,880 --> 00:05:54,400 those devices. And devices are complicated, it turns out. And this, as 84 00:05:54,400 --> 00:05:59,640 a result, contains a huge quantity of code. It's an order of magnitude more 85 00:05:59,640 --> 00:06:05,060 code than KVM.KO, even if you only consider the devices that are actually in 86 00:06:05,060 --> 00:06:08,280 use by a typical VM. If you consider, you know, all of the possible devices, 87 00:06:08,400 --> 00:06:14,700 it's even bigger. There's currently a project under way by the upstream 88 00:06:14,700 --> 00:06:19,000 Linux community to potentially replace this user space component with a 89 00:06:19,000 --> 00:06:23,680 separate developed more or less from scratch KVM binary that would be 90 00:06:23,680 --> 00:06:28,280 maintained in the Linux kernel tree. But that work is relatively new. It's 91 00:06:28,280 --> 00:06:32,480 not more than a year or two old. And it's going to be some years before it's 92 00:06:32,480 --> 00:06:35,880 stable enough that people are seriously using it and then some more years 93 00:06:37,140 --> 00:06:40,580 before distributions are actually shipping it and, you know, lots of people 94 00:06:40,580 --> 00:06:44,480 are using it in production. So if we're thinking about KVM, we're thinking 95 00:06:44,480 --> 00:06:50,140 about attacking or defending KVM for the near future QMU KVM is what we need 96 00:06:50,140 --> 00:06:53,660 to be talking about. All right. So those are the three things that we're 97 00:06:53,680 --> 00:06:57,540 talking about. Now we can go back and take a look at each one with an eye for 98 00:06:57,540 --> 00:07:04,720 attack surface. KVM.KO for an attacker is a very tempting target because it 99 00:07:04,720 --> 00:07:09,840 runs in the kernel mode on the host in ring zero. If I successfully find and 100 00:07:09,840 --> 00:07:14,740 exploit a bug here, I have ultimate privileges on the host with no further 101 00:07:14,740 --> 00:07:19,420 exploitation or privilege escalation needed. However, despite being a tempting 102 00:07:19,420 --> 00:07:22,760 target, it's also a bit of a tough target because there's not very much code 103 00:07:22,760 --> 00:07:26,860 there compared to this user space component. And what code there is is much 104 00:07:27,280 --> 00:07:31,380 of that is dedicated to interfacing with the user space component and so is not 105 00:07:31,380 --> 00:07:38,640 directly available for attack by a guest. That x86 emulator that I mentioned is 106 00:07:38,640 --> 00:07:42,040 definitely an interesting target because there's a lot of code there that is 107 00:07:42,040 --> 00:07:47,160 rarely exercised because the emulator is only used in some edge cases and that's 108 00:07:47,160 --> 00:07:51,840 sort of whenever I'm auditing a system, lots of subtle code that's rarely used is 109 00:07:51,840 --> 00:07:52,740 sort of the first place I'm going to go. So I'm going to go ahead and show you 110 00:07:52,760 --> 00:07:56,080 some of the places that I look for bugs. There have been a number of interesting 111 00:07:56,080 --> 00:08:01,380 bugs there that allowed privilege escalation within a guest because of bugs in the x86 112 00:08:01,380 --> 00:08:05,380 emulator. So there is a bit of a track record of bugs. Those aren't as interesting as 113 00:08:05,380 --> 00:08:09,580 breakout bugs, but again, it's a hint that this is an interesting place to look. It's 114 00:08:09,580 --> 00:08:12,540 unfortunately not the focus of this talk, but it's something that I want to highlight 115 00:08:12,540 --> 00:08:16,840 because I think future research should take a close look here. And then in addition to 116 00:08:16,840 --> 00:08:20,940 within the guest, just if we're talking about KVM.KO, we should keep in mind that 117 00:08:20,940 --> 00:08:23,860 there's also the possibility of privilege escalation in the host because 118 00:08:23,860 --> 00:08:29,940 unprivileged users can communicate with this privileged kernel module. Just another thing to 119 00:08:29,940 --> 00:08:36,460 note if we're looking at the whole scope of KVM exploitation. KVM Intel and KVM 120 00:08:36,460 --> 00:08:45,620 AMD.KO, the kernel modules that interface with the hardware, are not super interesting targets 121 00:08:45,620 --> 00:08:50,720 because there isn't a ton of code there. It's mostly sort of straight line code that just 122 00:08:50,720 --> 00:08:55,640 translates between the kernel's data structures that represent the VM and how the hardware 123 00:08:55,640 --> 00:09:00,720 wants to model those and just bridges back and forth. But there's a lot of subtlety and 124 00:09:00,720 --> 00:09:06,560 complexity in interacting with these hardware components. And so there's potentially scope for 125 00:09:06,560 --> 00:09:12,220 some interesting bugs where the KVM is using the hardware support slightly incorrectly or in an 126 00:09:13,040 --> 00:09:20,220 unusual way that allows for interesting attacks. But it's not the first place I'd look. And then 127 00:09:20,720 --> 00:09:26,400 finally we come again to QMU KVM, which as you can probably guess by now is the easiest place to 128 00:09:26,400 --> 00:09:31,560 look for targets here. Hundreds of thousands of lines of code, emulating device code, so the 129 00:09:31,560 --> 00:09:38,340 devices are talking directly to the guest via emulated memory mapped IO or IO ports. So they're 130 00:09:38,340 --> 00:09:44,680 parsing control structures that the guest is exporting and communicating these strange arcane 131 00:09:44,680 --> 00:09:50,700 hardware protocols. Lots of interesting code. And much of this code comes straight from QMU, 132 00:09:50,720 --> 00:09:56,620 which is mostly written by one guy, Fabrice Ballard, who is an absolute genius, brilliant 133 00:09:56,620 --> 00:10:02,540 programmer, but just one guy spewing out code over years with no one auditing it, bugs are going to 134 00:10:02,540 --> 00:10:11,140 happen. The one unfortunate thing about this as attackers is that it is often sandboxed using 135 00:10:11,140 --> 00:10:18,140 SELinux or app armor or some other technology. And so if we do successfully break out from KVM 136 00:10:18,140 --> 00:10:20,700 into QMU KVM, we're going to have a lot of problems. And so we're going to have a lot of problems. 137 00:10:21,860 --> 00:10:26,740 We'll probably need another privilege escalation attack to get full privileges on the host. 138 00:10:27,660 --> 00:10:32,540 Fortunately, we're all running Linux, which as any Slashdot reader knows has no bugs whatsoever, so 139 00:10:32,540 --> 00:10:42,700 we should be safe. All right. So that's the structure of KVM and taking a bit of a look at the 140 00:10:42,700 --> 00:10:47,760 attack surface. Now we're going to dive in on our bug, which is in fact, and the bug that I used, 141 00:10:47,900 --> 00:10:50,700 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 142 00:10:50,720 --> 00:10:57,100 QMU KVM in that user space driver program. I've got here the text from the Red Hat security 143 00:10:57,100 --> 00:11:03,080 advisory. The bug is CVE 2011 1751. All the major distributions have hatched it by now, so you can 144 00:11:03,080 --> 00:11:09,740 go look it up. Red Hat describes it as we found that the PIIX4 power management emulation layer 145 00:11:09,740 --> 00:11:16,000 did not properly check for hot plug eligibility. So what does that mean? First off, what is 146 00:11:16,000 --> 00:11:20,700 PIIX4? PIIX4 was an actual physical chip that was used to control the power of the device. 147 00:11:20,700 --> 00:11:28,200 It was the south bridge in circa 2000 Intel chipsets. And since it's a south bridge, what that 148 00:11:28,200 --> 00:11:35,140 means is that it talks to, it, most of the physical devices in computers of that era hung off of 149 00:11:35,140 --> 00:11:42,900 the south bridge architecturally. And so the PCI bus, the support, the real time clock, all of 150 00:11:42,900 --> 00:11:48,240 that stuff, the host, the CPU communicates with through the south bridge. And this is the default 151 00:11:48,240 --> 00:11:50,680 south bridge chip that QMU emulates. So we're going to take a look at that. So we're going to 152 00:11:50,700 --> 00:11:58,340 take a look at this. It supports PCI bus and PCI hot plug. It supports in physical hardware, 153 00:11:58,540 --> 00:12:03,340 what this would mean is a hot plug device, the chip and the PCI, you know, instructs the PCI bus 154 00:12:03,340 --> 00:12:08,760 to electronically disconnect it so that you can pull it out and be safe. In a virtual machine, 155 00:12:09,000 --> 00:12:14,320 what this just means is that we disconnect the, you know, virtual mappings for those I.O. ports 156 00:12:14,320 --> 00:12:20,600 and free the memory backing that device. It's expected that, you know, the destructor function 157 00:12:20,700 --> 00:12:26,780 for a virtual device performs this successful unplugging of everything. But not every device in 158 00:12:26,780 --> 00:12:31,920 QMU was implemented under the expectation that it might be hot plugged. And so many of these 159 00:12:31,920 --> 00:12:37,040 destructor functions are, some of these destructor functions are no ops or, you know, insufficiently 160 00:12:37,040 --> 00:12:40,740 clean up state. And that's supposed to be okay because you're not supposed to be able to hot plug 161 00:12:40,740 --> 00:12:47,440 these devices. And so it's okay if, you know, they don't successfully clean themselves up. But it 162 00:12:47,440 --> 00:12:50,680 turns out that, again, as the advisory says, it insufficiently cleans up state. And that's 163 00:12:50,700 --> 00:12:57,060 why we have sufficient checks for hot plug eligibility. The PIX4 PCI hot plug path, if you 164 00:12:57,060 --> 00:13:02,760 handed it a device, an identifier on the PCI bus to hot plug, it would just blindly go and hot 165 00:13:02,760 --> 00:13:09,260 plug it without actually checking the flag that says this is a hot pluggable device. In 166 00:13:09,260 --> 00:13:16,900 particular, a tempting target is the emulated ISA bridge. So who here actually remembers, you 167 00:13:16,900 --> 00:13:20,680 know, physical ISA cards and ISA buses? All right. Most of us. So, you know, we have a lot of 168 00:13:20,700 --> 00:13:24,960 those. We don't have those anymore, at least, you know, outside of, you know, hobbyist, you know, 169 00:13:24,980 --> 00:13:30,680 archaic hardware shows. But it turns out that your south bridge, even in your modern Intel chip sets, 170 00:13:30,860 --> 00:13:37,440 has an emulated ISA bus with emulated with virtual ISA devices hanging off of it. And QMU faithfully 171 00:13:37,440 --> 00:13:44,360 emulates this behavior and has a bunch of virtual ISA devices hanging off of a PCI ISA bridge. And 172 00:13:44,360 --> 00:13:49,180 we can just unplug that and all of those ISA devices, which include such things as the real time 173 00:13:49,180 --> 00:13:50,680 clock that QMU emulates. And we can just unplug that and all of those ISA devices, which include such 174 00:13:50,700 --> 00:13:55,580 things as the real time clock that QMU emulates. And we can just unplug that and all of those ISA devices 175 00:13:55,580 --> 00:14:01,440 real time clock, it turns out, is not expecting to be unplugged. In particular, it leaves around 176 00:14:01,440 --> 00:14:06,400 it's a real time clock, so it has timer events that it uses to keep track of the real time. It 177 00:14:06,400 --> 00:14:13,240 leaves those timers hanging around on QMU's run loop. And so we can just sort of look a little bit 178 00:14:13,240 --> 00:14:19,920 at what this means in code. The real time clock is emulated by this struct RTC state at the bottom 179 00:14:19,920 --> 00:14:20,680 of the screen. And we want to know what that means. So, for example, if you have a virtual machine 180 00:14:20,700 --> 00:14:26,660 And it has this second timer that it just schedules to get fired every second so that it can update the time. 181 00:14:26,960 --> 00:14:34,260 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. 182 00:14:34,540 --> 00:14:38,940 And then it has a callback and an opaque pointer that is passed to that callback. 183 00:14:39,240 --> 00:14:47,580 And so once a second, the QMU timer fires and calls this RTC update function on that RTC state struct. 184 00:14:48,380 --> 00:14:58,120 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. 185 00:14:58,180 --> 00:14:59,940 I've only shown the one that's relevant for this exploit. 186 00:15:00,220 --> 00:15:02,040 And so we're left with dangling pointers. 187 00:15:02,160 --> 00:15:06,920 That opaque pointer there is actually used as a pointer to the RTC state. 188 00:15:07,140 --> 00:15:09,940 And so we have a dangling pointer to a free object. 189 00:15:10,420 --> 00:15:17,400 And so within a second later, with the next second tick, we call this RTC update second function. 190 00:15:17,580 --> 00:15:19,720 And on a freed object. 191 00:15:20,000 --> 00:15:22,420 And we have a classic use after free bug here. 192 00:15:23,160 --> 00:15:32,700 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. 193 00:15:34,780 --> 00:15:40,320 And just to show how easy this is to reproduce, that's the reproducer. 194 00:15:40,400 --> 00:15:46,000 Compile that program, run it as root in a Linux machine on a vulnerable version of KVM, and KVM will seg fault. 195 00:15:50,460 --> 00:15:55,980 And you know, all this does is IOPL just gets privileges to be able to write out IOPORTS. 196 00:15:56,080 --> 00:15:58,860 And you write a single value to a single IOPORT and boom. 197 00:15:59,500 --> 00:16:06,640 In fact, this bug was found by a fuzzer that just literally wrote random values out random IOPORTS. 198 00:16:06,740 --> 00:16:09,680 And eventually it stumbled on this value and I get a signal 11. 199 00:16:10,040 --> 00:16:13,600 And I went and debugged it and wrote this talk. 200 00:16:14,860 --> 00:16:15,740 All right. 201 00:16:16,640 --> 00:16:17,160 So. 202 00:16:18,180 --> 00:16:19,060 That's the bug. 203 00:16:19,320 --> 00:16:21,640 So we have this beautiful use after free. 204 00:16:22,260 --> 00:16:30,320 But so what does it take to in the KVM environment go from a use after free like this to a working exploit? 205 00:16:32,860 --> 00:16:36,060 We'll talk about this process in three stages. 206 00:16:36,300 --> 00:16:40,820 First off, how do we go from this use after free to controlling the instruction pointer? 207 00:16:41,100 --> 00:16:45,140 How do we get KVM to jump to some address that we specify? 208 00:16:45,700 --> 00:16:47,180 And then once we have that. 209 00:16:47,580 --> 00:16:52,260 How do we leverage that into executing arbitrary shellcode inside the guest? 210 00:16:52,840 --> 00:16:54,040 And then once we have. 211 00:16:54,160 --> 00:17:01,000 And then I will have talked about those two assuming that I can guess addresses in the QMU KVM process. 212 00:17:01,680 --> 00:17:06,840 I.e. that there's no address space randomization in the process that I can predict addresses. 213 00:17:07,180 --> 00:17:11,700 And then we'll talk a little bit about what we have to do to get rid of that assumption. 214 00:17:11,880 --> 00:17:17,460 To work on a process with a randomized address space like KVM is going to be. 215 00:17:17,460 --> 00:17:19,280 In the wild on any real deployment. 216 00:17:21,340 --> 00:17:24,440 So for getting RIP control. 217 00:17:25,400 --> 00:17:28,340 The high level to do is sort of fairly straightforward. 218 00:17:28,600 --> 00:17:30,460 If you look at how the code works. 219 00:17:30,740 --> 00:17:35,360 We're going to need to create a fake QMU timer object that we control. 220 00:17:36,340 --> 00:17:38,900 And inject that at a known address. 221 00:17:39,440 --> 00:17:41,260 Then we'll trigger the eject. 222 00:17:41,600 --> 00:17:43,160 We'll dump the ISA bridge. 223 00:17:44,060 --> 00:17:46,840 And then we will force an allocation. 224 00:17:46,840 --> 00:17:52,060 Into the space previously occupied by the RTC state structure. 225 00:17:52,680 --> 00:17:56,420 That points that second timer field at our timer. 226 00:17:56,900 --> 00:17:59,080 Then on the next second boundary. 227 00:17:59,300 --> 00:18:03,120 RTC update second will run on our fake RTC state. 228 00:18:03,340 --> 00:18:07,160 And it will reschedule the second timer to run one second later. 229 00:18:07,680 --> 00:18:10,380 But we've hijacked that pointer to point at our timer. 230 00:18:10,540 --> 00:18:12,980 And so one second later our timer will run. 231 00:18:13,100 --> 00:18:16,100 Which includes the hour callback method. 232 00:18:16,180 --> 00:18:16,820 That it will jump. 233 00:18:16,840 --> 00:18:19,620 Into and it will jump to code that we control. 234 00:18:20,560 --> 00:18:22,080 So there's three steps here. 235 00:18:22,300 --> 00:18:24,020 I've already shown you how to do number two. 236 00:18:24,160 --> 00:18:25,760 How to eject the ISA bridge. 237 00:18:26,000 --> 00:18:27,280 And sort of actually trigger the vulnerability. 238 00:18:27,720 --> 00:18:29,720 Which leaves two things left to talk about. 239 00:18:29,900 --> 00:18:30,080 One. 240 00:18:30,460 --> 00:18:34,860 How do we construct fake devices in QMU KVM's address. 241 00:18:35,020 --> 00:18:38,040 Fake structures in QMU KVM's address space. 242 00:18:38,260 --> 00:18:40,560 How do we get objects that A. 243 00:18:41,080 --> 00:18:42,960 Appear in that address space somewhere. 244 00:18:43,120 --> 00:18:44,360 So that we can write pointers to them. 245 00:18:44,460 --> 00:18:44,780 And B. 246 00:18:44,860 --> 00:18:46,180 Appear at a known address. 247 00:18:46,180 --> 00:18:47,360 Or find out that address. 248 00:18:47,580 --> 00:18:49,040 So that we can predict that address. 249 00:18:50,180 --> 00:18:53,540 And write that second timer pointer to point at our thing. 250 00:18:54,540 --> 00:18:59,440 So this is one case where exploiting a virtual machine. 251 00:18:59,580 --> 00:19:01,040 Is actually sort of in some ways. 252 00:19:01,280 --> 00:19:04,420 Has a unique advantage over many other types of exploits. 253 00:19:04,520 --> 00:19:06,900 Is that that process turns out to be incredibly simple. 254 00:19:07,760 --> 00:19:14,520 The way that QMU KVM handles the emulated RAM for the guest. 255 00:19:14,760 --> 00:19:15,960 Is that it's just a big. 256 00:19:15,960 --> 00:19:18,760 M-Map region in QMU KVM's address space. 257 00:19:19,000 --> 00:19:20,780 That is the physical RAM of the guest. 258 00:19:22,280 --> 00:19:24,760 So there is no injection portion. 259 00:19:24,980 --> 00:19:26,740 Like I talked about here actually at all. 260 00:19:26,960 --> 00:19:28,920 We just literally allocate an object. 261 00:19:29,040 --> 00:19:30,340 However the hell we want. 262 00:19:30,440 --> 00:19:30,780 Statically. 263 00:19:31,020 --> 00:19:31,920 M-Map it. 264 00:19:32,040 --> 00:19:32,920 Allocate it on the stack. 265 00:19:33,520 --> 00:19:34,740 Whatever we feel like. 266 00:19:35,220 --> 00:19:37,540 Find the physical address of that. 267 00:19:38,320 --> 00:19:40,300 Find the physical address in the guest. 268 00:19:40,620 --> 00:19:42,500 Which we can do in a couple of ways. 269 00:19:42,760 --> 00:19:44,200 We can do in kernel mode. 270 00:19:44,280 --> 00:19:45,680 By walking the page tables explicitly. 271 00:19:45,960 --> 00:19:48,100 Or the kernel actually it turns out. 272 00:19:48,140 --> 00:19:49,480 Exports a proc file. 273 00:19:49,700 --> 00:19:50,860 That will just give us this information. 274 00:19:51,540 --> 00:19:52,020 But you know. 275 00:19:52,080 --> 00:19:53,820 Clearly we can find that from the guest. 276 00:19:54,120 --> 00:19:56,540 And then we just add that to the base address. 277 00:19:56,640 --> 00:19:59,720 Of the M-Map in the host process. 278 00:20:00,120 --> 00:20:01,440 And we found that object. 279 00:20:03,160 --> 00:20:03,860 For now. 280 00:20:04,440 --> 00:20:05,860 We're going to assume. 281 00:20:05,960 --> 00:20:08,600 That we know the base address of that M-Map. 282 00:20:08,920 --> 00:20:11,580 Because this base address is predictable. 283 00:20:13,420 --> 00:20:14,700 Assuming no ASLR. 284 00:20:14,940 --> 00:20:15,220 And again. 285 00:20:15,220 --> 00:20:17,260 We'll talk about how to get by that assumption later. 286 00:20:17,580 --> 00:20:18,700 So that's step one. 287 00:20:18,860 --> 00:20:20,840 Is injecting data at a controlled address. 288 00:20:21,240 --> 00:20:22,380 Actually totally easy. 289 00:20:22,480 --> 00:20:23,660 When you're attacking a virtual machine. 290 00:20:24,160 --> 00:20:25,140 And this is true. 291 00:20:25,300 --> 00:20:28,000 I believe in basically actually all current virtual machines. 292 00:20:28,100 --> 00:20:29,260 Do this or something similar. 293 00:20:29,400 --> 00:20:30,920 So this is not totally unique to KVM. 294 00:20:31,520 --> 00:20:33,100 Although this is the first exploit. 295 00:20:33,260 --> 00:20:35,060 I'm aware of that's talked about this technique. 296 00:20:36,480 --> 00:20:39,900 Second technique is forcing allocations. 297 00:20:40,100 --> 00:20:41,840 Inside the QMU KVM process. 298 00:20:42,080 --> 00:20:44,680 We need to get QMU KVM. 299 00:20:44,680 --> 00:20:46,240 To do a malloc. 300 00:20:46,360 --> 00:20:48,420 And then populate it with data that we control. 301 00:20:48,720 --> 00:20:50,300 Of an appropriate size. 302 00:20:50,640 --> 00:20:53,560 That it will with high probability. 303 00:20:53,680 --> 00:20:55,440 Get allocated into the space. 304 00:20:55,620 --> 00:20:57,360 That destruct RTC state. 305 00:20:57,540 --> 00:20:58,240 Used to occupy. 306 00:20:58,760 --> 00:20:59,840 To do that. 307 00:20:59,960 --> 00:21:03,020 I'm going to use a feature of the QMU KVM. 308 00:21:03,160 --> 00:21:04,520 Network stack that I'll talk about. 309 00:21:05,980 --> 00:21:07,220 QMU KVM. 310 00:21:07,660 --> 00:21:09,260 Inherits from QMU. 311 00:21:09,360 --> 00:21:11,160 A user mode networking stack. 312 00:21:11,480 --> 00:21:13,600 That implements an entire. 313 00:21:13,760 --> 00:21:14,620 Virtual LAN. 314 00:21:14,680 --> 00:21:16,860 Inside the QMU KVM process. 315 00:21:17,240 --> 00:21:18,380 This is used. 316 00:21:19,040 --> 00:21:20,640 As a way of allowing you. 317 00:21:20,700 --> 00:21:21,900 To get network access to the guest. 318 00:21:22,060 --> 00:21:24,460 Without having to mess around with bridge devices. 319 00:21:24,760 --> 00:21:25,600 And ton tap devices. 320 00:21:25,740 --> 00:21:28,100 Or other such madness on the host. 321 00:21:28,300 --> 00:21:30,700 And also without requiring privilege. 322 00:21:30,880 --> 00:21:32,840 On the part of the QMU KVM process. 323 00:21:33,400 --> 00:21:35,140 It's the default networking set up. 324 00:21:35,240 --> 00:21:36,480 So it's a reasonable thing to attack. 325 00:21:36,820 --> 00:21:38,480 Although in production environments. 326 00:21:38,860 --> 00:21:40,680 It's probably more common that we'll see. 327 00:21:40,760 --> 00:21:42,220 Bridge networks or something else. 328 00:21:42,560 --> 00:21:44,460 And so we'd have to modify this step there. 329 00:21:44,680 --> 00:21:46,860 But the same fundamental principles. 330 00:21:46,880 --> 00:21:48,080 Of most of the attack would apply. 331 00:21:49,080 --> 00:21:50,900 So this virtual network stack. 332 00:21:51,180 --> 00:21:52,680 You know emulates a DHCP server. 333 00:21:52,820 --> 00:21:54,020 DNS server and gateway NAT. 334 00:21:54,180 --> 00:21:55,820 All plugged into this virtual LAN. 335 00:21:56,020 --> 00:21:58,420 The way that this virtual LAN handles packet delivery. 336 00:21:58,740 --> 00:22:00,660 Is that normally packets are handled. 337 00:22:00,840 --> 00:22:01,360 Synchronously. 338 00:22:02,480 --> 00:22:05,000 When you inject a packet onto the virtual LAN. 339 00:22:05,200 --> 00:22:06,580 It just looks up the recipient. 340 00:22:06,920 --> 00:22:07,960 And you know calls there. 341 00:22:08,080 --> 00:22:09,700 Deliver packet callback method. 342 00:22:10,080 --> 00:22:10,600 Synchronously. 343 00:22:10,600 --> 00:22:12,360 And so there's no buffering. 344 00:22:12,380 --> 00:22:13,020 No queuing. 345 00:22:13,420 --> 00:22:16,260 But in order to prevent recursion. 346 00:22:16,320 --> 00:22:18,960 If a virtual device. 347 00:22:19,080 --> 00:22:20,100 Or virtual hosts. 348 00:22:20,120 --> 00:22:20,920 Deliver mechanism. 349 00:22:21,320 --> 00:22:23,300 Then injects a second packet. 350 00:22:23,440 --> 00:22:24,900 In response to the first packet. 351 00:22:25,120 --> 00:22:26,860 That packet is queued. 352 00:22:26,900 --> 00:22:27,740 Using malloc. 353 00:22:27,880 --> 00:22:30,520 With a small header appended. 354 00:22:30,900 --> 00:22:33,160 And then delivered later. 355 00:22:33,260 --> 00:22:34,700 Once we return to the main run loop. 356 00:22:35,080 --> 00:22:38,880 So if we can find a device. 357 00:22:39,000 --> 00:22:40,440 On the virtual LAN. 358 00:22:40,600 --> 00:22:43,360 That responds to packets. 359 00:22:43,520 --> 00:22:44,400 From the user. 360 00:22:45,220 --> 00:22:47,760 By synchronously generating a second packet. 361 00:22:48,020 --> 00:22:49,500 Whose contents are. 362 00:22:49,840 --> 00:22:51,540 All or almost all controlled. 363 00:22:51,760 --> 00:22:53,620 By the contents of the first packet. 364 00:22:54,080 --> 00:22:55,640 Then that will generate a malloc. 365 00:22:55,740 --> 00:22:56,640 With controlled contents. 366 00:22:57,420 --> 00:22:58,980 Can anyone think of a network service. 367 00:22:59,160 --> 00:22:59,840 That has that property? 368 00:23:01,940 --> 00:23:02,460 Ping. 369 00:23:02,580 --> 00:23:03,980 ICMP ping echo packets. 370 00:23:04,500 --> 00:23:06,820 So the way we're actually going to force allocations. 371 00:23:06,820 --> 00:23:07,660 Inside the guest. 372 00:23:07,900 --> 00:23:09,600 Is by pinging the virtual gateway. 373 00:23:11,380 --> 00:23:13,600 And it will reflect the ICMP packets. 374 00:23:13,700 --> 00:23:14,280 Back at us. 375 00:23:14,300 --> 00:23:16,200 With the payload of length we control. 376 00:23:16,440 --> 00:23:17,480 And contents we control. 377 00:23:17,920 --> 00:23:19,480 Which will force mallocs. 378 00:23:19,560 --> 00:23:21,680 And let us exploit this use after free. 379 00:23:30,200 --> 00:23:31,080 And so now. 380 00:23:31,140 --> 00:23:32,020 Putting the pieces together. 381 00:23:32,460 --> 00:23:35,040 We allocate a fake QMU timer. 382 00:23:35,960 --> 00:23:37,300 In our address space. 383 00:23:37,700 --> 00:23:39,280 With a callback method. 384 00:23:40,600 --> 00:23:42,380 Which is whatever address we want KVM to jump to. 385 00:23:42,740 --> 00:23:45,040 We'll calculate its address in the host. 386 00:23:45,180 --> 00:23:46,880 Using the arithmetic I showed earlier. 387 00:23:47,160 --> 00:23:48,560 We'll do that. 388 00:23:48,740 --> 00:23:49,600 Eject dance. 389 00:23:50,020 --> 00:23:51,900 And then we'll ping the emulated gateway. 390 00:23:52,140 --> 00:23:53,360 As fast as we can. 391 00:23:53,620 --> 00:23:55,940 With ICMP packets that are. 392 00:23:56,140 --> 00:23:58,980 Have pointers to our fake timer. 393 00:23:59,080 --> 00:23:59,600 In the host. 394 00:23:59,880 --> 00:24:02,220 And with extremely high probability. 395 00:24:02,500 --> 00:24:03,240 Basically one. 396 00:24:03,960 --> 00:24:06,100 One of those will end up allocated. 397 00:24:06,220 --> 00:24:08,500 Into the space previously occupied. 398 00:24:08,640 --> 00:24:09,880 By the RTC state. 399 00:24:10,940 --> 00:24:12,260 We have the ability. 400 00:24:12,480 --> 00:24:13,580 To get the QMU to jump. 401 00:24:13,660 --> 00:24:14,860 To an address we control. 402 00:24:15,560 --> 00:24:16,140 All right. 403 00:24:18,740 --> 00:24:19,980 So that's part one. 404 00:24:20,140 --> 00:24:21,480 We have RIP control. 405 00:24:21,780 --> 00:24:23,880 We can get QMU to jump to any address we want. 406 00:24:24,280 --> 00:24:25,940 But we still have to deal with. 407 00:24:26,020 --> 00:24:27,320 Non-executable pages. 408 00:24:27,760 --> 00:24:29,240 Or an injection shell code somehow. 409 00:24:29,400 --> 00:24:30,260 We're not quite there yet. 410 00:24:30,580 --> 00:24:32,500 So what are our options to do next? 411 00:24:33,600 --> 00:24:34,800 We'll start with the classics. 412 00:24:35,320 --> 00:24:38,580 We can set EIP equals 41414141. 413 00:24:38,860 --> 00:24:39,300 AAAA. 414 00:24:40,600 --> 00:24:42,260 That clearly demonstrates it's possible. 415 00:24:42,760 --> 00:24:43,500 And, you know, we're done. 416 00:24:44,180 --> 00:24:44,840 That's boring. 417 00:24:45,240 --> 00:24:46,680 I could disable NX. 418 00:24:46,840 --> 00:24:48,900 Non-executable pages in my BIOS. 419 00:24:48,900 --> 00:24:49,460 Or in my kernel. 420 00:24:50,380 --> 00:24:52,060 Blatch shell code wherever the hell I want. 421 00:24:52,460 --> 00:24:53,400 That's also boring. 422 00:24:54,200 --> 00:24:55,320 Getting more interesting. 423 00:24:55,620 --> 00:24:58,940 We could use a technique called return oriented programming. 424 00:24:59,220 --> 00:25:02,520 To chain together bits of QMU's own code. 425 00:25:02,720 --> 00:25:03,760 To do the standard. 426 00:25:04,560 --> 00:25:05,820 MMAP or MProtect. 427 00:25:05,980 --> 00:25:07,640 To allocate executable shell code. 428 00:25:08,040 --> 00:25:09,680 And then jump into the shell code that way. 429 00:25:10,600 --> 00:25:12,760 That's a technique used in most exploits these days. 430 00:25:12,980 --> 00:25:14,980 It would be a perfectly fine strategy here. 431 00:25:15,260 --> 00:25:17,360 But I have a slightly different strategy. 432 00:25:17,380 --> 00:25:19,080 That I happen to like. 433 00:25:19,220 --> 00:25:20,340 That I stumbled upon here. 434 00:25:20,660 --> 00:25:22,500 That I think has some cleaner properties. 435 00:25:22,720 --> 00:25:24,920 And was easier to develop than doing a rough payload. 436 00:25:25,140 --> 00:25:26,100 So we'll talk about that. 437 00:25:27,640 --> 00:25:29,300 So let's take another look. 438 00:25:29,440 --> 00:25:31,160 At that QMU timer structure. 439 00:25:31,320 --> 00:25:32,060 That we're faking. 440 00:25:33,040 --> 00:25:35,140 So we focused on the callback. 441 00:25:35,200 --> 00:25:36,720 And opaque members before. 442 00:25:37,180 --> 00:25:39,340 But now let's look at this expire time. 443 00:25:39,580 --> 00:25:40,520 And this next pointer. 444 00:25:40,600 --> 00:25:43,780 And so the way that these timers are implemented. 445 00:25:43,860 --> 00:25:46,080 Is they're stored in a sorted linked list. 446 00:25:46,480 --> 00:25:48,420 Threaded through by that next pointer. 447 00:25:48,960 --> 00:25:54,500 And every time the QMU main loop wakes up. 448 00:25:54,660 --> 00:25:55,960 It runs these timers. 449 00:25:56,020 --> 00:25:58,220 Which just walks these timers. 450 00:25:58,220 --> 00:25:59,960 By this linked list. 451 00:26:00,080 --> 00:26:01,400 By following that next pointer. 452 00:26:02,100 --> 00:26:04,400 As long as that expire time. 453 00:26:04,560 --> 00:26:06,140 Is before the current time. 454 00:26:07,160 --> 00:26:09,400 And so we control that next pointer. 455 00:26:09,520 --> 00:26:10,580 Because we control this entire time. 456 00:26:10,600 --> 00:26:12,320 So what we can do. 457 00:26:12,560 --> 00:26:14,380 Is construct multiple timers. 458 00:26:14,460 --> 00:26:15,640 And chain them together. 459 00:26:15,760 --> 00:26:16,820 By that next pointer. 460 00:26:17,440 --> 00:26:20,400 So that we can cause QMU KBM. 461 00:26:20,660 --> 00:26:22,260 To execute multiple functions. 462 00:26:22,320 --> 00:26:22,980 In a row. 463 00:26:23,620 --> 00:26:26,320 And perform sort of limited strings of computation. 464 00:26:26,780 --> 00:26:27,640 In the host. 465 00:26:27,780 --> 00:26:29,240 Rather than just doing one jump. 466 00:26:29,440 --> 00:26:31,860 And we're going to point these functions at existing functions. 467 00:26:32,280 --> 00:26:33,300 Inside the host. 468 00:26:33,360 --> 00:26:35,600 That we're going to string together in an unexpected way. 469 00:26:35,960 --> 00:26:37,180 So this is sort of. 470 00:26:37,720 --> 00:26:38,500 Halfway between. 471 00:26:39,900 --> 00:26:41,740 Traditional return to libc attacks. 472 00:26:42,020 --> 00:26:43,100 And ROP attacks. 473 00:26:44,040 --> 00:26:44,780 With you know. 474 00:26:44,840 --> 00:26:46,220 A slightly unorthodox method. 475 00:26:46,320 --> 00:26:47,260 Of doing the dispatching. 476 00:26:49,000 --> 00:26:50,120 So now one feature. 477 00:26:50,280 --> 00:26:51,060 We notice here. 478 00:26:51,180 --> 00:26:54,260 Is that we have a large number of one argument function calls. 479 00:26:54,540 --> 00:26:56,500 We want to do more argument function calls. 480 00:26:56,860 --> 00:26:57,780 So to do that. 481 00:26:57,980 --> 00:26:59,640 We're going to dive a little bit into. 482 00:26:59,800 --> 00:27:01,960 The detail of the AMD64 calling convention. 483 00:27:02,160 --> 00:27:02,780 How it works. 484 00:27:03,260 --> 00:27:04,920 So on AMD64. 485 00:27:07,180 --> 00:27:09,400 We pass arguments to functions. 486 00:27:09,760 --> 00:27:10,140 In registers. 487 00:27:10,400 --> 00:27:13,080 Starting with RDI, RSI, RDX, etc. 488 00:27:13,400 --> 00:27:15,460 So RDX is the first argument. 489 00:27:15,620 --> 00:27:17,240 That's going to get the opaque member. 490 00:27:17,460 --> 00:27:18,580 Of our fake timers. 491 00:27:19,140 --> 00:27:21,120 RSI we don't directly control. 492 00:27:21,600 --> 00:27:21,980 However. 493 00:27:22,320 --> 00:27:25,020 Every compiled version of QMU run timers. 494 00:27:25,040 --> 00:27:25,560 That I've encountered. 495 00:27:25,800 --> 00:27:27,600 Leaves RSI untouched. 496 00:27:27,660 --> 00:27:28,760 It doesn't clobber RSI. 497 00:27:29,540 --> 00:27:32,040 So suppose that we found a function. 498 00:27:32,140 --> 00:27:34,720 That looked like this hypothetical set RSI function. 499 00:27:35,060 --> 00:27:37,060 It sets the RSI register. 500 00:27:37,180 --> 00:27:38,760 Based on the RDI register. 501 00:27:39,040 --> 00:27:40,360 Which is the first argument register. 502 00:27:41,780 --> 00:27:42,820 So back. 503 00:27:42,880 --> 00:27:45,080 If we go back to our chain of function calls. 504 00:27:45,200 --> 00:27:46,060 We set F1. 505 00:27:46,300 --> 00:27:47,740 To set RSI. 506 00:27:48,520 --> 00:27:50,220 And we pass it some argument. 507 00:27:50,500 --> 00:27:52,540 So then we're going to populate RSI. 508 00:27:53,120 --> 00:27:55,020 With that first argument. 509 00:27:55,280 --> 00:27:57,560 That argument to the first timer. 510 00:27:57,980 --> 00:27:59,600 RSI is going to get populated with that. 511 00:28:00,080 --> 00:28:01,760 And then we call the second function. 512 00:28:01,940 --> 00:28:03,240 We now control both arguments. 513 00:28:03,560 --> 00:28:05,300 So we can get to two arguments this way. 514 00:28:05,300 --> 00:28:08,340 The same trick doesn't work with RDX. 515 00:28:08,480 --> 00:28:09,360 The third argument register. 516 00:28:09,680 --> 00:28:11,560 Because QMU run timers. 517 00:28:11,580 --> 00:28:12,540 Does clobber that. 518 00:28:12,840 --> 00:28:14,560 So we can't count on it being preserved. 519 00:28:14,680 --> 00:28:17,120 Across different timers. 520 00:28:17,420 --> 00:28:18,060 Timer calls. 521 00:28:19,160 --> 00:28:19,680 So. 522 00:28:20,660 --> 00:28:22,760 We'll talk about what we do there in a second. 523 00:28:22,940 --> 00:28:24,440 So this function. 524 00:28:24,920 --> 00:28:27,460 Happens to be the set RSI. 525 00:28:27,680 --> 00:28:28,720 Gadget that I chose. 526 00:28:30,940 --> 00:28:33,060 What this actually does in QMU. 527 00:28:33,140 --> 00:28:34,240 Is relatively unimportant. 528 00:28:35,300 --> 00:28:37,240 Because it takes a value. 529 00:28:37,360 --> 00:28:38,460 As its first argument. 530 00:28:38,740 --> 00:28:40,060 And then it calls a function. 531 00:28:40,240 --> 00:28:41,860 With that value as the second argument. 532 00:28:42,160 --> 00:28:44,380 Which means that it moves it into RSI. 533 00:28:44,880 --> 00:28:47,500 And so as long as we choose a value of adder. 534 00:28:47,600 --> 00:28:49,340 That makes IO port write. 535 00:28:49,500 --> 00:28:51,080 A no op or other or you know. 536 00:28:51,140 --> 00:28:52,280 Mostly harmless operation. 537 00:28:52,920 --> 00:28:55,500 This has the effect of populating RSI. 538 00:28:55,660 --> 00:28:56,220 From RDI. 539 00:28:57,460 --> 00:28:58,700 So that's that. 540 00:28:58,840 --> 00:29:01,100 So now to get to a three argument. 541 00:29:01,200 --> 00:29:02,220 M protect call. 542 00:29:03,400 --> 00:29:04,280 Because M protect. 543 00:29:05,300 --> 00:29:06,680 Calls to set things executable. 544 00:29:07,240 --> 00:29:08,760 We need to call with an address. 545 00:29:08,920 --> 00:29:10,920 A length and a protection value. 546 00:29:11,140 --> 00:29:13,040 That we want to contain at least prot exec. 547 00:29:13,980 --> 00:29:15,540 A little bit of searching. 548 00:29:15,640 --> 00:29:17,280 For useful patterns with some grep. 549 00:29:17,400 --> 00:29:18,940 Finds us this interesting function. 550 00:29:19,120 --> 00:29:20,560 IO port read L thunk. 551 00:29:20,860 --> 00:29:22,760 Again what this is actually supposed to do. 552 00:29:22,820 --> 00:29:23,500 Totally irrelevant. 553 00:29:24,120 --> 00:29:25,640 But it takes two arguments. 554 00:29:25,860 --> 00:29:26,780 Which again we control. 555 00:29:27,220 --> 00:29:29,300 And then it follows some function pointers. 556 00:29:29,600 --> 00:29:30,660 To call it. 557 00:29:31,120 --> 00:29:33,060 So we control those function pointers. 558 00:29:35,700 --> 00:29:37,720 With a size that we control. 559 00:29:38,080 --> 00:29:39,200 With a third argument. 560 00:29:39,240 --> 00:29:41,000 That just conveniently happens to be equal. 561 00:29:41,080 --> 00:29:41,820 To prot exec. 562 00:29:42,180 --> 00:29:44,040 So it will set that memory executable. 563 00:29:49,780 --> 00:29:50,500 All right. 564 00:29:50,680 --> 00:29:51,200 So. 565 00:29:52,280 --> 00:29:54,060 To put the steps together. 566 00:29:54,960 --> 00:29:56,680 We need to allocate a fake. 567 00:29:56,800 --> 00:29:58,680 IO range op struct. 568 00:29:58,980 --> 00:30:00,140 Which is that operation structure. 569 00:30:00,520 --> 00:30:02,640 We need to set the read op to M protect. 570 00:30:03,040 --> 00:30:05,160 We need to allocate an IO range object. 571 00:30:05,300 --> 00:30:06,220 That needs to be page aligned. 572 00:30:06,580 --> 00:30:08,320 Because we're going to call M protect on it. 573 00:30:08,740 --> 00:30:10,900 And it has ops pointing at fake ops. 574 00:30:11,100 --> 00:30:13,680 And base is set up such that that adder. 575 00:30:13,700 --> 00:30:15,240 Minus IO port base computation. 576 00:30:15,960 --> 00:30:17,140 Yields a useful length. 577 00:30:18,440 --> 00:30:20,620 And then we're going to copy our shell code. 578 00:30:20,740 --> 00:30:21,700 Into that same page. 579 00:30:21,860 --> 00:30:23,600 Immediately following the IO range. 580 00:30:24,280 --> 00:30:26,000 And then we'll do this timer chain. 581 00:30:26,180 --> 00:30:27,720 We'll call CPU out L. 582 00:30:27,920 --> 00:30:30,600 To set the second argument register to zero. 583 00:30:30,940 --> 00:30:32,480 Because that happens to be a value. 584 00:30:32,580 --> 00:30:34,360 That makes IO port read. 585 00:30:34,560 --> 00:30:34,780 Which. 586 00:30:35,300 --> 00:30:36,360 Is the IO port right up here. 587 00:30:36,460 --> 00:30:36,880 Harmless. 588 00:30:37,580 --> 00:30:40,720 And then we do this IO port read L thunk. 589 00:30:40,880 --> 00:30:42,820 Which through this hilarious chain of indirection. 590 00:30:42,960 --> 00:30:44,180 Will result in an M protect. 591 00:30:44,940 --> 00:30:47,840 And then we'll just jump right into that fake IO port. 592 00:30:47,960 --> 00:30:48,460 Plus one. 593 00:30:48,700 --> 00:30:50,260 Which is where we stuck our shell code. 594 00:30:52,060 --> 00:30:52,660 All right. 595 00:30:53,200 --> 00:30:54,520 And so that works. 596 00:30:54,660 --> 00:30:55,660 Turn off ASLR. 597 00:30:55,940 --> 00:30:57,120 Code up all the offsets right. 598 00:30:57,300 --> 00:30:58,300 And you've got code execution. 599 00:30:59,160 --> 00:31:00,220 So one question. 600 00:31:00,360 --> 00:31:03,840 Before we jump into getting by ASLR. 601 00:31:05,300 --> 00:31:05,480 I want to ask you a couple of questions. 602 00:31:05,600 --> 00:31:08,420 Return oriented programming is a standard well understood technique. 603 00:31:08,720 --> 00:31:10,740 That is the standard way to write these exploits. 604 00:31:10,820 --> 00:31:12,160 Why did I do this more creative thing? 605 00:31:13,960 --> 00:31:15,060 There's a couple of reasons. 606 00:31:15,320 --> 00:31:18,260 One is that this mechanism makes continuing execution. 607 00:31:18,720 --> 00:31:18,980 In the. 608 00:31:19,080 --> 00:31:21,820 Getting the QMU KVM process to continue executing. 609 00:31:22,060 --> 00:31:22,780 Dead simple. 610 00:31:23,120 --> 00:31:25,520 Because we're not corrupting the stack. 611 00:31:25,760 --> 00:31:27,200 We're not really smashing any state. 612 00:31:27,360 --> 00:31:30,780 We're just sort of hijacking the legitimate functionality of the run loop. 613 00:31:30,780 --> 00:31:32,640 And so after all of this returns. 614 00:31:32,840 --> 00:31:33,820 Everything is actually. 615 00:31:34,160 --> 00:31:37,080 Except for you know the things that we freed as a result of our exploit. 616 00:31:37,240 --> 00:31:38,480 Everything is in a sane state. 617 00:31:38,660 --> 00:31:39,860 And we can just continue executing. 618 00:31:40,880 --> 00:31:42,840 Another thing that I like about this technique. 619 00:31:43,080 --> 00:31:43,720 Is that. 620 00:31:43,980 --> 00:31:47,240 I've shown you virtually no assembly on the previous few slides. 621 00:31:47,500 --> 00:31:48,640 I've referred to one detail. 622 00:31:48,780 --> 00:31:50,340 But there's very little dependence on. 623 00:31:50,540 --> 00:31:52,680 How these functions actually got compiled. 624 00:31:53,180 --> 00:31:54,120 And ROP. 625 00:31:54,400 --> 00:31:56,780 Cares very deeply about how the functions got compiled. 626 00:31:56,960 --> 00:31:58,480 Because you're chaining together strings of assembly. 627 00:31:59,440 --> 00:32:00,400 When you're executing. 628 00:32:00,780 --> 00:32:02,540 When you're exploiting a program under Linux. 629 00:32:02,760 --> 00:32:03,900 You have to deal with the fact. 630 00:32:04,000 --> 00:32:06,540 That every Linux vendor has their own build of the program. 631 00:32:06,700 --> 00:32:08,060 With a slightly different source version. 632 00:32:08,200 --> 00:32:09,760 A slightly different GCC version. 633 00:32:09,900 --> 00:32:11,120 A slightly different set of flags. 634 00:32:11,660 --> 00:32:14,680 And so if you want to have to try to find ROP gadgets. 635 00:32:14,760 --> 00:32:16,020 Across every single one. 636 00:32:16,240 --> 00:32:17,460 It's a fair amount of work. 637 00:32:17,640 --> 00:32:19,380 And there aren't yet great tools. 638 00:32:19,440 --> 00:32:21,560 For doing this in a completely automated fashion on Linux. 639 00:32:22,040 --> 00:32:22,780 Whereas here. 640 00:32:22,980 --> 00:32:24,560 I just need to look at the source. 641 00:32:24,860 --> 00:32:26,420 And then I just need to grab the symbol table. 642 00:32:26,560 --> 00:32:28,140 From all of those different versions. 643 00:32:30,780 --> 00:32:32,400 Of these functions that exist in the C code. 644 00:32:32,640 --> 00:32:34,600 And so are more or less preserved. 645 00:32:34,700 --> 00:32:36,580 Across compilation by different versions. 646 00:32:37,620 --> 00:32:38,560 And then finally. 647 00:32:38,600 --> 00:32:39,320 The cop out answer. 648 00:32:39,500 --> 00:32:41,440 Is that I'm just personally not that good at ROP. 649 00:32:41,580 --> 00:32:43,380 I don't know great tools for doing it on Linux. 650 00:32:43,720 --> 00:32:45,400 And so I decided to try something different. 651 00:32:47,220 --> 00:32:47,840 All right. 652 00:32:47,940 --> 00:32:50,160 So we've got code execution. 653 00:32:50,520 --> 00:32:53,280 But I've been assuming no ASLR. 654 00:32:53,500 --> 00:32:55,140 So let's get rid of that restriction. 655 00:32:55,700 --> 00:32:57,340 So we need two addresses. 656 00:32:57,620 --> 00:32:59,220 There's fundamentally two addresses. 657 00:32:59,280 --> 00:33:00,000 That we've been using. 658 00:33:00,780 --> 00:33:02,360 One is the base address. 659 00:33:02,440 --> 00:33:04,060 Of the QMU KVM binary. 660 00:33:04,300 --> 00:33:06,420 If we assume we're attacking a known binary. 661 00:33:06,640 --> 00:33:08,900 We know the layout of all of the functions in memory. 662 00:33:09,040 --> 00:33:10,180 And so we know their offsets. 663 00:33:10,180 --> 00:33:11,740 From the executable base. 664 00:33:12,120 --> 00:33:14,120 But we need to know where that executable is loaded. 665 00:33:14,600 --> 00:33:15,340 And secondly. 666 00:33:15,480 --> 00:33:18,080 We need to know the address of that physical memory mapping. 667 00:33:18,220 --> 00:33:19,540 Inside QMU KVM. 668 00:33:19,860 --> 00:33:21,100 In order to. 669 00:33:22,480 --> 00:33:23,920 In order to get the address. 670 00:33:24,020 --> 00:33:25,600 Of fake objects we're injecting. 671 00:33:27,380 --> 00:33:29,180 So there's a couple of answers here. 672 00:33:29,240 --> 00:33:30,240 The classic answer is. 673 00:33:30,780 --> 00:33:31,380 Find a sufficiently. 674 00:33:32,080 --> 00:33:33,940 Sufficiently powerful information leak. 675 00:33:34,100 --> 00:33:36,780 That lets us leak the contents of pointers. 676 00:33:36,920 --> 00:33:37,820 Or the contents of memory. 677 00:33:38,140 --> 00:33:40,180 And back solve or back derive. 678 00:33:40,200 --> 00:33:42,240 Somehow these addresses from leaked information. 679 00:33:43,460 --> 00:33:44,880 That's a classic way of doing this. 680 00:33:45,040 --> 00:33:46,960 I didn't end up going that way. 681 00:33:47,200 --> 00:33:48,760 I decided to do. 682 00:33:48,940 --> 00:33:49,680 Again something else. 683 00:33:49,720 --> 00:33:51,460 That I think is a little more informative and interesting. 684 00:33:51,840 --> 00:33:53,000 The second option is. 685 00:33:53,040 --> 00:33:54,540 We can take advantage of the fact. 686 00:33:54,880 --> 00:33:57,060 That every major distribution. 687 00:33:57,100 --> 00:33:59,020 Still compiles KVM. 688 00:34:00,780 --> 00:34:02,780 Not as a position independent executable. 689 00:34:03,540 --> 00:34:04,620 If you're not familiar. 690 00:34:04,800 --> 00:34:05,960 With implementation. 691 00:34:06,200 --> 00:34:07,980 Of address space randomization. 692 00:34:08,280 --> 00:34:09,480 What this ends up meaning. 693 00:34:09,700 --> 00:34:13,120 Is that the QMU KVM actual core elf binary. 694 00:34:13,500 --> 00:34:14,980 Is loaded at a fixed address. 695 00:34:15,100 --> 00:34:15,620 Every time. 696 00:34:15,740 --> 00:34:17,520 It's always loaded at the same address in memory. 697 00:34:18,100 --> 00:34:20,360 And so addresses in the binary itself. 698 00:34:20,800 --> 00:34:22,600 Are not subject to randomization. 699 00:34:22,820 --> 00:34:24,120 And we can assume that we know them. 700 00:34:24,360 --> 00:34:25,640 So we can just assume that we know. 701 00:34:25,780 --> 00:34:27,740 For a given binary all of those code addresses. 702 00:34:27,740 --> 00:34:29,540 And that means. 703 00:34:29,640 --> 00:34:31,160 The only thing we have left to find. 704 00:34:31,320 --> 00:34:32,860 Is the FISMEM base address. 705 00:34:33,520 --> 00:34:35,460 To do that we're going to use. 706 00:34:35,540 --> 00:34:38,000 Yet another obscure feature of QMU KVM. 707 00:34:38,140 --> 00:34:39,220 That comes in handy here. 708 00:34:40,080 --> 00:34:41,740 There's this FWCFG. 709 00:34:42,440 --> 00:34:43,560 For firmware configuration. 710 00:34:44,220 --> 00:34:45,800 Subsystem in QMU KVM. 711 00:34:46,000 --> 00:34:47,240 It's sort of a virtual device. 712 00:34:47,420 --> 00:34:48,660 It emulates two IO ports. 713 00:34:49,040 --> 00:34:51,120 You're not supposed to know or care about it. 714 00:34:51,180 --> 00:34:53,460 Because it's used by QMU's bios. 715 00:34:53,560 --> 00:34:55,840 To communicate with the emulated hardware. 716 00:34:56,200 --> 00:34:57,720 But it's just listening on IO. 717 00:34:57,740 --> 00:34:59,260 There's no reason other software. 718 00:34:59,340 --> 00:35:00,920 That's not the bios can't talk to it. 719 00:35:01,460 --> 00:35:03,340 It's purpose is to export. 720 00:35:03,440 --> 00:35:05,340 Data tables that the bios needs. 721 00:35:05,560 --> 00:35:06,940 Such as the E820 map. 722 00:35:07,140 --> 00:35:08,840 That describes the layout of physical memory. 723 00:35:09,440 --> 00:35:11,160 The ACPE tables that describe. 724 00:35:11,260 --> 00:35:13,400 How to interface with the emulated ACPE hardware. 725 00:35:13,520 --> 00:35:13,780 Etc. 726 00:35:14,560 --> 00:35:17,200 It also has support for the bios. 727 00:35:17,280 --> 00:35:18,560 Feeding information back. 728 00:35:18,740 --> 00:35:20,040 To the emulated hardware. 729 00:35:20,480 --> 00:35:22,220 This is a little odd in that. 730 00:35:22,320 --> 00:35:23,900 As of the versions that I checked. 731 00:35:24,200 --> 00:35:25,900 This support for writable tables. 732 00:35:27,740 --> 00:35:28,580 In QMU KVM. 733 00:35:28,660 --> 00:35:30,940 There's actually no place that it exports tables. 734 00:35:31,000 --> 00:35:32,500 That the bios is expected to write. 735 00:35:32,700 --> 00:35:34,060 But the infrastructure is there. 736 00:35:34,320 --> 00:35:35,440 Not only is it there. 737 00:35:35,640 --> 00:35:38,460 It actually lets the bios or any software. 738 00:35:38,580 --> 00:35:40,720 Write to any of these exporting tables. 739 00:35:42,540 --> 00:35:43,560 And conveniently. 740 00:35:43,920 --> 00:35:45,500 Several of these tables are backed. 741 00:35:45,600 --> 00:35:47,380 By statically allocated buffers. 742 00:35:47,380 --> 00:35:48,200 Inside the host. 743 00:35:48,840 --> 00:35:51,360 Which means again assuming no PIE. 744 00:35:51,600 --> 00:35:53,500 They're loaded as part of the elf binary. 745 00:35:53,740 --> 00:35:54,700 At fixed addresses. 746 00:35:55,240 --> 00:35:57,440 Which means that we can write to them. 747 00:35:57,740 --> 00:35:59,200 And get nearly 500 bytes. 748 00:35:59,440 --> 00:36:01,000 Of writable data. 749 00:36:01,340 --> 00:36:04,520 At a fixed static predictable address. 750 00:36:04,660 --> 00:36:06,020 Even under ASLR. 751 00:36:06,560 --> 00:36:07,800 So that's enough. 752 00:36:08,020 --> 00:36:09,880 That we can inject our fake timers. 753 00:36:09,900 --> 00:36:11,660 Our fake structures into that space. 754 00:36:11,860 --> 00:36:13,220 Rather than into. 755 00:36:14,240 --> 00:36:16,140 The backing memory map. 756 00:36:17,060 --> 00:36:18,680 There's one complication. 757 00:36:19,020 --> 00:36:20,560 Which is that mprotect. 758 00:36:20,740 --> 00:36:21,200 As I mentioned. 759 00:36:21,300 --> 00:36:22,880 Needs a page aligned address. 760 00:36:23,300 --> 00:36:25,040 And so a 500 byte region. 761 00:36:25,220 --> 00:36:26,740 Is not likely to land. 762 00:36:28,120 --> 00:36:29,180 If it's allocated. 763 00:36:29,440 --> 00:36:30,620 If it's at a random offset. 764 00:36:30,800 --> 00:36:31,500 Which it basically is. 765 00:36:32,380 --> 00:36:33,880 So what we actually need to do. 766 00:36:34,360 --> 00:36:35,900 Or what I did at least. 767 00:36:36,080 --> 00:36:38,860 Is we construct a different set of fake timer chains. 768 00:36:39,060 --> 00:36:40,900 Using the same techniques. 769 00:36:41,300 --> 00:36:43,340 That does basically a read for. 770 00:36:43,720 --> 00:36:44,900 It reads four bytes. 771 00:36:45,040 --> 00:36:46,140 At an address we choose. 772 00:36:46,320 --> 00:36:47,160 From the guest. 773 00:36:47,480 --> 00:36:49,160 And then it writes it to a space. 774 00:36:49,400 --> 00:36:50,560 Or from the host process. 775 00:36:50,700 --> 00:36:52,900 And it writes it to a space that's visible to the guest. 776 00:36:54,220 --> 00:36:56,160 And so we use that read for. 777 00:36:57,740 --> 00:37:00,240 We build up our own information leak. 778 00:37:00,340 --> 00:37:01,240 And we chase pointers. 779 00:37:01,320 --> 00:37:02,300 Using those reads. 780 00:37:02,640 --> 00:37:05,420 To derive the value of that. 781 00:37:05,460 --> 00:37:06,620 Physical memory base map. 782 00:37:11,160 --> 00:37:13,220 Once we found the address of that mapping. 783 00:37:13,440 --> 00:37:15,200 We proceed exactly as before. 784 00:37:16,680 --> 00:37:17,940 There's a little bit. 785 00:37:17,980 --> 00:37:19,120 Of complexity here. 786 00:37:19,480 --> 00:37:20,740 Is that now this means. 787 00:37:20,880 --> 00:37:22,160 We actually need to do that read for. 788 00:37:22,440 --> 00:37:25,080 Then do computation based on the results of that read. 789 00:37:25,080 --> 00:37:27,420 And then execute more gadgets in the host. 790 00:37:27,900 --> 00:37:30,200 So rather than ending our timer chains. 791 00:37:30,400 --> 00:37:31,900 With next equals null. 792 00:37:32,200 --> 00:37:34,140 We actually end them with another timer. 793 00:37:34,300 --> 00:37:37,620 That calls that RTC update second function again. 794 00:37:38,200 --> 00:37:39,620 Which remember it takes. 795 00:37:39,780 --> 00:37:41,400 It follows pointers to find a timer. 796 00:37:41,580 --> 00:37:42,800 And then schedules that timer. 797 00:37:42,860 --> 00:37:44,380 At one second in the future. 798 00:37:45,060 --> 00:37:46,740 So we execute this chain. 799 00:37:46,940 --> 00:37:47,600 We do the read for. 800 00:37:47,880 --> 00:37:49,400 And then one second from now. 801 00:37:49,620 --> 00:37:50,840 A timer that we control. 802 00:37:51,040 --> 00:37:51,860 Will again get executed. 803 00:37:52,100 --> 00:37:52,760 But until then. 804 00:37:55,080 --> 00:37:55,380 In this context. 805 00:37:55,380 --> 00:37:57,020 The guest can do arithmetic. 806 00:37:57,200 --> 00:37:57,800 Do computation. 807 00:37:58,180 --> 00:37:59,940 Glue bits together based on that read. 808 00:38:00,260 --> 00:38:01,700 And then one second later. 809 00:38:01,880 --> 00:38:04,280 Will jump back into another set of gadgets. 810 00:38:04,520 --> 00:38:05,960 So we use this to chain. 811 00:38:06,160 --> 00:38:08,700 To chain multiple timer chains. 812 00:38:08,900 --> 00:38:09,460 One after another. 813 00:38:09,680 --> 00:38:12,180 With guest execution in between. 814 00:38:14,460 --> 00:38:15,800 And so that then. 815 00:38:17,260 --> 00:38:19,140 Is what it takes to. 816 00:38:19,600 --> 00:38:21,300 Or at least the steps that I took. 817 00:38:21,520 --> 00:38:22,360 And you know the broad steps. 818 00:38:22,460 --> 00:38:23,680 What it takes to make this exploit. 819 00:38:25,080 --> 00:38:25,180 And so. 820 00:38:25,180 --> 00:38:25,580 We're going to work against ASLR. 821 00:38:25,900 --> 00:38:27,240 Bypassing non-executable pages. 822 00:38:28,300 --> 00:38:30,360 And work completely. 823 00:38:30,600 --> 00:38:33,660 On a stock install of KVM. 824 00:38:33,760 --> 00:38:34,320 From a vendor. 825 00:38:36,420 --> 00:38:38,320 And we'll demo that in just a moment. 826 00:38:40,080 --> 00:38:40,520 So. 827 00:38:43,320 --> 00:38:44,500 Before we get to the demo. 828 00:38:44,620 --> 00:38:45,920 So that I can end the talk on the high note. 829 00:38:46,020 --> 00:38:46,420 With the demo. 830 00:38:47,080 --> 00:38:49,080 We'll talk about a couple of conclusions. 831 00:38:49,380 --> 00:38:50,160 That I think are. 832 00:38:50,620 --> 00:38:52,900 Thoughts that are raised by this work. 833 00:38:53,780 --> 00:38:55,060 One thing I want to emphasize. 834 00:38:55,080 --> 00:38:56,740 That I hope this work emphasizes. 835 00:38:56,900 --> 00:38:58,600 Is that virtual machine breakouts. 836 00:38:58,780 --> 00:39:00,080 Aren't magical at all. 837 00:39:00,540 --> 00:39:02,300 I think there's often a tendency. 838 00:39:02,540 --> 00:39:04,960 Virtual machines are kind of magic honestly. 839 00:39:05,260 --> 00:39:07,600 They do this weird thing. 840 00:39:07,720 --> 00:39:09,160 Where you get multiple computers in one computer. 841 00:39:09,380 --> 00:39:10,240 And they do that by. 842 00:39:10,400 --> 00:39:12,840 Doing lots of crazy low level. 843 00:39:12,880 --> 00:39:14,200 Hardware and software tricks. 844 00:39:14,340 --> 00:39:15,100 To make it possible. 845 00:39:15,660 --> 00:39:17,060 And so people tend to assume. 846 00:39:17,240 --> 00:39:19,520 That attacking virtual machines. 847 00:39:19,860 --> 00:39:21,480 Must be at least as complicated. 848 00:39:21,700 --> 00:39:23,380 As writing one in the first place. 849 00:39:25,080 --> 00:39:26,460 Which is probably a pretty strong security layer. 850 00:39:26,780 --> 00:39:28,480 But that's just not true. 851 00:39:29,960 --> 00:39:32,040 All of the usual software protections. 852 00:39:32,340 --> 00:39:34,080 ASLR, NX, etc. 853 00:39:34,300 --> 00:39:35,260 Apply to virtual machines. 854 00:39:35,560 --> 00:39:36,840 But that's about it. 855 00:39:37,080 --> 00:39:38,140 Virtual machine breakouts. 856 00:39:38,160 --> 00:39:39,800 Typically are memory corruption bugs. 857 00:39:40,000 --> 00:39:40,940 Like any other bugs. 858 00:39:41,120 --> 00:39:42,440 And frankly today. 859 00:39:42,560 --> 00:39:44,960 I would prefer to be attacking a virtual machine. 860 00:39:45,260 --> 00:39:46,940 Than a modern web browser. 861 00:39:47,300 --> 00:39:50,100 Because the state of mitigating exploits. 862 00:39:50,160 --> 00:39:51,940 And defending against exploits in sandboxing. 863 00:39:51,960 --> 00:39:53,740 Is much more advanced there. 864 00:39:55,080 --> 00:39:56,040 So virtual machines. 865 00:39:56,840 --> 00:39:59,260 Don't think of them as magic security boxes. 866 00:40:00,660 --> 00:40:02,280 They're just as vulnerable as anything else. 867 00:40:02,460 --> 00:40:03,160 And of course. 868 00:40:03,240 --> 00:40:04,220 As you might expect. 869 00:40:04,480 --> 00:40:06,560 The fake devices are the weak spot. 870 00:40:07,160 --> 00:40:09,300 And just sort of to drive this point home. 871 00:40:09,440 --> 00:40:10,180 And to give some context. 872 00:40:10,780 --> 00:40:12,740 We'll take a brief look at some past. 873 00:40:13,060 --> 00:40:14,460 Virtual machine breakouts. 874 00:40:15,720 --> 00:40:16,680 In 2008. 875 00:40:17,120 --> 00:40:18,280 Invisible things lab. 876 00:40:18,380 --> 00:40:19,140 Put out this paper. 877 00:40:19,340 --> 00:40:20,960 Adventures with a certain zen vulnerability. 878 00:40:21,440 --> 00:40:23,180 Where they demonstrated a zen breakout. 879 00:40:25,080 --> 00:40:25,640 And what they were exploiting. 880 00:40:25,760 --> 00:40:26,700 Was an integer overflow. 881 00:40:26,980 --> 00:40:29,480 In the para virtual frame buffer. 882 00:40:29,880 --> 00:40:31,940 And they did much the same standard tricks. 883 00:40:32,040 --> 00:40:32,360 That I did. 884 00:40:32,480 --> 00:40:33,440 Ret to lib C. 885 00:40:33,640 --> 00:40:34,580 Or return oriented programming. 886 00:40:34,760 --> 00:40:36,980 To copy a buffer in. 887 00:40:37,080 --> 00:40:37,840 And then jump into it. 888 00:40:39,320 --> 00:40:41,460 Immunity at black hat 2009. 889 00:40:41,800 --> 00:40:42,880 Presented cloud burst. 890 00:40:43,120 --> 00:40:44,480 A breakout exploit for VM ware. 891 00:40:44,700 --> 00:40:46,220 Again missing bounds checking. 892 00:40:46,420 --> 00:40:48,020 In the virtual SVGA device. 893 00:40:49,100 --> 00:40:50,220 Get memory corruption. 894 00:40:55,080 --> 00:40:56,780 To give a sense of where. 895 00:40:56,840 --> 00:40:58,280 Virtual machines can get interesting. 896 00:40:58,720 --> 00:41:00,620 And just earlier this year. 897 00:41:00,920 --> 00:41:01,960 Invisible things lab. 898 00:41:02,080 --> 00:41:03,620 Again put out another attack against zen. 899 00:41:03,920 --> 00:41:06,260 But this one based on a bug. 900 00:41:06,460 --> 00:41:07,920 In the way that zen uses. 901 00:41:08,640 --> 00:41:09,660 Intel's VTD. 902 00:41:10,060 --> 00:41:11,920 Which is the IO MMU. 903 00:41:12,040 --> 00:41:14,300 And hardware IO protection technology. 904 00:41:15,040 --> 00:41:16,160 It turns out that if you. 905 00:41:16,800 --> 00:41:18,240 Turns out that there was a subtle bug. 906 00:41:18,440 --> 00:41:19,860 In the way that interrupts were handled. 907 00:41:20,020 --> 00:41:22,180 That actually allowed a guest to escape. 908 00:41:22,580 --> 00:41:23,500 And so that's an example. 909 00:41:25,080 --> 00:41:26,140 That might be possible. 910 00:41:26,280 --> 00:41:29,340 With the KVM AMD and KVM Intel modules. 911 00:41:29,740 --> 00:41:31,280 Is that these things are complicated. 912 00:41:31,440 --> 00:41:32,680 And there probably are subtle attacks. 913 00:41:32,800 --> 00:41:33,920 That involve the hardware interaction. 914 00:41:34,460 --> 00:41:37,420 But once again the actual primitive. 915 00:41:37,520 --> 00:41:38,800 That they got out of that attack. 916 00:41:39,000 --> 00:41:40,760 Was memory corruption in the host. 917 00:41:41,300 --> 00:41:42,840 And so from there they proceeded. 918 00:41:42,900 --> 00:41:44,680 In exactly the same set of techniques. 919 00:41:44,780 --> 00:41:45,920 That everyone else uses. 920 00:41:45,960 --> 00:41:47,320 For exploiting memory corruption bugs. 921 00:41:48,460 --> 00:41:50,920 And so virtual machines are interesting. 922 00:41:51,260 --> 00:41:53,380 But they're not that much more interesting. 923 00:41:53,560 --> 00:41:54,520 They're not that much more secure. 924 00:41:55,080 --> 00:41:55,300 Than anything else. 925 00:41:57,220 --> 00:41:58,300 So that's some conclusions. 926 00:41:58,560 --> 00:42:01,540 Some further work. 927 00:42:01,700 --> 00:42:03,000 That I want to see done. 928 00:42:03,140 --> 00:42:04,020 And that I'm going to be pursuing. 929 00:42:04,240 --> 00:42:06,040 Is in hardening KVM. 930 00:42:06,700 --> 00:42:08,020 The first take away. 931 00:42:08,260 --> 00:42:09,900 Is that you absolutely should be. 932 00:42:10,020 --> 00:42:11,440 Sandboxing QMU KVM. 933 00:42:11,620 --> 00:42:13,060 If you're running it in a production environment. 934 00:42:13,840 --> 00:42:15,260 Fortunately if you're running it. 935 00:42:15,320 --> 00:42:18,360 At least under Ubuntu or Red Hat. 936 00:42:19,060 --> 00:42:20,020 Via libvert. 937 00:42:20,220 --> 00:42:22,000 They are already sandboxing it. 938 00:42:22,040 --> 00:42:23,720 Using SELinux and AppArmor. 939 00:42:25,080 --> 00:42:25,840 I haven't yet. 940 00:42:25,980 --> 00:42:27,720 But I want to take a look at those sandboxes. 941 00:42:27,780 --> 00:42:28,660 And see how hard they are. 942 00:42:28,780 --> 00:42:29,240 But at least. 943 00:42:29,580 --> 00:42:32,220 This is work that people do realize needs to be done. 944 00:42:32,480 --> 00:42:34,660 But if any of you are deploying KVM. 945 00:42:34,960 --> 00:42:35,620 Make sure. 946 00:42:36,200 --> 00:42:39,380 KVM in a remotely possibly sensitive context. 947 00:42:39,580 --> 00:42:41,020 Make sure that you're using a technology. 948 00:42:41,160 --> 00:42:42,340 That gives you that sandboxing. 949 00:42:42,920 --> 00:42:44,400 I think another no brainer. 950 00:42:44,640 --> 00:42:46,040 Is I'd like to see the distributions. 951 00:42:46,780 --> 00:42:49,400 Start building QMU KVM as PIE. 952 00:42:49,520 --> 00:42:50,480 As full position independent. 953 00:42:50,480 --> 00:42:53,100 The traditional reason not to do this. 954 00:42:53,180 --> 00:42:53,680 Is performance. 955 00:42:53,960 --> 00:42:55,600 Because it costs an extra register. 956 00:42:55,820 --> 00:42:56,960 To build as PIE. 957 00:42:57,200 --> 00:43:00,660 But virtually no one is running KVM on 32 bit platforms anymore. 958 00:43:00,960 --> 00:43:02,660 And AMB 64 has enough registers. 959 00:43:03,140 --> 00:43:04,740 That the performance impact. 960 00:43:05,100 --> 00:43:06,460 Is really negligible. 961 00:43:06,680 --> 00:43:08,900 I haven't done benchmarks on KVM. 962 00:43:09,040 --> 00:43:10,240 It's also on my to do list. 963 00:43:10,620 --> 00:43:12,860 But I think that's an obvious thing. 964 00:43:13,000 --> 00:43:14,440 That it will improve the state of the world. 965 00:43:15,120 --> 00:43:16,760 Then we can try some crazier ideas. 966 00:43:17,060 --> 00:43:17,660 We can try. 967 00:43:18,000 --> 00:43:19,680 To make things a little bit harder. 968 00:43:20,480 --> 00:43:21,100 The standard technique. 969 00:43:21,420 --> 00:43:23,200 XOR encoding key function pointers. 970 00:43:23,560 --> 00:43:25,080 XORing them with some constant value. 971 00:43:25,320 --> 00:43:26,280 That's stored at some address. 972 00:43:26,640 --> 00:43:28,860 And then on XORing them when you get them out. 973 00:43:29,020 --> 00:43:32,160 So that if a memory corruption bug tries to clobber them. 974 00:43:32,340 --> 00:43:34,000 They'll just basically end up with a random address. 975 00:43:34,820 --> 00:43:36,520 I think we'll need to do a little more thought. 976 00:43:36,600 --> 00:43:39,260 To figure out which things are likely to be targets. 977 00:43:39,440 --> 00:43:41,520 And might be worth applying this technique to. 978 00:43:41,720 --> 00:43:42,880 But it's a standard technique. 979 00:43:42,960 --> 00:43:44,260 That's used to harden applications. 980 00:43:44,300 --> 00:43:45,560 Against memory corruption bugs. 981 00:43:45,940 --> 00:43:48,380 And I think it's time that we start asking the question. 982 00:43:48,520 --> 00:43:49,560 Of whether it's appropriate here. 983 00:43:51,500 --> 00:43:53,720 And we can try doing even crazier things. 984 00:43:53,920 --> 00:43:55,740 Like protecting that guest memory region. 985 00:43:55,840 --> 00:43:58,740 By lazily mapping or protecting regions of it. 986 00:43:58,820 --> 00:44:00,720 Or sandboxing individual devices. 987 00:44:00,980 --> 00:44:03,400 So that they run in a thread. 988 00:44:03,500 --> 00:44:04,880 That somehow only has a view. 989 00:44:05,020 --> 00:44:06,880 Into that specific piece of memory. 990 00:44:07,000 --> 00:44:08,300 And this is a topic that. 991 00:44:08,700 --> 00:44:10,660 I've seen some discussion on the KVM lists. 992 00:44:10,880 --> 00:44:11,540 But that I think. 993 00:44:11,680 --> 00:44:12,940 Hopefully this work demonstrates. 994 00:44:13,040 --> 00:44:14,240 Does have potential value. 995 00:44:14,720 --> 00:44:15,560 And of course. 996 00:44:15,580 --> 00:44:18,540 I want to see more people looking at QMU KVM. 997 00:44:18,600 --> 00:44:19,820 And auditing it and fuzzing it. 998 00:44:20,480 --> 00:44:20,780 And fixing bugs. 999 00:44:23,340 --> 00:44:24,820 Some future research directions. 1000 00:44:24,940 --> 00:44:26,300 From the offensive side. 1001 00:44:28,560 --> 00:44:30,360 I want to go after KVM.KO. 1002 00:44:30,500 --> 00:44:32,060 I'd like to see someone else look into it. 1003 00:44:32,140 --> 00:44:33,860 Because I think that there probably are bugs there. 1004 00:44:34,040 --> 00:44:35,460 And we should understand those bugs. 1005 00:44:35,560 --> 00:44:36,220 And we should fix them. 1006 00:44:38,040 --> 00:44:39,240 Another interesting question. 1007 00:44:39,420 --> 00:44:40,560 That I don't know the answer to. 1008 00:44:40,640 --> 00:44:41,700 And I want to know the answer to. 1009 00:44:41,800 --> 00:44:42,620 Is how hard. 1010 00:44:43,080 --> 00:44:45,220 Given that I know that I'm running in a guest. 1011 00:44:45,380 --> 00:44:46,780 On QMU KVM. 1012 00:44:47,140 --> 00:44:49,820 Can I fingerprint the exact QMU KVM version. 1013 00:44:50,480 --> 00:44:51,380 Comparing the behavior. 1014 00:44:51,700 --> 00:44:53,600 Or possibly even more subtle. 1015 00:44:53,680 --> 00:44:54,360 Timing attacks. 1016 00:44:54,640 --> 00:44:57,400 Against the memory layout or something. 1017 00:44:57,800 --> 00:44:59,520 Because I've been assuming that I know. 1018 00:44:59,680 --> 00:45:02,120 Which QMU KVM binary I'm running against. 1019 00:45:02,660 --> 00:45:03,580 In the wild. 1020 00:45:03,660 --> 00:45:05,440 You can usually assume that you're running against. 1021 00:45:05,580 --> 00:45:06,660 A distros binary. 1022 00:45:06,940 --> 00:45:08,320 But that's still a range of binaries. 1023 00:45:08,560 --> 00:45:10,040 So how well can we fingerprint. 1024 00:45:11,060 --> 00:45:12,660 Which will give us an understanding of. 1025 00:45:12,800 --> 00:45:15,240 How weaponizable these attacks really. 1026 00:45:15,360 --> 00:45:17,480 How general such exploits can be made. 1027 00:45:18,300 --> 00:45:19,740 And then another question is. 1028 00:45:20,480 --> 00:45:22,520 What do information leaks. 1029 00:45:23,140 --> 00:45:24,940 In QMU KVM look like? 1030 00:45:26,700 --> 00:45:28,040 Assuming that we get things built. 1031 00:45:28,100 --> 00:45:29,260 As fully randomized. 1032 00:45:29,840 --> 00:45:32,140 We're going to start wanting to find information leaks. 1033 00:45:32,380 --> 00:45:34,080 To extract addresses. 1034 00:45:34,320 --> 00:45:36,100 In order to write these kinds of exploits. 1035 00:45:36,760 --> 00:45:38,580 Is there sort of a whole class. 1036 00:45:38,620 --> 00:45:40,800 Of information leaks that we haven't realized yet. 1037 00:45:40,880 --> 00:45:41,900 Or whole classes where. 1038 00:45:42,200 --> 00:45:44,660 The standard idioms that QMU uses. 1039 00:45:44,740 --> 00:45:45,680 For communicating with devices. 1040 00:45:46,000 --> 00:45:47,780 Have a tendency to leak addresses. 1041 00:45:48,220 --> 00:45:49,600 In a way that we hadn't realized. 1042 00:45:50,480 --> 00:45:52,260 That's analogous to Dan Rosenberg's work. 1043 00:45:52,440 --> 00:45:52,960 On the Linux kernel. 1044 00:45:53,120 --> 00:45:54,580 Where he demonstrated that. 1045 00:45:54,640 --> 00:45:56,720 As small as a four byte leak. 1046 00:45:56,780 --> 00:45:57,700 Of uninitialized memory. 1047 00:45:58,220 --> 00:45:59,800 Can reliably be used. 1048 00:45:59,900 --> 00:46:00,940 To deduce kernel addresses. 1049 00:46:01,120 --> 00:46:02,220 With a bunch of cleverness. 1050 00:46:04,640 --> 00:46:06,940 So that's sort of my immediate checklist. 1051 00:46:07,040 --> 00:46:08,200 Of future work on KVM. 1052 00:46:08,560 --> 00:46:10,440 And I also hope to. 1053 00:46:11,260 --> 00:46:12,340 Improve my fuzzer. 1054 00:46:12,400 --> 00:46:13,680 And point it at other virtual machines. 1055 00:46:13,960 --> 00:46:15,320 Because I haven't had a chance to do that. 1056 00:46:15,320 --> 00:46:16,620 But for KVM. 1057 00:46:16,700 --> 00:46:17,880 This is my checklist. 1058 00:46:18,080 --> 00:46:20,880 And if you're interested in any of these. 1059 00:46:21,000 --> 00:46:22,260 I encourage you to talk to me. 1060 00:46:22,480 --> 00:46:23,720 And share thoughts. 1061 00:46:23,920 --> 00:46:24,460 Whatever you have. 1062 00:46:25,040 --> 00:46:25,640 All right. 1063 00:46:25,680 --> 00:46:26,320 So finally. 1064 00:46:26,480 --> 00:46:27,900 After all of that talking at you. 1065 00:46:28,020 --> 00:46:30,360 It's time to demo my exploit. 1066 00:46:30,500 --> 00:46:30,960 In progress. 1067 00:46:31,220 --> 00:46:31,660 In action. 1068 00:46:33,200 --> 00:46:33,640 So. 1069 00:46:35,860 --> 00:46:36,300 Oh. 1070 00:46:36,300 --> 00:46:36,740 Well. 1071 00:46:37,860 --> 00:46:39,340 That text is not relevant. 1072 00:46:39,480 --> 00:46:42,140 I just ran a shell script that launched a KVM VM. 1073 00:46:42,500 --> 00:46:44,080 And I hope you can read this. 1074 00:46:44,800 --> 00:46:45,960 It's not super relevant. 1075 00:46:46,140 --> 00:46:48,060 But so I'm booting KVM. 1076 00:46:48,120 --> 00:46:50,000 Just into an Ubuntu kernel. 1077 00:46:50,140 --> 00:46:52,740 Just to show that this is a working KVM virtual machine. 1078 00:46:57,060 --> 00:46:57,720 You know. 1079 00:46:59,220 --> 00:47:00,080 We're in a VM. 1080 00:47:00,400 --> 00:47:01,940 Proc CPU info shows. 1081 00:47:02,040 --> 00:47:03,820 That we're running a QMU CPU. 1082 00:47:04,940 --> 00:47:05,980 And the way. 1083 00:47:08,240 --> 00:47:10,960 The way that I've implemented this exploit. 1084 00:47:11,180 --> 00:47:11,680 Is that. 1085 00:47:11,780 --> 00:47:14,580 Because we are hot plugging the ISA bus. 1086 00:47:14,940 --> 00:47:17,880 There are a lot of virtual devices that go away. 1087 00:47:18,280 --> 00:47:20,160 And if anyone tries to use those. 1088 00:47:20,240 --> 00:47:22,300 There's a high risk of the machine seg faulting. 1089 00:47:22,320 --> 00:47:23,140 Which is not useful. 1090 00:47:23,320 --> 00:47:24,280 If we want to exploit it. 1091 00:47:24,540 --> 00:47:27,100 So I've actually packaged my exploit. 1092 00:47:27,540 --> 00:47:29,580 Into an init RD. 1093 00:47:30,660 --> 00:47:31,860 That just contains. 1094 00:47:32,080 --> 00:47:33,760 A statically compiled version of my exploit. 1095 00:47:34,020 --> 00:47:35,040 And a minimal kernel. 1096 00:47:35,220 --> 00:47:36,280 That doesn't use any of my exploit. 1097 00:47:36,300 --> 00:47:38,760 And I've put it in grub. 1098 00:47:38,800 --> 00:47:40,400 So we can boot straight into the exploit. 1099 00:47:40,640 --> 00:47:42,700 And greatly increase the reliability. 1100 00:47:43,060 --> 00:47:43,520 Of this attack. 1101 00:47:44,340 --> 00:47:46,040 So we'll reboot the VM now. 1102 00:47:48,900 --> 00:47:50,420 And from the grub prompt. 1103 00:47:50,840 --> 00:47:52,760 We'll select my exploit. 1104 00:47:54,700 --> 00:47:55,660 Doing things. 1105 00:47:55,780 --> 00:47:56,440 Chasing pointers. 1106 00:47:57,440 --> 00:47:59,620 And we pop up a calculator in the host. 1107 00:48:09,980 --> 00:48:12,260 And obviously as I'm sure most of you are aware. 1108 00:48:12,420 --> 00:48:13,880 The calculator is the standard. 1109 00:48:14,080 --> 00:48:15,140 Yes I've exploited it demo. 1110 00:48:15,300 --> 00:48:16,560 But that could be arbitrary code. 1111 00:48:18,200 --> 00:48:19,000 And all right. 1112 00:48:19,620 --> 00:48:21,040 As you can see here. 1113 00:48:21,260 --> 00:48:23,360 The guest kernel is very upset. 1114 00:48:23,520 --> 00:48:25,180 That it's real time clock has gone away. 1115 00:48:25,520 --> 00:48:28,600 But it's valiantly stumbling forward. 1116 00:48:31,440 --> 00:48:32,280 All right. 1117 00:48:32,320 --> 00:48:35,600 So that is the end of this talk. 1118 00:48:36,700 --> 00:48:37,860 The goons have told me. 1119 00:48:37,880 --> 00:48:39,920 That I shouldn't bother heading to the QA room. 1120 00:48:40,000 --> 00:48:40,980 Because there's no one after me. 1121 00:48:41,060 --> 00:48:41,860 So if you have questions. 1122 00:48:41,960 --> 00:48:43,200 Just head up here and we can chat.