So I'm here today to talk to you about machine check exceptions. Basically what happens on your computer when something goes terribly, catastrophically wrong with the hardware. So why do we want to look at that? Well, it turns out that it's got some interesting applications for security that nobody's ever looked at before. But in order to explore it a little bit more, we really, really want to be able to see a machine check actually happen. So my setup here is on the left. I've got an x86 platform. And I'm going to be talking about x86 architecture today, but a lot of this will apply to other architectures as well. And on the right, we've got a little program running on this machine. It's just shuffling some letters around just to show us that something's actually running. And I think we're all probably familiar with what happens on a computer when something goes kind of wrong. We've probably seen, like, program crashes and seg faults and maybe a blue screen before. 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. The problem is computers today are actually pretty resilient and things just don't go catastrophically wrong very often. And we could watch this thing for, like, the next 10 years and probably not see any serious problems with the system. 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. So I thought, you know, maybe what if we rip off the heatsink from the CPU while the thing's running? Maybe that'll make something go wrong with the system. But if we try that, nothing really happened. We didn't get so much as an error message. Like I said, computers today are actually pretty resilient and things just don't go terribly wrong all that often. 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 . So if we really want something to go catastrophically wrong, we're going to have to give it a bigger nudge. We're going to have to make it so that the CPU can't just run away from its problems. We'll see if we can overheat this thing enough that underclocking is not going to help it. So we got to be a little careful here not to melt the rest of the platform. But if we're precise, we can hopefully make something go wrong with the hardware. And there it goes. The whole thing shut down. 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. And if the CPU cools down a little bit, this thing will boot back up, no worse for wear. But what's the point of all this? Well, it turns out that in the microseconds before everything comes crashing down for the CPU, some really interesting things start to happen. 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. So the idea here is you've got some code running on the CPU. You've got, you know, maybe you're setting X to 1 and Y to 2. But then you've also got other devices connected to the CPU, like a PCIe device. And that PCIe device might want the CPU's attention. So it sends like a signal over to the CPU requesting its attention. The CPU, whatever your program was doing, maybe it was setting X to 1, it's going to stop your program and interrupt it. It's going to give control over to an interrupt handler instead and then that interrupt handler can service the interrupt. 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. 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. At least that's how things normally go. But it's not always that easy. So suppose you were writing like a Linux kernel module and you wanted to be able to talk to hardware. Maybe your code looks like this. Maybe you write out the value 200 to some piece of hardware and then you want to get the hardware's response. But meanwhile, right after you write out that value 200, some other device on the platform interrupts the CPU. The CPU interrupts your program and gives control to an interrupt handler. 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. So interrupt handler's happy. 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. You get the response to the value 100 and your whole program comes falling apart. So it turns out that some things on a CPU just really shouldn't be interrupted. Things like privileged transitions or secure environments or all sorts of other stuff happening in the kernel, you might not want to have interrupted. So the solution to this is interrupt suppression. Basically, keep interrupts pending until you finish the work you need to do and then you can service the interrupts. So that would look something like this if you were writing a Linux kernel module. 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. 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. It turns out that that means difficulties arise. 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. Peterson showed something similar in 2018. 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. Google Project Zero showed something similar in TDX. When TDX was tearing down a secure environment, they could get an interrupt or exception to occur in order to get privilege escalation there. Schluter showed using unexpected interrupts to leak information from confidential VMs. So what we've seen over and over and over is interrupts act as kind of a state disruptor. They mess up the normal flow of execution in a program. 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. There's ways to use software to help you with that. Microcode can help suppress interrupts. Hardware can help you suppress interrupts. 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. But then that raises the question, like, can we break through this? And what would happen if we did? 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. 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. Even high-energy particles from the sun can cause machine check exceptions. What happens is the CPU detects the problem, and it generates this MC exception. MC is going to give control to the 18th interrupt handler in the interrupt descriptor table, which is installed by the OS. And that looks something like this. You've got your CPU running. Something bad happens on that CPU, and the CPU detects a problem. Maybe it's a bit-flipping SRA. Maybe it's a thermal limit violated. The CPU can assert a pin called catastrophic error to let the rest of the platform know things are going really wrong. Then the CPU will generate a machine check exception. It goes to this table in memory called the interrupt descriptor table, which is just a list of pointers that the operating system installed. And one of those pointers, the 18th one, points to a handler that the operating system wants to run to handle machine check exceptions. So we had a hardware failure happen. The machine check was generated by the CPU. Now the operating system has control. So what does the operating system do? Well, if we want to find out, we can dive into Linux kernel source and documentation, and we'll find things like this. It'll tell you that machine checks can be delivered at any time, even in a critical section when all normal interrupts are suppressed. They must be handled immediately. The system is already unstable. They will cause a machine panic. The kernel will always panic on a machine check. It's a bad idea to continue. There's no way out. There's no idea what we were executing. There's no chance to recover. You must die. No need to fumble too much. Just die. So I think we get the gist here. The kernel will determine the source of the error if it can. It'll print a message if it can. But the most important thing from the kernel's perspective is shut down the system before things get worse. So why shut down and not fix the problem? Well, usually with machine checks, you know that something went wrong, but you don't know quite what. You might know that a bit got flipped, but you don't know which bit, so you don't know how to fix it. 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. And there's some caveats to this. On more recent platforms, there's correctable machine checks, and it gets really complex. But traditionally, this is the strategy. Shut down as fast as possible. So all of this makes machine checks entirely unique amongst all the interrupts and exceptions on a platform. 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. So the only way to avoid handling machine checks is to disable them in what's called the CR4 register. 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. So if you're writing some kernel code, your options are basically either handle machine checks immediately or be reset when one is received. So what all this means is that our normal solution to interrupt suppression doesn't work anymore. 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. So all of this gives us a neat opportunity. Maybe we can use this to build some sort of hammer to break through these interrupt defenses. But we've got a challenge. Machine checks are just exceedingly rare. They're supposed to be this sporadic and unpredictable hardware failures. 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. And you'll see hundreds of interrupts and exceptions occurring every single second on the platform. But that highlighted line, machine checks, that'll stay zero. 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. Well, that's no good for us. That's no good for exploitation. We can't just wait 10 years for a machine check to come in. So then we have to wonder, can we generate these on demand? There's some way to simulate machine checks. There's like a tool out there called MCE Inject, but all that does is call the operating system handler. But that doesn't interrupt anything. We need real physical machine checks if we want to use these for security purposes. So I'm diving into that a little bit more. We need to know how machine checks work. So the gist behind machine checks is they've got these registers that track them, that are arranged in what they call banks. And you've got different banks devoted to different sources of machine checks. You'll have a bank for the L2 cache, and a bank for the decode unit, and a bank for the execution unit. And there's just many, many options for these different machine check sources. And you can start dumping all these different banks to see what types of things can cause machine checks. 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. 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. And the list just goes on and on and on and on and on. There are hundreds and hundreds of different sources of these different types of problems on a CPU. And if we want to dive into this, well, it's hard to know where to begin, but we've got to start somewhere. 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. And that seemed interesting because the north bridge seemed a little bit more configurable than some of the other things I could access. 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. So maybe we could start there, and then we can expand on this later. And all these details are going to vary across different generations of CPUs, but it gives us a starting point. 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. 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. That's perfect because that's something we can control. 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. So we can try that, and nothing really happens. We probably get a master abort, but we don't get a machine check here. And that's not too surprising. 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. But still, the data sheets suggest there's some way for a master abort to cause a machine check exception. So maybe we can figure this out by diving into the north bridge a little bit more. 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, but none of these had enough information to decide conclusively how do I get a machine check out of this. 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. And I thought, you know, maybe it's more than one bit that I need to change. 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. But I find any time you're stuck in CPU exploitation, a good strategy is just start smashing things and see what happens. 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, and if it gets a crash or a hang, we can just power cycle the system and try again. 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. We'll look at those MCA banks to see if any machine checks got logged. 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. 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. So that looks something like this. 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, 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. 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. 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. So this was super exciting. 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. But we've got a problem. 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. All that lets us do is interrupt our own code. Basically, we have a hammer now, but we can only hit ourselves with it. So we need to improve this a little bit. We need a way for one core to attack a different core with machine checks. Basically, we need to add a handle to our hammer. So we can modify the fuzzer a bit here and start looking for more complex bit configurations. 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. 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. 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. But we've still got an issue, because the platform's still resetting. So basically at this point, we just need to be a little bit more careful with our hammer. 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. So what we need to do is figure out, you know, why does the system go down? It's not actually because of the machine checks. It's because the operating system decides that's the safest thing to do when it gets a machine check. So to get around that, we can hijack the CPU interrupt descriptor table and install like a first pass machine check handler. So what that looks like is this. So normally the operating system sets up this IDT, which is the table that describes how to handle different interrupt events. And one of those entries tells the CPU how to handle a machine check. What we want to do is replace that interrupt descriptor table with our own, with a hijack IDT. And the hijack IDT is going to get control first whenever there's any interrupt event . 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 . That way we get first dibs on any interrupts or exceptions happening on the platform. 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. That way the operating system won't reset the platform because it can't figure out what caused the machine check to begin with. So that's kind of dangerous. Usually you want the system to reset if you get a machine check, but it's good enough for research and exploitation. So collectively that looks something like this. 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. And then I'm going to use the set PCI command to try to access a non -existent PCI device on the bus. 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. So this is great. This gives us like a state disruptor tool. It gives us a way to do on-demand generation of machine check exceptions entirely from software. 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. But the next question then is, well, what do we want to target with this? So we basically have a great hammer now, we just need to find a nail. 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. I think there's a lot of ways around that. But for now, let's look at something more privileged than the kernel, than ring 0. 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. But it turns out on modern platforms, it actually gets much, much deeper than just the kernel and ring 0. 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. What I settled on for exploitation here was system management mode as a really appealing target. I almost didn't pick system management mode just because it's been done before. But it turns out SMM has some really interesting properties that make it a great case study for machine check exploitation. So if you're not familiar with SMM, it's actually a pretty neat mode of execution on x86 processors. It's been around for like 35 years. It's invisible to the operating system and hypervisor, it can preempt anything that's running on the system. And because it's got all these neat permissions, a lot of people think of more privilege than ring 0 or the kernel. It's often called ring minus 2, negative 2. It's critical to platform security and server reliability. And one of the neatest aspects is it usually has read and write access to a platform's firmware. So you can use it to persist implants down to firmware, which can survive a hard drive wipe or an OS reinstall. But all of that looks something like this. 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. So firmware's going to start partitioning that memory space into some different regions. 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. 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. 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. Microcode's going to open up SM RAM temporarily. 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. So inside of SM RAM, all this code is executing that you never get to see. 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. It restores the CPU state, it closes SM RAM, and it jumps back to the original code. 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. So a demo for system management mode is a little bit anticlimactic. If we want to understand what is inside of this SM RAM, it might be a little unsettling. There's this whole region of memory on our computers we never get to see. We can figure out where that memory is by reading a very specific register from the CPU. 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? The CPU comes back and says, oh, just Fs. Don't worry about it. Nothing to see here. So, you know, it's a little bit concerning when there's stuff on our computers that we have no insight or control over. So it's always nice to try to break into these things. So how could we do that? How can we compromise this ring minus two? There's a couple things to keep in mind here. One is that SMM code is running inside of SM RAM. 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. And that payload then could unlock system management RAM for us. So moving forward with this, there's another key aspect we need to understand. And that's that different CPU modes have to share resources with differently privileged modes. 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. Ideally, what we'd like to do is we just reset all the CPU registers anytime we change modes. That way we don't have to worry about what was in those registers before. But when there's thousands of different registers on the CPU, we don't really have the luxury of resetting them all. 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. And if you do that all correctly, then even from a less privileged mode, you can't really impact a more privileged mode. So in investigating this, I started to look into the AMD documentation on how they implement system management mode. And if you dive deep enough into that documentation, you'll see a chart like the chart on the right. And it shows you exactly which registers get changed when you enter system management mode and which registers stay the same. It'll also tell you that they suppress all the normal interrupts when they enter system management mode, just like we talked about earlier. We don't want to have interrupts unexpectedly firing when we're entering secure modes. But one of the things in this chart caught my attention, and that's this IDTR is marked as unmodified. So why does that matter? 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. 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. 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. 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. So like anything at all that goes wrong in your code leads to immediate privilege escalation for somebody else. 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. And I, I guess that's, that's okay, but it seems like maybe we could be doing something better than that. 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. And I had to get these slides through all sorts of legal and other reviews. So we're going to call this a feature instead, and then we'll talk about how to exploit the feature. 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. We just have to make something, anything go wrong on the CPU. 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. So we've got a problem though, if we want to use machine checks to exploit this issue. 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. So to understand what we can do about that, we need to dive into SMM code a little bit more. So the source of this code is called EDK2. EDK2 is like an open source implementation or reference implementation for EFI firmware. It's basically showing everybody, here's one way to implement the EFI specification. And then vendors can take that EDT, EDK2 code and then adapt it for their platforms as needed. So this is how EDK2 shows you could implement an SMI handler in system management mode. There's a lot of code here, but the parts we're most interested in are right here with this CR4 load. EDK2 is going to enable handling of machine check exceptions. And then over here later on, we see an instruction called LIDT. That's going to reload a new interrupt table inside of system management mode. So collectively that gives us a couple of different attack windows inside of SMM. 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. 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. Your attack window's in between that CR4 reload and the upcoming LIDT instruction. So our machine check attack might look something like this. 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. But we've got yet another hurdle to overcome. The system management mode design basically has all threads entering and exiting system management mode simultaneously. 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. And then each thread gets to finish its current instruction, and then microcode's going to pull those threads into system management mode. And that's called thread quiescing. It basically ensures that all threads are inside of SMM at the same time. And indirectly, that prevents a non-SMM thread from attacking an SMM thread. And it's a pretty common pattern in designing these privileged execution modes. 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. Well, there's an observation here, and it's that threads do not technically all enter system management mode at the same time. 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. So what if we had an attack that looked like this? We begin some sort of magic instruction, and I don't know what this instruction is at this point, on an attacking thread. And then on a victim thread, the victim thread is going to trigger a system management interrupt. On x86, you can do that by writing to port B2. 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. So our victim thread is going to finish its instruction, then get pulled into system management mode. 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. 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. 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. 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, so that you can't attack it anymore. So this is where we're at. 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. 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. 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, and the whole platform is going to reset. So the question then is, does this magic instruction actually exist? 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? Essentially, what we need here is some sort of fuse, a way to delay, start the process, but delay the actual machine check. So how do we build this fuse? Well, we've got a lot of challenges to overcome. One is that we need an instruction that can actually generate a master abort. And master aborts are being done through MMIO on PCI space. The architecture requires that accessing MMIO follows a very specific form. You have to use a specific instruction and specific registers to access MMIO. 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. 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. It's basically fundamentally broken. So I started thinking about this when I thought, like, maybe there's no way forward here. And eventually it occurred to me that, you know, not all MMIO is going to be exactly the same. 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. 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. So what I found is that most things are taking like 700 cycles, but some things are taking like 4,000. And that's starting to get a little bit closer than where we were before. So I started wondering, can we increase this somehow? 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. But then I realized I was doing something stupid. I was following the CPU specs, and when you're trying to exploit something, that's the opposite of what you want to do. So the specs say you have to, if you want to access MMIO configuration space, you have to use instructions of the following form. It has to be a move instruction. It has to be either EAX, AX, or AL. That's a 4-byte, 2-byte, and 1-byte register. And you have to use some memory addressing form to do that. And it says nothing else is allowed. So this is what our instruction would normally look like for accessing MMIO space. 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? Well, if we try that, it turns out it works, and the access time jumps from 4,000 cycles to 8,000 cycles. 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. So I start to think, what else can we violate in the CPU specs? Well, there's another thing it tells us. It says all such accesses are required not to cross any naturally aligned DWORD boundary. And if you do, you'll get undefined behavior. Well, that sounds awesome. Let's find out what undefined behavior looks like in this case. So this is what our instruction looked like. 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. So we've got the pieces that we need here. All we need is a high access time on an existing PCI device, followed by a non-existing device. That non-existing device is what's going to give us the master abort that'll generate the MCE. 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, it doesn't even exist, and it's going to generate that master abort. So we've got all the pieces now. We've got this fuse instruction. 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, then no device is going to claim that request, resulting in a master abort. 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. 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. But we still need that machine check to arrive in a really precise attack window. 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. And what we got was this. 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. 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. We can shift that attack window down in order to get the MCE to arrive at the right time. So with that, we can actually deliver now these cross -core machine checks to victim threads at very, very precise times. So we've got all the pieces now. We've got a fantastic tool available to us. The last thing we need is just a name for our tool, and it's kind of staring us in the face. We built a hammer out of machine checks. We built an MC Hammer. So let's see what this whole tool is going to look like in action, because it gets a little bit convoluted. But you've got your CPU, and you've got some different devices attached to your CPU. And then on the right side, you've got your RAM, some MMIO space, and other stuff. While your CPU is booting, the firmware has control. 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. 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. 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. Meanwhile, we're going to start a victim thread on thread zero and an attacking thread on thread one. 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. 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. Inside of that hijack IDT, our machine check pointer points to a function that we want to run. It's going to be the malicious code that we want to run when a machine check is received. Now both threads are going to start this piece at exactly the same time. 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. 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. The CPU decodes that address, sends it to the north bridge. The north bridge decodes that address and sends it to the PCI bus. 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. And effectively what we've done is we've started a timer on the PCI device that's going to come back later on. 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. 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. 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. Well, the victim thread's going to finish its out instruction very quickly and it moves over to system management mode. Microcode unlocks SMRAM for the victim thread. Only the victim thread can see this. Everybody else still can't see inside of SMRAM. The victim thread's going to start executing the handler that was installed by the, uh, by the firmware. So the victim thread's going to execute, execute, execute, execute, and eventually the victim thread is going to write to that CR4 register. This opens up our attack window. It enables handling of machine checks inside of system management mode. So our attack window has begun, but very shortly the victim thread is going to load a new interrupt descriptor table. That's going to overwrite our hijack table and we won't gain execution if our machine check arrives after that LIDT instruction. 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. 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. Nobody's claiming that. So it's going to create the master abort. And PCIe parlance, that's an unsupported request, gets sent to the north bridge. The north bridge, because it's been poisoned, translates that into a very serious error, a CPU read data error sent to the CPU. The CPU gets that and translates that as a machine check exception. 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. Now thread zero needs to figure out how do I handle a machine check exception? Well, it looks up a handler inside of the interrupt descriptor table, the IDT that we poisoned before the whole process began. It follows the 18th entry in the IDT and begins executing the code that that points to. Meanwhile, eventually the attacking thread is going to finish up its fuse instruction, microcode sucks it into system management mode as well. We have no more control of the attacking thread here. It's just going to execute its handler like normal. 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. So a full demo of this looks a little bit something like this. We fire up the MC hammer tool and it's going to replace all the interrupt descriptor tables on each core. It's going to load up some MC kernel modules. It's going to do a bunch of timing on different PCI devices to find that fuse instruction that it needs . It'll poison the north bridge so that small errors become really big errors. And when we're ready, we'll go ahead and launch a system management interrupt and that fuse instruction at exactly the same time. And if we time them right, we gain code execution inside of system management mode. 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. And that gives us a way to talk to system management mode from the kernel. 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. So that's the, uh, the memory that dumped on the bottom here. 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. 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. So we can see that system management mode on the system has access to the platform flash. We can see it's got all sorts of USB drivers built into it for reasons I don't really understand. 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. Um, and we'll see the lock box is a way of storing secrets inside of system management mode. We'll see the S3 save state gives you access to all sorts of registers on the CPU you can't normally touch. 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. So it's, it's absolutely astounding how many different capabilities have been tossed into system management mode. And once you gain code execution, you get access to all of those things that we could never touch before. So where does that leave us as far as mitigations? 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. Um, on the other hand, the machine check exploit that I developed, I developed this on pre-zen CPUs. So at least one of these things we can fix ourselves. So to mitigate the machine check path, um, we can change the way EDK2 works. EDK2 is who's providing, uh, some of the code that ends up on our, on our firmware for our, our different platforms. So even though that EDK2 SMM code is correct, it does assume that the IDT was being made safe by microcode. 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. 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. 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. And I think there's still a lot to explore there. But back to the machine check piece. I think this is a really exciting piece moving forward, uh, more so than the SMM piece. 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. And I think there's a whole bunch of interesting ways that we can take this research. So one is we could start looking at other, uh, sources of machine check exceptions. Remember, there are hundreds of different ways to cause these different issues. I looked at one of them coming from the North Bridge. Uh, we could also look at ECC injection, different processor or rata are documented to cause machine check exceptions. We could look at doing, like, row hammer style bit flips in DRAM to cause machine check exceptions. 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. We could look at other MMIO devices to cause asynchronous MCEs or how to do this with no MMIO devices. The holy grail here would be finding a way to generate machine checks from user land from, from ring three. Uh, if you, uh, you could, you could, you could look at, like, ring three or rata that are related to machine checks. You could start looking at accessing faulty devices from user land to see if you can generate machine checks that way. 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, all those become viable, again, if you can use machine checks to break through those defenses. We could start looking at other exploit targets like hypervisors and secure guests and enclaves and secure loaders. 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. 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. 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. So I wanted to give a couple of, uh, resources here. 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. I wanted to give a big thanks to the DazzleCat team, namely Stephanie, who always gives me ideas on how to move forward. And there is a huge slew of fantastic research papers on how to use interrupts in unexpected ways for privilege escalation. And I only got to talk about a couple of those early on in the presentation. 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. But, um, I've got a lot, lot more to share, uh, with this research and there'll be a lot more to come. You can find me on most platforms under XOR, EAX, EAX, EAX. Um, right now I've shared some bare bones stuff on the MC Hammer GitHub repo. And I'll start pushing more and more stuff there as this issue is better mitigated. Um, but definitely check that out because, um, like I said, it's just the tip of the iceberg. So thanks everyone.