1 00:00:00,000 --> 00:00:03,900 So I'm here today to talk to you about machine check exceptions. 2 00:00:04,220 --> 00:00:09,980 Basically what happens on your computer when something goes terribly, catastrophically wrong with the hardware. 3 00:00:10,340 --> 00:00:11,820 So why do we want to look at that? 4 00:00:11,820 --> 00:00:16,020 Well, it turns out that it's got some interesting applications for security that nobody's ever looked at before. 5 00:00:16,020 --> 00:00:22,460 But in order to explore it a little bit more, we really, really want to be able to see a machine check actually happen. 6 00:00:22,660 --> 00:00:24,880 So my setup here is on the left. 7 00:00:24,880 --> 00:00:27,020 I've got an x86 platform. 8 00:00:27,020 --> 00:00:31,700 And I'm going to be talking about x86 architecture today, but a lot of this will apply to other architectures as well. 9 00:00:31,800 --> 00:00:35,980 And on the right, we've got a little program running on this machine. 10 00:00:36,300 --> 00:00:42,040 It's just shuffling some letters around just to show us that something's actually running. 11 00:00:42,040 --> 00:00:46,240 And I think we're all probably familiar with what happens on a computer when something goes kind of wrong. 12 00:00:46,240 --> 00:00:50,600 We've probably seen, like, program crashes and seg faults and maybe a blue screen before. 13 00:00:50,600 --> 00:00:57,380 But I want to look at what happens when something goes, like, catastrophically wrong with the hardware in a way that can't be recovered. 14 00:00:57,480 --> 00:01:03,940 The problem is computers today are actually pretty resilient and things just don't go catastrophically wrong very often. 15 00:01:03,940 --> 00:01:08,820 And we could watch this thing for, like, the next 10 years and probably not see any serious problems with the system. 16 00:01:08,820 --> 00:01:16,660 So if we want to research this a little bit more, we've got to give the computer, like, a little nudge to try to make something go catastrophically wrong with the hardware. 17 00:01:16,740 --> 00:01:21,300 So I thought, you know, maybe what if we rip off the heatsink from the CPU while the thing's running? 18 00:01:21,300 --> 00:01:24,420 Maybe that'll make something go wrong with the system. 19 00:01:24,600 --> 00:01:29,100 But if we try that, nothing really happened. 20 00:01:29,100 --> 00:01:31,340 We didn't get so much as an error message. 21 00:01:31,580 --> 00:01:37,120 Like I said, computers today are actually pretty resilient and things just don't go terribly wrong all that often. 22 00:01:37,120 --> 00:01:46,480 So what probably happened here is the CPU started to overheat and then it got a proc hot signal and it decided to underclock itself to stay within the desired thermal range for that system . 23 00:01:46,480 --> 00:01:51,160 So if we really want something to go catastrophically wrong, we're going to have to give it a bigger nudge. 24 00:01:51,160 --> 00:01:55,420 We're going to have to make it so that the CPU can't just run away from its problems. 25 00:01:55,580 --> 00:02:00,280 We'll see if we can overheat this thing enough that underclocking is not going to help it. 26 00:02:00,280 --> 00:02:03,540 So we got to be a little careful here not to melt the rest of the platform. 27 00:02:03,540 --> 00:02:08,840 But if we're precise, we can hopefully make something go wrong with the hardware. 28 00:02:08,840 --> 00:02:09,800 And there it goes. 29 00:02:09,800 --> 00:02:11,400 The whole thing shut down. 30 00:02:11,500 --> 00:02:19,960 So what happened there is probably the CPU got a therm trip signal and it decided that it needed to just power off the whole platform to avoid permanent damage. 31 00:02:20,680 --> 00:02:24,820 And if the CPU cools down a little bit, this thing will boot back up, no worse for wear. 32 00:02:24,880 --> 00:02:26,440 But what's the point of all this? 33 00:02:26,440 --> 00:02:34,840 Well, it turns out that in the microseconds before everything comes crashing down for the CPU, some really interesting things start to happen. 34 00:02:34,840 --> 00:02:40,480 But in order to explore that a little bit more, we need to talk about interrupts and exceptions and how they work on a processor. 35 00:02:40,480 --> 00:02:44,840 So the idea here is you've got some code running on the CPU. 36 00:02:45,340 --> 00:02:48,700 You've got, you know, maybe you're setting X to 1 and Y to 2. 37 00:02:48,740 --> 00:02:52,860 But then you've also got other devices connected to the CPU, like a PCIe device. 38 00:02:52,860 --> 00:02:55,560 And that PCIe device might want the CPU's attention. 39 00:02:55,560 --> 00:02:59,500 So it sends like a signal over to the CPU requesting its attention. 40 00:02:59,500 --> 00:03:05,080 The CPU, whatever your program was doing, maybe it was setting X to 1, it's going to stop your program and interrupt it. 41 00:03:05,080 --> 00:03:10,560 It's going to give control over to an interrupt handler instead and then that interrupt handler can service the interrupt. 42 00:03:10,560 --> 00:03:18,480 In this case, maybe the interrupt handler writes out a value like 100 over to the hardware and then gets the hardware's response and then it's all done and so it returns. 43 00:03:18,480 --> 00:03:25,180 The operating system gives control back to your program and your program continues executing totally unaware that it was ever interrupted in the first place. 44 00:03:25,320 --> 00:03:27,180 At least that's how things normally go. 45 00:03:27,180 --> 00:03:28,560 But it's not always that easy. 46 00:03:28,560 --> 00:03:33,080 So suppose you were writing like a Linux kernel module and you wanted to be able to talk to hardware. 47 00:03:33,080 --> 00:03:34,280 Maybe your code looks like this. 48 00:03:34,280 --> 00:03:39,340 Maybe you write out the value 200 to some piece of hardware and then you want to get the hardware's response. 49 00:03:39,480 --> 00:03:44,920 But meanwhile, right after you write out that value 200, some other device on the platform interrupts the CPU. 50 00:03:44,920 --> 00:03:48,140 The CPU interrupts your program and gives control to an interrupt handler. 51 00:03:48,140 --> 00:03:54,840 That interrupt handler writes the value 100 to the same piece of hardware you were trying to talk to and gets the hardware's response. 52 00:03:54,840 --> 00:03:56,000 So interrupt handler's happy. 53 00:03:56,000 --> 00:04:02,340 But when it gives control back to your program, you're trying to get the hardware's response to the value 200, but somebody just clobbered everything you were doing. 54 00:04:02,340 --> 00:04:06,340 You get the response to the value 100 and your whole program comes falling apart. 55 00:04:06,380 --> 00:04:11,220 So it turns out that some things on a CPU just really shouldn't be interrupted. 56 00:04:11,220 --> 00:04:17,700 Things like privileged transitions or secure environments or all sorts of other stuff happening in the kernel, you might not want to have interrupted. 57 00:04:17,720 --> 00:04:20,560 So the solution to this is interrupt suppression. 58 00:04:20,560 --> 00:04:25,540 Basically, keep interrupts pending until you finish the work you need to do and then you can service the interrupts. 59 00:04:25,540 --> 00:04:28,720 So that would look something like this if you were writing a Linux kernel module. 60 00:04:28,720 --> 00:04:37,660 You'd put on this IRQ save and IRQ restore before and after your code and that'll keep most of the interrupts on the platform suppressed until you finish your sensitive work. 61 00:04:37,740 --> 00:04:50,240 Now, it can be a little bit hard to know exactly which code can be safely interrupted and what can't be safely interrupted, but as long as you do everything perfectly all the time and for every fringe case, there's really no issues here. 62 00:04:50,300 --> 00:04:52,640 It turns out that that means difficulties arise. 63 00:04:52,740 --> 00:05:05,400 So as an example, back in 2012, Rafael showed how in the user lands a kernel transition, if you got an interrupt or exception at exactly the right point, you could get kernel level privilege escalation. 64 00:05:05,400 --> 00:05:08,060 Peterson showed something similar in 2018. 65 00:05:08,060 --> 00:05:18,520 He showed how you could use a pop SS instruction to delay interrupts and exception by just one instruction in order to get them to come into the kernel at exactly the wrong time, leading to kernel privilege escalation. 66 00:05:18,520 --> 00:05:21,600 Google Project Zero showed something similar in TDX. 67 00:05:21,600 --> 00:05:28,240 When TDX was tearing down a secure environment, they could get an interrupt or exception to occur in order to get privilege escalation there. 68 00:05:28,240 --> 00:05:33,080 Schluter showed using unexpected interrupts to leak information from confidential VMs. 69 00:05:33,080 --> 00:05:38,620 So what we've seen over and over and over is interrupts act as kind of a state disruptor. 70 00:05:38,620 --> 00:05:42,600 They mess up the normal flow of execution in a program. 71 00:05:42,600 --> 00:05:52,860 So the solution to all of those problems that we've had in the past has always been very, very heavy interrupt suppression, like make extra sure that no unexpected interrupts can possibly happen. 72 00:05:52,860 --> 00:05:54,840 There's ways to use software to help you with that. 73 00:05:54,920 --> 00:05:56,420 Microcode can help suppress interrupts. 74 00:05:56,420 --> 00:05:58,400 Hardware can help you suppress interrupts. 75 00:05:58,480 --> 00:06:16,320 So when you see some of these secure environments set up, you'll see things like clear the interrupt flag, clear the trap flag, clear DR7, latch non-maskable interrupts, latch SMIs, mask inits, clear debug control, etc., etc., etc., just going through all these hoops trying to make sure that no interrupts or exceptions can possibly occur when you don't want them to. 76 00:06:16,440 --> 00:06:19,280 But then that raises the question, like, can we break through this? 77 00:06:19,280 --> 00:06:20,760 And what would happen if we did? 78 00:06:20,760 --> 00:06:29,220 Well, it turns out that there's one interrupt that generally cannot be delayed or suppressed or latched or anything else, and that's called the machine check exception. 79 00:06:29,400 --> 00:06:41,000 So machine checks are supposed to happen when there's unpredictable hardware failures, things like memory corruptions or cache errors, and they're caused by aging devices or signal integrity, heat issues, like if somebody holds a blowtorch to your CPU. 80 00:06:41,000 --> 00:06:44,860 Even high-energy particles from the sun can cause machine check exceptions. 81 00:06:44,860 --> 00:06:48,980 What happens is the CPU detects the problem, and it generates this MC exception. 82 00:06:48,980 --> 00:06:54,000 MC is going to give control to the 18th interrupt handler in the interrupt descriptor table, which is installed by the OS. 83 00:06:54,000 --> 00:06:55,300 And that looks something like this. 84 00:06:55,300 --> 00:06:56,880 You've got your CPU running. 85 00:06:56,960 --> 00:07:00,240 Something bad happens on that CPU, and the CPU detects a problem. 86 00:07:00,240 --> 00:07:01,520 Maybe it's a bit-flipping SRA. 87 00:07:01,520 --> 00:07:03,320 Maybe it's a thermal limit violated. 88 00:07:03,320 --> 00:07:08,460 The CPU can assert a pin called catastrophic error to let the rest of the platform know things are going really wrong. 89 00:07:08,460 --> 00:07:10,700 Then the CPU will generate a machine check exception. 90 00:07:10,700 --> 00:07:17,160 It goes to this table in memory called the interrupt descriptor table, which is just a list of pointers that the operating system installed. 91 00:07:17,160 --> 00:07:23,680 And one of those pointers, the 18th one, points to a handler that the operating system wants to run to handle machine check exceptions. 92 00:07:23,700 --> 00:07:25,980 So we had a hardware failure happen. 93 00:07:25,980 --> 00:07:28,080 The machine check was generated by the CPU. 94 00:07:28,080 --> 00:07:29,640 Now the operating system has control. 95 00:07:29,640 --> 00:07:31,360 So what does the operating system do? 96 00:07:31,360 --> 00:07:38,360 Well, if we want to find out, we can dive into Linux kernel source and documentation, and we'll find things like this. 97 00:07:38,360 --> 00:07:45,180 It'll tell you that machine checks can be delivered at any time, even in a critical section when all normal interrupts are suppressed. 98 00:07:45,180 --> 00:07:47,580 They must be handled immediately. 99 00:07:47,580 --> 00:07:49,100 The system is already unstable. 100 00:07:49,100 --> 00:07:50,880 They will cause a machine panic. 101 00:07:50,880 --> 00:07:53,200 The kernel will always panic on a machine check. 102 00:07:53,200 --> 00:07:54,660 It's a bad idea to continue. 103 00:07:54,660 --> 00:07:55,580 There's no way out. 104 00:07:55,580 --> 00:07:57,560 There's no idea what we were executing. 105 00:07:57,580 --> 00:07:58,880 There's no chance to recover. 106 00:07:58,880 --> 00:07:59,860 You must die. 107 00:07:59,860 --> 00:08:01,280 No need to fumble too much. 108 00:08:01,280 --> 00:08:02,200 Just die. 109 00:08:02,200 --> 00:08:03,760 So I think we get the gist here. 110 00:08:03,880 --> 00:08:07,100 The kernel will determine the source of the error if it can. 111 00:08:07,160 --> 00:08:09,020 It'll print a message if it can. 112 00:08:09,020 --> 00:08:14,000 But the most important thing from the kernel's perspective is shut down the system before things get worse. 113 00:08:14,080 --> 00:08:16,280 So why shut down and not fix the problem? 114 00:08:16,300 --> 00:08:20,460 Well, usually with machine checks, you know that something went wrong, but you don't know quite what. 115 00:08:20,520 --> 00:08:24,640 You might know that a bit got flipped, but you don't know which bit, so you don't know how to fix it. 116 00:08:24,640 --> 00:08:30,000 And the safest thing to do, then, is to just reset, because if you ignore the problem, it's just going to snowball into bigger and bigger problems. 117 00:08:30,000 --> 00:08:31,560 And there's some caveats to this. 118 00:08:31,640 --> 00:08:35,100 On more recent platforms, there's correctable machine checks, and it gets really complex. 119 00:08:35,100 --> 00:08:36,940 But traditionally, this is the strategy. 120 00:08:36,940 --> 00:08:39,160 Shut down as fast as possible. 121 00:08:39,200 --> 00:08:44,580 So all of this makes machine checks entirely unique amongst all the interrupts and exceptions on a platform. 122 00:08:44,580 --> 00:08:53,280 They demand immediacy, and they represent this unexpected critical hardware failure that can't be masked or delayed or deprioritized or preempted in any way. 123 00:08:53,280 --> 00:08:59,180 So the only way to avoid handling machine checks is to disable them in what's called the CR4 register. 124 00:08:59,400 --> 00:09:07,620 And what happens if you do that is if a machine check does come in while they're disabled, while handling them is disabled, then the CPU will just reset. 125 00:09:07,660 --> 00:09:15,680 So if you're writing some kernel code, your options are basically either handle machine checks immediately or be reset when one is received. 126 00:09:16,100 --> 00:09:21,580 So what all this means is that our normal solution to interrupt suppression doesn't work anymore. 127 00:09:21,580 --> 00:09:32,480 All those heavy-handed techniques for protecting code don't apply to machine checks, because machine checks hit the CPU entirely unexpectedly, and they can break through all normal interrupt defenses. 128 00:09:32,520 --> 00:09:35,180 So all of this gives us a neat opportunity. 129 00:09:35,180 --> 00:09:39,780 Maybe we can use this to build some sort of hammer to break through these interrupt defenses. 130 00:09:39,860 --> 00:09:41,220 But we've got a challenge. 131 00:09:41,220 --> 00:09:43,660 Machine checks are just exceedingly rare. 132 00:09:43,660 --> 00:09:47,660 They're supposed to be this sporadic and unpredictable hardware failures. 133 00:09:47,660 --> 00:09:55,120 And to see exactly how rare they are, you can actually go into proc interrupts on a Linux system, and you can watch interrupts and exceptions coming into the platform on real time. 134 00:09:55,120 --> 00:09:59,740 And you'll see hundreds of interrupts and exceptions occurring every single second on the platform. 135 00:09:59,780 --> 00:10:02,800 But that highlighted line, machine checks, that'll stay zero. 136 00:10:02,800 --> 00:10:08,260 You could watch this for 10 years, and you won't see on a normal system a machine check actually received, because they're so rare. 137 00:10:08,420 --> 00:10:10,000 Well, that's no good for us. 138 00:10:10,000 --> 00:10:11,380 That's no good for exploitation. 139 00:10:11,500 --> 00:10:13,780 We can't just wait 10 years for a machine check to come in. 140 00:10:13,780 --> 00:10:17,040 So then we have to wonder, can we generate these on demand? 141 00:10:17,120 --> 00:10:19,240 There's some way to simulate machine checks. 142 00:10:19,240 --> 00:10:23,300 There's like a tool out there called MCE Inject, but all that does is call the operating system handler. 143 00:10:23,820 --> 00:10:25,660 But that doesn't interrupt anything. 144 00:10:25,660 --> 00:10:30,100 We need real physical machine checks if we want to use these for security purposes. 145 00:10:30,520 --> 00:10:32,920 So I'm diving into that a little bit more. 146 00:10:32,920 --> 00:10:34,460 We need to know how machine checks work. 147 00:10:34,460 --> 00:10:40,360 So the gist behind machine checks is they've got these registers that track them, that are arranged in what they call banks. 148 00:10:40,380 --> 00:10:43,400 And you've got different banks devoted to different sources of machine checks. 149 00:10:43,400 --> 00:10:48,040 You'll have a bank for the L2 cache, and a bank for the decode unit, and a bank for the execution unit. 150 00:10:48,040 --> 00:10:51,840 And there's just many, many options for these different machine check sources. 151 00:10:51,840 --> 00:10:56,360 And you can start dumping all these different banks to see what types of things can cause machine checks. 152 00:10:56,360 --> 00:11:08,140 And then there's extended banks, and it turns out that there's dozens of banks capturing hundreds of registers across thousands of bits, all of which represent different sources of these errors that can occur that the CPU can detect. 153 00:11:08,140 --> 00:11:19,180 And if you want to know exactly what are these individual bits, you can start diving into Linux kernel source, and it'll tell you things like some of these are main tag parity errors, and some are for instruction buffer, and some are for the micro-op queue. 154 00:11:19,180 --> 00:11:21,960 And the list just goes on and on and on and on and on. 155 00:11:21,960 --> 00:11:27,620 There are hundreds and hundreds of different sources of these different types of problems on a CPU. 156 00:11:27,860 --> 00:11:32,180 And if we want to dive into this, well, it's hard to know where to begin, but we've got to start somewhere. 157 00:11:32,520 --> 00:11:40,200 So when I was reading through some of this documentation, I noticed that some platforms are reserving the fourth machine check bank for logging errors from the north bridge. 158 00:11:40,200 --> 00:11:45,180 And that seemed interesting because the north bridge seemed a little bit more configurable than some of the other things I could access. 159 00:11:45,180 --> 00:11:51,860 Like, I don't have a lot of control over parity errors in the L2 cache, but the north bridge has registers that we can actually reach. 160 00:11:52,020 --> 00:11:54,680 So maybe we could start there, and then we can expand on this later. 161 00:11:54,680 --> 00:11:59,960 And all these details are going to vary across different generations of CPUs, but it gives us a starting point. 162 00:12:00,080 --> 00:12:07,080 So diving into the data sheets here, they suggest that machine check exceptions can be generated from master abort signals arriving from the north bridge. 163 00:12:07,080 --> 00:12:16,060 So I thought this was super interesting because master abort is what happens when one PCI device is trying to talk to another PCI device, and that other device doesn't respond. 164 00:12:16,060 --> 00:12:18,540 That's perfect because that's something we can control. 165 00:12:18,540 --> 00:12:25,600 Even just from the Linux command line, if we use set PCI to try to access a device on the PCI bus that doesn't exist, we should get a master abort. 166 00:12:25,600 --> 00:12:28,340 So we can try that, and nothing really happens. 167 00:12:28,340 --> 00:12:31,340 We probably get a master abort, but we don't get a machine check here. 168 00:12:31,340 --> 00:12:32,600 And that's not too surprising. 169 00:12:32,600 --> 00:12:39,480 That would kind of defeat the purpose of machine checks if they just came about from these totally benign things like accessing a non-existing device. 170 00:12:39,540 --> 00:12:45,400 But still, the data sheets suggest there's some way for a master abort to cause a machine check exception. 171 00:12:45,620 --> 00:12:49,140 So maybe we can figure this out by diving into the north bridge a little bit more. 172 00:12:49,140 --> 00:13:03,100 So I went through hundreds of pages of north bridge documentation trying to find out how can I generate a machine check here, and I found all these different registers that look like maybe they control in some abstract way the different kinds of errors that can be reported from the north bridge, 173 00:13:03,100 --> 00:13:08,080 but none of these had enough information to decide conclusively how do I get a machine check out of this. 174 00:13:08,140 --> 00:13:19,840 So I tried going through and flipping each of these bits one by one and accessing a non-existing device on the PCI space to see if I get a machine check, and a lot of those configurations were crashing or hanging the system, but I never did get a machine check. 175 00:13:19,980 --> 00:13:23,600 And I thought, you know, maybe it's more than one bit that I need to change. 176 00:13:23,600 --> 00:13:31,440 Maybe I need to change several of these bits, but that gets to be too many permutations to try by hand, and there's just not enough information in the specs to move forward here. 177 00:13:31,620 --> 00:13:38,960 But I find any time you're stuck in CPU exploitation, a good strategy is just start smashing things and see what happens. 178 00:13:38,960 --> 00:13:53,500 So I built a north bridge fuzzer here, and this fuzzer, basically, you'll specify which bits in the north bridge you're interested in, and in this case, there are about 25 or 24 different bits I thought might impact machine checks, and it's just going to start randomly flipping bits in the north bridge, 179 00:13:53,500 --> 00:13:58,440 and if it gets a crash or a hang, we can just power cycle the system and try again. 180 00:13:58,440 --> 00:14:09,060 On the other hand, if we don't get a crash or hang, we'll go ahead and try to access a non-existent device on the PCI space, and from that, we can see if we got any machine checks. 181 00:14:09,060 --> 00:14:12,140 We'll look at those MCA banks to see if any machine checks got logged. 182 00:14:12,200 --> 00:14:19,640 Now, the platform might reset with the machine check, but the neat thing about these banks is that they're what are called sticky, which means that they persist across the system reset. 183 00:14:19,640 --> 00:14:27,880 So even after the reset, we can check the MCA banks to see if we get any machine check issues, and we'll just repeat this over and over and over, seeing if we can generate a machine check. 184 00:14:27,880 --> 00:14:29,480 So that looks something like this. 185 00:14:29,480 --> 00:14:41,620 On the right side, I've got the console on my computer, and on the left side, I'm going to fire up the north bridge fuzzer, and I had to censor out some of the different bit names that it's looking at here, since this issue I'm going to talk about is not actually patched yet, 186 00:14:41,620 --> 00:14:50,220 but it's going to start flipping random bits in the north bridge and then trying to access a non-existing PCI device to see if we happen to get a machine check issue. 187 00:14:50,720 --> 00:14:59,040 And eventually, if we let this run a little bit, we'll get an error that very, very briefly spits out on the console window, and then the system's going to reset. 188 00:14:59,040 --> 00:15:06,600 But like I said, those MCA bank bits are sticky, so now we can look at the boot logs and see that, sure enough, we got a machine check log. 189 00:15:06,600 --> 00:15:08,400 So this was super exciting. 190 00:15:08,400 --> 00:15:18,640 I don't think anybody's ever found a way to generate machine checks on demand before, and what the fuzzer found was two specific bits in the north bridge, where if we flip those bits, now we can generate machine checks. 191 00:15:18,640 --> 00:15:20,140 But we've got a problem. 192 00:15:20,220 --> 00:15:31,760 Those machine checks are being delivered to the same core that generated the master abort, and from an exploitation perspective, it's not super useful to have a core that can just target itself with a machine check. 193 00:15:31,760 --> 00:15:34,320 All that lets us do is interrupt our own code. 194 00:15:34,580 --> 00:15:38,020 Basically, we have a hammer now, but we can only hit ourselves with it. 195 00:15:38,020 --> 00:15:39,860 So we need to improve this a little bit. 196 00:15:39,860 --> 00:15:44,520 We need a way for one core to attack a different core with machine checks. 197 00:15:44,520 --> 00:15:46,420 Basically, we need to add a handle to our hammer. 198 00:15:46,620 --> 00:15:51,500 So we can modify the fuzzer a bit here and start looking for more complex bit configurations. 199 00:15:51,780 --> 00:15:58,720 Still the same idea, generate a PCI abort from one core, but then look at the other cores to see if they got the machine check exception. 200 00:15:58,980 --> 00:16:08,900 And it takes a bit longer, but if you let the fuzzer run for a few hours, eventually it will find a different configuration of bits that lets us target different cores with the machine check exception. 201 00:16:08,900 --> 00:16:15,000 So it finds three bits in the north bridge, where if we flip them, we can attack core zero from any other core on the processor. 202 00:16:15,000 --> 00:16:17,940 But we've still got an issue, because the platform's still resetting. 203 00:16:18,000 --> 00:16:21,860 So basically at this point, we just need to be a little bit more careful with our hammer. 204 00:16:22,280 --> 00:16:28,360 So interrupting core zero from core one is neat, but it doesn't help us for exploitation if the system just goes down right away. 205 00:16:28,480 --> 00:16:31,520 So what we need to do is figure out, you know, why does the system go down? 206 00:16:31,520 --> 00:16:33,300 It's not actually because of the machine checks. 207 00:16:33,300 --> 00:16:38,120 It's because the operating system decides that's the safest thing to do when it gets a machine check. 208 00:16:38,140 --> 00:16:44,120 So to get around that, we can hijack the CPU interrupt descriptor table and install like a first pass machine check handler. 209 00:16:44,220 --> 00:16:45,340 So what that looks like is this. 210 00:16:45,340 --> 00:16:50,880 So normally the operating system sets up this IDT, which is the table that describes how to handle different interrupt events. 211 00:16:50,880 --> 00:16:54,420 And one of those entries tells the CPU how to handle a machine check. 212 00:16:54,420 --> 00:16:59,360 What we want to do is replace that interrupt descriptor table with our own, with a hijack IDT. 213 00:16:59,380 --> 00:17:02,940 And the hijack IDT is going to get control first whenever there's any interrupt event . 214 00:17:02,940 --> 00:17:11,640 And usually what I have the hijack IDT do is just kind of pre-process the event to see if there's anything about it that we want to change, and then hand off control to the operating system's IDT . 215 00:17:11,640 --> 00:17:15,460 That way we get first dibs on any interrupts or exceptions happening on the platform. 216 00:17:15,460 --> 00:17:25,320 So what I had the handler do, our hijack IDT, is just going to indiscriminately clear any of the log machine checks that it finds in the MCA banks before giving control to the operating system. 217 00:17:25,320 --> 00:17:29,960 That way the operating system won't reset the platform because it can't figure out what caused the machine check to begin with. 218 00:17:29,960 --> 00:17:30,880 So that's kind of dangerous. 219 00:17:30,880 --> 00:17:35,520 Usually you want the system to reset if you get a machine check, but it's good enough for research and exploitation. 220 00:17:35,880 --> 00:17:37,380 So collectively that looks something like this. 221 00:17:37,380 --> 00:17:52,020 Up at the top we've got our, we're watching the machine checks come in, and down here at the bottom what I'm going to do is I'm going to insert a hijack IDT onto the processor, and then we're going to poison the north bridge so that it'll turn any small error into a really big error. 222 00:17:52,020 --> 00:17:57,040 And then I'm going to use the set PCI command to try to access a non -existent PCI device on the bus. 223 00:17:57,040 --> 00:18:05,220 I'm going to try to access it from core 1, and you'll see that the machine check gets received on core 0, and best of all the whole system stays running. 224 00:18:05,220 --> 00:18:06,180 So this is great. 225 00:18:06,180 --> 00:18:08,300 This gives us like a state disruptor tool. 226 00:18:08,300 --> 00:18:14,200 It gives us a way to do on-demand generation of machine check exceptions entirely from software. 227 00:18:14,320 --> 00:18:28,120 So moving forward, I'm going to keep using this north bridge approach for generating these machine checks, and the configuration specifics of that are going to vary by what platform you're working on, but there's lots and lots of different ways to explore for generating machine checks beyond what I'm looking at here. 228 00:18:28,120 --> 00:18:32,140 But the next question then is, well, what do we want to target with this? 229 00:18:32,140 --> 00:18:36,880 So we basically have a great hammer now, we just need to find a nail. 230 00:18:36,940 --> 00:18:45,940 So the current machine check approach is using ring 0 for north bridge reconfiguration, which means we need to find targets that are more privileged than ring 0, and we can revisit that requirement later. 231 00:18:45,940 --> 00:18:47,740 I think there's a lot of ways around that. 232 00:18:47,740 --> 00:18:51,120 But for now, let's look at something more privileged than the kernel, than ring 0. 233 00:18:51,120 --> 00:18:57,640 So a lot of people think that once you gain code execution inside of the kernel or ring 0, that it's basically game over, you have everything you need. 234 00:18:57,640 --> 00:19:03,140 But it turns out on modern platforms, it actually gets much, much deeper than just the kernel and ring 0. 235 00:19:03,180 --> 00:19:09,880 So some of the different things we might look at are like hypervisors and secure guests and enclaves and secure loaders, the list goes on and on and on here. 236 00:19:09,980 --> 00:19:14,240 What I settled on for exploitation here was system management mode as a really appealing target. 237 00:19:14,300 --> 00:19:17,540 I almost didn't pick system management mode just because it's been done before. 238 00:19:17,540 --> 00:19:23,700 But it turns out SMM has some really interesting properties that make it a great case study for machine check exploitation. 239 00:19:24,060 --> 00:19:28,880 So if you're not familiar with SMM, it's actually a pretty neat mode of execution on x86 processors. 240 00:19:28,880 --> 00:19:30,700 It's been around for like 35 years. 241 00:19:30,700 --> 00:19:35,340 It's invisible to the operating system and hypervisor, it can preempt anything that's running on the system. 242 00:19:35,360 --> 00:19:40,760 And because it's got all these neat permissions, a lot of people think of more privilege than ring 0 or the kernel. 243 00:19:40,760 --> 00:19:43,840 It's often called ring minus 2, negative 2. 244 00:19:43,920 --> 00:19:47,000 It's critical to platform security and server reliability. 245 00:19:47,120 --> 00:19:51,820 And one of the neatest aspects is it usually has read and write access to a platform's firmware. 246 00:19:51,820 --> 00:19:57,580 So you can use it to persist implants down to firmware, which can survive a hard drive wipe or an OS reinstall. 247 00:19:57,580 --> 00:19:59,760 But all of that looks something like this. 248 00:19:59,760 --> 00:20:06,880 So the gist is when your computer first starts up, firmware has control before the operating system starts, and you've got some RAM on that system. 249 00:20:06,880 --> 00:20:10,540 So firmware's going to start partitioning that memory space into some different regions. 250 00:20:10,540 --> 00:20:19,160 It's going to pick a specific region inside of your memory to call SM RAM, system management RAM, and it's going to track SM RAM by a pointer called SM base. 251 00:20:19,420 --> 00:20:31,140 Then firmware's going to drop a whole bunch of code into SM RAM for handling system management interrupts later on, and then it's going to lock down SM RAM so that nobody ever gets to see what's inside of SM RAM again. 252 00:20:31,180 --> 00:20:41,100 Later on, when your operating system or your user code is running, some IO device on the system's going to trigger something called an SMI, a system management interrupt, which is then going to hand control to microcode. 253 00:20:41,340 --> 00:20:43,820 Microcode's going to open up SM RAM temporarily. 254 00:20:43,820 --> 00:20:50,000 It's going to save the CPU state, load a new SMM state, and then jump to the handler that firmware installed inside of SM RAM. 255 00:20:50,100 --> 00:20:53,420 So inside of SM RAM, all this code is executing that you never get to see. 256 00:20:53,420 --> 00:20:59,740 And when it's all done, it's going to execute this magic resume instruction, which gives control back to microcode, which does the reverse of the process. 257 00:20:59,740 --> 00:21:04,360 It restores the CPU state, it closes SM RAM, and it jumps back to the original code. 258 00:21:04,360 --> 00:21:10,820 Now, your original code that got interrupted has no idea that a whole bunch of stuff just ran underneath it that we never even got to see. 259 00:21:10,920 --> 00:21:14,380 So a demo for system management mode is a little bit anticlimactic. 260 00:21:14,380 --> 00:21:18,980 If we want to understand what is inside of this SM RAM, it might be a little unsettling. 261 00:21:18,980 --> 00:21:22,080 There's this whole region of memory on our computers we never get to see. 262 00:21:22,080 --> 00:21:26,700 We can figure out where that memory is by reading a very specific register from the CPU. 263 00:21:26,780 --> 00:21:34,900 But then if we try to access that memory, even if we're running inside of the kernel with ring zero level privileges, if we ask the CPU, hey, what's at this memory? 264 00:21:34,900 --> 00:21:36,940 The CPU comes back and says, oh, just Fs. 265 00:21:36,940 --> 00:21:37,980 Don't worry about it. 266 00:21:37,980 --> 00:21:39,240 Nothing to see here. 267 00:21:39,240 --> 00:21:44,540 So, you know, it's a little bit concerning when there's stuff on our computers that we have no insight or control over. 268 00:21:44,540 --> 00:21:47,860 So it's always nice to try to break into these things. 269 00:21:47,860 --> 00:21:48,820 So how could we do that? 270 00:21:48,820 --> 00:21:51,020 How can we compromise this ring minus two? 271 00:21:51,120 --> 00:21:52,440 There's a couple things to keep in mind here. 272 00:21:52,440 --> 00:21:55,820 One is that SMM code is running inside of SM RAM. 273 00:21:55,820 --> 00:22:04,980 And if we could corrupt or hijack the normal control flow of that code, then maybe we could use that to execute a malicious payload but inside of SMM with ring minus two privileges. 274 00:22:04,980 --> 00:22:08,340 And that payload then could unlock system management RAM for us. 275 00:22:08,740 --> 00:22:11,940 So moving forward with this, there's another key aspect we need to understand. 276 00:22:11,940 --> 00:22:17,100 And that's that different CPU modes have to share resources with differently privileged modes. 277 00:22:17,100 --> 00:22:25,540 So like if you're switching from user LAN down to the kernel, some of the CPU registers are going to be shared in between those two different execution modes. 278 00:22:25,540 --> 00:22:30,740 Ideally, what we'd like to do is we just reset all the CPU registers anytime we change modes. 279 00:22:30,740 --> 00:22:34,120 That way we don't have to worry about what was in those registers before. 280 00:22:34,120 --> 00:22:39,560 But when there's thousands of different registers on the CPU, we don't really have the luxury of resetting them all. 281 00:22:39,560 --> 00:22:47,880 So what architects have to do is they have to carefully decide which registers have to stay the same, and which registers do I need to change when I switch modes. 282 00:22:47,880 --> 00:22:53,820 And if you do that all correctly, then even from a less privileged mode, you can't really impact a more privileged mode. 283 00:22:53,820 --> 00:23:00,420 So in investigating this, I started to look into the AMD documentation on how they implement system management mode. 284 00:23:00,420 --> 00:23:04,280 And if you dive deep enough into that documentation, you'll see a chart like the chart on the right. 285 00:23:04,280 --> 00:23:09,820 And it shows you exactly which registers get changed when you enter system management mode and which registers stay the same. 286 00:23:09,820 --> 00:23:14,880 It'll also tell you that they suppress all the normal interrupts when they enter system management mode, just like we talked about earlier. 287 00:23:14,880 --> 00:23:18,800 We don't want to have interrupts unexpectedly firing when we're entering secure modes. 288 00:23:18,800 --> 00:23:25,280 But one of the things in this chart caught my attention, and that's this IDTR is marked as unmodified. 289 00:23:25,280 --> 00:23:26,100 So why does that matter? 290 00:23:26,100 --> 00:23:35,620 Well, IDTR is the register that points to the interrupt descriptor table, the table that tells the CPU how to handle any interrupts or exceptions that occur on the processor. 291 00:23:35,800 --> 00:23:50,960 Leaving that unmodified on entry to system management mode is a bit of a curious choice for implementing a secure mode, because it means that any interrupt or exception that does occur inside of system management mode is going to be delivered on an untrusted handler. 292 00:23:50,960 --> 00:23:58,540 And that's not just for interrupts, it's even pretty benign exceptions, like have a divide by zero error, call the untrusted code to deal with that. 293 00:23:58,540 --> 00:24:08,740 In fact, it's kind of like throwing all your code into a main function and then wrapping main in a try except, and then in your except block, just pop a shell for the attacker. 294 00:24:08,740 --> 00:24:14,620 So like anything at all that goes wrong in your code leads to immediate privilege escalation for somebody else. 295 00:24:14,620 --> 00:24:23,880 In fact, the, the CPU design documents say that because of this implementation, they strongly recommend that you write all your code without bugs in it. 296 00:24:23,880 --> 00:24:29,220 And I, I guess that's, that's okay, but it seems like maybe we could be doing something better than that. 297 00:24:29,220 --> 00:24:35,900 So when I reached out to the vendor about this and kind of expressed my concerns here, they, they really didn't want to call this a vulnerability. 298 00:24:35,900 --> 00:24:38,700 And I had to get these slides through all sorts of legal and other reviews. 299 00:24:38,700 --> 00:24:42,760 So we're going to call this a feature instead, and then we'll talk about how to exploit the feature. 300 00:24:43,000 --> 00:24:49,440 Because there's, there's lots of ways to approach this when anything at all that goes wrong is going to lead to privilege escalation. 301 00:24:49,440 --> 00:24:52,700 We just have to make something, anything go wrong on the CPU. 302 00:24:52,700 --> 00:25:00,040 Well, it's convenient then that we've actually got this tool to throw machine checks at other cores and cause things to go catastrophically wrong. 303 00:25:00,260 --> 00:25:04,380 So we've got a problem though, if we want to use machine checks to exploit this issue. 304 00:25:04,460 --> 00:25:13,720 And that's that microcode does clear out this CR4 register, which means that machine check handling is temporarily disabled on entry to system management mode. 305 00:25:13,820 --> 00:25:17,760 So to understand what we can do about that, we need to dive into SMM code a little bit more. 306 00:25:17,760 --> 00:25:20,900 So the source of this code is called EDK2. 307 00:25:20,900 --> 00:25:26,260 EDK2 is like an open source implementation or reference implementation for EFI firmware. 308 00:25:26,260 --> 00:25:30,460 It's basically showing everybody, here's one way to implement the EFI specification. 309 00:25:30,460 --> 00:25:35,740 And then vendors can take that EDT, EDK2 code and then adapt it for their platforms as needed. 310 00:25:35,740 --> 00:25:42,120 So this is how EDK2 shows you could implement an SMI handler in system management mode. 311 00:25:42,120 --> 00:25:47,300 There's a lot of code here, but the parts we're most interested in are right here with this CR4 load. 312 00:25:47,320 --> 00:25:51,020 EDK2 is going to enable handling of machine check exceptions. 313 00:25:51,080 --> 00:25:54,420 And then over here later on, we see an instruction called LIDT. 314 00:25:54,420 --> 00:25:59,100 That's going to reload a new interrupt table inside of system management mode. 315 00:25:59,100 --> 00:26:02,920 So collectively that gives us a couple of different attack windows inside of SMM. 316 00:26:02,920 --> 00:26:16,880 So the first attack window we've got is basically for any exception at all, other than machine checks, if you can cause something to go wrong before that LIDT instruction, it'll be handled on an untrusted interrupt descriptor table. 317 00:26:16,880 --> 00:26:22,680 On the other hand, if you want to look at machine checks specifically like I was doing for this research, your window's a little bit narrower. 318 00:26:22,800 --> 00:26:28,540 Your attack window's in between that CR4 reload and the upcoming LIDT instruction. 319 00:26:28,540 --> 00:26:31,200 So our machine check attack might look something like this. 320 00:26:31,200 --> 00:26:43,500 If we can create a machine check from a thread that's outside of SMM and receive that machine check on a thread that's inside of SMM exactly within that narrow attack window, then we could get privilege escalation. 321 00:26:43,500 --> 00:26:45,760 But we've got yet another hurdle to overcome. 322 00:26:45,760 --> 00:26:51,720 The system management mode design basically has all threads entering and exiting system management mode simultaneously. 323 00:26:51,720 --> 00:26:59,620 So what happens is a thread will generate a system management interrupt through some hardware event, and then that signal gets sent to all the different threads on the platform. 324 00:26:59,820 --> 00:27:05,780 And then each thread gets to finish its current instruction, and then microcode's going to pull those threads into system management mode. 325 00:27:05,780 --> 00:27:07,120 And that's called thread quiescing. 326 00:27:07,120 --> 00:27:10,880 It basically ensures that all threads are inside of SMM at the same time. 327 00:27:10,980 --> 00:27:16,340 And indirectly, that prevents a non-SMM thread from attacking an SMM thread. 328 00:27:16,340 --> 00:27:20,560 And it's a pretty common pattern in designing these privileged execution modes. 329 00:27:20,580 --> 00:27:29,160 So our challenge here then, we need a victim thread that's in SMM, and an attacking thread that's outside of SMM, but all threads enter SMM at the same time. 330 00:27:29,160 --> 00:27:35,700 Well, there's an observation here, and it's that threads do not technically all enter system management mode at the same time. 331 00:27:35,700 --> 00:27:47,360 Each thread gets to finish the instruction that it was currently working on before it gets pulled into SMM, which means that an attacking thread has one instruction with which to complete an attack here. 332 00:27:47,360 --> 00:27:49,920 So what if we had an attack that looked like this? 333 00:27:49,920 --> 00:27:55,100 We begin some sort of magic instruction, and I don't know what this instruction is at this point, on an attacking thread. 334 00:27:55,100 --> 00:27:58,540 And then on a victim thread, the victim thread is going to trigger a system management interrupt. 335 00:27:58,540 --> 00:28:01,000 On x86, you can do that by writing to port B2. 336 00:28:01,000 --> 00:28:09,120 Both threads are going to get the system management interrupt, and now microcode is going to force them to both enter, like finish up the instruction they're working on, and then enter system management mode. 337 00:28:09,120 --> 00:28:13,220 So our victim thread is going to finish its instruction, then get pulled into system management mode. 338 00:28:13,220 --> 00:28:19,780 It's not going to change its IDT, since that's the behavior on these processors, but it is going to disable handling of machine checks. 339 00:28:19,780 --> 00:28:25,940 It's going to start executing its handler, and then at some point in the future, the victim thread is going to enable handling machine checks. 340 00:28:26,000 --> 00:28:38,920 And if, at exactly this point, thread one, that instruction that it began at the beginning, if right here, thread one generated that machine check exception, received by thread zero, we would get code execution on the victim thread here. 341 00:28:39,000 --> 00:28:49,860 Eventually, the thread one attacking thread will end its current instruction, then get sucked into system management mode as well, and if we miss the machine check window, thread zero, the victim is going to load up a new safe interrupt descriptor table, 342 00:28:49,860 --> 00:28:51,780 so that you can't attack it anymore. 343 00:28:51,840 --> 00:28:53,980 So this is where we're at. 344 00:28:53,980 --> 00:29:04,080 We need some sort of magic instruction that can begin, and then 10,000 cycles later, I actually measured how long this would take, it would take 10,000 cycles before that attack window opens up. 345 00:29:04,260 --> 00:29:11,840 And if you can, after 10,000 cycles, then get a machine check to be delivered on the victim thread, within a hundred cycle attack window, you'll get execution. 346 00:29:11,960 --> 00:29:29,060 But right now, this is what we have, as far as how we generate machine checks right now, it only takes about 700 cycles for an attacking instruction to generate a machine check, which means we, we undershoot our attack window, the machine check is received on the victim before machine check handling is actually enabled, 347 00:29:29,060 --> 00:29:30,900 and the whole platform is going to reset. 348 00:29:30,900 --> 00:29:35,600 So the question then is, does this magic instruction actually exist? 349 00:29:35,600 --> 00:29:43,180 Is there an instruction that can generate a machine check exception after 10,000 cycles, but still be precise enough to hit it in a hundred cycle window? 350 00:29:43,180 --> 00:29:49,720 Essentially, what we need here is some sort of fuse, a way to delay, start the process, but delay the actual machine check. 351 00:29:49,760 --> 00:29:51,640 So how do we build this fuse? 352 00:29:51,640 --> 00:29:53,760 Well, we've got a lot of challenges to overcome. 353 00:29:53,760 --> 00:29:57,220 One is that we need an instruction that can actually generate a master abort. 354 00:29:57,220 --> 00:30:01,140 And master aborts are being done through MMIO on PCI space. 355 00:30:01,140 --> 00:30:06,180 The architecture requires that accessing MMIO follows a very specific form. 356 00:30:06,180 --> 00:30:10,400 You have to use a specific instruction and specific registers to access MMIO. 357 00:30:10,400 --> 00:30:20,980 So in looking at how long that, that instruction takes, it's like six cycles if you're hitting cache, or 250 cycles if you're hitting RAM, and 700 if you're hitting PCI MMIO. 358 00:30:21,400 --> 00:30:30,360 700 cycles is still an order of magnitude away from the 10,000 that we need for this fuse instruction to hit the actual attack window, which means the attack won't work. 359 00:30:30,360 --> 00:30:32,180 It's basically fundamentally broken. 360 00:30:32,420 --> 00:30:36,600 So I started thinking about this when I thought, like, maybe there's no way forward here. 361 00:30:36,600 --> 00:30:41,340 And eventually it occurred to me that, you know, not all MMIO is going to be exactly the same. 362 00:30:41,380 --> 00:30:49,260 Maybe the, the address I'm using right now for my machine checks takes 700 cycles, but surely there's other things on the PCI bus that take longer. 363 00:30:49,260 --> 00:30:59,280 So I built a little tool to actually measure the MMIO space and how long different accesses take, and started looking for anything on the PCI space that takes an unusually long time to access. 364 00:30:59,280 --> 00:31:04,500 So what I found is that most things are taking like 700 cycles, but some things are taking like 4,000. 365 00:31:04,500 --> 00:31:07,640 And that's starting to get a little bit closer than where we were before. 366 00:31:07,640 --> 00:31:09,740 So I started wondering, can we increase this somehow? 367 00:31:09,740 --> 00:31:23,060 And I started looking at adding additional MMIO traffic, or underclocking the CPU, or doing weird PCI topologies, and all of those could increase it a little bit, but nothing got me to the 10,000 cycles we needed, and the attack was becoming less and less practical. 368 00:31:23,220 --> 00:31:25,160 But then I realized I was doing something stupid. 369 00:31:25,160 --> 00:31:31,400 I was following the CPU specs, and when you're trying to exploit something, that's the opposite of what you want to do. 370 00:31:31,540 --> 00:31:38,160 So the specs say you have to, if you want to access MMIO configuration space, you have to use instructions of the following form. 371 00:31:38,160 --> 00:31:39,720 It has to be a move instruction. 372 00:31:39,880 --> 00:31:42,200 It has to be either EAX, AX, or AL. 373 00:31:42,200 --> 00:31:44,540 That's a 4-byte, 2-byte, and 1-byte register. 374 00:31:44,640 --> 00:31:47,660 And you have to use some memory addressing form to do that. 375 00:31:47,660 --> 00:31:49,800 And it says nothing else is allowed. 376 00:31:49,920 --> 00:31:54,780 So this is what our instruction would normally look like for accessing MMIO space. 377 00:31:55,080 --> 00:32:02,860 But what if instead of the EAX register like it tells us, what if we use a register that it tells us we can't use, like RAX is an 8-byte register? 378 00:32:02,980 --> 00:32:08,600 Well, if we try that, it turns out it works, and the access time jumps from 4,000 cycles to 8,000 cycles. 379 00:32:08,600 --> 00:32:15,960 And I think what's happening is behind the scenes, it's actually breaking this into two 4-byte accesses, which doubles the access time for that MMIO device. 380 00:32:16,120 --> 00:32:19,360 So I start to think, what else can we violate in the CPU specs? 381 00:32:19,360 --> 00:32:20,500 Well, there's another thing it tells us. 382 00:32:20,500 --> 00:32:25,100 It says all such accesses are required not to cross any naturally aligned DWORD boundary. 383 00:32:25,100 --> 00:32:27,340 And if you do, you'll get undefined behavior. 384 00:32:27,340 --> 00:32:28,320 Well, that sounds awesome. 385 00:32:28,320 --> 00:32:30,980 Let's find out what undefined behavior looks like in this case. 386 00:32:30,980 --> 00:32:32,460 So this is what our instruction looked like. 387 00:32:32,460 --> 00:32:40,480 We're accessing this on an aligned boundary, and if we unalign it just by one byte, all of a sudden, our access time jumps from 8,000 to 12,000 cycles. 388 00:32:40,620 --> 00:32:43,000 So we've got the pieces that we need here. 389 00:32:43,000 --> 00:32:48,200 All we need is a high access time on an existing PCI device, followed by a non-existing device. 390 00:32:48,200 --> 00:32:51,980 That non-existing device is what's going to give us the master abort that'll generate the MCE. 391 00:32:52,160 --> 00:33:04,160 And when we issue that instruction behind the scenes, it's going to do a 4-byte access here, taking 4,000 cycles, another 4-byte access taking 4,000 cycles, and it's going to take 4,000 cycles, but finally it's going to realize, hey, this last byte you asked for, 392 00:33:04,160 --> 00:33:07,040 it doesn't even exist, and it's going to generate that master abort. 393 00:33:07,180 --> 00:33:08,800 So we've got all the pieces now. 394 00:33:08,800 --> 00:33:10,320 We've got this fuse instruction. 395 00:33:10,320 --> 00:33:24,260 All we needed was an unaligned 8-byte PCIe access that straddles a slow PCIe device and a non-existing device where the CPU is going to perform three separate 4-byte MMIO accesses, and the final 4-byte MMIO access is going to hit a non-existing PCIe device, 396 00:33:24,260 --> 00:33:27,140 then no device is going to claim that request, resulting in a master abort. 397 00:33:27,140 --> 00:33:36,800 The master abort is going to be changed to a machine check by the north bridge that's going to be received by the CPU, which is going to send it to the victim core all 10,000 cycles after that instruction initially began. 398 00:33:36,800 --> 00:33:44,300 So we're getting close, but we've got one last problem, and that's that we don't really have control over how long this fuse instruction takes. 399 00:33:44,360 --> 00:33:48,220 But we still need that machine check to arrive in a really precise attack window. 400 00:33:48,220 --> 00:33:55,580 So we wanted the attack to look something like this, where exactly 10,000 cycles after the fuse instruction begins, the machine check's received. 401 00:33:55,580 --> 00:33:56,820 And what we got was this. 402 00:33:56,820 --> 00:34:03,960 It takes 12,000 cycles instead, and we overshoot the attack window, and the machine check's not going to be delivered on an untrusted interrupt descriptor table. 403 00:34:03,960 --> 00:34:10,040 We can fix this, though, by going to the victim thread and just adding a little bit of a delay before it starts triggering system management mode. 404 00:34:10,040 --> 00:34:14,620 We can shift that attack window down in order to get the MCE to arrive at the right time. 405 00:34:14,620 --> 00:34:21,320 So with that, we can actually deliver now these cross -core machine checks to victim threads at very, very precise times. 406 00:34:21,320 --> 00:34:23,600 So we've got all the pieces now. 407 00:34:23,620 --> 00:34:26,440 We've got a fantastic tool available to us. 408 00:34:26,440 --> 00:34:30,340 The last thing we need is just a name for our tool, and it's kind of staring us in the face. 409 00:34:30,340 --> 00:34:32,440 We built a hammer out of machine checks. 410 00:34:32,440 --> 00:34:34,280 We built an MC Hammer. 411 00:34:34,580 --> 00:34:41,160 So let's see what this whole tool is going to look like in action, because it gets a little bit convoluted. 412 00:34:41,280 --> 00:34:44,920 But you've got your CPU, and you've got some different devices attached to your CPU. 413 00:34:44,920 --> 00:34:48,260 And then on the right side, you've got your RAM, some MMIO space, and other stuff. 414 00:34:48,260 --> 00:34:50,840 While your CPU is booting, the firmware has control. 415 00:34:50,840 --> 00:34:56,560 The firmware's going to partition out some RAM, call that SM RAM, and it's going to drop a whole bunch of code into SM RAM. 416 00:34:56,560 --> 00:35:02,660 The firmware's going to lock down SM RAM so nobody gets to see it anymore, and then it's going to hand control off to the operating system. 417 00:35:02,660 --> 00:35:10,180 The operating system's going to install its own interrupt descriptor table, a list of pointers to different functions for handling interrupt and exception events. 418 00:35:10,220 --> 00:35:16,740 Meanwhile, we're going to start a victim thread on thread zero and an attacking thread on thread one. 419 00:35:16,900 --> 00:35:29,400 The attacking thread is going to reconfigure the north bridge with those magic bits that the fuzzer found to essentially poison the north bridge so that any small problem that the north bridge finds is going to escalate into a very big problem for the CPU. 420 00:35:29,420 --> 00:35:39,640 Next, the attacking thread is going to install a hijack interrupt descriptor table on top of the victim thread so that the hijack IDT will get control before the victim thread's normal interrupt descriptor table does. 421 00:35:39,640 --> 00:35:44,500 Inside of that hijack IDT, our machine check pointer points to a function that we want to run. 422 00:35:44,500 --> 00:35:47,860 It's going to be the malicious code that we want to run when a machine check is received. 423 00:35:47,880 --> 00:35:51,320 Now both threads are going to start this piece at exactly the same time. 424 00:35:51,320 --> 00:35:59,980 Thread zero, the victim, is going to start a carefully calibrated delay loop to shift down the attack window just a little bit, while the attacking thread is going to start its fuse instruction. 425 00:36:00,040 --> 00:36:08,960 So the victim thread is going to sit in a loop for just a little while to move that attack window down, and the attacking thread is going to send that address that it's trying to access over to the CPU. 426 00:36:08,960 --> 00:36:11,520 The CPU decodes that address, sends it to the north bridge. 427 00:36:11,520 --> 00:36:14,920 The north bridge decodes that address and sends it to the PCI bus. 428 00:36:14,920 --> 00:36:23,020 The PCI bus is very, very slowly going to start responding to that first four byte chunk, the next four byte chunk, and eventually the last chunk that doesn't actually exist. 429 00:36:23,020 --> 00:36:28,520 And effectively what we've done is we've started a timer on the PCI device that's going to come back later on. 430 00:36:28,520 --> 00:36:35,080 Meanwhile, the victim thread is going to finish up its carefully calibrated loop and it's going to write to port B2 to trigger a system management interrupt. 431 00:36:35,180 --> 00:36:44,460 The SMI request goes to the CPU, gets routed to some sort of external device, that IO device generates the system management interrupt, which gets sent to the attacking thread and the victim thread. 432 00:36:44,480 --> 00:36:51,180 Now microcode's going to make sure that both of these threads get to finish the instruction that they're on and then they get sucked into system management mode. 433 00:36:51,180 --> 00:36:56,180 Well, the victim thread's going to finish its out instruction very quickly and it moves over to system management mode. 434 00:36:56,340 --> 00:36:59,540 Microcode unlocks SMRAM for the victim thread. 435 00:36:59,540 --> 00:37:00,840 Only the victim thread can see this. 436 00:37:00,840 --> 00:37:02,880 Everybody else still can't see inside of SMRAM. 437 00:37:02,880 --> 00:37:07,580 The victim thread's going to start executing the handler that was installed by the, uh, by the firmware. 438 00:37:07,580 --> 00:37:14,460 So the victim thread's going to execute, execute, execute, execute, and eventually the victim thread is going to write to that CR4 register. 439 00:37:14,620 --> 00:37:16,200 This opens up our attack window. 440 00:37:16,200 --> 00:37:19,640 It enables handling of machine checks inside of system management mode. 441 00:37:19,640 --> 00:37:25,140 So our attack window has begun, but very shortly the victim thread is going to load a new interrupt descriptor table. 442 00:37:25,140 --> 00:37:32,200 That's going to overwrite our hijack table and we won't gain execution if our machine check arrives after that LIDT instruction. 443 00:37:32,380 --> 00:37:39,200 But, um, so the victim thread's going to execute, execute, and execute, and what we're hoping is that the MCE is going to arrive before we hit that LIDT. 444 00:37:39,200 --> 00:37:47,980 And if we've timed everything correctly, all of a sudden that PCIe device that we were trying to access is going to realize, hey, that last byte you asked for, that doesn't exist. 445 00:37:47,980 --> 00:37:49,340 Nobody's claiming that. 446 00:37:49,340 --> 00:37:51,140 So it's going to create the master abort. 447 00:37:51,140 --> 00:37:54,560 And PCIe parlance, that's an unsupported request, gets sent to the north bridge. 448 00:37:54,560 --> 00:38:00,980 The north bridge, because it's been poisoned, translates that into a very serious error, a CPU read data error sent to the CPU. 449 00:38:00,980 --> 00:38:04,260 The CPU gets that and translates that as a machine check exception. 450 00:38:04,260 --> 00:38:11,600 And even though thread one was the one that caused the machine check exception, that exception is delivered to thread zero, which is currently sitting inside of system management mode. 451 00:38:11,600 --> 00:38:14,680 Now thread zero needs to figure out how do I handle a machine check exception? 452 00:38:14,680 --> 00:38:20,500 Well, it looks up a handler inside of the interrupt descriptor table, the IDT that we poisoned before the whole process began. 453 00:38:20,520 --> 00:38:25,100 It follows the 18th entry in the IDT and begins executing the code that that points to. 454 00:38:25,100 --> 00:38:30,960 Meanwhile, eventually the attacking thread is going to finish up its fuse instruction, microcode sucks it into system management mode as well. 455 00:38:30,960 --> 00:38:33,060 We have no more control of the attacking thread here. 456 00:38:33,060 --> 00:38:35,220 It's just going to execute its handler like normal. 457 00:38:35,220 --> 00:38:46,720 And eventually it will resume and SMRAM gets locked up from the attacking thread's view, but that's okay because over here we now have our payload executing inside of system management mode with SMM privileges. 458 00:38:47,000 --> 00:38:49,880 So a full demo of this looks a little bit something like this. 459 00:38:49,880 --> 00:38:55,480 We fire up the MC hammer tool and it's going to replace all the interrupt descriptor tables on each core. 460 00:38:55,480 --> 00:38:57,660 It's going to load up some MC kernel modules. 461 00:38:57,660 --> 00:39:02,820 It's going to do a bunch of timing on different PCI devices to find that fuse instruction that it needs . 462 00:39:02,820 --> 00:39:06,500 It'll poison the north bridge so that small errors become really big errors. 463 00:39:06,500 --> 00:39:12,980 And when we're ready, we'll go ahead and launch a system management interrupt and that fuse instruction at exactly the same time. 464 00:39:12,980 --> 00:39:17,120 And if we time them right, we gain code execution inside of system management mode. 465 00:39:17,120 --> 00:39:25,320 And what I've got here is the malicious code that I executed inside of system management mode is a little command and control payload. 466 00:39:25,380 --> 00:39:28,920 And that gives us a way to talk to system management mode from the kernel. 467 00:39:28,920 --> 00:39:38,260 And what we can ask our C2 payload to do is to copy out SMRAM, that memory that we've never been able to touch before, we can copy that to a place where we can actually see it now. 468 00:39:38,260 --> 00:39:40,500 So that's the, uh, the memory that dumped on the bottom here. 469 00:39:40,500 --> 00:39:54,900 And the really neat thing is that on, on this platform, they left a whole bunch of build strings inside of system management RAM, which means that we can understand some of the capabilities that are inside of system management mode without ever having to do any sort of reverse engineering of the code. 470 00:39:54,900 --> 00:40:05,020 So if we look for some of these build strings that were in the protected SMRAM that we could never see before, we can get an idea of some of the things that, uh, that can, uh, that system management mode can do. 471 00:40:05,020 --> 00:40:08,780 So we can see that system management mode on the system has access to the platform flash. 472 00:40:08,780 --> 00:40:14,380 We can see it's got all sorts of USB drivers built into it for reasons I don't really understand. 473 00:40:14,380 --> 00:40:22,020 Um, if we scroll down, eventually we'll see that system management mode is able to persist, persist variables, uh, across boot by writing to the spy flash. 474 00:40:22,060 --> 00:40:26,340 Um, and we'll see the lock box is a way of storing secrets inside of system management mode. 475 00:40:26,340 --> 00:40:31,340 We'll see the S3 save state gives you access to all sorts of registers on the CPU you can't normally touch. 476 00:40:31,340 --> 00:40:40,420 We'll even see that system management mode on this platform has special, uh, doorbell registers to the platform security processor, the PSP, that we can't normally access from anywhere else. 477 00:40:40,440 --> 00:40:46,480 So it's, it's absolutely astounding how many different capabilities have been tossed into system management mode. 478 00:40:46,480 --> 00:40:51,240 And once you gain code execution, you get access to all of those things that we could never touch before. 479 00:40:51,300 --> 00:40:54,560 So where does that leave us as far as mitigations? 480 00:40:54,560 --> 00:41:04,960 Well, uh, the, the idea that the malicious interrupt descriptor table is allowed in system management mode, that is a feature on all AMD CPUs to date. 481 00:41:05,360 --> 00:41:10,900 Um, on the other hand, the machine check exploit that I developed, I developed this on pre-zen CPUs. 482 00:41:10,900 --> 00:41:13,760 So at least one of these things we can fix ourselves. 483 00:41:13,760 --> 00:41:19,240 So to mitigate the machine check path, um, we can change the way EDK2 works. 484 00:41:19,280 --> 00:41:24,860 EDK2 is who's providing, uh, some of the code that ends up on our, on our firmware for our, our different platforms. 485 00:41:24,860 --> 00:41:31,980 So even though that EDK2 SMM code is correct, it does assume that the IDT was being made safe by microcode. 486 00:41:31,980 --> 00:41:42,740 So on platforms that are leaving the IDT in an untrusted state, when entering system management mode, uh, EDK2 can be changed to basically reload the IDT at an earlier point. 487 00:41:42,740 --> 00:41:48,580 So I submitted a patch to EDK2 to remove the machine check vector, and that's still sort of trickling its way through the ecosystem. 488 00:41:48,640 --> 00:42:00,840 But meanwhile, the IDT, um, feature remains where, uh, essentially any small problem that occurs in early system management mode will be handled on untrusted, uh, interrupt handlers. 489 00:42:00,840 --> 00:42:02,620 And I think there's still a lot to explore there. 490 00:42:03,160 --> 00:42:04,900 But back to the machine check piece. 491 00:42:04,900 --> 00:42:09,180 I think this is a really exciting piece moving forward, uh, more so than the SMM piece. 492 00:42:09,180 --> 00:42:16,000 Uh, what we kind of find is that machine checks turn out to be this, like, really powerful thing for exploitation that have never been explored before. 493 00:42:16,160 --> 00:42:20,200 And I think there's a whole bunch of interesting ways that we can take this research. 494 00:42:20,200 --> 00:42:24,780 So one is we could start looking at other, uh, sources of machine check exceptions. 495 00:42:24,780 --> 00:42:27,860 Remember, there are hundreds of different ways to cause these different issues. 496 00:42:28,160 --> 00:42:30,660 I looked at one of them coming from the North Bridge. 497 00:42:30,660 --> 00:42:36,420 Uh, we could also look at ECC injection, different processor or rata are documented to cause machine check exceptions. 498 00:42:36,420 --> 00:42:41,140 We could look at doing, like, row hammer style bit flips in DRAM to cause machine check exceptions. 499 00:42:41,320 --> 00:42:49,940 We could look at other ways of doing asynchronous machine check exceptions, like there's a memory scrubber in ECC, uh, RAM that can cause asynchronous, uh, events. 500 00:42:49,940 --> 00:42:55,200 We could look at other MMIO devices to cause asynchronous MCEs or how to do this with no MMIO devices. 501 00:42:55,220 --> 00:43:01,440 The holy grail here would be finding a way to generate machine checks from user land from, from ring three. 502 00:43:01,500 --> 00:43:06,280 Uh, if you, uh, you could, you could, you could look at, like, ring three or rata that are related to machine checks. 503 00:43:06,280 --> 00:43:11,080 You could start looking at accessing faulty devices from user land to see if you can generate machine checks that way. 504 00:43:11,100 --> 00:43:23,980 The neat part about this is if somebody can find a way to generate machine checks from ring three, all those vulnerabilities that I talked about at the beginning of the presentation that were fixed with more aggressive interrupt and exception, uh, suppression, 505 00:43:23,980 --> 00:43:28,860 all those become viable, again, if you can use machine checks to break through those defenses. 506 00:43:28,860 --> 00:43:34,280 We could start looking at other exploit targets like hypervisors and secure guests and enclaves and secure loaders. 507 00:43:34,280 --> 00:43:46,100 And I think most exciting of all, we can start looking at other architectures because, uh, most of the major architectures out there have this idea of hardware error reporting and there's so much left to explore in ARM and RISC-V and MIPS and Power. 508 00:43:46,180 --> 00:43:53,440 And I think we're really just, you know, at the tip of the iceberg here as far as where we can take this kind of, kind of research. 509 00:43:53,440 --> 00:44:03,940 So anytime I do this, I feel like I'm standing on the shoulders of giants and, uh, people sometimes ask, you know, how can, how can they learn more and get up to, uh, uh, start doing some more of this type of research. 510 00:44:03,940 --> 00:44:06,620 So I wanted to give a couple of, uh, resources here. 511 00:44:06,620 --> 00:44:12,840 I wanted to give a big shout out to the fantastic int31 team who helped springboard and support a lot of these ideas in the early stages. 512 00:44:12,840 --> 00:44:17,360 I wanted to give a big thanks to the DazzleCat team, namely Stephanie, who always gives me ideas on how to move forward. 513 00:44:17,360 --> 00:44:24,260 And there is a huge slew of fantastic research papers on how to use interrupts in unexpected ways for privilege escalation. 514 00:44:24,260 --> 00:44:27,460 And I only got to talk about a couple of those early on in the presentation. 515 00:44:27,620 --> 00:44:35,500 And then after that, you can start diving into kernel source code to see how machine checks are handled in different situations and the different places machine checks can come from. 516 00:44:35,620 --> 00:44:40,860 But, um, I've got a lot, lot more to share, uh, with this research and there'll be a lot more to come. 517 00:44:40,860 --> 00:44:45,180 You can find me on most platforms under XOR, EAX, EAX, EAX. 518 00:44:45,180 --> 00:44:49,040 Um, right now I've shared some bare bones stuff on the MC Hammer GitHub repo. 519 00:44:49,040 --> 00:44:52,700 And I'll start pushing more and more stuff there as this issue is better mitigated. 520 00:44:52,700 --> 00:44:56,820 Um, but definitely check that out because, um, like I said, it's just the tip of the iceberg. 521 00:44:56,820 --> 00:44:58,140 So thanks everyone.